cancel
Showing results for 
Search instead for 
Did you mean: 

TCA9539 use as a GPIO Port Expander with STM32F091RCT6

Moza
Associate II

Hello. 
I would like touse TCA9539 use as a GPIO Port Expander with STM32F091RCT6 (IO-EXPANDER-EVM Evaluation board | TI.com). I have enabled I2C2 in STM and connect as follows 
STM32F091RCT6            

SCL------------------------>I2C2_SCL PB10

SDA------------------------>I2C2_SDA PB11

VCC------------------------>5V

GND------------------------>GND


And below the generated code 

 

 

#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
#include "stm32f0xx_hal.h"

#define TCA9539_ADDR 0x74 << 1

#define INPUT_PORT0_REG    0x00
#define OUTPUT_PORT0_REG   0x02
#define CONFIG_PORT0_REG   0x06
#define INPUT_PORT1_REG    0x01
#define OUTPUT_PORT1_REG   0x03
#define CONFIG_PORT1_REG   0x07


void SystemClock_Config(void);


void TCA9539_Init(void) {
    uint8_t data[3];

    data[0] = CONFIG_PORT0_REG;
    data[1] = 0x00; // 0x00 for output
    HAL_I2C_Master_Transmit(&hi2c2, TCA9539_ADDR, data, 2, HAL_MAX_DELAY);

    data[0] = CONFIG_PORT1_REG;
    data[1] = 0x00; // 0x00 for output
    HAL_I2C_Master_Transmit(&hi2c2, TCA9539_ADDR, data, 2, HAL_MAX_DELAY);
}



void Control_LEDs(uint8_t port0_state) {
    uint8_t data[2];

    data[0] = OUTPUT_PORT0_REG;
    data[1] = port0_state; // State of each pin: 0 for off, 1 for on
    HAL_I2C_Master_Transmit(&hi2c2, TCA9539_ADDR, data, 2, HAL_MAX_DELAY);
}



int main(void)
{

  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_I2C2_Init();
  MX_USART1_UART_Init();/

  TCA9539_Init();


  while (1)
  {
  Control_LEDs(0xFF);
  HAL_Delay(1000);

  Control_LEDs(0x00);
  HAL_Delay(1000);
  }
  /
}


void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

void Error_Handler(void)

  __disable_irq();
  while (1)
  {
  }

}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */

 



I have tried to turn on and off P00 ~ P07. But its not working. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

Welcome @Moza, to the community!

If you do not follow our recommendations for posting threads in this community, especially for releasing the source code, it will be more difficult to read the code.

Without having read this code: if you have the TCA9539 on the place of U2 of the IO-Expander board, you should also specify its address information. By default, A1 (pin 2) and A0 (pin 21) are connected via pull-up to VCC, which sets the address of the TCA9539 to 0x77. If signal ADDR_16 is set to 0, the address is 0x76. You would therefore have to change your programme accordingly.

Good luck!
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

4 REPLIES 4
Peter BENSCH
ST Employee

Welcome @Moza, to the community!

If you do not follow our recommendations for posting threads in this community, especially for releasing the source code, it will be more difficult to read the code.

Without having read this code: if you have the TCA9539 on the place of U2 of the IO-Expander board, you should also specify its address information. By default, A1 (pin 2) and A0 (pin 21) are connected via pull-up to VCC, which sets the address of the TCA9539 to 0x77. If signal ADDR_16 is set to 0, the address is 0x76. You would therefore have to change your programme accordingly.

Good luck!
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Andrew Neil
Evangelist III

@Moza wrote:

SCL------------------------>I2C2_SCL PB10

SDA------------------------>I2C2_SDA PB11

VCC------------------------>5V

GND------------------------>GND


What about the pullups?

 


@Moza wrote:

I have tried to turn on and off P00 ~ P07. But its not working. 


So what debugging have you done to find what's going wrong?

Have you used an oscilloscope to check what's happening on the I2C lines?

Is the Slave ACKing its address?

 

HAL_I2C_Master_Transmit( &hi2c2, TCA9539_ADDR, data, 2, HAL_MAX_DELAY );

All HAL functions return a status code - you should check it to see if it's reporting an error ...

 

 

I do not have any hardware to test the pin signal. But after debug in console I did not see any status code


@Moza wrote:

after debug in console I did not see any status code


That's because your code simply ignores it.

If you want to see it in a console, you will have to add code to send it to the console.