FreeRTOS学习笔记04-二值信号量

原文链接:
https://blog.csdn.net/xukai871105/article/details/43153177

1 前言

在嵌入式操作系统中二值型信号量是任务间、任务与中断间同步的重要手段。FreeRTOS的二值型信号量简单易用,下面结合一个具体例子说明FreeRTOS中的二值型信号量如何使用。

2 特别说明

二值型信号量的使用方法见图1所示,二值型信号量可以理解为任务与中断间或者两个任务间的标志,该标志非“满”即“空”。Send操作相当把该标志置“满”,Receive操作相关与把该标志取"空",经过send和receive操作实现任务与中断间或者两任务的操作同步。
在这里插入图片描述
图1 二值型信号量使用示意图
【特别说明】
V7.X版本和V8.X的信号量操作存在少许区别
V7.X版本中使用 vSemaphoreCreateBinary函数,使用该函数创建的信号量初始值为“满”,receive操作立刻有返回。相关代码见文章末尾补充代码1,从补充代码1中可以发现,创建信号量之后立刻调用xSemaphoreGive函数,使得信号量由“空”变“满”。
V8.X版本中使用xSemaphoreCreateBinary函数,使用该函数创建的信号量初始值为“空”,receive操作不会立刻返回。

3 参考代码

示例代码具有一个128字节的串口接收缓冲区,在串口中断中把接收到的字符存入缓冲区中,一旦接收到回车换行符(\r\n),便通过xSemaphoreGiveFromISR把信号量置“满”,打印任务中使用xSemaphoreTake实现于中断接收函数的同步, xSemaphoreTake把任务挂起,一旦查询到信号量为“满”,通过串口打印结束到的内容,并清空缓冲区。
【示例代码】

/* Standard includes. */
#include <stdio.h>
#include <string.h>
 
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
 
/* Library includes. */
#include "stm32f10x.h"
 
#define LED0_ON()   GPIO_SetBits(GPIOB,GPIO_Pin_5);
#define LED0_OFF()  GPIO_ResetBits(GPIOB,GPIO_Pin_5);
 
static void Setup(void);
static void PrintTask(void *pvParameters);
 
void LedInit(void);
void UART1Init(void);
 
uint8_t RxBuffer[128];
__IO uint8_t RxCounter = 0;
 
SemaphoreHandle_t xSemaphore;
 
int main(void)
{
    /* 初始化硬件平台 */
    Setup();
    
    /* 创建信号量 */
    xSemaphore = xSemaphoreCreateBinary();
    /* 建立Print任务 */
    xTaskCreate(PrintTask, "Print Task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+4, NULL);
    /* 启动OS */
    vTaskStartScheduler();
    
    return 0;
}
 
void PrintTask(void *pvParameters)
{
    for(;;)
    {
        if( xSemaphoreTake( xSemaphore, portMAX_DELAY ) == pdTRUE )
        {
            printf("receive:%s", RxBuffer);
            memset(RxBuffer, 0x00, 128);
            RxCounter = 0;
        }
    }
}
 
static void Setup( void )
{
    LedInit();
    UART1Init();
}
 
void LedInit( void )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
    /*LED0 @ GPIOB.5*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init( GPIOB, &GPIO_InitStructure );    
}
 
void UART1Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    
    /* 第1步:打开GPIO和USART时钟 */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
    
    /* 第2步:将USART1 Tx@PA9的GPIO配置为推挽复用模式 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    /* 第3步:将USART1 Rx@PA10的GPIO配置为浮空输入模式 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    /* 第4步:配置USART1参数
    波特率   = 9600
    数据长度 = 8
    停止位   = 1
    校验位   = No
    禁止硬件流控(即禁止RTS和CTS)
    使能接收和发送
    */
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
    
    /* 第5步:使能 USART1, 配置完毕 */
    USART_Cmd(USART1, ENABLE);
    
    /* 清除发送完成标志 */
    USART_ClearFlag(USART1, USART_FLAG_TC);
    
    /* 使能USART1发送中断和接收中断,并设置优先级 */
    NVIC_InitTypeDef NVIC_InitStructure;
    /* 设定USART1 中断优先级 */
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    /* 使能接收中断 */
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
}
 
int fputc(int ch, FILE *f)
{
    /* 写一个字节到USART1 */
    USART_SendData(USART1, (uint8_t) ch);
    /* 等待发送结束 */
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
    {}
    return ch;
}
 
void USART1_IRQHandler(void)
{
    static BaseType_t xHigherPriorityTaskWoken;
    
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
        if (RxCounter > 2 && RxBuffer[RxCounter-2] == '\r' && RxBuffer[RxCounter-1] == '\n') {
            // 在中断中发送信号量
            xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
        }
    }
    
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}

4 简单说明

SemaphoreHandle_t xSemaphore;
信号量句柄,二值型信号量、数值型信号量和互斥型信号量均使用 SemaphoreHandle_t类型声明
xSemaphore = xSemaphoreCreateBinary();
创建信号量,V8.X版本中新增加函数,创建信号量时初值为“空”。
xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
在中断中发送信号量,以FromISR结尾的函数具有保护功能,如果在任务中发送信号量可使用xSemaphoreGive。
xSemaphoreTake( xSemaphore, portMAX_DELAY );
等待信号量,等待时间为最大等待时间,如果信号量为“空”任务会处于挂起状态。

5 在中断中使用RTOS API注意点

【FromISR】
应使用xSemaphoreGiveFromISR,而不是 xSemaphoreGive。
【中断优先级设置】
串口中断的优先级应该低于configMAX_SYSCALL_INTERRUPT_PRIORITY(191,从另一个角度可以理解为11)设置的最高优先级,本文UART的响应优先级为configLIBRARY_KERNEL_INTERRUPT_PRIORITY(该宏的具体值为15,数值越大优先级越低)。

