@@ -122,6 +122,7 @@ void __fatal_error(const char *msg) {
122
122
#include "parse.h"
123
123
#include "compile.h"
124
124
#include "runtime.h"
125
+ #include "obj.h"
125
126
#include "repl.h"
126
127
127
128
static qstr pyb_config_source_dir = 0 ;
@@ -153,9 +154,11 @@ py_obj_t pyb_led(py_obj_t state) {
153
154
return state ;
154
155
}
155
156
156
- py_obj_t py_obj_new_user (const py_user_info_t * info , machine_uint_t data1 , machine_uint_t data2 );
157
- void py_user_get_data (py_obj_t o , machine_uint_t * data1 , machine_uint_t * data2 );
158
- void py_user_set_data (py_obj_t o , machine_uint_t data1 , machine_uint_t data2 );
157
+ void led_obj_print (py_obj_t self ) {
158
+ machine_uint_t led_id ;
159
+ py_user_get_data (self , & led_id , NULL );
160
+ printf ("<LED %lu>" , led_id );
161
+ }
159
162
160
163
py_obj_t led_obj_on (py_obj_t self ) {
161
164
machine_uint_t led_id ;
@@ -179,7 +182,7 @@ py_obj_t led_obj_off(py_obj_t self) {
179
182
180
183
const py_user_info_t led_obj_info = {
181
184
"Led" ,
182
- NULL , // print
185
+ led_obj_print ,
183
186
{
184
187
{"on" , 0 , led_obj_on },
185
188
{"off" , 0 , led_obj_off },
@@ -199,6 +202,12 @@ py_obj_t pyb_sw(void) {
199
202
}
200
203
}
201
204
205
+ void servo_obj_print (py_obj_t self ) {
206
+ machine_uint_t servo_id ;
207
+ py_user_get_data (self , & servo_id , NULL );
208
+ printf ("<Servo %lu>" , servo_id );
209
+ }
210
+
202
211
py_obj_t servo_obj_angle (py_obj_t self , py_obj_t angle ) {
203
212
machine_uint_t servo_id ;
204
213
py_user_get_data (self , & servo_id , NULL );
@@ -216,7 +225,7 @@ py_obj_t servo_obj_angle(py_obj_t self, py_obj_t angle) {
216
225
217
226
const py_user_info_t servo_obj_info = {
218
227
"Servo" ,
219
- NULL , // print
228
+ servo_obj_print ,
220
229
{
221
230
{"angle" , 1 , servo_obj_angle },
222
231
{NULL , 0 , NULL },
@@ -276,8 +285,40 @@ static const char fresh_boot_py[] =
276
285
"#pyb.error_log('error.txt')\n"
277
286
;
278
287
288
+ static const char fresh_main_py [] =
289
+ "# main.py -- put your code here!\n"
290
+ ;
291
+
292
+ static const char * help_text =
293
+ "Welcome to Micro Python!\n\n"
294
+ "This is a *very* early version of Micro Python and has minimal functionality.\n\n"
295
+ "Specific commands for the board:\n"
296
+ " pyb.info() -- print some general information\n"
297
+ " pyb.gc() -- run the garbage collector\n"
298
+ " pyb.delay(<n>) -- wait for n milliseconds\n"
299
+ " pyb.Led(<n>) -- create Led object for LED n (n=1,2)\n"
300
+ " Led methods: on(), off()\n"
301
+ " pyb.Servo(<n>) -- create Servo object for servo n (n=1,2,3,4)\n"
302
+ " Servo methods: angle(<x>)\n"
303
+ " pyb.switch() -- return True/False if switch pressed or not\n"
304
+ " pyb.accel() -- get accelerometer values\n"
305
+ " pyb.rand() -- get a 16-bit random number\n"
306
+ ;
307
+
308
+ // get some help about available functions
309
+ static py_obj_t pyb_help (void ) {
310
+ printf ("%s" , help_text );
311
+ return py_const_none ;
312
+ }
313
+
279
314
// get lots of info about the board
280
315
static py_obj_t pyb_info (void ) {
316
+ // get and print unique id; 96 bits
317
+ {
318
+ byte * id = (byte * )0x1fff7a10 ;
319
+ printf ("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n" , id [0 ], id [1 ], id [2 ], id [3 ], id [4 ], id [5 ], id [6 ], id [7 ], id [8 ], id [9 ], id [10 ], id [11 ]);
320
+ }
321
+
281
322
// get and print clock speeds
282
323
// SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
283
324
{
@@ -478,8 +519,8 @@ int readline(vstr_t *line, const char *prompt) {
478
519
}
479
520
480
521
void do_repl (void ) {
481
- stdout_tx_str ("Micro Python 0.5 ; STM32F405RG; PYBv2 \r\n" );
482
- stdout_tx_str ("Type \"help\" for more information.\r\n" );
522
+ stdout_tx_str ("Micro Python 0.1 ; STM32F405RG; PYBv3 \r\n" );
523
+ stdout_tx_str ("Type \"help() \" for more information.\r\n" );
483
524
484
525
vstr_t line ;
485
526
vstr_init (& line );
@@ -523,8 +564,11 @@ void do_repl(void) {
523
564
if (nlr_push (& nlr ) == 0 ) {
524
565
rt_call_function_0 (module_fun );
525
566
nlr_pop ();
526
- uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
527
- //printf("(took %lu ms)\n", ticks);
567
+ // optional timing
568
+ if (0 ) {
569
+ uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
570
+ printf ("(took %lu ms)\n" , ticks );
571
+ }
528
572
} else {
529
573
// uncaught exception
530
574
py_obj_print ((py_obj_t )nlr .ret_val );
@@ -592,12 +636,16 @@ void gc_collect(void) {
592
636
gc_collect_root ((void * * )HEAP_END , (RAM_END - HEAP_END ) / 4 ); // will trace regs since they now live in this function on the stack
593
637
gc_collect_end ();
594
638
uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
595
- gc_info_t info ;
596
- gc_info (& info );
597
- printf ("GC@%lu %lums\n" , start , ticks );
598
- printf (" %lu total\n" , info .total );
599
- printf (" %lu : %lu\n" , info .used , info .free );
600
- printf (" 1=%lu 2=%lu m=%lu\n" , info .num_1block , info .num_2block , info .max_block );
639
+
640
+ if (0 ) {
641
+ // print GC info
642
+ gc_info_t info ;
643
+ gc_info (& info );
644
+ printf ("GC@%lu %lums\n" , start , ticks );
645
+ printf (" %lu total\n" , info .total );
646
+ printf (" %lu : %lu\n" , info .used , info .free );
647
+ printf (" 1=%lu 2=%lu m=%lu\n" , info .num_1block , info .num_2block , info .max_block );
648
+ }
601
649
}
602
650
603
651
py_obj_t pyb_gc (void ) {
@@ -951,13 +999,15 @@ int main(void) {
951
999
timer_init ();
952
1000
953
1001
// RNG
954
- if (0 ) {
1002
+ if (1 ) {
955
1003
RCC_AHB2PeriphClockCmd (RCC_AHB2Periph_RNG , ENABLE );
956
1004
RNG_Cmd (ENABLE );
957
1005
}
958
1006
959
1007
// add some functions to the python namespace
960
1008
{
1009
+ rt_store_name (qstr_from_str_static ("help" ), rt_make_function_0 (pyb_help ));
1010
+
961
1011
py_obj_t m = py_module_new ();
962
1012
rt_store_attr (m , qstr_from_str_static ("info" ), rt_make_function_0 (pyb_info ));
963
1013
rt_store_attr (m , qstr_from_str_static ("stop" ), rt_make_function_0 (pyb_stop ));
@@ -968,16 +1018,16 @@ int main(void) {
968
1018
rt_store_attr (m , qstr_from_str_static ("gc" ), rt_make_function_0 (pyb_gc ));
969
1019
rt_store_attr (m , qstr_from_str_static ("delay" ), rt_make_function_1 (pyb_delay ));
970
1020
rt_store_attr (m , qstr_from_str_static ("led" ), rt_make_function_1 (pyb_led ));
971
- rt_store_attr (m , qstr_from_str_static ("sw " ), rt_make_function_0 (pyb_sw ));
1021
+ rt_store_attr (m , qstr_from_str_static ("switch " ), rt_make_function_0 (pyb_sw ));
972
1022
rt_store_attr (m , qstr_from_str_static ("servo" ), rt_make_function_2 (pyb_servo_set ));
973
1023
rt_store_attr (m , qstr_from_str_static ("pwm" ), rt_make_function_2 (pyb_pwm_set ));
974
- rt_store_attr (m , qstr_from_str_static ("mma " ), rt_make_function_0 (pyb_mma_read ));
1024
+ rt_store_attr (m , qstr_from_str_static ("accel " ), rt_make_function_0 (pyb_mma_read ));
975
1025
rt_store_attr (m , qstr_from_str_static ("hid" ), rt_make_function_1 (pyb_hid_send_report ));
976
1026
rt_store_attr (m , qstr_from_str_static ("time" ), rt_make_function_0 (pyb_rtc_read ));
977
1027
rt_store_attr (m , qstr_from_str_static ("uout" ), rt_make_function_1 (pyb_usart_send ));
978
1028
rt_store_attr (m , qstr_from_str_static ("uin" ), rt_make_function_0 (pyb_usart_receive ));
979
1029
rt_store_attr (m , qstr_from_str_static ("ustat" ), rt_make_function_0 (pyb_usart_status ));
980
- rt_store_attr (m , qstr_from_str_static ("rng " ), rt_make_function_0 (pyb_rng_get ));
1030
+ rt_store_attr (m , qstr_from_str_static ("rand " ), rt_make_function_0 (pyb_rng_get ));
981
1031
rt_store_attr (m , qstr_from_str_static ("Led" ), rt_make_function_1 (pyb_Led ));
982
1032
rt_store_attr (m , qstr_from_str_static ("Servo" ), rt_make_function_1 (pyb_Servo ));
983
1033
rt_store_name (qstr_from_str_static ("pyb" ), m );
@@ -1022,6 +1072,18 @@ int main(void) {
1022
1072
__fatal_error ("could not create LFS" );
1023
1073
}
1024
1074
1075
+ // create src directory
1076
+ res = f_mkdir ("0:/src" );
1077
+ // ignore result from mkdir
1078
+
1079
+ // create empty main.py
1080
+ FIL fp ;
1081
+ f_open (& fp , "0:/src/main.py" , FA_WRITE | FA_CREATE_ALWAYS );
1082
+ UINT n ;
1083
+ f_write (& fp , fresh_main_py , sizeof (fresh_main_py ) - 1 /* don't count null terminator */ , & n );
1084
+ // TODO check we could write n bytes
1085
+ f_close (& fp );
1086
+
1025
1087
// keep LED on for at least 200ms
1026
1088
sys_tick_wait_at_least (stc , 200 );
1027
1089
led_state (PYB_LED_R2 , 0 );
@@ -1343,9 +1405,9 @@ int main(void) {
1343
1405
}
1344
1406
1345
1407
// SD card testing
1346
- if (1 ) {
1408
+ if (0 ) {
1347
1409
extern void sdio_init (void );
1348
- // sdio_init();
1410
+ sdio_init ();
1349
1411
}
1350
1412
1351
1413
printf ("PYB: sync filesystems\n" );
0 commit comments