Communication program for the chrobotics UM6 9-DOF IMU AHRS.

Dependencies:   MODSERIAL mbed

Committer:
lhiggs
Date:
Fri Sep 28 00:40:29 2012 +0000
Revision:
0:03c649c76388
A UM6 IMU AHRS INTERFACE

Who changed what in which revision?

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