freertos.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. uint8_t power_set = 0;
  37. /* USER CODE END Includes */
  38. /* Private typedef -----------------------------------------------------------*/
  39. /* USER CODE BEGIN PTD */
  40. /* USER CODE END PTD */
  41. /* Private define ------------------------------------------------------------*/
  42. /* USER CODE BEGIN PD */
  43. /* USER CODE END PD */
  44. /* Private macro -------------------------------------------------------------*/
  45. /* USER CODE BEGIN PM */
  46. /* USER CODE END PM */
  47. /* Private variables ---------------------------------------------------------*/
  48. /* USER CODE BEGIN Variables */
  49. /* USER CODE END Variables */
  50. /* Definitions for defaultTask */
  51. osThreadId_t defaultTaskHandle;
  52. const osThreadAttr_t defaultTask_attributes = {
  53. .name = "defaultTask",
  54. .stack_size = 128 * 4,
  55. .priority = (osPriority_t) osPriorityRealtime,
  56. };
  57. /* Private function prototypes -----------------------------------------------*/
  58. /* USER CODE BEGIN FunctionPrototypes */
  59. //从机电源地址设置
  60. void constants_is_5_master(){
  61. uint8_t data1[2] = {0};
  62. data1[0] = get_reg_value(POWER_REG_ADDRESS);
  63. if (data1[0] == 0) {
  64. power_set++;
  65. }
  66. if ((power_set >= 20)||(data1[0] == 1)) {
  67. set_reg_value(SLAVE_POWER_ADDRESS, data1[0]);
  68. power_set = 0;
  69. }
  70. }
  71. /* USER CODE END FunctionPrototypes */
  72. void StartDefaultTask(void *argument);
  73. void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
  74. /**
  75. * @brief FreeRTOS initialization
  76. * @param None
  77. * @retval None
  78. */
  79. void MX_FREERTOS_Init(void) {
  80. /* USER CODE BEGIN Init */
  81. /* USER CODE END Init */
  82. /* USER CODE BEGIN RTOS_MUTEX */
  83. /* add mutexes, ... */
  84. /* USER CODE END RTOS_MUTEX */
  85. /* USER CODE BEGIN RTOS_SEMAPHORES */
  86. /* add semaphores, ... */
  87. /* USER CODE END RTOS_SEMAPHORES */
  88. /* USER CODE BEGIN RTOS_TIMERS */
  89. /* start timers, add new ones, ... */
  90. /* USER CODE END RTOS_TIMERS */
  91. /* USER CODE BEGIN RTOS_QUEUES */
  92. /* add queues, ... */
  93. /* USER CODE END RTOS_QUEUES */
  94. /* Create the thread(s) */
  95. /* creation of defaultTask */
  96. defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  97. /* USER CODE BEGIN RTOS_THREADS */
  98. /* add threads, ... */
  99. /* USER CODE END RTOS_THREADS */
  100. /* USER CODE BEGIN RTOS_EVENTS */
  101. /* add events, ... */
  102. /* USER CODE END RTOS_EVENTS */
  103. }
  104. /* USER CODE BEGIN Header_StartDefaultTask */
  105. /**
  106. * @brief Function implementing the defaultTask thread.
  107. * @param argument: Not used
  108. * @retval None
  109. */
  110. /* USER CODE END Header_StartDefaultTask */
  111. void StartDefaultTask(void *argument)
  112. {
  113. /* USER CODE BEGIN StartDefaultTask */
  114. /* Infinite loop */
  115. DEBUG_PRINTF("The system starts running.\r\n");
  116. uint8_t iwdg_count = 0;
  117. for (;;)
  118. {
  119. if ((get_reg_value(INNER_ERROR2_REG_ADDRESS) )){
  120. HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_RESET);
  121. } else {
  122. HAL_GPIO_WritePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin, GPIO_PIN_SET);
  123. }
  124. if (get_reg_value(INNER_ERROR1_REG_ADDRESS)){
  125. HAL_GPIO_WritePin(LED_LINK_GPIO_Port, LED_LINK_Pin, GPIO_PIN_RESET);
  126. } else {
  127. HAL_GPIO_WritePin(LED_LINK_GPIO_Port, LED_LINK_Pin, GPIO_PIN_SET);
  128. }
  129. HAL_GPIO_TogglePin(LED_STATE_GPIO_Port, LED_STATE_Pin);
  130. if(iwdg_count >= 10){
  131. HAL_IWDG_Refresh(&hiwdg); // 喂狗
  132. iwdg_count = 0;
  133. // DEBUG_PRINTF("IWDG feeding.\r\n");
  134. }
  135. iwdg_count++;
  136. constants_is_5_master();
  137. osDelay(600);
  138. }
  139. /* USER CODE END StartDefaultTask */
  140. }
  141. /* Private application code --------------------------------------------------*/
  142. /* USER CODE BEGIN Application */
  143. /* USER CODE END Application */