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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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) 2020 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. uint16_t AD_RES;
  42. int num;
  43. //Variable name for txt.file
  44. char txtVar[19];
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. ADC_HandleTypeDef hadc;
  51. SPI_HandleTypeDef hspi1;
  52. UART_HandleTypeDef huart2;
  53. /* USER CODE BEGIN PV */
  54. /* USER CODE END PV */
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. static void MX_GPIO_Init(void);
  58. static void MX_USART2_UART_Init(void);
  59. static void MX_SPI1_Init(void);
  60. static void MX_ADC_Init(void);
  61. /* USER CODE BEGIN PFP */
  62. /* USER CODE END PFP */
  63. /* Private user code ---------------------------------------------------------*/
  64. /* USER CODE BEGIN 0 */
  65. // sending to UART
  66. void transmit_uart(char *string){
  67. uint8_t len = strlen(string);
  68. HAL_UART_Transmit(&huart2, (uint8_t*) string, len, 200);
  69. }
  70. /*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  71. {
  72. // Read & Update The ADC Result
  73. AD_RES = HAL_ADC_GetValue(hadc);
  74. }*/
  75. /* USER CODE END 0 */
  76. /**
  77. * @brief The application entry point.
  78. * @retval int
  79. */
  80. int main(void)
  81. {
  82. /* USER CODE BEGIN 1 */
  83. /* USER CODE END 1 */
  84. /* MCU Configuration--------------------------------------------------------*/
  85. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  86. HAL_Init();
  87. /* USER CODE BEGIN Init */
  88. /* USER CODE END Init */
  89. /* Configure the system clock */
  90. SystemClock_Config();
  91. /* USER CODE BEGIN SysInit */
  92. /* USER CODE END SysInit */
  93. /* Initialize all configured peripherals */
  94. MX_GPIO_Init();
  95. MX_USART2_UART_Init();
  96. MX_SPI1_Init();
  97. MX_FATFS_Init();
  98. MX_ADC_Init();
  99. /* USER CODE BEGIN 2 */
  100. fres = f_mount(&fs, "", 0);
  101. if (fres == FR_OK) {
  102. transmit_uart("SD card is mounted successfully!\r\n");
  103. } else if (fres != FR_OK) {
  104. transmit_uart("SD card is not mounted!\r\n");
  105. }
  106. /*// FA_OPEN_APPEND opens file if it exists and if not then creates it,
  107. // the pointer is set at the end of the file for appending
  108. fres = f_open(&fil, "log-file_test.txt", FA_OPEN_APPEND | FA_WRITE | FA_READ);
  109. if (fres == FR_OK) {
  110. transmit_uart("File opened.\r\n");
  111. } else if (fres != FR_OK) {
  112. transmit_uart("File was not opened!\r\n");
  113. }*/
  114. /*fres = f_getfree("", &fre_clust, &pfs);
  115. totalSpace = (uint32_t) ((pfs->n_fatent - 2) * pfs->csize * 0.5);
  116. freeSpace = (uint32_t) (fre_clust * pfs->csize * 0.5);
  117. char mSz[12];
  118. sprintf(mSz, "%lu", freeSpace);
  119. if (fres == FR_OK) {
  120. transmit_uart("Free space: \r");
  121. transmit_uart(mSz);
  122. transmit_uart("\r\n");
  123. } else if (fres != FR_OK) {
  124. transmit_uart("Free space could not be determined!\r\n");
  125. }
  126. */
  127. /* Open file to read */
  128. /*fres = f_open(&fil, "log-file.txt", FA_READ);
  129. if (fres == FR_OK) {
  130. transmit_uart("File opened.\r\n");
  131. } else if (fres != FR_OK) {
  132. transmit_uart("File was not opened!\r\n");
  133. }
  134. while (f_gets(buffer, sizeof(buffer), &fil)) {
  135. char mRd[100];
  136. sprintf(mRd, "%s", buffer);
  137. transmit_uart(mRd);
  138. }*/
  139. /* Close file */
  140. /*fres = f_close(&fil);
  141. if (fres == FR_OK) {
  142. transmit_uart("File is closed.\r\n");
  143. } else if (fres != FR_OK) {
  144. transmit_uart("File was not closed.\r\n");
  145. }
  146. f_mount(NULL, "", 1);
  147. if (fres == FR_OK) {
  148. transmit_uart("SD card is unmounted!\r\n");
  149. } else if (fres != FR_OK) {
  150. transmit_uart("SD card was not unmounted!\r\n");
  151. }*/
  152. //create textfile Name (day.month.year_Values.txt) and saves to txtVar
  153. int day = 12;
  154. int month = 2;
  155. int year = 21;
  156. char str_month[3];
  157. char str_year[3];
  158. //int to char array
  159. sprintf(txtVar, "%ld", day);
  160. sprintf(str_month, "%ld", month);
  161. sprintf(str_year, "%ld", year);
  162. //Concatenate strings
  163. strcat(txtVar, ".");
  164. strcat(str_month, ".");
  165. strcat(str_year, "_");
  166. strcat(txtVar, str_month);
  167. strcat(txtVar, str_year);
  168. strcat(txtVar, "Values.txt");
  169. /* USER CODE END 2 */
  170. /* Infinite loop */
  171. /* USER CODE BEGIN WHILE */
  172. while (1)
  173. {
  174. /* USER CODE END WHILE */
  175. HAL_Delay(5000);
  176. // Start ADC Conversion
  177. HAL_ADC_Start(&hadc);
  178. // Poll ADC1 Perihperal & TimeOut = 1mSec
  179. HAL_ADC_PollForConversion(&hadc, 1);
  180. // Read The ADC Conversion Result
  181. AD_RES = HAL_ADC_GetValue(&hadc);
  182. num++;
  183. // FA_OPEN_APPEND opens file (txtVar) if it exists and if not then creates it,
  184. // the pointer is set at the end of the file for appending
  185. fres = f_open(&fil, txtVar, FA_OPEN_APPEND | FA_WRITE | FA_READ);
  186. if (fres == FR_OK) {
  187. transmit_uart("File opened.\r\n");
  188. } else if (fres != FR_OK) {
  189. transmit_uart("File was not opened!\r\n");
  190. }
  191. f_puts("ADC_value_", &fil);
  192. f_printf(&fil, "%d", num);
  193. f_puts(" = ", &fil);
  194. f_printf(&fil, "%d\n", AD_RES);
  195. /* Close file */
  196. fres = f_close(&fil);
  197. if (fres == FR_OK) {
  198. transmit_uart("File is closed.\r\n");
  199. } else if (fres != FR_OK) {
  200. transmit_uart("File was not closed.\r\n");
  201. }
  202. /* USER CODE BEGIN 3 */
  203. }
  204. /* USER CODE END 3 */
  205. }
  206. /**
  207. * @brief System Clock Configuration
  208. * @retval None
  209. */
  210. void SystemClock_Config(void)
  211. {
  212. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  213. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  214. /** Configure the main internal regulator output voltage
  215. */
  216. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  217. /** Initializes the RCC Oscillators according to the specified parameters
  218. * in the RCC_OscInitTypeDef structure.
  219. */
  220. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  221. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  222. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  223. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  224. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  225. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  226. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  227. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. /** Initializes the CPU, AHB and APB buses clocks
  232. */
  233. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  234. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  235. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  236. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  237. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  238. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  239. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  240. {
  241. Error_Handler();
  242. }
  243. }
  244. /**
  245. * @brief ADC Initialization Function
  246. * @param None
  247. * @retval None
  248. */
  249. static void MX_ADC_Init(void)
  250. {
  251. /* USER CODE BEGIN ADC_Init 0 */
  252. /* USER CODE END ADC_Init 0 */
  253. ADC_ChannelConfTypeDef sConfig = {0};
  254. /* USER CODE BEGIN ADC_Init 1 */
  255. /* USER CODE END ADC_Init 1 */
  256. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  257. */
  258. hadc.Instance = ADC1;
  259. hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  260. hadc.Init.Resolution = ADC_RESOLUTION_12B;
  261. hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  262. hadc.Init.ScanConvMode = ADC_SCAN_DISABLE;
  263. hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  264. hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
  265. hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  266. hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  267. hadc.Init.ContinuousConvMode = DISABLE;
  268. hadc.Init.NbrOfConversion = 1;
  269. hadc.Init.DiscontinuousConvMode = DISABLE;
  270. hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  271. hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  272. hadc.Init.DMAContinuousRequests = DISABLE;
  273. if (HAL_ADC_Init(&hadc) != HAL_OK)
  274. {
  275. Error_Handler();
  276. }
  277. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  278. */
  279. sConfig.Channel = ADC_CHANNEL_0;
  280. sConfig.Rank = ADC_REGULAR_RANK_1;
  281. sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
  282. if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  283. {
  284. Error_Handler();
  285. }
  286. /* USER CODE BEGIN ADC_Init 2 */
  287. /* USER CODE END ADC_Init 2 */
  288. }
  289. /**
  290. * @brief SPI1 Initialization Function
  291. * @param None
  292. * @retval None
  293. */
  294. static void MX_SPI1_Init(void)
  295. {
  296. /* USER CODE BEGIN SPI1_Init 0 */
  297. /* USER CODE END SPI1_Init 0 */
  298. /* USER CODE BEGIN SPI1_Init 1 */
  299. /* USER CODE END SPI1_Init 1 */
  300. /* SPI1 parameter configuration*/
  301. hspi1.Instance = SPI1;
  302. hspi1.Init.Mode = SPI_MODE_MASTER;
  303. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  304. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  305. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  306. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  307. hspi1.Init.NSS = SPI_NSS_SOFT;
  308. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  309. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  310. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  311. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  312. hspi1.Init.CRCPolynomial = 10;
  313. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  314. {
  315. Error_Handler();
  316. }
  317. /* USER CODE BEGIN SPI1_Init 2 */
  318. /* USER CODE END SPI1_Init 2 */
  319. }
  320. /**
  321. * @brief USART2 Initialization Function
  322. * @param None
  323. * @retval None
  324. */
  325. static void MX_USART2_UART_Init(void)
  326. {
  327. /* USER CODE BEGIN USART2_Init 0 */
  328. /* USER CODE END USART2_Init 0 */
  329. /* USER CODE BEGIN USART2_Init 1 */
  330. /* USER CODE END USART2_Init 1 */
  331. huart2.Instance = USART2;
  332. huart2.Init.BaudRate = 115200;
  333. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  334. huart2.Init.StopBits = UART_STOPBITS_1;
  335. huart2.Init.Parity = UART_PARITY_NONE;
  336. huart2.Init.Mode = UART_MODE_TX_RX;
  337. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  338. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  339. if (HAL_UART_Init(&huart2) != HAL_OK)
  340. {
  341. Error_Handler();
  342. }
  343. /* USER CODE BEGIN USART2_Init 2 */
  344. /* USER CODE END USART2_Init 2 */
  345. }
  346. /**
  347. * @brief GPIO Initialization Function
  348. * @param None
  349. * @retval None
  350. */
  351. static void MX_GPIO_Init(void)
  352. {
  353. GPIO_InitTypeDef GPIO_InitStruct = {0};
  354. /* GPIO Ports Clock Enable */
  355. __HAL_RCC_GPIOC_CLK_ENABLE();
  356. __HAL_RCC_GPIOH_CLK_ENABLE();
  357. __HAL_RCC_GPIOA_CLK_ENABLE();
  358. __HAL_RCC_GPIOB_CLK_ENABLE();
  359. /*Configure GPIO pin Output Level */
  360. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
  361. /*Configure GPIO pin : B1_Pin */
  362. GPIO_InitStruct.Pin = B1_Pin;
  363. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  364. GPIO_InitStruct.Pull = GPIO_NOPULL;
  365. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  366. /*Configure GPIO pin : PB6 */
  367. GPIO_InitStruct.Pin = GPIO_PIN_6;
  368. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  369. GPIO_InitStruct.Pull = GPIO_NOPULL;
  370. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  371. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  372. }
  373. /* USER CODE BEGIN 4 */
  374. /* USER CODE END 4 */
  375. /**
  376. * @brief This function is executed in case of error occurrence.
  377. * @retval None
  378. */
  379. void Error_Handler(void)
  380. {
  381. /* USER CODE BEGIN Error_Handler_Debug */
  382. /* User can add his own implementation to report the HAL error return state */
  383. /* USER CODE END Error_Handler_Debug */
  384. }
  385. #ifdef USE_FULL_ASSERT
  386. /**
  387. * @brief Reports the name of the source file and the source line number
  388. * where the assert_param error has occurred.
  389. * @param file: pointer to the source file name
  390. * @param line: assert_param error line source number
  391. * @retval None
  392. */
  393. void assert_failed(uint8_t *file, uint32_t line)
  394. {
  395. /* USER CODE BEGIN 6 */
  396. /* User can add his own implementation to report the file name and line number,
  397. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  398. /* USER CODE END 6 */
  399. }
  400. #endif /* USE_FULL_ASSERT */
  401. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/