]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/modserial/main.cpp
2 * To run this test program, link p9 to p10 so the Serial loops
3 * back and receives characters it sends.
10 DigitalOut
led3(LED3
);
11 DigitalOut
led4(LED4
);
13 MODSERIAL
pc(USBTX
, USBRX
);
16 * As experiement, you can define MODSERIAL as show here and see what
17 * effects it has on the LEDs.
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
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.
29 MODSERIAL
uart(p9
, p10
);
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
) {
37 // This function is called when TX buffer goes empty
38 void txEmpty(MODSERIAL_IRQ_INFO
*q
) {
43 // This function is called when a character goes into the RX buffer.
44 void rxCallback(MODSERIAL_IRQ_INFO
*q
) {
52 // Ensure the baud rate for the PC "USB" serial is much
53 // higher than "uart" baud rate below. (default: 9600)
56 // Use a deliberatly slow baud to fill up the TX buffer
59 uart
.attach(&txCallback
, MODSERIAL::ModTxIrq
);
60 uart
.attach(&rxCallback
, MODSERIAL::ModRxIrq
);
61 uart
.attach(&txEmpty
, MODSERIAL::ModTxEmpty
);
63 // Loop sending characters. We send 512
64 // which is twice the default TX/RX buffer size.
66 led1
= 1; // Show start of sending with LED1.
68 for (int loop
= 0; loop
< 512; loop
++) {
74 led1
= 0; // Show the end of sending by switching off LED1.
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.
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.
90 * ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV
91 * WXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR
92 * STUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMN
93 * OPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ
94 * KLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF
95 * GHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZAB
96 * CDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
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.
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.