Inital support for I2C bus scanning

- needs to be configurable
- needs to NOT trash the i2c bus when finished
This commit is contained in:
penfold42 2018-07-17 14:58:26 +10:00
parent 30f1d4747e
commit 1ed6fd45cd
3 changed files with 60 additions and 2 deletions

View File

@ -1126,6 +1126,31 @@ void DisplayOptions(int y_pos)
screen.PrintText(false, 0, y_pos += 16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); 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() static void CheckOptions()
{ {
FIL fp; FIL fp;
@ -1255,6 +1280,9 @@ extern "C"
if (options.ShowOptions()) if (options.ShowOptions())
DisplayOptions(y_pos+32); DisplayOptions(y_pos+32);
// if (options.I2CScan())
DisplayI2CScan(y_pos+32);
if (!options.QuickBoot()) if (!options.QuickBoot())
IEC_Bus::WaitMicroSeconds(3 * 1000000); IEC_Bus::WaitMicroSeconds(3 * 1000000);

View File

@ -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); //DEBUG_LOG("I2C Write %d %d\r\n", count, success);
return 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;
}

View File

@ -1,5 +1,5 @@
#ifndef RPI_AUX_H #ifndef RPI_I2C_H
#define RPI_AUX_H #define RPI_I2C_H
#include "rpi-base.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 void RPI_I2CSetClock(int BSCMaster, int clock_freq);
extern int RPI_I2CRead(int BSCMaster, unsigned char slaveAddress, void* buffer, unsigned count); 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_I2CWrite(int BSCMaster, unsigned char slaveAddress, void* buffer, unsigned count);
extern int RPI_I2CScan(int BSCMaster, unsigned char slaveAddress);
#endif #endif