Skip to content

Commit

Permalink
Clean up and final bug fixes of ADC task.
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittich committed Jun 27, 2019
1 parent c969cd8 commit 9569a6b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
113 changes: 60 additions & 53 deletions projects/project2/RandomTask.c → projects/project2/ADCMonitorTask.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* RandomTask.c
* ADCMonitorTask.c
*
* Created on: May 19, 2019
* Author: wittich
Expand All @@ -15,6 +15,9 @@
* We always use sequencer 0 for ADC1 and sequencer 1 for ADC0. The end of
* conversion is signaled by an interrupt, which is handled here.
*
* Todo:
* * handle errors but not with assert
* * change 2nd ADC to use sequence 0 (one fewer interrupt)
*/

// includes for types
Expand All @@ -26,6 +29,7 @@
#include "inc/hw_memmap.h"

// driverlib
#include "driverlib/rom.h"
#include "driverlib/adc.h"

// FreeRTOS
Expand All @@ -37,6 +41,8 @@
// Some signals must be scaled to fit in this range.
#define ADC_MAX_VOLTAGE_RANGE 2.5

#define ADC_CHANNEL_COUNT 21
#define ADC_INFO_TEMP_ENTRY 12

// a struct to hold some information about the ADC channel.
struct ADC_Info_t {
Expand Down Expand Up @@ -80,19 +86,19 @@ struct ADC_Info_t ADCs[] = {
{11, "K_MGTH_AVTT", 1.},
};

static float fADCvalues[21]; // ADC values in volts
static float fADCvalues[ADC_CHANNEL_COUNT]; // ADC values in volts

// read-only accessor functions for ADC names and values.

const char* getADCname(const int i)
{
configASSERT(i>=0&&i<21);
configASSERT(i>=0&&i<ADC_CHANNEL_COUNT);
return ADCs[i].name;
}

float getADCvalue(const int i)
{
configASSERT(i>=0&&i<21);
configASSERT(i>=0&&i<ADC_CHANNEL_COUNT);
return fADCvalues[i];
}

Expand All @@ -106,7 +112,7 @@ void ADCSeq0Interrupt()
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;

ADCIntClear(ADC1_BASE, 0);
ROM_ADCIntClear(ADC1_BASE, 0);

/* At this point xTaskToNotify should not be NULL as a transmission was
in progress. */
Expand All @@ -131,7 +137,7 @@ void ADCSeq1Interrupt()
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;

ADCIntClear(ADC0_BASE, 1);
ROM_ADCIntClear(ADC0_BASE, 1);

/* At this point xTaskToNotify should not be NULL as a transmission was
in progress. */
Expand All @@ -156,90 +162,90 @@ void ADCSeq1Interrupt()
static
void initADC1FirstSequence()
{
ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
ROM_ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);

ADCSequenceStepConfigure(ADC1_BASE, 0, 0, ADCs[0].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 1, ADCs[1].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 2, ADCs[2].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 3, ADCs[3].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 4, ADCs[4].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 5, ADCs[5].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 6, ADCs[6].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 7, ADCs[7].channel | ADC_CTL_IE | ADC_CTL_END);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 0, ADCs[0].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 1, ADCs[1].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 2, ADCs[2].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 3, ADCs[3].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 4, ADCs[4].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 5, ADCs[5].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 6, ADCs[6].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 7, ADCs[7].channel | ADC_CTL_IE | ADC_CTL_END);

ADCSequenceEnable(ADC1_BASE, 0);
ROM_ADCSequenceEnable(ADC1_BASE, 0);


ADCIntClear(ADC1_BASE, 0);
ROM_ADCIntClear(ADC1_BASE, 0);


}

static
void initADC1SecondSequence()
{
ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC1_BASE, 0, 0, ADCs[8].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 1, ADCs[9].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 2, ADCs[10].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 3, ADCs[11].channel);
ADCSequenceStepConfigure(ADC1_BASE, 0, 4, ADC_CTL_TS| ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC1_BASE, 0);
ADCIntClear(ADC1_BASE, 0);
ROM_ADCSequenceConfigure(ADC1_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 0, ADCs[8].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 1, ADCs[9].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 2, ADCs[10].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 3, ADCs[11].channel);
ROM_ADCSequenceStepConfigure(ADC1_BASE, 0, 4, ADC_CTL_TS| ADC_CTL_IE | ADC_CTL_END);
ROM_ADCSequenceEnable(ADC1_BASE, 0);
ROM_ADCIntClear(ADC1_BASE, 0);

}


static
void initADC0FirstSequence()
{
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADCs[13].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADCs[14].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADCs[15].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADCs[16].channel| ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 1);
ADCIntClear(ADC0_BASE, 1);
ROM_ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADCs[13].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADCs[14].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADCs[15].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADCs[16].channel| ADC_CTL_IE | ADC_CTL_END);
ROM_ADCSequenceEnable(ADC0_BASE, 1);
ROM_ADCIntClear(ADC0_BASE, 1);

}

static
void initADC0SecondSequence()
{
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADCs[17].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADCs[18].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADCs[19].channel);
ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADCs[20].channel| ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 1);
ADCIntClear(ADC0_BASE, 1);
ROM_ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADCs[17].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADCs[18].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADCs[19].channel);
ROM_ADCSequenceStepConfigure(ADC0_BASE, 1, 3, ADCs[20].channel| ADC_CTL_IE | ADC_CTL_END);
ROM_ADCSequenceEnable(ADC0_BASE, 1);
ROM_ADCIntClear(ADC0_BASE, 1);

}


// playground to test various things
void RandomTask(void *parameters)
void ADCMonitorTask(void *parameters)
{
// initialize to the current tick time
TickType_t xLastWakeTime = xTaskGetTickCount();
uint32_t iADCvalues[21]; // raw adc outputs
uint32_t iADCvalues[ADC_CHANNEL_COUNT]; // raw adc outputs


const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 20000 );

for (;;) {
// First sequence for both ADCs
// First sequence for ADC1
initADC1FirstSequence();

// Set up the task notification and start the conversion.
TaskNotifyADC = xTaskGetCurrentTaskHandle();
ADCProcessorTrigger(ADC1_BASE, 0);
ROM_ADCProcessorTrigger(ADC1_BASE, 0);

// Wait to be notified that the transmission is complete.
unsigned long ulNotificationValue = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );

if( ulNotificationValue == 1 ) {
ADCSequenceDataGet(ADC1_BASE, 0, iADCvalues);
ROM_ADCSequenceDataGet(ADC1_BASE, 0, iADCvalues);
}
else {
// handle error here
Expand All @@ -248,52 +254,53 @@ void RandomTask(void *parameters)
// ADC0,first sequence
initADC0FirstSequence();
TaskNotifyADC = xTaskGetCurrentTaskHandle();
ADCProcessorTrigger(ADC0_BASE, 1);
ROM_ADCProcessorTrigger(ADC0_BASE, 1);
ulNotificationValue = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );

if( ulNotificationValue == 1 ) {
ADCSequenceDataGet(ADC0_BASE, 1, iADCvalues+13); // check offset
ROM_ADCSequenceDataGet(ADC0_BASE, 1, iADCvalues+ADCs_ADC0_START); // check offset
}
else {
// handle error here
configASSERT(0);
}


// second sequence
// second sequence for ADC0
initADC0SecondSequence();
TaskNotifyADC = xTaskGetCurrentTaskHandle();
ADCProcessorTrigger(ADC0_BASE, 1);
ROM_ADCProcessorTrigger(ADC0_BASE, 1);
ulNotificationValue = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );

if( ulNotificationValue == 1 ) {
ADCSequenceDataGet(ADC0_BASE, 1, iADCvalues+ADCs_ADC1_ENTRIES+ADCs_ADC0_FIRST_SEQ_LENGTH); // check offset
ROM_ADCSequenceDataGet(ADC0_BASE, 1, iADCvalues+ADCs_ADC1_ENTRIES+ADCs_ADC0_FIRST_SEQ_LENGTH); // check offset
}
else {
// handle error here
configASSERT(0);
}
initADC1SecondSequence();
TaskNotifyADC = xTaskGetCurrentTaskHandle();
ADCProcessorTrigger(ADC1_BASE, 0);
ROM_ADCProcessorTrigger(ADC1_BASE, 0);

// Wait to be notified that the transmission is complete.
ulNotificationValue = ulTaskNotifyTake( pdTRUE, xMaxBlockTime );

if( ulNotificationValue == 1 ) {
ADCSequenceDataGet(ADC1_BASE, 0, iADCvalues+ADCs_ADC1_FIRST_SEQ_LENGTH);
ROM_ADCSequenceDataGet(ADC1_BASE, 0, iADCvalues+ADCs_ADC1_FIRST_SEQ_LENGTH);
}
else {
// handle error here
configASSERT(0);
}

