Real time write on sd card

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

   

I need to write a ECG signal on sd card but i can't because i can't get all samples with ADC.

   

This is code:

   

 

   

    for (i = 0; i < 2000; i++)
    {      
            
       if(ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT))
        { 
            output = ADC_DelSig_1_GetResult32();
            tensao_auxR = ADC_DelSig_1_CountsTo_mVolts(output);
        a=a+1;
        sprintf(buffer,"%4.2d\t %6.2f \n", a,tensao_auxR);
        FS_Write(pFile,buffer,sizeof(buffer));
        //CyDelay(0.1);
       
        }
        
    }

   

 

   

I can't find the error.

   

Can help me ?

   

 

   

Regards.

0 Likes
1 Solution
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Using a pointer you simply write doubles at a time, independent of what

   

the underlying variable is, then reconstruct on client side knowing

   

what is actually being sent.

   

 

   

Regards, Dana.

View solution in original post

0 Likes
33 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

What is the sample rate fo the ADC ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

the conversion rate (sps) is 4000.

   

Mode is multi-sample

   

Regards

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You are getting a sample every 250 uS, does the SDCARD write that

   

fast ? Eg. 4 bytes every 250 uS, or ~ 62 uS / byte write rate. When you

   

say you are not getting all samples, does that mean you miss writing some

   

of the samples the A/D is sending but that A/D is generating them all ?

   

 

   

Do a quick case, flip a pin high, write 2000 test vectors, say an incrementing

   

numeric, to the SDCARD, when done flip pin low. Measure pin high time

   

with a scope or whatever, and see what the write rate you are getting from

   

SDCARD controller (the one inside the SDCARD) is. A quick check is SDCARD

   

keeping up with sample rate.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

The sd card speed is 3.74mb/s .

   

I think is generate all of the samples..

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The only other thing here that interferes in speed is the sprintf() code

   

execution time, maybe it is causing samples to be skipped. Try the

   

test to get an idea of loop execution time.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

what do you mean about loop execution time?

0 Likes
Anonymous
Not applicable

I think the problem is in ADC.
I need to get sine wave with a frequency of 400Hz.
What kind of configuration is that the ADC should have?

   

Thanks.

   

Regards.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Loop execution is the time consumed each pass thru your for() loop.

   

 

   

If you are trying to sample a 400 Hz sine then Nyquist would have min sample

   

rate of 800 Hz.

   

 

   

What are you actually trying to measure ? RMS, Avg, Peak......or just capture the EKG ?

   

 

   

Regards, Dana,

0 Likes
Anonymous
Not applicable

If the loop execution consume time which advises to use?

   

I only need to capture the wave signal for the ECG signal.

   


Regards,

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Basically you want to sample at a rate min 2x, better yet 5 - 10 x the highest freq component in

   

EKG. Thats something you can experiment with.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Thanks. 

   

what is alternative to loop execution? i need to capture signal for 60 seconds.

   

 

   

Regards.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Loop quite appropriate to control sampling and subsequent SDCARD write.

   

 

   

But you could shorten loop by writing raw data to SDCARD instead of the sprintf

   

converted data, and interpret sdcard data at a later time. Main idea is just make sure

   

your sample rate is not being altered by sprintf() latency/execution time,

   

eg. missing samples on SDCARD. Flip a pin high just before sprintf() is called,

   

then flip low after call, and look at with scope to get an idea of time consumed.

   

It should be << 1 / SPS A/D.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

ok. 

   

now i use fwrite to write on txt file. which you recommend to write a int values on sdcard and it is fast?

   

Thanks.

   

Regards.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The write speed of the card will be the same, but the elimination of

   

the sprintf() execution will increase thruput.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

if i eliminate the sprintf() i can´t write on sdcard.

   

Regards.

0 Likes
Anonymous
Not applicable

any solution?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The SDCARD does not have any API to write byte/word data ?

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

