WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation


Changes a hardware UART receive mode between interrupt driven and polling modes.
UARTs normally work in interrupt driven mode and potentially have a receive queue to store all the received characters. However: when using half duplex mode you only expect to receive data after you have sent something ie you 'talk' then 'listen'. When running at very high baud rates (eg 1,000,000 baud) then the processing overhead of the interrupt handling plus the queue handling can be slow enough for you to loose incoming data. Hardware interrupts from other devices only make the problem worse.
In this scenario it is better to put the receiver into 'polling' mode and then poll the UART for each incoming byte. It may even be necessary to bracket the code with a CRITICAL_SECTION so that no other interrupts cause data to be lost. But you will definitely need to make sure that you have some kind of timeout just in case you don't receive a response as this would cause everything to 'die'.
This seems like a 'dangerous' thing to do but don't forget that we are doing it because the data rate is so high. Therefore the time it takes to receive the data is actually very short.
UartHW. setPollingMode(polling)
Where UartHW is the name you have given to the device in Project Designer.
TRUE for polling mode or FALSE for interrupt mode.
If you want to use this in your own code then here is a half duplex example for 'myUart' which expects a 10 byte response:
// Put uart into polling mode for receive
// Assume we have sent out some data to start with
uint8_t reply[10]; // Put the reply here
uint8_t replyPos=0; // Nothing received yet
int now = 0; // Create a variable to test for timeouts
boolean timeOut = FALSE; // We haven't timed out
// Wait till transmit has finished
// Turn off hardware interrupts
    while(replyPos < sizeof(reply)){
        int ch = myUart.read();
        if(ch != -1){
            reply[replyPos++] = (uint8_t)(ch & 0xff);
            now = 0; // Reset the timeout counter
        }else if(--now == 0){
            // Timeout after a while
            timeOut = TRUE;
} // Turn interrupts back on
    cout << "We timed out!";
    // We have got some data



Valid XHTML 1.0 Transitional