Jared Baxter
/
Impedance_Fast_Circuitry
Fork of DSP_200kHz by
dma.cpp@39:82dc3daecf32, 2015-01-29 (annotated)
- Committer:
- timmey9
- Date:
- Thu Jan 29 16:18:54 2015 +0000
- Revision:
- 39:82dc3daecf32
- Parent:
- 37:8bdc71f3e874
- Child:
- 40:bd6d8c35e822
Cleaned up the code. PDB still won't work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 34:44cc9b76a507 | 1 | /* |
timmey9 | 34:44cc9b76a507 | 2 | * dma.c |
timmey9 | 34:44cc9b76a507 | 3 | * |
timmey9 | 34:44cc9b76a507 | 4 | * Created on: Nov 25, 2014 |
timmey9 | 34:44cc9b76a507 | 5 | * Author: Manuel Alejandro |
timmey9 | 34:44cc9b76a507 | 6 | */ |
timmey9 | 34:44cc9b76a507 | 7 | #include "dma.h" |
timmey9 | 34:44cc9b76a507 | 8 | |
timmey9 | 34:44cc9b76a507 | 9 | /* dma_init() |
timmey9 | 34:44cc9b76a507 | 10 | * Initializes the DMA module to read the ADC results every time a conversion has |
timmey9 | 34:44cc9b76a507 | 11 | * finished and stores its value in a buffer |
timmey9 | 34:44cc9b76a507 | 12 | * |
timmey9 | 36:07d8a3143967 | 13 | * @array0 = destination where DMA0 writes |
timmey9 | 36:07d8a3143967 | 14 | * @array1 = destination where DMA1 writes |
timmey9 | 36:07d8a3143967 | 15 | * @array2 = destination where DMA2 writes |
timmey9 | 36:07d8a3143967 | 16 | * @len = the length of array1 and array2, and the number of reads the DMA completes |
timmey9 | 34:44cc9b76a507 | 17 | * */ |
timmey9 | 34:44cc9b76a507 | 18 | |
timmey9 | 39:82dc3daecf32 | 19 | void dma_init(uint16_t* array0, uint16_t* array1, uint16_t* array2, int len, Serial &pc) |
timmey9 | 34:44cc9b76a507 | 20 | { |
timmey9 | 36:07d8a3143967 | 21 | // select round-robin arbitration priority |
timmey9 | 36:07d8a3143967 | 22 | DMA_CR |= DMA_CR_ERCA_MASK; |
timmey9 | 36:07d8a3143967 | 23 | |
timmey9 | 34:44cc9b76a507 | 24 | // Enable clock for DMAMUX and DMA |
timmey9 | 34:44cc9b76a507 | 25 | SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK; |
timmey9 | 34:44cc9b76a507 | 26 | SIM_SCGC7 |= SIM_SCGC7_DMA_MASK; |
timmey9 | 34:44cc9b76a507 | 27 | |
timmey9 | 34:44cc9b76a507 | 28 | // Enable Channel 0 and set ADC0 as DMA request source |
timmey9 | 36:07d8a3143967 | 29 | DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(40); // see page 95 of user manual |
timmey9 | 36:07d8a3143967 | 30 | DMAMUX_CHCFG1 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(41); |
timmey9 | 37:8bdc71f3e874 | 31 | //DMAMUX_CHCFG2 |= DMAMUX_CHCFG_ENBL_MASK | |
timmey9 | 36:07d8a3143967 | 32 | |
timmey9 | 36:07d8a3143967 | 33 | |
timmey9 | 34:44cc9b76a507 | 34 | // Enable request signal for channel 0 |
timmey9 | 36:07d8a3143967 | 35 | DMA_ERQ = DMA_ERQ_ERQ0_MASK | DMA_ERQ_ERQ1_MASK | DMA_ERQ_ERQ2_MASK; |
timmey9 | 34:44cc9b76a507 | 36 | |
timmey9 | 36:07d8a3143967 | 37 | // Set memory address for source and destination for DMA0, DMA1, and DMA2 |
timmey9 | 34:44cc9b76a507 | 38 | DMA_TCD0_SADDR = (uint32_t)&ADC0_RA; |
timmey9 | 36:07d8a3143967 | 39 | DMA_TCD0_DADDR = (uint32_t) array0; |
timmey9 | 36:07d8a3143967 | 40 | DMA_TCD1_SADDR = (uint32_t)&ADC1_RA; |
timmey9 | 36:07d8a3143967 | 41 | DMA_TCD1_DADDR = (uint32_t) array1; |
timmey9 | 36:07d8a3143967 | 42 | DMA_TCD2_SADDR = (uint32_t)&rotary_count; |
timmey9 | 36:07d8a3143967 | 43 | DMA_TCD2_DADDR = (uint32_t) array2; |
timmey9 | 36:07d8a3143967 | 44 | |
timmey9 | 34:44cc9b76a507 | 45 | // Set an offset for source and destination address |
timmey9 | 34:44cc9b76a507 | 46 | DMA_TCD0_SOFF = 0x00; // Source address offset of 2 bits per transaction |
timmey9 | 34:44cc9b76a507 | 47 | DMA_TCD0_DOFF = 0x02; // Destination address offset of 1 bit per transaction |
timmey9 | 36:07d8a3143967 | 48 | DMA_TCD1_SOFF = 0x00; // Source address offset of 2 bits per transaction |
timmey9 | 36:07d8a3143967 | 49 | DMA_TCD1_DOFF = 0x02; // Destination address offset of 1 bit per transaction |
timmey9 | 36:07d8a3143967 | 50 | DMA_TCD2_SOFF = 0x00; // Source address offset of 2 bits per transaction |
timmey9 | 36:07d8a3143967 | 51 | DMA_TCD2_DOFF = 0x02; // Destination address offset of 1 bit per transaction |
timmey9 | 34:44cc9b76a507 | 52 | |
timmey9 | 34:44cc9b76a507 | 53 | // Set source and destination data transfer size |
timmey9 | 34:44cc9b76a507 | 54 | DMA_TCD0_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); |
timmey9 | 36:07d8a3143967 | 55 | DMA_TCD1_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); |
timmey9 | 36:07d8a3143967 | 56 | DMA_TCD2_ATTR = DMA_ATTR_SSIZE(1) | DMA_ATTR_DSIZE(1); |
timmey9 | 34:44cc9b76a507 | 57 | |
timmey9 | 34:44cc9b76a507 | 58 | // Number of bytes to be transfered in each service request of the channel |
timmey9 | 34:44cc9b76a507 | 59 | DMA_TCD0_NBYTES_MLNO = 0x02; |
timmey9 | 36:07d8a3143967 | 60 | DMA_TCD1_NBYTES_MLNO = 0x02; |
timmey9 | 36:07d8a3143967 | 61 | DMA_TCD2_NBYTES_MLNO = 0x02; |
timmey9 | 34:44cc9b76a507 | 62 | |
timmey9 | 34:44cc9b76a507 | 63 | // Current major iteration count (a single iteration of 5 bytes) |
timmey9 | 35:df40c4566826 | 64 | DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); |
timmey9 | 35:df40c4566826 | 65 | DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); |
timmey9 | 36:07d8a3143967 | 66 | DMA_TCD1_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); |
timmey9 | 36:07d8a3143967 | 67 | DMA_TCD1_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); |
timmey9 | 36:07d8a3143967 | 68 | DMA_TCD2_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(len); |
timmey9 | 36:07d8a3143967 | 69 | DMA_TCD2_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(len); |
timmey9 | 34:44cc9b76a507 | 70 | |
timmey9 | 34:44cc9b76a507 | 71 | // Adjustment value used to restore the source and destiny address to the initial value |
timmey9 | 36:07d8a3143967 | 72 | // After reading 'len' number of times, the DMA goes back to the beginning by subtracting len*2 from the address (going back to the address of 'array0') |
timmey9 | 34:44cc9b76a507 | 73 | DMA_TCD0_SLAST = 0x00; // Source address adjustment |
timmey9 | 35:df40c4566826 | 74 | DMA_TCD0_DLASTSGA = -(len*2); // Destination address adjustment |
timmey9 | 36:07d8a3143967 | 75 | DMA_TCD1_SLAST = 0x00; // Source address adjustment |
timmey9 | 36:07d8a3143967 | 76 | DMA_TCD1_DLASTSGA = -(len*2); // Destination address adjustment |
timmey9 | 36:07d8a3143967 | 77 | DMA_TCD2_SLAST = 0x00; // Source address adjustment |
timmey9 | 36:07d8a3143967 | 78 | DMA_TCD2_DLASTSGA = -(len*2); // Destination address adjustment |
timmey9 | 34:44cc9b76a507 | 79 | |
timmey9 | 34:44cc9b76a507 | 80 | // Setup control and status register |
timmey9 | 34:44cc9b76a507 | 81 | DMA_TCD0_CSR = 0; |
timmey9 | 36:07d8a3143967 | 82 | DMA_TCD1_CSR = 0; |
timmey9 | 36:07d8a3143967 | 83 | DMA_TCD2_CSR = 1; |
timmey9 | 39:82dc3daecf32 | 84 | |
timmey9 | 39:82dc3daecf32 | 85 | pc.printf("DMA_CR: %08x\r\n", DMA_CR); |
timmey9 | 39:82dc3daecf32 | 86 | pc.printf("DMA_ES: %08x\r\n", DMA_ES); |
timmey9 | 39:82dc3daecf32 | 87 | pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ); |
timmey9 | 39:82dc3daecf32 | 88 | pc.printf("DMA_EEI: %08x\r\n", DMA_EEI); |
timmey9 | 39:82dc3daecf32 | 89 | pc.printf("DMA_CEEI: %02x\r\n", DMA_CEEI); |
timmey9 | 39:82dc3daecf32 | 90 | pc.printf("DMA_SEEI: %02x\r\n", DMA_SEEI); |
timmey9 | 39:82dc3daecf32 | 91 | pc.printf("DMA_CERQ: %02x\r\n", DMA_CERQ); |
timmey9 | 39:82dc3daecf32 | 92 | pc.printf("DMA_SERQ: %02x\r\n", DMA_SERQ); |
timmey9 | 39:82dc3daecf32 | 93 | pc.printf("DMA_CDNE: %02x\r\n", DMA_CDNE); |
timmey9 | 39:82dc3daecf32 | 94 | pc.printf("DMA_SSRT: %02x\r\n", DMA_SSRT); |
timmey9 | 39:82dc3daecf32 | 95 | pc.printf("DMA_CERR: %02x\r\n", DMA_CERR); |
timmey9 | 39:82dc3daecf32 | 96 | pc.printf("DMA_CINT: %02x\r\n", DMA_CINT); |
timmey9 | 39:82dc3daecf32 | 97 | pc.printf("DMA_INT: %08x\r\n", DMA_INT); |
timmey9 | 39:82dc3daecf32 | 98 | pc.printf("DMA_ERR: %08x\r\n", DMA_ERR); |
timmey9 | 39:82dc3daecf32 | 99 | pc.printf("DMA_HRS: %08x\r\n", DMA_HRS); |
timmey9 | 34:44cc9b76a507 | 100 | } |