4.4.5 FS_FWrite()
Description
Writes data to an open file.
Prototype
U32 FS_FWrite(const void * pData,
U32 Size,
U32 N,
FS_FILE * pFile);
Return value
Number of elements written.
Parameter Description
pData Pointer to data to be written to the file.
Size Size of an element to be transferred from a data buffer to a file.
N Number of elements to be transferred to the file.
pFile Pointer to a data structure of type FS_FILE.
Table 4.25: FS_FWrite() parameter list

0 Likes
Anonymous
Not applicable

i check this function but the problem is same.

   

i have the integer values. how can i convert to void *pdata?

   

Regards

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Using a pointer you simply write doubles at a time, independent of what

   

the underlying variable is, then reconstruct on client side knowing

   

what is actually being sent.

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Converting a datafield to a pointer is done by the "&" operator which takes as the result the address of the referenced data.

   

In your special case it would be better (faster) not to write a single item, but to collect some more and write them as a bunch. You may consider using an array of n elements and when that got filled write out all of them.

   

Some lit on pointers, addresses and arrays in C language here.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

... and some more: the printf() family converts data into human-readable ASCII characters. Since neither the PSoC nor the SD_card are humans there is absolutely no urgent need converting your data before writing to the SD. Only at the time when the data has to be presented to a human (or sent in an email) a conversion is required.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Based on your sample size desired you should consider a buffer for

   

each period of waveform, and send that out. You should be able to do

   

that simultaneously while acquiring the next period of samples, using

   

a ping pong buffer arrangement.

   

 

   

Problem is detecting period, possibly peak detection, or a form of zero

   

cross would suffice. Solution would have to accommodate some form of AGC

   

so period to period detection not affected by system G.

   

 

   

Or to avoid all this keep sending individual samples, SDCARD can support

   

the data rate.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

One advantage to using sprintf() is exactly because they are printable chars,

   

debug the data stream, simple error detection (values have restricted ranges).....

   

 

   

Ability to frame your data with a non printable character.....

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

When i use this sintaxe, i have a error  ("Build error: Illegal pointer conversion")

   

FS_Write(pFile,ADC_DelSig_1_CountsTo_mVolts(ADC_DelSig_1_GetResult32()),sizeof(ADC_DelSig_1_CountsTo_mVolts(ADC_DelSig_1_GetResult32())));

   

how can i solve this?

   

Regards

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Yes, of course an error. The result of the function is data, not a datafield. When you store the result in a variable (datafield) first you can specify FS_Write(pFile,&MyADCvalue,....).

   

It is like a car: A car does not have got an address, the garage where you park it has an address. Read about pointers in the C-manual. They are not quite easy to understand, but when you got them, they enrich your programming technique immense!.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

but i use &MyADCvalue and didn't work...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

...and how did you define MyADCvalue and where did you store the value in?

   

Watch: everytime you get an error when compiling, it is you that made the error. The explanation is usually quite correct if you didn't forget some braces etc.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

i told you. the MyADCvalue is a unit32 and i need to convert on void*pdata...

   

thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

So step-by step:

   

MyADCvalue is an uint32

   

then

   

&MyADCvalue is a pointer to an uint32

   

and

   

(void*)&MyADCvalue is a void pointer to something (what the meaning of void* is). Putting the type specifier -in this case the void*- in brackets before a variable is named a "Type Cast" which even could imply some conversions like a typecast of an integer to a float.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

check the attach i add.

   

the error was "1111111111111111111111111"

   

 

   

Thanks

0 Likes
Anonymous
Not applicable

any help?

   

Thanks

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You can get help, but what do I have to understand under  the error was "1111111111111111111111111"

   

You can stop with debugger and watch your vars before writing them to SD.

   

And consider to post your complete project and not only snippets, some relevant information gets lost. To do so, use
Creator->File->Create Workspace Bundle
and attach the resulting file.

   

 

   

Bob

0 Likes