📄 uartfax.c
字号:
* Returns : Number of bytes sent.
*
******************************************************************************/
static SYS_UWORD16
send_break (t_uart *uart)
{
SYS_UWORD16 bytes_in_tx_fifo;
bytes_in_tx_fifo = 0;
uart->break_in_progress = 1;
dev_Uart_setLineControlRegister(UAF_UART_1, dev_Uart_getLineControlRegister(UAF_UART_1) | BREAK_CONTROL);
while (uart->break_length) {
dev_Uart_setTransmitHoldingRegister(UAF_UART_1, 0x00);
uart->break_length--;
bytes_in_tx_fifo++;
}
return (bytes_in_tx_fifo);
}
/*******************************************************************************
*
* start_receiver
*
* Purpose : Deactivates DTR or RTS or sends XON.
*
* Arguments: In : uart: Pointer on the UART structure.
* Out: none
*
* Returns : none
*
******************************************************************************/
static void
start_receiver (T_tr_UartId uartNo)
{
unsigned char mcRegister;
if(UAF_GetPortType(uartNo) == UA_TYPE_UART)
{
mcRegister = dev_Uart_getModemControlRegister(UAF_UART_1);
dev_Uart_setModemControlRegister(UAF_UART_1, mcRegister|MRTS);
}
}
/*******************************************************************************
*
* stop_receiver
*
* Purpose : Activates DTR or RTS or sends XOFF.
*
* Arguments: In : uart: Pointer on the UART structure.
* Out: none
*
* Returns : none
*
******************************************************************************/
static void
stop_receiver (T_tr_UartId uartNo)
{
unsigned char mcRegister;
if(UAF_GetPortType(uartNo) == UA_TYPE_UART)
{
mcRegister = dev_Uart_getModemControlRegister(UAF_UART_1);
dev_Uart_setModemControlRegister(UAF_UART_1, mcRegister&~MRTS);
}
}
/*******************************************************************************
*
* compute_break_time
*
* Purpose : Computes a number of TDMA from 3 parameters:
* - baudrate,
* - bits per character including start bit, stop bits and parity,
* - number of characters.
* Due to the TDMA value (4.6 ms), a minimal value is sent: 2 TDMA.
*
* Arguments: In : baudrate
* bits_per_char
* number_of_chars
* Out: none
*
* Returns : The number of TDMA.
*
******************************************************************************/
static UNSIGNED
compute_break_time (UNSIGNED baudrate,
UNSIGNED bits_per_char,
UNSIGNED number_of_chars)
{
UNSIGNED number_of_tdma;
number_of_tdma = CONVERT_TIME_IN_TDMA (
1000 * bits_per_char * number_of_chars / baudrate);
if (number_of_tdma == 0)
number_of_tdma = 1;
number_of_tdma++;
return (number_of_tdma);
}
/*******************************************************************************
*
* update_reading_callback
*
* Purpose : Updates the sizes array and the addresses array and get and builds
* the state parameter defined in UAF_GetLineState to call the
* readOutFunc function.
*
* Arguments: In : uart : Pointer on the UART structure.
* call_source: 0: application, 1: HISR (Rx or V24), 3: Rx HISR
* Out: none
*
* Returns : none
*
******************************************************************************/
static void
update_reading_callback (t_uart *uart,
SYS_BOOL call_source)
{
SYS_UWORD32 state;
SYS_UWORD8 fragments_number;
SYS_UWORD16 bytes_in_rx_buffer;
volatile SYS_UWORD8 *rx_in;
/*
* Update the sizes array and the addresses array.
* A copy of rx_in is used because it may be updated by the interrupt
* handler if this function is called from the application.
*/
rx_in = uart->rx_in;
if (uart->rx_out < rx_in) {
fragments_number = 1;
uart->rd_address[0] = uart->rx_out;
uart->rd_size_before_call[0] = (SYS_UWORD16) (rx_in - uart->rx_out);
uart->rd_size_after_call[0] = uart->rd_size_before_call[0];
uart->rd_size_before_call[1] = 0;
uart->rd_size_after_call[1] = 0;
bytes_in_rx_buffer = uart->rd_size_before_call[0];
} else if (rx_in == uart->rx_out) { /* RX buffer empty. */
fragments_number = 1;
uart->rd_address[0] = uart->rx_out;
uart->rd_size_before_call[0] = 0;
uart->rd_size_after_call[0] = 0;
uart->rd_size_before_call[1] = 0;
uart->rd_size_after_call[1] = 0;
bytes_in_rx_buffer = 0;
} else {
fragments_number = 2;
uart->rd_address[0] = uart->rx_out;
uart->rd_size_before_call[0] =
uart->buffer_size + 1 - (SYS_UWORD16) (uart->rx_out -
&(uart->rx_buffer[0]));
uart->rd_size_after_call[0] = uart->rd_size_before_call[0];
uart->rd_address[1] = &(uart->rx_buffer[0]);
uart->rd_size_before_call[1] = (SYS_UWORD16) (rx_in -
&(uart->rx_buffer[0]));
uart->rd_size_after_call[1] = uart->rd_size_before_call[1];
bytes_in_rx_buffer =
uart->rd_size_before_call[0] + uart->rd_size_before_call[1];
if (!uart->rd_size_before_call[1])
fragments_number = 1;
}
/*
* Build the state parameter defined in UAF_GetLineState.
* The field state_2 is used when state_1 is set to 0 to avoid to
* lose events detected in the RX interrupt handler.
*/
state = uart->state_2;
uart->state_2 = 0;
uart->state = &(uart->state_2);
state |= uart->state_1;
uart->state_1 = 0;
uart->state = &(uart->state_1);
state |= ((((SYS_UWORD32) uart->rts_level) << RTS) |
(((SYS_UWORD32) (uart->tx_stopped_by_application |
uart->tx_stopped_by_driver)) << TXSTP) |
(((SYS_UWORD32) (uart->rx_stopped_by_application |
uart->rx_stopped_by_driver)) << RXSTP) |
(((SYS_UWORD32) (uart->buffer_size - bytes_in_rx_buffer)) << RXBLEV));
/*
* Fields SA, SB and X are set according to the flow control:
*
* None RTS/CTS XON/XOFF
* SA DTR DTR DTR
* SB RTS 0 RTS
* X 0 RTS XON:0 XOFF:1 (transmitter)
*
* DTR is supported on C, D & E-Sample.
*/
if (uart->flow_control_mode != fc_rts)
state |= (((SYS_UWORD32) uart->rts_level) << SB);
if (uart->flow_control_mode == fc_rts)
state |= (((SYS_UWORD32) uart->rts_level) << X);
else if ((uart->flow_control_mode == fc_xoff) &&
(uart->tx_stopped_by_application ||
uart->tx_stopped_by_driver))
state |= (1 << X);
/*
* Call the readOutFunc function with these parameters.
*/
uart->rd_call_setup = rm_notDefined;
if(UAF_GetPortType(USB_MODEM+1) == UA_TYPE_USB)
{
uart->rd_call_from_hisr_in_progress = 1;
uart->reading_suspended = 0;
}
(*(uart->readOutFunc)) (call_source & 0x01, /* From HISR or application */
&(uart->rd_call_setup),
fragments_number,
&(uart->rd_address[0]),
&(uart->rd_size_after_call[0]),
state);
}
/*******************************************************************************
*
* update_writing_callback
*
* Purpose : Updates the sizes array and the addresses array to call the
* writeInFunc function.
*
* Arguments: In : uart : Pointer on the UART structure.
* call_source: 0: application, 1: HISR
* Out: none
*
* Returns : none
*
******************************************************************************/
static void
update_writing_callback (t_uart *uart,
SYS_BOOL call_source)
{
SYS_UWORD8 fragments_number;
volatile SYS_UWORD8 *tx_out;
tx_out = uart->tx_out;
if (uart->tx_in < tx_out)
{
fragments_number = 1;
uart->wr_address[0] = uart->tx_in;
uart->wr_size_before_call[0] =
(SYS_UWORD16) (tx_out - uart->tx_in - 1);
uart->wr_size_after_call[0] = uart->wr_size_before_call[0];
uart->wr_size_before_call[1] = 0;
uart->wr_size_after_call[1] = 0;
}
else if (tx_out == &(uart->tx_buffer[0]))
{
fragments_number = 1;
uart->wr_address[0] = uart->tx_in;
uart->wr_size_before_call[0] =
uart->buffer_size -
(SYS_UWORD16) (uart->tx_in - &(uart->tx_buffer[0]));
uart->wr_size_after_call[0] = uart->wr_size_before_call[0];
uart->wr_size_before_call[1] = 0;
uart->wr_size_after_call[1] = 0;
}
else
{
fragments_number = 2;
uart->wr_address[0] = uart->tx_in;
uart->wr_size_before_call[0] =
uart->buffer_size + 1 -
(SYS_UWORD16) (uart->tx_in - &(uart->tx_buffer[0]));
uart->wr_size_after_call[0] = uart->wr_size_before_call[0];
uart->wr_address[1] = &(uart->tx_buffer[0]);
uart->wr_size_before_call[1] =
(SYS_UWORD16) (tx_out - &(uart->tx_buffer[0]) - 1);
uart->wr_size_after_call[1] = uart->wr_size_before_call[1];
if (!uart->wr_size_before_call[1])
fragments_number = 1;
}
uart->wr_call_setup = rm_notDefined;
if(UAF_GetPortType(USB_MODEM+1) == UA_TYPE_USB)
{
uart->wr_call_from_hisr_in_progress = 1;
}
if(uart->writeInFunc)
(*(uart->writeInFunc)) (call_source,
&(uart->wr_call_setup),
fragments_number,
&(uart->wr_address[0]),
&(uart->wr_size_after_call[0]));
}
/*******************************************************************************
*
* get_bytes_in_rx_buffer
*
* Purpose : Gets the number of bytes in the RX buffer.
*
* Arguments: In : uart: Pointer on the UART structure.
* Out: none
*
* Returns : The number of bytes in the RX buffer.
*
******************************************************************************/
static SYS_UWORD16
get_bytes_in_rx_buffer (t_uart *uart)
{
SYS_UWORD16 bytes_in_rx_buffer;
volatile SYS_UWORD8 *rx_in;
rx_in = uart->rx_in;
if (uart->rx_out <= rx_in)
bytes_in_rx_buffer = (SYS_UWORD16) (rx_in - uart->rx_out);
else
bytes_in_rx_buffer =
(SYS_UWORD16) (rx_in - uart->rx_out + uart->buffer_size + 1);
return (bytes_in_rx_buffer);
}
/*******************************************************************************
*
* get_bytes_in_tx_buffer
*
* Purpose : Gets the number of bytes in the TX buffer.
*
* Arguments: In : uart: Pointer on the UART structure.
* Out: none
*
* Returns : The number of bytes in the TX buffer.
*
******************************************************************************/
static SYS_UWORD16
get_bytes_in_tx_buffer (t_uart *uart)
{
SYS_UWORD16 bytes_in_tx_buffer;
volatile SYS_UWORD8 *tx_out;
tx_out = uart->tx_out;
if (tx_out <= uart->tx_in)
bytes_in_tx_buffer = (SYS_UWORD16) (uart->tx_in - tx_out);
else
bytes_in_tx_buffer =
(SYS_UWORD16) (uart->tx_in - tx_out + uart->buffer_size + 1);
return (bytes_in_tx_buffer);
}
/*******************************************************************************
*
* hisr_start_break
*
* Purpose : Enables the timer used to control the time without character.
*
* Arguments: In : none
* Out: none
*
* Returns : none
*
******************************************************************************/
static VOID
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -