cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-L476RG: Write ADC values into SD_CARD using SPI

GChir.2
Associate II

Good Morning.

For my thesis work, I have to develop a sensor for seismocardiography based on sensortile.

Being unfamiliar with microcontrollers, I'm running some tests using an L476RG nucleo board.

I created a project that foresees continuous analog-digital conversion; I can save the data thus obtained in a circular buffer using dma but I cannot save the data obtained on sd.

When the HAL_ADC_ConvHalfCpltCallback interrupt occurs, I send the data contained in the first half of the buffer containing the readings of the adc to an auxiliary buffer using dma. At this point, when the memory to memory transfer is complete, I need a for loop to convert the data contained in the "TxBuffer" buffer into a string and concatenate these strings to create a single string to be saved on the SD (to reduce the time of writing).

The same should happen when the HAL_ADC_ConvCpltCallback interrupt occurs so that no reading of the ADC is lost.

However, a hard fault occurs when the first for loop is executed.

Where am I wrong?

Sorry for the banality of the question but, as mentioned, it is the first time that I work on microcontrollers

16 REPLIES 16
TDK
Guru

> uint16_t TxBuffer[ADC_buffer_Size/2];

>

> ...

>

> for(int indx=0; indx<sizeof(TxBuffer); indx++)

> {

> sprintf(adc_value, "%hu\r\n", TxBuffer[indx]);

sizeof(TxBuffer) = ADC_buffer_Size, not ADC_buffer_Size/2.

Consider adding another variable which gives the length of the array so you don't confuse yourself.

const uint32_t TxBuffer_length = ADC_buffer_Size/2;
uint16_t TxBuffer[TxBuffer_length];
 
...
 
    for(int indx=0; indx<TxBuffer_length; indx++)

There may be other errors.

You should be able to launch a debug session and figure out exactly where the code crashes.

> strcat(data_SD+strlen(data_SD),adc_value);

If you're going to use strlen to figure out how much data is stored already, you need to initialize the array with zero: data_SD[0] = 0. You should also be checking to make sure you're not writing past the end of the array.

If you feel a post has answered your question, please click "Accept as Solution".
GChir.2
Associate II

@TDK​  Thank you for your quick answer.

I tried to apply your suggestion using these lines of code

  1. const uint32_t TxBuffer_length = ADC_buffer_Size/2;
  2. uint16_t TxBuffer[TxBuffer_length];

but i had this error: variably modified 'TxBuffer' at file scope

I solved it like this:

  1. #define TxBuffer_length ADC_buffer_Size/2
  2.  
  3. ...
  4. uint16_t TxBuffer[TxBuffer_length];

Is it correct?

Now the for loop inside the "DMATransferComplete" interrupt is completed correctly (data_sd buffer is filled for just over half) and the code hangs in corrispondence of instruction "Mount_SD (" / ");" inside the interrupt. 

I don't understand why.

Thanks again for the help

XZhen.2
Associate II

@GChir.2​ Hi!

I am trying to access SD card using SPI and i think i am using the same FATFS&SPI code that you are using.

but i cant manage to get it work.

have you get your project work?

GChir.2
Associate II

@XZhen.2​ Hi.

In the archive there is a file called "readme.txt" where I tried to summarize the various steps to use my code. Let me know if everything is clear and if the code works. Sorry for my English but it's not my native language

XZhen.2
Associate II

@GChir.2​ It's kind of you share your code with me.

I tried your code but still couldn't get it work.

not sure what went wrong.

is there any different setting compare with your code?

BTW, i am using SPI1,and GPIOB,PIN6 as chip select pin

XZhen.2
Associate II

@GChir.2​ 

and this is the module that i am using0693W000008GMndQAG.jpg

That's a 5V adapter with a level shifter. At the 3.3V of the STM32 you should perhaps consider a more direct connection.

https://www.waveshare.com/micro-sd-storage-board.htm

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

ok, ordered one just now. I will try again.

thanks!

Hi @XZhen.2​ .

Tomorrow night I'll try to send you a working example with your settings so that you can test if your module works.

I have a module identical to yours and in my case it works without problems