Skip to content

Commit baba08b

Browse files
committed
Merge pull request #218 from iabdalkader/master
Move file obj to separate module
2 parents 41d02b6 + a215b09 commit baba08b

File tree

6 files changed

+124
-102
lines changed

6 files changed

+124
-102
lines changed

py/gc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ void gc_collect_end(void) {
187187
gc_sweep();
188188
}
189189

190+
extern void gc_helper_get_regs_and_clean_stack(machine_uint_t *regs, machine_uint_t heap_end);
191+
192+
void gc_collect(void) {
193+
extern char _ram_start; /* defined by linker script */
194+
extern char _ram_end; /* defined by linker script */
195+
extern char _heap_start; /* defined by linker script */
196+
extern char _heap_end; /* defined by linker script */
197+
198+
gc_collect_start();
199+
gc_collect_root((void**)&_ram_start, (&_heap_start - &_ram_start) / 4);
200+
machine_uint_t regs[10];
201+
gc_helper_get_regs_and_clean_stack(regs, (uint32_t) &_heap_end);
202+
gc_collect_root((void**)&_heap_end, (&_ram_end - &_heap_end) / 4); // will trace regs since they now live in this function on the stack
203+
gc_collect_end();
204+
}
205+
190206
void gc_info(gc_info_t *info) {
191207
info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
192208
info->used = 0;

stm/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SRC_C = \
6565
usrsw.c \
6666
adc.c \
6767
rtc.c \
68+
file.c \
6869
# pybwlan.c \
6970
7071
SRC_S = \

stm/file.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <stdio.h>
2+
#include <stm32f4xx.h>
3+
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "mpconfigport.h"
7+
#include "qstr.h"
8+
#include "obj.h"
9+
#include "file.h"
10+
#include "ff.h"
11+
12+
typedef struct _pyb_file_obj_t {
13+
mp_obj_base_t base;
14+
FIL fp;
15+
} pyb_file_obj_t;
16+
17+
void file_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
18+
printf("<file %p>", self_in);
19+
}
20+
21+
mp_obj_t file_obj_read(mp_obj_t self_in, mp_obj_t arg) {
22+
pyb_file_obj_t *self = self_in;
23+
int n = mp_obj_get_int(arg);
24+
byte *buf = m_new(byte, n);
25+
UINT n_out;
26+
f_read(&self->fp, buf, n, &n_out);
27+
return mp_obj_new_str(buf, n_out, false);
28+
}
29+
30+
mp_obj_t file_obj_write(mp_obj_t self_in, mp_obj_t arg) {
31+
pyb_file_obj_t *self = self_in;
32+
uint l;
33+
const byte *s = mp_obj_str_get_data(arg, &l);
34+
UINT n_out;
35+
FRESULT res = f_write(&self->fp, s, l, &n_out);
36+
if (res != FR_OK) {
37+
printf("File error: could not write to file; error code %d\n", res);
38+
} else if (n_out != l) {
39+
printf("File error: could not write all data to file; wrote %d / %d bytes\n", n_out, l);
40+
}
41+
return mp_const_none;
42+
}
43+
44+
mp_obj_t file_obj_close(mp_obj_t self_in) {
45+
pyb_file_obj_t *self = self_in;
46+
f_close(&self->fp);
47+
return mp_const_none;
48+
}
49+
50+
static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_read_obj, file_obj_read);
51+
static MP_DEFINE_CONST_FUN_OBJ_2(file_obj_write_obj, file_obj_write);
52+
static MP_DEFINE_CONST_FUN_OBJ_1(file_obj_close_obj, file_obj_close);
53+
54+
// TODO gc hook to close the file if not already closed
55+
56+
static const mp_method_t file_methods[] = {
57+
{ "read", &file_obj_read_obj },
58+
{ "write", &file_obj_write_obj },
59+
{ "close", &file_obj_close_obj },
60+
{NULL, NULL},
61+
};
62+
63+
static const mp_obj_type_t file_obj_type = {
64+
{ &mp_const_type },
65+
"File",
66+
.print = file_obj_print,
67+
.methods = file_methods,
68+
};
69+
70+
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode) {
71+
const char *filename = mp_obj_str_get_str(o_filename);
72+
const char *mode = mp_obj_str_get_str(o_mode);
73+
pyb_file_obj_t *self = m_new_obj(pyb_file_obj_t);
74+
self->base.type = &file_obj_type;
75+
if (mode[0] == 'r') {
76+
// open for reading
77+
FRESULT res = f_open(&self->fp, filename, FA_READ);
78+
if (res != FR_OK) {
79+
printf("FileNotFoundError: [Errno 2] No such file or directory: '%s'\n", filename);
80+
return mp_const_none;
81+
}
82+
} else if (mode[0] == 'w') {
83+
// open for writing, truncate the file first
84+
FRESULT res = f_open(&self->fp, filename, FA_WRITE | FA_CREATE_ALWAYS);
85+
if (res != FR_OK) {
86+
printf("?FileError: could not create file: '%s'\n", filename);
87+
return mp_const_none;
88+
}
89+
} else {
90+
printf("ValueError: invalid mode: '%s'\n", mode);
91+
return mp_const_none;
92+
}
93+
return self;
94+
}

stm/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mp_obj_t pyb_io_open(mp_obj_t o_filename, mp_obj_t o_mode);

stm/main.c

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@
4343
#include "usrsw.h"
4444
#include "adc.h"
4545
#include "rtc.h"
46+
#include "file.h"
4647

4748
int errno;
4849

4950
extern uint32_t _heap_start;
51+
extern uint32_t _heap_end;
5052

5153
static FATFS fatfs0;
5254

@@ -454,21 +456,12 @@ bool do_file(const char *filename) {
454456
}
455457
}
456458

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;
462461

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
472465

473466
if (0) {
474467
// print GC info
@@ -479,10 +472,7 @@ void gc_collect(void) {
479472
printf(" %lu : %lu\n", info.used, info.free);
480473
printf(" 1=%lu 2=%lu m=%lu\n", info.num_1block, info.num_2block, info.max_block);
481474
}
482-
}
483475

484-
mp_obj_t pyb_gc(void) {
485-
gc_collect();
486476
return mp_const_none;
487477
}
488478

@@ -544,90 +534,6 @@ mp_obj_t pyb_hid_send_report(mp_obj_t arg) {
544534
return mp_const_none;
545535
}
546536

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-
631537
mp_obj_t pyb_rng_get(void) {
632538
return mp_obj_new_int(RNG_GetRandomNumber() >> 16);
633539
}
@@ -692,7 +598,7 @@ int main(void) {
692598
soft_reset:
693599

694600
// GC init
695-
gc_init(&_heap_start, (void*)HEAP_END);
601+
gc_init(&_heap_start, &_heap_end);
696602

697603
// Micro Python init
698604
qstr_init();

stm/stm32f405.ld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ _minimum_heap_size = 16K;
1919
/* top end of the stack */
2020
_estack = ORIGIN(RAM) + LENGTH(RAM);
2121

22+
_ram_start = 0x20000000;
23+
_ram_end = 0x20020000;
24+
_heap_end = 0x2001c000;
25+
2226
/* define output sections */
2327
SECTIONS
2428
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy