38
38
39
39
typedef struct _machine_adc_obj_t {
40
40
mp_obj_base_t base ;
41
- uint8_t id ;
42
- #if NRF51
43
- uint8_t ain ;
44
- #endif
41
+ uint8_t id ;
42
+ #if NRF51
43
+ uint8_t ain ;
44
+ #endif
45
45
} machine_adc_obj_t ;
46
46
47
47
static const machine_adc_obj_t machine_adc_obj [] = {
48
- #if NRF51
48
+ #if NRF51
49
49
{{& machine_adc_type }, .id = 0 , .ain = NRF_ADC_CONFIG_INPUT_0 },
50
50
{{& machine_adc_type }, .id = 1 , .ain = NRF_ADC_CONFIG_INPUT_1 },
51
51
{{& machine_adc_type }, .id = 2 , .ain = NRF_ADC_CONFIG_INPUT_2 },
@@ -54,7 +54,7 @@ static const machine_adc_obj_t machine_adc_obj[] = {
54
54
{{& machine_adc_type }, .id = 5 , .ain = NRF_ADC_CONFIG_INPUT_5 },
55
55
{{& machine_adc_type }, .id = 6 , .ain = NRF_ADC_CONFIG_INPUT_6 },
56
56
{{& machine_adc_type }, .id = 7 , .ain = NRF_ADC_CONFIG_INPUT_7 },
57
- #else
57
+ #else
58
58
{{& machine_adc_type }, .id = 0 },
59
59
{{& machine_adc_type }, .id = 1 },
60
60
{{& machine_adc_type }, .id = 2 },
@@ -63,14 +63,14 @@ static const machine_adc_obj_t machine_adc_obj[] = {
63
63
{{& machine_adc_type }, .id = 5 },
64
64
{{& machine_adc_type }, .id = 6 },
65
65
{{& machine_adc_type }, .id = 7 },
66
- #endif
66
+ #endif
67
67
};
68
68
69
69
void adc_init0 (void ) {
70
- #if defined(NRF52_SERIES )
70
+ #if defined(NRF52_SERIES )
71
71
const uint8_t interrupt_priority = 6 ;
72
72
nrfx_saadc_init (interrupt_priority );
73
- #endif
73
+ #endif
74
74
}
75
75
76
76
static int adc_find (mp_obj_t id ) {
@@ -124,49 +124,49 @@ static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args
124
124
int adc_id = adc_find (args [ARG_id ].u_obj );
125
125
const machine_adc_obj_t * self = & machine_adc_obj [adc_id ];
126
126
127
- #if defined(NRF52_SERIES )
127
+ #if defined(NRF52_SERIES )
128
128
const nrfx_saadc_channel_t config = { \
129
129
.channel_config =
130
130
{
131
131
.resistor_p = NRF_SAADC_RESISTOR_DISABLED ,
132
132
.resistor_n = NRF_SAADC_RESISTOR_DISABLED ,
133
- .gain = NRF_SAADC_GAIN1_4 ,
134
- .reference = NRF_SAADC_REFERENCE_VDD4 ,
135
- .acq_time = NRF_SAADC_ACQTIME_3US ,
136
- .mode = NRF_SAADC_MODE_SINGLE_ENDED ,
137
- .burst = NRF_SAADC_BURST_DISABLED ,
133
+ .gain = NRF_SAADC_GAIN1_4 ,
134
+ .reference = NRF_SAADC_REFERENCE_VDD4 ,
135
+ .acq_time = NRF_SAADC_ACQTIME_3US ,
136
+ .mode = NRF_SAADC_MODE_SINGLE_ENDED ,
137
+ .burst = NRF_SAADC_BURST_DISABLED ,
138
138
},
139
- .pin_p = (nrf_saadc_input_t )(1 + self -> id ), // pin_p=0 is AIN0, pin_p=8 is AIN7
140
- .pin_n = NRF_SAADC_INPUT_DISABLED ,
141
- .channel_index = self -> id ,
139
+ .pin_p = (nrf_saadc_input_t )(1 + self -> id ), // pin_p=0 is AIN0, pin_p=8 is AIN7
140
+ .pin_n = NRF_SAADC_INPUT_DISABLED ,
141
+ .channel_index = self -> id ,
142
142
};
143
143
nrfx_saadc_channels_config (& config , 1 );
144
- #endif
144
+ #endif
145
145
146
146
return MP_OBJ_FROM_PTR (self );
147
147
}
148
148
149
- int16_t machine_adc_value_read (machine_adc_obj_t * adc_obj ) {
149
+ int16_t machine_adc_value_read (machine_adc_obj_t * adc_obj ) {
150
150
151
- #if NRF51
151
+ #if NRF51
152
152
nrf_adc_value_t value = 0 ;
153
153
154
154
nrfx_adc_channel_t channel_config = {
155
155
.config .resolution = NRF_ADC_CONFIG_RES_8BIT ,
156
- .config .input = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS ,
157
- .config .reference = NRF_ADC_CONFIG_REF_VBG ,
158
- .config .input = adc_obj -> ain ,
159
- .config .extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
156
+ .config .input = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS ,
157
+ .config .reference = NRF_ADC_CONFIG_REF_VBG ,
158
+ .config .input = adc_obj -> ain ,
159
+ .config .extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
160
160
};
161
161
162
162
nrfx_adc_sample_convert (& channel_config , & value );
163
- #else // NRF52
163
+ #else // NRF52
164
164
nrf_saadc_value_t value = 0 ;
165
165
166
166
nrfx_saadc_simple_mode_set ((1 << adc_obj -> id ), NRF_SAADC_RESOLUTION_8BIT , NRF_SAADC_INPUT_DISABLED , NULL );
167
167
nrfx_saadc_buffer_set (& value , 1 );
168
168
nrfx_saadc_mode_trigger ();
169
- #endif
169
+ #endif
170
170
return value ;
171
171
}
172
172
@@ -209,10 +209,9 @@ static MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_value_obj, machine_adc_value);
209
209
#define DIODE_VOLT_DROP_MILLIVOLT (270) // Voltage drop over diode.
210
210
211
211
#define BATTERY_MILLIVOLT (VALUE ) \
212
- ((((VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLT) / 255) * ADC_PRE_SCALING_MULTIPLIER)
212
+ ((((VALUE)* ADC_REF_VOLTAGE_IN_MILLIVOLT) / 255) * ADC_PRE_SCALING_MULTIPLIER)
213
213
214
- static uint8_t battery_level_in_percent (const uint16_t mvolts )
215
- {
214
+ static uint8_t battery_level_in_percent (const uint16_t mvolts ) {
216
215
uint8_t battery_level ;
217
216
218
217
if (mvolts >= 3000 ) {
@@ -236,42 +235,42 @@ static uint8_t battery_level_in_percent(const uint16_t mvolts)
236
235
/// Get battery level in percentage.
237
236
mp_obj_t machine_adc_battery_level (void ) {
238
237
239
- #if NRF51
238
+ #if NRF51
240
239
nrf_adc_value_t value = 0 ;
241
240
242
241
nrfx_adc_channel_t channel_config = {
243
242
.config .resolution = NRF_ADC_CONFIG_RES_8BIT ,
244
- .config .input = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD ,
245
- .config .reference = NRF_ADC_CONFIG_REF_VBG ,
246
- .config .input = NRF_ADC_CONFIG_INPUT_DISABLED ,
247
- .config .extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
243
+ .config .input = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD ,
244
+ .config .reference = NRF_ADC_CONFIG_REF_VBG ,
245
+ .config .input = NRF_ADC_CONFIG_INPUT_DISABLED ,
246
+ .config .extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
248
247
};
249
248
250
249
nrfx_adc_sample_convert (& channel_config , & value );
251
- #else // NRF52
250
+ #else // NRF52
252
251
nrf_saadc_value_t value = 0 ;
253
252
254
253
const nrfx_saadc_channel_t config = { \
255
254
.channel_config =
256
255
{
257
256
.resistor_p = NRF_SAADC_RESISTOR_DISABLED ,
258
257
.resistor_n = NRF_SAADC_RESISTOR_DISABLED ,
259
- .gain = NRF_SAADC_GAIN1_6 ,
260
- .reference = NRF_SAADC_REFERENCE_INTERNAL ,
261
- .acq_time = NRF_SAADC_ACQTIME_3US ,
262
- .mode = NRF_SAADC_MODE_SINGLE_ENDED ,
263
- .burst = NRF_SAADC_BURST_DISABLED ,
258
+ .gain = NRF_SAADC_GAIN1_6 ,
259
+ .reference = NRF_SAADC_REFERENCE_INTERNAL ,
260
+ .acq_time = NRF_SAADC_ACQTIME_3US ,
261
+ .mode = NRF_SAADC_MODE_SINGLE_ENDED ,
262
+ .burst = NRF_SAADC_BURST_DISABLED ,
264
263
},
265
- .pin_p = NRF_SAADC_INPUT_VDD ,
266
- .pin_n = NRF_SAADC_INPUT_DISABLED ,
267
- .channel_index = 0 ,
264
+ .pin_p = NRF_SAADC_INPUT_VDD ,
265
+ .pin_n = NRF_SAADC_INPUT_DISABLED ,
266
+ .channel_index = 0 ,
268
267
};
269
268
nrfx_saadc_channels_config (& config , 1 );
270
269
271
270
nrfx_saadc_simple_mode_set ((1 << 0 ), NRF_SAADC_RESOLUTION_8BIT , NRF_SAADC_INPUT_DISABLED , NULL );
272
271
nrfx_saadc_buffer_set (& value , 1 );
273
272
nrfx_saadc_mode_trigger ();
274
- #endif
273
+ #endif
275
274
276
275
uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT (value ) + DIODE_VOLT_DROP_MILLIVOLT ;
277
276
uint16_t batt_in_percent = battery_level_in_percent (batt_lvl_in_milli_volts );
0 commit comments