43
43
#include "usrsw.h"
44
44
#include "adc.h"
45
45
#include "rtc.h"
46
+ #include "file.h"
46
47
47
48
int errno ;
48
49
49
50
extern uint32_t _heap_start ;
51
+ extern uint32_t _heap_end ;
50
52
51
53
static FATFS fatfs0 ;
52
54
@@ -454,21 +456,12 @@ bool do_file(const char *filename) {
454
456
}
455
457
}
456
458
457
- #define RAM_START (0x20000000) // fixed for chip
458
- #define HEAP_END (0x2001c000) // tunable
459
- #define RAM_END (0x20020000) // fixed for chip
460
-
461
- void gc_helper_get_regs_and_clean_stack (machine_uint_t * regs , machine_uint_t heap_end );
459
+ mp_obj_t pyb_gc (void ) {
460
+ uint32_t start ,ticks ;
462
461
463
- void gc_collect (void ) {
464
- uint32_t start = sys_tick_counter ;
465
- gc_collect_start ();
466
- gc_collect_root ((void * * )RAM_START , (((uint32_t )& _heap_start ) - RAM_START ) / 4 );
467
- machine_uint_t regs [10 ];
468
- gc_helper_get_regs_and_clean_stack (regs , HEAP_END );
469
- gc_collect_root ((void * * )HEAP_END , (RAM_END - HEAP_END ) / 4 ); // will trace regs since they now live in this function on the stack
470
- gc_collect_end ();
471
- uint32_t ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
462
+ start = sys_tick_counter ;
463
+ gc_collect ();
464
+ ticks = sys_tick_counter - start ; // TODO implement a function that does this properly
472
465
473
466
if (0 ) {
474
467
// print GC info
@@ -479,10 +472,7 @@ void gc_collect(void) {
479
472
printf (" %lu : %lu\n" , info .used , info .free );
480
473
printf (" 1=%lu 2=%lu m=%lu\n" , info .num_1block , info .num_2block , info .max_block );
481
474
}
482
- }
483
475
484
- mp_obj_t pyb_gc (void ) {
485
- gc_collect ();
486
476
return mp_const_none ;
487
477
}
488
478
@@ -544,90 +534,6 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
544
534
return mp_const_none ;
545
535
}
546
536
547
- typedef struct _pyb_file_obj_t {
548
- mp_obj_base_t base ;
549
- FIL fp ;
550
- } pyb_file_obj_t ;
551
-
552
- void file_obj_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
553
- printf ("<file %p>" , self_in );
554
- }
555
-
556
- mp_obj_t file_obj_read (mp_obj_t self_in , mp_obj_t arg ) {
557
- pyb_file_obj_t * self = self_in ;
558
- int n = mp_obj_get_int (arg );
559
- byte * buf = m_new (byte , n );
560
- UINT n_out ;
561
- f_read (& self -> fp , buf , n , & n_out );
562
- return mp_obj_new_str (buf , n_out , false);
563
- }
564
-
565
- mp_obj_t file_obj_write (mp_obj_t self_in , mp_obj_t arg ) {
566
- pyb_file_obj_t * self = self_in ;
567
- uint l ;
568
- const byte * s = mp_obj_str_get_data (arg , & l );
569
- UINT n_out ;
570
- FRESULT res = f_write (& self -> fp , s , l , & n_out );
571
- if (res != FR_OK ) {
572
- printf ("File error: could not write to file; error code %d\n" , res );
573
- } else if (n_out != l ) {
574
- printf ("File error: could not write all data to file; wrote %d / %d bytes\n" , n_out , l );
575
- }
576
- return mp_const_none ;
577
- }
578
-
579
- mp_obj_t file_obj_close (mp_obj_t self_in ) {
580
- pyb_file_obj_t * self = self_in ;
581
- f_close (& self -> fp );
582
- return mp_const_none ;
583
- }
584
-
585
- static MP_DEFINE_CONST_FUN_OBJ_2 (file_obj_read_obj , file_obj_read ) ;
586
- static MP_DEFINE_CONST_FUN_OBJ_2 (file_obj_write_obj , file_obj_write ) ;
587
- static MP_DEFINE_CONST_FUN_OBJ_1 (file_obj_close_obj , file_obj_close ) ;
588
-
589
- // TODO gc hook to close the file if not already closed
590
-
591
- static const mp_method_t file_methods [] = {
592
- { "read" , & file_obj_read_obj },
593
- { "write" , & file_obj_write_obj },
594
- { "close" , & file_obj_close_obj },
595
- {NULL , NULL },
596
- };
597
-
598
- static const mp_obj_type_t file_obj_type = {
599
- { & mp_const_type },
600
- "File" ,
601
- .print = file_obj_print ,
602
- .methods = file_methods ,
603
- };
604
-
605
- mp_obj_t pyb_io_open (mp_obj_t o_filename , mp_obj_t o_mode ) {
606
- const char * filename = mp_obj_str_get_str (o_filename );
607
- const char * mode = mp_obj_str_get_str (o_mode );
608
- pyb_file_obj_t * self = m_new_obj (pyb_file_obj_t );
609
- self -> base .type = & file_obj_type ;
610
- if (mode [0 ] == 'r' ) {
611
- // open for reading
612
- FRESULT res = f_open (& self -> fp , filename , FA_READ );
613
- if (res != FR_OK ) {
614
- printf ("FileNotFoundError: [Errno 2] No such file or directory: '%s'\n" , filename );
615
- return mp_const_none ;
616
- }
617
- } else if (mode [0 ] == 'w' ) {
618
- // open for writing, truncate the file first
619
- FRESULT res = f_open (& self -> fp , filename , FA_WRITE | FA_CREATE_ALWAYS );
620
- if (res != FR_OK ) {
621
- printf ("?FileError: could not create file: '%s'\n" , filename );
622
- return mp_const_none ;
623
- }
624
- } else {
625
- printf ("ValueError: invalid mode: '%s'\n" , mode );
626
- return mp_const_none ;
627
- }
628
- return self ;
629
- }
630
-
631
537
mp_obj_t pyb_rng_get (void ) {
632
538
return mp_obj_new_int (RNG_GetRandomNumber () >> 16 );
633
539
}
@@ -692,7 +598,7 @@ int main(void) {
692
598
soft_reset :
693
599
694
600
// GC init
695
- gc_init (& _heap_start , ( void * ) HEAP_END );
601
+ gc_init (& _heap_start , & _heap_end );
696
602
697
603
// Micro Python init
698
604
qstr_init ();
0 commit comments