Skip to content

Commit ce1d1bc

Browse files
committed
Replace HAL_I2C with I2c in display drivers
Swap out the legacy HAL_I2C implementation for the new I2c interface across display drivers (HD44780, SSD1306, SSD1311). Update includes, constructors and method calls to use I2c, adapt write/read/register calls, and adjust initialization and detection logic accordingly. Add a debug i2c detect header (common/include/firmware/debug/debug_i2cdetect.h) and introduce debug trace calls in display code. Remove obsolete lib-hal i2c headers/sources and bump copyright years to 2026. Also contains assorted formatting/namespace cleanups and small fixes to cursor/clear logic.
1 parent 7cc2e0f commit ce1d1bc

14 files changed

Lines changed: 347 additions & 889 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @file debug_i2cdetect.h
3+
*
4+
*/
5+
/* Copyright (C) 2020-2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef FIRMWARE_DEBUG_DEBUG_I2CDETECT_H_
27+
#define FIRMWARE_DEBUG_DEBUG_I2CDETECT_H_
28+
29+
#include <cstdint>
30+
#include <cstdio>
31+
32+
#include "i2c.h"
33+
34+
namespace debug::i2c {
35+
#ifndef DEBUG_I2C
36+
void Detect() {}
37+
#else
38+
inline constexpr uint32_t kFirst = 0x03;
39+
inline constexpr uint32_t kLast = 0x77;
40+
void Detect() {
41+
::i2c::Begin();
42+
::i2c::SetBaudrate(::i2c::kNormalSpeed);
43+
44+
puts("\n 0 1 2 3 4 5 6 7 8 9 a b c d e f");
45+
46+
for (uint32_t i = 0; i < 128; i = (i + 16)) {
47+
printf("%02x: ", i);
48+
for (uint32_t j = 0; j < 16; j++) {
49+
// Skip unwanted addresses
50+
if ((i + j < kFirst) || (i + j > kLast)) {
51+
printf(" ");
52+
continue;
53+
}
54+
55+
if (::i2c::IsConnected(static_cast<uint8_t>(i + j))) {
56+
printf("%02x ", i + j);
57+
} else {
58+
printf("-- ");
59+
}
60+
}
61+
62+
puts("");
63+
}
64+
65+
::i2c::Begin();
66+
}
67+
#endif
68+
} // namespace debug::i2c
69+
70+
#endif // FIRMWARE_DEBUG_DEBUG_I2CDETECT_H_

lib-display/include/i2c/hd44780.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file hd44780.h
33
*
44
*/
5-
/* Copyright (C) 2017-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2017-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -29,17 +29,15 @@
2929
#include <cstdint>
3030

3131
#include "displayset.h"
32-
#include "hal_i2c.h"
32+
#include "i2c.h"
3333

34-
namespace hd44780::pcf8574t
35-
{
34+
namespace hd44780::pcf8574t {
3635
inline constexpr uint8_t kDefaultAddress = 0x27;
3736
inline constexpr uint8_t kTC2004Address = kDefaultAddress;
3837
inline constexpr uint8_t kTC1602Address = 0x26;
3938
} // namespace hd44780::pcf8574t
4039

41-
class Hd44780 final : public DisplaySet
42-
{
40+
class Hd44780 final : public DisplaySet {
4341
public:
4442
Hd44780();
4543
Hd44780(uint8_t cols, uint8_t rows);
@@ -67,7 +65,7 @@ class Hd44780 final : public DisplaySet
6765
void WriteReg(uint8_t reg);
6866

6967
private:
70-
HAL_I2C hal_i2c_;
68+
I2c i2c_;
7169
};
7270

73-
#endif // I2C_HD44780_H_
71+
#endif // I2C_HD44780_H_

lib-display/include/i2c/ssd1306.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file ssd1306.h
33
*
44
*/
5-
/* Copyright (C) 2017-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2017-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -29,25 +29,22 @@
2929
#include <cstdint>
3030

3131
#include "displayset.h"
32-
#include "hal_i2c.h"
32+
#include "i2c.h"
3333

3434
#define OLED_I2C_ADDRESS_DEFAULT 0x3C
3535

36-
enum class OledPanel
37-
{
36+
enum class OledPanel {
3837
k128x648Rows, ///< Default
3938
k128x644Rows,
4039
k128x324Rows
4140
};
4241

43-
class Ssd1306 final : public DisplaySet
44-
{
42+
class Ssd1306 final : public DisplaySet {
4543
public:
4644
Ssd1306();
4745
explicit Ssd1306(OledPanel);
4846
Ssd1306(uint8_t, OledPanel);
49-
~Ssd1306() override
50-
{
47+
~Ssd1306() override {
5148
#if defined(CONFIG_DISPLAY_ENABLE_CURSOR_MODE)
5249
delete[] shadow_ram_;
5350
shadow_ram_ = nullptr;
@@ -93,7 +90,7 @@ class Ssd1306 final : public DisplaySet
9390
void DumpShadowRam();
9491

9592
private:
96-
HAL_I2C hal_i2c_;
93+
I2c i2c_;
9794
OledPanel oled_panel_{OledPanel::k128x648Rows};
9895
bool have_sh1106_{false};
9996
uint32_t pages_;
@@ -111,4 +108,4 @@ class Ssd1306 final : public DisplaySet
111108
static inline Ssd1306* s_this;
112109
};
113110

114-
#endif // I2C_SSD1306_H_
111+
#endif // I2C_SSD1306_H_

lib-display/include/i2c/ssd1311.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @file ssd1311.h
33
*
44
*/
5-
/* Copyright (C) 2020-2025 by Arjan van Vught mailto:info@gd32-dmx.org
5+
/* Copyright (C) 2020-2026 by Arjan van Vught mailto:info@gd32-dmx.org
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -29,10 +29,9 @@
2929
#include <cstdint>
3030

3131
#include "displayset.h"
32-
#include "hal_i2c.h"
32+
#include "i2c.h"
3333

34-
class Ssd1311 final : public DisplaySet
35-
{
34+
class Ssd1311 final : public DisplaySet {
3635
public:
3736
Ssd1311();
3837
~Ssd1311() override = default;
@@ -63,27 +62,19 @@ class Ssd1311 final : public DisplaySet
6362
void SelectRamRom(uint32_t ram, uint32_t rom);
6463
void SetDDRAM(uint8_t address);
6564
void SetCGRAM(uint8_t address);
66-
enum class FunctionSet
67-
{
68-
kReZero = (0 << 1),
69-
kReOne = (1 << 1)
70-
};
65+
enum class FunctionSet { kReZero = (0 << 1), kReOne = (1 << 1) };
7166
void SetRE(FunctionSet re);
72-
enum class CommandSet
73-
{
74-
kDisabled = 0,
75-
kEnabled = 1
76-
};
67+
enum class CommandSet { kDisabled = 0, kEnabled = 1 };
7768
void SetSD(CommandSet sd);
7869
void SendCommand(uint8_t command);
7970
void SendData(uint8_t data);
8071
void SendData(const uint8_t* data, uint32_t length);
8172

8273
private:
83-
HAL_I2C hal_i2_c_;
74+
I2c i2c_;
8475
uint8_t display_control_{1U << 3}; // Section 9.1.4 Display ON/OFF Control
8576

8677
static inline Ssd1311* s_this;
8778
};
8879

89-
#endif // I2C_SSD1311_H_
80+
#endif // I2C_SSD1311_H_

0 commit comments

Comments
 (0)