Example #1
1
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
    if (length == 0) return 0;
  
    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);

    // Transmission process with 5 seconds timeout
    if (HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)address, (uint8_t *)data, length, 5000) != HAL_OK) {
        return 0; // Error
    }
    
    return length;
}
Example #2
1
uint8_t SCCB_Write(uint8_t addr, uint8_t data)
{
    uint8_t ret=0;
    uint8_t buf[2] = {0, 0};
		buf[0] = addr;
		buf[1] = data;
    //__disable_irq();
    if (HAL_I2C_Master_Transmit(&hnd, SLAVE_ADDR, buf, 2, TIMEOUT) != HAL_OK) {
        ret=0xFF;
    }
    //__enable_irq();
    return ret;
}
Example #3
1
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data)
{
    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_Transmit(pHandle ,addr_ << 1, data, len_, I2C_DEFAULT_TIMEOUT);
    else
        status = HAL_I2C_Mem_Write(pHandle ,addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,data, len_, I2C_DEFAULT_TIMEOUT);

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

    return true;
}
Example #4
0
File: sccb.c Project: 12019/openmv
uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
{
    uint8_t data=0;

    __disable_irq();
    if((HAL_I2C_Master_Transmit(&I2CHandle, slv_addr, &reg, 1, TIMEOUT) != HAL_OK)
    || (HAL_I2C_Master_Receive(&I2CHandle, slv_addr, &data, 1, TIMEOUT) != HAL_OK)) {
        data=0xFF;
    }
    __enable_irq();
    return data;
}
Example #5
0
uint8_t SCCB_Write(uint8_t addr, uint8_t data)
{
    uint8_t ret=0;
    uint8_t buf[] = {addr, data};

    __disable_irq();
    if (HAL_I2C_Master_Transmit(&I2CHandle, SLAVE_ADDR, buf, 2, TIMEOUT) != HAL_OK) {
        ret=0xFF;
    }
    __enable_irq();
    return ret;
}
void init_i2c_accel(I2C_HandleTypeDef *hi2c)
{
	i2c_Handle = hi2c;
	uint8_t buf[2];

	volatile HAL_StatusTypeDef error;

	buf[0] = 0x20; 						//CTRL_REG1
	buf[1] = 0x34;						//Data

	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LSM303DLHC_acel, (uint8_t*)buf,sizeof(buf),1000);
	if(error!=HAL_OK){} //TODO Error Handling
}
void BQ27542_read(unsigned char cmd, unsigned int bytes)
{
  unsigned char tx[1];

  tx[0] = cmd;
  if(HAL_I2C_Master_Transmit(&hi2c1, bq27542_ADDR, tx, 1, 0xFFFF) != HAL_OK){
	  BQ27542_error();
  }
  if(HAL_I2C_Master_Receive(&hi2c1, bq27542_ADDR, RxData, bytes, 0xFFFF) != HAL_OK){
	  BQ27542_error();
  }

}
Example #8
0
static void mma8652Reg8Writre (I2C_HandleTypeDef *i2cHandle, uint8_t reg, uint8_t value)
{
        uint8_t txBuffer[2] = {reg, value};

        while (HAL_I2C_Master_Transmit (i2cHandle, (uint8_t) MMA8652_I2C_ADDR, (uint8_t*) txBuffer, sizeof(txBuffer), 10000) != HAL_OK) {
                /* Error_Handler() function is called when Timeout error occurs.
                 When Acknowledge failure occurs (Slave don't acknowledge it's address)
                 Master restarts communication */
                if (HAL_I2C_GetError (i2cHandle) != HAL_I2C_ERROR_AF) {
                        Error_Handler ();
                }
        }
}
Example #9
0
void Baro::init(I2C_HandleTypeDef* hi2c, ros::NodeHandle* nh)
{
  i2c_handle_ = hi2c;
  nh_ = nh;
  nh_->subscribe<std_msgs::UInt8, Baro>(baro_config_sub_);

  BARO_H;
  //reset
  uint8_t reg[1];
  reg[0] = CMD_MS56XX_RESET;
  HAL_I2C_Master_Transmit(i2c_handle_, MS561101BA_ADDRESS, reg, 1, 100);

  HAL_Delay(10);
  uint16_t prom[8];
  if (!readCalib(prom)) return;
  calibrated_ = true;

  // Save factory calibration coefficients
  c1_ = prom[1];
  c2_ = prom[2];
  c3_ = prom[3];
  c4_ = prom[4];
  c5_ = prom[5];
  c6_ = prom[6];

  // Send a command to read temperature first
  reg[0] = ADDR_CMD_CONVERT_TEMPERATURE;
  //reg[0] = ADDR_CMD_CONVERT_PRESSURE;
  HAL_I2C_Master_Transmit(i2c_handle_, MS561101BA_ADDRESS, reg, 1, 100);
  last_timer_ = HAL_GetTick();
  state_ = 0;

  s_d1_ = 0;
  s_d2_ = 0;
  d1_count_ = 0;
  d2_count_ = 0;

}
Example #10
0
void test_EasterEgg(void)
{
	ExpanderSetbit(7,0);
	HAL_Delay(100);
	ExpanderSetbit(7,1);
	HAL_Delay(100);

	ssd1306Init(0);
	ssd1306ClearScreen();
	ssd1306Refresh();

	// I2C
	uint8_t aTxBuffer[3]; // = {control, c};
	aTxBuffer[0] = 0x0;
	aTxBuffer[1] = 0x1;
	aTxBuffer[2] = 0x5;

	uint8_t aRxBuffer[1]; // = {control, c};
	aRxBuffer[0] = 0x0;

	ssd1306ClearScreen();

	while(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)0x50<<1, (uint8_t*)aTxBuffer, 3, 10000)!= HAL_OK)
	{
	  /* Error_Handler() function is called when Timout error occurs.
		 When Acknowledge failure ocucurs (Slave don't acknowledge it's address)
		 Master restarts communication */
	  if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
	  {
		  ssd1306PrintInt(10,  15, "Bad Send", 0, &Font_5x8);
	  }

	}

	while(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)0x50<<1, (uint8_t*)aRxBuffer, 1, 10000)!= HAL_OK)
	{
	  /* Error_Handler() function is called when Timout error occurs.
		 When Acknowledge failure ocucurs (Slave don't acknowledge it's address)
		 Master restarts communication */
	  if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
	  {
		  ssd1306PrintInt(10,  25, "Bad recv", 0, &Font_5x8);
	  }

	}

	ssd1306PrintInt(10,  35, "Fini : ", aRxBuffer[0], &Font_5x8);
	ssd1306Refresh();

}
Example #11
0
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data)
{
    HAL_StatusTypeDef status;

    if(reg_ == 0xFF)
        status = HAL_I2C_Master_Transmit(&i2cHandle[device].Handle,addr_ << 1,data, len_, I2C_DEFAULT_TIMEOUT);
    else
        status = HAL_I2C_Mem_Write(&i2cHandle[device].Handle,addr_ << 1, reg_, I2C_MEMADD_SIZE_8BIT,data, len_, I2C_DEFAULT_TIMEOUT);

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

    return true;
}
Example #12
0
// Write n_bytes of data to device
int I2C_send_data(I2C_Device_t *device, 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_Master_Transmit(hi2c,device->address,p_data,n_bytes,device->timeout);

    return ret;
}
Example #13
0
/* Return 1 if TMP006 is successfully configured
 * Otherwise return 0
 */
uint8_t config_TMP006(I2C_HandleTypeDef *hi2c) {

    uint8_t data[3] = {0};
    data[0] = TMP006_CONFIG;
    data[1] = (TMP006_CFG_8SAMPLE | TMP006_CFG_MODEON | TMP006_CFG_DRDYEN) >> 8;
    data[2] = TMP006_CFG_8SAMPLE | TMP006_CFG_MODEON | TMP006_CFG_DRDYEN;

    /* configure TMP006 in accordance to user guide */
    if(HAL_I2C_Master_Transmit(hi2c, (uint16_t)(TMP006_ADDR_NORMAL << 1), data, 3, (uint32_t)0xFFFF)!= HAL_OK)
    {
        return 0;
    }

    return 1;
}
Example #14
0
/* Configs the IMU with various settings for sensitivity */
void IMUConfig(int accelrange, int gyrorange)
{
	/* Figures out what settings to apply */
	     if (accelrange == 4)  {accelRegister = 0x08; accelConvFactor = 8192; }
	else if (accelrange == 8)  {accelRegister = 0x10; accelConvFactor = 4096; }
	else if (accelrange == 16) {accelRegister = 0x18; accelConvFactor = 2048; }
	   else  /*Defults to 2*/  {accelRegister = 0x00; accelConvFactor = 16384;}

	     if (gyrorange == 500) {gyroRegister = 0x08; gyroConvFactor = 65.5; }
	else if (gyrorange == 1000){gyroRegister = 0x10; gyroConvFactor = 32.8; }
	else if (gyrorange == 2000){gyroRegister = 0x18; gyroConvFactor = 16.4; }
	   else /*Defults to 250*/ {gyroRegister = 0x00; gyroConvFactor = 131;  }
	
	/* Does the configuration */
	uint8_t addressAndData[2] = {IMU_WHOAMI, 0x00};
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)addressAndData, 1, 100);	/* This should return the WHOAMI register */
	HAL_I2C_Master_Receive(&hi2c1, IMU_ADDRESS<<1, (uint8_t *)rawIMU, 1, 100);
	if (rawIMU[0] == 0x68){print("\r\n[OK] IMU Found");}
	else{print("\r\n[ERROR] IMU not found");}
 
	addressAndData[0] = IMU_CFG;
	addressAndData[1] = 0x00;
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)addressAndData, 2, 100); /* Reset LPF */

	addressAndData[0] = IMU_ACCEL_CFG;
	addressAndData[1] = accelRegister;
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)addressAndData, 2, 100);

	addressAndData[0] = IMU_ACCEL_CFG;
	addressAndData[1] = accelRegister;
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)addressAndData, 2, 100);

	addressAndData[0] = IMU_PWR_MGMT1;
	addressAndData[1] = 0x00;
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)addressAndData, 2, 100); /* Bring IMU out of reset */
}
 /**
   * @brief Sleep device request to the sensor on the specified address.
   * @param  h_i2c: pointer to the i2c bus peripheral handle that the temperature sensor ics are connected.
   * @param  device_i2c_address: i2c address of the sensor to request measurement.
   * @retval TC_74_STATUS.
   */
 TC_74_STATUS TC74_device_sleep(I2C_HandleTypeDef *h_i2c, uint8_t device_i2c_address) {

	 TC_74_STATUS device_status;
	 /*put device to sleep*/
	 uint8_t transmit_packet[2] = { TC74_CONFIGURATION_REGISTER, TC74_STANDBY_COMMAND };
	 if (HAL_I2C_Master_Transmit(h_i2c, device_i2c_address, transmit_packet, 2, 100) != HAL_OK) {
		 //soft error handle
		 device_status = DEVICE_ERROR;
	 }

 	 if(device_status!=DEVICE_ERROR){
 		 device_status = TC74_read_device_status(h_i2c, device_i2c_address);
 	 }

	 return device_status;
 }
