I would like to know how to use the new Hardware UART, i.e., SCI I/O object model. It seems like some data bytes are missing when I try to read the incoming data.
When you use the hardware interrupt, you need to make some changes to your code or you could miss some incoming data. The best way to deal with RX streams is to employ two (or more) buffers and switch between the two (or more) while processing RX data.
See the following sample code snippet to understand how to use two buffers. The size of the buffer can be up to 255 bytes, but 200 bytes or above would be too large. You can experiment with the size.
In the code below, received data will be echoed (transfered) back by processRxData() function. For example, if you send in "abcdedfg," then, in some cases "abc" will be stored in buffer A and "defg" will be stored in buffer B. Alternatively, only "a" might be stored in buffer A and the rest in B. It all depends on when the interrupt occurs and io_in_ready() event returns true. However, as long as you use two buffers (or more, but not necessary) you won't miss the data.
Sample code that uses more buffers and with more detailed comment is available at: UARTtest.nc
Note that io_in_request() returns the count from the *previous* input transfer, not the current transfer. This happens so you can switch between buffers and write the logic, as seen in the sample code snippet below.
// pragmas #pragma num_alias_table_entries 0 #pragma specify_io_clock "10 MHz" // SCI_4800 etc., are declared in this header file #include <io_types.h> // SCI declaration IO_8 sci baud(SCI_4800) iosci; // Buffers and variable declarations char szRxBufA[50]; char szRxBufB[50]; unsigned short rx_count; int bufTog; // Function declaration void processRxData(char buffer[50], unsigned short rx_count); when(io_in_ready(iosci)) { // Switch to the other buffer and keep the RX count rx_count = io_in_request(iosci, (bufTog) ? szRxBufA : szRxBufB, sizeof(szRxBufA)); bufTog ^= 1; // toggle this // Do something with RX data using the filled data buffer. // It may be only a few bytes, or it may be close to 50 bytes. processRxData((bufTog) ? szRxBufA : szRxBufB, rx_count); } when (reset) { (void)io_in_request(iosci, szRxBufA, 50); bufTog = 0; // indicates that A is in use. } void processRxData(char buffer[50], unsigned short rx_count) { io_out_request(iosci, buffer, rx_count); }
- Neuron C
- Neuron Chips
- NodeBuilder 3.1
- PL 3120/PL 3150
Comments