micropython / micropython/micropython

STM32: How to avoid disabling USB IRQs in sdcard.c ?

Open
#7,148 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

port-stm32
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

https://github.com/micropython/micropython/blob/00d6a79b3d5dc80d840dc1d51166e7d95856b3d6/ports/stm32/sdcard.c#L599

Any suggestions for a better way to handle this other than disabling USB IRQs ? This is starting to be an issue especially when reading/writing many blocks in one call or reading/writing frequently in a loop, all USB IRQs are disabled and this stops other things that don't access MSC at all from working.

I tried setting a flag in sdcard.c when reading/writing instead of disabling IRQs, to indicate the card is busy and checking this flag in usbcd_msc_interface.c returning -1 from usbd_msc_IsReady() if the flag is set, it almost works except on the host I see I/O errors and things eventually fail... Is there anything else I can try ?

diff --git a/ports/stm32/usbd_msc_interface.c b/ports/stm32/usbd_msc_interface.c
index 62fe32bf5..71eb58259 100644
--- a/ports/stm32/usbd_msc_interface.c
+++ b/ports/stm32/usbd_msc_interface.c
@@ -242,9 +242,10 @@ STATIC int8_t usbd_msc_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *b
     return lu_ioctl(lun, MP_BLOCKDEV_IOCTL_BLOCK_COUNT, block_num);
 }
 
+extern volatile uint32_t sdcard_busy;
 // Check if a logical unit is ready
 STATIC int8_t usbd_msc_IsReady(uint8_t lun) {
-    if (lun >= usbd_msc_lu_num) {
+    if (lun >= usbd_msc_lu_num || sdcard_busy) {
         return -1;
     }
     return lu_flag_is_set(lun, FLAGS_STARTED) ? 0 : -1;

Also tried returning different status codes from here: https://publib.boulder.ibm.com/tividd/td/TSMM/GC32-0767-00/en_US/HTML/anrcms45.htm but all of them cause I/O errors on the host.

SCSI status codes, sense keys: https://www.t10.org/lists/1spc-lst.htm

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ports/stm32/sdcard.c around line 599 and trace how SD-card reads and writes disable USB IRQs. Then inspect ports/stm32/usbd_msc_interface.c, especially usbd_msc_IsReady(), and reproduce long or frequent block transfers with USB MSC active. Done means USB remains responsive during SD-card I/O without causing host I/O errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.