-
-
Notifications
You must be signed in to change notification settings - Fork 214
Add Nano33_updateBootloader sketch to support SoftDevices #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
760c106
Add example to update Nano 33 bootloader to support SoftDevices
giulcioffi a107e88
Always erase entire sector
giulcioffi e84d346
Adjust print to console
giulcioffi 91a77e0
Save magic and SD info at the beginning of last sector in flash + reboot
giulcioffi 95b8edd
Enable the bootloader by writing its address to the UICR memory
giulcioffi 2b63db6
Update binary code of the bootloader
giulcioffi 4021d0a
Add possibility to update only SoftDevice + use full SoftDevice.h wit…
giulcioffi fc55c8e
Add instructions
giulcioffi 08ee43c
Add missing ';'
giulcioffi e8ddac2
Fix typo
giulcioffi a4a984d
Use two different sketches to update the bootloader and the Soft Device
giulcioffi 89fff7d
Refactor the updaters back into one sketch
sebromero 88c3687
Improve console output
sebromero e1f3665
Update documentation on how to use SD
sebromero 408578a
Merge pull request #1 from sebromero/nanoBLwithSD
giulcioffi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
libraries/Nano33_System/examples/Nano33_updateBootloader/Nano33_updateBootloader.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* This sketch will allow to support SoftDevices on Nano 33 BLE. | ||
* To be able to support them, the bootloader needs to be updated. | ||
* Before uploading this sketch to the board, follow this procedure: | ||
* | ||
* - Convert your SoftDevice binary to a SoftDevice.h . | ||
* The nRF5-SDK website provides a SoftDevice.hex, so run the following commands: | ||
* | ||
* objcopy --input-target=ihex --output-target=binary --gap-fill 0xff SoftDevice.hex SoftDevice.bin | ||
* xxd -i SoftDevice.bin > SoftDevice.h | ||
* | ||
* - Copy the content of the generated header file to SoftDevice.h | ||
* | ||
* - Upload this sketch to a Nano33BLE and make your selection through the Serial monitor: | ||
* you can choose wheter to update only the SoftDevice or the full bootloader. | ||
* After completion, the board will reboot and enter the bootloader mode. | ||
* | ||
* - Now you can upload your sketch. | ||
* You can upload a sketch that uses the SoftDevice at 0x26000, using the following bossac command | ||
* | ||
* /path/to/bossac -d --port=yourPort --offset=0x16000 -U -i -e -w /path/to/sketch.bin -R | ||
* | ||
* Or you can still upload a standard sketch from the IDE at 0x10000. This will of course overwrite the SoftDevice. | ||
* So if you want to run a SoftDevice-related sketch, always remember to upload this sketch before. | ||
* | ||
*/ | ||
|
||
#include "FlashIAP.h" | ||
#include "SoftDevice.h" | ||
#include "bootloader.h" | ||
#include "nrf_nvmc.h" | ||
|
||
#define MBR_ADDR (0x0) | ||
#define SOFTDEVICE_ADDR (0xA0000) | ||
#define BOOTLOADER_ADDR (0xE0000) | ||
#define SOFTDEVICE_INFO_ADDR (0xFF000) | ||
#define UICR_BOOT_ADDR (0x10001014) | ||
|
||
const unsigned int magic = 0x5f27a93d; | ||
|
||
mbed::FlashIAP flash; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
while (!Serial) {} | ||
Serial.println("Do you want to update only the SoftDevice? Y/[n]"); | ||
Serial.println("Yes: upload only the binary of the DoftDevice"); | ||
Serial.println("No : upload the SoftDevice binary and update the bootloader to support SoftDevices") | ||
|
||
bool confirmation = false; | ||
while (confirmation == false) { | ||
if (Serial.available()) { | ||
char choice = Serial.read(); | ||
switch (choice) { | ||
case 'y': | ||
case 'Y': | ||
flash.init(); | ||
Serial.println("Storing SoftDevice length info at 0xFF000..."); | ||
writeSoftDeviceLen(SOFTDEVICE_INFO_ADDR); | ||
Serial.println("Flasing SoftDevice..."); | ||
applyUpdate(SOFTDEVICE_ADDR); | ||
flash.deinit(); | ||
Serial.println("SoftDevice update complete! The board is restarting..."); | ||
confirmation = true; | ||
NVIC_SystemReset(); | ||
break; | ||
case 'n': | ||
case 'N': | ||
flash.init(); | ||
Serial.println("Flasing MBR..."); | ||
applyUpdate(MBR_ADDR); | ||
Serial.println("Storing SoftDevice length info at 0xFF000..."); | ||
writeSoftDeviceLen(SOFTDEVICE_INFO_ADDR); | ||
Serial.println("Flasing SoftDevice..."); | ||
applyUpdate(SOFTDEVICE_ADDR); | ||
Serial.println("Flasing bootloader..."); | ||
applyUpdate(BOOTLOADER_ADDR); | ||
Serial.println("Write in UICR memory the address of the new bootloader..."); | ||
nrf_nvmc_write_word(UICR_BOOT_ADDR, BOOTLOADER_ADDR); | ||
flash.deinit(); | ||
Serial.println("SoftDevice + Bootloader update complete. The board is restarting..."); | ||
confirmation = true; | ||
NVIC_SystemReset(); | ||
break; | ||
default: | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
void applyUpdate(uint32_t address) { | ||
long len = 0; | ||
|
||
uint32_t bin_pointer = 0; | ||
uint32_t flash_pointer = 0; | ||
|
||
const uint32_t page_size = flash.get_page_size(); | ||
//Serial.print("Page size: "); | ||
//Serial.println(page_size); | ||
char *page_buffer = new char[page_size]; | ||
uint32_t addr = address; | ||
if (addr == MBR_ADDR) { | ||
bin_pointer = 0; | ||
len = 4096; | ||
} else if (addr == SOFTDEVICE_ADDR) { | ||
//Skip the MBR | ||
bin_pointer = 4096; | ||
len = Softdevice_bin_len - 4096; | ||
} else if (addr == BOOTLOADER_ADDR) { | ||
bin_pointer = 0; | ||
len = nano33_bootloader_hex_len; | ||
} | ||
uint32_t sector_size = flash.get_sector_size(addr); | ||
uint32_t next_sector = addr + sector_size; | ||
bool sector_erased = false; | ||
size_t pages_flashed = 0; | ||
uint32_t percent_done = 0; | ||
|
||
while (true) { | ||
|
||
if (flash_pointer >= len) { | ||
break; | ||
} | ||
|
||
flash.erase(addr + flash_pointer, sector_size); | ||
|
||
if ((len - flash_pointer) < sector_size) { | ||
sector_size = len - flash_pointer; | ||
} | ||
|
||
// Program page | ||
if (addr == MBR_ADDR) { | ||
flash.program(&Softdevice_bin[bin_pointer], addr + flash_pointer, sector_size); | ||
} else if (addr == SOFTDEVICE_ADDR) { | ||
flash.program(&Softdevice_bin[bin_pointer], addr + flash_pointer, sector_size); | ||
} else if (addr == BOOTLOADER_ADDR) { | ||
flash.program(&nano33_bootloader_hex[bin_pointer], addr + flash_pointer, sector_size); | ||
} | ||
|
||
Serial.print("Flash Address = "); | ||
Serial.println(addr + flash_pointer, HEX); | ||
|
||
bin_pointer = bin_pointer + sector_size; | ||
flash_pointer = flash_pointer + sector_size; | ||
|
||
uint32_t percent_done = flash_pointer * 100 / len; | ||
Serial.println("Flashed " + String(percent_done) + "%"); | ||
|
||
} | ||
Serial.println(); | ||
|
||
delete[] page_buffer; | ||
} | ||
|
||
void writeSoftDeviceLen(uint32_t address) { | ||
uint32_t sd_addr = SOFTDEVICE_ADDR; | ||
flash.erase(address, 16); | ||
//Write flag to let Bootloader understand that SoftDevice binary must be moved | ||
flash.program(&magic, address, 4); | ||
//Write address where the SoftDevice binary has been written | ||
flash.program(&sd_addr, address + 4, 4); | ||
//Write SoftDevice binary length | ||
unsigned int sd_len = Softdevice_bin_len - 4096; | ||
flash.program(&sd_len, address + 8, 4); | ||
} | ||
|
||
void loop() { | ||
delay(1000); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.