目录
什么是信号量?
什么是二值信号量?
二值信号量相关 API 函数
1. 创建二值信号量
2. 释放二值信号量
3. 获取二值信号量
实操
实验需求
cubeMX配置编辑
代码实现
计数型信号量
什么是计数型信号量?
计数型信号量相关 API 函数
实操
实验需求
cubeMX配置
代码实现
函数
|
描述
|
xSemaphoreCreateBinary()
|
使用动态方式创建二值信号量
|
xSemaphoreCreateBinaryStatic()
|
使用静态方式创建二值信号量
|
xSemaphoreGive()
|
释放信号量
|
xSemaphoreGiveFromISR()
|
在中断中释放信号量
|
xSemaphoreTake()
|
获取信号量
|
xSemaphoreTakeFromISR()
|
在中断中获取信号量
|
SemaphoreHandle_t xSemaphoreCreateBinary ( void )
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore )
BaseType_t xSemaphoreTake ( SemaphoreHandle_t xSemaphore , TickType_t xTicksToWait );
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* Create the semaphores(s) */
/* definition and creation of myBinarySem */
osSemaphoreDef(myBinarySem);
//myBinarySemHandle = osSemaphoreCreate(osSemaphore(myBinarySem), 1);
myBinarySemHandle = xSemaphoreCreateBinary();
/* 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 taskGive */
osThreadDef(taskGive, StartTaskGive, osPriorityNormal, 0, 128);
taskGiveHandle = osThreadCreate(osThread(taskGive), NULL);
/* definition and creation of taskTake */
osThreadDef(taskTake, StartTaskTake, osPriorityNormal, 0, 128);
taskTakeHandle = osThreadCreate(osThread(taskTake), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartTaskGive */
/**
* @brief Function implementing the taskGive thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskGive */
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 */
}
/* USER CODE BEGIN Header_StartTaskTake */
/**
* @brief Function implementing the taskTake thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskTake */
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 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */
函数
|
描述 |
xSemaphoreCreateCounting()
|
使用动态方法创建计数型信号量。
|
xSemaphoreCreateCountingStatic()
|
使用静态方法创建计数型信号量
|
uxSemaphoreGetCount()
|
获取信号量的计数值
|
SemaphoreHandle_t xSemaphoreCreateCounting ( UBaseType_t uxMaxCount ,UBaseType_t uxInitialCount );
将 Config parameters 标签里的 USE_COUNTING_SEMAPHORES 设置为 Enabled 。
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* Create the semaphores(s) */
/* definition and creation of myCountingSem */
osSemaphoreDef(myCountingSem);
// myCountingSemHandle = osSemaphoreCreate(osSemaphore(myCountingSem), 3);
myCountingSemHandle = xSemaphoreCreateCounting(3, 0);
/* 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 taskGive */
osThreadDef(taskGive, StartTaskGive, osPriorityNormal, 0, 128);
taskGiveHandle = osThreadCreate(osThread(taskGive), NULL);
/* definition and creation of taskTake */
osThreadDef(taskTake, StartTaskTake, osPriorityNormal, 0, 128);
taskTakeHandle = osThreadCreate(osThread(taskTake), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartTaskGive */
/**
* @brief Function implementing the taskGive thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskGive */
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 */
}
}
/* USER CODE BEGIN Header_StartTaskTake */
/**
* @brief Function implementing the taskTake thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskTake */
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 */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */