From 6cd149de66be04d568bbb6f4929be0d11de1486a Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 30 Jun 2018 23:40:07 +0200 Subject: [PATCH] USB: Do not forget to allocate PMA --- usb/Inc/app.h | 1 + usb/Inc/usb.h | 16 +++++++++++++--- usb/Src/bmp085.c | 2 ++ usb/Src/main.c | 3 ++- usb/Src/usb.c | 11 +++++++++-- usb/host/test.c | 5 +++-- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/usb/Inc/app.h b/usb/Inc/app.h index 18e58d1..d0061dd 100644 --- a/usb/Inc/app.h +++ b/usb/Inc/app.h @@ -18,6 +18,7 @@ void tx_packet_send(void); extern int adjusted_temp; extern int adjusted_press; +extern u32 bmp_counter; extern volatile byte *bmp_i2c_ptr; extern volatile byte bmp_i2c_len; diff --git a/usb/Inc/usb.h b/usb/Inc/usb.h index 2aea056..79b892c 100644 --- a/usb/Inc/usb.h +++ b/usb/Inc/usb.h @@ -21,14 +21,17 @@ struct usb { 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; @@ -38,6 +41,9 @@ struct usb { u16 desc_config_len; u16 desc_string_items; u16 desc_languages_len; + + // Internal use + u16 last_pma_alloc; }; enum usb_device_state { @@ -181,11 +187,15 @@ enum usb_ep_type { // 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); diff --git a/usb/Src/bmp085.c b/usb/Src/bmp085.c index 9b241a6..c5712c9 100644 --- a/usb/Src/bmp085.c +++ b/usb/Src/bmp085.c @@ -166,6 +166,7 @@ static u16 raw_temp; static u32 raw_press; int adjusted_temp; int adjusted_press; +u32 bmp_counter; void bmp_step(void) { @@ -201,6 +202,7 @@ 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; } diff --git a/usb/Src/main.c b/usb/Src/main.c index 0ff373e..e8d8abb 100644 --- a/usb/Src/main.c +++ b/usb/Src/main.c @@ -157,7 +157,8 @@ int main(void) 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); } diff --git a/usb/Src/usb.c b/usb/Src/usb.c index ffac01d..f7b92a6 100644 --- a/usb/Src/usb.c +++ b/usb/Src/usb.c @@ -6,6 +6,11 @@ #include +// 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)); @@ -17,8 +22,8 @@ void usb_init(struct usb *usb, PCD_HandleTypeDef *hpcd) 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); } @@ -223,6 +228,7 @@ static void dev_set_configuration(struct usb *usb, struct setup_request *setup) { usb->config = cfg; usb->state = USB_STATE_CONFIGURED; + usb->last_pma_alloc = PMA_USER; usb_dev_configure(usb); } usb_ctl_send_status(usb); @@ -239,6 +245,7 @@ static void dev_set_configuration(struct usb *usb, struct setup_request *setup) { usb_dev_unconfigure(usb); usb->config = cfg; + usb->last_pma_alloc = PMA_USER; usb_dev_configure(usb); } usb_ctl_send_status(usb); diff --git a/usb/host/test.c b/usb/host/test.c index 6533c10..2d94a98 100644 --- a/usb/host/test.c +++ b/usb/host/test.c @@ -87,11 +87,12 @@ int main(void) 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); -- 2.39.2