2009-11-05 09:53:48 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file adc.c
|
|
|
|
* @brief ADC Driver code.
|
|
|
|
* @addtogroup ADC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ch.h>
|
|
|
|
#include <adc.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ADC Driver initialization.
|
|
|
|
*/
|
|
|
|
void adcInit(void) {
|
|
|
|
|
|
|
|
adc_lld_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes the standard part of a @p ADCDriver structure.
|
|
|
|
*
|
2009-11-07 10:29:32 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
2009-11-05 09:53:48 +00:00
|
|
|
*/
|
|
|
|
void adcObjectInit(ADCDriver *adcp) {
|
|
|
|
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_state = ADC_STOP;
|
|
|
|
adcp->ad_config = NULL;
|
|
|
|
chSemInit(&adcp->ad_sem, 0);
|
2009-11-05 09:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the ADC peripheral.
|
|
|
|
*
|
2009-11-07 10:29:32 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
* @param[in] config pointer to the @p ADCConfig object
|
2009-11-05 09:53:48 +00:00
|
|
|
*/
|
|
|
|
void adcStart(ADCDriver *adcp, const ADCConfig *config) {
|
|
|
|
|
|
|
|
chDbgCheck((adcp != NULL) && (config != NULL), "adcStart");
|
2009-11-07 10:29:32 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2009-11-16 16:39:22 +00:00
|
|
|
chDbgAssert((adcp->ad_state == ADC_STOP) || (adcp->ad_state == ADC_READY),
|
2009-11-05 09:53:48 +00:00
|
|
|
"adcStart(), #1",
|
|
|
|
"invalid state");
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_config = config;
|
2009-11-05 09:53:48 +00:00
|
|
|
adc_lld_start(adcp);
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_state = ADC_READY;
|
2009-11-07 10:29:32 +00:00
|
|
|
chSysUnlock();
|
2009-11-05 09:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deactivates the ADC peripheral.
|
|
|
|
*
|
2009-11-07 10:29:32 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
2009-11-05 09:53:48 +00:00
|
|
|
*/
|
|
|
|
void adcStop(ADCDriver *adcp) {
|
|
|
|
|
|
|
|
chDbgCheck(adcp != NULL, "adcStop");
|
2009-11-07 10:29:32 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2009-11-16 16:39:22 +00:00
|
|
|
chDbgAssert((adcp->ad_state == ADC_STOP) || (adcp->ad_state == ADC_READY),
|
2009-11-05 09:53:48 +00:00
|
|
|
"adcStop(), #1",
|
|
|
|
"invalid state");
|
|
|
|
adc_lld_stop(adcp);
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_state = ADC_STOP;
|
2009-11-07 10:29:32 +00:00
|
|
|
chSysUnlock();
|
2009-11-05 09:53:48 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 19:55:58 +00:00
|
|
|
/**
|
|
|
|
* @brief Starts an ADC conversion.
|
2009-11-07 10:29:32 +00:00
|
|
|
* @details Starts a conversion operation, there are two kind of conversion
|
|
|
|
* modes:
|
|
|
|
* - <b>LINEAR</b>, this mode is activated when the @p callback
|
|
|
|
* parameter is set to @p NULL, in this mode the buffer is filled
|
|
|
|
* once and then the conversion stops automatically.
|
|
|
|
* - <b>CIRCULAR</b>, when a callback function is defined the
|
|
|
|
* conversion never stops and the buffer is filled circularly.
|
|
|
|
* During the conversion the callback function is invoked when
|
|
|
|
* the buffer is 50% filled and when the buffer is 100% filled,
|
|
|
|
* this way is possible to process the conversion stream in real
|
|
|
|
* time. This kind of conversion can only be stopped by explicitly
|
|
|
|
* invoking @p adcStopConversion().
|
|
|
|
* .
|
2009-11-05 19:55:58 +00:00
|
|
|
*
|
2009-11-07 10:29:32 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
* @param[in] grpp pointer to a @p ADCConversionGroup object
|
|
|
|
* @param[out] samples pointer to the samples buffer
|
|
|
|
* @param[in] depth buffer depth (matrix rows number). The buffer depth
|
|
|
|
* must be one or an even number.
|
|
|
|
* @param[in] callback pointer to the conversion callback function
|
2009-11-05 19:55:58 +00:00
|
|
|
* @return The operation status.
|
2009-11-07 10:29:32 +00:00
|
|
|
* @retval FALSE the conversion has been started.
|
|
|
|
* @retval TRUE the driver is busy, conversion not started.
|
2009-11-06 16:33:03 +00:00
|
|
|
*
|
|
|
|
* @note The buffer is organized as a matrix of M*N elements where M is the
|
|
|
|
* channels number configured into the conversion group and N is the
|
|
|
|
* buffer depth. The samples are sequentially written into the buffer
|
|
|
|
* with no gaps.
|
2009-11-05 19:55:58 +00:00
|
|
|
*/
|
|
|
|
bool_t adcStartConversion(ADCDriver *adcp,
|
|
|
|
ADCConversionGroup *grpp,
|
2009-11-06 16:33:03 +00:00
|
|
|
void *samples,
|
2009-11-07 10:29:32 +00:00
|
|
|
size_t depth,
|
|
|
|
adccallback_t callback) {
|
2009-11-05 19:55:58 +00:00
|
|
|
|
2009-11-06 16:33:03 +00:00
|
|
|
chDbgCheck((adcp != NULL) && (grpp != NULL) && (samples != NULL) &&
|
|
|
|
((depth == 1) || ((depth & 1) == 0)),
|
2009-11-05 19:55:58 +00:00
|
|
|
"adcStartConversion");
|
|
|
|
|
|
|
|
chSysLock();
|
2009-11-16 16:39:22 +00:00
|
|
|
chDbgAssert((adcp->ad_state == ADC_READY) ||
|
|
|
|
(adcp->ad_state == ADC_RUNNING),
|
2009-11-05 19:55:58 +00:00
|
|
|
"adcStartConversion(), #1",
|
|
|
|
"invalid state");
|
2009-11-16 16:39:22 +00:00
|
|
|
if (adcp->ad_state == ADC_RUNNING) {
|
2009-11-05 19:55:58 +00:00
|
|
|
chSysUnlock();
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-11-07 10:29:32 +00:00
|
|
|
adc_lld_start_conversion(adcp, grpp, samples, depth, callback);
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_state = ADC_RUNNING;
|
2009-11-05 19:55:58 +00:00
|
|
|
chSysUnlock();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stops an ongoing conversion.
|
|
|
|
*
|
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
*/
|
|
|
|
void adcStopConversion(ADCDriver *adcp) {
|
|
|
|
|
|
|
|
chDbgCheck(adcp != NULL, "adcStopConversion");
|
|
|
|
|
|
|
|
chSysLock();
|
2009-11-16 16:39:22 +00:00
|
|
|
chDbgAssert((adcp->ad_state == ADC_READY) ||
|
|
|
|
(adcp->ad_state == ADC_RUNNING),
|
2009-11-05 19:55:58 +00:00
|
|
|
"adcStopConversion(), #1",
|
|
|
|
"invalid state");
|
2009-11-05 20:57:24 +00:00
|
|
|
adc_lld_stop_conversion(adcp);
|
2009-11-16 16:39:22 +00:00
|
|
|
adcp->ad_state = ADC_READY;
|
2009-11-05 19:55:58 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Waits for completion.
|
|
|
|
*
|
2009-11-07 10:29:32 +00:00
|
|
|
* @param[in] adcp pointer to the @p ADCDriver object
|
|
|
|
* @param[in] timeout the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
2009-11-05 19:55:58 +00:00
|
|
|
* @return The operation result.
|
2009-11-06 16:33:03 +00:00
|
|
|
* @retval RDY_OK conversion finished (or not started).
|
2009-11-05 19:55:58 +00:00
|
|
|
* @retval RDY_TIMEOUT conversion not finished within the specified time.
|
|
|
|
*/
|
2009-11-07 10:29:32 +00:00
|
|
|
msg_t adcWaitConversion(ADCDriver *adcp, systime_t timeout) {
|
2009-11-05 19:55:58 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2009-11-16 16:39:22 +00:00
|
|
|
chDbgAssert((adcp->ad_state == ADC_READY) ||
|
|
|
|
(adcp->ad_state == ADC_RUNNING),
|
2009-11-05 19:55:58 +00:00
|
|
|
"adcWaitConversion(), #1",
|
|
|
|
"invalid state");
|
2009-11-16 16:39:22 +00:00
|
|
|
if (adcp->ad_state == ADC_RUNNING) {
|
|
|
|
if (chSemWaitTimeoutS(&adcp->ad_sem, timeout) == RDY_TIMEOUT) {
|
2009-11-06 16:33:03 +00:00
|
|
|
chSysUnlock();
|
|
|
|
return RDY_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 19:55:58 +00:00
|
|
|
chSysUnlock();
|
2009-11-06 16:33:03 +00:00
|
|
|
return RDY_OK;
|
2009-11-05 19:55:58 +00:00
|
|
|
}
|
|
|
|
|
2009-11-05 09:53:48 +00:00
|
|
|
/** @} */
|