Skip to content

Commit e06f9b7

Browse files
committed
Handle persistent reboot better in CDC
1 parent 2e19d27 commit e06f9b7

File tree

10 files changed

+228
-22
lines changed

10 files changed

+228
-22
lines changed

cores/esp32/esp32-hal-tinyusb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ static bool tinyusb_load_enabled_interfaces(){
381381
};
382382
memcpy(tinyusb_config_descriptor, descriptor, TUD_CONFIG_DESC_LEN);
383383
if ((tinyusb_loaded_interfaces_mask == (BIT(USB_INTERFACE_CDC) | BIT(USB_INTERFACE_DFU))) || (tinyusb_loaded_interfaces_mask == BIT(USB_INTERFACE_CDC))) {
384-
tinyusb_persist_set_enable(true);
384+
usb_persist_set_enable(true);
385385
log_d("USB Persist enabled");
386386
}
387387
log_d("Load Done: if_num: %u, descr_len: %u, if_mask: 0x%x", tinyusb_loaded_interfaces_num, tinyusb_config_descriptor_len, tinyusb_loaded_interfaces_mask);

libraries/USB/src/USB.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ static uint16_t load_dfu_descriptor(uint8_t * dst, uint8_t * itf)
4242
// Invoked on DFU_DETACH request to reboot to the bootloader
4343
void tud_dfu_rt_reboot_to_dfu(void)
4444
{
45-
tinyusb_persist_set_mode(REBOOT_BOOTLOADER_DFU);
46-
esp_restart();
45+
usb_persist_restart(RESTART_BOOTLOADER_DFU);
4746
}
4847
#endif /* CFG_TUD_DFU_RT */
4948

libraries/USB/src/USBCDC.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t eve
7070
((USBCDC*)arg)->_onUnplugged();
7171
}
7272

73-
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(false), reboot_enable(true), rx_queue(NULL) {
73+
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(usb_persist_this_boot()), reboot_enable(true), rx_queue(NULL) {
7474
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
7575
if(itf < MAX_USB_CDC_DEVICES){
7676
devices[itf] = this;
@@ -94,6 +94,15 @@ void USBCDC::begin(size_t rx_queue_len)
9494
if(!rx_queue){
9595
return;
9696
}
97+
if(connected){
98+
arduino_usb_cdc_event_data_t p = {0};
99+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_CONNECTED_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
100+
101+
arduino_usb_cdc_event_data_t l = {0};
102+
l.line_state.dtr = true;
103+
l.line_state.rts = true;
104+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
105+
}
97106
}
98107

99108
void USBCDC::end()
@@ -137,8 +146,7 @@ void USBCDC::_onLineState(bool _dtr, bool _rts){
137146
}
138147
} else if(!dtr && !rts){
139148
if(lineState == CDC_LINE_3){
140-
tinyusb_persist_set_mode(REBOOT_BOOTLOADER);
141-
esp_restart();
149+
usb_persist_restart(RESTART_BOOTLOADER);
142150
} else {
143151
lineState = CDC_LINE_IDLE;
144152
}
@@ -195,13 +203,6 @@ void USBCDC::_onRX(){
195203
}
196204

197205
void USBCDC::enableReboot(bool enable){
198-
if(enable != reboot_enable){
199-
if(enable){
200-
tinyusb_persist_set_mode(REBOOT_PERSIST);
201-
} else {
202-
tinyusb_persist_set_mode(REBOOT_NO_PERSIST);
203-
}
204-
}
205206
reboot_enable = enable;
206207
}
207208
bool USBCDC::rebootEnabled(void){

tools/sdk/esp32s2/include/tinyusb/port/esp32s2/include/usb_persist.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,34 @@ extern "C" {
2424
* USB Persistence API
2525
* */
2626
typedef enum {
27-
REBOOT_NO_PERSIST,
28-
REBOOT_PERSIST,
29-
REBOOT_BOOTLOADER,
30-
REBOOT_BOOTLOADER_DFU,
31-
REBOOT_TYPE_MAX
32-
} reboot_type_t;
27+
RESTART_NO_PERSIST,
28+
RESTART_PERSIST,
29+
RESTART_BOOTLOADER,
30+
RESTART_BOOTLOADER_DFU,
31+
RESTART_TYPE_MAX
32+
} restart_type_t;
3333

3434
/*
3535
* Init Persistence reboot system
3636
* */
37-
void tinyusb_persist_init(void);
37+
void usb_persist_init(void);
3838

3939
/*
4040
* Enable Persistence reboot
4141
*
4242
* Note: Persistence should be enabled when ONLY CDC and DFU are being used
4343
* */
44-
void tinyusb_persist_set_enable(bool enable);
44+
void usb_persist_set_enable(bool enable);
4545

4646
/*
4747
* Set Reboot mode. Call before esp_reboot();
4848
* */
49-
void tinyusb_persist_set_mode(reboot_type_t mode);
49+
void usb_persist_restart(restart_type_t mode);
50+
51+
/*
52+
* Check if last boot was persistent
53+
* */
54+
bool usb_persist_this_boot(void);
5055

5156
#ifdef __cplusplus
5257
}

tools/sdk/esp32s2/include/tinyusb/tinyusb/hw/bsp/board_mcu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@
113113
#elif CFG_TUSB_MCU == OPT_MCU_ESP32S2
114114
// no header needed
115115

116+
#elif CFG_TUSB_MCU == OPT_MCU_DA1469X
117+
#include "DA1469xAB.h"
118+
116119
#else
117120
#error "Missing MCU header"
118121
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This file was generated by Apache newt version: 1.9.0-dev
3+
*/
4+
5+
#ifndef H_MYNEWT_SYSCFG_
6+
#define H_MYNEWT_SYSCFG_
7+
8+
/**
9+
* This macro exists to ensure code includes this header when needed. If code
10+
* checks the existence of a setting directly via ifdef without including this
11+
* header, the setting macro will silently evaluate to 0. In contrast, an
12+
* attempt to use these macros without including this header will result in a
13+
* compiler error.
14+
*/
15+
#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name
16+
#define MYNEWT_VAL_CHOICE(_name, _val) MYNEWT_VAL_ ## _name ## __ ## _val
17+
18+
#ifndef MYNEWT_VAL_RAM_RESIDENT
19+
#define MYNEWT_VAL_RAM_RESIDENT (0)
20+
#endif
21+
22+
#ifndef MYNEWT_VAL_MCU_GPIO_MAX_IRQ
23+
#define MYNEWT_VAL_MCU_GPIO_MAX_IRQ (4)
24+
#endif
25+
26+
#ifndef MYNEWT_VAL_MCU_GPIO_RETAINABLE_NUM
27+
#define MYNEWT_VAL_MCU_GPIO_RETAINABLE_NUM (-1)
28+
#endif
29+
30+
#ifndef MYNEWT_VAL_MCU_CLOCK_XTAL32M_SETTLE_TIME_US
31+
#define MYNEWT_VAL_MCU_CLOCK_XTAL32M_SETTLE_TIME_US (2000)
32+
#endif
33+
34+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* This file was generated by Apache newt version: 1.9.0-dev
3+
*/
4+
5+
#ifndef H_MYNEWT_SYSCFG_
6+
#define H_MYNEWT_SYSCFG_
7+
8+
/**
9+
* This macro exists to ensure code includes this header when needed. If code
10+
* checks the existence of a setting directly via ifdef without including this
11+
* header, the setting macro will silently evaluate to 0. In contrast, an
12+
* attempt to use these macros without including this header will result in a
13+
* compiler error.
14+
*/
15+
#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name
16+
#define MYNEWT_VAL_CHOICE(_name, _val) MYNEWT_VAL_ ## _name ## __ ## _val
17+
18+
#ifndef MYNEWT_VAL_RAM_RESIDENT
19+
#define MYNEWT_VAL_RAM_RESIDENT (0)
20+
#endif
21+
22+
#ifndef MYNEWT_VAL_MCU_GPIO_MAX_IRQ
23+
#define MYNEWT_VAL_MCU_GPIO_MAX_IRQ (4)
24+
#endif
25+
26+
#ifndef MYNEWT_VAL_MCU_GPIO_RETAINABLE_NUM
27+
#define MYNEWT_VAL_MCU_GPIO_RETAINABLE_NUM (-1)
28+
#endif
29+
30+
#ifndef MYNEWT_VAL_MCU_CLOCK_XTAL32M_SETTLE_TIME_US
31+
#define MYNEWT_VAL_MCU_CLOCK_XTAL32M_SETTLE_TIME_US (2000)
32+
#endif
33+
34+
#endif
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
#include "esp_err.h"
21+
22+
/**
23+
* @brief LED Strip Type
24+
*
25+
*/
26+
typedef struct led_strip_s led_strip_t;
27+
28+
/**
29+
* @brief LED Strip Device Type
30+
*
31+
*/
32+
typedef void *led_strip_dev_t;
33+
34+
/**
35+
* @brief Declare of LED Strip Type
36+
*
37+
*/
38+
struct led_strip_s {
39+
/**
40+
* @brief Set RGB for a specific pixel
41+
*
42+
* @param strip: LED strip
43+
* @param index: index of pixel to set
44+
* @param red: red part of color
45+
* @param green: green part of color
46+
* @param blue: blue part of color
47+
*
48+
* @return
49+
* - ESP_OK: Set RGB for a specific pixel successfully
50+
* - ESP_ERR_INVALID_ARG: Set RGB for a specific pixel failed because of invalid parameters
51+
* - ESP_FAIL: Set RGB for a specific pixel failed because other error occurred
52+
*/
53+
esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);
54+
55+
/**
56+
* @brief Refresh memory colors to LEDs
57+
*
58+
* @param strip: LED strip
59+
* @param timeout_ms: timeout value for refreshing task
60+
*
61+
* @return
62+
* - ESP_OK: Refresh successfully
63+
* - ESP_ERR_TIMEOUT: Refresh failed because of timeout
64+
* - ESP_FAIL: Refresh failed because some other error occurred
65+
*
66+
* @note:
67+
* After updating the LED colors in the memory, a following invocation of this API is needed to flush colors to strip.
68+
*/
69+
esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);
70+
71+
/**
72+
* @brief Clear LED strip (turn off all LEDs)
73+
*
74+
* @param strip: LED strip
75+
* @param timeout_ms: timeout value for clearing task
76+
*
77+
* @return
78+
* - ESP_OK: Clear LEDs successfully
79+
* - ESP_ERR_TIMEOUT: Clear LEDs failed because of timeout
80+
* - ESP_FAIL: Clear LEDs failed because some other error occurred
81+
*/
82+
esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);
83+
84+
/**
85+
* @brief Free LED strip resources
86+
*
87+
* @param strip: LED strip
88+
*
89+
* @return
90+
* - ESP_OK: Free resources successfully
91+
* - ESP_FAIL: Free resources failed because error occurred
92+
*/
93+
esp_err_t (*del)(led_strip_t *strip);
94+
};
95+
96+
/**
97+
* @brief LED Strip Configuration Type
98+
*
99+
*/
100+
typedef struct {
101+
uint32_t max_leds; /*!< Maximum LEDs in a single strip */
102+
led_strip_dev_t dev; /*!< LED strip device (e.g. RMT channel, PWM channel, etc) */
103+
} led_strip_config_t;
104+
105+
/**
106+
* @brief Default configuration for LED strip
107+
*
108+
*/
109+
#define LED_STRIP_DEFAULT_CONFIG(number, dev_hdl) \
110+
{ \
111+
.max_leds = number, \
112+
.dev = dev_hdl, \
113+
}
114+
115+
/**
116+
* @brief Install a new ws2812 driver (based on RMT peripheral)
117+
*
118+
* @param config: LED strip configuration
119+
* @return
120+
* LED strip instance or NULL
121+
*/
122+
led_strip_t *led_strip_new_rmt_ws2812(const led_strip_config_t *config);
123+
124+
#ifdef __cplusplus
125+
}
126+
#endif

tools/sdk/esp32s2/include/tinyusb/tinyusb/src/tusb_option.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define OPT_MCU_SAMD21 200 ///< MicroChip SAMD21
5959
#define OPT_MCU_SAMD51 201 ///< MicroChip SAMD51
6060
#define OPT_MCU_SAMG 202 ///< MicroChip SAMDG series
61+
#define OPT_MCU_SAME5X 203 ///< MicroChip SAM E5x
6162

6263
// STM32
6364
#define OPT_MCU_STM32F0 300 ///< ST STM32F0
@@ -92,6 +93,9 @@
9293
// Espressif
9394
#define OPT_MCU_ESP32S2 900 ///< Espressif ESP32-S2
9495

96+
// Dialog
97+
#define OPT_MCU_DA1469X 1000 ///< Dialog Semiconductor DA1469x
98+
9599
/** @} */
96100

97101
/** \defgroup group_supported_os Supported RTOS

tools/sdk/esp32s2/lib/libtinyusb.a

1.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)