From b3b922f177ee7553b2d06edf7bf3df6c2076b03a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 6 Sep 2017 23:03:01 +1000 Subject: [PATCH] stm32/usbdev: Simplify HID tx/rx buffer passing. --- ports/stm32/usbd_hid_interface.c | 15 +++++++-------- .../stm32/usbdev/class/inc/usbd_cdc_msc_hid.h | 7 +++---- .../stm32/usbdev/class/src/usbd_cdc_msc_hid.c | 19 ++++++------------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index badf0144bf..f14bcf52c1 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -42,25 +42,24 @@ #include "irq.h" #include "usb.h" -void usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { +uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) { hid->usb = pdev; hid->current_read_buffer = 0; hid->last_read_len = 0; hid->current_write_buffer = 0; - USBD_HID_SetRxBuffer(pdev, hid->buffer[hid->current_write_buffer]); + + // Return the buffer to place the first USB OUT packet + return hid->buffer[hid->current_write_buffer]; } // Data received over USB OUT endpoint is processed here. -// buf: Buffer of data received -// len: Number of data received (in bytes) +// len: number of bytes received into the buffer we passed to USBD_HID_ReceivePacket // Returns USBD_OK if all operations are OK else USBD_FAIL -// The buffer we are passed here is just hid->buffer, so we are free to modify it. -int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len, uint8_t *buf) { +int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len) { hid->current_write_buffer = !hid->current_write_buffer; hid->last_read_len = len; // initiate next USB packet transfer, to append to existing data in buffer - USBD_HID_SetRxBuffer(hid->usb, hid->buffer[hid->current_write_buffer]); - USBD_HID_ReceivePacket(hid->usb); + USBD_HID_ReceivePacket(hid->usb, hid->buffer[hid->current_write_buffer]); // Set NAK to indicate we need to process read buffer USBD_HID_SetNAK(hid->usb); return USBD_OK; diff --git a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h index a7f8d0a9b8..2fb19bce94 100644 --- a/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h +++ b/ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h @@ -98,8 +98,7 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops); -uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev); +uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf); int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev); uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len); uint8_t USBD_HID_SetNAK(USBD_HandleTypeDef *pdev); @@ -113,7 +112,7 @@ int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, size_t len); // These are provided externally to implement the HID interface struct _usbd_hid_itf_t; -void usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); -int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len, uint8_t *buf); +uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev); +int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len); #endif // _USB_CDC_MSC_CORE_H_ diff --git a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c index f7c0824636..41cd2a8cba 100644 --- a/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c +++ b/ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c @@ -82,8 +82,6 @@ typedef struct { uint32_t IdleState; uint32_t AltSetting; HID_StateTypeDef state; - uint8_t *RxBuffer; - uint32_t RxLength; } USBD_HID_HandleTypeDef; static uint8_t usbd_mode; @@ -724,10 +722,10 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) { USBD_EP_TYPE_INTR, mps_out); - usbd_hid_init(state->hid, pdev); + uint8_t *buf = usbd_hid_init(state->hid, pdev); // Prepare Out endpoint to receive next packet - USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out); + USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); HID_ClassData.state = HID_IDLE; } @@ -973,8 +971,8 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) MSC_BOT_DataOut(pdev, epnum); return USBD_OK; } else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_out_ep & 0x7f)) { - HID_ClassData.RxLength = USBD_LL_GetRxDataSize(pdev, epnum); - usbd_hid_receive(state->hid, HID_ClassData.RxLength, HID_ClassData.RxBuffer); + size_t len = USBD_LL_GetRxDataSize(pdev, epnum); + usbd_hid_receive(state->hid, len); } return USBD_OK; @@ -1032,13 +1030,8 @@ uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef * } } -uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) { - HID_ClassData.RxBuffer = pbuff; - return USBD_OK; -} - // prepare OUT endpoint for reception -uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) { +uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) { // Suspend or Resume USB Out process if (pdev->dev_speed == USBD_SPEED_HIGH) { return USBD_FAIL; @@ -1048,7 +1041,7 @@ uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) { uint16_t mps_out = hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO] | (hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8); - USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out); + USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out); return USBD_OK; }