用UCOS3写一个串口通信程序
时间: 2023-08-22 16:09:59 浏览: 171
以下是一个基于UCOS3的串口通信程序示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32f4xx.h"
#include "os.h"
#include "app_cfg.h"
#define UART_RX_BUF_SIZE 64
#define UART_TX_BUF_SIZE 64
// 串口接收缓冲区
static CPU_CHAR uart_rx_buf[UART_RX_BUF_SIZE];
// 串口发送缓冲区
static CPU_CHAR uart_tx_buf[UART_TX_BUF_SIZE];
// 串口接收信号量
static OS_SEM uart_rx_sem;
// 串口句柄
static USART_TypeDef *uart = USART2;
// 串口中断处理函数
void USART2_IRQHandler(void)
{
OSIntEnter();
if (USART_GetITStatus(uart, USART_IT_RXNE) != RESET)
{
// 读取接收到的数据
CPU_CHAR data = USART_ReceiveData(uart);
// 将数据存入接收缓冲区
OS_ERR err;
OSSemPend(&uart_rx_sem, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
uart_rx_buf[strlen(uart_rx_buf)] = data;
OSSemPost(&uart_rx_sem, OS_OPT_POST_1, &err);
}
OSIntExit();
}
// 初始化串口
void uart_init(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStruct);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART2, ENABLE);
OS_ERR err;
OSSemCreate(&uart_rx_sem, "UART RX Sem", 1, &err);
}
// 发送数据
void uart_send(CPU_CHAR *data)
{
CPU_INT08U len = strlen(data);
CPU_INT08U i;
for (i = 0; i < len; i++)
{
USART_SendData(uart, data[i]);
while (USART_GetFlagStatus(uart, USART_FLAG_TXE) == RESET);
}
}
// 接收数据
CPU_CHAR *uart_receive(void)
{
OS_ERR err;
OSSemPend(&uart_rx_sem, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
CPU_CHAR *data = uart_rx_buf;
uart_rx_buf[0] = '\0';
OSSemPost(&uart_rx_sem, OS_OPT_POST_1, &err);
return data;
}
// 串口任务
void uart_task(void *p_arg)
{
while (1)
{
// 接收并处理数据
CPU_CHAR *data = uart_receive();
printf("Received data: %s\n", data);
// 发送数据
uart_send("Hello, world!\n");
OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
int main(void)
{
OS_ERR err;
OSInit(&err);
uart_init();
OSTaskCreate((OS_TCB *)&AppTaskStartTCB, /* Create the start task */
(CPU_CHAR *)"Start Task",
(OS_TASK_PTR)AppTaskStart,
(void *)0,
(OS_PRIO)APP_CFG_TASK_START_PRIO,
(CPU_STK *)&AppTaskStartStk[0],
(CPU_STK_SIZE)APP_CFG_TASK_START_STK_SIZE_LIMIT,
(CPU_STK_SIZE)APP_CFG_TASK_START_STK_SIZE,
(OS_MSG_QTY)0u,
(OS_TICK)0u,
(void *)0,
(OS_OPT)(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSStart(&err);
while (1);
}
```
在该示例中,我们使用了USART2作为串口,并在串口中断处理函数中将接收到的数据存入接收缓冲区,并通过信号量进行同步。在串口任务中,我们通过调用uart_receive函数从接收缓冲区中获取数据,并通过调用uart_send函数发送数据到串口。
阅读全文