📄 uart.h
字号:
/******************************************************************************/
/******************************************************************************/
/* UART.H - TLC16C550C UART driver routines */
/* */
/* This module is the device library for the TLC16C550C UART on the */
/* 54XX DSK board. */
/* */
/* MACRO FUNCTIONS: */
/* */
/* FUNCTIONS: */
/* */
/* uart_init() - Perform required initialization and default configuration */
/* uart_reset() - Reset the codec */
/* int uart_read_reg() - Read uart register */
/* int uart_write_reg() - Write uart register */
/* uart_interrupt_control() - Enables uart interrupts */
/* uart_interrupt_hook() - Install ISR for uart interrupt */
/* uart_interrupt_info() - retrieves interrupt information from IIR */
/* int uart_read() - Read a buffer of data from uart */
/* int uart_write() - Write a buffer of data to uart */
/* int uart_setup() - change values of settable parameters */
/* int uart_fgetc() - read a character from the uart. */
/* int uart_fputc() - write a character to the uart. */
/* char* uart_fgets() - read a string from the uart. */
/* int uart_fputs() - write a string to the uart. */
/* */
/******************************************************************************/
#ifndef _UART_H_
#define _UART_H_
#include <type.h>
#include <board.h> /* public header file */
/*****************************************************************************/
/* DEFINES */
/*****************************************************************************/
#undef OK
#define OK 0
#undef ERROR
#define ERROR -1
#define DLAB_SET_REG 0x0100
//C54XX interrupt the uart is mapped to
#define UART_INTR_FLAG INT1
#define UART_INTR_TRAP INT1_TRAP
//LCR
#define UART_STOPBIT_ENABLE 0x04
#define UART_PARITY_ENABLE 0x08
#define UART_PARITY_SELECT 0x10
#define DLAB_MASK 0x80
//MCR
#define UART_LOOP_ENABLE 0x10
//FCR
#define FIFO_TRIG01 0x00
#define FIFO_TRIG04 0x01
#define FIFO_TRIG08 0x10
#define FIFO_TRIG14 0x11
//uart register addresses
volatile ioport u16 port4000;
volatile ioport u16 port4001;
volatile ioport u16 port4002;
volatile ioport u16 port4003;
volatile ioport u16 port4004;
volatile ioport u16 port4005;
volatile ioport u16 port4006;
volatile ioport u16 port4007;
//uart registers
#define UART_RBR_REG port4000
#define UART_THR_REG port4000
#define UART_IER_REG port4001
#define UART_IIR_REG port4002
#define UART_FCR_REG port4002
#define UART_LCR_REG port4003
#define UART_MCR_REG port4004
#define UART_LSR_REG port4005
#define UART_MSR_REG port4006
#define UART_SCR_REG port4007
//for these registers ensure DLAB=1 (b7 of LCR) before accessing
#define UART_DLL_REG port4000
#define UART_DLM_REG port4001
/*****************************************************************************/
/* typedefs & enums */
/*****************************************************************************/
//TL16C550C UART registers
//If b8 of register enumeration equals 1 (UART_DLL & UART_DLM), it implies DLAB
//must be set to 1 before register can be addressed.
//The actual port address used in the portio routines is the enumerated value
//with b8 set to zero.
//
//e.g. to read UART_DLL,
//
// unsigned int val;
// UART_LCR_REG |= 0x80
// val = portRead(UART_DLL & ~0x0100)
//
typedef enum
{
UART_RBR = 0x4000, // rx buffer
UART_THR = 0x4000, // tx hold
UART_IER = 0x4001, // interrupt enable
UART_IIR = 0x4002, // interrupt identification
UART_FCR = 0x4002, // FIFO control
UART_LCR = 0x4003, // line control
UART_MCR = 0x4004, // modem control
UART_LSR = 0x4005, // line status
UART_MSR = 0x4006, // modem status
UART_SCR = 0x4007, // scratch
UART_DLL = 0x4100, // divisor latch (LSB)
UART_DLM = 0x4101 // divisor latch (MSB)
} UartReg, *PUartReg;
// TL16C550C UART interrupts
typedef enum
{
UART_RINT = 0x01, // Enable rx data available interrupt
UART_TINT = 0x02, // Enable tx hold register empty interrupt
UART_LSINT = 0x04, // Enable rx line status interrupt
UART_MSINT = 0x08, // Enable modem status interrupt
UART_ALLINT = 0x0f // Enable all interrupts
} UartIntr, *PUartIntr;
// TL16C550C UART serial character word length
typedef enum
{
UART_WORD_NOCHANGE = -1, // 5-bit
UART_WORD5 = 0x00, // 5-bit
UART_WORD6 = 0x01, // 6-bit
UART_WORD7 = 0x02, // 7-bit
UART_WORD8 = 0x03 // 8-bit
} UartWordLen, *PUartWordLen;
// TL16C550C UART stop bits
typedef enum
{
UART_STOPBITS_NOCHANGE = -1,
UART_STOP1 = 0, // 1 stop bit
UART_STOP1_PLUS_HALF = 1, // 1 1/2 stop bits
UART_STOP2 = 2 // 2 stop bits
} UartStopBits, *PUartStopBits;
// TL16C550C UART flow control
typedef enum
{
UART_LOOP_NOCHANGE = -1,
UART_LOOPBACK = 0,
UART_NO_LOOPBACK = 1
} UartLoop, *PUArtLoop;
// TL16C550C UART parity select
typedef enum
{
UART_PARITY_NOCHANGE = -1, //disable parity checking
UART_DISABLE_PARITY = 0, //disable parity checking
UART_EVEN_PARITY = 0x10, //enable even parity
UART_ODD_PARITY = ~0x10 //enable odd parity
} UartParity, *PUartParity;
// TL16C550C UART baud rates
typedef enum
{
UART_BAUD_NOCHANGE = -1,
UART_BAUD_50 = 4608,
UART_BAUD_75 = 3072,
UART_BAUD_110 = 2094,
UART_BAUD_150 = 1536,
UART_BAUD_300 = 768,
UART_BAUD_600 = 384,
UART_BAUD_1200 = 192,
UART_BAUD_1800 = 128,
UART_BAUD_2000 = 116,
UART_BAUD_2400 = 96,
UART_BAUD_3600 = 64,
UART_BAUD_4800 = 48,
UART_BAUD_7200 = 32,
UART_BAUD_9600 = 24,
UART_BAUD_19200 = 12,
UART_BAUD_38400 = 6,
UART_BAUD_57600 = 4,
UART_BAUD_115200 = 2
} UartBaud, *PUartBaud;
// TL16C550C UART FIFO control
typedef enum
{
UART_FIFO_NOCHANGE = -1,
UART_FIFO_ENABLE = 0x01,
UART_FIFO_RX_RESET = 0x02,
UART_FIFO_TX_RESET = 0x04,
UART_FIFO_DMA = 0x08,
UART_FIFO_TRG_RX_LSB = 0x40,
UART_FIFO_TRG_RX_MSB = 0x80,
UART_FIFO_DISABLE = 0x10,
//FIFO rx trigger levels (1, 4, 8, or 14 bytes)
UART_FIFO_TRIG01 = 0x11,
UART_FIFO_TRIG04 = 0x12,
UART_FIFO_TRIG08 = 0x13,
UART_FIFO_TRIG14 = 0x14
} UartFifoControl, *PUartFifoControl;
//TL16C550C UART interrupt information as indicated
//by b0-b3 of UART_IIR_REG
typedef enum
{
UART_LSR_INTR = 0x1, //rx line status - overrun error, parity error,
//framing error, or break interrupt. Read line
//status register to determine exact interrupt
//source.
UART_RXRDY_INTR = 0x6, //rx data available
UART_CHAR_TIMEOUT = 0xc, //character timeout
UART_TXRDY_INTR = 0x2, //tx holding register empty
UART_MSR_INTR = 0x1 //modem status - clear to send, data set ready,
//ring indicator, or data carrier detect.
//Read modem status register to exact interrupt
//source.
} UartIntrInfo, *PUartIntrInfo;
/*****************************************************************************/
/* PUBLIC FUNCTION PROTOTYPES */
/*****************************************************************************/
/*****************************************************************************/
/* s16 uart_init(void) */
/* */
/* This routine performs required initialization and default configuration. */
/* */
/* Parameters: */
/* - None */
/* */
/* Return: */
/* - OK success */
/* - ERROR failure */
/* */
/* Notes: */
/* */
/*****************************************************************************/
int uart_init(void);
/*****************************************************************************/
/* s16 uart_reset(void) */
/* */
/* This routine resets the TLC16C550 ACE internal registers to their */
/* default values. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -