Skip to content

Commit c5adbda

Browse files
facchinmsoburi
authored andcommitted
unoq: replace flash_mode with wait_for_app
Flash mode (RAM) has always been a hack; get rid of it with a better syncronization with arduino-router and arduino-appl-cli. If compiled in wait_for_app mode, the sketch will wait until a command like /opt/openocd/bin/openocd -d2 -s /opt/openocd -s /opt/openocd/share/openocd/scripts -f openocd_gpiod.cfg -c "init; mwb 0x40036400 8; shutdown" is issued by the Linux side. The storage in BKPSRAM is always assured to be at offset 0 If using remoteocd, no extra command is required
1 parent 22ef7cb commit c5adbda

8 files changed

Lines changed: 29 additions & 47 deletions

File tree

boards.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
menu.debug=Debug
22
menu.link_mode=Link mode
3-
menu.flash_mode=Flash mode
43
menu.wait_linux_boot=Startup mode
54

65
##########################################################################################
@@ -706,13 +705,11 @@ unoq.menu.link_mode.static=Static
706705
unoq.menu.link_mode.static.build.link_mode=static
707706
unoq.menu.link_mode.static.upload.extension=bin-zsk.bin
708707

709-
unoq.menu.flash_mode.flash=Flash
710-
unoq.menu.flash_mode.flash.openocd_cfg=flash_sketch.cfg
711-
unoq.menu.flash_mode.ram=RAM
712-
unoq.menu.flash_mode.ram.openocd_cfg=flash_sketch_ram.cfg
713708
unoq.menu.wait_linux_boot.yes=Wait for Linux
714709
unoq.menu.wait_linux_boot.no=Immediate
715710
unoq.menu.wait_linux_boot.no.build.boot_mode=immediate
711+
unoq.menu.wait_linux_boot.app=Wait for App
712+
unoq.menu.wait_linux_boot.app.build.boot_mode=app
716713

717714
unoq.build.zephyr_target=arduino_uno_q
718715
unoq.build.zephyr_args=
@@ -724,6 +721,8 @@ unoq.build.fpu=-mfpu=fpv5-sp-d16
724721
unoq.build.architecture=cortex-m33
725722
unoq.compiler.zephyr.arch.define=
726723

724+
unoq.openocd_cfg=flash_sketch.cfg
725+
727726
unoq.build.float-abi=-mfloat-abi=hard
728727
unoq.build.extra_flags=
729728
unoq.build.postbuild.cmd="{tools.imgtool.path}/{tools.imgtool.cmd}" exit

extra/artifacts/_common.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{
3131
"packager": "arduino",
3232
"name": "zephyr-sketch-tool",
33-
"version": "0.2.1"
33+
"version": "0.3.0"
3434
},
3535
{
3636
"packager": "arduino",

loader/main.c

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ struct sketch_header_v1 {
3434
uint8_t flags; // @ 0x0e
3535
} __attribute__((packed));
3636

37-
#define SKETCH_FLAG_DEBUG 0x01
38-
#define SKETCH_FLAG_LINKED 0x02
39-
#define SKETCH_FLAG_IMMEDIATE 0x04
37+
#define SKETCH_FLAG_DEBUG 0x01
38+
#define SKETCH_FLAG_LINKED 0x02
39+
#define SKETCH_FLAG_IMMEDIATE 0x04
40+
#define SKETCH_FLAG_WAIT_FOR_APP 0x08
4041

4142
#define SKETCH_RAM_BUFFER_LEN 131072
4243

@@ -110,6 +111,11 @@ __attribute__((retain)) const uintptr_t sketch_max_size = DT_REG_SIZE(DT_NODELAB
110111
#endif
111112
__attribute__((retain)) const uintptr_t loader_max_size = LOADER_MAX_SIZE;
112113

114+
struct backup_store {
115+
uint32_t wait_for_app_magic;
116+
};
117+
volatile __stm32_backup_sram_section struct backup_store backup;
118+
113119
static int loader(const struct shell *sh) {
114120
const struct flash_area *fa;
115121
int rc;
@@ -194,6 +200,8 @@ static int loader(const struct shell *sh) {
194200
char buf_loop;
195201
};
196202

203+
backup.wait_for_app_magic = 0;
204+
197205
uintptr_t bootanimation_addr = DT_REG_ADDR(DT_GPARENT(DT_NODELABEL(bootanimation))) +
198206
DT_REG_ADDR(DT_NODELABEL(bootanimation));
199207

@@ -213,17 +221,6 @@ static int loader(const struct shell *sh) {
213221

214222
gpio_pin_configure_dt(&spec, GPIO_INPUT | GPIO_PULL_DOWN);
215223
k_sleep(K_MSEC(200));
216-
uint8_t *ram_firmware = NULL;
217-
uint32_t *ram_start = (uint32_t *)0x20000000;
218-
if (!sketch_valid) {
219-
ram_firmware = (uint8_t *)malloc(SKETCH_RAM_BUFFER_LEN);
220-
if (!ram_firmware) {
221-
printk("Failed to allocate RAM for firmware\n");
222-
return -ENOMEM;
223-
}
224-
memset(ram_firmware, 0, SKETCH_RAM_BUFFER_LEN);
225-
*ram_start = (uint32_t)&ram_firmware[0];
226-
}
227224
if (gpio_pin_get_dt(&spec) == 0) {
228225
matrixBegin();
229226
matrixSetGrayscaleBits(8);
@@ -236,15 +233,10 @@ static int loader(const struct shell *sh) {
236233
k_sleep(K_MSEC(10));
237234
matrixEnd();
238235
}
239-
while (!sketch_valid) {
240-
__asm__("bkpt");
241-
// poll the first bytes, if filled try to use them for booting
242-
sketch_hdr = (struct sketch_header_v1 *)(ram_firmware + 7);
243-
if (sketch_hdr->ver == 0x1 && sketch_hdr->magic == 0x2341) {
244-
// Found valid data, use it for booting
245-
base_addr = (uintptr_t)ram_firmware;
246-
*ram_start = 0;
247-
sketch_valid = true;
236+
237+
if (sketch_hdr->flags & SKETCH_FLAG_WAIT_FOR_APP) {
238+
while (backup.wait_for_app_magic == 0) {
239+
k_sleep(K_MSEC(100));
248240
}
249241
}
250242
}

platform.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ build.zsk_args.debug=
7777
build.zsk_args.mode-dynamic=
7878
build.zsk_args.mode-static=-prelinked
7979
build.zsk_args.startup-mode-wait=
80+
build.zsk_args.startup-mode-app=-wait_for_app
8081
build.zsk_args.startup-mode-immediate=-immediate
8182

8283
# These can be overridden in platform.local.txt

variants/arduino_uno_q_stm32u585xx/arduino_uno_q_stm32u585xx.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ CONFIG_DYNAMIC_THREAD_ALLOC=y
4141

4242
CONFIG_MAIN_THREAD_PRIORITY=5
4343

44+
CONFIG_STM32_BACKUP_SRAM=y
45+
4446
#CONFIG_VIDEO=y
4547
#CONFIG_VIDEO_LOG_LEVEL_DBG=y
4648
#CONFIG_VIDEO_STM32_DCMI=y

variants/arduino_uno_q_stm32u585xx/arduino_uno_q_stm32u585xx.overlay

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
pinctrl-names = "default";
4545
};
4646

47+
&backup_sram {
48+
status = "okay";
49+
};
50+
4751
/*
4852
&dcmi {
4953
status = "okay";

variants/arduino_uno_q_stm32u585xx/flash_sketch.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ halt
88
flash info 0
99
flash write_image erase ${filename} 0x8100000 bin
1010
reset
11+
sleep 100
12+
mww 0x40036400 0xCAFFEEEE
1113
shutdown

variants/arduino_uno_q_stm32u585xx/flash_sketch_ram.cfg

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)