Added I2C address scan

This commit is contained in:
dario 2024-07-01 11:06:45 +02:00
parent 7efa3c333e
commit 9578485117
3 changed files with 20 additions and 0 deletions

View File

@ -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_;

View File

@ -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;

View File

@ -1,5 +1,6 @@
#include <sta/devices/stm32/bus/i2c.hpp>
#include <sta/debug/debug.hpp>
#include <sta/debug/assert.hpp>
#include <cstring>
@ -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)