From 5055242df843949d2b5c266f184d5cf07fd5aef7 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 13 Mar 2024 10:43:14 +0100 Subject: [PATCH] Refactoring, update offsets --- cpp/buses/bus.h | 6 +-- cpp/buses/in_process_bus.h | 4 ++ cpp/buses/rpi_bus.cpp | 86 +++++++++++++++++++++++++++----------- cpp/buses/rpi_bus.h | 6 ++- 4 files changed, 71 insertions(+), 31 deletions(-) diff --git a/cpp/buses/bus.h b/cpp/buses/bus.h index 0b8c3a17..26481ed6 100644 --- a/cpp/buses/bus.h +++ b/cpp/buses/bus.h @@ -101,7 +101,6 @@ constexpr static int GPIO_INPUT = 0; constexpr static int GPIO_OUTPUT = 1; constexpr static int GPIO_PULLNONE = 0; constexpr static int GPIO_PULLDOWN = 1; -constexpr static int GPIO_PULLUP = 2; // Constant declarations (Control signals) #define ACT_OFF !ACT_ON @@ -136,10 +135,7 @@ class Bus virtual void SetSEL(bool) = 0; - virtual bool GetIO() - { - return GetSignal(PIN_IO); - } + virtual bool GetIO() = 0; virtual void SetIO(bool) = 0; virtual uint8_t GetDAT() = 0; diff --git a/cpp/buses/in_process_bus.h b/cpp/buses/in_process_bus.h index a92ce08c..21524756 100644 --- a/cpp/buses/in_process_bus.h +++ b/cpp/buses/in_process_bus.h @@ -44,6 +44,10 @@ class InProcessBus : public Bus SetSignal(PIN_SEL, state); } + bool GetIO() override + { + return GetSignal(PIN_IO); + } void SetIO(bool state) override { SetSignal(PIN_IO, state); diff --git a/cpp/buses/rpi_bus.cpp b/cpp/buses/rpi_bus.cpp index 768e8bda..6ebbaf87 100644 --- a/cpp/buses/rpi_bus.cpp +++ b/cpp/buses/rpi_bus.cpp @@ -27,6 +27,7 @@ bool RpiBus::Init(bool target) off_t base_addr = 0; uint32_t gpio_offset = GPIO_OFFSET; + uint32_t pads_offset = PADS_OFFSET; switch (pi_type) { case RpiBus::PiType::pi_1: base_addr = 0x20000000; @@ -44,6 +45,7 @@ bool RpiBus::Init(bool target) case RpiBus::PiType::pi_5: base_addr = 0x1f00000000; gpio_offset = GPIO_OFFSET_PI5; + pads_offset = PADS_OFFSET_PI5; break; default: @@ -93,17 +95,17 @@ bool RpiBus::Init(bool target) level = &gpio[GPIO_LEV_0]; // PADS - pads = map + PADS_OFFSET / sizeof(uint32_t); + pads = map + pads_offset / sizeof(uint32_t); - // Interrupt controller + // Interrupt controller (Pi 1) irp_ctl = map + IRPT_OFFSET / sizeof(uint32_t); - // Quad-A7 control + // Quad-A7 control (Pi 2/3) qa7_regs = map + QA7_OFFSET / sizeof(uint32_t); // Map GIC interrupt priority mask register if (pi_type == PiType::pi_4) { - gicc_mpr = static_cast(mmap(nullptr, 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PI4_ARM_GICC_BASE)); + gicc_mpr = static_cast(mmap(nullptr, 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PI4_ARM_GICC_CTLR)); if (gicc_mpr == MAP_FAILED) { critical("Can't map GIC: {}", strerror(errno)); close(fd); @@ -319,6 +321,38 @@ void RpiBus::SetSEL(bool state) SetSignal(PIN_SEL, state); } +bool RpiBus::GetIO() +{ + const bool state = GetSignal(PIN_IO); + + if (!IsTarget()) { + // Change the data input/output direction by IO signal + if (state) { + SetControl(PIN_DTD, DTD_IN); + SetMode(PIN_DT0, IN); + SetMode(PIN_DT1, IN); + SetMode(PIN_DT2, IN); + SetMode(PIN_DT3, IN); + SetMode(PIN_DT4, IN); + SetMode(PIN_DT5, IN); + SetMode(PIN_DT6, IN); + SetMode(PIN_DT7, IN); + } else { + SetControl(PIN_DTD, DTD_OUT); + SetMode(PIN_DT0, OUT); + SetMode(PIN_DT1, OUT); + SetMode(PIN_DT2, OUT); + SetMode(PIN_DT3, OUT); + SetMode(PIN_DT4, OUT); + SetMode(PIN_DT5, OUT); + SetMode(PIN_DT6, OUT); + SetMode(PIN_DT7, OUT); + } + } + + return state; +} + void RpiBus::SetIO(bool state) { assert(IsTarget()); @@ -546,12 +580,11 @@ void RpiBus::DisableIRQ() { #ifdef __linux__ switch (pi_type) { - case PiType::pi_4: - // RPI4 disables interrupts via the GIC - gicc_pmr_saved = *gicc_mpr; - *gicc_mpr = 0; + case PiType::pi_1: + // Stop system timer interrupt with interrupt controller + irpt_enb = irp_ctl[IRPT_ENB_IRQ_1]; + irp_ctl[IRPT_DIS_IRQ_1] = irpt_enb & 0xf; break; - case PiType::pi_2: case PiType::pi_3: // RPI2,3 disable core timer IRQ @@ -560,10 +593,14 @@ void RpiBus::DisableIRQ() qa7_regs[tint_core] = 0; break; + case PiType::pi_4: + // RPI4 disables interrupts via the GIC + gicc_pmr_saved = *gicc_mpr; + *gicc_mpr = 0; + break; + default: - // Stop system timer interrupt with interrupt controller - irpt_enb = irp_ctl[IRPT_ENB_IRQ_1]; - irp_ctl[IRPT_DIS_IRQ_1] = irpt_enb & 0xf; + // Currently do nothing break; } #endif @@ -573,9 +610,9 @@ void RpiBus::EnableIRQ() { #ifdef __linux__ switch (pi_type) { - case PiType::pi_4: - // RPI4 enables interrupts via the GIC - *gicc_mpr = gicc_pmr_saved; + case PiType::pi_1: + // Restart the system timer interrupt with the interrupt controller + irp_ctl[IRPT_ENB_IRQ_1] = irpt_enb & 0xf; break; case PiType::pi_2: @@ -584,9 +621,13 @@ void RpiBus::EnableIRQ() qa7_regs[tint_core] = tint_ctl; break; + case PiType::pi_4: + // RPI4 enables interrupts via the GIC + *gicc_mpr = gicc_pmr_saved; + break; + default: - // Restart the system timer interrupt with the interrupt controller - irp_ctl[IRPT_ENB_IRQ_1] = irpt_enb & 0xf; + // Currently do nothing break; } #endif @@ -608,7 +649,7 @@ void RpiBus::PinConfig(int pin, int mode) } const int index = pin / 10; - uint32_t mask = ~(0x7 << ((pin % 10) * 3)); + uint32_t mask = ~(7 << ((pin % 10) * 3)); gpio[index] = (gpio[index] & mask) | ((mode & 0x7) << ((pin % 10) * 3)); } @@ -620,22 +661,19 @@ void RpiBus::PullConfig(int pin, int mode) return; } - if (pi_type == PiType::pi_4) { + if (pi_type == PiType::pi_4 || pi_type == PiType::pi_5) { uint32_t pull; switch (mode) { case GPIO_PULLNONE: pull = 0; break; - case GPIO_PULLUP: - pull = 1; - break; - case GPIO_PULLDOWN: pull = 2; break; default: + assert(false); return; } @@ -652,7 +690,7 @@ void RpiBus::PullConfig(int pin, int mode) pin &= 0x1f; gpio[GPIO_PUD] = mode & 0x3; nanosleep(&ts, nullptr); - gpio[GPIO_CLK_0] = 0x1 << pin; + gpio[GPIO_CLK_0] = 1 << pin; nanosleep(&ts, nullptr); gpio[GPIO_PUD] = 0; gpio[GPIO_CLK_0] = 0; diff --git a/cpp/buses/rpi_bus.h b/cpp/buses/rpi_bus.h index 65116e65..568625ca 100644 --- a/cpp/buses/rpi_bus.h +++ b/cpp/buses/rpi_bus.h @@ -49,7 +49,8 @@ class RpiBus final : public Bus void SetSEL(bool) override; - void SetIO(bool ast) override; + bool GetIO() override; + void SetIO(bool) override; uint8_t GetDAT() override; void SetDAT(uint8_t) override; @@ -169,9 +170,10 @@ class RpiBus final : public Bus constexpr static uint32_t IRPT_OFFSET = 0x0000B200; constexpr static uint32_t PADS_OFFSET = 0x00100000; + constexpr static uint32_t PADS_OFFSET_PI5 = 0x000f0000; constexpr static uint32_t GPIO_OFFSET = 0x00200000; constexpr static uint32_t GPIO_OFFSET_PI5 = 0x000d0000; constexpr static uint32_t QA7_OFFSET = 0x01000000; - constexpr static uint32_t PI4_ARM_GICC_BASE = 0xFF842000; + constexpr static uint32_t PI4_ARM_GICC_CTLR = 0xFF842000; };