]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Maxim/TARGET_MAX32610/analogout_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Maxim / TARGET_MAX32610 / analogout_api.c
1 /*******************************************************************************
2  * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
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.
21  *
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.
25  *
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
30  * ownership rights.
31  *******************************************************************************
32  */
33
34 #include "mbed_assert.h"
35 #include "analogout_api.h"
36 #include "clkman_regs.h"
37 #include "pwrman_regs.h"
38 #include "afe_regs.h"
39 #include "PeripheralPins.h"
40
41 //******************************************************************************
42 void analogout_init(dac_t *obj, PinName pin)
43 {
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);
47
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);
52
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)));
57
58     
59     // Setup the OPAMP in follower mode
60     switch(obj->index) {
61         case 0:
62             // Enable DAC clock
63             MXC_CLKMAN->clk_ctrl_14_dac0 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
64
65             // Enable OPAMP
66             MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP0;
67
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)));
74
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);
78         break;
79         case 1:
80             // Enable DAC clock
81             MXC_CLKMAN->clk_ctrl_15_dac1 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
82
83             // Enable OPAMP
84             MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP1;
85
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)));
92
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);
96
97         break;
98         case 2:
99             // Enable DAC clock
100             MXC_CLKMAN->clk_ctrl_16_dac2 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
101
102             // Enable OPAMP
103             MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP2;
104
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)));
111
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);
115         break;
116         case 3:
117             // Enable DAC clock
118             MXC_CLKMAN->clk_ctrl_17_dac3 = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
119
120             // Enable OPAMP
121             MXC_AFE->ctrl5 &= ~MXC_F_AFE_CTRL5_OP_CMP3;
122
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)));
129
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);
133         break;
134     }
135
136     // Enable AFE power
137     MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED;
138
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)));
143
144     // Disable interpolation
145     obj->dac->ctrl0 &= MXC_F_DAC_CTRL0_INTERP_MODE;
146 }
147
148 //******************************************************************************
149 void analogout_write(dac_t *obj, float value)
150 {
151     analogout_write_u16(obj, (uint16_t)((value/1.0) * 0xFFFF));
152 }
153
154 //******************************************************************************
155 void analogout_write_u16(dac_t *obj, uint16_t value)
156 {
157     // Enable the OPAMP
158         // Setup the OPAMP in follower mode
159     switch(obj->index) {
160         case 0:
161             MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP0;
162         break;
163         case 1:
164             MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP1;
165         break;
166         case 2:
167             MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP2;
168         break;
169         case 3:
170             MXC_AFE->ctrl3 |= MXC_F_AFE_CTRL3_POWERUP_OPAMP3;
171         break;
172     }
173
174     // Output 1 sample with minimal delay
175     obj->dac->rate |= 0x1;
176
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);
179
180     // Enable the DAC
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);
184
185     if(obj->index < 2) {
186         obj->out = (value);
187         obj->dac_fifo->output_16 = (obj->out);
188
189     } else {
190         // Convert 16 bits to 8 bits
191         obj->out = (value >> 8);
192         obj->dac_fifo->output_8 = (obj->out);
193     }
194 }
195
196 //******************************************************************************
197 float analogout_read(dac_t *obj)
198 {
199     return (((float)analogout_read_u16(obj) / (float)0xFFFF) * 1.5);
200 }
201
202 //******************************************************************************
203 uint16_t analogout_read_u16(dac_t *obj)
204 {
205     if(obj->index < 2) {
206         // Convert 12 bits to 16 bits
207         return (obj->out << 4);
208     } else {
209         // Convert 8 bits to 16 bits
210         return (obj->out << 8);
211     }
212 }