将AVR上的队列串口驱动程序修改后,运行在STM32开发板,采用中断方式接收和中断发送,并加入了缓冲收发队列操作。由于该驱动是用来操作西门子的TC35或MC55等通信模块,所以加入了“等待串口接收完成”函数,该函数需要一个10ms的定时进行计数累加。
#define SMS_UART0_c
/*
*************************************************************************************************************
*
* STM32 UART1 driver
*
* File : UART0.c
* By : hjjft
*************************************************************************************************************
*/
/////////////////////////////////////////////////
// 这里将串口1写作0,主要原因是AVR是串口0,为了方便移植,这里仍然称为串口0 //
/////////////////////////////////////////////////
static char UART0_RxBuf[UART0_RX_BUFFER_SIZE];
static volatile unsigned char UART0_RxHead;//
static volatile unsigned char UART0_RxTail;
static char UART0_TxBuf[UART0_TX_BUFFER_SIZE];//
static volatile unsigned char UART0_TxHead;
static volatile unsigned char UART0_TxTail;
//------------------------------------------------------------
static volatile unsigned char Frame_counting;
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_USART_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 10;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configures the USART1.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(unsigned long baudrate)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitqlt;
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitStructure.USART_BaudRate = baudrate;
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_ClockInitqlt.USART_Clock= USART_Clock_Disable;
USART_ClockInitqlt.USART_CPOL = USART_CPOL_Low; // USART_ClockInitqlt.USART_CPHA = USART_CPHA_2Edge; // USART_ClockInitqlt.USART_LastBit = USART_LastBit_Disable;//
USART_ClockInit(USART1,&USART_ClockInitqlt);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);//
}
//串口初始化
void UART0_InitUART( unsigned long baudrate )
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
NVIC_USART_Configuration();
GPIO_USART_Configuration();
USART_Configuration(baudrate);
UART0_RxTail = 0;
UART0_RxHead = 0;
UART0_TxTail = 0;
UART0_TxHead = 0;
}
//--------------------------------------------------
void SIGNAL_Usart0_RX(void) //接收中断
{
u8 data;
u8 tmphead;
data = USART_ReceiveData(USART1); //
Frame_counting = 0;
tmphead = ( UART0_RxHead + 1 ) & UART0_RX_BUFFER_MASK; // UART0_RxHead = tmphead; //
if ( tmphead == UART0_RxTail )
{
//这里加入队列溢出保护
}
UART0_RxBuf[tmphead] = data;
}
//--------------------------------------------------
void SIGNAL_Usart0_TX(void) //发送中断
{
u8 tmptail;
if ( UART0_TxHead != UART0_TxTail )
{
tmptail = ( UART0_TxTail + 1 ) & UART0_TX_BUFFER_MASK; UART0_TxTail = tmptail;
USART_SendData(USART1, UART0_TxBuf[tmptail]);
}
else
{
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);//
}
}
//从接收队列读取一个字符
unsigned char UART0_ReceiveByte( void )
{
unsigned char tmptail;
while ( UART0_RxHead == UART0_RxTail ); //
tmptail = ( UART0_RxTail + 1 ) & UART0_RX_BUFFER_MASK;
UART0_RxTail = tmptail;
return UART0_RxBuf[tmptail];
}
//将一个字节放入发送队列
void UART0_TransmitByte( char data )
{
unsigned char tmphead;
tmphead = ( UART0_TxHead + 1 ) & UART0_TX_BUFFER_MASK;
while ( tmphead == UART0_TxTail );
UART0_TxBuf[tmphead] = data;
UART0_TxHead = tmphead;
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//
}
//发送一个字符串
void UART0_Transmitstr( char *str) //
{
// unsigned int i="0";
while(*str)
{
UART0_TransmitByte(*str++);
}
}
//清空接收缓冲区
void UART0_ClearspatialReceiveBuffer(void)
{
unsigned char x;
x = 0;
UART0_RxTail = x;
UART0_RxHead = x;
}
//等待接收完成
void UART0_RXEND(void)
{
//UART0_Transmitstr("wite_start-");
unsigned int x="0";
while((!UART0_DataInReceiveBuffer())&(x<100))//
{
x++;
DelayMs(10);//10ms
}
x=0;
while((Frame_counting<2)&&(x<1000))//?D??êy?Y?óê?íê3é,×??à1sí?3?
{
x++;
DelayMs(1);//1ms
}
}
/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);//
SIGNAL_Usart0_RX();
}
if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE); //
SIGNAL_Usart0_TX();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
文章评论(0条评论)
登录后参与讨论