Ejemplo n.º 1
0
void SensorsTaskOSInit(void)
{   
    osSemaphoreDef(i2cTransactSem);
        
    i2cSem = osSemaphoreCreate(osSemaphore(i2cTransactSem), 1);
    
    ALBI2CHandle.Instance              = ALB_I2C;
    ALBI2CHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    ALBI2CHandle.Init.Timing           = I2C_TIMING_100KHZ;
    ALBI2CHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    ALBI2CHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    ALBI2CHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    ALBI2CHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
    ALBI2CHandle.Init.OwnAddress1      = 0x00;
    ALBI2CHandle.Init.OwnAddress2      = 0x00;
    
    assert_param(HAL_I2C_Init(&ALBI2CHandle) == HAL_OK);
    
    SLBI2CHandle.Instance              = SLB_I2C;
    SLBI2CHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    SLBI2CHandle.Init.Timing           = I2C_TIMING_100KHZ;
    SLBI2CHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    SLBI2CHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    SLBI2CHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    SLBI2CHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
    SLBI2CHandle.Init.OwnAddress1      = 0x00;
    SLBI2CHandle.Init.OwnAddress2      = 0x00;
    
    assert_param(HAL_I2C_Init(&SLBI2CHandle) == HAL_OK);
}
Ejemplo n.º 2
0
/**
  * @brief I2C1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_I2C1_Init(void)
{

  /* USER CODE BEGIN I2C1_Init 0 */

  /* USER CODE END I2C1_Init 0 */

  /* USER CODE BEGIN I2C1_Init 1 */

  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */

  /* USER CODE END I2C1_Init 2 */

}
Ejemplo n.º 3
0
/**
 * @brief  Configures I2C interface
 * @param  None
 * @retval HAL status
 */
