Skip to content

Commit

Permalink
Cleanup of some issues and expansion of some macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Vojtech Vosahlo committed Oct 7, 2024
1 parent 48d80d4 commit 761e654
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 41 deletions.
12 changes: 6 additions & 6 deletions adc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace stmcpp::adc {
((static_cast<uint8_t>(leftShift) & 0b1111) << ADC_CFGR2_LSHIFT_Pos) |
((static_cast<uint16_t>(oversampling) & 0x3FF) << ADC_CFGR2_OVSR_Pos) |
((static_cast<uint8_t>(rightShift) & 0b111) << ADC_CFGR2_OVSS_Pos) |
((static_cast<uint8_t>(regularOversampled) & 0b1) << ADC_CFGR2_ROVSE_Pos)
((static_cast<uint8_t>(regularOversampled) & 0b1) << ADC_CFGR2_ROVSE_Pos) |
((static_cast<uint8_t>(injectedOversampled) & 0b1) << ADC_CFGR2_JOVSE_Pos)
);
}
Expand Down Expand Up @@ -268,13 +268,13 @@ namespace stmcpp::adc {

for(int i = 0; i < sequence.size(); i++) {
if (i < 4){
reg::change(std::ref(adcHandle_->SQR1), 0b1111, ch.getNumber(), (i + 1) * 6);
reg::change(std::ref(adcHandle_->SQR1), 0b1111, sequence.at(i).getNumber(), (i + 1) * 6);
} else if (i < 9) {
reg::change(std::ref(adcHandle_->SQR2), 0b1111, ch.getNumber(), (i - 4) * 6);
reg::change(std::ref(adcHandle_->SQR2), 0b1111, sequence.at(i).getNumber(), (i - 4) * 6);
} else if (i < 14) {
reg::change(std::ref(adcHandle_->SQR3), 0b1111, ch.getNumber(), (i - 9) * 6);
reg::change(std::ref(adcHandle_->SQR3), 0b1111, sequence.at(i).getNumber(), (i - 9) * 6);
} else {
reg::change(std::ref(adcHandle_->SQR3), 0b1111, ch.getNumber(), (i - 14) * 6);
reg::change(std::ref(adcHandle_->SQR3), 0b1111, sequence.at(i).getNumber(), (i - 14) * 6);
}
}
}
Expand All @@ -293,7 +293,7 @@ namespace stmcpp::adc {
// Set up the number of channels in the sequence and trigger event
reg::write(std::ref(adcHandle_->JSQR), 0b11, sequence.size() | (triggerEvent << ADC_JSQR_JEXTSEL_Pos) | (static_cast<uint8_t>(edge) << ADC_JSQR_JEXTEN_Pos));
for(int i = 0; i < sequence.size(); i++) {
reg::change(std::ref(adcHandle_->SQR1), 0b1111, ch.getNumber(), (i * 6) + 9);
reg::change(std::ref(adcHandle_->SQR1), 0b1111, sequence.at(i).getNumber(), (i * 6) + 9);
}
}

Expand Down
47 changes: 35 additions & 12 deletions clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ namespace stmcpp::clock {

static_assert(M >= clock::pll::range_div_m.first && M <= clock::pll::range_div_m.second, "The M divider is out of range.");
static_assert(N >= clock::pll::range_div_n.first && N <= clock::pll::range_div_n.second, "The N divider is out of range.");
static_assert(P >= clock::pll::range_div_p.first && P <= clock::pll::range_div_p.second, "The P divider is out of range.");
static_assert(Q >= clock::pll::range_div_q.first && Q <= clock::pll::range_div_q.second, "The Q divider is out of range.");
static_assert(R >= clock::pll::range_div_r.first && R <= clock::pll::range_div_r.second, "The R divider is out of range.");
static_assert((P >= clock::pll::range_div_p.first && P <= clock::pll::range_div_p.second) || P == 0, "The P divider is out of range.");
static_assert((Q >= clock::pll::range_div_q.first && Q <= clock::pll::range_div_q.second) || Q == 0, "The Q divider is out of range.");
static_assert((R >= clock::pll::range_div_r.first && R <= clock::pll::range_div_r.second) || R == 0, "The R divider is out of range.");

static_assert(!((Peripheral == peripheral::pll1) && (P % 2)), "The P divider for PLL1 must be an even number!");

Expand All @@ -322,13 +322,21 @@ namespace stmcpp::clock {
static_cast<uint8_t>(inputRange) << RCC_PLLCFGR_PLL1RGE_Pos |
static_cast<uint8_t>(vcoRange) << RCC_PLLCFGR_PLL1VCOSEL_Pos
), 4 * getPllIdx_());

reg::write(std::ref(*pllCfgrAdd_), (N - 1) << RCC_PLL1DIVR_N1_Pos);

if constexpr (P != 0) {
reg::set(std::ref(*pllCfgrAdd_), (P - 1) << RCC_PLL1DIVR_P1_Pos);
} else stmcpp::reg::clear(std::ref(RCC->PLLCFGR), RCC_PLLCFGR_DIVP1EN, 3 * getPllIdx_());

if constexpr (Q != 0) {
reg::set(std::ref(*pllCfgrAdd_), (Q - 1) << RCC_PLL1DIVR_Q1_Pos);
} else stmcpp::reg::clear(std::ref(RCC->PLLCFGR), RCC_PLLCFGR_DIVQ1EN, 3 * getPllIdx_());

if constexpr (R != 0) {
reg::set(std::ref(*pllCfgrAdd_), (R - 1) << RCC_PLL1DIVR_R1_Pos);
} else stmcpp::reg::clear(std::ref(RCC->PLLCFGR), RCC_PLLCFGR_DIVR1EN, 3 * getPllIdx_());

reg::write(std::ref(*pllCfgrAdd_),
(N - 1) << RCC_PLL1DIVR_N1_Pos |
(P - 1) << RCC_PLL1DIVR_P1_Pos |
(Q - 1) << RCC_PLL1DIVR_Q1_Pos |
(R - 1) << RCC_PLL1DIVR_R1_Pos
);
};

void enable() const {
Expand All @@ -347,9 +355,9 @@ namespace stmcpp::clock {
}

namespace systick {
inline volatile static std::uint32_t ticks_ = 0;
static inline duration resolution_ = 1_ms;
static inline bool initialized_ = false;
inline volatile /*static*/ std::uint32_t ticks_ = 0;
inline /*static*/ duration resolution_ = 1_ms;
inline /*volatile static*/ bool initialized_ = false;

static void init(duration resolution = 1_ms) {
resolution_ = resolution;
Expand Down Expand Up @@ -386,6 +394,21 @@ namespace stmcpp::clock {
while(getDuration() < (timestamp_ + time)){;}
}

static void waitForTrue(bool condition, std::function<void()> onTimeout = nullptr, duration timeout = 1000_ms){
duration timestamp_ = stmcpp::clock::systick::getDuration();

while (stmcpp::clock::systick::getDuration() < (timestamp_ + timeout)) {
if(condition) {
return;
}
}

// Call the timeout handler if timeout occured
if(onTimeout){
onTimeout();
}
}

static inline void increment() {
++ticks_;
}
Expand Down
22 changes: 11 additions & 11 deletions dac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace stmcpp::dac {
noWave = 0b00,
noise = 0b01,
triangle = 0b10
}
};

enum class waveAmplitude {
bit1 = 0b0000,
Expand All @@ -81,25 +81,25 @@ namespace stmcpp::dac {
bit10 = 0b1001,
bit11 = 0b1010,
bit12 = 0b1011
}
};

enum class bufferMode {
externalBuffered = 0b000,
externalInternalBuffered = 0b001,
externalUnbuffered = 0b010,
internalUnuffered = 0b011
}
};

enum class mode {
normal = 0b0000,
sampleAndHold = 0b1000
}
};

enum class status {
busy = DAC_SR_BWST1,
calibration = DAC_SR_CAL_FLAG1,
underrun = DAC_SR_DMAUDR1
}
};

template<channel Channel>
class dac {
Expand All @@ -110,15 +110,15 @@ namespace stmcpp::dac {
reg::write(std::ref(DAC1->CR),
(static_cast<uint32_t>(triggerEnable) << DAC_CR_TEN1_Pos) |
(static_cast<uint32_t>(trigger) << DAC_CR_TSEL1_Pos) |
(static_cast<uint32_t>(waveGeneration) << DAC_CR_WAVE1_Pos) |
(static_cast<uint32_t>(waveAmplitude) << DAC_CR_MAMP1_Pos)
(static_cast<uint32_t>(generation) << DAC_CR_WAVE1_Pos) |
(static_cast<uint32_t>(amplitude) << DAC_CR_MAMP1_Pos)
);
} else {
reg::write(std::ref(DAC1->CR),
(static_cast<uint32_t>(triggerEnable) << DAC_CR_TEN2_Pos) |
(static_cast<uint32_t>(trigger) << DAC_CR_TSEL2_Pos) |
(static_cast<uint32_t>(waveGeneration) << DAC_CR_WAVE2_Pos) |
(static_cast<uint32_t>(waveAmplitude) << DAC_CR_MAMP2_Pos)
(static_cast<uint32_t>(generation) << DAC_CR_WAVE2_Pos) |
(static_cast<uint32_t>(amplitude) << DAC_CR_MAMP2_Pos)
);
}

Expand Down Expand Up @@ -171,8 +171,8 @@ namespace stmcpp::dac {

bool getStatusFlag(status flag) {
if constexpr (Channel == channel::ch1) {
return static_cast<bool>(reg::read(std::ref(DAC->SR), static_cast<std::uint32_t>(flag)));
} else return static_cast<bool>(reg::read(std::ref(DAC->SR), static_cast<std::uint32_t>(flag) << 16));
return static_cast<bool>(reg::read(std::ref(DAC1->SR), static_cast<std::uint32_t>(flag)));
} else return static_cast<bool>(reg::read(std::ref(DAC1->SR), static_cast<std::uint32_t>(flag) << 16));
}

void calibrate() {
Expand Down
26 changes: 22 additions & 4 deletions dma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ namespace stmcpp::dma{
bool doublebuffer = false, bool bufferedtransfers = false, flowController flowController = flowController::dma,
burstsize pburst = burstsize::single, burstsize mburst = burstsize::single
) {

reg::write(std::ref(streamHandle_->PAR), paddress);
reg::write(std::ref(streamHandle_->M0AR), m0address);
reg::write(std::ref(streamHandle_->M1AR), m1address);
reg::write(std::ref(streamHandle_->NDTR), numofdata);

reg::write(std::ref(streamHandle_->CR),
//Interrupts are not enabled here and the channel is not yet being enabled
((static_cast<std::uint8_t>(flowController) & 0b1) << DMA_SxCR_PFCTRL_Pos) |
Expand All @@ -138,10 +144,6 @@ namespace stmcpp::dma{
((static_cast<std::uint8_t>(mburst) & 0b11) << DMA_SxCR_MBURST_Pos)
);

reg::write(std::ref(streamHandle_->PAR), paddress);
reg::write(std::ref(streamHandle_->M0AR), m0address);
reg::write(std::ref(streamHandle_->M1AR), m1address);
reg::write(std::ref(streamHandle_->NDTR), numofdata);
}

void enable() const {
Expand All @@ -156,6 +158,14 @@ namespace stmcpp::dma{
reg::change(std::ref(streamHandle_->CR), 0x01, static_cast<std::uint8_t>(memory), DMA_SxCR_CT_Pos);
}

uint8_t getTargetMemory() const {
return reg::read(std::ref(streamHandle_->CR), 0x01, DMA_SxCR_CT_Pos);
}

void setNumberOfData(uint16_t numofdata) const {
reg::write(std::ref(streamHandle_->NDTR), numofdata);
}

void enableInterrupt(const std::vector<interrupt> interrupts) const {

std::uint8_t mask_ = 0;
Expand Down Expand Up @@ -339,6 +349,14 @@ namespace stmcpp::dma{
fifoStat getFifoStatus() const {
return static_cast<fifoStat>(reg::read(std::ref(streamHandle_->FCR), 0x07, DMA_SxFCR_FS_Pos));
}

void enableDoubleBuffer() const {
reg::set(std::ref(streamHandle_->CR), DMA_SxCR_DBM);
}

void disableDoubleBuffer() const {
reg::clear(std::ref(streamHandle_->CR), DMA_SxCR_DBM);
}
};
}

Expand Down
1 change: 1 addition & 0 deletions gpio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace stmcpp::gpio{
constexpr pin(gpio::mode mode, gpio::pull pull) : pin (mode, gpio::otype::pushPull, gpio::speed::low, pull) { }
constexpr pin(gpio::mode mode, gpio::otype otype, gpio::pull pull) : pin (mode, otype, gpio::speed::low, pull) { }
constexpr pin(gpio::mode mode, gpio::speed ospeed) : pin (mode, gpio::otype::pushPull, ospeed, gpio::pull::noPull) { }
constexpr pin(gpio::mode mode, gpio::speed ospeed, gpio::pull pull) : pin (mode, gpio::otype::pushPull, ospeed, pull) { }

void set() const {
reg::write(std::ref(gpioHandle_->BSRR), 0x00000001, Pin);
Expand Down
32 changes: 24 additions & 8 deletions register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,47 @@ namespace stmcpp::reg {
address.get() ^= (mask << bitshift);
}

template <typename A = regbase, typename M>
/*template <typename A = regbase, typename M>
constexpr void waitForBitState(std::reference_wrapper<A> address, M mask, bool state, std::function<void()> onTimeout = nullptr, duration timeout = 1000_ms) {
duration timestamp_ = stmcpp::clock::systick::getDuration();
while (stmcpp::clock::systick::getDuration() < (timestamp_ + timeout)) {
if(read(address, static_cast<regbase>(mask)) == state) {
if(static_cast<bool>(read(address, static_cast<regbase>(mask))) == state) {
return;
}

// Call the timeout handler if timeout occured
if(onTimeout){
onTimeout();
}
// Call the timeout handler if timeout occured
if(onTimeout){
onTimeout();
}
}*/

template <typename A = regbase, typename M, typename V>
constexpr void waitForBitsEqual(std::reference_wrapper<A> address, M mask, V value, std::function<void()> onTimeout = nullptr, duration timeout = 1000_ms) {
duration timestamp_ = stmcpp::clock::systick::getDuration();

while (stmcpp::clock::systick::getDuration() < (timestamp_ + timeout)) {
if(read(address, static_cast<regbase>(mask)) == static_cast<regbase>(value)) {
return;
}
}
// Call the timeout handler if timeout occured
if(onTimeout){
onTimeout();
}
}

template <typename A = regbase, typename M>
constexpr void waitForBitSet(std::reference_wrapper<A> address, M mask, std::function<void()> onTimeout = nullptr, duration timeout = 1000_ms) {
waitForBitState(address, mask, true, onTimeout, timeout);
waitForBitsEqual(address, mask, mask, onTimeout, timeout);
}

template <typename A = regbase, typename M>
constexpr void waitForBitClear(std::reference_wrapper<A> address, M mask, std::function<void()> onTimeout = nullptr, duration timeout = 1000_ms) {
waitForBitState(address, mask, true, onTimeout, timeout);
waitForBitsEqual(address, mask, 0, onTimeout, timeout);
}


}

#endif
16 changes: 16 additions & 0 deletions spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ namespace stmcpp::spi {
reg::clear(std::ref(spiHandle_->CFG1), SPI_CFG1_TXDMAEN);
}

void enableIoSwap () const {
reg::set(std::ref(spiHandle_->CFG2), SPI_CFG2_IOSWP);
}

void disableIoSwap () const {
reg::clear(std::ref(spiHandle_->CFG2), SPI_CFG2_IOSWP);
}

void enableSoftwareSS () const {
reg::set(std::ref(spiHandle_->CFG2), SPI_CFG2_SSM);
}

void disableSoftwareSS () const {
reg::clear(std::ref(spiHandle_->CFG2), SPI_CFG2_SSM);
}

void enableInterrupt(interrupt interrupt) const {
//All these three interrupts are affected by this bit
if (interrupt == interrupt::endOfTransfer || interrupt == interrupt::suspend || interrupt == interrupt::txComplete) {
Expand Down

0 comments on commit 761e654

Please sign in to comment.