From 957848511752c6382002c009e61e409364f8f90e Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 1 Jul 2024 11:06:45 +0200 Subject: [PATCH] Added I2C address scan --- include/sta/bus/i2c/i2c.hpp | 5 +++++ include/sta/devices/stm32/bus/i2c.hpp | 1 + src/devices/stm32/bus/i2c.cpp | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/include/sta/bus/i2c/i2c.hpp b/include/sta/bus/i2c/i2c.hpp index e9896fc..e4f7c19 100644 --- a/include/sta/bus/i2c/i2c.hpp +++ b/include/sta/bus/i2c/i2c.hpp @@ -35,6 +35,11 @@ namespace sta */ void setSettings(uint16_t address, bool master, bool blocking); + /** + * @brief Prints all available addresses via the debug printable. + * + */ + virtual void scanAddresses() = 0; protected: /// @brief The peripheral's address to communicate with. uint16_t address_; diff --git a/include/sta/devices/stm32/bus/i2c.hpp b/include/sta/devices/stm32/bus/i2c.hpp index c195709..ff81833 100644 --- a/include/sta/devices/stm32/bus/i2c.hpp +++ b/include/sta/devices/stm32/bus/i2c.hpp @@ -27,6 +27,7 @@ namespace sta void receive(uint8_t * buffer, size_t size) override; void fill(uint8_t value, size_t count) override; + void scanAddresses() override; private: I2C_HandleTypeDef * handle_; const uint32_t timeout_ = 1000; diff --git a/src/devices/stm32/bus/i2c.cpp b/src/devices/stm32/bus/i2c.cpp index 29ba02d..7b79a4c 100644 --- a/src/devices/stm32/bus/i2c.cpp +++ b/src/devices/stm32/bus/i2c.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -116,6 +117,19 @@ namespace sta delete [] buffer; } + void STM32I2C::scanAddresses() + { + for (uint8_t i = 0; i < 128; ++i) + { + uint8_t ret = HAL_I2C_IsDeviceReady(handle_, (uint16_t)(i << 1), 3, 5); + + if (ret != HAL_OK) + STA_DEBUG_PRINTF("Address %d is available", i); + else + STA_DEBUG_PRINTF("Address %d is unavailable", i); + } + } + STM32I2CDevice::STM32I2CDevice(STM32I2C * intf, int address, bool master, bool blocking) /* The address is bit-shifted by one to work properly for the STM32 HAL. */ : I2CDevice(intf, address << 1, master, blocking)