15 #if GADC_MAX_HIGH_SPEED_SAMPLERATE > GADC_MAX_SAMPLE_FREQUENCY/2
16 #error "GADC: GADC_MAX_HIGH_SPEED_SAMPLERATE has been set too high. It must be less than half the maximum CPU rate"
19 #define GADC_HSADC_GTIMER 0x8000
20 #define GADC_ADC_RUNNING 0x4000
21 #define GADC_HSADC_CONVERTION 0x2000
23 typedef struct NonTimerData_t {
33 static volatile gU16 hsFlags;
34 static gMemSize hsBytesPerConv;
37 static gfxQueueGSync hsListDone;
44 static gfxQueueGSync lsListToDo;
45 static gfxQueueGSync lsListDone;
46 static NonTimerData *lsData;
49 if ((hsFlags & GADC_HSADC_CONVERTION)) {
55 if (n && hsJob.done < hsJob.todo)
65 gfxBufferReleaseI(hsData);
70 hsData->len = hsJob.done * hsBytesPerConv;
78 if (hsFlags & GADC_HSADC_GTIMER)
87 }
else if (!(hsData = gfxBufferGetI())) {
98 #if GFX_USE_OS_CHIBIOS
100 hsJob.todo = (hsData->size / hsBytesPerConv) & ~1;
102 hsJob.todo = hsData->size / hsBytesPerConv;
105 hsJob.buffer = (adcsample_t *)(hsData+1);
109 if ((lsData = (NonTimerData *)gfxQueueGSyncGetI(&lsListToDo))) {
110 hsFlags &= ~GADC_HSADC_CONVERTION;
113 hsFlags |= GADC_HSADC_CONVERTION;
116 hsFlags &= ~GADC_ADC_RUNNING;
129 if (lsData->callback) {
142 hsFlags |= GADC_HSADC_CONVERTION;
144 }
else if ((lsData = (NonTimerData *)gfxQueueGSyncGetI(&lsListToDo))) {
145 hsFlags &= ~GADC_HSADC_CONVERTION;
148 hsFlags &= ~GADC_ADC_RUNNING;
157 gfxQueueGSyncInit(&hsListDone);
162 gfxQueueGSyncInit(&lsListToDo);
163 gfxQueueGSyncInit(&lsListDone);
166 void _gadcDeinit(
void)
171 gfxQueueGSyncDeinit(&hsListDone);
176 gfxQueueGSyncDeinit(&lsListToDo);
177 gfxQueueGSyncDeinit(&lsListDone);
181 static void HighSpeedGTimerCallback(
void *param) {
183 GSourceListener *psl;
194 pe->
type = GEVENT_ADC;
208 hsJob.physdev = physdev;
209 hsJob.frequency = frequency;
217 gtimerStart(&hsGTimer, HighSpeedGTimerCallback, 0, gTrue, gDelayForever);
218 hsFlags |= GADC_HSADC_GTIMER;
219 return (GSourceHandle)&hsGTimer;
224 hsISRcallback = isrfn;
228 return (
GDataBuffer *)gfxQueueGSyncGet(&hsListDone, ms);
232 return (
GDataBuffer *)gfxQueueGSyncGetI(&hsListDone);
237 if (!hsJob.frequency || !hsBytesPerConv)
242 if (!(hsData = gfxBufferGetI())) {
246 if (hsFlags & GADC_HSADC_GTIMER)
253 #if GFX_USE_OS_CHIBIOS
255 hsJob.todo = (hsData->size / hsBytesPerConv) & ~1;
257 hsJob.todo = hsData->size / hsBytesPerConv;
260 hsJob.buffer = (adcsample_t *)(hsData+1);
267 if (!(hsFlags & GADC_ADC_RUNNING)) {
268 hsFlags |= (GADC_HSADC_CONVERTION|GADC_ADC_RUNNING);
279 while ((hsFlags & GADC_HSADC_CONVERTION))
283 static void LowSpeedGTimerCallback(
void *param) {
288 while ((pdata = (NonTimerData *)gfxQueueGSyncGet(&lsListDone, gDelayNone))) {
289 pdata->callback(pdata->job.buffer, pdata->param);
299 ndata.job.physdev = physdev;
300 ndata.job.buffer = buffer;
305 if (!(hsFlags & GADC_ADC_RUNNING)) {
308 hsFlags |= GADC_ADC_RUNNING;
309 hsFlags &= ~GADC_HSADC_CONVERTION;
327 gtimerStart(&lsGTimer, LowSpeedGTimerCallback, 0, gTrue, gDelayForever);
330 if (!(pdata =
gfxAlloc(
sizeof(NonTimerData))))
332 pdata->job.physdev = physdev;
333 pdata->job.buffer = buffer;
334 pdata->callback = fn;
335 pdata->param = param;
339 if (!(hsFlags & GADC_ADC_RUNNING)) {
342 hsFlags |= GADC_ADC_RUNNING;
343 hsFlags &= ~GADC_HSADC_CONVERTION;
#define GADC_HSADC_LOSTEVENT
The event flag values.
#define GADC_HSADC_RUNNING
The High Speed ADC is currently running.
#define GADC_HSADC_GOTBUFFER
A buffer is ready for processing.
#define GADC_HSADC_STALL
The High Speed ADC has stalled due to no free buffers.
GADC - Periodic ADC driver header file.
void gadc_lld_stop_timerI(void)
Stop the periodic timer for high frequency conversions.
void gadc_lld_start_timerI(gU32 freq)
Start a periodic timer for high frequency conversions.
void gadcGotDataI(gMemSize n)
These routines are the callbacks that the driver uses.
void gadc_lld_nontimerjobI(GadcNonTimerJob *pjob)
Start a non-timer conversion.
void gadc_lld_init(void)
Initialise the driver.
void gadc_lld_timerjobI(GadcTimerJob *pjob)
Start a set of high frequency conversions.
gMemSize gadc_lld_samplesperconversion(gU32 physdev)
Using the hardware dependant "physdev", return the number of samples for each conversion.
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.
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.
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.
void gadcHighSpeedStart(void)
Start the high speed ADC conversions.
GEvent * geventGetEventBuffer(GSourceListener *psl)
Get the event buffer from the GSourceListener.
void geventSendEvent(GSourceListener *psl)
Called by a source to indicate the listener's event buffer has been filled.
GSourceListener * geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr)
Called by a source with a possible event to get a listener record.
void gfxYield(void)
Yield the current thread.
gBool gfxSemWait(gSem *psem, gDelay ms)
Wait on a semaphore.
void gfxSystemUnlock(void)
Unlock the operating system previous locked by gfxSystemLock()
void * gfxAlloc(gMemSize sz)
Allocate memory.
void gfxSystemLock(void)
Lock the operating system to protect a sequence of code.
void gfxSemDestroy(gSem *psem)
Destroy a Counted Semaphore.
void gfxSemInit(gSem *psem, gSemcount val, gSemcount limit)
Initialise a Counted Semaphore.
void gfxFree(void *ptr)
Free memory.
void gfxSemSignalI(gSem *psem)
Signal a semaphore.
void gtimerJabI(GTimer *pt)
Jab a timer causing the current period to immediate expire.
void gtimerStart(GTimer *pt, GTimerFunction fn, void *param, gBool periodic, gDelay millisec)
Set a timer going or alter its properties if it is already going.
void gtimerInit(GTimer *pt)
Initialise a timer.
void gtimerDeinit(GTimer *pt)
Deinitialise a timer.
gBool gtimerIsActive(GTimer *pt)
Test if a timer is currently active.
The High Speed ADC event structure.
GEventType type
The type of this event (GEVENT_ADC)
gU16 flags
The event flags.
The structure passed to do a single conversion.
The structure passed to start a timer conversion.