You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function forwards the call to "shared-module/sdcardio/SDCard.c", function "common_hal_sdcardio_sdcard_writeblocks":
intcommon_hal_sdcardio_sdcard_writeblocks(sdcardio_sdcard_obj_t*self, uint32_tstart_block, mp_buffer_info_t*buf) {
// deinit check is in lock_and_configure_bus()if (buf->len % 512!=0) {
mp_raise_ValueError_varg(MP_ERROR_TEXT("Buffer must be a multiple of %d bytes"), 512);
}
lock_and_configure_bus(self);
intr=sdcardio_sdcard_writeblocks(MP_OBJ_FROM_PTR(self), buf->buf, start_block, buf->len / 512);
extraclock_and_unlock_bus(self);
returnr;
}
In this function, the SPI locking and setting is performed and then the "sdcardio_sdcard_writeblocks" function is called in which the locking is called again:
mp_uint_tsdcardio_sdcard_writeblocks(mp_obj_tself_in, uint8_t*buf, uint32_tstart_block, uint32_tnblocks) {
// deinit check is in lock_and_configure_bus()sdcardio_sdcard_obj_t*self=MP_OBJ_TO_PTR(self_in);
if (!lock_and_configure_bus(self)) {
returnMP_EAGAIN;
}
…
Since the line is already taken, the lock fails and an MP_EAGAIN error is returned. But besides the fact that the SPI is taken twice, the returned error is greater than zero, which is treated as no error in the "_sdcardio_sdcard_writeblocks" function and is ignored.
The problem does not occur when using storage|vfsFAT, because in the file "extmod/vfs_blockdev.c", functions "mp_vfs_blockdev_init":
It is necessary to replace errors (MP_EAGAIN) with (-MP_EAGAIN), and remove line lock/unlock in the function "common_hal_sdcardio_sdcard_writeblocks", as it is done in "common_hal_sdcardio_sdcard_readblocks".
The text was updated successfully, but these errors were encountered:
Thanks for the detail issue description, I looked through the modules you covered and I could be mistaken but I think there is an unsigned integer issue.
sdcardio_sdcard_writeblocks/readblocks return mp_uint_t values so the negative errors end up as positive 2s-compliments in common_hal versions of the routines. The mp_unit_t write_blocks routine appears to only return EAGAIN, errors with mangled negative codes or a zero so the return value could be forced negative if non-zero in the calling routine.
That feels like a bit of a hack but I don't know if changing the native sdcardio_sdcard routine return value type to mp_int_t would cause problems, the only other place I spotted it being used was "extmod/vfs_blockdev.c". If the native routines return type was changed then I think more consistent error handling could be implemented throughout the routines.
I'm assuming that all functions that call “sdcardio_sdcard_writeblocks” and “sdcardio_sdcard_readblocks” are of int type, so changing the function type to mp_int_t and negating MP_EAGAIN should not break anything.
If do not change the function type, everything will still work. A negative error will become just a large positive number, for example MP_EAGAIN = 11, but if you change it to -11, the functions will return something like 0xFFFFFFF5. But since all calling functions are of int type, the error will convert to -11 again.
I have already tested sdcardio working if I change only the MP_EAGAIN sign, and it works as expected, giving a busy error to the console.
CircuitPython version and board name
Code/REPL
Behavior
Description
The writeblocks function from the sdcardio library does not work in principle. Any attempts to use it fail. And here's why:
Since the line is already taken, the lock fails and an MP_EAGAIN error is returned. But besides the fact that the SPI is taken twice, the returned error is greater than zero, which is treated as no error in the "_sdcardio_sdcard_writeblocks" function and is ignored.
The problem does not occur when using storage|vfsFAT, because in the file "extmod/vfs_blockdev.c", functions "mp_vfs_blockdev_init":
Binds calls without “common_hal_”.
Additional information
It is necessary to replace errors (MP_EAGAIN) with (-MP_EAGAIN), and remove line lock/unlock in the function "common_hal_sdcardio_sdcard_writeblocks", as it is done in "common_hal_sdcardio_sdcard_readblocks".
The text was updated successfully, but these errors were encountered: