main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2022 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. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. /* USER CODE END Includes */
  24. /* Private typedef -----------------------------------------------------------*/
  25. /* USER CODE BEGIN PTD */
  26. /* USER CODE END PTD */
  27. /* Private define ------------------------------------------------------------*/
  28. /* USER CODE BEGIN PD */
  29. /* USER CODE END PD */
  30. /* Private macro -------------------------------------------------------------*/
  31. /* USER CODE BEGIN PM */
  32. /* USER CODE END PM */
  33. /* Private variables ---------------------------------------------------------*/
  34. ADC_HandleTypeDef hadc1;
  35. UART_HandleTypeDef huart2;
  36. /* USER CODE BEGIN PV */
  37. #define ADC_SAMPLES 15
  38. uint32_t adcData[ADC_SAMPLES];
  39. uint32_t adcCounter = 0;
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. static void MX_GPIO_Init(void);
  44. static void MX_USART2_UART_Init(void);
  45. static void MX_ADC1_Init(void);
  46. static void MX_NVIC_Init(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. /*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  52. {
  53. // Conversion Complete & DMA Transfer Complete As Well
  54. // So The AD_RES Is Now Updated & Let's Move IT To The PWM CCR1
  55. // Update The PWM Duty Cycle With Latest ADC Conversion Result
  56. //TIM2->CCR1 = (AD_RES<<4);
  57. //HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
  58. adcCounter++;
  59. }*/
  60. /* USER CODE END 0 */
  61. /**
  62. * @brief The application entry point.
  63. * @retval int
  64. */
  65. int main(void)
  66. {
  67. /* USER CODE BEGIN 1 */
  68. /* USER CODE END 1 */
  69. /* MCU Configuration--------------------------------------------------------*/
  70. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71. HAL_Init();
  72. /* USER CODE BEGIN Init */
  73. /* USER CODE END Init */
  74. /* Configure the system clock */
  75. SystemClock_Config();
  76. /* USER CODE BEGIN SysInit */
  77. /* USER CODE END SysInit */
  78. /* Initialize all configured peripherals */
  79. MX_GPIO_Init();
  80. MX_USART2_UART_Init();
  81. MX_ADC1_Init();
  82. /* Initialize interrupts */
  83. MX_NVIC_Init();
  84. /* USER CODE BEGIN 2 */
  85. // Start continous ADC-conversion
  86. HAL_Delay(10);
  87. ADC1->SR = 0;
  88. ADC1->CR2 = ADC_CR2_ADON | ADC_CR2_CONT;
  89. ADC1->CR1 = ADC_IT_EOC;
  90. ADC1->CR2 |= ADC_CR2_ADON;
  91. /* USER CODE END 2 */
  92. /* Infinite loop */
  93. /* USER CODE BEGIN WHILE */
  94. while (1)
  95. {
  96. /* USER CODE END WHILE */
  97. /* USER CODE BEGIN 3 */
  98. HAL_GPIO_TogglePin (GPIOC, GPIO_PIN_13);
  99. HAL_Delay (1000);
  100. }
  101. /* USER CODE END 3 */
  102. }
  103. /**
  104. * @brief System Clock Configuration
  105. * @retval None
  106. */
  107. void SystemClock_Config(void)
  108. {
  109. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  110. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  111. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  112. /** Initializes the RCC Oscillators according to the specified parameters
  113. * in the RCC_OscInitTypeDef structure.
  114. */
  115. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  116. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  117. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  118. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  119. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  120. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  121. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL7;
  122. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  123. {
  124. Error_Handler();
  125. }
  126. /** Initializes the CPU, AHB and APB buses clocks
  127. */
  128. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  129. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  130. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  131. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  132. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  133. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  134. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  135. {
  136. Error_Handler();
  137. }
  138. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  139. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  140. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  141. {
  142. Error_Handler();
  143. }
  144. }
  145. /**
  146. * @brief NVIC Configuration.
  147. * @retval None
  148. */
  149. static void MX_NVIC_Init(void)
  150. {
  151. /* ADC1_2_IRQn interrupt configuration */
  152. HAL_NVIC_SetPriority(ADC1_2_IRQn, 0, 0);
  153. HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
  154. /* USART2_IRQn interrupt configuration */
  155. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  156. HAL_NVIC_EnableIRQ(USART2_IRQn);
  157. }
  158. /**
  159. * @brief ADC1 Initialization Function
  160. * @param None
  161. * @retval None
  162. */
  163. static void MX_ADC1_Init(void)
  164. {
  165. /* USER CODE BEGIN ADC1_Init 0 */
  166. /* USER CODE END ADC1_Init 0 */
  167. ADC_ChannelConfTypeDef sConfig = {0};
  168. /* USER CODE BEGIN ADC1_Init 1 */
  169. /* USER CODE END ADC1_Init 1 */
  170. /** Common config
  171. */
  172. hadc1.Instance = ADC1;
  173. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  174. hadc1.Init.ContinuousConvMode = ENABLE;
  175. hadc1.Init.DiscontinuousConvMode = DISABLE;
  176. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  177. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  178. hadc1.Init.NbrOfConversion = 1;
  179. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  180. {
  181. Error_Handler();
  182. }
  183. /** Configure Regular Channel
  184. */
  185. sConfig.Channel = ADC_CHANNEL_5;
  186. sConfig.Rank = ADC_REGULAR_RANK_1;
  187. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  188. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  189. {
  190. Error_Handler();
  191. }
  192. /* USER CODE BEGIN ADC1_Init 2 */
  193. HAL_ADCEx_Calibration_Start(&hadc1);
  194. // Set sampling frequency
  195. sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
  196. HAL_ADC_ConfigChannel(&hadc1, &sConfig);
  197. /*
  198. * Tconv = Sampling time + 12.5 cycles
  199. *
  200. * Example:
  201. * With an ADCCLK = 14 MHz and a sampling time of 1.5 cycles: Tconv = 1.5 + 12.5 = 14 cycles = 1 μs
  202. *
  203. * In this application we want to sample a 50Hz signal = 20mS
  204. *
  205. * Calcs here: https://docs.google.com/spreadsheets/d/1an5f3Aog4bdwpe-rDquWTxlXBpsXEK-1DVhAsiCqlq0/edit#gid=304302332
  206. *
  207. * Select: 56MHz /4 55,5 = 205 882,35Hz = 4 117,6 samples / 50Hz wave
  208. *
  209. *
  210. *
  211. */
  212. /* USER CODE END ADC1_Init 2 */
  213. }
  214. /**
  215. * @brief USART2 Initialization Function
  216. * @param None
  217. * @retval None
  218. */
  219. static void MX_USART2_UART_Init(void)
  220. {
  221. /* USER CODE BEGIN USART2_Init 0 */
  222. /* USER CODE END USART2_Init 0 */
  223. /* USER CODE BEGIN USART2_Init 1 */
  224. /* USER CODE END USART2_Init 1 */
  225. huart2.Instance = USART2;
  226. huart2.Init.BaudRate = 19200;
  227. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  228. huart2.Init.StopBits = UART_STOPBITS_1;
  229. huart2.Init.Parity = UART_PARITY_NONE;
  230. huart2.Init.Mode = UART_MODE_TX_RX;
  231. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  232. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  233. if (HAL_UART_Init(&huart2) != HAL_OK)
  234. {
  235. Error_Handler();
  236. }
  237. /* USER CODE BEGIN USART2_Init 2 */
  238. /* USER CODE END USART2_Init 2 */
  239. }
  240. /**
  241. * @brief GPIO Initialization Function
  242. * @param None
  243. * @retval None
  244. */
  245. static void MX_GPIO_Init(void)
  246. {
  247. GPIO_InitTypeDef GPIO_InitStruct = {0};
  248. /* GPIO Ports Clock Enable */
  249. __HAL_RCC_GPIOC_CLK_ENABLE();
  250. __HAL_RCC_GPIOD_CLK_ENABLE();
  251. __HAL_RCC_GPIOA_CLK_ENABLE();
  252. /*Configure GPIO pin Output Level */
  253. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  254. /*Configure GPIO pin : PC13 */
  255. GPIO_InitStruct.Pin = GPIO_PIN_13;
  256. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  257. GPIO_InitStruct.Pull = GPIO_NOPULL;
  258. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  259. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  260. }
  261. /* USER CODE BEGIN 4 */
  262. /* USER CODE END 4 */
  263. /**
  264. * @brief This function is executed in case of error occurrence.
  265. * @retval None
  266. */
  267. void Error_Handler(void)
  268. {
  269. /* USER CODE BEGIN Error_Handler_Debug */
  270. /* User can add his own implementation to report the HAL error return state */
  271. __disable_irq();
  272. while (1)
  273. {
  274. }
  275. /* USER CODE END Error_Handler_Debug */
  276. }
  277. #ifdef USE_FULL_ASSERT
  278. /**
  279. * @brief Reports the name of the source file and the source line number
  280. * where the assert_param error has occurred.
  281. * @param file: pointer to the source file name
  282. * @param line: assert_param error line source number
  283. * @retval None
  284. */
  285. void assert_failed(uint8_t *file, uint32_t line)
  286. {
  287. /* USER CODE BEGIN 6 */
  288. /* User can add his own implementation to report the file name and line number,
  289. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  290. /* USER CODE END 6 */
  291. }
  292. #endif /* USE_FULL_ASSERT */