Exemplo n.º 1
1
/**
  * @brief read a block of data from the M24LR16E EEPROM
	* @param[in] EEPROM's internal address to read from
	* @param[in] number of bytes to read from the EEPROM
  * @param[in] pointer to the buffer that receives the data read from the EEPROM
  * @retval None
  */
void M24LR04E_read_buffer (uint16_t address, uint8_t buffer_length, uint8_t* buffer) {
  int32_t I2C_TimeOut = M24LR04E_I2C_TIMEOUT;
	
	if(HAL_I2C_Mem_Read(&I2CHandle, M24LR04E_I2C_ADDRESS, address, I2C_MEMADD_SIZE_16BIT, buffer, buffer_length, I2C_TimeOut) != HAL_OK)
  {
		get_app_config()->error_code = I2C_ERROR;
    Error_Handler();
  }
}
/*******************************************************************************
 * @brief  : Read-Modify-Write (not atomic) of one periph register using I2C2
 *           Read the value, modify relevant bits using write mask, write back.
 * @param  : RegAdd = Register Address (or name alias)
 *           RegVal: value to write.
 * @return : none.
 ******************************************************************************/
void LBF_I2CSensors_RmodWSingleReg (uint8_t ChipID, uint16_t RegAdd, uint8_t RegMask, uint8_t RegUpdateVal)
{

uint8_t RegVal = 0;
uint16_t MemAddSize = I2C_MEMADD_SIZE_8BIT;

    //VL6180X uses 16-bit register addressing, other sensors on board use 8 bit
    if (ChipID==VL6180X_CHIPID)
    {
        MemAddSize = I2C_MEMADD_SIZE_16BIT;
    }


    // Read the register
    HAL_I2C_Mem_Read( &hi2c2, (uint16_t)(ChipID<<1), RegAdd,
                               MemAddSize, &RegVal, 0x1, 1000);

    // Update read value according to mask and update bits
    RegVal =  (RegVal & ~(RegMask)) | (RegUpdateVal & RegMask) ;

    // Write back result
    HAL_I2C_Mem_Write( &hi2c2, (uint16_t) (ChipID<<1), (uint16_t) RegAdd,  
                               MemAddSize, &RegVal, 0x1, 1000) ;
  
}
/**
  * @brief  Reads a register of the device through BUS.
  * @param  Addr: Device address on BUS Bus.  
  * @param  Reg: The target register address to write
  * @retval Data read at register address
  */
static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t value = 0;
  
  status = HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, I2cxTimeout);
 
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Re-Initialize the BUS */
    I2Cx_Error();
		HAL_I2C_Mem_Read(&I2cHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &value, 1, I2cxTimeout);
  }
  return value;
}
Exemplo n.º 4
0
_EXTERN_ATTRIB int stm32_i2c_read(unsigned char slave_addr, unsigned char reg_addr,unsigned char length, unsigned char *data)
{
	HAL_StatusTypeDef status;
	status = HAL_I2C_Mem_Read(&hi2c2,slave_addr<<1,reg_addr,I2C_MEMADD_SIZE_8BIT,(uint8_t *)data,length,I2C_SHIM_TIMEOUT_MS);
	if ( status != HAL_OK ) {
		status = HAL_TIMEOUT;
	}
	return ((status == HAL_OK ) ? 0 : -1);
}
Exemplo n.º 5
0
bool AT24C_Read(I2C_HandleTypeDef * i2c, uint16_t adress, uint8_t * buff, uint16_t len)
{
	if(HAL_I2C_IsDeviceReady(i2c, 174, 1, 100)!=HAL_OK){osDelay(10);}
	
	if(HAL_I2C_Mem_Read(i2c, 174, adress, I2C_MEMADD_SIZE_16BIT, buff, len, 100)!= HAL_OK)
	{
		return true;
	}
	return false;
}
Exemplo n.º 6
0
void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count)
{
	HAL_I2C_Mem_Read(&imu_i2c_handler,
					address,				//address of g in the i2c bus
					subAddress|0x80,				//the start address of the first data register
					I2C_MEMADD_SIZE_8BIT,		//the size of the memory address of imu
					dest,					//the address of the vector
					count,							//the size of the vector containing all the control registers
					10);						//timeout
}
Exemplo n.º 7
0
int OV7670_ReadReg(OV7670_HandleTypeDef *hov, uint8_t regAddr, uint8_t *pData)
{
	if (HAL_I2C_Mem_Read(hov->hi2c, hov->addr, regAddr, I2C_MEMADD_SIZE_8BIT, pData, 1, hov->timeout) == HAL_OK)
	{
		return OV7670_OK;
	}
	else
	{
		return OV7670_ERROR;
	}
}
Exemplo n.º 8
0
uint8_t I2CreadByte(uint8_t address, uint8_t subAddress)
{
	uint8_t  data;
	HAL_I2C_Mem_Read(&imu_i2c_handler,
					address,				//address of g in the i2c bus
					subAddress,				//the start address of the first data register
					I2C_MEMADD_SIZE_8BIT,		//the size of the memory address of imu
					&data,					//the address of the vector
					1,							//the size of the vector containing all the control registers
					10);						//timeout
	return data;
}
Exemplo n.º 9
0
void MPU6050_Get6AxisRawData(int16_t *accel,int16_t* gyro)
{
  uint8_t data[14];
  HAL_I2C_Mem_Read(&DEV,MPU_ADDR,0x3b,I2C_MEMADD_SIZE_8BIT,data,14,100);
  accel[0] = (data[0] << 8) | data[1];
  accel[1] = (data[2] << 8) | data[3];
  accel[2] = (data[4] << 8) | data[5];

  gyro[0] = (data[8] << 8) | data[9];
  gyro[1] = (data[10] << 8) | data[11];
  gyro[2] = (data[12] << 8) | data[13];
  
}
Exemplo n.º 10
0
float GetTempLM75(void){
 _TwoBytesS Result; 
 float RetRes;
 uint8_t LM75Data[2]; 
 HAL_I2C_Mem_Read(&hi2c2,(uint16_t)LM75_ADDR, (uint16_t)LM75_TEMP, I2C_MEMADD_SIZE_8BIT, LM75Data, 2, 10);
 Result.Bytes[1] = LM75Data[0];
 Result.Bytes[0] = LM75Data[1];
 Result.Word /= 128;
 
 RetRes = (float)(Result.Word)* LM75_TEMP_CORR;

return RetRes; 
}
Exemplo n.º 11
0
/* Read temperature value */
uint16_t read_TMP006(I2C_HandleTypeDef *hi2c)
{
    uint8_t data[2] = {0};
    uint16_t raw_temp = 0;
    double temp = 0;

    /* read from 0x02, memory address in TMP006 */
    if(HAL_I2C_Mem_Read(hi2c, (uint16_t)(TMP006_ADDR_NORMAL << 1), (uint16_t)TMP006_TAMB, 1, data, 2, (uint32_t)0xFFFF) == HAL_OK)
    {
        raw_temp = (data[0] << 8) | data[1];
        raw_temp = raw_temp >> 2;
        temp = raw_temp * 0.03125; // convert to celsius
    }
Exemplo n.º 12
0
/**
  * @brief  Reads buffer through I2C.
  * @param  Addr: Device address on I2C
  * @param  Reg: The target address to read
  * @param  RegSize : The target register size (can be 8BIT or 16BIT)
  * @param  pBuffer: The address to store the read data
  * @param  Lenght: buffer size to be read
  * @retval None   
  */
static HAL_StatusTypeDef I2C2_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
{
  HAL_StatusTypeDef status = HAL_OK;

  status = HAL_I2C_Mem_Read(&heval_I2c2, Addr, (uint16_t)Reg, RegSize, pBuffer, Length, I2c2Timeout);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Execute user timeout callback */
    I2C2_Error();
  }
  return status;
}
Exemplo n.º 13
0
/**
  * @brief  Reads multiple data.
  * @param  Addr: I2C address
  * @param  Reg: Reg address 
  * @param  Buffer: Pointer to data buffer
  * @param  Length: Length of the data
  * @retval Number of read data
  */
static HAL_StatusTypeDef I2Cx_ReadMultiple(uint8_t Addr, uint16_t Reg, uint16_t MemAddress, uint8_t *Buffer, uint16_t Length)
{
  HAL_StatusTypeDef status = HAL_OK;
  
  status = HAL_I2C_Mem_Read(&heval_I2c, Addr, (uint16_t)Reg, MemAddress, Buffer, Length, I2C_TIMEOUT);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* I2C error occured */
    I2Cx_Error(Addr);
  }
  return status;    
}
Exemplo n.º 14
0
/**
  * @brief  Reads multiple data on the BUS.
  * @param  Addr  : I2C Address
  * @param  Reg   : Reg Address 
  * @param  RegSize : The target register size (can be 8BIT or 16BIT)
  * @param  pBuffer : pointer to read data buffer
  * @param  Length : length of the data
  * @retval 0 if no problems to read multiple data
  */
static HAL_StatusTypeDef I2C1_ReadBuffer(uint16_t Addr, uint8_t Reg, uint16_t RegSize, uint8_t *pBuffer, uint16_t Length)
{
  HAL_StatusTypeDef status = HAL_OK;
  
  status = HAL_I2C_Mem_Read(&heval_I2c1, Addr, Reg, RegSize, pBuffer, Length, I2c1Timeout);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Re-Initiaize the BUS */
    I2C1_Error();
  }
  return status;
}
Exemplo n.º 15
0
// Read from MemAddress in device
int I2C_mem_read(I2C_Device_t *device, uint16_t MemAddress, uint8_t *p_data, uint16_t n_bytes) {
    int ret;
    I2C_HandleTypeDef *hi2c;
    if (device->I2Cx == I2C1) {
        hi2c = &hi2c1;
    } else {
        return -1; // Not implemented
    }

    ret = HAL_I2C_Mem_Read(hi2c,device->address, MemAddress, device->mem_add_size, p_data, n_bytes, device->timeout);


    return ret;
}
Exemplo n.º 16
0
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf)
{
    HAL_StatusTypeDef status;

    if(reg_ == 0xFF)
        status = HAL_I2C_Master_Receive(&i2cHandle[device].Handle,addr_ << 1,buf, len, I2C_DEFAULT_TIMEOUT);
    else
        status = HAL_I2C_Mem_Read(&i2cHandle[device].Handle,addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,buf, len, I2C_DEFAULT_TIMEOUT);

    if(status != HAL_OK)
        return i2cHandleHardwareFailure(device);

    return true;
}
Exemplo n.º 17
0
/**
  * @brief  Reads a single data.
  * @param  Addr: I2C address
  * @param  Reg: Register address 
  * @retval Read data
  */
static uint8_t I2C1_Read(uint8_t Addr, uint8_t Reg)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t Value = 0;
  
  status = HAL_I2C_Mem_Read(&heval_I2c1, Addr, Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, 1000);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Execute user timeout callback */
    I2C1_Error();
  }
  return Value;   
}
Exemplo n.º 18
0
/**
  * @brief  Read a register of the device through BUS
  * @param  Addr: Device address on BUS  
  * @param  Reg: The target register address to read
  * @retval HAL status
  */
static uint8_t  I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t value = 0;
  
  status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &value, 1,I2cxTimeout);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Execute user timeout callback */
    I2Cx_Error(Addr);
  }
  return value;
}
/**
  * @brief  Read a value in a register of the device through BUS.
  * @param  Addr: Device address on BUS Bus.
  * @param  Reg: The target register address to write
  * @retval HAL_StatusTypeDef status
  *
  * @see HAL_StatusTypeDef
  */
HAL_StatusTypeDef I2Cbar_ReadDataLen(uint16_t Addr, uint8_t Reg, uint8_t * pData, uint16_t Len)
{
  HAL_StatusTypeDef status = HAL_OK;

  status = HAL_I2C_Mem_Read(&I2CbarHandle, Addr, Reg, I2C_MEMADD_SIZE_8BIT, pData, Len, I2CbarTimeout);

  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Execute user timeout callback */
    I2Cbar_Error();
  }

  return status;
}
Exemplo n.º 20
0
/**
 * @brief  Read the value of a register of the device through the bus
 * @param  pBuffer the pointer to data to be read
 * @param  Addr the device address on bus
 * @param  Reg the target register address to be read
 * @param  Size the size in bytes of the value to be read
 * @retval HAL status.
 */
static HAL_StatusTypeDef I2C_SHIELDS_ReadData(uint8_t* pBuffer, uint8_t Addr, uint8_t Reg, uint16_t Size)
{
    HAL_StatusTypeDef status = HAL_OK;

    status = HAL_I2C_Mem_Read(&I2C_SHIELDS_Handle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Size, I2C_SHIELDS_Timeout);

    /* Check the communication status */
    if(status != HAL_OK)
    {
        /* Execute user timeout callback */
        I2C_SHIELDS_Error(Addr);
    }

    return status;
}
Exemplo n.º 21
0
/**
  * @brief  Read a register of the device through I2C
  * @param  Addr: Device address on I2C.  
  * @param  Reg: The target register address to read
  * @param  RegSize: The target register size (can be 8BIT or 16BIT)
  * @retval Read register value
  */
static uint8_t I2C2_ReadData(uint16_t Addr, uint8_t Reg, uint16_t RegSize)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t value = 0;
  
  status = HAL_I2C_Mem_Read(&heval_I2c2, Addr, Reg, RegSize, &value, 1, I2c2Timeout);
  
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Execute user timeout callback */
    I2C2_Error();
  }
  return value;
}
Exemplo n.º 22
0
/**
  * @brief  Read a register of the device through BUS
  * @param  Addr: Device address on BUS
  * @param  Reg: The target register address to read
  * @param  RegSize: The target register size (can be 8BIT or 16BIT)
  * @retval read register value
  */
static uint8_t I2Cx_ReadData(uint16_t Addr, uint8_t Reg, uint16_t RegSize)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint8_t value = 0;
  
  status = HAL_I2C_Mem_Read(&heval_I2c, Addr, Reg, RegSize, &value, 1, I2cxTimeout);
 
  /* Check the communication status */
  if(status != HAL_OK)
  {
    /* Re-Initiaize the BUS */
    I2Cx_Error();
  
  }
  return value;
}
/**
  * @brief  Reads multiple data on the BUS.
  * @param  Addr: I2C Address
  * @param  Reg: Reg Address 
  * @param  pBuffer: pointer to read data buffer
  * @param  Length: length of the data
  * @retval 0 if no problems to read multiple data
  */
static uint8_t I2Cx_ReadBuffer(uint8_t Addr, uint8_t Reg, uint8_t *pBuffer, uint16_t Length)
{
  HAL_StatusTypeDef status = HAL_OK;

  status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, pBuffer, Length, I2cxTimeout);
  
  /* Check the communication status */
  if(status == HAL_OK)
  {
    return 0;
  }
  else
  {
    /* Re-Initialize the BUS */
    I2Cx_Error();

    return 1;
  }
}
Exemplo n.º 24
0
uint8_t MPU_Init()
{
  uint8_t data;
  
  HAL_I2C_Mem_Read(&DEV,MPU_ADDR,0x75,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  if(data!=0x68) return 1;
  data=0;//sample data rate =1K or 8k(dlfp is enabled)
  HAL_I2C_Mem_Write(&DEV,MPU_ADDR,0x19,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  data=0x03;//dlfp accel 44hz, gyro 42hz
  HAL_I2C_Mem_Write(&DEV,MPU_ADDR,0x1A,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  data=0x03<<3;//gyro +-2000
  HAL_I2C_Mem_Write(&DEV,MPU_ADDR,0x1B,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  data=0;//accel +-2g
  HAL_I2C_Mem_Write(&DEV,MPU_ADDR,0x1C,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  data=0x01;//choose the x gyo pll as the clock
  HAL_I2C_Mem_Write(&DEV,MPU_ADDR,0x6b,I2C_MEMADD_SIZE_8BIT,&data,1,100);
  
  return 0;
}
Exemplo n.º 25
0
/// \method filtered_xyz()
/// Get a 3-tuple of filtered x, y and z values.
STATIC mp_obj_t pyb_accel_filtered_xyz(mp_obj_t self_in) {
    pyb_accel_obj_t *self = self_in;

    memmove(self->buf, self->buf + NUM_AXIS, NUM_AXIS * (FILT_DEPTH - 1) * sizeof(int16_t));

    uint8_t data[NUM_AXIS];
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_X, I2C_MEMADD_SIZE_8BIT, data, NUM_AXIS, 200);

    mp_obj_t tuple[NUM_AXIS];
    for (int i = 0; i < NUM_AXIS; i++) {
        self->buf[NUM_AXIS * (FILT_DEPTH - 1) + i] = MMA_AXIS_SIGNED_VALUE(data[i]);
        int32_t val = 0;
        for (int j = 0; j < FILT_DEPTH; j++) {
            val += self->buf[i + NUM_AXIS * j];
        }
        tuple[i] = mp_obj_new_int(val);
    }

    return mp_obj_new_tuple(3, tuple);
}
Exemplo n.º 26
0
STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
    pyb_i2c_obj_t *self = args[0];

    if (!in_master_mode(self)) {
        nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "I2C must be a master"));
    }

    // parse args
    mp_arg_val_t vals[PYB_I2C_MEM_READ_NUM_ARGS];
    mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_MEM_READ_NUM_ARGS, pyb_i2c_mem_read_args, vals);

    // get the buffer to read into
    mp_buffer_info_t bufinfo;
    mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &bufinfo);

    // get the addresses
    mp_uint_t i2c_addr = vals[1].u_int << 1;
    mp_uint_t mem_addr = vals[2].u_int;
    // determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
    mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
    if (vals[4].u_int != 8) {
        mem_addr_size = I2C_MEMADD_SIZE_16BIT;
    }

    HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int);

    if (status != HAL_OK) {
        // TODO really need a HardwareError object, or something
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Mem_Read failed with code %d", status));
    }

    // return the read data
    if (o_ret == MP_OBJ_NULL) {
        return vals[0].u_obj;
    } else {
        return mp_obj_str_builder_end(o_ret);
    }
}
/*******************************************************************************
 * @brief  : Read values from successive registers of peripheral IC using I2C protocoal.
 * @param  : RegAdd = Initial Register Address (or name alias)
 *           pVal: pointer to array to store values read
 *           Nb: number of successive registers to read
 * @return : register contents.
 ******************************************************************************/
void LBF_I2CSensors_ReadMultipleReg (uint8_t ChipID, uint16_t RegAdd, uint8_t* pVal, uint16_t NumByteToRead )
{


uint16_t MemAddSize = I2C_MEMADD_SIZE_8BIT;


/*****   ATTENTION   ATTENTION    ATTENTION   ATTENTION   ATTENTION   ********/
/*****  The "7-bit device address that HAL expects is actually the    ********/
/*****  8-bit I2C write address (i.e. shift left by one bit, pad a 0) !!! ****/

    //VL6180X uses 16-bit register addressing, other sensors on board use 8 bit
    if (ChipID==VL6180X_CHIPID)
    {
        MemAddSize = I2C_MEMADD_SIZE_16BIT;
    }

    // I2C2, read 1 register in blocking mode with 1s timeout
    // (hi2c2 is global)
    HAL_I2C_Mem_Read( &hi2c2, (uint16_t)(ChipID<<1), RegAdd,
                               MemAddSize, pVal, NumByteToRead, 1000);

}
Exemplo n.º 28
0
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf)
{
    if (device == I2CINVALID || device > I2CDEV_COUNT) {
        return false;
    }

    I2C_HandleTypeDef *pHandle = &i2cDevice[device].handle;

    if (!pHandle->Instance) {
        return false;
    }

    HAL_StatusTypeDef status;

    if (reg_ == 0xFF)
        status = HAL_I2C_Master_Receive(pHandle ,addr_ << 1, buf, len, I2C_DEFAULT_TIMEOUT);
    else
        status = HAL_I2C_Mem_Read(pHandle, addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,buf, len, I2C_DEFAULT_TIMEOUT);

    if (status != HAL_OK)
        return i2cHandleHardwareFailure(device);

    return true;
}
Exemplo n.º 29
0
STATIC mp_obj_t pyb_accel_read(mp_obj_t self_in, mp_obj_t reg) {
    uint8_t data[1];
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, mp_obj_get_int(reg), I2C_MEMADD_SIZE_8BIT, data, 1, 200);
    return mp_obj_new_int(data[0]);
}
Exemplo n.º 30
0
/// \method tilt()
/// Get the tilt register.
STATIC mp_obj_t pyb_accel_tilt(mp_obj_t self_in) {
    uint8_t data[1];
    HAL_I2C_Mem_Read(&I2CHandle1, MMA_ADDR, MMA_REG_TILT, I2C_MEMADD_SIZE_8BIT, data, 1, 200);
    return mp_obj_new_int(data[0]);
}