// convert data to float values
for ( int i = 0; i < 21; ++i ) {
for ( int i = 0; i < ADC_CHANNEL_COUNT; ++i ) {
fADCvalues[i] = iADCvalues[i]/4096.*ADC_MAX_VOLTAGE_RANGE * ADCs[i].scale;
}
// special: temperature of Tiva die. Tiva manu 15.3.6, last equation.
fADCvalues[12] = 147.5 - ( 75 * ADC_MAX_VOLTAGE_RANGE * iADCvalues[12])/4096;
fADCvalues[ADC_INFO_TEMP_ENTRY] = 147.5
- ( 75 * ADC_MAX_VOLTAGE_RANGE * iADCvalues[ADC_INFO_TEMP_ENTRY])/4096.;


// wait x ms for next iteration
Expand Down
2 changes: 1 addition & 1 deletion projects/project2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ ${COMPILER}/project2.axf: ${COMPILER}/smbus.o
${COMPILER}/project2.axf: ${COMPILER}/power_ctl.o
${COMPILER}/project2.axf: ${COMPILER}/CommandLineTask.o
${COMPILER}/project2.axf: ${COMPILER}/PowerSupplyTask.o
${COMPILER}/project2.axf: ${COMPILER}/RandomTask.o
${COMPILER}/project2.axf: ${COMPILER}/ADCMonitorTask.o
${COMPILER}/project2.axf: ${COMPILER}/MonitorTask.o
${COMPILER}/project2.axf: ${COMPILER}/LedTask.o
${COMPILER}/project2.axf: ${COMPILER}/startup_${COMPILER}.o
Expand Down
24 changes: 12 additions & 12 deletions projects/project2/project2.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ void SystemInit()
//
// Run from the PLL, internal oscillator, at the defined clock speed configCPU_CLOCK_HZ
//
g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_OSC_INT|SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_320), configCPU_CLOCK_HZ);
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_OSC_INT|SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_320), configCPU_CLOCK_HZ);

// initialize all pins, using file setup by TI PINMUX tool
PinoutSet();
Expand All @@ -228,17 +228,17 @@ void SystemInit()
initI2C6(g_ui32SysClock); // controller for FPGAs
initI2C4(g_ui32SysClock); // controller for K optics

// initialize the ADCs. This should all go into SystemInit()
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
// initialize the ADCs.
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC1);
// Set the reference to external
ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V );
ADCReferenceSet(ADC1_BASE, ADC_REF_EXT_3V );
ROM_ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V );
ROM_ADCReferenceSet(ADC1_BASE, ADC_REF_EXT_3V );
ROM_ADCIntEnable(ADC0_BASE, 1); // enable interrupt for sequence 1
ROM_ADCIntEnable(ADC1_BASE, 0); // enable interrupt for sequence 0
// clear the interrupts
ADCIntClear(ADC0_BASE, 1);
ADCIntClear(ADC1_BASE, 0);
ROM_ADCIntClear(ADC0_BASE, 1);
ROM_ADCIntClear(ADC1_BASE, 0);
// FreeRTOS insists that the priority of interrupts be set up like this.
ROM_IntPrioritySet( INT_ADC0SS1, configKERNEL_INTERRUPT_PRIORITY );
ROM_IntPrioritySet( INT_ADC1SS0, configKERNEL_INTERRUPT_PRIORITY );
Expand Down Expand Up @@ -298,8 +298,8 @@ void MonitorTask(void *parameters);
void vCommandLineTask(void *parameters);


// playground to test various things
void RandomTask(void *parameters); // @suppress("Unused function declaration")
// Monitoring using the ADC inputs
void ADCMonitorTask(void *parameters);

void ShortDelay()
{
Expand All @@ -323,7 +323,7 @@ int main( void )
xTaskCreate(PowerSupplyTask, "POW", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+5, NULL);
xTaskCreate(LedTask, "LED", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+2, NULL);
xTaskCreate(vCommandLineTask,"CON", 512, NULL, tskIDLE_PRIORITY+1, NULL);
xTaskCreate(RandomTask, "RDM", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+0, NULL);
xTaskCreate(ADCMonitorTask, "ADC", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+4, NULL);
xTaskCreate(MonitorTask, "MON", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+4, NULL);

// queue for the LED
Expand Down

0 comments on commit 9569a6b

Please sign in to comment.