X-Git-Url: http://cloudbase.mooo.com/gitweb/irmp-demo.git/blobdiff_plain/b8dc40706bc01c881b69d8a460b4c1f0fb794a7d..refs/heads/master:/irmp-main.c diff --git a/irmp-main.c b/irmp-main.c index 1399546..bc6623b 100644 --- a/irmp-main.c +++ b/irmp-main.c @@ -4,7 +4,7 @@ * Copyright (c) 2017 Leo C. * * This demo program is essentially a combination of some libopencm3 examples - * from https://github.com/libopencm3/libopencm3-examples, + * from https://github.com/libopencm3/libopencm3-examples * and irmp-main-stm32.c from the IRMP distribution, which is * Copyright (c) 2009-2016 Frank Meyer - frank(at)fli4l.de * @@ -41,6 +41,12 @@ #define DEBUG_IRMP_TIMER_INT 0 #endif +#if DEBUG_IRMP_TIMER_INT +#define DEBUG_LED_PORT GPIOB +#define DEBUG_LED_PIN GPIO0 +#define DEBUG_LED_PORT_RCC RCC_GPIOB +#endif + /** * Do all initialisations, that are not done by a specific module here. */ @@ -70,13 +76,16 @@ static void setup_clock_and_gpios(void) /* IRMP */ /*--------------------------------------------------------------------------*/ -#define TIM_IRMP_CR1 TIM_CR1(TIM_IRMP) -#define TIM_IRMP_DIER TIM_DIER(TIM_IRMP) -#define TIM_IRMP_SR TIM_SR(TIM_IRMP) -#define TIM_IRMP_ARR TIM_ARR(TIM_IRMP) -#define RCC_TIM_IRMP CONCAT(RCC_TIM, IRMP_TIMER) -#define NVIC_TIM_IRMP_IRQ CONCAT(CONCAT(NVIC_TIM, IRMP_TIMER), _IRQ) -#define IRMP_TIMER_ISR CONCAT(CONCAT(tim, IRMP_TIMER), _isr) +#define IRMP_TIMER_NUMBER 3 +#define IRMP_TIMER CONCAT(TIM, IRMP_TIMER_NUMBER) + +#define IRMP_TIMER_CR1 TIM_CR1(IRMP_TIMER) +#define IRMP_TIMER_DIER TIM_DIER(IRMP_TIMER) +#define IRMP_TIMER_SR TIM_SR(IRMP_TIMER) +#define IRMP_TIMER_ARR TIM_ARR(IRMP_TIMER) +#define IRMP_TIMER_RCC CONCAT(RCC_TIM, IRMP_TIMER_NUMBER) +#define NVIC_IRMP_TIMER_IRQ CONCAT(CONCAT(NVIC_TIM, IRMP_TIMER_NUMBER), _IRQ) +#define IRMP_TIMER_ISR CONCAT(CONCAT(tim, IRMP_TIMER_NUMBER), _isr) /** Retrieve the actual input clock of a timer @@ -87,20 +96,17 @@ static void setup_clock_and_gpios(void) uint32_t timer_internal_clock_get(uint32_t timer_peripheral) { uint32_t timer_frequency; - uint32_t ppre; /* Get preripheral bus frequency and prescaler mask */ - if (timer_peripheral == TIM1 || timer_peripheral == TIM8) { - /* Advanced timers TIM1 and TIM8 are on APB2 */ - ppre = RCC_CFGR_PPRE2; - timer_frequency = rcc_apb2_frequency; - } else { - /* Other timers are on APB1 */ - ppre = RCC_CFGR_PPRE1; + if ((timer_peripheral >= TIM2 && timer_peripheral <= TIM5) + || (timer_peripheral >= TIM12 && timer_peripheral <= TIM14)) + { timer_frequency = rcc_apb1_frequency; + } else { + timer_frequency = rcc_apb2_frequency; } /* Timer clock is doubled, if the APB prescaler is greater than 1 */ - if ((RCC_CFGR & ppre) != 0) + if (timer_frequency != rcc_ahb_frequency) timer_frequency *= 2; return timer_frequency; @@ -113,14 +119,14 @@ void irmp_timer_init (void) { #if DEBUG_IRMP_TIMER_INT /* Output pin for debugging */ - rcc_periph_clock_enable(RCC_GPIOA); - gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, - GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); + rcc_periph_clock_enable(DEBUG_LED_PORT_RCC); + gpio_set_mode(DEBUG_LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, DEBUG_LED_PIN); #endif /* Enable timer clock. */ - rcc_periph_clock_enable(RCC_TIM_IRMP); - nvic_set_priority(NVIC_TIM_IRMP_IRQ, 4*16); - nvic_enable_irq(NVIC_TIM_IRMP_IRQ); + rcc_periph_clock_enable(IRMP_TIMER_RCC); + nvic_set_priority(NVIC_IRMP_TIMER_IRQ, 4*16); + nvic_enable_irq(NVIC_IRMP_TIMER_IRQ); #if USE_OPENCM3_API /* Using API functions: */ @@ -131,22 +137,22 @@ void irmp_timer_init (void) * (These are actually default values after reset, so this call * is strictly unnecessary, but demos the api for alternative settings) */ - timer_set_mode(TIM_IRMP, TIM_CR1_CKD_CK_INT, + timer_set_mode(IRMP_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); - timer_set_period(TIM_IRMP, timer_internal_clock_get(TIM_IRMP) / F_INTERRUPTS); + timer_set_period(IRMP_TIMER, timer_internal_clock_get(IRMP_TIMER) / F_INTERRUPTS - 1); /* Enable Channel 1 compare interrupt to recalculate compare values */ - timer_enable_irq(TIM_IRMP, TIM_DIER_UIE); + timer_enable_irq(IRMP_TIMER, TIM_DIER_UIE); /* Counter enable. */ - timer_enable_counter(TIM_IRMP); + timer_enable_counter(IRMP_TIMER); #else /* Manually */ - TIM_IRMP_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP; - TIM_IRMP_ARR = timer_internal_clock_get(TIM_IRMP) / F_INTERRUPTS; + IRMP_TIMER_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP; + IRMP_TIMER_ARR = timer_internal_clock_get(IRMP_TIMER) / F_INTERRUPTS - 1; /* Enable Timer interrupt and timer */ - TIM_IRMP_DIER = TIM_DIER_UIE; - TIM_IRMP_CR1 |= TIM_CR1_CEN; + IRMP_TIMER_DIER = TIM_DIER_UIE; + IRMP_TIMER_CR1 |= TIM_CR1_CEN; #endif } @@ -156,15 +162,15 @@ void IRMP_TIMER_ISR(void) #if DEBUG_IRMP_TIMER_INT # if USE_OPENCM3_API /* Using API functions: */ - gpio_clear(GPIOA, GPIO2); + gpio_clear(DEBUG_LED_PORT, DEBUG_LED_PIN); /* Clear update interrupt flag. */ - timer_clear_flag(TIM_IRMP, TIM_SR_UIF); + timer_clear_flag(IRMP_TIMER, TIM_SR_UIF); # else /* Manually */ - GPIO_BRR(GPIOA) = GPIO2; + GPIO_BRR(DEBUG_LED_PORT) = DEBUG_LED_PIN; # endif #endif /* Clear update interrupt flag. */ - TIM_IRMP_SR = ~TIM_SR_UIF; + IRMP_TIMER_SR = ~TIM_SR_UIF; (void) irmp_ISR(); // call irmp ISR @@ -172,9 +178,9 @@ void IRMP_TIMER_ISR(void) #if DEBUG_IRMP_TIMER_INT # if USE_OPENCM3_API /* Using API functions: */ - gpio_set(GPIOA, GPIO2); + gpio_set(DEBUG_LED_PORT, DEBUG_LED_PIN); # else /* Manually */ - GPIO_BSRR(GPIOA) = GPIO2; + GPIO_BSRR(DEBUG_LED_PORT) = DEBUG_LED_PIN; # endif #endif } @@ -380,7 +386,7 @@ int main (void) " System frequency: %luHz\n" "IRMP timer input frequency (CK_INT): %luHz\n" " IRMP interrupt frequency: %uHz\n", - rcc_ahb_frequency, timer_internal_clock_get(TIM_IRMP), F_INTERRUPTS); + rcc_ahb_frequency, timer_internal_clock_get(IRMP_TIMER), F_INTERRUPTS); systick_setup(); irmp_timer_init(); // initialize timer for irmp