Skip to content

Commit 94186c8

Browse files
committed
Implement boot-up commands; run main script after boot.
1 parent 9fc7933 commit 94186c8

2 files changed

Lines changed: 109 additions & 48 deletions

File tree

stm/lib/usbd_storage_msd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ int8_t STORAGE_Write (uint8_t lun,
308308
while (SD_GetStatus() != SD_TRANSFER_OK);
309309
#endif
310310
*/
311-
//disk_write(0, buf, blk_addr, blk_len);
311+
disk_write(0, buf, blk_addr, blk_len);
312312
return (0);
313313
}
314314

stm/main.c

Lines changed: 108 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@
1616
#include "usb.h"
1717
#include "ff.h"
1818

19+
static FATFS fatfs0;
20+
1921
extern uint32_t _heap_start;
2022

23+
void flash_error(int n) {
24+
for (int i = 0; i < n; i++) {
25+
led_state(PYB_LED_R1, 1);
26+
led_state(PYB_LED_R2, 0);
27+
sys_tick_delay_ms(250);
28+
led_state(PYB_LED_R1, 0);
29+
led_state(PYB_LED_R2, 1);
30+
sys_tick_delay_ms(250);
31+
}
32+
led_state(PYB_LED_R2, 0);
33+
}
34+
2135
static void impl02_c_version() {
2236
int x = 0;
2337
while (x < 400) {
@@ -58,14 +72,8 @@ int sw_get() {
5872
void __fatal_error(const char *msg) {
5973
lcd_print_strn("\nFATAL ERROR:\n", 14);
6074
lcd_print_strn(msg, strlen(msg));
61-
6275
for (;;) {
63-
led_state(PYB_LED_R1, 1);
64-
led_state(PYB_LED_R2, 0);
65-
sys_tick_delay_ms(150);
66-
led_state(PYB_LED_R1, 0);
67-
led_state(PYB_LED_R2, 1);
68-
sys_tick_delay_ms(150);
76+
flash_error(1);
6977
}
7078
}
7179

@@ -79,16 +87,27 @@ void __fatal_error(const char *msg) {
7987
#include "runtime.h"
8088
#include "repl.h"
8189

90+
static qstr pyb_config_source_dir = 0;
91+
static qstr pyb_config_main = 0;
92+
8293
py_obj_t pyb_source_dir(py_obj_t source_dir) {
94+
pyb_config_source_dir = py_get_qstr(source_dir);
8395
return py_const_none;
8496
}
8597

8698
py_obj_t pyb_main(py_obj_t main) {
99+
pyb_config_main = py_get_qstr(main);
100+
return py_const_none;
101+
}
102+
103+
// sync all file systems
104+
py_obj_t pyb_sync() {
105+
storage_flush();
87106
return py_const_none;
88107
}
89108

90109
py_obj_t pyb_delay(py_obj_t count) {
91-
sys_tick_delay_ms(rt_get_int(count));
110+
sys_tick_delay_ms(py_get_int(count));
92111
return py_const_none;
93112
}
94113

@@ -105,8 +124,6 @@ py_obj_t pyb_sw() {
105124
}
106125
}
107126

108-
FATFS fatfs0;
109-
110127
/*
111128
void g(uint i) {
112129
printf("g:%d\n", i);
@@ -185,27 +202,27 @@ static py_obj_t pyb_info() {
185202
printf("_heap_start=%p\n", &_heap_start);
186203
}
187204

205+
// GC info
206+
{
207+
gc_info_t info;
208+
gc_info(&info);
209+
printf("GC:\n");
210+
printf(" %lu total\n", info.total);
211+
printf(" %lu : %lu\n", info.used, info.free);
212+
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
213+
}
214+
188215
// free space on flash
189216
{
190217
DWORD nclst;
191218
FATFS *fatfs;
192219
f_getfree("0:", &nclst, &fatfs);
193-
printf("free=%u\n", (uint)(nclst * fatfs->csize * 512));
220+
printf("LFS free: %u bytes\n", (uint)(nclst * fatfs->csize * 512));
194221
}
195222

196223
return py_const_none;
197224
}
198225

199-
/*
200-
void gc_print_info() {
201-
gc_info_t info;
202-
gc_info(&info);
203-
printf("! %lu total\n", info.total);
204-
printf("! %lu : %lu\n", info.used, info.free);
205-
printf("! 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
206-
}
207-
*/
208-
209226
int readline(vstr_t *line, const char *prompt) {
210227
usb_vcp_send_str(prompt);
211228
int len = vstr_len(line);
@@ -287,7 +304,46 @@ void do_repl() {
287304
}
288305
}
289306

290-
usb_vcp_send_str("\r\nMicro Python REPL finished\r\n");
307+
usb_vcp_send_str("\r\n");
308+
}
309+
310+
bool do_file(const char *filename) {
311+
py_lexer_file_buf_t fb;
312+
py_lexer_t *lex = py_lexer_new_from_file(filename, &fb);
313+
314+
if (lex == NULL) {
315+
printf("could not open file '%s' for reading\n", filename);
316+
return false;
317+
}
318+
319+
py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
320+
py_lexer_free(lex);
321+
322+
if (pn == PY_PARSE_NODE_NULL) {
323+
return false;
324+
}
325+
326+
bool comp_ok = py_compile(pn, false);
327+
if (!comp_ok) {
328+
return false;
329+
}
330+
331+
py_obj_t module_fun = rt_make_function_from_id(1);
332+
if (module_fun == py_const_none) {
333+
return false;
334+
}
335+
336+
nlr_buf_t nlr;
337+
if (nlr_push(&nlr) == 0) {
338+
rt_call_function_0(module_fun);
339+
nlr_pop();
340+
return true;
341+
} else {
342+
// uncaught exception
343+
py_obj_print((py_obj_t)nlr.ret_val);
344+
printf("\n");
345+
return false;
346+
}
291347
}
292348

293349
#define RAM_START (0x20000000) // fixed for chip
@@ -356,6 +412,7 @@ int main() {
356412
rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
357413
rt_store_attr(m, qstr_from_str_static("source_dir"), rt_make_function_1(pyb_source_dir));
358414
rt_store_attr(m, qstr_from_str_static("main"), rt_make_function_1(pyb_main));
415+
rt_store_attr(m, qstr_from_str_static("sync"), rt_make_function_0(pyb_sync));
359416
rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc));
360417
rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
361418
rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led));
@@ -426,38 +483,38 @@ int main() {
426483
}
427484
}
428485