static HAL_StatusTypeDef I2C_SHIELDS_Init(void)
{
    HAL_StatusTypeDef ret_val = HAL_OK;
  
    if(HAL_I2C_GetState(&I2C_SHIELDS_Handle) == HAL_I2C_STATE_RESET)
    {
        /* I2C_SHIELDS peripheral configuration */
      //  I2C_SHIELDS_Handle.Init.ClockSpeed = NUCLEO_I2C_SHIELDS_SPEED;
      //  I2C_SHIELDS_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
#ifdef STM32F401xE      
        I2C_SHIELDS_Handle.Init.ClockSpeed = NUCLEO_I2C_SHIELDS_SPEED;
        I2C_SHIELDS_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
#endif
#ifdef STM32L053xx
        I2C_SHIELDS_Handle.Init.Timing = 0x0070D8FF;                            /*Refer AN4235-Application note Document*/
#endif        
        I2C_SHIELDS_Handle.Init.OwnAddress1 = 0x33;
        I2C_SHIELDS_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
        I2C_SHIELDS_Handle.Instance = NUCLEO_I2C_SHIELDS;

        /* Init the I2C */
        I2C_SHIELDS_MspInit();
        ret_val = HAL_I2C_Init(&I2C_SHIELDS_Handle);
    }
    
    return ret_val;
}
Ejemplo n.º 4
0
/* I2C2 init function */
void MX_I2C2_Init(void)
{

  hi2c2.Instance = I2C2;
  hi2c2.Init.Timing = 0x20303E5D;
  hi2c2.Init.OwnAddress1 = 0;
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c2.Init.OwnAddress2 = 0;
  hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  {
    Error_Handler();
  }

    /**Configure Analogue filter 
    */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }

}
Ejemplo n.º 5
0
void VL6180x_Shield_I2C1_Init(I2C_HandleTypeDef *hi2c1) {
    GPIO_InitTypeDef GPIO_InitStruct;
    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();

    /**I2C1 GPIO Configuration
     PB8     ------> I2C1_SCL	
     PB9     ------> I2C1_SDA
     */
    GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    hi2c1->Instance = I2C1;
    hi2c1->Init.ClockSpeed = 400000;
    hi2c1->Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c1->Init.OwnAddress1 = 0;
    hi2c1->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    hi2c1->Init.OwnAddress2 = 0;
    hi2c1->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    hi2c1->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
    HAL_I2C_Init(hi2c1);
}
Ejemplo n.º 6
0
/* I2C3 init function */
void MX_I2C3_Init(void)
{

	/*
	 * The following is the required sequence in master mode.
	 *
	 * Program the peripheral input clock in I2C_CR2 Register in order to generate correct
	 * timings
	 * Configure the clock control registers
	 * Configure the rise time register
	 * Program the I2C_CR1 register to enable the peripheral
	 * Set the START bit in the I2C_CR1 register to generate a Start condition
	 */

  hi2c3.Instance = I2C3;
  hi2c3.Init.ClockSpeed = 100000;
  hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c3.Init.OwnAddress1 = 0;
  hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
  hi2c3.Init.OwnAddress2 = 0;
  hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
  hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
  HAL_I2C_Init(&hi2c3);

}
Ejemplo n.º 7
0
int initMPU(void){
	int initOkay = -1;
	HAL_I2C_StateTypeDef state;
	uint8_t tempByte = 13;
	uint8_t buffer[10] = {0,0,0,0,0,0,0,0,0,0};
	hnd.Instance = I2C1;
	hnd.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	hnd.Init.ClockSpeed	= 400000;
	hnd.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	hnd.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
	hnd.Init.DutyCycle = I2C_DUTYCYCLE_2;
	hnd.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
	hnd.Init.OwnAddress1 = 0x00;
	
	HAL_I2C_Init(&hnd);
	__HAL_I2C_ENABLE(&hnd);
	state = HAL_I2C_GetState(&hnd); 
	if(state == HAL_I2C_STATE_READY){
		initOkay = 0;
	}
	buffer[0]=MPU6050_RA_PWR_MGMT_1;
	buffer[1]=0x80;
	printf("READ: %u",SCCB_Read(MPU6050_RA_WHO_AM_I));
	printf("error: %u",HAL_I2C_GetError(&hnd));
	return initOkay;

}
Ejemplo n.º 8
0
CSensor::CSensor() : mMPU1(false, mHandle),
					 	 	 	      	  	  	  	  	 mMPU2(true, mHandle)
{
	mHandle.Instance = I2C1;
	__HAL_RCC_GPIOB_CLK_ENABLE();
	__HAL_RCC_I2C1_CLK_ENABLE();

	GPIO_InitTypeDef initPort;
	initPort.Pin 		= GPIO_PIN_6 | GPIO_PIN_7;
	initPort.Mode 		= GPIO_MODE_AF_OD;
	initPort.Pull 		= GPIO_PULLUP;
	initPort.Speed 		= GPIO_SPEED_FREQ_VERY_HIGH;
	initPort.Alternate 	= GPIO_AF4_I2C1;
	HAL_GPIO_Init(GPIOB, &initPort);

	mHandle.Init.ClockSpeed 		= 400000U;
	mHandle.Init.DutyCycle 			= I2C_DUTYCYCLE_2;
	mHandle.Init.OwnAddress2 		= 0x00U;
	mHandle.Init.OwnAddress1 		= 0x00U;
	mHandle.Init.AddressingMode 	= I2C_ADDRESSINGMODE_7BIT;
	mHandle.Init.DualAddressMode 	= I2C_DUALADDRESS_DISABLE;
	mHandle.Init.GeneralCallMode 	= I2C_GENERALCALL_DISABLE;
	mHandle.Init.NoStretchMode 		= I2C_NOSTRETCH_DISABLE;
	HAL_I2C_Init(&mHandle);

	if(false == (mMPU1.init() && mMPU2.init()))
	{
		//Error-Handler
	}
	mMPU1.writeRegister(MPU6050::CONFIG, static_cast<UInt8>(ELowPass::BANDWIDTH_44));
	mMPU2.writeRegister(MPU6050::CONFIG, static_cast<UInt8>(ELowPass::BANDWIDTH_44));
}
Ejemplo n.º 9
0
/**
 * @brief  Configures I2C interface
 * @retval HAL status
 */
