From 1ed6fd45cdac48ca794ab990a726374a16d7248f Mon Sep 17 00:00:00 2001 From: penfold42 Date: Tue, 17 Jul 2018 14:58:26 +1000 Subject: [PATCH] Inital support for I2C bus scanning - needs to be configurable - needs to NOT trash the i2c bus when finished --- src/main.cpp | 28 ++++++++++++++++++++++++++++ src/rpi-i2c.c | 29 +++++++++++++++++++++++++++++ src/rpi-i2c.h | 5 +++-- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f5200c7..a002ae4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1126,6 +1126,31 @@ void DisplayOptions(int y_pos) screen.PrintText(false, 0, y_pos += 16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); } +void DisplayI2CScan(int y_pos) +{ + int BSCMaster = options.I2CBusMaster(); + + snprintf(tempBuffer, tempBufferSize, "Scanning i2c devices on bus %d ...\r\n", BSCMaster); + screen.PrintText(false, 0, y_pos += 16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); + RPI_I2CInit(BSCMaster, 1); + int address = 0; + for (int y=0; y<8; y++) + { + int x=0; + for (x=0; x<16; x++) + { + int ret = RPI_I2CScan(BSCMaster, address); + if (ret) + tempBuffer[x] = '*'; + else + tempBuffer[x] = '.'; + address++; + } + tempBuffer[x] = 0; + screen.PrintText(false, 0, y_pos += 16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); + } +} + static void CheckOptions() { FIL fp; @@ -1255,6 +1280,9 @@ extern "C" if (options.ShowOptions()) DisplayOptions(y_pos+32); +// if (options.I2CScan()) + DisplayI2CScan(y_pos+32); + if (!options.QuickBoot()) IEC_Bus::WaitMicroSeconds(3 * 1000000); diff --git a/src/rpi-i2c.c b/src/rpi-i2c.c index 4072ab4..8630ee7 100644 --- a/src/rpi-i2c.c +++ b/src/rpi-i2c.c @@ -184,3 +184,32 @@ int RPI_I2CWrite(int BSCMaster, unsigned char slaveAddress, void* buffer, unsign //DEBUG_LOG("I2C Write %d %d\r\n", count, success); return success; } + +int RPI_I2CScan(int BSCMaster, unsigned char slaveAddress) +{ + int success = 1; + if (slaveAddress < 0x80) + { + unsigned baseAddress = GetBaseAddress(BSCMaster); + + write32(baseAddress + I2C_BSC_A, slaveAddress); + write32(baseAddress + I2C_BSC_C, CONTROL_BIT_CLEAR1); + write32(baseAddress + I2C_BSC_S, STATUS_BIT_CLKT | STATUS_BIT_ERR | STATUS_BIT_DONE); + write32(baseAddress + I2C_BSC_DLEN, 1); + write32(baseAddress + I2C_BSC_C, CONTROL_BIT_I2CEN | CONTROL_BIT_ST | CONTROL_BIT_READ); + + while (!(read32(baseAddress + I2C_BSC_S) & STATUS_BIT_DONE)) + { + } + + unsigned status = read32(baseAddress + I2C_BSC_S); + if (status & (STATUS_BIT_ERR | STATUS_BIT_CLKT) ) + { + success = 0; + } + + write32(baseAddress + I2C_BSC_S, STATUS_BIT_CLKT | STATUS_BIT_ERR | STATUS_BIT_DONE); + } + return success; +} + diff --git a/src/rpi-i2c.h b/src/rpi-i2c.h index ac7a81a..0adcc94 100644 --- a/src/rpi-i2c.h +++ b/src/rpi-i2c.h @@ -1,5 +1,5 @@ -#ifndef RPI_AUX_H -#define RPI_AUX_H +#ifndef RPI_I2C_H +#define RPI_I2C_H #include "rpi-base.h" @@ -7,5 +7,6 @@ extern void RPI_I2CInit(int BSCMaster, int fast); extern void RPI_I2CSetClock(int BSCMaster, int clock_freq); extern int RPI_I2CRead(int BSCMaster, unsigned char slaveAddress, void* buffer, unsigned count); extern int RPI_I2CWrite(int BSCMaster, unsigned char slaveAddress, void* buffer, unsigned count); +extern int RPI_I2CScan(int BSCMaster, unsigned char slaveAddress); #endif