31 #include "generated/airframe.h"
33 #include <libopencm3/stm32/rcc.h>
34 #include <libopencm3/stm32/gpio.h>
35 #include <libopencm3/stm32/timer.h>
36 #include <libopencm3/cm3/nvic.h>
44 #define ONE_MHZ_CLK 1000000
45 #ifdef NVIC_TIM_IRQ_PRIO
46 #define PWM_INPUT_IRQ_PRIO NVIC_TIM_IRQ_PRIO
48 #define PWM_INPUT_IRQ_PRIO 2
53 timer_set_mode(tim, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
54 timer_set_period(tim, 0xFFFF);
56 timer_set_prescaler(tim, (timer_clk / (ticks_per_usec *
ONE_MHZ_CLK)) - 1);
57 timer_enable_counter(tim);
76 #if USE_PWM_INPUT_TIM1
77 rcc_periph_clock_enable(RCC_TIM1);
78 rcc_periph_reset_pulse(RST_TIM1);
81 #if USE_PWM_INPUT_TIM2
82 rcc_periph_clock_enable(RCC_TIM2);
83 rcc_periph_reset_pulse(RST_TIM2);
86 #if USE_PWM_INPUT_TIM3
87 rcc_periph_clock_enable(RCC_TIM3);
88 rcc_periph_reset_pulse(RST_TIM3);
91 #if USE_PWM_INPUT_TIM4
92 rcc_periph_clock_enable(RCC_TIM4);
93 rcc_periph_reset_pulse(RST_TIM4);
96 #if USE_PWM_INPUT_TIM5
97 rcc_periph_clock_enable(RCC_TIM5);
98 rcc_periph_reset_pulse(RST_TIM5);
101 #if USE_PWM_INPUT_TIM8
102 rcc_periph_clock_enable(RCC_TIM8);
103 rcc_periph_reset_pulse(RST_TIM8);
106 #if USE_PWM_INPUT_TIM9
107 rcc_periph_clock_enable(RCC_TIM9);
108 rcc_periph_reset_pulse(RST_TIM9);
112 #ifdef USE_PWM_INPUT1
121 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
124 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
137 #ifdef PWM_INPUT1_IRQ2
150 #ifdef USE_PWM_INPUT2
159 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
162 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
175 #ifdef PWM_INPUT2_IRQ2
177 nvic_enable_irq(PWM_INPUT2_IRQ2);
191 #if USE_PWM_INPUT_TIM1
194 void tim1_up_isr(
void)
196 #elif defined(STM32F4)
197 void tim1_up_tim10_isr(
void) {
199 if ((TIM1_SR & TIM_SR_UIF) != 0) {
200 timer_clear_flag(TIM1, TIM_SR_UIF);
205 void tim1_cc_isr(
void) {
220 #if USE_PWM_INPUT_TIM2
222 void tim2_isr(
void) {
223 if ((TIM2_SR & TIM2_CC_IF_PERIOD) != 0) {
224 timer_clear_flag(TIM2, TIM2_CC_IF_PERIOD);
228 if ((TIM2_SR & TIM2_CC_IF_DUTY) != 0) {
229 timer_clear_flag(TIM2, TIM2_CC_IF_DUTY);
233 if ((TIM2_SR & TIM_SR_UIF) != 0) {
234 timer_clear_flag(TIM2, TIM_SR_UIF);
241 #if USE_PWM_INPUT_TIM3
243 void tim3_isr(
void) {
244 if ((TIM3_SR & TIM3_CC_IF_PERIOD) != 0) {
245 timer_clear_flag(TIM3, TIM3_CC_IF_PERIOD);
249 if ((TIM3_SR & TIM3_CC_IF_DUTY) != 0) {
250 timer_clear_flag(TIM3, TIM3_CC_IF_DUTY);
254 if ((TIM3_SR & TIM_SR_UIF) != 0) {
255 timer_clear_flag(TIM3, TIM_SR_UIF);
262 #if USE_PWM_INPUT_TIM4
264 void tim4_isr(
void) {
265 if ((TIM4_SR & TIM4_CC_IF_PERIOD) != 0) {
266 timer_clear_flag(TIM4, TIM4_CC_IF_PERIOD);
270 if ((TIM4_SR & TIM4_CC_IF_DUTY) != 0) {
271 timer_clear_flag(TIM4, TIM4_CC_IF_DUTY);
275 if ((TIM4_SR & TIM_SR_UIF) != 0) {
276 timer_clear_flag(TIM4, TIM_SR_UIF);
283 #if USE_PWM_INPUT_TIM5
285 void tim5_isr(
void) {
286 if ((TIM5_SR & TIM5_CC_IF_PERIOD) != 0) {
287 timer_clear_flag(TIM5, TIM5_CC_IF_PERIOD);
291 if ((TIM5_SR & TIM5_CC_IF_DUTY) != 0) {
292 timer_clear_flag(TIM5, TIM5_CC_IF_DUTY);
296 if ((TIM5_SR & TIM_SR_UIF) != 0) {
297 timer_clear_flag(TIM5, TIM_SR_UIF);
304 #if USE_PWM_INPUT_TIM8
307 void tim8_up_isr(
void)
309 #elif defined(STM32F4)
310 void tim8_up_tim13_isr(
void) {
312 if ((TIM8_SR & TIM_SR_UIF) != 0) {
313 timer_clear_flag(TIM8, TIM_SR_UIF);
318 void tim8_cc_isr(
void) {
319 if ((TIM8_SR & TIM8_CC_IF_PERIOD) != 0) {
320 timer_clear_flag(TIM8, TIM8_CC_IF_PERIOD);
324 if ((TIM8_SR & TIM8_CC_IF_DUTY) != 0) {
325 timer_clear_flag(TIM8, TIM8_CC_IF_DUTY);
333 #if USE_PWM_INPUT_TIM9
336 void tim1_brk_tim9_isr(
void) {
347 if ((TIM9_SR & TIM_SR_UIF) != 0) {
348 timer_clear_flag(TIM9, TIM_SR_UIF);
#define PWM_INPUT2_GPIO_PORT
#define PWM_INPUT2_GPIO_AF
#define PWM_INPUT2_GPIO_PIN
#define PWM_INPUT1_TIMER_INPUT
#define TIM9_PWM_INPUT_IDX
#define PWM_INPUT2_SLAVE_TRIG
#define TIM1_CC_IF_PERIOD
#define TIM9_CC_IF_PERIOD
#define PWM_INPUT1_SLAVE_TRIG
#define PWM_INPUT1_CHANNEL_PERIOD
#define TIM1_PWM_INPUT_IDX
#define PWM_INPUT1_CHANNEL_DUTY
#define PWM_INPUT2_CHANNEL_DUTY
#define PWM_INPUT2_CHANNEL_PERIOD
#define PWM_INPUT2_TIMER_INPUT
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
#define PWM_INPUT1_GPIO_PORT
#define PWM_INPUT1_GPIO_AF
#define PWM_INPUT1_GPIO_PIN
Some architecture independent helper functions for GPIOs.
uint32_t timer_get_frequency(uint32_t timer_peripheral)
Get Timer clock frequency (before prescaling) Only valid if using the internal clock for the timer.
Architecture independent timing functions.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.