⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.c

📁 a/d公司8位微控制器aduc812(10路10位adc)应用笔记
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
   Author:           Paul Conway
   Contact Details:  PEI Technologies Ltd.
                     Foundation Building
                     National Technological Park
                     Limerick
                     Ireland
                     http://www.ul.ie/~pei

   Release Date:     May 1999.
   Version:          1.01
   Company:          Analog Devices
*/

#include <aduc812.h>
#include "datatype.h"
#include "function.h"
#include "tii.h"
#include "teds.h"
#include "stim.h"


// Module Level Variables:
// ----------------------

// This variable is required by the 'trigger' functions:
//
static unsigned char mucTrigChanAddr = 0xFF;


// Data Transport Functions:
// ------------------------

// Note that there are 256 bytes of Data RAM available on-chip.
// This transport length cannot approach that amount (due to stack 
// and running data-space requirements)
// 
#define MAX_TRANSPORT_LENGTH 100

boolean DAT_TransportActive(void)
{
   return TII_DetectNIOE();
}
//
// The checksum will be the one's complement of the sum (modulo 2^16) of all 
// the data structure's preceding bytes - including the initial length field
// and excluding the checksum field. 
//
// The data structure length field is 4 bytes long. It represents the total
// number of bytes in the data block, excluding itself (i.e. excluding 4 bytes)
//
U16C DAT_CalcChecksum(unsigned char *pBuf)
{
   U32L Offset=0;
   U32L NumBytes=0;
   U16C ChkSum=0;

   NumBytes = (U32L) *(U32L*)pBuf;

      // Include the length field & exclude the checksum field (i.e.4-2 => +2).
      // NumBytes will then represent all the bytes excluding the checksum.
      //
   NumBytes += 2;  

   for(Offset=0; Offset<NumBytes; Offset++)
   {
      ChkSum += *(pBuf+Offset);
   }

      // The actual checksum is the 1's complement => negate bits.
      //
   return ~ChkSum;
}

