1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 Glenn Ruben Bakke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This file is never compiled standalone, it's included directly from
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
#include "py/mphal.h"
#include "adc.h"
#if NRF51
#include "nrfx_adc.h"
#else
#include "nrfx_saadc.h"
#endif
typedef struct _machine_adc_obj_t {
mp_obj_base_t base;
uint8_t id;
#if NRF51
uint8_t ain;
#endif
} machine_adc_obj_t;
static const machine_adc_obj_t machine_adc_obj[] = {
#if NRF51
{{&machine_adc_type}, .id = 0, .ain = NRF_ADC_CONFIG_INPUT_0},
{{&machine_adc_type}, .id = 1, .ain = NRF_ADC_CONFIG_INPUT_1},
{{&machine_adc_type}, .id = 2, .ain = NRF_ADC_CONFIG_INPUT_2},
{{&machine_adc_type}, .id = 3, .ain = NRF_ADC_CONFIG_INPUT_3},
{{&machine_adc_type}, .id = 4, .ain = NRF_ADC_CONFIG_INPUT_4},
{{&machine_adc_type}, .id = 5, .ain = NRF_ADC_CONFIG_INPUT_5},
{{&machine_adc_type}, .id = 6, .ain = NRF_ADC_CONFIG_INPUT_6},
{{&machine_adc_type}, .id = 7, .ain = NRF_ADC_CONFIG_INPUT_7},
#else
{{&machine_adc_type}, .id = 0},
{{&machine_adc_type}, .id = 1},
{{&machine_adc_type}, .id = 2},
{{&machine_adc_type}, .id = 3},
{{&machine_adc_type}, .id = 4},
{{&machine_adc_type}, .id = 5},
{{&machine_adc_type}, .id = 6},
{{&machine_adc_type}, .id = 7},
#endif
};
void adc_init0(void) {
#if defined(NRF52_SERIES)
const uint8_t interrupt_priority = 6;
nrfx_saadc_init(interrupt_priority);
#endif
}
static int adc_find(mp_obj_t id) {
int adc_idx;
if (mp_obj_is_int(id)) {
// Given an integer id
adc_idx = mp_obj_get_int(id);
} else {
// Assume it's a pin-compatible object and convert it to an ADC channel number
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(id);
if (pin->adc_num & PIN_ADC1) {
adc_idx = pin->adc_channel;
} else {
mp_raise_ValueError(MP_ERROR_TEXT("invalid Pin for ADC"));
}
}
if (adc_idx >= 0 && adc_idx < MP_ARRAY_SIZE(machine_adc_obj)
&& machine_adc_obj[adc_idx].id != (uint8_t)-1) {
return adc_idx;
}
mp_raise_ValueError(MP_ERROR_TEXT("ADC doesn't exist"));
}
/******************************************************************************/
/* MicroPython bindings for machine API */
// These are ad-hoc legacy methods and need to be removed.
#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) }, /* instance method */ \
{ MP_ROM_QSTR(MP_QSTR_battery_level), MP_ROM_PTR(&mp_machine_adc_battery_level_obj) }, /* class method */ \
// Return a string describing the ADC object.
static void mp_machine_adc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
machine_adc_obj_t *self = o;
mp_printf(print, "ADC(%u)", self->id);
}
// for make_new
static mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_id };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1) } },
};
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int adc_id = adc_find(args[ARG_id].u_obj);
const machine_adc_obj_t *self = &machine_adc_obj[adc_id];
#if defined(NRF52_SERIES)
const nrfx_saadc_channel_t config = { \
.channel_config =
{
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_4,
.reference = NRF_SAADC_REFERENCE_VDD4,
.acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
},
.pin_p = (nrf_saadc_input_t)(1 + self->id), // pin_p=0 is AIN0, pin_p=8 is AIN7
.pin_n = NRF_SAADC_INPUT_DISABLED,
.channel_index = self->id,
};
nrfx_saadc_channels_config(&config, 1);
#endif
return MP_OBJ_FROM_PTR(self);
}
int16_t machine_adc_value_read(machine_adc_obj_t *adc_obj) {
#if NRF51
nrf_adc_value_t value = 0;
nrfx_adc_channel_t channel_config = {
.config.resolution = NRF_ADC_CONFIG_RES_8BIT,
.config.input = NRF_ADC_CONFIG_SCALING_INPUT_TWO_THIRDS,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = adc_obj->ain,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
};
nrfx_adc_sample_convert(&channel_config, &value);
#else // NRF52
nrf_saadc_value_t value = 0;
nrfx_saadc_simple_mode_set((1 << adc_obj->id), NRF_SAADC_RESOLUTION_8BIT, NRF_SAADC_INPUT_DISABLED, NULL);
nrfx_saadc_buffer_set(&value, 1);
nrfx_saadc_mode_trigger();
#endif
return value;
}
// read_u16()
static mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
int16_t raw = machine_adc_value_read(self);
#if defined(NRF52_SERIES)
// raw is signed but the channel is in single-ended mode and this method cannot return negative values
if (raw < 0) {
raw = 0;
}
#endif
// raw is an 8-bit value
return (raw << 8) | raw;
}
/// \method value()
/// Read adc level.
mp_obj_t machine_adc_value(mp_obj_t self_in) {
machine_adc_obj_t *self = self_in;
int16_t value = machine_adc_value_read(self);
return MP_OBJ_NEW_SMALL_INT(value);
}
static MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_value_obj, machine_adc_value);
#if NRF51
#define ADC_REF_VOLTAGE_IN_MILLIVOLT (1200) // Reference voltage in mV (1.2V).
#define ADC_PRE_SCALING_MULTIPLIER (3) // VDD 1/3 prescaling as input. Hence, multiplied by 3 to get the value of the battery voltage.
#else // NRF52
#define ADC_REF_VOLTAGE_IN_MILLIVOLT (600) // Reference voltage in mV (0.6V).
#define ADC_PRE_SCALING_MULTIPLIER (6) // VDD 1/6 prescaling as input. Hence, multiplied by 6 to get the value of the battery voltage.
#endif
#define DIODE_VOLT_DROP_MILLIVOLT (270) // Voltage drop over diode.
#define BATTERY_MILLIVOLT(VALUE) \
((((VALUE)*ADC_REF_VOLTAGE_IN_MILLIVOLT) / 255) * ADC_PRE_SCALING_MULTIPLIER)
static uint8_t battery_level_in_percent(const uint16_t mvolts) {
uint8_t battery_level;
if (mvolts >= 3000) {
battery_level = 100;
} else if (mvolts > 2900) {
battery_level = 100 - ((3000 - mvolts) * 58) / 100;
} else if (mvolts > 2740) {
battery_level = 42 - ((2900 - mvolts) * 24) / 160;
} else if (mvolts > 2440) {
battery_level = 18 - ((2740 - mvolts) * 12) / 300;
} else if (mvolts > 2100) {
battery_level = 6 - ((2440 - mvolts) * 6) / 340;
} else {
battery_level = 0;
}
return battery_level;
}
/// \method battery_level()
/// Get battery level in percentage.
mp_obj_t machine_adc_battery_level(void) {
#if NRF51
nrf_adc_value_t value = 0;
nrfx_adc_channel_t channel_config = {
.config.resolution = NRF_ADC_CONFIG_RES_8BIT,
.config.input = NRF_ADC_CONFIG_SCALING_SUPPLY_ONE_THIRD,
.config.reference = NRF_ADC_CONFIG_REF_VBG,
.config.input = NRF_ADC_CONFIG_INPUT_DISABLED,
.config.extref = ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos // Currently not defined in nrfx/hal.
};
nrfx_adc_sample_convert(&channel_config, &value);
#else // NRF52
nrf_saadc_value_t value = 0;
const nrfx_saadc_channel_t config = { \
.channel_config =
{
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_6,
.reference = NRF_SAADC_REFERENCE_INTERNAL,
.acq_time = NRF_SAADC_ACQTIME_3US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
},
.pin_p = NRF_SAADC_INPUT_VDD,
.pin_n = NRF_SAADC_INPUT_DISABLED,
.channel_index = 0,
};
nrfx_saadc_channels_config(&config, 1);
nrfx_saadc_simple_mode_set((1 << 0), NRF_SAADC_RESOLUTION_8BIT, NRF_SAADC_INPUT_DISABLED, NULL);
nrfx_saadc_buffer_set(&value, 1);
nrfx_saadc_mode_trigger();
#endif
uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT(value) + DIODE_VOLT_DROP_MILLIVOLT;
uint16_t batt_in_percent = battery_level_in_percent(batt_lvl_in_milli_volts);
return MP_OBJ_NEW_SMALL_INT(batt_in_percent);
}
static MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_adc_battery_level_obj, machine_adc_battery_level);
|