Skip to content

Commit

Permalink
supports software reboot selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dukelec committed Feb 18, 2025
1 parent 00f8755 commit c357015
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion mdrv_bl/cdnet
Submodule cdnet updated 2 files
+7 −7 dev/cdctl_it.c
+2 −2 utils/cd_debug.c
2 changes: 1 addition & 1 deletion mdrv_bl/stm32g431cbux_flash.ld
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
RAM (xrw) : ORIGIN = 0x20000004, LENGTH = 32K - 4
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 24K /* 128k */
}

Expand Down
18 changes: 10 additions & 8 deletions mdrv_bl/usr/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
extern SPI_HandleTypeDef hspi2;
extern UART_HandleTypeDef huart3;

uint32_t *bl_args = (uint32_t *)BL_ARGS;
gpio_t led_g = { .group = LED_G_GPIO_Port, .num = LED_G_Pin };
uart_t debug_uart = { .huart = &huart3 };

Expand All @@ -34,12 +35,13 @@ static void device_init(void)
cdn_init_ns(&dft_ns, &packet_free_head, &frame_free_head);

for (i = 0; i < FRAME_MAX; i++)
list_put(&frame_free_head, &frame_alloc[i].node);
cd_list_put(&frame_free_head, &frame_alloc[i]);
for (i = 0; i < PACKET_MAX; i++)
list_put(&packet_free_head, &packet_alloc[i].node);
cdn_list_put(&packet_free_head, &packet_alloc[i]);

cdctl_cfg_t cfg = csa.bus_cfg;
cfg.baud_l = cfg.baud_h = 115200;
if (!csa.keep_in_bl)
cfg.baud_l = cfg.baud_h = 115200;
cdctl_dev_init(&r_dev, &frame_free_head, &cfg, &r_spi, NULL);

// 16MHz / (2 + 2) * (73 + 2) / 2^1 = 150MHz
Expand Down Expand Up @@ -92,22 +94,22 @@ void app_main(void)
bool dbg_en_bk = csa.dbg_en;
csa.dbg_en = false; // silence
debug_init(&dft_ns, &csa.dbg_dst, &csa.dbg_en);
csa.keep_in_bl = *bl_args == 0xcdcd0001;
device_init();
common_service_init();
printf("conf: %s\n", csa.conf_from ? "flash" : "dft");
printf("conf: %s, args: %08lx\n", csa.conf_from ? "flash" : "dft", *bl_args);
gpio_set_val(&led_g, 1);

uint32_t t_last = get_systick();
uint32_t boot_time = get_systick();
bool update_baud = false;
bool update_baud = csa.keep_in_bl;

while (true) {
if (get_systick() - t_last > (update_baud ? 100000 : 200000) / SYSTICK_US_DIV) {
t_last = get_systick();
gpio_set_val(&led_g, !gpio_get_val(&led_g));
}

if (!csa.keep_in_bl && !update_baud && get_systick() - boot_time > 1000000 / SYSTICK_US_DIV) {
if (!csa.keep_in_bl && !update_baud && get_systick() > 1000000 / SYSTICK_US_DIV) {
update_baud = true;
if (csa.bus_cfg.baud_l != 115200 || csa.bus_cfg.baud_h != 115200) {
cdctl_set_baud_rate(&r_dev, csa.bus_cfg.baud_l, csa.bus_cfg.baud_h);
Expand All @@ -118,7 +120,7 @@ void app_main(void)
csa.dbg_en = dbg_en_bk;
}

if (!csa.keep_in_bl && get_systick() - boot_time > 2000000 / SYSTICK_US_DIV)
if (!csa.keep_in_bl && get_systick() > 2000000 / SYSTICK_US_DIV)
jump_to_app();

cdctl_routine(&r_dev);
Expand Down
4 changes: 3 additions & 1 deletion mdrv_bl/usr/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define P_3F(x) (int)(x), abs(((x)-(int)(x))*1000) // "%d.%.3d"


#define BL_ARGS 0x20000000 // first word
#define APP_CONF_ADDR 0x0801f800 // page 63, the last page
#define APP_CONF_VER 0x0100

Expand All @@ -38,7 +39,7 @@ typedef struct {
uint16_t magic_code; // 0xcdcd
uint16_t conf_ver;
uint8_t conf_from; // 0: default, 1: load from flash
bool do_reboot;
uint8_t do_reboot;
bool keep_in_bl;
bool save_conf;

Expand Down Expand Up @@ -70,5 +71,6 @@ void common_service_routine(void);
extern gpio_t led_r;
extern gpio_t led_g;
extern cdn_ns_t dft_ns;
extern uint32_t *bl_args;

#endif
4 changes: 3 additions & 1 deletion mdrv_bl/usr/common_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ void common_service_routine(void)
csa.save_conf = false;
save_conf();
}
if (csa.do_reboot)
if (csa.do_reboot) {
*bl_args = 0xcdcd0000 | csa.do_reboot;
NVIC_SystemReset();
}

p1_service_routine();
p5_service_routine();
Expand Down
2 changes: 1 addition & 1 deletion mdrv_fw/cdnet
Submodule cdnet updated 2 files
+7 −7 dev/cdctl_it.c
+2 −2 utils/cd_debug.c
2 changes: 1 addition & 1 deletion mdrv_fw/stm32g431cbux_flash.ld
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
RAM (xrw) : ORIGIN = 0x20000004, LENGTH = 32K - 4
FLASH (rx) : ORIGIN = 0x8006000, LENGTH = 86K /* 128 -24 (bl) -2 (config) -8 (encoder cali) -8 (anticogging cali) = 86 */
}

Expand Down
4 changes: 2 additions & 2 deletions mdrv_fw/usr/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ static void device_init(void)
cdn_init_ns(&dft_ns, &packet_free_head, &frame_free_head);

for (i = 0; i < FRAME_MAX; i++)
list_put(&frame_free_head, &frame_alloc[i].node);
cd_list_put(&frame_free_head, &frame_alloc[i]);
for (i = 0; i < PACKET_MAX; i++)
list_put(&packet_free_head, &packet_alloc[i].node);
cdn_list_put(&packet_free_head, &packet_alloc[i]);

spi_wr_init(&r_spi);
cdctl_dev_init(&r_dev, &frame_free_head, &csa.bus_cfg, &r_spi, NULL, &r_int, EXTI9_5_IRQn);
Expand Down
3 changes: 2 additions & 1 deletion mdrv_fw/usr/app_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define P_3F(x) (int)(x), abs(((x)-(int)(x))*1000) // "%d.%.3d"


#define BL_ARGS 0x20000000 // first word
#define CALI_ENCODER_TBL 0x0801b800 // 8k, 2bytes x 4096
#define ANTICOGGING_TBL 0x0801d800 // 8k, 2bytes x 4096
#define APP_CONF_ADDR 0x0801f800 // page 63, the last page
Expand Down Expand Up @@ -59,7 +60,7 @@ typedef struct {
uint16_t magic_code; // 0xcdcd
uint16_t conf_ver;
uint8_t conf_from; // 0: default, 1: all from flash, 2: partly from flash
bool do_reboot;
uint8_t do_reboot;
bool _reserved_bl; // keep_in_bl for bl
bool save_conf;

Expand Down
2 changes: 1 addition & 1 deletion mdrv_fw/usr/app_motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void raw_dbg(int idx)
}

if (pkt_raw[idx]->len >= csa.dbg_raw_th) {
list_put(&raw_pend, &pkt_raw[idx]->node);
cdn_list_put(&raw_pend, pkt_raw[idx]);
pkt_raw[idx] = NULL;
}
}
Expand Down
1 change: 1 addition & 0 deletions mdrv_fw/usr/common_services.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ void common_service_routine(void)
save_conf();
}
if (csa.do_reboot) {
*(uint32_t *)BL_ARGS = 0xcdcd0000 | csa.do_reboot;
gpio_set_val(&drv_en, 0);
NVIC_SystemReset();
}
Expand Down
2 changes: 1 addition & 1 deletion mdrv_fw/usr/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void csa_list_show(void)
CSA_SHOW(1, magic_code, "Magic code: 0xcdcd");
CSA_SHOW(1, conf_ver, "Config version");
CSA_SHOW(1, conf_from, "0: default config, 1: all from flash, 2: partly from flash");
CSA_SHOW(0, do_reboot, "Write 1 to reboot");
CSA_SHOW(0, do_reboot, "1: reboot to bl, 2: reboot to app");
CSA_SHOW(0, save_conf, "Write 1 to save current config to flash");
d_info("\n"); debug_flush(true);

Expand Down

0 comments on commit c357015

Please sign in to comment.