Skip to content

Commit af10512

Browse files
committed
populate bootloader address and mbr param if not available in MBR
check SD MAGIC before using sd API when init usb
1 parent ed7c5ac commit af10512

File tree

7 files changed

+49
-68
lines changed

7 files changed

+49
-68
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ sd:
345345
# flash MBR only
346346
mbr:
347347
@echo Flashing: $(MBR_HEX)
348-
$(NRFJPROG) --program $(MBR_HEX) -f nrf52 --sectorerase
348+
$(NRFJPROG) --program $(MBR_HEX) -f nrf52 --sectorerase --reset
349349

350350
gdbflash: $(BUILD)/$(MERGED_FILE).hex
351351
@echo Flashing: $<

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ uint32_t bootloader_dfu_sd_update_continue(void);
9292
*/
9393
uint32_t bootloader_dfu_sd_update_finalize(void);
9494

95+
96+
void bootloader_mbr_addrs_populate(void);
97+
9598
#endif // BOOTLOADER_H__
9699

97100
/**@} */

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

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,23 @@
1313
#include "bootloader_settings.h"
1414
#include <stdint.h>
1515
#include <dfu_types.h>
16+
#include "nrfx_nvmc.h"
1617

17-
#if defined ( __CC_ARM )
18+
/**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
19+
__attribute__ ((section(".bootloaderSettings")))
20+
uint8_t m_boot_settings[CODE_PAGE_SIZE];
1821

19-
uint8_t m_boot_settings[CODE_PAGE_SIZE] __attribute__((at(BOOTLOADER_SETTINGS_ADDRESS))) __attribute__((used)); /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
20-
uint32_t m_uicr_bootloader_start_address __attribute__((at(NRF_UICR_BOOT_START_ADDRESS))) = BOOTLOADER_REGION_START; /**< This variable makes the linker script write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
22+
/**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
23+
__attribute__ ((section(".uicrBootStartAddress")))
24+
volatile uint32_t m_uicr_bootloader_start_address = BOOTLOADER_REGION_START;
2125

22-
#elif defined ( __GNUC__ )
23-
24-
uint8_t m_boot_settings[CODE_PAGE_SIZE] __attribute__ ((section(".bootloaderSettings"))); /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
25-
volatile uint32_t m_uicr_bootloader_start_address __attribute__ ((section(".uicrBootStartAddress"))) = BOOTLOADER_REGION_START; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
26-
27-
#elif defined ( __ICCARM__ )
28-
29-
__no_init uint8_t m_boot_settings[CODE_PAGE_SIZE] @ BOOTLOADER_SETTINGS_ADDRESS; /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
30-
__root const uint32_t m_uicr_bootloader_start_address @ NRF_UICR_BOOT_START_ADDRESS = BOOTLOADER_REGION_START; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
31-
32-
#endif
33-
34-
#if defined(NRF52_SERIES)
35-
#if defined ( __CC_ARM )
36-
37-
uint8_t m_mbr_params_page[CODE_PAGE_SIZE] __attribute__((at(BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS))) __attribute__((used)); /**< This variable reserves a codepage for mbr parameters, to ensure the compiler doesn't locate any code or variables at his location. */
38-
uint32_t m_uicr_mbr_params_page_address __attribute__((at(NRF_UICR_MBR_PARAMS_PAGE_ADDRESS)))
39-
= BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable makes the linker script write the mbr parameters page address to the UICR register. This value will be written in the HEX file and thus written to the UICR when the bootloader is flashed into the chip */
40-
41-
#elif defined (__GNUC__ )
42-
43-
uint8_t m_mbr_params_page[CODE_PAGE_SIZE] __attribute__ ((section(".mbrParamsPage"))); /**< This variable reserves a codepage for mbr parameters, to ensure the compiler doesn't locate any code or variables at his location. */
44-
volatile uint32_t m_uicr_mbr_params_page_address __attribute__ ((section(".uicrMbrParamsPageAddress")))
45-
= BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable makes the linker script write the mbr parameters page address to the UICR register. This value will be written in the HEX file and thus written to the UICR when the bootloader is flashed into the chip */
46-
47-
#elif defined (__ICCARM__ )
48-
49-
__no_init uint8_t m_mbr_params_page[CODE_PAGE_SIZE] @ BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable reserves a codepage for bootloader specific settings, to ensure the compiler doesn't locate any code or variables at his location. */
50-
__root const uint32_t m_uicr_mbr_params_page_address @ NRF_UICR_MBR_PARAMS_PAGE_ADDRESS = BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS; /**< This variable ensures that the linker script will write the bootloader start address to the UICR register. This value will be written in the HEX file and thus written to UICR when the bootloader is flashed into the chip. */
51-
52-
#endif
53-
#endif // NRF52_SERIES
26+
/**< This variable reserves a codepage for mbr parameters, to ensure the compiler doesn't locate any code or variables at his location. */
27+
__attribute__ ((section(".mbrParamsPage")))
28+
uint8_t m_mbr_params_page[CODE_PAGE_SIZE];
5429

30+
/**< This variable makes the linker script write the mbr parameters page address to the UICR register. This value will be written in the HEX file and thus written to the UICR when the bootloader is flashed into the chip */
31+
__attribute__ ((section(".uicrMbrParamsPageAddress")))
32+
volatile uint32_t m_uicr_mbr_params_page_address = BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS;
5533

