⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smbus.c

📁 iic for avr very good
💻 C
📖 第 1 页 / 共 4 页
字号:
    }

    //The rcvd message is valid enough to warrant calling the command's handler now.


    //Note that none of the regular SMBus commands use Block Mode to send info
    // *TO* the Smart Battery, so TW_RxBuf[2] will NEVER be a byte count, but
    // will always be DATA.  For OptionalMfgFunction(5), the associated command
    // function itself is aware that [2] is the byte count and that the actual
    // data starts at offset=[3].
    TW_RxBufIndex = 2;			//point to the first byte of Received Data.


    if(0 != SMB_WriteCmd[CurrentCmd]())	//After interpreting, was there an error??
    {
      TW_RxBufIndex = 0;		//Wipe out anything in the buffer, just in case.
      TW_RxBufCnt = 0;			

      //start the 26mS timer to generate a bus timeout error.
      SetGenericTimer(SMBfaultTimer, 26);
      TWI_CmdFlags = 0;			//clear the flag that brought us here.
      return;
    }
    //At this point it looks like everything went OK.
    TWI_CmdFlags = 0;			//clear the flag that brought us here.
    return;
  }
}





//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
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -