cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeMX, FatFS and SDIO?

antonius
Senior
Posted on February 20, 2017 at 22:57

Everyone,

How can I use cubeMX for SDcard ? I have generated for MDK but :

this function doesn't work , from UM1721

User manual

Developing Applications on STM32Cube with FatFs page 21.

:

uint32_t wbytes; /* File write counts */

uint8_t wtext[] = 'text to write logical disk'; /* File write buffer */

if(FATFS_LinkDriver(&mynewdisk_Driver, mynewdiskPath) == 0)

{

if(f_mount(&mynewdiskFatFs, (TCHAR const*)mynewdiskPath, 0) == FR_OK)

{

if(f_open(&MyFile, 'STM32.TXT', FA_CREATE_ALWAYS | FA_WRITE) == FR_OK)

{

if(f_write(&MyFile, wtext, sizeof(wtext), (void *)&wbytes) == FR_OK);

{

f_close(&MyFile);

}

}

}

}

FATFS_UnLinkDriver(mynewdiskPath);

This variable :

&mynewdisk_Driver doesn't exist, which file is related ?

Thank you

54 REPLIES 54
rwmao
Senior
Posted on March 01, 2017 at 01:25

h.Rick

Clive is right! please follow his advise.

You have to start from the low level sdio testing. Forget about whatever ff, fmount, fopen etc.

YOu can try to use this to test.

int main(void)
{
 char usbbuffer[500];
 uint8_t SD_state = MSD_OK;
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
/* Configure the system clock */
 SystemClock_Config();
/* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_SDIO_SD_Init();

/* USER CODE BEGIN 2 */

 /* USER CODE BEGIN 2 */
SD_Test_LowLevel(); 
while(1){;}
 }

SD_Test_LowLevel() is defined as:

the function sendtextmsgtousb is for debugging only. you don't need that.

Error_Handler() is showing error by LED and halt system.

void SD_Test_LowLevel(void)
{ 
 uint8_t SD_state = MSD_OK;
 static uint8_t prev_status = 0; 
 char usbbuffer[200];
SD_state = BSP_SD_Init();
 
 if(SD_state != MSD_OK)
 {
 sprintf(usbbuffer,'
---initialize failed ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 Error_Handler(); 
 }
//initialization is done. Now go ahead for SD operation in low level
 
 
 SD_state = BSP_SD_Erase(BLOCK_START_ADDR, (BLOCKSIZE * NUM_OF_BLOCKS));
 
 if(SD_state != MSD_OK)
 {
 sprintf(usbbuffer,'
---erase failed ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 Error_Handler();
 }
 
 /* Fill the buffer to write */
 Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF);
 SD_state = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS);
 
 if(SD_state != MSD_OK)
 {
 sprintf(usbbuffer,'
---writing failed ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 Error_Handler();
 }
SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS);
 if(SD_state != MSD_OK)
 {
 sprintf(usbbuffer,'
---reading failed ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 Error_Handler();
 }
 else
 {
 sprintf(usbbuffer,'
---Reading OK ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0)
 {
 sprintf(usbbuffer,'
---reading/writing compare failed ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 Error_Handler(); 
 }
 else
 {
 sprintf(usbbuffer,'
---Testing is done, OK ---
');//export it to char buffer first. 
 SendTextMsgToUSB(usbbuffer); //send the txt back 
 }
 }
 
 
 
 
 GREEN_LED_ON;//Good
 
 
 while (1)
 {
}
}

Helpful

https://community.st.com/message/149707?commentID=149707

https://community.st.com/message/149707?commentID=149707

Like

Show 0 Likes

https://community.st.com/message/149707?commentID=149707

antonius
Senior
Posted on March 01, 2017 at 11:46

I reduce the SDIO clock, and it can write properly already, thanks a lot guys

Posted on March 01, 2017 at 11:32

Mao,

Ok, thanks fore your suggestion and test code, I'll keep in post, learn and share what I have.

Posted on March 01, 2017 at 11:37

Can it be because my Init SDIO clock too fast ? Why can't I read this message properly ? Always got unexpected error ? server problem here ?

Posted on March 01, 2017 at 12:25

Where  can I fidn SDIO_CK variable in the code ?

antonius
Senior
Posted on March 01, 2017 at 22:14

I changed this configuration in this file : stm32f1xx_II_sdmmc.h

And now it's working properly

/* SDIO Intialization Frequency (400KHz max) */

#define SDIO_INIT_CLK_DIV ((uint8_t)0xC3)

/* SDIO Data Transfer Frequency */

#define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x3)

Posted on April 26, 2017 at 22:27

Hi h.rick,

Could you provide your setting on each tab on CubeMX, including the 'FATFS' and 'SDIO' windows under the 'Configuration' tab?

Did you use these setting for allocated memory: CSTACK: 0x800 & HEAP: 0x400?

Did you connect an SD breakout board with 4-pin SDIO mode directly to your STM32F103 board, or do you need level shifters in between?

I'm trying to do the same thing with my Nucleo-STM32F401RE board with connected SD breakout board. That eval board has no external crystal, so my clock setting have to be from the internal clock.

Thanks.

Seton Schroeder
Associate II
Posted on April 27, 2017 at 01:43

Thanks. After posting, I actually re-read the Nucleo user manual realized, yes, by default it connects to the STLINK 8MHz external crystal. So I made the changes and when I build I still get these issues:

Warnings:

- implicit declaration of function 'strcmp'

- passing arguments 4 of 'f_read' from incompatible pointer type

Infos:

- expected 'UINT *{aka unsigned int *}' but argument is of type 'unint32_t *{aka long unsigned int *}'

Still, I read nothing to the SD card using the Example you pointed out in the STM32CubeMX manual. 

I'll try adding the pull-up resistors and try again.

Posted on April 27, 2017 at 00:46

You don't need level converters, I would however recommend very short connections and pull-up resistors.

The Nucleo has an 8 MHz HSE clock provided by the ST-LINK.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 27, 2017 at 01:27

I'm not using level shifter, 4K7 pull up resistor on each SDIO and

control pins