/*
*********************************************************************************************************
* Analog I/O Module
*
* (c) Copyright 1999, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* Filename : AIO.C
* Programmer : Jean J. Labrosse
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define AIO_GLOBALS
#include "ucos_ii.h"
#include "AIO.h"
/*
*********************************************************************************************************
* LOCAL VARIABLES
*********************************************************************************************************
*/
static OS_STK AIOTaskStk[AIO_TASK_STK_SIZE];
static OS_EVENT *AIOSem;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void AIOTask(void *data);
static void AIInit(void);
static void AIUpdate(void);
static void AOInit(void);
static void AOUpdate(void);
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE CALIBRATION PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure an analog input channel.
* Arguments : n is the analog input channel to configure:
* gain is the calibration gain
* offset is the calibration offset
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
*********************************************************************************************************
*/
INT8U AICfgCal (INT8U n, FP32 gain, FP32 offset)
{
INT8U err;
AIO *paio;
if (n < AIO_MAX_AI) {
paio = &AITbl[n]; /* Point to Analog Input structure */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AI channel */
paio->AIOCalGain = gain; /* Store new cal. gain and offset into struct */
paio->AIOCalOffset = offset;
paio->AIOGain = paio->AIOCalGain * paio->AIOConvGain; /* Compute overall gain */
paio->AIOOffset = paio->AIOCalOffset + paio->AIOConvOffset; /* Compute overall offset */
OSSemPost(AIOSem); /* Release AI channel */
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE CONVERSION PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure an analog input channel.
* Arguments : n is the analog channel to configure (0..AIO_MAX_AI-1).
* gain is the conversion gain
* offset is the conversion offset
* pass is the value for the pass counts
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
*********************************************************************************************************
*/
INT8U AICfgConv (INT8U n, FP32 gain, FP32 offset, INT8U pass)
{
INT8U err;
AIO *paio;
if (n < AIO_MAX_AI) {
paio = &AITbl[n]; /* Point to Analog Input structure */
OSSemPend(AIOSem, 0, &err); /* Obtain exclusive access to AI channel */
paio->AIOConvGain = gain; /* Store new conv. gain and offset into struct */
paio->AIOConvOffset = offset;
paio->AIOGain = paio->AIOCalGain * paio->AIOConvGain; /* Compute overall gain */
paio->AIOOffset = paio->AIOCalOffset + paio->AIOConvOffset; /* Compute overall offset */
paio->AIOPassCnts = pass;
OSSemPost(AIOSem); /* Release AI channel */
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* CONFIGURE THE SCALING PARAMETERS OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to configure the scaling parameters associated with an analog
* input channel.
* Arguments : n is the analog input channel to configure (0..AIO_MAX_AI-1).
* arg is a pointer to arguments needed by the scaling function
* fnct is a pointer to a scaling function
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
*********************************************************************************************************
*/
INT8U AICfgScaling (INT8U n, void (*fnct)(AIO *paio), void *arg)
{
AIO *paio;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (n < AIO_MAX_AI) {
paio = &AITbl[n]; /* Faster to use a pointer to the structure */
OS_ENTER_CRITICAL();
paio->AIOScaleFnct = (void (*)())fnct;
paio->AIOScaleFnctArg = arg;
OS_EXIT_CRITICAL();
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
* GET THE VALUE OF AN ANALOG INPUT CHANNEL
*
* Description : This function is used to get the currect value of an analog input channel (in engineering
* units).
* Arguments : n is the analog input channel (0..AIO_MAX_AI-1).
* pval is a pointer to the destination engineering units of the analog input channel
* Returns : 0 if successfull.
* 1 if you specified an invalid analog input channel number.
* In this case, the destination is not changed.
*********************************************************************************************************
*/
INT8U AIGet (INT8U n, FP32 *pval)
{
AIO *paio;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
if (n < AIO_MAX_AI) {
paio = &AITbl[n];
OS_ENTER_CRITICAL(); /* Obtain exclusive access to AI channel */
*pval = paio->AIOEU; /* Get the engineering units of the analog input channel */
OS_EXIT_CRITICAL(); /* Release AI channel */
return (0);
} else {
return (1);
}
}
/*$PAGE*/
/*
*********************************************************************************************************
*