Example #1
0
bool i2cWriteBuffer(uint8_t addr, uint8_t reg, uint8_t len, uint8_t * data)
{
    int i;
    if (!I2C_Start())
        return false;
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) 
    {
        I2C_Stop();
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    for (i = 0; i < len; i++) 
    {
        I2C_SendByte(data[i]);
        if (!I2C_WaitAck()) 
        {
            I2C_Stop();
            return false;
        }
    }
    I2C_Stop();
    return true;
}
Example #2
0
/**
 * @brief  I2C write mutiple data
 * @param[in]  instance instance of i2c moudle
 * \param[in]  chipAddr    i2c slave addr
 * \param[in]  addr        i2c slave register offset
 * \param[in]  addrLen     len of slave register addr(in byte)
 * \param[in]  buf         data buf
 * \param[in]  len         data length
 * \retval 0 success
 * \retval 1 failure
 */
int I2C_BurstWrite(uint32_t instance ,uint8_t chipAddr, uint32_t addr, uint32_t addrLen, uint8_t *buf, uint32_t len)
{
    uint8_t *p;
    uint8_t err;
    
    p = (uint8_t*)&addr;
    err = 0;
    chipAddr <<= 1;
    
    I2C_Start();
    I2C_SendByte(chipAddr);
    err += I2C_WaitAck();

    while(addrLen--)
    {
        I2C_SendByte(*p++);
        err += I2C_WaitAck();
    }
    
    while(len--)
    {
        I2C_SendByte(*buf++);
        err += I2C_WaitAck();  
    }

    I2C_Stop();
    return err;
}
Example #3
0
int I2C_SW::read_regs(uint8_t SlaveAddress, uint8_t startRegister, uint8_t*out, int count)
{
	int i;

	if (!I2C_Start()) {
			return -1;
	}

	I2C_SendByte(SlaveAddress&0xFE);

	if (!I2C_WaitAck()) {
			I2C_Stop();
			return -1;
	}

	I2C_SendByte(startRegister);
	I2C_WaitAck();

	I2C_Start();
	I2C_SendByte((SlaveAddress&0xFE)|0x01);
	I2C_WaitAck();

	for(i=0; i<count; i++)
	{
		out[i] = I2C_ReceiveByte();
		if (i==count-1)
			I2C_SendNoAck();
		else
			I2C_SendAck();
	}

	I2C_Stop();
	
	return 0;
}
Example #4
0
bool i2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
{
    if (!I2C_Start())
        return false;
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) {
        I2C_Stop();
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    I2C_Start();
    I2C_SendByte(addr << 1 | I2C_Direction_Receiver);
    I2C_WaitAck();
    while (len) {
        *buf = I2C_ReceiveByte();
        if (len == 1)
            I2C_NoAck();
        else
            I2C_Ack();
        buf++;
        len--;
    }
    I2C_Stop();
    return true;
}
Example #5
0
bool i2cRead(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
{
    UNUSED(device);
    
    if (!I2C_Start()) {
        return false;
    }
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) {
        I2C_Stop();
        i2cErrorCount++;
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    I2C_Start();
    I2C_SendByte(addr << 1 | I2C_Direction_Receiver);
    I2C_WaitAck();
    while (len) {
        *buf = I2C_ReceiveByte();
        if (len == 1) {
            I2C_NoAck();
        }
        else {
            I2C_Ack();
        }
        buf++;
        len--;
    }
    I2C_Stop();
    return true;
}
Example #6
0
int I2C_SW::write_regs(uint8_t SlaveAddress, uint8_t startRegister, const uint8_t*data, int count)
{
	int i;
    if (!I2C_Start()) {
        return -1;
    }

    I2C_SendByte(SlaveAddress&0xFE);

    if (!I2C_WaitAck()) {
        I2C_Stop();
        return -1;
    }

    I2C_SendByte(startRegister&0xFF);
    I2C_WaitAck();

    for(i=0; i<count; i++)
    {
        I2C_SendByte(data[i]&0xFF);
        
        if (!I2C_WaitAck())
        {
            I2C_Stop();
            return -1;
        }
    }

    I2C_Stop();

    return 0;
}
Example #7
0
bool i2cWriteBuffer(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t * data)
{
    UNUSED(device);
    
    int i;
    if (!I2C_Start()) {
        i2cErrorCount++;
        return false;
    }
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) {
        I2C_Stop();
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    for (i = 0; i < len; i++) {
        I2C_SendByte(data[i]);
        if (!I2C_WaitAck()) {
            I2C_Stop();
            i2cErrorCount++;
            return false;
        }
    }
    I2C_Stop();
    return true;
}
Example #8
0
int SCCB_ReadSingleRegister(uint32_t instance, uint8_t chipAddr, uint8_t subAddr, uint8_t* pData)
{
    uint32_t time_out = 0;
    I2C_GenerateSTART(instance);
    /* Send 7bit Data with WRITE operation */
    I2C_Send7bitAddress(instance, chipAddr, kI2C_Write);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance) && (time_out < 10000))
        {
            time_out++;
        }
        return 1;
    }
    /* send reg address */
    I2C_SendData(instance, subAddr);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance)  && (time_out < 10000))
        {
            time_out++;
        }
        return 2;
    }
    I2C_GenerateSTOP(instance);
    while(!I2C_IsBusy(instance));
    time_out = 0;
    /* generate START signal */
    I2C_GenerateSTART(instance);
    /* resend 7bit Address, This time we use READ Command */
    I2C_Send7bitAddress(instance, chipAddr, kI2C_Read);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance));
        return 3;
    }
    /* set master in slave mode */
    I2C_SetMasterMode(instance,kI2C_Read);
    /* dummy read */
    I2C_ReadData(instance);
	I2C_GenerateAck(instance);
	I2C_WaitAck(instance);
    *pData = I2C_ReadData(instance);
    /* stop and finish */
    I2C_GenerateNAck(instance);
    I2C_WaitAck(instance);
    I2C_GenerateSTOP(instance);
    while(!I2C_IsBusy(instance));
    return 0;
}
Example #9
0
Status TMP102_Init(void)//初始化TMP102
{
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send start------------------1
    if(I2C_Start()!=BUS_READY){
        ERROR_MACRO(TMP102_DEV,INT_ERROR_SEND_START);
        return ERROR_FAILED;
    }

    //send SlaveADDR with write
    I2C_SendByte(TMP102_WRITE_ADDR);//------------------2
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,INT_ERROR_SEND_DEVADDR);//------------------3
        return ERROR_FAILED;
    }

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send CONF_ADDR------------------4
    I2C_SendByte(TMP102_CONF_REG);
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,INT_ERROR_SEND_CONFADDR);//------------------5
        return ERROR_FAILED;
    }

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //写Configuration Register 12位温度 连续转换//------------------6
    I2C_SendByte(0x60);
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,INT_ERROR_WRITE_CONFA);//------------------7
        return ERROR_FAILED;
    }

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    I2C_SendByte(TMP102_8HZ);//------------------8
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,INT_ERROR_WRITE_CONFB);//------------------9
        return ERROR_FAILED;
    }

    //send stop
    I2C_Stop();//------------------10
    return OK_PASS;
}
Example #10
0
/**
 * @brief  设置i2c在主模式下的连续数据写操作
 * @code
 *     //使用i2c的1模块将data中的10字节数据写入地址为0x55的从机中起始地址为0x01的设备宏
 *    I2C_BurstWrite(HW_I2C1, 0x55, 0x01, 1, &data, 10);
 * @endcode
 * @param  instance :I2C模块号 
 *         @arg HW_I2C0  :I2C0模块
 *         @arg HW_I2C1  :I2C1模块
 *         @arg HW_I2C2  :I2C2模块
 * @param  deviceAddress :从机设备地址0~127
 * @param  subAddress    :写入的起始地址
 * @param  subAddressLen :地址的长度
 * @param  buf         :保存数据的首地址
 * @param  len       :写入数据的长度
 * @retval 数据的读取状态 0 ;成功 其它 :失败
 */
uint8_t I2C_BurstWrite(uint32_t instance ,uint8_t deviceAddress, uint32_t subAddress, uint32_t subAddressLen, uint8_t *buf, uint32_t len)
{
    uint32_t time_out = 0;
    uint8_t * p = (uint8_t*)&subAddress;
    /* generate START signal */
    I2C_GenerateSTART(instance);
    /* send 7bit Data with WRITE operation */
    I2C_Send7bitAddress(instance, deviceAddress, kI2C_Write);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance) && (time_out < 10000))
        {
            time_out++;
        }
        return 1;
    }
    /* send all address */
    while(subAddressLen--)
    {
        I2C_SendData(instance, *(p++));
        if(I2C_WaitAck(instance))
        {
            I2C_GenerateSTOP(instance);
            time_out = 0;
            while(!I2C_IsBusy(instance) && (time_out < 10000))
            {
                time_out++;
            }
            return 2;
        }
    }
    /* send all data */
    while(len--)
    {
        I2C_SendData(instance, *(buf++));
        if(I2C_WaitAck(instance))
        {
            I2C_GenerateSTOP(instance);
            time_out = 0;
            while(!I2C_IsBusy(instance) && (time_out < 10000))
            {
                time_out++;
            }
            return 3;
        }
    }
    /* generate stop and wait for line idle */
    I2C_GenerateSTOP(instance);
    time_out = 0;
    while(!I2C_IsBusy(instance));
    return 0;
}
Example #11
0
/********************单字节写入***********************/
uint8_t MPU6050_WriteByte(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data)		   
{
	if(!I2C_Start())return 0;
    I2C_SendByte(SlaveAddress);   //发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 
    if(!I2C_WaitAck()){I2C_Stop(); return 0;}
    I2C_SendByte(REG_Address );   //设置低起始地址      
    if(!I2C_WaitAck()){I2C_Stop(); return 0;}//I2C_WaitAck();	
    I2C_SendByte(REG_data);
    if(!I2C_WaitAck()){I2C_Stop(); return 0;}//I2C_WaitAck();   
    I2C_Stop(); 
    return 1;
}
Example #12
0
uint16_t Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data)		     //void
{
  	if(!I2C_Start())return FALSE;
    I2C_SendByte(SlaveAddress);   //发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址 
    if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
    I2C_SendByte(REG_Address );   //设置低起始地址      
    I2C_WaitAck();	
    I2C_SendByte(REG_data);
    I2C_WaitAck();   
    I2C_Stop(); 
    delay5ms();
    return TRUE;
}
Example #13
0
bool Single_Write(unsigned char SlaveAddress,unsigned char REG_Address,unsigned char REG_data)		     //void
{
  	if(!I2C_Start())return FALSE;
    I2C_SendByte(SlaveAddress);   //发送设备地址+写信号
    if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
    I2C_SendByte(REG_Address );   //设置低起始地址      
    I2C_WaitAck();	
    I2C_SendByte(REG_data);
    I2C_WaitAck();   
    I2C_Stop(); 
    delay5ms();
    return TRUE;
}
Example #14
0
bool i2cWrite(uint8_t addr, uint8_t reg, uint8_t data)
{
    if (!I2C_Start())
        return false;
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) {
        I2C_Stop();
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    I2C_SendByte(data);
    I2C_WaitAck();
    I2C_Stop();
    return true;
}
Example #15
0
/***********************************************************
* Function:       // 函数名称
* Description:    // 函数功能、性能等的描述
* Input:          // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系
* Input:          // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系
* Output:         // 1.输出参数1,说明
* Return:         // 函数返回值的说明
* Others:         // 其它说明
***********************************************************/
static uint8_t IIC_RegRead( uint8_t address, uint8_t reg, uint8_t *value )
{
    uint8_t err = ERR_NONE;

    err = I2C_Start( );
    if( err )
    {
        goto lbl_ERR_REG_RD_STOP;
    }
    I2C_SendByte( address << 1 );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_RD_STOP;
    }

    I2C_SendByte( reg );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_RD_STOP;
    }

    err = I2C_Start( );
    if( err )
    {
        goto lbl_ERR_REG_RD;
    }

    I2C_SendByte( ( address << 1 ) | 0x01 );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_RD_STOP;
    }

    *value = I2C_ReceiveByte( );
    I2C_NoAck( );
    I2C_Stop( );
    return ERR_NONE;

