stm32/usbdev: Simplify CDC tx/rx buffer passing.

This commit is contained in:
Damien George 2017-09-06 22:57:20 +10:00
parent 77e1da40e2
commit e04b478050
3 changed files with 21 additions and 42 deletions

View File

@ -56,7 +56,7 @@
#define CDC_SET_CONTROL_LINE_STATE 0x22
#define CDC_SEND_BREAK 0x23
void usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) {
uint8_t *usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) {
cdc->usb = pdev;
cdc->rx_buf_put = 0;
cdc->rx_buf_get = 0;
@ -66,8 +66,9 @@ void usbd_cdc_init(usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev) {
cdc->tx_buf_ptr_wait_count = 0;
cdc->tx_need_empty_packet = 0;
cdc->dev_is_connected = 0;
USBD_CDC_SetTxBuffer(pdev, cdc->tx_buf, 0);
USBD_CDC_SetRxBuffer(pdev, cdc->rx_packet_buf);
// Return the buffer to place the first USB OUT packet
return cdc->rx_packet_buf;
}
// Manage the CDC class requests
@ -180,9 +181,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
buffptr = cdc->tx_buf_ptr_out_shadow;
USBD_CDC_SetTxBuffer(hpcd->pData, (uint8_t*)&cdc->tx_buf[buffptr], buffsize);
if (USBD_CDC_TransmitPacket(hpcd->pData) == USBD_OK) {
if (USBD_CDC_TransmitPacket(hpcd->pData, buffsize, &cdc->tx_buf[buffptr]) == USBD_OK) {
cdc->tx_buf_ptr_out_shadow += buffsize;
if (cdc->tx_buf_ptr_out_shadow == USBD_CDC_TX_DATA_SIZE) {
cdc->tx_buf_ptr_out_shadow = 0;
@ -201,13 +200,11 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) {
}
// 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_CDC_ReceivePacket
// Returns USBD_OK if all operations are OK else USBD_FAIL
// The buffer we are passed here is just cdc_rx_packet_buf, so we are free to modify it.
int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len) {
int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, size_t len) {
// copy the incoming data into the circular buffer
for (uint8_t *src = Buf, *top = Buf + *Len; src < top; ++src) {
for (const uint8_t *src = cdc->rx_packet_buf, *top = cdc->rx_packet_buf + len; src < top; ++src) {
if (mp_interrupt_char != -1 && *src == mp_interrupt_char) {
pendsv_kbd_intr();
} else {
@ -222,8 +219,7 @@ int8_t usbd_cdc_receive(usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len) {
}
// initiate next USB packet transfer
USBD_CDC_SetRxBuffer(cdc->usb, cdc->rx_packet_buf);
USBD_CDC_ReceivePacket(cdc->usb);
USBD_CDC_ReceivePacket(cdc->usb, cdc->rx_packet_buf);
return USBD_OK;
}

View File

@ -31,10 +31,6 @@ typedef struct {
uint32_t data[CDC_DATA_FS_MAX_PACKET_SIZE/4]; /* Force 32bits alignment */
uint8_t CmdOpCode;
uint8_t CmdLength;
uint8_t *RxBuffer;
uint8_t *TxBuffer;
uint32_t RxLength;
uint32_t TxLength;
__IO uint32_t TxState;
__IO uint32_t RxState;
@ -97,10 +93,8 @@ int USBD_SelectMode(uint32_t mode, USBD_HID_ModeInfoTypeDef *hid_info);
// returns the current usb mode
uint8_t USBD_GetMode();
uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length);
uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev, uint8_t *pbuff);
uint8_t USBD_CDC_ReceivePacket (USBD_HandleTypeDef *pdev);
uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef *pdev);
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf);
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf);
uint8_t USBD_MSC_RegisterStorage(USBD_HandleTypeDef *pdev, USBD_StorageTypeDef *fops);
@ -113,9 +107,9 @@ uint8_t USBD_HID_ClearNAK(USBD_HandleTypeDef *pdev);
// These are provided externally to implement the CDC interface
struct _usbd_cdc_itf_t;
void usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev);
uint8_t *usbd_cdc_init(struct _usbd_cdc_itf_t *cdc, USBD_HandleTypeDef *pdev);
int8_t usbd_cdc_control(struct _usbd_cdc_itf_t *cdc, uint8_t cmd, uint8_t* pbuf, uint16_t length);
int8_t usbd_cdc_receive(struct _usbd_cdc_itf_t *cdc, uint8_t* Buf, uint32_t *Len);
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;

View File

@ -669,14 +669,14 @@ static uint8_t USBD_CDC_MSC_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) {
CDC_CMD_PACKET_SIZE);
// Init physical Interface components
usbd_cdc_init(state->cdc, pdev);
uint8_t *buf = usbd_cdc_init(state->cdc, pdev);
// Init Xfer states
CDC_ClassData.TxState =0;
CDC_ClassData.RxState =0;
// Prepare Out endpoint to receive next packet
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, CDC_ClassData.RxBuffer, CDC_DATA_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE);
}
if (usbd_mode & USBD_MODE_MSC) {
@ -962,11 +962,11 @@ static uint8_t USBD_CDC_MSC_HID_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
usbd_cdc_msc_hid_state_t *state = pdev->pClassData;
if ((usbd_mode & USBD_MODE_CDC) && epnum == (CDC_OUT_EP & 0x7f)) {
/* Get the received data length */
CDC_ClassData.RxLength = USBD_LL_GetRxDataSize (pdev, epnum);
size_t len = USBD_LL_GetRxDataSize (pdev, epnum);
/* USB data will be immediately processed, this allow next USB traffic being
NAKed till the end of the application Xfer */
usbd_cdc_receive(state->cdc, CDC_ClassData.RxBuffer, &CDC_ClassData.RxLength);
usbd_cdc_receive(state->cdc, len);
return USBD_OK;
} else if ((usbd_mode & USBD_MODE_MSC) && epnum == (MSC_OUT_EP & 0x7f)) {
@ -995,22 +995,11 @@ uint8_t *USBD_CDC_MSC_HID_GetDeviceQualifierDescriptor (uint16_t *length) {
return NULL;
}
uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint16_t length) {
CDC_ClassData.TxBuffer = pbuff;
CDC_ClassData.TxLength = length;
return USBD_OK;
}
uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) {
CDC_ClassData.RxBuffer = pbuff;
return USBD_OK;
}
// data received on non-control OUT endpoint
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) {
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, size_t len, const uint8_t *buf) {
if (CDC_ClassData.TxState == 0) {
// transmit next packet
USBD_LL_Transmit(pdev, CDC_IN_EP, CDC_ClassData.TxBuffer, CDC_ClassData.TxLength);
USBD_LL_Transmit(pdev, CDC_IN_EP, (uint8_t*)buf, len);
// Tx transfer in progress
CDC_ClassData.TxState = 1;
@ -1021,14 +1010,14 @@ uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) {
}
// prepare OUT endpoint for reception
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) {
uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev, uint8_t *buf) {
// Suspend or Resume USB Out process
if (pdev->dev_speed == USBD_SPEED_HIGH) {
return USBD_FAIL;
}
// Prepare Out endpoint to receive next packet
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, CDC_ClassData.RxBuffer, CDC_DATA_OUT_PACKET_SIZE);
USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, buf, CDC_DATA_OUT_PACKET_SIZE);
return USBD_OK;
}