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