lbl_ERR_REG_RD_STOP:
    I2C_Stop( );
lbl_ERR_REG_RD:
    //rt_kprintf( "reg_rd error=%02x reg=%02x\r\n", err, reg );
    return err;
}
Example #16
0
void WriteI2C(u8 ch, u8 address, u8 WriteAddress)
{
	I2C_Start();			// 启动总线,开始传输数据;
	I2C_SendByte(WriteAddress);			I2C_WaitAck();
	//while(TestAck());		// 发送从器件硬件地址;
	I2C_SendByte(address);				I2C_WaitAck();
	//while(TestAck());		// 发送从器件存储器字节地址;
	I2C_SendByte(ch);					I2C_WaitAck();
	//while(TestAck());		// 发送数据;
#if ENABLE_I2C_DELAY
	I2C_delay();
#endif
	I2C_Stop();				// 发送停止位,发送数据结束;
#if ENABLE_I2C_DELAY
	I2C_delay();
#endif
}
Example #17
0
Status HAL_I2C_WaitAck(void) 	 //返回为:=1有ACK,=0无ACK
{
#ifdef USE_I2C_OLD
    return I2C_WaitAck()
#else
    return IIC_Wait_Ack();
#endif
}
Example #18
0
/******************************************************************************
/ 函数功能:单字节写入
/ 修改日期:none
/ 输入参数:
/   @arg SlaveAddress   从器件地址
/   @arg REG_Address    寄存器地址
/ 输出参数: 读出的字节数据
/ 使用说明:这时一个完整的单字节读取函数
******************************************************************************/
uint8_t Single_Read(uint8_t SlaveAddress,uint8_t REG_Address)
{   
    uint8_t REG_data;       
    I2C_Start();//if(!I2C_Start())return 0
    I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 
    I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();return 0;}
    I2C_SendByte((uint8_t) REG_Address);   //设置低起始地址      
    I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();return 0;}
    I2C_Start();//if(!I2C_Start())return 0;
    I2C_SendByte(SlaveAddress+1);
	
    I2C_WaitAck();//if(!I2C_Start())return 0;
    REG_data = I2C_ReadByte();
    I2C_NoAck();
    I2C_Stop();
    return REG_data;
}
Example #19
0
//单字节读取*****************************************
unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address)
{   unsigned char REG_data;     	
	if(!I2C_Start())return FALSE;
    I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 
    if(!I2C_WaitAck()){I2C_Stop(); return FALSE;}
    I2C_SendByte((u8) REG_Address);   //设置低起始地址      
    I2C_WaitAck();
    I2C_Start();
    I2C_SendByte(SlaveAddress+1);
    I2C_WaitAck();

	REG_data= I2C_ReadByte();
    I2C_NoAck();
    I2C_Stop();
	return REG_data;

}
Example #20
0
/***********************单字节读取******************/
uint8_t MPU6050_ReadByte(uint8_t SlaveAddress,uint8_t REG_Address)
{   
	unsigned char REG_data;     	
	I2C_Start();//if(!I2C_Start())return 0
    I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 
    I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();test=1; return 0;}
    I2C_SendByte((uint8_t) REG_Address);   //设置低起始地址      
    I2C_WaitAck();  //if(!I2C_WaitAck()){I2C_Stop(); return 0;}
		
    I2C_Start();//if(!I2C_Start())return 0;
    I2C_SendByte(SlaveAddress+1);
    I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop(); return 0;}
    REG_data= I2C_ReadByte();
    I2C_NoAck();
    I2C_Stop();
	return REG_data;
}						      
Example #21
0
u8 ReadI2C(u8 address, u8 WriteAddress)	// 读出1串数据
{
	u8 ch;				// 定义存储读出数据的临时变量;

	I2C_Start();		// 启动总线,开始传输数据;
	I2C_SendByte(WriteAddress);
	I2C_WaitAck();		// 发送从器件硬件地址;
	I2C_SendByte(address);
	I2C_WaitAck();		// 发送从器件内部数据存储器的地址;
	I2C_Start();		// 重新启动总线,开始传输数据;
	I2C_SendByte(WriteAddress + 1);
	I2C_WaitAck();		// 发送从器件内部数据存储器的地址;
	ch = I2C_ReceiveByte();
	I2C_NoAck();		// 将读出的一个字节数据存入临时变量,发送非应答位;
	I2C_Stop();			// 发送停止信号,释放总路线;

	return (ch);
}
Example #22
0
/*******************************************************************************
* Function Name  : I2C_ReadByte
* Description    : 读取一串数据
* Input          : - pBuffer: 存放读出数据
*           	   - length: 待读出长度
*                  - ReadAddress: 待读出地址
*                  - DeviceAddress: 器件类型
* Output         : None
* Return         : 返回为:=1成功读入,=0失败
* Attention		 : None
*******************************************************************************/          
int I2C_ReadByte(uint8_t* pBuffer,   uint16_t length,   uint8_t ReadAddress,  uint8_t DeviceAddress)
{	
    if(!I2C_Start())
	{
	    return DISABLE;
	}
    I2C_SendByte( DeviceAddress );         /* 器件地址 */
    if( !I2C_WaitAck() )
	{
		I2C_Stop(); 
		return DISABLE;
	}
    I2C_SendByte( ReadAddress );           /* 设置低起始地址 */      
    I2C_WaitAck();	
    I2C_Stop(); 
	
    if(!I2C_Start())
	{
		return DISABLE;
	}
    I2C_SendByte( DeviceAddress + 1 );               /* 器件地址 */ 
    if(!I2C_WaitAck())
	{
		I2C_Stop(); 
		return DISABLE;
	}
    while(length)
    {
      *pBuffer = I2C_ReceiveByte();
      if(length == 1)
	  {
		  I2C_NoAck();
	  }
      else
	  {
		I2C_Ack(); 
	  }
      pBuffer++;
      length--;
    }
    I2C_Stop();
    return ENABLE;
}
Example #23
0
bool i2cWrite(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t data)
{
    UNUSED(device);
    
    if (!I2C_Start()) {
        return false;
    }
    I2C_SendByte(addr << 1 | I2C_Direction_Transmitter);
    if (!I2C_WaitAck()) {
        I2C_Stop();
        i2cErrorCount++;
        return false;
    }
    I2C_SendByte(reg);
    I2C_WaitAck();
    I2C_SendByte(data);
    I2C_WaitAck();
    I2C_Stop();
    return true;
}
Example #24
0
/**
 * @brief  proble i2c bus
 * @param[in]  instance instance of i2c moudle
 * \param[in]  chipAddr i2c slave addr
 * @note   see if it's available i2c slave on the bus
 * \retval 0 success
 * \retval 1 failure
 */
int I2C_Probe(uint32_t instance, uint8_t chipAddr)
{
    uint8_t err;
    
    err = 0;
    chipAddr <<= 1;
    
    I2C_Start();
    I2C_SendByte(chipAddr);
    err = I2C_WaitAck();
    I2C_Stop();
    return err;
}
Example #25
0
/***********************************************************
* Function:       // 函数名称
* Description:    // 函数功能、性能等的描述
* Input:          // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系
* Input:          // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系
* Output:         // 1.输出参数1,说明
* Return:         // 函数返回值的说明
* Others:         // 其它说明
***********************************************************/
static uint8_t IIC_RegWrite( uint8_t address, uint8_t reg, uint8_t val )
{
    uint8_t err = ERR_NONE;
    err = I2C_Start( );
    if( err )
    {
        goto lbl_ERR_REG_WR;
    }
    I2C_SendByte( MMA845X_ADDR << 1 );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_WR_STOP;
    }

    I2C_SendByte( reg );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_WR_STOP;
    }

    I2C_SendByte( val );
    err = I2C_WaitAck( );
    if( err )
    {
        goto lbl_ERR_REG_WR_STOP;
    }
    I2C_Stop( );
    //todo:这里需要延时5ms吗?
    return ERR_NONE;

lbl_ERR_REG_WR_STOP:
    I2C_Stop( );
