@@ -77,6 +77,15 @@ void __fatal_error(const char *msg) {
7777#include "parse.h"
7878#include "compile.h"
7979#include "runtime.h"
80+ #include "repl.h"
81+
82+ py_obj_t pyb_source_dir (py_obj_t source_dir ) {
83+ return py_const_none ;
84+ }
85+
86+ py_obj_t pyb_main (py_obj_t main ) {
87+ return py_const_none ;
88+ }
8089
8190py_obj_t pyb_delay (py_obj_t count ) {
8291 sys_tick_delay_ms (rt_get_int (count ));
@@ -98,7 +107,6 @@ py_obj_t pyb_sw() {
98107
99108FATFS fatfs0 ;
100109
101-
102110/*
103111void g(uint i) {
104112 printf("g:%d\n", i);
@@ -149,7 +157,7 @@ static const char fresh_boot_py[] =
149157;
150158
151159// get lots of info about the board
152- static void board_info () {
160+ static py_obj_t pyb_info () {
153161 // get and print clock speeds
154162 // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
155163 {
@@ -184,75 +192,79 @@ static void board_info() {
184192 f_getfree ("0:" , & nclst , & fatfs );
185193 printf ("free=%u\n" , (uint )(nclst * fatfs -> csize * 512 ));
186194 }
195+
196+ return py_const_none ;
187197}
188198
189- char * readline (const char * prompt ) {
190- vstr_t vstr ;
191- vstr_init (& vstr );
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+
209+ int readline (vstr_t * line , const char * prompt ) {
192210 usb_vcp_send_str (prompt );
211+ int len = vstr_len (line );
193212 for (;;) {
194- //extern int rx_buf_in;
195- //extern int rx_buf_out;
196213 while (usb_vcp_rx_any () == 0 ) {
197- //printf("nope %x %x\n", rx_buf_in, rx_buf_out);
198214 sys_tick_delay_ms (10 );
199215 }
200216 char c = usb_vcp_rx_get ();
201- if (c == 4 && vstr_len (& vstr ) == 0 ) {
202- return NULL ;
217+ if (c == 4 && vstr_len (line ) == len ) {
218+ return 0 ;
203219 } else if (c == '\r' ) {
204220 usb_vcp_send_str ("\r\n" );
205- return vstr_str ( & vstr ) ;
221+ return 1 ;
206222 } else if (c == 127 ) {
207- if (vstr_len (& vstr ) > 0 ) {
208- vstr_cut_tail (& vstr , 1 );
223+ if (vstr_len (line ) > len ) {
224+ vstr_cut_tail (line , 1 );
209225 usb_vcp_send_str ("\b \b" );
210226 }
211227 } else if (32 <= c && c <= 126 ) {
212- vstr_add_char (& vstr , c );
228+ vstr_add_char (line , c );
213229 usb_vcp_send_strn (& c , 1 );
214230 }
215231 sys_tick_delay_ms (100 );
216232 }
217- return NULL ;
218- }
219-
220- /*
221- void gc_print_info() {
222- gc_info_t info;
223- gc_info(&info);
224- printf("! %lu total\n", info.total);
225- printf("! %lu : %lu\n", info.used, info.free);
226- printf("! 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
227233}
228- */
229234
230235void do_repl () {
231- usb_vcp_send_str ("Micro Python\r\n" );
236+ usb_vcp_send_str ("Micro Python 0.5; STM32F405RG; PYBv2\r\n" );
237+ usb_vcp_send_str ("Type \"help\" for more information.\r\n" );
238+
239+ vstr_t line ;
240+ vstr_init (& line );
232241
233242 for (;;) {
234- char * line = readline (">>> " );
235- if (line == NULL ) {
243+ vstr_reset (& line );
244+ int ret = readline (& line , ">>> " );
245+ if (ret == 0 ) {
236246 // EOF
237- return ;
247+ break ;
248+ }
249+
250+ if (vstr_len (& line ) == 0 ) {
251+ continue ;
238252 }
239- /*
240- if (is_compound_stmt( line)) {
253+
254+ if (py_repl_is_compound_stmt ( vstr_str ( & line ) )) {
241255 for (;;) {
242- char *line2 = readline("... ");
243- if (line2 == NULL || strlen(line2) == 0) {
256+ vstr_add_char (& line , '\n' );
257+ int len = vstr_len (& line );
258+ int ret = readline (& line , "... " );
259+ if (ret == 0 || vstr_len (& line ) == len ) {
260+ // done entering compound statement
244261 break ;
245262 }
246- char *line3 = str_join(line, '\n', line2);
247- m_free(line);
248- m_free(line2);
249- line = line3;
250263 }
251264 }
252- */
253265
254266 py_lexer_str_buf_t sb ;
255- py_lexer_t * lex = py_lexer_new_from_str_len ("<stdin>" , line , strlen ( line ), false, & sb );
267+ py_lexer_t * lex = py_lexer_new_from_str_len ("<stdin>" , vstr_str ( & line ), vstr_len ( & line ), false, & sb );
256268 py_parse_node_t pn = py_parse (lex , PY_PARSE_SINGLE_INPUT );
257269 py_lexer_free (lex );
258270
@@ -274,6 +286,8 @@ void do_repl() {
274286 }
275287 }
276288 }
289+
290+ usb_vcp_send_str ("\r\nMicro Python REPL finished\r\n" );
277291}
278292
279293#define RAM_START (0x20000000) // fixed for chip
@@ -299,7 +313,7 @@ void gc_collect() {
299313 printf (" 1=%lu 2=%lu m=%lu\n" , info .num_1block , info .num_2block , info .max_block );
300314}
301315
302- py_obj_t py_gc_collect () {
316+ py_obj_t pyb_gc () {
303317 gc_collect ();
304318 return py_const_none ;
305319}
@@ -322,24 +336,35 @@ int main() {
322336
323337 // more sub-system init
324338 sw_init ();
325- lcd_init ();
326339 storage_init ();
327340
341+ soft_reset :
342+
343+ // LCD init
344+ lcd_init ();
345+
328346 // GC init
329347 gc_init (& _heap_start , (void * )HEAP_END );
330348
331- // Python init
349+ // Micro Python init
332350 qstr_init ();
333351 rt_init ();
334352
335353 // add some functions to the python namespace
336- rt_store_name (qstr_from_str_static ("gc" ), rt_make_function_0 (py_gc_collect ));
337- rt_store_name (qstr_from_str_static ("pyb_delay" ), rt_make_function_1 (pyb_delay ));
338- rt_store_name (qstr_from_str_static ("pyb_led" ), rt_make_function_1 (pyb_led ));
339- rt_store_name (qstr_from_str_static ("pyb_sw" ), rt_make_function_0 (pyb_sw ));
354+ {
355+ py_obj_t m = py_module_new ();
356+ rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
357+ rt_store_attr (m , qstr_from_str_static ("source_dir" ), rt_make_function_1 (pyb_source_dir ));
358+ rt_store_attr (m , qstr_from_str_static ("main" ), rt_make_function_1 (pyb_main ));
359+ rt_store_attr (m , qstr_from_str_static ("gc" ), rt_make_function_0 (pyb_gc ));
360+ rt_store_attr (m , qstr_from_str_static ("delay" ), rt_make_function_1 (pyb_delay ));
361+ rt_store_attr (m , qstr_from_str_static ("led" ), rt_make_function_1 (pyb_led ));
362+ rt_store_attr (m , qstr_from_str_static ("sw" ), rt_make_function_0 (pyb_sw ));
363+ rt_store_name (qstr_from_str_static ("pyb" ), m );
364+ }
340365
341- // print a message
342- printf (" micro py board\n" );
366+ // print a message to the LCD
367+ lcd_print_str (" micro py board\n" );
343368
344369 // local filesystem init
345370 {
@@ -391,7 +416,7 @@ int main() {
391416 FIL fp ;
392417 f_open (& fp , "0:/boot.py" , FA_WRITE | FA_CREATE_ALWAYS );
393418 UINT n ;
394- f_write (& fp , fresh_boot_py , sizeof (fresh_boot_py ), & n );
419+ f_write (& fp , fresh_boot_py , sizeof (fresh_boot_py ) - 1 /* don't count null terminator */ , & n );
395420 // TODO check we could write n bytes
396421 f_close (& fp );
397422
@@ -401,8 +426,11 @@ int main() {
401426 }
402427 }
403428
429+ // USB
430+ usb_init ();
431+
404432 // run /boot.py
405- if (0 ) {
433+ if (1 ) {
406434 py_lexer_file_buf_t fb ;
407435 py_lexer_t * lex = py_lexer_new_from_file ("0:/boot.py" , & fb );
408436 py_parse_node_t pn = py_parse (lex , PY_PARSE_FILE_INPUT );
@@ -430,11 +458,6 @@ int main() {
430458 // turn boot-up LED off
431459 led_state (PYB_LED_G1 , 0 );
432460
433- // USB
434- if (1 ) {
435- usb_init ();
436- }
437-
438461 //printf("init;al=%u\n", m_get_total_bytes_allocated()); // 1600, due to qstr_init
439462 //sys_tick_delay_ms(1000);
440463
@@ -653,31 +676,5 @@ int main() {
653676 //sdio_init();
654677 }
655678
656- int i = 0 ;
657- int n = 0 ;
658- uint32_t stc = sys_tick_counter ;
659-
660- for (;;) {
661- sys_tick_delay_ms (10 );
662- if (sw_get ()) {
663- led_state (PYB_LED_G1 , 1 );
664- i = 1 - i ;
665- if (i ) {
666- printf (" angel %05x.\n" , n );
667- //usb_vcp_send("hello!\r\n", 8);
668- } else {
669- printf (" mishka %4u.\n" , n );
670- //usb_vcp_send("angel!\r\n", 8);
671- }
672- n += 1 ;
673- } else {
674- led_state (PYB_LED_G1 , 0 );
675- }
676- if (sys_tick_has_passed (stc , 500 )) {
677- stc += 500 ;
678- led_toggle (PYB_LED_G2 );
679- }
680- }
681-
682- return 0 ;
679+ goto soft_reset ;
683680}
0 commit comments