📄 uart0_drv.c
字号:
//----------------------------------------------------------------------------
// Filename : uart0_drv.c
//----------------------------------------------------------------------------
//
// Copyright (c) 2008,东莞太平计算机科技有限公司
// All rights reserved.
// www.pacific-gold.com.cn
//
// 历史版本:
//
// 版本: V1.0
// 作者: 罗先能
// 日期: 2007-12-21
// 描述: 建立第一版本
//
//
// 描述:
// 1. 串行通讯口UART0的驱动程序,
// 2. 波特率 57.6kbps, 1位起始位+8bit+1位停止位,
// 3. 接收缓冲区 128字节; 发送缓冲区 128字节。
//
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------
// system headers
#include "71x_lib.h"
// internal headers
#include "uart0_drv.h"
// extern headers
#include "uart.h"
//----------------------------------------------------------------------------
// Local Constant Definitions
//----------------------------------------------------------------------------
#define UART0_Rx_Pin (0x0001<<8)
#define UART0_Tx_Pin (0x0001<<9)
//#define UART1_Rx_Pin (0x0001<<10)
//#define UART1_Tx_Pin (0x0001<<11)
//#define UART2_Rx_Pin (0x0001<<13)
//#define UART2_Tx_Pin (0x0001<<14)
//#define UART3_Rx_Pin (0x0001<<1)
//#define UART3_Tx_Pin (0x0001<<0)
// define UART0_Tx_Pin
#define UARTx_Tx_Pin UART0_Tx_Pin
// define UART0_Rx_Pin
#define UARTx_Rx_Pin UART0_Rx_Pin
// define UART0_Periph
#define UARTx_Periph UART0_Periph
// define UART0
#define UARTx UART0
// define UART0_IRQChannel
#define UARTx_IRQChannel UART0_IRQChannel
// define BandRate
#define UARTx_BANDRATE 115200
// define buffer size
#define SEND_MAX_SIZE 128
#define REC_MAX_SIZE 128
#define FLAG_FULL 0x01
#define FLAG_EMPTY 0x02
#define FLAG_BUSY 0x04
//----------------------------------------------------------------------------
// Local Variables Definitions
//----------------------------------------------------------------------------
/* RXD */
private u8 rec_buffer[ REC_MAX_SIZE ];
private u8 *rec_head_ptr, *rec_tail_ptr;
private u16 rec_length;
private u16 rec_flag;
/* TXD */
private u8 send_buffer[ SEND_MAX_SIZE ];
private u8 *send_head_ptr,*send_tail_ptr;
private u16 send_length;
private u16 send_flag;
//----------------------------------------------------------------------------
// Global Variables Definitions
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Local Function Definitions
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Global Function Definitions
//----------------------------------------------------------------------------
/**************************************************************
* Function: uart0_init(...)
* Description:
* initialize the device of UART0
* Input: no
*
* Return:
*
**************************************************************/
public bool uart0_init( void )
{
// 1. Enable UARTx clock on APB1
// APB_ClockConfig (APB1, ENABLE, UARTx_Periph);
// 2. Configure the GPIO pins
GPIO_Config(GPIO0, UARTx_Tx_Pin, GPIO_AF_PP);
GPIO_Config(GPIO0, UARTx_Rx_Pin, GPIO_IN_TRI_CMOS);
// Initialize UARTx data
UART_Init(UARTx);
UARTx->TOR = 40; // Timeout ticks of clock
// 3. Configure the UART X
UART_FifoConfig(UARTx, DISABLE ); // disable FIFOs
UART_FifoReset(UARTx, UART_RxFIFO); // Reset the UART_RxFIFO
UART_FifoReset(UARTx, UART_TxFIFO); // Reset the UART_TxFIFO
UART_LoopBackConfig(UARTx, DISABLE); // Disable Loop Back
UART_Config(UARTx, UARTx_BANDRATE, UART_NO_PARITY, UART_1_StopBits, UARTM_8D);
// 4. Configure the EIC channel interrupt
EIC_IRQChannelPriorityConfig(UARTx_IRQChannel, 2);
EIC_IRQChannelConfig(UARTx_IRQChannel, ENABLE);
/* initialize data */
rec_head_ptr = rec_tail_ptr = &rec_buffer[0];
send_head_ptr= send_tail_ptr = &send_buffer[0];
rec_length = send_length = 0;
rec_flag &= ~FLAG_FULL;
send_flag = FLAG_EMPTY;
return TRUE;
}
/**************************************************************
* Function: uart0_open(...)
* Description:
*
* Input:
*
* Return:
*
**************************************************************/
public bool uart0_open( void )
{
// 1. open UARTx
UART_FifoConfig(UARTx, ENABLE); // enable FIFOs
UART_FifoReset(UARTx, UART_RxFIFO); // Reset the UART_RxFIFO
UART_FifoReset(UARTx, UART_TxFIFO); // Reset the UART_TxFIFO
// 2. Configure the interrupt source
UART_ItConfig(UARTx, UART_RxHalfFull, ENABLE);
UART_ItConfig(UARTx, UART_TimeOutNotEmpty, ENABLE);
// UART_ItConfig(UARTx, UART_TxEmpty, ENABLE);
// 3. Turn uart on
UART_RxConfig(UARTx, ENABLE); // Enable Rx
UART_OnOffConfig(UARTx, ENABLE); // Turn UARTX on
return TRUE;
}
/**************************************************************
* Function: uart0_close(...);
* Desription:
*
* Input:
*
* Return:
*
**************************************************************/
public bool uart0_close( void )
{
// 1. Turn uart off
UART_OnOffConfig(UARTx, DISABLE); // Turn UARTX off
UART_RxConfig(UARTx, DISABLE); // disable Rx
// 2. Configure the interruption source
UART_ItConfig(UARTx, UART_RxHalfFull, DISABLE);
UART_ItConfig(UARTx, UART_TimeOutNotEmpty, DISABLE);
UART_ItConfig(UARTx, UART_TxEmpty, DISABLE);
// 3. close UARTx
UART_FifoConfig(UARTx, DISABLE); // disable FIFOs
UART_FifoReset(UARTx, UART_RxFIFO); // Reset the UART_RxFIFO
UART_FifoReset(UARTx, UART_TxFIFO); // Reset the UART_TxFIFO
return TRUE;
}
/**************************************************************
* Function: uart0_write(...)
* Description:
* write datas to uart1
* Input: size [--> ]: the number of bytes
* buffer [--> ]: the buffer of sent datas
*
* Return: TRUE : OK
* FALSE: error
**************************************************************/
public bool uart0_write( u16 size, u8 *buffer)
{
u8 data;
u16 len;
// 1. check parameters
if( size == 0 ) return FALSE;
if( (size+send_length) >= SEND_MAX_SIZE ) return FALSE;
// 2. send data to send_buffer[*]
for( len=0; len<size; len++ )
{ *send_tail_ptr =(u8) buffer[len];
// increase
send_tail_ptr++;
if( send_tail_ptr > &send_buffer[SEND_MAX_SIZE-1] )
{ send_tail_ptr = &send_buffer[0]; }
send_length++;
// clear flag
send_flag &= ~FLAG_EMPTY;
}
// 3. send data to the FIFO of TXD1
if( send_head_ptr == send_tail_ptr ) return TRUE;
if( send_flag & FLAG_BUSY ) return TRUE;
send_flag |= FLAG_BUSY;
for(len=0; len<8; len++)
{
data = *send_head_ptr;
// increase pointer
send_head_ptr++;
if( send_head_ptr > &send_buffer[SEND_MAX_SIZE-1] )
{ send_head_ptr = &send_buffer[0]; }
send_length--;
UART_ByteSend(UARTx, &data);
if( send_head_ptr == send_tail_ptr )
{ send_flag |= FLAG_EMPTY;
send_length = 0;
break;
}
}
// open interrupt
UART_ItConfig(UARTx, UART_TxEmpty, ENABLE);
return TRUE;
}
/**************************************************************
* Function: uart0_read(...)
* Description: get datas from the bufer of UART0
*
* Input: size_ptr [--> ]: the size of wang to get datas
* [<-- ]: the size of getted datas
* buffer [<-- ]: the buffer of return datas
* Return: TRUE : OK
* FALSE: error
**************************************************************/
public bool uart0_read(u16* size_ptr, u8* buffer)
{ u16 len,count;
u16 i;
/* 1. check */
if( *size_ptr==0) return FALSE;
if( (rec_head_ptr==rec_tail_ptr &&
(rec_flag & FLAG_FULL)==0) ) return FALSE;
/* 2. get data from rec_buffer[*] */
i = (u16)( *size_ptr<rec_length ? *size_ptr : rec_length );
if( i==0 )
{ rec_head_ptr = rec_tail_ptr = &rec_buffer[0];
rec_length = 0;
rec_flag &= ~FLAG_FULL;
}
for(len=0,count=0; len<i; len++)
{
buffer[len] = (u8) *rec_head_ptr;
count++;
// increase pointer
rec_head_ptr++;
if( rec_head_ptr > &rec_buffer[REC_MAX_SIZE-1] )
{ rec_head_ptr = rec_buffer; }
rec_length--;
// clear flag
rec_flag &= ~FLAG_FULL;
// if buffer is empty
if( rec_head_ptr == rec_tail_ptr )
{ rec_length=0; break; }
}
*size_ptr = count;
return TRUE;
}
/**************************************************************
* Function: uart0_ctrl(...)
* Description:
*
* Input:
*
* Return:
*
**************************************************************/
public bool uart0_ctrl( void)
{
return TRUE;
}
/**************************************************************
* Function: uart0_isr(...)
* Description:
* ISR(Interrupt Service Routine) for uart0
* Input:
*
* Return:
*
**************************************************************/
public void uart0_isr(void)
{
u16 i;
u8 rec_temp;
u16 status;
status = UART_FlagStatus(UARTx);
// 1. send datas
if( status & UART_TxEmpty )
{
// send_buffer is empty ?
if( send_flag & FLAG_EMPTY )
{ // close interrupt
UART_ItConfig( UARTx, UART_TxEmpty, DISABLE );
// reset variables
send_head_ptr= send_tail_ptr = &send_buffer[0];
send_length =0;
send_flag &= ~FLAG_BUSY;
}
else // send data to TX1 port
{ for(i=0; i<8; i++)
{ // send to TX1
UARTx->TxBUFR = *send_head_ptr;
send_length--;
// increase pointer
send_head_ptr++;
if( send_head_ptr > &send_buffer[SEND_MAX_SIZE-1] )
{ send_head_ptr = &send_buffer[0]; }
// send_buffer is empty ?
if( send_head_ptr == send_tail_ptr )
{ send_flag |= FLAG_EMPTY; break; }
}
}
}
// 2. receive datas
if( status & (UART_RxHalfFull | UART_TimeOutNotEmpty))
{ // open timeout idle interrupt
UART_ItConfig(UARTx, UART_TimeOutIdle, ENABLE);
// get 8bytes data
while( UART_FlagStatus(UARTx) & UART_RxBufFull)
{
if( rec_flag & FLAG_FULL )
{ // throw away the data
rec_temp = (u8)UARTx->RxBUFR;
}
else
{ // receive data from RX port
*rec_tail_ptr = (u8)UARTx->RxBUFR;
rec_length++;
// increase pointer
rec_tail_ptr++;
if( rec_tail_ptr > &rec_buffer[REC_MAX_SIZE-1] )
{ rec_tail_ptr = &rec_buffer[0]; }
if( rec_tail_ptr == rec_head_ptr )
{ rec_flag |= FLAG_FULL;
rec_length = REC_MAX_SIZE;
}
}
}
}
else if( status & UART_TimeOutIdle )
{ // close Timeout idle interrupt
UART_ItConfig(UARTx, UART_TimeOutIdle, DISABLE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -