CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SD card read-write

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
georpo



Joined: 18 Nov 2008
Posts: 278
Location: Athens, Greece.

View user's profile Send private message

SD card read-write
PostPosted: Thu Aug 04, 2011 2:34 pm     Reply with quote

Hello forum!

I am making a 2 channel audio recorder.
For ADC I am using PCM1801 interfaced to a DSPIC33FJ64GP802 via DCI and for DAC I use the built in 16bit audio DAC of the dspic.

Files are stored/played back on a microSD card through SPI, no file system. Just raw data.

case 1) I can record the 2 channels simultaneously with success.
case 2) I can playback the 2 channels simultaneously with success.

Now, the problem is that for example I want to record only 1 channel, then play it back and at the same time record the other channel.
Let's say that you record a guitar in the 1st channel and then you want to listen to it while you record vocals on the second channel.
Just like a multitrack recorder.

As you may know, when you write to the sd card, you must write an entire sector=512bytes.
Now, half of this sector (256 bytes) belongs to the one channel, and the other half belongs to the 2nd channel.
This means that first word (2 bytes) is for channel #1, second word (2 bytes) is for channel #2 and so on.

I have to write 1 word (16bits - 2 bytes) and at the same time keep the other written word unmodified.

First thing that came in mind is to first read the entire sector to RAM, then modify each 2nd word that belongs to the 2nd channel and then write the sector back to the card.
This will contain the data from the 1st channel unmodified plus the data from the second channel.

This is impossible to happen with real time running audio.
Reading an entire sector, then modifying and writing it back, takes lots of time.

Come on experts! I need your help

Rolling Eyes

**

I just measured that a sector read takes 1.92mS at 10MHz SPI clock.
This is too slow...
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

Re: SD card read-write
PostPosted: Thu Aug 04, 2011 3:22 pm     Reply with quote

georpo wrote:
Hello forum!

I am making a 2 channel audio recorder.
For ADC I am using PCM1801 interfaced to a DSPIC33FJ64GP802 via DCI and for DAC I use the built in 16bit audio DAC of the dspic.

Files are stored/played back on a microSD card through SPI, no file system. Just raw data.

case 1) I can record the 2 channels simultaneously with success.
case 2) I can playback the 2 channels simultaneously with success.

Now, the problem is that for example I want to record only 1 channel, then play it back and at the same time record the other channel.
Let's say that you record a guitar in the 1st channel and then you want to listen to it while you record vocals on the second channel.
Just like a multitrack recorder.

As you may know, when you write to the sd card, you must write an entire sector=512bytes.
Now, half of this sector (256 bytes) belongs to the one channel, and the other half belongs to the 2nd channel.
This means that first word (2 bytes) is for channel #1, second word (2 bytes) is for channel #2 and so on.

I have to write 1 word (16bits - 2 bytes) and at the same time keep the other written word unmodified.

First thing that came in mind is to first read the entire sector to RAM, then modify each 2nd word that belongs to the 2nd channel and then write the sector back to the card.
This will contain the data from the 1st channel unmodified plus the data from the second channel.

This is impossible to happen with real time running audio.
Reading an entire sector, then modifying and writing it back, takes lots of time.

Come on experts! I need your help

Rolling Eyes

**

I just measured that a sector read takes 1.92mS at 10MHz SPI clock.
This is too slow...


A few thoughts:


1. Are you hardware limited to a 10MHz SPI clock?
2. The sector read time you have given, is this for the first read or for successive reads?
3. You could decouple the low level reading and writing from the audio capture / playback. Basically you would have a large ring buffer which contains multiple 512 byte blocks. This decoupling deals with the latency variation associated with SD card operations. This technique should work in your case because you are performing linear operations on the data. The low level reading and writing to the card would be a mainline operation while the payback and capture would be timer based interrupt driver.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
georpo



Joined: 18 Nov 2008
Posts: 278
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 3:38 pm     Reply with quote

Hello Andrew,

My hardware is not limited to 10MHz but:
I want to use maximum speed of the dspic so I clock it to 80Mhz which gives fcy of 40MHz.
SPI clock options for my case are:

a)SPI_CLK_DIV_1 = 40MHz. Too fast for the SD card. I tried it. It does not work.
b)SPI_CLK_DIV_4 = 10MHz. works OK but limits my speed.

I think that a good SD card could operate at 25MHz or so.
Of course I could use SPI_CLK_DIV_1 but then I will have to operate the dspic at a clock freq. of 50MHz max which gives me fcy of 25Mhz.


The sector time that I mention is for reading just one sector which includes the commands to the sd card, the response from the card, data token, 512 bytes of data, 2 CRC bytes.

Of course successive read would be much faster but in my case I need to read only 1 sector in RAM and then modify each 2nd word based on my incoming data. So, I have to use single sector read.


Quote:

You could decouple the low level reading and writing from the audio capture / playback


I do not understand what you mean here...

The ring buffer should be in RAM. Right?


Last edited by georpo on Thu Aug 04, 2011 3:46 pm; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Aug 04, 2011 3:40 pm     Reply with quote

Yes. The ring buffer would be in RAM.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
georpo



Joined: 18 Nov 2008
Posts: 278
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 3:45 pm     Reply with quote

The capture/playback does not have to be timer based int.
I get interrupts from the dspic DAC and from the DCI module when data is needed.

could you give me a flowchart of your idea??
I mean what is the sequence of this?
_________________
George.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group