Skip to content

Commit 66b6712

Browse files
authored
Merge pull request #223 from adafruit/enhance
only try to detect double reset when reset by nRST
2 parents 3d04d61 + 0d97c21 commit 66b6712

File tree

7 files changed

+108
-71
lines changed

7 files changed

+108
-71
lines changed

Makefile

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ endif
124124
#------------------------------------------------------------------------------
125125

126126
# all files in src
127-
C_SRC += $(wildcard src/*.c)
127+
C_SRC += \
128+
src/dfu_ble_svc.c \
129+
src/dfu_init.c \
130+
src/flash_nrf5x.c \
131+
src/main.c \
128132

129133
# all files in boards
130-
C_SRC += $(wildcard src/boards/*.c)
131-
132-
# all sources files in specific board
133-
C_SRC += $(wildcard src/boards/$(BOARD)/*.c)
134+
C_SRC += src/boards/boards.c
134135

135136
# nrfx
136137
C_SRC += $(NRFX_PATH)/drivers/src/nrfx_power.c
@@ -172,18 +173,25 @@ IPATH += $(SDK_PATH)/drivers_nrf/uart
172173

173174
else
174175

176+
# pinconfig is required for 840 for CF2
177+
C_SRC += src/boards/$(BOARD)/pinconfig.c
178+
175179
# USB Application ( MSC + UF2 )
176-
C_SRC += $(wildcard src/usb/*.c)
177-
C_SRC += $(wildcard src/usb/uf2/*.c)
180+
C_SRC += \
181+
src/usb/msc_uf2.c \
182+
src/usb/usb_desc.c \
183+
src/usb/usb.c \
184+
src/usb/uf2/ghostfat.c
178185

179186
# TinyUSB stack
180-
C_SRC += $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c
181-
C_SRC += $(TUSB_PATH)/common/tusb_fifo.c
182-
C_SRC += $(TUSB_PATH)/device/usbd.c
183-
C_SRC += $(TUSB_PATH)/device/usbd_control.c
184-
C_SRC += $(TUSB_PATH)/class/cdc/cdc_device.c
185-
C_SRC += $(TUSB_PATH)/class/msc/msc_device.c
186-
C_SRC += $(TUSB_PATH)/tusb.c
187+
C_SRC += \
188+
$(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c \
189+
$(TUSB_PATH)/common/tusb_fifo.c \
190+
$(TUSB_PATH)/device/usbd.c \
191+
$(TUSB_PATH)/device/usbd_control.c \
192+
$(TUSB_PATH)/class/cdc/cdc_device.c \
193+
$(TUSB_PATH)/class/msc/msc_device.c \
194+
$(TUSB_PATH)/tusb.c
187195

188196
endif
189197

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
736736
ble_gatts_rw_authorize_reply_params_t auth_reply;
737737

738738
#ifdef CFG_DEBUG
739-
extern const char* dbg_ble_event_str(uint16_t evt_id);
740-
PRINTF("BLE %s\r\n", dbg_ble_event_str(p_ble_evt->header.evt_id));
739+
extern void print_ble_event(uint16_t evt_id);
740+
print_ble_event(p_ble_evt->header.evt_id);
741741
#endif
742742

743743
switch (p_ble_evt->header.evt_id)
@@ -1085,6 +1085,7 @@ uint32_t dfu_transport_ble_close()
10851085

10861086
#ifdef CFG_DEBUG
10871087

1088+
#if 0
10881089
typedef struct
10891090
{
10901091
uint32_t key;
@@ -1173,18 +1174,22 @@ lookup_table_t const _strevt_table =
11731174
.items = _strevt_lookup
11741175
};
11751176

1176-
const char* dbg_ble_event_str(uint16_t evt_id)
1177-
{
1178-
const char * str = (const char *) lookup_find(&_strevt_table, evt_id);
1179-
static char unknown_str[7] = {0};
1177+
#endif
11801178

1179+
void print_ble_event(uint16_t evt_id)
1180+
{
1181+
// const char * str = (const char *) lookup_find(&_strevt_table, evt_id);
1182+
const char * str = NULL;
1183+
PRINTF("BLE event: ");
11811184
if ( str == NULL )
11821185
{
1183-
sprintf(unknown_str, "0x%04X", evt_id);
1184-
str = unknown_str;
1186+
PRINTF("0x%04X", evt_id);
1187+
}else
1188+
{
1189+
PRINTF(str);
11851190
}
1186-
1187-
return str;
1191+
PRINTF("\r\n");
11881192
}
11891193

1194+
11901195
#endif

src/boards/electronut_labs_papyr/board.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
#define UF2_BOARD_ID "nRF52840-Papyr-v1"
6161
#define UF2_INDEX_URL "https://docs.electronut.in/papyr"
6262

63-
#define USB_DESC_VID 0x239A
64-
#define USB_DESC_UF2_PID 0x003B
63+
#define USB_DESC_VID 0x239A
64+
#define USB_DESC_UF2_PID 0x003B
65+
#define USB_DESC_CDC_ONLY_PID 0x003B
6566

6667
#endif // PPAPYR_H

src/main.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,15 @@ static void check_dfu_mode(void)
238238
_ota_dfu = (gpregret == DFU_MAGIC_OTA_APPJUM) || (gpregret == DFU_MAGIC_OTA_RESET);
239239

240240
// Serial only mode
241-
bool serial_only_dfu = (gpregret == DFU_MAGIC_SERIAL_ONLY_RESET);
242-
bool uf2_dfu = (gpregret == DFU_MAGIC_UF2_RESET);
241+
bool const serial_only_dfu = (gpregret == DFU_MAGIC_SERIAL_ONLY_RESET);
242+
bool const uf2_dfu = (gpregret == DFU_MAGIC_UF2_RESET);
243+
bool const dfu_skip = (gpregret == DFU_MAGIC_SKIP);
244+
245+
bool const reason_reset_pin = (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk) ? true : false;
243246

244247
// start either serial, uf2 or ble
245248
bool dfu_start = _ota_dfu || serial_only_dfu || uf2_dfu ||
246-
(((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk));
247-
248-
bool const dfu_skip = (gpregret == DFU_MAGIC_SKIP);
249+
(((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && reason_reset_pin);
249250

250251
// Clear GPREGRET if it is our values
251252
if (dfu_start || dfu_skip) NRF_POWER->GPREGRET = 0;
@@ -265,12 +266,9 @@ static void check_dfu_mode(void)
265266

266267
if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) dfu_start = 1;
267268

268-
// App mode: register 1st reset or DFU startup for nrf52832
269+
// App mode: Double Reset detection or DFU startup for nrf52832
269270
if ( ! (just_start_app || dfu_start || !valid_app) )
270271
{
271-
// Register our first reset for double reset detection
272-
(*dbl_reset_mem) = DFU_DBL_RESET_MAGIC;
273-
274272
#ifdef NRF52832_XXAA
275273
/* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup
276274
* to support auto programming from Arduino IDE
@@ -279,8 +277,15 @@ static void check_dfu_mode(void)
279277
*/
280278
bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL, false);
281279
#else
282-
// if RST is pressed during this delay (double reset)--> if will enter dfu
283-
NRFX_DELAY_MS(DFU_DBL_RESET_DELAY);
280+
// Note: RESETREAS is not clear by bootloader, it should be cleared by application upon init()
281+
if (reason_reset_pin)
282+
{
283+
// Register our first reset for double reset detection
284+
(*dbl_reset_mem) = DFU_DBL_RESET_MAGIC;
285+
286+
// if RST is pressed during this delay (double reset)--> if will enter dfu
287+
NRFX_DELAY_MS(DFU_DBL_RESET_DELAY);
288+
}
284289
#endif
285290
}
286291

