|
| 1 | +From 642eadbf613b36db5f5d0e0a0c1406d3cd551397 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Paolo Calao <paolo.calao@gmail.com> |
| 3 | +Date: Wed, 13 May 2020 11:59:46 +0200 |
| 4 | +Subject: [PATCH 05/31] Add AnalogIn configure function |
| 5 | + |
| 6 | +This adds a weak method to configure AnalogIn objects. |
| 7 | +Also, a strong implementation of such method for NRF5284 is provided. |
| 8 | +--- |
| 9 | + drivers/AnalogIn.h | 7 ++++ |
| 10 | + drivers/source/AnalogIn.cpp | 7 ++++ |
| 11 | + hal/analogin_api.h | 13 +++++++ |
| 12 | + .../TARGET_NRF5x/TARGET_NRF52/analogin_api.c | 34 +++++++++++++++++++ |
| 13 | + .../TARGET_NRF5x/TARGET_NRF52/objects.h | 14 ++++++++ |
| 14 | + 5 files changed, 75 insertions(+) |
| 15 | + |
| 16 | +diff --git a/drivers/AnalogIn.h b/drivers/AnalogIn.h |
| 17 | +index 38c1fc130d..9cfd6600ca 100644 |
| 18 | +--- a/drivers/AnalogIn.h |
| 19 | ++++ b/drivers/AnalogIn.h |
| 20 | +@@ -88,6 +88,13 @@ public: |
| 21 | + */ |
| 22 | + AnalogIn(PinName pin, float vref = MBED_CONF_TARGET_DEFAULT_ADC_VREF); |
| 23 | + |
| 24 | ++ |
| 25 | ++ /** Reconfigure the adc object using the given configuration |
| 26 | ++ * |
| 27 | ++ * @param config reference to structure which holds AnalogIn configuration |
| 28 | ++ */ |
| 29 | ++ void configure(const analogin_config_t &config); |
| 30 | ++ |
| 31 | + /** Read the input voltage, represented as a float in the range [0.0, 1.0] |
| 32 | + * |
| 33 | + * @returns A floating-point value representing the current input voltage, measured as a percentage |
| 34 | +diff --git a/drivers/source/AnalogIn.cpp b/drivers/source/AnalogIn.cpp |
| 35 | +index 596d034f19..f9e271d5d9 100644 |
| 36 | +--- a/drivers/source/AnalogIn.cpp |
| 37 | ++++ b/drivers/source/AnalogIn.cpp |
| 38 | +@@ -38,6 +38,13 @@ AnalogIn::AnalogIn(const PinMap &pinmap, float vref) : _vref(vref) |
| 39 | + unlock(); |
| 40 | + } |
| 41 | + |
| 42 | ++void AnalogIn::configure(const analogin_config_t &config) |
| 43 | ++{ |
| 44 | ++ lock(); |
| 45 | ++ analogin_configure(&_adc, &config); |
| 46 | ++ unlock(); |
| 47 | ++} |
| 48 | ++ |
| 49 | + float AnalogIn::read() |
| 50 | + { |
| 51 | + lock(); |
| 52 | +diff --git a/hal/analogin_api.h b/hal/analogin_api.h |
| 53 | +index d172607c69..07208443f5 100644 |
| 54 | +--- a/hal/analogin_api.h |
| 55 | ++++ b/hal/analogin_api.h |
| 56 | +@@ -33,6 +33,10 @@ extern "C" { |
| 57 | + */ |
| 58 | + typedef struct analogin_s analogin_t; |
| 59 | + |
| 60 | ++/** Analogin configuration hal structure. analogin_config_s is declared in the target's hal |
| 61 | ++ */ |
| 62 | ++typedef struct analogin_config_s analogin_config_t; |
| 63 | ++ |
| 64 | + /** |
| 65 | + * \defgroup hal_analogin Analogin hal functions |
| 66 | + * |
| 67 | +@@ -77,6 +81,15 @@ void analogin_init_direct(analogin_t *obj, const PinMap *pinmap); |
| 68 | + */ |
| 69 | + void analogin_init(analogin_t *obj, PinName pin); |
| 70 | + |
| 71 | ++/** Initialize the analogin peripheral |
| 72 | ++ * |
| 73 | ++ * Configures the pin used by analogin. |
| 74 | ++ * @param obj The analogin object to initialize |
| 75 | ++ * @param pin The analogin pin name |
| 76 | ++ * @param pinmap pointer to structure which holds analogin configuration |
| 77 | ++ */ |
| 78 | ++void __attribute__((weak)) analogin_configure(analogin_t *obj, const analogin_config_t *config); |
| 79 | ++ |
| 80 | + /** Release the analogin peripheral |
| 81 | + * |
| 82 | + * Releases the pin used by analogin. |
| 83 | +diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/analogin_api.c b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/analogin_api.c |
| 84 | +index e66be66f50..863c7b090d 100644 |
| 85 | +--- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/analogin_api.c |
| 86 | ++++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/analogin_api.c |
| 87 | +@@ -122,6 +122,40 @@ void analogin_init(analogin_t *obj, PinName pin) |
| 88 | + ANALOGIN_INIT_DIRECT(obj, &static_pinmap); |
| 89 | + } |
| 90 | + |
| 91 | ++/** Reconfigure the analogin peripheral |
| 92 | ++ * |
| 93 | ++ * Configures the pin used by analogin. |
| 94 | ++ * @param obj The analogin object to initialize |
| 95 | ++ * @param config pointer to structure which holds analogin configuration |
| 96 | ++ */ |
| 97 | ++void analogin_configure(analogin_t *obj, const analogin_config_t *config) |
| 98 | ++{ |
| 99 | ++ MBED_ASSERT(obj); |
| 100 | ++ |
| 101 | ++ /* Get associated channel from the adc object. */ |
| 102 | ++ uint32_t channel = obj->channel; |
| 103 | ++ MBED_ASSERT(channel != (uint32_t) NC); |
| 104 | ++ |
| 105 | ++ /* Account for an off-by-one in Channel definition and Input definition. */ |
| 106 | ++ nrf_saadc_input_t input = channel + 1; |
| 107 | ++ |
| 108 | ++ /* Configure channel and pin */ |
| 109 | ++ nrf_saadc_channel_config_t channel_config = { |
| 110 | ++ .resistor_p = config->resistor_p, |
| 111 | ++ .resistor_n = config->resistor_n, |
| 112 | ++ .gain = config->gain, |
| 113 | ++ .reference = config->reference, |
| 114 | ++ .acq_time = config->acq_time, |
| 115 | ++ .mode = config->mode, |
| 116 | ++ .burst = config->burst, |
| 117 | ++ .pin_p = input, |
| 118 | ++ .pin_n = config->pin_n |
| 119 | ++ }; |
| 120 | ++ |
| 121 | ++ ret_code_t result = nrfx_saadc_channel_init(channel, &channel_config); |
| 122 | ++ MBED_ASSERT(result == NRFX_SUCCESS); |
| 123 | ++} |
| 124 | ++ |
| 125 | + /** Read the input voltage, represented as a float in the range [0.0, 1.0] |
| 126 | + * |
| 127 | + * @param obj The analogin object |
| 128 | +diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/objects.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/objects.h |
| 129 | +index 916e75985d..6200030c27 100644 |
| 130 | +--- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/objects.h |
| 131 | ++++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/objects.h |
| 132 | +@@ -46,6 +46,8 @@ |
| 133 | + #include "nrfx_spi.h" |
| 134 | + #include "nrf_twi.h" |
| 135 | + |
| 136 | ++#include "nrf_saadc.h" |
| 137 | ++ |
| 138 | + #include "nrf_pwm.h" |
| 139 | + |
| 140 | + #ifdef __cplusplus |
| 141 | +@@ -140,6 +142,18 @@ struct analogin_s { |
| 142 | + uint8_t channel; |
| 143 | + }; |
| 144 | + |
| 145 | ++struct analogin_config_s { |
| 146 | ++ nrf_saadc_resistor_t resistor_p; |
| 147 | ++ nrf_saadc_resistor_t resistor_n; |
| 148 | ++ nrf_saadc_gain_t gain; |
| 149 | ++ nrf_saadc_reference_t reference; |
| 150 | ++ nrf_saadc_acqtime_t acq_time; |
| 151 | ++ nrf_saadc_mode_t mode; |
| 152 | ++ nrf_saadc_burst_t burst; |
| 153 | ++ nrf_saadc_input_t pin_p; |
| 154 | ++ nrf_saadc_input_t pin_n; |
| 155 | ++}; |
| 156 | ++ |
| 157 | + struct gpio_irq_s { |
| 158 | + uint32_t ch; |
| 159 | + }; |
| 160 | +-- |
| 161 | +2.27.0 |
| 162 | + |
0 commit comments