📄 smbus.c
字号:
//This function restores the SMBus & the TWI_ISR state machine to normal(①正[平]常的,常[正]态的②普通的,平均的③正规的,标准[称]的④规定的,额定的⑤精神健全//的,智力正常的⑥正交的, 垂直的,法线) after
// we have deliberately(①故意的,有意识的,经过仔细考虑的,慎重的,从容的) generated a bus timeout error (in order to tell the
// Master that something was wrong with his last command).
void SMB_RestoreBus(void)
{
TWCR = 0;//shut down(关闭 停工,停产,停止运转) the peripheral(①周围的,外围的,边缘的②周边的,圆周的③外表面的④末梢的⑥外部设备,外围装置,附加设备 )
TWISR_state = TW_IDLE; //force an init of the state machine
TWAR = BATTERY_ADDR;
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE); // | (1<<TWSTO) re-enable
//Note(①笔录,记下,记录,略记,回想,摘录②注,注解③说明,附注,注记④注意,注目⑤指明, 表明,特别提到⑥对…加注释, 加记号⑦用音符记出) that we must be careful not to generate some kind of bus error
// as a result of waking back up, or we will get into an endless loop
// by generating another bus timeout if the IDLE state doesn't like
// something it sees when it comes back to life.
}
/* *************************************************************************
*
* Individual handlers for SMBus READ-type commands.
*
* When any of these functions is called, the SMBus is in a halted state,
* pending the completion of the called function. If an error is detected
* while processing any of these, a bus timeout error will have to be
* generated to inform the Master that there was a problem. Otherwise,
* if everything is OK, the data that is being requested by the Master
* must be set up in the TW_TxBuf buffer for transmission to the Master.
* The functions below do not need to calculate the PEC value, as this
* will be handled in the function SMB_CmdInterpreter() automatically.
*
* The valid return values for all functions below are:
* 0: error detected; must generate a bus timeout error.
* 1: requested data is present in the TW_TxBuf buffer.
*
************************************************************************* */
void FillResponseInt(unsigned int info)
{
TW_TxBuf[0] = (unsigned char) info;
TW_TxBuf[1] = (unsigned char) (info >> 8);
TW_TxBufIndex = 0;
TW_TxBufCnt = 2;
}
void FillResponseStr(char __flash * source)
{
unsigned char * dest = TW_TxBuf;
unsigned char ctr = 0;
for(;;)
{
if(*dest++ = *source++)
ctr++;
else
break;
}
TW_TxBufIndex = 0;
TW_TxBufCnt = ctr;
}
/* ************************************************** */
unsigned char SMBR_MfrAccess(void) // Cmd # 0
{
FillResponseInt(SMBvar_int[SMBV_MfrAccess]);
return 0;
}
unsigned char SMBR_RemCapAlm(void) // 1
{
FillResponseInt(SMBvar_int[SMBV_RemCapAlm]);
return 0;
}
unsigned char SMBR_RemTimeAlm(void) // 2
{
FillResponseInt(SMBvar_int[SMBV_RemTimeAlm]);
return 0;
}
unsigned char SMBR_BattMode(void) // 3
{
FillResponseInt(SMBvar_int[SMBV_BattMode]);
return 0;
}
unsigned char SMBR_AtRate(void) // 4
{
FillResponseInt(SMBvar_int[SMBV_AtRate]);
return 0;
}
unsigned char SMBR_AtRateTTF(void) // 5
{
unsigned int temp = AtRateTTF();
SMBvar_int[SMBV_AtRateTTF] = temp; //save local copy of result for DEBUG PURPOSES ONLY
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_AtRateTTE(void) // 6
{
unsigned int temp = AtRateTTE();
SMBvar_int[SMBV_AtRateTTE] = temp;
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_AtRateOK(void) // 7
{
unsigned int temp = AtRateOK();
SMBvar_int[SMBV_AtRateOK] = temp;
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_Temperature(void) // 8
{
unsigned int temp = GetTemperature();
SMBvar_int[SMBV_Temperature] = temp;
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_Voltage(void) // 9
{
unsigned int volt = GetVoltage();
SMBvar_int[SMBV_Voltage] = volt;
FillResponseInt(volt);
return 0;
}
unsigned char SMBR_Current(void) // 10
{
signed int current = Current1Sec();
SMBvar_int[SMBV_Current] = (unsigned int) current;
FillResponseInt((unsigned int) current);
return 0;
}
unsigned char SMBR_AvgCurrent(void) // 11
{
signed int current = CCarray_Average();
SMBvar_int[SMBV_AvgCurrent] = (unsigned int) current;
FillResponseInt((unsigned int) current);
return 0;
}
unsigned char SMBR_MaxError(void) // 12
{
FillResponseInt(SMBvar_int[SMBV_MaxError] = 0);
return 0;
}
unsigned char SMBR_RelSOC(void) // 13
{
unsigned int temp = RelativeSOC();
SMBvar_int[SMBV_RelSOC] = temp;
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_AbsSOC(void) // 14
{
unsigned int temp = AbsoluteSOC();
SMBvar_int[SMBV_AbsSOC] = temp;
FillResponseInt(temp);
return 0;
}
unsigned char SMBR_RemCap(void) // 15
{
unsigned int cap = RemainingCap();
FillResponseInt(cap);
SMBvar_int[SMBV_RemCap] = cap;
return 0;
}
unsigned char SMBR_FullChgCap(void) // 16
{
unsigned int cap = FullChgCap();
FillResponseInt(cap);
SMBvar_int[SMBV_FullChgCap] = cap;
return 0;
}
unsigned char SMBR_RunTTE(void) // 17
{
unsigned int temp = TimeToEmpty(0);
FillResponseInt(temp);
SMBvar_int[SMBV_RunTTE] = temp;
return 0;
}
unsigned char SMBR_AvgTTE(void) // 18
{
unsigned int temp = TimeToEmpty(1);
FillResponseInt(temp);
SMBvar_int[SMBV_AvgTTE] = temp;
return 0;
}
unsigned char SMBR_AvgTTF(void) // 19
{
unsigned int temp = AvgTimeToFull();
FillResponseInt(temp);
SMBvar_int[SMBV_AvgTTF] = temp;
return 0;
}
/* ********************************************** */
// These two messages are sent from either a Charger or a Host
unsigned char SMBR_ChgCurrent(void) // 20
{
SMBvariables[SMBV_BattStatus][lobyte] &= 0xF0; //since this cmd is OK, clear Error bits per sbdat110, 4.3.2
FillResponseInt(SMBvar_int[SMBV_ChgCurrent]);
return 0;
}
unsigned char SMBR_ChgVoltage(void) // 21
{
SMBvariables[SMBV_BattStatus][lobyte] &= 0xF0; //since this cmd is OK, clear Error bits per sbdat110, 4.3.2
FillResponseInt(SMBvar_int[SMBV_ChgVoltage]);
return 0;
}
/* ********************************************** */
unsigned char SMBR_BattStatus(void) // 22
{
FillResponseInt(SMBvar_int[SMBV_BattStatus]);
SMBvariables[SMBV_BattStatus][lobyte] &= 0xF0;
return 0;
}
unsigned char SMBR_CycleCount(void) // 23
{
FillResponseInt(SMBvar_int[SMBV_CycleCount]);
return 0;
}
unsigned char SMBR_DesignCap(void) // 24
{
unsigned long temp;
if(SMBvariables[SMBV_BattMode][hibyte] & CAPACITY_MODE) //use mW in calculations
temp = PACK_DESIGNCAPMW;
else
temp = PACK_DESIGNCAPC5;
FillResponseInt(temp);
SMBvar_int[SMBV_DesignCap] = (unsigned int)temp;
return 0;
}
unsigned char SMBR_DesignVolt(void) // 25
{
FillResponseInt(PACK_NOMINALV);
SMBvar_int[SMBV_DesignVolt] = PACK_NOMINALV;
return 0;
}
unsigned char SMBR_SpecInfo(void) // 26
{
FillResponseInt(SMBvar_int[SMBV_SpecInfo]); //! \todo this value is filled in as const by init.
return 0;
}
unsigned char SMBR_MfrDate(void) // 27
{
FillResponseInt(SMBvar_int[SMBV_MfrDate]); //! \todo this value is filled in as const by init.
return 0;
}
unsigned char SMBR_SerialNo(void) // 28
{
FillResponseInt(SMBvar_int[SMBV_SerialNo]); //! \todo this value is filled in as const by init.
return 0;
}
unsigned char SMBR_MfrName(void) // 32
{
FillResponseStr(str_MfrName); //! \todo Modify as needed. __flash char defined in smbus.h
return 0;
}
unsigned char SMBR_DeviceName(void) // 33
{
FillResponseStr(str_DeviceName); //! \todo Modify as needed. __flash char defined in smbus.h
return 0;
}
unsigned char SMBR_DeviceChem(void) // 34
{
FillResponseStr(str_DeviceChem); //! \todo Modify as needed. __flash char defined in smbus.h
return 0;
}
unsigned char SMBR_MfrData(void) // 35
{
FillResponseStr(str_MfrData); //! \todo Modify as needed. __flash char defined in smbus.h
return 0;
}
unsigned char SMBR_Opt5(void) // 0x2F
{
FillResponseInt(12345); //! \todo this value is defined as a constant here.
return 0;
// return SMBerr_ReservedCommand;
}
unsigned char SMBR_Opt4(void) // 0x3C (serves as Calibration control)
{
FillResponseInt( calibration_state ); // Prepare current state in TX buf.
return 0; // Return "OK, there are data to transmitted".
}
unsigned char SMBR_Opt3(void) // 0x3D
{
return SMBerr_ReservedCommand; //unused
}
unsigned char SMBR_Opt2(void) // 0x3E
{
return SMBerr_ReservedCommand; //unused
}
unsigned char SMBR_Opt1(void) // 0x3F
{
return SMBerr_ReservedCommand; //unused
}
unsigned char SMBR_invalid(void) //This should never execute, if error is caught early!
{
return SMBerr_UnsuptdCommand;
}
//typedef unsigned char (*ptr2funcUC_V)(void);
//Table of pointers to functions, indexed from the received SMBus Command byte.
ptr2funcUC_V SMB_ReadCmd[HIGHEST_SMB_CMD+1] =
{
SMBR_MfrAccess, // 0
SMBR_RemCapAlm, // 1
SMBR_RemTimeAlm, // 2
SMBR_BattMode, // 3
SMBR_AtRate, // 4
SMBR_AtRateTTF, // 5
SMBR_AtRateTTE, // 6
SMBR_AtRateOK, // 7
SMBR_Temperature, // 8
SMBR_Voltage, // 9
SMBR_Current, // 10
SMBR_AvgCurrent, // 11
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -