二值信号量
信号量简介
目的:共享资源访问、与任务同步
信号量类型:二值信号量、计数型信号量、互斥信号量、递归互斥信号量
本质上是一种只包含一个项数的队列
二值信号量
0 和 1,一种内核机制。 内核同步,资源共享。
极简例子
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);
#define LED0_TASK_PRIO 2
#define LED0_STK_SIZE 50
TaskHandle_t LED0Task_Handler;
void led0_task(void *pvParameters);
#define LED1_TASK_PRIO 3
#define LED1_STK_SIZE 50
TaskHandle_t LED1Task_Handler;
void led1_task(void *pvParameters);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
delay_init();
uart_init(115200);
LED_Init();
xTaskCreate((TaskFunction_t )start_task,
(const char* )"start_task",
(uint16_t )START_STK_SIZE,
(void* )NULL,
(UBaseType_t )START_TASK_PRIO,
(TaskHandle_t* )&StartTask_Handler);
vTaskStartScheduler();
}
xSemaphoreHandle Sempore_Bin = NULL;
void start_task(void *pvParameters)
{
taskENTER_CRITICAL();
Sempore_Bin = xSemaphoreCreateBinary();
if(Sempore_Bin != NULL) printf("创建成功\r\n");
else printf("创建失败\r\n");
xTaskCreate((TaskFunction_t )led0_task,
(const char* )"led0_task",
(uint16_t )LED0_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED0_TASK_PRIO,
(TaskHandle_t* )&LED0Task_Handler);
xTaskCreate((TaskFunction_t )led1_task,
(const char* )"led1_task",
(uint16_t )LED1_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED1_TASK_PRIO,
(TaskHandle_t* )&LED1Task_Handler);
vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}
void led0_task(void *pvParameters)
{
u8 key= 0;
while(1){
printf("%d\r\n",key);
key++;
if(key == 10){
if(Sempore_Bin!=NULL&&key==10){
if (xSemaphoreGive(Sempore_Bin)==pdTRUE)
printf("释放成功\r\n");
else
printf("释放失败\r\n");
}
}
vTaskDelay(1000);
}
}
void led1_task(void *pvParameters)
{
while(1){
if(xSemaphoreTake(Sempore_Bin, 10)== pdTRUE)
printf("获取成功\r\n");
else
printf("获取失败\r\n");
vTaskDelay(1000);
}
}
中断与二值信号量实验
主要函数
if (xSemaphoreGiveFromISR(Sempore_Bin, &xHigherPrioityTaskWoken)==pdTRUE)
if(xSemaphoreTake(Sempore_Bin, 10)== pdTRUE){}
delay_xms(10);
主要代码
void EXTIX_Init(void){
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
KEY_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x06;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
extern xSemaphoreHandle Sempore_Bin;
void EXTI0_IRQHandler(void)
{
delay_xms(10);
BaseType_t xHigherPrioityTaskWoken;
if(WK_UP==1)
{
printf("按下按键\r\n");
if(Sempore_Bin!=NULL){
if (xSemaphoreGiveFromISR(Sempore_Bin, &xHigherPrioityTaskWoken)==pdTRUE)
printf("释放成功\r\n");
else
printf("释放失败\r\n");
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void led0_task(void *pvParameters)
{
u8 key_sum= 0;
while(1){
if(xSemaphoreTake(Sempore_Bin, 10)== pdTRUE){
printf("接受成功\r\n");
key_sum++;
printf("按键被按下%d次",key_sum);
}
else{
}
vTaskDelay(10);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)