Example #16
0
/* Gets register values for accel, gyro and temp data and puts them into correct format */
void IMUGetMotion()
{
	uint8_t address[] = {0x3B}; /* Start Address */
	HAL_I2C_Master_Transmit(&hi2c1, IMU_ADDRESS<<1, (uint8_t*)address, 1, 100);
	HAL_I2C_Master_Receive(&hi2c1, IMU_ADDRESS<<1, (uint8_t *)rawIMU, 14, 200);
    
    Motion.x = (float) ( (int16_t)((rawIMU[0] << 8) | rawIMU[1]) ) / accelConvFactor;
    Motion.y = (float) ( (int16_t)((rawIMU[2] << 8) | rawIMU[3]) ) / accelConvFactor;
    Motion.z = (float) ( (int16_t)((rawIMU[4] << 8) | rawIMU[5]) ) / accelConvFactor;
    
    Motion.temp = ((float) ( (int16_t)((rawIMU[6] << 8) | rawIMU[7]) ) + 12412.0) / 340.0;

    Motion.roll  = (float) ( (int16_t)((rawIMU[8]  << 8) | rawIMU[9] ) ) / gyroConvFactor;
    Motion.pitch = (float) ( (int16_t)((rawIMU[10] << 8) | rawIMU[11]) ) / gyroConvFactor;
    Motion.yaw   = (float) ( (int16_t)((rawIMU[12] << 8) | rawIMU[13]) ) / gyroConvFactor;
}
Example #17
0
File: sccb.c Project: 12019/openmv
uint8_t SCCB_Probe()
{
    uint8_t reg = 0x00;
    uint8_t slv_addr = 0x00;

    for (int i=0; i<127; i++) {
        if (HAL_I2C_Master_Transmit(&I2CHandle, i, &reg, 1, TIMEOUT) == HAL_OK) {
            slv_addr = i;
            break;
        }
        if (i!=126) {
            systick_sleep(1); // Necessary for OV7725 camera (not for OV2640).
        }
    }
    return slv_addr;
}
Example #18
0
uint8_t SCCB_Read(uint8_t addr)
{
    uint8_t data=0;

    __disable_irq();
    if (HAL_I2C_Master_Transmit(&I2CHandle, SLAVE_ADDR, &addr, 1, TIMEOUT) != HAL_OK) {
        data = 0xFF;
        goto error_w;
    }
    if (HAL_I2C_Master_Receive(&I2CHandle, SLAVE_ADDR, &data, 1, TIMEOUT) != HAL_OK) {
        data = 0xFF;
    }
error_w:
    __enable_irq();
    return data;
}
Example #19
0
uint8_t SCCB_Read(uint8_t addr)
{
    uint8_t data=0;
		uint8_t res =  HAL_I2C_Master_Transmit(&hnd, SLAVE_ADDR, &addr, 1, TIMEOUT);
	printf("res: %u",res);
    //__disable_irq();
    if (res != HAL_OK) {
        data = 0xFF;
        goto error_w;
    }
    if (HAL_I2C_Master_Receive(&hnd, SLAVE_ADDR, &data, 1, TIMEOUT) != HAL_OK) {
        data = 0xFF;
    }
error_w:
    //__enable_irq();
    return data;
}
 /**
   * @brief Wake up request to the sensor on the specified address.
   * @param  h_i2c: pointer to the i2c bus peripheral handle that the temperature sensor ics are connected.
   * @param  device_i2c_address: i2c address of the sensor to request measurement.
   * @retval TC_74_STATUS.
   */
 TC_74_STATUS TC74_device_wake_up(I2C_HandleTypeDef *h_i2c, uint8_t device_i2c_address) {

	 TC_74_STATUS device_status = DEVICE_STATUS_LAST_VALUE;
	 HAL_StatusTypeDef res;
	 /*wake up */
	 uint8_t transmit_packet[2] = { TC74_CONFIGURATION_REGISTER, TC74_AWAKE_COMMAND };
	 if ((res = HAL_I2C_Master_Transmit(h_i2c, device_i2c_address, transmit_packet, 2,	100)) != HAL_OK) {
		 //soft error handle
		 device_status = DEVICE_ERROR;
	 }

 	 if(device_status!=DEVICE_ERROR){
 		 device_status = TC74_read_device_status(h_i2c, device_i2c_address);
 	 }

	 return device_status;
 }
