/******************************************************************************* * rtpcExample.c - MAXv real-time position example code * * This file contains example code for processing real-time position capture data * from the MAXv motor controller. This example code is written for an Intel * architecture little endian processor and compiler utilizing the ILP32 data * model. The MAXv controller, which creates the real-time position capture data * has a big endian PowerPC processor. */ #define RTPC_TBL_SZ 100 /* user interface real-time position capture table size */ /* * Position capture table for the user interface */ typedef struct rtpc_data { unsigned encoder; /* encoder position */ unsigned char axis; /* axis */ unsigned char home; /* home switch and home event bits */ unsigned short ioPos; /* positive I/O bits */ unsigned short ioNeg; /* negative I/O bits */ } RTPC_DATA; typedef struct rtpc_tbl { int isrInsert; /* ISR data insert index */ int dispIdx; /* User Interface data removal index */ RTPC_DATA Entry[RTPC_TBL_SZ]; } RTPC_TBL; #define XFER_TABLE_SIZE 55 /* position capture data transfer table size */ /* * Position capture table in shared memory */ #pragma pack(2) typedef struct rtpcTblEntry { unsigned encoder; /* encoder position */ unsigned char axis; /* axis */ unsigned char home; /* home switch and home event bits */ unsigned short ioPos; /* positive I/O bits */ unsigned short ioNeg; /* negative I/O bits */ } RTPC_TBL_ENTRY; typedef struct capTblXfer { unsigned char cntrlInsert; /* controller data insert index */ unsigned char hostRemove; /* host data removal index */ RTPC_TBL_ENTRY Entry[XFER_TABLE_SIZE]; } CAP_TBL_XFER; RTPC_TBL RtPosCap; /* User interface real-time position capture data */ /* Used to convert from axis index to ASCII character axis ID */ const char AxisId[8]={'x','y','z','t','u','v','r','s'}; /******************************************************************************* * initUiRtPosCap() - Initialize user interface real-time position capture data * * This function initializes the user interface real-time position capture data * structure. * * RETURNS: N/A */ void initUiRtPosCap(void) { /* clear the entire data structure */ (void)memset((void *)&RtPosCap, 0, sizeof(RTPC_TBL)); } /******************************************************************************* * displayUiRtPosCap() - Display user interface real-time position capture data * * This function displays the contents of the user interface real-time position * capture data structure. This function is similar to the implementation of the * PCE command in the MAXv controller. This function displays data that has * previously not been displayed. It advances the displayed index in the user * interface real-time position capture data structure for each entry displayed. * * RETURNS: N/A */ void displayUiRtPosCap(void) { RTPC_DATA * pTblEntry; /* points to a position capture table entry */ /* process all undisplayed table entries */ while (RtPosCap.dispIdx != RtPosCap.isrInsert) { /* * verify data in the capture table and * validate the table array index */ if ((0 <= RtPosCap.dispIdx) && (RtPosCap.dispIdx < RTPC_TBL_SZ)) { /* point to table entry to be displayed */ pTblEntry = &RtPosCap.Entry[RtPosCap.dispIdx]; /* display the table entry */ (void)printf("RTPC=%c,%d,%X,%X,%X\n", AxisId[pTblEntry->axis], /* axis */ pTblEntry->encoder, /* encoder position */ pTblEntry->home, /* home bits */ pTblEntry->ioPos, /* positive edge bits */ pTblEntry->ioNeg); /* negative edge bits */ /* advance the display table entry index */ if ((RtPosCap.dispIdx + 1) >= RTPC_TBL_SZ) RtPosCap.dispIdx = 0; else RtPosCap.dispIdx++; } else { /* Invalid display index value, reset it to zero. */ RtPosCap.dispIdx = 0; } } } /******************************************************************************* * byteSwap32() - Byte swap a 32-bit value * * This function converts the 32-bit value passed to it from one endian form to * the opposite. This function is used in the context of this example to convert * 32-bit values from big endian to little endian. * * RETURNS: 32-bit byte swapped value */ unsigned byteSwap32 ( unsigned val /* 32-bit value to be byte swapped */ ) { /* Copy of the passed in value before byte swap */ union { unsigned wordVal; unsigned char byteVal[4]; } temp1; /* Copy of the passed in value after byte swap */ union { unsigned wordVal; unsigned char byteVal[4]; } temp2; /* make a copy of the 32-bit value */ temp1.wordVal = val; /* Reverse the byte order. */ temp2.byteVal[0] = temp1.byteVal[3]; temp2.byteVal[1] = temp1.byteVal[2]; temp2.byteVal[2] = temp1.byteVal[1]; temp2.byteVal[3] = temp1.byteVal[0]; /* Return byte swapped 32-bit value */ return (temp2.wordVal); } /******************************************************************************* * byteSwap16() - Byte swap a 16-bit value * * This function converts the 16-bit value passed to it from one endian form to * the opposite. This function is used in the context of this example to convert * 16-bit values from big endian to little endian. * * RETURNS: 16-bit byte swapped value */ unsigned short byteSwap16 ( unsigned short val /* 16-bit value to be byte swapped */ ) { /* Copy of the passed in value before byte swap */ union { unsigned short shortVar; unsigned char byteVal[2]; } temp1; /* Copy of the passed in value after byte swap */ union { unsigned short shortVar; unsigned char byteVal[2]; } temp2; /* make a copy of the 16-bit value */ temp1.shortVar = val; /* Reverse the byte order.*/ temp2.byteVal[0] = temp1.byteVal[1]; temp2.byteVal[1] = temp1.byteVal[0]; /* Return byte swapped 16-bit value */ return(temp2.shortVar); } /******************************************************************************* * collectRtPosCapData() - Collect real-time position capture data * * This function is called from the ISR that services the VME interrupts. This * function is to be executed when bit number 8 (0x00000100 BE, 0x00010000 LE) of * the MAXv controller status word 2 is set by the MAXv controller. The address * of the real-time position capture data transfer area in the VME shared memory * is passed to this function as the first parameter. The second parameter is * the address of the user interface real-time position capture data. This * function copies the data from the VME shared memory area to the user interface * data structure. The MAXv controller has a PowerPC big endian (BE) processor, * and this function was written for an Intel architecture little endian (LE) * processor. Therefore, the bytes of the 32-bit and 16-bit elements of the * real-time position capture table in the VME shared memory are reversed when * copying them to the user interface data structure. * * It is recommended that an initialization routine be executed before the ISR * for real-time position capture is enabled. This initialization routine would * synchronize the host removal index (hostRemove) with the controller insert * index (cntrlInsert) in the shared memory. This initialization routine will * prevent this function from collecting invalid data. * * RETURNS: N/A */ CAP_TBL_XFER * pCapTbl; RTPC_TBL * pRtPosCap; void collectRtPosCapData ( CAP_TBL_XFER * pCapTbl, /* points to capture data in VME shared memory */ RTPC_TBL * pRtPosCap; /* points to user interface capture data */ ) { RTPC_TBL_ENTRY * pCtlData; /* points to capture data table entry */ RTPC_DATA * pData; /* points to user interface table entry */ /* remove all data posted by the MAXv controller */ while (pCapTbl->hostRemove != pCapTbl->cntrlInsert) { /* * verify data in the host removal index is valid */ if ((0 <= pCapTbl->hostRemove) && (pCapTbl->hostRemove < XFER_TABLE_SIZE)) { /* point to data entry to be transferred from */ pCtlData = &pCapTbl->Entry[pCapTbl->hostRemove]; /* point to data entry to be transferred to */ pData = &pRtPosCap->Entry[pRtPosCap->isrInsert]; /* transfer the data */ pData->encoder = byteSwap32(pCtlData->encoder); pData->axis = pCtlData->axis; pData->home = pCtlData->home; pData->ioPos = byteSwap16(pCtlData->ioPos); pData->ioNeg = byteSwap16(pCtlData->ioNeg); /* Advance the ISR table insert index */ if ((pRtPosCap->isrInsert + 1) >= RTPC_TBL_SZ) pRtPosCap->isrInsert = 0; else pRtPosCap->isrInsert++; /* Advance the host removal index */ if ((pCapTbl->hostRemove + 1) >= XFER_TABLE_SIZE) pCapTbl->hostRemove = 0; else pCapTbl->hostRemove++; } else { /* Invalid host removal index value, reset it to zero. */ pCapTbl->hostRemove = 0; } } } void display() { CAP_TBL_XFER * pCapTbl; /* points to capture data in VME shared memory */ RTPC_TBL pRtPosCap; pCapTbl=0xd94; collectRtPosCapData(pCapTbl,pRtPosCap); displayUiRtPosCap(); }