8000 stm32: Move factory reset files and code to separate source file. · micropython/micropython@97753a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 97753a1

Browse files
committed
stm32: Move factory reset files and code to separate source file.
The new function factory_reset_make_files() populates the given filesystem with the default factory files. It is defined with weak linkage so it can be overridden by a board. This commit also brings some minor user-facing changes: - boot.py is now no longer created unconditionally if it doesn't exist, it is now only created when the filesystem is formatted and the other files are populated (so, before, if the user deleted boot.py it would be recreated at next boot; now it won't be). - pybcdc.inf and README.txt are only created if the board has USB, because they only really make sense if the filesystem is exposed via USB.
1 parent 34942d0 commit 97753a1

File tree

4 files changed

+135
-75
lines changed

4 files changed

+135
-75
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ SRC_C = \
229229
systick.c \
230230
powerctrl.c \
231231
pybthread.c \
232+
factoryreset.c \
232233
timer.c \
233234
led.c \
234235
pin.c \

ports/stm32/factoryreset.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "factoryreset.h"
29+
30+
#if MICROPY_HW_ENABLE_STORAGE
31+
32+
static const char fresh_boot_py[] =
33+
"# boot.py -- run on boot-up\r\n"
34+
"# can run arbitrary Python, but best to keep it minimal\r\n"
35+
"\r\n"
36+
"import machine\r\n"
37+
"import pyb\r\n"
38+
"#pyb.main('main.py') # main script to run after this one\r\n"
39+
#if MICROPY_HW_ENABLE_USB
40+
"#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n"
41+
"#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n"
42+
#endif
43+
;
44+
45+
static const char fresh_main_py[] =
46+
"# main.py -- put your code here!\r\n"
47+
;
48+
49+
#if MICROPY_HW_ENABLE_USB
50+
static const char fresh_pybcdc_inf[] =
51+
#include "genhdr/pybcdc_inf.h"
52+
;
53+
54+
static const char fresh_readme_txt[] =
55+
"This is a MicroPython board\r\n"
56+
"\r\n"
57+
"You can get started right away by writing your Python code in 'main.py'.\r\n"
58+
"\r\n"
59+
"For a serial prompt:\r\n"
60+
" - Windows: you need to go to 'Device manager', right click on the unknown device,\r\n"
61+
" then update the driver software, using the 'pybcdc.inf' file found on this drive.\r\n"
62+
" Then use a terminal program like Hyperterminal or putty.\r\n"
63+
" - Mac OS X: use the command: screen /dev/tty.usbmodem*\r\n"
64+
" - Linux: use the command: screen /dev/ttyACM0\r\n"
65+
"\r\n"
66+
"Please visit http://micropython.org/help/ for further help.\r\n"
67+
;
68+
#endif
69+
70+
typedef struct _factory_file_t {
71+
const char *name;
72+
size_t len;
73+
const char *data;
74+
} factory_file_t;
75+
76+
static const factory_file_t factory_files[] = {
77+
{"boot.py", sizeof(fresh_boot_py) - 1, fresh_boot_py},
78+
{"main.py", sizeof(fresh_main_py) - 1, fresh_main_py},
79+
#if MICROPY_HW_ENABLE_USB
80+
{"pybcdc.inf", sizeof(fresh_pybcdc_inf) - 1, fresh_pybcdc_inf},
81+
{"README.txt", sizeof(fresh_readme_txt) - 1, fresh_readme_txt},
82+
#endif
83+
};
84+
85+
MP_WEAK void factory_reset_make_files(FATFS *fatfs) {
86+
for (int i = 0; i < MP_ARRAY_SIZE(factory_files); ++i) {
87+
const factory_file_t *f = &factory_files[i];
88+
FIL fp;
89+
FRESULT res = f_open(fatfs, &fp, f->name, FA_WRITE | FA_CREATE_ALWAYS);
90+
if (res == FR_OK) {
91+
UINT n;
92+
f_write(&fp, f->data, f->len, &n);
93+
f_close(&fp);
94+
}
95+
}
96+
}
97+
98+
#endif // MICROPY_HW_ENABLE_STORAGE

ports/stm32/factoryreset.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_STM32_FACTORYRESET_H
27+
#define MICROPY_INCLUDED_STM32_FACTORYRESET_H
28+
29+
#include "lib/oofatfs/ff.h"
30+
31+
void factory_reset_make_files(FATFS *fatfs);
32+
33+
#endif // MICROPY_INCLUDED_STM32_FACTORYRESET_H

ports/stm32/main.c

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "pendsv.h"
4646
#include "pybthread.h"
4747
#include "gccollect.h"
48+
#include "factoryreset.h"
4849
#include "modmachine.h"
4950
#include "i2c.h"
5051
#include "spi.h"
@@ -148,42 +149,6 @@ STATIC mp_obj_t pyb_main(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a
148149
MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main);
149150

150151
#if MICROPY_HW_ENABLE_STORAGE
151-
static const char fresh_boot_py[] =
152-
"# boot.py -- run on boot-up\r\n"
153-
"# can run arbitrary Python, but best to keep it minimal\r\n"
154-
"\r\n"
155-
"import machine\r\n"
156-
"import pyb\r\n"
157-
"#pyb.main('main.py') # main script to run after this one\r\n"
158-
#if MICROPY_HW_ENABLE_USB
159-
"#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n"
160-
"#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse\r\n"
161-
#endif
162-
;
163-
164-
static const char fresh_main_py[] =
165-
"# main.py -- put your code here!\r\n"
166-
;
167-
168-
static const char fresh_pybcdc_inf[] =
169-
#include "genhdr/pybcdc_inf.h"
170-
;
171-
172-
static const char fresh_readme_txt[] =
173-
"This is a MicroPython board\r\n"
174-
"\r\n"
175-
"You can get started right away by writing your Python code in 'main.py'.\r\n"
176-
"\r\n"
177-
"For a serial prompt:\r\n"
178-
" - Windows: you need to go to 'Device manager', right click on the unknown device,\r\n"
179-
" then update the driver software, using the 'pybcdc.inf' file found on this drive.\r\n"
180-
" Then use a terminal program like Hyperterminal or putty.\r\n"
181-
" - Mac OS X: use the command: screen /dev/tty.usbmodem*\r\n"
182-
" - Linux: use the command: screen /dev/ttyACM0\r\n"
183-
"\r\n"
184-
"Please visit http://micropython.org/help/ for further help.\r\n"
185-
;
186-
187152
// avoid inlining to avoid stack usage within main()
188153
MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
189154
// init the vfs object
@@ -213,23 +178,8 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
213178
// set label
214179
f_setlabel(&vfs_fat->fatfs, MICROPY_HW_FLASH_FS_LABEL);
215180

216-
// create empty main.py
217-
FIL fp;
218-
f_open(&vfs_fat->fatfs, &fp, "/main.py", FA_WRITE | FA_CREATE_ALWAYS);
219-
UINT n;
220-
f_write(&fp, fresh_main_py, sizeof(fresh_main_py) - 1 /* don't count null terminator */, &n);
221-
// TODO check we could write n bytes
222-
f_close(&fp);
223-
224-
// create .inf driver file
225-
f_open(&vfs_fat->fatfs, &fp, "/pybcdc.inf", FA_WRITE | FA_CREATE_ALWAYS);
226-
f_write(&fp, fresh_pybcdc_inf, sizeof(fresh_pybcdc_inf) - 1 /* don't count null terminator */, &n);
227-
f_close(&fp);
228-
229-
// create readme file
230-
f_open(&vfs_fat->fatfs, &fp, "/README.txt", FA_WRITE | FA_CREATE_ALWAYS);
231-
f_write(&fp, fresh_readme_txt, sizeof(fresh_readme_txt) - 1 /* don't count null terminator */, &n);
232-
f_close(&fp);
181+
// populate the filesystem with factory files
182+
factory_reset_make_files(&vfs_fat->fatfs);
233183

234184
// keep LED on for at least 200ms
235185
systick_wait_at_least(start_tick, 200);
@@ -258,28 +208,6 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) {
258208
// It is set to the internal flash filesystem by default.
259209
MP_STATE_PORT(vfs_cur) = vfs;
260210

261-
// Make sure we have a /flash/boot.py. Create it if needed.
262-
FILINFO fno;
263-
res = f_stat(&vfs_fat->fatfs, "/boot.py", &fno);
264-
if (res != FR_OK) {
265-
// doesn't exist, create fresh file
266-
267-
// LED on to indicate creation of boot.py
268-
led_state(PYB_LED_GREEN, 1);
269-
uint32_t start_tick = HAL_GetTick();
270-
271-
FIL fp;
272-
f_open(&vfs_fat->fatfs, &fp, "/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
273-
UINT n;
274-
f_write(&fp, fresh_boot_py, sizeof(fresh_boot_py) - 1 /* don't count null terminator */, &n);
275-
// TODO check we could write n bytes
276-
f_close(&fp);
277-
278-
// keep LED on for at least 200ms
279-
systick_wait_at_least(start_tick, 200);
280-
led_state(PYB_LED_GREEN, 0);
281-
}
282-
283211
return true;
284212
}
285213
#endif

0 commit comments

Comments
 (0)
0