Skip to content

Commit b30c02a

Browse files
committed
stmhal: Get USB enumerating a CDC device.
Enumerates CDC device over USB, but doesn't transmit/receive yet.
1 parent 536dde2 commit b30c02a

52 files changed

Lines changed: 1947 additions & 17 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

stmhal/Makefile

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ include ../py/py.mk
88

99
CMSIS_DIR=cmsis
1010
HAL_DIR=hal
11-
#STMUSB_DIR=stmusb
12-
#STMUSBD_DIR=stmusbd
13-
#STMUSBH_DIR=stmusbh
11+
USBDEV_DIR=usbdev
12+
#USBHOST_DIR=usbhost
1413
#FATFS_DIR=fatfs
1514
#CC3K_DIR=cc3k
1615
DFU=../tools/dfu.py
@@ -22,9 +21,8 @@ INC += -I$(PY_SRC)
2221
INC += -I$(CMSIS_DIR)/inc
2322
INC += -I$(CMSIS_DIR)/devinc
2423
INC += -I$(HAL_DIR)/inc
25-
#INC += -I$(STMUSB_DIR)
26-
#INC += -I$(STMUSBD_DIR)
27-
#INC += -I$(STMUSBH_DIR)
24+
INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/cdc/inc
25+
#INC += -I$(USBHOST_DIR)
2826
#INC += -I$(FATFS_DIR)
2927
#INC += -I$(CC3K_DIR)
3028

@@ -57,10 +55,15 @@ SRC_C = \
5755
system_stm32f4xx.c \
5856
stm32f4xx_it.c \
5957
stm32f4xx_hal_msp.c \
58+
usbd_conf.c \
59+
usbd_desc.c \
60+
usbd_cdc_interface.c \
61+
pendsv.c \
6062
systick.c \
6163
led.c \
6264
pin.c \
6365
usart.c \
66+
usb.c \
6467
printf.c \
6568
math.c \
6669
malloc0.c \
@@ -70,14 +73,12 @@ SRC_C = \
7073
import.c \
7174
lexerfatfs.c \
7275

73-
# pendsv.c \
7476
# gpio.c \
7577
# lcd.c \
7678
# servo.c \
7779
# flash.c \
7880
# storage.c \
7981
# accel.c \
80-
# usb.c \
8182
# timer.c \
8283
# audio.c \
8384
# sdcard.c \
@@ -100,8 +101,10 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
100101
stm32f4xx_hal_cortex.c \
101102
stm32f4xx_hal_dma.c \
102103
stm32f4xx_hal_gpio.c \
104+
stm32f4xx_hal_pcd.c \
103105
stm32f4xx_hal_rcc.c \
104106
stm32f4xx_hal_uart.c \
107+
stm32f4xx_ll_usb.c \
105108
)
106109

107110
SRC_STMPERIPH = $(addprefix $(STMPERIPH_DIR)/,\
@@ -136,8 +139,14 @@ SRC_STMUSB = $(addprefix $(STMUSB_DIR)/,\
136139
)
137140
# usb_otg.c \
138141
139-
SRC_STMUSBD = $(addprefix $(STMUSBD_DIR)/,\
140-
usbd_core.c \
142+
SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
143+
core/src/usbd_core.c \
144+
core/src/usbd_ctlreq.c \
145+
core/src/usbd_ioreq.c \
146+
class/cdc/src/usbd_cdc.c \
147+
)
148+
149+
# usbd_core.c \
141150
usbd_ioreq.c \
142151
usbd_req.c \
143152
usbd_usr.c \
@@ -186,7 +195,7 @@ OBJ += $(PY_O)
186195
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
187196
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
188197
OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o))
189-
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSB:.c=.o))
198+
OBJ += $(addprefix $(BUILD)/, $(SRC_USBDEV:.c=.o))
190199
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSBD:.c=.o))
191200
#OBJ += $(addprefix $(BUILD)/, $(SRC_STMUSBH:.c=.o))
192201
#OBJ += $(addprefix $(BUILD)/, $(SRC_FATFS:.c=.o))

stmhal/boards/PYBOARD4/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
/* Exported constants --------------------------------------------------------*/
4848

4949
#define STM32F405xx
50+
#define USE_USB_FS
5051

