You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "fatfs.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "fatfs_sd.h"
  26. #include "string.h"
  27. #include "stdio.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. FATFS fs;
  35. FATFS *pfs;
  36. FIL fil;
  37. FRESULT fres;
  38. DWORD fre_clust;
  39. uint32_t totalSpace, freeSpace;
  40. char buffer[100];
  41. /* USER CODE END PD */
  42. /* Private macro -------------------------------------------------------------*/
  43. /* USER CODE BEGIN PM */
  44. /* USER CODE END PM */
  45. /* Private variables ---------------------------------------------------------*/
  46. SPI_HandleTypeDef hspi1;
  47. UART_HandleTypeDef huart2;
  48. /* USER CODE BEGIN PV */
  49. /* USER CODE END PV */
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_USART2_UART_Init(void);
  54. static void MX_SPI1_Init(void);
  55. /* USER CODE BEGIN PFP */
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. // sending to UART
  60. void transmit_uart(char *string){
  61. uint8_t len = strlen(string);
  62. HAL_UART_Transmit(&huart2, (uint8_t*) string, len, 200);
  63. }
  64. /* USER CODE END 0 */
  65. /**
  66. * @brief The application entry point.
  67. * @retval int
  68. */
  69. int main(void)
  70. {
  71. /* USER CODE BEGIN 1 */
  72. /* USER CODE END 1 */
  73. /* MCU Configuration--------------------------------------------------------*/
  74. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  75. HAL_Init();
  76. /* USER CODE BEGIN Init */
  77. /* USER CODE END Init */
  78. /* Configure the system clock */
  79. SystemClock_Config();
  80. /* USER CODE BEGIN SysInit */
  81. /* USER CODE END SysInit */
  82. /* Initialize all configured peripherals */
  83. MX_GPIO_Init();
  84. MX_USART2_UART_Init();
  85. MX_SPI1_Init();
  86. MX_FATFS_Init();
  87. /* USER CODE BEGIN 2 */
  88. /* Waiting for the Micro SD module to initialize */
  89. HAL_Delay(500);
  90. fres = f_mount(&fs, "", 0);
  91. if (fres == FR_OK) {
  92. transmit_uart("SD card is mounted successfully!\r\n");
  93. } else if (fres != FR_OK) {
  94. transmit_uart("SD card is not mounted!\r\n");
  95. }
  96. // FA_OPEN_APPEND opens file if it exists and if not then creates it,
  97. // the pointer is set at the end of the file for appending
  98. fres = f_open(&fil, "log-file.txt", FA_OPEN_APPEND | FA_WRITE | FA_READ);
  99. if (fres == FR_OK) {
  100. transmit_uart("File opened.\r\n");
  101. } else if (fres != FR_OK) {
  102. transmit_uart("File was not opened!\r\n");
  103. }
  104. fres = f_getfree("", &fre_clust, &pfs);
  105. totalSpace = (uint32_t) ((pfs->n_fatent - 2) * pfs->csize * 0.5);
  106. freeSpace = (uint32_t) (fre_clust * pfs->csize * 0.5);
  107. char mSz[12];
  108. sprintf(mSz, "%lu", freeSpace);
  109. if (fres == FR_OK) {
  110. transmit_uart("Free space: \r");
  111. transmit_uart(mSz);
  112. transmit_uart("\r\n");
  113. } else if (fres != FR_OK) {
  114. transmit_uart("Free space could not be determined!\r\n");
  115. }
  116. f_puts("Example Text\n", &fil);
  117. fres = f_close(&fil);
  118. if (fres == FR_OK) {
  119. transmit_uart("File is closed.\r\n");
  120. } else if (fres != FR_OK) {
  121. transmit_uart("File was not closed.\r\n");
  122. }
  123. /* Open file to read */
  124. fres = f_open(&fil, "log-file.txt", FA_READ);
  125. if (fres == FR_OK) {
  126. transmit_uart("File opened.\r\n");
  127. } else if (fres != FR_OK) {
  128. transmit_uart("File was not opened!\r\n");
  129. }
  130. while (f_gets(buffer, sizeof(buffer), &fil)) {
  131. char mRd[100];
  132. sprintf(mRd, "%s", buffer);
  133. transmit_uart(mRd);
  134. }
  135. /* Close file */
  136. fres = f_close(&fil);
  137. if (fres == FR_OK) {
  138. transmit_uart("File is closed.\r\n");
  139. } else if (fres != FR_OK) {
  140. transmit_uart("File was not closed.\r\n");
  141. }
  142. f_mount(NULL, "", 1);
  143. if (fres == FR_OK) {
  144. transmit_uart("SD card is unmounted!\r\n");
  145. } else if (fres != FR_OK) {
  146. transmit_uart("SD card was not unmounted!\r\n");
  147. }
  148. /* USER CODE END 2 */
  149. /* Infinite loop */
  150. /* USER CODE BEGIN WHILE */
  151. while (1)
  152. {
  153. /* USER CODE END WHILE */
  154. /* USER CODE BEGIN 3 */
  155. }
  156. /* USER CODE END 3 */
  157. }
  158. /**
  159. * @brief System Clock Configuration
  160. * @retval None
  161. */
  162. void SystemClock_Config(void)
  163. {
  164. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  165. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  166. /** Configure the main internal regulator output voltage
  167. */
  168. __HAL_RCC_PWR_CLK_ENABLE();
  169. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  170. /** Initializes the RCC Oscillators according to the specified parameters
  171. * in the RCC_OscInitTypeDef structure.
  172. */
  173. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  174. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  175. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  176. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  177. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  178. RCC_OscInitStruct.PLL.PLLM = 16;
  179. RCC_OscInitStruct.PLL.PLLN = 336;
  180. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  181. RCC_OscInitStruct.PLL.PLLQ = 7;
  182. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  183. {
  184. Error_Handler();
  185. }
  186. /** Initializes the CPU, AHB and APB buses clocks
  187. */
  188. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  189. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  190. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  191. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  192. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  193. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  194. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. }
  199. /**
  200. * @brief SPI1 Initialization Function
  201. * @param None
  202. * @retval None
  203. */
  204. static void MX_SPI1_Init(void)
  205. {
  206. /* USER CODE BEGIN SPI1_Init 0 */
  207. /* USER CODE END SPI1_Init 0 */
  208. /* USER CODE BEGIN SPI1_Init 1 */
  209. /* USER CODE END SPI1_Init 1 */
  210. /* SPI1 parameter configuration*/
  211. hspi1.Instance = SPI1;
  212. hspi1.Init.Mode = SPI_MODE_MASTER;
  213. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  214. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  215. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  216. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  217. hspi1.Init.NSS = SPI_NSS_SOFT;
  218. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  219. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  220. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  221. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  222. hspi1.Init.CRCPolynomial = 10;
  223. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  224. {
  225. Error_Handler();
  226. }
  227. /* USER CODE BEGIN SPI1_Init 2 */
  228. /* USER CODE END SPI1_Init 2 */
  229. }
  230. /**
  231. * @brief USART2 Initialization Function
  232. * @param None
  233. * @retval None
  234. */
  235. static void MX_USART2_UART_Init(void)
  236. {
  237. /* USER CODE BEGIN USART2_Init 0 */
  238. /* USER CODE END USART2_Init 0 */
  239. /* USER CODE BEGIN USART2_Init 1 */
  240. /* USER CODE END USART2_Init 1 */
  241. huart2.Instance = USART2;
  242. huart2.Init.BaudRate = 115200;
  243. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  244. huart2.Init.StopBits = UART_STOPBITS_1;
  245. huart2.Init.Parity = UART_PARITY_NONE;
  246. huart2.Init.Mode = UART_MODE_TX_RX;
  247. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  248. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  249. if (HAL_UART_Init(&huart2) != HAL_OK)
  250. {
  251. Error_Handler();
  252. }
  253. /* USER CODE BEGIN USART2_Init 2 */
  254. /* USER CODE END USART2_Init 2 */
  255. }
  256. /**
  257. * @brief GPIO Initialization Function
  258. * @param None
  259. * @retval None
  260. */
  261. static void MX_GPIO_Init(void)
  262. {
  263. GPIO_InitTypeDef GPIO_InitStruct = {0};
  264. /* GPIO Ports Clock Enable */
  265. __HAL_RCC_GPIOC_CLK_ENABLE();
  266. __HAL_RCC_GPIOH_CLK_ENABLE();
  267. __HAL_RCC_GPIOA_CLK_ENABLE();
  268. __HAL_RCC_GPIOB_CLK_ENABLE();
  269. /*Configure GPIO pin Output Level */
  270. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
  271. /*Configure GPIO pin : B1_Pin */
  272. GPIO_InitStruct.Pin = B1_Pin;
  273. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  274. GPIO_InitStruct.Pull = GPIO_NOPULL;
  275. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  276. /*Configure GPIO pin : PB6 */
  277. GPIO_InitStruct.Pin = GPIO_PIN_6;
  278. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  279. GPIO_InitStruct.Pull = GPIO_NOPULL;
  280. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  281. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  282. }
  283. /* USER CODE BEGIN 4 */
  284. /* USER CODE END 4 */
  285. /**
  286. * @brief This function is executed in case of error occurrence.
  287. * @retval None
  288. */
  289. void Error_Handler(void)
  290. {
  291. /* USER CODE BEGIN Error_Handler_Debug */
  292. /* User can add his own implementation to report the HAL error return state */
  293. __disable_irq();
  294. while (1)
  295. {
  296. }
  297. /* USER CODE END Error_Handler_Debug */
  298. }
  299. #ifdef USE_FULL_ASSERT
  300. /**
  301. * @brief Reports the name of the source file and the source line number
  302. * where the assert_param error has occurred.
  303. * @param file: pointer to the source file name
  304. * @param line: assert_param error line source number
  305. * @retval None
  306. */
  307. void assert_failed(uint8_t *file, uint32_t line)
  308. {
  309. /* USER CODE BEGIN 6 */
  310. /* User can add his own implementation to report the file name and line number,
  311. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  312. /* USER CODE END 6 */
  313. }
  314. #endif /* USE_FULL_ASSERT */
  315. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/