cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F769i-discovery i2C BSP code question

benyBoy
Associate II
Posted on April 26, 2017 at 17:20

Hi , I'm trying to use external I2C devices in the Audio_playback_and_record project for STM32F769i-discovery.

I think the code is using the BSP to configure the GPIO's for I2C used for internal devices ( audio codec, screen etc )

I have looked at the I2C example but it sets up the GPIO's directly in main for external I2C.

I have tried to merge the I2C example to the Audio_playback_and_record project but I get nothing on the scope from

the I2C bus. The I2C example works.

I am defining the I2C_HandleTypeDef like this;

I2C_HandleTypeDef I2cHandle; 

i'm initialising with;

HAL_I2C_Init(&I2cHandle);

then i'm trying to write or check external I2C devices with line like;

error = HAL_I2C_Master_Transmit (&I2cHandle, devaddress, nulldata, i2cpacketsize, timeout);

uint8_t i2ctest = HAL_I2C_IsDeviceReady(&I2cHandle,devaddress,4,100);  // check device 0x38

i'm asuming the BUS opperation sets up the external gpio's for I2C if I2C_HandleTypeDef is anything but &hI2cAudioHandler ?

I'm not really understanding the need for I2C_HandleTypeDef or how it works ? is it so we can define and setup more than one I2C bus? if so , what is the best way to do this? Or can I ignore the &hI2cAudioHandler and just manualy define another I2C bus ?

I'd like to use external and internal and external I2C devices on the STM32F769i-discovery. So I dont think I can simply just merge the I2C example code and expect it to work !

any ideas are welcome , i'm new to STM32F7 although I just got basic external GPIO's working on the board for my project.

/*******************************************************************************

                            BUS OPERATIONS

*******************************************************************************/

/******************************* I2C Routines *********************************/

/**

  * @brief  Initializes I2C MSP.

  * @param  i2c_handler : I2C handler

  * @retval None

  */

static void I2Cx_MspInit(I2C_HandleTypeDef *i2c_handler)

{

  GPIO_InitTypeDef  gpio_init_structure;

  if (i2c_handler == (I2C_HandleTypeDef*)(&hI2cAudioHandler))

  {

  /*** Configure the GPIOs ***/

  /* Enable GPIO clock */

  DISCOVERY_AUDIO_I2Cx_SCL_GPIO_CLK_ENABLE();

  DISCOVERY_AUDIO_I2Cx_SDA_GPIO_CLK_ENABLE();

  /* Configure I2C Tx as alternate function */

  gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SCL_PIN;

  gpio_init_structure.Mode = GPIO_MODE_AF_OD;

  gpio_init_structure.Pull = GPIO_NOPULL;

  gpio_init_structure.Speed = GPIO_SPEED_FAST;

  gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SCL_AF;

  HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SCL_GPIO_PORT, &gpio_init_structure);

  /* Configure I2C Rx as alternate function */

  gpio_init_structure.Pin = DISCOVERY_AUDIO_I2Cx_SDA_PIN;

  gpio_init_structure.Alternate = DISCOVERY_AUDIO_I2Cx_SDA_AF;

  HAL_GPIO_Init(DISCOVERY_AUDIO_I2Cx_SDA_GPIO_PORT, &gpio_init_structure);

  /*** Configure the I2C peripheral ***/

  /* Enable I2C clock */

  DISCOVERY_AUDIO_I2Cx_CLK_ENABLE();

  /* Force the I2C peripheral clock reset */

  DISCOVERY_AUDIO_I2Cx_FORCE_RESET();

  /* Release the I2C peripheral clock reset */

  DISCOVERY_AUDIO_I2Cx_RELEASE_RESET();

  /* Enable and set I2C1 Interrupt to a lower priority */

  HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_EV_IRQn, 0x0F, 0);

  HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_EV_IRQn);

  /* Enable and set I2C1 Interrupt to a lower priority */

  HAL_NVIC_SetPriority(DISCOVERY_AUDIO_I2Cx_ER_IRQn, 0x0F, 0);

  HAL_NVIC_EnableIRQ(DISCOVERY_AUDIO_I2Cx_ER_IRQn);    

    

  }

  else

  {

  /*** Configure the GPIOs ***/

  /* Enable GPIO clock */

  DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();

  /* Configure I2C Tx as alternate function */

  gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SCL_PIN;

  gpio_init_structure.Mode = GPIO_MODE_AF_OD;

  gpio_init_structure.Pull = GPIO_NOPULL;

  gpio_init_structure.Speed = GPIO_SPEED_FAST;

  gpio_init_structure.Alternate = DISCOVERY_EXT_I2Cx_SCL_SDA_AF;

  HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);

  /* Configure I2C Rx as alternate function */

  gpio_init_structure.Pin = DISCOVERY_EXT_I2Cx_SDA_PIN;

  HAL_GPIO_Init(DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT, &gpio_init_structure);

  /*** Configure the I2C peripheral ***/

  /* Enable I2C clock */

  DISCOVERY_EXT_I2Cx_CLK_ENABLE();

  /* Force the I2C peripheral clock reset */

  DISCOVERY_EXT_I2Cx_FORCE_RESET();

  /* Release the I2C peripheral clock reset */

  DISCOVERY_EXT_I2Cx_RELEASE_RESET();

  /* Enable and set I2C1 Interrupt to a lower priority */

  HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_EV_IRQn, 0x0F, 0);

  HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_EV_IRQn);

  /* Enable and set I2C1 Interrupt to a lower priority */

  HAL_NVIC_SetPriority(DISCOVERY_EXT_I2Cx_ER_IRQn, 0x0F, 0);

  HAL_NVIC_EnableIRQ(DISCOVERY_EXT_I2Cx_ER_IRQn);

  }

}

#bus #i2c #external #audio-playback #audio-recording
2 REPLIES 2
john doe
Lead
Posted on April 27, 2017 at 01:37

the wm8994 is on i2c4 on an stm32f769i-discovery board. look in stm32f769i_discovery.h, stm32f769i_discovery_audio.h and .c, and in wm8994.h and .c

Posted on April 27, 2017 at 05:05

 ,

 ,

Hi John, thanks for your reply,

Yes it looks like there are 2 I2C Ports defined. I2C4 with internal discovery devices and I2C1 as external I2C on pins PB8 &, PB9.

it looks like stm32f769i_discovery_audio.c &, .h deal with audio over SAI port &, codec control etc over I2C4. I can't see any I2C1 initialization code there. I think its initialized in the HAL.

how do I point to I2C1 , I2C_HandleTypeDef ? what is the pointer of I2C1 Handle?

how do I initialize port I2C1? Its seams to be defined in stm32f769i-discovery.h and code in my 1st post.

// check I2C device 0x38

int devaddress = 0x38,

HAL_I2C_IsDeviceReady(&,I2cHandle**POINTER TO I2C1 HANDLE?,devaddress,retrys,timeout), ,

I should also say that PB7 &, PB8 external sda /scl arduino pins are driven high so the external bus I2C1 is already initialized?

 , * @brief User can use this section to tailor I2C1/I2C1 instance used and associated

 ,

 , * resources.

 ,

 , * Definition for I2C1 clock resources

 ,

 , */

 ,

♯ define DISCOVERY_EXT_I2Cx , , , , , , , , , , , , , , , , , , , , , , , , , , , , I2C1

 ,

♯ define DISCOVERY_EXT_I2Cx_CLK_ENABLE() , , , , , , , , , , , __HAL_RCC_I2C1_CLK_ENABLE()

 ,

♯ define DISCOVERY_DMAx_CLK_ENABLE() , , , , , , , , , , , , , , , __HAL_RCC_DMA1_CLK_ENABLE()

 ,

♯ define DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_CLK_ENABLE() , , __HAL_RCC_GPIOB_CLK_ENABLE()

♯ define DISCOVERY_EXT_I2Cx_FORCE_RESET() , , , , , , , , , , , , , , __HAL_RCC_I2C1_FORCE_RESET()

 ,

♯ define DISCOVERY_EXT_I2Cx_RELEASE_RESET() , , , , , , , , , , , , __HAL_RCC_I2C1_RELEASE_RESET()

/** @brief Definition for I2C1 Pins

 ,

 , */

 ,

♯ define DISCOVERY_EXT_I2Cx_SCL_PIN , , , , , , , , , , , , , , , , , , , , GPIO_PIN_8 /*!<, PB8 */

 ,

♯ define DISCOVERY_EXT_I2Cx_SCL_SDA_GPIO_PORT , , , , , , , , , , GPIOB

 ,

♯ define DISCOVERY_EXT_I2Cx_SCL_SDA_AF , , , , , , , , , , , , , , , , , GPIO_AF4_I2C1

 ,

♯ define DISCOVERY_EXT_I2Cx_SDA_PIN , , , , , , , , , , , , , , , , , , , , GPIO_PIN_9 /*!<, PB9 */

/** @brief Definition of I2C interrupt requests

 ,

 , */

 ,

♯ define DISCOVERY_EXT_I2Cx_EV_IRQn , , , , , , , , , , , , , , , , , , , , I2C1_EV_IRQn

 ,

♯ define DISCOVERY_EXT_I2Cx_ER_IRQn , , , , , , , , , , , , , , , , , , , , I2C1_ER_IRQn

/* I2C TIMING Register define when I2C clock source is SYSCLK */

 ,

/* I2C TIMING is calculated from APB1 source clock = 50 MHz */

 ,

/* Due to the big MOFSET capacity for adapting the camera level the rising time is very large (>,1us) */

 ,

/* 0x40912732 takes in account the big rising and aims a clock of 100khz */

 ,

♯ ifndef DISCOVERY_I2Cx_TIMING  ,

 ,

♯ define DISCOVERY_I2Cx_TIMING , , , , , , , , , , , , , , , , , , , , , ((uint32_t)0x40912732)

 ,

♯ endif /* DISCOVERY_I2Cx_TIMING */