Jump to content
Sign in to follow this  
js9013

Help setting up ADC using Energia WiFi APIs

Recommended Posts

Hello,

I recently ported over an Energia sketch that had working WiFi functionality in order to help with remote logging for a project I'm working on. The WiFi works well, but now the CCS code that was handling the ADC interrupts and measuring data is no longer functioning. If possible, I would like to make minimal changes to the CCS code as it was working exactly as we desired.

I feel like there must be a relatively simple fix that I'm missing but I can't seem to figure it out. Can someone please help figure out what's wrong the ADC interrupt in the following code?

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "sysctl.h"
#include "gpio.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "driverlib/debug.h"
#include "driverlib/adc.h"
#include "inc/tm4c123gh6pm.h"
#include <string.h>
#include <WiFiClient.h>

#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>

// your network name also called SSID
char ssid[] = "";
// your network password
char password[] = "";
WiFiClient client;
IPAddress server();
uint16_t port = ;

#define SAMPLING_RATE 80000.0 //
#define TARGET_FREQUENCY 10000.0 //
#define BLOCK_SIZE 80
#define BUFFER_SIZE 255

//result cannot be declared in interrupt handler
uint8_t flag = 0;
uint32_t buffer[BUFFER_SIZE];
uint32_t bufferIndex = 0;

float goertzel(uint32_t buffer[]) {
    int k, i;

    const float floatN = (float)BLOCK_SIZE;
    float omega;
    k = (int) (0.5 + ((floatN * TARGET_FREQUENCY ) / SAMPLING_RATE));
    omega = (2.0 * 3.14 * k)/floatN;
    float sine = sin(omega);
    float cosine = cos(omega);
    float coeff = 2.0 * cosine;

    float Q0 = 0;
    float Q1 = 0;
    float Q2 = 0;
    float scaling_factor = BLOCK_SIZE/2.0;

    for (i = 0; i < BUFFER_SIZE; i++)
    {
          Q0 = coeff * Q1 - Q2 + buffer[i];
          Q2 = Q1;
          Q1 = Q0;
    }

    float imag, real, result;
    real = (Q1 - Q2 * cosine) / scaling_factor;
    imag = (Q2 * sine) / scaling_factor;

    result = sqrtf(real * real + imag * imag);

    return result;
}

void ADC3IntHandler(void)
{
   float result = 0.0;

   ADCIntClear(ADC0_BASE, 3);

    if (flag == 0)
    {
        //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_PIN_0);
        flag = 1;
    }
    else
    {
        //GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0);
        flag = 0;
    }

    if (bufferIndex >= 255)
    {
        bufferIndex = 0;
        ADCSequenceDisable(ADC0_BASE, 3);
        result = goertzel(buffer);
        client.println(result);
        ADCSequenceEnable(ADC0_BASE, 3);
    }

    ADCSequenceDataGet(ADC0_BASE, 3, (buffer + bufferIndex));
    bufferIndex++;
}

void setupADC(void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_25MHZ);

    //Enable ADC0 and timer
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

    //pin for testing
    //GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0);

    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    TimerClockSourceSet(TIMER0_BASE, TIMER_CLOCK_SYSTEM);
    //625 - 40 kHz sampling rate
    TimerLoadSet(TIMER0_BASE, TIMER_B, 625);
    TimerControlTrigger(TIMER0_BASE, TIMER_B, true);

    //Disable before configuring
    ADCSequenceDisable(ADC0_BASE, 3);
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);

    //sequencer 3 on PE3
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);

    //PE3 ADC pin
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);

    IntRegister(INT_ADC0SS3, ADC3IntHandler);
    ADCIntRegister(ADC0_BASE, 3, ADC3IntHandler);
    IntMasterEnable();
    IntEnable(INT_ADC0SS3);
    ADCIntEnable(ADC0_BASE, 3);
    ADCSequenceEnable(ADC0_BASE, 3);
    SysCtlDelay(10);
    TimerEnable(TIMER0_BASE, TIMER_B); //Timer A conflicts with SPI of wifi module

    ADCIntClear(ADC0_BASE, 3);
}

void setup() {
  setupADC();

  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    delay(300);
  }

  while (WiFi.localIP() == INADDR_NONE) {
    delay(300);
  }

  uint8_t tries = 0;
  while (client.connect(server, port) == false) {
    if (tries++ > 100) {
      while(1);
    }
    delay(100);
  }
  delay(1000);
}
void loop()
{
      //client.println("hello");
}

 


Thank you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×