main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. //#ifdef FUNC_USE_STM32_SBSFU
  36. //#include "mpu.h" //for MPU_EnterUnprivilegedMode
  37. //#endif
  38. /* Private includes ----------------------------------------------------------*/
  39. /* USER CODE BEGIN Includes */
  40. #include "stm32f4xx_selftest.h"
  41. /* USER CODE END Includes */
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* USER CODE BEGIN PTD */
  44. /* USER CODE END PTD */
  45. /* Private define ------------------------------------------------------------*/
  46. /* USER CODE BEGIN PD */
  47. /* USER CODE END PD */
  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */
  50. /* USER CODE END PM */
  51. /* Private variables ---------------------------------------------------------*/
  52. /* USER CODE BEGIN PV */
  53. /* USER CODE END PV */
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. void MX_FREERTOS_Init(void);
  57. /* USER CODE BEGIN PFP */
  58. extern void setLedMotion(uint8_t action);
  59. #ifdef FUNC_AW48_NET_LED
  60. extern void setNetLedMotion(uint8_t action);
  61. #endif
  62. /* USER CODE END PFP */
  63. /* Private user code ---------------------------------------------------------*/
  64. /* USER CODE BEGIN 0 */
  65. /* USER CODE END 0 */
  66. /**
  67. * @brief The application entry point.
  68. * @retval int
  69. */
  70. int main(void)
  71. {
  72. /* USER CODE BEGIN 1 */
  73. //mmain
  74. /* USER CODE END 1 */
  75. /* MCU Configuration--------------------------------------------------------*/
  76. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  77. HAL_Init();
  78. /* USER CODE BEGIN Init */
  79. Charger.memory.EVSE_Config.data.item.isDispDebug = ON;
  80. /* USER CODE END Init */
  81. /* Configure the system clock */
  82. SystemClock_Config();
  83. /* USER CODE BEGIN SysInit */
  84. /* USER CODE END SysInit */
  85. /* Initialize all configured peripherals */
  86. MX_GPIO_Init();
  87. MX_DMA_Init();
  88. MX_ADC1_Init();
  89. MX_ADC3_Init();
  90. MX_CRC_Init();
  91. MX_IWDG_Init();
  92. MX_RTC_Init();
  93. MX_TIM5_Init();
  94. MX_TIM1_Init();
  95. MX_TIM4_Init();
  96. MX_UART5_Init();
  97. #ifdef FUNC_ENABLE_USART1_RFID
  98. MX_USART1_UART_Init();
  99. #endif
  100. #ifdef FUNC_ENABLE_USART1_LIN_LL_LIB
  101. MX_USART1_UART_Init();
  102. #endif
  103. #ifndef FUNC_DISABLE_USART2_TO_PASS_AW48_RF_TEST_20240116_DEKRA
  104. MX_USART2_UART_Init();
  105. #endif
  106. MX_USART3_UART_Init();
  107. MX_USART6_UART_Init();
  108. MX_ADC2_Init();
  109. MX_I2C2_Init();
  110. MX_SPI1_Init();
  111. MX_TIM11_Init();
  112. MX_TIM9_Init();
  113. MX_TIM10_Init();
  114. MX_TIM12_Init();
  115. MX_TIM13_Init();
  116. MX_TIM14_Init();
  117. //MX_WWDG_Init();
  118. MX_TIM6_Init();
  119. MX_UART4_Init();
  120. /* USER CODE BEGIN 2 */
  121. __enable_irq();
  122. Charger.Mode = MODE_INITIAL ;
  123. //#ifdef FUNC_USE_STM32_SBSFU
  124. // //MPU_EnterUnprivilegedMode();
  125. // //https://community.st.com/t5/stm32-mcus-security/hello-i-m-using-sbsfu-2-6-2-for-user-application-over-freertos/td-p/71351
  126. // //FreeRTOS kernel must run in privilege mode
  127. //#endif
  128. if(Startup_Test() == TEST_OK)
  129. {
  130. // ADC DMA start
  131. if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC1_Buffer, ADC1_CHANEL_COUNT*ADC1_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  132. if(HAL_ADC_Start_DMA(&hadc2, (uint32_t*)&ADC2_Buffer, ADC2_CHANEL_COUNT*ADC2_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  133. if(HAL_ADC_Start_DMA(&hadc3, (uint32_t*)&ADC3_Buffer, ADC3_CHANEL_COUNT*ADC3_SAMPLE_COUNT)!=HAL_OK)Error_Handler();
  134. // UART interrupt enable
  135. #ifdef FUNC_ENABLE_USART1_RFID
  136. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  137. #endif
  138. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
  139. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  140. __HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE);
  141. #ifdef FUNC_DEBUG_CONSOLE
  142. __HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
  143. #endif
  144. __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
  145. // IWDG interrupt enable
  146. __HAL_IWDG_START(&hiwdg);
  147. // TIMER interrupt enable
  148. HAL_TIM_Base_Start_IT(&htim5);
  149. HAL_TIM_Base_Start_IT(&htim6);
  150. // PVD initialization
  151. PWR_PVDTypeDef PWR_PVDStruct;
  152. PWR_PVDStruct.PVDLevel = PWR_PVDLEVEL_6;
  153. PWR_PVDStruct.Mode = PWR_PVD_MODE_IT_RISING;
  154. HAL_PWR_ConfigPVD(&PWR_PVDStruct);
  155. HAL_PWR_EnablePVD();
  156. }
  157. else
  158. {
  159. Charger.Alarm_Code |= ALARM_MCU_TESTFAIL;
  160. Error_Handler();
  161. }
  162. /* USER CODE END 2 */
  163. /* Call init function for freertos objects (in freertos.c) */
  164. MX_FREERTOS_Init();
  165. /* Start scheduler */
  166. osKernelStart();
  167. /* We should never get here as control is now taken by the scheduler */
  168. /* Infinite loop */
  169. /* USER CODE BEGIN WHILE */
  170. while (1)
  171. {
  172. /* USER CODE END WHILE */
  173. /* USER CODE BEGIN 3 */
  174. }
  175. /* USER CODE END 3 */
  176. }
  177. /**
  178. * @brief System Clock Configuration
  179. * @retval None
  180. */
  181. void SystemClock_Config(void)
  182. {
  183. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  184. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  185. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  186. /** Configure the main internal regulator output voltage
  187. */
  188. __HAL_RCC_PWR_CLK_ENABLE();
  189. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  190. /** Initializes the CPU, AHB and APB busses clocks
  191. */
  192. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE
  193. |RCC_OSCILLATORTYPE_LSE;
  194. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  195. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  196. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  197. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  198. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  199. RCC_OscInitStruct.PLL.PLLM = 4;
  200. RCC_OscInitStruct.PLL.PLLN = 168;
  201. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  202. RCC_OscInitStruct.PLL.PLLQ = 7;
  203. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  204. {
  205. Error_Handler();
  206. }
  207. /** Initializes the CPU, AHB and APB busses clocks
  208. */
  209. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  210. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  211. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  212. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  213. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  214. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  215. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  216. {
  217. Error_Handler();
  218. }
  219. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  220. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  221. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  222. {
  223. Error_Handler();
  224. }
  225. }
  226. /* USER CODE BEGIN 4 */
  227. /* USER CODE END 4 */
  228. /**
  229. * @brief Period elapsed callback in non blocking mode
  230. * @note This function is called when TIM7 interrupt took place, inside
  231. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  232. * a global variable "uwTick" used as application time base.
  233. * @param htim : TIM handle
  234. * @retval None
  235. */
  236. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  237. {
  238. /* USER CODE BEGIN Callback 0 */
  239. if(htim->Instance == TIM6) {
  240. }
  241. /* USER CODE END Callback 0 */
  242. if (htim->Instance == TIM7) {
  243. HAL_IncTick();
  244. }
  245. /* USER CODE BEGIN Callback 1 */
  246. if(htim->Instance == TIM13){
  247. user_pwm_setvalue(PWM_CH_GFCI_TEST, (199 + (uint16_t)(0.8*200*sin(2*3.14*SPWM_idx/167.0))));
  248. if(++SPWM_idx> 167)
  249. SPWM_idx = 0;
  250. }
  251. if(htim->Instance == TIM14) {
  252. Interrupt_Test_Flag = 1;
  253. ClockCheckHSE++;
  254. ClockCheckHSI++;
  255. }
  256. /* USER CODE END Callback 1 */
  257. }
  258. /**
  259. * @brief This function is executed in case of error occurrence.
  260. * @retval None
  261. */
  262. void Error_Handler(void)
  263. {
  264. /* USER CODE BEGIN Error_Handler_Debug */
  265. /* User can add his own implementation to report the HAL error return state */
  266. DEBUG_INFO("Error occur...\r\n");
  267. /* USER CODE END Error_Handler_Debug */
  268. }
  269. #ifdef USE_FULL_ASSERT
  270. /**
  271. * @brief Reports the name of the source file and the source line number
  272. * where the assert_param error has occurred.
  273. * @param file: pointer to the source file name
  274. * @param line: assert_param error line source number
  275. * @retval None
  276. */
  277. void assert_failed(uint8_t *file, uint32_t line)
  278. {
  279. /* USER CODE BEGIN 6 */
  280. /* User can add his own implementation to report the file name and line number,
  281. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  282. /* USER CODE END 6 */
  283. }
  284. #endif /* USE_FULL_ASSERT */
  285. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/