From 1ed6fd45cdac48ca794ab990a726374a16d7248f Mon Sep 17 00:00:00 2001 From: penfold42 Date: Tue, 17 Jul 2018 14:58:26 +1000 Subject: [PATCH 1/4] 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 From c223b7aa5729b94a20069914854a7fea16856932 Mon Sep 17 00:00:00 2001 From: penfold42 Date: Tue, 17 Jul 2018 18:25:15 +1000 Subject: [PATCH 2/4] i2c scanning is now optional: i2cScan = 1 prints the 7 bit addresses in decimal --- src/main.cpp | 37 +++++++++++++++++++------------------ src/options.cpp | 2 ++ src/options.h | 3 +++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a002ae4..00b1413 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1130,25 +1130,26 @@ 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); + snprintf(tempBuffer, tempBufferSize, "Scanning i2c bus %d ...\r\n", BSCMaster); + screen.PrintText(false, 0, y_pos , tempBuffer, COLOUR_WHITE, COLOUR_BLACK); + RPI_I2CInit(BSCMaster, 1); - int address = 0; - for (int y=0; y<8; y++) + + int count=0; + int ptr = 0; + ptr = snprintf (tempBuffer+ptr, tempBufferSize-ptr, "Found "); + for (int address = 0; address<128; address++) { - int x=0; - for (x=0; x<16; x++) + if (RPI_I2CScan(BSCMaster, address)) { - int ret = RPI_I2CScan(BSCMaster, address); - if (ret) - tempBuffer[x] = '*'; - else - tempBuffer[x] = '.'; - address++; + ptr += snprintf (tempBuffer+ptr, tempBufferSize-ptr, "%3d ", address); + count++; } - tempBuffer[x] = 0; - screen.PrintText(false, 0, y_pos += 16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); } + if (count == 0) + ptr += snprintf (tempBuffer+ptr, tempBufferSize-ptr, "Nothing"); + + screen.PrintText(false, 0, y_pos+16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); } static void CheckOptions() @@ -1277,11 +1278,11 @@ extern "C" snprintf(tempBuffer, tempBufferSize, "This is free software, and you are welcome to redistribute it."); screen.PrintText(false, 0, y_pos+=16, tempBuffer, COLOUR_WHITE, COLOUR_BLACK); - if (options.ShowOptions()) - DisplayOptions(y_pos+32); + if (options.I2CScan()) + DisplayI2CScan(y_pos+=32); -// if (options.I2CScan()) - DisplayI2CScan(y_pos+32); + if (options.ShowOptions()) + DisplayOptions(y_pos+=32); if (!options.QuickBoot()) IEC_Bus::WaitMicroSeconds(3 * 1000000); diff --git a/src/options.cpp b/src/options.cpp index 4ebde18..fb4569c 100644 --- a/src/options.cpp +++ b/src/options.cpp @@ -142,6 +142,7 @@ Options::Options(void) , screenHeight(768) , i2cBusMaster(1) , i2cLcdAddress(0x3C) + , i2cScan(0) , i2cLcdFlip(0) , i2cLcdOnContrast(127) , i2cLcdModel(0) @@ -216,6 +217,7 @@ void Options::Process(char* buffer) ELSE_CHECK_DECIMAL_OPTION(screenHeight) ELSE_CHECK_DECIMAL_OPTION(i2cBusMaster) ELSE_CHECK_DECIMAL_OPTION(i2cLcdAddress) + ELSE_CHECK_DECIMAL_OPTION(i2cScan) ELSE_CHECK_DECIMAL_OPTION(i2cLcdFlip) ELSE_CHECK_DECIMAL_OPTION(i2cLcdOnContrast) ELSE_CHECK_DECIMAL_OPTION(i2cLcdDimContrast) diff --git a/src/options.h b/src/options.h index f53d758..b5eb17b 100644 --- a/src/options.h +++ b/src/options.h @@ -72,11 +72,13 @@ public: inline unsigned int I2CBusMaster() const { return i2cBusMaster; } inline unsigned int I2CLcdAddress() const { return i2cLcdAddress; } + inline unsigned int I2CScan() const { return i2cScan; } inline unsigned int I2CLcdFlip() const { return i2cLcdFlip; } inline unsigned int I2CLcdOnContrast() const { return i2cLcdOnContrast; } inline unsigned int I2CLcdDimContrast() const { return i2cLcdDimContrast; } inline unsigned int I2CLcdDimTime() const { return i2cLcdDimTime; } inline unsigned int I2CLcdModel() const { return i2cLcdModel; } + inline const char* GetLcdLogoName() const { return LcdLogoName; } inline float ScrollHighlightRate() const { return scrollHighlightRate; } @@ -114,6 +116,7 @@ private: unsigned int i2cBusMaster; unsigned int i2cLcdAddress; + unsigned int i2cScan; unsigned int i2cLcdFlip; unsigned int i2cLcdOnContrast; unsigned int i2cLcdDimContrast; From efe0f0bd6d13be6dbd52e5dabf551e71e2ed9c19 Mon Sep 17 00:00:00 2001 From: penfold42 Date: Tue, 17 Jul 2018 19:56:01 +1000 Subject: [PATCH 3/4] fix contrast setting for sh1106 --- src/SSD1306.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SSD1306.cpp b/src/SSD1306.cpp index d57c370..3cb61f3 100644 --- a/src/SSD1306.cpp +++ b/src/SSD1306.cpp @@ -183,7 +183,8 @@ void SSD1306::SetContrast(u8 value) contrast = value; SendCommand(SSD1306_CMD_SET_CONTRAST_CONTROL); SendCommand(value); - SetVCOMDeselect( value >> 5); + if (type == 1306) + SetVCOMDeselect( value >> 8); } void SSD1306::SetVCOMDeselect(u8 value) From 8c76614858f31a86fb3493f0591dc03df1b6264c Mon Sep 17 00:00:00 2001 From: penfold42 Date: Tue, 17 Jul 2018 20:04:09 +1000 Subject: [PATCH 4/4] Fix compiler warning --- src/SSD1306.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SSD1306.cpp b/src/SSD1306.cpp index 3cb61f3..8f20f1a 100644 --- a/src/SSD1306.cpp +++ b/src/SSD1306.cpp @@ -139,7 +139,7 @@ void SSD1306::RefreshScreen() void SSD1306::RefreshRows(u32 start, u32 amountOfRows) { - int i; + unsigned int i; start <<= 1; amountOfRows <<= 1;