Skip to content

Commit f7faa19

Browse files
committed
Bootloader: added erase flash command.
1 parent 0e4e0a9 commit f7faa19

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

bootloaders/zero/sam_ba_monitor.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "cdc_enumerate.h"
3737

3838
const char RomBOOT_Version[] = SAM_BA_VERSION;
39+
const char RomBOOT_ExtendedCapabilities[] = "[Arduino:X]";
3940

4041
/* Provides one common interface to handle both USART and USB-CDC */
4142
typedef struct
@@ -166,12 +167,18 @@ uint8_t command, *ptr_data, *ptr, data[SIZEBUFMAX];
166167
uint8_t j;
167168
uint32_t u32tmp;
168169

170+
uint32_t PAGE_SIZE, PAGES, MAX_FLASH;
169171

170172
/**
171173
* \brief This function starts the SAM-BA monitor.
172174
*/
173175
void sam_ba_monitor_run(void)
174176
{
177+
uint32_t pageSizes[] = { 8, 16, 32, 64, 128, 256, 512, 1024 };
178+
PAGE_SIZE = pageSizes[NVMCTRL->PARAM.bit.PSZ];
179+
PAGES = NVMCTRL->PARAM.bit.NVMP;
180+
MAX_FLASH = PAGE_SIZE * PAGES;
181+
175182
ptr_data = NULL;
176183
command = 'z';
177184
while (1) {
@@ -277,6 +284,9 @@ void sam_ba_monitor_loop(void)
277284
ptr_monitor_if->putdata((uint8_t *) RomBOOT_Version,
278285
strlen(RomBOOT_Version));
279286
ptr_monitor_if->putdata(" ", 1);
287+
ptr_monitor_if->putdata((uint8_t *) RomBOOT_ExtendedCapabilities,
288+
strlen(RomBOOT_ExtendedCapabilities));
289+
ptr_monitor_if->putdata(" ", 1);
280290
ptr = (uint8_t*) &(__DATE__);
281291
i = 0;
282292
while (*ptr++ != '\0')
@@ -290,6 +300,29 @@ void sam_ba_monitor_loop(void)
290300
ptr_monitor_if->putdata((uint8_t *) &(__TIME__), i);
291301
ptr_monitor_if->putdata("\n\r", 2);
292302
}
303+
else if (command == 'X')
304+
{
305+
// Syntax: X[ADDR]#
306+
// Erase the flash memory starting from ADDR to the end of flash.
307+
308+
// Note: the flash memory is erased in ROWS, that is in block of 4 pages.
309+
// Even if the starting address is the last byte of a ROW the entire
310+
// ROW is erased anyway.
311+
312+
uint32_t dst_addr = current_number; // starting address
313+
314+
while (dst_addr < MAX_FLASH) {
315+
// Execute "ER" Erase Row
316+
NVMCTRL->ADDR.reg = dst_addr / 2;
317+
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;
318+
while (NVMCTRL->INTFLAG.bit.READY == 0)
319+
;
320+
dst_addr += PAGE_SIZE * 4; // Skip a ROW
321+
}
322+
323+
// Notify command completed
324+
ptr_monitor_if->putdata("X\n\r", 3);
325+
}
293326

294327
command = 'z';
295328
current_number = 0;

bootloaders/zero/samd21_sam_ba.bin

244 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)