STM32CubeMX

Table Of Contents

- STM32CubeMX documentation
- CubeMX User Manual
  - 1 STM32Cube overview
  - 2 Getting started with STM32CubeMX
  - 3 Installing and running STM32CubeMX
  - 4 STM32CubeMX user interface
  - 5 STM32CubeMX tools
  - 6 STM32CubeMX C Code generation overview
  - 7 Code generation for dual-core MCUs (STM32H7 dual-core product lines only)
  - 8 Code generation with TrustZone enabled (STM32L5 series only)
  - 9 Device tree generation (STM32MPUs only)
  - 10 Support of additional software components using CMSIS-Pack standard
  - 11 Tutorial 1: From pinout to project C code generation using an MCU of the STM32F4 series
  - 12 Tutorial 2 - Example of FatFs on an SD card using STM32429I-EVAL evaluation board
  - 13 Tutorial 3 - Using the Power Consumption Calculator to optimize the embedded application consumption and more
  - 14 Tutorial 4 - Example of UART communications with an STM32L053xx Nucleo board
  - 15 Tutorial 5: Exporting current project configuration to a compatible MCU
  - 16 Tutorial 6 - Adding embedded software packs to user projects
  - 17 Tutorial 7 - Using the X-Cube-BLE1 software pack
  - 18 Creating LPBAM projects
  - Appendix A STM32CubeMX pin assignment rules
  - Appendix B STM32CubeMX C code generation design choices and limitations
  - Appendix C STM32 microcontrollers power consumption parameters
  - Appendix D STM32Cube embedded software packages
  - Revision history
- Release Notes
  - Release details
  - Release fixes
  - Known limitations
    - Miscellaneous
    - 2.1. When selecting microcontrollers in the STM32C0 series
    - 2.2. When selecting microcontrollers in the STM32G0 series
    - 2.3. When selecting microcontrollers in the STM32G4 series
    - 2.4. When selecting microcontrollers in the STM32H5 series
    - 2.5. When selecting microcontrollers in the STM32H7 series
    - 2.6. When selecting dual-core microcontrollers in the STM32H7 series
    - 2.7. When selecting microcontrollers in the STM32L5 series
    - 3.4. How to improve the wake-up from Stop sequences without relying on the global SystemClock_Config()
    - 3.3. List pinout-compatible microcontrollers
    - 3.2. DDR binaries for the STM32MP1 boards
    - 3.1. Command line authentication required
    - 2.21. Symbolic links on Ubuntu
    - 2.20. User manual
    - 2.19. DDR test suite
    - 2.18. STM32CubeIDE toolchain
    - 2.13. When selecting microcontrollers in the STM32WL series
    - 2.11. When selecting microcontrollers in the STM32WB series
    - 2.17. When using additional software packs
    - 2.16. When using the Example Selector
    - 2.15. When using the Memory Management Tool
    - 2.14. When selecting microprocessors in the STM32MP1 series
    - 2.11.1. When selecting microcontrollers in the STM32WB0 product lines
    - 2.12. When selecting microcontrollers in the STM32WBA series
    - 2.13.1. When selecting microcontrollers in the STM32WL33 product lines
    - 2.10. When selecting microcontrollers in the STM32U5 series
    - 2.9. When selecting microcontrollers in the STM32U3 series
    - 2.8. When selecting microcontrollers in the STM32N6 series
- Running STM32CubeMX in command-line mode
- FAQ
- Community

### On this page

- STM32429I-EVAL evaluation board
  - Figure 642. Board peripheral initialization dialog box

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

> Figure 641. Import Project menu

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

> Figure 643. Board selection

7. 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.

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

9. Under the Middlewares category, check SD card as FatFs mode (see Figure 645).

> Figure 645. FatFs mode configuration

> From the Pinout view on the right, enable, as GPIO input, a pin to be used for the SDIO detection.

> In the configuration panel below the mode panel, go to the platform settings tab and configure the SD_detection using the pin previously enabled.

> Finally, go to FatFs “Advanced settings tab” and enable “Use DMA template”.

10. Configure the clocks as follows:

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

> Figure 646. RCC peripheral configuration

2. Configure the clock tree from the clock tab (see Figure 647).

> Figure 647. Clock tree view

11. 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

13. 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

14. 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).

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.

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

16. 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.

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

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

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

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

> 17. 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 */

2. 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();

> 3. 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 #########*/