main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. RX_uart[0]=0xAA;
  167. while (1){
  168. HAL_UART_Transmit(&huart6, RX_uart, 1,1000);
  169. /*if(HAL_UART_Receive(&huart6, RX_uart,1,1000)==HAL_OK){
  170. HAL_Delay(10);
  171. }
  172. else{
  173. HAL_Delay(100);
  174. }*/
  175. HAL_Delay(500);
  176. /*
  177. if(gl_Essai){//EN essai
  178. Essai();
  179. }
  180. else if(gl_Stop){//Tous arreté
  181. Stop();
  182. }
  183. else{//Fonctionnement normal
  184. uint16_t Hmin = ReadHeureMin();
  185. GerePWM(Hmin);
  186. }
  187. HAL_Delay(DELAY);
  188. /* USER CODE END WHILE */
  189. /* USER CODE BEGIN 3 */
  190. }
  191. /* USER CODE END 3 */
  192. }
  193. /**
  194. * @brief System Clock Configuration
  195. * @retval None
  196. */
  197. void SystemClock_Config(void)
  198. {
  199. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  200. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  201. /** Configure the main internal regulator output voltage
  202. */
  203. __HAL_RCC_PWR_CLK_ENABLE();
  204. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  205. /** Initializes the RCC Oscillators according to the specified parameters
  206. * in the RCC_OscInitTypeDef structure.
  207. */
  208. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  209. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  210. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  211. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  212. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  213. RCC_OscInitStruct.PLL.PLLM = 25;
  214. RCC_OscInitStruct.PLL.PLLN = 336;
  215. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  216. RCC_OscInitStruct.PLL.PLLQ = 7;
  217. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  218. {
  219. Error_Handler();
  220. }
  221. /** Initializes the CPU, AHB and APB buses clocks
  222. */
  223. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  224. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  225. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  226. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  227. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  228. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  229. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  230. {
  231. Error_Handler();
  232. }
  233. }
  234. /* USER CODE BEGIN 4 */
  235. /* USER CODE END 4 */
  236. /**
  237. * @brief Period elapsed callback in non blocking mode
  238. * @note This function is called when TIM5 interrupt took place, inside
  239. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  240. * a global variable "uwTick" used as application time base.
  241. * @param htim : TIM handle
  242. * @retval None
  243. */
  244. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  245. {
  246. /* USER CODE BEGIN Callback 0 */
  247. /* USER CODE END Callback 0 */
  248. if (htim->Instance == TIM5) {
  249. HAL_IncTick();
  250. }
  251. /* USER CODE BEGIN Callback 1 */
  252. /* USER CODE END Callback 1 */
  253. }
  254. /**
  255. * @brief This function is executed in case of error occurrence.
  256. * @retval None
  257. */
  258. void Error_Handler(void)
  259. {
  260. /* USER CODE BEGIN Error_Handler_Debug */
  261. //TODO a refaire l'erreur handler pour envoyer en reset
  262. /* User can add his own implementation to report the HAL error return state */
  263. __disable_irq();
  264. while (1)
  265. {
  266. }
  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. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  282. /* USER CODE END 6 */
  283. }
  284. #endif /* USE_FULL_ASSERT */