boolean DAT_ReadMetaTEDS(void)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];
       
      // Meta TEDS *must* be implemented... if it's there, send it.
      //
   if(TEDS_GetTEDSHandle(TEDS_META, NUL, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE If there has been a problem finding it.. return a null TEDS field
      //
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadChannelTEDS(U8E ChanNum)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];
       
      // Channel TEDS *must* be implemented... if it's there, send it.
      //
   if(TEDS_GetTEDSHandle(TEDS_CHANNEL, ChanNum, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE If there has been a problem finding it (of if the Channel Number
      //      specified is invalid)... return a null TEDS field
      //
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadMetaIdTEDS(void)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];

      // IF there is a Meta-ID TEDS implemented, read it then send it:
      //
   if(TEDS_GetTEDSHandle(TEDS_META_ID, NUL, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE There is no Meta-ID TEDS implemented: return a 'field-
      //      length' (4 bytes long) containing '0' to indicate that there 
      //      will be no data-fields to follow.
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadChannelIdTEDS(U8E ChanNum)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];

      // IF there is a Channel-ID TEDS implemented, read it then send it:
      //
   if(TEDS_GetTEDSHandle(TEDS_CHANNEL_ID, ChanNum, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE There is no Channel-ID TEDS implemented: return a 'field-
      //      length' (4 bytes long) containing '0' to indicate that there 
      //      will be no data-fields to follow.
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadCalibrationTEDS(void)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];

      // IF there is a Calibration TEDS implemented, read it then send it:
      //
   if(TEDS_GetTEDSHandle(TEDS_CALIBRATION, NUL, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE There is no 'Calibration TEDS' implemented: return a 'field-
      //      length' (4 bytes long) containing '0' to indicate that there 
      //      will be no data-fields to follow.
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadCalibrationIdTEDS(void)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];

      // IF there is a 'Calibration-ID' TEDS implemented, read it then send it:
      //
   if(TEDS_GetTEDSHandle(TEDS_CALIB_ID, NUL, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE There is no 'Calibration-ID' TEDS implemented: return a 'field-
      //      length' (4 bytes long) containing '0' to indicate that there 
      //      will be no data-fields to follow.
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadEndUserAppTEDS(void)
{
   boolean             bRet=FALSE;
   unsigned char idata pDataTransportBuffer[MAX_TRANSPORT_LENGTH];

      // IF there is an 'End User Application Specific' TEDS implemented, 
      // read it then send it:
      //
   if(TEDS_GetTEDSHandle(TEDS_END_USER, NUL, pDataTransportBuffer))
   {
      U32L TEDSLength = (U32L) (*(U32L*)pDataTransportBuffer+sizeof(U32L));

      bRet = TII_ReadFrame(TEDSLength, pDataTransportBuffer);
   }
      // ELSE There is no 'End User Application Specific' TEDS implemented: 
      //      return a 'field-length' (4 bytes long) containing '0' to 
      //      indicate that there will be no data-fields to follow.
   else
   {
      U32L NullTEDS = ZERO_LENGTH_FIELD;
      bRet = TII_ReadFrame( 4, (unsigned char*) &NullTEDS );
   }

   return bRet;
}

boolean DAT_ReadXdcrData(unsigned char ucChNum)
{
   boolean       bRet = FALSE;
   unsigned char ucChanCount=0, ucChan=0;
   XDCR_DATA     Data;

      // If ucChNum==CHANNEL_ZERO, read data for ALL channels (starting at 
      //  Channel 1)...
      //
   if(ucChNum==CHANNEL_ZERO) { ucChanCount = NUM_CHANNELS; ucChan = 1; }
   else                      { ucChanCount = 1; ucChan = ucChNum; }

      // Iterate at least once, for the selected channel (if it's not Ch_0).
      // If it is Ch_0, then iterate for ALL the channels.
      //
   while(ucChanCount--)
   {
      bRet=STIM_GetXdcrData(ucChan, (unsigned char*)&Data);

      if(bRet) 
      {
         unsigned char DataLength;

         if(ucChan==1)        DataLength = CH1_DATA_MODEL_LENGTH;
         else if(ucChan==2)   DataLength = CH2_DATA_MODEL_LENGTH;
            //
            // [NOTE] Fill in here for all implemented channels...
            //
         else                 DataLength = 0;
         
         bRet=TII_ReadFrame(DataLength, (unsigned char*)&Data);
      }
         // if transport was aborted, exit FALSE now...
         //
      else break;

      ucChan++;
   }

   return bRet;
}

boolean DAT_ReadStatus(U8E eReg, unsigned char ucChNum)
{
   STATUS_REGISTER StatusReg = 0;
   boolean         bRet = FALSE;
   STATUS_REGISTER StdImplementedMask = (ucChNum==CHANNEL_ZERO)
                                       ?STD_CH_ZERO
                                       :STD_CH_NONZERO;
   STATUS_REGISTER AuxImplementedMask = (ucChNum==CHANNEL_ZERO)
                                       ?AUX_CH_ZERO
                                       :AUX_IMPLEMENTED;

   switch(eReg)
   {
      case(STANDARD):   
               StatusReg = STA_GetStdStatus(ucChNum);
               bRet = TII_ReadFrame( sizeof(STATUS_REGISTER), 
                                    (unsigned char*)&StatusReg);
               if(bRet) STA_ClearStdStatus(ucChNum, STD_SERVICE_REQUEST |
                                                    STD_TRIGGER_ACK     |
                                                    STD_RESET_COMPLETE  |
                                                    STD_INVALID_COMMAND |
                                                    StdImplementedMask  );
         break;
      case(AUXILIARY):
               StatusReg = STA_GetAuxStatus(ucChNum);
               bRet = TII_ReadFrame( sizeof(STATUS_REGISTER), 
                                     (unsigned char*)&StatusReg);
               if(bRet) STA_ClearAuxStatus(ucChNum, AUX_CHANNEL_OUT_OF_RANGE |
                                                    AuxImplementedMask  );
         break;
      default: bRet = FALSE;
   }
   return bRet;
}

boolean DAT_ReadTrigChAddr(void)
{
   unsigned char ucTrigChAddr;

   ucTrigChAddr = TRIG_GetTrigChanAddr();

   return TII_ReadFrame( 1, &ucTrigChAddr);
}

boolean DAT_ReadIntMask(U8E eReg, unsigned char ucChNum)
{
   INTERRUPT_MASK iMask;

   iMask = INT_GetInterruptMask(eReg, ucChNum);

   return TII_ReadFrame(sizeof(INTERRUPT_MASK), (unsigned char*)&iMask);
}

// pauconapr99: this is fine for the current implementation: it's not a generic
//              way to do this though, it'll need modification.
boolean DAT_ReadSTIMVersion(void)
{
   U32L Ver;

   STIM_GetVersion((unsigned char *)&Ver);

   return TII_ReadFrame(sizeof(U32L), (unsigned char *)&Ver);
}

boolean DAT_WriteXdcrData(unsigned char ucChNum)
{
   boolean       bRet = FALSE;
   unsigned char ucChanCount=0;
   unsigned char ucChan=0;

      // If ucChNum==CHANNEL_ZERO, write data for ALL channels (starting at 
      //  Channel 1)...
      //
   if(ucChNum==CHANNEL_ZERO) { ucChanCount = NUM_CHANNELS; ucChan = 1; }
   else                      { ucChanCount = 1; ucChan = ucChNum; }

      // Iterate at least once, for the selected channel (if it's not Ch_0).
      // If it is Ch_0, then iterate for ALL the channels.
      //
   while(ucChanCount--)
   {
      XDCR_DATA     Data;
      unsigned char DataLength;

      if(ucChan==1)        DataLength = CH1_DATA_MODEL_LENGTH;
      else if(ucChan==2)   DataLength = CH2_DATA_MODEL_LENGTH;
         //
         // [NOTE] Fill in here for all implemented channels...
         //
      else                 DataLength = 0;
         
         // Receive 'DataLength' bytes from transport and put them into 'Data'.
         //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -