Skip to content

Commit 3a1c454

Browse files
committed
Bootloader: added multi-page flash write command.
1 parent 8d22aee commit 3a1c454

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

bootloaders/zero/sam_ba_monitor.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "cdc_enumerate.h"
3737

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

4141
/* Provides one common interface to handle both USART and USB-CDC */
4242
typedef struct
@@ -338,6 +338,61 @@ void sam_ba_monitor_loop(void)
338338
// Notify command completed
339339
ptr_monitor_if->putdata("X\n\r", 3);
340340
}
341+
else if (command == 'Y')
342+
{
343+
// This command writes the content of a buffer in SRAM into flash memory.
344+
345+
// Syntax: Y[ADDR],0#
346+
// Set the starting address of the SRAM buffer.
347+
348+
// Syntax: Y[ROM_ADDR],[SIZE]#
349+
// Write the first SIZE bytes from the SRAM buffer (previously set) into
350+
// flash memory starting from address ROM_ADDR
351+
352+
static uint32_t *src_buff_addr = NULL;
353+
354+
if (current_number == 0) {
355+
// Set buffer address
356+
src_buff_addr = ptr_data;
357+
358+
} else {
359+
// Write to flash
360+
uint32_t size = current_number/4;
361+
uint32_t *src_addr = src_buff_addr;
362+
uint32_t *dst_addr = ptr_data;
363+
364+
// Set automatic page write
365+
NVMCTRL->CTRLB.bit.MANW = 0;
366+
367+
// Do writes in pages
368+
while (size) {
369+
// Execute "PBC" Page Buffer Clear
370+
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_PBC;
371+
while (NVMCTRL->INTFLAG.bit.READY == 0)
372+
;
373+
374+
// Fill page buffer
375+
uint32_t i;
376+
for (i=0; i<(PAGE_SIZE/4) && i<size; i++) {
377+
dst_addr[i] = src_addr[i];
378+
}
379+
380+
// Execute "WP" Write Page
381+
//NVMCTRL->ADDR.reg = ((uint32_t)dst_addr) / 2;
382+
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_WP;
383+
while (NVMCTRL->INTFLAG.bit.READY == 0)
384+
;
385+
386+
// Advance to next page
387+
dst_addr += i;
388+
src_addr += i;
389+
size -= i;
390+
}
391+
}
392+
393+
// Notify command completed
394+
ptr_monitor_if->putdata("Y\n\r", 3);
395+
}
341396

342397
command = 'z';
343398
current_number = 0;

bootloaders/zero/samd21_sam_ba.bin

148 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)