EVE 1.0
adc0.h
Go to the documentation of this file.
1 #ifndef DRIVER_ADC0_H
2 #define DRIVER_ADC0_H
3 /**********************************************************************/
4 /*
5  * Copyright (c) 2013-2015, Jetro AS
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  * derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28  * OF SUCH DAMAGE.
29  *
30  * This file is part of the EVE platform.
31  */
32 
33 /**
34  * \file
35  * \brief Driver for ADC scheduling and integration.
36  *
37  * \author KLO, Jetro AS
38  */ /******************************************************************/
39 
40 #include <stdint.h>
41 #include <stdbool.h>
42 #include <core/clk.h>
43 #include <hal/nrf_saadc.h>
44 
45 /**
46  * \defgroup ADC nRF52 ADC driver
47  * \ingroup nrf52
48  * \{
49  *
50  * This ADC driver can be used for ADC0.
51  *
52  * All ADC channels must be initialized before the ADC driver may be used.
53  * An ADC channel can be configured to have an analog mux (1-64 mux inputs).
54  * If an ADC channel has an analog multiplexer, the MUX control lines must be
55  * controlled by the same IO port with consecutive port pins.
56  * For each configured ADC input, individually voltage reference etc. can be set.
57  * Individual buffers must be allocated.
58  *
59  * It is possible to just start a single AD convertion.
60  * It is also possible that each configured ADC input can be started
61  * with its own scanning scheduler. Scanning interval is is set with resolution
62  * 1/1.024 ms and is based on the mwork scheduler.
63  * If multiple configured ADC channels should have the same starting time
64  * in the scheduler even though they are started sequentially, they can be
65  * started based on a common read time reference.
66  * If the Interval parameter is set to zero/0, the application may call the Adc0ManualScheduler()
67  * function and do the scheduler timing itself. This manual scheduler will use the same
68  * Buffer and IntegratedValue as the automatic mwork scheduler.
69  *
70  * If the configured ADC input has mux, the allocated buffer will be shared
71  * linearly for all the mux inputs.
72  * While AD convertion is in progress, or while the scheduler is doing
73  * AD convertions, the clock is locked and energy mode EM1 is minimum required.
74  */
75 
76 /***********************************************************************
77  * Global defines
78 ***********************************************************************/
79 /**
80  * adc_state_t structure holds pointers to buffers and variables used by the ADC driver.
81  * IntegratedValue, VoltageMuxBuffer and Size must be initialized before
82  * the PWM driver may be used.
83  *
84  */
86 {
87  uint32_t *IntegratedValue; // The scheduler will use this location for storing the integrated value
88  uint16_t *Buffer; // Pointer to buffer area per ADC channel used by the scheduler
89  uint32_t TimeRef; // Used by scheduler for next time to schedule current channel
90  uint16_t Size; // Sizeof Buffer used for data stored by scheduler
91  uint16_t Index; // Used by scheduler to index Buffer.
92  uint16_t Count; // Contains the number of bytes stored within Buffer.
93 };
94 
95 /**
96  * adc_streamer_state_t structure holds state of the ADC streamer
97  */
98 
100 {
101  uint8_t SampleBufIndex; /**< Index of the active sampling buffer */
102 };
103 
104 struct adc_streamer_t;
105 
106 /**
107  * adc_cb_parameter_t enum holds parameter used by the ADC driver callback function.
108  *
109  */
111 {
112  ADC_MEASURE_NETWORK_OFF,
113  ADC_MEASURE_NETWORK_ON
114 };
115 
116 /**
117  * adc_callback_t Prototype ADC driver callback function.
118  *
119  */
120 typedef void (* adc_callback_t)(enum adc_cb_parameter_t AdcCbParameter);
121 
122 /**
123  * adc_streamer_callback_t Prototype ADC driver callback function.
124  *
125  */
126 typedef void (* adc_streamer_callback_t)(const struct adc_streamer_t *Adc, uint8_t SampleBufIndex);
127 
128 /**
129  * adc_mux_config_t structure is used for configuration of the ADC multiplexor.
130  * The structure must be initialized before the ADC driver may be used.
131  *
132  */
134 {
135  uint32_t NumberOfChannels : 7; /**< 0 if no mux, else number of mux inputs (1-64) */
136  uint32_t AddressPinCount : 3; /**< Number of pins used for mux addresses */
137  uint32_t FirstChannel : 6; /**< First mux channel (0 - 63) */
138  uint32_t AddressPinLsb : 5; /**< Pin for LSB mux address lines - must be continuously */
139 };
140 
141 /**
142  * adc_analog_config_t structure is used for configuration of the analog part of the AD conversion
143  * The structure must be initialized before the ADC driver may be used.
144  *
145  */
147 {
148  int16_t LowLimit; /**< Low limit */
149  int16_t HighLimit; /**< High limit */
150  uint32_t InputP : 4; /**< nrf_saadc_input_t */
151  uint32_t ResistorP : 2; /**< nrf_saadc_resistor_t */
152  uint32_t InputN : 4; /**< nrf_saadc_input_t */
153  uint32_t ResistorN : 2; /**< nrf_saadc_resistor_t */
154  uint32_t Gain : 3; /**< nrf_saadc_gain_t */
155  uint32_t Reference : 1; /**< nrf_saadc_reference_t */
156  uint32_t AcquistionTime : 3; /**< nrf_saadc_acqtime_t */
157 };
158 
159 /**
160  * adc_digital_config_t structure is used for configuration of the digital part of the AD conversion
161  * The structure must be initialized before the ADC driver may be used.
162  *
163  */
165 {
166  uint32_t Resolution : 2; /**< nrf_saadc_resolution_t */
167  uint32_t Oversampling : 4; /**< nrf_saadc_oversample_t */
168 };
169 
170 /**
171  * adc_channel_t structure is used for configuration one ADC channel.
172  * The channel can be configured to have an analog mux.
173  * The structure must be initialized before the ADC driver may be used.
174  *
175  */
177 {
178  struct adc_state_t *State;
179  struct adc_analog_config_t AnalogConfig; /**< Configuration of the analog part of the ADC */
180  struct adc_digital_config_t DigitalConfig; /**< Configuration of the digital part of the ADC */
181  struct adc_mux_config_t MuxConfig; /**< Multiplexor configuration */
182  uint32_t Interval; /**< 1/1.024 ms resolution */
183  adc_callback_t Callback; /**< Callback function pointer for external power-up of ADC channel */
184  uint32_t uDelay; /**< Delay in usec to wait after callback for power-on*/
185 };
186 
187 
188 /**
189  * adc_streamer_t structure is used for configuration of an ADC streamer.
190  * The structure must be initialized before the ADC streamer may be used.
191  *
192  */
194 {
195  struct adc_streamer_state_t *State;
196  struct adc_analog_config_t AnalogConfig; /**< Configuration of the analog part of the ADC */
197  struct adc_digital_config_t DigitalConfig; /**< Configuration of the digital part of the ADC */
198  uint32_t SampleRate; /**< Sample rate in samples / sec, 0 for external trigger. */
199  adc_streamer_callback_t Callback; /**< The callback is called when a buffer is filled */
200  void *SampleBuffer[2]; /**< Sample buffer */
201  uint16_t SampleBufferSize; /**< Size of a single buffer of the double buffer */
202  uint8_t InterruptPriority; /**< NRF_APP_PRIORITIES */
203  uint8_t PpiChannel; /**< PPI channel, internally used by the streamer */
204  bool OversamlingInBurstMode; /**< true if oversampling is done in bursts */
205 };
206 
207 /**
208  * adc_schedule_t structure holds parameter used by the ADC scheduler.
209  *
210  */
212 {
213  struct adc_channel_t const **Table;
214  uint16_t NumberOfChannels;
215 };
216 
217  /**
218  * @name Functions called from application programs
219  * @{
220  */
221 
222 /**********************************************************************/
223 /**
224  * @brief Name: Adc0Init\n
225  * Initializes pointers used by ADC scheduler.
226  * Called from main program level.
227  *
228  * @param AdcSchedule Pointer to ADC scheduler table in application layer.
229  ***********************************************************************/
230 extern void Adc0Init(const struct adc_schedule_t *AdcSchedule);
231 
232 /**********************************************************************/
233 /**
234  * @brief Name: Adc0ChannelConfig\n
235  * Configures an analog channel in scan mode.
236  *
237  * @param Adc Configuration structure for ADC input to be used
238  * @param Index Scan index. 0 for the main channel, > 0 enables scan and
239  * correspondes to the particular scan slot.
240  * @param Config Analog channel configuration
241  ***********************************************************************/
242 extern bool Adc0ChannelConfig(const struct adc_channel_t *Adc, int Index, const struct adc_analog_config_t *Config);
243 
244 /**********************************************************************/
245 /**
246  * @brief Name: Adc0ChannelStart\n
247  * Starts one ADC channel scheduler based on input parameters.
248  *
249  * @param Adc Configuration structure for ADC input to be used
250  * @param TimeRef Time reference tick got from clock_time() function
251  * @return true if the ADC input was successfully started, otherwise false
252  ***********************************************************************/
253 extern bool Adc0ChannelStart(const struct adc_channel_t *Adc, uint32_t TimeRef);
254 
255 /**********************************************************************/
256 /**
257  * @brief Name: Adc0ManualScheduler\n
258  * Called by application for manual data scheduling.
259  *
260  * @param Adc Configuration structure for ADC input to be used
261  ***********************************************************************/
262 extern void Adc0ManualScheduler(const struct adc_channel_t *Adc);
263 
264 /**********************************************************************/
265 /**
266  * @brief Name: Adc0ChannelDisable\n
267  * Disables one ADC channel scheduler based on input parameters.
268  * When all channels are disabled, the scheduler is stopped.
269  *
270  * @param Adc Configuration structure for ADC input to be used
271  ***********************************************************************/
272 extern void Adc0ChannelDisable(const struct adc_channel_t *Adc);
273 
274 /**********************************************************************/
275 /**
276  * @brief Name: Adc0ChannelDisableAll\n
277  * Disables all ADC channel schedulers and stops the scheduler.
278  ***********************************************************************/
279 extern void Adc0ChannelDisableAll(void);
280 
281 /**********************************************************************/
282 /**
283  * @brief Name: Adc0GetMuxValue\n
284  * Starts ADC for selected ADC channel and mux input.
285  *
286  * @param Adc Configuration structure for ADC input to be used
287  * @param MuxChannel
288  * @return Converted ADC value for selected channel and mux input
289  ***********************************************************************/
290 extern uint16_t Adc0GetMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel);
291 
292 /**********************************************************************/
293 /**
294  * @brief Name: Adc0GetValue\n
295  * Starts ADC for selected ADC channel.
296  *
297  * @param Adc Configuration structure for ADC input to be used
298  * @return Converted ADC value for selected channel
299  ***********************************************************************/
300 static inline uint16_t Adc0GetValue(const struct adc_channel_t *Adc)
301 {
302  return Adc0GetMuxValue(Adc, 0);
303 }
304 
305 /**********************************************************************/
306 /**
307  * @brief Name: Adc0GetLastMuxValue
308  * Returns last converted ADC value for selected ADC channel and mux input.
309  *
310  * @param Adc Configuration structure for ADC input to be used
311  * @param MuxChannel
312  * @return Last converted ADC value for selected ADC channel and mux input
313  ***********************************************************************/
314 extern uint16_t Adc0GetLastMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel);
315 
316 /**********************************************************************/
317 /**
318  * @brief Name: Adc0GetLastValue
319  * Returns last converted ADC value for selected ADC channel.
320  *
321  * @param Adc Configuration structure for ADC input to be used
322  * @return Last converted ADC value for selected ADC channel
323  ***********************************************************************/
324 static inline uint16_t Adc0GetLastValue(const struct adc_channel_t *Adc)
325 {
326  return Adc0GetLastMuxValue(Adc, 0);
327 }
328 
329 /**********************************************************************/
330 /**
331  * @brief Name: Adc0GetIntegratedMuxValue
332  * Returns integrated ADC value for selected ADC channel and mux input.
333  *
334  * @param Adc Configuration structure for ADC input to be used
335  * @param MuxChannel
336  * @param Count Number of samples in the integrated value is returned in Count
337  * @return Integrated ADC value for selected channel and mux input
338  ***********************************************************************/
339 extern uint32_t Adc0GetIntegratedMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel, uint16_t *Count);
340 
341 /**********************************************************************/
342 /**
343  * @brief Name: Adc0GetIntegratedValue
344  * Returns integrated ADC value for selected ADC channel.
345  *
346  * @param Adc Configuration structure for ADC input to be used
347  * @param Count Number of samples in the integrated value is returned in Count
348  * @return Integrated ADC value for selected channel
349  ***********************************************************************/
350 static inline uint32_t Adc0GetIntegratedValue(const struct adc_channel_t *Adc, uint16_t *Count)
351 {
352  return Adc0GetIntegratedMuxValue(Adc, 0, Count);
353 }
354 
355 /**********************************************************************/
356 /**
357  * @brief Name: Adc0GetMeanMuxValue
358  * Returns mean ADC value for selected ADC channel and mux input.
359  *
360  * @param Adc Configuration structure for ADC input to be used
361  * @param MuxChannel
362  * @return Mean ADC value for selected channel and mux input
363  ***********************************************************************/
364 extern uint16_t Adc0GetMeanMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel);
365 
366 /**********************************************************************/
367 /**
368  * @brief Name: Adc0GetMeanValue
369  * Returns mean ADC value for selected ADC channel.
370  *
371  * @param Adc Configuration structure for ADC input to be used
372  * @return Mean ADC value for selected channel
373  ***********************************************************************/
374 static inline uint16_t Adc0GetMeanValue(const struct adc_channel_t *Adc)
375 {
376  return Adc0GetMeanMuxValue(Adc, 0);
377 }
378 
379 /**********************************************************************/
380 /**
381  * @brief Name: Adc0StreamerStart\n
382  * Starts continous sampling and streaming of data.
383  *
384  * @param Adc Configuration structure for the streamer
385  * @return true if the streamer was started, false otherwise
386  ***********************************************************************/
387 extern bool Adc0StreamerStart(const struct adc_streamer_t *Adc);
388 
389 /**********************************************************************/
390 /**
391  * @brief Name: Adc0StreamerDisable\n
392  * Stops continous sampling and streaming of data.
393  *
394  * @param Adc Configuration structure for the streamer
395  ***********************************************************************/
396 extern void Adc0StreamerDisable(const struct adc_streamer_t *Adc);
397 
398 /** @} */
399 
400 /** \} ADC */
401 
402 #endif //DRIVER_ADC0_H
void Adc0ChannelDisable(const struct adc_channel_t *Adc)
Name: Adc0ChannelDisable Disables one ADC channel scheduler based on input parameters. When all channels are disabled, the scheduler is stopped.
uint8_t SampleBufIndex
Definition: adc0.h:101
uint16_t SampleBufferSize
Definition: adc0.h:201
static uint16_t Adc0GetValue(const struct adc_channel_t *Adc)
Name: Adc0GetValue Starts ADC for selected ADC channel.
Definition: adc0.h:300
adc_streamer_callback_t Callback
Definition: adc0.h:199
uint8_t PpiChannel
Definition: adc0.h:203
static uint16_t Adc0GetLastValue(const struct adc_channel_t *Adc)
Name: Adc0GetLastValue Returns last converted ADC value for selected ADC channel. ...
Definition: adc0.h:324
uint32_t Adc0GetIntegratedMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel, uint16_t *Count)
Name: Adc0GetIntegratedMuxValue Returns integrated ADC value for selected ADC channel and mux input...
static uint16_t Adc0GetMeanValue(const struct adc_channel_t *Adc)
Name: Adc0GetMeanValue Returns mean ADC value for selected ADC channel.
Definition: adc0.h:374
adc_callback_t Callback
Definition: adc0.h:183
uint32_t SampleRate
Definition: adc0.h:198
void(* adc_callback_t)(enum adc_cb_parameter_t AdcCbParameter)
Definition: adc0.h:120
void Adc0ChannelDisableAll(void)
Name: Adc0ChannelDisableAll Disables all ADC channel schedulers and stops the scheduler.
uint16_t Adc0GetMeanMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel)
Name: Adc0GetMeanMuxValue Returns mean ADC value for selected ADC channel and mux input...
uint32_t Interval
Definition: adc0.h:182
uint8_t InterruptPriority
Definition: adc0.h:202
adc_cb_parameter_t
Definition: adc0.h:110
uint16_t Adc0GetMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel)
Name: Adc0GetMuxValue Starts ADC for selected ADC channel and mux input.
Header file for the EVE clock management framework.
int16_t LowLimit
Definition: adc0.h:148
void(* adc_streamer_callback_t)(const struct adc_streamer_t *Adc, uint8_t SampleBufIndex)
Definition: adc0.h:126
bool Adc0StreamerStart(const struct adc_streamer_t *Adc)
Name: Adc0StreamerStart Starts continous sampling and streaming of data.
uint16_t Adc0GetLastMuxValue(const struct adc_channel_t *Adc, uint8_t MuxChannel)
Name: Adc0GetLastMuxValue Returns last converted ADC value for selected ADC channel and mux input...
bool Adc0ChannelStart(const struct adc_channel_t *Adc, uint32_t TimeRef)
Name: Adc0ChannelStart Starts one ADC channel scheduler based on input parameters.
bool OversamlingInBurstMode
Definition: adc0.h:204
void Adc0ManualScheduler(const struct adc_channel_t *Adc)
Name: Adc0ManualScheduler Called by application for manual data scheduling.
void Adc0Init(const struct adc_schedule_t *AdcSchedule)
Name: Adc0Init Initializes pointers used by ADC scheduler. Called from main program level...
bool Adc0ChannelConfig(const struct adc_channel_t *Adc, int Index, const struct adc_analog_config_t *Config)
Name: Adc0ChannelConfig Configures an analog channel in scan mode.
uint32_t uDelay
Definition: adc0.h:184
void Adc0StreamerDisable(const struct adc_streamer_t *Adc)
Name: Adc0StreamerDisable Stops continous sampling and streaming of data.
static uint32_t Adc0GetIntegratedValue(const struct adc_channel_t *Adc, uint16_t *Count)
Name: Adc0GetIntegratedValue Returns integrated ADC value for selected ADC channel.
Definition: adc0.h:350
int16_t HighLimit
Definition: adc0.h:149