Skip to content

Commit a0425d9

Browse files
authored
Merge pull request adafruit#966 from arturo182/nrf_analogio
nrf: Rewrite the AnalogIn common-hal using nrfx
2 parents 91427b0 + af5cb9c commit a0425d9

File tree

15 files changed

+48
-1420
lines changed

15 files changed

+48
-1420
lines changed

ports/nrf/Makefile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,8 @@ LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc
9191

9292
SRC_HAL = $(addprefix hal/,\
9393
hal_uart.c \
94-
hal_uarte.c \
9594
hal_time.c \
96-
hal_timer.c \
97-
hal_twi.c \
98-
hal_adc.c \
99-
hal_adce.c \
100-
hal_gpio.c \
10195
hal_rng.c \
102-
hal_pwm.c \
10396
)
10497

10598
SRC_NRFX = $(addprefix nrfx/,\

ports/nrf/common-hal/analogio/AnalogIn.c

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2018 Artur Pacholec
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -25,24 +26,19 @@
2526
*/
2627

2728
#include "common-hal/analogio/AnalogIn.h"
29+
#include "py/runtime.h"
2830

29-
#include <string.h>
31+
#include "nrfx_saadc.h"
32+
#include "nrf_gpio.h"
3033

31-
#include "py/gc.h"
32-
#include "py/nlr.h"
33-
#include "py/runtime.h"
34-
#include "py/binary.h"
35-
#include "py/mphal.h"
36-
#include "shared-bindings/analogio/AnalogIn.h"
37-
#include "nrf.h"
38-
39-
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) {
40-
if (!pin->adc_channel) {
41-
// No ADC function on that pin
34+
#define CHANNEL_NO 0
35+
36+
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
37+
if (pin->adc_channel == 0)
4238
mp_raise_ValueError("Pin does not have ADC capabilities");
43-
}
4439

4540
hal_gpio_cfg_pin(pin->port, pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
41+
4642
self->pin = pin;
4743
}
4844

@@ -51,66 +47,61 @@ bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
5147
}
5248

5349
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
54-
if (common_hal_analogio_analogin_deinited(self)) {
50+
if (common_hal_analogio_analogin_deinited(self))
5551
return;
56-
}
57-
reset_pin(self->pin->pin);
58-
self->pin = mp_const_none;
59-
}
6052

61-
void analogin_reset() {
53+
nrf_gpio_cfg_default(NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin));
54+
55+
self->pin = mp_const_none;
6256
}
6357

6458
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
65-
// Something else might have used the ADC in a different way,
66-
// so we completely re-initialize it.
67-
68-
int16_t value;
69-
70-
NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit;
71-
NRF_SAADC->ENABLE = 1;
72-
73-
for (int i = 0; i < 8; i++) {
74-
NRF_SAADC->CH[i].PSELN = SAADC_CH_PSELP_PSELP_NC;
75-
NRF_SAADC->CH[i].PSELP = SAADC_CH_PSELP_PSELP_NC;
76-
}
77-
78-
NRF_SAADC->CH[0].CONFIG = ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
79-
| ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
80-
| ((SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
81-
| ((SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
82-
| ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
83-
| ((SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk);
84-
NRF_SAADC->CH[0].PSELN = self->pin->adc_channel;
85-
NRF_SAADC->CH[0].PSELP = self->pin->adc_channel;
59+
// Something else might have used the ADC in a different way,
60+
// so we completely re-initialize it.
8661

62+
nrf_saadc_value_t value;
8763

88-
NRF_SAADC->RESULT.PTR = (uint32_t)&value;
89-
NRF_SAADC->RESULT.MAXCNT = 1;
64+
const nrf_saadc_channel_config_t config = {
65+
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
66+
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
67+
.gain = NRF_SAADC_GAIN1_6,
68+
.reference = NRF_SAADC_REFERENCE_INTERNAL,
69+
.acq_time = NRF_SAADC_ACQTIME_3US,
70+
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
71+
.burst = NRF_SAADC_BURST_DISABLED,
72+
.pin_p = self->pin->adc_channel,
73+
.pin_n = self->pin->adc_channel,
74+
};
9075

91-
NRF_SAADC->TASKS_START = 0x01UL;
76+
nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
77+
nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED);
78+
nrf_saadc_enable();
9279

93-
while (!NRF_SAADC->EVENTS_STARTED);
94-
NRF_SAADC->EVENTS_STARTED = 0x00UL;
80+
for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++)
81+
nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
9582

96-
NRF_SAADC->TASKS_SAMPLE = 0x01UL;
83+
nrf_saadc_channel_init(CHANNEL_NO, &config);
84+
nrf_saadc_buffer_init(&value, 1);
9785

98-
while (!NRF_SAADC->EVENTS_END);
99-
NRF_SAADC->EVENTS_END = 0x00UL;
86+
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
87+
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0);
88+
nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
10089

101-
NRF_SAADC->TASKS_STOP = 0x01UL;
90+
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
91+
while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0);
92+
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
10293

103-
while (!NRF_SAADC->EVENTS_STOPPED);
104-
NRF_SAADC->EVENTS_STOPPED = 0x00UL;
94+
nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
95+
while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0);
96+
nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED);
10597

106-
if (value < 0) {
107-
value = 0;
108-
}
98+
nrf_saadc_disable();
10999

110-
NRF_SAADC->ENABLE = 0;
100+
if (value < 0)
101+
value = 0;
111102

112-
// Map value to from 14 to 16 bits
113-
return (value << 2);
103+
// Map value to from 14 to 16 bits
104+
return (value << 2);
114105
}
115106

116107
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {

ports/nrf/common-hal/analogio/AnalogIn.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,4 @@ typedef struct {
3636
const mcu_pin_obj_t * pin;
3737
} analogio_analogin_obj_t;
3838

39-
void analogin_reset(void);
40-
4139
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H

ports/nrf/hal/hal_adc.c

Lines changed: 0 additions & 129 deletions
This file was deleted.

ports/nrf/hal/hal_adc.h

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)