hall_tmr.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "bsp/bsp_driver.h"
  2. #include "libs/utils.h"
  3. #include "libs/logger.h"
  4. #include "hall_tmr.h"
  5. void hall_tmr_init(void) {
  6. gpio_init_type gpio_init_struct = {0};
  7. tmr_input_config_type tmr_input_config_struct;
  8. crm_periph_clock_enable(HALL_TIMER_RCU, TRUE);
  9. crm_periph_clock_enable(HALL_A_RCU, TRUE);
  10. crm_periph_clock_enable(HALL_B_RCU, TRUE);
  11. crm_periph_clock_enable(HALL_C_RCU, TRUE);
  12. /* timer hall sensor pin Configuration */
  13. gpio_default_para_init(&gpio_init_struct);
  14. gpio_init_struct.gpio_pins = HALL_A_PIN;
  15. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  16. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  17. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  18. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  19. gpio_init(HALL_A_GROUP, &gpio_init_struct);
  20. gpio_init_struct.gpio_pins = HALL_B_PIN;
  21. gpio_init(HALL_B_GROUP, &gpio_init_struct);
  22. gpio_init_struct.gpio_pins = HALL_C_PIN;
  23. gpio_init(HALL_C_GROUP, &gpio_init_struct);
  24. /* remap hall timer input pins */
  25. gpio_pin_remap_config(HALL_CAPTURE_IOMUX, TRUE);
  26. /* timer hall interface configuration */
  27. tmr_base_init(HALL_TIMER, 0xFFFF, 0);
  28. tmr_cnt_dir_set(HALL_TIMER, TMR_COUNT_UP);
  29. /* config ti1 trc as input source */
  30. tmr_input_default_para_init(&tmr_input_config_struct);
  31. tmr_input_config_struct.input_channel_select = TMR_SELECT_CHANNEL_1;
  32. tmr_input_config_struct.input_mapped_select = TMR_CC_CHANNEL_MAPPED_STI;
  33. tmr_input_config_struct.input_polarity_select = TMR_INPUT_RISING_EDGE;
  34. tmr_input_config_struct.input_filter_value = 0xF;
  35. tmr_input_channel_init(HALL_TIMER, &tmr_input_config_struct, TMR_CHANNEL_INPUT_DIV_1);
  36. /* xor funtion enable */
  37. tmr_channel1_input_select(HALL_TIMER, TMR_CHANEL1_2_3_CONNECTED_C1IRAW_XOR);
  38. /* select the tmr input trigger: C1INC */
  39. tmr_trigger_input_select(HALL_TIMER, TMR_SUB_INPUT_SEL_C1INC);
  40. /* select the slave mode: reset mode */
  41. tmr_sub_mode_select(HALL_TIMER, TMR_SUB_RESET_MODE);
  42. /* clear interrupt flag of hall timer */
  43. tmr_flag_clear(HALL_TIMER, TMR_TRIGGER_FLAG | TMR_OVF_FLAG);
  44. /* enable overflow flag of hall timer */
  45. tmr_interrupt_enable(HALL_TIMER, TMR_TRIGGER_INT, TRUE);
  46. /* enable hall timer */
  47. tmr_counter_enable(HALL_TIMER, TRUE);
  48. nvic_irq_enable(HALL_TMR_IRQn, HALL_IRQ_PRIORITY, 0);
  49. }
  50. __weak void HALL_IRQHandler(void) {
  51. }
  52. void HALL_TMR_IRQHandler(void) {
  53. if (tmr_flag_get(HALL_TIMER, TMR_TRIGGER_FLAG | TMR_C1_FLAG) != RESET) {
  54. tmr_flag_clear(HALL_TIMER, TMR_TRIGGER_FLAG | TMR_C1_FLAG | TMR_OVF_FLAG);
  55. HALL_IRQHandler();
  56. }
  57. }