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