http://http://diytec.web.fc2.com/mark2r2/

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Committer:
mark2r2
Date:
Tue Sep 20 12:46:26 2011 +0000
Revision:
0:08a4d61cd84c
V1.0

Who changed what in which revision?

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