MODSERIAL with support for more devices

Dependents:   1D-Pong BMT-K9_encoder BMT-K9-Regelaar programma_filter ... more

Check the cookbook page for more information: https://mbed.org/cookbook/MODSERIAL

Did you add a device? Please send a pull request so we can keep everything in one library instead of many copies. In that case also send a PM, since currently mbed does not inform of new pull requests. I will then also add you to the developers of this library so you can do other changes directly.

Committer:
AjK
Date:
Sat Nov 20 16:54:05 2010 +0000
Revision:
0:eb2522b41db8
Child:
2:b936b4acbd92
1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:eb2522b41db8 1 #ifdef COMPILE_EXAMPLE_CODE_MODSERIAL
AjK 0:eb2522b41db8 2
AjK 0:eb2522b41db8 3 /*
AjK 0:eb2522b41db8 4 * To run this test program, link p9 to p10 so the Serial loops
AjK 0:eb2522b41db8 5 * back and receives characters it sends.
AjK 0:eb2522b41db8 6 */
AjK 0:eb2522b41db8 7
AjK 0:eb2522b41db8 8 #include "mbed.h"
AjK 0:eb2522b41db8 9 #include "MODSERIAL.h"
AjK 0:eb2522b41db8 10
AjK 0:eb2522b41db8 11 DigitalOut led1(LED1);
AjK 0:eb2522b41db8 12 DigitalOut led2(LED2);
AjK 0:eb2522b41db8 13 DigitalOut led3(LED3);
AjK 0:eb2522b41db8 14 DigitalOut led4(LED4);
AjK 0:eb2522b41db8 15
AjK 0:eb2522b41db8 16 MODSERIAL pc(USBTX, USBRX);
AjK 0:eb2522b41db8 17
AjK 0:eb2522b41db8 18 /*
AjK 0:eb2522b41db8 19 * As experiement, you can define MODSERIAL as show here and see what
AjK 0:eb2522b41db8 20 * effects it has on the LEDs.
AjK 0:eb2522b41db8 21 *
AjK 0:eb2522b41db8 22 * MODSERIAL uart(TX_PIN, RX_PIN, 512);
AjK 0:eb2522b41db8 23 * With this, the 512 characters sent can straight into the buffer
AjK 0:eb2522b41db8 24 * vary quickly. This means LED1 is only on briefly as the TX buffer
AjK 0:eb2522b41db8 25 * fills.
AjK 0:eb2522b41db8 26 *
AjK 0:eb2522b41db8 27 * MODSERIAL uart(TX_PIN, RX_PIN, 32);
AjK 0:eb2522b41db8 28 * With this, the buffer is smaller than the default 256 bytes and
AjK 0:eb2522b41db8 29 * therefore LED1 stays on much longer while the system waits for
AjK 0:eb2522b41db8 30 * room in the TX buffer.
AjK 0:eb2522b41db8 31 */
AjK 0:eb2522b41db8 32 MODSERIAL uart(TX_PIN, RX_PIN);
AjK 0:eb2522b41db8 33
AjK 0:eb2522b41db8 34 // This function is called when a character goes from the TX buffer
AjK 0:eb2522b41db8 35 // to the Uart THR FIFO register.
AjK 0:eb2522b41db8 36 void txCallback(void) {
AjK 0:eb2522b41db8 37 led2 = !led2;
AjK 0:eb2522b41db8 38 }
AjK 0:eb2522b41db8 39
AjK 0:eb2522b41db8 40 // This function is called when TX buffer goes empty
AjK 0:eb2522b41db8 41 void txEmpty(void) {
AjK 0:eb2522b41db8 42 led2 = 0;
AjK 0:eb2522b41db8 43 pc.puts(" Done. ");
AjK 0:eb2522b41db8 44 }
AjK 0:eb2522b41db8 45
AjK 0:eb2522b41db8 46 // This function is called when a character goes into the RX buffer.
AjK 0:eb2522b41db8 47 void rxCallback(void) {
AjK 0:eb2522b41db8 48 led3 = !led3;
AjK 0:eb2522b41db8 49 pc.putc(uart.getc());
AjK 0:eb2522b41db8 50 }
AjK 0:eb2522b41db8 51
AjK 0:eb2522b41db8 52 int main() {
AjK 0:eb2522b41db8 53 int c = 'A';
AjK 0:eb2522b41db8 54
AjK 0:eb2522b41db8 55 // Ensure the baud rate for the PC "USB" serial is much
AjK 0:eb2522b41db8 56 // higher than "uart" baud rate below.
AjK 0:eb2522b41db8 57 pc.baud(115200);
AjK 0:eb2522b41db8 58
AjK 0:eb2522b41db8 59 // Use a deliberatly slow baud to fill up the TX buffer
AjK 0:eb2522b41db8 60 uart.baud(1200);
AjK 0:eb2522b41db8 61
AjK 0:eb2522b41db8 62 uart.attach(&txCallback, MODSERIAL::TxIrq);
AjK 0:eb2522b41db8 63 uart.attach(&rxCallback, MODSERIAL::RxIrq);
AjK 0:eb2522b41db8 64 uart.attach(&txEmpty, MODSERIAL::TxEmpty);
AjK 0:eb2522b41db8 65
AjK 0:eb2522b41db8 66 // Loop sending characters. We send 512
AjK 0:eb2522b41db8 67 // which is twice the default TX/RX buffer size.
AjK 0:eb2522b41db8 68
AjK 0:eb2522b41db8 69 led1 = 1; // Show start of sending with LED1.
AjK 0:eb2522b41db8 70
AjK 0:eb2522b41db8 71 for (int loop = 0; loop < 512; loop++) {
AjK 0:eb2522b41db8 72 uart.printf("%c", c);
AjK 0:eb2522b41db8 73 c++;
AjK 0:eb2522b41db8 74 if (c > 'Z') c = 'A';
AjK 0:eb2522b41db8 75 }
AjK 0:eb2522b41db8 76
AjK 0:eb2522b41db8 77 led1 = 0; // Show the end of sending by switching off LED1.
AjK 0:eb2522b41db8 78
AjK 0:eb2522b41db8 79 // End program. Flash LED4. Notice how LED 2 and 3 continue
AjK 0:eb2522b41db8 80 // to flash for a short period while the interrupt system
AjK 0:eb2522b41db8 81 // continues to send the characters left in the TX buffer.
AjK 0:eb2522b41db8 82
AjK 0:eb2522b41db8 83 while(1) {
AjK 0:eb2522b41db8 84 led4 = !led4;
AjK 0:eb2522b41db8 85 wait(0.25);
AjK 0:eb2522b41db8 86 }
AjK 0:eb2522b41db8 87 }
AjK 0:eb2522b41db8 88
AjK 0:eb2522b41db8 89 /*
AjK 0:eb2522b41db8 90 * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
AjK 0:eb2522b41db8 91 * machine that is connected to the "pc" USB serial port.
AjK 0:eb2522b41db8 92 *
AjK 0:eb2522b41db8 93 * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
AjK 0:eb2522b41db8 94 * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
AjK 0:eb2522b41db8 95 * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
AjK 0:eb2522b41db8 96 * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
AjK 0:eb2522b41db8 97 * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
AjK 0:eb2522b41db8 98 * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
AjK 0:eb2522b41db8 99 * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
AjK 0:eb2522b41db8 100 *
AjK 0:eb2522b41db8 101 * Of interest is that last "R" character after the system has said "Done."
AjK 0:eb2522b41db8 102 * This comes from the fact that the TxEmpty callback is made when the TX buffer
AjK 0:eb2522b41db8 103 * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
AjK 0:eb2522b41db8 104 * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
AjK 0:eb2522b41db8 105 * when the TxEmpty callback is made, the TX buffer is empty, but that just means
AjK 0:eb2522b41db8 106 * the "last few characters" were written to the TX FIFO. So although the TX
AjK 0:eb2522b41db8 107 * buffer has gone empty, the Uart's transmit system is still sending any remaining
AjK 0:eb2522b41db8 108 * characters from it's TX FIFO. If you want to be truely sure all the characters
AjK 0:eb2522b41db8 109 * you have sent have left the Mbed then call txIsBusy(); This function will
AjK 0:eb2522b41db8 110 * return true if characters are still being sent. If it returns false after
AjK 0:eb2522b41db8 111 * the Tx buffer is empty then all your characters have been sent.
AjK 0:eb2522b41db8 112 *
AjK 0:eb2522b41db8 113 * In a similar way, when characters are received into the RX FIFO, the entire
AjK 0:eb2522b41db8 114 * FIFO contents is moved to the RX buffer, assuming there is room left in the
AjK 0:eb2522b41db8 115 * RX buffer. If there is not, any remaining characters are left in the RX FIFO
AjK 0:eb2522b41db8 116 * and will be moved to the RX buffer on the next interrupt or when the running
AjK 0:eb2522b41db8 117 * program removes a character(s) from the RX buffer with the getc() method.
AjK 0:eb2522b41db8 118 */
AjK 0:eb2522b41db8 119
AjK 0:eb2522b41db8 120 #endif