static HAL_StatusTypeDef I2C_EXPBD_Init(void)
{
    HAL_StatusTypeDef ret_val = HAL_OK;

    if(HAL_I2C_GetState(&I2C_EXPBD_Handle) == HAL_I2C_STATE_RESET)
    {
        /* I2C_EXPBD peripheral configuration */
#if ((defined (USE_STM32F4XX_NUCLEO)) || (defined (USE_STM32L1XX_NUCLEO)))
        I2C_EXPBD_Handle.Init.ClockSpeed = NUCLEO_I2C_EXPBD_SPEED;
        I2C_EXPBD_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2;
#endif

#if (defined (USE_STM32L0XX_NUCLEO))
        I2C_EXPBD_Handle.Init.Timing = NUCLEO_I2C_EXPBD_TIMING_400KHZ;                            /* 400KHz */
#endif
        I2C_EXPBD_Handle.Init.OwnAddress1 = 0x33;
        I2C_EXPBD_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
        I2C_EXPBD_Handle.Instance = NUCLEO_I2C_EXPBD;

        /* Init the I2C */
        I2C_EXPBD_MspInit();
        ret_val = HAL_I2C_Init(&I2C_EXPBD_Handle);
    }

    return ret_val;
}
Ejemplo n.º 10
0
void i2c_frequency(i2c_t *obj, int hz) {
    MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);

    // Common settings: I2C clock = 32 MHz, Analog filter = ON, Digital filter coefficient = 0
    switch (hz) {
        case 100000:
            I2cHandle.Init.Timing = 0x20602938; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
            break;
        case 400000:
            I2cHandle.Init.Timing = 0x00B0122A; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
            break;
        case 1000000:
            I2cHandle.Init.Timing = 0x0030040E; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
            break;
        default:
            break;
    }

    // I2C configuration
    I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
    I2cHandle.Init.OwnAddress1      = 0;
    I2cHandle.Init.OwnAddress2      = 0;
    I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    HAL_I2C_Init(&I2cHandle);
}
Ejemplo n.º 11
0
/*******************************************************************************
 * @brief  : Initialise et configure le peripherique I2C #2
 * @param  : Aucun.
 * @return : Rien.
 ******************************************************************************/
void LBF_I2C1_Init (uint32_t I2C1_Speed)
{
// Following settings assume I2C1 core clock (=APB1 clock in Default LimiFrog setting)
// is 8MHz and target I2C1 speed is 100KHz
// TODO Find a way to ensure I2C2 clock remains 8MHz if STM32L4 core clock modified

#define I2C1_PRESC	0x1   // tPRESC = (PRESC+1) x tI2C2CLK  -- 0.25us / 4MHz
#define I2C1_SCLL  	0x13   // tSCLL = (SCLL+1) x tPRESC  --  5us
#define I2C1_SCLH 	0x0F   // tSCLH = (SCLH+1) x tPRESC  --  4us
#define I2C1_SDADEL 	0x2   // tSDADEL = SDADEL x tPRESC  -- 0.5us
#define I2C1_SCLDEL	0x4   // tSCLDEL = (SCLDEL+1) x tPRESC  -- 1.25us
// I2C speed: 400KHz,
//  defined by tSCL = tSCLL + tSCLH + tSYNC1 + tSYNC2 ~ 2.5us  (400KHz)
//  where tSYNC1 and tSYNC2 are delays introduced by the analog and/or digital noise filters and resync mechasinsms -- refer to Ref Manual I2C section (I2C master mode)
// SCLDEL defines setup time: SDA stable before SCL rising edge
// SDADEL defines hold time: SDA stable after SCL *falling* edge (as per I2C spec?)

  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = ((uint32_t)I2C1_PRESC ) << 28
		    | ((uint32_t)I2C1_SCLDEL ) << 20
		    | ((uint32_t)I2C1_SDADEL ) << 16
		    | ((uint32_t)I2C1_SCLH ) << 8
		    | ((uint32_t)I2C1_SCLL )  ;
      //Contents of the I2C_TIMINGR_register value
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; 
  HAL_I2C_Init(&hi2c1);

}
Ejemplo n.º 12
0
/* I2C1 init function */
static void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x2000090E;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Analogue filter
    */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Digital filter
    */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}
