29
29
#include " sdkconfig.h"
30
30
31
31
#include " soc/soc.h"
32
+
32
33
#include " soc/efuse_reg.h"
34
+ #include " soc/periph_defs.h"
33
35
#include " soc/rtc_cntl_reg.h"
34
- #include " soc/usb_struct.h"
36
+
37
+ #include " soc/system_reg.h"
38
+ #include " soc/timer_group_struct.h"
39
+ #include " soc/usb_periph.h"
35
40
#include " soc/usb_reg.h"
41
+ #include " soc/usb_struct.h"
36
42
#include " soc/usb_wrap_reg.h"
37
43
#include " soc/usb_wrap_struct.h"
38
- #include " soc/usb_periph.h"
39
- #include " soc/periph_defs.h"
40
- #include " soc/timer_group_struct.h"
41
- #include " soc/system_reg.h"
42
44
43
45
#include " hal/usb_hal.h"
44
46
45
47
// compiler error with gpio_ll_get_drive_capability()
46
- // invalid conversion from 'uint32_t' {aka 'unsigned int'} to 'gpio_drive_cap_t' [-fpermissive]
47
- // #include "hal/gpio_ll.h"
48
+ // invalid conversion from 'uint32_t' {aka 'unsigned int'} to 'gpio_drive_cap_t'
49
+ // [-fpermissive] #include "hal/gpio_ll.h"
48
50
49
51
#include " freertos/FreeRTOS.h"
50
52
#include " freertos/task.h"
58
60
59
61
#include " esp32-hal.h"
60
62
61
- #include " esp32s2/rom/usb/usb_persist.h"
62
- #include " esp32s2/rom/usb/usb_dc.h"
63
63
#include " esp32s2/rom/usb/chip_usb_dw_wrapper.h"
64
+ #include " esp32s2/rom/usb/usb_dc.h"
65
+ #include " esp32s2/rom/usb/usb_persist.h"
64
66
65
67
#include " arduino/Adafruit_TinyUSB_API.h"
66
68
#include " arduino/Adafruit_USBD_Device.h"
69
71
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
70
72
// --------------------------------------------------------------------+
71
73
72
- // Note: persist mode is mostly copied from esp32-hal-tinyusb.c from arduino-esp32 core.
74
+ // Note: persist mode is mostly copied from esp32-hal-tinyusb.c from
75
+ // arduino-esp32 core.
73
76
74
77
typedef enum {
75
- RESTART_NO_PERSIST,
76
- RESTART_PERSIST,
77
- RESTART_BOOTLOADER,
78
- RESTART_BOOTLOADER_DFU,
79
- RESTART_TYPE_MAX
78
+ RESTART_NO_PERSIST,
79
+ RESTART_PERSIST,
80
+ RESTART_BOOTLOADER,
81
+ RESTART_BOOTLOADER_DFU,
82
+ RESTART_TYPE_MAX
80
83
} restart_type_t ;
81
84
82
85
static bool usb_persist_enabled = false ;
83
86
static restart_type_t usb_persist_mode = RESTART_NO_PERSIST;
84
87
85
- static void IRAM_ATTR usb_persist_shutdown_handler (void )
86
- {
87
- if (usb_persist_mode != RESTART_NO_PERSIST){
88
- if (usb_persist_enabled) {
89
- usb_dc_prepare_persist ();
90
- }
91
- if (usb_persist_mode == RESTART_BOOTLOADER) {
92
- // USB CDC Download
93
- if (usb_persist_enabled) {
94
- chip_usb_set_persist_flags (USBDC_PERSIST_ENA);
95
- } else {
96
- periph_module_reset (PERIPH_USB_MODULE);
97
- periph_module_enable (PERIPH_USB_MODULE);
98
- }
99
- REG_WRITE (RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
100
- } else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
101
- // DFU Download
102
- chip_usb_set_persist_flags (USBDC_BOOT_DFU);
103
- REG_WRITE (RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
104
- } else if (usb_persist_enabled) {
105
- // USB Persist reboot
106
- chip_usb_set_persist_flags (USBDC_PERSIST_ENA);
107
- }
88
+ static void IRAM_ATTR usb_persist_shutdown_handler (void ) {
89
+ if (usb_persist_mode != RESTART_NO_PERSIST) {
90
+ if (usb_persist_enabled) {
91
+ usb_dc_prepare_persist ();
108
92
}
93
+ if (usb_persist_mode == RESTART_BOOTLOADER) {
94
+ // USB CDC Download
95
+ if (usb_persist_enabled) {
96
+ chip_usb_set_persist_flags (USBDC_PERSIST_ENA);
97
+ } else {
98
+ periph_module_reset (PERIPH_USB_MODULE);
99
+ periph_module_enable (PERIPH_USB_MODULE);
100
+ }
101
+ REG_WRITE (RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
102
+ } else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
103
+ // DFU Download
104
+ chip_usb_set_persist_flags (USBDC_BOOT_DFU);
105
+ REG_WRITE (RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
106
+ } else if (usb_persist_enabled) {
107
+ // USB Persist reboot
108
+ chip_usb_set_persist_flags (USBDC_PERSIST_ENA);
109
+ }
110
+ }
109
111
}
110
112
111
- static void configure_pins (usb_hal_context_t *usb)
112
- {
113
- for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1 ; ++iopin) {
114
- if ((usb->use_external_phy ) || (iopin->ext_phy_only == 0 )) {
115
- esp_rom_gpio_pad_select_gpio (iopin->pin );
116
- if (iopin->is_output ) {
117
- esp_rom_gpio_connect_out_signal (iopin->pin , iopin->func , false , false );
118
- } else {
119
- esp_rom_gpio_connect_in_signal (iopin->pin , iopin->func , false );
120
- if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH)) {
121
- // workaround for compiler error with including "hal/gpio_ll.h"
122
- // gpio_ll_input_enable(&GPIO, (gpio_num_t) iopin->pin);
123
- PIN_INPUT_ENABLE (GPIO_PIN_MUX_REG[iopin->pin ]);
124
- }
125
- }
126
- esp_rom_gpio_pad_unhold (iopin->pin );
113
+ static void configure_pins (usb_hal_context_t *usb) {
114
+ for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1 ;
115
+ ++iopin) {
116
+ if ((usb->use_external_phy ) || (iopin->ext_phy_only == 0 )) {
117
+ esp_rom_gpio_pad_select_gpio (iopin->pin );
118
+ if (iopin->is_output ) {
119
+ esp_rom_gpio_connect_out_signal (iopin->pin , iopin->func , false , false );
120
+ } else {
121
+ esp_rom_gpio_connect_in_signal (iopin->pin , iopin->func , false );
122
+ if ((iopin->pin != GPIO_FUNC_IN_LOW) &&
123
+ (iopin->pin != GPIO_FUNC_IN_HIGH)) {
124
+ // workaround for compiler error with including "hal/gpio_ll.h"
125
+ // gpio_ll_input_enable(&GPIO, (gpio_num_t) iopin->pin);
126
+ PIN_INPUT_ENABLE (GPIO_PIN_MUX_REG[iopin->pin ]);
127
127
}
128
+ }
129
+ esp_rom_gpio_pad_unhold (iopin->pin );
128
130
}
129
- if (!usb->use_external_phy ) {
130
- gpio_set_drive_capability ((gpio_num_t ) USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
131
- gpio_set_drive_capability ((gpio_num_t ) USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
132
- }
131
+ }
132
+ if (!usb->use_external_phy ) {
133
+ gpio_set_drive_capability ((gpio_num_t )USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
134
+ gpio_set_drive_capability ((gpio_num_t )USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
135
+ }
133
136
}
134
137
135
138
// --------------------------------------------------------------------+
@@ -141,8 +144,11 @@ static void configure_pins(usb_hal_context_t *usb)
141
144
// USB Device Driver task
142
145
// This top level thread processes all usb events and invokes callbacks
143
146
static void usb_device_task (void *param) {
144
- (void )param;
145
- while (1 ) tud_task (); // RTOS forever loop
147
+ (void )param;
148
+ // RTOS forever loop
149
+ while (1 ) {
150
+ tud_task ();
151
+ }
146
152
}
147
153
148
154
void TinyUSB_Port_InitDevice (uint8_t rhport) {
@@ -151,29 +157,28 @@ void TinyUSB_Port_InitDevice(uint8_t rhport) {
151
157
// from esp32-hal_tinyusb
152
158
bool usb_did_persist = (USB_WRAP.date .val == USBDC_PERSIST_ENA);
153
159
154
- // if(usb_did_persist && usb_persist_enabled){
160
+ // if(usb_did_persist && usb_persist_enabled){
155
161
// Enable USB/IO_MUX peripheral reset, if coming from persistent reboot
156
162
REG_CLR_BIT (RTC_CNTL_USB_CONF_REG, RTC_CNTL_IO_MUX_RESET_DISABLE);
157
163
REG_CLR_BIT (RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE);
158
164
// } else
159
- if (!usb_did_persist || !usb_persist_enabled){
165
+ if (!usb_did_persist || !usb_persist_enabled) {
160
166
// Reset USB module
161
167
periph_module_reset (PERIPH_USB_MODULE);
162
168
periph_module_enable (PERIPH_USB_MODULE);
163
169
}
164
170
165
171
esp_register_shutdown_handler (usb_persist_shutdown_handler);
166
172
167
- usb_hal_context_t hal = {
168
- .use_external_phy = false
169
- };
173
+ usb_hal_context_t hal = {.use_external_phy = false };
170
174
usb_hal_init (&hal);
171
175
configure_pins (&hal);
172
176
173
177
tusb_init ();
174
178
175
179
// Create a task for tinyusb device stack
176
- xTaskCreate (usb_device_task, " usbd" , USBD_STACK_SZ, NULL , configMAX_PRIORITIES - 1 , NULL );
180
+ xTaskCreate (usb_device_task, " usbd" , USBD_STACK_SZ, NULL ,
181
+ configMAX_PRIORITIES - 1 , NULL );
177
182
}
178
183
179
184
void TinyUSB_Port_EnterDFU (void ) {
@@ -186,18 +191,20 @@ uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) {
186
191
uint32_t *serial_32 = (uint32_t *)serial_id;
187
192
188
193
/* Get the MAC address */
189
- const uint32_t mac0 = __builtin_bswap32 (REG_GET_FIELD (EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_MAC_0));
190
- const uint16_t mac1 = __builtin_bswap16 ((uint16_t ) REG_GET_FIELD (EFUSE_RD_MAC_SPI_SYS_1_REG, EFUSE_MAC_1));
194
+ const uint32_t mac0 =
195
+ __builtin_bswap32 (REG_GET_FIELD (EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_MAC_0));
196
+ const uint16_t mac1 = __builtin_bswap16 (
197
+ (uint16_t )REG_GET_FIELD (EFUSE_RD_MAC_SPI_SYS_1_REG, EFUSE_MAC_1));
191
198
192
199
memcpy (serial_id, &mac1, 2 );
193
- memcpy (serial_id+ 2 , &mac0, 4 );
200
+ memcpy (serial_id + 2 , &mac0, 4 );
194
201
195
202
return 6 ;
196
203
}
197
204
198
205
extern " C" void yield (void ) {
199
- TinyUSB_Device_FlushCDC ();
200
- vPortYield ();
206
+ TinyUSB_Device_FlushCDC ();
207
+ vPortYield ();
201
208
}
202
209
203
210
#endif // USE_TINYUSB
0 commit comments