STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
sd_fatfs.c
Go to the documentation of this file.
1 
23 /* Includes ------------------------------------------------------------------*/
24 #include "sd_fatfs.h"
25 
26 #include "log.h"
27 #include "sd_diskio.h"
28 #include "sdmmc.h"
29 
30 /* Private variables ---------------------------------------------------------*/
31 
33 static char sdPath[4];
34 
35 /* Functions -----------------------------------------------------------------*/
36 bool SdCardMount(void)
37 {
38  static FATFS sdFileSystem;
39 
40  // Message: A function should have a single point of exit at the end
41  // [misra-c2012-15.5]
42  // Reason: Multiple and early returns are used for avoiding a complex
43  // implementation of checking the results of the intermediate
44  // operations. It also allows better code readability.
45  // Risk: A single point of exit is required by IEC 61508 and ISO
46  // 26262 as part of the requirements for a modular approach.
47  // Early returns may lead to the unintentional omission of
48  // function termination code. If a function has exit points
49  // interspersed with statements that produce persistent side
50  // effects, it is not easy to determine which side effects will
51  // occur when the function is executed.
52  // Prevention: Code reviews and testing.
53  // cppcheck-suppress-begin [misra-c2012-15.5]
54 
55  // Initialize the SD card
56  if (!SdmmcInit())
57  {
58  LogPrint("SD card cannot be initialized.\n");
59  return false;
60  }
61 
62  // Initialize the FatFs driver
63  if (FATFS_LinkDriver(SdDiskIoGetDriver(), sdPath) != 0U)
64  {
65  LogPrint("FatFs cannot be initialized.\n");
66  return false;
67  }
68 
69  /* Mount the SD card */
70  if (FR_OK != f_mount(&sdFileSystem, (TCHAR const*)sdPath, 1))
71  {
72  LogPrint("SD card cannot be mounted.\n");
73  return false;
74  }
75 
76  return true;
77  // cppcheck-suppress-end [misra-c2012-15.5]
78 }
79 
80 void SdCardUnMount(void)
81 {
82  (void)f_mount(NULL, (TCHAR const*)sdPath, 0);
83  (void)FATFS_UnLinkDriver(sdPath);
84  SdmmcDeInit();
85 }
86 
const Diskio_drvTypeDef * SdDiskIoGetDriver(void)
Get a pointer to the SD card disk I/O driver structure containing the initialize, status,...
Definition: sd_diskio.c:103
static char sdPath[4]
Definition: sd_fatfs.c:33
bool SdCardMount(void)
Mount the FatFs file system on the SD card by linking the disk I/O driver and mounting the default lo...
Definition: sd_fatfs.c:36
void SdCardUnMount(void)
Unmount the FatFs file system from the SD card and unlink the disk I/O driver.
Definition: sd_fatfs.c:80
void LogPrint(const char *format,...)
Print a formatted log message over the UART interface.
Definition: log.c:40
bool SdmmcInit(void)
Initialize the SDMMC peripheral and the SD card.
Definition: sdmmc.c:62
void SdmmcDeInit(void)
De-initialize the SDMMC peripheral.
Definition: sdmmc.c:115
This file contains the UART-based logging function prototypes for the debug output.
This file contains the SD card disk I/O driver interface for the FatFs middleware.
This file contains the SD card FatFs file system mount and unmount function prototypes.
This file contains the SDMMC peripheral driver function prototypes for SD card initialization,...