RISC-V/V203F6P6/blink/pwmclass.h

36 lines
974 B
C
Raw Permalink Normal View History

2025-01-29 15:29:40 +01:00
#ifndef PWMCLASS_H
#define PWMCLASS_H
#include "system.h"
#include "oneway.h"
static constexpr unsigned MAXPWM = 6000u;
/* Používá TIM2, PWM kanál 3, DMA1 kanál 2, přerušení DMA1_Channel2_IRQHandler */
class PwmClass {
static constexpr unsigned HALF_LEN = 24u;
static constexpr unsigned FULL_LEN = 2u * HALF_LEN;
volatile unsigned count;
uint16_t * const pL;
uint16_t * const pH;
uint16_t buffer [FULL_LEN];
OneWay<uint16_t> * src;
public:
explicit PwmClass () noexcept;
void attach (OneWay<uint16_t> & s) { src = & s; }
void send (const bool b) {
if (!src) return;
if (b) src->Send (pH, HALF_LEN);
else src->Send (pL, HALF_LEN);
if (count) count -= 1u;
}
void delay (const unsigned frames = 50u) {
count = frames;
while (count) {
asm volatile ("nop");
}
}
protected:
void DmaInit () noexcept;
void TimInit () noexcept;
};
#endif // PWMCLASS_H