Ejemplo n.º 13
0
void i2c_hal_intialization()
{
 /*------------------- Initialization I2C bus -------------------*/
    /*---------- Setup GPIOB - I2C1 port --------------*/

    __GPIOB_CLK_ENABLE(); // Enable clock source for GPIOB

    I2C1_GPIO_struct.Pin = I2C1_SDA|I2C1_SCL;
    I2C1_GPIO_struct.Mode = GPIO_MODE_AF_OD;
    I2C1_GPIO_struct.Pull = GPIO_PULLUP;
    I2C1_GPIO_struct.Speed = GPIO_SPEED_HIGH;
    I2C1_GPIO_struct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, & I2C1_GPIO_struct);

    /*------------------- Setup I2C1 -------------------*/

    __I2C1_CLK_ENABLE(); // Enable clock source for I2C peripherals

    I2C1_struct.Instance = I2C1;
    I2C1_struct.Init.ClockSpeed = 400000;
    I2C1_struct.Init.DutyCycle = I2C_DUTYCYCLE_2;
    I2C1_struct.Init.OwnAddress1 = 0;
    I2C1_struct.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    I2C1_struct.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    I2C1_struct.Init.OwnAddress2 = 0;
    I2C1_struct.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    I2C1_struct.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
    HAL_I2C_Init(&I2C1_struct);
}
Ejemplo n.º 14
0
void i2c_frequency(i2c_t *obj, int hz)
{
    MBED_ASSERT((hz > 0) && (hz <= 400000));
    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
    int timeout;

    // wait before init
    timeout = LONG_TIMEOUT;
    while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));

    // I2C configuration
    I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Init.ClockSpeed      = hz;
    I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE_2;
    I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLED;
    I2cHandle.Init.OwnAddress1     = 0;
    I2cHandle.Init.OwnAddress2     = 0;
    HAL_I2C_Init(&I2cHandle);
    if (obj->slave) {
        /* Enable Address Acknowledge */
        I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
    }

}
Ejemplo n.º 15
0
/**
  * @brief  Initializes peripherals used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
bool TWI_open(new_twi* TwiStruct)
{
	if(!TwiStruct)
		return false;
	  I2C_HandleTypeDef *I2cHandle = calloc(1, sizeof(I2C_HandleTypeDef));
	  if(!I2cHandle)
		  return false;
	   //I2C_InitTypeDef  I2C_InitStructure;
	  TwiStruct->udata = (void *)I2cHandle;
	  sEE_LowLevel_Init(TwiStruct);
/*##-1- Configure the I2C peripheral #######################################*/
	  I2cHandle->Instance             = sEE_I2C[TwiStruct->TwiNr];

	  I2cHandle->Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
	  I2cHandle->Init.Timing      		= I2C_TIMING;
	  I2cHandle->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
	  //I2cHandle->Init.DutyCycle       = I2C_DUTYCYCLE_2;
	  I2cHandle->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	  I2cHandle->Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
	  I2cHandle->Init.OwnAddress1     = TwiStruct->MasterSlaveAddr;
	  //I2cHandle.Init.OwnAddress2     = 0xFF;

	  if(HAL_I2C_Init(I2cHandle) != HAL_OK)
	  {
		  /* Initialization Error */
		  return false;
	  }
	  return true;
}
Ejemplo n.º 16
0
void i2c_frequency(i2c_t *obj, int hz) {
    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);

    // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
    switch (hz) {
        case 100000:
            I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
            break;
        case 400000:
            I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
            break;
        case 1000000:
            I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
            break;
        default:
            error("Only 100kHz, 400kHz and 1MHz I2C frequencies are supported.");
            break;
    }

    // I2C configuration
    I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
    I2cHandle.Init.OwnAddress1      = 0;
    I2cHandle.Init.OwnAddress2      = 0;
    I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    HAL_I2C_Init(&I2cHandle);
}
Ejemplo n.º 17
0
bool ADXL345::init(RANGE range, DATA_RATE dataRate){
	GPIO PB10(GPIOB, GPIO::PIN10);
	GPIO PB11(GPIOB, GPIO::PIN11);
	
	PB10.init(GPIO::AF, GPIO::OD, GPIO::NONE, GPIO::MEDIUM);
	PB11.init(GPIO::AF, GPIO::OD, GPIO::NONE, GPIO::MEDIUM);
	
	/* Init I2C peripherals */
	__HAL_RCC_I2C2_CLK_ENABLE();	

	i2c2.Instance             = I2C2;
	i2c2.Init.ClockSpeed      = 100000; // clock speed 100kHz
	i2c2.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
	i2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
	i2c2.Init.DutyCycle	      = I2C_DUTYCYCLE_2;
	i2c2.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
	i2c2.Init.OwnAddress1     = 0x00;
	i2c2.Init.OwnAddress2     = 0x00;
	i2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
	HAL_I2C_Init(&i2c2);
	if(this->getDevID() != 0xE5){
		return false;
	}
	this->writeByte(POWER_CTL_REG, 0x08); // enable mesuare mode
	this->clearSettings();
	this->setRange(range);
	this->setDataRate(dataRate);
	
	return true;
}
Ejemplo n.º 18
0
void i2c_frequency(i2c_t *obj, int hz)
{
    MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
    int timeout;

    // wait before init
    timeout = LONG_TIMEOUT;
    while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));

    // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
    switch (hz) {
        case 100000:
            I2cHandle.Init.Timing = 0x10805E89; // Standard mode with Rise Time = 400ns and Fall Time = 100ns
            break;
        case 400000:
            I2cHandle.Init.Timing = 0x00901850; // Fast mode with Rise Time = 250ns and Fall Time = 100ns
            break;
        case 1000000:
            I2cHandle.Init.Timing = 0x00700818; // Fast mode Plus with Rise Time = 60ns and Fall Time = 100ns
            break;
        default:
            break;
    }

    // I2C configuration
    I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;
    I2cHandle.Init.OwnAddress1      = 0;
    I2cHandle.Init.OwnAddress2      = 0;
    I2cHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    HAL_I2C_Init(&I2cHandle);
}
Ejemplo n.º 19
0
int tca9545_init(void){
	GPIO_InitTypeDef GPIO_InitStruct;
	uint8_t buf;

	GPIO_InitStruct.Pin = tca9545RESET_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    HAL_GPIO_Init(tca9545RESET_PORT, &GPIO_InitStruct);
    tca9545_reset();

	/* Init I2C Bus */
	hi2c1.Instance = I2C1;
	hi2c1.Init.ClockSpeed = 400000;
	hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
	hi2c1.Init.OwnAddress1 = 0;
	hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
	hi2c1.Init.OwnAddress2 = 0;
	hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
	hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
	HAL_I2C_Init(&hi2c1);

	/* Check the chip information */
	buf = tca9545_get();
	return 0;
}
Ejemplo n.º 20
0
void I2C_Reset(void)
{
  HAL_GPIO_WritePin(I2Cx_WAKEUP_GPIO_PORT, I2Cx_WAKEUP_PIN, GPIO_PIN_RESET);

  HAL_I2C_DeInit(&I2cHandle);

  HAL_I2C_Init(&I2cHandle);

  HAL_Delay(5);
  HAL_GPIO_WritePin(I2Cx_WAKEUP_GPIO_PORT, I2Cx_WAKEUP_PIN, GPIO_PIN_SET);
  HAL_Delay(400);
}
Ejemplo n.º 21
0
/* I2C1 init function */
static void MX_I2C1_Init(void)
{

    hi2c1.Instance = I2C1;
//    hi2c1.Init.Timing = 0x2000090E;

    hi2c1.Init.Timing =
        (1 << I2C_TIMINGR_PRESC_Pos)    |
        (0x13 << I2C_TIMINGR_SCLL_Pos)  |
        (0x0F << I2C_TIMINGR_SCLH_Pos)  |
        (0x2 << I2C_TIMINGR_SDADEL_Pos) |
        (0x4 << I2C_TIMINGR_SCLDEL_Pos) ;


    hi2c1.Init.OwnAddress1 = I2C_SLAVE_ADDR << 1;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    if (HAL_I2C_Init(&hi2c1) != HAL_OK)
    {
        Error_Handler();
    }

    /**Configure Analogue filter
    */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        Error_Handler();
    }


    uart_printf("i2c init..\r\n");

    HAL_I2C_Init(&hi2c1);

    uart_printf("i2c init done..\r\n");
}
Ejemplo n.º 22
0
/* I2C3 init function */
void MX_I2C3_Init(void)
{
    hi2c3.Instance = I2C3;
    hi2c3.Init.ClockSpeed = 400000;
    hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c3.Init.OwnAddress1 = 0x32 << 1;
    hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    hi2c3.Init.OwnAddress2 = 0;
    hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
    HAL_I2C_Init(&hi2c3);
}
Ejemplo n.º 23
0
void SSD1306_I2C_Init(void) {
    hi2c.Instance = SSD1306_I2Cx;
    hi2c.Init.ClockSpeed = SSD1306_CLOCK;
    hi2c.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c.Init.OwnAddress1 = 0;
    hi2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    hi2c.Init.OwnAddress2 = 0;
    hi2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    hi2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
    HAL_I2C_MspInit(&hi2c);
    HAL_I2C_Init(&hi2c);
}
Ejemplo n.º 24
0
/**
 * @brief	Initializes the I2C
 * @param	None
 * @retval	None
 */
