Added address scan and verification for STM32 I2C

This commit is contained in:
dario 2024-04-20 16:05:39 +02:00
parent 7f5d918c96
commit 48cb90c64b
2 changed files with 25 additions and 1 deletions

View File

@ -25,6 +25,8 @@ namespace sta
void transfer16(uint16_t value) override;
void transfer(const uint8_t * buffer, size_t size) override;
void receive(uint8_t * buffer, size_t size) override;
bool hasAddress(int address);
void addressScan();
void fill(uint8_t value, size_t count) override;
private:

View File

@ -1,8 +1,12 @@
#include <sta/devices/stm32/bus/i2c.hpp>
#include <sta/debug/assert.hpp>
#include <sta/debug/debug.hpp>
#include <cstring>
#include <i2c.h>
#ifdef STA_STM32_I2C_ENABLED
namespace sta
@ -116,11 +120,29 @@ namespace sta
delete [] buffer;
}
bool STM32I2C::hasAddress(int address)
{
return HAL_I2C_IsDeviceReady(handle_, (uint16_t)(address << 1), 3, 5) == HAL_OK;
}
void STM32I2C::addressScan()
{
for(uint8_t i = 1; i < 128; i++)
{
if (hasAddress(i))
{
STA_DEBUG_PRINTF("Address %d exists", 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)
{
intf->acquire();
STA_ASSERT(intf->hasAddress(address));
intf->release();
}
} // namespace sta