38 #include <libopencm3/stm32/rcc.h>
39 #include <libopencm3/stm32/gpio.h>
40 #include <libopencm3/stm32/timer.h>
41 #include <libopencm3/cm3/nvic.h>
48 #define ONE_MHZ_CLK 1000000
49 #ifdef NVIC_TIM_IRQ_PRIO
50 #define PPM_IRQ_PRIO NVIC_TIM_IRQ_PRIO
52 #define PPM_IRQ_PRIO 2
62 #define PPM_TIMER TIM2
63 #define RCC_TIM_PPM RCC_TIM2
68 #define PPM_TIMER TIM3
69 #define RCC_TIM_PPM RCC_TIM3
74 #define PPM_TIMER TIM4
75 #define RCC_TIM_PPM RCC_TIM4
80 #define PPM_TIMER TIM5
81 #define RCC_TIM_PPM RCC_TIM5
86 #define PPM_TIMER TIM1
87 #define RCC_TIM_PPM RCC_TIM1
92 #define PPM_TIMER TIM8
93 #define RCC_TIM_PPM RCC_TIM8
96 #error Unknown PPM input timer configuration.
102 rcc_periph_clock_enable(RCC_TIM_PPM);
109 timer_set_mode(
PPM_TIMER, TIM_CR1_CKD_CK_INT,
110 TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
118 #if defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_POSITIVE
120 #elif defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_NEGATIVE
123 #error "Unknown PPM_PULSE_TYPE"
155 timer_clear_flag(TIM2, PPM_CC_IF);
159 }
else if ((TIM2_SR & TIM_SR_UIF) != 0) {
161 timer_clear_flag(TIM2, TIM_SR_UIF);
170 if ((TIM3_SR & PPM_CC_IF) != 0) {
171 timer_clear_flag(TIM3, PPM_CC_IF);
175 }
else if ((TIM3_SR & TIM_SR_UIF) != 0) {
177 timer_clear_flag(TIM3, TIM_SR_UIF);
185 if ((TIM4_SR & PPM_CC_IF) != 0) {
186 timer_clear_flag(TIM4, PPM_CC_IF);
190 }
else if ((TIM4_SR & TIM_SR_UIF) != 0) {
192 timer_clear_flag(TIM4, TIM_SR_UIF);
200 if ((TIM5_SR & PPM_CC_IF) != 0) {
201 timer_clear_flag(TIM5, PPM_CC_IF);
205 }
else if ((TIM5_SR & TIM_SR_UIF) != 0) {
207 timer_clear_flag(TIM5, TIM_SR_UIF);
215 void tim1_up_isr(
void)
217 #elif defined(STM32F4)
218 void tim1_up_tim10_isr(
void) {
220 if ((TIM1_SR & TIM_SR_UIF) != 0) {
222 timer_clear_flag(TIM1, TIM_SR_UIF);
226 void tim1_cc_isr(
void) {
227 if ((TIM1_SR & PPM_CC_IF) != 0) {
228 timer_clear_flag(TIM1, PPM_CC_IF);
235 #elif USE_PPM_TIM8 && defined(STM32F4)
238 void tim8_up_isr(
void) {
239 #elif defined(STM32F4)
240 void tim8_up_tim13_isr(
void) {
242 if ((TIM8_SR & TIM_SR_UIF) != 0) {
244 timer_clear_flag(TIM8, TIM_SR_UIF);
248 void tim8_cc_isr(
void) {
249 if ((TIM8_SR & PPM_CC_IF) != 0) {
250 timer_clear_flag(TIM8, PPM_CC_IF);
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 RC_PPM_TICKS_PER_USEC
The ppm counter is set-up to have 1/6 us resolution.
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...
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void ppm_arch_init(void)
Architecture dependant code.
static uint32_t timer_rollover_cnt
void ppm_decode_frame(uint32_t ppm_time)
Decode a PPM frame from global timer value.