]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Maxim/TARGET_MAX32610/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Maxim / TARGET_MAX32610 / analogin_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 "analogin_api.h"
36 #include "clkman_regs.h"
37 #include "pwrman_regs.h"
38 #include "afe_regs.h"
39 #include "PeripheralPins.h"
40
41 #define PGA_TRK_CNT 0x1F
42 #define ADC_ACT_CNT 0x1
43 #define ADC_PGA_CNT 0x1
44 #define ADC_ACQ_CNT 0x1
45 #define ADC_SLP_CNT 0x1
46
47 //******************************************************************************
48 void analogin_init(analogin_t *obj, PinName pin)
49 {
50 // Make sure pin is an analog pin we can use for ADC
51 MBED_ASSERT((ADCName)pinmap_peripheral(pin, PinMap_ADC) != (ADCName)NC);
52
53 // Set the object pointer
54 obj->adc = MXC_ADC;
55 obj->adccfg = MXC_ADCCFG;
56 obj->adc_fifo = MXC_ADC_FIFO;
57 obj->adc_pin = pin;
58
59 // Set the ADC clock to the system clock frequency
60 MXC_SET_FIELD(&MXC_CLKMAN->clk_ctrl, MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT,
61 (MXC_F_CLKMAN_CLK_CTRL_ADC_GATE_N | (MXC_E_CLKMAN_ADC_SOURCE_SELECT_SYSTEM <<
62 MXC_F_CLKMAN_CLK_CTRL_ADC_SOURCE_SELECT_POS)));
63
64 // Enable AFE power
65 MXC_PWRMAN->pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED;
66
67 // Setup and hold window
68 MXC_SET_FIELD(&obj->adc->tg_ctrl0, MXC_F_ADC_TG_CTRL0_PGA_TRK_CNT, PGA_TRK_CNT);
69
70 // Setup sampling count and timing
71 MXC_SET_FIELD(&obj->adc->tg_ctrl1, (MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT |
72 MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT | MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT),
73 ((ADC_PGA_CNT << MXC_F_ADC_TG_CTRL1_PGA_ACQ_CNT_POS) |
74 (ADC_ACQ_CNT << MXC_F_ADC_TG_CTRL1_ADC_ACQ_CNT_POS) |
75 (ADC_SLP_CNT << MXC_F_ADC_TG_CTRL1_ADC_SLP_CNT_POS) |
76 (MXC_F_ADC_TG_CTRL1_ADC_BRST_CNT)));
77 }
78
79 //******************************************************************************
80 float analogin_read(analogin_t *obj)
81 {
82 // Convert integer to float
83 return (((float)analogin_read_u16(obj)/(float)0xFFFF));
84 }
85
86 //******************************************************************************
87 uint16_t analogin_read_u16(analogin_t *obj)
88 {
89 // Set the pin to take readings from
90 unsigned mux_pos;
91 unsigned diff = 0;
92 if(obj->adc_pin >> PORT_SHIFT == 0xB) {
93 mux_pos = (obj->adc_pin & 0xF) + 8;
94 } else {
95 mux_pos = (obj->adc_pin & 0xF);
96 }
97
98 if(obj->adc_pin >> PORT_SHIFT == 0xC) {
99 diff = 1;
100 mux_pos = (obj->adc_pin & 0xF) + 8;
101 }
102
103 // Reset the ADC
104 obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_RST;
105
106 // Enable the ADC
107 obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_EN;
108
109 // Setup the ADC clock
110 MXC_SET_FIELD(&obj->adc->ctrl0, (MXC_F_ADC_CTRL0_ADC_MODE | MXC_F_ADC_CTRL0_AVG_MODE |
111 MXC_F_ADC_CTRL0_ADC_CLK_MODE | MXC_F_ADC_CTRL0_ADC_BI_POL),
112 ((MXC_E_ADC_MODE_SMPLCNT_FULL_RATE << MXC_F_ADC_CTRL0_ADC_MODE_POS) |
113 (MXC_E_ADC_AVG_MODE_FILTER_OUTPUT << MXC_F_ADC_CTRL0_AVG_MODE_POS) |
114 (0x2 << MXC_F_ADC_CTRL0_ADC_CLK_MODE_POS) |
115 MXC_F_ADC_CTRL0_ADC_CLK_EN));
116
117 // Setup the input multiplexor
118 MXC_SET_FIELD(&obj->adc->pga_ctrl, (MXC_F_ADC_PGA_CTRL_MUX_CH_SEL |
119 MXC_F_ADC_PGA_CTRL_MUX_DIFF | MXC_F_ADC_PGA_CTRL_PGA_GAIN),
120 ((mux_pos << MXC_F_ADC_PGA_CTRL_MUX_CH_SEL_POS) |
121 (diff << MXC_F_ADC_PGA_CTRL_MUX_DIFF_POS)));
122
123 // Setup voltage reference
124 MXC_SET_FIELD(&MXC_AFE->ctrl1, MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL,
125 (MXC_F_AFE_CTRL1_REF_ADC_POWERUP | MXC_F_AFE_CTRL1_REF_BLK_POWERUP |
126 (MXC_E_AFE_REF_VOLT_SEL_1500 << MXC_F_AFE_CTRL1_REF_ADC_VOLT_SEL_POS)));
127
128 // Clear the done bit
129 obj->adc->intr = MXC_F_ADC_INTR_DONE_IF;
130
131 // Take one sample
132 obj->adc->tg_ctrl0 |= (1 << MXC_F_ADC_TG_CTRL0_ADC_SMPL_CNT_POS);
133
134 // Set the start bit to take the sample
135 obj->adc->ctrl0 |= MXC_F_ADC_CTRL0_CPU_ADC_START;
136
137 // Wait for the conversion to complete
138 while(!(obj->adc->intr & MXC_F_ADC_INTR_DONE_IF)) {}
139
140 // Get sample from the fifo
141 uint16_t sample = (uint16_t)(obj->adc->out & 0xFFFF);
142
143 // Disable ADC
144 obj->adc->ctrl0 &= ~MXC_F_ADC_CTRL0_CPU_ADC_EN;
145
146 return (sample - 1);
147 }
Imprint / Impressum