5

I have a i2c driver which writes to the i2c Bus:

  • The register address of the slave (temperature sensor)

  • The value which have to be written into this address

Also I perform a check (read back) of the value to ensure it is the correct one. Everything works fine on the Hardware. I'm using a Software in the Loop environment and Unit Tests so that I can execute my code without the Hardware. To be able to read back the value from the register, I have to "simulate" the the content of the Data Receive Register(the value which is read back from the I2C slave).

Because, I'm not very experienced with low level programming, any idea how to do this in my case?

Here is the code snipet:

typedef struct
   uint8      address;    // The register address of the slave
   uint8      value;      //The value which have to be writte into the   register 
} MessageStruct;

MessageStruct *pMessage
uint8 Readback = 0;

Write_Toc_I2C( pMessage->address, pMessage->value );   // Function which writes to ic2 bus. For definition see below.

Readback = Read_From_I2c( pMessage->address);         // I store the read back value in a variable Readback. Declaration of Read_From_I2c() below

 if( Readback == pMessage->value )     /** In this block i perform the check of the red back value. Because this is executed in SIL environment, Readback is 0
    {
       status = OK;
    }
else
    {
       status = NOT_OK,
    }


LOCAL void Write_Toc_I2C( uint8 address, uint8 address )   // Write to I2C Function
{
    ...
    ...
    ...
       I2C_HW_Instance()->I2C_DATA_TRANSMIT_REGISTER  = address;
       I2C_Block_For_Event(I2C_DATA_TRANSMIT_READY);               /*I2C_DATA_TRANSMIT_READY is the status flag*/
       I2C_HwInstance()->I2C_DATA_TRANSMIT_REGISTER  = value;
       I2C_Block_For_Event(I2C_DATA_TRANSMIT_READY);               /*I2C_DATA_TRANSMIT_READY is the status flag*/
       I2C_HwInstance()->I2C_MODE_REGISTER = I2C_STOP;
}

Read_From_I2c( uint8 address )    // Read from I2C Function
{
...
...
   I2C_HW_Instance()->I2C_DATA_TRANSMIT_REGISTER  =  address;
   I2C_Block_For_Event(I2C_DATA_TRANSMIT_READY);
...
...
...
   I2C_Block_For_Event(I2C_RECEIVE_REGISTER);
   return ((uint8) I2C_HwInstance()->I2C_DATA_RECEIVE_REGISTER);
}

static inline I2C_Base_Register *I2C_HW_Instance( void )
{
   static I2C_Base_Register *pToBaseReg;

   #if (UTEST == ON)
   {
      static I2C_Base_Register BaseReg;

      pToBaseReg = &BaseReg;
   }
   #else
      pToBaseReg = (I2C_Base_Register *)Register_Of_I2c_Adress;
   #endif

   return (pToBaseReg);
} 

static inline void I2C_Block_For_Event( i2cStatusFlag evt )
{
   #if !defined(__SIL__)         // Software in the Loop
   while((i2c_HwInstance()->I2C_INTERRUPT_STATUS_REGISTER & evt) == 0)
   {};

   #elif defined UTEST || __SIL__
   // hmmm, what to do?
   #endif


typedef volatile struct I2C_BASE
{
// Here are the registers listed, there are 32, but I listed only the relevant one

         uint8 I2C_DATA_RECEIVE_REGISTER;            /*I2C Data Receive register */
         uint8 I2C_DATA_TRANSMIT_REGISTER;           /*I2C Data Transmit register */           
         uint32 I2C_MODE_REGISTER;                   /*0x0024 I2C Mode register  */                    
         uint32 I2C_INTERRUPT_STATUS_REGISTER;       /*0x0008 I2C Interrupt Status register */      

} I2C_Base_Register;
JohnDoe
  • 825
  • 1
  • 13
  • 31

0 Answers0