Skip to content

Commit

Permalink
Align sketch with selfmain
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Jan 16, 2017
1 parent d4583c7 commit dc8d298
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ $(BUILD_PATH)/%.o: src/%.c $(wildcard inc/*.h boards/*/*.h)
$(BUILD_PATH)/%.o: $(BUILD_PATH)/%.c
@$(CC) $(CFLAGS) $(BLD_EXTA_FLAGS) $(INCLUDES) $< -o $@

$(BUILD_PATH)/selfdata.c: $(EXECUTABLE) scripts/gendata.js
$(BUILD_PATH)/selfdata.c: $(EXECUTABLE) scripts/gendata.js src/sketch.cpp
node scripts/gendata.js $(BUILD_PATH) $(NAME).bin

clean:
Expand Down
2 changes: 1 addition & 1 deletion inc/uf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define INDEX_URL "https://www.pxt.io/"
#endif

#define UF2_VERSION_BASE "v1.9.4"
#define UF2_VERSION_BASE "v1.9.5"

// needs to be more than ~4200 (to force FAT16)
#define NUM_FAT_BLOCKS 8000
Expand Down
43 changes: 27 additions & 16 deletions src/sketch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,23 @@ void flash_write_row(uint32_t *dst, uint32_t *src) {
flash_write_words(dst, src, FLASH_ROW_SIZE / 4);
}

static inline void exec_cmd(uint32_t cmd) {
NVMCTRL->ADDR.reg = (uint32_t)NVM_USER_MEMORY / 2;
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | cmd;
while (NVMCTRL->INTFLAG.bit.READY == 0) {
}
}
#define exec_cmd(cmd) \
do { \
NVMCTRL->STATUS.reg |= NVMCTRL_STATUS_MASK; \
NVMCTRL->ADDR.reg = (uint32_t)NVMCTRL_USER / 2; \
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | cmd; \
while (NVMCTRL->INTFLAG.bit.READY == 0) \
; \
} while (0)

void setBootProt(int v) {
uint32_t fuses[2];
fuses[0] = NVM_USER_MEMORY[0] | (NVM_USER_MEMORY[1] << 16);
fuses[1] = NVM_USER_MEMORY[2] | (NVM_USER_MEMORY[3] << 16);

while (!(NVMCTRL->INTFLAG.reg & NVMCTRL_INTFLAG_READY))
;

fuses[0] = *((uint32_t *)NVMCTRL_AUX0_ADDRESS);
fuses[1] = *(((uint32_t *)NVMCTRL_AUX0_ADDRESS) + 1);

uint32_t bootprot = (fuses[0] & NVMCTRL_FUSES_BOOTPROT_Msk) >> NVMCTRL_FUSES_BOOTPROT_Pos;

Expand All @@ -90,7 +96,7 @@ void setBootProt(int v) {

fuses[0] = (fuses[0] & ~NVMCTRL_FUSES_BOOTPROT_Msk) | (v << NVMCTRL_FUSES_BOOTPROT_Pos);

NVMCTRL->CTRLB.bit.MANW = 1;
NVMCTRL->CTRLB.reg = NVMCTRL->CTRLB.reg | NVMCTRL_CTRLB_CACHEDIS | NVMCTRL_CTRLB_MANW;

exec_cmd(NVMCTRL_CTRLA_CMD_EAR);
exec_cmd(NVMCTRL_CTRLA_CMD_PBC);
Expand All @@ -100,9 +106,6 @@ void setBootProt(int v) {

exec_cmd(NVMCTRL_CTRLA_CMD_WAP);

fuses[0] = NVM_USER_MEMORY[0] | (NVM_USER_MEMORY[1] << 16);
fuses[1] = NVM_USER_MEMORY[2] | (NVM_USER_MEMORY[3] << 16);

NVIC_SystemReset();
}

Expand All @@ -116,7 +119,6 @@ void mydelay(int ms) {
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
setBootProt(7);

if (8 << NVMCTRL->PARAM.bit.PSZ != FLASH_PAGE_SIZE)
while (1) {
Expand Down Expand Up @@ -145,15 +147,24 @@ void setup() {
flash_write_row((uint32_t *)(void *)i, (uint32_t *)(void *)pageBuf);
}

while (1) {
// re-base int vector back to bootloader, so that the flash erase below doesn't write over the
// vectors
SCB->VTOR = 0;

// erase first row of this updater app, so the bootloader doesn't start us again
flash_erase_row((uint32_t *)(void *)(BOOTLOADER_K * 1024));

for (i = 0; i < 20; ++i) {
digitalWrite(LED_BUILTIN, HIGH);
mydelay(100);
digitalWrite(LED_BUILTIN, LOW);
mydelay(200);
}

// erase first row of this updater app, so the bootloader doesn't start us again
// flash_erase_row((void *)i); - this seems to cause trouble
setBootProt(2); // 8k

while (1) {
}
}

void loop() {}

0 comments on commit dc8d298

Please sign in to comment.