Skip to content

Commit 3770cd2

Browse files
committed
stmhal: Expose flash and SD card as proper objects with block protocol.
You can now create (singleton) objects representing the flash and SD card, using: flash = pyb.Flash() sdcard = pyb.SDCard() These objects provide the block protocol.
1 parent f7e5e67 commit 3770cd2

File tree

6 files changed

+169
-15
lines changed

6 files changed

+169
-15
lines changed

stmhal/modpyb.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,13 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
190190
{ MP_OBJ_NEW_QSTR(MP_QSTR_Switch), (mp_obj_t)&pyb_switch_type },
191191
#endif
192192

193+
#if MICROPY_HW_HAS_FLASH
194+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Flash), (mp_obj_t)&pyb_flash_type },
195+
#endif
196+
193197
#if MICROPY_HW_HAS_SDCARD
194-
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj },
198+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SD), (mp_obj_t)&pyb_sdcard_obj }, // now obsolete
199+
{ MP_OBJ_NEW_QSTR(MP_QSTR_SDCard), (mp_obj_t)&pyb_sdcard_type },
195200
#endif
196201

197202
#if defined(MICROPY_HW_LED1)

stmhal/qstrdefsport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ Q(write)
6161
Q(repl_uart)
6262
Q(time)
6363
Q(rng)
64+
Q(Flash)
6465
Q(SD)
65-
Q(SDcard)
66+
Q(SDCard)
6667
Q(FileIO)
6768
Q(flush)
6869
Q(boot.py)

stmhal/sdcard.c

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include STM32_HAL_H
28-
2927
#include "py/nlr.h"
3028
#include "py/runtime.h"
29+
#include "lib/fatfs/ff.h"
30+
#include "extmod/fsusermount.h"
31+
3132
#include "sdcard.h"
3233
#include "pin.h"
3334
#include "genhdr/pins.h"
@@ -279,9 +280,18 @@ mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t n
279280
/******************************************************************************/
280281
// Micro Python bindings
281282
//
282-
// Note: these function are a bit ad-hoc at the moment and are mainly intended
283-
// for testing purposes. In the future SD should be a proper class with a
284-
// consistent interface and methods to mount/unmount it.
283+
// Expose the SD card as an object with the block protocol.
284+
285+
// there is a singleton SDCard object
286+
const mp_obj_base_t pyb_sdcard_obj = {&pyb_sdcard_type};
287+
288+
STATIC mp_obj_t pyb_sdcard_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
289+
// check arguments
290+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
291+
292+
// return singleton object
293+
return (mp_obj_t)&pyb_sdcard_obj;
294+
}
285295

286296
STATIC mp_obj_t sd_present(mp_obj_t self) {
287297
return mp_obj_new_bool(sdcard_is_present());
@@ -316,6 +326,7 @@ STATIC mp_obj_t sd_info(mp_obj_t self) {
316326
}
317327
STATIC MP_DEFINE_CONST_FUN_OBJ_1(sd_info_obj, sd_info);
318328

329+
// now obsolete, kept for backwards compatibility
319330
STATIC mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
320331
uint8_t *dest = m_new(uint8_t, SDCARD_BLOCK_SIZE);
321332
mp_uint_t ret = sdcard_read_blocks(dest, mp_obj_get_int(block_num), 1);
@@ -329,6 +340,7 @@ STATIC mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) {
329340
}
330341
STATIC MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read);
331342

343+
// now obsolete, kept for backwards compatibility
332344
STATIC mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t data) {
333345
mp_buffer_info_t bufinfo;
334346
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
@@ -346,22 +358,70 @@ STATIC mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t data) {
346358
}
347359
STATIC MP_DEFINE_CONST_FUN_OBJ_3(sd_write_obj, sd_write);
348360

349-
STATIC const mp_map_elem_t sdcard_locals_dict_table[] = {
361+
STATIC mp_obj_t pyb_sdcard_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
362+
mp_buffer_info_t bufinfo;
363+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
364+
mp_uint_t ret = sdcard_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
365+
return mp_obj_new_bool(ret == 0);
366+
}
367+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_readblocks_obj, pyb_sdcard_readblocks);
368+
369+
STATIC mp_obj_t pyb_sdcard_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
370+
mp_buffer_info_t bufinfo;
371+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
372+
mp_uint_t ret = sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE);
373+
return mp_obj_new_bool(ret == 0);
374+
}
375+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_writeblocks_obj, pyb_sdcard_writeblocks);
376+
377+
STATIC mp_obj_t pyb_sdcard_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
378+
mp_int_t cmd = mp_obj_get_int(cmd_in);
379+
switch (cmd) {
380+
case 1: // INIT
381+
if (!sdcard_power_on()) {
382+
return MP_OBJ_NEW_SMALL_INT(-1); // error
383+
}
384+
return MP_OBJ_NEW_SMALL_INT(0); // success
385+
386+
case 2: // DEINIT
387+
sdcard_power_off();
388+
return MP_OBJ_NEW_SMALL_INT(0); // success
389+
390+
case 3: // SYNC
391+
// nothing to do
392+
return MP_OBJ_NEW_SMALL_INT(0); // success
393+
394+
case 4: // SEC_COUNT
395+
return MP_OBJ_NEW_SMALL_INT(0); // TODO
396+
397+
case 5: // SEC_SIZE
398+
return MP_OBJ_NEW_SMALL_INT(SDCARD_BLOCK_SIZE);
399+
400+
default: // unknown command
401+
return MP_OBJ_NEW_SMALL_INT(-1); // error
402+
}
403+
}
404+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sdcard_ioctl_obj, pyb_sdcard_ioctl);
405+
406+
STATIC const mp_map_elem_t pyb_sdcard_locals_dict_table[] = {
350407
{ MP_OBJ_NEW_QSTR(MP_QSTR_present), (mp_obj_t)&sd_present_obj },
351408
{ MP_OBJ_NEW_QSTR(MP_QSTR_power), (mp_obj_t)&sd_power_obj },
352409
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&sd_info_obj },
353410
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&sd_read_obj },
354411
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&sd_write_obj },
412+
// block device protocol
413+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_sdcard_readblocks_obj },
414+
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_sdcard_writeblocks_obj },
415+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_sdcard_ioctl_obj },
355416
};
356417

357-
STATIC MP_DEFINE_CONST_DICT(sdcard_locals_dict, sdcard_locals_dict_table);
418+
STATIC MP_DEFINE_CONST_DICT(pyb_sdcard_locals_dict, pyb_sdcard_locals_dict_table);
358419

359-
static const mp_obj_type_t sdcard_type = {
420+
const mp_obj_type_t pyb_sdcard_type = {
360421
{ &mp_type_type },
361-
.name = MP_QSTR_SDcard,
362-
.locals_dict = (mp_obj_t)&sdcard_locals_dict,
422+
.name = MP_QSTR_SDCard,
423+
.make_new = pyb_sdcard_make_new,
424+
.locals_dict = (mp_obj_t)&pyb_sdcard_locals_dict,
363425
};
364426

365-
const mp_obj_base_t pyb_sdcard_obj = {&sdcard_type};
366-
367427
#endif // MICROPY_HW_HAS_SDCARD

stmhal/sdcard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ uint64_t sdcard_get_capacity_in_bytes(void);
3737
mp_uint_t sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
3838
mp_uint_t sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks);
3939

40+
extern const struct _mp_obj_type_t pyb_sdcard_type;
4041
extern const struct _mp_obj_base_t pyb_sdcard_obj;

stmhal/storage.c

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626

2727
#include <stdint.h>
2828
#include <string.h>
29-
#include STM32_HAL_H
3029

3130
#include "py/obj.h"
31+
#include "py/runtime.h"
32+
#include "lib/fatfs/ff.h"
33+
#include "extmod/fsusermount.h"
34+
3235
#include "systick.h"
3336
#include "led.h"
3437
#include "flash.h"
@@ -306,3 +309,81 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
306309
return true;
307310
}
308311
}
312+
313+
mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
314+
for (size_t i = 0; i < num_blocks; i++) {
315+
if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) {
316+
return 1; // error
317+
}
318+
}
319+
return 0; // success
320+
}
321+
322+
mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
323+
for (size_t i = 0; i < num_blocks; i++) {
324+
if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) {
325+
return 1; // error
326+
}
327+
}
328+
return 0; // success
329+
}
330+
331+
/******************************************************************************/
332+
// MicroPython bindings
333+
//
334+
// Expose the flash as an object with the block protocol.
335+
336+
// there is a singleton Flash object
337+
STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type};
338+
339+
STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
340+
// check arguments
341+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
342+
343+
// return singleton object
344+
return (mp_obj_t)&pyb_flash_obj;
345+
}
346+
347+
STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
348+
mp_buffer_info_t bufinfo;
349+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE);
350+
mp_uint_t ret = storage_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
351+
return MP_OBJ_NEW_SMALL_INT(ret);
352+
}
353+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks);
354+
355+
STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) {
356+
mp_buffer_info_t bufinfo;
357+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
358+
mp_uint_t ret = storage_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE);
359+
return MP_OBJ_NEW_SMALL_INT(ret);
360+
}
361+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks);
362+
363+
STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) {
364+
mp_int_t cmd = mp_obj_get_int(cmd_in);
365+
switch (cmd) {
366+
case 1: storage_init(); return MP_OBJ_NEW_SMALL_INT(0); // INIT
367+
case 2: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // DEINIT; TODO properly
368+
case 3: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // SYNC
369+
case 4: return MP_OBJ_NEW_SMALL_INT(storage_get_block_count()); // SEC_COUNT
370+
case 5: return MP_OBJ_NEW_SMALL_INT(storage_get_block_size()); // SEC_SIZE
371+
default: return mp_const_none;
372+
}
373+
}
374+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl);
375+
376+
STATIC const mp_map_elem_t pyb_flash_locals_dict_table[] = {
377+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_flash_readblocks_obj },
378+
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_flash_writeblocks_obj },
379+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_flash_ioctl_obj },
380+
};
381+
382+
STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table);
383+
384+
const mp_obj_type_t pyb_flash_type = {
385+
{ &mp_type_type },
386+
.name = MP_QSTR_Flash,
387+
.make_new = pyb_flash_make_new,
388+
.locals_dict = (mp_obj_t)&pyb_flash_locals_dict,
389+
};

stmhal/storage.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ void storage_irq_handler(void);
3636
void storage_flush(void);
3737
bool storage_read_block(uint8_t *dest, uint32_t block);
3838
bool storage_write_block(const uint8_t *src, uint32_t block);
39+
40+
// these return 0 on success, non-zero on error
41+
mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
42+
mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks);
43+
44+
extern const struct _mp_obj_type_t pyb_flash_type;

0 commit comments

Comments
 (0)