Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_ADC.h
Go to the documentation of this file.
1/*
2 * Author: Crownstone Team
3 * Copyright: Crownstone (https://crownstone.rocks)
4 * Date: 6 Nov., 2014
5 * Triple-license: LGPLv3+, Apache License, and/or MIT
6 */
7#pragma once
8
9#include <ble/cs_Nordic.h>
10#include <cfg/cs_Config.h>
14
16 CS_ADC_GAIN4 = NRF_SAADC_GAIN4, // gain is 4/1, maps [0, 0.15] to [0, 0.6]
17 CS_ADC_GAIN2 = NRF_SAADC_GAIN2, // gain is 2/1, maps [0, 0.3] to [0, 0.6]
18 CS_ADC_GAIN1 = NRF_SAADC_GAIN1, // gain is 1/1, maps [0, 0.6] to [0, 0.6]
19 CS_ADC_GAIN1_2 = NRF_SAADC_GAIN1_2, // gain is 1/2, maps [0, 1.2] to [0, 0.6]
20 CS_ADC_GAIN1_3 = NRF_SAADC_GAIN1_3, // gain is 1/3, maps [0, 1.8] to [0, 0.6]
21 CS_ADC_GAIN1_4 = NRF_SAADC_GAIN1_4, // gain is 1/4, maps [0, 2.4] to [0, 0.6]
22 CS_ADC_GAIN1_5 = NRF_SAADC_GAIN1_5, // gain is 1/5, maps [0, 3.0] to [0, 0.6]
23 CS_ADC_GAIN1_6 = NRF_SAADC_GAIN1_6, // gain is 1/6, maps [0, 3.6] to [0, 0.6]
24};
25
31typedef void (*adc_done_cb_t)(adc_buffer_id_t bufIndex);
32
33typedef void (*adc_zero_crossing_cb_t)();
34
35// State of this class
39 ADC_STATE_WAITING_TO_START, // Wait for buffers to be released.
40 ADC_STATE_READY_TO_START // This state is a dummy, to work around the state check in start(). It is set just before
41 // calling start() again.
42};
43
44// State of the SAADC
46 ADC_SAADC_STATE_IDLE, // When saadc is idle.
47 ADC_SAADC_STATE_BUSY, // When saadc is busy sampling.
48 ADC_SAADC_STATE_STOPPING // When saadc is or will be commanded to stop.
49};
50
51// Max number of buffers in the SAADC peripheral.
52#define CS_ADC_NUM_SAADC_BUFFERS 2
53
85
86public:
88 static ADC& getInstance() {
89 static ADC instance;
90 return instance;
91 }
92
99
102 void start();
103
107 void stop();
108
114
123
129 void enableZeroCrossingInterrupt(adc_channel_id_t channel, int32_t zeroVal);
130
141
142 // Handle events as EventListener.
143 void handleEvent(event_t& event);
144
147 void _restart();
148
152
156 void _handleAdcLimitInterrupt(nrf_saadc_limit_t type);
157
166
167private:
171
172 // This class is singleton, deny implementation
173 ADC(ADC const&);
174
175 // This class is singleton, deny implementation
176 void operator=(ADC const&);
177
183 bool _changeConfig = false;
184
191
198
205
212 nrf_ppi_channel_t _ppiChannelSample;
213
219 nrf_ppi_channel_t _ppiChannelStart;
220
227
236
237 // True when next buffer is the first after start.
238 bool _firstBuffer = true;
239
240 // State of this class.
242
249
250 // Callback function
252
253 inline bool dataCallbackRegistered() { return (_doneCallback != nullptr); }
254
261
268
274 nrf_saadc_event_t _eventLimitLow;
275
281 nrf_saadc_event_t _eventLimitHigh;
282
289
296
302 int32_t _zeroValue = 0;
303
308
310
319
320 // Set the adc limit such that it triggers when going above zero
322
323 // Set the adc limit such that it triggers when going below zero
325
326 // Initialize buffer queue
328
339
347 cs_ret_code_t _fillSaadcQueue(bool fromInterrupt);
348
361
368
374
379
381
382 // Function to apply a new config. Should only be called when SAADC has stopped.
384
385 // Helper function that returns the adc pin number, given the AIN number.
386 static nrf_saadc_input_t getAdcPin(adc_pin_id_t pinNum);
387
388 // Helper function to get the ppi channel, given the index.
389 static nrf_ppi_channel_t getPpiChannel(uint8_t index);
390
391 // Helper function to get the limit event, given the channel.
392 static nrf_saadc_event_t getLimitLowEvent(adc_channel_id_t channel);
393
394 // Helper function to get the limit event, given the channel.
395 static nrf_saadc_event_t getLimitHighEvent(adc_channel_id_t channel);
396
397 // Helper function to get the gpiote task out, given the index.
398 static nrf_gpiote_tasks_t getGpioteTaskOut(uint8_t index);
399};
Analog-Digital conversion.
Definition: cs_ADC.h:84
adc_state_t _state
Definition: cs_ADC.h:241
void enterCriticalRegion()
Enter a region of code where variables are used that are also used in SAADC interrupts.
cs_ret_code_t _addBufferToSaadcQueue(adc_buffer_id_t bufIndex)
Same as addBufferToSaadcQueue(), but without critical region enter/exit.
void applyConfig()
nrf_saadc_event_t _eventLimitHigh
Cache limit event.
Definition: cs_ADC.h:281
void enableZeroCrossingInterrupt(adc_channel_id_t channel, int32_t zeroVal)
Enable zero crossing detection on given channel, generating interrupts.
ADC(ADC const &)
bool _firstBuffer
Definition: cs_ADC.h:238
adc_channel_id_t _zeroCrossingChannel
The channel which is checked for zero crossings.
Definition: cs_ADC.h:267
bool _changeConfig
Whether or not the config should be changed.
Definition: cs_ADC.h:183
cs_ret_code_t initSaadc()
void _handleAdcLimitInterrupt(nrf_saadc_limit_t type)
Called when the sampled value is above upper limit, or below lower limit.
cs_ret_code_t initChannel(adc_channel_id_t channel, adc_channel_config_t &config)
Configure a channel.
static nrf_ppi_channel_t getPpiChannel(uint8_t index)
cs_ret_code_t init(const adc_config_t &config)
Initialize ADC.
void exitCriticalRegion()
Exit a region of code where variables are used that are also used in SAADC interrupts.
void setLimitUp()
void setZeroCrossingCallback(adc_zero_crossing_cb_t callback)
Set the callback which is called on a zero crossing interrupt.
ADC()
Constructor.
cs_ret_code_t fillSaadcQueue()
Try to add all queued buffers to the SAADC queue.
CircularBuffer< adc_buffer_id_t > _bufferQueue
Queue of buffers that are free to be added to the SAADC queue.
Definition: cs_ADC.h:226
static nrf_saadc_event_t getLimitHighEvent(adc_channel_id_t channel)
CircularBuffer< adc_buffer_id_t > _saadcBufferQueue
Keeps up which buffers that are queued in the SAADC peripheral.
Definition: cs_ADC.h:235
void setLimitDown()
void start()
Start the ADC sampling.
void setDoneCallback(adc_done_cb_t callback)
Set the callback which is called when a buffer is filled.
void handleEvent(event_t &event)
Handle events.
adc_done_cb_t _doneCallback
Definition: cs_ADC.h:251
bool _zeroCrossingEnabled
Keep up whether zero crossing is enabled.
Definition: cs_ADC.h:288
uint32_t _lastZeroCrossUpTime
Store the RTC timestamp of the last upwards zero crossing.
Definition: cs_ADC.h:295
static ADC & getInstance()
Use static variant of singleton, no dynamic memory allocation.
Definition: cs_ADC.h:88
adc_zero_crossing_cb_t _zeroCrossingCallback
The zero crossing callback.
Definition: cs_ADC.h:260
int _criticalRegionEntered
Keep up number of nested critical regions entered.
Definition: cs_ADC.h:307
static nrf_saadc_event_t getLimitLowEvent(adc_channel_id_t channel)
void operator=(ADC const &)
static nrf_saadc_input_t getAdcPin(adc_pin_id_t pinNum)
void _handleAdcInterrupt()
Handles the ADC interrupt.
nrf_saadc_event_t _eventLimitLow
Cache limit event.
Definition: cs_ADC.h:274
void printQueues()
cs_ret_code_t addBufferToSaadcQueue(adc_buffer_id_t bufIndex)
Puts a buffer in the SAADC queue.
int32_t _zeroValue
Store the zero value used to detect zero crossings.
Definition: cs_ADC.h:302
cs_ret_code_t changeChannel(adc_channel_id_t channel, adc_channel_config_t &config)
Change channel config.
void _handleAdcDone(adc_buffer_id_t bufIndex)
Handle buffer, called in main thread.
cs_ret_code_t _fillSaadcQueue(bool fromInterrupt)
Same as fillSaadcQueue(), but without critical region enter/exit.
adc_config_t _config
Configuration of this class.
Definition: cs_ADC.h:190
volatile adc_saadc_state_t _saadcState
Sate of the SAADC peripheral.
Definition: cs_ADC.h:248
nrf_ppi_channel_t _ppiChannelStart
PPI channel used to start the SAADC on end event.
Definition: cs_ADC.h:219
adc_buffer_seq_nr_t _bufSeqNr
Buffer sequence number.
Definition: cs_ADC.h:204
bool dataCallbackRegistered()
Definition: cs_ADC.h:253
cs_ret_code_t initBufferQueue()
nrf_ppi_channel_t _ppiChannelSample
PPI channel to sample each tick, and count these.
Definition: cs_ADC.h:212
adc_channel_config_result_t _channelResultConfigs[CS_ADC_NUM_CHANNELS]
Resulting configuration of this class.
Definition: cs_ADC.h:197
void _restart()
Restart.
static nrf_gpiote_tasks_t getGpioteTaskOut(uint8_t index)
void stop()
Stop the ADC sampling Only to be called from main thread.
Circular Buffer implementation.
Definition: cs_CircularBuffer.h:27
Event listener.
Definition: cs_EventListener.h:17
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
void(* adc_zero_crossing_cb_t)()
Definition: cs_ADC.h:33
adc_gain_t
Definition: cs_ADC.h:15
@ CS_ADC_GAIN1_2
Definition: cs_ADC.h:19
@ CS_ADC_GAIN2
Definition: cs_ADC.h:17
@ CS_ADC_GAIN1
Definition: cs_ADC.h:18
@ CS_ADC_GAIN1_3
Definition: cs_ADC.h:20
@ CS_ADC_GAIN1_4
Definition: cs_ADC.h:21
@ CS_ADC_GAIN1_5
Definition: cs_ADC.h:22
@ CS_ADC_GAIN4
Definition: cs_ADC.h:16
@ CS_ADC_GAIN1_6
Definition: cs_ADC.h:23
adc_state_t
Definition: cs_ADC.h:36
@ ADC_STATE_READY_TO_START
Definition: cs_ADC.h:40
@ ADC_STATE_IDLE
Definition: cs_ADC.h:37
@ ADC_STATE_BUSY
Definition: cs_ADC.h:38
@ ADC_STATE_WAITING_TO_START
Definition: cs_ADC.h:39
adc_saadc_state_t
Definition: cs_ADC.h:45
@ ADC_SAADC_STATE_STOPPING
Definition: cs_ADC.h:48
@ ADC_SAADC_STATE_IDLE
Definition: cs_ADC.h:46
@ ADC_SAADC_STATE_BUSY
Definition: cs_ADC.h:47
void(* adc_done_cb_t)(adc_buffer_id_t bufIndex)
The typedef adc_done_cb_t is a function pointer to a function with the buffer index as argument.
Definition: cs_ADC.h:31
#define CS_ADC_NUM_CHANNELS
Definition: cs_Config.h:147
uint8_t adc_buffer_seq_nr_t
Definition: cs_Typedefs.h:35
uint8_t adc_pin_id_t
Definition: cs_Typedefs.h:32
uint8_t adc_buffer_id_t
Definition: cs_Typedefs.h:30
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
uint8_t adc_channel_id_t
Definition: cs_Typedefs.h:31
Result struct after configuring an ADC channel.
Definition: cs_PacketsInternal.h:333
Struct to configure an ADC channel.
Definition: cs_PacketsInternal.h:293
Struct to configure the ADC.
Definition: cs_PacketsInternal.h:306