main.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "cmsis_os.h"
  23. #include "adc.h"
  24. #include "crc.h"
  25. #include "dma.h"
  26. #include "i2c.h"
  27. #include "iwdg.h"
  28. #include "rtc.h"
  29. #include "spi.h"
  30. #include "tim.h"
  31. #include "usart.h"
  32. #include "usb_device.h"
  33. #include "wwdg.h"
  34. #include "gpio.h"
  35. /* Private includes ----------------------------------------------------------*/
  36. /* USER CODE BEGIN Includes */
  37. #include "stm32f4xx_selftest.h"
  38. /* USER CODE END Includes */
  39. /* Private typedef -----------------------------------------------------------*/
  40. /* USER CODE BEGIN PTD */
  41. /* USER CODE END PTD */
  42. /* Private define ------------------------------------------------------------*/
  43. /* USER CODE BEGIN PD */
  44. /* USER CODE END PD */
  45. /* Private macro -------------------------------------------------------------*/
  46. /* USER CODE BEGIN PM */
  47. /* USER CODE END PM */
  48. /* Private variables ---------------------------------------------------------*/
  49. /* USER CODE BEGIN PV */
  50. /* USER CODE END PV */
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. void MX_FREERTOS_Init(void);
  54. /* USER CODE BEGIN PFP */
  55. extern void setLedMotion(uint8_t action);
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. /* USER CODE END 0 */
  60. /**
  61. * @brief The application entry point.
  62. * @retval int
  63. */
  64. int main(void)
  65. {
  66. /* USER CODE BEGIN 1 */
  67. /* USER CODE END 1 */
  68. /* MCU Configuration--------------------------------------------------------*/
  69. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  70. HAL_Init();
  71. /* USER CODE BEGIN Init */
  72. Charger.memory.EVSE_Config.data.item.isDispDebug = ON;
  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_DMA_Init();
  81. MX_ADC1_Init();
  82. MX_ADC3_Init();
  83. MX_CRC_Init();
  84. MX_IWDG_Init();
  85. MX_RTC_Init();
  86. MX_TIM5_Init();
  87. MX_TIM1_Init();
  88. MX_TIM4_Init();
  89. MX_UART5_Init();
  90. MX_USART1_UART_Init();
  91. MX_USART2_UART_Init();
  92. MX_USART3_UART_Init();
  93. MX_USART6_UART_Init();
  94. MX_ADC2_Init();
  95. MX_I2C2_Init();
  96. MX_SPI1_Init();
  97. MX_TIM11_Init();
  98. MX_TIM9_Init();
  99. MX_TIM10_Init();
  100. MX_TIM12_Init();
  101. MX_TIM13_Init();
  102. MX_TIM14_Init();
  103. //MX_WWDG_Init();
  104. MX_TIM6_Init();
  105. /* USER CODE BEGIN 2 */
  106. __enable_irq();
  107. Charger.Mode = MODE_INITIAL ;
  108. if(Startup_Test() == TEST_OK)
  109. {
  110. // ADC DMA start
  111. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  112. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC2_Buffer, ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  113. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t*)&ADC3_Buffer, ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  114. // UART interrupt enable
  115. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  116. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  117. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  118. __HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE);
  119. __HAL_IWDG_START(&hiwdg);
  120. HAL_TIM_Base_Start_IT(&htim5);
  121. HAL_TIM_Base_Start_IT(&htim6);
  122. // PVD initialization
  123. PWR_PVDTypeDef PWR_PVDStruct;
  124. PWR_PVDStruct.PVDLevel = PWR_PVDLEVEL_6;
  125. PWR_PVDStruct.Mode = PWR_PVD_MODE_IT_RISING;
  126. HAL_PWR_ConfigPVD(&PWR_PVDStruct);
  127. HAL_PWR_EnablePVD();
  128. }
  129. else
  130. {
  131. Charger.Alarm_Code |= ALARM_MCU_TESTFAIL;
  132. Error_Handler();
  133. }
  134. /* USER CODE END 2 */
  135. /* Call init function for freertos objects (in freertos.c) */
  136. MX_FREERTOS_Init();
  137. /* Start scheduler */
  138. osKernelStart();
  139. /* We should never get here as control is now taken by the scheduler */
  140. /* Infinite loop */
  141. /* USER CODE BEGIN WHILE */
  142. while (1)
  143. {
  144. /* USER CODE END WHILE */
  145. /* USER CODE BEGIN 3 */
  146. }
  147. /* USER CODE END 3 */
  148. }
  149. /**
  150. * @brief System Clock Configuration
  151. * @retval None
  152. */
  153. void SystemClock_Config(void)
  154. {
  155. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  156. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  157. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  158. /** Configure the main internal regulator output voltage
  159. */
  160. __HAL_RCC_PWR_CLK_ENABLE();
  161. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  162. /** Initializes the CPU, AHB and APB busses clocks
  163. */
  164. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE
  165. |RCC_OSCILLATORTYPE_LSE;
  166. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  167. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  168. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  169. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  170. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  171. RCC_OscInitStruct.PLL.PLLM = 4;
  172. RCC_OscInitStruct.PLL.PLLN = 168;
  173. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  174. RCC_OscInitStruct.PLL.PLLQ = 7;
  175. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  176. {
  177. Error_Handler();
  178. }
  179. /** Initializes the CPU, AHB and APB busses clocks
  180. */
  181. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  182. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  183. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  184. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  185. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  186. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  187. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  192. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  193. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. }
  198. /* USER CODE BEGIN 4 */
  199. /* USER CODE END 4 */
  200. /**
  201. * @brief Period elapsed callback in non blocking mode
  202. * @note This function is called when TIM7 interrupt took place, inside
  203. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  204. * a global variable "uwTick" used as application time base.
  205. * @param htim : TIM handle
  206. * @retval None
  207. */
  208. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  209. {
  210. /* USER CODE BEGIN Callback 0 */
  211. if(htim->Instance == TIM6) {
  212. }
  213. /* USER CODE END Callback 0 */
  214. if (htim->Instance == TIM7) {
  215. HAL_IncTick();
  216. }
  217. /* USER CODE BEGIN Callback 1 */
  218. if(htim->Instance == TIM13){
  219. user_pwm_setvalue(PWM_CH_GFCI_TEST, (199 + (uint16_t)(0.8*200*sin(2*3.14*SPWM_idx/167.0))));
  220. if(++SPWM_idx> 167)
  221. SPWM_idx = 0;
  222. }
  223. if(htim->Instance == TIM14) {
  224. Interrupt_Test_Flag = 1;
  225. ClockCheckHSE++;
  226. ClockCheckHSI++;
  227. }
  228. /* USER CODE END Callback 1 */
  229. }
  230. /**
  231. * @brief This function is executed in case of error occurrence.
  232. * @retval None
  233. */
  234. void Error_Handler(void)
  235. {
  236. /* USER CODE BEGIN Error_Handler_Debug */
  237. /* User can add his own implementation to report the HAL error return state */
  238. DEBUG_INFO("Error occur...\r\n");
  239. /* USER CODE END Error_Handler_Debug */
  240. }
  241. #ifdef USE_FULL_ASSERT
  242. /**
  243. * @brief Reports the name of the source file and the source line number
  244. * where the assert_param error has occurred.
  245. * @param file: pointer to the source file name
  246. * @param line: assert_param error line source number
  247. * @retval None
  248. */
  249. void assert_failed(uint8_t *file, uint32_t line)
  250. {
  251. /* USER CODE BEGIN 6 */
  252. /* User can add his own implementation to report the file name and line number,
  253. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  254. /* USER CODE END 6 */
  255. }
  256. #endif /* USE_FULL_ASSERT */
  257. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/