SD Ident / Card Stuck

Discussion about writing software for VS1005 and the VSOS Operating System. Also posts about VS1005-related hardware design and device drivers should be posted here.
johns
User
Posts: 9
Joined: Mon 2016-04-04 23:42

SD Ident / Card Stuck

Post by johns » Tue 2016-04-05 22:14

Hello,

I am experiencing a bit of strange behavior with the SD card on the VS1005 (using VSOS kernel 3.25).
In short, I have a simple WAV recorder application that runs in this sequence:

Initialize variables
Load drivers (AUIADC, AUINPUT, FILEBUF, SDSD)
Open audio input and set bits and rate
Wait for button press
Record while button is pressed (open wav file, write header, write samples)
Continue writing samples until button released
When released, update wav header, close file, close libraries (FILEBUF, AUIADC, AUINPUT, SDSD)
Infinite loop until reset or power cycle

Now the issue I am facing is this: On first boot, the SD card mounts properly and the recording works as intended, and all drivers and files are closed at the end. If I reset via the RESET pin, the program restarts and everything continues to work. However, when I cut power and then power it on again, when the SD driver loads, I get the error message "E'SD Ident / Card Stuck" but the driver seems to load anyway, as I also get the message "D: SD card in SD mode". (Of course when I try to record a wav file in this state, it's bad news.) When I power cycle again without attempting to record a wav file, the SD driver loads properly and I am able to record.

Here is a summary of difference scenarios I am seeing:

Boot > SD loads properly > Record WAV > Reset > Works fine
Boot > SD loads properly > Record WAV > Power cycle > SD Ident ERROR > Power cycle again > Works fine

Do you know why the SD driver would exhibit this kind of behavior?

The end goal is to have a device that powers on, boots, and begins recording a wav file until it is shut off by cutting power (still need to implement a shutdown circuit). I hope I am being clear with all of this information. Please let me know if there is anything I need to clarify!

Thank you for your help!
-John

User avatar
Henrik
VLSI Staff
Posts: 1136
Joined: Tue 2010-06-22 14:10

Re: SD Ident / Card Stuck

Post by Henrik » Wed 2016-04-06 15:04

Hello John!

Although I am not quite sure, I think the big issue for you may be that you try to record until you forcefully power off the board. This will leave the file system in an unclean state and will cause you trouble later.

Another reason might be if the power to the SD card is not removed along with the power to VS1005. If so, the SD card will in the best case be left inbetween two block writes inside a larger Multiple Block Write operation, and in the worst case in the middle of a single block write. The latter case may break the SD card permanently.

All in all, no system writing to an SD card should abruptly power off. The only way for the user to cut power should be by managed powerdown: the user pushes the power button (which is read as a software switch), then the firmware finalizes writing to the file (fclose() etc), then turns power off with software. For the VS1005 Developer Board there is a little program called PowerOff which is provided with source code here:
viewtopic.php?f=13&t=1752

See main.c; it's a small piece of code.

Then again, I cannot really explain why the power cycle should be different from a reset cycle. I tried both ones (but without writing to the SD card), and as I think you said yourself, in this case there were no problems.

Hope this helps you!

Kind regards,
- Henrik
Good signatures never die. They just fade away.

johns
User
Posts: 9
Joined: Mon 2016-04-04 23:42

Re: SD Ident / Card Stuck

Post by johns » Wed 2016-04-06 17:05

Hi Henrik,

Thank you for the quick response!

I learned not to cut power abruptly from an SD the hard way... I already permanently broke one :oops:
Right now, the SD is actually powered by a separate 3V3 regulator with the VHIGH as the input, so when I cut VHIGH, the SD power is cut at the same time.

In the final application, I was planning on implementing a sort of safe shutdown circuit using capacitors, so that when the power is cut, the board will finish what it is doing and then go into a shutdown routine, with the capacitor providing power for the amount of time necessary as it is discharging. For now, though, when I finish writing a file, I make sure to fclose() and close all drivers and then go into an infinite idle loop before I cut power.

I will go ahead and give the software shutdown a try though and report back!

Thanks,
-John

User avatar
Henrik
VLSI Staff
Posts: 1136
Joined: Tue 2010-06-22 14:10

Re: SD Ident / Card Stuck

Post by Henrik » Thu 2016-04-07 10:37

Hi John!
johns wrote:I learned not to cut power abruptly from an SD the hard way... I already permanently broke one :oops:
Yes, we also have a few cards that have died in this very way. And while I'm at it: another great way to kill an SD card permamnently is to mash random bits to it - happened me a couple of times when developing the SDSD driver and it crashed.
johns wrote:In the final application, I was planning on implementing a sort of safe shutdown circuit using capacitors, so that when the power is cut, the board will finish what it is doing and then go into a shutdown routine, with the capacitor providing power for the amount of time necessary as it is discharging. For now, though, when I finish writing a file, I make sure to fclose() and close all drivers and then go into an infinite idle loop before I cut power.
If you cannot control the power disconnect itself, this is definitely the way to do it. Just remember that fclose() can at worst case take up to almost half a second if the SD card just happens to do its internal updates at that point, so make sure you have enough capacitor power for both the CPU and the SD card!

Kind regards,
- Henrik
Good signatures never die. They just fade away.

User avatar
Panu
VLSI Staff
Posts: 2674
Joined: Tue 2010-06-22 13:43

Re: SD Ident / Card Stuck

Post by Panu » Thu 2016-04-07 16:42

You can sometimes rescue a bricked SD card with an old Nokia phone that has Symbian OS. You can also try a Canon digital camera that has an SD card slot. Software, such as Nokia's, that understands SD cards and does not treat them as just abstract mass storage can clear the password and protections in the SD card when you reformat the card. Authors of such software must be members of the SD Association so they have the access to the secure data protocols. Often SD cards are bricked by inadvertently setting a garbled password. This can somewhat easily happen by sending garbage data waveforms to the card's cmd pin. And power failures during writing can mess up the card registers in funny ways.

-Panu
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

johns
User
Posts: 9
Joined: Mon 2016-04-04 23:42

Re: SD Ident / Card Stuck

Post by johns » Wed 2016-04-13 16:42

Hi,

I was messing around a bit more, and I did implement the safe shutdown code. Unfortunately, that did not quite fix the issue (but I imagine it's a good idea to use anyway).
I did find, however, that changing the power source seemed to change things. Before, I was actually powering the SD card from a separate 3.3V regulator, powered by VHIGH, since that is how the developer board powers the SD card (unless I am misinterpreting the schematic). When I switched the SD power source to the IOVDD from the VS1005's regulator, the SD error "Ident/Card Stuck" no longer happens after recording audio and resetting.

But now, strangely, when the board is reset WITHOUT recording audio, the SD shows an error "SD data bus set up". To get it working again, I need to disconnect the SD, reset, then reconnect the SD and reset again. This scenario technically should never happen in my application anyway, since the device should begin recording a new file immediately upon booting.

I am still working on the safe shutdown circuit, but other than that, I will report back again if I face more issues!

Thanks again for your support!
-John

User avatar
Panu
VLSI Staff
Posts: 2674
Joined: Tue 2010-06-22 13:43

Re: SD Ident / Card Stuck

Post by Panu » Thu 2016-04-14 8:59

The SD card needs to be reset by pulling all pins low, including the vcc pin. For this, there is a function ResetSdCard() in the kernel. The regulator needs to have an enable pin, which must be controllable by the VS1005. From what I've read, could it be that the regulator enable pin in your original board is not behaving correctly? The Developer Board has a latch to drive this signal and the kernel for the developer board is using this latch. If you don't have it or the ehable pin is connected differently, it might be toggling on and off during normal operation, which would be a sure way to kill the card. And the "sd card data set up" error strongly points to not being able to fully reset the card at startup by cycling the sd power off and on.

-Panu

PS. For boards other than the developer board, we've either rewritten ResetSdCard() in the kernel to use another enable pin for the sd power, or hooked a custom handler to the SetPower hook, like we did for the VS1005 amp board.
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

johns
User
Posts: 9
Joined: Mon 2016-04-04 23:42

Re: SD Ident / Card Stuck

Post by johns » Sat 2016-04-23 0:05

Hi Panu,

I have verified that your suggestion is in fact what was wrong with my setup. When I cut power, and everything shuts down (I have implemented my safe shut down circuit now) and then simply reset it as it is, the SD boots the next time with an error and it is unable to write. However, when I disconnect the SD power (with a pull-down resistor) and connect it again before rebooting, everything works as intended :)

Now I will just need to work out a good way to automatically do this switching on boot up.

I think now everything should be resolved, as far as this topic goes! Thanks again for your support!

-John

User avatar
Panu
VLSI Staff
Posts: 2674
Joined: Tue 2010-06-22 13:43

Re: SD Ident / Card Stuck

Post by Panu » Sat 2016-04-23 10:02

Hi, John!

That's great to hear! Keep in touch if you need help in understanding how the SetPower() hook or the ResetSdCard() function is supposed to work.

At boot time and whenever there's a problem with the SD card, the kernel makes an IOCTL_RESTART call to the SD driver, which calls ResetSdCard in kernel:power_devboard.c. That pulls the SD card's IO pins low and then calls SetPower(PCL_SDCARD, 0) to switch the SD card regulator off, and after a small delay it calls SetPower(PCL_SDCARD, 1) to switch the SD card regulator on. For the best reliability, you should have a regulator with enable for the SD card and implement this function (SetPower) to switch the regulator on and off.

The SetPower() function is hookable since VSOS 3.27. With VSOS 3.26 or earlier, you must make the modifications to SetPower() function in the kernel.

-Panu

PS. You really only need to control the SD card regulator in your SetPower function. The only other use for SetPower is in the USBHOST driver, which calls SetPower(PCL_USB_A | PCL_USB_5V, 1) to switch on the USB host port power supply in the developer board.
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

User avatar
Panu
VLSI Staff
Posts: 2674
Joined: Tue 2010-06-22 13:43

Re: SD Ident / Card Stuck

Post by Panu » Sat 2016-04-23 10:16

Dear John,

Here's a driver that Henrik wrote for the Amp Board to switch that board's SD card regulator on and off. Simply loading this driver from CONFIG.TXT in VSOS3.27 or later changes the operation of SetPower() to use GPIO0.7 pin to control the SD card regulator enable instead of the latch in the developer board. It's written for an active high enable.

Code: Select all

/// \file ampbconf.c Driver needed to get VS1005 Amp Board work with SD card.
/// \author Henrik Herranen, VLSI Solution Oy
#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <kernel.h>	  // Kernel exported functions
#include <timers.h>
#include <devboard.h>
__mem_y u_int16 origSetPower = 0;

void AmpBoardSetPower(register u_int16 mask, register u_int16 onoff) {
  if (mask == PCL_SDCARD) {
    GpioSetPin(0x07, onoff);
  }
}

ioresult init(char *paramStr) {
  origSetPower = (u_int16)(ReadIMem((void *)SetPower) >> 6);
  SetHandler(SetPower, AmpBoardSetPower);
  ResetSdCard();
  return S_OK;
}

void fini(void) {
  SetHandler(SetPower, (void *)origSetPower);
}
For your reference, here's a sample CONFIG.TXT that uses the driver.

Code: Select all

[0]
# Load AmpBoard-specific compatibility driver (needed by SD driver)
AMPBCONF
# Start SD card as device D
SDSD D
# Try to run update from SD card if available
D:UPDATE.DL3
# New 2015 audio DAC out driver
AUODAC s
# New 2015 audio ADC in driver
AUIADC s 48000 line2_1 line2_2
# S/PDIF automatic out, parameter can be either 48000 or 96000 (default)
# If parameter "v" is defined, SPDIF automatically copies stdaudioout volume
AUOSPDA 96000
# Start UART in/out driver
UARTIN
# Automatically execute code from SD card if available
RUN D:AUTOEXEC.DL3
# Wait 150 ms before setting audio driver parameters
RUN DELAY 150
# Set output volume to -30 dB of maximum. Note that AUOUTPUT is not a
# driver and we don't want it to remain in memory after execution, so we
# use it with "RUN". Note also that we set both input and output to
# 32-bit mode for the best possible sound quality. If you intend to write
# 16-bit-audio applications, you need to change -b32 to -b16, or do
# appropriate ioctl() calls in your code. See the VSOS Audio Subsystem
# document for details.
RUN AUOUTPUT -b32 -l-30
# Set correct audio inputs for the Amp Board
RUN AUINPUT -b32 line2_1 line2_2
# Try to play files from main folder of SD card if available
RUN PLAYFILES D:*
# Start the shell
S:SHELL.AP3
-Panu
Attachments
AMPBCONF.dl3
Driver to change SD card power control to pin GPIO0.7. VSOS3 Executable.
(516 Bytes) Downloaded 182 times
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

Post Reply