|
| 1 | +/* |
| 2 | + * This file is part of the unicore-mx project. |
| 3 | + * |
| 4 | + * Copyright (C) 2015 Kuldeep Singh Dhaka <[email protected]> |
| 5 | + * |
| 6 | + * This library is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <unicore-mx/efm32/cmu.h> |
| 21 | +#include <unicore-mx/efm32/gpio.h> |
| 22 | +#include <unicore-mx/efm32/dma.h> |
| 23 | +#include <unicore-mx/efm32/adc.h> |
| 24 | +#include <unicore-mx/efm32/prs.h> |
| 25 | +#include <unicore-mx/efm32/timer.h> |
| 26 | + |
| 27 | +#define SAMPLES_COUNT 1000 |
| 28 | +static uint16_t samples[SAMPLES_COUNT] __attribute__((aligned(16))); |
| 29 | + |
| 30 | +/* we need only 1 channel (0th channel) */ |
| 31 | +static uint8_t dma_desc_mem[DMA_DESC_CH_SIZE * 1] __attribute__((aligned(256))); |
| 32 | + |
| 33 | +static void clock_setup(void) |
| 34 | +{ |
| 35 | + cmu_clock_setup_in_hfxo_out_48mhz(); |
| 36 | + |
| 37 | + cmu_periph_clock_enable(CMU_GPIO); |
| 38 | + cmu_periph_clock_enable(CMU_ADC0); |
| 39 | + cmu_periph_clock_enable(CMU_DMA); |
| 40 | + cmu_periph_clock_enable(CMU_TIMER0); |
| 41 | + cmu_periph_clock_enable(CMU_PRS); |
| 42 | +} |
| 43 | + |
| 44 | +static void led_setup(void) |
| 45 | +{ |
| 46 | + gpio_clear(GPIOE, GPIO2); |
| 47 | + gpio_mode_setup(GPIOE, GPIO_MODE_PUSH_PULL, GPIO2); |
| 48 | +} |
| 49 | + |
| 50 | +static void adc_setup(void) |
| 51 | +{ |
| 52 | + /* |
| 53 | + * Tconv = (Ta + N) * OVS |
| 54 | + * (Tconv / 12Mhz) < (1 / 500Khz) [500Khz is timer (PRS CH0) trigger rate] |
| 55 | + * Tconv < (120 / 5) |
| 56 | + * Tconv < 24 |
| 57 | + * |
| 58 | + * we need atleast 12bit resolution, N = 13 |
| 59 | + * (Ta + 13) * OVS < 24 |
| 60 | + * for OVS = 1 |
| 61 | + * |
| 62 | + * Ta < 11 |
| 63 | + * so, Ta = 8 |
| 64 | + * Acquisition time = 8 cycles |
| 65 | + */ |
| 66 | + |
| 67 | + /* |
| 68 | + * ADC Warm up = 48Mhz / Timebase = 48Mhz / (95 + 1) = 2us (and is <= 1us) |
| 69 | + * prescalar = 48Mhz / (3 + 1) = 12Mhz (and is <= 13Mhz) |
| 70 | + * keep adc warm between samples |
| 71 | + */ |
| 72 | + /* TODO: workaround(ADC_E117) */ |
| 73 | + ADC0_CTRL = ADC_CTRL_TIMEBASE(95) | ADC_CTRL_PRESC(3) | |
| 74 | + ADC_CTRL_WARMUPMODE_KEEPADCWARM; |
| 75 | + |
| 76 | + /* |
| 77 | + * get trigger from PRS Ch0 (connected to timer0, at 500Khz) |
| 78 | + * Reference: VDD |
| 79 | + * Acquisition Time: 8 ADC Clocks |
| 80 | + * Resolution: 12bit |
| 81 | + * Channels: {0, 1, 2, 3, 4, 5, 6, 7} |
| 82 | + */ |
| 83 | + ADC0_SCANCTRL = ADC_SCANCTRL_PRSSEL_PRSCH0 | ADC_SCANCTRL_PRSEN | |
| 84 | + ADC_SCANCTRL_AT_8CYCLES | ADC_SCANCTRL_REF_VDD | |
| 85 | + ADC_SCANCTRL_INPUTSEL_CH0 | ADC_SCANCTRL_INPUTSEL_CH1 | |
| 86 | + ADC_SCANCTRL_INPUTSEL_CH2 | ADC_SCANCTRL_INPUTSEL_CH3 | |
| 87 | + ADC_SCANCTRL_INPUTSEL_CH4 | ADC_SCANCTRL_INPUTSEL_CH5 | |
| 88 | + ADC_SCANCTRL_INPUTSEL_CH6 | ADC_SCANCTRL_INPUTSEL_CH7 | |
| 89 | + ADC_SCANCTRL_RES_12BIT; |
| 90 | +} |
| 91 | + |
| 92 | +static void dma_setup(void) |
| 93 | +{ |
| 94 | + uint32_t dma_desc = (uint32_t) dma_desc_mem; |
| 95 | + |
| 96 | + /* DMA global */ |
| 97 | + DMA_CONFIG = DMA_CONFIG_EN; |
| 98 | + DMA_CTRLBASE = dma_desc; |
| 99 | + |
| 100 | + /* channel descriptor */ |
| 101 | + uint16_t n_minus_1 = SAMPLES_COUNT - 1; |
| 102 | + DMA_DESC_CHx_CFG(dma_desc, 0) = |
| 103 | + DMA_DESC_CH_CFG_DEST_INC_HALFWORD | DMA_DESC_CH_CFG_DEST_SIZE_HALFWORD | |
| 104 | + DMA_DESC_CH_CFG_SRC_INC_NOINC | DMA_DESC_CH_CFG_SRC_SIZE_HALFWORD | |
| 105 | + DMA_DESC_CH_CFG_R_POWER(DMA_R_POWER_1) | DMA_DESC_CH_CFG_CYCLE_CTRL_BASIC | |
| 106 | + DMA_DESC_CH_CFG_N_MINUS_1(n_minus_1); |
| 107 | + DMA_DESC_CHx_DEST_DATA_END_PTR(dma_desc, 0) = ((uint32_t) samples) + (n_minus_1 << 1); |
| 108 | + DMA_DESC_CHx_SRC_DATA_END_PTR(dma_desc, 0) = (uint32_t) &ADC0_SCANDATA; |
| 109 | + |
| 110 | + /* Channel 0 */ |
| 111 | + DMA_CH0_CTRL = DMA_CH_CTRL_SOURCESEL_ADC0 | DMA_CH_CTRL_SIGSEL_ADC0SCAN; |
| 112 | + DMA_CHREQMASKC = DMA_CHREQMASKC_CH0SREQMASKC; |
| 113 | + DMA_RDS = DMA_RDS_RDSCH0; |
| 114 | + DMA_CHENS = DMA_CHENS_CH0SENS; |
| 115 | +} |
| 116 | + |
| 117 | +static void timer_setup(void) |
| 118 | +{ |
| 119 | + TIMER0_TOP = (48 * 2 * 8) - 1; |
| 120 | +} |
| 121 | + |
| 122 | +static void prs_setup(void) |
| 123 | +{ |
| 124 | + PRS_CH0_CTRL = PRS_CH_CTRL_SOURCESEL_TIMER0 | PRS_CH_CTRL_SIGSEL_TIMER0OF; |
| 125 | +} |
| 126 | + |
| 127 | +int main(void) |
| 128 | +{ |
| 129 | + clock_setup(); |
| 130 | + led_setup(); |
| 131 | + adc_setup(); |
| 132 | + dma_setup(); |
| 133 | + timer_setup(); |
| 134 | + prs_setup(); |
| 135 | + |
| 136 | + /* start timer */ |
| 137 | + TIMER0_CMD = TIMER_CMD_START; |
| 138 | + |
| 139 | + /* wait till dma not received data */ |
| 140 | + while(!(DMA_IF & DMA_IF_CH0DONE)); |
| 141 | + |
| 142 | + /* stop timer */ |
| 143 | + TIMER0_CMD = TIMER_CMD_STOP; |
| 144 | + |
| 145 | + /* turn "LED0" on */ |
| 146 | + gpio_set(GPIOE, GPIO2); |
| 147 | + |
| 148 | + while(1); |
| 149 | + return 0; |
| 150 | +} |
0 commit comments