freertos.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : freertos.c
  5. * Description : Code for freertos applications
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* USER CODE END Header */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "FreeRTOS.h"
  52. #include "task.h"
  53. #include "main.h"
  54. #include "cmsis_os.h"
  55. /* Private includes ----------------------------------------------------------*/
  56. /* USER CODE BEGIN Includes */
  57. #include "Tab.h"
  58. #include "PWM.h"
  59. #include "Comunication.h"
  60. #include <RTC.h>
  61. #include <FIFO_COM.h>
  62. #include "usart.h"
  63. /* USER CODE END Includes */
  64. /* Private typedef -----------------------------------------------------------*/
  65. /* USER CODE BEGIN PTD */
  66. /* USER CODE END PTD */
  67. /* Private define ------------------------------------------------------------*/
  68. /* USER CODE BEGIN PD */
  69. /* USER CODE END PD */
  70. /* Private macro -------------------------------------------------------------*/
  71. /* USER CODE BEGIN PM */
  72. /* USER CODE END PM */
  73. /* Private variables ---------------------------------------------------------*/
  74. /* USER CODE BEGIN Variables */
  75. extern uint8_t gl_Stop;
  76. extern uint8_t gl_Essai;
  77. uint8_t RX_uart[8];
  78. FIFO_COM fifo;
  79. /* USER CODE END Variables */
  80. osThreadId PWMHandle;
  81. osThreadId DecodeHandle;
  82. osThreadId LedHandle;
  83. /* Private function prototypes -----------------------------------------------*/
  84. /* USER CODE BEGIN FunctionPrototypes */
  85. /* USER CODE END FunctionPrototypes */
  86. void PWMTask(void const * argument);
  87. void DecodeTask(void const * argument);
  88. void StartTask03(void const * argument);
  89. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  90. /* GetIdleTaskMemory prototype (linked to static allocation support) */
  91. void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
  92. /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
  93. static StaticTask_t xIdleTaskTCBBuffer;
  94. static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
  95. void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
  96. StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) {
  97. *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  98. *ppxIdleTaskStackBuffer = &xIdleStack[0];
  99. *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  100. /* place for user code */
  101. }
  102. /* USER CODE END GET_IDLE_TASK_MEMORY */
  103. /**
  104. * @brief FreeRTOS initialization
  105. * @param None
  106. * @retval None
  107. */
  108. void MX_FREERTOS_Init(void) {
  109. /* USER CODE BEGIN Init */
  110. initRTC(&hi2c3);
  111. HAL_UART_Receive_IT(&huart6,RX_uart, 1);
  112. initTab(tabSortie);
  113. HAL_Delay(2000);
  114. Sync();
  115. /* USER CODE END Init */
  116. /* USER CODE BEGIN RTOS_MUTEX */
  117. /* add mutexes, ... */
  118. /* USER CODE END RTOS_MUTEX */
  119. /* USER CODE BEGIN RTOS_SEMAPHORES */
  120. /* add semaphores, ... */
  121. /* USER CODE END RTOS_SEMAPHORES */
  122. /* USER CODE BEGIN RTOS_TIMERS */
  123. /* start timers, add new ones, ... */
  124. /* USER CODE END RTOS_TIMERS */
  125. /* USER CODE BEGIN RTOS_QUEUES */
  126. /* add queues, ... */
  127. /* USER CODE END RTOS_QUEUES */
  128. /* Create the thread(s) */
  129. /* definition and creation of PWM */
  130. osThreadDef(PWM, PWMTask, osPriorityNormal, 0, 128);
  131. PWMHandle = osThreadCreate(osThread(PWM), NULL);
  132. /* definition and creation of Decode */
  133. osThreadDef(Decode, DecodeTask, osPriorityIdle, 0, 128);
  134. DecodeHandle = osThreadCreate(osThread(Decode), NULL);
  135. /* definition and creation of Led */
  136. osThreadDef(Led, StartTask03, osPriorityIdle, 0, 128);
  137. LedHandle = osThreadCreate(osThread(Led), NULL);
  138. /* USER CODE BEGIN RTOS_THREADS */
  139. /* add threads, ... */
  140. /* USER CODE END RTOS_THREADS */
  141. }
  142. /* USER CODE BEGIN Header_PWMTask */
  143. /**
  144. * @brief Function implementing the PWM thread.
  145. * @param argument: Not used
  146. * @retval None
  147. */
  148. /* USER CODE END Header_PWMTask */
  149. void PWMTask(void const * argument)
  150. {
  151. /* USER CODE BEGIN PWMTask */
  152. /* Infinite loop */
  153. uint16_t heure; //Heure en minute
  154. for (;;) {
  155. heure = HeureEnMinute();
  156. if (gl_Stop){
  157. if(gl_Essai == 0){
  158. for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
  159. uint8_t Status = newStatus(&tabSortie[i], heure);
  160. tabSortie[i].Status = Status;
  161. switch (Status) {
  162. case (ALLUME):
  163. GerePWM(tabSortie[i].OUT, tabSortie[i].PuissanceMax);
  164. break;
  165. case (ETEIN):
  166. GerePWM(tabSortie[i].OUT, 0);
  167. break;
  168. case (ENALLUME):
  169. case (ENETEIN):
  170. GerePWM(tabSortie[i].OUT,PWMPourcentage(&tabSortie[i], heure));
  171. break;
  172. default:
  173. //TODO Erreur;
  174. break;
  175. }
  176. }
  177. }
  178. else{// En essai
  179. for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
  180. GerePWM(tabSortie[i].OUT, tabSortie[i].PuissanceEssai);
  181. }
  182. }
  183. }
  184. else{//Stop
  185. for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
  186. GerePWM(tabSortie[i].OUT, 0);
  187. }
  188. }
  189. osDelay(50);
  190. }
  191. /* USER CODE END PWMTask */
  192. }
  193. /* USER CODE BEGIN Header_DecodeTask */
  194. /**
  195. * @brief Function implementing the Decode thread.
  196. * @param argument: Not used
  197. * @retval None
  198. */
  199. /* USER CODE END Header_DecodeTask */
  200. void DecodeTask(void const * argument)
  201. {
  202. /* USER CODE BEGIN DecodeTask */
  203. /* Infinite loop */
  204. initFIFO(&fifo);
  205. uint8_t mes[8];
  206. for (;;) {
  207. if (FIFO_nbRead(&fifo)!=0){
  208. ReadMes(&fifo,mes);
  209. parse(mes);
  210. }
  211. osDelay(10);
  212. }
  213. /* USER CODE END DecodeTask */
  214. }
  215. /* USER CODE BEGIN Header_StartTask03 */
  216. /**
  217. * @brief Function implementing the Led thread.
  218. * @param argument: Not used
  219. * @retval None
  220. */
  221. /* USER CODE END Header_StartTask03 */
  222. void StartTask03(void const * argument)
  223. {
  224. /* USER CODE BEGIN StartTask03 */
  225. /* Infinite loop */
  226. for(;;)
  227. {
  228. HAL_GPIO_TogglePin(LED_vert_GPIO_Port,LED_vert_Pin);
  229. osDelay(500);
  230. }
  231. /* USER CODE END StartTask03 */
  232. }
  233. /* Private application code --------------------------------------------------*/
  234. /* USER CODE BEGIN Application */
  235. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
  236. writeFifo(&fifo,RX_uart,1);
  237. HAL_UART_Receive_IT(&huart6,RX_uart,1);
  238. }
  239. /* USER CODE END Application */
  240. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/