src/usb/uf2/ghostfat.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "flash_nrf5x.h"
3232
#include <string.h>
3333
#include <stdio.h>
34+
#include <stdlib.h>
3435

3536
#include "bootloader_settings.h"
3637
#include "bootloader.h"
@@ -119,7 +120,8 @@ STATIC_ASSERT(FAT_ENTRIES_PER_SECTOR == 256); // FAT
119120
char infoUf2File[128*3] =
120121
"UF2 Bootloader " UF2_VERSION "\r\n"
121122
"Model: " UF2_PRODUCT_NAME "\r\n"
122-
"Board-ID: " UF2_BOARD_ID "\r\n";
123+
"Board-ID: " UF2_BOARD_ID "\r\n"
124+
"Date: " __DATE__ "\r\n";
123125

124126
const char indexFile[] =
125127
"<!doctype html>\n"
@@ -250,17 +252,33 @@ void uf2_init(void)
250252
uint32_t const sd_id = SD_ID_GET(MBR_SIZE);
251253
uint32_t const sd_version = SD_VERSION_GET(MBR_SIZE);
252254

253-
uint32_t const ver1 = sd_version / 1000000;
254-
uint32_t const ver2 = (sd_version % 1000000)/1000;
255-
uint32_t const ver3 = sd_version % 1000;
255+
uint32_t ver[3];
256+
ver[0] = sd_version / 1000000;
257+
ver[1] = (sd_version - ver[0]*1000000)/1000;
258+
ver[2] = (sd_version - ver[0]*1000000 - ver[1]*1000);
256259

