SemaphoreHandle_t xSemaphoreCreateBinary( void )
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore )
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait );
myBinarySemHandle = xSemaphoreCreateBinary();
void StartTaskGive(void const * argument)
{
/* USER CODE BEGIN StartTaskGive */
/* Infinite loop */
for(;;)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
if(xSemaphoreGive(myBinarySemHandle) == pdTRUE)
printf("二值信号量放入成功\r\n");
else
printf("二值信号量放入失败\r\n");
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET);
}
osDelay(10);
}
/* USER CODE END StartTaskGive */
}
void StartTaskTake(void const * argument)
{
/* USER CODE BEGIN StartTaskTake */
/* Infinite loop */
for(;;)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
if(xSemaphoreTake(myBinarySemHandle, portMAX_DELAY) == pdTRUE)
printf("二值信号量获取成功\r\n");
else
printf("二值信号量获取失败\r\n");
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET);
}
osDelay(10);
}
/* USER CODE END StartTaskTake */
}
SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount);
myCountingSemHandle = xSemaphoreCreateCounting(3, 0);
void StartTaskGive(void const * argument)
{
/* USER CODE BEGIN StartTaskGive */
/* Infinite loop */
for(;;)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
if (xSemaphoreGive(myCountingSemHandle) == pdTRUE)
printf("计数信号量放入成功\r\n");
else
printf("计数信号量放入失败\r\n");
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET);
}
osDelay(10);
}
/* USER CODE END StartTaskGive */
}
void StartTaskTake(void const * argument)
{
/* USER CODE BEGIN StartTaskTake */
/* Infinite loop */
for(;;)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
if (xSemaphoreTake(myCountingSemHandle, 0 ) == pdTRUE)
printf("计数信号量获取成功\r\n");
else
printf("计数信号量获取失败\r\n");
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET);
}
osDelay(10);
}
/* USER CODE END StartTaskTake */
}
SemaphoreHandle_t xSemaphoreCreateMutex( void )
void StartTaskH(void const * argument)
{
/* USER CODE BEGIN StartTaskH */
/* Infinite loop */
for(;;)
{
xSemaphoreTake(myMutexHandle, portMAX_DELAY);
printf("TaskH: 我开始进入厕所,发功中。。。\r\n");
HAL_Delay(3000);
printf("TaskH: 我上完厕所了,真舒服。。。\r\n");
xSemaphoreGive(myMutexHandle);
osDelay(1000);
osDelay(1);
}
/* USER CODE END StartTaskH */
}
void StartTaskM(void const * argument)
{
/* USER CODE BEGIN StartTaskM */
/* Infinite loop */
for(;;)
{
printf("TaskM: 我就是为了占有CPU资源,带女朋友去兜风~~~\r\n");
osDelay(1000);
}
/* USER CODE END StartTaskM */
}
void StartTaskL(void const * argument)
{
/* USER CODE BEGIN StartTaskL */
/* Infinite loop */
for(;;)
{
xSemaphoreTake(myMutexHandle, portMAX_DELAY);//获取互斥量
printf("TaskL: 我开始进入厕所,发功中。。。\r\n");
HAL_Delay(3000);
printf("TaskL: 我上完厕所了,真舒服。。。\r\n");
xSemaphoreGive(myMutexHandle);//释放互斥量
osDelay(1);
}
/* USER CODE END StartTaskL */
}
EventGroupHandle_t xEventGroupCreate( void );
EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet );
EventBits_t xEventGroupClearBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear );
EventBits_t xEventGroupWaitBits(
const EventGroupHandle_t xEventGroup,
const EventBits_t uxBitsToWaitFor,
const BaseType_t xClearOnExit,
const BaseType_t xWaitForAllBits,
TickType_t xTicksToWait );
EventGroupHandle_t eventgroup_handle;
eventgroup_handle = xEventGroupCreate();
void StartTask1(void const * argument)
{
/* USER CODE BEGIN StartTask1 */
/* Infinite loop */
for(;;)
{
// 等待 KEY1 按下
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
xEventGroupSetBits(eventgroup_handle, 0x01);
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET);
}
// 等待 KEY2 按下
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
osDelay(20);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
xEventGroupSetBits(eventgroup_handle, 0x02);
}
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET);
}
osDelay(1);
}
/* USER CODE END StartTask1 */
}
void StartTask2(void const * argument)
{
/* USER CODE BEGIN StartTask2 */
EventBits_t event_bit =0;
/* Infinite loop */
for(;;)
{
event_bit = xEventGroupWaitBits(eventgroup_handle, 0x01 | 0x02, pdTRUE, pdFALSE, portMAX_DELAY);
printf("返回值:%#x,请假成功,可以去玩啦!\r\n", event_bit);
osDelay(1);
}
/* USER CODE END StartTask2 */
}
BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction );
BaseType_t xTaskNotifyAndQuery( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotifyValue );
BaseType_t xTaskNotifyGive( TaskHandle_t xTaskToNotify );
uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait );
BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait );
1、configUSE_TIMERS
2、configTIMER_TASK_PRIORITY
3、configTIMER_QUEUE_LENGTH
4、configTIMER_TASK_STACK_DEPTH
TimerHandle_t xTimerCreate
( const char * const pcTimerName,
const TickType_t xTimerPeriod,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction );
BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xBlockTime );
BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xBlockTime );
BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xBlockTime );
BaseType_t xTimerChangePeriod( TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xBlockTime );
#include "cmsis_os.h"
extern osMessageQId myQueue01Handle;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
uint32_t snd = 6;
xQueueSendFromISR(myQueue01Handle, &snd, NULL);
}
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN StartDefaultTask */
uint32_t rev = 0;
/* Infinite loop */
for(;;)
{
if (xQueueReceive(myQueue01Handle, &rev, portMAX_DELAY) == pdTRUE)
printf("rev = %d\r\n", rev);
osDelay(1);
}
/* USER CODE END StartDefaultTask */
}