Skip to content

Commit

Permalink
Add user-space program to update bootloader
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Nov 22, 2016
1 parent 5e7060f commit dd3e685
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ SELF_OBJECTS = $(patsubst src/%.c,$(BUILD_PATH)/%.o,$(SELF_SOURCES)) $(BUILD_PAT

NAME=uf2-bootloader
EXECUTABLE=$(BUILD_PATH)/$(NAME).bin
SELF_EXECUTABLE=$(BUILD_PATH)/self-$(NAME).bin
SELF_EXECUTABLE=$(BUILD_PATH)/self-$(NAME).uf2

all: dirs $(EXECUTABLE) $(SELF_EXECUTABLE) build/uf2conv

Expand All @@ -73,6 +73,9 @@ wait:
logs:
sh scripts/getlogs.sh $(BUILD_PATH)/$(NAME).map

selflogs:
sh scripts/getlogs.sh $(BUILD_PATH)/self-$(NAME).map

dirs:
@echo "Building $(BOARD)"
-@mkdir -p $(BUILD_PATH)
Expand All @@ -86,12 +89,12 @@ $(EXECUTABLE): $(OBJECTS)
@node -p '"Free space: " + (8192 - require("fs").readFileSync("$(BUILD_PATH)/$(NAME).bin").length)'


$(SELF_EXECUTABLE): $(SELF_OBJECTS)
$(SELF_EXECUTABLE): $(SELF_OBJECTS) build/uf2conv
$(CC) -L$(BUILD_PATH) $(LDFLAGS) \
-T./scripts/samd21j18a_self.ld \
-Wl,-Map,$(BUILD_PATH)/self-$(NAME).map -o $(BUILD_PATH)/self-$(NAME).elf $(SELF_OBJECTS)
arm-none-eabi-objcopy -O binary $(BUILD_PATH)/self-$(NAME).elf $@

arm-none-eabi-objcopy -O binary $(BUILD_PATH)/self-$(NAME).elf $(BUILD_PATH)/self-$(NAME).bin
./build/uf2conv $(BUILD_PATH)/self-$(NAME).bin $@

$(BUILD_PATH)/%.o: src/%.c $(wildcard inc/*.h)
@echo "$<"
Expand Down
5 changes: 4 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
* [x] organize board configs in directories
* [x] if `!USE_CDC && !USE_UART` - don't compile monitor
* [x] if `!USE_CDC` don't compile the CDC code (not only exclude descriptors)
* [ ] write user program for updating bootloader
* [x] write user program for updating bootloader
* [ ] document self-updater
* [ ] write u2fconv in .js
* [ ] investigate some blinking

## Bigger
* [ ] look into reset into bootloader from host to continue flashing
Expand Down
3 changes: 2 additions & 1 deletion inc/uf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define INDEX_URL "https://www.pxt.io/"
#endif

#define UF2_VERSION "v1.0.0"
#define UF2_VERSION "v1.0.5"

// needs to be more than ~4200 (to force FAT16)
#define NUM_FAT_BLOCKS 8000
Expand Down Expand Up @@ -123,6 +123,7 @@ inline void bulb_off(void) { PORT->Group[BULB_PORT].OUTCLR.reg = (1 << BULB_PIN)

extern uint32_t timerHigh, resetHorizon, blinkHorizon;
void timerTick(void);
void delay(uint32_t ms);

#define CONCAT_1(a, b) a ## b
#define CONCAT_0(a, b) CONCAT_1(a, b)
Expand Down
2 changes: 1 addition & 1 deletion scripts/samd21j18a_self.ld
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ SEARCH_DIR(.)
/* Memory Spaces Definitions */
MEMORY
{
rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00020000
rom (rx) : ORIGIN = 0x00002000, LENGTH = 0x00020000
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
}

Expand Down
9 changes: 3 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ static void check_start_application(void) {

/**
* Test reset vector of application @APP_START_ADDRESS+4
* Stay in SAM-BA if *(APP_START+0x4) == 0xFFFFFFFF
* Application erased condition
* Sanity check on the Reset_Handler address
*/
if (app_start_address == 0xFFFFFFFF) {
if (app_start_address < APP_START_ADDRESS || app_start_address > FLASH_SIZE) {
/* Stay in bootloader */
return;
}
Expand All @@ -108,9 +107,7 @@ static void check_start_application(void) {
} else {
if (*DBL_TAP_PTR != DBL_TAP_MAGIC_QUICK_BOOT) {
*DBL_TAP_PTR = DBL_TAP_MAGIC;
for (int i = 1; i < 100000; ++i) {
asm("nop");
}
delay(500);
}
*DBL_TAP_PTR = 0;
}
Expand Down
8 changes: 8 additions & 0 deletions src/selfmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ int main(void) {

logmsg("Update successful!");

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

for (i = 0; i < 20; ++i) {
bulb_toggle();
delay(200);
}

bulb_off();

resetIntoBootloader();
Expand Down
7 changes: 7 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
static uint32_t timerLow;
uint32_t timerHigh, resetHorizon, blinkHorizon;

void delay(uint32_t ms) {
ms <<= 8;
for (int i = 1; i < ms; ++i) {
asm("nop");
}
}

void timerTick(void) {
if (timerLow-- == 0) {
timerLow = TIMER_STEP;
Expand Down

0 comments on commit dd3e685

Please sign in to comment.