📄 lh7a400_uart_driver.c
字号:
err = LH7A400_uart_rxb (uart, &buf);
if (err == SIOERR_NO_ERROR)
{
*rxs++ = buf;
rxs_timeout = default_rx_string_timeout;
}
else
rxs_timeout--;
if (rxs_timeout < 0)
{
err = SIOERR_RX_TIMEOUT;
break;
}
} while (buf != ASCII_NUL);
if (err != SIOERR_NO_ERROR)
*rxs = 0; // Put a NUL on the string
break;
case STRING_TERM_LF:
case STRING_TERM_CRLF:
do {
err = LH7A400_uart_rxb (uart, &buf);
if (err == SIOERR_NO_ERROR)
{
*rxs++ = buf;
rxs_timeout = default_rx_string_timeout;
}
else
rxs_timeout--;
if (rxs_timeout < 0)
{
err = SIOERR_RX_TIMEOUT;
break;
}
} while (buf != ASCII_LF);
*rxs = 0; // Put a NUL on the string
break;
case STRING_TERM_CR:
do {
err = LH7A400_uart_rxb (uart, &buf);
if (err == SIOERR_NO_ERROR)
{
*rxs++ = buf;
rxs_timeout = default_rx_string_timeout;
}
else
rxs_timeout--;
if (rxs_timeout < 0)
{
err = SIOERR_RX_TIMEOUT;
break;
}
} while (buf != ASCII_LF);
*rxs = 0; // Put a NUL on the string
break;
default:
while (term > 0)
{
err = LH7A400_uart_rxb (uart, &buf);
if (err == SIOERR_NO_ERROR)
{
*rxs++ = buf;
term--;
rxs_timeout = default_rx_string_timeout;
}
else
rxs_timeout--;
if (rxs_timeout < 0)
{
err = SIOERR_RX_TIMEOUT;
break;
}
}
*rxs = 0; // Put a NUL on the string
break;
}
return (err);
}
/***********************************************************************
*
* Function: SIO_ERROR LH7A400_uart_rxb (UARTREGS * uart,
* UNS_8 * rx_octet)
*
* Purpose:
* To receive one octet inbound from a selected UART in polling
* mode
*
* Processing:
* if receive FIFO empty
* store NUL in the data buffer provided by user
* return FIFO empty code
* else
* retrieve data octet from FIFO and store in buffer
* return NO_ERROR code
*
* Parameters:
* UARTREGS * uart - pointer to UART
* uart arguments: {UART1 | UART2 | UART3}
*
* UNS_8 * rx_octet - pointer to buffer for octet
*
* Outputs: None
*
* Returns: SIO_ERROR - {SIOERR_NO_ERROR | SIOERR_RX_FIFO_EMPTY}
*
* Notes: [if any]
* (1) Does not wait for data. Returns immediately whether or not
* a datum is available.
*
**********************************************************************/
SIO_ERROR LH7A400_uart_rxb (UARTREGS * uart, UNS_8 * rx_octet)
{
SIO_ERROR ret = SIOERR_NO_ERROR;
if (uart->status & UART_STATUS_RXFE)
{
ret = SIOERR_RX_FIFO_EMPTY;
*rx_octet = 0;
}
else
*rx_octet = GETDATABYTE(uart->data);
return (ret);
}
/***********************************************************************
*
* Function: SIO_ERROR LH7A400_uart_txs (UARTREGS * uart,
* UNS_8 * const txs,
* INT_32 term);
*
* Purpose: To transmit a string of octets outbound to a selected
* UART in polling mode.
*
* Processing:
*
* if term == NUL
* transmit octets one at a time on selected UART until NUL octet
* is encountered in string, then transmit final NUL octet.
*
* if term == linefeed
* transmit octets one at a time on selected UART until LF octet
* is encountered in string, then send LF and final NUL octet.
*
* if term == carriage return
* transmit octets one at a time on selected UART until CR octet
* is encountered, then send CR and final NUL octet.
*
* if term == carriage-return-linefeed
* transmit octets one at a time on selected UART until LF octet
* is encountered in string, then send LF and final NUL octet.
*
* if term == any positive integer
* send octets one at a time on selected UART until exactly
* <positive integer> octets have been sent.
* Do NOT transmit a NUL terminator.
*
* return SIO_ERROR.
*
* Parameters:
* UARTREGS * uart - pointer to UART
* uart arguments: {UART1 | UART2 | UART3}
*
* UNS_8 * txs - pointer the first octet in string buffer
*
* INT_32 term - string terminator code (flexible parameter)
* term arguments:
* { STRING_TERM_NUL | STRING_TERM_LF| STRING_TERM_CR |
* <positive number> }
* STRING_TERM_NUL - a NUL ('\0', 0x0, ASC_NUL) character
* STRING_TERM_LF - a LF (\n, 0x0A, ASC_LF) character
* STRING_TERM_CR - a CR (\r, 0x0D, ASC_CR) character
* STRING_TERM_CRLF - CR-LF (\r\n, 0x0D0A) characters
* <positive number> - number of octets in string
*
* Outputs: Serial data on UARTx outbound signal line.
*
* Returns: SIO_ERROR
* See LH7A400_uart_driver.h for SIO_ERROR values
*
* Notes:
* (1) Arguments defined in LH7A400_uart.h and LH7A400_uart_driver.h
* (2) STRING_TERM_LF and STRING_TERM_CRLF are handled the same.
* (3) User is responsible for providing a properly terminated string
* (4) Function does not return until entire string is transmitted.
*
**********************************************************************/
SIO_ERROR LH7A400_uart_txs (UARTREGS * uart, UNS_8 * txs, INT_32 term)
{
SIO_ERROR err = SIOERR_NO_ERROR;
switch (term)
{
case STRING_TERM_NUL: // NUL terminated string
do {
err = LH7A400_uart_txb (uart, *txs);
} while (*txs++ != ASCII_NUL);
break;
case STRING_TERM_LF:
case STRING_TERM_CRLF:
do {
err = LH7A400_uart_txb (uart, *txs);
} while (*txs++ != ASCII_LF);
break;
case STRING_TERM_CR:
do {
err = LH7A400_uart_txb (uart, *txs);
} while (*txs++ != ASCII_CR);
break;
default:
for ( ; term > 0; term--)
{
err = LH7A400_uart_txb (uart, *txs++);
}
break;
}
return (err);
}
/***********************************************************************
*
* Function: SIO_ERROR LH7A400_uart_txb (UARTREGS * uart,
* UNS_8 tx_octet)
*
* Purpose:
* To transmit one octet outbound to a selected UART in polling
* mode
*
* Processing:
* Wait reasonable time for space in FIFO
* If space in FIFO becomes available before timeout, write octet
* to FIFO
* return SIO_ERROR
*
* Parameters:
* UARTREGS * uart - pointer to UART
* uart arguments: {UART1 | UART2 | UART3}
*
* UNS_8 tx_octet - single octet value
*
* Outputs: Serial data on UARTx outbound signal line.
*
* Returns: SIO_ERROR
* See LH7A400_uart_driver.h for SIO_ERROR values
*
* Notes:
* (1) Works whether FIFO is enabled or not.
* (2) Timeout is a simple countdown loop; SIO_TXC_TIMEOUT_COUNT
* should be adjusted by user to provide at least two character
* times at any anticipated baud rate and CPU clock.
*
**********************************************************************/
SIO_ERROR LH7A400_uart_txb (UARTREGS * uart, UNS_8 tx_octet)
{
SIO_ERROR ret;
//INT_32 ctr;
//for (ctr = default_txb_timeout; ctr > 0 ; ctr--)
while(1)
{
if (uart->status & UART_STATUS_TXFF)
{
continue;
}
else
{
ret = SIOERR_NO_ERROR;
uart->data = tx_octet;
break;
}
}
//if (ctr <= 0)
ret = SIOERR_TX_TIMEOUT;
return (ret);
}
/***********************************************************************
*
* Function: void LH7A400_uart_fifo_flush (UARTREGS * uart)
*
* Purpose:
* To flush the UART FIFO's
*
* Processing:
* Save UART control register state
* Disable the FIFO, whether enabled or not
* Restore UART control register state
*
* Parameters:
* UARTREGS * uart - pointer to UART
* uart arguments: {UART1 | UART2 | UART3}
*
* Outputs: None.
*
* Returns: None
*
* Notes:
* (1) Works whether FIFO is enabled or not.
*
**********************************************************************/
void LH7A400_uart_fifo_flush (UARTREGS * uart)
{
UNS_32 buffer;
// Save the UART control state.
buffer = uart->control;
// Clear the FIFO's by disabling the UART before enabling it.
uart->control = buffer & ~UART_CONTROL_UART_ENABLE;
// Restore the UART state.
uart->control = buffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -