mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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