Skip to content

Commit cc7d439

Browse files
committed
tested working with nrf
1 parent 485b51a commit cc7d439

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

.github/workflows/githubci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ jobs:
4545
uses: actions/setup-python@v1
4646
with:
4747
python-version: '3.x'
48+
4849
- name: Checkout code
4950
uses: actions/checkout@v2
51+
5052
- name: Checkout adafruit/ci-arduino
5153
uses: actions/checkout@v2
5254
with:
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019, hathach for Adafruit
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#if defined ARDUINO_NRF52_ADAFRUIT && defined USE_TINYUSB
26+
27+
#include "nrfx.h"
28+
#include "nrfx_power.h"
29+
30+
#include "Arduino.h"
31+
#include "arduino/Adafruit_USBD_Device.h"
32+
33+
//--------------------------------------------------------------------+
34+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
35+
//--------------------------------------------------------------------+
36+
37+
#define USBD_STACK_SZ (200)
38+
39+
// tinyusb function that handles power event (detected, ready, removed)
40+
// We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled.
41+
extern "C" void tusb_hal_nrf_power_event(uint32_t event);
42+
43+
//--------------------------------------------------------------------+
44+
// Forward USB interrupt events to TinyUSB IRQ Handler
45+
//--------------------------------------------------------------------+
46+
extern "C" void USBD_IRQHandler(void)
47+
{
48+
#if CFG_SYSVIEW
49+
SEGGER_SYSVIEW_RecordEnterISR();
50+
#endif
51+
52+
tud_int_handler(0);
53+
54+
#if CFG_SYSVIEW
55+
SEGGER_SYSVIEW_RecordExitISR();
56+
#endif
57+
}
58+
59+
//--------------------------------------------------------------------+
60+
// Porting API
61+
//--------------------------------------------------------------------+
62+
63+
static void usb_hardware_init(void);
64+
65+
// USB Device Driver task
66+
// This top level thread process all usb events and invoke callbacks
67+
static void usb_device_task(void* param)
68+
{
69+
(void) param;
70+
71+
// RTOS forever loop
72+
while (1)
73+
{
74+
tud_task();
75+
76+
vTaskDelay(1);
77+
}
78+
}
79+
80+
void TinyUSB_Port_InitDeviceController(uint8_t rhport)
81+
{
82+
(void) rhport;
83+
84+
usb_hardware_init();
85+
86+
// Create a task for tinyusb device stack
87+
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
88+
}
89+
90+
void TinyUSB_Port_EnterDFU(void)
91+
{
92+
enterSerialDfu();
93+
}
94+
95+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16])
96+
{
97+
uint32_t* serial_32 = (uint32_t*) serial_id;
98+
99+
serial_32[0] = __builtin_bswap32(NRF_FICR->DEVICEID[1]);
100+
serial_32[1] = __builtin_bswap32(NRF_FICR->DEVICEID[0]);
101+
102+
return 8;
103+
}
104+
105+
//--------------------------------------------------------------------+
106+
// Helper
107+
//--------------------------------------------------------------------+
108+
109+
// Init usb hardware when starting up. Softdevice is not enabled yet
110+
static void usb_hardware_init(void)
111+
{
112+
// Priorities 0, 1, 4 (nRF52) are reserved for SoftDevice
113+
// 2 is highest for application
114+
NVIC_SetPriority(USBD_IRQn, 2);
115+
116+
// USB power may already be ready at this time -> no event generated
117+
// We need to invoke the handler based on the status initially
118+
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
119+
120+
// Power module init
121+
const nrfx_power_config_t pwr_cfg = { 0 };
122+
nrfx_power_init(&pwr_cfg);
123+
124+
// Register tusb function as USB power handler
125+
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
126+
nrfx_power_usbevt_init(&config);
127+
128+
nrfx_power_usbevt_enable();
129+
130+
if ( usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk ) tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_DETECTED);
131+
}
132+
133+
#endif // USE_TINYUSB

src/arduino/ports/Adafruit_TinyUSB_samd.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ extern "C" int serial1_printf(const char *__restrict format, ...)
8383
//--------------------------------------------------------------------+
8484
void TinyUSB_Port_InitDeviceController(uint8_t rhport)
8585
{
86+
(void) rhport;
87+
8688
/* Enable USB clock */
8789
#if defined(__SAMD51__)
8890
MCLK->APBBMASK.reg |= MCLK_APBBMASK_USB;

0 commit comments

Comments
 (0)