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