HelloWorld program showing SimpleDMA with mainly UART stuff
Dependencies: SimpleDMA mbed-rtos mbed
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "SimpleDMA.h" 00004 00005 DigitalOut led1(LED1); 00006 SimpleDMA dma(0); 00007 RawSerial pc(USBTX, USBRX); 00008 00009 void callback(void) { 00010 pc.printf("Callback!\r\n"); 00011 } 00012 00013 void led_thread(void const *args) { 00014 00015 while (true) { 00016 led1 = !led1; 00017 Thread::wait(300); 00018 } 00019 } 00020 00021 00022 int main() { 00023 printf("Start\r\n"); 00024 00025 printf("Use DMA to send 10 characters from buffer to buffer\r\n"); 00026 printf("Then send them to UART0 (PC)\r\n"); 00027 00028 char characters[] = "abcdefgh\r\n"; //Characters we send 00029 char characters2[11]; //Second buffer 00030 00031 //Set source and destination, second argument is true since 00032 //we should run through both arrays 00033 dma.source(characters, true); 00034 dma.destination(characters2, true); 00035 //dma.attach(callback); 00036 //Start transfer of 10 characters 00037 dma.start(10); 00038 00039 while(dma.isBusy()); 00040 00041 //Now to UART, enable DMA in UART, destination is now 00042 //a fixed address, so address pointer should not be incremented, 00043 //thus second argument is false. Also set trigger to UART0_RX. 00044 //This sends a new value to the UART as soon as it is possible 00045 #ifdef TARGET_LPC1768 00046 LPC_UART0->FCR |= 1<<3; 00047 dma.destination(&LPC_UART0->THR, false); 00048 #endif 00049 #ifdef TARGET_KL25Z 00050 UART0->C5 |= (1<<7) | (1<<5); 00051 dma.destination(&UART0->D, false); 00052 #endif 00053 dma.source(characters2, true); 00054 dma.trigger(Trigger_UART0_TX); 00055 00056 dma.start(10); 00057 while(dma.isBusy()); 00058 00059 printf("\r\n\nNow to show if it doesn't increment the address\r\n"); 00060 printf("Also we attach a callback\r\n"); 00061 dma.source(characters2, false); 00062 dma.attach(callback); 00063 dma.start(10); 00064 while(dma.isBusy()); 00065 00066 00067 printf("\r\n\nFinally we make it a loopback, and use RTOS\r\n"); 00068 printf("The LED in another thread continues blinking while DMA send is busy\r\n"); 00069 00070 //Make a thread with low priority, to show main thread with DMA gives way to other thread while busy sending 00071 Thread thread(led_thread); 00072 thread.set_priority(osPriorityLow); 00073 00074 #ifdef TARGET_LPC1768 00075 dma.source(&LPC_UART0->THR, false); 00076 #endif 00077 #ifdef TARGET_KL25Z 00078 dma.source(&UART0->D, false); 00079 #endif 00080 00081 //Trigger is now the receiving on the UART 00082 dma.trigger(Trigger_UART0_RX); 00083 00084 //dma.wait blocks the current Thread until finished while allowing other threads to run 00085 dma.wait(10); 00086 00087 printf("\r\n\nFinished :-)\r\n"); 00088 00089 //Notice the LED doesn't blink now, since this thread uses all resources in while(1) 00090 while(1); 00091 }
Generated on Sat Jul 16 2022 19:16:04 by 1.7.2