void Gyro_Check_ID() { uint8_t devId; int errCount = 0; devId = Gyro_Read_Register(0xF, 1); // get device ID while (devId != 0b11010011) { // datasheet says it should be 0xD3 __delay_us(100); if (++errCount == 10) // Attempt reading up to 10 times Display_Error(ErrGyro); devId = Gyro_Read_Register(0x0, 1); } }
/************************************************************************* ** ** Function : canOpen_driver ** ** Description : Initializes the Control and Message Channels ** Parameters : s_BOARD *board - pointer to board information ** Returnvalue : (CAN_HANDLE)board - handle for CAN controller ** *************************************************************************/ CAN_HANDLE __stdcall canOpen_driver(s_BOARD *board) { HANDLE hEnumerator; // enumerator handle VCIDEVICEINFO VCIDeviceInfo; // device info HRESULT hResult; int index, boardNum; BOOL bResult; struct sLook_up_table sBitRate_lookup[9] = { {"10K",0x31,0x1C}, {"20K",0x18,0x1C}, {"50K",0x09,0x1C}, {"100K",0x04,0x1C}, {"125K",0x03,0x1C}, {"250K",0x01,0x1C}, {"500K",0x00,0x1C}, {"800K",0x00,0x16}, {"1M",0x00,0x14}}; struct sInterface_lookup_table sInterfaceList[4]={ {"vcan0",0}, {"vcan1",1}, {"vcan2",2}, {"vcan3",3}}; for (boardNum =0 ; boardNum<4;boardNum++) // determine canline selected { if (strcmp(sInterfaceList[boardNum].board_num,board->busname )==0) break; } for (index = 0; index < 10; ++index) // determine baudrate { if (strcmp(sBitRate_lookup[index].baud_rate,board->baudrate)==0) break; } if (index == 9) { MSG_ERR_DRV("IXXAT::open: The given baudrate %S is invalid.", baud_rate); return NULL ; } /* ** The following can be used when the client has multiple CAN interfaces to slect from, and does not wish to use the ** selection dialog box /* /* hResult= vciEnumDeviceOpen(&hEnumerator); // This loop will increment the index of the device list and returns the decription of the CAN line chose by user if (hResult== VCI_OK) { for (index1=0; index1 <= sInterfaceList[boardNum].num; index1++){ hResult=vciEnumDeviceNext(hEnumerator, &VCIDeviceInfo); } } printf("Device Selected: %s %s\n",VCIDeviceInfo.Manufacturer, VCIDeviceInfo.Description); if (hResult== VCI_OK) hResult=vciEnumDeviceClose(hEnumerator); if (hResult== VCI_OK) hResult= vciDeviceOpen(&VCIDeviceInfo.VciObjectId, &hDevice); */ /* ** Display Interface Selection Dialog Box */ hResult= vciDeviceOpenDlg(0, &hDevice); /* ** Establish and activate the message Channel */ if (hResult== VCI_OK) hResult= canChannelOpen(hDevice, 0, TRUE, &hCanChn); // Select Rx fifo size, Rx threshold, Tx fifo size, Tx threshold if (hResult== VCI_OK) hResult=canChannelInitialize( hCanChn, 1024, 1,128,1); if (hResult== VCI_OK) hResult=canChannelActivate(hCanChn, TRUE); /* ** Establish and Activate the Contol Channel */ if (hResult== VCI_OK) hResult=canControlOpen(hDevice, 0, &hCanCtl); // Select 11 or 29 bit IDs, Select operating mode if (hResult== VCI_OK) hResult=canControlInitialize( hCanCtl, CAN_OPMODE_STANDARD | CAN_OPMODE_ERRFRAME, sBitRate_lookup[index].bt0, sBitRate_lookup[index].bt1 ); // With VCI it is possible to filter IDs, See VCI V3 manual. The following will accept all IDs if (hResult== VCI_OK) hResult= canControlSetAccFilter( hCanCtl, FALSE, CAN_ACC_CODE_ALL, CAN_ACC_MASK_ALL); if (hResult== VCI_OK) hResult=canControlStart(hCanCtl, TRUE); if (hResult!=VCI_OK) { Display_Error(hResult); return NULL; } /* ** Create timer to poll bus status */ bResult= CreateTimerQueueTimer( & hTimerHandle, NULL, TimerProc1, // Callback function NULL, 0, STATUS_POLLING_CYCLE, WT_EXECUTEINIOTHREAD ); return (CAN_HANDLE)board; }