Skip to content

Commit d6b4c66

Browse files
committed
stm: add Led object; add option to reset filesystem.
1 parent 065f8a5 commit d6b4c66

File tree

1 file changed

+60
-4
lines changed

1 file changed

+60
-4
lines changed

stm/main.c

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,44 @@ py_obj_t pyb_led(py_obj_t state) {
127127
return state;
128128
}
129129

130+
py_obj_t py_obj_new_user(const py_user_info_t *info, machine_uint_t data1, machine_uint_t data2);
131+
void py_user_get_data(py_obj_t o, machine_uint_t *data1, machine_uint_t *data2);
132+
void py_user_set_data(py_obj_t o, machine_uint_t data1, machine_uint_t data2);
133+
134+
py_obj_t led_obj_on(py_obj_t self) {
135+
machine_uint_t led_id;
136+
py_user_get_data(self, &led_id, NULL);
137+
switch (led_id) {
138+
case 1: led_state(PYB_LED_G1, 1); break;
139+
case 2: led_state(PYB_LED_G2, 1); break;
140+
}
141+
return py_const_none;
142+
}
143+
144+
py_obj_t led_obj_off(py_obj_t self) {
145+
machine_uint_t led_id;
146+
py_user_get_data(self, &led_id, NULL);
147+
switch (led_id) {
148+
case 1: led_state(PYB_LED_G1, 0); break;
149+
case 2: led_state(PYB_LED_G2, 0); break;
150+
}
151+
return py_const_none;
152+
}
153+
154+
const py_user_info_t led_obj_info = {
155+
"Led",
156+
NULL, // print
157+
{
158+
{"on", 0, led_obj_on},
159+
{"off", 0, led_obj_off},
160+
{NULL, 0, NULL},
161+
}
162+
};
163+
164+
py_obj_t pyb_Led(py_obj_t led_id) {
165+
return py_obj_new_user(&led_obj_info, (machine_uint_t)py_obj_get_int(led_id), 0);
166+
}
167+
130168
py_obj_t pyb_sw(void) {
131169
if (sw_get()) {
132170
return py_const_true;
@@ -749,6 +787,9 @@ py_obj_t pyb_rng_get(void) {
749787
int main(void) {
750788
// TODO disable JTAG
751789

790+
// update the SystemCoreClock variable
791+
SystemCoreClockUpdate();
792+
752793
// set interrupt priority config to use all 4 bits for pre-empting
753794
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
754795

@@ -833,6 +874,7 @@ int main(void) {
833874
rt_store_attr(m, qstr_from_str_static("uin"), rt_make_function_0(pyb_usart_receive));
834875
rt_store_attr(m, qstr_from_str_static("ustat"), rt_make_function_0(pyb_usart_status));
835876
rt_store_attr(m, qstr_from_str_static("rng"), rt_make_function_0(pyb_rng_get));
877+
rt_store_attr(m, qstr_from_str_static("Led"), rt_make_function_1(pyb_Led));
836878
rt_store_name(qstr_from_str_static("pyb"), m);
837879

838880
rt_store_name(qstr_from_str_static("open"), rt_make_function_2(pyb_io_open));
@@ -841,14 +883,28 @@ int main(void) {
841883
// print a message to the LCD
842884
lcd_print_str(" micro py board\n");
843885

886+
// check if user switch held (initiates reset of filesystem)
887+
bool reset_filesystem = false;
888+
if (sw_get()) {
889+
reset_filesystem = true;
890+
for (int i = 0; i < 50; i++) {
891+
if (!sw_get()) {
892+
reset_filesystem = false;
893+
break;
894+
}
895+
sys_tick_delay_ms(10);
896+
}
897+
}
898+
844899
// local filesystem init
845900
{
846901
// try to mount the flash
847902
FRESULT res = f_mount(&fatfs0, "0:", 1);
848-
if (res == FR_OK) {
903+
if (!reset_filesystem && res == FR_OK) {
849904
// mount sucessful
850-
} else if (res == FR_NO_FILESYSTEM) {
905+
} else if (reset_filesystem || res == FR_NO_FILESYSTEM) {
851906
// no filesystem, so create a fresh one
907+
// TODO doesn't seem to work correctly when reset_filesystem is true...
852908

853909
// LED on to indicate creation of LFS
854910
led_state(PYB_LED_R2, 1);
@@ -1165,8 +1221,8 @@ int main(void) {
11651221
}
11661222

11671223
// wifi
1168-
pyb_wlan_init();
1169-
pyb_wlan_start();
1224+
//pyb_wlan_init();
1225+
//pyb_wlan_start();
11701226

11711227
do_repl();
11721228

0 commit comments

Comments
 (0)