486+
// run /boot.py
487+
if (!do_file("0:/boot.py")) {
488+
flash_error(4);
489+
}
490+
429491
// USB
430492
usb_init();
431493

432-
// run /boot.py
433-
if (1) {
434-
py_lexer_file_buf_t fb;
435-
py_lexer_t *lex = py_lexer_new_from_file("0:/boot.py", &fb);
436-
py_parse_node_t pn = py_parse(lex, PY_PARSE_FILE_INPUT);
437-
py_lexer_free(lex);
494+
// turn boot-up LED off
495+
led_state(PYB_LED_G1, 0);
438496

439-
if (pn != PY_PARSE_NODE_NULL) {
440-
bool comp_ok = py_compile(pn, true);
441-
if (comp_ok) {
442-
py_obj_t module_fun = rt_make_function_from_id(1);
443-
if (module_fun != py_const_none) {
444-
nlr_buf_t nlr;
445-
if (nlr_push(&nlr) == 0) {
446-
rt_call_function_0(module_fun);
447-
nlr_pop();
448-
} else {
449-
// uncaught exception
450-
py_obj_print((py_obj_t)nlr.ret_val);
451-
printf("\n");
452-
}
453-
}
454-
}
497+
// run main script
498+
{
499+
vstr_t *vstr = vstr_new();
500+
vstr_add_str(vstr, "0:/");
501+
if (pyb_config_source_dir == 0) {
502+
vstr_add_str(vstr, "src");
503+
} else {
504+
vstr_add_str(vstr, qstr_str(pyb_config_source_dir));
505+
}
506+
vstr_add_char(vstr, '/');
507+
if (pyb_config_main == 0) {
508+
vstr_add_str(vstr, "main.py");
509+
} else {
510+
vstr_add_str(vstr, qstr_str(pyb_config_main));
511+
}
512+
if (!do_file(vstr_str(vstr))) {
513+
flash_error(3);
455514
}
515+
vstr_free(vstr);
456516
}
457517

458-
// turn boot-up LED off
459-
led_state(PYB_LED_G1, 0);
460-
461518
//printf("init;al=%u\n", m_get_total_bytes_allocated()); // 1600, due to qstr_init
462519
//sys_tick_delay_ms(1000);
463520

@@ -676,5 +733,9 @@ int main() {
676733
//sdio_init();
677734
}
678735

736+
printf("PYB: sync filesystems\n");
737+
pyb_sync();
738+
739+
printf("PYB: soft reboot\n");
679740
goto soft_reset;
680741
}

0 commit comments

Comments
 (0)