Advertisement
Guest User

Untitled

a guest
Feb 10th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "fatfs.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35.  
  36. /* USER CODE END PD */
  37.  
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40.  
  41. /* USER CODE END PM */
  42.  
  43. /* Private variables ---------------------------------------------------------*/
  44. SD_HandleTypeDef hsd;
  45. DMA_HandleTypeDef hdma_sdio_rx;
  46. DMA_HandleTypeDef hdma_sdio_tx;
  47.  
  48. /* USER CODE BEGIN PV */
  49. FATFS SDFatFs; /* File system object for SD card logical drive */
  50. FIL MyFile; /* File object */
  51. extern char SDPath[4]; /* SD card logical drive path */
  52. static uint8_t buffer[_MAX_SS]; /* a work buffer for the f_mkfs() */
  53. /* USER CODE END PV */
  54.  
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. static void MX_GPIO_Init(void);
  58. static void MX_DMA_Init(void);
  59. static void MX_SDIO_SD_Init(void);
  60. /* USER CODE BEGIN PFP */
  61.  
  62. /* USER CODE END PFP */
  63.  
  64. /* Private user code ---------------------------------------------------------*/
  65. /* USER CODE BEGIN 0 */
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70. * @brief The application entry point.
  71. * @retval int
  72. */
  73. int main(void)
  74. {
  75. /* USER CODE BEGIN 1 */
  76. FRESULT res; /* FatFs function common result code */
  77. uint32_t byteswritten, bytesread; /* File write/read counts */
  78. uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */
  79. uint8_t rtext[100]; /* File read buffer */
  80. /* USER CODE END 1 */
  81.  
  82. /* MCU Configuration--------------------------------------------------------*/
  83.  
  84. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  85. HAL_Init();
  86.  
  87. /* USER CODE BEGIN Init */
  88.  
  89. /* USER CODE END Init */
  90.  
  91. /* Configure the system clock */
  92. SystemClock_Config();
  93.  
  94. /* USER CODE BEGIN SysInit */
  95.  
  96. /* USER CODE END SysInit */
  97.  
  98. /* Initialize all configured peripherals */
  99. MX_GPIO_Init();
  100. MX_DMA_Init();
  101. MX_SDIO_SD_Init();
  102. MX_FATFS_Init();
  103. /* USER CODE BEGIN 2 */
  104.  
  105. if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0)
  106. {
  107. /*##-2- Register the file system object to the FatFs module ##############*/
  108. if(f_mount(&SDFatFs, (TCHAR const*)SDPath, 1) != FR_OK)
  109. {
  110. /* FatFs Initialization Error */
  111. Error_Handler();
  112. }
  113. else
  114. {
  115. /*##-3- Create a FAT file system (format) on the logical drive #########*/
  116. /* WARNING: Formatting the uSD card will delete all content on the device */
  117. //if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, buffer, sizeof(buffer)) != FR_OK)
  118. if(0)
  119. {
  120. /* FatFs Format Error */
  121. Error_Handler();
  122. }
  123. else
  124. {
  125. /*##-4- Create and Open a new text file object with write access #####*/
  126. if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
  127. {
  128. /* 'STM32.TXT' file Open for write Error */
  129. Error_Handler();
  130. }
  131. else
  132. {
  133. /*##-5- Write data to the text file ################################*/
  134. res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
  135.  
  136. if((byteswritten == 0) || (res != FR_OK))
  137. {
  138. /* 'STM32.TXT' file Write or EOF Error */
  139. Error_Handler();
  140. }
  141. else
  142. {
  143. /*##-6- Close the open text file #################################*/
  144. f_close(&MyFile);
  145.  
  146. /*##-7- Open the text file object with read access ###############*/
  147. if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
  148. {
  149. /* 'STM32.TXT' file Open for read Error */
  150. Error_Handler();
  151. }
  152. else
  153. {
  154. /*##-8- Read data from the text file ###########################*/
  155. res = f_read(&MyFile, rtext, sizeof(rtext), (UINT*)&bytesread);
  156.  
  157. if((bytesread == 0) || (res != FR_OK))
  158. {
  159. /* 'STM32.TXT' file Read or EOF Error */
  160. Error_Handler();
  161. }
  162. else
  163. {
  164. /*##-9- Close the open text file #############################*/
  165. f_close(&MyFile);
  166.  
  167. /*##-10- Compare read data with the expected data ############*/
  168. if((bytesread != byteswritten))
  169. {
  170. /* Read data is different from the expected data */
  171. Error_Handler();
  172. }
  173. else
  174. {
  175. /* Success of the demo: no error occurrence */
  176. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185.  
  186. /* USER CODE END 2 */
  187.  
  188. /* Infinite loop */
  189. /* USER CODE BEGIN WHILE */
  190.  
  191. //HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
  192.  
  193. while (1)
  194. {
  195. /* USER CODE END WHILE */
  196.  
  197. /* USER CODE BEGIN 3 */
  198. }
  199. /* USER CODE END 3 */
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement