Wouter van Kleunen
/
SimpleDma-test
Test for the SimpleDMA library for KL46z
main.cpp@0:4b9437bac3eb, 2017-05-12 (annotated)
- Committer:
- wkleunen
- Date:
- Fri May 12 10:27:39 2017 +0000
- Revision:
- 0:4b9437bac3eb
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wkleunen | 0:4b9437bac3eb | 1 | #include "mbed.h" |
wkleunen | 0:4b9437bac3eb | 2 | #include "SimpleDMA.h" |
wkleunen | 0:4b9437bac3eb | 3 | |
wkleunen | 0:4b9437bac3eb | 4 | DigitalOut led1(LED_GREEN); |
wkleunen | 0:4b9437bac3eb | 5 | SimpleDMA dma(0); |
wkleunen | 0:4b9437bac3eb | 6 | RawSerial pc(USBTX, USBRX); |
wkleunen | 0:4b9437bac3eb | 7 | |
wkleunen | 0:4b9437bac3eb | 8 | AnalogIn light_sensor(PTE22); |
wkleunen | 0:4b9437bac3eb | 9 | |
wkleunen | 0:4b9437bac3eb | 10 | EventQueue queue(32*EVENTS_EVENT_SIZE); |
wkleunen | 0:4b9437bac3eb | 11 | |
wkleunen | 0:4b9437bac3eb | 12 | char characters[] = "abcdefgh\r\n"; //Characters we send |
wkleunen | 0:4b9437bac3eb | 13 | char characters2[sizeof(characters)]; //Second buffer |
wkleunen | 0:4b9437bac3eb | 14 | |
wkleunen | 0:4b9437bac3eb | 15 | void blink() |
wkleunen | 0:4b9437bac3eb | 16 | { |
wkleunen | 0:4b9437bac3eb | 17 | led1 = !led1; |
wkleunen | 0:4b9437bac3eb | 18 | } |
wkleunen | 0:4b9437bac3eb | 19 | |
wkleunen | 0:4b9437bac3eb | 20 | void DMA_Done() |
wkleunen | 0:4b9437bac3eb | 21 | { |
wkleunen | 0:4b9437bac3eb | 22 | pc.printf("Done!!"); |
wkleunen | 0:4b9437bac3eb | 23 | } |
wkleunen | 0:4b9437bac3eb | 24 | |
wkleunen | 0:4b9437bac3eb | 25 | void DMA_UART_Done(int channel) |
wkleunen | 0:4b9437bac3eb | 26 | { |
wkleunen | 0:4b9437bac3eb | 27 | DMA_Done(); |
wkleunen | 0:4b9437bac3eb | 28 | } |
wkleunen | 0:4b9437bac3eb | 29 | |
wkleunen | 0:4b9437bac3eb | 30 | void DMA_UART_Start() |
wkleunen | 0:4b9437bac3eb | 31 | { |
wkleunen | 0:4b9437bac3eb | 32 | //Now to UART, enable DMA in UART, destination is now |
wkleunen | 0:4b9437bac3eb | 33 | //a fixed address, so address pointer should not be incremented, |
wkleunen | 0:4b9437bac3eb | 34 | //thus second argument is false. Also set trigger to UART0_RX. |
wkleunen | 0:4b9437bac3eb | 35 | //This sends a new value to the UART as soon as it is possible |
wkleunen | 0:4b9437bac3eb | 36 | #ifdef TARGET_LPC1768 |
wkleunen | 0:4b9437bac3eb | 37 | LPC_UART0->FCR |= 1<<3; |
wkleunen | 0:4b9437bac3eb | 38 | dma.destination(&LPC_UART0->THR, false); |
wkleunen | 0:4b9437bac3eb | 39 | #endif |
wkleunen | 0:4b9437bac3eb | 40 | #ifdef TARGET_KL46Z |
wkleunen | 0:4b9437bac3eb | 41 | UART0->C5 |= (1<<7) | (1<<5); |
wkleunen | 0:4b9437bac3eb | 42 | dma.destination(&UART0->D, false); |
wkleunen | 0:4b9437bac3eb | 43 | #endif |
wkleunen | 0:4b9437bac3eb | 44 | dma.source(characters2, true); |
wkleunen | 0:4b9437bac3eb | 45 | dma.trigger(Trigger_UART0_TX); |
wkleunen | 0:4b9437bac3eb | 46 | dma.attach(queue.event(DMA_UART_Done)); |
wkleunen | 0:4b9437bac3eb | 47 | |
wkleunen | 0:4b9437bac3eb | 48 | dma.start(sizeof(characters)); |
wkleunen | 0:4b9437bac3eb | 49 | } |
wkleunen | 0:4b9437bac3eb | 50 | |
wkleunen | 0:4b9437bac3eb | 51 | void DMA_Memcpy_Done(osThreadId id, int channel) |
wkleunen | 0:4b9437bac3eb | 52 | { |
wkleunen | 0:4b9437bac3eb | 53 | osSignalSet(id, 0x01); |
wkleunen | 0:4b9437bac3eb | 54 | } |
wkleunen | 0:4b9437bac3eb | 55 | |
wkleunen | 0:4b9437bac3eb | 56 | void DMA_Memcpy_Start() |
wkleunen | 0:4b9437bac3eb | 57 | { |
wkleunen | 0:4b9437bac3eb | 58 | pc.printf("Start\r\n"); |
wkleunen | 0:4b9437bac3eb | 59 | pc.printf("Use DMA to send 10 characters from buffer to buffer\r\n"); |
wkleunen | 0:4b9437bac3eb | 60 | pc.printf("Then send them to UART0 (PC)\r\n"); |
wkleunen | 0:4b9437bac3eb | 61 | |
wkleunen | 0:4b9437bac3eb | 62 | osThreadId id = Thread::gettid(); |
wkleunen | 0:4b9437bac3eb | 63 | |
wkleunen | 0:4b9437bac3eb | 64 | //Set source and destination, second argument is true since |
wkleunen | 0:4b9437bac3eb | 65 | //we should run through both arrays |
wkleunen | 0:4b9437bac3eb | 66 | dma.source(characters, true); |
wkleunen | 0:4b9437bac3eb | 67 | dma.destination(characters2, true); |
wkleunen | 0:4b9437bac3eb | 68 | dma.attach(mbed::callback(DMA_Memcpy_Done, id)); |
wkleunen | 0:4b9437bac3eb | 69 | |
wkleunen | 0:4b9437bac3eb | 70 | //Start transfer of 10 characters |
wkleunen | 0:4b9437bac3eb | 71 | dma.start(10); |
wkleunen | 0:4b9437bac3eb | 72 | |
wkleunen | 0:4b9437bac3eb | 73 | // And wait for the signal to arrive |
wkleunen | 0:4b9437bac3eb | 74 | Thread::signal_wait(0x1); |
wkleunen | 0:4b9437bac3eb | 75 | pc.printf("Memcpy transfer done\n\r"); |
wkleunen | 0:4b9437bac3eb | 76 | |
wkleunen | 0:4b9437bac3eb | 77 | } |
wkleunen | 0:4b9437bac3eb | 78 | |
wkleunen | 0:4b9437bac3eb | 79 | int main() |
wkleunen | 0:4b9437bac3eb | 80 | { |
wkleunen | 0:4b9437bac3eb | 81 | queue.call_every(300, blink); |
wkleunen | 0:4b9437bac3eb | 82 | queue.call(DMA_Memcpy_Start); |
wkleunen | 0:4b9437bac3eb | 83 | queue.dispatch(); |
wkleunen | 0:4b9437bac3eb | 84 | } |