【main.c】
/* 使能USART1发送中断和接收中断,并设置优先级 /
NVIC_InitTypeDef NVIC_InitStructure;
/
设定USART1 中断优先级 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

【FreeRTOSConfig.h】

/* This is the raw value as per the Cortex-M3 NVIC. Values can be 255
(lowest) to 0 (1?) (highest). /
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191 /
equivalent to 0xb0, or priority 11. /
/
This is the value being used as per the ST library which permits 16
priority values, 0 to 15. This must correspond to the
configKERNEL_INTERRUPT_PRIORITY setting. Here 15 corresponds to the lowest
NVIC value of 255. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY 15

鉴于Cortex M3的NVIC的特性,请详细参考——【 在Cortex M3平台上运行FreeRTOS】

/* FreeRTOS kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "semphr.h"

/* Freescale includes. */
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"

#include "pin_mux.h"
#include "clock_config.h"
/*******************************************************************************
 * Definitions
 ******************************************************************************/
SemaphoreHandle_t valveSemaphore;

/* Task priorities. */
#define hello_task_PRIORITY (configMAX_PRIORITIES - 1)
#define TASK1_PRIORITY  3 //任务优先级
#define TASK1_STK_SIZE  256 //任务堆栈大小
TaskHandle_t Task1_Handle; //任务句柄
static void vTask1(void *pvParameters); //Task1任务函数

#define TASK2_PRIORITY  2 //任务优先级
#define TASK2_STK_SIZE  256 //任务堆栈大小
TaskHandle_t Task2_Handle; //任务句柄
static void vTask2(void *pvParameters); //Task1任务函数

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
static void hello_task(void *pvParameters); //hello_task任务函数
TaskHandle_t Hello_Task_Handle; //任务句柄

/*******************************************************************************
 * Code
 ******************************************************************************/

/*!
 * @brief Application entry point.
 */
int main(void)
{
    /* Init board hardware. */
    gpio_pin_config_t led_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};

    BOARD_ConfigMPU();
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    /* Set systick reload value to generate 1ms interrupt */
    if(SysTick_Config(SystemCoreClock / 1000U))
    {
        while(1)
        {
        }
    }

    /* Init output LED GPIO. */
    GPIO_PinInit(BOARD_USER_VALVE1_GPIO, BOARD_USER_VALVE1_GPIO_PIN, &led_config);

    /* 创建二值信号量 */
    valveSemaphore = xSemaphoreCreateBinary();

    //创建任务
    if (xTaskCreate(hello_task, "Hello_task", configMINIMAL_STACK_SIZE + 10, NULL, hello_task_PRIORITY, &Hello_Task_Handle) != pdPASS)
    {
        PRINTF("Hello_task creation failed!.\r\n");
        while (1)
            ;
    }
    if (xTaskCreate(vTask1, "vTask1", TASK1_STK_SIZE, NULL, TASK1_PRIORITY, &Task1_Handle) != pdPASS)
    {
        PRINTF("Task1 creation failed!.\r\n");
    }

    if (xTaskCreate(vTask2, "vTask2", TASK2_STK_SIZE, NULL, TASK2_PRIORITY, &Task2_Handle) != pdPASS)
    {
        PRINTF("Task1 creation failed!.\r\n");
    }

    //启动OS
    vTaskStartScheduler();
    for (;;)
        ;
}

/*!
 * @brief Task responsible for printing of "Hello world." message.
 */
static void hello_task(void *pvParameters)
{
    static bool bFlag = false;

    for (;;)
    {
        PRINTF("hello_task running $$$$$$$$$$$$$$$$$$$$$$$$$$$.\r\n");
        vTaskDelay(2000);
        if(false == bFlag)
        {
            PRINTF("hello_task will suspend.\r\n");
            vTaskSuspend(NULL);
            bFlag = true;
        }
    }
}

static void vTask1(void *pvParameters) //Task1任务函数
{
    static float value = 10.0022;

    for (;;)
    {
        if( xSemaphoreTake(valveSemaphore, portMAX_DELAY) == pdTRUE)
        {
            PRINTF("vTask1 is running. ====1=====1====1===");
            printf("%.4f\r\n",value);
            //PRINTF("%.4f\n",value);
            value = value * 1.01;
            vTaskDelay(1000);
            GPIO_PinWrite(BOARD_USER_VALVE1_GPIO, BOARD_USER_VALVE1_GPIO_PIN, 1U);
            vTaskDelay(1000);
            GPIO_PinWrite(BOARD_USER_VALVE1_GPIO, BOARD_USER_VALVE1_GPIO_PIN, 0U);
        }
    }

}


static void vTask2(void *pvParameters) //Task1任务函数
{
    BaseType_t xReturn = pdPASS;/* 定义一个创建信息返回值,默认为 pdPASS */
    static int value = 1;

    for (;;)
    {
        PRINTF("vTask2 is running. ******2******2******2******");
        printf("%d\r\n",value);
        value++;
        if( value > 60)
        {
            vTaskResume(Hello_Task_Handle); //恢复Hello_Task
        }
        if( value % 10 == 0)
        {
            xReturn = xSemaphoreGive(valveSemaphore);

            if( pdTRUE == xReturn )
            {
                PRINTF("Receive:Task1_Handle任务获得信号量!\r\n");
            }
        }
        vTaskDelay(100);
    }
}

6 总结

【1】V8.X中使用 xSemaphoreCreateBinary() 创建的信号量初始值为"空"。
【2】中断中发送信号量尽量使用XXXXFromISR。
【3】某中断的优先级数值应大于configMAX_SYSCALL_INTERRUPT_PRIORITY。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值