diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c index 22882ddb60..8952161b69 100644 --- a/stmhal/sdcard.c +++ b/stmhal/sdcard.c @@ -29,6 +29,7 @@ #include #include "mpconfig.h" +#include "nlr.h" #include "misc.h" #include "qstr.h" #include "obj.h" @@ -36,6 +37,7 @@ #include "sdcard.h" #include "pin.h" #include "genhdr/pins.h" +#include "bufhelper.h" #if MICROPY_HW_HAS_SDCARD @@ -154,7 +156,7 @@ bool sdcard_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) // buffer and we can't let it fill up in the middle of a read. // This will not be needed when SD uses DMA for transfer. __disable_irq(); - HAL_SD_ErrorTypedef err = HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); + HAL_SD_ErrorTypedef err = HAL_SD_ReadBlocks(&sd_handle, (uint32_t*)dest, (uint64_t)block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); __enable_irq(); if (err != SD_OK) { @@ -179,7 +181,7 @@ bool sdcard_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl // buffer and we can't let it drain to empty in the middle of a write. // This will not be needed when SD uses DMA for transfer. __disable_irq(); - HAL_SD_ErrorTypedef err = HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); + HAL_SD_ErrorTypedef err = HAL_SD_WriteBlocks(&sd_handle, (uint32_t*)src, (uint64_t)block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); __enable_irq(); if (err != SD_OK) { @@ -203,7 +205,7 @@ bool sdcard_read_blocks_dma(uint8_t *dest, uint32_t block_num, uint32_t num_bloc } // do the read - if (HAL_SD_ReadBlocks_DMA(&sd_handle, (uint32_t*)dest, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE) != SD_OK) { + if (HAL_SD_ReadBlocks_DMA(&sd_handle, (uint32_t*)dest, (uint64_t)block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE) != SD_OK) { return false; } @@ -228,7 +230,7 @@ bool sdcard_write_blocks_dma(const uint8_t *src, uint32_t block_num, uint32_t nu SD_Error status; - status = HAL_SD_WriteBlock_DMA(&sd_handle, (uint32_t*)src, block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); + status = HAL_SD_WriteBlock_DMA(&sd_handle, (uint32_t*)src, (uint64_t)block_num * SDCARD_BLOCK_SIZE, SDCARD_BLOCK_SIZE, num_blocks); if (status != SD_OK) { return false; } @@ -276,10 +278,28 @@ static mp_obj_t sd_read(mp_obj_t self, mp_obj_t block_num) { static MP_DEFINE_CONST_FUN_OBJ_2(sd_read_obj, sd_read); +static mp_obj_t sd_write(mp_obj_t self, mp_obj_t block_num, mp_obj_t source) { + mp_buffer_info_t bufinfo; + uint8_t tmp[1]; + + pyb_buf_get_for_send(source, &bufinfo, tmp); + if (bufinfo.len % SDCARD_BLOCK_SIZE != 0) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "writes must be aligned to SDCARD_BLOCK_SIZE (%d) bytes", SDCARD_BLOCK_SIZE)); + } + + if (!sdcard_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SDCARD_BLOCK_SIZE)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "sdcard_write_blocks failed")); + } + return mp_const_none; +} + +static MP_DEFINE_CONST_FUN_OBJ_3(sd_write_obj, sd_write); + STATIC const mp_map_elem_t sdcard_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_present), (mp_obj_t)&sd_present_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_power), (mp_obj_t)&sd_power_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&sd_read_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&sd_write_obj }, }; STATIC MP_DEFINE_CONST_DICT(sdcard_locals_dict, sdcard_locals_dict_table);