lbl_ERR_REG_WR:
    //rt_kprintf( "reg_wr error=%02x reg=%02x value=%02x\r\n", err, reg, val );
    return err;
}
Example #26
0
/*******************************************************************************
* Function Name  : I2C_WriteByte
* Description    : 写一字节数据
* Input          : - WriteAddress: 待写入地址
*           	   - SendByte: 待写入数据
*                  - DeviceAddress: 器件类型
* Output         : None
* Return         : 返回为:=1成功写入,=0失败
* Attention		 : None
*******************************************************************************/           
int I2C_WriteByte( uint16_t WriteAddress , uint8_t SendByte , uint8_t DeviceAddress)
{		
    if(!I2C_Start())
	{
	    return DISABLE;
	}
	I2C_delay();
    I2C_SendByte( DeviceAddress );                    /* 器件地址 */
    if( !I2C_WaitAck() )
	{
		I2C_Stop(); 
		return DISABLE;
	}
	I2C_delay();
    I2C_SendByte((uint8_t)(WriteAddress & 0x00FF));   /* 设置低起始地址 */      
    I2C_WaitAck();
	I2C_delay();	
    I2C_SendByte(SendByte);
    I2C_WaitAck();
	I2C_delay();   
    I2C_Stop(); 
	I2C_delay();
    return ENABLE;
}									 
Example #27
0
/**
 * @brief  SCCB(protocol,the same as i2c) read single register value
 * \param[in]  instance   instance of i2c moudle
 * \param[in]  chipAddr   i2c slave addr
 * \param[in]  addr       i2c slave register offset
 * \param[out] data       data pointer
 * @note   usually used on i2c sensor devices
 * \retval 0 success
 * \retval 1 failure
 */
int SCCB_ReadSingleRegister(uint32_t instance, uint8_t chipAddr, uint8_t addr, uint8_t* data)
{
    uint8_t err;
    uint8_t retry;
    
    retry = 10;
    chipAddr <<= 1;
    
    while(retry--)
    {
        err = 0;
        I2C_Start();
        I2C_SendByte(chipAddr);
        err += I2C_WaitAck();
        
        I2C_SendByte(addr);
        err += I2C_WaitAck();
        
        I2C_Stop();
        I2C_Start();
        I2C_SendByte(chipAddr+1);
        err += I2C_WaitAck();
        
        *data = I2C_GetByte();
       // err += I2C_WaitAck();
        
        I2C_NAck();
        I2C_Stop();
        if(!err)
        {
            break;
        }
    }

    return err;
}
Example #28
0
/**
 * @brief  设置i2c在主模式下的连续数据读操作
 * @code
 *     //使用i2c的1模块读取数据地址为0x55的从机中起始地址为0x01的数据10字节,存储在data的首地址
 *    I2C_BurstRead(HW_I2C1, 0x55, 0x01, 1, &data, 10);
 * @endcode
 * @param  instance :I2C模块号 
 *         @arg HW_I2C0  :I2C0模块
 *         @arg HW_I2C1  :I2C1模块
 *         @arg HW_I2C2  :I2C2模块
 * @param  deviceAddress :从机设备地址0~127
 * @param  subAddress    :读取的起始地址
 * @param  subAddressLen :地址的长度
 * @param  buf         :保存数据的首地址
 * @param  len       :读取数据的长度
 * @retval 数据的读取状态 0 ;成功 其它 :失败
 */
