Skip to content

Commit

Permalink
Refactoring, update offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Mar 13, 2024
1 parent 8ac8c71 commit 5055242
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
6 changes: 1 addition & 5 deletions cpp/buses/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions cpp/buses/in_process_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
86 changes: 62 additions & 24 deletions cpp/buses/rpi_bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down Expand Up @@ -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<uint32_t*>(mmap(nullptr, 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, PI4_ARM_GICC_BASE));
gicc_mpr = static_cast<uint32_t*>(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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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));
}

Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions cpp/buses/rpi_bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};

0 comments on commit 5055242

Please sign in to comment.