📄 uart.c
字号:
* character on success, -1 if no character is available
*
*****************************************************************************/
int uart0Getch(void)
{
#ifdef UART0_RX_INT_MODE
uint8_t ch;
if (uart0_rx_insert_idx == uart0_rx_extract_idx) // check if character is available
return -1;
ch = uart0_rx_buffer[uart0_rx_extract_idx++]; // get character, bump pointer
uart0_rx_extract_idx %= UART0_RX_BUFFER_SIZE; // limit the pointer
return ch;
#else
if (U0LSR & ULSR_RDR) // check if character is available
return U0RBR; // return character
return -1;
#endif
}
#endif
#if UART1_SUPPORT
/******************************************************************************
*
* Function Name: uart1Init()
*
* Description:
* This function initializes the UART for async mode
*
* Calling Sequence:
* baudrate divisor - use UART_BAUD macro
* mode - see typical modes (uart.h)
* fmode - see typical fmodes (uart.h)
*
* Returns:
* void
*
* NOTE: uart1Init(UART_BAUD(9600), UART_8N1, UART_FIFO_8);
*
*****************************************************************************/
void uart1Init(uint16_t baud, uint8_t mode, uint8_t fmode)
{
// set port pins for UART1
PINSEL0 = (PINSEL0 & ~U1_PINMASK) | U1_PINSEL;
U1IER = 0x00; // disable all interrupts
U1IIR; // clear interrupt ID
U1RBR; // clear receive register
U1LSR; // clear line status register
// set the baudrate
U1LCR = ULCR_DLAB_ENABLE; // select divisor latches
U1DLL = (uint8_t)baud; // set for baud low byte
U1DLM = (uint8_t)(baud >> 8); // set for baud high byte
// set the number of characters and other
// user specified operating parameters
U1LCR = (mode & ~ULCR_DLAB_ENABLE);
U1FCR = fmode;
#if defined(UART1_TX_INT_MODE) || defined(UART1_RX_INT_MODE)
// initialize the interrupt vector
VICIntSelect &= ~VIC_BIT(VIC_UART1); // UART1 selected as IRQ
VICIntEnable = VIC_BIT(VIC_UART1); // UART1 interrupt enabled
VICVectCntl1 = VIC_ENABLE | VIC_UART1;
VICVectAddr1 = (uint32_t)uart1ISR; // address of the ISR
#ifdef UART1_TX_INT_MODE
uart1_tx_extract_idx = uart1_tx_insert_idx = 0;
uart1_tx_running = 0;
#endif
#ifdef UART1_RX_INT_MODE
// initialize data queues
uart1_rx_extract_idx = uart1_rx_insert_idx = 0;
// enable receiver interrupts
U1IER |= UIER_ERBFI;
#endif
#endif
}
/******************************************************************************
*
* Function Name: uart1Putch()
*
* Description:
* This function puts a character into the UART output queue for
* transmission.
*
* Calling Sequence:
* ch - character to be transmitted
*
* Returns:
* ch on success, -1 on error (queue full)
*
*****************************************************************************/
int uart1Putch(int ch)
{
#ifdef UART1_TX_INT_MODE
uint16_t temp;
unsigned cpsr;
temp = (uart1_tx_insert_idx + 1) % UART1_TX_BUFFER_SIZE;
if (temp == uart1_tx_extract_idx)
return -1; // no room
cpsr = disableIRQ(); // disable global interrupts
U1IER &= ~UIER_ETBEI; // disable TX interrupts
restoreIRQ(cpsr); // restore global interrupts
// check if in process of sending data
if (uart1_tx_running)
{
// add to queue
uart1_tx_buffer[uart1_tx_insert_idx] = (uint8_t)ch;
uart1_tx_insert_idx = temp;
}
else
{
// set running flag and write to output register
uart1_tx_running = 1;
U1THR = (uint8_t)ch;
}
cpsr = disableIRQ(); // disable global interrupts
U1IER |= UIER_ETBEI; // enable TX interrupts
restoreIRQ(cpsr); // restore global interrupts
#else
while (!(U1LSR & ULSR_THRE)) // wait for TX buffer to empty
continue; // also either WDOG() or swap()
U1THR = (uint8_t)ch;
#endif
return (uint8_t)ch;
}
/******************************************************************************
*
* Function Name: uart1Space()
*
* Description:
* This function gets the available space in the transmit queue
*
* Calling Sequence:
* void
*
* Returns:
* available space in the transmit queue
*
*****************************************************************************/
uint16_t uart1Space(void)
{
#ifdef UART1_TX_INT_MODE
int space;
if ((space = (uart1_tx_extract_idx - uart1_tx_insert_idx)) <= 0)
space += UART1_TX_BUFFER_SIZE;
return (uint16_t)(space - 1);
#else
return USHRT_MAX;
#endif
}
/******************************************************************************
*
* Function Name: uart1Puts()
*
* Description:
* This function writes a NULL terminated 'string' to the UART output
* queue, returning a pointer to the next character to be written.
*
* Calling Sequence:
* address of the string
*
* Returns:
* a pointer to the next character to be written
* (\0 if full string is written)
*
*****************************************************************************/
const char *uart1Puts(const char *string)
{
register char ch;
while ((ch = *string) && (uart1Putch(ch) >= 0))
string++;
return string;
}
/******************************************************************************
*
* Function Name: uart1Write()
*
* Description:
* This function writes 'count' characters from 'buffer' to the UART
* output queue.
*
* Calling Sequence:
*
*
* Returns:
* 0 on success, -1 if insufficient room, -2 on error
* NOTE: if insufficient room, no characters are written.
*
*****************************************************************************/
int uart1Write(const char *buffer, uint16_t count)
{
#ifdef UART1_TX_INT_MODE
if (count > uart1Space())
return -1;
#endif
while (count && (uart1Putch(*buffer++) >= 0))
count--;
return (count ? -2 : 0);
}
/******************************************************************************
*
* Function Name: uart1TxEmpty()
*
* Description:
* This function returns the status of the UART transmit data
* registers.
*
* Calling Sequence:
* void
*
* Returns:
* FALSE - either the tx holding or shift register is not empty
* !FALSE - if both the tx holding & shift registers are empty
*
*****************************************************************************/
int uart1TxEmpty(void)
{
return (U1LSR & (ULSR_THRE | ULSR_TEMT)) == (ULSR_THRE | ULSR_TEMT);
}
/******************************************************************************
*
* Function Name: uart1TxFlush()
*
* Description:
* This function removes all characters from the UART transmit queue
* (without transmitting them).
*
* Calling Sequence:
* void
*
* Returns:
* void
*
*****************************************************************************/
void uart1TxFlush(void)
{
#ifdef UART1_TX_INT_MODE
unsigned cpsr;
U1FCR |= UFCR_TX_FIFO_RESET; // clear the TX fifo
// "Empty" the transmit buffer.
cpsr = disableIRQ(); // disable global interrupts
U1IER &= ~UIER_ETBEI; // disable TX interrupts
restoreIRQ(cpsr); // restore global interrupts
uart1_tx_insert_idx = uart1_tx_extract_idx = 0;
#else
U1FCR |= UFCR_TX_FIFO_RESET; // clear the TX fifo
#endif
}
/******************************************************************************
*
* Function Name: uart1Getch()
*
* Description:
* This function gets a character from the UART receive queue
*
* Calling Sequence:
* void
*
* Returns:
* character on success, -1 if no character is available
*
*****************************************************************************/
int uart1Getch(void)
{
#ifdef UART1_RX_INT_MODE
uint8_t ch;
if (uart1_rx_insert_idx == uart1_rx_extract_idx) // check if character is available
return -1;
ch = uart1_rx_buffer[uart1_rx_extract_idx++]; // get character, bump pointer
uart1_rx_extract_idx %= UART1_RX_BUFFER_SIZE; // limit the pointer
return ch;
#else
if (U1LSR & ULSR_RDR) // check if character is available
return U1RBR; // return character
return -1;
#endif
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -