Exemplo n.º 1
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // Determine the I2C to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT(obj->i2c != (I2CName)NC);

    // Enable I2C clock
    if (obj->i2c == I2C_1) {
        __I2C1_CLK_ENABLE();
    }
    if (obj->i2c == I2C_2) {
        __I2C2_CLK_ENABLE();
    }
    if (obj->i2c == I2C_3) {
        __I2C3_CLK_ENABLE();
    }

    // Configure I2C pins
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);
    pin_mode(sda, OpenDrain);
    pin_mode(scl, OpenDrain);
    
    // Reset to clear pending flags if any
    i2c_reset(obj);
    
    // I2C configuration
    i2c_frequency(obj, 100000); // 100 kHz per default
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }

}
Exemplo n.º 3
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

    GPIO_InitTypeDef GPIO_InitStruct;
    if(hi2c->Instance==I2C1)
    {
        /* USER CODE BEGIN I2C1_MspInit 0 */

        /* USER CODE END I2C1_MspInit 0 */

        /**I2C1 GPIO Configuration
        PB7     ------> I2C1_SDA
        PB8     ------> I2C1_SCL
        */
        GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

        /* Peripheral clock enable */
        __I2C1_CLK_ENABLE();

        /* Peripheral interrupt init*/
        HAL_NVIC_SetPriority(I2C1_EV_IRQn, 7, 0);
        HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
        HAL_NVIC_SetPriority(I2C1_ER_IRQn, 6, 0);
        HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
        /* USER CODE BEGIN I2C1_MspInit 1 */

        /* USER CODE END I2C1_MspInit 1 */
    }
}
Exemplo n.º 4
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);
}
Exemplo n.º 5
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
    // Determine the I2C to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);

    if (obj->i2c == (I2CName)NC) {
        error("I2C error: pinout mapping failed.");
    }

    // Enable I2C clock
    if (obj->i2c == I2C_1) {
        __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
        __I2C1_CLK_ENABLE();
    }
    if (obj->i2c == I2C_2) {
        __I2C2_CLK_ENABLE();
    }

    // Configure I2C pins
    pinmap_pinout(sda, PinMap_I2C_SDA);
    pinmap_pinout(scl, PinMap_I2C_SCL);
    pin_mode(sda, OpenDrain);
    pin_mode(scl, OpenDrain);

    // Reset to clear pending flags if any
    i2c_reset(obj);

    // I2C configuration
    i2c_frequency(obj, 100000); // 100 kHz per default
}
Exemplo n.º 6
0
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
  GPIO_InitTypeDef  GPIO_InitStruct;

  //Enable the GPIO-clock
  __GPIOB_CLK_ENABLE();
 
  //Enable the I2C-Clock
  __I2C1_CLK_ENABLE();
 
  //Configure GPIOB Pin_6 as SCL for I2C
  GPIO_InitStruct.Pin       = GPIO_PIN_6;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  //init GPIOB PIN6 (SCL-Pin)
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  //Configure GPIOB Pin_7 as SDA for USART
  GPIO_InitStruct.Pin = GPIO_PIN_7;
  GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  //init GPIOB PIN7 (SDA-Pin)
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  //Setup the Interruptlevel
  HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 1);
	HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 1);
  //enable the interrupt for USART1
  HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
	HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
}
Exemplo n.º 7
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**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_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }

}
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
    /* Peripheral clock enable */
    I2C1_SDA_GPIO_CLK_ENABLE();
    I2C1_SCL_GPIO_CLK_ENABLE();
    __I2C1_CLK_ENABLE();

    /**I2C1 GPIO Configuration    
    */
    GPIO_InitStruct.Pin = I2C1_SDA_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = I2C1_SDA_AF;
    HAL_GPIO_Init(I2C1_SDA_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = I2C1_SCL_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
    GPIO_InitStruct.Alternate = I2C1_SCL_AF;
    HAL_GPIO_Init(I2C1_SCL_PORT, &GPIO_InitStruct);
  }
}
Exemplo n.º 9
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);
}
Exemplo n.º 10
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
    static int i2c1_inited = 0;
    static int i2c2_inited = 0;
#if defined(I2C3_BASE)
    static int i2c3_inited = 0;
#endif

    // Determine the I2C to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT(obj->i2c != (I2CName)NC);

    // Enable I2C1 clock and pinout if not done
    if ((obj->i2c == I2C_1) && !i2c1_inited) {
        i2c1_inited = 1;
        __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK);
        __I2C1_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }

    // Enable I2C2 clock and pinout if not done
    if ((obj->i2c == I2C_2) && !i2c2_inited) {
        i2c2_inited = 1;
        __I2C2_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }

#if defined(I2C3_BASE)
    // Enable I2C3 clock and pinout if not done
    if ((obj->i2c == I2C_3) && !i2c3_inited) {
        i2c3_inited = 1;
        __I2C3_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }
#endif

    // Reset to clear pending flags if any
    i2c_reset(obj);

    // I2C configuration
    i2c_frequency(obj, 100000); // 100 kHz per default
}
Exemplo n.º 11
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();

    /* Peripheral DMA init*/
  
    hdma_i2c1_rx.Instance = DMA1_Stream5;
    hdma_i2c1_rx.Init.Channel = DMA_CHANNEL_1;
    hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
    hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
    hdma_i2c1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    HAL_DMA_Init(&hdma_i2c1_rx);

    __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx);

    /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 13);
    HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
    HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 15);
    HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
  /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }
  else if(hi2c->Instance==I2C2)
  {
  /* USER CODE BEGIN I2C2_MspInit 0 */

  /* USER CODE END I2C2_MspInit 0 */
  
    /**I2C2 GPIO Configuration    
    PB10     ------> I2C2_SCL
    PB11     ------> I2C2_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C2_CLK_ENABLE();
  /* Peripheral interrupt init*/
    HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
    HAL_NVIC_SetPriority(I2C2_ER_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
  /* USER CODE BEGIN I2C2_MspInit 1 */

  /* USER CODE END I2C2_MspInit 1 */
  }

}
Exemplo n.º 13
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();

    /* Peripheral DMA init*/
  
    hdma_i2c1_rx.Instance = DMA1_Channel7;
    hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c1_rx.Init.MemInc = DMA_MINC_DISABLE;
    hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
    hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_LOW;
    HAL_DMA_Init(&hdma_i2c1_rx);

    __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx);

    hdma_i2c1_tx.Instance = DMA1_Channel6;
    hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c1_tx.Init.MemInc = DMA_MINC_DISABLE;
    hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c1_tx.Init.Mode = DMA_NORMAL;
    hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW;
    HAL_DMA_Init(&hdma_i2c1_tx);

    __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c1_tx);

  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }

}
Exemplo n.º 14
0
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
    // Determine the I2C to use
    I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
    I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);

    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
    MBED_ASSERT(obj->i2c != (I2CName)NC);

    // Enable I2C1 clock and pinout if not done
    if ((obj->i2c == I2C_1) && !i2c1_inited) {
        i2c1_inited = 1;
        __I2C1_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }
    // Enable I2C2 clock and pinout if not done
    if ((obj->i2c == I2C_2) && !i2c2_inited) {
        i2c2_inited = 1;
        __I2C2_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }
    // Enable I2C3 clock and pinout if not done
    if ((obj->i2c == I2C_3) && !i2c3_inited) {
        i2c3_inited = 1;
        __I2C3_CLK_ENABLE();
        // Configure I2C pins
        pinmap_pinout(sda, PinMap_I2C_SDA);
        pinmap_pinout(scl, PinMap_I2C_SCL);
        pin_mode(sda, OpenDrain);
        pin_mode(scl, OpenDrain);
    }

    // Reset to clear pending flags if any
    i2c_reset(obj);

    // I2C configuration
    i2c_frequency(obj, 100000); // 100 kHz per default

    // I2C master by default
    obj->slave = 0;
}
//******************************************************************************
//  board_i2c_enable_clock
//
//           Ensures that the specified Timer's clock is turned on.
//******************************************************************************
int  board_i2c_enable_clock (int module_id)
{
      //--------------------------------------------------------
      // Turn on clock for the associated I2Cx module.
      //--------------------------------------------------------
    switch (module_id)
     { case 1:                           // I2C1
             __I2C1_CLK_ENABLE();
             break;
        case 2:                          // I2C2
             __I2C2_CLK_ENABLE();
             break;
     }

    return (0);          // denote everything worked OK
}
Exemplo n.º 16
0
// Implement I2C MSP Init, as called for in the stm32f4xx_hal_i2c.c file
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
	///// SET UP GPIO /////
	//GPIO initialization constants
	GPIO_InitTypeDef GPIO_InitStruct;

	//Enable peripheral and GPIO clockS
	__GPIOB_CLK_ENABLE();
	__I2C1_CLK_ENABLE();

	//IMU GPIO configuration
	/*
	 T_SWO	-> PB3
	 SCL1	-> PB8 (pin 23 on MPU6500)
	 SDA1	-> PB9 (pin 24 on MPU6500)
	 Be mindful that the SPI pins are also located on this bus!
	 Also:
	 IMUINT (pin 12 on MPU6500)

	 Note: AD0/SDO grounded -> address for MPU6500 is 0b1101000
	 */

	//Config inputs:
	//We are configuring these pins.
	GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
	//I2C wants to have open drain lines pulled up by resistors
	GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	//Although we need pullups for I2C, we have them externally on
	// the board.
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	//Set GPIO speed to fastest speed.
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	//Assign function to pins.
	GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
	//Initialize the pins.
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	///// SET UP NVIC ///// (interrupts!)
	#if I2C_USE_INT == 1
	HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 1);    //event interrupt
	HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
	HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 1);//error interrupt
	HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
	#endif
}
Exemplo n.º 17
0
void i2c_init(I2C_HandleTypeDef *i2c) {
    // init the GPIO lines
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines

    const pin_obj_t *pins[2];
    if (0) {
#if MICROPY_HW_ENABLE_I2C1
    } else if (i2c == &I2CHandle1) {
        // X-skin: X9=PB6=SCL, X10=PB7=SDA
        pins[0] = &pin_B6;
        pins[1] = &pin_B7;
        GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
        // enable the I2C clock
        __I2C1_CLK_ENABLE();
#endif
    } else if (i2c == &I2CHandle2) {
        // Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
        pins[0] = &pin_B10;
        pins[1] = &pin_B11;
        GPIO_InitStructure.Alternate = GPIO_AF4_I2C2;
        // enable the I2C clock
        __I2C2_CLK_ENABLE();
    } else {
        // I2C does not exist for this board (shouldn't get here, should be checked by caller)
        return;
    }

    // init the GPIO lines
    for (uint i = 0; i < 2; i++) {
        GPIO_InitStructure.Pin = pins[i]->pin_mask;
        HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
    }

    // init the I2C device
    if (HAL_I2C_Init(i2c) != HAL_OK) {
        // init error
        // TODO should raise an exception, but this function is not necessarily going to be
        // called via Python, so may not be properly wrapped in an NLR handler
        printf("HardwareError: HAL_I2C_Init failed\n");
        return;
    }
}
Exemplo n.º 18
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  	
	__I2C2_FORCE_RESET();  
    __I2C2_RELEASE_RESET(); 
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**I2C1 GPIO Configuration    
    PB8     ------> I2C1_SCL
    PB9     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	//hi2c->Instance->CR1 = I2C_CR1_SWRST; //复位I2C控制器
	//hi2c->Instance->CR1 = 0;  //	解除复位(不会自动清除)
	
    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
    /* USER CODE BEGIN I2C1_MspInit 1 */

    /* USER CODE END I2C1_MspInit 1 */

	/*##-3- Configure the NVIC for I2C #########################################*/   
	/* NVIC for I2C1 */
	//  HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 1);
	//  HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
	//  HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 2);
	//  HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
  }

}
Exemplo n.º 19
0
/* PCA9506DGG IO Expander works over I2C1
PB8: SCL
PB9: SDA
PC13: *INT (interrupt pin)
PC14: *OE pin (output enable)
PC15: *RESET pin
 */
void InitializeIOexpander()
{
	__GPIOB_CLK_ENABLE(); /* Enable I2C1 pins peripheral clock */

	__I2C1_CLK_ENABLE(); /* Enable I2C1 peripheral clock */

	GPIO_InitTypeDef GPIO_I2C1; /* Struct for I2C1 pins */
	GPIO_I2C1.Mode = GPIO_MODE_AF_OD; /* Alternate function, Open-Drain */
	GPIO_I2C1.Pin = GPIO_PIN_8 | GPIO_PIN_9; /* PB8:SCL PB9:SDA */
	GPIO_I2C1.Pull = GPIO_PULLUP; /* Pull up resistor for I2C */
	GPIO_I2C1.Speed = GPIO_SPEED_HIGH; /* High pin speed */
	GPIO_I2C1.Alternate = GPIO_AF4_I2C1; /* Pins connected to I2C1 */
	HAL_GPIO_Init(GPIOB, &GPIO_I2C1); /* Write pin config to registers */



	handleI2C1.Instance = I2C1; /* I2C1 is used */
	handleI2C1.Init.ClockSpeed = 400000; /* 400kHz bus speed */
	handleI2C1.Init.DutyCycle = I2C_DUTYCYCLE_2; /* 50:50 duty cycle */
	handleI2C1.Init.OwnAddress1 = 0x00; /* Address for this device. Not necessary */
	handleI2C1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; /* 7 bit address mode */
	handleI2C1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; /* Single address is enough */
	handleI2C1.Init.OwnAddress2 = 0x00; /* Not important since dual address mode is disabled */
	handleI2C1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; /* General call disabled, since this device is not slave */
	handleI2C1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; /* Stretch mode is free, slave can stretch the bus */
	Console("I2C1 HAL_I2C_Init (0 is ok):", HAL_I2C_Init(&handleI2C1)); /* Write configuration to I2C register */



	__GPIOC_CLK_ENABLE(); /* Enable C Port peripheral clock */

	GPIO_InitTypeDef GPIO_INT; /* Create a struct for INTerrupt pin */
	GPIO_INT.Pin = GPIO_PIN_13; /* PC13 */
	GPIO_INT.Mode = GPIO_MODE_IT_RISING_FALLING; /* INTerrupt on both edges */
	GPIO_INT.Pull = GPIO_PULLUP; /* Pull up resistor */
	HAL_GPIO_Init(GPIOC, &GPIO_INT); /* Write to PORTC register */

	HAL_NVIC_SetPriority(EXTI15_10_IRQn, 1, 0); /* Pre group 1, sub 2 */
	HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); /* Enable External interrupt on PC13 */.
Exemplo n.º 20
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
    /**I2C1 GPIO Configuration
    PF0-OSC_IN     ------> I2C1_SDA
    PF1-OSC_OUT     ------> I2C1_SCL
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
  }

}
Exemplo n.º 21
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();
  
    /**I2C1 GPIO Configuration    
    PB6     ------> I2C1_SCL
    PB7     ------> I2C1_SDA 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  }

}
Exemplo n.º 22
0
Arquivo: Init.c Projeto: antgrem/Meteo
void Init_BMP085 (void)
{
	  GPIO_InitTypeDef GPIO_InitStruct;


        __I2C1_CLK_ENABLE();
        __GPIOB_CLK_ENABLE();
				__HAL_RCC_AFIO_CLK_ENABLE();
	// Init I2C
        GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_8;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	__HAL_AFIO_REMAP_I2C1_ENABLE();

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

    
    I2Cdev_hi2c = &hi2c2; // init of i2cdevlib.  
    // You can select other i2c device anytime and 
    // call the same driver functions on other sensors

    while(!BMP085_testConnection()) ;

    BMP085_initialize();
}
Exemplo n.º 23
0
void i2c_init(I2C_HandleTypeDef *i2c) {
    int i2c_unit;
    const pin_obj_t *scl_pin;
    const pin_obj_t *sda_pin;

    if (0) {
    #if defined(MICROPY_HW_I2C1_SCL)
    } else if (i2c == &I2CHandle1) {
        i2c_unit = 1;
        scl_pin = &MICROPY_HW_I2C1_SCL;
        sda_pin = &MICROPY_HW_I2C1_SDA;
        __I2C1_CLK_ENABLE();
    #endif
    #if defined(MICROPY_HW_I2C2_SCL)
    } else if (i2c == &I2CHandle2) {
        i2c_unit = 2;
        scl_pin = &MICROPY_HW_I2C2_SCL;
        sda_pin = &MICROPY_HW_I2C2_SDA;
        __I2C2_CLK_ENABLE();
    #endif
    #if defined(MICROPY_HW_I2C3_SCL)
    } else if (i2c == &I2CHandle3) {
        i2c_unit = 3;
        scl_pin = &MICROPY_HW_I2C3_SCL;
        sda_pin = &MICROPY_HW_I2C3_SDA;
        __I2C3_CLK_ENABLE();
    #endif
    #if defined(MICROPY_HW_I2C4_SCL)
    } else if (i2c == &I2CHandle4) {
        i2c_unit = 4;
        scl_pin = &MICROPY_HW_I2C4_SCL;
        sda_pin = &MICROPY_HW_I2C4_SDA;
        __I2C3_CLK_ENABLE();
    #endif
    } else {
        // I2C does not exist for this board (shouldn't get here, should be checked by caller)
        return;
    }

    // init the GPIO lines
    uint32_t mode = MP_HAL_PIN_MODE_ALT_OPEN_DRAIN;
    uint32_t pull = MP_HAL_PIN_PULL_NONE; // have external pull-up resistors on both lines
    mp_hal_pin_config_alt(scl_pin, mode, pull, AF_FN_I2C, i2c_unit);
    mp_hal_pin_config_alt(sda_pin, mode, pull, AF_FN_I2C, i2c_unit);

    // init the I2C device
    if (HAL_I2C_Init(i2c) != HAL_OK) {
        // init error
        // TODO should raise an exception, but this function is not necessarily going to be
        // called via Python, so may not be properly wrapped in an NLR handler
        printf("OSError: HAL_I2C_Init failed\n");
        return;
    }

    // invalidate the DMA channels so they are initialised on first use
    const pyb_i2c_obj_t *self = &pyb_i2c_obj[i2c_unit - 1];
    dma_invalidate_channel(self->tx_dma_descr);
    dma_invalidate_channel(self->rx_dma_descr);
    
    if (0) {
    #if defined(MICROPY_HW_I2C1_SCL)
    } else if (i2c->Instance == I2C1) {
        HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
        HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
    #endif
    #if defined(MICROPY_HW_I2C2_SCL)
    } else if (i2c->Instance == I2C2) {
        HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
        HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
    #endif
    #if defined(MICROPY_HW_I2C3_SCL)
    } else if (i2c->Instance == I2C3) {
        HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);
        HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);
    #endif
    #if defined(MICROPY_HW_I2C4_SCL)
    } else if (i2c->Instance == I2C4) {
        HAL_NVIC_EnableIRQ(I2C4_EV_IRQn);
        HAL_NVIC_EnableIRQ(I2C4_ER_IRQn);
    #endif
    }
}
Exemplo n.º 24
0
/*
    (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
        (##) Enable the I2Cx interface clock
        (##) I2C pins configuration
            (+++) Enable the clock for the I2C GPIOs
            (+++) Configure I2C pins as alternate function open-drain
        (##) NVIC configuration if you need to use interrupt process
            (+++) Configure the I2Cx interrupt priority
            (+++) Enable the NVIC I2C IRQ Channel
        (##) DMA Configuration if you need to use DMA process
            (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
            (+++) Enable the DMAx interface clock using
            (+++) Configure the DMA handle parameters
            (+++) Configure the DMA Tx or Rx channel
            (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
            (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on 
                  the DMA Tx or Rx channel
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) {

//    (#)Initialize the I2C low level resources by implementing the HAL_I2C_MspInit() API:
//        (##) Enable the I2Cx interface clock
    //__HAL_I2C_ENABLE(hi2c); //necessary?
    __I2C1_CLK_ENABLE();

//        (##) I2C pins configuration
//            (+++) Enable the clock for the I2C GPIOs
    __GPIOB_CLK_ENABLE();

//            (+++) Configure I2C pins as alternate function open-drain

    GPIO_InitTypeDef GPIO_InitStruct = {
            .Pin = I2C_SCL_GPIO_PIN | I2C_SDA_GPIO_PIN,
            .Mode = GPIO_MODE_AF_OD,
            .Pull = GPIO_PULLUP, //?
            .Speed = GPIO_SPEED_FREQ_HIGH, //?
            .Alternate = GPIO_AF4_I2C1
    };
    HAL_GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStruct);

    //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);

//        (##) NVIC configuration if you need to use interrupt process
//            (+++) Configure the I2Cx interrupt priority
//            (+++) Enable the NVIC I2C IRQ Channel
//        (##) DMA Configuration if you need to use DMA process
//            (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
//            (+++) Enable the DMAx interface clock using
//            (+++) Configure the DMA handle parameters
//            (+++) Configure the DMA Tx or Rx channel
//            (+++) Associate the initialized DMA handle to the hi2c DMA Tx or Rx handle
//            (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on 
//                  the DMA Tx or Rx channel

}
static I2C_HandleTypeDef hi2c1;
static I2C_HandleTypeDef hi2c2;

int I2C_init(I2C_TypeDef *I2Cx) {

    I2C_HandleTypeDef *hi2c;
    if (I2Cx == I2C1) {
        hi2c = &hi2c1;
        hi2c->Instance = I2C1;
    } else {
        return -1; // Not implemented
    }


    hi2c->Init.ClockSpeed = 400000;
		hi2c->Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c->Init.OwnAddress1 = 0x16;
    hi2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c->Init.OwnAddress2 = 0xFE;
    hi2c->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c->Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;


    if (HAL_I2C_Init(hi2c) != HAL_OK)
        return -1;

    return 0;    

}
Exemplo n.º 25
0
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(hi2c->Instance==I2C1)
  {
  /* USER CODE BEGIN I2C1_MspInit 0 */

  /* USER CODE END I2C1_MspInit 0 */
  
    /**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_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C1_CLK_ENABLE();

    /* Peripheral DMA init*/
  
    hdma_i2c1_rx.Instance = DMA1_Stream5;
    hdma_i2c1_rx.Init.Channel = DMA_CHANNEL_1;
    hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c1_rx.Init.Mode = DMA_NORMAL;
    hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    hdma_i2c1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    HAL_DMA_Init(&hdma_i2c1_rx);

    __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx);

    hdma_i2c1_tx.Instance = DMA1_Stream6;
    hdma_i2c1_tx.Init.Channel = DMA_CHANNEL_1;
    hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c1_tx.Init.Mode = DMA_NORMAL;
    hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW;
    hdma_i2c1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    HAL_DMA_Init(&hdma_i2c1_tx);

    __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c1_tx);

  /* USER CODE BEGIN I2C1_MspInit 1 */

  /* USER CODE END I2C1_MspInit 1 */
  }
  else if(hi2c->Instance==I2C3)
  {
  /* USER CODE BEGIN I2C3_MspInit 0 */

  /* USER CODE END I2C3_MspInit 0 */
  
    /**I2C3 GPIO Configuration    
    PC9     ------> I2C3_SDA
    PA8     ------> I2C3_SCL 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Peripheral clock enable */
    __I2C3_CLK_ENABLE();

    /* Peripheral DMA init*/
  
    hdma_i2c3_tx.Instance = DMA1_Stream4;
    hdma_i2c3_tx.Init.Channel = DMA_CHANNEL_3;
    hdma_i2c3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_i2c3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c3_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_i2c3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c3_tx.Init.Mode = DMA_NORMAL;
    hdma_i2c3_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
    hdma_i2c3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    HAL_DMA_Init(&hdma_i2c3_tx);

    __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c3_tx);

    hdma_i2c3_rx.Instance = DMA1_Stream2;
    hdma_i2c3_rx.Init.Channel = DMA_CHANNEL_3;
    hdma_i2c3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_i2c3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_i2c3_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_i2c3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_i2c3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_i2c3_rx.Init.Mode = DMA_NORMAL;
    hdma_i2c3_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
    hdma_i2c3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    HAL_DMA_Init(&hdma_i2c3_rx);

    __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c3_rx);

  /* USER CODE BEGIN I2C3_MspInit 1 */

  /* USER CODE END I2C3_MspInit 1 */
  }
}
Exemplo n.º 26
0
void cHAL::Init(void) {

	__HAL_RCC_GPIOA_CLK_ENABLE();
	__HAL_RCC_GPIOB_CLK_ENABLE();
	__HAL_RCC_GPIOC_CLK_ENABLE();
	__HAL_RCC_CAN2_CLK_ENABLE();
	__HAL_RCC_CAN1_CLK_ENABLE();
	__HAL_RCC_USART3_CLK_ENABLE();
	__HAL_RCC_UART4_CLK_ENABLE();

	GPIO_InitTypeDef gi;
	HAL_StatusTypeDef status;

	//Enable UART
	gi.Pin = GPIO_PIN_10 | GPIO_PIN_11; //C10=TX, C11=RX
	gi.Mode = GPIO_MODE_AF_PP;
	gi.Pull = GPIO_PULLUP;
	gi.Speed = GPIO_SPEED_LOW;
	gi.Alternate = GPIO_AF7_USART3;
	HAL_GPIO_Init(GPIOC, &gi);
	InitAndTestUSART();

	//Onboard LEDs
	gi.Mode = GPIO_MODE_OUTPUT_PP;
	gi.Alternate = 0;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_LOW;
	gi.Pin = GPIO_PIN_7;
	HAL_GPIO_Init(GPIOB, &gi);
	LOGI(BSP::SUCCESSFUL_STRING, "GPIO for LED");

	if(InitDWTCounter())
	{
		LOGI(BSP::SUCCESSFUL_STRING, "DWTCounter");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "DWTCounter");
	}

	//MP3-Player
	gi.Pin = GPIO_PIN_0 | GPIO_PIN_1; //A0=USART4_TX, A1=USART4_RX, Kerbe nach oben; ansicht von Pinseite, rechts von oben
	//VCC, RX, TX, DACR, DACL, SPK1, GND, SPK2
	//Also: PA0 --> RX
	gi.Mode = GPIO_MODE_AF_PP;
	gi.Pull = GPIO_PULLUP;
	gi.Speed = GPIO_SPEED_LOW;
	gi.Alternate = GPIO_AF8_UART4;
	HAL_GPIO_Init(GPIOA, &gi);
	BELL.Instance = UART4;
	BELL.Init.BaudRate = 9600;
	BELL.Init.WordLength = UART_WORDLENGTH_8B;
	BELL.Init.StopBits = UART_STOPBITS_1;
	BELL.Init.Parity = UART_PARITY_NONE;
	BELL.Init.Mode = UART_MODE_TX_RX;
	BELL.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	BELL.Init.OverSampling = UART_OVERSAMPLING_16;
	HAL_UART_Init(&BSP::BELL);
	LOGI(SUCCESSFUL_STRING, "UART4 for MP3-Module");

	__I2C1_CLK_ENABLE();
	__I2C2_CLK_ENABLE();
	/*
	 PB08     ------> I2C1_SCL
	 PB09     ------> I2C1_SDA
	 */
	gi.Pin = GPIO_PIN_8 | GPIO_PIN_9;
	gi.Mode = GPIO_MODE_AF_OD;
	gi.Pull = GPIO_PULLUP;
	gi.Speed = GPIO_SPEED_MEDIUM;
	gi.Alternate = GPIO_AF4_I2C1;
	HAL_GPIO_Init(GPIOB, &gi);
	/*
	 PB10     ------> I2C2_SCL
	 PB11     ------> I2C2_SDA
	 */
	gi.Pin = GPIO_PIN_10 | GPIO_PIN_11;
	gi.Mode = GPIO_MODE_AF_OD;
	gi.Pull = GPIO_PULLUP;
	gi.Speed = GPIO_SPEED_MEDIUM;
	gi.Alternate = GPIO_AF4_I2C2;
	HAL_GPIO_Init(GPIOB, &gi);

	i2cbus[0].Instance = I2C1;
	i2cbus[0].Init.ClockSpeed = 100000;
	i2cbus[0].Init.DutyCycle = I2C_DUTYCYCLE_2;
	i2cbus[0].Init.OwnAddress1 = 0;
	i2cbus[0].Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	i2cbus[0].Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
	i2cbus[0].Init.OwnAddress2 = 0;
	i2cbus[0].Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
	i2cbus[0].Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
	HAL_I2C_Init(&i2cbus[0]);
	LOGI("I2C1 configured for onboard digital io");

	i2cbus[1].Instance = I2C2;
	i2cbus[1].Init.ClockSpeed = 100000;
	i2cbus[1].Init.DutyCycle = I2C_DUTYCYCLE_2;
	i2cbus[1].Init.OwnAddress1 = 0;
	i2cbus[1].Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
	i2cbus[1].Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
	i2cbus[1].Init.OwnAddress2 = 0;
	i2cbus[1].Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
	i2cbus[1].Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
	HAL_I2C_Init(&BSP::i2c2);
	LOGI(BSP::SUCCESSFUL_STRING, "I2C2 for 1wire and external");


	if(drivers::cPCA9685::SoftwareReset(&BSP::i2c1))
	{
		LOGI(SUCCESSFUL_STRING, "i2c1 reset");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "i2c1 reset");
	}


	if(pca9685_U7.Setup())//next to CPU, all A-Pins @ GND b01
	{
		LOGI(SUCCESSFUL_STRING, "pca9685_U7");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "pca9685_U7");
	}
	if(pca9685_U9.Setup())
	{
		LOGI(SUCCESSFUL_STRING, "pca9685_U9");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "pca9685_U9");
	}

	//Interrupt-Pins for PCA9555
	gi.Pin = GPIO_PIN_0|GPIO_PIN_1;
	gi.Mode = GPIO_MODE_INPUT;
	gi.Pull = GPIO_PULLUP;
	HAL_GPIO_Init(GPIOC, &gi);

	if(pca9555_U19.Setup())
	{
		LOGI(BSP::SUCCESSFUL_STRING, "pca9555_U19");
	}
	else
	{
		LOGE(BSP::NOT_SUCCESSFUL_STRING, "pca9555_U19");
	}
	if(pca9555_U18.Setup())
	{
		LOGI(BSP::SUCCESSFUL_STRING, "pca9555_U18");
	}
	else
	{
		LOGE(BSP::NOT_SUCCESSFUL_STRING, "pca9555_U18");
	}

	uint16_t tmp = pca9555_U18.GetInput();
	inputState[WORD_I2C] = (inputState[WORD_I2C] & 0xFFFF0000) + tmp;
	tmp = pca9555_U19.GetInput();
	inputState[WORD_I2C] = (inputState[WORD_I2C] & 0x0000FFFF) + (tmp << 16);

	rcSwitch.enableReceive();






	if(drivers::cPCA9685::SoftwareReset(&BSP::i2c2))
	{
		LOGI(SUCCESSFUL_STRING, "i2c2 reset");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "i2c2 reset");
	}

	SearchI2C("I2C2", &i2c2);
	Init1wire();

	//Enable Rotary Encoder Switch Input
	gi.Mode = GPIO_MODE_INPUT;
	gi.Alternate = 0;
	gi.Pull = GPIO_PULLUP;
	gi.Speed = GPIO_SPEED_LOW;
	gi.Pin = GPIO_PIN_13;
	HAL_GPIO_Init(GPIOC, &gi);
	LOGI(SUCCESSFUL_STRING, "GPIO for Rotary Encoder");
#ifdef DCF77
	//DCF77
	gi.Mode = GPIO_MODE_INPUT;
	gi.Alternate=0;
	gi.Pull=GPIO_PULLUP;
	gi.Speed=GPIO_SPEED_FREQ_LOW;
	gi.Pin=DCF77_PIN;
	HAL_GPIO_Init(DCF77_PORT, &gi);
#endif


#endif
	//=====PWM-Timers
	//===============

	//Overall GPIO-Settings
	//All gpios enabled!
	gi.Mode = GPIO_MODE_AF_PP;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_HIGH;

	//Overall base timer settings
	TIM_HandleTypeDef TimHandle;
	TimHandle.Init.Prescaler = 0;
	TimHandle.Init.Period = UINT16_MAX;
	TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

	//Overall OC-settings
	TIM_OC_InitTypeDef sConfig;
	sConfig.OCMode = TIM_OCMODE_PWM1;
	sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
	sConfig.OCFastMode = TIM_OCFAST_DISABLE;
	sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
	sConfig.Pulse = 0;

	//===Slow Timers (84MHz) TIM4 and TIM12
	//TIM1, TIM8		SystemCoreClock/1
	//Others			SystemCoreClock/2
	//Prescaler (uint16_t) ((SystemCoreClock / 1 bzw 2) / TimerTickFrq) - 1;
	//Einstellungen führen zu PWM-Frequenz von 116,5Hz (rechnerisch ermittelt, per LogicAnalyzer bestätigt)



#ifdef SENSACTHS07
	TimHandle.Init.Prescaler = 10; //for 84MHz-Timers
	__TIM12_CLK_ENABLE()
	;
	gi.Pin = GPIO_PIN_14 | GPIO_PIN_15;
	gi.Alternate = GPIO_AF9_TIM12;
	HAL_GPIO_Init(GPIOB, &gi);
	TimHandle.Instance = TIM12;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); //PB14 O1.1
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); ///PB15 O1.2
	CLEAR_BIT(TIM12->CCMR1, TIM_CCMR1_OC1PE);
	CLEAR_BIT(TIM12->CCMR1, TIM_CCMR1_OC2PE);
	LOGI(BSP::SUCCESSFUL_STRING, "TIM12");

	//===Fast Timers (168MHz) TIM1 and TIM8
	TimHandle.Init.Prescaler = 20;

	__TIM8_CLK_ENABLE()
	;
	gi.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_9;
	gi.Alternate = GPIO_AF3_TIM8;
	HAL_GPIO_Init(GPIOC, &gi);
	TimHandle.Instance = TIM8;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); //C6 O1.3
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); //C7 O1.4
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4); //C9 O1.5
	CLEAR_BIT(TIM8->CCMR1, TIM_CCMR1_OC1PE);
	CLEAR_BIT(TIM8->CCMR1, TIM_CCMR1_OC2PE);
	CLEAR_BIT(TIM8->CCMR2, TIM_CCMR2_OC4PE);
	LOGI(SUCCESSFUL_STRING, "TIM8");

	__TIM1_CLK_ENABLE()
	;
	gi.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
	gi.Alternate = GPIO_AF1_TIM1;
	HAL_GPIO_Init(GPIOA, &gi);
	TimHandle.Instance = TIM1;
	TIM_ClockConfigTypeDef sClockSourceConfig;
	TIM_MasterConfigTypeDef sMasterConfig;
	TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;


	HAL_TIM_Base_Init(&TimHandle);
	sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
	HAL_TIM_ConfigClockSource(&TimHandle, &sClockSourceConfig);
	HAL_TIM_PWM_Init(&TimHandle);

	sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
	sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
	HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &sMasterConfig);

	sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
	sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
	sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
	sBreakDeadTimeConfig.DeadTime = 0;
	sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
	sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
	sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
	HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sBreakDeadTimeConfig);



	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); //A8 O1.6
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); //A9 O1.7
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3); //A10 O1.8
	CLEAR_BIT(TIM1->CCMR1, TIM_CCMR1_OC1PE);
	CLEAR_BIT(TIM1->CCMR1, TIM_CCMR1_OC2PE);
	CLEAR_BIT(TIM1->CCMR2, TIM_CCMR2_OC3PE);
	LOGI(SUCCESSFUL_STRING, "TIM1");


#endif
#ifdef SENSACTHS04
	TimHandle.Init.Prescaler = 10; //for 84MHz-Timers

	__TIM4_CLK_ENABLE();
	gi.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9;
	gi.Alternate = GPIO_AF2_TIM4;
	HAL_GPIO_Init(GPIOB, &gi);
	TimHandle.Instance = TIM4;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); //PB6 o2.1
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); //PB7 o2.2
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3); //PB8 o1.1
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4); //PB9 o1.2
	TIM4->CCMR1 &= ~TIM_CCMR1_OC1PE;
	TIM4->CCMR1 &= ~TIM_CCMR1_OC2PE;
	TIM4->CCMR2 &= ~TIM_CCMR2_OC3PE;
	TIM4->CCMR2 &= ~TIM_CCMR2_OC4PE;
	LOGI(SUCCESSFUL_STRING, "TIM4");


	__TIM12_CLK_ENABLE();

	gi.Pin = GPIO_PIN_14 | GPIO_PIN_15;
	gi.Alternate = GPIO_AF9_TIM12;
	HAL_GPIO_Init(GPIOB, &gi);
	TimHandle.Instance = TIM12;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1); //PB14 O7.2
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2); ///PB15 O4.1
	TIM12->CCMR1 &= ~TIM_CCMR1_OC1PE;
	TIM12->CCMR1 &= ~TIM_CCMR1_OC2PE;
	LOGI(SUCCESSFUL_STRING, "TIM12");

	//===Fast Timers (168MHz) TIM1 and TIM8
	TimHandle.Init.Prescaler = 20;

	__TIM1_CLK_ENABLE();

	gi.Pin = GPIO_PIN_8;
	gi.Alternate = GPIO_AF1_TIM1;
	HAL_GPIO_Init(GPIOA, &gi);
	TimHandle.Instance = TIM1;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);

	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3);

	TIM1->CCMR1 &= ~TIM_CCMR1_OC1PE;

	TIM1->CCMR2 &= ~TIM_CCMR2_OC3PE;
	LOGI(SUCCESSFUL_STRING, "TIM1");

	__TIM8_CLK_ENABLE();

	gi.Pin = GPIO_PIN_6 | GPIO_PIN_7  | GPIO_PIN_9;
	gi.Alternate = GPIO_AF3_TIM8;
	HAL_GPIO_Init(GPIOC, &gi);
	TimHandle.Instance = TIM8;
	HAL_TIM_PWM_Init(&TimHandle);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
	HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_4);
	TIM8->CCMR1 &= ~TIM_CCMR1_OC1PE;
	TIM8->CCMR1 &= ~TIM_CCMR1_OC2PE;
	TIM8->CCMR2 &= ~TIM_CCMR2_OC4PE;
	LOGI(SUCCESSFUL_STRING, "TIM8");
#endif
	//===SPI for Relais
	//PA15=LATCH,PB3=CLK PB4=MISO, PB5=MOSI
	//DRV8066 DIN=2, CLK(low@inak)=3, Latch@pos edge=4
	__SPI3_CLK_ENABLE()
	;
	gi.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
	gi.Mode = GPIO_MODE_AF_PP;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_HIGH;
	gi.Alternate = GPIO_AF6_SPI3;
	HAL_GPIO_Init(GPIOB, &gi);

#ifdef SENSACTHS07
	BSP::spi.Init.DataSize = SPI_DATASIZE_8BIT;
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_SET);
	gi.Pin = GPIO_PIN_14;
	gi.Mode = GPIO_MODE_OUTPUT_PP;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_MEDIUM;
	HAL_GPIO_Init(GPIOC, &gi);
#endif
#ifdef SENSACTHS04
	BSP::spi.Init.DataSize = SPI_DATASIZE_16BIT;
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
	gi.Pin = GPIO_PIN_15;
	gi.Mode = GPIO_MODE_OUTPUT_PP;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_MEDIUM;
	HAL_GPIO_Init(GPIOA, &gi);
#endif



	BSP::spi.Instance = SPI3;
	BSP::spi.Init.Mode = SPI_MODE_MASTER;
	BSP::spi.Init.Direction = SPI_DIRECTION_2LINES;

	BSP::spi.Init.CLKPolarity = SPI_POLARITY_LOW;
	BSP::spi.Init.CLKPhase = SPI_PHASE_1EDGE;
	BSP::spi.Init.NSS = SPI_NSS_SOFT;
	BSP::spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
	BSP::spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
	BSP::spi.Init.TIMode = SPI_TIMODE_DISABLED;
	BSP::spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
	BSP::spi.Init.CRCPolynomial = 1;
	status = HAL_SPI_Init(&BSP::spi);
	if (status != HAL_OK) {
		LOGE("Unable to configure SPI for Relays");
		while(1) {};
	}
	uint8_t tx[] = {0, 0, 0};
#ifdef SENSACTHS07
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_RESET);
	HAL_SPI_Transmit(&BSP::spi, tx, 3, 100);
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_SET);
	LOGI(SUCCESSFUL_STRING, "SPI for DRV8860");
#endif
#ifdef SENSACTHS04
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
	if (HAL_SPI_Transmit(&BSP::spi, tx, 1, 100) == HAL_OK) {
		LOGI(SUCCESSFUL_STRING, "SPI for DRV8860");
	}
	else
	{
		LOGE(NOT_SUCCESSFUL_STRING, "SPI for DRV8860");
	}
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
#endif





	/**CAN2 GPIO Configuration
	 PB12     ------> CAN2_RX
	 PB13     ------> CAN2_TX
	 */
	gi.Pin = GPIO_PIN_12 | GPIO_PIN_13;
	gi.Mode = GPIO_MODE_AF_PP;
	gi.Pull = GPIO_NOPULL;
	gi.Speed = GPIO_SPEED_LOW;
	gi.Alternate = GPIO_AF9_CAN2;
	HAL_GPIO_Init(GPIOB, &gi);
	InitCAN();
	return;
}