Code: Select all
#include "ch.h"
#include "hal.h"
#include "chdebug.h"
#include "chprintf.h"
#define RC_PWM_FREQ 50
volatile uint16_t val;
CH_IRQ_HANDLER(TIM2_IRQHandler) {
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
val = TIM2->CCR1; //read value to clear IRQ flag
chSysUnlockFromIsr();
CH_IRQ_EPILOGUE();
}
void initRCin(void){
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = STM32_PCLK1*2 / ((1<<16) * RC_PWM_FREQ); //50Hz frequency
TIM2->CCMR1 |= TIM_CCMR1_CC1S_0; //channel 1 = input on T1
TIM2->DIER |= TIM_DIER_CC1IE; //enable capture/compare interrupt
TIM2->CCER |= TIM_CCER_CC1E; //Capture/Compare output enable
nvicEnableVector(TIM2_IRQn, 12);
TIM2->CR1 |= TIM_CR1_CEN; //counter enable
}
int main(void) {
halInit();
chSysInit();
initRCin();
while (TRUE) {
chThdSleepMilliseconds(100);
}
}