Skip to main content
GChir.2
Associate
July 16, 2020
Question

NUCLEO-L476RG: Write ADC values into SD_CARD using SPI

  • July 16, 2020
  • 16 replies
  • 3581 views

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

This topic has been closed for replies.

16 replies

TDK
July 16, 2020

> 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
GChir.2Author
Associate
July 16, 2020

@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
February 16, 2021

@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
GChir.2Author
Associate
February 16, 2021

@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
February 17, 2021

@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

GChir.2
GChir.2Author
Associate
February 17, 2021

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

XZhen.2
Associate II
February 17, 2021

@GChir.2​ 

and this is the module that i am using0693W000008GMndQAG.jpg

Tesla DeLorean
Guru
February 17, 2021

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
XZhen.2
Associate II
February 17, 2021

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

thanks!

GChir.2
GChir.2Author
Associate
February 17, 2021

@XZhen.2​ Try this simple code (PB6 as cs pin and SP1 enabled).

Make a backup of your sd because, in the code, I inserted the command for formatting the sd card. On my board, with an sd module similar to your, the code works without problems (connect the vcc pin of the sd module to 5V on the board).

Let me know if this solves your problems ;)

XZhen.2
Associate II
February 17, 2021

@GChir.2​ 

Hi. thanks for the code.

still failed.

this is the info I got from serial comm.

ERROR in mounting SD CARD...

Total Drive Space: 2147481600 KiB (2047.997 GiB)

Space Available:      0 KiB (0.000 GiB)

ERROR!!! No. 1 in creating file *Test.txt*

ERROR!!! Test.txt does not exists

there might be something wrong with my Nucleo-L476RG board

GChir.2
GChir.2Author
Associate
February 17, 2021

Could you share a photo with module's connection to the board?

Have you used my code?