Skip to content

Commit bb28faf

Browse files
committed
arturo182 review
1 parent a0b9871 commit bb28faf

File tree

4 files changed

+46
-23
lines changed

4 files changed

+46
-23
lines changed

lib/tinyusb

ports/nrf/interrupt_char.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626

2727
#include "py/obj.h"
2828
#include "py/mpstate.h"
29+
30+
#ifdef NRF52840_XXAA
2931
#include "usb.h"
32+
#endif
3033

3134
#if MICROPY_KBD_EXCEPTION
3235

ports/nrf/usb/usb.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
#include "usb.h"
3131
#include "lib/utils/interrupt_char.h"
3232

33-
#ifdef NRF52840_XXAA
33+
#ifdef SOFTDEVICE_PRESENT
34+
#include "nrf_sdm.h"
35+
#include "nrf_soc.h"
36+
#endif
3437

3538
//--------------------------------------------------------------------+
3639
// STRING DESCRIPTORS
@@ -71,26 +74,53 @@ tud_desc_set_t tud_desc_set =
7174
.hid_report = NULL
7275
};
7376

77+
78+
/* tinyusb function that handles power event (detected, ready, removed)
79+
* We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
80+
*/
81+
extern void tusb_hal_nrf_power_event(uint32_t event);
82+
7483
void usb_init(void) {
7584

85+
// USB power may already be ready at this time -> no event generated
86+
// We need to invoke the handler based on the status initially
87+
uint32_t usb_reg;
88+
7689
#ifdef SOFTDEVICE_PRESENT
77-
// TODO support Softdevice config
90+
uint8_t sd_en = false;
91+
(void) sd_softdevice_is_enabled(&sd_en);
92+
93+
if ( sd_en ) {
94+
sd_power_usbdetected_enable(true);
95+
sd_power_usbpwrrdy_enable(true);
96+
sd_power_usbremoved_enable(true);
97+
98+
sd_power_usbregstatus_get(&usb_reg);
99+
}else
78100
#else
79-
// Softdevice is not present, init power module and register tusb power event function
80-
// for vusb detect, ready, removed
81-
extern void tusb_hal_nrf_power_event(uint32_t event);
101+
{
102+
// Power module init
103+
const nrfx_power_config_t pwr_cfg = { 0 };
104+
nrfx_power_init(&pwr_cfg);
82105

83-
// Power module init
84-
const nrfx_power_config_t pwr_cfg = { 0 };
85-
nrfx_power_init(&pwr_cfg);
106+
// Register tusb function as USB power handler
107+
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
108+
nrfx_power_usbevt_init(&config);
86109

87-
// USB Power detection
88-
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
89-
nrfx_power_usbevt_init(&config);
110+
nrfx_power_usbevt_enable();
90111

91-
nrfx_power_usbevt_enable();
112+
usb_reg = NRF_POWER->USBREGSTATUS;
113+
}
92114
#endif
93115

116+
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) {
117+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
118+
}
119+
120+
if ( usb_reg & POWER_USBREGSTATUS_OUTPUTRDY_Msk ) {
121+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
122+
}
123+
94124
tusb_init();
95125
}
96126

@@ -129,5 +159,3 @@ void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
129159

130160
#endif
131161

132-
#endif // NRF52840_XXAA
133-

ports/nrf/usb/usb.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,8 @@
2727
#ifndef MICROPY_INCLUDED_NRF_USB_H
2828
#define MICROPY_INCLUDED_NRF_USB_H
2929

30-
#ifdef NRF52840_XXAA
31-
3230
#include "tusb.h"
3331

3432
void usb_init(void);
3533

36-
#else
37-
38-
#define usb_init()
39-
40-
#endif
41-
4234
#endif // MICROPY_INCLUDED_NRF_USB_H

0 commit comments

Comments
 (0)