8000 stm: flush storage cache immediately; improve user interface. · comfuture/micropython@8c3da5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c3da5c

Browse files
committed
stm: flush storage cache immediately; improve user interface.
1 parent 0f08267 commit 8c3da5c

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

stm/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ PY_O = \
5454
emitnthumb.o \
5555
emitinlinethumb.o \
5656
runtime.o \
57+
map.o \
58+
obj.o \
59+
builtin.o \
5760
vm.o \
5861
repl.o \
5962

stm/main.c

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void __fatal_error(const char *msg) {
122122
#include "parse.h"
123123
#include "compile.h"
124124
#include "runtime.h"
125+
#include "obj.h"
125126
#include "repl.h"
126127

127128
static qstr pyb_config_source_dir = 0;
@@ -153,9 +154,11 @@ py_obj_t pyb_led(py_obj_t state) {
153154
return state;
154155
}
155156

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+
}
159162

160163
py_obj_t led_obj_on(py_obj_t self) {
161164
machine_uint_t led_id;
@@ -179,7 +182,7 @@ py_obj_t led_obj_off(py_obj_t self) {
179182

180183
const py_user_info_t led_obj_info = {
181184
"Led",
182-
NULL, // print
185+
led_obj_print,
183186
{
184187
{"on", 0, led_obj_on},
185188
{"off", 0, led_obj_off},
@@ -199,6 +202,12 @@ py_obj_t pyb_sw(void) {
199202
}
200203
}
201204

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+
202211
py_obj_t servo_obj_angle(py_obj_t self, py_obj_t angle) {
203212
machine_uint_t servo_id;
204213
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) {
216225

217226
const py_user_info_t servo_obj_info = {
218227
"Servo",
219-
NULL, // print
228+
servo_obj_print,
220229
{
221230
{"angle", 1, servo_obj_angle},
222231
{NULL, 0, NULL},
@@ -276,8 +285,40 @@ static const char fresh_boot_py[] =
276285
"#pyb.error_log('error.txt')\n"
277286
;
278287

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+
279314
// get lots of info about the board
280315
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+
281322
// get and print clock speeds
282323
// SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
283324
{
@@ -478,8 +519,8 @@ int readline(vstr_t *line, const char *prompt) {
478519
}
479520

480521
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");
483524

484525
vstr_t line;
485526
vstr_init(&line);
@@ -523,8 +564,11 @@ void do_repl(void) {
523564
if (nlr_push(&nlr) == 0) {
524565
rt_call_function_0(module_fun);
525566
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+
}
528572
} else {
529573
// uncaught exception
530574
py_obj_print((py_obj_t)nlr.ret_val);
@@ -592,12 +636,16 @@ void gc_collect(void) {
592636
gc_collect_root((void**)HEAP_END, (RAM_END - HEAP_END) / 4); // will trace regs since they now live in this function on the stack
593637
gc_collect_end();
594638
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+
}
601649
}
602650

603651
py_obj_t pyb_gc(void) {
@@ -951,13 +999,15 @@ int main(void) {
951999
timer_init();
9521000

9531001
// RNG
954-
if (0) {
1002+
if (1) {
9551003
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
9561004
RNG_Cmd(ENABLE);
9571005
}
9581006

9591007
// add some functions to the python namespace
9601008
{
1009+
rt_store_name(qstr_from_str_static("help"), rt_make_function_0(pyb_help));
1010+
9611011
py_obj_t m = py_module_new();
9621012
rt_store_attr(m, qstr_from_str_static("info"), rt_make_function_0(pyb_info));
9631013
rt_store_attr(m, qstr_from_str_static("stop"), rt_make_function_0(pyb_stop));
@@ -968,16 +1018,16 @@ int main(void) {
9681018
rt_store_attr(m, qstr_from_str_static("gc"), rt_make_function_0(pyb_gc));
9691019
rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
9701020
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));
9721022
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set));
9731023
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));
9751025
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
9761026
rt_store_attr(m, qstr_from_str_static("time"), rt_make_function_0(pyb_rtc_read));
9771027
rt_store_attr(m, qstr_from_str_static("uout"), rt_make_function_1(pyb_usart_send));
9781028
rt_store_attr(m, qstr_from_str_static("uin"), rt_make_function_0(pyb_usart_receive));
9791029
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));
9811031
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
9821032
rt_store_attr(m, qstr_from_str_static("Servo"), rt_make_function_1(pyb_Servo));
9831033
rt_store_name(qstr_from_str_static("pyb"), m);
@@ -1022,6 +1072,18 @@ int main(void) {
10221072
__fatal_error("could not create LFS");
10231073
}
10241074

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+
10251087
// keep LED on for at least 200ms
10261088
sys_tick_wait_at_least(stc, 200);
10271089
led_state(PYB_LED_R2, 0);
@@ -1343,9 +1405,9 @@ int main(void) {
13431405
}
13441406

13451407
// SD card testing
1346-
if (1) {
1408+
if (0) {
13471409
extern void sdio_init(void);
1348-
//sdio_init();
1410+
sdio_init();
13491411
}
13501412

13511413
printf("PYB: sync filesystems\n");

stm/storage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
152152
uint8_t *dest = cache_get_addr_for_write(flash_addr);
153153
memcpy(dest, src, BLOCK_SIZE);
154154
sys_tick_counter_last_write = sys_tick_counter;
155+
cache_flush(); // XXX hack for now so that the cache is always flushed
155156
return true;
156157

157158
} else {

0 commit comments

Comments
 (0)
0