Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3b922f

Browse files
committed
stm32/usbdev: Simplify HID tx/rx buffer passing.
1 parent e04b478 commit b3b922f

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

ports/stm32/usbd_hid_interface.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,24 @@
4242
#include "irq.h"
4343
#include "usb.h"
4444

45-
void usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) {
45+
uint8_t *usbd_hid_init(usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev) {
4646
hid->usb = pdev;
4747
hid->current_read_buffer = 0;
4848
hid->last_read_len = 0;
4949
hid->current_write_buffer = 0;
50-
USBD_HID_SetRxBuffer(pdev, hid->buffer[hid->current_write_buffer]);
50+
51+
// Return the buffer to place the first USB OUT packet
52+
return hid->buffer[hid->current_write_buffer];
5153
}
5254

5355
// Data received over USB OUT endpoint is processed here.
54-
// buf: Buffer of data received
55-
// len: Number of data received (in bytes)
56+
// len: number of bytes received into the buffer we passed to USBD_HID_ReceivePacket
5657
// Returns USBD_OK if all operations are OK else USBD_FAIL
57-
// The buffer we are passed here is just hid->buffer, so we are free to modify it.
58-
int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len, uint8_t *buf) {
58+
int8_t usbd_hid_receive(usbd_hid_itf_t *hid, size_t len) {
5959
hid->current_write_buffer = !hid->current_write_buffer;
6060
hid->last_read_len = len;
6161
// initiate next USB packet transfer, to append to existing data in buffer
62-
USBD_HID_SetRxBuffer(hid->usb, hid->buffer[hid->current_write_buffer]);
63-
USBD_HID_ReceivePacket(hid->usb);
62+
USBD_HID_ReceivePacket(hid->usb, hid->buffer[hid->current_write_buffer]);
6463
// Set NAK to indicate we need to process read buffer
6564
USBD_HID_SetNAK(hid->usb);
6665
return USBD_OK;

ports/stm32/usbdev/class/inc/usbd_cdc_msc_hid.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint
9898

9999
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops);
100100

101-
uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff);
102-
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev);
101+
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf);
103102
int USBD_HID_CanSendReport(USBD_HandleTypeDef *pdev);
104103
uint8_t USBD_HID_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, uint16_t len);
105104
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);
113112

114113
// These are provided externally to implement the HID interface
115114
struct _usbd_hid_itf_t;
116-
void usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev);
117-
int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len, uint8_t *buf);
115+
uint8_t *usbd_hid_init(struct _usbd_hid_itf_t *hid, USBD_HandleTypeDef *pdev);
116+
int8_t usbd_hid_receive(struct _usbd_hid_itf_t *hid, size_t len);
118117

119118
#endif // _USB_CDC_MSC_CORE_H_

ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ typedef struct {
8282
uint32_t IdleState;
8383
uint32_t AltSetting;
8484
HID_StateTypeDef state;
85-
uint8_t *RxBuffer;
86-
uint32_t RxLength;
8785
} USBD_HID_HandleTypeDef;
8886

8987
static uint8_t usbd_mode;
@@ -724,10 +722,10 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) {
724722
USBD_EP_TYPE_INTR,
725723
mps_out);
726724

727-
usbd_hid_init(state->hid, pdev);
725+
uint8_t *buf = usbd_hid_init(state->hid, pdev);
728726

729727
// Prepare Out endpoint to receive next packet
730-
USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out);
728+
USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out);
731729

732730
HID_ClassData.state = HID_IDLE;
733731
}
@@ -973,8 +971,8 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
973971
MSC_BOT_DataOut(pdev, epnum);
974972
return USBD_OK;
975973
} else if ((usbd_mode & USBD_MODE_HID) && epnum == (hid_out_ep & 0x7f)) {
976-
HID_ClassData.RxLength = USBD_LL_GetRxDataSize(pdev, epnum);
977-
usbd_hid_receive(state->hid, HID_ClassData.RxLength, HID_ClassData.RxBuffer);
974+
size_t len = USBD_LL_GetRxDataSize(pdev, epnum);
975+
usbd_hid_receive(state->hid, len);
978976
}
979977

980978
return USBD_OK;
@@ -1032,13 +1030,8 @@ uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *
10321030
}
10331031
}
10341032

1035-
uint8_t USBD_HID_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) {
1036-
HID_ClassData.RxBuffer = pbuff;
1037-
return USBD_OK;
1038-
}
1039-
10401033
// prepare OUT endpoint for reception
1041-
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) {
1034+
uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) {
10421035
// Suspend or Resume USB Out process
10431036
if (pdev->dev_speed == USBD_SPEED_HIGH) {
10441037
return USBD_FAIL;
@@ -1048,7 +1041,7 @@ uint8_t USBD_HID_ReceivePacket(USBD_HandleTypeDef *pdev) {
10481041
uint16_t mps_out =
10491042
hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_LO]
10501043
| (hid_desc[HID_DESC_OFFSET_MAX_PACKET_OUT_HI] << 8);
1051-
USBD_LL_PrepareReceive(pdev, hid_out_ep, HID_ClassData.RxBuffer, mps_out);
1044+
USBD_LL_PrepareReceive(pdev, hid_out_ep, buf, mps_out);
10521045

10531046
return USBD_OK;
10541047
}

0 commit comments

Comments
 (0)