3838
3939typedef struct _machine_adc_obj_t {
4040 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
4545} machine_adc_obj_t ;
4646
4747static const machine_adc_obj_t machine_adc_obj [] = {
48- #if NRF51
48+ #if NRF51
4949 {{& machine_adc_type }, .id = 0 , .ain = NRF_ADC_CONFIG_INPUT_0 },
5050 {{& machine_adc_type }, .id = 1 , .ain = NRF_ADC_CONFIG_INPUT_1 },
5151 {{& machine_adc_type }, .id = 2 , .ain = NRF_ADC_CONFIG_INPUT_2 },
@@ -54,7 +54,7 @@ static const machine_adc_obj_t machine_adc_obj[] = {
5454 {{& machine_adc_type }, .id = 5 , .ain = NRF_ADC_CONFIG_INPUT_5 },
5555 {{& machine_adc_type }, .id = 6 , .ain = NRF_ADC_CONFIG_INPUT_6 },
5656 {{& machine_adc_type }, .id = 7 , .ain = NRF_ADC_CONFIG_INPUT_7 },
57- #else
57+ #else
5858 {{& machine_adc_type }, .id = 0 },
5959 {{& machine_adc_type }, .id = 1 },
6060 {{& machine_adc_type }, .id = 2 },
@@ -63,14 +63,14 @@ static const machine_adc_obj_t machine_adc_obj[] = {
6363 {{& machine_adc_type }, .id = 5 },
6464 {{& machine_adc_type }, .id = 6 },
6565 {{& machine_adc_type }, .id = 7 },
66- #endif
66+ #endif
6767};
6868
6969void adc_init0 (void ) {
70- #if defined(NRF52_SERIES )
70+ #if defined(NRF52_SERIES )
7171 const uint8_t interrupt_priority = 6 ;
7272 nrfx_saadc_init (interrupt_priority );
73- #endif
73+ #endif
7474}
7575
7676static 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
124124 int adc_id = adc_find (args [ARG_id ].u_obj );
125125 const machine_adc_obj_t * self = & machine_adc_obj [adc_id ];
126126
127- #if defined(NRF52_SERIES )
127+ #if defined(NRF52_SERIES )
128128 const nrfx_saadc_channel_t config = { \
129129 .channel_config =
130130 {
131131 .resistor_p = NRF_SAADC_RESISTOR_DISABLED ,
132132 .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 ,
138138 },
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 ,
142142 };
143143 nrfx_saadc_channels_config (& config , 1 );
144- #endif
144+ #endif
145145
146146 return MP_OBJ_FROM_PTR (self );
147147}
148148
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 ) {
150150
151- #if NRF51
151+ #if NRF51
152152 nrf_adc_value_t value = 0 ;
153153
154154 nrfx_adc_channel_t channel_config = {
155155 .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.
160160 };
161161
162162 nrfx_adc_sample_convert (& channel_config , & value );
163- #else // NRF52
163+ #else // NRF52
164164 nrf_saadc_value_t value = 0 ;
165165
166166 nrfx_saadc_simple_mode_set ((1 << adc_obj -> id ), NRF_SAADC_RESOLUTION_8BIT , NRF_SAADC_INPUT_DISABLED , NULL );
167167 nrfx_saadc_buffer_set (& value , 1 );
168168 nrfx_saadc_mode_trigger ();
169- #endif
169+ #endif
170170 return value ;
171171}
172172
@@ -209,10 +209,9 @@ static MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_value_obj, machine_adc_value);
209209#define DIODE_VOLT_DROP_MILLIVOLT (270) // Voltage drop over diode.
210210
211211#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)
213213
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 ) {
216215 uint8_t battery_level ;
217216
218217 if (mvolts >= 3000 ) {
@@ -236,42 +235,42 @@ static uint8_t battery_level_in_percent(const uint16_t mvolts)
236235/// Get battery level in percentage.
237236mp_obj_t machine_adc_battery_level (void ) {
238237
239- #if NRF51
238+ #if NRF51
240239 nrf_adc_value_t value = 0 ;
241240
242241 nrfx_adc_channel_t channel_config = {
243242 .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.
248247 };
249248
250249 nrfx_adc_sample_convert (& channel_config , & value );
251- #else // NRF52
250+ #else // NRF52
252251 nrf_saadc_value_t value = 0 ;
253252
254253 const nrfx_saadc_channel_t config = { \
255254 .channel_config =
256255 {
257256 .resistor_p = NRF_SAADC_RESISTOR_DISABLED ,
258257 .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 ,
264263 },
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 ,
268267 };
269268 nrfx_saadc_channels_config (& config , 1 );
270269
271270 nrfx_saadc_simple_mode_set ((1 << 0 ), NRF_SAADC_RESOLUTION_8BIT , NRF_SAADC_INPUT_DISABLED , NULL );
272271 nrfx_saadc_buffer_set (& value , 1 );
273272 nrfx_saadc_mode_trigger ();
274- #endif
273+ #endif
275274
276275 uint16_t batt_lvl_in_milli_volts = BATTERY_MILLIVOLT (value ) + DIODE_VOLT_DROP_MILLIVOLT ;
277276 uint16_t batt_in_percent = battery_level_in_percent (batt_lvl_in_milli_volts );
0 commit comments