Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Kuker committed Sep 10, 2022
1 parent 4bf0416 commit 0134b81
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 92 deletions.
26 changes: 13 additions & 13 deletions src/raspberrypi/hal/gpiobus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ bool GPIOBUS::Init(mode_e mode)
pads = (DWORD *)map;
pads += PADS_OFFSET / sizeof(DWORD);

// System timer
SysTimer::Init(
(DWORD *)map + SYST_OFFSET / sizeof(DWORD),
(DWORD *)map + ARMT_OFFSET / sizeof(DWORD));
// // System timer
// SysTimer::Init(
// (DWORD *)map + SYST_OFFSET / sizeof(DWORD),
// (DWORD *)map + ARMT_OFFSET / sizeof(DWORD));

// Interrupt controller
irpctl = (DWORD *)map;
Expand Down Expand Up @@ -705,7 +705,7 @@ int GPIOBUS::CommandHandShake(BYTE *buf)
bool ret = WaitSignal(PIN_ACK, TRUE);

// Wait until the signal line stabilizes
SysTimer::instance.SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);
SysTimer::instance().SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);

// Get data
*buf = GetDAT();
Expand Down Expand Up @@ -742,7 +742,7 @@ int GPIOBUS::CommandHandShake(BYTE *buf)

ret = WaitSignal(PIN_ACK, TRUE);

SysTimer::instance.SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);
SysTimer::instance().SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);

// Get the actual SCSI command
*buf = GetDAT();
Expand Down Expand Up @@ -776,7 +776,7 @@ int GPIOBUS::CommandHandShake(BYTE *buf)
ret = WaitSignal(PIN_ACK, TRUE);

// Wait until the signal line stabilizes
SysTimer::instance.SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);
SysTimer::instance().SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);

// Get data
*buf = GetDAT();
Expand Down Expand Up @@ -827,7 +827,7 @@ int GPIOBUS::ReceiveHandShake(BYTE *buf, int count)
bool ret = WaitSignal(PIN_ACK, TRUE);

// Wait until the signal line stabilizes
SysTimer::instance.SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);
SysTimer::instance().SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);

// Get data
*buf = GetDAT();
Expand Down Expand Up @@ -870,7 +870,7 @@ int GPIOBUS::ReceiveHandShake(BYTE *buf, int count)
}

// Wait until the signal line stabilizes
SysTimer::instance.SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);
SysTimer::instance()->SleepNsec(SCSI_DELAY_BUS_SETTLE_DELAY_NS);

// Get data
*buf = GetDAT();
Expand Down Expand Up @@ -922,7 +922,7 @@ int GPIOBUS::SendHandShake(BYTE *buf, int count, int delay_after_bytes)
for (i = 0; i < count; i++) {
if(i==delay_after_bytes){
LOGTRACE("%s DELAYING for %dus after %d bytes", __PRETTY_FUNCTION__, SCSI_DELAY_SEND_DATA_DAYNAPORT_US, (int)delay_after_bytes)
SysTimer::instance.SleepUsec(SCSI_DELAY_SEND_DATA_DAYNAPORT_US);
SysTimer::instance()->SleepUsec(SCSI_DELAY_SEND_DATA_DAYNAPORT_US);
}

// Set the DATA signals
Expand Down Expand Up @@ -965,7 +965,7 @@ int GPIOBUS::SendHandShake(BYTE *buf, int count, int delay_after_bytes)
for (i = 0; i < count; i++) {
if(i==delay_after_bytes){
LOGTRACE("%s DELAYING for %dus after %d bytes", __PRETTY_FUNCTION__, SCSI_DELAY_SEND_DATA_DAYNAPORT_US, (int)delay_after_bytes)
SysTimer::instance.SleepUsec(SCSI_DELAY_SEND_DATA_DAYNAPORT_US);
SysTimer::instance()->SleepUsec(SCSI_DELAY_SEND_DATA_DAYNAPORT_US);
}

// Set the DATA signals
Expand Down Expand Up @@ -1252,7 +1252,7 @@ void GPIOBUS::SetSignal(int pin, bool ast)
bool GPIOBUS::WaitSignal(int pin, BOOL ast)
{
// Get current time
DWORD now = SysTimer::GetTimerLow();
DWORD now = SysTimer::instance()->GetTimerLow();

// Calculate timeout (3000ms)
DWORD timeout = 3000 * 1000;
Expand All @@ -1269,7 +1269,7 @@ bool GPIOBUS::WaitSignal(int pin, BOOL ast)
if (((signals >> pin) ^ ~ast) & 1) {
return true;
}
} while ((SysTimer::GetTimerLow() - now) < timeout);
} while ((SysTimer::instance()->GetTimerLow() - now) < timeout);

// We timed out waiting for the signal
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/raspberrypi/hal/sbc_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void SBC_Version::Init(){
str_buffer << input_stream.rdbuf();
device_tree_model = str_buffer.str();

for (const auto& [key, value] : m_device_model_map) {
for (const auto& [key, value] : m_proc_device_tree_mapping) {
if(device_tree_model.rfind(key,0) == 0){
m_sbc_version = value;
LOGINFO("Detected device %s", GetString()->c_str());
Expand Down
79 changes: 31 additions & 48 deletions src/raspberrypi/hal/systimer_allwinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//
//---------------------------------------------------------------------------

#include "hal/systimer.h"
#include "hal/systimer_allwinner.h"
#include <sys/mman.h>

#include "os.h"
Expand All @@ -20,66 +20,49 @@
#include "config.h"
#include "log.h"


ControllerManager& ControllerManager::instance()
{
// If we haven't set up the private instance yet, do that now
if(private_instance == nullptr){
if(SBC_Version::IsRaspberryPi()){
private_instance = std::make_shared<SysTimer_Raspberry>();
private_instance->Init();
}
else if(SBC_Version::IsBananaPi()){
private_instance = std::make_shared<SysTimer_AllWinner>();
private_instance->Init();
}
}
return private_instance;
}

//---------------------------------------------------------------------------
//
// Initialize the system timer
//
//---------------------------------------------------------------------------
void SysTimer::Init(DWORD *syst, DWORD *armt)
void SysTimer_AllWinner::Init()
{
// RPI Mailbox property interface
// Get max clock rate
// Tag: 0x00030004
//
// Request: Length: 4
// Value: u32: clock id
// Response: Length: 8
// Value: u32: clock id, u32: rate (in Hz)
//
// Clock id
// 0x000000004: CORE
DWORD maxclock[32] = { 32, 0, 0x00030004, 8, 0, 4, 0, 0 };

// Save the base address
systaddr = syst;
armtaddr = armt;

// Change the ARM timer to free run mode
armtaddr[ARMT_CTRL] = 0x00000282;

// Get the core frequency
corefreq = 0;
int fd = open("/dev/vcio", O_RDONLY);
if (fd >= 0) {
ioctl(fd, _IOWR(100, 0, char *), maxclock);
corefreq = maxclock[6] / 1000000;
}
close(fd);
// //
// // Request: Length: 4
// // Value: u32: clock id
// // Response: Length: 8
// // Value: u32: clock id, u32: rate (in Hz)
// //
// // Clock id
// // 0x000000004: CORE
// DWORD maxclock[32] = { 32, 0, 0x00030004, 8, 0, 4, 0, 0 };

// // Save the base address
// systaddr = syst;
// armtaddr = armt;

// // Change the ARM timer to free run mode
// armtaddr[ARMT_CTRL] = 0x00000282;

// // Get the core frequency
// corefreq = 0;
// int fd = open("/dev/vcio", O_RDONLY);
// if (fd >= 0) {
// ioctl(fd, _IOWR(100, 0, char *), maxclock);
// corefreq = maxclock[6] / 1000000;
// }
// close(fd);
}

//---------------------------------------------------------------------------
//
// Get system timer low byte
//
//---------------------------------------------------------------------------
DWORD SysTimer::GetTimerLow() {
DWORD SysTimer_AllWinner::GetTimerLow() {
return systaddr[SYST_CLO];
}

Expand All @@ -88,7 +71,7 @@ DWORD SysTimer::GetTimerLow() {
// Get system timer high byte
//
//---------------------------------------------------------------------------
DWORD SysTimer::GetTimerHigh() {
DWORD SysTimer_AllWinner::GetTimerHigh() {
return systaddr[SYST_CHI];
}

Expand All @@ -97,7 +80,7 @@ DWORD SysTimer::GetTimerHigh() {
// Sleep in nanoseconds
//
//---------------------------------------------------------------------------
void SysTimer::instance.SleepNsec(DWORD nsec)
void SysTimer_AllWinner::SleepNsec(DWORD nsec)
{
// If time is 0, don't do anything
if (nsec == 0) {
Expand All @@ -124,7 +107,7 @@ void SysTimer::instance.SleepNsec(DWORD nsec)
// Sleep in microseconds
//
//---------------------------------------------------------------------------
void SysTimer::instance.SleepUsec(DWORD usec)
void SysTimer_AllWinner::SleepUsec(DWORD usec)
{
// If time is 0, don't do anything
if (usec == 0) {
Expand Down
10 changes: 5 additions & 5 deletions src/raspberrypi/hal/systimer_allwinner.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
class SysTimer_AllWinner : SysTimer
{
public:
override void Init(uint32_t *syst, uint32_t *armt);
void Init();
// Initialization
override uint32_t GetTimerLow();
uint32_t GetTimerLow();
// Get system timer low byte
override uint32_t GetTimerHigh();
uint32_t GetTimerHigh();
// Get system timer high byte
override void SleepNsec(uint32_t nsec);
void SleepNsec(uint32_t nsec);
// Sleep for N nanoseconds
override void SleepUsec(uint32_t usec);
void SleepUsec(uint32_t usec);
// Sleep for N microseconds

private:
Expand Down
29 changes: 10 additions & 19 deletions src/raspberrypi/hal/systimer_raspberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,22 @@
//
//---------------------------------------------------------------------------

#include "hal/systimer.h"
#include "hal/systimer_raspberry.h"
#include <sys/mman.h>
#include <memory>

#include "os.h"
#include "hal/gpiobus.h"
#include "hal/sbc_version.h"

#include "config.h"
#include "log.h"


ControllerManager& ControllerManager::instance()
{
// If we haven't set up the private instance yet, do that now
if(private_instance == nullptr){
if(SBC_Version::IsRaspberryPi()){
private_instance = std::make_shared<SysTimer_Raspberry>();
private_instance->Init();
}
else if(SBC_Version::IsBananaPi()){
private_instance = std::make_shared<SysTimer_AllWinner>();
private_instance->Init();
}
}
return private_instance;
}
static volatile uint32_t *systaddr = 0;
// System timer address
static volatile uint32_t *armtaddr = 0;
// ARM timer address
static volatile uint32_t corefreq = 0;

//---------------------------------------------------------------------------
//
Expand All @@ -56,7 +47,7 @@ void SysTimer_Raspberry::Init()
}

// Map peripheral region memory
map = mmap(NULL, 0x1000100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, baseaddr);
void *map = mmap(NULL, 0x1000100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, baseaddr);
if (map == MAP_FAILED) {
LOGERROR("Error: Unable to map memory")
close(fd);
Expand All @@ -78,7 +69,7 @@ void SysTimer_Raspberry::Init()

// Save the base address
systaddr = (DWORD *)map + SYST_OFFSET / sizeof(DWORD);
armtaddr = (DWORD *)map + ARMT_OFFSET / sizeof(DWORD));
armtaddr = (DWORD *)map + ARMT_OFFSET / sizeof(DWORD);

// Change the ARM timer to free run mode
armtaddr[ARMT_CTRL] = 0x00000282;
Expand Down
12 changes: 6 additions & 6 deletions src/raspberrypi/hal/systimer_raspberry.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
class SysTimer_Raspberry : SysTimer
{
public:
override void Init(uint32_t *syst, uint32_t *armt);
void Init();
// Initialization
override uint32_t GetTimerLow();
uint32_t GetTimerLow();
// Get system timer low byte
override uint32_t GetTimerHigh();
uint32_t GetTimerHigh();
// Get system timer high byte
override void SleepNsec(uint32_t nsec);
void SleepNsec(uint32_t nsec);
// Sleep for N nanoseconds
override void SleepUsec(uint32_t usec);
void SleepUsec(uint32_t usec);
// Sleep for N microseconds

private:
static volatile uint32_t *systaddr;
// System timer address
static volatile uint32_t *armtaddr;
// ARM timer address
static volatile uint32_t corefreq = 0;
static volatile uint32_t corefreq;
// Core frequency
};

0 comments on commit 0134b81

Please sign in to comment.