35
35
#include "analogin_api.h"
36
36
#include "clkman_regs.h"
37
37
#include "pwrman_regs.h"
38
+ #include "trim_regs.h"
38
39
#include "PeripheralPins.h"
39
40
40
- #define UNTRIM
41
- #define MXC_BASE_FTR_TRIM_REG11 0x4000102C
42
- #define MXC_BASE_FTR_TRIM_REG12 0x40001030
43
-
44
41
#define PGA_TRK_CNT 0x1F
45
42
#define ADC_ACT_CNT 0x1
46
43
#define ADC_PGA_CNT 0x1
47
44
#define ADC_ACQ_CNT 0x1
48
45
#define ADC_SLP_CNT 0x1
49
46
50
- #define ADC_FULL_SCALE 0x3FF
47
+ #define ADC_FULL_SCALE 0x3FF
51
48
#define ADC_EXTERNAL_LAST_INPUT 3
52
49
53
50
// Only allow initialization once
@@ -57,44 +54,45 @@ static int initialized = 0;
57
54
void analogin_init (analogin_t * obj , PinName pin )
58
55
{
59
56
// Make sure pin is an analog pin we can use for ADC
60
- MBED_ASSERT ((ADCName )pinmap_peripheral (pin , PinMap_ADC ) != (ADCName )NC );
57
+ MBED_ASSERT ((ADCName )pinmap_peripheral (pin , PinMap_ADC ) != (ADCName )NC );
61
58
62
59
// Set the object pointer
63
60
obj -> adc = MXC_ADC ;
64
61
obj -> adc_pin = pin ;
65
62
66
- if (initialized == 0 ) {
67
- // Enable AFE power
68
- MXC_PWRMAN -> pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED ;
63
+ if (initialized == 0 ) {
64
+ // Enable AFE power
65
+ MXC_PWRMAN -> pwr_rst_ctrl |= MXC_F_PWRMAN_PWR_RST_CTRL_AFE_POWERED ;
69
66
70
- // Enable the clock
71
- MXC_CLKMAN -> clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_ADC_CLOCK_ENABLE ;
67
+ // Enable the clock
68
+ MXC_CLKMAN -> clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_ADC_CLOCK_ENABLE ;
72
69
73
- // Enable clock gate
74
- MXC_CLKMAN -> clk_gate_ctrl2 |= MXC_F_CLKMAN_CLK_GATE_CTRL2_ADC_CLK_GATER ;
70
+ // Enable clock gate
71
+ MXC_CLKMAN -> clk_gate_ctrl2 |= MXC_F_CLKMAN_CLK_GATE_CTRL2_ADC_CLK_GATER ;
75
72
76
- // Enable interface clock
77
- obj -> adc -> ctrl |= MXC_F_ADC_CTRL_ADC_CLK_EN ;
73
+ // Enable interface clock
74
+ obj -> adc -> ctrl |= MXC_F_ADC_CTRL_ADC_CLK_EN ;
78
75
79
- #ifdef UNTRIM
80
- * (volatile uint32_t * ) MXC_BASE_FTR_TRIM_REG11 = 0x02000200 ;
81
- * (volatile uint32_t * ) MXC_BASE_FTR_TRIM_REG12 = 0x02000200 ;
82
- #endif
76
+ if ((MXC_TRIM -> reg11_adc_trim0 == 0xFFFFFFFF ) && (MXC_TRIM -> reg12_adc_trim1 == 0xFFFFFFFF )) {
77
+ // Set default trim for untrimmed parts.
78
+ MXC_TRIM -> reg11_adc_trim0 = 0x02000200 ;
79
+ MXC_TRIM -> reg12_adc_trim1 = 0x02000200 ;
80
+ }
83
81
84
- // Clear ADC ready interrupt (wite 1 to clr)
85
- obj -> adc -> intr = (obj -> adc -> intr & 0xFFFF ) | MXC_F_ADC_INTR_ADC_REF_READY_IF ;
82
+ // Clear ADC ready interrupt (wite 1 to clr)
83
+ obj -> adc -> intr = (obj -> adc -> intr & 0xFFFF ) | MXC_F_ADC_INTR_ADC_REF_READY_IF ;
86
84
87
- // Using internal reference of 1.20V
85
+ // Using internal reference of 1.20V
88
86
89
- // Enable ADC power bypass the buffer
90
- obj -> adc -> ctrl |= (MXC_F_ADC_CTRL_ADC_PU | MXC_F_ADC_CTRL_ADC_REFBUF_PU |
91
- MXC_F_ADC_CTRL_ADC_CHGPUMP_PU | MXC_F_ADC_CTRL_BUF_BYPASS );
87
+ // Enable ADC power bypass the buffer
88
+ obj -> adc -> ctrl |= (MXC_F_ADC_CTRL_ADC_PU | MXC_F_ADC_CTRL_ADC_REFBUF_PU |
89
+ MXC_F_ADC_CTRL_ADC_CHGPUMP_PU | MXC_F_ADC_CTRL_BUF_BYPASS );
92
90
93
- // Wait for ADC ready
94
- while ( !(obj -> adc -> intr & MXC_F_ADC_INTR_ADC_REF_READY_IF ) );
91
+ // Wait for ADC ready
92
+ while (!(obj -> adc -> intr & MXC_F_ADC_INTR_ADC_REF_READY_IF ));
95
93
96
- initialized = 1 ;
97
- }
94
+ initialized = 1 ;
95
+ }
98
96
}
99
97
100
98
//******************************************************************************
@@ -111,36 +109,37 @@ uint16_t analogin_read_u16(analogin_t *obj)
111
109
uint32_t adc_input = PINNAME_TO_PIN (obj -> adc_pin );
112
110
113
111
// Select the channel
114
- obj -> adc -> ctrl &= ~MXC_F_ADC_CTRL_ADC_CHSEL ;
115
- obj -> adc -> ctrl |= (adc_input << MXC_F_ADC_CTRL_ADC_CHSEL_POS ) & MXC_F_ADC_CTRL_ADC_CHSEL ;
112
+ obj -> adc -> ctrl &= ~MXC_F_ADC_CTRL_ADC_CHSEL ;
113
+ obj -> adc -> ctrl |= (adc_input << MXC_F_ADC_CTRL_ADC_CHSEL_POS ) & MXC_F_ADC_CTRL_ADC_CHSEL ;
116
114
117
- // We want unity gain, to get full 0-Vref range
118
- // So, both ref and adc input scale should be enabled
119
- obj -> adc -> ctrl |= MXC_F_ADC_CTRL_ADC_SCALE | MXC_F_ADC_CTRL_ADC_REFSCL ;
115
+ // We want unity gain, to get full 0-Vref range
116
+ // So, both ref and adc input scale should be enabled
117
+ obj -> adc -> ctrl |= MXC_F_ADC_CTRL_ADC_SCALE | MXC_F_ADC_CTRL_ADC_REFSCL ;
120
118
121
- // Not using internal buffer, disable anyway
122
- obj -> adc -> ctrl &= ~MXC_F_ADC_CTRL_BUF_PU ;
123
- obj -> adc -> ctrl |= MXC_F_ADC_CTRL_BUF_BYPASS ;
119
+ // Not using internal buffer, disable anyway
120
+ obj -> adc -> ctrl &= ~MXC_F_ADC_CTRL_BUF_PU ;
121
+ obj -> adc -> ctrl |= MXC_F_ADC_CTRL_BUF_BYPASS ;
124
122
125
- // Normal LSB justified data alignment
123
+ // Normal LSB justified data alignment
126
124
127
- // Not using limits
125
+ // Not using limits
128
126
129
127
// Clear ADC done flag (wite 1 to clr)
130
128
obj -> adc -> intr = (obj -> adc -> intr & 0xFFFF ) | MXC_F_ADC_INTR_ADC_DONE_IF ;
131
129
132
- // Start the conversion
133
- obj -> adc -> ctrl |= MXC_F_ADC_CTRL_CPU_ADC_START ;
130
+ // Start the conversion
131
+ obj -> adc -> ctrl |= MXC_F_ADC_CTRL_CPU_ADC_START ;
134
132
135
- // Wait for ADC done
136
- while ( !(obj -> adc -> intr & MXC_F_ADC_INTR_ADC_DONE_IF ) );
133
+ // Wait for ADC done
134
+ while (!(obj -> adc -> intr & MXC_F_ADC_INTR_ADC_DONE_IF ));
137
135
138
136
// Get sample from the fifo
139
- uint16_t sample = ( uint16_t )( obj -> adc -> data & 0xFFFF ) ;
137
+ uint16_t sample = obj -> adc -> data ;
140
138
141
- // Check for overflow, hardware will report overflow as 0
142
- if ( (obj -> adc -> status & MXC_F_ADC_STATUS_ADC_OVERFLOW ) == MXC_F_ADC_STATUS_ADC_OVERFLOW )
143
- sample = (uint16_t )ADC_FULL_SCALE ;
139
+ // Check for overflow, hardware will report overflow as 0
140
+ if (obj -> adc -> status & MXC_F_ADC_STATUS_ADC_OVERFLOW ) {
141
+ sample = (uint16_t )ADC_FULL_SCALE ;
142
+ }
144
143
145
144
return sample ;
146
145
}
0 commit comments