208 lines
4.3 KiB
C
208 lines
4.3 KiB
C
#include "bsp_usart6.h"
|
|
#include "global.h"
|
|
#include "irq_vector.h"
|
|
|
|
|
|
|
|
|
|
#include "rthw.h"
|
|
#define IRQ_DISABLE() rt_base_t irq_stat=rt_hw_interrupt_disable( )
|
|
#define IRQ_ENABLE() rt_hw_interrupt_enable (irq_stat)
|
|
|
|
|
|
|
|
|
|
static struct rt_semaphore g_uart={0};
|
|
static rt_err_t g_uart_err=RT_ERROR;
|
|
|
|
|
|
|
|
//#define UART_SEM_INIT() g_uart_err=rt_sem_init(&g_uart,"g_uart",0,RT_IPC_FLAG_PRIO)
|
|
|
|
//#define UART_ENTER_USED() {rt_sem_take (&g_uart,RT_WAITING_FOREVER);}
|
|
//#define UART_EXIT_USED() {rt_sem_release (&g_uart);}
|
|
|
|
#define UART_SEM_INIT()
|
|
|
|
#define UART_ENTER_USED()
|
|
#define UART_EXIT_USED()
|
|
|
|
|
|
|
|
|
|
|
|
static usart_recv_buff g_usart6_recv_buff={0};
|
|
static int g_data_recv_num=0;
|
|
|
|
void usart1_irqhandler (void);
|
|
|
|
void bsp_usart6_init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
|
|
|
|
/* 使能 UART 时钟 */
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
|
|
/* 连接 PXx 到 USARTx_Tx*/
|
|
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9, GPIO_AF_USART1);
|
|
|
|
/* 连接 PXx 到 USARTx__Rx*/
|
|
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
|
|
|
|
/* 配置Tx引脚为复用功能 */
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* 配置Rx引脚为复用功能 */
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
/* 配置串DEBUG_USART 模式 */
|
|
USART_DeInit(USART1);
|
|
USART_InitStructure.USART_BaudRate = 115200;
|
|
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);
|
|
USART_Cmd(USART1, ENABLE);
|
|
|
|
|
|
irq_vector_set_irq(USART1_IRQn,usart1_irqhandler);
|
|
|
|
/* 打开接收中断 */
|
|
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //定时器中断
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //子优先级3
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
g_usart6_recv_buff.buff_len=USART_RECV_BUFF_LEN;
|
|
|
|
UART_SEM_INIT();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int buff_save_byte(usart_recv_buff *buff,uint8_t data)
|
|
{
|
|
int ret=-1;
|
|
|
|
IRQ_DISABLE();
|
|
if(data==0xff) buff->active=1;
|
|
if ((buff->buff_used<buff->buff_len)&&buff->active)
|
|
{
|
|
buff->buff[buff->save_ptr]=data;
|
|
buff->buff_used++;
|
|
if(data==0xfc) {
|
|
buff->frem_num++;
|
|
buff->active=0;
|
|
}
|
|
buff->save_ptr++;
|
|
if (buff->save_ptr>=buff->buff_len)
|
|
buff->save_ptr=0;
|
|
ret= 0;
|
|
}
|
|
IRQ_ENABLE();
|
|
return ret;
|
|
}
|
|
|
|
static int buff_read_byte(usart_recv_buff *buff,uint8_t *data)
|
|
{
|
|
int ret=-1;
|
|
IRQ_DISABLE();
|
|
if ((buff->frem_num)&&(buff->buff_used))
|
|
{
|
|
*data=buff->buff[buff->read_ptr];
|
|
buff->buff_used--;
|
|
if(*data==0xfc) buff->frem_num--;
|
|
buff->read_ptr++;
|
|
if (buff->read_ptr>=buff->buff_len)
|
|
buff->read_ptr=0;
|
|
ret= 0;
|
|
}
|
|
IRQ_ENABLE();
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
int bsp_usart6_get_byte(uint8_t *data)
|
|
{
|
|
return buff_read_byte(&g_usart6_recv_buff,data);
|
|
}
|
|
|
|
|
|
int bsp_usart6_put_byte(uint8_t data)
|
|
{
|
|
/* 发送一个字节数据到串口DEBUG_USART */
|
|
USART_SendData(USART1, data);
|
|
|
|
/* 等待发送完毕 */
|
|
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int bsp_usart6_clear(void)
|
|
{
|
|
IRQ_DISABLE();
|
|
g_usart6_recv_buff.buff_used=0;
|
|
g_usart6_recv_buff.read_ptr=0;
|
|
g_usart6_recv_buff.save_ptr=0;
|
|
g_usart6_recv_buff.frem_num=0;
|
|
g_usart6_recv_buff.active=0;
|
|
IRQ_ENABLE();
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
void usart1_irqhandler (void)
|
|
{
|
|
CALL_BACK_ENTER();
|
|
uint8_t t=0;
|
|
// if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
|
|
if(USART1->SR&USART_FLAG_RXNE)
|
|
{
|
|
//t=USART_ReceiveData(USART1);
|
|
t=USART1->DR;
|
|
buff_save_byte(&g_usart6_recv_buff,t);
|
|
g_data_recv_num++;
|
|
}
|
|
else
|
|
{
|
|
t=USART1->SR;
|
|
t=USART1->DR;
|
|
}
|
|
CALL_BACK_EXIT();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|