main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. uint16_t ReadEepromHeure(uint8_t add){
  65. uint8_t tab[2];
  66. if (readEeprom(add, tab,2)!=HAL_OK){
  67. /*
  68. * Grosse merde ca fonctionne pas du tout
  69. * Mettre les valeurs a :
  70. * Demmarage = 9H
  71. * Extinxtion = 20H
  72. * PuissanceMax = 50%
  73. * Pente = 0
  74. */
  75. if (add%5==0)//Si demmarage
  76. return 9*60;
  77. else
  78. return 20*60;
  79. }
  80. return tab[0]*60+tab[1];
  81. }
  82. uint8_t ReadEepromVal(uint8_t add){
  83. uint8_t tab[1];
  84. if (readEeprom(add, tab,1)!=HAL_OK){
  85. /*
  86. * Grosse merde ca fonctionne pas du tout
  87. * Mettre les valeurs a :
  88. * Demmarage = 9H
  89. * Extinxtion = 20H
  90. * PuissanceMax = 50%
  91. * Pente = 0
  92. */
  93. return 50;
  94. }
  95. return tab[0];
  96. }
  97. void ChargeEeprom(){
  98. for(int i = 0; i<10; i++){
  99. tabSortie[i].Extinxtion = ReadEepromHeure(i*5);
  100. tabSortie[i].Demmarage = ReadEepromHeure(i*5+2);
  101. tabSortie[i].PuissanceMax = ReadEepromVal(i*5+3);
  102. tabSortie[i].Pente= ReadEepromVal(i*5+4);
  103. }
  104. gl_Stop = 0;
  105. gl_Essai = 0;
  106. }
  107. /* USER CODE END 0 */
  108. /**
  109. * @brief The application entry point.
  110. * @retval int
  111. */
  112. int main(void)
  113. {
  114. /* USER CODE BEGIN 1 */
  115. /*
  116. * Fonctionnement de l'algo
  117. *
  118. * On initialise :
  119. * - Cube MX
  120. * - Eeprom
  121. * - On charge les valeur de l'Eeprom
  122. * - On demarre la reception de L'Uart
  123. *
  124. * Si le Blynk est en vie
  125. * on recoit un status, Le code erreur etant a 1 l'ESP32 nous renvoie toute les valeurs
  126. * A partir de ce moment l'ESP doit envoyer un Heart bit avant la fin du timer sinon on restart
  127. * A chaque commande on ecrit la valeurs dans l'EEPROM
  128. *
  129. * Si le Blynk est eteint
  130. * On fonctionne sur les valeurs de l'Eeprom
  131. * On repart sur le fonctionnement "normal" des que l'ESP envoie une commande
  132. */
  133. /* USER CODE END 1 */
  134. /* MCU Configuration--------------------------------------------------------*/
  135. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  136. HAL_Init();
  137. /* USER CODE BEGIN Init */
  138. /* USER CODE END Init */
  139. /* Configure the system clock */
  140. SystemClock_Config();
  141. /* USER CODE BEGIN SysInit */
  142. /* USER CODE END SysInit */
  143. /* Initialize all configured peripherals */
  144. MX_GPIO_Init();
  145. MX_TIM2_Init();
  146. MX_TIM3_Init();
  147. MX_TIM4_Init();
  148. MX_TIM9_Init();
  149. MX_I2C3_Init();
  150. MX_I2C2_Init();
  151. MX_ADC1_Init();
  152. MX_USART6_UART_Init();
  153. MX_RTC_Init();
  154. MX_TIM10_Init();
  155. /* USER CODE BEGIN 2 */
  156. initEprom(&hi2c3,WC_GPIO_Port,WC_Pin);
  157. //On charge les valeur de l'Eeprom
  158. ChargeEeprom();
  159. //initialise le timer de heard beat et l'uart
  160. erreur=1;
  161. __HAL_TIM_CLEAR_FLAG(&htim10, TIM_FLAG_UPDATE);
  162. HAL_UART_Receive_IT(&huart6, RX_uart, 8);
  163. /* USER CODE END 2 */
  164. /* Infinite loop */
  165. /* USER CODE BEGIN WHILE */
  166. while (1){
  167. if(gl_Essai){//EN essai
  168. Essai();
  169. }
  170. else if(gl_Stop){//Tous arreté
  171. Stop();
  172. }
  173. else{//Fonctionnement normal
  174. uint16_t Hmin = ReadHeureMin();
  175. GerePWM(Hmin);
  176. }
  177. HAL_Delay(DELAY);
  178. /* USER CODE END WHILE */
  179. /* USER CODE BEGIN 3 */
  180. }
  181. /* USER CODE END 3 */
  182. }
  183. /**
  184. * @brief System Clock Configuration
  185. * @retval None
  186. */
  187. void SystemClock_Config(void)
  188. {
  189. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  190. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  191. /** Configure the main internal regulator output voltage
  192. */
  193. __HAL_RCC_PWR_CLK_ENABLE();
  194. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  195. /** Initializes the RCC Oscillators according to the specified parameters
  196. * in the RCC_OscInitTypeDef structure.
  197. */
  198. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  199. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  200. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  201. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  202. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  203. RCC_OscInitStruct.PLL.PLLM = 25;
  204. RCC_OscInitStruct.PLL.PLLN = 336;
  205. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  206. RCC_OscInitStruct.PLL.PLLQ = 7;
  207. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  208. {
  209. Error_Handler();
  210. }
  211. /** Initializes the CPU, AHB and APB buses clocks
  212. */
  213. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  214. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  215. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  216. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  217. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  218. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  219. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  220. {
  221. Error_Handler();
  222. }
  223. }
  224. /* USER CODE BEGIN 4 */
  225. /* USER CODE END 4 */
  226. /**
  227. * @brief Period elapsed callback in non blocking mode
  228. * @note This function is called when TIM5 interrupt took place, inside
  229. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  230. * a global variable "uwTick" used as application time base.
  231. * @param htim : TIM handle
  232. * @retval None
  233. */
  234. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  235. {
  236. /* USER CODE BEGIN Callback 0 */
  237. /* USER CODE END Callback 0 */
  238. if (htim->Instance == TIM5) {
  239. HAL_IncTick();
  240. }
  241. /* USER CODE BEGIN Callback 1 */
  242. /* USER CODE END Callback 1 */
  243. }
  244. /**
  245. * @brief This function is executed in case of error occurrence.
  246. * @retval None
  247. */
  248. void Error_Handler(void)
  249. {
  250. /* USER CODE BEGIN Error_Handler_Debug */
  251. //TODO a refaire l'erreur handler pour envoyer en reset
  252. /* User can add his own implementation to report the HAL error return state */
  253. __disable_irq();
  254. while (1)
  255. {
  256. }
  257. /* USER CODE END Error_Handler_Debug */
  258. }
  259. #ifdef USE_FULL_ASSERT
  260. /**
  261. * @brief Reports the name of the source file and the source line number
  262. * where the assert_param error has occurred.
  263. * @param file: pointer to the source file name
  264. * @param line: assert_param error line source number
  265. * @retval None
  266. */
  267. void assert_failed(uint8_t *file, uint32_t line)
  268. {
  269. /* USER CODE BEGIN 6 */
  270. /* User can add his own implementation to report the file name and line number,
  271. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  272. /* USER CODE END 6 */
  273. }
  274. #endif /* USE_FULL_ASSERT */