GPDMA (Direct Memory Access) and LLI (Link List Item) test see: http://mbed.org/users/okini3939/notebook/dma_jp/
MODDMA/example4.h@0:de79d4a48e63, 2013-09-13 (annotated)
- Committer:
- okini3939
- Date:
- Fri Sep 13 14:49:52 2013 +0000
- Revision:
- 0:de79d4a48e63
GPDMA (Direct Memory Access) and LLI (Link List Item) test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:de79d4a48e63 | 1 | /* |
okini3939 | 0:de79d4a48e63 | 2 | * Demonstrates sending a buffer repeatedly to the DAC using DMA. |
okini3939 | 0:de79d4a48e63 | 3 | * Connect an oscilloscope to Mbed pin 18. This example doesn't |
okini3939 | 0:de79d4a48e63 | 4 | * output anything else (nothing on any serial ports). |
okini3939 | 0:de79d4a48e63 | 5 | */ |
okini3939 | 0:de79d4a48e63 | 6 | #include "mbed.h" |
okini3939 | 0:de79d4a48e63 | 7 | #include "MODDMA.h" |
okini3939 | 0:de79d4a48e63 | 8 | |
okini3939 | 0:de79d4a48e63 | 9 | // Make the buffer size match the number of degrees |
okini3939 | 0:de79d4a48e63 | 10 | // in a circle since we are going to output a sinewave. |
okini3939 | 0:de79d4a48e63 | 11 | #define BUFFER_SIZE 360 |
okini3939 | 0:de79d4a48e63 | 12 | |
okini3939 | 0:de79d4a48e63 | 13 | // Set DAC output power mode. |
okini3939 | 0:de79d4a48e63 | 14 | #define DAC_POWER_MODE (1 << 16) |
okini3939 | 0:de79d4a48e63 | 15 | |
okini3939 | 0:de79d4a48e63 | 16 | DigitalOut led1(LED1); |
okini3939 | 0:de79d4a48e63 | 17 | DigitalOut led3(LED3); |
okini3939 | 0:de79d4a48e63 | 18 | DigitalOut led4(LED4); |
okini3939 | 0:de79d4a48e63 | 19 | |
okini3939 | 0:de79d4a48e63 | 20 | int buffer[2][BUFFER_SIZE]; |
okini3939 | 0:de79d4a48e63 | 21 | |
okini3939 | 0:de79d4a48e63 | 22 | AnalogOut signal(p18); |
okini3939 | 0:de79d4a48e63 | 23 | |
okini3939 | 0:de79d4a48e63 | 24 | MODDMA dma; |
okini3939 | 0:de79d4a48e63 | 25 | MODDMA_Config *conf0, *conf1; |
okini3939 | 0:de79d4a48e63 | 26 | |
okini3939 | 0:de79d4a48e63 | 27 | void TC0_callback(void); |
okini3939 | 0:de79d4a48e63 | 28 | void ERR0_callback(void); |
okini3939 | 0:de79d4a48e63 | 29 | |
okini3939 | 0:de79d4a48e63 | 30 | void TC1_callback(void); |
okini3939 | 0:de79d4a48e63 | 31 | void ERR1_callback(void); |
okini3939 | 0:de79d4a48e63 | 32 | |
okini3939 | 0:de79d4a48e63 | 33 | int main() { |
okini3939 | 0:de79d4a48e63 | 34 | volatile int life_counter = 0; |
okini3939 | 0:de79d4a48e63 | 35 | |
okini3939 | 0:de79d4a48e63 | 36 | // Create a sinewave buffer for testing. |
okini3939 | 0:de79d4a48e63 | 37 | for (int i = 0; i <= 90; i++) buffer[0][i] = (512 * sin(3.14159/180.0 * i)) + 512; |
okini3939 | 0:de79d4a48e63 | 38 | for (int i = 91; i <= 180; i++) buffer[0][i] = buffer[0][180 - i]; |
okini3939 | 0:de79d4a48e63 | 39 | for (int i = 181; i <= 270; i++) buffer[0][i] = 512 - (buffer[0][i - 180] - 512); |
okini3939 | 0:de79d4a48e63 | 40 | for (int i = 271; i < 360; i++) buffer[0][i] = 512 - (buffer[0][360 - i] - 512); |
okini3939 | 0:de79d4a48e63 | 41 | |
okini3939 | 0:de79d4a48e63 | 42 | // Adjust the sinewave buffer for use with DAC hardware. |
okini3939 | 0:de79d4a48e63 | 43 | for (int i = 0; i < 360; i++) { |
okini3939 | 0:de79d4a48e63 | 44 | buffer[0][i] = DAC_POWER_MODE | ((buffer[0][i] << 6) & 0xFFC0); |
okini3939 | 0:de79d4a48e63 | 45 | buffer[1][i] = buffer[0][i]; // Just create a copy of buffer0 to continue sinewave. |
okini3939 | 0:de79d4a48e63 | 46 | } |
okini3939 | 0:de79d4a48e63 | 47 | |
okini3939 | 0:de79d4a48e63 | 48 | // Prepare the GPDMA system for buffer0. |
okini3939 | 0:de79d4a48e63 | 49 | conf0 = new MODDMA_Config; |
okini3939 | 0:de79d4a48e63 | 50 | conf0 |
okini3939 | 0:de79d4a48e63 | 51 | ->channelNum ( MODDMA::Channel_0 ) |
okini3939 | 0:de79d4a48e63 | 52 | ->srcMemAddr ( (uint32_t) &buffer[0] ) |
okini3939 | 0:de79d4a48e63 | 53 | ->dstMemAddr ( MODDMA::DAC ) |
okini3939 | 0:de79d4a48e63 | 54 | ->transferSize ( 360 ) |
okini3939 | 0:de79d4a48e63 | 55 | ->transferType ( MODDMA::m2p ) |
okini3939 | 0:de79d4a48e63 | 56 | ->dstConn ( MODDMA::DAC ) |
okini3939 | 0:de79d4a48e63 | 57 | ->attach_tc ( &TC0_callback ) |
okini3939 | 0:de79d4a48e63 | 58 | ->attach_err ( &ERR0_callback ) |
okini3939 | 0:de79d4a48e63 | 59 | ; // config end |
okini3939 | 0:de79d4a48e63 | 60 | |
okini3939 | 0:de79d4a48e63 | 61 | |
okini3939 | 0:de79d4a48e63 | 62 | // Prepare the GPDMA system for buffer1. |
okini3939 | 0:de79d4a48e63 | 63 | conf1 = new MODDMA_Config; |
okini3939 | 0:de79d4a48e63 | 64 | conf1 |
okini3939 | 0:de79d4a48e63 | 65 | ->channelNum ( MODDMA::Channel_1 ) |
okini3939 | 0:de79d4a48e63 | 66 | ->srcMemAddr ( (uint32_t) &buffer[1] ) |
okini3939 | 0:de79d4a48e63 | 67 | ->dstMemAddr ( MODDMA::DAC ) |
okini3939 | 0:de79d4a48e63 | 68 | ->transferSize ( 360 ) |
okini3939 | 0:de79d4a48e63 | 69 | ->transferType ( MODDMA::m2p ) |
okini3939 | 0:de79d4a48e63 | 70 | ->dstConn ( MODDMA::DAC ) |
okini3939 | 0:de79d4a48e63 | 71 | ->attach_tc ( &TC1_callback ) |
okini3939 | 0:de79d4a48e63 | 72 | ->attach_err ( &ERR1_callback ) |
okini3939 | 0:de79d4a48e63 | 73 | ; // config end |
okini3939 | 0:de79d4a48e63 | 74 | |
okini3939 | 0:de79d4a48e63 | 75 | |
okini3939 | 0:de79d4a48e63 | 76 | // Calculating the transfer frequency: |
okini3939 | 0:de79d4a48e63 | 77 | // By default, the Mbed library sets the PCLK_DAC clock value |
okini3939 | 0:de79d4a48e63 | 78 | // to 24MHz. One complete sinewave cycle in each buffer is 360 |
okini3939 | 0:de79d4a48e63 | 79 | // points long. So, for a 1Hz wave we would need to transfer 360 |
okini3939 | 0:de79d4a48e63 | 80 | // values per second. That would be 24000000/360 which is approx |
okini3939 | 0:de79d4a48e63 | 81 | // 66,666. But that's no good! The count val is only 16bits in size |
okini3939 | 0:de79d4a48e63 | 82 | // so bare this in mind. If you need to go slower you will need to |
okini3939 | 0:de79d4a48e63 | 83 | // alter PCLK_DAC from CCLK/4 to CCLK/8. |
okini3939 | 0:de79d4a48e63 | 84 | // For our demo we are going to have the sinewave run at 1kHz. |
okini3939 | 0:de79d4a48e63 | 85 | // That's 24000000/360000 which is approx 66. Experimentation |
okini3939 | 0:de79d4a48e63 | 86 | // however showed 65 to get closer to 1kHz (on my Mbed and scope |
okini3939 | 0:de79d4a48e63 | 87 | // at least). |
okini3939 | 0:de79d4a48e63 | 88 | LPC_DAC->DACCNTVAL = 65; // 6500 for 10Hz |
okini3939 | 0:de79d4a48e63 | 89 | |
okini3939 | 0:de79d4a48e63 | 90 | // Prepare first configuration. |
okini3939 | 0:de79d4a48e63 | 91 | if (!dma.Prepare( conf0 )) { |
okini3939 | 0:de79d4a48e63 | 92 | error("Doh!"); |
okini3939 | 0:de79d4a48e63 | 93 | } |
okini3939 | 0:de79d4a48e63 | 94 | |
okini3939 | 0:de79d4a48e63 | 95 | // Begin (enable DMA and counter). Note, don't enable |
okini3939 | 0:de79d4a48e63 | 96 | // DBLBUF_ENA as we are using DMA double buffering. |
okini3939 | 0:de79d4a48e63 | 97 | LPC_DAC->DACCTRL |= (3UL << 2); |
okini3939 | 0:de79d4a48e63 | 98 | |
okini3939 | 0:de79d4a48e63 | 99 | while (1) { |
okini3939 | 0:de79d4a48e63 | 100 | // There's not a lot to do as DMA and interrupts are |
okini3939 | 0:de79d4a48e63 | 101 | // now handling the buffer transfers. So we'll just |
okini3939 | 0:de79d4a48e63 | 102 | // flash led1 to show the Mbed is alive and kicking. |
okini3939 | 0:de79d4a48e63 | 103 | if (life_counter++ > 1000000) { |
okini3939 | 0:de79d4a48e63 | 104 | led1 = !led1; // Show some sort of life. |
okini3939 | 0:de79d4a48e63 | 105 | life_counter = 0; |
okini3939 | 0:de79d4a48e63 | 106 | } |
okini3939 | 0:de79d4a48e63 | 107 | } |
okini3939 | 0:de79d4a48e63 | 108 | } |
okini3939 | 0:de79d4a48e63 | 109 | |
okini3939 | 0:de79d4a48e63 | 110 | // Configuration callback on TC |
okini3939 | 0:de79d4a48e63 | 111 | void TC0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 112 | |
okini3939 | 0:de79d4a48e63 | 113 | // Just show sending buffer0 complete. |
okini3939 | 0:de79d4a48e63 | 114 | led3 = !led3; |
okini3939 | 0:de79d4a48e63 | 115 | |
okini3939 | 0:de79d4a48e63 | 116 | // Get configuration pointer. |
okini3939 | 0:de79d4a48e63 | 117 | MODDMA_Config *config = dma.getConfig(); |
okini3939 | 0:de79d4a48e63 | 118 | |
okini3939 | 0:de79d4a48e63 | 119 | // Finish the DMA cycle by shutting down the channel. |
okini3939 | 0:de79d4a48e63 | 120 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
okini3939 | 0:de79d4a48e63 | 121 | |
okini3939 | 0:de79d4a48e63 | 122 | // Swap to buffer1 |
okini3939 | 0:de79d4a48e63 | 123 | dma.Prepare( conf1 ); |
okini3939 | 0:de79d4a48e63 | 124 | |
okini3939 | 0:de79d4a48e63 | 125 | // Clear DMA IRQ flags. |
okini3939 | 0:de79d4a48e63 | 126 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
okini3939 | 0:de79d4a48e63 | 127 | } |
okini3939 | 0:de79d4a48e63 | 128 | |
okini3939 | 0:de79d4a48e63 | 129 | // Configuration callback on Error |
okini3939 | 0:de79d4a48e63 | 130 | void ERR0_callback(void) { |
okini3939 | 0:de79d4a48e63 | 131 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
okini3939 | 0:de79d4a48e63 | 132 | } |
okini3939 | 0:de79d4a48e63 | 133 | |
okini3939 | 0:de79d4a48e63 | 134 | // Configuration callback on TC |
okini3939 | 0:de79d4a48e63 | 135 | void TC1_callback(void) { |
okini3939 | 0:de79d4a48e63 | 136 | |
okini3939 | 0:de79d4a48e63 | 137 | // Just show sending buffer1 complete. |
okini3939 | 0:de79d4a48e63 | 138 | led4 = !led4; |
okini3939 | 0:de79d4a48e63 | 139 | |
okini3939 | 0:de79d4a48e63 | 140 | // Get configuration pointer. |
okini3939 | 0:de79d4a48e63 | 141 | MODDMA_Config *config = dma.getConfig(); |
okini3939 | 0:de79d4a48e63 | 142 | |
okini3939 | 0:de79d4a48e63 | 143 | // Finish the DMA cycle by shutting down the channel. |
okini3939 | 0:de79d4a48e63 | 144 | dma.Disable( (MODDMA::CHANNELS)config->channelNum() ); |
okini3939 | 0:de79d4a48e63 | 145 | |
okini3939 | 0:de79d4a48e63 | 146 | // Swap to buffer0 |
okini3939 | 0:de79d4a48e63 | 147 | dma.Prepare( conf0 ); |
okini3939 | 0:de79d4a48e63 | 148 | |
okini3939 | 0:de79d4a48e63 | 149 | // Clear DMA IRQ flags. |
okini3939 | 0:de79d4a48e63 | 150 | if (dma.irqType() == MODDMA::TcIrq) dma.clearTcIrq(); |
okini3939 | 0:de79d4a48e63 | 151 | } |
okini3939 | 0:de79d4a48e63 | 152 | |
okini3939 | 0:de79d4a48e63 | 153 | // Configuration callback on Error |
okini3939 | 0:de79d4a48e63 | 154 | void ERR1_callback(void) { |
okini3939 | 0:de79d4a48e63 | 155 | error("Oh no! My Mbed EXPLODED! :( Only kidding, go find the problem"); |
okini3939 | 0:de79d4a48e63 | 156 | } |