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