Part Number: TM4C123GH6PM
I have been trying to program ADC1 with sequence 3, triggered by Timer. The code is working for sequence 0 (number of samples 8, depth of FIFO), however, when I am trying to change it to sequence 3, it never got to work. I tried to debug, set breakpoint, for breakpoint, I can sample 64 data, however, when I am trying to run full-speed, it never works. Here is the code with SS3. I'm also using FreeRTOS.
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/timer.h"
#include "priorities.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "st7735.h"
#include "adcbatch_task.h"
#include "mic_commands.h"
#include "rgbled.h"
#include "lcdgatekeeper.h"
typedef struct {
int16_t real,imag;
} Complex_t;
extern void unDec2BCD(uint32_t Hex, uint8_t *Dec,uint8_t NDigits);
extern void cr4_fft_64_stm32(Complex_t *fftresult,Complex_t *data, uint32_t numberofPoints);
extern uint32_t g_ui32SysClock;
static uint32_t * Buffer;
//uint32_t bufferbank=0;
static uint32_t adcbuff0[64],adcbuff1[64]; // two buffer to store sampled 64 points
static uint32_t bufferbank = 0; // current ADC 64-point sampling memory bank either adcbuff0 or adcbuff1
//extern xSemaphoreHandle g_pLCDSemaphore;
extern xQueueHandle g_pLCDQueue;
#define ADCBATCHTASKSTACKSIZE 1024 // Stack size in words
volatile static uint32_t time1;
volatile static uint32_t time2;
volatile static uint32_t fftlatency;
extern void unDec2BCD(uint32_t Hex, uint8_t *Dec,uint8_t NDigits);
extern uint32_t displaymode;
// Newton's method
// s is an integer
// sqrt(s) is an integer
uint32_t sqrtint(uint32_t s){
uint32_t t; // t*t will become s
int n; // loop counter to make sure it stops running
t = s/10+1; // initial guess
for(n = 16; n; --n){ // guaranteed to finish
t = ((t*t+s)/t)/2;
}
return t;
}
/* store the handle of the ADCBATCH_TASK to which interrupt processing is deferred and can be referred */
TaskHandle_t xADCBatchTaskHandler = NULL;
void ADC1Seq3_Handler(void){
static uint32_t idx=0;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Clear flag
ADCIntClear(ADC1_BASE, 0);
ADCSequenceDataGet(ADC1_BASE, 3, (uint32_t *)Buffer);
Buffer +=1;
idx++;
if(idx==8)
{
idx=0;
// switch to memory buffer 2 for ADC sampling
if(bufferbank ==0)
{
bufferbank = 1;
Buffer = adcbuff1;
}
else
{
bufferbank = 0;
Buffer = adcbuff0;
}
// PF3 ^=(1u<<3);
// time1 = TimerValueGet(TIMER3_BASE,TIMER_A);
// xSemaphoreGiveFromISR(g_pADCBATCHSemaphore,NULL);
vTaskNotifyGiveFromISR(xADCBatchTaskHandler,&xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
}
#define BANK0 0x00
#define BANK1 0x01
#define TRUE 1
#define FALSE 0
//*****************************************************************************
//
// This task reads 64 points sampled value and performed FFT.
//
//*****************************************************************************
void
ADCBatchTask(void *pvParameters)
{
Complex_t x[64],y[64];
int16_t *pdata;
int16_t *pbkdata;
int32_t real,imag;
uint32_t mag[32]; // magnitude of FFT power spectrum
uint8_t i;
uint32_t maxmag;
uint32_t display = TRUE;
struct LCDItem LCDMessage;
//
// Loop forever.
//
while(1)
{
// xSemaphoreTake(g_pADCBATCHSemaphore, portMAX_DELAY);
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
// waiting for the semaphore to be posted ADC 64-point sampling
if(bufferbank ==1) // last memory bank switch in ADC
{
pdata = (int16_t *)adcbuff0;
}
else
{
pdata = (int16_t *)adcbuff1;
}
pbkdata = pdata; // save a copy of the pdata
for(i=0; i<64; i++)
{
x[i].real = (*pbkdata)-2048; // remove DC bias 1/2 3.3V
x[i].imag = 0;
pbkdata = pbkdata+2; // adcbuff0 32-bit, pdata is 16-bit
}
// call 64-point FFT calculation
cr4_fft_64_stm32(y,x,64);
// time2 = TimerValueGet(TIMER3_BASE,TIMER_A);
// fftlatency = time2 - time1; // FFT latency from the completion of 64-point to FFT calculation finish
PF3 ^=(1u<<3);
for(i=0; i<32; i++)
{
real = y[i].real;
imag = y[i].imag;
mag[i] = sqrtint(real*real+imag*imag);
}
// find max magnitude maxmag
maxmag = mag[0];
for(i=1; i<32; i++)
{
if(mag[i]>maxmag)
{
maxmag = mag[i];
}
}
if(display==TRUE) {
LCDMessage.lcdcmd = 2;
LCDMessage.dxmin=0; LCDMessage.dxmax = 63; LCDMessage.dymin=0; LCDMessage.dymax=4095;
LCDMessage.sxmin=0; LCDMessage.sxmax = 127; LCDMessage.symin=0; LCDMessage.symax = 79;
LCDMessage.dxy = (uint32_t *)pdata;
xQueueSend(g_pLCDQueue, &LCDMessage, portMAX_DELAY);
LCDMessage.lcdcmd = 4;
LCDMessage.dxmin=0; LCDMessage.dxmax = 31; LCDMessage.dymin=0; LCDMessage.dymax=maxmag; // 2^32-1 for fft
LCDMessage.sxmin=0; LCDMessage.sxmax = 127; LCDMessage.symin=90; LCDMessage.symax = 159;
LCDMessage.dxy = mag;
xQueueSend(g_pLCDQueue, &LCDMessage, portMAX_DELAY);
}
if(displaymode == ONESHOT) {
display = FALSE;
}
else
{
display = TRUE;
}
// IntDisable(INT_ADC1SS0); // oneshot display
}
}
// fs - 400-8000Hz up to 8Khz,
// channelNum - ADC1 channel number channelNum = 3
// buffer - data buffer to be sampled into
// numberOfSamples - # of samples = 64 points only
//
uint32_t ADCBatchTaskInit(void){
uint32_t i;
uint32_t period;
// Setting global pointer to point at array passed by funciton
Buffer = adcbuff0;
//
//ADC0 and PORTE have already been enalbed in previous funcdtions
// The ADC0 peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
//
// For this example ADC0 is used with AIN0, AIN1, AIN2, AIN3 on port E.
// The actual port and pins used may be different on your part, consult
// the data sheet for more information. GPIO port E needs to be enabled
// so these pins can be used.
// TODO: change this to whichever GPIO port you are using.
//
// SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//
// Check if the peripheral access is enabled.
//
// while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE))
// {
// }
//
// Select the analog ADC function for these pins.
// Consult the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0); // AIN3 PE0 - AIN3
// following the steps on page 818 datasheet
//1) disable sample sequence 0 of ADC1
// ADCSequenceDisable(ADC1_BASE, 0);
ADCSequenceDisable(ADC1_BASE, 3);
//2) configure the trigger event for sample sequence 0
// Enable the ADC0 for interrupt Sequence 0 with lower priority =3 from lab3 part 3a
// ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_TIMER, 3);
ADCSequenceConfigure(ADC1_BASE, 3, ADC_TRIGGER_TIMER, 3);
//4) for each sample in the sample sequence (total 8 samples), conifigure the correponding input source
// ADCSequenceStepConfigure(ADC1_BASE, 3, 0, ADC_CTL_CH3);
// for(i = 0; i < 7; i++){
// ADCSequenceStepConfigure(ADC1_BASE, 0, i, ADC_CTL_CH3);
// }
// 5) for the last one, END bit is set and IE is enabled (interrupt)
// ADCSequenceStepConfigure(ADC1_BASE, 0, 7, ADC_CTL_CH3 | ADC_CTL_END | ADC_CTL_IE);
ADCSequenceStepConfigure(ADC1_BASE, 3, 0, ADC_CTL_CH3 | ADC_CTL_END | ADC_CTL_IE);
//6) set the corresponding MASK bit in ADCIM register
// ADCIntEnableEx(ADC1_BASE,ADC_INT_SS0); // interrupt due to ADC sample sequence 0
ADCIntEnableEx(ADC1_BASE,ADC_INT_SS3); // interrupt due to ADC sample sequence 0
// or ADCIntEnable(ADC1_BASE, 0);
//7) enable the sample sequence logic ASEN0 bit in ADCACTSS register
// ADCIntRegister(ADC0_BASE, 0, ADC0Seq0_Handler );
ADCIntRegister(ADC1_BASE, 3, ADC1Seq3_Handler );
// ADCSequenceEnable(ADC1_BASE, 0);
ADCSequenceEnable(ADC1_BASE, 3);
//
// The Timer0 peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
// Disabling Timer0A for configuration
TimerDisable(TIMER0_BASE, TIMER_A);
// Configure as 16 bit timer and trigger ADC conversion
// TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PERIODIC); // here
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // here
TimerControlTrigger(TIMER0_BASE, TIMER_A, true);
//
//
// TODO: configure this to calculate load value based on frequency inputed
//
//
//period = g_ui32SysClock /400; // 400 Hz
period = (uint32_t) g_ui32SysClock /25; // 400 Hz
// testing 5000 Hz good
// testing 8000 Hz good
// testing 9000 Hz 9Khz not working
// testing 10000 Hz 10Khz not working, too fast
TimerLoadSet(TIMER0_BASE, TIMER_A, period-1);
TimerEnable(TIMER0_BASE, TIMER_A);
ADCIntClear(ADC1_BASE, 0);
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// IntPrioritySet(INT_ADC1SS0,(3<<5));
IntPrioritySet(INT_ADC1SS3,(3<<5));
// IntEnable(INT_ADC1SS0);
IntEnable(INT_ADC1SS3);
//TimerIntEnable(TIMER0_BASE, TIMER_A); // THIS may not be needed, TIMER0A interrupt not needed.
// if(xTaskCreate(ADCBatchTask, (const portCHAR *)"ADC Batch",
// ADCBATCHTASKSTACKSIZE, NULL, tskIDLE_PRIORITY +
// PRIORITY_ADCBATCH_TASK, NULL) != pdPASS)
if(xTaskCreate(ADCBatchTask, (const portCHAR *)"ADC Batch",
ADCBATCHTASKSTACKSIZE, NULL, tskIDLE_PRIORITY +
PRIORITY_ADCBATCH_TASK, &xADCBatchTaskHandler) != pdPASS)
{
return(1);
}
configASSERT(xADCBatchTaskHandler);
//
// Success.
//
return(0);
}