Bug fix release

Dependents:   AntiTheftGPS XbeeReceive XbeeSend Superball_Ball2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example1.cpp Source File

example1.cpp

00001 #ifdef COMPILE_EXAMPLE1_CODE_MODSERIAL
00002 
00003 /*
00004  * To run this test program, link p9 to p10 so the Serial loops
00005  * back and receives characters it sends.
00006  */
00007  
00008 #include "mbed.h"
00009 #include "MODSERIAL.h"
00010 
00011 DigitalOut led1(LED1);
00012 DigitalOut led2(LED2);
00013 DigitalOut led3(LED3);
00014 DigitalOut led4(LED4);
00015 
00016 MODSERIAL pc(USBTX, USBRX);
00017 
00018 /*
00019  * As experiement, you can define MODSERIAL as show here and see what
00020  * effects it has on the LEDs.
00021  *
00022  * MODSERIAL uart(TX_PIN, RX_PIN, 512);
00023  *   With this, the 512 characters sent can straight into the buffer
00024  *   vary quickly. This means LED1 is only on briefly as the TX buffer
00025  *   fills.
00026  *
00027  * MODSERIAL uart(TX_PIN, RX_PIN, 32);
00028  *   With this, the buffer is smaller than the default 256 bytes and
00029  *   therefore LED1 stays on much longer while the system waits for
00030  *   room in the TX buffer.
00031  */
00032 MODSERIAL uart(TX_PIN, RX_PIN);
00033 
00034 // This function is called when a character goes from the TX buffer
00035 // to the Uart THR FIFO register.
00036 void txCallback(MODSERIAL_IRQ_INFO *q) {
00037     led2 = !led2;
00038 }
00039 
00040 // This function is called when TX buffer goes empty
00041 void txEmpty(MODSERIAL_IRQ_INFO *q) {
00042     led2 = 0;
00043     pc.puts(" Done. ");
00044 }
00045 
00046 // This function is called when a character goes into the RX buffer.
00047 void rxCallback(MODSERIAL_IRQ_INFO *q) {
00048     led3 = !led3;
00049     pc.putc(uart.getc());
00050 }
00051 
00052 int main() {
00053     int c = 'A';
00054     
00055     // Ensure the baud rate for the PC "USB" serial is much
00056     // higher than "uart" baud rate below.
00057     pc.baud(PC_BAUD);
00058     
00059     // Use a deliberatly slow baud to fill up the TX buffer
00060     uart.baud(1200);
00061     
00062     uart.attach(&txCallback, MODSERIAL::TxIrq);
00063     uart.attach(&rxCallback, MODSERIAL::RxIrq);
00064     uart.attach(&txEmpty,    MODSERIAL::TxEmpty);
00065     
00066     // Loop sending characters. We send 512
00067     // which is twice the default TX/RX buffer size.
00068     
00069     led1 = 1; // Show start of sending with LED1.
00070     
00071     for (int loop = 0; loop < 512; loop++) {
00072         uart.printf("%c", c);        
00073         c++;
00074         if (c > 'Z') c = 'A';
00075     }
00076     
00077     led1 = 0; // Show the end of sending by switching off LED1.
00078     
00079     // End program. Flash LED4. Notice how LED 2 and 3 continue
00080     // to flash for a short period while the interrupt system 
00081     // continues to send the characters left in the TX buffer.
00082     
00083     while(1) {
00084         led4 = !led4;
00085         wait(0.25);
00086     }
00087 }
00088 
00089 /*
00090  * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
00091  * machine that is connected to the "pc" USB serial port.
00092  *
00093  * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
00094  * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
00095  * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
00096  * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
00097  * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
00098  * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
00099  * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
00100  *
00101  * Of interest is that last "R" character after the system has said "Done."
00102  * This comes from the fact that the TxEmpty callback is made when the TX buffer
00103  * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the 
00104  * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
00105  * when the TxEmpty callback is made, the TX buffer is empty, but that just means
00106  * the "last few characters" were written to the TX FIFO. So although the TX
00107  * buffer has gone empty, the Uart's transmit system is still sending any remaining
00108  * characters from it's TX FIFO. If you want to be truely sure all the characters
00109  * you have sent have left the Mbed then call txIsBusy(); This function will
00110  * return true if characters are still being sent. If it returns false after
00111  * the Tx buffer is empty then all your characters have been sent.
00112  *
00113  * In a similar way, when characters are received into the RX FIFO, the entire
00114  * FIFO contents is moved to the RX buffer, assuming there is room left in the
00115  * RX buffer. If there is not, any remaining characters are left in the RX FIFO
00116  * and will be moved to the RX buffer on the next interrupt or when the running 
00117  * program removes a character(s) from the RX buffer with the getc() method.
00118  */
00119  
00120 #endif