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:
Fri Nov 14 06:05:59 2014 +0000
Revision:
31:7eaa5e881b56
Mostly working.  Wrote CAN in/out code.  Xbee still needs work.  My guess is that MODDMA breaks when used with RTOS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pspatel321 31:7eaa5e881b56 1 #ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA
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
pspatel321 31:7eaa5e881b56 10 /* Note, this example requires that you also import into the Mbed
pspatel321 31:7eaa5e881b56 11 compiler the MODDMA project as well as MODSERIAL
pspatel321 31:7eaa5e881b56 12 http://mbed.org/users/AjK/libraries/MODDMA/latest
pspatel321 31:7eaa5e881b56 13 MODDMA.h MUST come before MODSERIAL.h */
pspatel321 31:7eaa5e881b56 14 #include "MODDMA.h" // <--- Declare first
pspatel321 31:7eaa5e881b56 15 #include "MODSERIAL.h" // Flollowed by MODSERIAL
pspatel321 31:7eaa5e881b56 16
pspatel321 31:7eaa5e881b56 17 DigitalOut led1(LED1);
pspatel321 31:7eaa5e881b56 18 DigitalOut led2(LED2);
pspatel321 31:7eaa5e881b56 19 DigitalOut led3(LED3);
pspatel321 31:7eaa5e881b56 20 DigitalOut led4(LED4);
pspatel321 31:7eaa5e881b56 21
pspatel321 31:7eaa5e881b56 22 MODSERIAL pc(USBTX, USBRX);
pspatel321 31:7eaa5e881b56 23
pspatel321 31:7eaa5e881b56 24 /*
pspatel321 31:7eaa5e881b56 25 * As experiement, you can define MODSERIAL as show here and see what
pspatel321 31:7eaa5e881b56 26 * effects it has on the LEDs.
pspatel321 31:7eaa5e881b56 27 *
pspatel321 31:7eaa5e881b56 28 * MODSERIAL uart(TX_PIN, RX_PIN, 512);
pspatel321 31:7eaa5e881b56 29 * With this, the 512 characters sent can straight into the buffer
pspatel321 31:7eaa5e881b56 30 * vary quickly. This means LED1 is only on briefly as the TX buffer
pspatel321 31:7eaa5e881b56 31 * fills.
pspatel321 31:7eaa5e881b56 32 *
pspatel321 31:7eaa5e881b56 33 * MODSERIAL uart(TX_PIN, RX_PIN, 32);
pspatel321 31:7eaa5e881b56 34 * With this, the buffer is smaller than the default 256 bytes and
pspatel321 31:7eaa5e881b56 35 * therefore LED1 stays on much longer while the system waits for
pspatel321 31:7eaa5e881b56 36 * room in the TX buffer.
pspatel321 31:7eaa5e881b56 37 */
pspatel321 31:7eaa5e881b56 38 MODSERIAL uart(TX_PIN, RX_PIN);
pspatel321 31:7eaa5e881b56 39
pspatel321 31:7eaa5e881b56 40 MODDMA dma;
pspatel321 31:7eaa5e881b56 41
pspatel321 31:7eaa5e881b56 42 // This function is called when a character goes from the TX buffer
pspatel321 31:7eaa5e881b56 43 // to the Uart THR FIFO register.
pspatel321 31:7eaa5e881b56 44 void txCallback(void) {
pspatel321 31:7eaa5e881b56 45 led2 = !led2;
pspatel321 31:7eaa5e881b56 46 }
pspatel321 31:7eaa5e881b56 47
pspatel321 31:7eaa5e881b56 48 // This function is called when TX buffer goes empty
pspatel321 31:7eaa5e881b56 49 void txEmpty(void) {
pspatel321 31:7eaa5e881b56 50 led2 = 0;
pspatel321 31:7eaa5e881b56 51 pc.puts(" Done. ");
pspatel321 31:7eaa5e881b56 52 }
pspatel321 31:7eaa5e881b56 53
pspatel321 31:7eaa5e881b56 54 void dmaComplete(void) {
pspatel321 31:7eaa5e881b56 55 led1 = 1;
pspatel321 31:7eaa5e881b56 56 }
pspatel321 31:7eaa5e881b56 57
pspatel321 31:7eaa5e881b56 58 // This function is called when a character goes into the RX buffer.
pspatel321 31:7eaa5e881b56 59 void rxCallback(void) {
pspatel321 31:7eaa5e881b56 60 led3 = !led3;
pspatel321 31:7eaa5e881b56 61 pc.putc(uart.getc());
pspatel321 31:7eaa5e881b56 62 }
pspatel321 31:7eaa5e881b56 63
pspatel321 31:7eaa5e881b56 64 int main() {
pspatel321 31:7eaa5e881b56 65 char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ";
pspatel321 31:7eaa5e881b56 66 int c = 'A';
pspatel321 31:7eaa5e881b56 67
pspatel321 31:7eaa5e881b56 68 // Tell MODSERIAL where the MODDMA controller is.
pspatel321 31:7eaa5e881b56 69 pc.MODDMA( &dma );
pspatel321 31:7eaa5e881b56 70
pspatel321 31:7eaa5e881b56 71 // Ensure the baud rate for the PC "USB" serial is much
pspatel321 31:7eaa5e881b56 72 // higher than "uart" baud rate below.
pspatel321 31:7eaa5e881b56 73 pc.baud( PC_BAUD );
pspatel321 31:7eaa5e881b56 74
pspatel321 31:7eaa5e881b56 75 // Use a deliberatly slow baud to fill up the TX buffer
pspatel321 31:7eaa5e881b56 76 uart.baud(1200);
pspatel321 31:7eaa5e881b56 77
pspatel321 31:7eaa5e881b56 78 uart.attach( &txCallback, MODSERIAL::TxIrq );
pspatel321 31:7eaa5e881b56 79 uart.attach( &rxCallback, MODSERIAL::RxIrq );
pspatel321 31:7eaa5e881b56 80 uart.attach( &txEmpty, MODSERIAL::TxEmpty );
pspatel321 31:7eaa5e881b56 81
pspatel321 31:7eaa5e881b56 82 // Loop sending characters. We send 512
pspatel321 31:7eaa5e881b56 83 // which is twice the default TX/RX buffer size.
pspatel321 31:7eaa5e881b56 84
pspatel321 31:7eaa5e881b56 85 led1 = 0;
pspatel321 31:7eaa5e881b56 86
pspatel321 31:7eaa5e881b56 87 // Send the buffer s using DMA channel 7
pspatel321 31:7eaa5e881b56 88 pc.attach_dmaSendComplete( &dmaComplete );
pspatel321 31:7eaa5e881b56 89 pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 );
pspatel321 31:7eaa5e881b56 90
pspatel321 31:7eaa5e881b56 91 for (int loop = 0; loop < 512; loop++) {
pspatel321 31:7eaa5e881b56 92 uart.printf("%c", c);
pspatel321 31:7eaa5e881b56 93 c++;
pspatel321 31:7eaa5e881b56 94 if (c > 'Z') c = 'A';
pspatel321 31:7eaa5e881b56 95 }
pspatel321 31:7eaa5e881b56 96
pspatel321 31:7eaa5e881b56 97 led1 = 0; // Show the end of sending by switching off LED1.
pspatel321 31:7eaa5e881b56 98
pspatel321 31:7eaa5e881b56 99 // End program. Flash LED4. Notice how LED 2 and 3 continue
pspatel321 31:7eaa5e881b56 100 // to flash for a short period while the interrupt system
pspatel321 31:7eaa5e881b56 101 // continues to send the characters left in the TX buffer.
pspatel321 31:7eaa5e881b56 102
pspatel321 31:7eaa5e881b56 103 while(1) {
pspatel321 31:7eaa5e881b56 104 led4 = !led4;
pspatel321 31:7eaa5e881b56 105 wait(0.25);
pspatel321 31:7eaa5e881b56 106 }
pspatel321 31:7eaa5e881b56 107 }
pspatel321 31:7eaa5e881b56 108
pspatel321 31:7eaa5e881b56 109 /*
pspatel321 31:7eaa5e881b56 110 * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host
pspatel321 31:7eaa5e881b56 111 * machine that is connected to the "pc" USB serial port.
pspatel321 31:7eaa5e881b56 112 *
pspatel321 31:7eaa5e881b56 113 * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE
pspatel321 31:7eaa5e881b56 114 * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA
pspatel321 31:7eaa5e881b56 115 * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW
pspatel321 31:7eaa5e881b56 116 * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS
pspatel321 31:7eaa5e881b56 117 * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO
pspatel321 31:7eaa5e881b56 118 * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK
pspatel321 31:7eaa5e881b56 119 * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG
pspatel321 31:7eaa5e881b56 120 * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R
pspatel321 31:7eaa5e881b56 121 *
pspatel321 31:7eaa5e881b56 122 * Note how the DMA blocks the TX buffer sending under standard interrupt control.
pspatel321 31:7eaa5e881b56 123 * Not until the DMA transfer is complete will "normal" buffered TX sending resume.
pspatel321 31:7eaa5e881b56 124 *
pspatel321 31:7eaa5e881b56 125 * Of interest is that last "R" character after the system has said "Done."
pspatel321 31:7eaa5e881b56 126 * This comes from the fact that the TxEmpty callback is made when the TX buffer
pspatel321 31:7eaa5e881b56 127 * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the
pspatel321 31:7eaa5e881b56 128 * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that
pspatel321 31:7eaa5e881b56 129 * when the TxEmpty callback is made, the TX buffer is empty, but that just means
pspatel321 31:7eaa5e881b56 130 * the "last few characters" were written to the TX FIFO. So although the TX
pspatel321 31:7eaa5e881b56 131 * buffer has gone empty, the Uart's transmit system is still sending any remaining
pspatel321 31:7eaa5e881b56 132 * characters from it's TX FIFO. If you want to be truely sure all the characters
pspatel321 31:7eaa5e881b56 133 * you have sent have left the Mbed then call txIsBusy(); This function will
pspatel321 31:7eaa5e881b56 134 * return true if characters are still being sent. If it returns false after
pspatel321 31:7eaa5e881b56 135 * the Tx buffer is empty then all your characters have been sent.
pspatel321 31:7eaa5e881b56 136 *
pspatel321 31:7eaa5e881b56 137 * In a similar way, when characters are received into the RX FIFO, the entire
pspatel321 31:7eaa5e881b56 138 * FIFO contents is moved to the RX buffer, assuming there is room left in the
pspatel321 31:7eaa5e881b56 139 * RX buffer. If there is not, any remaining characters are left in the RX FIFO
pspatel321 31:7eaa5e881b56 140 * and will be moved to the RX buffer on the next interrupt or when the running
pspatel321 31:7eaa5e881b56 141 * program removes a character(s) from the RX buffer with the getc() method.
pspatel321 31:7eaa5e881b56 142 */
pspatel321 31:7eaa5e881b56 143
pspatel321 31:7eaa5e881b56 144 #endif