31 extern uint8_t TIMER_IC_PRIORITY;
106 float pwmIn(uint8_t pin);
119 void pwmInRaw(uint8_t name,
int *period,
int *pulseWidth);
124 void timerInit(uint8_t timer,
int freqHz);
126 void pinTimerInit(uint8_t pin);
129 void timerInitHelper(uint8_t timer, uint16_t prescaler, uint32_t period);
134 static inline void timerCCxISR(TIM_TypeDef *TIMx, TimerChannelData *C,
int current, uint32_t currRollover)
__attribute__((always_inline, unused));
135 static inline void timerCCxISR(TIM_TypeDef *TIMx, TimerChannelData *C,
int current, uint32_t currRollover) {
137 if (C->bPwmIn == 1) {
139 int newRollovers = currRollover - C->lastRollover;
140 int delta = current + TIMx->ARR * newRollovers - C->risingEdge;
144 if (delta > PWM_IN_MAXPERIOD)
145 C->period = delta - TIMx->ARR;
146 else if (delta < PWM_IN_MINPERIOD)
147 C->period = delta + TIMx->ARR;
151 C->risingEdge = current;
152 C->lastRollover = currRollover;
155 C->pulseWidth = delta;
158 if (C->pulseWidth > C->period)
159 C->pulseWidth -= C->period;
160 if (C->pulseWidth < 0)
161 C->pulseWidth += C->period;
167 #if defined(KODUINO_ISRS_INLINE)
168 static inline void timerISR(uint8_t timer)
__attribute__((always_inline, unused));
170 static inline void timerISR(uint8_t timer);
172 static inline void timerISR(uint8_t timer) {
173 TIM_TypeDef *TIMx = TIMER_MAP[timer].TIMx;
174 TimerInfo *cfg = &TIMER_MAP[timer];
177 if (TIM_GetITStatus(TIMx, TIM_IT_Update) != RESET) {
178 TIM_ClearITPendingBit(TIMx, TIM_IT_Update);
183 if (TIM_GetITStatus(TIMx, TIM_IT_CC1) != RESET) {
184 TIM_ClearITPendingBit(TIMx, TIM_IT_CC1);
185 timerCCxISR(TIMx, &cfg->channelData[0], TIM_GetCapture1(TIMx), cfg->numRollovers);
187 if (TIM_GetITStatus(TIMx, TIM_IT_CC2) != RESET) {
188 TIM_ClearITPendingBit(TIMx, TIM_IT_CC2);
189 timerCCxISR(TIMx, &cfg->channelData[1], TIM_GetCapture2(TIMx), cfg->numRollovers);
191 if (TIM_GetITStatus(TIMx, TIM_IT_CC3) != RESET) {
192 TIM_ClearITPendingBit(TIMx, TIM_IT_CC3);
193 timerCCxISR(TIMx, &cfg->channelData[2], TIM_GetCapture3(TIMx), cfg->numRollovers);
195 if (TIM_GetITStatus(TIMx, TIM_IT_CC4) != RESET) {
196 TIM_ClearITPendingBit(TIMx, TIM_IT_CC4);
197 timerCCxISR(TIMx, &cfg->channelData[3], TIM_GetCapture4(TIMx), cfg->numRollovers);
203 void complementaryPWM(uint8_t timer,
int freqHz, uint16_t deadtime);
208 #endif // __cplusplus
float pwmIn(uint8_t pin)
Read a PWM signal (non-blocking)
Definition: timer.c:129
static uint8_t digitalRead(uint8_t pin) __attribute__((always_inline
Read a digital pin.
Definition: gpio.h:57
void analogWriteFloat(uint8_t pin, float duty)
Change the PWM duty cycle on a pin.
Definition: timer.c:106
void analogWriteResolution(uint8_t nbits)
Set the resolution for the analogWrite() function.
Definition: timer.c:69
void analogWrite(uint8_t pin, uint32_t duty)
Change the PWM duty cycle on a pin.
Definition: timer.c:125
void pwmInExpectedPeriod(int expectedUs)
Set the expected period for PWM_IN or PWM_IN_EXTI.
Definition: timer.c:40
void analogWriteFrequency(uint8_t pin, int freqHz)
Assign a PWM frequency to the specified pin (default = 1000 Hz)
Definition: timer.c:65