µGFX  2.9
version 2.9
gadc.h
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 /**
9  * @file src/gadc/gadc.h
10  *
11  * @addtogroup GADC
12  *
13  * @brief Module to abstract the very variable ADC interfaces of the underlying systems
14  *
15  * @details The reason why uGFX has it's own ADC abstraction is because
16  * the Chibi-OS drivers are very CPU specific and do not
17  * provide a way across all hardware platforms to create periodic
18  * ADC conversions. There are also issues with devices with different
19  * characteristics or periodic requirements on the same ADC
20  * device (but different channels). This layer attempts to solve these
21  * problems to provide a architecture neutral API. It also provides extra
22  * features such as multi-buffer chaining for high speed ADC sources.
23  * It provides one high speed virtual ADC device (eg a microphone) and
24  * numerous low speed (less than 100Hz) virtual ADC devices (eg dials,
25  * temperature sensors etc). The high speed device has timer based polling
26  * to ensure exact conversion periods and a buffer management system.
27  * The low speed devices are assumed to be non-critical timing devices
28  * and do not have any buffer management.
29  * Note that while only one high speed device has been provided it can
30  * be used to read multiple physical ADC channels on the one physical
31  * ADC device.
32  * All callback routines are thread based unlike the Chibi-OS interrupt based
33  * routines.
34  *
35  * @{
36  */
37 
38 #ifndef _GADC_H
39 #define _GADC_H
40 
41 #include "../../gfx.h"
42 
43 #if GFX_USE_GADC || defined(__DOXYGEN__)
44 
45 /* Include the driver defines */
46 #include "gadc_lld_config.h"
47 
48 /*===========================================================================*/
49 /* Type definitions */
50 /*===========================================================================*/
51 
52 // Event types for GADC
53 #define GEVENT_ADC (GEVENT_GADC_FIRST+0)
54 
55 /**
56  * @brief The High Speed ADC event structure.
57  * @{
58  */
59 typedef struct GEventADC_t {
60  #if GFX_USE_GEVENT || defined(__DOXYGEN__)
61  /**
62  * @brief The type of this event (GEVENT_ADC)
63  */
64  GEventType type;
65  #endif
66 
67  /**
68  * @brief The event flags
69  */
70  gU16 flags;
71  /**
72  * @brief The event flag values.
73  * @{
74  */
75  #define GADC_HSADC_LOSTEVENT 0x0001 /**< @brief The last GEVENT_HSDADC event was lost */
76  #define GADC_HSADC_RUNNING 0x0002 /**< @brief The High Speed ADC is currently running */
77  #define GADC_HSADC_GOTBUFFER 0x0004 /**< @brief A buffer is ready for processing */
78  #define GADC_HSADC_STALL 0x0008 /**< @brief The High Speed ADC has stalled due to no free buffers */
79  /** @} */
81 /** @} */
82 
83 /**
84  * @brief A callback function (executed in a thread context) for a low speed conversion
85  */
86 typedef void (*GADCCallbackFunction)(adcsample_t *buffer, void *param);
87 
88 /**
89  * @brief A callback function (executed in an ISR context) for a high speed conversion
90  */
91 typedef void (*GADCISRCallbackFunction)(void);
92 
93 /*===========================================================================*/
94 /* External declarations. */
95 /*===========================================================================*/
96 
97 /**
98  * @brief Initialise the high speed ADC.
99  * @details Initialises but does not start the conversions.
100  *
101  * @param[in] physdev A value passed to describe which physical ADC devices/channels to use.
102  * @param[in] frequency The frequency to create ADC conversions
103  *
104  * @note If the high speed ADC is running it will be stopped. The Event subsystem is
105  * disconnected from the high speed ADC and any binary semaphore event is forgotten.
106  * @note ChibiOS ONLY: Due to a bug in ChibiOS each buffer on the free-list must contain an even number of
107  * samples and for multi-channel devices it must hold a number of samples that is evenly divisible
108  * by 2 times the number of active channels.
109  * @note The physdev parameter may be used to turn on more than one ADC channel.
110  * Each channel is then interleaved into the provided buffer. Make sure your buffers all hold
111  * a number of samples evenly divisible by the number of active channels.
112  * As an example, if physdev turns on 2 devices then the buffer contains
113  * alternate device samples and the buffer must contain multiples of 2 samples.
114  * The exact meaning of physdev is hardware dependent.
115  * @note While the high speed ADC is running, low speed conversions can only occur at
116  * the frequency of the high speed events. Thus if high speed events are
117  * being created at 50Hz (eg 100 samples/buffer, frequency = 5kHz) then the maximum
118  * frequency for low speed conversions will be 50Hz.
119  * @note Only a single sample format is supported - that provided by the GADC driver. That sample
120  * format applies to both high speed and low speed sampling.
121  *
122  * @api
123  */
124 void gadcHighSpeedInit(gU32 physdev, gU32 frequency);
125 
126 #if GFX_USE_GEVENT || defined(__DOXYGEN__)
127  /**
128  * @brief Turn on sending results to the GEVENT sub-system.
129  * @details Returns a GSourceHandle to listen for GEVENT_ADC events.
130  *
131  * @note The high speed ADC will not use the GEVENT system unless this is
132  * called first. This saves processing time if the application does
133  * not want to use the GEVENT sub-system for the high speed ADC.
134  * Once turned on it can only be turned off by calling @p gadcHighSpeedInit() again.
135  * @note The high speed ADC is capable of signalling via this method, an ISR callback and a
136  * binary semaphore at the same time.
137  *
138  * @return The GSourceHandle
139  *
140  * @api
141  */
142  GSourceHandle gadcHighSpeedGetSource(void);
143 #endif
144 
145 /**
146  * @brief Allow retrieving of results from the high speed ADC using an ISR callback.
147  *
148  * @param[in] isrfn The callback function (called in an ISR context).
149  *
150  * @note Passing a NULL for isrfn will turn off signalling via this method as will calling
151  * @p gadcHighSpeedInit().
152  * @note The high speed ADC is capable of signalling via this method, a blocked thread and the GEVENT
153  * sub-system at the same time.
154  *
155  * @api
156  */
158 
159 /**
160  * @brief Get a filled buffer from the ADC
161  * @return A GDataBuffer pointer or NULL if the timeout is exceeded
162  *
163  * @param[in] ms The maximum amount of time in milliseconds to wait for data if some is not currently available.
164  *
165  * @note After processing the data, your application must return the buffer to the free-list so that
166  * it can be used again. This can be done using @p gfxBufferRelease().
167  * @note A buffer may be returned to the free-list before you have finished processing it provided you finish
168  * processing it before GADC re-uses it. This is useful when RAM usage is critical to reduce the number
169  * of buffers required. It works before the free list is a FIFO queue and therefore buffers are kept
170  * in the queue as long as possible before they are re-used.
171  * @note The function ending with "I" is the interrupt class function.
172  * @api
173  * @{
174  */
176 GDataBuffer *gadcHighSpeedGetDataI(void);
177 /** @} */
178 
179 /**
180  * @brief Start the high speed ADC conversions.
181  * @pre It must have been initialised first with @p gadcHighSpeedInit()
182  *
183  * @api
184  */
186 
187 /**
188  * @brief Stop the high speed ADC conversions.
189  *
190  * @api
191  */
192 void gadcHighSpeedStop(void);
193 
194 /**
195  * @brief Perform a single low speed ADC conversion
196  * @details Blocks until the conversion is complete
197  * @pre This should not be called from within a GTimer callback as this routine
198  * blocks until the conversion is ready.
199  *
200  * @param[in] physdev A value passed to describe which physical ADC devices/channels to use.
201  * @param[in] buffer The static buffer to put the ADC samples into.
202  *
203  * @note This may take a while to complete if the high speed ADC is running as the
204  * conversion is interleaved with the high speed ADC conversions on a buffer
205  * completion.
206  * @note The result buffer must be large enough to store one sample per device
207  * described by the 'physdev' parameter.
208  * @note Specifying more than one device in physdev is possible but discouraged as the
209  * calculations to ensure the high speed ADC correctness will be incorrect. Symptoms
210  * from over-running the high speed ADC include high speed device stalling or samples being lost.
211  *
212  * @api
213  */
214 void gadcLowSpeedGet(gU32 physdev, adcsample_t *buffer);
215 
216 /**
217  * @brief Perform a low speed ADC conversion with callback (in a thread context)
218  * @details Returns gFalse if internal memory allocation fails
219  *
220  * @param[in] physdev A value passed to describe which physical ADC devices/channels to use.
221  * @param[in] buffer The static buffer to put the ADC samples into.
222  * @param[in] fn The callback function to call when the conversion is complete.
223  * @param[in] param A parameter to pass to the callback function.
224  *
225  * @return gFalse if no free low speed ADC slots.
226  *
227  * @note This may be safely called from within a GTimer callback.
228  * @note The callback may take a while to occur if the high speed ADC is running as the
229  * conversion is interleaved with the high speed ADC conversions on a buffer
230  * completion.
231  * @note The result buffer must be large enough to store one sample per device
232  * described by the 'physdev' parameter.
233  * @note Specifying more than one device in physdev is possible but discouraged as the
234  * calculations to ensure the high speed ADC correctness will be incorrect. Symptoms
235  * from over-running the high speed ADC include high speed samples being lost.
236  *
237  * @api
238  */
239 gBool gadcLowSpeedStart(gU32 physdev, adcsample_t *buffer, GADCCallbackFunction fn, void *param);
240 
241 #endif /* GFX_USE_GADC */
242 
243 #endif /* _GADC_H */
244 /** @} */
gBool gadcLowSpeedStart(gU32 physdev, adcsample_t *buffer, GADCCallbackFunction fn, void *param)
Perform a low speed ADC conversion with callback (in a thread context)
void(* GADCCallbackFunction)(adcsample_t *buffer, void *param)
A callback function (executed in a thread context) for a low speed conversion.
Definition: gadc.h:86
void gadcHighSpeedSetISRCallback(GADCISRCallbackFunction isrfn)
Allow retrieving of results from the high speed ADC using an ISR callback.
GDataBuffer * gadcHighSpeedGetData(gDelay ms)
Get a filled buffer from the ADC.
GSourceHandle gadcHighSpeedGetSource(void)
Turn on sending results to the GEVENT sub-system.
void gadcHighSpeedInit(gU32 physdev, gU32 frequency)
Initialise the high speed ADC.
void gadcHighSpeedStop(void)
Stop the high speed ADC conversions.
struct GEventADC_t GEventADC
The High Speed ADC event structure.
void gadcLowSpeedGet(gU32 physdev, adcsample_t *buffer)
Perform a single low speed ADC conversion.
void(* GADCISRCallbackFunction)(void)
A callback function (executed in an ISR context) for a high speed conversion.
Definition: gadc.h:91
void gadcHighSpeedStart(void)
Start the high speed ADC conversions.
A Data Buffer Queue.
Definition: gqueue.h:78
The High Speed ADC event structure.
Definition: gadc.h:59
GEventType type
The type of this event (GEVENT_ADC)
Definition: gadc.h:64
gU16 flags
The event flags.
Definition: gadc.h:70