freertos.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : freertos.c
  5. * Description : Code for freertos applications
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "FreeRTOS.h"
  21. #include "task.h"
  22. #include "main.h"
  23. #include "cmsis_os.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "event_groups.h"
  27. #include <stdio.h>
  28. #include "iwdg.h"
  29. #include "modbus_slave.h"
  30. #define LED_STATE_GPIO_Port GPIOC
  31. #define LED_STATE_Pin GPIO_PIN_13
  32. #define LED_ERROR_GPIO_Port GPIOB
  33. #define LED_ERROR_Pin GPIO_PIN_12
  34. #define LED_LINK_GPIO_Port GPIOB
  35. #define LED_LINK_Pin GPIO_PIN_13
  36. /* USER CODE END Includes */
  37. /* Private typedef -----------------------------------------------------------*/
  38. /* USER CODE BEGIN PTD */
  39. /* USER CODE END PTD */
  40. /* Private define ------------------------------------------------------------*/
  41. /* USER CODE BEGIN PD */
  42. /* USER CODE END PD */
  43. /* Private macro -------------------------------------------------------------*/
  44. /* USER CODE BEGIN PM */
  45. /* USER CODE END PM */
  46. /* Private variables ---------------------------------------------------------*/
  47. /* USER CODE BEGIN Variables */
  48. /* USER CODE END Variables */
  49. /* Definitions for defaultTask */
  50. osThreadId_t defaultTaskHandle;
  51. const osThreadAttr_t defaultTask_attributes = {
  52. .name = "defaultTask",
  53. .stack_size = 128 * 4,
  54. .priority = (osPriority_t) osPriorityRealtime,
  55. };
  56. /* Private function prototypes -----------------------------------------------*/
  57. /* USER CODE BEGIN FunctionPrototypes */
  58. /* USER CODE END FunctionPrototypes */
  59. void StartDefaultTask(void *argument);
  60. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  61. /**
  62. * @brief FreeRTOS initialization
  63. * @param None
  64. * @retval None
  65. */
  66. void MX_FREERTOS_Init(void) {
  67. /* USER CODE BEGIN Init */
  68. /* USER CODE END Init */
  69. /* USER CODE BEGIN RTOS_MUTEX */
  70. /* add mutexes, ... */
  71. /* USER CODE END RTOS_MUTEX */
  72. /* USER CODE BEGIN RTOS_SEMAPHORES */
  73. /* add semaphores, ... */
  74. /* USER CODE END RTOS_SEMAPHORES */
  75. /* USER CODE BEGIN RTOS_TIMERS */
  76. /* start timers, add new ones, ... */
  77. /* USER CODE END RTOS_TIMERS */
  78. /* USER CODE BEGIN RTOS_QUEUES */
  79. /* add queues, ... */
  80. /* USER CODE END RTOS_QUEUES */
  81. /* Create the thread(s) */
  82. /* creation of defaultTask */
  83. defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  84. /* USER CODE BEGIN RTOS_THREADS */
  85. /* add threads, ... */
  86. /* USER CODE END RTOS_THREADS */
  87. /* USER CODE BEGIN RTOS_EVENTS */
  88. /* add events, ... */
  89. /* USER CODE END RTOS_EVENTS */
  90. }
  91. /* USER CODE BEGIN Header_StartDefaultTask */
  92. /**
  93. * @brief Function implementing the defaultTask thread.
  94. * @param argument: Not used
  95. * @retval None
  96. */
  97. /* USER CODE END Header_StartDefaultTask */
  98. void StartDefaultTask(void *argument)
  99. {
  100. /* USER CODE BEGIN StartDefaultTask */
  101. /* Infinite loop */
  102. DEBUG_PRINTF("The system starts running.\r\n");
  103. uint8_t iwdg_count = 0;
  104. for (;;)
  105. {
  106. if ((get_reg_value(INNER_ERROR2_REG_ADDRESS) )){
  107. HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_RESET);
  108. } else {
  109. HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
  110. }
  111. if (get_reg_value(INNER_ERROR1_REG_ADDRESS)){
  112. HAL_GPIO_WritePin(LED_LINK_GPIO_Port, LED_LINK_Pin, GPIO_PIN_RESET);
  113. } else {
  114. HAL_GPIO_WritePin(LED_LINK_GPIO_Port, LED_LINK_Pin, GPIO_PIN_SET);
  115. }
  116. HAL_GPIO_TogglePin(LED_STATE_GPIO_Port, LED_STATE_Pin);
  117. if(iwdg_count >= 10){
  118. HAL_IWDG_Refresh(&hiwdg); // 喂狗
  119. iwdg_count = 0;
  120. // DEBUG_PRINTF("IWDG feeding.\r\n");
  121. }
  122. iwdg_count++;
  123. osDelay(600);
  124. }
  125. /* USER CODE END StartDefaultTask */
  126. }
  127. /* Private application code --------------------------------------------------*/
  128. /* USER CODE BEGIN Application */
  129. /* USER CODE END Application */