main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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) 2022 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 "adc.h"
  23. #include "i2c.h"
  24. #include "rtc.h"
  25. #include "tim.h"
  26. #include "usart.h"
  27. #include "gpio.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. #include "Tab.h"
  31. #include "PWM.h"
  32. #include "Comunication.h"
  33. /* USER CODE END Includes */
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* USER CODE BEGIN PTD */
  36. /* USER CODE END PTD */
  37. /* Private define ------------------------------------------------------------*/
  38. /* USER CODE BEGIN PD */
  39. #define DELAY 10000
  40. /* USER CODE END PD */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43. /* USER CODE END PM */
  44. /* Private variables ---------------------------------------------------------*/
  45. /* USER CODE BEGIN PV */
  46. Sortie tabSortie[10];
  47. uint8_t gl_Stop;
  48. uint8_t gl_Essai;
  49. uint8_t RX_uart[SizeMes];
  50. uint8_t erreur = 0;
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. /* USER CODE BEGIN PFP */
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  59. __HAL_TIM_SET_COUNTER(&htim10,0);
  60. HAL_TIM_Base_Start_IT(&htim10);
  61. parse(RX_uart);
  62. HAL_UART_Receive_IT(&huart6, RX_uart, 8);
  63. }
  64. void InitValeur(){
  65. for(int i = 0; i<10; i++){
  66. tabSortie[i].Extinxtion = 22*60;
  67. tabSortie[i].Demmarage = 10*60;
  68. tabSortie[i].PuissanceMax = 50;
  69. tabSortie[i].Pente= 0;
  70. }
  71. gl_Stop = 0;
  72. gl_Essai = 0;
  73. }
  74. /* USER CODE END 0 */
  75. /**
  76. * @brief The application entry point.
  77. * @retval int
  78. */
  79. int main(void)
  80. {
  81. /* USER CODE BEGIN 1 */
  82. /*
  83. * Fonctionnement de l'algo
  84. *
  85. * On initialise :
  86. * - Cube MX
  87. * - Eeprom
  88. * - On charge les valeur de l'Eeprom
  89. * - On demarre la reception de L'Uart
  90. *
  91. * Si le Blynk est en vie
  92. * on recoit un status, Le code erreur etant a 1 l'ESP32 nous renvoie toute les valeurs
  93. * A partir de ce moment l'ESP doit envoyer un Heart bit avant la fin du timer sinon on restart
  94. * A chaque commande on ecrit la valeurs dans l'EEPROM
  95. *
  96. * Si le Blynk est eteint
  97. * On fonctionne sur les valeurs de l'Eeprom
  98. * On repart sur le fonctionnement "normal" des que l'ESP envoie une commande
  99. */
  100. /* USER CODE END 1 */
  101. /* MCU Configuration--------------------------------------------------------*/
  102. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  103. HAL_Init();
  104. /* USER CODE BEGIN Init */
  105. /* USER CODE END Init */
  106. /* Configure the system clock */
  107. SystemClock_Config();
  108. /* USER CODE BEGIN SysInit */
  109. /* USER CODE END SysInit */
  110. /* Initialize all configured peripherals */
  111. MX_GPIO_Init();
  112. MX_TIM2_Init();
  113. MX_TIM3_Init();
  114. MX_TIM4_Init();
  115. MX_TIM9_Init();
  116. MX_I2C3_Init();
  117. MX_I2C2_Init();
  118. MX_ADC1_Init();
  119. MX_USART6_UART_Init();
  120. MX_RTC_Init();
  121. MX_TIM10_Init();
  122. /* USER CODE BEGIN 2 */
  123. InitValeur();
  124. //initialise le timer de heard beat et l'uart
  125. erreur=1;
  126. __HAL_TIM_CLEAR_FLAG(&htim10, TIM_FLAG_UPDATE);
  127. HAL_UART_Receive_IT(&huart6, RX_uart, 8);
  128. /* USER CODE END 2 */
  129. /* Infinite loop */
  130. /* USER CODE BEGIN WHILE */
  131. while (1){
  132. if(gl_Essai){//EN essai
  133. Essai();
  134. }
  135. else if(gl_Stop){//Tous arreté
  136. Stop();
  137. }
  138. else{//Fonctionnement normal
  139. uint16_t Hmin = ReadHeureMin();
  140. GerePWM(Hmin);
  141. }
  142. HAL_Delay(DELAY);
  143. /* USER CODE END WHILE */
  144. /* USER CODE BEGIN 3 */
  145. }
  146. /* USER CODE END 3 */
  147. }
  148. /**
  149. * @brief System Clock Configuration
  150. * @retval None
  151. */
  152. void SystemClock_Config(void)
  153. {
  154. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  155. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  156. /** Configure the main internal regulator output voltage
  157. */
  158. __HAL_RCC_PWR_CLK_ENABLE();
  159. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  160. /** Initializes the RCC Oscillators according to the specified parameters
  161. * in the RCC_OscInitTypeDef structure.
  162. */
  163. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  164. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  165. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  166. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  167. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  168. RCC_OscInitStruct.PLL.PLLM = 25;
  169. RCC_OscInitStruct.PLL.PLLN = 336;
  170. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  171. RCC_OscInitStruct.PLL.PLLQ = 7;
  172. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  173. {
  174. Error_Handler();
  175. }
  176. /** Initializes the CPU, AHB and APB buses clocks
  177. */
  178. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  179. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  180. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  181. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  182. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  183. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  184. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  185. {
  186. Error_Handler();
  187. }
  188. }
  189. /* USER CODE BEGIN 4 */
  190. /* USER CODE END 4 */
  191. /**
  192. * @brief Period elapsed callback in non blocking mode
  193. * @note This function is called when TIM5 interrupt took place, inside
  194. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  195. * a global variable "uwTick" used as application time base.
  196. * @param htim : TIM handle
  197. * @retval None
  198. */
  199. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  200. {
  201. /* USER CODE BEGIN Callback 0 */
  202. /* USER CODE END Callback 0 */
  203. if (htim->Instance == TIM5) {
  204. HAL_IncTick();
  205. }
  206. /* USER CODE BEGIN Callback 1 */
  207. /* USER CODE END Callback 1 */
  208. }
  209. /**
  210. * @brief This function is executed in case of error occurrence.
  211. * @retval None
  212. */
  213. void Error_Handler(void)
  214. {
  215. /* USER CODE BEGIN Error_Handler_Debug */
  216. //TODO a refaire l'erreur handler pour envoyer en reset
  217. /* User can add his own implementation to report the HAL error return state */
  218. __disable_irq();
  219. while (1)
  220. {
  221. }
  222. /* USER CODE END Error_Handler_Debug */
  223. }
  224. #ifdef USE_FULL_ASSERT
  225. /**
  226. * @brief Reports the name of the source file and the source line number
  227. * where the assert_param error has occurred.
  228. * @param file: pointer to the source file name
  229. * @param line: assert_param error line source number
  230. * @retval None
  231. */
  232. void assert_failed(uint8_t *file, uint32_t line)
  233. {
  234. /* USER CODE BEGIN 6 */
  235. /* User can add his own implementation to report the file name and line number,
  236. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  237. /* USER CODE END 6 */
  238. }
  239. #endif /* USE_FULL_ASSERT */