📄 dy4max6634.c
字号:
/* dy4max6634.c - CWC VME-182/183 Temperature Sensor driver *//************************************************************************** * * Copyright (c) 2005 Curtiss-Wright Controls, Inc. All rights * reserved. This Source Code is the Property of Curtiss-Wright * Controls, Inc. and can only be used in accordance with Source * Code License Agreement(s) of Curtiss-Wright Controls, Inc. or any * of its subsidiaries. * **************************************************************************//*FILE HEADER************************************************************** * Product Name: VME-182/183 and CCA-145/146 VxWorks BSPs * * Header %name: dy4max6634.c % * Instance: kanata_1 * * Description: On-board CPU thermal sensor library (MAX6634) * While it is true that the thermal chips (4xMAX6634) default to SMBus mode, * the SMBus spec and the I2C spec are essentially the same. The notable difference * is that SMBus has a timeout defined, should any access take longer than 35mS). * The bus is set up to run at 100KHz. This means, if things are working correctly, * accesses should be made in the 0.01mS range, * * %created_by: tissa % * %date_created: Mon Dec 12 17:44:02 2005 % * * Notes: * * Log: * 01j, 12dec05, tis rename Discovery_II directory to Discovery_III CR#12858. * 01f, 12oct05,rsr -CR3323 revise header descriptions for each function; * -CR2333 correct SMBus comment in dy4ThermalHwInit * 01g, 09may05,tis - add support for CCA-146 * 01f, 20apr05,aak fix return value out of range * 01e, 20feb05,aak diab compile warnings * 01d, 20jan05,tis -moved intEnable from dy4ThermalHwInit2()to dy4ThermalConnect() PT#2484 * -add intLock() to the routines dy4ThermalHwInit2() and dy4ThermalConnect(). * 01c, 17nov04,jb fix dy4ThermalUsrRoutine Global Variable for CCA-145 * 01b, 10nov04,tis - add support for CCA-145 * - add function dy4ThermalGetTemp() * 01a, 27oct04,mb PT 2250: fix for thermal * 2003-10-06-JCS Re-written * *END FILE HEADER*********************************************************/#include "vxWorks.h"#include "intLib.h"#include "stdio.h"#include "taskLib.h"#include "config.h"#ifdef VME_182#include "dy4182.h"#include "intCtrl182_dy4.h"#include "fpga182_dy4.h"#endif#ifdef VME_183#include "cwv183.h"#include "intCtrl183.h"#include "fpga183.h"#define TEMP1 TEMPA_INT_SRC#endif#ifdef CCA_145#include "cca145.h"#include "intCtrl145.h"#include "fpga145.h"#endif#ifdef CCA_146#include "cca146.h"#include "intCtrl146.h"#include "fpga146.h"#endif#ifdef VME_183#include "h/drv/discovery_III/i2c/gtI2c.h"#else#include "h/drv/discovery_II/i2c/gtI2c.h"#endif#include "h/drv/thermal/dy4max6634.h"#ifdef INCLUDE_DY4DEBUG#include "h/drv/dy4Debug/dy4Debug.h"#else#define DEBUGP(a) /* normal debug output */#define DEBUGMP(a) /* message output */#define DEBUGEP(a) /* printf a */ /* error output */#define DEBUGRP(a) /* resource output */#define DEBUGIP(a) /* extra info output */#define DEBUGXP(a) /* temporary messages */#define DEBUGINTDP(a) /* interrupt level normal debug output */#define DEBUGINTMP(a) /* interrupt level message output */#define DEBUGINTEP(a) /* interrupt level error output */#define DEBUGINTIP(a) /* interrupt level extra info output */#define DEBUGINTXP(a) /* interrupt level temporary messages */#endif/* * Static Global Variables */LOCAL int dy4ThermalUsrArg[DY4THERMAL_MAX];LOCAL void (*dy4ThermalUsrRoutine[DY4THERMAL_MAX])(int) = {NULL, NULL#if !(defined (CCA_145) || defined (CCA_146)) ,NULL, NULL#endif };/* * Forward decalarations for internal use functions */LOCAL void dy4ThermalIntHandler();LOCAL int dy4ThermalConvertTemp(DY4THERMAL_UNIT unit, int data);/******************************************************************************** dy4ThermalHwInit - Initializes temperature sensors** DESCRIPTION: This routine initializes the temperature sensors.** INPUT:* None** RETURN:* None - displays message of error via DEBUGEP function*******************************************************************************/void dy4ThermalHwInit ( void ) { volatile int waiting,retry; int i, tempVal; char cfgVal; /* * only CPU0 is allowed to configure the temperature sensors */ if (sysProcId != CPU_ID_0) { DEBUGEP(("ERR:dy4ThermalHwInit:Error, cpu 0 must do init\n")); return; } /* * The code below sets the bus to I2C mode. The bus is set up to run at 100KHz. * This means, if things are working correctly, accesses should be made in the 0.01mS range, * much faster than the SMBus timeout, which may cause the I2C accesses to fail. * So we will loop on the writes until we read back what we set. This should * only have to be done for the init. */ for (i = 0; i < DY4THERMAL_MAX; i++) { waiting = TRUE; retry = 0; while((waiting == TRUE)&&(retry < 5)) { if( dy4ThermalSetConfig( i, DY4THERMAL_CONFIG_INIT_VALUE ) == OK ) { if( dy4ThermalGetConfig( i, &cfgVal ) == OK ) { if( cfgVal == DY4THERMAL_CONFIG_INIT_VALUE) { waiting = FALSE; } } } retry++; } if( waiting == TRUE ) { DEBUGEP(("ERR:dy4ThermalHwInit:Error, could not write config reg for sensor %d\n",i)); } } for (i = 0; i < DY4THERMAL_MAX; i++) { waiting = TRUE; retry = 0; while((waiting == TRUE)&&(retry < 5)) { if( dy4ThermalSetHighTemp( i, dy4ThermalTHighTemps[i] ) == OK ) { if( dy4ThermalGetHighTemp( i, &tempVal ) == OK ) { if( tempVal == dy4ThermalTHighTemps[i]) { waiting = FALSE; } } } retry++; } if( waiting == TRUE ) { DEBUGEP(("ERR:dy4ThermalHwInit:Error, could not write Thigh reg for sensor %d\n",i)); } } for (i = 0; i < DY4THERMAL_MAX; i++) { waiting = TRUE; retry = 0; while((waiting == TRUE)&&(retry < 5)) { if( dy4ThermalSetLowTemp( i, dy4ThermalTLowTemps[i] ) == OK ) { if( dy4ThermalGetLowTemp( i, &tempVal ) == OK ) { if( tempVal == dy4ThermalTLowTemps[i]) { waiting = FALSE; } } } retry++; } if( waiting == TRUE ) { DEBUGEP(("ERR:dy4ThermalHwInit:Error, could not write Tlow reg for sensor %d\n",i)); } } for (i = 0; i < DY4THERMAL_MAX; i++) { waiting = TRUE; retry = 0; while((waiting == TRUE)&&(retry < 5)) { if( dy4ThermalSetHystTemp( i, dy4ThermalTHystTemps[i] ) == OK ) { if( dy4ThermalGetHystTemp( i, &tempVal ) == OK ) { if( tempVal == dy4ThermalTHystTemps[i]) { waiting = FALSE; } } } retry++; } if( waiting == TRUE ) { DEBUGEP(("ERR:dy4ThermalHwInit:Error, could not write Thyst reg for sensor %d\n",i)); } } }/******************************************************************************** dy4ThermalIntHandler - Temperature sensor interrupt handling routine** DESCRIPTION: Initiates interrupt routine corresponding to the index value passed.** INPUT:* Integer index for interrupt number, corresponds to thermal interrupt table** RETURN:* None*******************************************************************************/LOCAL void dy4ThermalIntHandler ( int index ) { if (fpgaIsIntActive(TEMP1 + index)) { if (dy4ThermalUsrRoutine[DY4THERMAL_1 + index]) { (dy4ThermalUsrRoutine[DY4THERMAL_1 + index])(dy4ThermalUsrArg[DY4THERMAL_1 + index]); } } }/******************************************************************************** dy4ThermalDelay*** DESCRIPTION: Provides delay such that registers can change state and settle,* preventing erroneous reading of the registers.* INPUT:* None** RETURN:* None*******************************************************************************/void dy4ThermalDelay ( void ) { taskDelay(20); /* A delay to let the registers on the thermal device settle */ }/******************************************************************************** dy4ThermalHwInit2 - Initialize temperature sensor interrupts** DESCRIPTION: Set up temp sensor interrupts. Loop through interrupt and* enable with dy4ThermalIntHandler* INPUT:* None** RETURN:* None*******************************************************************************/void dy4ThermalHwInit2 ( void ) { int i; UINT32 lock; lock = intLock(); for (i = 0; i < DY4THERMAL_MAX; i++) { intConnect((void *)(TEMP1 + i), (void *)dy4ThermalIntHandler, i); } intUnlock(lock); }/******************************************************************************** dy4ThermalConnect - Connect a C routine to temperature interrupt** DESCRIPTION: Connect Interrupt Service Routine to interrupt, based-off passed* parameters* INPUT:* interrupt index number, routine to connect interrupt number to, arguments* to be passed to routine to handle interrupt* RETURN:* None*******************************************************************************/void dy4ThermalConnect ( DY4THERMAL_ID id, void (*routine)(int), int arg ) { UINT32 lock; lock = intLock(); if( id < DY4THERMAL_MAX ) { dy4ThermalUsrRoutine[id] = routine; dy4ThermalUsrArg[id] = arg; intEnable(TEMP1 + id); } intUnlock(lock); }/******************************************************************************** dy4ThermalGetConfig - Get the max6634 config register** DESCRIPTION: Reads the configuration register of the specified temp sensor** INPUT:* Sensor ID to read from, pointer to storage location where retrieved sensor* configuration information will be stored* RETURN:* OK if no errors or ERROR if error results*******************************************************************************/STATUS dy4ThermalGetConfig ( DY4THERMAL_ID sensId, char *data ) { unsigned char value[1]; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalGetConfig:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalGetConfig:Error, could not read config reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterRead(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_CONFIG_REG, &value[0], 1); dy4ThermalDelay(); timeoutValue++; } /* */ *data = value[0]; DEBUGP(("DEB:dy4ThermalGetConfig: Read Config as 0x%x for sensor %d\n", *data, sensId)); return( OK ); }/******************************************************************************** dy4ThermalSetConfig - Set the max6634 config register** DESCRIPTION: Sets the configuration register of the specified temp sensor** INPUT:* Sensor ID to affect, data you wish to write to the configuration register* RETURN:* OK if no errors or ERROR if error results*******************************************************************************/STATUS dy4ThermalSetConfig ( DY4THERMAL_ID sensId, char data ) { unsigned char value[1]; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalSetConfig:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } DEBUGP(("DEB:dy4ThermalSetConfig: Setting Config to 0x%x for sensor %d\n", data, sensId)); value[0] = (data & 0xFF); /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { /* * Report this error at the next level up * since the first few access may fail until * we can get through and set the I2C complience bit */ DEBUGEP(("ERR:dy4ThermalSetConfig:Error, could not write config reg for sensor %d\n",sensId)); return( ERROR ); } status = dy4I2cMasterWrite(I2C_TEMP_SENS, I2C_TEMP1_ADDR + sensId, DY4THERMAL_CONFIG_REG, &value[0], 1); dy4ThermalDelay(); timeoutValue++; } /* */ return( OK ); } /******************************************************************************** dy4ThermalGetHighTemp - Get the High temperature warning value** DESCRIPTION: Takes sensor ID and a pointer to a storage location, retreives* the sensor's high temperature warning value and stores at pointer* INPUT:* sensor ID, pointer to storage location for data** RETURN:* OK if successful or ERRROR if fails to read from the sensor register*******************************************************************************/STATUS dy4ThermalGetHighTemp ( DY4THERMAL_ID sensId, int *data ) { unsigned char value[2]; int temp; volatile STATUS status = ERROR; unsigned int timeoutValue = 0; if( sensId >= DY4THERMAL_MAX ) { DEBUGEP(("ERR:dy4ThermalGetHighTemp:Error, invalid sensor id %d\n",sensId)); return( ERROR ); } /* */ while (status != STAT_I2C_OK) { if(timeoutValue >= 10) { DEBUGEP(("ERR:dy4ThermalGetHighTemp:Error, could not read Thigh reg for sensor %d\n",sensId)); return( ERROR ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -