trying to make telnetserver repo public

Fork of MODDMA by Andy K

Committer:
AjK
Date:
Mon Mar 14 00:11:56 2011 +0000
Revision:
14:15939b260dfa
Parent:
13:846c8b6ffb7f
Child:
15:e01144bae101
1.10 See ChangeLog.c

Who changed what in which revision?

UserRevisionLine numberNew 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 14:15939b260dfa 32 MODDMA_Config *conf, conf_copy;
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
AjK 12:1dfee7208043 40 // Macros defined in iomacros.h, saves messing with DigitalIn
AjK 12:1dfee7208043 41 p30_AS_INPUT; p30_MODE( PIN_PULLUP ); // P0.4
AjK 12:1dfee7208043 42 p29_AS_INPUT; p29_MODE( PIN_PULLUP ); // P0.5
AjK 12:1dfee7208043 43 p8_AS_INPUT; p8_MODE( PIN_PULLUP ); // P0.6
AjK 12:1dfee7208043 44 p7_AS_INPUT; p7_MODE( PIN_PULLUP ); // 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 // Setup the serial port to print out results.
AjK 12:1dfee7208043 50 pc.baud(115200);
AjK 12:1dfee7208043 51 pc.printf("Starting up...\n");
AjK 12:1dfee7208043 52
AjK 12:1dfee7208043 53 // Set-up timer1 as a periodic timer.
AjK 12:1dfee7208043 54 LPC_SC->PCONP |= (1UL << 2); // TIM1 On
AjK 12:1dfee7208043 55 LPC_SC->PCLKSEL0 |= (3UL << 4); // CCLK/8 = 12MHz
AjK 12:1dfee7208043 56 LPC_TIM1->PR = 11; // TC clocks at 1MHz.
AjK 12:1dfee7208043 57 LPC_TIM1->MCR = 2; // Reset TCR to zero on match.
AjK 12:1dfee7208043 58 LPC_TIM1->MR0 = SAMPLE_PERIOD;
AjK 12:1dfee7208043 59
AjK 12:1dfee7208043 60 // Prepare the GPDMA system.
AjK 12:1dfee7208043 61 conf = new MODDMA_Config;
AjK 12:1dfee7208043 62 conf
AjK 12:1dfee7208043 63 ->channelNum ( MODDMA::Channel_0 )
AjK 12:1dfee7208043 64 ->srcMemAddr ( (uint32_t)&LPC_GPIO0->FIOPIN )
AjK 12:1dfee7208043 65 ->dstMemAddr ( (uint32_t)&buffer[0] )
AjK 12:1dfee7208043 66 ->transferSize ( NUM_OF_SAMPLES )
AjK 12:1dfee7208043 67 ->transferType ( MODDMA::g2m ) // pseudo transfer code MODDMA understands.
AjK 12:1dfee7208043 68 ->transferWidth ( MODDMA::word )
AjK 12:1dfee7208043 69 ->srcConn ( MODDMA::MAT1_0 )
AjK 12:1dfee7208043 70 ->dmacSync ( MODDMA::MAT1_0 )
AjK 12:1dfee7208043 71 ->attach_tc ( TC0_callback )
AjK 12:1dfee7208043 72 ->attach_err ( ERR0_callback )
AjK 12:1dfee7208043 73 ; // end conf.
AjK 12:1dfee7208043 74
AjK 12:1dfee7208043 75 // Prepare configuration.
AjK 12:1dfee7208043 76 if (!dma.Setup( conf )) {
AjK 12:1dfee7208043 77 error("Doh!");
AjK 12:1dfee7208043 78 }
AjK 12:1dfee7208043 79
AjK 12:1dfee7208043 80 // Enable GPDMA to be ready for the TIM1 "ticks".
AjK 12:1dfee7208043 81 dma.Enable( conf );
AjK 12:1dfee7208043 82
AjK 12:1dfee7208043 83 // Begin.
AjK 12:1dfee7208043 84 LPC_TIM1->TCR = 1;
AjK 12:1dfee7208043 85
AjK 14:15939b260dfa 86 while (1) {
AjK 14:15939b260dfa 87 if (life_counter++ > 1000000) {
AjK 14:15939b260dfa 88 led1 = !led1; // Show some sort of life.
AjK 14:15939b260dfa 89 life_counter = 0;
AjK 14:15939b260dfa 90 }
AjK 12:1dfee7208043 91
AjK 12:1dfee7208043 92 if (dmaTransferComplete) {
AjK 12:1dfee7208043 93 dmaTransferComplete = false;
AjK 12:1dfee7208043 94 for (int i = 0; i < NUM_OF_SAMPLES; i++) {
AjK 12:1dfee7208043 95 int val = (buffer[i] >> 4) & 0xF;
AjK 12:1dfee7208043 96 pc.printf("Buffer index %d = 0x%x\n", i, val);
AjK 12:1dfee7208043 97 }
AjK 13:846c8b6ffb7f 98 pc.printf("Done.\n");
AjK 13:846c8b6ffb7f 99
AjK 13:846c8b6ffb7f 100 // Schedule another grab.
AjK 13:846c8b6ffb7f 101 if (dma.Setup( conf )) {
AjK 14:15939b260dfa 102 dma.Enable( conf );
AjK 13:846c8b6ffb7f 103 }
AjK 12:1dfee7208043 104 }
AjK 12:1dfee7208043 105 }
AjK 12:1dfee7208043 106 }
AjK 12:1dfee7208043 107
AjK 12:1dfee7208043 108 // Configuration callback on TC
AjK 12:1dfee7208043 109 void TC0_callback(void) {
AjK 12:1dfee7208043 110
AjK 12:1dfee7208043 111 // Just show sample sequence grab complete.
AjK 12:1dfee7208043 112 led3 = !led3;
AjK 14:15939b260dfa 113
AjK 12:1dfee7208043 114 // Get configuration pointer.
AjK 12:1dfee7208043 115 MODDMA_Config *config = dma.getConfig();
AjK 12:1dfee7208043 116
AjK 12:1dfee7208043 117 // Finish the DMA cycle by shutting down the channel.
AjK 12:1dfee7208043 118 dma.Disable( (MODDMA::CHANNELS)config->channelNum() );
AjK 12:1dfee7208043 119
AjK 12:1dfee7208043 120 // Tell main() while(1) loop to print the results.
AjK 12:1dfee7208043 121 dmaTransferComplete = true;
AjK 12:1dfee7208043 122
AjK 12:1dfee7208043 123 // Clear DMA IRQ flags.
AjK 12:1dfee7208043 124 if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq();
AjK 13:846c8b6ffb7f 125 if (dma.irqType() == MODDMA::ErrIrq) dma.clearErrIrq();
AjK 12:1dfee7208043 126 }
AjK 12:1dfee7208043 127
AjK 12:1dfee7208043 128 // Configuration callback on Error
AjK 12:1dfee7208043 129 void ERR0_callback(void) {
AjK 12:1dfee7208043 130 error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem");
AjK 12:1dfee7208043 131 }