5634
void bootloader_util_settings_get(const bootloader_settings_t ** pp_bootloader_settings)
5735
{
@@ -61,3 +39,16 @@ void bootloader_util_settings_get(const bootloader_settings_t ** pp_bootloader_s
6139

6240
*pp_bootloader_settings = p_bootloader_settings;
6341
}
42+
43+
void bootloader_mbr_addrs_populate(void)
44+
{
45+
if (*(const uint32_t *)MBR_BOOTLOADER_ADDR == 0xFFFFFFFF)
46+
{
47+
nrfx_nvmc_word_write(MBR_BOOTLOADER_ADDR, BOOTLOADER_REGION_START);
48+
}
49+
50+
if (*(const uint32_t *)MBR_PARAM_PAGE_ADDR == 0xFFFFFFFF)
51+
{
52+
nrfx_nvmc_word_write(MBR_PARAM_PAGE_ADDR, BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS);
53+
}
54+
}

lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
#include "nrf.h"
3030
#include "app_util.h"
3131

32-
#define SD_MAGIC_NUMBER 0x51b1e5db
33-
#define SD_MAGIC_OK() (*((uint32_t*)(SOFTDEVICE_INFO_STRUCT_ADDRESS+4)) == 0x51b1e5db)
32+
#ifndef SD_MAGIC_NUMBER
33+
#define SD_MAGIC_NUMBER 0x51B1E5DB
34+
#endif
35+
36+
#define SD_MAGIC_OK() (*((uint32_t*)(SOFTDEVICE_INFO_STRUCT_ADDRESS+4)) == SD_MAGIC_NUMBER)
3437

3538
#define NRF_UICR_BOOT_START_ADDRESS (NRF_UICR_BASE + 0x14) /**< Register where the bootloader start address is stored in the UICR register. */
3639
#define NRF_UICR_MBR_PARAMS_PAGE_ADDRESS (NRF_UICR_BASE + 0x18) /**< Register where the mbr params page is stored in the UICR register. (Only in use in nRF52 MBR).*/
3740

41+
// TODO more testing/checking
3842
#define CODE_REGION_1_START (SD_MAGIC_OK() ? SD_SIZE_GET(MBR_SIZE) : MBR_SIZE) /**< This field should correspond to the size of Code Region 0, (which is identical to Start of Code Region 1), found in UICR.CLEN0 register. This value is used for compile safety, as the linker will fail if application expands into bootloader. Runtime, the bootloader will use the value found in UICR.CLEN0. */
3943
#define SOFTDEVICE_REGION_START MBR_SIZE /**< This field should correspond to start address of the bootloader, found in UICR.RESERVED, 0x10001014, register. This value is used for sanity check, so the bootloader will fail immediately if this value differs from runtime value. The value is used to determine max application size for updating. */
4044
#define CODE_PAGE_SIZE 0x1000 /**< Size of a flash codepage. Used for size of the reserved flash space in the bootloader region. Will be runtime checked against NRF_UICR->CODEPAGESIZE to ensure the region is correct. */

src/boards.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ void SysTick_Handler(void)
132132
}
133133

134134

135-
uint32_t tusb_hal_millis(void)
136-
{
137-
return ( ( ((uint64_t)app_timer_cnt_get())*1000*(APP_TIMER_CONFIG_RTC_FREQUENCY+1)) / APP_TIMER_CLOCK_FREQ );
138-
}
139-
140135
void pwm_teardown(NRF_PWM_Type* pwm )
141136
{
142137
pwm->TASKS_SEQSTART[0] = 0;

src/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ void softdev_mbr_init(void)
166166
//--------------------------------------------------------------------+
167167
int main(void)
168168
{
169+
// Populate Boot Address and MBR Param if not already
170+
bootloader_mbr_addrs_populate();
171+
169172
// SD is already Initialized in case of BOOTLOADER_DFU_OTA_MAGIC
170173
bool sd_inited = (NRF_POWER->GPREGRET == DFU_MAGIC_OTA_APPJUM);
171174

src/usb/usb.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void USBD_IRQHandler(void)
5656
//------------- IMPLEMENTATION -------------//
5757
void usb_init(bool cdc_only)
5858
{
59+
// 0, 1 is reserved for SD
5960
NVIC_SetPriority(USBD_IRQn, 2);
6061

6162
// USB power may already be ready at this time -> no event generated
@@ -64,9 +65,14 @@ void usb_init(bool cdc_only)
6465

6566
#ifdef SOFTDEVICE_PRESENT
6667
uint8_t sd_en = false;
67-
(void) sd_softdevice_is_enabled(&sd_en);
6868

69-
if ( sd_en ) {
69+
if ( SD_MAGIC_OK() )
70+
{
71+
sd_softdevice_is_enabled(&sd_en);
72+
}
73+
74+
if ( sd_en )
75+
{
7076
sd_power_usbdetected_enable(true);
7177
sd_power_usbpwrrdy_enable(true);
7278
sd_power_usbremoved_enable(true);
@@ -106,29 +112,8 @@ void usb_init(bool cdc_only)
106112

107113
void usb_teardown(void)
108114
{
109-
if ( NRF_USBD->ENABLE )
110-
{
111-
// Abort all transfers
112-
113-
// Disable pull up
114-
nrf_usbd_pullup_disable(NRF_USBD);
115-
116-
// Disable Interrupt
117-
NVIC_DisableIRQ(USBD_IRQn);
118-
119-
// disable all interrupt
120-
NRF_USBD->INTENCLR = NRF_USBD->INTEN;
121-
122-
nrf_usbd_disable(NRF_USBD);
123-
124-
#if defined(SOFTDEVICE_PRESENT)
125-
sd_clock_hfclk_release();
126-
127-
sd_power_usbdetected_enable(false);
128-
sd_power_usbpwrrdy_enable(false);
129-
sd_power_usbremoved_enable(false);
130-
#endif
131-
}
115+
// Simulate an disconnect which cause pullup disable, USB perpheral disable and hclk disable
116+
tusb_hal_nrf_power_event(NRFX_POWER_USB_EVT_REMOVED);
132117
}
133118

134119
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)