Skip to content

Commit 3525d65

Browse files
committed
should fix adafruit#1021
- update tinyusb for wanted char - move usb code into usb.c
1 parent bfe14ff commit 3525d65

File tree

8 files changed

+154
-53
lines changed

8 files changed

+154
-53
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ SRC_C += \
122122
ifeq ($(MCU_SUB_VARIANT),nrf52840)
123123

124124
SRC_C += \
125+
usb/usb.c \
125126
usb/tusb_descriptors.c \
126127
usb/usb_msc_flash.c \
127128
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \

ports/nrf/README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,10 @@ Prerequisite steps for building the nrf port:
5151
git submodule update --init
5252
make -C mpy-cross
5353

54-
By default, the feather52832 is used as compile target. To build and flash issue the following command inside the ports/nrf/ folder:
54+
To build and flash issue the following command inside the ports/nrf/ folder:
5555

56-
make
57-
make flash
58-
59-
Alternatively the target board could be defined:
60-
61-
make BOARD=pca10056
62-
make flash
56+
make BOARD=pca10056
57+
make BOARD=pca10056 flash
6358

6459
## Compile and Flash with Bluetooth Stack
6560

ports/nrf/background.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
void run_background_tasks(void) {
3030
#ifdef NRF52840_XXAA
3131
tusb_task();
32-
tud_cdc_flush();
32+
tud_cdc_write_flush();
3333
#endif
3434
}

ports/nrf/boards/pca10056/board.c

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,17 @@
2626

2727
#include <string.h>
2828
#include <stdbool.h>
29-
30-
#include "nrfx.h"
31-
#include "nrfx_power.h"
3229
#include "boards/board.h"
33-
34-
#include "tick.h"
35-
#include "tusb.h"
30+
#include "nrfx.h"
31+
#include "usb.h"
3632

3733
void board_init(void) {
3834

3935
// Clock
4036
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
4137
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
4238

43-
// Init USB
44-
45-
#ifdef SOFTDEVICE_PRESENT
46-
// TODO support Softdevice config
47-
#else
48-
// Softdevice is not present, init power module and register tusb power event function
49-
// for vusb detect, ready, removed
50-
extern void tusb_hal_nrf_power_event(uint32_t event);
51-
52-
// Power module init
53-
const nrfx_power_config_t pwr_cfg = { 0 };
54-
nrfx_power_init(&pwr_cfg);
55-
56-
// USB Power detection
57-
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
58-
nrfx_power_usbevt_init(&config);
59-
60-
nrfx_power_usbevt_enable();
61-
#endif
62-
63-
tusb_init();
39+
usb_init();
6440
}
6541

6642
bool board_requests_safe_mode(void) {
@@ -72,21 +48,6 @@ void reset_board(void) {
7248
}
7349

7450

75-
//--------------------------------------------------------------------+
76-
// tinyusb callbacks
77-
//--------------------------------------------------------------------+
78-
void tud_mount_cb(uint8_t rhport) {
79-
(void) rhport;
80-
}
8151

82-
void tud_umount_cb(uint8_t rhport) {
83-
(void) rhport;
84-
}
8552

86-
uint32_t tusb_hal_millis(void) {
87-
uint64_t ms;
88-
uint32_t us;
89-
current_tick(&ms, &us);
90-
return (uint32_t) ms;
91-
}
9253

ports/nrf/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ extern const struct _mp_obj_module_t mp_module_ubluepy;
232232
// We need to provide a declaration/definition of alloca()
233233
#include <alloca.h>
234234

235-
extern void run_background_tasks(void);
235+
void run_background_tasks(void);
236236
#define MICROPY_VM_HOOK_LOOP run_background_tasks();
237237
#define MICROPY_VM_HOOK_RETURN run_background_tasks();
238238

ports/nrf/usb/usb.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 hathach for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "nrfx.h"
28+
#include "nrfx_power.h"
29+
#include "tick.h"
30+
#include "usb.h"
31+
32+
#include "lib/mp-readline/readline.h"
33+
#include "lib/utils/interrupt_char.h"
34+
35+
#ifdef NRF52840_XXAA
36+
37+
void usb_init(void) {
38+
39+
#ifdef SOFTDEVICE_PRESENT
40+
// TODO support Softdevice config
41+
#else
42+
// Softdevice is not present, init power module and register tusb power event function
43+
// for vusb detect, ready, removed
44+
extern void tusb_hal_nrf_power_event(uint32_t event);
45+
46+
// Power module init
47+
const nrfx_power_config_t pwr_cfg = { 0 };
48+
nrfx_power_init(&pwr_cfg);
49+
50+
// USB Power detection
51+
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
52+
nrfx_power_usbevt_init(&config);
53+
54+
nrfx_power_usbevt_enable();
55+
#endif
56+
57+
tusb_init();
58+
59+
#if MICROPY_KBD_EXCEPTION
60+
// set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() fired when Ctrl+C is received
61+
// TODO should be called in mp_hal_set_interrupt_char()
62+
tud_cdc_set_wanted_char(CHAR_CTRL_C);
63+
#endif
64+
}
65+
66+
//--------------------------------------------------------------------+
67+
// tinyusb callbacks
68+
//--------------------------------------------------------------------+
69+
void tud_mount_cb(void) {
70+
}
71+
72+
void tud_umount_cb(void) {
73+
}
74+
75+
uint32_t tusb_hal_millis(void) {
76+
uint64_t ms;
77+
uint32_t us;
78+
current_tick(&ms, &us);
79+
return (uint32_t) ms;
80+
}
81+
82+
#if MICROPY_KBD_EXCEPTION
83+
84+
/**
85+
* Callback invoked when received an "wanted" char.
86+
* @param itf Interface index (for multiple cdc interfaces)
87+
* @param wanted_char The wanted char (set previously)
88+
*/
89+
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
90+
{
91+
(void) itf; // not used
92+
93+
if (mp_interrupt_char == wanted_char) {
94+
tud_cdc_read_flush(); // flush read fifo
95+
mp_keyboard_interrupt();
96+
}
97+
}
98+
99+
#endif
100+
101+
#endif
102+

ports/nrf/usb/usb.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 hathach for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_NRF_USB_H
28+
#define MICROPY_INCLUDED_NRF_USB_H
29+
30+
#include "tusb.h"
31+
32+
#ifdef NRF52840_XXAA
33+
34+
void usb_init(void);
35+
36+
#else
37+
38+
#define usb_init()
39+
40+
#endif
41+
42+
#endif // MICROPY_INCLUDED_NRF_USB_H

0 commit comments

Comments
 (0)