Bug fix release
Dependents: AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more
MODSERIAL is an easy to use library that extends Serial to add fully buffered input and output.
The features of MODSERIAL include:-
- Directly compatible with Serial
- Fully customisable buffer settings
- Current buffer information via the API
- Circular buffers that allow for dynamic compile-time and/or run-time resizing
- Bulk buffer sending using MODDMA GPDMA direct memory to peripheral data transfers.
Connecting up the MODSERIAL module
The starting point for using MODSERIAL is the Mbed's own handbook for Serial library object. MODSERIAL inherits Serial and adds extensions for buffering. So getting started is easy. Follow the Mbed instructions for Serial to get setup. Here's a reproduction of Serial's simple code starter:-
1 #include "mbed.h" 2 3 Serial pc(USBTX, USBRX); // tx, rx 4 5 int main() { 6 pc.printf("Hello World!"); 7 while(1) { 8 pc.putc(pc.getc() + 1); 9 } 10 }
All we need to do to use MODSERIAL is to add a #include and alter one line thus:-
1 #include "mbed.h" 2 #include "MODSERIAL.h" 3 MODSERIAL pc(USBTX, USBRX); // tx, rx 4 5 int main() { 6 pc.printf("Hello World!"); 7 while(1) { 8 pc.putc(pc.getc() + 1); 9 } 10 }
As we can see, all we have done is add the header at line 2 and changed line 3 to specify the use of MODSERIAL in replacement for Serial. The default settings for MODSERIAL are that both the TX and RX buffers are assigned 256 bytes each of storage space. This storage space is acquired from the heap using malloc.
The default buffer assignment can be manipulated in three ways. First is the compile time setting which alters the default parameters used when creating a MODSERIAL object. This is done thus:-
1 #include "mbed.h" 2 3 #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 512 4 #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 1024 5 #include "MODSERIAL.h" 6 7 MODSERIAL pc(USBTX, USBRX); // tx, rx 8 ...
By defining the two #defines before the #include "MODSERIAL.h" alters the defaults MODSERIAL uses to create it's buffers.
The second method is the run-time version. To get TX at 1024 and RX buffer at 512 as above during run-time initialisation, alter the constructor thus:-
1 #include "mbed.h" 2 #include "MODSERIAL.h" 3 4 // Make TX buffer 1024bytes and RX buffer use 512bytes. 5 MODSERIAL pc(USBTX, USBRX, 1024, 512); // tx, rx 6 ...
If you supply only one numeric value, as shown below, both TX and RX will have the same buffer sizes assigned to them:-
1 #include "mbed.h" 2 #include "MODSERIAL.h" 3 4 // Make both TX and RX use a 512byte buffer. 5 MODSERIAL pc(USBTX, USBRX, 512); // tx, rx 6 ...
The third method is reassigning a new buffer while the program is running. This allows the program to grow and shrink either buffer as required. However, there are caveats to do this as will be shown below.
First, expanding the buffer involves increasing the buffer size. This is fairly straight forward and is accomplished thus:-
1 #include "mbed.h" 2 #include "MODSERIAL.h" 3 MODSERIAL pc(USBTX, USBRX); // tx, rx 4 5 int main() { 6 7 // Increase the TX buffer from the default 256bytes to 1024bytes. 8 if (pc.txBufferSetSize(1024) != MODSERIAL::Ok) { 9 error("Failed to allocate memory for new buffer"); 10 } 11 12 pc.printf("Hello World!"); 13 while(1) { 14 pc.putc(pc.getc() + 1); 15 } 16 }
As can be seen, growing the buffer is fairly straight forward. However, how it is done should be understood by the user. First, a new buffer allocation is made using malloc. Once acquired the current buffer is checked for contents. If the current buffer is not empty it is copied to the new buffer so the old buffer contents is maintained after resizing. The last step is then to free() the old memory buffer.
The buffer can also be contracted to a smaller length buffer. Here's the code:-
1 #include "mbed.h" 2 #include "MODSERIAL.h" 3 MODSERIAL pc(USBTX, USBRX); // tx, rx 4 5 int main() { 6 int result; 7 8 // Decrease the TX buffer from the default 256bytes to 32bytes. 9 result = pc.txBufferSetSize(32); 10 if (result != MODSERIAL::Ok) { 11 switch(result) { 12 case MODSERIAL::BufferOversize: 13 error("Contents too big to fit into new allocation"); 14 break; 15 case MODSERIAL::NoMemory: 16 error("Not enough memory for new allocation"); 17 break; 18 } 19 } 11 12 pc.printf("Hello World!"); 13 while(1) { 14 pc.putc(pc.getc() + 1); 15 } 16 }
Since buffer resizing involves the copying over of any existing old buffer contents the possibility exists that the current buffer contains more bytes than will fit into the new requested buffer. In these conditions the user must handle the return value of the resize functions. If the contents are of no concern then calling txBufferFlush() to empty of the contents before resizing.
MODSERIAL Interrupts
Users of Serial will be familar with the fact that you can attach functions or methods to TxIrq or RxIrq. This attachment of callbacks allows users to have Interrupt Service Routines (ISR) for both the TX and RX channel of the Uart. MODSERIAL uses both of these callbacks to maintain it's buffers and so are not available to users. However, MODSERIAL does contain five potential callbacks the user can use. These are:-
- TxIrq - This callback is used to inform the user's program that a character was transferred from the TX buffer to the Uart's TX THR FIFO.
- RxIrq - This callback is used to inform the user's program that a character was transferred from the Uart's RX FIFO RBR to the RX buffer.
- RxOvIrq - This callback is used to inform the user's program that a character in the Uart's RX FIFO RBR failed to transfer to the RX buffer because the RX buffer was full. The failed byte is availble via xxGetLastChar() methods.
- TxOvIrq - As RX overflow above
- TxEmpty - This callback is made when the last byte in the TX buffer is transferred to the Uart's TX THR FIFO. It informs the user's program that the TX buffer has become empty. However, it does not mean transmission is complete. See the example1.cpp example for more information.
Delineating "packets"
Many devices send information on RS232 interfaces in distinct "packets". As an example of this is NMEA information sent by many GPS modules. Each NMEA sentence is delineated by a '\n' newline character. Each sentence can be of vary length depending upon the information being sent, however, all are seperated by a '\n' newline. Detecting this if very simple with MODSERIAL. Here's an example:-
#include "mbed.h" #include "MODSERIAL.h" // Connect the TX of the GPS module to p10 RX input MODSERIAL gps(NC, p10); bool newline_detected = false; // Called everytime a new character goes into // the RX buffer. Test that character for \n // Note, rxGetLastChar() gets the last char that // we received but it does NOT remove it from // the RX buffer. void rxCallback(MODSERIAL_IRQ_INFO *q) { MODSERIAL *serial = q->serial; if ( serial->rxGetLastChar() == '\n') { newline_detected = true; } } int main() { gps.baud(9600); gps.attach(&rxCallback, MODSERIAL::RxIrq); // Wait here until we detect the \n going into the buffer. while (! newline_detected ) ; // When we get here the RX buffer now contains a NMEA sentence. // ... }
Note, the txGetLastChar() and rxGetLastChar() methods only return the last character but they do not remove that character from the associated buffer.
If this is your first time using MODSERIAL or would just like to test it out then see the example.cpp that comes with the library.
example_dma.cpp@11:a93a62eeeb9d, 2010-11-24 (annotated)
- Committer:
- AjK
- Date:
- Wed Nov 24 00:33:40 2010 +0000
- Revision:
- 11:a93a62eeeb9d
- Parent:
- 9:b3cdae80e7a9
- Child:
- 12:8c7394e2ae7f
1.11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 9:b3cdae80e7a9 | 1 | #ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA |
AjK | 9:b3cdae80e7a9 | 2 | |
AjK | 9:b3cdae80e7a9 | 3 | /* |
AjK | 9:b3cdae80e7a9 | 4 | * To run this test program, link p9 to p10 so the Serial loops |
AjK | 9:b3cdae80e7a9 | 5 | * back and receives characters it sends. |
AjK | 9:b3cdae80e7a9 | 6 | */ |
AjK | 9:b3cdae80e7a9 | 7 | |
AjK | 9:b3cdae80e7a9 | 8 | #include "mbed.h" |
AjK | 9:b3cdae80e7a9 | 9 | |
AjK | 9:b3cdae80e7a9 | 10 | /* Note, this example requires that you also import into the Mbed |
AjK | 9:b3cdae80e7a9 | 11 | compiler the MODDMA project as well as MODSERIAL |
AjK | 9:b3cdae80e7a9 | 12 | http://mbed.org/users/AjK/libraries/MODDMA/latest |
AjK | 9:b3cdae80e7a9 | 13 | MODDMA.h MUST come before MODSERIAL.h */ |
AjK | 9:b3cdae80e7a9 | 14 | #include "MODDMA.h" // <--- Declare first |
AjK | 9:b3cdae80e7a9 | 15 | #include "MODSERIAL.h" // Flollowed by MODSERIAL |
AjK | 9:b3cdae80e7a9 | 16 | |
AjK | 9:b3cdae80e7a9 | 17 | DigitalOut led1(LED1); |
AjK | 9:b3cdae80e7a9 | 18 | DigitalOut led2(LED2); |
AjK | 9:b3cdae80e7a9 | 19 | DigitalOut led3(LED3); |
AjK | 9:b3cdae80e7a9 | 20 | DigitalOut led4(LED4); |
AjK | 9:b3cdae80e7a9 | 21 | |
AjK | 9:b3cdae80e7a9 | 22 | MODSERIAL pc(USBTX, USBRX); |
AjK | 9:b3cdae80e7a9 | 23 | |
AjK | 9:b3cdae80e7a9 | 24 | /* |
AjK | 9:b3cdae80e7a9 | 25 | * As experiement, you can define MODSERIAL as show here and see what |
AjK | 9:b3cdae80e7a9 | 26 | * effects it has on the LEDs. |
AjK | 9:b3cdae80e7a9 | 27 | * |
AjK | 9:b3cdae80e7a9 | 28 | * MODSERIAL uart(TX_PIN, RX_PIN, 512); |
AjK | 9:b3cdae80e7a9 | 29 | * With this, the 512 characters sent can straight into the buffer |
AjK | 9:b3cdae80e7a9 | 30 | * vary quickly. This means LED1 is only on briefly as the TX buffer |
AjK | 9:b3cdae80e7a9 | 31 | * fills. |
AjK | 9:b3cdae80e7a9 | 32 | * |
AjK | 9:b3cdae80e7a9 | 33 | * MODSERIAL uart(TX_PIN, RX_PIN, 32); |
AjK | 9:b3cdae80e7a9 | 34 | * With this, the buffer is smaller than the default 256 bytes and |
AjK | 9:b3cdae80e7a9 | 35 | * therefore LED1 stays on much longer while the system waits for |
AjK | 9:b3cdae80e7a9 | 36 | * room in the TX buffer. |
AjK | 9:b3cdae80e7a9 | 37 | */ |
AjK | 9:b3cdae80e7a9 | 38 | MODSERIAL uart(TX_PIN, RX_PIN); |
AjK | 9:b3cdae80e7a9 | 39 | |
AjK | 9:b3cdae80e7a9 | 40 | MODDMA dma; |
AjK | 9:b3cdae80e7a9 | 41 | |
AjK | 9:b3cdae80e7a9 | 42 | // This function is called when a character goes from the TX buffer |
AjK | 9:b3cdae80e7a9 | 43 | // to the Uart THR FIFO register. |
AjK | 9:b3cdae80e7a9 | 44 | void txCallback(void) { |
AjK | 9:b3cdae80e7a9 | 45 | led2 = !led2; |
AjK | 9:b3cdae80e7a9 | 46 | } |
AjK | 9:b3cdae80e7a9 | 47 | |
AjK | 9:b3cdae80e7a9 | 48 | // This function is called when TX buffer goes empty |
AjK | 9:b3cdae80e7a9 | 49 | void txEmpty(void) { |
AjK | 9:b3cdae80e7a9 | 50 | led2 = 0; |
AjK | 9:b3cdae80e7a9 | 51 | pc.puts(" Done. "); |
AjK | 9:b3cdae80e7a9 | 52 | } |
AjK | 9:b3cdae80e7a9 | 53 | |
AjK | 9:b3cdae80e7a9 | 54 | void dmaComplete(void) { |
AjK | 9:b3cdae80e7a9 | 55 | led1 = 1; |
AjK | 9:b3cdae80e7a9 | 56 | } |
AjK | 9:b3cdae80e7a9 | 57 | |
AjK | 9:b3cdae80e7a9 | 58 | // This function is called when a character goes into the RX buffer. |
AjK | 9:b3cdae80e7a9 | 59 | void rxCallback(void) { |
AjK | 9:b3cdae80e7a9 | 60 | led3 = !led3; |
AjK | 9:b3cdae80e7a9 | 61 | pc.putc(uart.getc()); |
AjK | 9:b3cdae80e7a9 | 62 | } |
AjK | 9:b3cdae80e7a9 | 63 | |
AjK | 9:b3cdae80e7a9 | 64 | int main() { |
AjK | 9:b3cdae80e7a9 | 65 | char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* "; |
AjK | 9:b3cdae80e7a9 | 66 | int c = 'A'; |
AjK | 9:b3cdae80e7a9 | 67 | |
AjK | 9:b3cdae80e7a9 | 68 | // Tell MODSERIAL where the MODDMA controller is. |
AjK | 9:b3cdae80e7a9 | 69 | pc.MODDMA( &dma ); |
AjK | 9:b3cdae80e7a9 | 70 | |
AjK | 9:b3cdae80e7a9 | 71 | // Ensure the baud rate for the PC "USB" serial is much |
AjK | 9:b3cdae80e7a9 | 72 | // higher than "uart" baud rate below. |
AjK | 9:b3cdae80e7a9 | 73 | pc.baud( PC_BAUD ); |
AjK | 9:b3cdae80e7a9 | 74 | |
AjK | 9:b3cdae80e7a9 | 75 | // Use a deliberatly slow baud to fill up the TX buffer |
AjK | 9:b3cdae80e7a9 | 76 | uart.baud(1200); |
AjK | 9:b3cdae80e7a9 | 77 | |
AjK | 9:b3cdae80e7a9 | 78 | uart.attach( &txCallback, MODSERIAL::TxIrq ); |
AjK | 9:b3cdae80e7a9 | 79 | uart.attach( &rxCallback, MODSERIAL::RxIrq ); |
AjK | 9:b3cdae80e7a9 | 80 | uart.attach( &txEmpty, MODSERIAL::TxEmpty ); |
AjK | 9:b3cdae80e7a9 | 81 | |
AjK | 9:b3cdae80e7a9 | 82 | // Loop sending characters. We send 512 |
AjK | 9:b3cdae80e7a9 | 83 | // which is twice the default TX/RX buffer size. |
AjK | 9:b3cdae80e7a9 | 84 | |
AjK | 9:b3cdae80e7a9 | 85 | led1 = 0; |
AjK | 9:b3cdae80e7a9 | 86 | |
AjK | 9:b3cdae80e7a9 | 87 | // Send the buffer s using DMA channel 7 |
AjK | 11:a93a62eeeb9d | 88 | pc.attach_dmaSendComplete( &dmaComplete ); |
AjK | 9:b3cdae80e7a9 | 89 | pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 ); |
AjK | 9:b3cdae80e7a9 | 90 | |
AjK | 9:b3cdae80e7a9 | 91 | for (int loop = 0; loop < 512; loop++) { |
AjK | 9:b3cdae80e7a9 | 92 | uart.printf("%c", c); |
AjK | 9:b3cdae80e7a9 | 93 | c++; |
AjK | 9:b3cdae80e7a9 | 94 | if (c > 'Z') c = 'A'; |
AjK | 9:b3cdae80e7a9 | 95 | } |
AjK | 9:b3cdae80e7a9 | 96 | |
AjK | 9:b3cdae80e7a9 | 97 | led1 = 0; // Show the end of sending by switching off LED1. |
AjK | 9:b3cdae80e7a9 | 98 | |
AjK | 9:b3cdae80e7a9 | 99 | // End program. Flash LED4. Notice how LED 2 and 3 continue |
AjK | 9:b3cdae80e7a9 | 100 | // to flash for a short period while the interrupt system |
AjK | 9:b3cdae80e7a9 | 101 | // continues to send the characters left in the TX buffer. |
AjK | 9:b3cdae80e7a9 | 102 | |
AjK | 9:b3cdae80e7a9 | 103 | while(1) { |
AjK | 9:b3cdae80e7a9 | 104 | led4 = !led4; |
AjK | 9:b3cdae80e7a9 | 105 | wait(0.25); |
AjK | 9:b3cdae80e7a9 | 106 | } |
AjK | 9:b3cdae80e7a9 | 107 | } |
AjK | 9:b3cdae80e7a9 | 108 | |
AjK | 9:b3cdae80e7a9 | 109 | /* |
AjK | 9:b3cdae80e7a9 | 110 | * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host |
AjK | 9:b3cdae80e7a9 | 111 | * machine that is connected to the "pc" USB serial port. |
AjK | 9:b3cdae80e7a9 | 112 | * |
AjK | 9:b3cdae80e7a9 | 113 | * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE |
AjK | 9:b3cdae80e7a9 | 114 | * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA |
AjK | 9:b3cdae80e7a9 | 115 | * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW |
AjK | 9:b3cdae80e7a9 | 116 | * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS |
AjK | 9:b3cdae80e7a9 | 117 | * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO |
AjK | 9:b3cdae80e7a9 | 118 | * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK |
AjK | 9:b3cdae80e7a9 | 119 | * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG |
AjK | 9:b3cdae80e7a9 | 120 | * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R |
AjK | 9:b3cdae80e7a9 | 121 | * |
AjK | 9:b3cdae80e7a9 | 122 | * Note how the DMA blocks the TX buffer sending under standard interrupt control. |
AjK | 9:b3cdae80e7a9 | 123 | * Not until the DMA transfer is complete will "normal" buffered TX sending resume. |
AjK | 9:b3cdae80e7a9 | 124 | * |
AjK | 9:b3cdae80e7a9 | 125 | * Of interest is that last "R" character after the system has said "Done." |
AjK | 9:b3cdae80e7a9 | 126 | * This comes from the fact that the TxEmpty callback is made when the TX buffer |
AjK | 9:b3cdae80e7a9 | 127 | * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the |
AjK | 9:b3cdae80e7a9 | 128 | * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that |
AjK | 9:b3cdae80e7a9 | 129 | * when the TxEmpty callback is made, the TX buffer is empty, but that just means |
AjK | 9:b3cdae80e7a9 | 130 | * the "last few characters" were written to the TX FIFO. So although the TX |
AjK | 9:b3cdae80e7a9 | 131 | * buffer has gone empty, the Uart's transmit system is still sending any remaining |
AjK | 9:b3cdae80e7a9 | 132 | * characters from it's TX FIFO. If you want to be truely sure all the characters |
AjK | 9:b3cdae80e7a9 | 133 | * you have sent have left the Mbed then call txIsBusy(); This function will |
AjK | 9:b3cdae80e7a9 | 134 | * return true if characters are still being sent. If it returns false after |
AjK | 9:b3cdae80e7a9 | 135 | * the Tx buffer is empty then all your characters have been sent. |
AjK | 9:b3cdae80e7a9 | 136 | * |
AjK | 9:b3cdae80e7a9 | 137 | * In a similar way, when characters are received into the RX FIFO, the entire |
AjK | 9:b3cdae80e7a9 | 138 | * FIFO contents is moved to the RX buffer, assuming there is room left in the |
AjK | 9:b3cdae80e7a9 | 139 | * RX buffer. If there is not, any remaining characters are left in the RX FIFO |
AjK | 9:b3cdae80e7a9 | 140 | * and will be moved to the RX buffer on the next interrupt or when the running |
AjK | 9:b3cdae80e7a9 | 141 | * program removes a character(s) from the RX buffer with the getc() method. |
AjK | 9:b3cdae80e7a9 | 142 | */ |
AjK | 9:b3cdae80e7a9 | 143 | |
AjK | 9:b3cdae80e7a9 | 144 | #endif |