Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:26 22 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : CMM2: Question on using MM.INFO$(SDCARD) (and tiny error in manual)

Author Message
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 367
Posted: 04:31pm 21 Oct 2024
Copy link to clipboard 
Print this post

Hi all,

I'm trying to monitor the removal and insertion of an SD Card into my CMM2 (for my file manager).

I've been using MM.INFO$(SDCARD) (as noted below from the manual).



It works great to start with. I can see that the SD Card is "Ready" when it's in. When I remove it, it changes to "Not present" - but when I put it back in again - it changes to "Unused". It stays at "Unused" and I can't see a way to get it back to "Ready". Any file actions I try and take on this "Unused" SD Card generate an I/O error.

P.S. The tiny error in the manual is that the returned states are not all capitalised - they start with a capital and are then lower case (e.g. "Ready", "Unused"). Not a big deal - I quickly found the issue by using "PRINT MM.INFO$(SDCARD)" when my logic failed to find "READY".

Here's the code I've been using to test this behaviour. This displays the SD Card status each time it changes. Pop the SDCard out and then pop it back in again and you'll see what I mean.

The code does create a folder on your drive call "GUI" just as a test of the file I/O. That will fail when the card is "Unused".

OPTION EXPLICIT
DIM STRING SDCardStatus

'Main loop
DO
 ReadInsertedSDCard
LOOP

'Subroutine to check SD card
SUB ReadInsertedSDCard
 IF MM.INFO$(SDCARD)=SDCardStatus THEN EXIT SUB ' Nothing has changed

 SDCardStatus=MM.INFO$(SDCARD)
 PRINT "New SD Card status:" + SDCardStatus

 IF SDCardStatus<>"Ready" AND SDCardStatus<>"Unused" THEN
   EXIT SUB
 END IF

 ' If we are here, then the SD card is okay
 IF MM.INFO(EXISTS DIR "A:\GUI")=0 THEN
   MKDIR "A:\GUI"
 ENDIF
END SUB
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9084
Posted: 05:07pm 21 Oct 2024
Copy link to clipboard 
Print this post

From what I remember the CMM2 doesn't support card removal and replacement inside a program. I assume if you do this at the command line it works?
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 367
Posted: 05:16pm 21 Oct 2024
Copy link to clipboard 
Print this post

Thanks Peter. Yes, I can confirm that it works fine from the command line.

Not to worry. I was hoping to use it to copy files between SD Cards using a RAM disk. i.e. I would store the directory being copied into graphics memory (as binary data) swap disks and then write the directory back down to the second SD Card.

But, if it doesn't support it, then it's not a problem. I do have a plan B, which is maybe even better.  

Thanks for looking into it and clarifying.
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1057
Posted: 09:49pm 21 Oct 2024
Copy link to clipboard 
Print this post

I don't know if this is helpful or not but I set/reset a flag "No_SD_Flag" using the up & down keys on a connected keyboard or IR remote.
All routines accessing the SD card check the flag first. If no SD card, Exit Sub
I press the down arrow, wait for the screen message & then remove the card. When finished, reinsert the card & press the UP arrow key. The sub routine does check that the card is actually there.

If you insert a different card, you will need another flags & commands.

 '==================================================================================================
SUB SD_Card_Rem  'Set flag if removed. If inserted, update data as necessary
 FONT 2
 No_SD_Flag = 1               'set flag to stop R/W. Card can be removed
 TEXT 1700, 940, "REMOVE SD CARD ", "LT", 2, 1, RGB(RED), RGB(BLACK):PRINT @(0,0)
END SUB
 
 '=====================================================================================================
 
SUB SD_Card_Ins  'Set flag if removed. If inserted, update data as necessary
 FONT 2
 print @(1700, 960) mm.info(sdcard) "     "
 if MM.INFO(sdcard) <> "Not present" then
   No_SD_Flag = 0
   Data_Flag = 0      'data_Flag & SD Card inserted
   TEXT 1700, 940, "SD Card OK     ", "LT", 2, 1, RGB(green), RGB(BLACK):PRINT @(0,0)
   Check_SD
 endif
END SUB
 
 '=======================================================================================================
 
SUB Data_Save     'saves most screen data daily every 5 mins to SD card.
 FONT 2
 if No_SD_Flag = 1 then exit sub
.
.
.


Brian
ChopperP
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 367
Posted: 07:59pm 22 Oct 2024
Copy link to clipboard 
Print this post

  Chopperp said  I don't know if this is helpful or not but I set/reset a flag "No_SD_Flag" using the up & down keys on a connected keyboard or IR remote. ....
Brian


Thanks Brian. Could you clarify that you do get the SD card working after it has been re-inserted into the CMM2? If so, then I'm wondering if this is a quirk of the type of SD card I'm using.

Pete
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1057
Posted: 11:57pm 22 Oct 2024
Copy link to clipboard 
Print this post

Hi Pete

Yep, I reinsert the card, the CMM seems to detect it OK & I then press the appropriate key to kill the flag & off she goes. Works on 3 units.

I use micro SD's in adaptors.

Brian
ChopperP
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 367
Posted: 06:52pm 23 Oct 2024
Copy link to clipboard 
Print this post

  Chopperp said  Hi Pete

Yep, I reinsert the card, the CMM seems to detect it OK & I then press the appropriate key to kill the flag & off she goes. Works on 3 units.

I use micro SD's in adaptors.

Brian


Thanks Brian - your information pointed me in the right direction, and I managed to get it working.

The key part is that you are waiting until a keypress before testing the card. It turns out that part of my problem was that I needed to "pause" before trying to read the SD Card. The second part of the solution was that I need to call a specific MM.INFO call - which flips the SD card status back to "Ready".

To clarify:
If the SD Card status is "Unused", you need to pause for 100ms, then call either MM.INFO(DISK SIZE) or MM.INFO(EXISTS DIR "A:"). I guess the pause gives the drive a chance to initialise, and then the MM.INFO looks for file information, but doesn't actually do any FILE INPUT/OUTPUT. This call then mounts the SD Card in MMBASIC (or some other process that updates the Status to "Ready")?????

Any other attempts to access the drive before either of those MM.INFO calls will crash the program.

The "DISK SIZE" call takes a second or two to run, so I just use MM.INFO(EXISTS DIR "A:") which just checks for the directory A: - which should always exists - and this runs instantly.

Here is the full code if anyone has the same issue.  Removing either the Pause or the MM.INFO calls at the end of the program will cause the program to break with an error.

OPTION EXPLICIT
DIM STRING SDCardStatus
PRINT "RESTARTED"
'Main loop
DO
 ReadInsertedSDCard
LOOP

'Subroutine to check SD card
SUB ReadInsertedSDCard
 IF MM.INFO$(SDCARD)=SDCardStatus THEN EXIT SUB ' Nothing has changed

 SDCardStatus=MM.INFO$(SDCARD)

 PRINT "New SD Card status:" + SDCardStatus
 IF SDCardStatus<>"Ready" AND SDCardStatus<>"Unused" THEN
   EXIT SUB
 END IF

 IF SDCardStatus="Unused" THEN
   PAUSE(100)     ' This is the magic line that prevents an error msg when we query MM.INFO
 END IF

 PRINT "SD Card Status before MM.INFO : " + MM.INFO$(SDCARD)
 PRINT MM.INFO(EXISTS DIR "A:")
'  PRINT MM.INFO(DISK SIZE)     ' This also works but takes a second or two
 PRINT "SD Card Status after MM.INFO : " + MM.INFO$(SDCARD)

END SUB



Or to pare it right down to the important logic:
 IF SDCardStatus="Unused" THEN
   PAUSE(100)
   PRINT MM.INFO(EXISTS DIR "A:")
   ' SD card is now "ready"
 END IF


Edited 2024-10-24 04:59 by PeteCotton
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1057
Posted: 09:09pm 23 Oct 2024
Copy link to clipboard 
Print this post

Hi Pete
I'm glad you sorted it out.
Brian
ChopperP
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024