From 2e6153a653f0ad0b2e4832d4bb3ec478e34d9869 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Thu, 27 Dec 2018 21:11:35 -0800 Subject: [PATCH] I2C: Device probe function I wrote, didn't need, but thought might be useful in the future. --- firmware/common/i2c_lpc.c | 11 +++++++++++ firmware/common/i2c_lpc.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/firmware/common/i2c_lpc.c b/firmware/common/i2c_lpc.c index 607a1a0a..04f3788e 100644 --- a/firmware/common/i2c_lpc.c +++ b/firmware/common/i2c_lpc.c @@ -66,3 +66,14 @@ void i2c_lpc_transfer(i2c_bus_t* const bus, i2c_stop(port); } + +bool i2c_probe(i2c_bus_t* const bus, const uint_fast8_t device_address) { + const uint32_t port = (uint32_t)bus->obj; + + i2c_tx_start(port); + i2c_tx_byte(port, (device_address << 1) | I2C_WRITE); + const bool detected = (I2C_STAT(port) == 0x18); + i2c_stop(port); + + return detected; +} diff --git a/firmware/common/i2c_lpc.h b/firmware/common/i2c_lpc.h index 32c79fcd..41a2dd6a 100644 --- a/firmware/common/i2c_lpc.h +++ b/firmware/common/i2c_lpc.h @@ -24,6 +24,7 @@ #include #include +#include #include "i2c_bus.h" @@ -38,5 +39,6 @@ void i2c_lpc_transfer(i2c_bus_t* const bus, const uint8_t* const data_tx, const size_t count_tx, uint8_t* const data_rx, const size_t count_rx ); +bool i2c_probe(i2c_bus_t* const bus, const uint_fast8_t device_address); #endif/*__I2C_LPC_H__*/