mbed library sources. Supersedes mbed-src. Add PORTG support for STM32L476JG (SensorTile kit)
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32600/analogout_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Maxim/TARGET_MAX32600/analogout_api.c@144:ef7eb2e8f9f7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
<> | 144:ef7eb2e8f9f7 | 5 | * copy of this software and associated documentation files (the "Software"), |
<> | 144:ef7eb2e8f9f7 | 6 | * to deal in the Software without restriction, including without limitation |
<> | 144:ef7eb2e8f9f7 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
<> | 144:ef7eb2e8f9f7 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
<> | 144:ef7eb2e8f9f7 | 9 | * Software is furnished to do so, subject to the following conditions: |
<> | 144:ef7eb2e8f9f7 | 10 | * |
<> | 144:ef7eb2e8f9f7 | 11 | * The above copyright notice and this permission notice shall be included |
<> | 144:ef7eb2e8f9f7 | 12 | * in all copies or substantial portions of the Software. |
<> | 144:ef7eb2e8f9f7 | 13 | * |
<> | 144:ef7eb2e8f9f7 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
<> | 144:ef7eb2e8f9f7 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
<> | 144:ef7eb2e8f9f7 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
<> | 144:ef7eb2e8f9f7 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
<> | 144:ef7eb2e8f9f7 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
<> | 144:ef7eb2e8f9f7 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
<> | 144:ef7eb2e8f9f7 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
<> | 144:ef7eb2e8f9f7 | 21 | * |
<> | 144:ef7eb2e8f9f7 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
<> | 144:ef7eb2e8f9f7 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
<> | 144:ef7eb2e8f9f7 | 24 | * Products, Inc. Branding Policy. |
<> | 144:ef7eb2e8f9f7 | 25 | * |
<> | 144:ef7eb2e8f9f7 | 26 | * The mere transfer of this software does not imply any licenses |
<> | 144:ef7eb2e8f9f7 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
<> | 144:ef7eb2e8f9f7 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
<> | 144:ef7eb2e8f9f7 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
<> | 144:ef7eb2e8f9f7 | 30 | * ownership rights. |
<> | 144:ef7eb2e8f9f7 | 31 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 32 | */ |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 35 | #include "analogout_api.h" |
<> | 144:ef7eb2e8f9f7 | 36 | #include "clkman_regs.h" |
<> | 144:ef7eb2e8f9f7 | 37 | #include "pwrman_regs.h" |
<> | 144:ef7eb2e8f9f7 | 38 | #include "afe_regs.h" |
<> | 144:ef7eb2e8f9f7 | 39 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 40 | |
<> | 144:ef7eb2e8f9f7 | 41 | //****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 42 | void analogout_init(dac_t *obj, PinName pin) |
<> | 144:ef7eb2e8f9f7 | 43 | { |
<> | 144:ef7eb2e8f9f7 | 44 | // Make sure pin is an analog pin we can use for ADC |
<> | 144:ef7eb2e8f9f7 | 45 | DACName dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); |
<> | 144:ef7eb2e8f9f7 | 46 | MBED_ASSERT((DACName)dac != (DACName)NC); |
<> | 144:ef7eb2e8f9f7 | 47 | |
<> | 144:ef7eb2e8f9f7 | 48 | // Set the object pointer |
<> | 144:ef7eb2e8f9f7 | 49 | obj->dac = ((mxc_dac_regs_t*)MXC_DAC_GET_DAC((pin & 0x3))); |
<> | 144:ef7eb2e8f9f7 | 50 | obj->dac_fifo = ((mxc_dac_fifo_regs_t*)MXC_DAC_GET_FIFO((pin & 0x3))); |
<> | 144:ef7eb2e8f9f7 | 51 | obj->index = (pin & 0x3); |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | // Set the ADC clock to the system clock frequency |
<> | 144:ef7eb2e8f9f7 | 54 | MXC_SET_FIELD(&MXC_CLKMAN->clk_ctrl, MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT, |
<> | 144:ef7eb2e8f9f7 | 55 | (MXC_F_CLKMAN_CLK_CTRL_ADC_GATE_N | (MXC_E_CLKMAN_ADC_SOURCE_SELECT_SYSTEM << |
<> | 144:ef7eb2e8f9f7 | 56 | MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT_POS))); |
<> | 144:ef7eb2e8f9f7 | 57 | |
<> | 144:ef7eb2e8f9f7 | 58 | |
<> | 144:ef7eb2e8f9f7 | 59 | // Setup the OPAMP in follower mode |
<> | 144:ef7eb2e8f9f7 | 60 | switch(obj->index) { |
<> | 144:ef7eb2e8f9f7 | 61 | case 0: |
<> | 144:ef7eb2e8f9f7 | 62 | // Enable DAC clock |
<> | 144:ef7eb2e8f9f7 | 63 | MXC_CLKMAN->clk_ctrl_14_dac0 = MXC_E_CLKMAN_CLK_SCALE_ENABLED; |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | // Enable OPAMP |
<> | 144:ef7eb2e8f9f7 | 66 | MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP0; |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | // Set the positive and negative inputs |
<> | 144:ef7eb2e8f9f7 | 69 | MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_A | |
<> | 144:ef7eb2e8f9f7 | 70 | MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP0 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP0), |
<> | 144:ef7eb2e8f9f7 | 71 | ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP0_POS) | |
<> | 144:ef7eb2e8f9f7 | 72 | (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP0_POS) | |
<> | 144:ef7eb2e8f9f7 | 73 | (0x0 << MXC_F_AFE_CTRL4_DAC_SEL_A_POS))); |
<> | 144:ef7eb2e8f9f7 | 74 | |
<> | 144:ef7eb2e8f9f7 | 75 | // Enable N and P channel inputs |
<> | 144:ef7eb2e8f9f7 | 76 | MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP0 | |
<> | 144:ef7eb2e8f9f7 | 77 | MXC_F_AFE_CTRL3_EN_NCH_OPAMP0); |
<> | 144:ef7eb2e8f9f7 | 78 | break; |
<> | 144:ef7eb2e8f9f7 | 79 | case 1: |
<> | 144:ef7eb2e8f9f7 | 80 | // Enable DAC clock |
<> | 144:ef7eb2e8f9f7 | 81 | MXC_CLKMAN->clk_ctrl_15_dac1 = MXC_E_CLKMAN_CLK_SCALE_ENABLED; |
<> | 144:ef7eb2e8f9f7 | 82 | |
<> | 144:ef7eb2e8f9f7 | 83 | // Enable OPAMP |
<> | 144:ef7eb2e8f9f7 | 84 | MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP1; |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | // Set the positive and negative inputs |
<> | 144:ef7eb2e8f9f7 | 87 | MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_B | |
<> | 144:ef7eb2e8f9f7 | 88 | MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP1 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP1), |
<> | 144:ef7eb2e8f9f7 | 89 | ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP1_POS) | |
<> | 144:ef7eb2e8f9f7 | 90 | (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP1_POS) | |
<> | 144:ef7eb2e8f9f7 | 91 | (0x1 << MXC_F_AFE_CTRL4_DAC_SEL_B_POS))); |
<> | 144:ef7eb2e8f9f7 | 92 | |
<> | 144:ef7eb2e8f9f7 | 93 | // Enable N and P channel inputs |
<> | 144:ef7eb2e8f9f7 | 94 | MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP1 | |
<> | 144:ef7eb2e8f9f7 | 95 | MXC_F_AFE_CTRL3_EN_NCH_OPAMP1); |
<> | 144:ef7eb2e8f9f7 | 96 | |
<> | 144:ef7eb2e8f9f7 | 97 | break; |
<> | 144:ef7eb2e8f9f7 | 98 | case 2: |
<> | 144:ef7eb2e8f9f7 | 99 | // Enable DAC clock |
<> | 144:ef7eb2e8f9f7 | 100 | MXC_CLKMAN->clk_ctrl_16_dac2 = MXC_E_CLKMAN_CLK_SCALE_ENABLED; |
<> | 144:ef7eb2e8f9f7 | 101 | |
<> | 144:ef7eb2e8f9f7 | 102 | // Enable OPAMP |
<> | 144:ef7eb2e8f9f7 | 103 | MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP2; |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | // Set the positive and negative inputs |
<> | 144:ef7eb2e8f9f7 | 106 | MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_C | |
<> | 144:ef7eb2e8f9f7 | 107 | MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP2 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP2), |
<> | 144:ef7eb2e8f9f7 | 108 | ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP2_POS) | |
<> | 144:ef7eb2e8f9f7 | 109 | (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP2_POS) | |
<> | 144:ef7eb2e8f9f7 | 110 | (0x2 << MXC_F_AFE_CTRL4_DAC_SEL_C_POS))); |
<> | 144:ef7eb2e8f9f7 | 111 | |
<> | 144:ef7eb2e8f9f7 | 112 | // Enable N and P channel inputs |
<> | 144:ef7eb2e8f9f7 | 113 | MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP2 | |
<> | 144:ef7eb2e8f9f7 | 114 | MXC_F_AFE_CTRL3_EN_NCH_OPAMP2); |
<> | 144:ef7eb2e8f9f7 | 115 | break; |
<> | 144:ef7eb2e8f9f7 | 116 | case 3: |
<> | 144:ef7eb2e8f9f7 | 117 | // Enable DAC clock |
<> | 144:ef7eb2e8f9f7 | 118 | MXC_CLKMAN->clk_ctrl_17_dac3 = MXC_E_CLKMAN_CLK_SCALE_ENABLED; |
<> | 144:ef7eb2e8f9f7 | 119 | |
<> | 144:ef7eb2e8f9f7 | 120 | // Enable OPAMP |
<> | 144:ef7eb2e8f9f7 | 121 | MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP3; |
<> | 144:ef7eb2e8f9f7 | 122 | |
<> | 144:ef7eb2e8f9f7 | 123 | // Set the positive and negative inputs |
<> | 144:ef7eb2e8f9f7 | 124 | MXC_SET_FIELD(&MXC_AFE->ctrl4, (MXC_F_AFE_CTRL4_DAC_SEL_D | |
<> | 144:ef7eb2e8f9f7 | 125 | MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP3 | MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP3), |
<> | 144:ef7eb2e8f9f7 | 126 | ((0x1 << MXC_F_AFE_CTRL4_P_IN_SEL_OPAMP3_POS) | |
<> | 144:ef7eb2e8f9f7 | 127 | (0x1 << MXC_F_AFE_CTRL4_N_IN_SEL_OPAMP3_POS) | |
<> | 144:ef7eb2e8f9f7 | 128 | (0x3 << MXC_F_AFE_CTRL4_DAC_SEL_D_POS))); |
<> | 144:ef7eb2e8f9f7 | 129 | |
<> | 144:ef7eb2e8f9f7 | 130 | // Enable N and P channel inputs |
<> | 144:ef7eb2e8f9f7 | 131 | MXC_AFE->ctrl3 |= (MXC_F_AFE_CTRL3_EN_PCH_OPAMP3 | |
<> | 144:ef7eb2e8f9f7 | 132 | MXC_F_AFE_CTRL3_EN_NCH_OPAMP3); |
<> | 144:ef7eb2e8f9f7 | 133 | break; |
<> | 144:ef7eb2e8f9f7 | 134 | } |
<> | 144:ef7eb2e8f9f7 | 135 | |
<> | 144:ef7eb2e8f9f7 | 136 | // Enable AFE power |
<> | 144:ef7eb2e8f9f7 | 137 | MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED; |
<> | 144:ef7eb2e8f9f7 | 138 | |
<> | 144:ef7eb2e8f9f7 | 139 | // Setup internal voltage references |
<> | 144:ef7eb2e8f9f7 | 140 | MXC_SET_FIELD(&MXC_AFE->ctrl1, (MXC_F_AFE_CTRL1_REF_DAC_VOLT_SEL | MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL), |
<> | 144:ef7eb2e8f9f7 | 141 | (MXC_F_AFE_CTRL1_REF_ADC_POWERUP | MXC_F_AFE_CTRL1_REF_BLK_POWERUP | |
<> | 144:ef7eb2e8f9f7 | 142 | (MXC_E_AFE_REF_VOLT_SEL_1500 << MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL_POS))); |
<> | 144:ef7eb2e8f9f7 | 143 | |
<> | 144:ef7eb2e8f9f7 | 144 | // Disable interpolation |
<> | 144:ef7eb2e8f9f7 | 145 | obj->dac->ctrl0 &= ~MXC_F_DAC_CTRL0_INTERP_MODE; |
<> | 144:ef7eb2e8f9f7 | 146 | } |
<> | 144:ef7eb2e8f9f7 | 147 | |
<> | 144:ef7eb2e8f9f7 | 148 | //****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 149 | void analogout_write(dac_t *obj, float value) |
<> | 144:ef7eb2e8f9f7 | 150 | { |
<> | 144:ef7eb2e8f9f7 | 151 | analogout_write_u16(obj, (uint16_t)((value/1.0) * 0xFFFF)); |
<> | 144:ef7eb2e8f9f7 | 152 | } |
<> | 144:ef7eb2e8f9f7 | 153 | |
<> | 144:ef7eb2e8f9f7 | 154 | //****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 155 | void analogout_write_u16(dac_t *obj, uint16_t value) |
<> | 144:ef7eb2e8f9f7 | 156 | { |
<> | 144:ef7eb2e8f9f7 | 157 | // Enable the OPAMP |
<> | 144:ef7eb2e8f9f7 | 158 | // Setup the OPAMP in follower mode |
<> | 144:ef7eb2e8f9f7 | 159 | switch(obj->index) { |
<> | 144:ef7eb2e8f9f7 | 160 | case 0: |
<> | 144:ef7eb2e8f9f7 | 161 | MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP0; |
<> | 144:ef7eb2e8f9f7 | 162 | break; |
<> | 144:ef7eb2e8f9f7 | 163 | case 1: |
<> | 144:ef7eb2e8f9f7 | 164 | MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP1; |
<> | 144:ef7eb2e8f9f7 | 165 | break; |
<> | 144:ef7eb2e8f9f7 | 166 | case 2: |
<> | 144:ef7eb2e8f9f7 | 167 | MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP2; |
<> | 144:ef7eb2e8f9f7 | 168 | break; |
<> | 144:ef7eb2e8f9f7 | 169 | case 3: |
<> | 144:ef7eb2e8f9f7 | 170 | MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP3; |
<> | 144:ef7eb2e8f9f7 | 171 | break; |
<> | 144:ef7eb2e8f9f7 | 172 | } |
<> | 144:ef7eb2e8f9f7 | 173 | |
<> | 144:ef7eb2e8f9f7 | 174 | // Output 1 sample with minimal delay |
<> | 144:ef7eb2e8f9f7 | 175 | obj->dac->rate |= 0x1; |
<> | 144:ef7eb2e8f9f7 | 176 | |
<> | 144:ef7eb2e8f9f7 | 177 | // Set the start mode to output once data is in the FIFO |
<> | 144:ef7eb2e8f9f7 | 178 | obj->dac->ctrl0 &= ~(MXC_F_DAC_CTRL0_START_MODE | MXC_F_DAC_CTRL0_OP_MODE); |
<> | 144:ef7eb2e8f9f7 | 179 | |
<> | 144:ef7eb2e8f9f7 | 180 | // Enable the DAC |
<> | 144:ef7eb2e8f9f7 | 181 | obj->dac->ctrl0 |= (MXC_F_DAC_CTRL0_POWER_MODE_2 | |
<> | 144:ef7eb2e8f9f7 | 182 | MXC_F_DAC_CTRL0_POWER_MODE_1_0 | MXC_F_DAC_CTRL0_POWER_ON | |
<> | 144:ef7eb2e8f9f7 | 183 | MXC_F_DAC_CTRL0_CLOCK_GATE_EN | MXC_F_DAC_CTRL0_CPU_START); |
<> | 144:ef7eb2e8f9f7 | 184 | |
<> | 144:ef7eb2e8f9f7 | 185 | if(obj->index < 2) { |
<> | 144:ef7eb2e8f9f7 | 186 | obj->out = (value); |
<> | 144:ef7eb2e8f9f7 | 187 | obj->dac_fifo->output_16 = (obj->out); |
<> | 144:ef7eb2e8f9f7 | 188 | |
<> | 144:ef7eb2e8f9f7 | 189 | } else { |
<> | 144:ef7eb2e8f9f7 | 190 | // Convert 16 bits to 8 bits |
<> | 144:ef7eb2e8f9f7 | 191 | obj->out = (value >> 8); |
<> | 144:ef7eb2e8f9f7 | 192 | obj->dac_fifo->output_8 = (obj->out); |
<> | 144:ef7eb2e8f9f7 | 193 | } |
<> | 144:ef7eb2e8f9f7 | 194 | } |
<> | 144:ef7eb2e8f9f7 | 195 | |
<> | 144:ef7eb2e8f9f7 | 196 | //****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 197 | float analogout_read(dac_t *obj) |
<> | 144:ef7eb2e8f9f7 | 198 | { |
<> | 144:ef7eb2e8f9f7 | 199 | return (((float)analogout_read_u16(obj) / (float)0xFFFF) * 1.5); |
<> | 144:ef7eb2e8f9f7 | 200 | } |
<> | 144:ef7eb2e8f9f7 | 201 | |
<> | 144:ef7eb2e8f9f7 | 202 | //****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 203 | uint16_t analogout_read_u16(dac_t *obj) |
<> | 144:ef7eb2e8f9f7 | 204 | { |
<> | 144:ef7eb2e8f9f7 | 205 | if(obj->index < 2) { |
<> | 144:ef7eb2e8f9f7 | 206 | // Convert 12 bits to 16 bits |
<> | 144:ef7eb2e8f9f7 | 207 | return (obj->out << 4); |
<> | 144:ef7eb2e8f9f7 | 208 | } else { |
<> | 144:ef7eb2e8f9f7 | 209 | // Convert 8 bits to 16 bits |
<> | 144:ef7eb2e8f9f7 | 210 | return (obj->out << 8); |
<> | 144:ef7eb2e8f9f7 | 211 | } |
<> | 144:ef7eb2e8f9f7 | 212 | } |