cancel
Showing results for 
Search instead for 
Did you mean: 

SMBus communication between STM32 and BQ25700A(charger ic)

Mohan1
Associate III

Hello,

I am trying to do communication between Charger IC(BQ25700A) and STM32 controller by using I2C communication protocol.For taking the status of charging/discharging current of battery from charger ic,i have to configure registers in charger ic as shown below:

0690X00000BwUj6QAF.png

I am sending data packets as per the SMBus Write-Word protocol and receiving the data as per the SMBus Read-Word protocol. SMBus slave device with address 0b00010010 (0x12H)0690X00000BwUmUQAV.png

I have attached my c code,when i run the code getting zero value.Please give me your suggestions.

Datasheet of BQ25700A:  http://www.ti.com/lit/ds/symlink/bq25700a.pdf

#include "main.h"
 
 
 
 
I2C_HandleTypeDef hi2c1;
 
UART_HandleTypeDef huart3;
 
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART3_UART_Init(void);
 
 
int main(void)
{
 
int Buf1[4];
int Buf2[4];
int Buf3[4];
int Buf4[4];
int Buf5[4];
int Buf6[4];
int Buf7[7];
uint8_t TxData;
uint16_t RxData;
 
int i;
TxData=0x24;
 
 
 
 
  HAL_Init();
 
  /
  SystemClock_Config();
 
  
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USART3_UART_Init();
  
  for(i=0;i<5;i++)
  {
 
	  //Register:Charge Option0//
 
	  Buf1[0]=0x24; //slave address+write
	  Buf1[1]=0x12; //command byte
          Buf1[2]=0x00;  //LSB Data
	  Buf1[3]=0x0A;  //MSB Data
	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf1,4,100);
 
 
 
	  //Charge current register//
 
	  Buf2[0]=0x24;
	  Buf2[1]=0x14;
	  Buf2[2]=0x00;
	  Buf2[3]=0x02;
	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf2,4,100);
 
	  //Charge voltage register//
 
	          Buf3[0]=0x24;
	  	  Buf3[1]=0x15;
	  	  Buf3[2]=0x60;
	  	  Buf3[3]=0x10;
	  	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf3,4,100);
 
 
	  	  //Input voltage register//
 
	  	                  Buf4[0]=0x24;
	  		  	  Buf4[1]=0x3D;
	  		  	  Buf4[2]=0x00;
	  		  	  Buf4[3]=0x12;
	  		  	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf4,4,100);
 
	  		  	  //minimum system voltage//
 
	  		  	                                  Buf5[0]=0x24;
	  		  		  		  	  Buf5[1]=0x3E;
	  		  		  		  	  Buf5[2]=0x00;
	  		  		  		  	  Buf5[3]=0x0E;
	  		  		  		  	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf5,4,100);
 
	  		  	//Input current register//
 
	  		  		  		                                  Buf6[0]=0x24;
	  		  		  			  		  	  Buf6[1]=0x3F;
	  		  		  			  		  	  Buf6[2]=0x00;
	  		  		  			  		  	  Buf6[3]=0x41;
	  		  		  			  		  	  HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf6,4,100);
 
	  		  		//Read: charge/discharge current register  //
 
	  		  		  			  		     Buf7[0]=0x24;
	  		  		  			  	             Buf7[1]=0x24;
	  		  		  			                     Buf7[2]=0x25;
	  		  		  		                             Buf7[3]=0x00;
	  		  		  		                             Buf7[4]=0x07;
	  		  		  			  	         
                                                HAL_I2C_Master_Transmit(&hi2c1,0x12,Buf7,5,100);
	  		  		  			  	           
                                               HAL_I2C_Master_Receive(&hi2c1,0x12,&RxData,1,100);
 
 
  }
 
}
 
 
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
  
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}
 
 
static void MX_I2C1_Init(void)
{
 
 
  hi2c1.Instance = I2C1;
  hi2c1.Init.Timing = 0x00000E14;
  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();
  }
  
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
 
 
}
 
 
static void MX_USART3_UART_Init(void)
{
 
  
  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart3) != HAL_OK)
  {
    Error_Handler();
  }
 
 
}
 
 
static void MX_GPIO_Init(void)
{
 
  
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
}
 
 
void Error_Handler(void)
{
  
}
 
#ifdef  USE_FULL_ASSERT
 
void assert_failed(char *file, uint32_t line)
{ 
  
}
#endif 
 

0 REPLIES 0