2019-09-28 06:26 AM
I am trying to do I2C communication between STM32L476RC and Fuel gauge IC(BQ40Z50R2),for that i am using following components:
1) STM32L476RC Controller
2) BQ40Z50R2 Fuel gauge IC
3) STM32 CubeMx software
4) Keil software
The datasheet of fuel gauge is http://www.ti.com/lit/ds/symlink/bq40z50-r2.pdf and datasheet of STM32L476RC is https://www.st.com/resource/en/datasheet/stm32l476rc.pdf.
I tried to write a code to take data(voltage,current and temperature) from fuel gauge where 3.7v Li-Ion battery is connected.To writting a code of I2C communication with Stm32l476 is getting little bit tough because nowhere given a single I2C program with this STM32L476 controller.
I initialise I2C pins in CubeMx and code is generated in keil. Then in while loop i used HAL_I2C_Master_Transmit and HAL_I2C_Master_Receive but not getting output in watch window ,also i checked it on oscilloscope but scl and sdl line shows just a dc horizontal line. Also I used HAL_I2C_Mem_Read,but not working.
I edited the code only in while ,rest of the code is generated by CubeMx.I am facing following problems: 1) My code is not giving any output 2) I am checking output on watch window in keil,is it right or wrong?
2019-09-28 06:39 AM
What pins specifically are you using?
I2C examples like
STM32Cube_FW_L4_V1.14.0\Drivers\BSP\STM32L476G-Discovery\stm32l476g_discovery.c
Or ten or so around
STM32Cube_FW_L4_V1.14.0\Projects\STM32L476G-EVAL\Examples\I2C\I2C_EEPROM
2019-09-28 10:26 AM
I do beleive that if there was a utility function to get started with SW emulated bitbang Master I2C would reduce drastically all these posts about I2C or SPI maydays.
My bitbang I2C shared in this forum works like a charm and evolved since its inception in 1999... Never failed, works through hot plug unplug with most non-clock stretching slave spectrum.
2019-09-29 12:30 AM
Thanks for your response sir, I am using I2C pins that is SDA and SCL.
2019-09-29 12:35 AM
Thanks for your response sir,but sir i am very new to this STM32 environment.So i dont know what should i do with this bitbang I2C that you shared.Can you please elaborate it clearly.Sorry sir but please.
Thanks
Mohan
2019-09-29 04:55 AM
No, what pins on the chip, the number or GPIO bank.
Which I2C peripheral.
You fail to provide a sufficient level of detail, or coding, to understand what you are doing wrong.
2019-09-29 05:45 AM
Sorry sir,I will provide code.
#include "main.h"
#include "stm32l4xx_hal.h"
#include "stdio.h"
/* Private variables ---------------------------------------------------------*/
SMBUS_HandleTypeDef hsmbus1;
I2C_HandleTypeDef hi2c; //Declare a I2C_HandleTypeDef handle structure
/* USER CODE BEGIN PV */
unsigned char buffer[5];
unsigned char buffer1[5];
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_SMBUS_Init(void);
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_SMBUS_Init();
while (1)
{
/* USER CODE END WHILE */
buffer[0] = 0x09; // Temperature
HAL_I2C_Master_Transmit(&hi2c, 0x16 << 1, buffer, 1, 100);
HAL_Delay(50);
HAL_I2C_Master_Receive(&hi2c, 0x16 << 1, buffer1, 1, 100);
printf("output= %d", buffer1[0]);
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
/**Initializes the CPU, AHB and APB busses clocks
*/
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();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
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_I2C1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
/**Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_SMBUS_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 */
hsmbus1.Instance = I2C1;
hsmbus1.Init.Timing = 0x00000E14;
hsmbus1.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE;
hsmbus1.Init.OwnAddress1 = 2;
hsmbus1.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT;
hsmbus1.Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLE;
hsmbus1.Init.OwnAddress2 = 0;
hsmbus1.Init.OwnAddress2Masks = SMBUS_OA2_NOMASK;
hsmbus1.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLE;
hsmbus1.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLE;
hsmbus1.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLE;
hsmbus1.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE;
hsmbus1.Init.SMBusTimeout = 0x00008030;
if (HAL_SMBUS_Init(&hsmbus1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(char *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2019-09-30 01:00 AM
Can you tell how to use bit-banging for STM32L476RC controller?
Thanks
Mohan.
2019-09-30 01:34 AM
Hello Clive Two.zero sir, I am using PB6 as SCL and PB7 as SDL.