/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012 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 .
*/
/**
* @file adc.c
* @brief ADC Driver code.
*
* @addtogroup ADC
* @{
*/
#include "ch.h"
#include "hal.h"
#if HAL_USE_ADC || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief ADC Driver initialization.
* @note This function is implicitly invoked by @p halInit(), there is
* no need to explicitly initialize the driver.
*
* @init
*/
void adcInit(void) {
adc_lld_init();
}
/**
* @brief Initializes the standard part of a @p ADCDriver structure.
*
* @param[out] adcp pointer to the @p ADCDriver object
*
* @init
*/
void adcObjectInit(ADCDriver *adcp) {
adcp->state = ADC_STOP;
adcp->config = NULL;
adcp->samples = NULL;
adcp->depth = 0;
adcp->grpp = NULL;
#if ADC_USE_WAIT
adcp->thread = NULL;
#endif /* ADC_USE_WAIT */
#if ADC_USE_MUTUAL_EXCLUSION
#if CH_USE_MUTEXES
chMtxInit(&adcp->mutex);
#else
chSemInit(&adcp->semaphore, 1);
#endif
#endif /* ADC_USE_MUTUAL_EXCLUSION */
#if defined(ADC_DRIVER_EXT_INIT_HOOK)
ADC_DRIVER_EXT_INIT_HOOK(adcp);
#endif
}
/**
* @brief Configures and activates the ADC peripheral.
*
* @param[in] adcp pointer to the @p ADCDriver object
* @param[in] config pointer to the @p ADCConfig object. Depending on
* the implementation the value can be @p NULL.
*
* @api
*/
void adcStart(ADCDriver *adcp, const ADCConfig *config) {
chDbgCheck(adcp != NULL, "adcStart");
chSysLock();
chDbgAssert((adcp->state == ADC_STOP) || (adcp->state == ADC_READY),
"adcStart(), #1", "invalid state");
adcp->config = config;
adc_lld_start(adcp);
adcp->state = ADC_READY;
chSysUnlock();
}
/**
* @brief Deactivates the ADC peripheral.
*
* @param[in] adcp pointer to the @p ADCDriver object
*
* @api
*/
void adcStop(ADCDriver *adcp) {
chDbgCheck(adcp != NULL, "adcStop");
chSysLock();
chDbgAssert((adcp->state == ADC_STOP) || (adcp->state == ADC_READY),
"adcStop(), #1", "invalid state");
adc_lld_stop(adcp);
adcp->state = ADC_STOP;
chSysUnlock();
}
/**
* @brief Starts an ADC conversion.
* @details Starts an asynchronous conversion operation.
* @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.
*
* @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.
*
* @api
*/
void adcStartConversion(ADCDriver *adcp,
const ADCConversionGroup *grpp,
adcsample_t *samples,
size_t depth) {
chSysLock();
adcStartConversionI(adcp, grpp, samples, depth);
chSysUnlock();
}
/**
* @brief Starts an ADC conversion.
* @details Starts an asynchronous conversion operation.
* @post The callbacks associated to the conversion group will be invoked
* on buffer fill and error events.
* @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.
*
* @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.
*
* @iclass
*/
void adcStartConversionI(ADCDriver *adcp,
const ADCConversionGroup *
#include <stdio.h>
#include <assert.h>
#include "test00_uut.c"
uint32_t xorshift32()
{
static uint32_t x32 = 314159265;
x32 ^= x32 << 13;
x32 ^= x32 >> 17;
x32 ^= x32 << 5;
return x32;
}
int main()
{
struct test_state_t state;
uint32_t a, b, c, x, y, z, w;
bool first_eval = true;
for (int i = 0; i < 10; i++)
{
a = xorshift32();
b = xorshift32();
c = xorshift32();
x = (a & b) | c;
y = a & (b | c);
z = a ^ b ^ c;
w = z;
state.a.value_7_0 = a;
state.a.value_15_8 = a >> 8;
state.a.value_23_16 = a >> 16;
state.a.value_31_24 = a >> 24;
state.b.value_7_0 = b;
state.b.value_15_8 = b >> 8;
state.b.value_23_16 = b >> 16;
state.b.value_31_24 = b >> 24;
state.c.value_7_0 = c;
state.c.value_15_8 = c >> 8;
state.c.value_23_16 = c >> 16;
state.c.value_31_24 = c >> 24;
if (first_eval) {
first_eval = false;
test_init(&state);
} else {
test_eval(&state);
}
uint32_t uut_x = 0;
uut_x |= (uint32_t)state.x.value_7_0;
uut_x |= (uint32_t)state.x.value_15_8 << 8;
uut_x |= (uint32_t)state.x.value_23_16 << 16;
uut_x |= (uint32_t)state.x.value_31_24 << 24;
uint32_t uut_y = 0;
uut_y |= (uint32_t)state.y.value_7_0;
uut_y |= (uint32_t)state.y.value_15_8 << 8;
uut_y |= (uint32_t)state.y.value_23_16 << 16;
uut_y |= (uint32_t)state.y.value_31_24 << 24;
uint32_t uut_z = 0;
uut_z |= (uint32_t)state.z.value_7_0;
uut_z |= (uint32_t)state.z.value_15_8 << 8;
uut_z |= (uint32_t)state.z.value_23_16 << 16;
uut_z |= (uint32_t)state.z.value_31_24 << 24;
uint32_t uut_w = 0;
uut_w |= (uint32_t)state.w.value_7_0;
uut_w |= (uint32_t)state.w.value_15_8 << 8;
uut_w |= (uint32_t)state.w.value_23_16 << 16;
uut_w |= (uint32_t)state.w.value_31_24 << 24;
printf("---\n");
printf("A: 0x%08x\n", a);
printf("B: 0x%08x\n", b);
printf("C: 0x%08x\n", c);
printf("X: 0x%08x 0x%08x\n", x, uut_x);
printf("Y: 0x%08x 0x%08x\n", y, uut_y);
printf("Z: 0x%08x 0x%08x\n", z, uut_z);
printf("W: 0x%08x 0x%08x\n", w, uut_w);
assert(x == uut_x);
assert(y == uut_y);
assert(z == uut_z);
assert(w == uut_w);
}
return 0;
}