summaryrefslogtreecommitdiff
path: root/ports/nrf/common-hal
diff options
context:
space:
mode:
authorarturo182 <arturo182@tlen.pl>2018-06-27 19:55:26 +0200
committerarturo182 <arturo182@tlen.pl>2018-06-27 20:41:30 +0200
commit2a12fcd18b333283f6a2d5088d5aaa1393370cff (patch)
tree5d71ba1a56606157e7d25f97a9096fc2096231b9 /ports/nrf/common-hal
parent91427b0b2366a792e165b48e81453d0cf98e1fb9 (diff)
nrf: Rewrite the AnalogIn common-hal using nrfx
Diffstat (limited to 'ports/nrf/common-hal')
-rw-r--r--ports/nrf/common-hal/analogio/AnalogIn.c105
-rw-r--r--ports/nrf/common-hal/analogio/AnalogIn.h2
2 files changed, 48 insertions, 59 deletions
diff --git a/ports/nrf/common-hal/analogio/AnalogIn.c b/ports/nrf/common-hal/analogio/AnalogIn.c
index 9923e62f5..043f66852 100644
--- a/ports/nrf/common-hal/analogio/AnalogIn.c
+++ b/ports/nrf/common-hal/analogio/AnalogIn.c
@@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
+ * Copyright (c) 2018 Artur Pacholec
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -25,24 +26,19 @@
*/
#include "common-hal/analogio/AnalogIn.h"
+#include "py/runtime.h"
-#include <string.h>
+#include "nrfx_saadc.h"
+#include "nrf_gpio.h"
-#include "py/gc.h"
-#include "py/nlr.h"
-#include "py/runtime.h"
-#include "py/binary.h"
-#include "py/mphal.h"
-#include "shared-bindings/analogio/AnalogIn.h"
-#include "nrf.h"
-
-void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) {
- if (!pin->adc_channel) {
- // No ADC function on that pin
+#define CHANNEL_NO 0
+
+void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) {
+ if (pin->adc_channel == 0)
mp_raise_ValueError("Pin does not have ADC capabilities");
- }
hal_gpio_cfg_pin(pin->port, pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED);
+
self->pin = pin;
}
@@ -51,66 +47,61 @@ bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
}
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
- if (common_hal_analogio_analogin_deinited(self)) {
+ if (common_hal_analogio_analogin_deinited(self))
return;
- }
- reset_pin(self->pin->pin);
- self->pin = mp_const_none;
-}
-void analogin_reset() {
+ nrf_gpio_cfg_default(NRF_GPIO_PIN_MAP(self->pin->port, self->pin->pin));
+
+ self->pin = mp_const_none;
}
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
- // Something else might have used the ADC in a different way,
- // so we completely re-initialize it.
-
- int16_t value;
-
- NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_14bit;
- NRF_SAADC->ENABLE = 1;
-
- for (int i = 0; i < 8; i++) {
- NRF_SAADC->CH[i].PSELN = SAADC_CH_PSELP_PSELP_NC;
- NRF_SAADC->CH[i].PSELP = SAADC_CH_PSELP_PSELP_NC;
- }
-
- NRF_SAADC->CH[0].CONFIG = ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos) & SAADC_CH_CONFIG_RESP_Msk)
- | ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
- | ((SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
- | ((SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
- | ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
- | ((SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk);
- NRF_SAADC->CH[0].PSELN = self->pin->adc_channel;
- NRF_SAADC->CH[0].PSELP = self->pin->adc_channel;
+ // Something else might have used the ADC in a different way,
+ // so we completely re-initialize it.
+ nrf_saadc_value_t value;
- NRF_SAADC->RESULT.PTR = (uint32_t)&value;
- NRF_SAADC->RESULT.MAXCNT = 1;
+ const nrf_saadc_channel_config_t 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 = self->pin->adc_channel,
+ .pin_n = self->pin->adc_channel,
+ };
- NRF_SAADC->TASKS_START = 0x01UL;
+ nrf_saadc_resolution_set(NRF_SAADC_RESOLUTION_14BIT);
+ nrf_saadc_oversample_set(NRF_SAADC_OVERSAMPLE_DISABLED);
+ nrf_saadc_enable();
- while (!NRF_SAADC->EVENTS_STARTED);
- NRF_SAADC->EVENTS_STARTED = 0x00UL;
+ for (uint32_t i = 0; i < NRF_SAADC_CHANNEL_COUNT; i++)
+ nrf_saadc_channel_input_set(i, NRF_SAADC_INPUT_DISABLED, NRF_SAADC_INPUT_DISABLED);
- NRF_SAADC->TASKS_SAMPLE = 0x01UL;
+ nrf_saadc_channel_init(CHANNEL_NO, &config);
+ nrf_saadc_buffer_init(&value, 1);
- while (!NRF_SAADC->EVENTS_END);
- NRF_SAADC->EVENTS_END = 0x00UL;
+ nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
+ while (nrf_saadc_event_check(NRF_SAADC_EVENT_STARTED) == 0);
+ nrf_saadc_event_clear(NRF_SAADC_EVENT_STARTED);
- NRF_SAADC->TASKS_STOP = 0x01UL;
+ nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
+ while (nrf_saadc_event_check(NRF_SAADC_EVENT_END) == 0);
+ nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
- while (!NRF_SAADC->EVENTS_STOPPED);
- NRF_SAADC->EVENTS_STOPPED = 0x00UL;
+ nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
+ while (nrf_saadc_event_check(NRF_SAADC_EVENT_STOPPED) == 0);
+ nrf_saadc_event_clear(NRF_SAADC_EVENT_STOPPED);
- if (value < 0) {
- value = 0;
- }
+ nrf_saadc_disable();
- NRF_SAADC->ENABLE = 0;
+ if (value < 0)
+ value = 0;
- // Map value to from 14 to 16 bits
- return (value << 2);
+ // Map value to from 14 to 16 bits
+ return (value << 2);
}
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
diff --git a/ports/nrf/common-hal/analogio/AnalogIn.h b/ports/nrf/common-hal/analogio/AnalogIn.h
index 95f599e71..e0e95bad4 100644
--- a/ports/nrf/common-hal/analogio/AnalogIn.h
+++ b/ports/nrf/common-hal/analogio/AnalogIn.h
@@ -36,6 +36,4 @@ typedef struct {
const mcu_pin_obj_t * pin;
} analogio_analogin_obj_t;
-void analogin_reset(void);
-
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ANALOGIO_ANALOGIN_H