Skip to content

Instantly share code, notes, and snippets.

@linux4life798
Created September 12, 2018 00:17
Show Gist options
  • Save linux4life798/04d41dce2b7e14d0dad5bc8f5c9b4132 to your computer and use it in GitHub Desktop.
Save linux4life798/04d41dce2b7e14d0dad5bc8f5c9b4132 to your computer and use it in GitHub Desktop.
The somewhat broken CC2650 drivelib ADC with GPTimer
/*
* sample_noise.c
*
* Created on: Sep 10, 2018
* Author: Craig Hesling
*/
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h> // BIOS_WAIT_FOREVER
#include <ti/sysbios/knl/Event.h>
/* TI-RTOS drivers */
#include <ti/drivers/ADC.h>
#include <ti/drivers/adc/ADCCC26XX.h>
#include <ti/drivers/PIN.h>
#include <ti/drivers/pin/PINCC26XX.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26XX.h>
//#include <ti/drivers/timer/GPTimerCC26XX.h>
#include <ti/drivers/ADCBuf.h>
/* driverlib header files */
#include <inc/hw_memmap.h>
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_aux_evctl.h>
#include <driverlib/aux_adc.h>
#include <driverlib/aux_wuc.h>
#include <driverlib/aux_smph.h>
#include <driverlib/sys_ctrl.h>
#include <driverlib/ioc.h>
#include <driverlib/aon_ioc.h>
#include <driverlib/timer.h>
#include <driverlib/event.h>
#include <PERIPHERALS.h>
#include <io.h>
#include "sensors.h"
#include <math.h>
extern const ADCCC26XX_HWAttrs adcCC26xxHWAttrs[LORABUG_ADCCOUNT];
static inline
int64_t int_square(int64_t x) {
return x*x;
}
// From https://gist.github.com/foobaz/3287f153d125277eefea
static inline
uint32_t int_sqrt64(uint64_t x) // 780 µs
{
uint32_t res = 0;
uint32_t add = 0x80000000;
uint8_t i;
for (i = 0; i < 32; i++)
{
uint32_t temp = res | add;
uint64_t g2 = temp;
g2 *= temp;
if (x >= g2)
{
res = temp;
}
add >>= 1;
}
return res;
}
uint32_t sampleNoiseOld(void) {
// GPTimerCC26XX_Params timerParams;
// GPTimerCC26XX_Handle timerHandle;
// GPTimerCC26XX_Params_init(&timerParams);
//
//// timerParams.mode = GPT_MODE_PERIODIC_UP;
//// timerParams.width = GPT_CONFIG_16BIT;
//
// timerHandle = GPTimerCC26XX_open(LORABUG_GPTIMER2A, &timerParams);
// if (timerHandle == NULL) {
// debugprintf("Failed to open GPTimer\r\n");
// }
//// GPTimerCC26XX_setLoadValue(timerHandle, 5);
//// GPTimerCC26XX_setMatchValue(timerHandle, 10);
// GPTimerCC26XX_start(timerHandle);
/* Register power dependency - i.e. power up and enable clock for GPTimer.
If both GPT_A and GPT_B is used then two dependencies will be set on GPTimer */
Power_setDependency(PowerCC26XX_PERIPH_GPT0);
uint32_t gptBase = GPT0_BASE;
TimerConfigure(gptBase, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PERIODIC);
TimerPrescaleSet(gptBase, TIMER_A, 255);
TimerLoadSet(gptBase, TIMER_A, 65000);
TimerEnable(gptBase, TIMER_A);
// TimerIntEnable(gptBase, TIMER_TIMA_TIMEOUT);
debugprintf("Value = %d\r\n", TimerValueGet(gptBase, TIMER_A));
ADCCC26XX_HWAttrs const *hwAttrs = &adcCC26xxHWAttrs[ADC_INDEX_MIC];
/* The following two clock enabled are from ADC_open.
* The close does not seem to disable them. */
/* Turn on the ANAIF clock. ANIAF contains the aux ADC. */
AUXWUCClockEnable(AUX_WUC_ANAIF_CLOCK);
AUXWUCClockEnable(AUX_WUC_ADI_CLOCK);
/* Set constraints to guarantee operation */
Power_setConstraint(PowerCC26XX_SB_DISALLOW);
/* Acquire the ADC hw semaphore. This call polls if the semaphore is available */
if(!AUXSMPHTryAcquire(AUX_SMPH_2)){
Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);
debugprintf("Failed to get hardware semaphore\r\n");
return 0;
}
/* Specify input in ADC module */
AUXADCSelectInput(hwAttrs->adcCompBInput);
/* Flush the ADC FIFO in case we have triggered prior to this call */
AUXADCFlushFifo();
/* Use synchronous sampling mode and prepare for trigger */
// AUXADCEnableSync(hwAttrs->refSource, hwAttrs->samplingDuration, hwAttrs->triggerSource);
// AUXADCEnableAsync(hwAttrs->refSource, hwAttrs->triggerSource);
AUXADCEnableAsync(hwAttrs->refSource, AUXADC_TRIGGER_GPT0A);
// EventRegister(EVENT_O_AUXSEL0, EVENT_AUXSEL0_EV_GPT0A);
uint32_t adcGain = AUXADCGetAdjustmentGain(hwAttrs->refSource);
uint32_t adcOffset = AUXADCGetAdjustmentOffset(hwAttrs->refSource);
uint32_t adcValue;
uint32_t minV = UINT32_MAX, maxV = 0;
uint64_t sampleSum = 0;
uint64_t squaredSum = 0;
int count;
// AUXADCGenManualTrigger();
for (count = 0; count < MIC_SAMPLES; count++) {
debugprintf("Value = %d\r\n", TimerValueGet(gptBase, TIMER_A));
/* Manually trigger the ADC once */
// AUXADCGenManualTrigger();
/* Poll until the sample is ready */
adcValue = AUXADCReadFifo();
adcValue = AUXADCAdjustValueForGainAndOffset(adcValue, adcGain, adcOffset);
uint32_t uVolts = AUXADCValueToMicrovolts((hwAttrs->inputScalingEnabled ? AUXADC_FIXED_REF_VOLTAGE_NORMAL : AUXADC_FIXED_REF_VOLTAGE_UNSCALED), adcValue);
sampleSum += (uint64_t)uVolts;
// squaredSum += int_square((int64_t)uVolts - (int64_t)lastMICAvg);
squaredSum += int_square((int64_t)uVolts - (int64_t)900000);
if(maxV < uVolts) maxV = uVolts;
if(minV > uVolts) minV = uVolts;
debugprintf("Value = %d\r\n", TimerValueGet(gptBase, TIMER_A));
debugprintf(".\r\n");
}
uartputs("");
/* Shut down the ADC peripheral */
AUXADCDisable();
/* Release the ADC hw semaphore */
AUXSMPHRelease(AUX_SMPH_2);
/* Allow entering standby again after ADC conversion complete */
Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);
// GPTimerCC26XX_close(timerHandle);
TimerDisable(gptBase, TIMER_A);
Power_releaseDependency(PowerCC26XX_PERIPH_GPT2);
uint32_t avg = sampleSum/MIC_SAMPLES;
uint64_t variance = squaredSum/MIC_SAMPLES;
uint32_t stddev = int_sqrt64(variance);
uartprintf("MIC min: %d\r\n" , minV);
uartprintf("MIC max: %d\r\n", maxV);
uartprintf("MIC max-min: %d\r\n", maxV-minV);
uartprintf("MIC avg: %d in %d samples\r\n", avg, count);
uartprintf("MIC avg: %f in %d samples\r\n", (((double)sampleSum)/(double)MIC_SAMPLES)/1000.0/1000.0, count);
uartprintf("MIC squaredsum: %lld\r\n", squaredSum);
uartprintf("MIC variance: %lld\r\n", variance);
uartprintf("MIC stddev: %ld\r\n", stddev);
uartprintf("MIC stddev: %f\r\n", sqrtf((float)variance));
uartprintf("MIC stddev: %f\r\n", (sqrt((double)variance) / (double)4096)*4.3);
uartputs("");
// lastMICAvg = avg;
return (uint16_t)stddev;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment