Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MODSERIAL by
example_dma.cpp
00001 #ifdef COMPILE_EXAMPLE_CODE_MODSERIAL_MODDMA 00002 00003 /* 00004 * To run this test program, link p9 to p10 so the Serial loops 00005 * back and receives characters it sends. 00006 */ 00007 00008 #include "mbed.h" 00009 00010 /* Note, this example requires that you also import into the Mbed 00011 compiler the MODDMA project as well as MODSERIAL 00012 http://mbed.org/users/AjK/libraries/MODDMA/latest 00013 MODDMA.h MUST come before MODSERIAL.h */ 00014 #include "MODDMA.h" // <--- Declare first 00015 #include "MODSERIAL.h" // Flollowed by MODSERIAL 00016 00017 DigitalOut led1(LED1); 00018 DigitalOut led2(LED2); 00019 DigitalOut led3(LED3); 00020 DigitalOut led4(LED4); 00021 00022 MODSERIAL pc(USBTX, USBRX); 00023 00024 /* 00025 * As experiement, you can define MODSERIAL as show here and see what 00026 * effects it has on the LEDs. 00027 * 00028 * MODSERIAL uart(TX_PIN, RX_PIN, 512); 00029 * With this, the 512 characters sent can straight into the buffer 00030 * vary quickly. This means LED1 is only on briefly as the TX buffer 00031 * fills. 00032 * 00033 * MODSERIAL uart(TX_PIN, RX_PIN, 32); 00034 * With this, the buffer is smaller than the default 256 bytes and 00035 * therefore LED1 stays on much longer while the system waits for 00036 * room in the TX buffer. 00037 */ 00038 MODSERIAL uart(TX_PIN, RX_PIN); 00039 00040 MODDMA dma; 00041 00042 // This function is called when a character goes from the TX buffer 00043 // to the Uart THR FIFO register. 00044 void txCallback(void) { 00045 led2 = !led2; 00046 } 00047 00048 // This function is called when TX buffer goes empty 00049 void txEmpty(void) { 00050 led2 = 0; 00051 pc.puts(" Done. "); 00052 } 00053 00054 void dmaComplete(void) { 00055 led1 = 1; 00056 } 00057 00058 // This function is called when a character goes into the RX buffer. 00059 void rxCallback(void) { 00060 led3 = !led3; 00061 pc.putc(uart.getc()); 00062 } 00063 00064 int main() { 00065 char s1[] = " *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* "; 00066 int c = 'A'; 00067 00068 // Tell MODSERIAL where the MODDMA controller is. 00069 pc.MODDMA( &dma ); 00070 00071 // Ensure the baud rate for the PC "USB" serial is much 00072 // higher than "uart" baud rate below. 00073 pc.baud( PC_BAUD ); 00074 00075 // Use a deliberatly slow baud to fill up the TX buffer 00076 uart.baud(1200); 00077 00078 uart.attach( &txCallback, MODSERIAL::TxIrq ); 00079 uart.attach( &rxCallback, MODSERIAL::RxIrq ); 00080 uart.attach( &txEmpty, MODSERIAL::TxEmpty ); 00081 00082 // Loop sending characters. We send 512 00083 // which is twice the default TX/RX buffer size. 00084 00085 led1 = 0; 00086 00087 // Send the buffer s using DMA channel 7 00088 pc.attach_dmaSendComplete( &dmaComplete ); 00089 pc.dmaSend( s1, sizeof(s1), MODDMA::Channel_7 ); 00090 00091 for (int loop = 0; loop < 512; loop++) { 00092 uart.printf("%c", c); 00093 c++; 00094 if (c > 'Z') c = 'A'; 00095 } 00096 00097 led1 = 0; // Show the end of sending by switching off LED1. 00098 00099 // End program. Flash LED4. Notice how LED 2 and 3 continue 00100 // to flash for a short period while the interrupt system 00101 // continues to send the characters left in the TX buffer. 00102 00103 while(1) { 00104 led4 = !led4; 00105 wait(0.25); 00106 } 00107 } 00108 00109 /* 00110 * Notes. Here is the sort of output you can expect on your PC/Mac/Linux host 00111 * machine that is connected to the "pc" USB serial port. 00112 * 00113 * *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* *DMA* ABCDEFGHIJKLMNOPQRSTUVWXYZABCDE 00114 * FGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZA 00115 * BCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVW 00116 * XYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRS 00117 * TUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNO 00118 * PQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK 00119 * LMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFG 00120 * HIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ Done. R 00121 * 00122 * Note how the DMA blocks the TX buffer sending under standard interrupt control. 00123 * Not until the DMA transfer is complete will "normal" buffered TX sending resume. 00124 * 00125 * Of interest is that last "R" character after the system has said "Done." 00126 * This comes from the fact that the TxEmpty callback is made when the TX buffer 00127 * becomes empty. MODSERIAL makes use of the fact that the Uarts built into the 00128 * LPC17xx device use a 16 byte FIFO on both RX and TX channels. This means that 00129 * when the TxEmpty callback is made, the TX buffer is empty, but that just means 00130 * the "last few characters" were written to the TX FIFO. So although the TX 00131 * buffer has gone empty, the Uart's transmit system is still sending any remaining 00132 * characters from it's TX FIFO. If you want to be truely sure all the characters 00133 * you have sent have left the Mbed then call txIsBusy(); This function will 00134 * return true if characters are still being sent. If it returns false after 00135 * the Tx buffer is empty then all your characters have been sent. 00136 * 00137 * In a similar way, when characters are received into the RX FIFO, the entire 00138 * FIFO contents is moved to the RX buffer, assuming there is room left in the 00139 * RX buffer. If there is not, any remaining characters are left in the RX FIFO 00140 * and will be moved to the RX buffer on the next interrupt or when the running 00141 * program removes a character(s) from the RX buffer with the getc() method. 00142 */ 00143 00144 #endif
Generated on Tue Jul 12 2022 23:00:19 by
