diff --git a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/Make.defs b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/Make.defs index 3a39020afa..e0a30c4fd1 100644 --- a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/Make.defs +++ b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/Make.defs @@ -43,6 +43,10 @@ ifeq ($(CONFIG_LCD_SSD1306),y) CSRCS += stm32_lcd_ssd1306.c endif +ifeq ($(CONFIG_SENSORS_INA226),y) +CSRCS += stm32_ina226.c +endif + DEPPATH += --dep-path board VPATH += :board CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board) diff --git a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_bringup.c b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_bringup.c index 9c7a0777b8..a0b75f2310 100644 --- a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_bringup.c +++ b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_bringup.c @@ -89,6 +89,18 @@ int stm32_bringup(void) board_lcd_initialize(); #endif +#ifdef CONFIG_SENSORS_INA226 + /* Initialize and register the INA226 */ + + ret = stm32_ina226_initialization(); + if (ret != OK) + { + syslog(LOG_ERR, + "ERROR: Failed to register the INA226 drivers: %d\n", ret); + return ret; + } +#endif + UNUSED(ret); return OK; } diff --git a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_ina226.c b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_ina226.c new file mode 100644 index 0000000000..deba4ff286 --- /dev/null +++ b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_ina226.c @@ -0,0 +1,121 @@ +/**************************************************************************** + * boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32_ina226.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include + +#include "stm32_i2c.h" +#include "stm32g071b-disco.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_STM32F0L0G0_I2C1 +# error I2C1 must be enabled! +#endif + +#define INA226_1_I2C_ADDR 0x40 /* VBUS */ +#define INA226_2_I2C_ADDR 0x41 /* CC1 */ +#define INA226_3_I2C_ADDR 0x42 /* CC2 */ + +#define INA226_I2C_BUS 1 +#define INA226_SHUNT_VAL 15000 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_ina226_initialization + * + * Description: + * Initialize and configure the INA226 + * + ****************************************************************************/ + +int stm32_ina226_initialization(void) +{ + struct i2c_master_s *i2c = NULL; + static bool initialized = false; + int ret = OK; + + /* Have we already initialized? */ + + if (!initialized) + { + /* No.. Get the I2C bus driver */ + + i2c = stm32_i2cbus_initialize(INA226_I2C_BUS); + if (!i2c) + { + serr("ERROR: Failed to initialize I2C%d\n", INA226_I2C_BUS); + goto errout; + } + + /* Now bind the I2C interface to the INA226 drivers */ + + ret = ina226_register("/dev/ina226_1", + i2c, INA226_1_I2C_ADDR, + INA226_SHUNT_VAL, 0); + if (ret < 0) + { + serr("ERROR: Failed to bind I2C%d to the INA226_1 driver\n", + INA226_I2C_BUS); + goto errout; + } + + ret = ina226_register("/dev/ina226_2", + i2c, INA226_2_I2C_ADDR, + INA226_SHUNT_VAL, 0); + if (ret < 0) + { + serr("ERROR: Failed to bind I2C%d to the INA226_2 driver\n", + INA226_I2C_BUS); + goto errout; + } + + ret = ina226_register("/dev/ina226_3", + i2c, INA226_3_I2C_ADDR, + INA226_SHUNT_VAL, 0); + if (ret < 0) + { + serr("ERROR: Failed to bind I2C%d to the INA226_3 driver\n", + INA226_I2C_BUS); + goto errout; + } + + /* Now we are initialized */ + + initialized = true; + } + +errout: + return ret; +} diff --git a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32g071b-disco.h b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32g071b-disco.h index 5c0c2e1306..a19f690100 100644 --- a/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32g071b-disco.h +++ b/boards/arm/stm32f0l0g0/stm32g071b-disco/src/stm32g071b-disco.h @@ -114,4 +114,16 @@ void stm32_spidev_initialize(void); int stm32_djoy_initialization(void); #endif +/**************************************************************************** + * Name: stm32_ina226_initialization + * + * Description: + * Initialize and configure the INA226 + * + ****************************************************************************/ + +#ifdef CONFIG_SENSORS_INA226 +int stm32_ina226_initialization(void); +#endif + #endif /* __BOARDS_ARM_STM32F0L0G0_STM32G071B_DISCO_SRC_STM32G071B_DISCO_H */