From 3b32b3d1b31f957d2ed57d69bc0c503bbdf2a4cf Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 5 Aug 2021 12:21:58 +1000 Subject: [PATCH] stm32/usbd_cdc_interface: Rename USBD_CDC_xx opts to MICROPY_HW_USB_xx. Signed-off-by: Damien George --- .../boards/B_L072Z_LRWAN1/mpconfigboard.h | 4 ++-- .../stm32/boards/NUCLEO_L432KC/mpconfigboard.h | 4 ++-- ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h | 4 ++-- .../boards/USBDONGLE_WB55/mpconfigboard.h | 4 ++-- ports/stm32/mpconfigboard_common.h | 16 +++++++++++++++- ports/stm32/usbd_cdc_interface.c | 18 +++++++++--------- ports/stm32/usbd_cdc_interface.h | 15 +++++---------- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h index 0cded1bc86..08ded0e03e 100644 --- a/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h +++ b/ports/stm32/boards/B_L072Z_LRWAN1/mpconfigboard.h @@ -67,5 +67,5 @@ #define MICROPY_HW_USB_FS (1) #define MICROPY_HW_USB_MSC (0) #define MICROPY_HW_USB_HID (0) -#define USBD_CDC_RX_DATA_SIZE (256) -#define USBD_CDC_TX_DATA_SIZE (256) +#define MICROPY_HW_USB_CDC_RX_DATA_SIZE (256) +#define MICROPY_HW_USB_CDC_TX_DATA_SIZE (256) diff --git a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h index 0f1134491f..8b3a8f38b1 100644 --- a/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_L432KC/mpconfigboard.h @@ -67,5 +67,5 @@ #define MICROPY_HW_USB_FS (MICROPY_HW_ENABLE_USB) #define MICROPY_HW_USB_MSC (0) #define MICROPY_HW_USB_HID (0) -#define USBD_CDC_RX_DATA_SIZE (256) -#define USBD_CDC_TX_DATA_SIZE (256) +#define MICROPY_HW_USB_CDC_RX_DATA_SIZE (256) +#define MICROPY_HW_USB_CDC_TX_DATA_SIZE (256) diff --git a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h index 179369d943..a7473b9d6a 100644 --- a/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h +++ b/ports/stm32/boards/NUCLEO_WB55/mpconfigboard.h @@ -59,8 +59,8 @@ // USB config #define MICROPY_HW_USB_FS (1) -#define USBD_CDC_RX_DATA_SIZE (512) -#define USBD_CDC_TX_DATA_SIZE (512) +#define MICROPY_HW_USB_CDC_RX_DATA_SIZE (512) +#define MICROPY_HW_USB_CDC_TX_DATA_SIZE (512) // Bluetooth config #define MICROPY_HW_BLE_UART_ID (0) diff --git a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h index fdb061900b..d4b12ae5bf 100644 --- a/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h +++ b/ports/stm32/boards/USBDONGLE_WB55/mpconfigboard.h @@ -43,8 +43,8 @@ // USB config #define MICROPY_HW_USB_FS (1) -#define USBD_CDC_RX_DATA_SIZE (512) -#define USBD_CDC_TX_DATA_SIZE (512) +#define MICROPY_HW_USB_CDC_RX_DATA_SIZE (512) +#define MICROPY_HW_USB_CDC_TX_DATA_SIZE (512) // Bluetooth config #define MICROPY_HW_BLE_UART_ID (0) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 24c5e1a3cd..0cf6adce33 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -189,7 +189,9 @@ || defined(USBD_CONFIGURATION_HS_STRING) \ || defined(USBD_INTERFACE_HS_STRING) \ || defined(USBD_CONFIGURATION_FS_STRING) \ - || defined(USBD_INTERFACE_FS_STRING) + || defined(USBD_INTERFACE_FS_STRING) \ + || defined(USBD_CDC_RX_DATA_SIZE) \ + || defined(USBD_CDC_TX_DATA_SIZE) #error "Old USBD_xxx configuration option used, renamed to MICROPY_HW_USB_xxx" #endif @@ -244,6 +246,18 @@ #define MICROPY_HW_USB_INTERFACE_FS_STRING "Pyboard Interface" #endif +// Amount of incoming buffer space for each CDC instance. +// This must be 2 or greater, and a power of 2. +#ifndef MICROPY_HW_USB_CDC_RX_DATA_SIZE +#define MICROPY_HW_USB_CDC_RX_DATA_SIZE (1024) +#endif + +// Amount of outgoing buffer space for each CDC instance. +// This must be a power of 2 and no greater than 16384. +#ifndef MICROPY_HW_USB_CDC_TX_DATA_SIZE +#define MICROPY_HW_USB_CDC_TX_DATA_SIZE (1024) +#endif + /*****************************************************************************/ // General configuration diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 5f27bbe29a..61a7c82486 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -172,7 +172,7 @@ int8_t usbd_cdc_control(usbd_cdc_state_t *cdc_in, uint8_t cmd, uint8_t *pbuf, ui } static inline uint16_t usbd_cdc_tx_buffer_mask(uint16_t val) { - return val & (USBD_CDC_TX_DATA_SIZE - 1); + return val & (MICROPY_HW_USB_CDC_TX_DATA_SIZE - 1); } static inline uint16_t usbd_cdc_tx_buffer_size(usbd_cdc_itf_t *cdc) { @@ -188,18 +188,18 @@ static inline bool usbd_cdc_tx_buffer_will_be_empty(usbd_cdc_itf_t *cdc) { } static inline bool usbd_cdc_tx_buffer_full(usbd_cdc_itf_t *cdc) { - return usbd_cdc_tx_buffer_size(cdc) == USBD_CDC_TX_DATA_SIZE; + return usbd_cdc_tx_buffer_size(cdc) == MICROPY_HW_USB_CDC_TX_DATA_SIZE; } static uint16_t usbd_cdc_tx_send_length(usbd_cdc_itf_t *cdc) { - uint16_t to_end = USBD_CDC_TX_DATA_SIZE - usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_out); + uint16_t to_end = MICROPY_HW_USB_CDC_TX_DATA_SIZE - usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_out); return MIN(usbd_cdc_tx_buffer_size(cdc), to_end); } static void usbd_cdc_tx_buffer_put(usbd_cdc_itf_t *cdc, uint8_t data, bool check_overflow) { cdc->tx_buf[usbd_cdc_tx_buffer_mask(cdc->tx_buf_ptr_in)] = data; cdc->tx_buf_ptr_in++; - if (check_overflow && usbd_cdc_tx_buffer_size(cdc) > USBD_CDC_TX_DATA_SIZE) { + if (check_overflow && usbd_cdc_tx_buffer_size(cdc) > MICROPY_HW_USB_CDC_TX_DATA_SIZE) { cdc->tx_buf_ptr_out++; cdc->tx_buf_ptr_out_next = cdc->tx_buf_ptr_out; } @@ -270,7 +270,7 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { bool usbd_cdc_rx_buffer_full(usbd_cdc_itf_t *cdc) { int get = cdc->rx_buf_get, put = cdc->rx_buf_put; - int remaining = (get - put) + (-((int)(get <= put)) & USBD_CDC_RX_DATA_SIZE); + int remaining = (get - put) + (-((int)(get <= put)) & MICROPY_HW_USB_CDC_RX_DATA_SIZE); return remaining < CDC_DATA_MAX_PACKET_SIZE + 1; } @@ -298,7 +298,7 @@ int8_t usbd_cdc_receive(usbd_cdc_state_t *cdc_in, size_t len) { if (cdc->attached_to_repl && *src == mp_interrupt_char) { pendsv_kbd_intr(); } else { - uint16_t next_put = (cdc->rx_buf_put + 1) & (USBD_CDC_RX_DATA_SIZE - 1); + uint16_t next_put = (cdc->rx_buf_put + 1) & (MICROPY_HW_USB_CDC_RX_DATA_SIZE - 1); if (next_put == cdc->rx_buf_get) { // overflow, we just discard the rest of the chars break; @@ -322,7 +322,7 @@ int8_t usbd_cdc_receive(usbd_cdc_state_t *cdc_in, size_t len) { int usbd_cdc_tx_half_empty(usbd_cdc_itf_t *cdc) { int32_t tx_waiting = usbd_cdc_tx_buffer_size(cdc); - return tx_waiting <= USBD_CDC_TX_DATA_SIZE / 2; + return tx_waiting <= MICROPY_HW_USB_CDC_TX_DATA_SIZE / 2; } // Writes only the data that fits if flow & CTS, else writes all data @@ -405,7 +405,7 @@ void usbd_cdc_tx_always(usbd_cdc_itf_t *cdc, const uint8_t *buf, uint32_t len) { int usbd_cdc_rx_num(usbd_cdc_itf_t *cdc) { int32_t rx_waiting = (int32_t)cdc->rx_buf_put - (int32_t)cdc->rx_buf_get; if (rx_waiting < 0) { - rx_waiting += USBD_CDC_RX_DATA_SIZE; + rx_waiting += MICROPY_HW_USB_CDC_RX_DATA_SIZE; } usbd_cdc_rx_check_resume(cdc); return rx_waiting; @@ -434,7 +434,7 @@ int usbd_cdc_rx(usbd_cdc_itf_t *cdc, uint8_t *buf, uint32_t len, uint32_t timeou // Copy byte from device to user buffer buf[i] = cdc->rx_user_buf[cdc->rx_buf_get]; - cdc->rx_buf_get = (cdc->rx_buf_get + 1) & (USBD_CDC_RX_DATA_SIZE - 1); + cdc->rx_buf_get = (cdc->rx_buf_get + 1) & (MICROPY_HW_USB_CDC_RX_DATA_SIZE - 1); } usbd_cdc_rx_check_resume(cdc); diff --git a/ports/stm32/usbd_cdc_interface.h b/ports/stm32/usbd_cdc_interface.h index 6b510f2399..76b5c9e79b 100644 --- a/ports/stm32/usbd_cdc_interface.h +++ b/ports/stm32/usbd_cdc_interface.h @@ -31,12 +31,7 @@ ****************************************************************************** */ -#ifndef USBD_CDC_RX_DATA_SIZE -#define USBD_CDC_RX_DATA_SIZE (1024) // this must be 2 or greater, and a power of 2 -#endif -#ifndef USBD_CDC_TX_DATA_SIZE -#define USBD_CDC_TX_DATA_SIZE (1024) // This must be a power of 2 and no greater than 16384 -#endif +#include "py/mpconfig.h" // Values for connect_state #define USBD_CDC_CONNECT_STATE_DISCONNECTED (0) @@ -52,14 +47,14 @@ typedef struct _usbd_cdc_itf_t { usbd_cdc_state_t base; // state for the base CDC layer uint8_t rx_packet_buf[CDC_DATA_MAX_PACKET_SIZE]; // received data from USB OUT endpoint is stored in this buffer - uint8_t rx_user_buf[USBD_CDC_RX_DATA_SIZE]; // received data is buffered here until the user reads it + uint8_t rx_user_buf[MICROPY_HW_USB_CDC_RX_DATA_SIZE]; // received data is buffered here until the user reads it volatile uint16_t rx_buf_put; // circular buffer index uint16_t rx_buf_get; // circular buffer index uint8_t rx_buf_full; // rx from host will be blocked while this is true - uint8_t tx_buf[USBD_CDC_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer - uint16_t tx_buf_ptr_in; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when new data is available - volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo USBD_CDC_TX_DATA_SIZE when data is drained + uint8_t tx_buf[MICROPY_HW_USB_CDC_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer + uint16_t tx_buf_ptr_in; // increment this pointer modulo MICROPY_HW_USB_CDC_TX_DATA_SIZE when new data is available + volatile uint16_t tx_buf_ptr_out; // increment this pointer modulo MICROPY_HW_USB_CDC_TX_DATA_SIZE when data is drained uint16_t tx_buf_ptr_out_next; // next position of above once transmission finished uint8_t tx_need_empty_packet; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size