HelloWorld program showing SimpleDMA with mainly UART stuff
Dependencies: SimpleDMA mbed-rtos mbed
main.cpp@1:418889681f13, 2014-01-04 (annotated)
- Committer:
- Sissors
- Date:
- Sat Jan 04 14:58:41 2014 +0000
- Revision:
- 1:418889681f13
- Parent:
- 0:cf18a31facd6
LPC1768 support added
;
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 | 1:418889681f13 | 6 | SimpleDMA dma(0); |
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 | 1:418889681f13 | 44 | //This sends a new value to the UART as soon as it is possible |
Sissors | 1:418889681f13 | 45 | #ifdef TARGET_LPC1768 |
Sissors | 1:418889681f13 | 46 | LPC_UART0->FCR |= 1<<3; |
Sissors | 1:418889681f13 | 47 | dma.destination(&LPC_UART0->THR, false); |
Sissors | 1:418889681f13 | 48 | #endif |
Sissors | 1:418889681f13 | 49 | #ifdef TARGET_KL25Z |
Sissors | 0:cf18a31facd6 | 50 | UART0->C5 |= (1<<7) | (1<<5); |
Sissors | 1:418889681f13 | 51 | dma.destination(&UART0->D, false); |
Sissors | 1:418889681f13 | 52 | #endif |
Sissors | 0:cf18a31facd6 | 53 | dma.source(characters2, true); |
Sissors | 0:cf18a31facd6 | 54 | dma.trigger(Trigger_UART0_TX); |
Sissors | 0:cf18a31facd6 | 55 | |
Sissors | 0:cf18a31facd6 | 56 | dma.start(10); |
Sissors | 0:cf18a31facd6 | 57 | while(dma.isBusy()); |
Sissors | 0:cf18a31facd6 | 58 | |
Sissors | 0:cf18a31facd6 | 59 | printf("\r\n\nNow to show if it doesn't increment the address\r\n"); |
Sissors | 0:cf18a31facd6 | 60 | printf("Also we attach a callback\r\n"); |
Sissors | 0:cf18a31facd6 | 61 | dma.source(characters2, false); |
Sissors | 0:cf18a31facd6 | 62 | dma.attach(callback); |
Sissors | 0:cf18a31facd6 | 63 | dma.start(10); |
Sissors | 0:cf18a31facd6 | 64 | while(dma.isBusy()); |
Sissors | 0:cf18a31facd6 | 65 | |
Sissors | 0:cf18a31facd6 | 66 | |
Sissors | 0:cf18a31facd6 | 67 | printf("\r\n\nFinally we make it a loopback, and use RTOS\r\n"); |
Sissors | 0:cf18a31facd6 | 68 | printf("The LED in another thread continues blinking while DMA send is busy\r\n"); |
Sissors | 0:cf18a31facd6 | 69 | |
Sissors | 0:cf18a31facd6 | 70 | //Make a thread with low priority, to show main thread with DMA gives way to other thread while busy sending |
Sissors | 0:cf18a31facd6 | 71 | Thread thread(led_thread); |
Sissors | 0:cf18a31facd6 | 72 | thread.set_priority(osPriorityLow); |
Sissors | 0:cf18a31facd6 | 73 | |
Sissors | 1:418889681f13 | 74 | #ifdef TARGET_LPC1768 |
Sissors | 1:418889681f13 | 75 | dma.source(&LPC_UART0->THR, false); |
Sissors | 1:418889681f13 | 76 | #endif |
Sissors | 1:418889681f13 | 77 | #ifdef TARGET_KL25Z |
Sissors | 0:cf18a31facd6 | 78 | dma.source(&UART0->D, false); |
Sissors | 1:418889681f13 | 79 | #endif |
Sissors | 1:418889681f13 | 80 | |
Sissors | 0:cf18a31facd6 | 81 | //Trigger is now the receiving on the UART |
Sissors | 0:cf18a31facd6 | 82 | dma.trigger(Trigger_UART0_RX); |
Sissors | 0:cf18a31facd6 | 83 | |
Sissors | 0:cf18a31facd6 | 84 | //dma.wait blocks the current Thread until finished while allowing other threads to run |
Sissors | 0:cf18a31facd6 | 85 | dma.wait(10); |
Sissors | 0:cf18a31facd6 | 86 | |
Sissors | 0:cf18a31facd6 | 87 | printf("\r\n\nFinished :-)\r\n"); |
Sissors | 0:cf18a31facd6 | 88 | |
Sissors | 0:cf18a31facd6 | 89 | //Notice the LED doesn't blink now, since this thread uses all resources in while(1) |
Sissors | 0:cf18a31facd6 | 90 | while(1); |
Sissors | 0:cf18a31facd6 | 91 | } |