|
9 | 9 | #include "py/runtime.h" |
10 | 10 |
|
11 | 11 | #include "common-hal/microcontroller/Pin.h" |
| 12 | +#include "extmod/vfs.h" |
12 | 13 | #include "shared-bindings/sdioio/SDCard.h" |
13 | 14 | #include "shared-bindings/microcontroller/Pin.h" |
14 | 15 | #include "shared-bindings/microcontroller/__init__.h" |
@@ -170,43 +171,85 @@ static void debug_print_state(sdioio_sdcard_obj_t *self, const char *what, sd_mm |
170 | 171 | #endif |
171 | 172 | } |
172 | 173 |
|
173 | | -int common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
174 | | - check_for_deinit(self); |
175 | | - check_whole_block(bufinfo); |
| 174 | +mp_errno_t sdioio_sdcard_writeblocks(mp_obj_t self_in, uint8_t *buf, |
| 175 | + uint32_t start_block, uint32_t num_blocks) { |
| 176 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
176 | 177 | wait_write_complete(self); |
177 | 178 | self->state_programming = true; |
178 | | - sd_mmc_err_t r = sd_mmc_init_write_blocks(0, start_block, bufinfo->len / 512); |
| 179 | + sd_mmc_err_t r = sd_mmc_init_write_blocks(0, start_block, num_blocks); |
179 | 180 | if (r != SD_MMC_OK) { |
180 | 181 | debug_print_state(self, "sd_mmc_init_write_blocks", r); |
181 | | - return -EIO; |
| 182 | + return -MP_EIO; |
182 | 183 | } |
183 | | - r = sd_mmc_start_write_blocks(bufinfo->buf, bufinfo->len / 512); |
| 184 | + r = sd_mmc_start_write_blocks(buf, num_blocks); |
184 | 185 | if (r != SD_MMC_OK) { |
185 | 186 | debug_print_state(self, "sd_mmc_start_write_blocks", r); |
186 | | - return -EIO; |
| 187 | + return -MP_EIO; |
187 | 188 | } |
188 | | - // debug_print_state(self, "after writeblocks OK"); |
189 | 189 | return 0; |
190 | 190 | } |
191 | 191 |
|
192 | | -int common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 192 | +mp_errno_t common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
193 | 193 | check_for_deinit(self); |
194 | 194 | check_whole_block(bufinfo); |
| 195 | + |
| 196 | + uint32_t num_blocks = bufinfo->len / 512; |
| 197 | + return sdioio_sdcard_writeblocks(MP_OBJ_FROM_PTR(self), bufinfo->buf, |
| 198 | + start_block, num_blocks); |
| 199 | +} |
| 200 | + |
| 201 | +mp_errno_t sdioio_sdcard_readblocks(mp_obj_t self_in, uint8_t *buf, |
| 202 | + uint32_t start_block, uint32_t num_blocks) { |
| 203 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
195 | 204 | wait_write_complete(self); |
196 | | - sd_mmc_err_t r = sd_mmc_init_read_blocks(0, start_block, bufinfo->len / 512); |
| 205 | + sd_mmc_err_t r = sd_mmc_init_read_blocks(0, start_block, num_blocks); |
197 | 206 | if (r != SD_MMC_OK) { |
198 | 207 | debug_print_state(self, "sd_mmc_init_read_blocks", r); |
199 | | - return -EIO; |
| 208 | + return -MP_EIO; |
200 | 209 | } |
201 | | - r = sd_mmc_start_read_blocks(bufinfo->buf, bufinfo->len / 512); |
| 210 | + r = sd_mmc_start_read_blocks(buf, num_blocks); |
202 | 211 | if (r != SD_MMC_OK) { |
203 | 212 | debug_print_state(self, "sd_mmc_start_read_blocks", r); |
204 | | - return -EIO; |
| 213 | + return -MP_EIO; |
205 | 214 | } |
206 | 215 | sd_mmc_wait_end_of_write_blocks(0); |
207 | 216 | return 0; |
208 | 217 | } |
209 | 218 |
|
| 219 | +mp_errno_t common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *bufinfo) { |
| 220 | + check_for_deinit(self); |
| 221 | + check_whole_block(bufinfo); |
| 222 | + |
| 223 | + uint32_t num_blocks = bufinfo->len / 512; |
| 224 | + return sdioio_sdcard_readblocks(MP_OBJ_FROM_PTR(self), bufinfo->buf, |
| 225 | + start_block, num_blocks); |
| 226 | +} |
| 227 | + |
| 228 | +// Native function for VFS blockdev layer |
| 229 | +bool sdioio_sdcard_ioctl(mp_obj_t self_in, size_t cmd, size_t arg, |
| 230 | + mp_int_t *out_value) { |
| 231 | + sdioio_sdcard_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 232 | + *out_value = 0; |
| 233 | + |
| 234 | + switch (cmd) { |
| 235 | + case MP_BLOCKDEV_IOCTL_DEINIT: |
| 236 | + case MP_BLOCKDEV_IOCTL_SYNC: |
| 237 | + // SDIO operations are synchronous, no action needed |
| 238 | + return true; |
| 239 | + |
| 240 | + case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: |
| 241 | + *out_value = common_hal_sdioio_sdcard_get_count(self); |
| 242 | + return true; |
| 243 | + |
| 244 | + case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: |
| 245 | + *out_value = 512; // SD cards use 512-byte sectors |
| 246 | + return true; |
| 247 | + |
| 248 | + default: |
| 249 | + return false; // Unsupported command |
| 250 | + } |
| 251 | +} |
| 252 | + |
210 | 253 | bool common_hal_sdioio_sdcard_configure(sdioio_sdcard_obj_t *self, uint32_t frequency, uint8_t bits) { |
211 | 254 | check_for_deinit(self); |
212 | 255 | return true; |
|
0 commit comments