Skip to content

Commit 126115a

Browse files
committed
update repo to work with latest tinyusb and nrfx module
1 parent 3d99a55 commit 126115a

File tree

12 files changed

+210
-356
lines changed

12 files changed

+210
-356
lines changed

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ C_SOURCE_FILES += $(SRC_PATH)/dfu_init.c
108108

109109
# nrfx
110110
C_SOURCE_FILES += $(NRFX_PATH)/drivers/src/nrfx_power.c
111-
C_SOURCE_FILES += $(NRFX_PATH)/hal/nrf_nvmc.c
111+
C_SOURCE_FILES += $(NRFX_PATH)/drivers/src/nrfx_nvmc.c
112112

113113
# SDK 11 files
114114
C_SOURCE_FILES += $(SDK11_PATH)/libraries/bootloader_dfu/bootloader.c
@@ -159,13 +159,11 @@ C_SOURCE_FILES += $(NRFX_PATH)/mdk/system_nrf52840.c
159159

160160
# Tinyusb stack
161161
C_SOURCE_FILES += $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c
162-
C_SOURCE_FILES += $(TUSB_PATH)/portable/nordic/nrf5x/hal_nrf5x.c
163162
C_SOURCE_FILES += $(TUSB_PATH)/common/tusb_fifo.c
164163
C_SOURCE_FILES += $(TUSB_PATH)/device/usbd.c
165164
C_SOURCE_FILES += $(TUSB_PATH)/device/usbd_control.c
166165
C_SOURCE_FILES += $(TUSB_PATH)/class/cdc/cdc_device.c
167166
C_SOURCE_FILES += $(TUSB_PATH)/class/msc/msc_device.c
168-
C_SOURCE_FILES += $(TUSB_PATH)/class/custom/custom_device.c
169167
C_SOURCE_FILES += $(TUSB_PATH)/tusb.c
170168

171169
endif
@@ -198,6 +196,7 @@ IPATH += $(NRFX_PATH)
198196
IPATH += $(NRFX_PATH)/mdk
199197
IPATH += $(NRFX_PATH)/hal
200198
IPATH += $(NRFX_PATH)/drivers/include
199+
IPATH += $(NRFX_PATH)/drivers/src
201200

202201
IPATH += $(SDK11_PATH)/libraries/bootloader_dfu/hci_transport
203202
IPATH += $(SDK11_PATH)/libraries/bootloader_dfu

lib/sdk11/components/libraries/bootloader_dfu/bootloader.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ static void wait_for_events(void)
119119

120120
// Feed all Watchdog just in case application enable it
121121
// WDT cannot be disabled once started. It even last through soft reset (NVIC Reset)
122-
if ( nrf_wdt_started() )
122+
if ( nrf_wdt_started(NRF_WDT) )
123123
{
124-
for (uint8_t i=0; i<8; i++) nrf_wdt_reload_request_set(i);
124+
for (uint8_t i=0; i<8; i++) nrf_wdt_reload_request_set(NRF_WDT, i);
125125
}
126126

127127
// Event received. Process it from the scheduler.
@@ -194,8 +194,8 @@ static void bootloader_settings_save(bootloader_settings_t * p_settings)
194194
}
195195
else
196196
{
197-
nrf_nvmc_page_erase(BOOTLOADER_SETTINGS_ADDRESS);
198-
nrf_nvmc_write_words(BOOTLOADER_SETTINGS_ADDRESS, (uint32_t *) p_settings, sizeof(bootloader_settings_t) / 4);
197+
nrfx_nvmc_page_erase(BOOTLOADER_SETTINGS_ADDRESS);
198+
nrfx_nvmc_words_write(BOOTLOADER_SETTINGS_ADDRESS, (uint32_t *) p_settings, sizeof(bootloader_settings_t) / 4);
199199

200200
pstorage_callback_handler(&m_bootsettings_handle, PSTORAGE_STORE_OP_CODE, NRF_SUCCESS, (uint8_t *) p_settings, sizeof(bootloader_settings_t));
201201
}

lib/sdk11/components/libraries/bootloader_dfu/dfu_single_bank.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static void dfu_prepare_func_app_erase(uint32_t image_size)
151151

152152
for ( uint32_t i = 0; i < page_count; i++ )
153153
{
154-
nrf_nvmc_page_erase(DFU_BANK_0_REGION_START + i * CODE_PAGE_SIZE);
154+
nrfx_nvmc_page_erase(DFU_BANK_0_REGION_START + i * CODE_PAGE_SIZE);
155155
}
156156

157157
// invoke complete callback

src/flash_nrf5x.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void flash_nrf5x_flush (bool need_erase)
4444
// - nRF52840 dfu serial/uf2 are USB-based which are DMA and should have no problems.
4545
//
4646
// Note: MSC uf2 does not erase page in advance like dfu serial
47-
if ( need_erase ) nrf_nvmc_page_erase(_fl_addr);
47+
if ( need_erase ) nrfx_nvmc_page_erase(_fl_addr);
4848

49-
nrf_nvmc_write_words(_fl_addr, (uint32_t *) _fl_buf, FLASH_PAGE_SIZE / 4);
49+
nrfx_nvmc_words_write(_fl_addr, (uint32_t *) _fl_buf, FLASH_PAGE_SIZE / 4);
5050
}
5151

5252
_fl_addr = FLASH_CACHE_INVALID_ADDR;

src/flash_nrf5x.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <stdint.h>
2929
#include <stdbool.h>
3030

31-
#include "nrf_nvmc.h"
31+
#include "nrfx_nvmc.h"
3232

3333
#ifdef __cplusplus
3434
extern "C" {

src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
#include "pstorage_platform.h"
6666
#include "nrf_mbr.h"
6767
#include "pstorage.h"
68+
#include "nrfx_nvmc.h"
6869

69-
#include "nrf_nvmc.h"
7070

7171
#ifdef NRF52840_XXAA
7272
#include "nrf_usbd.h"
@@ -275,11 +275,11 @@ void adafruit_factory_reset(void)
275275
// clear all App Data if any
276276
if ( DFU_APP_DATA_RESERVED )
277277
{
278-
nrf_nvmc_page_erase(APPDATA_ADDR_START);
278+
nrfx_nvmc_page_erase(APPDATA_ADDR_START);
279279
}
280280

281281
// Only need to erase the 1st page of Application code to make it invalid
282-
nrf_nvmc_page_erase(DFU_BANK_0_REGION_START);
282+
nrfx_nvmc_page_erase(DFU_BANK_0_REGION_START);
283283

284284
// back to normal
285285
led_state(STATE_FACTORY_RESET_FINISHED);

src/nrfx_config.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#define NRFX_CONFIG_H__
33

44
// Power
5-
#define NRFX_POWER_ENABLED 1
6-
#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7
5+
#define NRFX_POWER_ENABLED 1
6+
#define NRFX_POWER_DEFAULT_CONFIG_IRQ_PRIORITY 7
7+
8+
#define NRFX_CLOCK_ENABLED 0
9+
10+
#define NRFX_NVMC_ENABLED 1
711

812
// UART
913
#ifdef NRF52832_XXAA
10-
#define NRFX_UART_ENABLED 1
11-
#define NRFX_UART0_ENABLED 1
14+
#define NRFX_UART_ENABLED 1
15+
#define NRFX_UART0_ENABLED 1
1216

1317
#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 7
1418
#define NRFX_UART_DEFAULT_CONFIG_HWFC NRF_UART_HWFC_DISABLED

src/usb/msc_uf2.c

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,47 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet, WriteState *state)
4545
// tinyusb callbacks
4646
//--------------------------------------------------------------------+
4747

48+
// Invoked when received SCSI_CMD_INQUIRY
49+
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
50+
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
51+
{
52+
(void) lun;
53+
54+
const char vid[] = "Adafruit";
55+
const char pid[] = "Bluefruit UF2";
56+
const char rev[] = "1.0";
57+
58+
memcpy(vendor_id , vid, strlen(vid));
59+
memcpy(product_id , pid, strlen(pid));
60+
memcpy(product_rev, rev, strlen(rev));
61+
}
62+
63+
// Invoked when received Test Unit Ready command.
64+
// return true allowing host to read/write this LUN e.g SD card inserted
65+
bool tud_msc_test_unit_ready_cb(uint8_t lun)
66+
{
67+
(void) lun;
68+
return true;
69+
}
70+
4871
// Callback invoked when received an SCSI command not in built-in list below
4972
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
5073
// - READ10 and WRITE10 has their own callbacks
5174
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
5275
{
5376
void const* response = NULL;
54-
int32_t resplen = 0;
55-
memset(buffer, 0, bufsize);
77+
uint16_t resplen = 0;
5678

57-
switch ( scsi_cmd[0] )
58-
{
59-
case SCSI_CMD_TEST_UNIT_READY:
60-
// Command that host uses to check our readiness before sending other commands
61-
resplen = 0;
62-
break;
79+
// most scsi handled is input
80+
bool in_xfer = true;
6381

82+
switch (scsi_cmd[0])
83+
{
6484
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
6585
// Host is about to read/write etc ... better not to disconnect disk
6686
resplen = 0;
6787
break;
6888

69-
case SCSI_CMD_START_STOP_UNIT:
70-
// Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power
71-
/* scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd;
72-
// Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well
73-
// Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage
74-
start_stop->start;
75-
start_stop->load_eject;
76-
*/
77-
resplen = 0;
78-
break;
79-
8089
default:
8190
// Set Sense = Invalid Command Operation
8291
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
@@ -86,13 +95,18 @@ int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer,
8695
break;
8796
}
8897

89-
// return len must not larger than bufsize
90-
if ( resplen > (int32_t)bufsize ) resplen = bufsize;
98+
// return resplen must not larger than bufsize
99+
if ( resplen > bufsize ) resplen = bufsize;
91100

92-
// copy response to stack's buffer if any
93-
if ( response && resplen )
101+
if ( response && (resplen > 0) )
94102
{
95-
memcpy(buffer, response, resplen);
103+
if(in_xfer)
104+
{
105+
memcpy(buffer, response, resplen);
106+
}else
107+
{
108+
// SCSI output
109+
}
96110
}
97111

98112
return resplen;
@@ -161,6 +175,8 @@ void tud_msc_write10_complete_cb(uint8_t lun)
161175
}
162176
}
163177

178+
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
179+
// Application update block count and block size
164180
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
165181
{
166182
(void) lun;
@@ -169,4 +185,26 @@ void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_siz
169185
*block_size = 512;
170186
}
171187

188+
// Invoked when received Start Stop Unit command
189+
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
190+
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
191+
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
192+
{
193+
(void) lun;
194+
(void) power_condition;
195+
196+
if ( load_eject )
197+
{
198+
if (start)
199+
{
200+
// load disk storage
201+
}else
202+
{
203+
// unload disk storage
204+
}
205+
}
206+
207+
return true;
208+
}
209+
172210
#endif

src/usb/tusb_config.h

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@
5858
//------------- Class enabled -------------//
5959
#define CFG_TUD_CDC 1
6060
#define CFG_TUD_MSC 1
61-
#define CFG_TUD_HID_KEYBOARD 0
62-
#define CFG_TUD_HID_MOUSE 0
63-
#define CFG_TUD_HID_GENERIC 0 // not supported yet
64-
#define CFG_TUD_CUSTOM_CLASS 0
65-
6661

6762
/*------------------------------------------------------------------*/
6863
/* CLASS DRIVER
@@ -72,34 +67,15 @@
7267
#define CFG_TUD_CDC_RX_BUFSIZE 1024
7368
#define CFG_TUD_CDC_TX_BUFSIZE 1024
7469

75-
/* TX is sent automatically on every Start of Frame event ~ 1ms.
76-
* If not enabled, application must call tud_cdc_flush() periodically
77-
* Note: Enabled this could overflow device task, if it does, define
78-
* CFG_TUD_TASK_QUEUE_SZ with large value
79-
*/
80-
#define CFG_TUD_CDC_FLUSH_ON_SOF 0
81-
82-
// Number of supported Logical Unit Number
83-
#define CFG_TUD_MSC_MAXLUN 1
84-
8570
// Buffer size for each read/write transfer, the more the better
8671
#define CFG_TUD_MSC_BUFSIZE (4*1024)
8772

88-
// Vendor name included in Inquiry response, max 8 bytes
89-
#define CFG_TUD_MSC_VENDOR "Adafruit"
90-
91-
// Product name included in Inquiry response, max 16 bytes
92-
#define CFG_TUD_MSC_PRODUCT "Feather nRF52840"
93-
94-
// Product revision string included in Inquiry response, max 4 bytes
95-
#define CFG_TUD_MSC_PRODUCT_REV "1.0"
96-
9773

9874
//--------------------------------------------------------------------+
9975
// USB RAM PLACEMENT
10076
//--------------------------------------------------------------------+
10177
#define CFG_TUSB_ATTR_USBRAM
102-
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4)
78+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
10379

10480

10581
#define BREAKPOINT_IGNORE_COUNT(n) \

src/usb/usb.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@
4040
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
4141
//--------------------------------------------------------------------+
4242

43-
// from usb_desc.c for dynamic descriptor
44-
extern tusb_desc_device_t usb_desc_dev;
45-
extern usb_desc_cfg_t usb_desc_cfg;
46-
4743
// Serial string using unique Device ID
48-
extern uint16_t usb_desc_str_serial[1+16];
44+
extern char usb_desc_str_serial[1+16];
4945

5046
/* tinyusb function that handles power event (detected, ready, removed)
5147
* We must call it within SD's SOC event handler, or set it as power event handler if SD is not enabled. */
@@ -94,24 +90,10 @@ void usb_init(bool cdc_only)
9490
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_READY);
9591
}
9692

97-
if ( cdc_only )
98-
{
99-
// Change PID to CDC only
100-
usb_desc_dev.idProduct = USB_DESC_CDC_ONLY_PID;
101-
102-
// Remove MSC interface = reduce total interface + adjust config desc length
103-
usb_desc_cfg.config.bNumInterfaces--;
104-
usb_desc_cfg.config.wTotalLength -= sizeof(usb_desc_cfg.msc);
105-
}
93+
usb_desc_set_mode(cdc_only);
10694

10795
// Create Serial string descriptor
108-
char tmp_serial[17];
109-
sprintf(tmp_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
110-
111-
for(uint8_t i=0; i<16; i++)
112-
{
113-
usb_desc_str_serial[1+i] = tmp_serial[i];
114-
}
96+
sprintf(usb_desc_str_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
11597

11698
// Init tusb stack
11799
tusb_init();
@@ -124,15 +106,15 @@ void usb_teardown(void)
124106
// Abort all transfers
125107

126108
// Disable pull up
127-
nrf_usbd_pullup_disable();
109+
nrf_usbd_pullup_disable(NRF_USBD);
128110

129111
// Disable Interrupt
130112
NVIC_DisableIRQ(USBD_IRQn);
131113

132114
// disable all interrupt
133115
NRF_USBD->INTENCLR = NRF_USBD->INTEN;
134116

135-
nrf_usbd_disable();
117+
nrf_usbd_disable(NRF_USBD);
136118
sd_clock_hfclk_release();
137119

138120
sd_power_usbdetected_enable(false);

0 commit comments

Comments
 (0)