5152
/* ########################## Module Selection ############################## */
5253
/**

stmhal/main.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "misc.h"
2222
#include "systick.h"
23+
#include "pendsv.h"
2324
#include "led.h"
2425
#include "usart.h"
2526
#include "mpconfig.h"
@@ -37,16 +38,15 @@
3738
#include "gccollect.h"
3839
#include "pyexec.h"
3940
#include "pybmodule.h"
41+
#include "usb.h"
4042
#if 0
4143
#include "ff.h"
4244
#include "lexerfatfs.h"
43-
#include "pendsv.h"
4445
#include "servo.h"
4546
#include "lcd.h"
4647
#include "storage.h"
4748
#include "sdcard.h"
4849
#include "accel.h"
49-
#include "usb.h"
5050
#include "timer.h"
5151
#include "pybwlan.h"
5252
#include "usrsw.h"
@@ -82,8 +82,10 @@ void flash_error(int n) {
8282

8383
void __fatal_error(const char *msg) {
8484
#if MICROPY_HW_HAS_LCD
85+
#if 0
8586
lcd_print_strn("\nFATAL ERROR:\n", 14);
8687
lcd_print_strn(msg, strlen(msg));
88+
#endif
8789
#endif
8890
for (;;) {
8991
flash_error(1);
@@ -116,6 +118,9 @@ void fatality(void) {
116118
led_state(PYB_LED_G1, 1);
117119
led_state(PYB_LED_R2, 1);
118120
led_state(PYB_LED_G2, 1);
121+
for (;;) {
122+
flash_error(1);
123+
}
119124
}
120125

121126
static const char fresh_boot_py[] =
@@ -244,9 +249,7 @@ int main(void) {
244249

245250
// basic sub-system init
246251
sys_tick_init();
247-
#if 0
248252
pendsv_init();
249-
#endif
250253
led_init();
251254

252255
#if 0
@@ -469,6 +472,7 @@ int main(void) {
469472
}
470473
}
471474
#endif
475+
#endif
472476

473477
#ifdef USE_HOST_MODE
474478
// USB host
@@ -478,6 +482,22 @@ int main(void) {
478482
pyb_usb_dev_init(PYB_USB_DEV_VCP_MSC);
479483
#endif
480484

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

stmhal/pendsv.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <stdlib.h>
2+
#include <stm32f4xx_hal.h>
3+
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "qstr.h"
7+
#include "obj.h"
8+
#include "pendsv.h"
9+
10+
static void *pendsv_object = NULL;
11+
12+
void pendsv_init(void) {
13+
// set PendSV interrupt at lowest priority
14+
HAL_NVIC_SetPriority(PendSV_IRQn, 0xf, 0xf);
15+
}
16+
17+
// call this function to raise a pending exception during an interrupt
18+
// it will wait until all interrupts are finished then raise the given
19+
// exception object using nlr_jump in the context of the top-level thread
20+
void pendsv_nlr_jump(void *o) {
21+
pendsv_object = o;
22+
SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
23+
}
24+
25+
// since we play tricks with the stack, the compiler must not generate a
26+
// prelude for this function
27+
void pendsv_isr_handler(void) __attribute__((naked));
28+
29+
void pendsv_isr_handler(void) {
30+
// re-jig the stack so that when we return from this interrupt handler
31+
// it returns instead to nlr_jump with argument pendsv_object
32+
// note that stack has a different layout if DEBUG is enabled
33+
//
34+
// on entry to this (naked) function, stack has the following layout:
35+
//
36+
// stack layout with DEBUG disabled:
37+
// sp[6]: pc
38+
// sp[5]: ?
39+
// sp[4]: ?
40+
// sp[3]: ?
41+
// sp[2]: ?
42+
// sp[1]: ?
43+
// sp[0]: r0
44+
//
45+
// stack layout with DEBUG enabled:
46+
// sp[8]: pc
47+
// sp[7]: lr
48+
// sp[6]: ?
49+
// sp[5]: ?
50+
// sp[4]: ?
51+
// sp[3]: ?
52+
// sp[2]: r0
53+
// sp[1]: 0xfffffff9
54+
// sp[0]: ?
55+
56+
__asm volatile (
57+
"ldr r0, pendsv_object_ptr\n"
58+
"ldr r0, [r0]\n"
59+
#if defined(PENDSV_DEBUG)
60+
"str r0, [sp, #8]\n"
61+
#else
62+
"str r0, [sp, #0]\n"
63+
#endif
64+
"ldr r0, nlr_jump_ptr\n"
65+
#if defined(PENDSV_DEBUG)
66+
"str r0, [sp, #32]\n"
67+
#else
68+
"str r0, [sp, #24]\n"
69+
#endif
70+
"bx lr\n"
71+
".align 2\n"
72+
"pendsv_object_ptr: .word pendsv_object\n"
73+
"nlr_jump_ptr: .word nlr_jump\n"
74+
);
75+
76+
/*
77+
uint32_t x[2] = {0x424242, 0xdeaddead};
78+
printf("PendSV: %p\n", x);
79+
for (uint32_t *p = (uint32_t*)(((uint32_t)x - 15) & 0xfffffff0), i = 64; i > 0; p += 4, i -= 4) {
80+
printf(" %p: %08x %08x %08x %08x\n", p, (uint)p[0], (uint)p[1], (uint)p[2], (uint)p[3]);
81+
}
82+
*/
83+
}

stmhal/pendsv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void pendsv_init(void);
2+
void pendsv_nlr_jump(void *val);
3+
void pendsv_isr_handler(void);

0 commit comments

Comments
 (0)