trying to make telnetserver repo public
Fork of MODDMA by
example3.h@12:1dfee7208043, 2011-03-13 (annotated)
- Committer:
- AjK
- Date:
- Sun Mar 13 23:28:21 2011 +0000
- Revision:
- 12:1dfee7208043
- Child:
- 13:846c8b6ffb7f
1.8 See ChangeLog.c
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 | 12:1dfee7208043 | 16 | // Value is in microseconds. (50000 is half a second). |
AjK | 12:1dfee7208043 | 17 | #define SAMPLE_PERIOD 500000 |
AjK | 12:1dfee7208043 | 18 | |
AjK | 12:1dfee7208043 | 19 | #define NUM_OF_SAMPLES 5 |
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 | 12:1dfee7208043 | 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 | 12:1dfee7208043 | 38 | |
AjK | 12:1dfee7208043 | 39 | // Macros defined in iomacros.h, saves messing with DigitalIn |
AjK | 12:1dfee7208043 | 40 | p30_AS_INPUT; p30_MODE( PIN_PULLUP ); // P0.4 |
AjK | 12:1dfee7208043 | 41 | p29_AS_INPUT; p29_MODE( PIN_PULLUP ); // P0.5 |
AjK | 12:1dfee7208043 | 42 | p8_AS_INPUT; p8_MODE( PIN_PULLUP ); // P0.6 |
AjK | 12:1dfee7208043 | 43 | p7_AS_INPUT; p7_MODE( PIN_PULLUP ); // P0.7 |
AjK | 12:1dfee7208043 | 44 | |
AjK | 12:1dfee7208043 | 45 | // Clear the buffer. |
AjK | 12:1dfee7208043 | 46 | memset(buffer, 0, sizeof(buffer)); |
AjK | 12:1dfee7208043 | 47 | |
AjK | 12:1dfee7208043 | 48 | // Setup the serial port to print out results. |
AjK | 12:1dfee7208043 | 49 | pc.baud(115200); |
AjK | 12:1dfee7208043 | 50 | pc.printf("Starting up...\n"); |
AjK | 12:1dfee7208043 | 51 | |
AjK | 12:1dfee7208043 | 52 | // Set-up timer1 as a periodic timer. |
AjK | 12:1dfee7208043 | 53 | LPC_SC->PCONP |= (1UL << 2); // TIM1 On |
AjK | 12:1dfee7208043 | 54 | LPC_SC->PCLKSEL0 |= (3UL << 4); // CCLK/8 = 12MHz |
AjK | 12:1dfee7208043 | 55 | LPC_TIM1->PR = 11; // TC clocks at 1MHz. |
AjK | 12:1dfee7208043 | 56 | LPC_TIM1->MCR = 2; // Reset TCR to zero on match. |
AjK | 12:1dfee7208043 | 57 | LPC_TIM1->MR0 = SAMPLE_PERIOD; |
AjK | 12:1dfee7208043 | 58 | |
AjK | 12:1dfee7208043 | 59 | // Prepare the GPDMA system. |
AjK | 12:1dfee7208043 | 60 | conf = new MODDMA_Config; |
AjK | 12:1dfee7208043 | 61 | conf |
AjK | 12:1dfee7208043 | 62 | ->channelNum ( MODDMA::Channel_0 ) |
AjK | 12:1dfee7208043 | 63 | ->srcMemAddr ( (uint32_t)&LPC_GPIO0->FIOPIN ) |
AjK | 12:1dfee7208043 | 64 | ->dstMemAddr ( (uint32_t)&buffer[0] ) |
AjK | 12:1dfee7208043 | 65 | ->transferSize ( NUM_OF_SAMPLES ) |
AjK | 12:1dfee7208043 | 66 | ->transferType ( MODDMA::g2m ) // pseudo transfer code MODDMA understands. |
AjK | 12:1dfee7208043 | 67 | ->transferWidth ( MODDMA::word ) |
AjK | 12:1dfee7208043 | 68 | ->srcConn ( MODDMA::MAT1_0 ) |
AjK | 12:1dfee7208043 | 69 | ->dmacSync ( MODDMA::MAT1_0 ) |
AjK | 12:1dfee7208043 | 70 | ->attach_tc ( TC0_callback ) |
AjK | 12:1dfee7208043 | 71 | ->attach_err ( ERR0_callback ) |
AjK | 12:1dfee7208043 | 72 | ; // end conf. |
AjK | 12:1dfee7208043 | 73 | |
AjK | 12:1dfee7208043 | 74 | // Prepare configuration. |
AjK | 12:1dfee7208043 | 75 | if (!dma.Setup( conf )) { |
AjK | 12:1dfee7208043 | 76 | error("Doh!"); |
AjK | 12:1dfee7208043 | 77 | } |
AjK | 12:1dfee7208043 | 78 | |
AjK | 12:1dfee7208043 | 79 | // Enable GPDMA to be ready for the TIM1 "ticks". |
AjK | 12:1dfee7208043 | 80 | dma.Enable( conf ); |
AjK | 12:1dfee7208043 | 81 | |
AjK | 12:1dfee7208043 | 82 | // Begin. |
AjK | 12:1dfee7208043 | 83 | LPC_TIM1->TCR = 1; |
AjK | 12:1dfee7208043 | 84 | |
AjK | 12:1dfee7208043 | 85 | while (1) { |
AjK | 12:1dfee7208043 | 86 | led1 = !led1; // Show some sort of life. |
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 | 12:1dfee7208043 | 94 | pc.printf("Done.\n"); |
AjK | 12:1dfee7208043 | 95 | } |
AjK | 12:1dfee7208043 | 96 | } |
AjK | 12:1dfee7208043 | 97 | } |
AjK | 12:1dfee7208043 | 98 | |
AjK | 12:1dfee7208043 | 99 | // Configuration callback on TC |
AjK | 12:1dfee7208043 | 100 | void TC0_callback(void) { |
AjK | 12:1dfee7208043 | 101 | |
AjK | 12:1dfee7208043 | 102 | // Just show sample sequence grab complete. |
AjK | 12:1dfee7208043 | 103 | led3 = !led3; |
AjK | 12:1dfee7208043 | 104 | |
AjK | 12:1dfee7208043 | 105 | // Disable firing timer. |
AjK | 12:1dfee7208043 | 106 | LPC_TIM1->TCR = 0; |
AjK | 12:1dfee7208043 | 107 | |
AjK | 12:1dfee7208043 | 108 | // Get configuration pointer. |
AjK | 12:1dfee7208043 | 109 | MODDMA_Config *config = dma.getConfig(); |
AjK | 12:1dfee7208043 | 110 | |
AjK | 12:1dfee7208043 | 111 | // Finish the DMA cycle by shutting down the channel. |
AjK | 12:1dfee7208043 | 112 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
AjK | 12:1dfee7208043 | 113 | |
AjK | 12:1dfee7208043 | 114 | // Tell main() while(1) loop to print the results. |
AjK | 12:1dfee7208043 | 115 | dmaTransferComplete = true; |
AjK | 12:1dfee7208043 | 116 | |
AjK | 12:1dfee7208043 | 117 | // Clear DMA IRQ flags. |
AjK | 12:1dfee7208043 | 118 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
AjK | 12:1dfee7208043 | 119 | if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq(); |
AjK | 12:1dfee7208043 | 120 | } |
AjK | 12:1dfee7208043 | 121 | |
AjK | 12:1dfee7208043 | 122 | // Configuration callback on Error |
AjK | 12:1dfee7208043 | 123 | void ERR0_callback(void) { |
AjK | 12:1dfee7208043 | 124 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
AjK | 12:1dfee7208043 | 125 | } |