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
59 timer_set_mode(tim, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
60 timer_set_period(tim, 0xFFFF);
62 timer_set_prescaler(tim, (timer_clk / (ticks_per_usec *
ONE_MHZ_CLK)) - 1);
63 timer_enable_counter(tim);
82 #if USE_PWM_INPUT_TIM1
83 rcc_periph_clock_enable(RCC_TIM1);
86 #if USE_PWM_INPUT_TIM2
87 rcc_periph_clock_enable(RCC_TIM2);
90 #if USE_PWM_INPUT_TIM3
91 rcc_periph_clock_enable(RCC_TIM3);
94 #if USE_PWM_INPUT_TIM4
95 rcc_periph_clock_enable(RCC_TIM4);
98 #if USE_PWM_INPUT_TIM5
99 rcc_periph_clock_enable(RCC_TIM5);
102 #if USE_PWM_INPUT_TIM8
103 rcc_periph_clock_enable(RCC_TIM8);
106 #if USE_PWM_INPUT_TIM9
107 rcc_periph_clock_enable(RCC_TIM9);
111 #ifdef USE_PWM_INPUT1
120 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
123 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
136 #ifdef PWM_INPUT1_IRQ2
149 #ifdef USE_PWM_INPUT2
158 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
161 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
174 #ifdef PWM_INPUT2_IRQ2
176 nvic_enable_irq(PWM_INPUT2_IRQ2);
197 #if USE_PWM_INPUT_TIM1
200 void tim1_up_isr(
void)
202 #elif defined(STM32F4)
203 void tim1_up_tim10_isr(
void) {
205 if ((TIM1_SR & TIM_SR_UIF) != 0) {
211 void tim1_cc_isr(
void) {
226 #if USE_PWM_INPUT_TIM2
228 void tim2_isr(
void) {
229 if ((TIM2_SR & TIM2_CC_IF_PERIOD) != 0) {
234 if ((TIM2_SR & TIM2_CC_IF_DUTY) != 0) {
239 if ((TIM2_SR & TIM_SR_UIF) != 0) {
247 #if USE_PWM_INPUT_TIM3
249 void tim3_isr(
void) {
250 if ((TIM3_SR & TIM3_CC_IF_PERIOD) != 0) {
255 if ((TIM3_SR & TIM3_CC_IF_DUTY) != 0) {
260 if ((TIM3_SR & TIM_SR_UIF) != 0) {
268 #if USE_PWM_INPUT_TIM4
270 void tim4_isr(
void) {
271 if ((TIM4_SR & TIM4_CC_IF_PERIOD) != 0) {
276 if ((TIM4_SR & TIM4_CC_IF_DUTY) != 0) {
281 if ((TIM4_SR & TIM_SR_UIF) != 0) {
289 #if USE_PWM_INPUT_TIM5
291 void tim5_isr(
void) {
292 if ((TIM5_SR & TIM5_CC_IF_PERIOD) != 0) {
297 if ((TIM5_SR & TIM5_CC_IF_DUTY) != 0) {
302 if ((TIM5_SR & TIM_SR_UIF) != 0) {
310 #if USE_PWM_INPUT_TIM8
313 void tim8_up_isr(
void)
315 #elif defined(STM32F4)
316 void tim8_up_tim13_isr(
void) {
318 if ((TIM8_SR & TIM_SR_UIF) != 0) {
324 void tim8_cc_isr(
void) {
325 if ((TIM8_SR & TIM8_CC_IF_PERIOD) != 0) {
330 if ((TIM8_SR & TIM8_CC_IF_DUTY) != 0) {
339 #if USE_PWM_INPUT_TIM9
342 void tim1_brk_tim9_isr(
void) {
353 if ((TIM9_SR & TIM_SR_UIF) != 0) {
#define PWM_INPUT1_GPIO_PIN
Some architecture independent helper functions for GPIOs.
#define PWM_INPUT2_GPIO_AF
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...
#define PWM_INPUT1_TIMER_INPUT
#define PWM_INPUT2_CHANNEL_DUTY
timer_clear_flag(TIMx, TIM_SR_UIF)
#define TIM1_PWM_INPUT_IDX
#define PWM_INPUT1_GPIO_PORT
Architecture independent timing functions.
#define PWM_INPUT1_GPIO_AF
#define PWM_INPUT2_TIMER_INPUT
#define PWM_INPUT1_CHANNEL_DUTY
#define PWM_INPUT2_CHANNEL_PERIOD
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af)
Setup a gpio for input or output with alternate function.
#define PWM_INPUT2_SLAVE_TRIG
#define PWM_INPUT1_SLAVE_TRIG
#define PWM_INPUT2_GPIO_PORT
#define PWM_INPUT2_GPIO_PIN
#define TIM9_PWM_INPUT_IDX
#define TIM1_CC_IF_PERIOD
#define TIM9_CC_IF_PERIOD
#define PWM_INPUT1_CHANNEL_PERIOD