📄 ski2c.c
字号:
/* Init data as input line */ I2C_DATA_IN(IoC); SkDgWaitTime(IoC, NS2BCLK(T_CLK_2_DATA_OUT)); I2C_CLK_HIGH(IoC); SkDgWaitTime(IoC, NS2BCLK(T_CLK_HIGH)); SK_I2C_GET_SW(IoC, &I2cSwCtrl); if (I2cSwCtrl & I2C_DATA) { Bit = 1; } else { Bit = 0; } I2C_CLK_LOW(IoC); SkDgWaitTime(IoC, NS2BCLK(T_CLK_LOW-T_CLK_2_DATA_OUT)); return(Bit);} /* SkI2cRcvBit *//* * Receive an ACK. * * returns 0 If acknoledged * 1 in case of an error */int SkI2cRcvAck(SK_IOC IoC) /* I/O Context */{ /* * Received bit must be zero. */ return (SkI2cRcvBit(IoC) != 0);} /* SkI2cRcvAck *//* * Send an NACK. */void SkI2cSndNAck(SK_IOC IoC) /* I/O Context */{ /* * Received bit must be zero. */ SkI2cSndBit(IoC, 1);} /* SkI2cSndNAck *//* * Send an ACK. */void SkI2cSndAck(SK_IOC IoC) /* I/O Context */{ /* * Received bit must be zero. * */ SkI2cSndBit(IoC, 0);} /* SkI2cSndAck *//* * Send one byte to the I2C device and wait for ACK. * * Return acknoleged status. */int SkI2cSndByte(SK_IOC IoC, /* I/O Context */int Byte) /* byte to send */{ int i; for (i = 0; i < 8; i++) { if (Byte & (1<<(7-i))) { SkI2cSndBit(IoC, 1); } else { SkI2cSndBit(IoC, 0); } } return(SkI2cRcvAck(IoC));} /* SkI2cSndByte *//* * Receive one byte and ack it. * * Return byte. */int SkI2cRcvByte(SK_IOC IoC, /* I/O Context */int Last) /* Last Byte Flag */{ int i; int Byte = 0; for (i = 0; i < 8; i++) { Byte <<= 1; Byte |= SkI2cRcvBit(IoC); } if (Last) { SkI2cSndNAck(IoC); } else { SkI2cSndAck(IoC); } return(Byte);} /* SkI2cRcvByte *//* * Start dialog and send device address * * Return 0 if acknoleged, 1 in case of an error */int SkI2cSndDev(SK_IOC IoC, /* I/O Context */int Addr, /* Device Address */int Rw) /* Read / Write Flag */{ SkI2cStart(IoC); Rw = ~Rw; Rw &= I2C_WRITE; return(SkI2cSndByte(IoC, (Addr<<1) | Rw));} /* SkI2cSndDev */#endif /* SK_DIAG *//*----------------- I2C CTRL Register Functions ----------*//* * waits for a completion of an I2C transfer * * returns 0: success, transfer completes * 1: error, transfer does not complete, I2C transfer * killed, wait loop terminated. */int SkI2cWait(SK_AC *pAC, /* Adapter Context */SK_IOC IoC, /* I/O Context */int Event) /* complete event to wait for (I2C_READ or I2C_WRITE) */{ SK_U64 StartTime; SK_U32 I2cCtrl; StartTime = SkOsGetTime(pAC); do { if (SkOsGetTime(pAC) - StartTime > SK_TICKS_PER_SEC / 8) { SK_I2C_STOP(IoC);#ifndef SK_DIAG SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E002, SKERR_I2C_E002MSG);#endif /* !SK_DIAG */ return(1); } SK_I2C_GET_CTL(IoC, &I2cCtrl); } while ((I2cCtrl & I2C_FLAG) == (SK_U32)Event << 31); return(0);} /* SkI2cWait *//* * waits for a completion of an I2C transfer * * Returns * Nothing */void SkI2cWaitIrq(SK_AC *pAC, /* Adapter Context */SK_IOC IoC) /* I/O Context */{ SK_SENSOR *pSen; SK_U64 StartTime; SK_U32 IrqSrc; pSen = &pAC->I2c.SenTable[pAC->I2c.CurrSens]; if (pSen->SenState == SK_SEN_IDLE) { return; } StartTime = SkOsGetTime(pAC); do { if (SkOsGetTime(pAC) - StartTime > SK_TICKS_PER_SEC / 8) { SK_I2C_STOP(IoC);#ifndef SK_DIAG SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E016, SKERR_I2C_E016MSG);#endif /* !SK_DIAG */ return; } SK_IN32(IoC, B0_ISRC, &IrqSrc); } while ((IrqSrc & IS_I2C_READY) == 0); pSen->SenState = SK_SEN_IDLE; return;} /* SkI2cWaitIrq */#ifdef SK_DIAG/* * writes a single byte or 4 bytes into the I2C device * * returns 0: success * 1: error */int SkI2cWrite(SK_AC *pAC, /* Adapter Context */SK_IOC IoC, /* I/O Context */SK_U32 I2cData, /* I2C Data to write */int I2cDev, /* I2C Device Address */int I2cReg, /* I2C Device Register Address */int I2cBurst) /* I2C Burst Flag ( 0 || I2C_BURST ) */{ SK_OUT32(IoC, B2_I2C_DATA, I2cData); SK_I2C_CTL(IoC, I2C_WRITE, I2cDev, I2cReg, I2cBurst); return(SkI2cWait(pAC, IoC, I2C_WRITE));} /* SkI2cWrite*//* * reads a single byte or 4 bytes from the I2C device * * returns the word read */SK_U32 SkI2cRead(SK_AC *pAC, /* Adapter Context */SK_IOC IoC, /* I/O Context */int I2cDev, /* I2C Device Address */int I2cReg, /* I2C Device Register Address */int I2cBurst) /* I2C Burst Flag ( 0 || I2C_BURST ) */{ SK_U32 Data; SK_OUT32(IoC, B2_I2C_DATA, 0); SK_I2C_CTL(IoC, I2C_READ, I2cDev, I2cReg, I2cBurst); if (SkI2cWait(pAC, IoC, I2C_READ)) { w_print("I2C Transfer Timeout!\n"); } SK_IN32(IoC, B2_I2C_DATA, &Data); return(Data);} /* SkI2cRead */#endif /* SK_DIAG *//* * read a sensor's value * * This function reads a sensor's value from the I2C sensor chip. The sensor * is defined by its index into the sensors database in the struct pAC points * to. * Returns * 1 if the read is completed * 0 if the read must be continued (I2C Bus still allocated) */int SkI2cReadSensor(SK_AC *pAC, /* Adapter Context */SK_IOC IoC, /* I/O Context */SK_SENSOR *pSen) /* Sensor to be read */{ return((*pSen->SenRead)(pAC, IoC, pSen));} /* SkI2cReadSensor*//* * Do the Init state 0 initialization */static int SkI2cInit0(SK_AC *pAC) /* Adapter Context */{ int i; /* Begin with first sensor */ pAC->I2c.CurrSens = 0; /* Set to mimimum sensor number */ pAC->I2c.MaxSens = SK_MIN_SENSORS;#ifndef SK_DIAG /* Initialize Number of Dummy Reads */ pAC->I2c.DummyReads = SK_MAX_SENSORS;#endif for (i = 0; i < SK_MAX_SENSORS; i ++) { switch (i) { case 0: pAC->I2c.SenTable[i].SenDesc = "Temperature"; pAC->I2c.SenTable[i].SenType = SK_SEN_TEMP; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH0; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW0; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH0; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW0; pAC->I2c.SenTable[i].SenReg = LM80_TEMP_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 1: pAC->I2c.SenTable[i].SenDesc = "Voltage PCI"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH1; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW1; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH1; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW1; pAC->I2c.SenTable[i].SenReg = LM80_VT0_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 2: pAC->I2c.SenTable[i].SenDesc = "Voltage PCI-IO"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH2; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW2; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH2; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW2; pAC->I2c.SenTable[i].SenReg = LM80_VT1_IN; pAC->I2c.SenTable[i].SenInit = SK_FALSE; break; case 3: pAC->I2c.SenTable[i].SenDesc = "Voltage ASIC"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH3; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW3; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH3; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW3; pAC->I2c.SenTable[i].SenReg = LM80_VT2_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 4: pAC->I2c.SenTable[i].SenDesc = "Voltage PMA"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH4; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW4; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH4; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW4; pAC->I2c.SenTable[i].SenReg = LM80_VT3_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 5: pAC->I2c.SenTable[i].SenDesc = "Voltage PHY 2V5"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH5; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW5; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH5; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW5; pAC->I2c.SenTable[i].SenReg = LM80_VT4_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 6: pAC->I2c.SenTable[i].SenDesc = "Voltage PHY B PLL"; pAC->I2c.SenTable[i].SenType = SK_SEN_VOLT; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH6; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW6; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH6; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW6; pAC->I2c.SenTable[i].SenReg = LM80_VT5_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; case 7: pAC->I2c.SenTable[i].SenDesc = "Speed Fan"; pAC->I2c.SenTable[i].SenType = SK_SEN_FAN; pAC->I2c.SenTable[i].SenThreErrHigh = SK_SEN_ERRHIGH; pAC->I2c.SenTable[i].SenThreErrLow = SK_SEN_ERRLOW; pAC->I2c.SenTable[i].SenThreWarnHigh = SK_SEN_WARNHIGH; pAC->I2c.SenTable[i].SenThreWarnLow = SK_SEN_WARNLOW; pAC->I2c.SenTable[i].SenReg = LM80_FAN2_IN; pAC->I2c.SenTable[i].SenInit = SK_TRUE; break; default: SK_ERR_LOG(pAC, SK_ERRCL_INIT | SK_ERRCL_SW, SKERR_I2C_E001, SKERR_I2C_E001MSG); break; } pAC->I2c.SenTable[i].SenValue = 0; pAC->I2c.SenTable[i].SenErrFlag = SK_SEN_ERR_OK; pAC->I2c.SenTable[i].SenErrCts = 0; pAC->I2c.SenTable[i].SenBegErrTS = 0; pAC->I2c.SenTable[i].SenState = SK_SEN_IDLE; pAC->I2c.SenTable[i].SenRead = SkLm80ReadSensor; pAC->I2c.SenTable[i].SenDev = LM80_ADDR; } /* Now we are "INIT data"ed */ pAC->I2c.InitLevel = SK_INIT_DATA; return(0);} /* SkI2cInit0*//* * Do the init state 1 initialization * * initialize the following register of the LM80: * Configuration register: * - START, noINT, activeLOW, noINT#Clear, noRESET, noCI, noGPO#, noINIT * * Interrupt Mask Register 1: * - all interrupts are Disabled (0xff) * * Interrupt Mask Register 2: * - all interrupts are Disabled (0xff) Interrupt modi doesn't matter. * * Fan Divisor/RST_OUT register: * - Divisors set to 1 (bits 00), all others 0s. * * OS# Configuration/Temperature resolution Register: * - all 0s * */static int SkI2cInit1(SK_AC *pAC, /* Adapter Context */SK_IOC IoC) /* I/O Context */{ if (pAC->I2c.InitLevel != SK_INIT_DATA) { /* ReInit not needed in I2C module */ return(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -