Skip to content

Serial discard line break #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions cores/arduino/UARTClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ int UARTClass::read( void )

void UARTClass::flush( void )
{
while (_tx_buffer->_iHead != _tx_buffer->_iTail); //wait for transmit data to be sent
while (_tx_buffer->_iHead != *(volatile int*)&(_tx_buffer->_iTail)); //wait for transmit data to be sent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason for casting the pointer to a volatile pointer first prior to using it? Is the code being optimized out by the compiler if the volatile casting is omitted?

Copy link
Contributor Author

@descampsa descampsa Oct 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that iTail can be updated in the irq handler.
Without the volatile, the compiler may optimize the code by reading it only once from memory, and keeping its value in register. That would lead to an infinite loop.

I don't remember if i actually ran into this problem or not, and i did not check the resulting assembly, but that looks like a typical case where volatile is needed.

Note that this more related to pull request #266 than to #274

// Wait for transmission to complete
while(!uart_tx_complete(CONFIG_UART_CONSOLE_INDEX));
}
Expand All @@ -179,11 +179,11 @@ size_t UARTClass::write( const uint8_t uc_data )
return(0);

// Is the hardware currently busy?
if (_tx_buffer->_iTail != _tx_buffer->_iHead)
if (_tx_buffer->_iTail != _tx_buffer->_iHead || !uart_tx_ready(CONFIG_UART_CONSOLE_INDEX))
{
// If busy we buffer
int l = (_tx_buffer->_iHead + 1) % SERIAL_BUFFER_SIZE;
while (_tx_buffer->_iTail == l)
while (*(volatile int*)&(_tx_buffer->_iTail) == l)
; // Spin locks if we're about to overwrite the buffer. This continues once the data is sent

_tx_buffer->_aucBuffer[_tx_buffer->_iHead] = uc_data;
Expand All @@ -201,17 +201,22 @@ size_t UARTClass::write( const uint8_t uc_data )

void UARTClass::IrqHandler( void )
{
uint8_t uc_data;
int ret;
ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data);

while ( ret != -1 ) {
_rx_buffer->store_char(uc_data);
uart_irq_update(CONFIG_UART_CONSOLE_INDEX);
// if irq is Receiver Data Available
if(uart_irq_rx_ready(CONFIG_UART_CONSOLE_INDEX))
{
uint8_t uc_data;
int ret;
ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data);

while ( ret != -1 ) {
_rx_buffer->store_char(uc_data);
ret = uart_poll_in(CONFIG_UART_CONSOLE_INDEX, &uc_data);
}
}

// Do we need to keep sending data?
if (!uart_irq_tx_ready(CONFIG_UART_CONSOLE_INDEX))
// if irq is Transmitter Holding Register
else if(uart_irq_tx_ready(CONFIG_UART_CONSOLE_INDEX))
{
if (_tx_buffer->_iTail != _tx_buffer->_iHead) {
uart_poll_out(CONFIG_UART_CONSOLE_INDEX, _tx_buffer->_aucBuffer[_tx_buffer->_iTail]);
Expand Down
15 changes: 14 additions & 1 deletion system/libarc32_arduino101/drivers/ns16550.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ unsigned char uart_poll_out(
)
{
/* wait for transmitter to ready to accept a character */
while ((INBYTE(LSR(which)) & LSR_TEMT) == 0)
while ((INBYTE(LSR(which)) & LSR_THRE) == 0)
;

OUTBYTE(THR(which), outChar);
Expand Down Expand Up @@ -640,6 +640,19 @@ uint8_t uart_tx_complete(int which)
return INBYTE(LSR(which)) & LSR_TEMT;
}

/*******************************************************************************
*
* uart_tx_complete - check if tx holding register is empty
*
* RETURNS: zero if register is non-empty,
* non-zero if register is empty (ready to receive new data)
*/

uint8_t uart_tx_ready(int which)
{
return INBYTE(LSR(which)) & LSR_THRE;
}

/*******************************************************************************
*
* uart_loop_enable - enable loopback
Expand Down
1 change: 1 addition & 0 deletions system/libarc32_arduino101/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int uart_break_check(int port);
void uart_break_send(int port, int delay);
void uart_disable(int port);
uint8_t uart_tx_complete(int which);
uint8_t uart_tx_ready(int which);
void uart_loop_enable(int which);
void uart_loop_disable(int which);

Expand Down