/* Includes ------------------------------------------------------------------*/ #include"FreeRTOS.h" #include"task.h" #include"main.h" #include"cmsis_os.h"
/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include"gpio.h" /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ extern UART_HandleTypeDef huart2; extern KEY_Status KEYStatus; /* USER CODE END Variables */ osThreadId defaultTaskHandle; osThreadId SerialTaskHandle; osThreadId keyTaskHandle;
/* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */ static StaticTask_t xIdleTaskTCBBuffer; static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
voidvApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize ) { *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer; *ppxIdleTaskStackBuffer = &xIdleStack[0]; *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE; /* place for user code */ } /* USER CODE END GET_IDLE_TASK_MEMORY */
/** * @brief FreeRTOS initialization * @param None * @retval None */ voidMX_FREERTOS_Init(void){ /* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */ /* definition and creation of defaultTask */ osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of SerialTask */ osThreadDef(SerialTask, StartSerialTask, osPriorityIdle, 0, 128); SerialTaskHandle = osThreadCreate(osThread(SerialTask), NULL);
/* definition and creation of keyTask */ osThreadDef(keyTask, StartTaskKEY, osPriorityIdle, 0, 128); keyTaskHandle = osThreadCreate(osThread(keyTask), NULL);
/* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartDefaultTask */ /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ voidStartDefaultTask(voidconst * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); osDelay(1000); } /* USER CODE END StartDefaultTask */ }
/* USER CODE BEGIN Header_StartSerialTask */ /** * @brief Function implementing the SerialTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartSerialTask */ voidStartSerialTask(voidconst * argument) { /* USER CODE BEGIN StartSerialTask */ /* Infinite loop */ uint8_t hello[] = "hello,stm32\n"; for(;;) { HAL_UART_Transmit(&huart2, hello, sizeof(hello), 100); osDelay(1000); } /* USER CODE END StartSerialTask */ }
/* USER CODE BEGIN Header_StartTaskKEY */ /** * @brief Function implementing the keyTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartTaskKEY */ voidStartTaskKEY(voidconst * argument) { /* USER CODE BEGIN StartTaskKEY */ /* Infinite loop */ for(;;) { if (KEYStatus == KEY_DOWN) { vTaskSuspend(defaultTaskHandle); // 挂起 led 任务 KEYStatus = KEY_UNDEFINED; // 按键状态置位为 未知状态 } elseif(KEYStatus == KEY_UP) { vTaskResume(defaultTaskHandle); // 恢复 led 任务 KEYStatus = KEY_UNDEFINED; // 按键状态置位为 未知状态 } osDelay(10); } /* USER CODE END StartTaskKEY */ }
/* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */
/* USER CODE END Application */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
gpio.h
1 2 3 4 5 6 7
/* USERCODE BEGIN Private defines */ typedef enum { KEY_DOWN, KEY_UP, KEY_UNDEFINED } KEY_Status; /* USERCODE END Private defines */
gpio.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* USER CODE BEGIN 0 */ KEY_Status KEYStatus = KEY_UNDEFINED; /* USER CODE END 0 */