2017-02-20 01:57 PM
Everyone,
How can I use cubeMX for SDcard ? I have generated for MDK but :
this function doesn't work , from UM1721
User manualDeveloping 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
2017-02-28 04:25 PM
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 Likes2017-03-01 02:46 AM
I reduce the SDIO clock, and it can write properly already, thanks a lot guys
2017-03-01 03:32 AM
Mao,
Ok, thanks fore your suggestion and test code, I'll keep in post, learn and share what I have.
2017-03-01 03:37 AM
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 ?
2017-03-01 04:25 AM
Where can I fidn SDIO_CK variable in the code ?
2017-03-01 01:14 PM
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)
2017-04-26 03:27 PM
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.
2017-04-26 04:43 PM
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.
2017-04-26 05:46 PM
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.
2017-04-26 06:27 PM
I'm not using level shifter, 4K7 pull up resistor on each SDIO and
control pins