Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_Maxim/TARGET_MAX32610/analogin_api.c@636:b0d178e9fa10, 2016-07-07 (annotated)
- Committer:
- lzbpli
- Date:
- Thu Jul 07 06:48:59 2016 +0000
- Revision:
- 636:b0d178e9fa10
- Parent:
- 515:7467ef1f4ad8
l053
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 507:d4fc7603a669 | 1 | /******************************************************************************* |
mbed_official | 507:d4fc7603a669 | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
mbed_official | 507:d4fc7603a669 | 3 | * |
mbed_official | 507:d4fc7603a669 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
mbed_official | 507:d4fc7603a669 | 5 | * copy of this software and associated documentation files (the "Software"), |
mbed_official | 507:d4fc7603a669 | 6 | * to deal in the Software without restriction, including without limitation |
mbed_official | 507:d4fc7603a669 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
mbed_official | 507:d4fc7603a669 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 507:d4fc7603a669 | 9 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 507:d4fc7603a669 | 10 | * |
mbed_official | 507:d4fc7603a669 | 11 | * The above copyright notice and this permission notice shall be included |
mbed_official | 507:d4fc7603a669 | 12 | * in all copies or substantial portions of the Software. |
mbed_official | 507:d4fc7603a669 | 13 | * |
mbed_official | 507:d4fc7603a669 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
mbed_official | 507:d4fc7603a669 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
mbed_official | 507:d4fc7603a669 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
mbed_official | 507:d4fc7603a669 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
mbed_official | 507:d4fc7603a669 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
mbed_official | 507:d4fc7603a669 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
mbed_official | 507:d4fc7603a669 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 507:d4fc7603a669 | 21 | * |
mbed_official | 507:d4fc7603a669 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
mbed_official | 507:d4fc7603a669 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
mbed_official | 507:d4fc7603a669 | 24 | * Products, Inc. Branding Policy. |
mbed_official | 507:d4fc7603a669 | 25 | * |
mbed_official | 507:d4fc7603a669 | 26 | * The mere transfer of this software does not imply any licenses |
mbed_official | 507:d4fc7603a669 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
mbed_official | 507:d4fc7603a669 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
mbed_official | 507:d4fc7603a669 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
mbed_official | 507:d4fc7603a669 | 30 | * ownership rights. |
mbed_official | 507:d4fc7603a669 | 31 | ******************************************************************************* |
mbed_official | 507:d4fc7603a669 | 32 | */ |
mbed_official | 507:d4fc7603a669 | 33 | |
mbed_official | 507:d4fc7603a669 | 34 | #include "mbed_assert.h" |
mbed_official | 507:d4fc7603a669 | 35 | #include "analogin_api.h" |
mbed_official | 507:d4fc7603a669 | 36 | #include "clkman_regs.h" |
mbed_official | 507:d4fc7603a669 | 37 | #include "pwrman_regs.h" |
mbed_official | 507:d4fc7603a669 | 38 | #include "afe_regs.h" |
mbed_official | 507:d4fc7603a669 | 39 | #include "PeripheralPins.h" |
mbed_official | 507:d4fc7603a669 | 40 | |
mbed_official | 515:7467ef1f4ad8 | 41 | #define PGA_TRK_CNT 0x1F |
mbed_official | 507:d4fc7603a669 | 42 | #define ADC_ACT_CNT 0x1 |
mbed_official | 507:d4fc7603a669 | 43 | #define ADC_PGA_CNT 0x1 |
mbed_official | 507:d4fc7603a669 | 44 | #define ADC_ACQ_CNT 0x1 |
mbed_official | 515:7467ef1f4ad8 | 45 | #define ADC_SLP_CNT 0x1 |
mbed_official | 507:d4fc7603a669 | 46 | |
mbed_official | 507:d4fc7603a669 | 47 | //****************************************************************************** |
mbed_official | 507:d4fc7603a669 | 48 | void analogin_init(analogin_t *obj, PinName pin) |
mbed_official | 507:d4fc7603a669 | 49 | { |
mbed_official | 507:d4fc7603a669 | 50 | // Make sure pin is an analog pin we can use for ADC |
mbed_official | 507:d4fc7603a669 | 51 | MBED_ASSERT((ADCName)pinmap_peripheral(pin, PinMap_ADC) != (ADCName)NC); |
mbed_official | 507:d4fc7603a669 | 52 | |
mbed_official | 507:d4fc7603a669 | 53 | // Set the object pointer |
mbed_official | 507:d4fc7603a669 | 54 | obj->adc = MXC_ADC; |
mbed_official | 507:d4fc7603a669 | 55 | obj->adccfg = MXC_ADCCFG; |
mbed_official | 507:d4fc7603a669 | 56 | obj->adc_fifo = MXC_ADC_FIFO; |
mbed_official | 507:d4fc7603a669 | 57 | obj->adc_pin = pin; |
mbed_official | 507:d4fc7603a669 | 58 | |
mbed_official | 507:d4fc7603a669 | 59 | // Set the ADC clock to the system clock frequency |
mbed_official | 507:d4fc7603a669 | 60 | MXC_SET_FIELD(&MXC_CLKMAN->clk_ctrl, MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT, |
mbed_official | 507:d4fc7603a669 | 61 | (MXC_F_CLKMAN_CLK_CTRL_ADC_GATE_N | (MXC_E_CLKMAN_ADC_SOURCE_SELECT_SYSTEM << |
mbed_official | 507:d4fc7603a669 | 62 | MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT_POS))); |
mbed_official | 507:d4fc7603a669 | 63 | |
mbed_official | 507:d4fc7603a669 | 64 | // Enable AFE power |
mbed_official | 507:d4fc7603a669 | 65 | MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED; |
mbed_official | 507:d4fc7603a669 | 66 | |
mbed_official | 507:d4fc7603a669 | 67 | // Setup and hold window |
mbed_official | 507:d4fc7603a669 | 68 | MXC_SET_FIELD(&obj->adc->tg_ctrl0, MXC_F_ADC_TG_CTRL0_PGA_TRK_CNT, PGA_TRK_CNT); |
mbed_official | 507:d4fc7603a669 | 69 | |
mbed_official | 507:d4fc7603a669 | 70 | // Setup sampling count and timing |
mbed_official | 507:d4fc7603a669 | 71 | MXC_SET_FIELD(&obj->adc->tg_ctrl1, (MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT | |
mbed_official | 507:d4fc7603a669 | 72 | MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT | MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT), |
mbed_official | 507:d4fc7603a669 | 73 | ((ADC_PGA_CNT << MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT_POS) | |
mbed_official | 507:d4fc7603a669 | 74 | (ADC_ACQ_CNT << MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT_POS) | |
mbed_official | 507:d4fc7603a669 | 75 | (ADC_SLP_CNT << MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT_POS) | |
mbed_official | 507:d4fc7603a669 | 76 | (MXC_F_ADC_TG_CTRL1_ADC_BRST_CNT))); |
mbed_official | 507:d4fc7603a669 | 77 | } |
mbed_official | 507:d4fc7603a669 | 78 | |
mbed_official | 507:d4fc7603a669 | 79 | //****************************************************************************** |
mbed_official | 507:d4fc7603a669 | 80 | float analogin_read(analogin_t *obj) |
mbed_official | 507:d4fc7603a669 | 81 | { |
mbed_official | 507:d4fc7603a669 | 82 | // Convert integer to float |
mbed_official | 507:d4fc7603a669 | 83 | return (((float)analogin_read_u16(obj)/(float)0xFFFF)); |
mbed_official | 507:d4fc7603a669 | 84 | } |
mbed_official | 507:d4fc7603a669 | 85 | |
mbed_official | 507:d4fc7603a669 | 86 | //****************************************************************************** |
mbed_official | 507:d4fc7603a669 | 87 | uint16_t analogin_read_u16(analogin_t *obj) |
mbed_official | 507:d4fc7603a669 | 88 | { |
mbed_official | 507:d4fc7603a669 | 89 | // Set the pin to take readings from |
mbed_official | 507:d4fc7603a669 | 90 | unsigned mux_pos; |
mbed_official | 507:d4fc7603a669 | 91 | unsigned diff = 0; |
mbed_official | 507:d4fc7603a669 | 92 | if(obj->adc_pin >> PORT_SHIFT == 0xB) { |
mbed_official | 507:d4fc7603a669 | 93 | mux_pos = (obj->adc_pin & 0xF) + 8; |
mbed_official | 507:d4fc7603a669 | 94 | } else { |
mbed_official | 507:d4fc7603a669 | 95 | mux_pos = (obj->adc_pin & 0xF); |
mbed_official | 507:d4fc7603a669 | 96 | } |
mbed_official | 507:d4fc7603a669 | 97 | |
mbed_official | 507:d4fc7603a669 | 98 | if(obj->adc_pin >> PORT_SHIFT == 0xC) { |
mbed_official | 507:d4fc7603a669 | 99 | diff = 1; |
mbed_official | 507:d4fc7603a669 | 100 | mux_pos = (obj->adc_pin & 0xF) + 8; |
mbed_official | 507:d4fc7603a669 | 101 | } |
mbed_official | 507:d4fc7603a669 | 102 | |
mbed_official | 515:7467ef1f4ad8 | 103 | // Reset the ADC |
mbed_official | 515:7467ef1f4ad8 | 104 | obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_RST; |
mbed_official | 515:7467ef1f4ad8 | 105 | |
mbed_official | 515:7467ef1f4ad8 | 106 | // Enable the ADC |
mbed_official | 515:7467ef1f4ad8 | 107 | obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_EN; |
mbed_official | 515:7467ef1f4ad8 | 108 | |
mbed_official | 507:d4fc7603a669 | 109 | // Setup the ADC clock |
mbed_official | 507:d4fc7603a669 | 110 | MXC_SET_FIELD(&obj->adc->ctrl0, (MXC_F_ADC_CTRL0_ADC_MODE | MXC_F_ADC_CTRL0_AVG_MODE | |
mbed_official | 507:d4fc7603a669 | 111 | MXC_F_ADC_CTRL0_ADC_CLK_MODE | MXC_F_ADC_CTRL0_ADC_BI_POL), |
mbed_official | 507:d4fc7603a669 | 112 | ((MXC_E_ADC_MODE_SMPLCNT_FULL_RATE << MXC_F_ADC_CTRL0_ADC_MODE_POS) | |
mbed_official | 507:d4fc7603a669 | 113 | (MXC_E_ADC_AVG_MODE_FILTER_OUTPUT << MXC_F_ADC_CTRL0_AVG_MODE_POS) | |
mbed_official | 507:d4fc7603a669 | 114 | (0x2 << MXC_F_ADC_CTRL0_ADC_CLK_MODE_POS) | |
mbed_official | 507:d4fc7603a669 | 115 | MXC_F_ADC_CTRL0_ADC_CLK_EN)); |
mbed_official | 507:d4fc7603a669 | 116 | |
mbed_official | 507:d4fc7603a669 | 117 | // Setup the input multiplexor |
mbed_official | 507:d4fc7603a669 | 118 | MXC_SET_FIELD(&obj->adc->pga_ctrl, (MXC_F_ADC_PGA_CTRL_MUX_CH_SEL | |
mbed_official | 507:d4fc7603a669 | 119 | MXC_F_ADC_PGA_CTRL_MUX_DIFF | MXC_F_ADC_PGA_CTRL_PGA_GAIN), |
mbed_official | 507:d4fc7603a669 | 120 | ((mux_pos << MXC_F_ADC_PGA_CTRL_MUX_CH_SEL_POS) | |
mbed_official | 507:d4fc7603a669 | 121 | (diff << MXC_F_ADC_PGA_CTRL_MUX_DIFF_POS))); |
mbed_official | 507:d4fc7603a669 | 122 | |
mbed_official | 507:d4fc7603a669 | 123 | // Setup voltage reference |
mbed_official | 507:d4fc7603a669 | 124 | MXC_SET_FIELD(&MXC_AFE->ctrl1, MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL, |
mbed_official | 507:d4fc7603a669 | 125 | (MXC_F_AFE_CTRL1_REF_ADC_POWERUP | MXC_F_AFE_CTRL1_REF_BLK_POWERUP | |
mbed_official | 507:d4fc7603a669 | 126 | (MXC_E_AFE_REF_VOLT_SEL_1500 << MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL_POS))); |
mbed_official | 507:d4fc7603a669 | 127 | |
mbed_official | 507:d4fc7603a669 | 128 | // Clear the done bit |
mbed_official | 507:d4fc7603a669 | 129 | obj->adc->intr = MXC_F_ADC_INTR_DONE_IF; |
mbed_official | 507:d4fc7603a669 | 130 | |
mbed_official | 507:d4fc7603a669 | 131 | // Take one sample |
mbed_official | 507:d4fc7603a669 | 132 | obj->adc->tg_ctrl0 |= (1 << MXC_F_ADC_TG_CTRL0_ADC_SMPL_CNT_POS); |
mbed_official | 507:d4fc7603a669 | 133 | |
mbed_official | 507:d4fc7603a669 | 134 | // Set the start bit to take the sample |
mbed_official | 507:d4fc7603a669 | 135 | obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_START; |
mbed_official | 507:d4fc7603a669 | 136 | |
mbed_official | 507:d4fc7603a669 | 137 | // Wait for the conversion to complete |
mbed_official | 507:d4fc7603a669 | 138 | while(!(obj->adc->intr & MXC_F_ADC_INTR_DONE_IF)) {} |
mbed_official | 507:d4fc7603a669 | 139 | |
mbed_official | 507:d4fc7603a669 | 140 | // Get sample from the fifo |
mbed_official | 507:d4fc7603a669 | 141 | uint16_t sample = (uint16_t)(obj->adc->out & 0xFFFF); |
mbed_official | 507:d4fc7603a669 | 142 | |
mbed_official | 507:d4fc7603a669 | 143 | // Disable ADC |
mbed_official | 507:d4fc7603a669 | 144 | obj->adc->ctrl0 &= ~MXC_F_ADC_CTRL0_CPU_ADC_EN; |
mbed_official | 507:d4fc7603a669 | 145 | |
mbed_official | 507:d4fc7603a669 | 146 | return (sample - 1); |
mbed_official | 507:d4fc7603a669 | 147 | } |