Skip to content

Commit

Permalink
Add 'applet' make file target
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Feb 7, 2017
1 parent 5abdd4b commit 5fa044f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files.associations": {}
}
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ clean:
gdb:
arm-none-eabi-gdb $(BUILD_PATH)/$(NAME).elf

tui:
arm-none-eabi-gdb -tui $(BUILD_PATH)/$(NAME).elf

applet: $(EXECUTABLE)
rm -f flash.asm
arm-none-eabi-objdump -d $(BUILD_PATH)/flash.o > flash.asm
node scripts/genapplet.js flash.asm flash_write
@rm -f flash.asm

drop-board: all
@echo "*** Copy files for $(BOARD)"
mkdir -p build/drop
Expand Down
36 changes: 36 additions & 0 deletions scripts/genapplet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
let fs = require("fs")
let s = fs.readFileSync(process.argv[2], "utf8")
let pref = ""
let infun = false
let words = []
for (let l of s.split(/\n/)) {
let m = /^00000000 <(.*)>:/.exec(l)
if (m && m[1] == process.argv[3]) infun = true
if (/^Disassembly/.test(l)) infun = false
if (!infun) continue
m = /^\s*[0-9a-f]+:\s+([0-9a-f]+)\s+/.exec(l)
if (m) {
let n = m[1]
if (n.length == 4) {
if (pref) {
words.push("0x" + n + pref)
pref = ""
} else {
pref = n
}
} else if (n.length == 8) {
if (pref) throw new Error()
words.push("0x" + n)
} else {
throw new Error()
}
}
}

let r = ""
for (let i = 0; i < words.length; i++) {
if (i%6 == 0) r += "\n"
r += words[i] + ", "
}

console.log(r)
37 changes: 33 additions & 4 deletions src/flash.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "uf2.h"

static inline void wait_ready(void) {
while (NVMCTRL->INTFLAG.bit.READY == 0) {
}
}
// this actually generates less code than a function
#define wait_ready() \
while (NVMCTRL->INTFLAG.bit.READY == 0) \
;

void flash_erase_row(uint32_t *dst) {
wait_ready();
Expand Down Expand Up @@ -44,6 +44,35 @@ void flash_write_words(uint32_t *dst, uint32_t *src, uint32_t n_words) {
}
}

void flash_write() {
uint32_t *src = (void *)0x20006000;
uint32_t *dst = (void *)*src++;
uint32_t n_pages = *src++;

NVMCTRL->CTRLB.bit.MANW = 1;
while (n_pages--) {
wait_ready();
NVMCTRL->STATUS.reg = NVMCTRL_STATUS_MASK;

// Execute "ER" Erase Row
NVMCTRL->ADDR.reg = (uint32_t)dst / 2;
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;
wait_ready();

// Execute "PBC" Page Buffer Clear
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_PBC;
wait_ready();

uint32_t len = FLASH_ROW_SIZE >> 2;
while (len--)
*dst++ = *src++;

// Execute "WP" Write Page
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_WP;
wait_ready();
}
}

// Skip writing blocks that are identical to the existing block.
// only disable for debugging/timing
#define QUICK_FLASH 1
Expand Down

0 comments on commit 5fa044f

Please sign in to comment.