nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
impl.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2015-2019 Peter A. Bigot */
3 
8 #ifndef NRFCXX_NRF51_IMPL_HPP
9 #define NRFCXX_NRF51_IMPL_HPP
10 #pragma once
11 
12 #include <nrf51_bitfields.h>
13 
14 namespace nrfcxx {
15 namespace nrf5 {
16 namespace series {
17 
19 struct ADC_Peripheral : public ADC_Base
20 {
21  using mutex_type = mutex_irq<static_cast<IRQn_Type>(nrf5::ADC.IRQn)>;
22 
23  static constexpr IRQn_Type IRQn = ADC_IRQn;
24 
26  static constexpr unsigned int VBG_mV = 1200;
27 
35  static constexpr uint16_t TOLERANCE_adc16 = (1U << 7);
36 
47  static constexpr bool near_zero (uint16_t v_adc16)
48  {
49  return ((TOLERANCE_adc16 > v_adc16)
50  || (v_adc16 >= ((1U << 16) - TOLERANCE_adc16)));
51  }
52 
86  static constexpr
87  unsigned int
88  make_config (int ain,
89  unsigned int res,
90  unsigned int prescale,
91  unsigned int refsel,
92  unsigned int extrefsel = ADC_CONFIG_EXTREFSEL_None)
93  {
94  return 0
95  | (ADC_CONFIG_RES_Msk & (res << ADC_CONFIG_RES_Pos))
96  | (ADC_CONFIG_INPSEL_Msk & (prescale << ADC_CONFIG_INPSEL_Pos))
97  | (ADC_CONFIG_REFSEL_Msk & (refsel << ADC_CONFIG_REFSEL_Pos))
98  | ((((0 <= ain) && (7 >= ain))
99  ? (ADC_CONFIG_PSEL_AnalogInput0 << ain)
100  : ADC_CONFIG_PSEL_Disabled) << ADC_CONFIG_PSEL_Pos)
101  | (ADC_CONFIG_EXTREFSEL_Msk & (extrefsel << ADC_CONFIG_EXTREFSEL_Pos));
102  }
103 
104  static constexpr uint32_t inten = 0
105  | (ADC_INTENSET_END_Set << ADC_INTENSET_END_Pos);
106 
108  static bool busy ()
109  {
110  return nrf5::ADC->BUSY;
111  }
112 
118  static void trigger ()
119  {
120  nrf5::ADC->TASKS_START = 1;
121  }
122 
123  static void enable_bi ()
124  {
125  nrf5::ADC->EVENTS_END = 0;
126  nrf5::ADC->INTENSET = inten;
127  nvic_ClearPendingIRQ(IRQn);
128  nvic_EnableIRQ(IRQn);
129  nrf5::ADC->ENABLE = 1;
130  }
131 
132  static void disable_bi ()
133  {
134  nrf5::ADC->ENABLE = 0;
135  nvic_DisableIRQ(IRQn);
136  nrf5::ADC->INTENCLR = inten;
137  }
138 
139  static int calibrate_bi ()
140  {
141  return 0;
142  }
143 
144  static int start_bi ()
145  {
146  enable_bi();
147  result_idx_ = 0;
148  nrf5::ADC->TASKS_START = 1;
149  return 0;
150  }
151 
152  static void stopped_bi ()
153  {
154  disable_bi();
155  }
156 
157  /* nRF51 doesn't support DMA so the FLIH needs to know which sample
158  * we're on so the converted value can be stored into the result
159  * block, and the client can be asked to provide the configuration
160  * for the next sample. */
161  static uint16_t result_idx_;
162 };
163 
164 using ADC_Variant = ADC_Peripheral;
165 
166 } // ns series
167 } // ns nrf5
168 
170 static constexpr auto IRQ_PRIORITY_SD_HIGH = 0;
171 
173 static constexpr auto IRQ_PRIORITY_APP_HIGH = 1;
174 
176 static constexpr auto IRQ_PRIORITY_SD_LOW = 2;
177 
179 static constexpr auto IRQ_PRIORITY_APP_LOW = 3;
180 
181 } // ns nrfcxx
182 
183 #endif /* NRFCXX_NRF51_IMPL_HPP */
nrfcxx::nrf5::series::ADC_Peripheral::near_zero
static constexpr bool near_zero(uint16_t v_adc16)
Test whether a value appears to be indistinguishable from zero.
Definition: impl.hpp:47
nrfcxx::nrf5::series::ADC_Peripheral
Constants and function specific to the nRF51 ADC peripheral.
Definition: impl.hpp:19
nrfcxx::IRQ_PRIORITY_APP_HIGH
static constexpr auto IRQ_PRIORITY_APP_HIGH
NVIC IRQ priority reserved for critical application interrupts.
Definition: impl.hpp:173
nrfcxx::nrf5::series::ADC_Peripheral::busy
static bool busy()
Return true iff the ADC has an in-progress conversion.
Definition: impl.hpp:108
nrfcxx::nrf5::series::ADC_Base
Material common to all ADC peripheral implementations.
Definition: impl.hpp:22
nrfcxx::nrf5::series::ADC_Peripheral::trigger
static void trigger()
Initiate an ADC sample with the current configuration.
Definition: impl.hpp:118
nrfcxx::mutex_irq
nvic_BlockIRQ as a template type.
Definition: core.hpp:497
nrfcxx::nvic_ClearPendingIRQ
static void nvic_ClearPendingIRQ(int irqn)
Wrapper around NVIC_ClearPending to work around issues with peripheral::IRQn.
Definition: core.hpp:412
nrfcxx::nvic_EnableIRQ
static void nvic_EnableIRQ(int irqn)
Wrapper around NVIC_EnableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:376
nrfcxx::nrf5::series::ADC_Peripheral::TOLERANCE_adc16
static constexpr uint16_t TOLERANCE_adc16
ADC offset error is documented +/- 2%, with a 1.5% error for VBG.
Definition: impl.hpp:35
nrfcxx::IRQ_PRIORITY_SD_HIGH
static constexpr auto IRQ_PRIORITY_SD_HIGH
NVIC IRQ priority reserved for critical soft-device interrupts.
Definition: impl.hpp:170
nrfcxx::nrf5::series::ADC_Peripheral::VBG_mV
static constexpr unsigned int VBG_mV
nRF51 reference voltage is 1.2 V.
Definition: impl.hpp:26
nrfcxx::nrf5::series::ADC_Peripheral::make_config
static constexpr unsigned int make_config(int ain, unsigned int res, unsigned int prescale, unsigned int refsel, unsigned int extrefsel=ADC_CONFIG_EXTREFSEL_None)
Helper to build up a ADC CONFIG.
Definition: impl.hpp:88
nrfcxx::IRQ_PRIORITY_SD_LOW
static constexpr auto IRQ_PRIORITY_SD_LOW
NVIC IRQ priority reserved for non-critical soft-device interrupts.
Definition: impl.hpp:176
nrfcxx::IRQ_PRIORITY_APP_LOW
static constexpr auto IRQ_PRIORITY_APP_LOW
NVIC IRQ priority reserved for non-critical application interrupts.
Definition: impl.hpp:179
nrfcxx::nvic_DisableIRQ
static void nvic_DisableIRQ(int irqn)
Wrapper around NVIC_DisableIRQ to work around issues with peripheral::IRQn.
Definition: core.hpp:385
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17