Test
example3.h@18:e7d62a489a83, 2019-04-25 (annotated)
- Committer:
- ktseng
- Date:
- Thu Apr 25 19:20:22 2019 +0000
- Revision:
- 18:e7d62a489a83
- Parent:
- 16:cb10aec6feb1
Test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 12:1dfee7208043 | 1 | /* |
AjK | 12:1dfee7208043 | 2 | * Demonstrates capturing the GPIO P0.4 to P0.7 "nibble" to memory |
AjK | 12:1dfee7208043 | 3 | * using GPDMA. The transfers from port pins to memory buffer are |
AjK | 12:1dfee7208043 | 4 | * triggered using Timer1 MAT1.0 match compare. |
AjK | 12:1dfee7208043 | 5 | * |
AjK | 12:1dfee7208043 | 6 | * In this example all inputs have pullups. So with nothing connected |
AjK | 12:1dfee7208043 | 7 | * the P0.4/7 reads as 0xF. Connecting a wire from one or more of the four |
AjK | 12:1dfee7208043 | 8 | * inputs to ground will show up in the captured buffer sequence. |
AjK | 12:1dfee7208043 | 9 | */ |
AjK | 12:1dfee7208043 | 10 | |
AjK | 12:1dfee7208043 | 11 | #include "mbed.h" |
AjK | 12:1dfee7208043 | 12 | #include "MODDMA.h" |
AjK | 12:1dfee7208043 | 13 | #include "iomacros.h" // within MODDMA library. |
AjK | 12:1dfee7208043 | 14 | |
AjK | 12:1dfee7208043 | 15 | // How long between grabbing GPIO FIO0PIN register. |
AjK | 15:e01144bae101 | 16 | // Value is in microseconds. (500000 is half a second). |
AjK | 12:1dfee7208043 | 17 | #define SAMPLE_PERIOD 500000 |
AjK | 12:1dfee7208043 | 18 | |
ktseng | 18:e7d62a489a83 | 19 | #define NUM_OF_SAMPLES 16 |
AjK | 12:1dfee7208043 | 20 | |
AjK | 12:1dfee7208043 | 21 | Serial pc(USBTX, USBRX); |
AjK | 12:1dfee7208043 | 22 | |
AjK | 12:1dfee7208043 | 23 | DigitalOut led1(LED1); |
AjK | 12:1dfee7208043 | 24 | DigitalOut led2(LED2); |
AjK | 12:1dfee7208043 | 25 | DigitalOut led3(LED3); |
AjK | 12:1dfee7208043 | 26 | |
AjK | 12:1dfee7208043 | 27 | uint32_t buffer[NUM_OF_SAMPLES]; |
AjK | 12:1dfee7208043 | 28 | |
AjK | 12:1dfee7208043 | 29 | bool dmaTransferComplete; |
AjK | 12:1dfee7208043 | 30 | |
AjK | 12:1dfee7208043 | 31 | MODDMA dma; |
AjK | 16:cb10aec6feb1 | 32 | MODDMA_Config *conf; |
AjK | 12:1dfee7208043 | 33 | |
AjK | 12:1dfee7208043 | 34 | void TC0_callback(void); |
AjK | 12:1dfee7208043 | 35 | void ERR0_callback(void); |
AjK | 12:1dfee7208043 | 36 | |
AjK | 12:1dfee7208043 | 37 | int main() { |
AjK | 14:15939b260dfa | 38 | volatile int life_counter = 0; |
AjK | 14:15939b260dfa | 39 | |
ktseng | 18:e7d62a489a83 | 40 | // Sets bits 4-7 on port 0 to pulled down outputs. |
ktseng | 18:e7d62a489a83 | 41 | p30_AS_OUTPUT; p30_MODE( PIN_PULLDOWN ); // P0.4 |
ktseng | 18:e7d62a489a83 | 42 | p29_AS_OUTPUT; p29_MODE( PIN_PULLDOWN ); // P0.5 |
ktseng | 18:e7d62a489a83 | 43 | p8_AS_OUTPUT; p8_MODE( PIN_PULLDOWN ); // P0.6 |
ktseng | 18:e7d62a489a83 | 44 | p7_AS_OUTPUT; p7_MODE( PIN_PULLDOWN ); // P0.7 |
AjK | 12:1dfee7208043 | 45 | |
AjK | 12:1dfee7208043 | 46 | // Clear the buffer. |
AjK | 12:1dfee7208043 | 47 | memset(buffer, 0, sizeof(buffer)); |
AjK | 12:1dfee7208043 | 48 | |
AjK | 12:1dfee7208043 | 49 | // Set-up timer1 as a periodic timer. |
AjK | 12:1dfee7208043 | 50 | LPC_SC->PCONP |= (1UL << 2); // TIM1 On |
AjK | 12:1dfee7208043 | 51 | LPC_SC->PCLKSEL0 |= (3UL << 4); // CCLK/8 = 12MHz |
AjK | 12:1dfee7208043 | 52 | LPC_TIM1->PR = 11; // TC clocks at 1MHz. |
AjK | 12:1dfee7208043 | 53 | LPC_TIM1->MCR = 2; // Reset TCR to zero on match. |
AjK | 12:1dfee7208043 | 54 | LPC_TIM1->MR0 = SAMPLE_PERIOD; |
AjK | 12:1dfee7208043 | 55 | |
AjK | 12:1dfee7208043 | 56 | // Prepare the GPDMA system. |
AjK | 12:1dfee7208043 | 57 | conf = new MODDMA_Config; |
AjK | 12:1dfee7208043 | 58 | conf |
AjK | 12:1dfee7208043 | 59 | ->channelNum ( MODDMA::Channel_0 ) |
ktseng | 18:e7d62a489a83 | 60 | ->srcMemAddr ( (uint32_t)&buffer[0] ) //Buffer data to be transferred |
ktseng | 18:e7d62a489a83 | 61 | ->dstMemAddr ( (uint32_t)&LPC_GPIO0->FIOPIN ) //GPIO to be transferred |
AjK | 12:1dfee7208043 | 62 | ->transferSize ( NUM_OF_SAMPLES ) |
ktseng | 18:e7d62a489a83 | 63 | ->transferType ( MODDMA::m2g ) // pseudo transfer code MODDMA understands. |
AjK | 12:1dfee7208043 | 64 | ->transferWidth ( MODDMA::word ) |
AjK | 12:1dfee7208043 | 65 | ->srcConn ( MODDMA::MAT1_0 ) |
AjK | 12:1dfee7208043 | 66 | ->dmacSync ( MODDMA::MAT1_0 ) |
AjK | 12:1dfee7208043 | 67 | ->attach_tc ( TC0_callback ) |
AjK | 12:1dfee7208043 | 68 | ->attach_err ( ERR0_callback ) |
AjK | 12:1dfee7208043 | 69 | ; // end conf. |
AjK | 12:1dfee7208043 | 70 | |
AjK | 12:1dfee7208043 | 71 | // Prepare configuration. |
AjK | 12:1dfee7208043 | 72 | if (!dma.Setup( conf )) { |
AjK | 12:1dfee7208043 | 73 | error("Doh!"); |
AjK | 12:1dfee7208043 | 74 | } |
AjK | 12:1dfee7208043 | 75 | |
AjK | 12:1dfee7208043 | 76 | // Enable GPDMA to be ready for the TIM1 "ticks". |
AjK | 12:1dfee7208043 | 77 | dma.Enable( conf ); |
AjK | 12:1dfee7208043 | 78 | |
AjK | 12:1dfee7208043 | 79 | // Begin. |
AjK | 12:1dfee7208043 | 80 | LPC_TIM1->TCR = 1; |
AjK | 12:1dfee7208043 | 81 | |
AjK | 14:15939b260dfa | 82 | while (1) { |
AjK | 14:15939b260dfa | 83 | if (life_counter++ > 1000000) { |
AjK | 14:15939b260dfa | 84 | led1 = !led1; // Show some sort of life. |
AjK | 14:15939b260dfa | 85 | life_counter = 0; |
AjK | 14:15939b260dfa | 86 | } |
AjK | 12:1dfee7208043 | 87 | |
AjK | 12:1dfee7208043 | 88 | if (dmaTransferComplete) { |
AjK | 12:1dfee7208043 | 89 | dmaTransferComplete = false; |
AjK | 12:1dfee7208043 | 90 | for (int i = 0; i < NUM_OF_SAMPLES; i++) { |
AjK | 12:1dfee7208043 | 91 | int val = (buffer[i] >> 4) & 0xF; |
AjK | 12:1dfee7208043 | 92 | pc.printf("Buffer index %d = 0x%x\n", i, val); |
AjK | 12:1dfee7208043 | 93 | } |
AjK | 13:846c8b6ffb7f | 94 | pc.printf("Done.\n"); |
AjK | 13:846c8b6ffb7f | 95 | |
AjK | 13:846c8b6ffb7f | 96 | // Schedule another grab. |
AjK | 13:846c8b6ffb7f | 97 | if (dma.Setup( conf )) { |
AjK | 14:15939b260dfa | 98 | dma.Enable( conf ); |
AjK | 13:846c8b6ffb7f | 99 | } |
AjK | 12:1dfee7208043 | 100 | } |
AjK | 12:1dfee7208043 | 101 | } |
AjK | 12:1dfee7208043 | 102 | } |
AjK | 12:1dfee7208043 | 103 | |
AjK | 12:1dfee7208043 | 104 | // Configuration callback on TC |
AjK | 12:1dfee7208043 | 105 | void TC0_callback(void) { |
AjK | 12:1dfee7208043 | 106 | |
AjK | 12:1dfee7208043 | 107 | // Just show sample sequence grab complete. |
AjK | 12:1dfee7208043 | 108 | led3 = !led3; |
AjK | 14:15939b260dfa | 109 | |
AjK | 12:1dfee7208043 | 110 | // Get configuration pointer. |
AjK | 12:1dfee7208043 | 111 | MODDMA_Config *config = dma.getConfig(); |
AjK | 12:1dfee7208043 | 112 | |
AjK | 12:1dfee7208043 | 113 | // Finish the DMA cycle by shutting down the channel. |
AjK | 12:1dfee7208043 | 114 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
AjK | 12:1dfee7208043 | 115 | |
AjK | 12:1dfee7208043 | 116 | // Tell main() while(1) loop to print the results. |
AjK | 12:1dfee7208043 | 117 | dmaTransferComplete = true; |
AjK | 12:1dfee7208043 | 118 | |
AjK | 12:1dfee7208043 | 119 | // Clear DMA IRQ flags. |
AjK | 12:1dfee7208043 | 120 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
AjK | 13:846c8b6ffb7f | 121 | if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq(); |
AjK | 12:1dfee7208043 | 122 | } |
AjK | 12:1dfee7208043 | 123 | |
AjK | 12:1dfee7208043 | 124 | // Configuration callback on Error |
AjK | 12:1dfee7208043 | 125 | void ERR0_callback(void) { |
AjK | 12:1dfee7208043 | 126 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
AjK | 12:1dfee7208043 | 127 | } |