Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
build
build_test
*.log
_codeql_build_dir
_codeql_detected_source_root
13 changes: 12 additions & 1 deletion include/rpl4/peripheral/aux_spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@

namespace rpl {

class AuxSpi : public SpiBase {
// Forward declaration for factory
class AuxSpi;

// Helper struct to enable make_shared with private constructor
struct AuxSpiFactory {
static std::shared_ptr<AuxSpi> Create(AuxSpiRegisterMap* register_map);
};

class AuxSpi : public SpiBase, public std::enable_shared_from_this<AuxSpi> {
public:
enum class Port : size_t {
kAuxSpi1 = 0,
Expand Down Expand Up @@ -229,6 +237,9 @@ class AuxSpi : public SpiBase {

void ConfigureDataShiftTx();
void ConfigureDataShiftRx();

// Allow factory struct to access private constructor
friend struct AuxSpiFactory;
};

} // namespace rpl
Expand Down
10 changes: 9 additions & 1 deletion include/rpl4/peripheral/dma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace rpl {

class Dma {
class Dma : public std::enable_shared_from_this<Dma> {
public:
enum class Channel : size_t {
kChannel0 = 0,
Expand Down Expand Up @@ -191,6 +191,14 @@ class Dma {

DmaRegisterMap* register_map_;
Channel channel_;

// Allow factory struct to access private constructor
friend struct DmaFactory;
};

// Helper struct to enable make_shared with private constructor
struct DmaFactory {
static std::shared_ptr<Dma> Create(DmaRegisterMap* register_map, Dma::Channel channel);
};

} // namespace rpl
Expand Down
13 changes: 12 additions & 1 deletion include/rpl4/peripheral/gpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

namespace rpl {

class Gpio {
// Forward declaration for factory
class Gpio;

// Helper struct to enable make_shared with private constructor
struct GpioFactory {
static std::shared_ptr<Gpio> Create(uint8_t pin);
};

class Gpio : public std::enable_shared_from_this<Gpio> {
public:
/**
* @brief Resistor Select
Expand Down Expand Up @@ -109,6 +117,9 @@ class Gpio {
static std::array<std::shared_ptr<Gpio>, kNumOfInstances> instances_;

uint8_t pin_;

// Allow factory struct to access private constructor
friend struct GpioFactory;
};

} // namespace rpl
Expand Down
10 changes: 9 additions & 1 deletion include/rpl4/peripheral/pwm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace rpl {

class Pwm {
class Pwm : public std::enable_shared_from_this<Pwm> {
public:
enum class Port : size_t {
kPwm0 = 0,
Expand Down Expand Up @@ -216,6 +216,14 @@ class Pwm {
Port port_;
double clock_frequency_;
static constexpr double kDefaultClockFrequency = 25000000.0; // 25 MHz

// Allow factory struct to access private constructor
friend struct PwmFactory;
};

// Helper struct to enable make_shared with private constructor
struct PwmFactory {
static std::shared_ptr<Pwm> Create(PwmRegisterMap* register_map, Pwm::Port port);
};

} // namespace rpl
Expand Down
13 changes: 12 additions & 1 deletion include/rpl4/peripheral/spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

namespace rpl {

class Spi : public SpiBase {
// Forward declaration for factory
class Spi;

// Helper struct to enable make_shared with private constructor
struct SpiFactory {
static std::shared_ptr<Spi> Create(SpiRegisterMap* register_map);
};

class Spi : public SpiBase, public std::enable_shared_from_this<Spi> {
public:
enum class Port : size_t {
kSpi0 = 0,
Expand Down Expand Up @@ -221,6 +229,9 @@ class Spi : public SpiBase {
static std::array<std::shared_ptr<Spi>, kNumOfInstances> instances_;

SpiRegisterMap* register_map_;

// Allow factory struct to access private constructor
friend struct SpiFactory;
};

} // namespace rpl
Expand Down
13 changes: 9 additions & 4 deletions src/peripheral/aux_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

namespace rpl {

std::shared_ptr<AuxSpi> AuxSpiFactory::Create(AuxSpiRegisterMap* register_map) {
struct EnableMakeShared : public AuxSpi {
explicit EnableMakeShared(AuxSpiRegisterMap* reg_map) : AuxSpi(reg_map) {}
};
return std::make_shared<EnableMakeShared>(register_map);
}

std::array<std::shared_ptr<AuxSpi>, AuxSpi::kNumOfInstances>
AuxSpi::instances_ = {nullptr};

Expand All @@ -17,12 +24,10 @@ std::shared_ptr<AuxSpi> AuxSpi::GetInstance(Port port) {
} else if (instances_[static_cast<size_t>(port)] == nullptr) {
switch (port) {
case Port::kAuxSpi1:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<AuxSpi>(new AuxSpi(REG_SPI1));
instances_[static_cast<size_t>(port)] = AuxSpiFactory::Create(REG_SPI1);
break;
case Port::kAuxSpi2:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<AuxSpi>(new AuxSpi(REG_SPI2));
instances_[static_cast<size_t>(port)] = AuxSpiFactory::Create(REG_SPI2);
break;
default:
Log(LogLevel::Fatal,
Expand Down
9 changes: 8 additions & 1 deletion src/peripheral/dma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

namespace rpl {

std::shared_ptr<Dma> DmaFactory::Create(DmaRegisterMap* register_map, Dma::Channel channel) {
struct EnableMakeShared : public Dma {
EnableMakeShared(DmaRegisterMap* reg_map, Dma::Channel ch) : Dma(reg_map, ch) {}
};
return std::make_shared<EnableMakeShared>(register_map, channel);
}

std::array<std::shared_ptr<Dma>, Dma::kNumOfInstances> Dma::instances_ = {
nullptr};

Expand Down Expand Up @@ -69,7 +76,7 @@ std::shared_ptr<Dma> Dma::GetInstance(Channel channel) {
reg_map = REG_DMA14;
break;
}
instances_[index] = std::shared_ptr<Dma>(new Dma(reg_map, channel));
instances_[index] = DmaFactory::Create(reg_map, channel);
}
return instances_[index];
}
Expand Down
9 changes: 8 additions & 1 deletion src/peripheral/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

namespace rpl {

std::shared_ptr<Gpio> GpioFactory::Create(uint8_t pin) {
struct EnableMakeShared : public Gpio {
explicit EnableMakeShared(uint8_t p) : Gpio(p) {}
};
return std::make_shared<EnableMakeShared>(pin);
}

std::array<std::shared_ptr<Gpio>, Gpio::kNumOfInstances> Gpio::instances_ = {
nullptr};

Expand All @@ -17,7 +24,7 @@ std::shared_ptr<Gpio> Gpio::GetInstance(uint8_t pin) {
if (!IsInitialized()) {
Log(LogLevel::Error, "[Gpio::GetInstance()] RPL is not initialized.");
} else if (instances_[static_cast<size_t>(pin)] == nullptr) {
instances_[static_cast<size_t>(pin)] = std::shared_ptr<Gpio>(new Gpio(pin));
instances_[static_cast<size_t>(pin)] = GpioFactory::Create(pin);
}
return instances_[static_cast<size_t>(pin)];
}
Expand Down
9 changes: 8 additions & 1 deletion src/peripheral/pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

namespace rpl {

std::shared_ptr<Pwm> PwmFactory::Create(PwmRegisterMap* register_map, Pwm::Port port) {
struct EnableMakeShared : public Pwm {
EnableMakeShared(PwmRegisterMap* reg_map, Pwm::Port p) : Pwm(reg_map, p) {}
};
return std::make_shared<EnableMakeShared>(register_map, port);
}

std::array<std::shared_ptr<Pwm>, Pwm::kNumOfInstances> Pwm::instances_ = {
nullptr};

Expand All @@ -28,7 +35,7 @@ std::shared_ptr<Pwm> Pwm::GetInstance(Port port) {
reg_map = REG_PWM1;
break;
}
instances_[index] = std::shared_ptr<Pwm>(new Pwm(reg_map, port));
instances_[index] = PwmFactory::Create(reg_map, port);
}
return instances_[index];
}
Expand Down
22 changes: 12 additions & 10 deletions src/peripheral/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

namespace rpl {

std::shared_ptr<Spi> SpiFactory::Create(SpiRegisterMap* register_map) {
struct EnableMakeShared : public Spi {
explicit EnableMakeShared(SpiRegisterMap* reg_map) : Spi(reg_map) {}
};
return std::make_shared<EnableMakeShared>(register_map);
}

std::array<std::shared_ptr<Spi>, Spi::kNumOfInstances> Spi::instances_ = {
nullptr};

Expand All @@ -17,24 +24,19 @@ std::shared_ptr<Spi> Spi::GetInstance(Port port) {
} else if (instances_[static_cast<size_t>(port)] == nullptr) {
switch (port) {
case Port::kSpi0:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<Spi>(new Spi(REG_SPI0));
instances_[static_cast<size_t>(port)] = SpiFactory::Create(REG_SPI0);
break;
case Port::kSpi3:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<Spi>(new Spi(REG_SPI3));
instances_[static_cast<size_t>(port)] = SpiFactory::Create(REG_SPI3);
break;
case Port::kSpi4:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<Spi>(new Spi(REG_SPI4));
instances_[static_cast<size_t>(port)] = SpiFactory::Create(REG_SPI4);
break;
case Port::kSpi5:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<Spi>(new Spi(REG_SPI5));
instances_[static_cast<size_t>(port)] = SpiFactory::Create(REG_SPI5);
break;
case Port::kSpi6:
instances_[static_cast<size_t>(port)] =
std::shared_ptr<Spi>(new Spi(REG_SPI6));
instances_[static_cast<size_t>(port)] = SpiFactory::Create(REG_SPI6);
break;
default:
Log(LogLevel::Fatal,
Expand Down