Test for the SimpleDMA library for KL46z

Dependencies:   SimpleDMA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SimpleDMA.h"
00003 
00004 DigitalOut led1(LED_GREEN);
00005 SimpleDMA dma(0);
00006 RawSerial pc(USBTX, USBRX);
00007 
00008 AnalogIn light_sensor(PTE22);
00009 
00010 EventQueue queue(32*EVENTS_EVENT_SIZE);
00011 
00012 char characters[] = "abcdefgh\r\n";     //Characters we send
00013 char characters2[sizeof(characters)];   //Second buffer
00014 
00015 void blink() 
00016 {
00017     led1 = !led1;
00018 }
00019    
00020 void DMA_Done()
00021 {    
00022     pc.printf("Done!!");
00023 }   
00024 
00025 void DMA_UART_Done(int channel)
00026 {
00027     DMA_Done();
00028 }   
00029 
00030 void DMA_UART_Start()
00031 {
00032     //Now to UART, enable DMA in UART, destination is now
00033     //a fixed address, so address pointer should not be incremented,
00034     //thus second argument is false. Also set trigger to UART0_RX.
00035     //This sends a new value to the UART as soon as it is possible
00036     #ifdef TARGET_LPC1768
00037     LPC_UART0->FCR |= 1<<3;
00038     dma.destination(&LPC_UART0->THR, false);
00039     #endif
00040     #ifdef TARGET_KL46Z
00041     UART0->C5 |= (1<<7) | (1<<5);
00042     dma.destination(&UART0->D, false);
00043     #endif
00044     dma.source(characters2, true);
00045     dma.trigger(Trigger_UART0_TX);
00046     dma.attach(queue.event(DMA_UART_Done));
00047 
00048     dma.start(sizeof(characters));
00049 }   
00050  
00051 void DMA_Memcpy_Done(osThreadId id, int channel)
00052 {
00053     osSignalSet(id, 0x01);     
00054 }
00055     
00056 void DMA_Memcpy_Start()
00057 {                
00058     pc.printf("Start\r\n");    
00059     pc.printf("Use DMA to send 10 characters from buffer to buffer\r\n");
00060     pc.printf("Then send them to UART0 (PC)\r\n");
00061     
00062     osThreadId id = Thread::gettid();
00063     
00064     //Set source and destination, second argument is true since
00065     //we should run through both arrays
00066     dma.source(characters, true);
00067     dma.destination(characters2, true);
00068     dma.attach(mbed::callback(DMA_Memcpy_Done, id));
00069  
00070     //Start transfer of 10 characters
00071     dma.start(10);     
00072     
00073     // And wait for the signal to arrive
00074     Thread::signal_wait(0x1);        
00075     pc.printf("Memcpy transfer done\n\r");
00076     
00077 }
00078 
00079 int main() 
00080 {
00081     queue.call_every(300, blink);    
00082     queue.call(DMA_Memcpy_Start);
00083     queue.dispatch();  
00084 }