1.前言
/* 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 ); }
#define vSemaphoreCreateBinary( xSemaphore ) { ( xSemaphore ) = xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); if( ( xSemaphore ) != NULL ) { ( void ) xSemaphoreGive( ( xSemaphore ) ); } }
原文地址:http://blog.csdn.net/xukai871105/article/details/43153177