extern int adjusted_temp;
extern int adjusted_press;
+extern u32 bmp_counter;
extern volatile byte *bmp_i2c_ptr;
extern volatile byte bmp_i2c_len;
PCD_HandleTypeDef *hpcd;
byte state; // USB_STATE_xxx
byte pre_suspend_state;
- byte address;
- byte config;
- byte remote_wakeup;
+ byte address; // Device address assigned by the host (0=none)
+ byte config; // Selected configuration (0=none)
+ byte remote_wakeup; // State of remote wakeup feature
+
+ // State of endpoint 00
byte ep0_state; // USB_EP0_xxx
u16 ep0_setup_data_length;
u16 ep0_remaining_length;
u16 ep0_total_length;
byte ep0_buf[USB_EP0_BUF_SIZE];
+
// Descriptor data to be filled by the user during usb_dev_reset()
const byte *desc_device;
const byte *desc_config;
u16 desc_config_len;
u16 desc_string_items;
u16 desc_languages_len;
+
+ // Internal use
+ u16 last_pma_alloc;
};
enum usb_device_state {
// Wrappers return HAL_OK / HAL_ERROR / HAL_BUSY / HAL_TIMEOUT
+// Call from configure callback
static inline HAL_StatusTypeDef usb_ep_open(struct usb *usb, byte ep_addr, byte ep_type, byte ep_max_size)
{
+ HAL_PCDEx_PMAConfig(usb->hpcd, ep_addr, PCD_SNG_BUF, usb->last_pma_alloc);
+ usb->last_pma_alloc += (ep_max_size + 7) & ~7U;
return HAL_PCD_EP_Open(usb->hpcd, ep_addr, ep_max_size, ep_type);
}
+// Call from unconfigure callback
static inline HAL_StatusTypeDef usb_ep_close(struct usb *usb, byte ep_addr)
{
return HAL_PCD_EP_Close(usb->hpcd, ep_addr);
static u32 raw_press;
int adjusted_temp;
int adjusted_press;
+u32 bmp_counter;
void bmp_step(void)
{
bmp_debug("BMP: Raw pressure: %06x\n", raw_press);
bmp_recalc(raw_temp, raw_press, BMP_OSS, bmp_constants, &adjusted_temp, &adjusted_press);
bmp_debug("BMP: Adjusted temp %d, press %d\n", adjusted_temp, adjusted_press);
+ bmp_counter++;
bmp_state = BMP_IDLE;
break;
}
tx_packet_state = 1;
put_u32_be(tx_packet, adjusted_temp);
put_u32_be(tx_packet + 4, adjusted_press);
- usb_ep_send(&usb, 0x82, tx_packet, 8);
+ put_u32_be(tx_packet + 8, bmp_counter);
+ usb_ep_send(&usb, 0x82, tx_packet, 12);
rx_packet_state = 0;
usb_ep_receive(&usb, 0x01, rx_packet, 64);
}
#include <string.h>
+// Layout of packet memory
+#define PMA_EP00 0x18
+#define PMA_EP80 0x58
+#define PMA_USER 0x98
+
void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd)
{
memset(usb, 0, sizeof(*usb));
void usb_start(struct usb *usb)
{
- HAL_PCDEx_PMAConfig(usb->hpcd, 0x00, PCD_SNG_BUF, 0x18);
- HAL_PCDEx_PMAConfig(usb->hpcd, 0x80, PCD_SNG_BUF, 0x58);
+ HAL_PCDEx_PMAConfig(usb->hpcd, 0x00, PCD_SNG_BUF, PMA_EP00);
+ HAL_PCDEx_PMAConfig(usb->hpcd, 0x80, PCD_SNG_BUF, PMA_EP80);
HAL_PCD_Start(usb->hpcd);
}
{
usb->config = cfg;
usb->state = USB_STATE_CONFIGURED;
+ usb->last_pma_alloc = PMA_USER;
usb_dev_configure(usb);
}
usb_ctl_send_status(usb);
{
usb_dev_unconfigure(usb);
usb->config = cfg;
+ usb->last_pma_alloc = PMA_USER;
usb_dev_configure(usb);
}
usb_ctl_send_status(usb);
if (err = libusb_bulk_transfer(devh, 0x82, resp, 64, &received, 2000))
die("Receive failed: error %d\n", err);
// printf("Received %d bytes\n", received);
- if (received >= 8)
+ if (received >= 12)
{
int t = get_u32_be(resp);
int p = get_u32_be(resp + 4);
- msg(L_INFO, "Temperature %d ddegC, pressure %d Pa", t, p);
+ uint cnt = get_u32_be(resp + 8);
+ msg(L_INFO, "Temperature %d ddegC, pressure %d Pa, cnt %u", t, p, cnt);
}
sleep(1);