int32_t I2C_BurstRead(uint32_t instance, uint8_t deviceAddress, uint32_t subAddress, uint32_t subAddressLen, uint8_t* buf, uint32_t len)
{
    uint32_t time_out = 0;
    uint8_t i;
    uint8_t * p = (uint8_t*)&subAddress;
    /* generate START signal */
    I2C_GenerateSTART(instance);
    /* Send 7bit Data with WRITE operation */
    I2C_Send7bitAddress(instance, deviceAddress, kI2C_Write);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance) && (time_out < 10000))
        {
            time_out++;
        }
        return 1;
    }
    /* if len = 0 then return */
    if(!len)
    {
        I2C_GenerateSTOP(instance);
        while(!I2C_IsBusy(instance));
        return 0;
    }
    /* send sub address */
    for(i = 0; i < subAddressLen; i++)
    {
        /* send reg address */
        I2C_SendData(instance, *p++);
        if(I2C_WaitAck(instance))
        {
            I2C_GenerateSTOP(instance);
            time_out = 0;
            while(!I2C_IsBusy(instance)  && (time_out < 10000))
            {
                time_out++;
            }
            return 2;
        }
    }
    /* generate RESTART signal */
    I2C_GenerateRESTART(instance);
    /* resend 7bit Address, This time we use READ Command */
    I2C_Send7bitAddress(instance, deviceAddress, kI2C_Read);
    if(I2C_WaitAck(instance))
    {
        I2C_GenerateSTOP(instance);
        time_out = 0;
        while(!I2C_IsBusy(instance));
        return 3;
    }
    /* set master in slave mode */
    I2C_SetMasterMode(instance,kI2C_Read);
    /* dummy read */
    I2C_ReadData(instance); //dummy read
	I2C_GenerateAck(instance);
	I2C_WaitAck(instance);
    /* actually read */
	for(i = 0; i < len - 1; i++)
	{
		*(buf++) = I2C_ReadData(instance);
		I2C_GenerateAck(instance);
        I2C_WaitAck(instance);
	}
    /* read last */
	*buf = I2C_ReadData(instance);
    /* stop and finish */
    I2C_GenerateNAck(instance);
    I2C_WaitAck(instance);
    I2C_GenerateSTOP(instance);
    while(!I2C_IsBusy(instance));
    return 0;
}
Example #29
0
Status ReadTMP102(u16 *TO_EEPROM,u16 *Data_ptr)//读取tmp101的2个字节温度
{
    u16 TMP102_Data=0x0000;


    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	


    //send start------------------1
    if(I2C_Start()!=BUS_READY){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_START);
        return ERROR_FAILED;
    }


    //send SlaveADDR with write
    I2C_SendByte(TMP102_WRITE_ADDR);//------------------2



    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_DEVADDR);//------------------3
        return ERROR_FAILED;
    }



	


    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send CONF_ADDR------------------4
    I2C_SendByte(TMP102_TEMP_REG);
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_TEMPADDR);//------------------5
        return ERROR_FAILED;
    }

    I2C_Stop();//------------------NiMa没看到!!!!!!!!!!!!!!

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send start------------------6
    if(I2C_Start()!=BUS_READY){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_RESTART);
        return ERROR_FAILED;
    }

    //send SlaveADDR with write
    I2C_SendByte(TMP102_READ_ADDR);//------------------7
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_RDEVADDR);//------------------8
        return ERROR_FAILED;
    }

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //read TEMP
    TMP102_Data=0x0000;

    TMP102_Data = I2C_ReceiveByte();//--Byte1-------9
    I2C_Ack();//------------------10


    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    TMP102_Data<<=8;
    TMP102_Data |= I2C_ReceiveByte();//---Byte2-----11
    I2C_Ack();//------------------12

    //send stop
    I2C_Stop();//------------------13

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~   Done   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    TMP102_Data>>=4;


    *Data_ptr = TMP102_Data;

    //==============================================================
    //-25~80



    if(TMP102_Data&(0x800)){
        //负数要转换
        TMP102_Data-=(0x0E70-0x0500-1);
    }

    if(TMP102_Data<=0x690){
        *TO_EEPROM = TMP102_Data*4;
    }
    else{
        printf("Out of range from TMP102,TMP102=%X\t\r\n",*Data_ptr);
        *TO_EEPROM = TO_EEPROM_ERROR;
    }

    //printf("TMP102=%X\tEEPROM=%X\r\n",*Data_ptr,*TO_EEPROM);
    return OK_PASS;
}
Example #30
0
u16 ReadTMP102_REG(unsigned char reg)
{
    u16 TMP102_Data=0x0000;


    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 1 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/



    //send start------------------1
    if(I2C_Start()!=BUS_READY){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_START);
    }


    //send SlaveADDR with write
    I2C_SendByte(TMP102_WRITE_ADDR);//------------------2
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_DEVADDR);//------------------3
    }






    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send CONF_ADDR------------------4
    I2C_SendByte(reg);
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_TEMPADDR);//------------------5
    }

    I2C_Stop();//------------------NiMa没看到!!!!!!!!!!!!!!

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //send start------------------6
    if(I2C_Start()!=BUS_READY){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_RESTART);
        return ERROR_FAILED;
    }

    //send SlaveADDR with write
    I2C_SendByte(TMP102_READ_ADDR);//------------------7
    if(I2C_WaitAck()!=BUS_ACK){
        ERROR_MACRO(TMP102_DEV,RTMP_ERROR_SEND_RDEVADDR);//------------------8
        return ERROR_FAILED;
    }

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    //read TEMP
    TMP102_Data=0x0000;

    TMP102_Data = I2C_ReceiveByte();//--Byte1-------9
    I2C_Ack();//------------------10

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~  Frame 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    TMP102_Data=(TMP102_Data<<8);
    TMP102_Data |=(u8)I2C_ReceiveByte();//---Byte2-----11
    I2C_Ack();//------------------12

    //send stop
    I2C_Stop();//------------------13

    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~   Done   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    return TMP102_Data;

}