257-
sprintf(infoUf2File + strlen(infoUf2File), "S%lu version %lu.%lu.%lu\r\n", sd_id, ver1, ver2, ver3);
260+
char str[10];
261+
utoa(sd_id, str, 10);
262+
263+
strcat(infoUf2File, "S");
264+
strcat(infoUf2File, str);
265+
strcat(infoUf2File, " ");
266+
267+
utoa(ver[0], str, 10);
268+
strcat(infoUf2File, str);
269+
strcat(infoUf2File, ".");
270+
271+
utoa(ver[1], str, 10);
272+
strcat(infoUf2File, str);
273+
strcat(infoUf2File, ".");
274+
275+
utoa(ver[2], str, 10);
276+
strcat(infoUf2File, str);
277+
strcat(infoUf2File, "\r\n");
258278
}else
259279
{
260280
strcat(infoUf2File, "not found\r\n");
261281
}
262-
263-
strcat(infoUf2File, "Date: " __DATE__ "\r\n");
264282
}
265283

266284
/*------------------------------------------------------------------*/

src/usb/usb_desc.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
#include "usb_desc.h"
2626

2727
enum {
28-
ITF_STR_LANGUAGE = 0 ,
29-
ITF_STR_MANUFACTURER ,
30-
ITF_STR_PRODUCT ,
31-
ITF_STR_SERIAL ,
32-
ITF_STR_CDC ,
33-
ITF_STR_MSC
28+
STRID_LANGUAGE = 0 ,
29+
STRID_MANUFACTURER ,
30+
STRID_PRODUCT ,
31+
STRID_SERIAL ,
32+
STRID_CDC ,
33+
STRID_MSC
3434
};
3535

3636
// CDC + MSC or CDC only mode
@@ -84,25 +84,25 @@ enum {
8484
ITF_NUM_TOTAL
8585
};
8686

87-
uint8_t const desc_configuration_cdc_msc[] =
87+
uint8_t desc_configuration_cdc_msc[] =
8888
{
8989
// Interface count, string index, total length, attribute, power in mA
90-
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
90+
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN, 0, 100),
9191

9292
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
93-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64),
93+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64),
9494

9595
// Interface number, string index, EP Out & EP In address, EP size
96-
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, ITF_STR_MSC, 0x03, 0x83, 64),
96+
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, STRID_MSC, 0x03, 0x83, 64),
9797
};
9898

99-
uint8_t const desc_configuration_cdc_only[] =
99+
uint8_t desc_configuration_cdc_only[] =
100100
{
101101
// Interface count, string index, total length, attribute, power in mA
102-
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL-1, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
102+
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL-1, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, 0, 100),
103103

104104
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
105-
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64),
105+
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64),
106106
};
107107

108108

@@ -127,7 +127,19 @@ void usb_desc_init(bool cdc_only)
127127
}
128128

129129
// Create Serial string descriptor
130-
sprintf(desc_str_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]);
130+
uint8_t const* device_id = (uint8_t const*) &NRF_FICR->DEVICEID;
131+
132+
for ( uint8_t i = 0; i < 8; i++ )
133+
{
134+
for ( uint8_t j = 0; j < 2; j++ )
135+
{
136+
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
137+
138+
uint8_t nibble = (device_id[i] >> (j * 4)) & 0xf;
139+
desc_str_serial[15 - (i * 2 + j)] = nibble_to_hex[nibble]; // memory is little endian
140+
}
141+
}
142+
desc_str_serial[16] = 0;
131143
}
132144

133145
//--------------------------------------------------------------------+
@@ -156,7 +168,7 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
156168

157169
uint8_t chr_count;
158170

159-
if ( index == 0)
171+
if ( index == STRID_LANGUAGE )
160172
{
161173
memcpy(&_desc_str[1], string_desc_arr[0], 2);
162174
chr_count = 1;

src/usb/usb_desc.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,4 @@
3030

3131
void usb_desc_init(bool cdc_only);
3232

33-
#ifndef USB_DESC_VID
34-
#define USB_DESC_VID 0x239A
35-
#endif
36-
37-
#ifndef USB_DESC_UF2_PID
38-
#define USB_DESC_UF2_PID 0x0029
39-
#endif
40-
41-
#ifndef USB_DESC_CDC_ONLY_PID
42-
#define USB_DESC_CDC_ONLY_PID 0x002A
43-
#endif
44-
4533
#endif /* USB_DESC_H_ */

0 commit comments

Comments
 (0)