Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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