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