freertos.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "PWM.h"
  58. #include "RTC.h"
  59. #include "Tab.h"
  60. /* USER CODE END Includes */
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* USER CODE BEGIN PTD */
  63. /* USER CODE END PTD */
  64. /* Private define ------------------------------------------------------------*/
  65. /* USER CODE BEGIN PD */
  66. /* USER CODE END PD */
  67. /* Private macro -------------------------------------------------------------*/
  68. /* USER CODE BEGIN PM */
  69. /* USER CODE END PM */
  70. /* Private variables ---------------------------------------------------------*/
  71. /* USER CODE BEGIN Variables */
  72. uint8_t Arret = 0;
  73. /* USER CODE END Variables */
  74. osThreadId TimerHandle;
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* USER CODE BEGIN FunctionPrototypes */
  77. void init();
  78. uint16_t minimum(uint16_t a,uint16_t b);
  79. /* USER CODE END FunctionPrototypes */
  80. void TimerTask(void const * argument);
  81. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  82. /**
  83. * @brief FreeRTOS initialization
  84. * @param None
  85. * @retval None
  86. */
  87. void MX_FREERTOS_Init(void) {
  88. /* USER CODE BEGIN Init */
  89. init();
  90. /* USER CODE END Init */
  91. /* USER CODE BEGIN RTOS_MUTEX */
  92. /* add mutexes, ... */
  93. /* USER CODE END RTOS_MUTEX */
  94. /* USER CODE BEGIN RTOS_SEMAPHORES */
  95. /* add semaphores, ... */
  96. /* USER CODE END RTOS_SEMAPHORES */
  97. /* USER CODE BEGIN RTOS_TIMERS */
  98. /* start timers, add new ones, ... */
  99. /* USER CODE END RTOS_TIMERS */
  100. /* Create the thread(s) */
  101. /* definition and creation of Timer */
  102. osThreadDef(Timer, TimerTask, osPriorityNormal, 0, 128);
  103. TimerHandle = osThreadCreate(osThread(Timer), NULL);
  104. /* USER CODE BEGIN RTOS_THREADS */
  105. /* add threads, ... */
  106. /* USER CODE END RTOS_THREADS */
  107. /* USER CODE BEGIN RTOS_QUEUES */
  108. /* add queues, ... */
  109. /* USER CODE END RTOS_QUEUES */
  110. }
  111. /* USER CODE BEGIN Header_TimerTask */
  112. /**
  113. * @brief Function implementing the Timer thread.
  114. * @param argument: Not used
  115. * @retval None
  116. */
  117. /* USER CODE END Header_TimerTask */
  118. void TimerTask(void const * argument) {
  119. /* USER CODE BEGIN TimerTask */
  120. /* Infinite loop */
  121. for (;;) {
  122. uint8_t heu = readHeure();
  123. if (heu == 0)
  124. Arret = 0;
  125. uint16_t value = PWMvalue(readMinute());
  126. for (int i = 1; i < 11; i++) {
  127. uint8_t val = readCase(i, heu);
  128. switch (val) {
  129. case ALLUME:
  130. PWMSetMAX(i);
  131. break;
  132. case ENALLUMAGE:
  133. PWMSetDutyCycle(i, value);
  134. break;
  135. case ENEXTINXION:
  136. if (Arret==1)// Si on a eu un probleme d'extinction on est déja arreté on ne reprend pas
  137. PWMSetZero(i);
  138. else
  139. PWMSetDutyCycle(i, PWM_MAX - value);
  140. break;
  141. default: //ETEIN
  142. PWMSetZero(i);
  143. break;
  144. }
  145. }
  146. osDelay(DELAYTIMER);
  147. }
  148. /* USER CODE END TimerTask */
  149. }
  150. /* Private application code --------------------------------------------------*/
  151. /* USER CODE BEGIN Application */
  152. void init(){ // si il y a une coupure on rallume la lumiere progrésivement
  153. Date ancienDate;
  154. Date newDate;
  155. readDate(&ancienDate);
  156. redemareRTC();
  157. readDate(&newDate);
  158. for(uint8_t time = 0;time<10;time++){ // on fait 10 cycle d'une minute chaqu'un.
  159. uint16_t value= PWMvalue(readMinute()); // valeur du PWM normal si allumage
  160. uint16_t valPWMRapide = PWM_MAX/(time+1); // valeur du PWM en allumage rapide
  161. for (int i = 1; i < 11; i++) {// on parcours toute les sortie
  162. uint8_t val = readCase(i, newDate.heure);
  163. switch (val) {
  164. case ALLUME: // si la sortie doit etre a 1 on allume progressivement 10min jusqu'a 1
  165. PWMSetDutyCycle(i, valPWMRapide);
  166. break;
  167. case ENALLUMAGE: // si la sortie doit etre a ENALLUMAGE on allume progressivement 10min jusqu'a la valeurs qu'elle devrais se trouvé
  168. PWMSetDutyCycle(i,minimum(value,valPWMRapide) );
  169. break;
  170. case ENEXTINXION:
  171. /*
  172. * Si on est en mode extinxtion on ne reprend pas.
  173. * On doit donc mettre une variable a 1 pour quand l'on repasse
  174. * Cette variable sera remis a zero a minuit.
  175. */
  176. Arret = 1;
  177. PWMSetZero(i);
  178. break;
  179. default: //ETEIN
  180. PWMSetZero(i);
  181. break;
  182. }
  183. }
  184. osDelay(DELAYTIMER);
  185. }
  186. }
  187. uint16_t minimum(uint16_t a,uint16_t b){
  188. if (a>b)
  189. return b;
  190. else
  191. return a;
  192. }
  193. /* USER CODE END Application */
  194. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/