HelloWorld program showing SimpleDMA with mainly UART stuff
Dependencies: SimpleDMA mbed-rtos mbed
main.cpp@0:cf18a31facd6, 2013-12-26 (annotated)
- Committer:
- Sissors
- Date:
- Thu Dec 26 16:32:59 2013 +0000
- Revision:
- 0:cf18a31facd6
- Child:
- 1:418889681f13
v1.0
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:cf18a31facd6 | 1 | #include "mbed.h" |
Sissors | 0:cf18a31facd6 | 2 | #include "rtos.h" |
Sissors | 0:cf18a31facd6 | 3 | #include "SimpleDMA.h" |
Sissors | 0:cf18a31facd6 | 4 | |
Sissors | 0:cf18a31facd6 | 5 | DigitalOut led1(LED1); |
Sissors | 0:cf18a31facd6 | 6 | SimpleDMA dma; |
Sissors | 0:cf18a31facd6 | 7 | RawSerial pc(USBTX, USBRX); |
Sissors | 0:cf18a31facd6 | 8 | |
Sissors | 0:cf18a31facd6 | 9 | void callback(void) { |
Sissors | 0:cf18a31facd6 | 10 | pc.printf("Callback!\r\n"); |
Sissors | 0:cf18a31facd6 | 11 | } |
Sissors | 0:cf18a31facd6 | 12 | |
Sissors | 0:cf18a31facd6 | 13 | void led_thread(void const *args) { |
Sissors | 0:cf18a31facd6 | 14 | |
Sissors | 0:cf18a31facd6 | 15 | while (true) { |
Sissors | 0:cf18a31facd6 | 16 | led1 = !led1; |
Sissors | 0:cf18a31facd6 | 17 | Thread::wait(300); |
Sissors | 0:cf18a31facd6 | 18 | } |
Sissors | 0:cf18a31facd6 | 19 | } |
Sissors | 0:cf18a31facd6 | 20 | |
Sissors | 0:cf18a31facd6 | 21 | |
Sissors | 0:cf18a31facd6 | 22 | int main() { |
Sissors | 0:cf18a31facd6 | 23 | printf("Start\r\n"); |
Sissors | 0:cf18a31facd6 | 24 | |
Sissors | 0:cf18a31facd6 | 25 | printf("Use DMA to send 10 characters from buffer to buffer\r\n"); |
Sissors | 0:cf18a31facd6 | 26 | printf("Then send them to UART0 (PC)\r\n"); |
Sissors | 0:cf18a31facd6 | 27 | |
Sissors | 0:cf18a31facd6 | 28 | char characters[] = "abcdefgh\r\n"; //Characters we send |
Sissors | 0:cf18a31facd6 | 29 | char characters2[11]; //Second buffer |
Sissors | 0:cf18a31facd6 | 30 | |
Sissors | 0:cf18a31facd6 | 31 | //Set source and destination, second argument is true since |
Sissors | 0:cf18a31facd6 | 32 | //we should run through both arrays |
Sissors | 0:cf18a31facd6 | 33 | dma.source(characters, true); |
Sissors | 0:cf18a31facd6 | 34 | dma.destination(characters2, true); |
Sissors | 0:cf18a31facd6 | 35 | //dma.attach(callback); |
Sissors | 0:cf18a31facd6 | 36 | //Start transfer of 10 characters |
Sissors | 0:cf18a31facd6 | 37 | dma.start(10); |
Sissors | 0:cf18a31facd6 | 38 | |
Sissors | 0:cf18a31facd6 | 39 | while(dma.isBusy()); |
Sissors | 0:cf18a31facd6 | 40 | |
Sissors | 0:cf18a31facd6 | 41 | //Now to UART, enable DMA in UART, destination is now |
Sissors | 0:cf18a31facd6 | 42 | //a fixed address, so address pointer should not be incremented, |
Sissors | 0:cf18a31facd6 | 43 | //thus second argument is false. Also set trigger to UART0_RX. |
Sissors | 0:cf18a31facd6 | 44 | //This sends a new value to the UART as soon as it is possible |
Sissors | 0:cf18a31facd6 | 45 | UART0->C5 |= (1<<7) | (1<<5); |
Sissors | 0:cf18a31facd6 | 46 | dma.source(characters2, true); |
Sissors | 0:cf18a31facd6 | 47 | dma.destination(&UART0->D, false); |
Sissors | 0:cf18a31facd6 | 48 | dma.trigger(Trigger_UART0_TX); |
Sissors | 0:cf18a31facd6 | 49 | |
Sissors | 0:cf18a31facd6 | 50 | dma.start(10); |
Sissors | 0:cf18a31facd6 | 51 | while(dma.isBusy()); |
Sissors | 0:cf18a31facd6 | 52 | |
Sissors | 0:cf18a31facd6 | 53 | printf("\r\n\nNow to show if it doesn't increment the address\r\n"); |
Sissors | 0:cf18a31facd6 | 54 | printf("Also we attach a callback\r\n"); |
Sissors | 0:cf18a31facd6 | 55 | dma.source(characters2, false); |
Sissors | 0:cf18a31facd6 | 56 | dma.attach(callback); |
Sissors | 0:cf18a31facd6 | 57 | dma.start(10); |
Sissors | 0:cf18a31facd6 | 58 | while(dma.isBusy()); |
Sissors | 0:cf18a31facd6 | 59 | |
Sissors | 0:cf18a31facd6 | 60 | |
Sissors | 0:cf18a31facd6 | 61 | printf("\r\n\nFinally we make it a loopback, and use RTOS\r\n"); |
Sissors | 0:cf18a31facd6 | 62 | printf("The LED in another thread continues blinking while DMA send is busy\r\n"); |
Sissors | 0:cf18a31facd6 | 63 | |
Sissors | 0:cf18a31facd6 | 64 | //Make a thread with low priority, to show main thread with DMA gives way to other thread while busy sending |
Sissors | 0:cf18a31facd6 | 65 | Thread thread(led_thread); |
Sissors | 0:cf18a31facd6 | 66 | thread.set_priority(osPriorityLow); |
Sissors | 0:cf18a31facd6 | 67 | |
Sissors | 0:cf18a31facd6 | 68 | dma.source(&UART0->D, false); |
Sissors | 0:cf18a31facd6 | 69 | //Trigger is now the receiving on the UART |
Sissors | 0:cf18a31facd6 | 70 | dma.trigger(Trigger_UART0_RX); |
Sissors | 0:cf18a31facd6 | 71 | |
Sissors | 0:cf18a31facd6 | 72 | //dma.wait blocks the current Thread until finished while allowing other threads to run |
Sissors | 0:cf18a31facd6 | 73 | dma.wait(10); |
Sissors | 0:cf18a31facd6 | 74 | |
Sissors | 0:cf18a31facd6 | 75 | printf("\r\n\nFinished :-)\r\n"); |
Sissors | 0:cf18a31facd6 | 76 | |
Sissors | 0:cf18a31facd6 | 77 | //Notice the LED doesn't blink now, since this thread uses all resources in while(1) |
Sissors | 0:cf18a31facd6 | 78 | while(1); |
Sissors | 0:cf18a31facd6 | 79 | } |