void init_i2c_magn(I2C_HandleTypeDef *hi2c)
{
	i2c_Handle = hi2c;
	uint8_t buf[2];
	volatile HAL_StatusTypeDef error;

#if Enable_LIS3MDL
	buf[0] = 0x20; 						//CTRL_REG1
	buf[1] = 0x74;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LIS3MDL, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling

	buf[0] = 0x21; 						//CTRL_REG2
	buf[1] = 0x00;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LIS3MDL, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling

	buf[0] = 0x22; 						//CTRL_REG3
	buf[1] = 0x00;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LIS3MDL, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling

	buf[0] = 0x23; 						//CTRL_REG4
	buf[1] = 0x0C;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LIS3MDL, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling
#endif

#if Enable_LSM303DLHC
	buf[0] = 0x00; 						//CRA_REG_M
	buf[1] = 0x14;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LSM303DLHC, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling

	buf[0] = 0x01; 						//CRB_REG_M
	buf[1] = 0x20;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LSM303DLHC, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling

	buf[0] = 0x02; 						//MR_REG_M
	buf[1] = 0x00;						//Data
	error = HAL_I2C_Master_Transmit(i2c_Handle, (uint16_t)adress_LSM303DLHC, (uint8_t*)buf,2,1000);
	if(error!=HAL_OK){} //TODO Error Handling
#endif

}
Example #22
0
void PCF8563_read_datetime(date_time_t volatile *dt)
{
union{  
  struct{
    uint8_t bcd_sec;
    uint8_t bcd_min;
    uint8_t bcd_hrs;
    uint8_t bcd_day;
    uint8_t wek;   
    uint8_t bcd_mon;
    uint8_t bcd_yer;
  }Fields;
  uint8_t arrWeekDays[7];
}Time;
uint8_t data = 0;

//  LowVoltage = VL_sec.7
//  Century    = C_Mon.7

// Disable interrupts so the i2c process is not disrupted.
data = PCF8563_SECONDS_REG;

 HAL_I2C_Master_Transmit(&hi2c2, PCF8563_WRITE_ADDRESS, &data, 1, 200);
// Read the date/time registers inside the PCF8563.

HAL_I2C_Master_Receive(&hi2c2, PCF8563_READ_ADDRESS, Time.arrWeekDays, 7, 100); 
 
// Convert the date/time values from BCD to
// unsigned 8-bit integers.  Unpack the bits
// in the PCF8563 registers where required.
 Time.Fields.bcd_sec &= 0x7F;
 Time.Fields.bcd_mon &= 0x7F;
 
dt->seconds = bcd2bin(Time.Fields.bcd_sec);   
dt->minutes = bcd2bin(Time.Fields.bcd_min);     
dt->hours   = bcd2bin(Time.Fields.bcd_hrs & 0x3F);
dt->day     = bcd2bin(Time.Fields.bcd_day & 0x3F);
dt->month   = bcd2bin(Time.Fields.bcd_mon & 0x1F);
dt->weekday =    Time.Fields.wek & 0x07;
dt->year    = bcd2bin(Time.Fields.bcd_yer);   

}
Example #23
0
void i2c_send1(void)
{
    char buf[] = {0x00, 0x00};
    if(HAL_I2C_Master_Transmit(&hi2c, (uint16_t)0xAE, (uint8_t*)buf, 2, 10000) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c) != HAL_I2C_ERROR_AF)
        {
            led_on();
            return;
        }
    }
    while(HAL_I2C_Master_Receive(&hi2c, (uint16_t)0xAF, (uint8_t *)buf, 2, 10000) != HAL_OK)
    {
        if (HAL_I2C_GetError(&hi2c) != HAL_I2C_ERROR_AF)
        {
            led_on();
            return;
        }
    }
}
Example #24
0
void i2c (){

		
	I2C_tx_buffer[0]=(uint8_t)MODE;
	
	HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_tx_buffer[0], 1,100);
	//HAL_Delay(10);
	HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin);
		//HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_tx_buffer[0], 1);
		while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY); //waiting for the end of current transfer before starting a new one
	

		HAL_I2C_Master_Receive(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_rx_buffer[0], 1,100);
		while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY); //waiting for the end of current transfer

	for(int i=0;i<10;i++){
		HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin);
		HAL_Delay (500);
	}
	
}
/**
  * @brief Reads the word in temperature measurement register of the device and returns the current status of the specified address.
  * @param  h_i2c: pointer to the i2c bus peripheral handle that the temperature sensor ics are connected.
  * @param  device_i2c_address: i2c adress of the sensor to request measurement.
  * @param  receive_word: pointer to the word where the measurement register will be returned.
  * @retval TC_74_STATUS.
  */
 TC_74_STATUS TC74_read_device_temperature(I2C_HandleTypeDef *h_i2c, uint8_t device_i2c_address, int8_t *receive_word ) {

	 TC_74_STATUS device_status;
	 /*Read temperature*/
	 /*master sends the slave's temperature register adrress to read back*/
	 uint8_t transmit_packet = TC74_TEMPERATURE_REGISTER;
	 if (HAL_I2C_Master_Transmit(h_i2c, device_i2c_address, (uint8_t*)&transmit_packet, 1, 100) != HAL_OK) {
		 //soft error handle
		 device_status = DEVICE_ERROR;
	 }

 	 if (HAL_I2C_Master_Receive(h_i2c, device_i2c_address, receive_word, 1, 100) != HAL_OK) {
		 //soft error handle
 		device_status = DEVICE_ERROR;
	 }

 	 if(device_status!=DEVICE_ERROR){
 		 device_status = TC74_read_device_status(h_i2c, device_i2c_address);
 	 }

	 return device_status;
 }
 /**
   * @brief Reads the word in device status register of the device and returns the current status of the specified address.
   * @param  h_i2c: pointer to the i2c bus peripheral handle that the temperature sensor ics are connected.
   * @param  device_i2c_address: i2c adress of the sensor to request measurement.
   * @param  receive_word: pointer to the word where the measurement register will be returned.
   * @retval TC_74_STATUS.
   */
 TC_74_STATUS TC74_read_device_status(I2C_HandleTypeDef *h_i2c, uint8_t device_i2c_address ) {

	 TC_74_STATUS device_status = DEVICE_STATUS_LAST_VALUE;
	 HAL_StatusTypeDef res;
	/*Read control regiter*/
	/*master sends the slave's temperature register adrress to read back*/
	uint8_t transmit_packet[2] = { TC74_CONFIGURATION_REGISTER, TC74_AWAKE_COMMAND };
	if ((res=HAL_I2C_Master_Transmit(h_i2c, device_i2c_address, transmit_packet, 1, 100) )!= HAL_OK) {
		//soft error handle
		device_status = DEVICE_ERROR;
	}

	uint8_t receive_word;
 	if ((res=HAL_I2C_Master_Receive(h_i2c, device_i2c_address, &receive_word, 1, 100)) != HAL_OK) {
		//soft error handle
 		device_status = DEVICE_ERROR;
	}

 	if(device_status!=DEVICE_ERROR){
 		if ((receive_word&0b10111111) == TC74_STANDBY_COMMAND){
 			device_status = DEVICE_STANDBY;
 		}
 		else if((receive_word&0b10111111) == TC74_AWAKE_COMMAND){
 			device_status = DEVICE_NORMAL_DATA_NOT_READY;
 			/*check data ready bit*/
 			if ((receive_word&0b11011111) == TC74_DATA_READY_FLAG){
 				device_status = DEVICE_NORMAL_DATA_READY;
 			}

 		}
 		else{
 			device_status = DEVICE_STATUS_LAST_VALUE;
 		}
 	}


	return device_status;
}
Example #27
0
uint8_t SSD1306_WriteData(int numofbytes, uint8_t *data) {

    /*
     uint8_t a = 0;
     a |= 1 << 6; /// D/C = 1 data

     if (SSD1306_start() != 0) {
     return 0xff;
     }
     if (SSD1306_write(a) != 0) {
     return 0xff;
     }

     for (int i = 0; i < numofbytes; i++) {
     if (SSD1306_write(data[i]) != 0) {
     return 0xff;
     }
     }
     SSD1306_stop();
     */

    uint8_t data2[1 + numofbytes];

    data2[0] |= 1 << 6;

    for (int i = 0; i < numofbytes; i++) {
        data2[1 + i] = data[i];
    }

    if (HAL_I2C_Master_Transmit(&hi2c, SSD1306_ADDRESS, data2, 1 + numofbytes,
                                I2C_TIMEOUT_MAX)) {
        return 0xff;
    }

    return 0;
}
Example #28
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();

  /* Configure LED3 and LED4 */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED4);

  /* Configure the system clock to 168 MHz */
  SystemClock_Config();

  /*##-1- Configure the I2C peripheral #######################################*/
  I2cHandle.Instance             = I2Cx;

  I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_10BIT;
  I2cHandle.Init.ClockSpeed      = 400000;
  I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE_16_9;
  I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
  I2cHandle.Init.OwnAddress1     = I2C_ADDRESS;
  I2cHandle.Init.OwnAddress2     = 0xFE;

  if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

#ifdef MASTER_BOARD

  /* Configure USER Button */
  BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);

  /* Wait for USER Button press before starting the Communication */
  while (BSP_PB_GetState(BUTTON_KEY) != 1)
  {
  }

  /* Wait for USER Button release before starting the Communication */
  while (BSP_PB_GetState(BUTTON_KEY) != 0)
  {
  }

  /* The board sends the message and expects to receive it back */

  /*##-2- Start the transmission process #####################################*/
  /* While the I2C in reception process, user can transmit data through
     "aTxBuffer" buffer */
  /* Timeout is set to 10S */
  while(HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK)
  {
    /* Error_Handler() function is called when Timeout error occurs.
       When Acknowledge failure occurs (Slave don't acknowledge it's address)
       Master restarts communication */
    if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
    {
      Error_Handler();
    }
  }

  /* Turn LED3 on: Transfer in Transmission process is correct */
  BSP_LED_On(LED3);

  /* Wait for USER Button press before starting the Communication */
  while (BSP_PB_GetState(BUTTON_KEY) != 1)
  {
  }

  /* Wait for USER Button release before starting the Communication */
  while (BSP_PB_GetState(BUTTON_KEY) != 0)
  {
  }

  /*##-3- Put I2C peripheral in reception process ############################*/
  /* Timeout is set to 10S */
  while(HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 10000) != HAL_OK)
  {
    /* Error_Handler() function is called when Timeout error occurs.
       When Acknowledge failure occurs (Slave don't acknowledge it's address)
       Master restarts communication */
    if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
    {
      Error_Handler();
    }
  }

  /* Turn LED3 off: Transfer in reception process is correct */
  BSP_LED_Off(LED3);

#else

  /* The board receives the message and sends it back */

  /*##-2- Put I2C peripheral in reception process ############################*/
  /* Timeout is set to 10S  */
  if(HAL_I2C_Slave_Receive(&I2cHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 10000) != HAL_OK)
  {
    /* Transfer error in reception process */
    Error_Handler();
  }

  /* Turn LED3 on: Transfer in reception process is correct */
  BSP_LED_On(LED3);

  /*##-3- Start the transmission process #####################################*/
  /* While the I2C in reception process, user can transmit data through
     "aTxBuffer" buffer */
  /* Timeout is set to 10S */
  if(HAL_I2C_Slave_Transmit(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK)
  {
    /* Transfer error in transmission process */
    Error_Handler();
  }

  /* Turn LED3 off: Transfer in transmission process is correct */
  BSP_LED_Off(LED3);

#endif /* MASTER_BOARD */

  /*##-4- Compare the sent and received buffers ##############################*/
  if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  {
    /* Processing Error */
    Error_Handler();
  }

  /* Infinite loop */
  while (1)
  {
  }
}
void BQ27542_blockWrite(unsigned char *buffer, unsigned int length)
{
  if(HAL_I2C_Master_Transmit(&hi2c1, bq27542_ADDR, buffer, length, 0xFFFF) != HAL_OK){
	  BQ27542_error();
  }
}
Example #30
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    sAPP_PIXARM_READ_DATA* data;
    sAPP_PIXARM_READ_REQ* req;
    uint8_t iter = 1;
    HAL_StatusTypeDef stat;
    
    /* STM32F4xx HAL library initialization:
        - Configure the Flash prefetch, instruction and Data caches
        - Configure the Systick to generate an interrupt each 1 msec
        - Set NVIC Group Priority to 4
        - Global MSP (MCU Support Package) initialization
        */
    HAL_Init();
    
    /* Configure LED4, LED5 and LED6 */
    BSP_LED_Init(LED4);
    BSP_LED_Init(LED5);
    BSP_LED_Init(LED6);
    BSP_LED_Init(LED3);
    
    /* Configure the system clock to 168 MHz */
    SystemClock_Config();
    
    /*##-1- Configure the I2C peripheral ######################################*/
    I2cHandle.Instance             = I2Cx;
    
    I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Init.ClockSpeed      = 400000;
    I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE_16_9;
    I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
    I2cHandle.Init.OwnAddress1     = I2C_ADDRESS;
    I2cHandle.Init.OwnAddress2     = 0x0;
    
    if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();    
    }
    
    Test_Log_Init();
    
    /* Configure USER Button */
    BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
    
    /* Wait for USER Button press before starting the Communication */
    while (BSP_PB_GetState(BUTTON_KEY) != 1)
    {
    }
    
    /* Wait for USER Button release before starting the Communication */
    while (BSP_PB_GetState(BUTTON_KEY) != 0)
    {
    }
    
    BSP_LED_On(LED3);
    BSP_LED_Off(LED6);
    BSP_LED_Off(LED4);
    
    /* The board sends the message and expects to receive it back */
    Test_Log("Starting I2C Handshake.\r\n");
    
    /*##-2- Start the transmission process #####################################*/  
    /* Timeout is set to 10S */
    while(HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK)
    {
        /* Error_Handler() function is called when Timeout error occurs.
        When Acknowledge failure occurs (Slave don't acknowledge it's address)
        Master restarts communication */
        if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
        {
            Error_Handler();
        }
    }
    
    
    
    /*##-3- Put I2C peripheral in reception process ############################*/ 
    /* Timeout is set to 10S */ 
    while((stat = HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE, 10000)) != HAL_OK)
    {
        /* Error_Handler() function is called when Timeout error occurs.
        When Acknowledge failure occurs (Slave don't acknowledge it's address)
        Master restarts communication */
        if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
        {
        Error_Handler();
        }   
    }
    
    /* Turn LED6 on: Transfer in reception process is correct */
    BSP_LED_On(LED6);
    
    /*##-4- Compare the sent and received buffers ##############################*/
    //if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
    //{
    //    /* Processing Error */
    //    Error_Handler();
    //}
    
    Test_Log("Finished I2C Handshaking.\r\n");
    
    
    req = (sAPP_PIXARM_READ_REQ *) bTxBuffer;
    data = (sAPP_PIXARM_READ_DATA *) aRxBuffer;
    
    /* Infinite loop */  
    while (1)
    {
        
        /*##-2- Start the transmission process #####################################*/  
        /* Timeout is set to 10S */
        Test_Log("Iteration %d\r\n", iter);
        while((stat = HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, bTxBuffer, TXBUFFERSIZE, 10000)) !=  HAL_OK)
        {
            /* Error_Handler() function is called when Timeout error occurs.
            When Acknowledge failure occurs (Slave don't acknowledge it's address)
            Master restarts communication */
            if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
            {
                Test_Log("Error during Transmission. Transmission Error no. %d\r\n", stat);
                Test_Log("Error During Transmission.\r\n");
                Error_Handler();
            }
            //Test_Log("Transmit Failed. Error no. %d. Trying again.\r\n", stat);
        }
        
        while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY)
        {
        }
        
        req->rotation_absolute = 0;
        
        /*##-3- Put I2C peripheral in reception process ############################*/ 
        /* Timeout is set to 10S */ 
        while((stat = HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE, 10000)) != HAL_OK)
        {
            /* Error_Handler() function is called when Timeout error occurs.
            When Acknowledge failure occurs (Slave don't acknowledge it's address)
            Master restarts communication */
            if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
            {
                Test_Log("Error during Reception. Receive Error no. %d\r\n", stat);
                Test_Log("Error during Reception. I2C Error no. %d\r\n", HAL_I2C_GetError(&I2cHandle));
                Error_Handler();
            }
            //Test_Log("Receive Failed. Error no. %d. Trying again.\r\n", stat);
        }
        
        while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY)
        {
        }
        
        // Validate the data
        if (data->cmd != 0x04)
        {
            Test_Log("CMD incorrect on packet. Received %x.\r\n", data->cmd);
        }
        if (data->x_intensity != 4)
        {
            Test_Log("X incorrect on packet. Received %d.\r\n", data->x_intensity);
        }
        if (data->y_intensity != 4)
        {
            Test_Log("Y incorrect on packet. Received %d.\r\n", data->y_intensity);
        }
        if (data->z_intensity != 4)
        {
            Test_Log("Z incorrect on packet. Received %d.\r\n", data->z_intensity);
        }
        if (data->rotation_absolute != (int16_t) 36000)
        {
            Test_Log("Rotation in packet was %d.\r\n", data->rotation_absolute);
            Test_Log("Replying with same rotation.\r\n");
            req->rotation_absolute = data->rotation_absolute;
        }
        if (data->flag != 0xFF)
        {
            Test_Log("Flag incorrect on packet. Received %x.\r\n", data->flag);
        }
        
        HAL_Delay(100);
        iter++;
    }
}