📄 uartfax.c
字号:
(uart->rd_call_setup == rm_reInstall))) {
if ((bytes_in_rx_buffer >= uart->rx_threshold_level) ||
uart->break_received ||
xon_xoff_detected) {
uart->rd_call_from_hisr_in_progress = 1;
update_reading_callback (uart, 3); //
uart->reading_suspended = 0;
uart->break_received = 0;
uart->esc_seq_received = 0;
}
}
}
}
/****************************************************************************************************************
* FUNCTION : bsp_Usart_rxCallback
* ARGUMENTS :
irq : if irq set to TRUE otherwise FALSE
reinstallPtr : Reinstall pointer for reinstall after each thresh-hold
cb : Circular buffer for data manupulation.
numBytesAvailable : Number of byte availabe in circular buffer.
state : which state the UART is.
CommPort : which UART.
* GLOBAL VARIABLES : rxBuf : Circular buffer for Receive.
* RETURN VALUE : numRetrieved : Number of bytes retrieved from the buffer.
* DESCRIPTION : The RX callback, called from RX interrupt, which can't be interrupted by read thread.
* Copies data from BSP_USART to RX buffer
****************************************************************************************************************/
BspUsart_ResultCode
bsp_Usart_rxCallback( Uint16 irq,
BspUsart_ReInstallMode *reinstallPtr,
BspUtil_CircBuf_Handle cb,
Uint16 numBytesAvailable,
BspUsart_State state,
BspUsart_Id CommPort )
{
Uint16 numRetrieved = 0;
t_uart *uart;
SYS_UWORD8 *first_byte;
SYS_UWORD8 *current_byte;
SYS_UWORD16 *bytes_in_rx_buffer;
SYS_UWORD16 bytes_received;
uart = &uart_parameters;
bytes_received = 0;
receive_counter1 += numBytesAvailable;
first_byte = uart->rx_buffer_used_by_rx_lisr;
if (first_byte == &(uart->rx_fifo_byte_1[0])) {
first_byte = &(uart->rx_fifo_byte_2[0]);
bytes_in_rx_buffer = &(uart->bytes_in_rx_buffer_2);
} else {
first_byte = &(uart->rx_fifo_byte_1[0]);
bytes_in_rx_buffer = &(uart->bytes_in_rx_buffer_1);
}
current_byte = first_byte;
uart->rx_buffer_used_by_rx_lisr = first_byte;
while( numBytesAvailable > 0 )
{
bspUtil_CircBuf_dequeueUint8( cb, current_byte++ );
numBytesAvailable--;
numRetrieved ++;
}
bytes_received = (SYS_UWORD16) (current_byte - first_byte);
*bytes_in_rx_buffer = bytes_received;
if (!bytes_received) {
if (uart->rx_buffer_used_by_rx_lisr == &(uart->rx_fifo_byte_1[0]))
uart->rx_buffer_used_by_rx_lisr = &(uart->rx_fifo_byte_2[0]);
else
uart->rx_buffer_used_by_rx_lisr = &(uart->rx_fifo_byte_1[0]);
}
if (bytes_received > uart->max_rx_fifo_level)
uart->max_rx_fifo_level = bytes_received;
*reinstallPtr = BSP_USART_REINSTALL_MODE_REINSTALL;
NU_Activate_HISR (&(uart->rx_hisr_ctrl_block));
return numRetrieved;
}
/*******************************************************************************
*
* UAF_StartRec
*
* Purpose : If a flow control mode is set, this function tells the terminal
* equipment that the receiver is again able to receive more data.
* If the buffer has already reached the high water mark the driver
* sends the signal only if the buffer drains to a low water mark.
* XON/XOFF: XON is sent.
* DTR/DSR : DTR is activated.
* RTS/CTS : RTS is activated.
*
* Arguments: In : uartNo: Used UART.
* Out: none
*
* Returns : FD_OK : Successful operation.
* FD_NOT_SUPPORTED: Wrong UART number.
* FD_INTERNAL_ERR : Internal problem with the hardware.
*
******************************************************************************/
T_FDRET
UAF_StartRec (T_tr_UartId uartNo)
{
t_uart *uart;
uartNo += 1;
if (uartNo != UAF_UART_1)
return (FD_NOT_SUPPORTED);
/*
* There is no case where FD_INTERNAL_ERR may be returned.
*/
uart = &uart_parameters;
if ((uart->flow_control_mode != fc_none) && (!uart->rx_stopped_by_driver))
start_receiver (uartNo);
uart->rx_stopped_by_application = 0;
return (FD_OK);
}
/*******************************************************************************
*
* UAF_StopRec
*
* Purpose : If a flow control mode is set, this function tells the terminal
* equipment that no more data can be received.
* XON/XOFF: XOFF is sent.
* DTR/DSR : DTR is desactivated.
* RTS/CTS : RTS is deactivated.
*
* Arguments: In : uartNo: Used UART.
* Out: none
*
* Returns : FD_OK : Successful operation.
* FD_NOT_SUPPORTED: Wrong UART number.
* FD_INTERNAL_ERR : Internal problem with the hardware.
*
******************************************************************************/
T_FDRET
UAF_StopRec (T_tr_UartId uartNo)
{
t_uart *uart;
uartNo += 1;
if (uartNo != UAF_UART_1)
return (FD_NOT_SUPPORTED);
/*
* There is no case where FD_INTERNAL_ERR may be returned.
*/
uart = &uart_parameters;
if (uart->flow_control_mode != fc_none)
stop_receiver (uartNo);
uart->rx_stopped_by_application = 1;
return (FD_OK);
}
/****************************************************************************************************************
* FUNCTION : UAF_Open.
* ARGUMENTS : None
* RETURN VALUE : RetCode : return code.
* GLOBAL VARIABLES : rxBuf,txBuf : Circular buffer for Transmitt/receuve.
* DESCRIPTION :Test case for Testing of Setting UART and UART Parameters uid 2,baudRate 9600,bpc 8,sb 1 and parity 0
****************************************************************************************************************/
Int32 UAF_Open( BspUsart_Id uid,BspUsart_Baud baudRate )
{
BspUsart_BitsPerChar bpc = BSP_USART_BITS_PER_CHAR_8;
BspUsart_StopBits sb = BSP_USART_STOP_BITS_1;
BspUsart_Parity parity = BSP_USART_PARITY_NONE;
Int16 RetCode= -10 , RetValue = -10 ;
if(UAF_GetPortType(uid) == UA_TYPE_UART)
{
RetValue = bspUsart_unreserve(uid) ;
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
if( bspUsart_reserve(uid) == BSP_USART_RESULT_CODE_OK )
{
RetValue = bspUsart_enable( uid, FALSE );
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
RetValue = bspUsart_setComPar( uid, baudRate, bpc, sb, parity );
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
RetValue = bspUsart_setBuffer( uid, Rx_buffer,Tx_buffer, RXBUFSIZE, TXBUFSIZE, RXTHRESH, TXTHRESH);
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
RetValue = bspUsart_setFlowCtrl( uid, BSP_USART_FLOW_CONTROL_NONE, 0x0, 0x0);
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
RetValue = bspUsart_enable( uid, TRUE );
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
bspUsart_writeData((BspUsart_Id)uid, BSP_USART_SUSPEND_MODE_SUSPEND, bsp_Usart_txCallback );
RetValue = bspUsart_readData((BspUsart_Id)uid, BSP_USART_SUSPEND_MODE_SUSPEND, bsp_Usart_rxCallback );
if ( RetValue == BSP_USART_RESULT_CODE_OK )
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
else
{
RetCode = BSP_USART_RESULT_CODE_INTERNAL_ERR ;
}
}
}
else if(UAF_GetPortType(uid) == UA_TYPE_USB) /* USB virtual serial ports */
{
RetCode = BSP_USART_RESULT_CODE_OK;
}
return RetCode;
}
/*******************************************************************************
*
* UAF_Init
*
* Purpose : Initializes the UART hardware and installs interrupt handlers.
* The parameters are set to the default values:
* - 19200 baud,
* - 8 bits / character,
* - no parity,
* - 1 stop bit,
* - no flow control.
* All functionalities of the UART driver are disabled.
*
* Arguments: In : uartNo: Used UART.
* Out: none
*
* Returns : FD_OK : Successful operation.
* FD_NOT_SUPPORTED: Wrong UART number.
* FD_INTERNAL_ERR : Internal problem.
*
******************************************************************************/
T_FDRET
UAF_Init (T_tr_UartId uartNo)
{
t_uart *uart;
volatile SYS_UWORD8 status;
uartNo += 1;
if (uartNo != UAF_UART_1 )
return (FD_NOT_SUPPORTED);
uart = &uart_parameters;
if(UAF_GetPortType(uartNo) == UA_TYPE_UART)
{
memset (&(uart->rx_hisr_stack[0]), 0xFE, RX_HISR_STACK_SIZE);
if (NU_Create_HISR (&(uart->rx_hisr_ctrl_block),
"UAF_Rx",
hisr_execute_rx_operations,
RX_HISR_PRIORITY,
&(uart->rx_hisr_stack[0]),
RX_HISR_STACK_SIZE) != NU_SUCCESS)
return (FD_INTERNAL_ERR);
/*
* Create the 3 HISR actived in the RX/TX and V24 interrupt handlers.
* A return is used to simplify the code if an error occurs.
* All stacks are entirely filled with the pattern 0xFE.
*/
memset (&(uart->tx_hisr_stack[0]), 0xFE, TX_HISR_STACK_SIZE);
if (NU_Create_HISR (&(uart->tx_hisr_ctrl_block),
"UAF_Tx",
hisr_execute_tx_operations,
TX_HISR_PRIORITY,
&(uart->tx_hisr_stack[0]),
TX_HISR_STACK_SIZE) != NU_SUCCESS)
return (FD_INTERNAL_ERR);
memset (&(uart->v24_hisr_stack[0]), 0xFE, V24_HISR_STACK_SIZE);
if (NU_Create_HISR (&(uart->v24_hisr_ctrl_block),
"UAF_V24",
hisr_execute_v24_operations,
V24_HISR_PRIORITY,
&(uart->v24_hisr_stack[0]),
V24_HISR_STACK_SIZE) != NU_SUCCESS)
return (FD_INTERNAL_ERR);
/*
* Create the HISR used to send a break.
* A return is used to simplify the code if an error occurs.
* The stack is entirely filled with the pattern 0xFE.
*/
memset (&(uart->break_hisr_stack[0]), 0xFE, BREAK_HISR_STACK_SIZE);
if (NU_Create_HISR (&(uart->break_hisr_ctrl_block),
"UAF_Brk",
hisr_start_break,
BREAK_HISR_PRIORITY,
&(uart->break_hisr_stack[0]),
BREAK_HISR_STACK_SIZE) != NU_SUCCESS)
return (FD_INTERNAL_ERR);
/*
* Create a timer used in the break HISR.
* A return is used to simplify the code if an error occurs.
*/
if (NU_Create_Timer (&CTShigh_Timer,
"Sleep",
CTShigh_timeout,
0, /* Parameter supplied to the routine: not used. */
1, /* This parameter is set when the timer is reset. */
0, /* The timer expires once. */
NU_DISABLE_TIMER) != NU_SUCCESS)
return (FD_INTERNAL_ERR);
}
uart->baudrate = baudrate_value[FD_BAUD_115200];
uart->bits_per_char = 10;
/*
* Select the current array used to store received bytes.
*/
uart->rx_buffer_used_by_rx_lisr = &(uart->rx_fifo_byte_2[0]);
uart->rx_buffer_used_by_rx_hisr = &(uart->rx_fifo_byte_2[0]);
/*
* RX and TX buffers.
*/
uart->buffer_size = FD_MAX_BUFFER_SIZE;
uart->rx_threshold_level = 1;
uart->tx_threshold_level = 0;
uart->rx_in = &(uart->rx_buffer[0]);
uart->rx_out = &(uart->rx_buffer[0]);
uart->tx_in = &(uart->tx_buffer[0]);
uart->tx_out = &(uart->tx_buffer[0]);
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -