From 3f24e702244adb99817d36746d8d37b388057eb1 Mon Sep 17 00:00:00 2001 From: Tharo <17233964+Thar0@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:45:40 +0100 Subject: [PATCH 1/4] Decompile errorled.c, GPIO register defines --- include/PR/bcp.h | 38 +++++++++++++++++++++++++--- src/bb/misc/boxid.c | 2 +- src/bb/misc/errorled.c | 14 +++++++++++ src/bb/misc/power.c | 6 ++--- src/bb/misc/rtc.c | 56 +++++++++++++++++++++--------------------- src/os/initialize.c | 2 +- 6 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 src/bb/misc/errorled.c diff --git a/include/PR/bcp.h b/include/PR/bcp.h index 59367442..b3bae4b3 100644 --- a/include/PR/bcp.h +++ b/include/PR/bcp.h @@ -70,10 +70,40 @@ //! ? #define PI_5C_REG (PI_BASE_REG + 0x5C) - - -//! PI_MISC_REG ? -#define PI_60_REG (PI_BASE_REG + 0x60) +/** + * [31:16] Box ID + * [31:30] ?? (osInitialize checks this and sets __osBbIsBb to 2 if != 0) + * [29:27] ?? (unused so far) + * [26:25] ?? (system clock speed identifier?) + * [24:22] ?? (bootrom, checked against MI_10_REG and copied there if mismatch) + * [ 7: 6] RTC mask + * [5] Error LED mask + * [4] Power Control mask + * [ 3: 2] RTC control + * [1] Error LED (1=on, 0=off) + * [0] Power Control (1=on, 0=off) + */ +#define PI_GPIO_REG (PI_BASE_REG + 0x60) + +#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16) +#define PI_GPIO_GET_PWR(reg) (((reg) >> 0) & 1) +#define PI_GPIO_GET_LED(reg) (((reg) >> 1) & 1) + +/* Box ID masks */ +#define PI_GPIO_BOXID_MASK_30_31 (3 << 30) + +/* Enable masks */ +#define PI_GPIO_MASK_PWR (1 << 4) +#define PI_GPIO_MASK_LED (2 << 4) +#define PI_GPIO_MASK_RTC_0 (4 << 4) +#define PI_GPIO_MASK_RTC_1 (8 << 4) +/* RTC (TODO) */ +/* LED */ +#define PI_GPIO_LED_ON (1 << 1) +#define PI_GPIO_LED_OFF (0 << 1) +/* Power */ +#define PI_GPIO_PWR_ON (1 << 0) +#define PI_GPIO_PWR_OFF (0 << 0) diff --git a/src/bb/misc/boxid.c b/src/bb/misc/boxid.c index 4bc1ce32..3b4b493f 100644 --- a/src/bb/misc/boxid.c +++ b/src/bb/misc/boxid.c @@ -2,5 +2,5 @@ #include "PR/bcp.h" u32 osBbGetBoxId(void) { - return IO_READ(PI_60_REG) >> 0x10; + return PI_GPIO_GET_BOXID(IO_READ(PI_GPIO_REG)); } diff --git a/src/bb/misc/errorled.c b/src/bb/misc/errorled.c new file mode 100644 index 00000000..048bd9d5 --- /dev/null +++ b/src/bb/misc/errorled.c @@ -0,0 +1,14 @@ +#include "PR/os_internal.h" +#include "PR/bcp.h" + +void osBbSetErrorLed(u32 value) { + u32 mask = IO_READ(PI_GPIO_REG); + mask &= ~PI_GPIO_LED_ON; + mask &= ~PI_GPIO_MASK_LED; + IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_ON : PI_GPIO_LED_OFF) | PI_GPIO_MASK_LED); +} + +u32 osBbGetErrorLed(void) { + u32 v = PI_GPIO_GET_LED(IO_READ(PI_GPIO_REG)); + return v; +} diff --git a/src/bb/misc/power.c b/src/bb/misc/power.c index 4ab77ead..979fcd04 100644 --- a/src/bb/misc/power.c +++ b/src/bb/misc/power.c @@ -2,11 +2,9 @@ #include "PR/bcp.h" void osBbPowerOn(void) { - // Power control = 1, Power mask = 1 - IO_WRITE(PI_60_REG, 0x11); + IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_ON); } void osBbPowerOff(void) { - // Power control = 0, Power mask = 1 - IO_WRITE(PI_60_REG, 0x10); + IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_OFF); } diff --git a/src/bb/misc/rtc.c b/src/bb/misc/rtc.c index 62d309ff..ebdb4331 100644 --- a/src/bb/misc/rtc.c +++ b/src/bb/misc/rtc.c @@ -5,29 +5,29 @@ void __osBbDelay(u32 usec); static void write_rtc(u32 x) { - IO_WRITE(PI_60_REG, x); + IO_WRITE(PI_GPIO_REG, x); __osBbDelay(2); } static void send_start(u8 write) { u32 i; u32 j; - u32 mask = IO_READ(PI_60_REG) & ~0xCC; + u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); u8 byte[2]; byte[0] = (!write) ? 0xD1 : 0xD0; byte[1] = 0; - write_rtc(mask | 0xC4); - write_rtc(mask | 0xC0); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 4)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0)); for (i = 0; i < write + 1; i++) { for (j = 0; j < 8; j++) { - u32 b = ((byte[i] >> (7 - j)) & 1) ? 8 : 0; + u32 b = ((byte[i] >> (7 - j)) & 1) ? (2 << 2) : (0 << 2); - write_rtc(mask | (0x80 | b) | 0x40); - write_rtc(mask | (0x80 | b) | 0x44); - write_rtc(mask | (0x80 | b) | 0x40); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); } write_rtc(mask | 0x40); write_rtc(mask | 0x44); @@ -36,39 +36,39 @@ static void send_start(u8 write) { } static void send_stop(void) { - u32 mask = IO_READ(PI_60_REG) & ~0xCC; - write_rtc(mask | 0x80 | 0x40); - write_rtc(mask | 0x80 | 0x44); - write_rtc(mask | 0x80 | 0x4C); + u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_1 | PI_GPIO_MASK_RTC_0 | 0xC); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x4)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0xC)); } static void read_bytes(u8* bytes, u8 len) { u32 ack; u32 i; - u32 mask = IO_READ(PI_60_REG) & ~0xCC; + u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); while (len-- > 0) { u32 x = 0; for (i = 0; i < 8; i++) { - write_rtc(mask | 0x40); - write_rtc(mask | 0x44); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4)); x <<= 1; - x |= (IO_READ(PI_60_REG) >> 3) & 1; + x |= (IO_READ(PI_GPIO_REG) >> 3) & 1; } *(bytes++) = x; - ack = (len == 0) ? 0x88 : 0x80; + ack = (len == 0) ? (PI_GPIO_MASK_RTC_1 | 8) : (PI_GPIO_MASK_RTC_1 | 0); - write_rtc(mask | 0x40 | 0x80); - write_rtc(mask | 0x44 | ack); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4) | ack); } send_stop(); } static void write_bytes(u8* bytes, u8 len) { u32 i; - u32 mask = IO_READ(PI_60_REG) & ~0xCC; + u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); while (len-- > 0) { u32 x = *(bytes++); @@ -76,21 +76,21 @@ static void write_bytes(u8* bytes, u8 len) { for (i = 0; i < 8; i++) { u32 b = (x & 0x80) ? 8 : 0; - write_rtc(mask | (0x80 | b) | 0x40); - write_rtc(mask | (0x80 | b) | 0x44); - write_rtc(mask | (0x80 | b) | 0x44); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); + write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); x <<= 1; } - write_rtc(mask | 0x40); - write_rtc(mask | 0x44); - IO_READ(PI_60_REG); - write_rtc(mask | 0x40); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4)); + IO_READ(PI_GPIO_REG); + write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); } send_stop(); } void osBbRtcInit(void) { - write_rtc(IO_READ(PI_60_REG) | 0xCC); + write_rtc(IO_READ(PI_GPIO_REG) | PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); } void osBbRtcSet(u8 year, u8 month, u8 day, u8 dow, u8 hour, u8 min, u8 sec) { diff --git a/src/os/initialize.c b/src/os/initialize.c index 7d8821ef..37e28e91 100644 --- a/src/os/initialize.c +++ b/src/os/initialize.c @@ -106,7 +106,7 @@ void INITIALIZE_FUNC() { } if (__osBbIsBb) { - if (IO_READ(PI_60_REG) & 0xC0000000) { + if (IO_READ(PI_GPIO_REG) & PI_GPIO_BOXID_MASK_30_31) { __osBbIsBb = 2; } } From eacc415b9e135b502c853e242c4fd27446a0c9f3 Mon Sep 17 00:00:00 2001 From: Tharo <17233964+Thar0@users.noreply.github.com> Date: Mon, 29 Apr 2024 00:03:58 +0100 Subject: [PATCH 2/4] LED_OFF <-> LED_ON --- include/PR/bcp.h | 6 +++--- src/bb/misc/errorled.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/PR/bcp.h b/include/PR/bcp.h index b3bae4b3..d062c807 100644 --- a/include/PR/bcp.h +++ b/include/PR/bcp.h @@ -99,11 +99,11 @@ #define PI_GPIO_MASK_RTC_1 (8 << 4) /* RTC (TODO) */ /* LED */ -#define PI_GPIO_LED_ON (1 << 1) -#define PI_GPIO_LED_OFF (0 << 1) +#define PI_GPIO_LED_ON (0 << 1) +#define PI_GPIO_LED_OFF (1 << 1) /* Power */ -#define PI_GPIO_PWR_ON (1 << 0) #define PI_GPIO_PWR_OFF (0 << 0) +#define PI_GPIO_PWR_ON (1 << 0) diff --git a/src/bb/misc/errorled.c b/src/bb/misc/errorled.c index 048bd9d5..65009202 100644 --- a/src/bb/misc/errorled.c +++ b/src/bb/misc/errorled.c @@ -3,9 +3,9 @@ void osBbSetErrorLed(u32 value) { u32 mask = IO_READ(PI_GPIO_REG); - mask &= ~PI_GPIO_LED_ON; + mask &= ~PI_GPIO_LED_OFF; mask &= ~PI_GPIO_MASK_LED; - IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_ON : PI_GPIO_LED_OFF) | PI_GPIO_MASK_LED); + IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_OFF : PI_GPIO_LED_ON) | PI_GPIO_MASK_LED); } u32 osBbGetErrorLed(void) { From e470498d1cbf9653bc9b7cfca9f2c8234ea5a4a3 Mon Sep 17 00:00:00 2001 From: Tharo <17233964+Thar0@users.noreply.github.com> Date: Mon, 29 Apr 2024 03:54:22 +0100 Subject: [PATCH 3/4] RTC doc --- include/PR/bcp.h | 34 ++++++++----- src/bb/misc/rtc.c | 120 +++++++++++++++++++++++++++++++++------------- 2 files changed, 108 insertions(+), 46 deletions(-) diff --git a/include/PR/bcp.h b/include/PR/bcp.h index d062c807..13a47125 100644 --- a/include/PR/bcp.h +++ b/include/PR/bcp.h @@ -85,25 +85,33 @@ */ #define PI_GPIO_REG (PI_BASE_REG + 0x60) -#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16) -#define PI_GPIO_GET_PWR(reg) (((reg) >> 0) & 1) -#define PI_GPIO_GET_LED(reg) (((reg) >> 1) & 1) +#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16) +#define PI_GPIO_GET_PWR(reg) (((reg) >> 0) & 1) +#define PI_GPIO_GET_LED(reg) (((reg) >> 1) & 1) +#define PI_GPIO_GET_RTC_CLK(reg) (((reg) >> 2) & 1) +#define PI_GPIO_GET_RTC_DAT(reg) (((reg) >> 3) & 1) /* Box ID masks */ #define PI_GPIO_BOXID_MASK_30_31 (3 << 30) /* Enable masks */ -#define PI_GPIO_MASK_PWR (1 << 4) -#define PI_GPIO_MASK_LED (2 << 4) -#define PI_GPIO_MASK_RTC_0 (4 << 4) -#define PI_GPIO_MASK_RTC_1 (8 << 4) -/* RTC (TODO) */ -/* LED */ -#define PI_GPIO_LED_ON (0 << 1) -#define PI_GPIO_LED_OFF (1 << 1) +#define PI_GPIO_MASK_PWR (1 << 4) +#define PI_GPIO_MASK_LED (2 << 4) +#define PI_GPIO_MASK_RTC_CLK (4 << 4) +#define PI_GPIO_MASK_RTC_DAT (8 << 4) + +/* Controls */ /* Power */ -#define PI_GPIO_PWR_OFF (0 << 0) -#define PI_GPIO_PWR_ON (1 << 0) +#define PI_GPIO_PWR_OFF (0 << 0) +#define PI_GPIO_PWR_ON (1 << 0) +/* LED */ +#define PI_GPIO_LED_ON (0 << 1) +#define PI_GPIO_LED_OFF (1 << 1) +/* RTC */ +#define PI_GPIO_RTC_CLK_LO (0 << 2) +#define PI_GPIO_RTC_CLK_HI (1 << 2) +#define PI_GPIO_RTC_DAT_LO (0 << 3) +#define PI_GPIO_RTC_DAT_HI (1 << 3) diff --git a/src/bb/misc/rtc.c b/src/bb/misc/rtc.c index ebdb4331..160a5f11 100644 --- a/src/bb/misc/rtc.c +++ b/src/bb/misc/rtc.c @@ -2,6 +2,12 @@ #include "bcp.h" #include "macros.h" +#define RTC_MASK ((PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)) + +#define RTC_ADDR 0xD0 +#define RTC_WR 0 +#define RTC_RD 1 + void __osBbDelay(u32 usec); static void write_rtc(u32 x) { @@ -12,85 +18,133 @@ static void write_rtc(u32 x) { static void send_start(u8 write) { u32 i; u32 j; - u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); + u32 mask = IO_READ(PI_GPIO_REG) & ~RTC_MASK; u8 byte[2]; - byte[0] = (!write) ? 0xD1 : 0xD0; + byte[0] = (!write) ? (RTC_ADDR | RTC_RD) : (RTC_ADDR | RTC_WR); byte[1] = 0; - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 4)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0)); + // Send start signal (DAT HIGH -> LOW while CLK HIGH) assuming both CLK and DAT were initially HIGH + // Cyc -1 | 0 | 1 + // CLK ‾‾‾|‾‾‾|___ + // DAT ‾‾‾|___|___ + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); for (i = 0; i < write + 1; i++) { + // Send address in byte[0], for writes also send word address in byte[1] for (j = 0; j < 8; j++) { - u32 b = ((byte[i] >> (7 - j)) & 1) ? (2 << 2) : (0 << 2); - - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); + u32 b = ((byte[i] >> (7 - j)) & 1) ? PI_GPIO_RTC_DAT_HI : PI_GPIO_RTC_DAT_LO; + + // Transmit single bit to the RTC + // Cyc 0 | 1 | 2 + // CLK ___|‾‾‾|___ + // DAT b | b | b + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); } - write_rtc(mask | 0x40); - write_rtc(mask | 0x44); + + // Toggle CLK to receive ACK from the RTC, but don't read it + // Cyc 0 | 1 + // CLK ___|‾‾‾ + // DAT x | x + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } - write_rtc(mask | 0x40); + + // End on CLK LOW + // Cyc 0 + // CLK ___ + // DAT x + write_rtc(mask | PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO); } static void send_stop(void) { - u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_1 | PI_GPIO_MASK_RTC_0 | 0xC); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0x4)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0xC)); + u32 mask = IO_READ(PI_GPIO_REG) & ~RTC_MASK; + + // Send stop signal (DAT LOW -> HIGH while CLK HIGH) + // Cyc 0 | 1 | 2 + // CLK ___|‾‾‾|‾‾‾ + // DAT ___|___|‾‾‾ + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } static void read_bytes(u8* bytes, u8 len) { u32 ack; u32 i; - u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); + u32 mask = IO_READ(PI_GPIO_REG) & ~RTC_MASK; while (len-- > 0) { u32 x = 0; + // Read 1 byte for (i = 0; i < 8; i++) { - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4)); + // Toggle CLK to receive the bit from the RTC + // Cyc 0 | 1 + // CLK ___|‾‾‾ + // DAT x | x + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + + // Read the bit sent by the RTC x <<= 1; - x |= (IO_READ(PI_GPIO_REG) >> 3) & 1; + x |= PI_GPIO_GET_RTC_DAT(IO_READ(PI_GPIO_REG)); } *(bytes++) = x; - ack = (len == 0) ? (PI_GPIO_MASK_RTC_1 | 8) : (PI_GPIO_MASK_RTC_1 | 0); + ack = (len == 0) ? (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) : (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | 0) | (PI_GPIO_MASK_RTC_0 | 0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4) | ack); + // Send ACK or NACK, DAT HIGH is NACK while DAT LOW is ACK. NACK is sent at th end. + // Cyc 0 | 1 + // CLK ___|‾‾‾ + // DAT ___| a + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI) | ack); } send_stop(); } static void write_bytes(u8* bytes, u8 len) { u32 i; - u32 mask = IO_READ(PI_GPIO_REG) & ~(PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); + u32 mask = IO_READ(PI_GPIO_REG) & ~RTC_MASK; while (len-- > 0) { u32 x = *(bytes++); for (i = 0; i < 8; i++) { - u32 b = (x & 0x80) ? 8 : 0; - - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); - write_rtc(mask | (PI_GPIO_MASK_RTC_1 | b) | (PI_GPIO_MASK_RTC_0 | 4)); + u32 b = (x & 0x80) ? PI_GPIO_RTC_DAT_HI : PI_GPIO_RTC_DAT_LO; + + // Transmit single bit to the RTC + // Cyc 0 | 1 | 2 + // CLK ___|‾‾‾|‾‾‾ + // DAT b | b | b + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); x <<= 1; } - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 4)); + + // Toggle CLK to receive ACK from the RTC, read but don't check? + // Cyc 0 | 1 | 2 | 3 + // CLK ___|‾‾‾|‾‾‾|___ + // DAT x | x | x | x + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); IO_READ(PI_GPIO_REG); - write_rtc(mask | (PI_GPIO_MASK_RTC_0 | 0)); + write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); } send_stop(); } void osBbRtcInit(void) { - write_rtc(IO_READ(PI_GPIO_REG) | PI_GPIO_MASK_RTC_0 | PI_GPIO_MASK_RTC_1 | 0xC); + // Set line state to idle (both CLK and DAT HIGH) + // Cyc 0 + // CLK ‾‾‾ + // DAT ‾‾‾ + write_rtc(IO_READ(PI_GPIO_REG) | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } void osBbRtcSet(u8 year, u8 month, u8 day, u8 dow, u8 hour, u8 min, u8 sec) { From 89ac02251ecdc365d0eba7068609c8cb577f72b7 Mon Sep 17 00:00:00 2001 From: Tharo <17233964+Thar0@users.noreply.github.com> Date: Tue, 30 Apr 2024 01:07:41 +0100 Subject: [PATCH 4/4] Correct GPIO --- include/PR/bcp.h | 68 ++++++++++++++++++++++++------------------ src/bb/misc/errorled.c | 4 +-- src/bb/misc/power.c | 4 +-- src/bb/misc/rtc.c | 48 ++++++++++++++--------------- 4 files changed, 67 insertions(+), 57 deletions(-) diff --git a/include/PR/bcp.h b/include/PR/bcp.h index 13a47125..29c68ca6 100644 --- a/include/PR/bcp.h +++ b/include/PR/bcp.h @@ -76,43 +76,53 @@ * [29:27] ?? (unused so far) * [26:25] ?? (system clock speed identifier?) * [24:22] ?? (bootrom, checked against MI_10_REG and copied there if mismatch) - * [ 7: 6] RTC mask - * [5] Error LED mask - * [4] Power Control mask - * [ 3: 2] RTC control - * [1] Error LED (1=on, 0=off) - * [0] Power Control (1=on, 0=off) + * [21:16] ?? (unused so far) + * [ 7: 4] GPIO direction control + * [7] RTC Data output enable + * [6] RTC Clock output enable + * [5] Error LED output enable + * [4] Power Control output enable + * [ 3: 0] GPIO in/out value + * [3] RTC Data output value (0=low, 1=high) + * [2] RTC Clock output value (0=low, 1=high) + * [1] Error LED (0=on, 1=off) + * [0] Power Control (0=off, 1=on) */ #define PI_GPIO_REG (PI_BASE_REG + 0x60) -#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16) +/* Box ID */ +#define PI_GPIO_GET_BOXID(reg) ((reg) >> 16) +#define PI_GPIO_BOXID_MASK_30_31 (3 << 30) + +/* Input/Output enables */ +#define PI_GPIO_I_PWR ((0 << 0) << 4) +#define PI_GPIO_O_PWR ((1 << 0) << 4) +#define PI_GPIO_I_LED ((0 << 1) << 4) +#define PI_GPIO_O_LED ((1 << 1) << 4) +#define PI_GPIO_I_RTC_CLK ((0 << 2) << 4) +#define PI_GPIO_O_RTC_CLK ((1 << 2) << 4) +#define PI_GPIO_I_RTC_DAT ((0 << 3) << 4) +#define PI_GPIO_O_RTC_DAT ((1 << 3) << 4) + +/* Output controls */ +/* Power */ +#define PI_GPIO_PWR_OFF (0 << 0) +#define PI_GPIO_PWR_ON (1 << 0) +/* LED */ +#define PI_GPIO_LED_ON (0 << 1) +#define PI_GPIO_LED_OFF (1 << 1) +/* RTC */ +#define PI_GPIO_RTC_CLK_LO (0 << 2) +#define PI_GPIO_RTC_CLK_HI (1 << 2) +#define PI_GPIO_RTC_DAT_LO (0 << 3) +#define PI_GPIO_RTC_DAT_HI (1 << 3) + +/* Input getters */ #define PI_GPIO_GET_PWR(reg) (((reg) >> 0) & 1) #define PI_GPIO_GET_LED(reg) (((reg) >> 1) & 1) #define PI_GPIO_GET_RTC_CLK(reg) (((reg) >> 2) & 1) #define PI_GPIO_GET_RTC_DAT(reg) (((reg) >> 3) & 1) -/* Box ID masks */ -#define PI_GPIO_BOXID_MASK_30_31 (3 << 30) - -/* Enable masks */ -#define PI_GPIO_MASK_PWR (1 << 4) -#define PI_GPIO_MASK_LED (2 << 4) -#define PI_GPIO_MASK_RTC_CLK (4 << 4) -#define PI_GPIO_MASK_RTC_DAT (8 << 4) - -/* Controls */ -/* Power */ -#define PI_GPIO_PWR_OFF (0 << 0) -#define PI_GPIO_PWR_ON (1 << 0) -/* LED */ -#define PI_GPIO_LED_ON (0 << 1) -#define PI_GPIO_LED_OFF (1 << 1) -/* RTC */ -#define PI_GPIO_RTC_CLK_LO (0 << 2) -#define PI_GPIO_RTC_CLK_HI (1 << 2) -#define PI_GPIO_RTC_DAT_LO (0 << 3) -#define PI_GPIO_RTC_DAT_HI (1 << 3) - //! ? diff --git a/src/bb/misc/errorled.c b/src/bb/misc/errorled.c index 65009202..8cf3e53e 100644 --- a/src/bb/misc/errorled.c +++ b/src/bb/misc/errorled.c @@ -4,8 +4,8 @@ void osBbSetErrorLed(u32 value) { u32 mask = IO_READ(PI_GPIO_REG); mask &= ~PI_GPIO_LED_OFF; - mask &= ~PI_GPIO_MASK_LED; - IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_OFF : PI_GPIO_LED_ON) | PI_GPIO_MASK_LED); + mask &= ~PI_GPIO_O_LED; + IO_WRITE(PI_GPIO_REG, mask | ((value == 0) ? PI_GPIO_LED_OFF : PI_GPIO_LED_ON) | PI_GPIO_O_LED); } u32 osBbGetErrorLed(void) { diff --git a/src/bb/misc/power.c b/src/bb/misc/power.c index 979fcd04..f87d2723 100644 --- a/src/bb/misc/power.c +++ b/src/bb/misc/power.c @@ -2,9 +2,9 @@ #include "PR/bcp.h" void osBbPowerOn(void) { - IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_ON); + IO_WRITE(PI_GPIO_REG, PI_GPIO_O_PWR | PI_GPIO_PWR_ON); } void osBbPowerOff(void) { - IO_WRITE(PI_GPIO_REG, PI_GPIO_MASK_PWR | PI_GPIO_PWR_OFF); + IO_WRITE(PI_GPIO_REG, PI_GPIO_O_PWR | PI_GPIO_PWR_OFF); } diff --git a/src/bb/misc/rtc.c b/src/bb/misc/rtc.c index 160a5f11..f42766d1 100644 --- a/src/bb/misc/rtc.c +++ b/src/bb/misc/rtc.c @@ -2,7 +2,7 @@ #include "bcp.h" #include "macros.h" -#define RTC_MASK ((PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)) +#define RTC_MASK ((PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)) #define RTC_ADDR 0xD0 #define RTC_WR 0 @@ -28,8 +28,8 @@ static void send_start(u8 write) { // Cyc -1 | 0 | 1 // CLK ‾‾‾|‾‾‾|___ // DAT ‾‾‾|___|___ - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); for (i = 0; i < write + 1; i++) { // Send address in byte[0], for writes also send word address in byte[1] @@ -40,24 +40,24 @@ static void send_start(u8 write) { // Cyc 0 | 1 | 2 // CLK ___|‾‾‾|___ // DAT b | b | b - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); } // Toggle CLK to receive ACK from the RTC, but don't read it // Cyc 0 | 1 // CLK ___|‾‾‾ // DAT x | x - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } // End on CLK LOW // Cyc 0 // CLK ___ // DAT x - write_rtc(mask | PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); } static void send_stop(void) { @@ -67,9 +67,9 @@ static void send_stop(void) { // Cyc 0 | 1 | 2 // CLK ___|‾‾‾|‾‾‾ // DAT ___|___|‾‾‾ - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } static void read_bytes(u8* bytes, u8 len) { @@ -86,8 +86,8 @@ static void read_bytes(u8* bytes, u8 len) { // Cyc 0 | 1 // CLK ___|‾‾‾ // DAT x | x - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); // Read the bit sent by the RTC x <<= 1; @@ -95,14 +95,14 @@ static void read_bytes(u8* bytes, u8 len) { } *(bytes++) = x; - ack = (len == 0) ? (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) : (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO); + ack = (len == 0) ? (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_HI) : (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO); // Send ACK or NACK, DAT HIGH is NACK while DAT LOW is ACK. NACK is sent at th end. // Cyc 0 | 1 // CLK ___|‾‾‾ // DAT ___| a - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI) | ack); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_LO) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI) | ack); } send_stop(); } @@ -121,9 +121,9 @@ static void write_bytes(u8* bytes, u8 len) { // Cyc 0 | 1 | 2 // CLK ___|‾‾‾|‾‾‾ // DAT b | b | b - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); - write_rtc(mask | (PI_GPIO_MASK_RTC_DAT | b) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | (PI_GPIO_O_RTC_DAT | b) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); x <<= 1; } @@ -131,10 +131,10 @@ static void write_bytes(u8* bytes, u8 len) { // Cyc 0 | 1 | 2 | 3 // CLK ___|‾‾‾|‾‾‾|___ // DAT x | x | x | x - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); IO_READ(PI_GPIO_REG); - write_rtc(mask | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_LO)); + write_rtc(mask | PI_GPIO_I_RTC_DAT | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_LO)); } send_stop(); } @@ -144,7 +144,7 @@ void osBbRtcInit(void) { // Cyc 0 // CLK ‾‾‾ // DAT ‾‾‾ - write_rtc(IO_READ(PI_GPIO_REG) | (PI_GPIO_MASK_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_MASK_RTC_CLK | PI_GPIO_RTC_CLK_HI)); + write_rtc(IO_READ(PI_GPIO_REG) | (PI_GPIO_O_RTC_DAT | PI_GPIO_RTC_DAT_HI) | (PI_GPIO_O_RTC_CLK | PI_GPIO_RTC_CLK_HI)); } void osBbRtcSet(u8 year, u8 month, u8 day, u8 dow, u8 hour, u8 min, u8 sec) {