void I2C2_Init()
{
	/* Make sure we only initialize it once */
	if (!prvInitialized)
	{
		/* Mutex semaphore for mutual exclusion to the I2C2 device */
		xSemaphore = xSemaphoreCreateMutex();

		if (xSemaphoreTake(xSemaphore, 100) == pdTRUE)
		{
			/* I2C clock & GPIOB enable */
			__GPIOB_CLK_ENABLE();
			__I2C2_CLK_ENABLE();

			/* I2C SDA and SCL configuration */
			GPIO_InitTypeDef GPIO_InitStructure;
			GPIO_InitStructure.Pin  		= I2C_SCL_PIN | I2C_SDA_PIN;
			GPIO_InitStructure.Mode  		= GPIO_MODE_AF_OD;
			GPIO_InitStructure.Alternate 	= GPIO_AF4_I2C2;
			GPIO_InitStructure.Pull			= GPIO_NOPULL;
			GPIO_InitStructure.Speed 		= GPIO_SPEED_HIGH;
			HAL_GPIO_Init(I2C_PORT, &GPIO_InitStructure);

		//	/* NVIC Configuration */
		//	NVIC_InitTypeDef NVIC_InitStructure;
		//	/* Event interrupt */
		//	NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
		//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY;
		//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
		//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		//	NVIC_Init(&NVIC_InitStructure);
		//	/* Error interrupt */
		//	NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn;
		//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14;
		//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
		//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
		//	NVIC_Init(&NVIC_InitStructure);

			/* I2C Init */
			HAL_I2C_Init(&I2C_Handle);

			/* Enable the I2C1 interrupts */
		//	I2C_ITConfig(I2C_PERIPHERAL, I2C_IT_EVT, ENABLE);
		//	I2C_ITConfig(I2C_PERIPHERAL, I2C_IT_ERR, ENABLE);

			xSemaphoreGive(xSemaphore);
		}

		prvInitialized = true;
	}
}
Ejemplo n.º 25
0
void MX_I2C3_Init( I2C_HandleTypeDef &i2c, uint32_t speed  )
{
  i2c.Instance              = I2C3;
  i2c.Init.ClockSpeed       = speed;
  i2c.State                 = HAL_I2C_STATE_RESET;
  i2c.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
  i2c.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
  i2c.Init.DutyCycle        = I2C_DUTYCYCLE_16_9;
  i2c.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
  i2c.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
  i2c.Init.OwnAddress1      = 0;
  i2c.Init.OwnAddress2      = 0;
  HAL_I2C_Init( &i2c );
}
Ejemplo n.º 26
0
// Initialize i2c1. Currently connected to the IMU and the digital pot
void init_i2c1(void)
{
	//I2C_HandleTypeDef *hi2c1 contains our handle information
	//set config for the initial state of the i2c.
	hi2c1.Instance = I2C1;
	hi2c1.Init.ClockSpeed = I2C_CLOCK_RATE;  				//clock frequency; less than 400kHz
	hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; 				//for fast mode (doesn't matter now)
	hi2c1.Init.OwnAddress1 = 0x0; 							//device address of the STM32 (doesn't matter)
	hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;	//using 7 bit addresses
	hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;  //disable dual address
	hi2c1.Init.OwnAddress2 = 0x0;							//second device addr (doesn't matter)
	hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;  //don't use 0x0 addr
	hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; 		//allow slave to stretch SCL
	hi2c1.State = HAL_I2C_STATE_RESET;
	HAL_I2C_Init(&hi2c1);
}
Ejemplo n.º 27
0
/**
  * @brief  Configures I2C interface.
  * @param  None
  * @retval None
  */
