2009-11-29 08:50:13 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @file adc.h
|
|
|
|
* @brief ADC Driver macros and structures.
|
|
|
|
*
|
2009-11-29 08:50:13 +00:00
|
|
|
* @addtogroup ADC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ADC_H_
|
|
|
|
#define _ADC_H_
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_ADC || defined(__DOXYGEN__)
|
2009-11-29 08:50:13 +00:00
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-23 13:36:25 +00:00
|
|
|
/**
|
|
|
|
* @name ADC configuration options
|
|
|
|
* @{
|
|
|
|
*/
|
2010-09-11 10:57:11 +00:00
|
|
|
/**
|
2010-10-12 15:19:15 +00:00
|
|
|
* @brief Enables synchronous APIs.
|
|
|
|
* @note Disabling this option saves both code and data space.
|
2010-09-11 10:57:11 +00:00
|
|
|
*/
|
|
|
|
#if !defined(ADC_USE_WAIT) || defined(__DOXYGEN__)
|
|
|
|
#define ADC_USE_WAIT TRUE
|
|
|
|
#endif
|
|
|
|
|
2010-10-12 15:19:15 +00:00
|
|
|
/**
|
|
|
|
* @brief Enables the @p adcAcquireBus() and @p adcReleaseBus() APIs.
|
|
|
|
* @note Disabling this option saves both code and data space.
|
|
|
|
*/
|
|
|
|
#if !defined(ADC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
|
|
|
|
#define ADC_USE_MUTUAL_EXCLUSION TRUE
|
|
|
|
#endif
|
2011-08-23 13:36:25 +00:00
|
|
|
/** @} */
|
2010-10-12 15:19:15 +00:00
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-02-26 13:29:15 +00:00
|
|
|
#if ADC_USE_MUTUAL_EXCLUSION && !CH_USE_MUTEXES && !CH_USE_SEMAPHORES
|
|
|
|
#error "ADC_USE_MUTUAL_EXCLUSION requires CH_USE_MUTEXES and/or CH_USE_SEMAPHORES"
|
2009-11-29 08:50:13 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
/**
|
2010-02-06 16:17:30 +00:00
|
|
|
* @brief Driver state machine possible states.
|
2009-11-29 08:50:13 +00:00
|
|
|
*/
|
|
|
|
typedef enum {
|
2010-10-12 15:19:15 +00:00
|
|
|
ADC_UNINIT = 0, /**< Not initialized. */
|
|
|
|
ADC_STOP = 1, /**< Stopped. */
|
|
|
|
ADC_READY = 2, /**< Ready. */
|
|
|
|
ADC_ACTIVE = 3, /**< Converting. */
|
2011-09-22 14:53:42 +00:00
|
|
|
ADC_COMPLETE = 4, /**< Conversion complete. */
|
|
|
|
ADC_ERROR = 5 /**< Conversion complete. */
|
2009-11-29 08:50:13 +00:00
|
|
|
} adcstate_t;
|
|
|
|
|
|
|
|
#include "adc_lld.h"
|
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-23 13:36:25 +00:00
|
|
|
/**
|
|
|
|
* @name Low Level driver helper macros
|
|
|
|
* @{
|
|
|
|
*/
|
2010-10-12 15:19:15 +00:00
|
|
|
#if ADC_USE_WAIT || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* @brief Resumes a thread waiting for a conversion completion.
|
2010-12-12 14:51:21 +00:00
|
|
|
*
|
2010-10-12 15:19:15 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_reset_i(adcp) { \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->thread != NULL) { \
|
|
|
|
Thread *tp = (adcp)->thread; \
|
|
|
|
(adcp)->thread = NULL; \
|
2010-10-12 15:19:15 +00:00
|
|
|
tp->p_u.rdymsg = RDY_RESET; \
|
|
|
|
chSchReadyI(tp); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resumes a thread waiting for a conversion completion.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_reset_s(adcp) { \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->thread != NULL) { \
|
|
|
|
Thread *tp = (adcp)->thread; \
|
|
|
|
(adcp)->thread = NULL; \
|
2010-10-12 15:19:15 +00:00
|
|
|
chSchWakeupS(tp, RDY_RESET); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2010-10-12 20:47:30 +00:00
|
|
|
/**
|
|
|
|
* @brief Wakes up the waiting thread.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2010-11-25 18:32:45 +00:00
|
|
|
#define _adc_wakeup_isr(adcp) { \
|
2011-11-16 21:21:28 +00:00
|
|
|
chSysLockFromIsr(); \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->thread != NULL) { \
|
2011-03-10 13:00:39 +00:00
|
|
|
Thread *tp; \
|
|
|
|
tp = (adcp)->thread; \
|
|
|
|
(adcp)->thread = NULL; \
|
2010-10-12 20:47:30 +00:00
|
|
|
tp->p_u.rdymsg = RDY_OK; \
|
|
|
|
chSchReadyI(tp); \
|
|
|
|
} \
|
2011-11-16 21:21:28 +00:00
|
|
|
chSysUnlockFromIsr(); \
|
2010-10-12 20:47:30 +00:00
|
|
|
}
|
2010-10-12 15:19:15 +00:00
|
|
|
|
2011-09-22 14:53:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Wakes up the waiting thread with a timeout message.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_timeout_isr(adcp) { \
|
2011-11-16 21:21:28 +00:00
|
|
|
chSysLockFromIsr(); \
|
2011-09-22 14:53:42 +00:00
|
|
|
if ((adcp)->thread != NULL) { \
|
|
|
|
Thread *tp; \
|
|
|
|
tp = (adcp)->thread; \
|
|
|
|
(adcp)->thread = NULL; \
|
|
|
|
tp->p_u.rdymsg = RDY_TIMEOUT; \
|
|
|
|
chSchReadyI(tp); \
|
|
|
|
} \
|
2011-11-16 21:21:28 +00:00
|
|
|
chSysUnlockFromIsr(); \
|
2011-09-22 14:53:42 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 20:47:30 +00:00
|
|
|
#else /* !ADC_USE_WAIT */
|
2010-10-12 15:19:15 +00:00
|
|
|
#define _adc_reset_i(adcp)
|
|
|
|
#define _adc_reset_s(adcp)
|
2010-11-25 18:32:45 +00:00
|
|
|
#define _adc_wakeup_isr(adcp)
|
2011-09-22 14:53:42 +00:00
|
|
|
#define _adc_timeout_isr(adcp)
|
2010-10-12 20:47:30 +00:00
|
|
|
#endif /* !ADC_USE_WAIT */
|
2010-10-12 15:19:15 +00:00
|
|
|
|
2010-10-12 20:47:30 +00:00
|
|
|
/**
|
2010-11-21 15:46:17 +00:00
|
|
|
* @brief Common ISR code, half buffer event.
|
2010-10-12 20:47:30 +00:00
|
|
|
* @details This code handles the portable part of the ISR code:
|
|
|
|
* - Callback invocation.
|
|
|
|
* .
|
|
|
|
* @note This macro is meant to be used in the low level drivers
|
|
|
|
* implementation only.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_isr_half_code(adcp) { \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->grpp->end_cb != NULL) { \
|
|
|
|
(adcp)->grpp->end_cb(adcp, (adcp)->samples, (adcp)->depth / 2); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
2010-10-12 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 20:47:30 +00:00
|
|
|
/**
|
2010-11-21 15:46:17 +00:00
|
|
|
* @brief Common ISR code, full buffer event.
|
2010-10-12 20:47:30 +00:00
|
|
|
* @details This code handles the portable part of the ISR code:
|
|
|
|
* - Callback invocation.
|
|
|
|
* - Waiting thread wakeup, if any.
|
|
|
|
* - Driver state transitions.
|
|
|
|
* .
|
|
|
|
* @note This macro is meant to be used in the low level drivers
|
|
|
|
* implementation only.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_isr_full_code(adcp) { \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->grpp->circular) { \
|
2010-10-12 20:47:30 +00:00
|
|
|
/* Callback handling.*/ \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->grpp->end_cb != NULL) { \
|
|
|
|
if ((adcp)->depth > 1) { \
|
2010-10-12 20:47:30 +00:00
|
|
|
/* Invokes the callback passing the 2nd half of the buffer.*/ \
|
2011-03-08 10:09:57 +00:00
|
|
|
size_t half = (adcp)->depth / 2; \
|
2012-06-18 08:52:26 +00:00
|
|
|
size_t half_index = half * (adcp)->grpp->num_channels; \
|
|
|
|
(adcp)->grpp->end_cb(adcp, (adcp)->samples + half_index, half); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
/* Invokes the callback passing the whole buffer.*/ \
|
2011-03-08 10:09:57 +00:00
|
|
|
(adcp)->grpp->end_cb(adcp, (adcp)->samples, (adcp)->depth); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
/* End conversion.*/ \
|
|
|
|
adc_lld_stop_conversion(adcp); \
|
2011-03-08 10:09:57 +00:00
|
|
|
if ((adcp)->grpp->end_cb != NULL) { \
|
|
|
|
(adcp)->state = ADC_COMPLETE; \
|
|
|
|
if ((adcp)->depth > 1) { \
|
2010-10-12 20:47:30 +00:00
|
|
|
/* Invokes the callback passing the 2nd half of the buffer.*/ \
|
2011-03-08 10:09:57 +00:00
|
|
|
size_t half = (adcp)->depth / 2; \
|
2012-06-18 08:52:26 +00:00
|
|
|
size_t half_index = half * (adcp)->grpp->num_channels; \
|
2013-02-17 14:04:11 +00:00
|
|
|
(adcp)->grpp->end_cb(adcp, (adcp)->samples + half_index, half); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
/* Invokes the callback passing the whole buffer.*/ \
|
2011-03-08 10:09:57 +00:00
|
|
|
(adcp)->grpp->end_cb(adcp, (adcp)->samples, (adcp)->depth); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
2013-02-17 14:00:57 +00:00
|
|
|
if ((adcp)->state == ADC_COMPLETE) { \
|
2011-03-08 10:09:57 +00:00
|
|
|
(adcp)->state = ADC_READY; \
|
2013-02-17 14:00:57 +00:00
|
|
|
(adcp)->grpp = NULL; \
|
|
|
|
} \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
2013-02-17 14:00:57 +00:00
|
|
|
else { \
|
2011-04-16 09:27:46 +00:00
|
|
|
(adcp)->state = ADC_READY; \
|
2013-02-17 14:00:57 +00:00
|
|
|
(adcp)->grpp = NULL; \
|
|
|
|
} \
|
2010-11-25 18:32:45 +00:00
|
|
|
_adc_wakeup_isr(adcp); \
|
2010-10-12 20:47:30 +00:00
|
|
|
} \
|
|
|
|
}
|
2011-09-22 14:53:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Common ISR code, error event.
|
|
|
|
* @details This code handles the portable part of the ISR code:
|
|
|
|
* - Callback invocation.
|
|
|
|
* - Waiting thread timeout signaling, if any.
|
|
|
|
* - Driver state transitions.
|
|
|
|
* .
|
|
|
|
* @note This macro is meant to be used in the low level drivers
|
|
|
|
* implementation only.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
2011-09-23 15:48:55 +00:00
|
|
|
* @param[in] err platform dependent error code
|
2011-09-22 14:53:42 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define _adc_isr_error_code(adcp, err) { \
|
|
|
|
adc_lld_stop_conversion(adcp); \
|
|
|
|
if ((adcp)->grpp->error_cb != NULL) { \
|
|
|
|
(adcp)->state = ADC_ERROR; \
|
|
|
|
(adcp)->grpp->error_cb(adcp, err); \
|
|
|
|
if ((adcp)->state == ADC_ERROR) \
|
|
|
|
(adcp)->state = ADC_READY; \
|
|
|
|
} \
|
|
|
|
(adcp)->grpp = NULL; \
|
|
|
|
_adc_timeout_isr(adcp); \
|
|
|
|
}
|
2011-08-23 13:36:25 +00:00
|
|
|
/** @} */
|
2010-10-12 15:19:15 +00:00
|
|
|
|
2009-12-29 11:12:05 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2009-11-29 08:50:13 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
void adcInit(void);
|
|
|
|
void adcObjectInit(ADCDriver *adcp);
|
|
|
|
void adcStart(ADCDriver *adcp, const ADCConfig *config);
|
|
|
|
void adcStop(ADCDriver *adcp);
|
2010-10-12 15:19:15 +00:00
|
|
|
void adcStartConversion(ADCDriver *adcp,
|
2012-03-13 15:41:46 +00:00
|
|
|
const ADCConversionGroup *grpp,
|
|
|
|
adcsample_t *samples,
|
|
|
|
size_t depth);
|
|
|
|
void adcStartConversionI(ADCDriver *adcp,
|
2010-10-12 15:19:15 +00:00
|
|
|
const ADCConversionGroup *grpp,
|
|
|
|
adcsample_t *samples,
|
|
|
|
size_t depth);
|
2009-11-29 08:50:13 +00:00
|
|
|
void adcStopConversion(ADCDriver *adcp);
|
2010-09-11 10:14:05 +00:00
|
|
|
void adcStopConversionI(ADCDriver *adcp);
|
2010-09-11 10:57:11 +00:00
|
|
|
#if ADC_USE_WAIT
|
2010-10-12 15:19:15 +00:00
|
|
|
msg_t adcConvert(ADCDriver *adcp,
|
|
|
|
const ADCConversionGroup *grpp,
|
|
|
|
adcsample_t *samples,
|
|
|
|
size_t depth);
|
2010-09-11 10:57:11 +00:00
|
|
|
#endif
|
2010-10-12 15:19:15 +00:00
|
|
|
#if ADC_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
|
|
|
|
void adcAcquireBus(ADCDriver *adcp);
|
|
|
|
void adcReleaseBus(ADCDriver *adcp);
|
|
|
|
#endif /* ADC_USE_MUTUAL_EXCLUSION */
|
2009-11-29 08:50:13 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_ADC */
|
2009-11-29 08:50:13 +00:00
|
|
|
|
|
|
|
#endif /* _ADC_H_ */
|
|
|
|
|
|
|
|
/** @} */
|