Skip to content

Commit e285511

Browse files
committed
stmhal: Get USB CDC REPL working.
New USB HAL is quite a bit improved over previous one. Now has better callbacks and flow control. REPL over USB CDC now works as before, except for soft-reset (since USB driver uses malloc...).
1 parent 87e423b commit e285511

9 files changed

Lines changed: 174 additions & 198 deletions

File tree

stmhal/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
103103
stm32f4xx_hal_gpio.c \
104104
stm32f4xx_hal_pcd.c \
105105
stm32f4xx_hal_rcc.c \
106+
stm32f4xx_hal_tim.c \
107+
stm32f4xx_hal_tim_ex.c \
106108
stm32f4xx_hal_uart.c \
107109
stm32f4xx_ll_usb.c \
108110
)

stmhal/main.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,15 @@ int main(void) {
251251
pendsv_init();
252252
led_init();
253253

254+
// turn on LED to indicate bootup
255+
led_state(PYB_LED_GREEN, 1);
256+
254257
#if 0
255258
#if MICROPY_HW_ENABLE_RTC
256259
rtc_init();
257260
#endif
258261
#endif
259262

260-
// turn on LED to indicate bootup
261-
led_state(PYB_LED_G1, 1);
262-
263263
#if 0
264264
// more sub-system init
265265
#if MICROPY_HW_HAS_SDCARD
@@ -454,7 +454,7 @@ int main(void) {
454454
}
455455

456456
// turn boot-up LED off
457-
led_state(PYB_LED_G1, 0);
457+
led_state(PYB_LED_GREEN, 0);
458458

459459
#if 0
460460
#if MICROPY_HW_HAS_SDCARD
@@ -481,21 +481,6 @@ int main(void) {
481481
pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC);
482482
#endif
483483

484-
#if 0
485-
// test USB CDC
486-
extern uint8_t UserTxBuffer[];/* Received Data over UART (CDC interface) are stored in this buffer */
487-
extern uint32_t UserTxBufPtrOut; /* Increment this pointer or roll it back to
488-
start address when data are sent over USB */
489-
for (;;) {
490-
UserTxBuffer[UserTxBufPtrOut++] = 'a';
491-
UserTxBuffer[UserTxBufPtrOut++] = 'b';
492-
UserTxBuffer[UserTxBufPtrOut++] = 'c';
493-
UserTxBuffer[UserTxBufPtrOut++] = 'd';
494-
HAL_Delay(500);
495-
led_toggle(PYB_LED_BLUE);
496-
}
497-
#endif
498-
499484
#if 0
500485
// run main script
501486
{

stmhal/printf.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
#include "lcd.h"
1313
#endif
1414
#include "usart.h"
15-
#if 0
1615
#include "usb.h"
17-
#endif
1816

1917
#if MICROPY_ENABLE_FLOAT
2018
#include "formatfloat.h"
@@ -273,12 +271,10 @@ void stdout_print_strn(void *data, const char *str, unsigned int len) {
273271
usart_tx_strn_cooked(pyb_usart_global_debug, str, len);
274272
any = true;
275273
}
276-
#if 0
277274
if (usb_vcp_is_enabled()) {
278275
usb_vcp_send_strn_cooked(str, len);
279276
any = true;
280277
}
281-
#endif
282278
if (!any) {
283279
#if 0
284280
#if MICROPY_HW_HAS_LCD

stmhal/pyexec.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#include "pyexec.h"
2323
#if 0
2424
#include "storage.h"
25-
#include "usb.h"
2625
#endif
26+
#include "usb.h"
2727
#include "usart.h"
2828

2929
static bool repl_display_debugging_info = 0;
@@ -35,9 +35,7 @@ void stdout_tx_str(const char *str) {
3535
#if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
3636
lcd_print_str(str);
3737
#endif
38-
#if 0
3938
usb_vcp_send_str(str);
40-
#endif
4139
}
4240

4341
int stdin_rx_chr(void) {
@@ -51,12 +49,9 @@ int stdin_rx_chr(void) {
5149
}
5250
#endif
5351
#endif
54-
#if 0
5552
if (usb_vcp_rx_any() != 0) {
5653
return usb_vcp_rx_get();
57-
} else
58-
#endif
59-
if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
54+
} else if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
6055
return usart_rx_char(pyb_usart_global_debug);
6156
}
6257
HAL_Delay(1);
@@ -80,13 +75,6 @@ char *str_dup(const char *str) {
8075

8176
static const char *readline_hist[READLINE_HIST_SIZE] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
8277

83-
#if 0
84-
#else
85-
#define VCP_CHAR_CTRL_A (1)
86-
#define VCP_CHAR_CTRL_C (3)
87-
#define VCP_CHAR_CTRL_D (4)
88-
#endif
89-
9078
int readline(vstr_t *line, const char *prompt) {
9179
stdout_tx_str(prompt);
9280
int len = vstr_len(line);
@@ -173,21 +161,15 @@ bool parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bo
173161
bool ret;
174162
uint32_t start = HAL_GetTick();
175163
if (nlr_push(&nlr) == 0) {
176-
#if 0
177164
usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); // allow ctrl-C to interrupt us
178-
#endif
179165
rt_call_function_0(module_fun);
180-
#if 0
181166
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
182-
#endif
183167
nlr_pop();
184168
ret = true;
185169
} else {
186170
// uncaught exception
187171
// FIXME it could be that an interrupt happens just before we disable it here
188-
#if 0
189172
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
190-
#endif
191173
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
192174
ret = false;
193175
}

stmhal/stm32f4xx_hal_msp.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
/* Includes ------------------------------------------------------------------*/
4949
#include "stm32f4xx_hal.h"
50+
#include "usbd_cdc.h"
51+
#include "usbd_cdc_interface.h"
5052

5153
/** @addtogroup STM32F4xx_HAL_Driver
5254
* @{
@@ -73,23 +75,22 @@
7375
* @param None
7476
* @retval None
7577
*/
76-
void HAL_MspInit(void)
77-
{
78-
/* NOTE : This function is generated automatically by MicroXplorer and eventually
79-
modified by the user
80-
*/
78+
void HAL_MspInit(void) {
79+
// set up the timer for USBD CDC
80+
USBD_CDC_TIMx_CLK_ENABLE();
81+
HAL_NVIC_SetPriority(USBD_CDC_TIMx_IRQn, 6, 0);
82+
HAL_NVIC_EnableIRQ(USBD_CDC_TIMx_IRQn);
8183
}
8284

8385
/**
8486
* @brief DeInitializes the Global MSP.
8587
* @param None
8688
* @retval None
8789
*/
88-
void HAL_MspDeInit(void)
89-
{
90-
/* NOTE : This function is generated automatically by MicroXplorer and eventually
91-
modified by the user
92-
*/
90+
void HAL_MspDeInit(void) {
91+
// reset USBD CDC timer
92+
USBD_CDC_TIMx_FORCE_RESET();
93+
USBD_CDC_TIMx_RELEASE_RESET();
9394
}
9495

9596
/**

stmhal/stm32f4xx_it.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
extern void fatality();
6666
extern PCD_HandleTypeDef hpcd;
67+
extern TIM_HandleTypeDef USBD_CDC_TimHandle;
6768

6869
/* Private function prototypes -----------------------------------------------*/
6970
/* Private functions ---------------------------------------------------------*/
@@ -348,4 +349,9 @@ void RTC_WKUP_IRQHandler(void) {
348349
Handle_EXTI_Irq(EXTI_RTC_WAKEUP);
349350
}
350351

352+
void TIM3_IRQHandler(void) {
353+
// USBD CDC timer is TIM3
354+
HAL_TIM_IRQHandler(&USBD_CDC_TimHandle);
355+
}
356+
351357
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

stmhal/usb.c

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515
#include "mpconfig.h"
1616
#include "qstr.h"
1717
#include "obj.h"
18-
#include "pendsv.h"
18+
//#include "pendsv.h"
1919
#include "usb.h"
2020

2121
#ifdef USE_DEVICE_MODE
22-
//extern CDC_IF_Prop_TypeDef VCP_fops;
2322
USBD_HandleTypeDef hUSBDDevice;
2423
#endif
2524

2625
static int dev_is_enabled = 0;
2726
uint32_t APP_dev_is_connected = 0; /* used by usbd_cdc_vcp */
28-
static char rx_buf[64];
29-
static int rx_buf_in;
30-
static int rx_buf_out;
31-
static int interrupt_char = VCP_CHAR_NONE;
3227
mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
3328

3429
void pyb_usb_dev_init(int usb_dev_type) {
@@ -52,9 +47,6 @@ void pyb_usb_dev_init(int usb_dev_type) {
5247
break;
5348
}
5449
}
55-
rx_buf_in = 0;
56-
rx_buf_out = 0;
57-
interrupt_char = VCP_CHAR_NONE;
5850
dev_is_enabled = 1;
5951

6052
// create an exception object for interrupting by VCP
@@ -72,54 +64,19 @@ bool usb_vcp_is_connected(void) {
7264

7365
void usb_vcp_set_interrupt_char(int c) {
7466
if (dev_is_enabled) {
75-
interrupt_char = c;
76-
}
77-
}
78-
79-
void usb_vcp_receive(const char *buf, uint32_t len) {
80-
if (dev_is_enabled) {
81-
for (int i = 0; i < len; i++) {
82-
83-
// catch special interrupt character
84-
if (buf[i] == interrupt_char) {
85-
// raise exception when interrupts are finished
86-
mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
87-
pendsv_nlr_jump(mp_const_vcp_interrupt);
88-
interrupt_char = VCP_CHAR_NONE;
89-
continue;
90-
}
91-
92-
rx_buf[rx_buf_in++] = buf[i];
93-
if (rx_buf_in >= sizeof(rx_buf)) {
94-
rx_buf_in = 0;
95-
}
96-
if (rx_buf_in == rx_buf_out) {
97-
rx_buf_out = rx_buf_in + 1;
98-
if (rx_buf_out >= sizeof(rx_buf)) {
99-
rx_buf_out = 0;
100-
}
101-
}
67+
if (c != VCP_CHAR_NONE) {
68+
mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
10269
}
70+
USBD_CDC_SetInterrupt(c, mp_const_vcp_interrupt);
10371
}
10472
}
10573

10674
int usb_vcp_rx_any(void) {
107-
if (rx_buf_in >= rx_buf_out) {
108-
return rx_buf_in - rx_buf_out;
109-
} else {
110-
return rx_buf_in + sizeof(rx_buf) - rx_buf_out;
111-
}
75+
return USBD_CDC_RxAny();
11276
}
11377

11478
char usb_vcp_rx_get(void) {
115-
while (rx_buf_out == rx_buf_in) {
116-
}
117-
char c = rx_buf[rx_buf_out];
118-
rx_buf_out += 1;
119-
if (rx_buf_out >= sizeof(rx_buf)) {
120-
rx_buf_out = 0;
121-
}
122-
return c;
79+
return USBD_CDC_RxGet();
12380
}
12481

12582
void usb_vcp_send_str(const char *str) {
@@ -129,39 +86,22 @@ void usb_vcp_send_str(const char *str) {
12986
void usb_vcp_send_strn(const char *str, int len) {
13087
#ifdef USE_DEVICE_MODE
13188
if (dev_is_enabled) {
132-
#if 0
133-
USBD_CDC_fops.pIf_DataTx((const uint8_t*)str, len);
134-
#endif
89+
USBD_CDC_Tx(str, len);
13590
}
13691
#endif
13792
}
13893

139-
#include "usbd_conf.h"
140-
141-
/* These are external variables imported from CDC core to be used for IN
142-
transfer management. */
143-
#ifdef USE_DEVICE_MODE
144-
extern uint8_t UserRxBuffer[];/* Received Data over USB are stored in this buffer */
145-
extern uint8_t UserTxBuffer[];/* Received Data over UART (CDC interface) are stored in this buffer */
146-
extern uint32_t BuffLength;
147-
extern uint32_t UserTxBufPtrIn;/* Increment this pointer or roll it back to
148-
start address when data are received over USART */
149-
extern uint32_t UserTxBufPtrOut; /* Increment this pointer or roll it back to
150-
start address when data are sent over USB */
151-
#endif
152-
15394
void usb_vcp_send_strn_cooked(const char *str, int len) {
15495
#ifdef USE_DEVICE_MODE
155-
#if 0
156-
for (const char *top = str + len; str < top; str++) {
157-
if (*str == '\n') {
158-
APP_Rx_Buffer[APP_Rx_ptr_in] = '\r';
159-
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
96+
if (dev_is_enabled) {
97+
for (const char *top = str + len; str < top; str++) {
98+
if (*str == '\n') {
99+
USBD_CDC_Tx("\r\n", 2);
100+
} else {
101+
USBD_CDC_Tx(str, 1);
102+
}
160103
}
161-
APP_Rx_Buffer[APP_Rx_ptr_in] = *str;
162-
APP_Rx_ptr_in = (APP_Rx_ptr_in + 1) & (APP_RX_DATA_SIZE - 1);
163104
}
164-
#endif
165105
#endif
166106
}
167107

0 commit comments

Comments
 (0)