diff --git a/Makefile b/Makefile index 5f9ba98..4a4efa9 100644 --- a/Makefile +++ b/Makefile @@ -124,13 +124,14 @@ endif #------------------------------------------------------------------------------ # all files in src -C_SRC += $(wildcard src/*.c) +C_SRC += \ + src/dfu_ble_svc.c \ + src/dfu_init.c \ + src/flash_nrf5x.c \ + src/main.c \ # all files in boards -C_SRC += $(wildcard src/boards/*.c) - -# all sources files in specific board -C_SRC += $(wildcard src/boards/$(BOARD)/*.c) +C_SRC += src/boards/boards.c # nrfx C_SRC += $(NRFX_PATH)/drivers/src/nrfx_power.c @@ -172,18 +173,25 @@ IPATH += $(SDK_PATH)/drivers_nrf/uart else +# pinconfig is required for 840 for CF2 +C_SRC += src/boards/$(BOARD)/pinconfig.c + # USB Application ( MSC + UF2 ) -C_SRC += $(wildcard src/usb/*.c) -C_SRC += $(wildcard src/usb/uf2/*.c) +C_SRC += \ + src/usb/msc_uf2.c \ + src/usb/usb_desc.c \ + src/usb/usb.c \ + src/usb/uf2/ghostfat.c # TinyUSB stack -C_SRC += $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c -C_SRC += $(TUSB_PATH)/common/tusb_fifo.c -C_SRC += $(TUSB_PATH)/device/usbd.c -C_SRC += $(TUSB_PATH)/device/usbd_control.c -C_SRC += $(TUSB_PATH)/class/cdc/cdc_device.c -C_SRC += $(TUSB_PATH)/class/msc/msc_device.c -C_SRC += $(TUSB_PATH)/tusb.c +C_SRC += \ + $(TUSB_PATH)/portable/nordic/nrf5x/dcd_nrf5x.c \ + $(TUSB_PATH)/common/tusb_fifo.c \ + $(TUSB_PATH)/device/usbd.c \ + $(TUSB_PATH)/device/usbd_control.c \ + $(TUSB_PATH)/class/cdc/cdc_device.c \ + $(TUSB_PATH)/class/msc/msc_device.c \ + $(TUSB_PATH)/tusb.c endif diff --git a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c index 2b5a36e..4b459d7 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/dfu_transport_ble.c @@ -736,8 +736,8 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) ble_gatts_rw_authorize_reply_params_t auth_reply; #ifdef CFG_DEBUG - extern const char* dbg_ble_event_str(uint16_t evt_id); - PRINTF("BLE %s\r\n", dbg_ble_event_str(p_ble_evt->header.evt_id)); + extern void print_ble_event(uint16_t evt_id); + print_ble_event(p_ble_evt->header.evt_id); #endif switch (p_ble_evt->header.evt_id) @@ -1085,6 +1085,7 @@ uint32_t dfu_transport_ble_close() #ifdef CFG_DEBUG +#if 0 typedef struct { uint32_t key; @@ -1173,18 +1174,22 @@ lookup_table_t const _strevt_table = .items = _strevt_lookup }; -const char* dbg_ble_event_str(uint16_t evt_id) -{ - const char * str = (const char *) lookup_find(&_strevt_table, evt_id); - static char unknown_str[7] = {0}; +#endif +void print_ble_event(uint16_t evt_id) +{ + // const char * str = (const char *) lookup_find(&_strevt_table, evt_id); + const char * str = NULL; + PRINTF("BLE event: "); if ( str == NULL ) { - sprintf(unknown_str, "0x%04X", evt_id); - str = unknown_str; + PRINTF("0x%04X", evt_id); + }else + { + PRINTF(str); } - - return str; + PRINTF("\r\n"); } + #endif diff --git a/src/boards/electronut_labs_papyr/board.h b/src/boards/electronut_labs_papyr/board.h index 738dd50..2dd931c 100644 --- a/src/boards/electronut_labs_papyr/board.h +++ b/src/boards/electronut_labs_papyr/board.h @@ -60,7 +60,8 @@ #define UF2_BOARD_ID "nRF52840-Papyr-v1" #define UF2_INDEX_URL "https://docs.electronut.in/papyr" -#define USB_DESC_VID 0x239A -#define USB_DESC_UF2_PID 0x003B +#define USB_DESC_VID 0x239A +#define USB_DESC_UF2_PID 0x003B +#define USB_DESC_CDC_ONLY_PID 0x003B #endif // PPAPYR_H diff --git a/src/main.c b/src/main.c index cd0a342..8cbe93f 100644 --- a/src/main.c +++ b/src/main.c @@ -238,14 +238,15 @@ static void check_dfu_mode(void) _ota_dfu = (gpregret == DFU_MAGIC_OTA_APPJUM) || (gpregret == DFU_MAGIC_OTA_RESET); // Serial only mode - bool serial_only_dfu = (gpregret == DFU_MAGIC_SERIAL_ONLY_RESET); - bool uf2_dfu = (gpregret == DFU_MAGIC_UF2_RESET); + bool const serial_only_dfu = (gpregret == DFU_MAGIC_SERIAL_ONLY_RESET); + bool const uf2_dfu = (gpregret == DFU_MAGIC_UF2_RESET); + bool const dfu_skip = (gpregret == DFU_MAGIC_SKIP); + + bool const reason_reset_pin = (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk) ? true : false; // start either serial, uf2 or ble bool dfu_start = _ota_dfu || serial_only_dfu || uf2_dfu || - (((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk)); - - bool const dfu_skip = (gpregret == DFU_MAGIC_SKIP); + (((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && reason_reset_pin); // Clear GPREGRET if it is our values if (dfu_start || dfu_skip) NRF_POWER->GPREGRET = 0; @@ -265,12 +266,9 @@ static void check_dfu_mode(void) if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) dfu_start = 1; - // App mode: register 1st reset or DFU startup for nrf52832 + // App mode: Double Reset detection or DFU startup for nrf52832 if ( ! (just_start_app || dfu_start || !valid_app) ) { - // Register our first reset for double reset detection - (*dbl_reset_mem) = DFU_DBL_RESET_MAGIC; - #ifdef NRF52832_XXAA /* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup * to support auto programming from Arduino IDE @@ -279,8 +277,15 @@ static void check_dfu_mode(void) */ bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL, false); #else - // if RST is pressed during this delay (double reset)--> if will enter dfu - NRFX_DELAY_MS(DFU_DBL_RESET_DELAY); + // Note: RESETREAS is not clear by bootloader, it should be cleared by application upon init() + if (reason_reset_pin) + { + // Register our first reset for double reset detection + (*dbl_reset_mem) = DFU_DBL_RESET_MAGIC; + + // if RST is pressed during this delay (double reset)--> if will enter dfu + NRFX_DELAY_MS(DFU_DBL_RESET_DELAY); + } #endif } diff --git a/src/usb/uf2/ghostfat.c b/src/usb/uf2/ghostfat.c index c040b22..d748904 100644 --- a/src/usb/uf2/ghostfat.c +++ b/src/usb/uf2/ghostfat.c @@ -31,6 +31,7 @@ #include "flash_nrf5x.h" #include #include +#include #include "bootloader_settings.h" #include "bootloader.h" @@ -119,7 +120,8 @@ STATIC_ASSERT(FAT_ENTRIES_PER_SECTOR == 256); // FAT char infoUf2File[128*3] = "UF2 Bootloader " UF2_VERSION "\r\n" "Model: " UF2_PRODUCT_NAME "\r\n" - "Board-ID: " UF2_BOARD_ID "\r\n"; + "Board-ID: " UF2_BOARD_ID "\r\n" + "Date: " __DATE__ "\r\n"; const char indexFile[] = "\n" @@ -250,17 +252,33 @@ void uf2_init(void) uint32_t const sd_id = SD_ID_GET(MBR_SIZE); uint32_t const sd_version = SD_VERSION_GET(MBR_SIZE); - uint32_t const ver1 = sd_version / 1000000; - uint32_t const ver2 = (sd_version % 1000000)/1000; - uint32_t const ver3 = sd_version % 1000; + uint32_t ver[3]; + ver[0] = sd_version / 1000000; + ver[1] = (sd_version - ver[0]*1000000)/1000; + ver[2] = (sd_version - ver[0]*1000000 - ver[1]*1000); - sprintf(infoUf2File + strlen(infoUf2File), "S%lu version %lu.%lu.%lu\r\n", sd_id, ver1, ver2, ver3); + char str[10]; + utoa(sd_id, str, 10); + + strcat(infoUf2File, "S"); + strcat(infoUf2File, str); + strcat(infoUf2File, " "); + + utoa(ver[0], str, 10); + strcat(infoUf2File, str); + strcat(infoUf2File, "."); + + utoa(ver[1], str, 10); + strcat(infoUf2File, str); + strcat(infoUf2File, "."); + + utoa(ver[2], str, 10); + strcat(infoUf2File, str); + strcat(infoUf2File, "\r\n"); }else { strcat(infoUf2File, "not found\r\n"); } - - strcat(infoUf2File, "Date: " __DATE__ "\r\n"); } /*------------------------------------------------------------------*/ diff --git a/src/usb/usb_desc.c b/src/usb/usb_desc.c index effc252..ac1a8ac 100644 --- a/src/usb/usb_desc.c +++ b/src/usb/usb_desc.c @@ -25,12 +25,12 @@ #include "usb_desc.h" enum { - ITF_STR_LANGUAGE = 0 , - ITF_STR_MANUFACTURER , - ITF_STR_PRODUCT , - ITF_STR_SERIAL , - ITF_STR_CDC , - ITF_STR_MSC + STRID_LANGUAGE = 0 , + STRID_MANUFACTURER , + STRID_PRODUCT , + STRID_SERIAL , + STRID_CDC , + STRID_MSC }; // CDC + MSC or CDC only mode @@ -84,25 +84,25 @@ enum { ITF_NUM_TOTAL }; -uint8_t const desc_configuration_cdc_msc[] = +uint8_t desc_configuration_cdc_msc[] = { // Interface count, string index, total length, attribute, power in mA - 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), + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN, 0, 100), // Interface number, string index, EP notification address and size, EP data address (out, in) and size. - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64), + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64), // Interface number, string index, EP Out & EP In address, EP size - TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, ITF_STR_MSC, 0x03, 0x83, 64), + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, STRID_MSC, 0x03, 0x83, 64), }; -uint8_t const desc_configuration_cdc_only[] = +uint8_t desc_configuration_cdc_only[] = { // Interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL-1, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL-1, 0, TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN, 0, 100), // Interface number, string index, EP notification address and size, EP data address (out, in) and size. - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, ITF_STR_CDC, 0x81, 8, 0x02, 0x82, 64), + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC, 0x81, 8, 0x02, 0x82, 64), }; @@ -127,7 +127,19 @@ void usb_desc_init(bool cdc_only) } // Create Serial string descriptor - sprintf(desc_str_serial, "%08lX%08lX", NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0]); + uint8_t const* device_id = (uint8_t const*) &NRF_FICR->DEVICEID; + + for ( uint8_t i = 0; i < 8; i++ ) + { + for ( uint8_t j = 0; j < 2; j++ ) + { + const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + uint8_t nibble = (device_id[i] >> (j * 4)) & 0xf; + desc_str_serial[15 - (i * 2 + j)] = nibble_to_hex[nibble]; // memory is little endian + } + } + desc_str_serial[16] = 0; } //--------------------------------------------------------------------+ @@ -156,7 +168,7 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) uint8_t chr_count; - if ( index == 0) + if ( index == STRID_LANGUAGE ) { memcpy(&_desc_str[1], string_desc_arr[0], 2); chr_count = 1; diff --git a/src/usb/usb_desc.h b/src/usb/usb_desc.h index 7553794..3b318c2 100644 --- a/src/usb/usb_desc.h +++ b/src/usb/usb_desc.h @@ -30,16 +30,4 @@ void usb_desc_init(bool cdc_only); -#ifndef USB_DESC_VID -#define USB_DESC_VID 0x239A -#endif - -#ifndef USB_DESC_UF2_PID -#define USB_DESC_UF2_PID 0x0029 -#endif - -#ifndef USB_DESC_CDC_ONLY_PID -#define USB_DESC_CDC_ONLY_PID 0x002A -#endif - #endif /* USB_DESC_H_ */