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