static void I2Cx_Init(void)
{
  if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
  {
    /* DISCOVERY_I2Cx peripheral configuration */
    I2cHandle.Init.ClockSpeed = 10000;
    I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
    I2cHandle.Init.OwnAddress1 = 0x33;
    I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    I2cHandle.Instance = DISCOVERY_I2Cx;
      
    /* Init the I2C */
    I2Cx_MspInit();
    HAL_I2C_Init(&I2cHandle);
  }
}
Ejemplo n.º 28
0
/**
  * @brief  initializes the M24LR04E
  * @param  None
  * @retval None
  */
void M24LR04E_init () 
{
	I2CHandle.Instance              = I2C;
  I2CHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
  I2CHandle.Init.Timing           = I2C_TIMING_100KHZ;
  I2CHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
  I2CHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  I2CHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
  I2CHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;
  I2CHandle.Init.OwnAddress1      = 0xFE;
  I2CHandle.Init.OwnAddress2      = 0xFE;
  if(HAL_I2C_Init(&I2CHandle) != HAL_OK) {
		get_app_config()->error_code = I2C_ERROR;
    Error_Handler();    
  }
}
/**
  * @brief Discovery I2Cx Bus initialization
  * @param None
  * @retval None
  */
void I2Cbar_Init(void)
{
  if(HAL_I2C_GetState(&I2CbarHandle) == HAL_I2C_STATE_RESET)
  {
    I2CbarHandle.Instance = DISCOVERY_I2Cbar;
    I2CbarHandle.Init.OwnAddress1 =  BAROMETER_I2C_ADDRESS;
    I2CbarHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    I2CbarHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
    I2CbarHandle.Init.OwnAddress2 = 0;
    I2CbarHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
    I2CbarHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;

    /* Init the I2C */
    I2Cbar_MspInit();
    HAL_I2C_Init(&I2CbarHandle);
  }
}
Ejemplo n.º 30
0
/* I2C1 init function */
void MX_I2C1_Init(void)
{

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 400000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = I2C_ADDR_HOST_MCU;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLED;
  HAL_I2C_Init(&hi2c1);
  NVIC_SetPriority(I2C1_EV_IRQn, 6);
  HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);

}