Penn Electric Racing / Mbed 2 deprecated SystemManagement

Dependencies:   mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP

Fork of SystemManagement by Martin Deng

Committer:
pspatel321
Date:
Sun Nov 16 02:43:58 2014 +0000
Revision:
32:e70407021ad2
Parent:
31:7eaa5e881b56
Changed watchdog to 110ms from 250ms.  Forgot to change it back.

Who changed what in which revision?

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