12 Tutorial 2 - Example of FatFs on an SD card using STM32429I-EVAL evaluation board

Figure 641. Import Project menu

../../../../_images/image815.webp

12 Tutorial 2 - Example of FatFs on an SD card using

STM32429I-EVAL evaluation board

The tutorial consists in creating and writing to a file on the STM32429I-EVAL1 SD card using the FatFs file system middleware.

To generate a project and run tutorial 2, follow the sequence below:

  1. Launch STM32CubeMX.

  2. Select File > New Project. The Project window opens.

  3. Click the Board Selector Tab to display the list of ST boards.

  4. Select EvalBoard as type of Board and STM32F4 as Series to filter down the list.

  5. Answer Yes to Initialize all peripherals with their default mode so that the code is generated only for the peripherals used by the application.

  6. Select the STM32429I-EVAL board and click OK. Answer No in the dialog box asking to initialize all peripherals to their default modes (see Figure 642). The Pinout view is loaded, matching the MCU pinout configuration on the evaluation board (see Figure 643).

Figure 642. Board peripheral initialization dialog box

../../../../_images/image817.webp
  1. From the Peripheral tree on the left, expand the SDIO peripheral and select “SD 4 bits wide bus” (see Figure 644). In the configuration panel, from the DMA settings tab, add SDIO_RX and SDIO_TX DMA requests.

  2. Finally, go pack to the peripheral tree panel, select NVIC and enable the SDIO global interrupt from the configuration panel.

../../../../_images/image1248.webp
  1. Under the Middlewares category, check SD card as FatFs mode (see Figure 645).

Figure 645. FatFs mode configuration

  1. Configure the clocks as follows:

    1. Select the RCC peripheral from the Pinout view (see Figure 646).

Figure 646. RCC peripheral configuration

../../../../_images/image820.webp
  1. Configure the clock tree from the clock tab (see Figure 647).

Figure 647. Clock tree view

../../../../_images/image821.webp
  1. In the Project tab, specify the project name and destination folder. Then, select the EWARM IDE toolchain.

Note that project heap and stack size can be adjusted to the minimum required for the FATFS application.

Figure 648. FATFS tutorial - Project settings

12. Click Ok. Then, on the toolbar menu, click

../../../../_images/image1253.webp
  1. Upon code generation completion, click Open Project in the Code Generation dialog window (see Figure 649). This opens the project directly in the IDE.

Figure 649. C code generation completion message

../../../../_images/image823.webp
  1. In the IDE, check that heap and stack sizes are sufficient: right click the project name and select Options, then select Linker. Check Override default to use the icf file from STM32CubeMX generated project folder. if not already done through STM32CubeMX User interface (under Linker Settings from Project Manager’s project tab), adjust the heap and stack sizes (see Figure 650).

../../../../_images/image1257.webp

Note: When using the MDK-Arm toolchain, go to the Application/MDK-ARM folder and double-click the startup_xx.s file to edit and adjust the heap and stack sizes there.

  1. Go to the Application/User folder. Double-click the main.c file and edit it.

  2. The tutorial consists in creating and writing to a file on the evaluation board SD card using the FatFs file system middleware: a) At startup all LEDs are OFF.

  1. The red LED is turned ON to indicate that an error occurred (e.g. FatFs initialization, file read/write access errors).

  2. The orange LED is turned ON to indicate that the FatFs link has been successfully mounted on the SD driver.

  3. The blue LED is turned ON to indicate that the file has been successfully written to the SD card.

  4. The green LED is turned ON to indicate that the file has been successfully read from file the SD card.

  1. For use case implementation, update main.c with the following code:

  1. Insert main.c private variables in a dedicated user code section:

/* USER CODE BEGIN PV */

/* Private variables ——————————————*/

FATFS SDFatFs; /* File system object for SD card logical drive */ FIL MyFile; /* File object */ const char wtext[] = “Hello World!”; static uint8_t buffer[_MAX_SS]; /* a work buffer for the f_mkfs() */

/* USER CODE END PV */

  1. Insert main functional local variables:

int main(void) {

/* USER CODE BEGIN 1 */

FRESULT res; /* FatFs function common result code */ uint32_t byteswritten, bytesread; /* File write/read counts */ char rtext[256]; /* File read buffer */

/* USER CODE END 1 */

/* MCU Configuration—————————————-*/

/* Reset of all peripherals, Initializes the Flash interface and the

Systick. */

HAL_Init();

  1. Insert user code in the main function, after initialization calls and before the while loop, to perform actual read/write from/to the SD card:

int main(void)

{

….

MX_FATFS_Init();

/* USER CODE BEGIN 2 */

/*##-0- Turn all LEDs off(red, green, orange and blue) */

HAL_GPIO_WritePin(GPIOG, (GPIO_PIN_10 | GPIO_PIN_6 | GPIO_PIN_7 |

GPIO_PIN_12), GPIO_PIN_SET);

/*##-1- FatFS: Link the SD disk I/O driver ##########*/

if(retSD == 0){

/* success: set the orange LED on */

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET); /*##-2- Register the file system object to the FatFs module ###*/ if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 0) != FR_OK){ /* FatFs Initialization Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/*##-3- Create a FAT file system (format) on the logical drive#*/ /* WARNING: Formatting the uSD card will delete all content on the device */ if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer))

!= FR_OK){

/* FatFs Format Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/*##-4- Create & Open a new text file object with write access#*/ if(f_open(&MyFile, “Hello.txt”, FA_CREATE_ALWAYS | FA_WRITE) !=

FR_OK){

/* ‘Hello.txt’ file Open for write Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/*##-5- Write data to the text file ####################*/ res = f_write(&MyFile, wtext, sizeof(wtext), (void

*)&byteswritten); if((byteswritten == 0) || (res != FR_OK)){

/* ‘Hello.txt’ file Write or EOF Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/*##-6- Successful open/write : set the blue LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); f_close(&MyFile);

/*##-7- Open the text file object with read access #*/ if(f_open(&MyFile, “Hello.txt”, FA_READ) != FR_OK){

/* ‘Hello.txt’ file Open for read Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/*##-8- Read data from the text file #########*/ res = f_read(&MyFile, rtext, sizeof(wtext), &bytesread); if((byteswritten == 0)|| (res != FR_OK)){

/* ‘Hello.txt’ file Read or EOF Error : set the red LED on */ HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET); while(1);

} else {

/* Successful read : set the green LED On */

HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET);

/*##-9- Close the open text file ################*/ f_close(&MyFile);

/*##-10- Unlink the micro SD disk I/O driver #########*/