enc_intf.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "bsp/bsp_driver.h"
  2. #include "libs/logger.h"
  3. static void enc_gpio_init(gpio_type *gpiox, gpio_mode_type mode, u32 pin) {
  4. gpio_init_type gpio_init_struct = {0};
  5. gpio_default_para_init(&gpio_init_struct);
  6. gpio_init_struct.gpio_mode = mode;
  7. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  8. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  9. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  10. /* High-side, Phase A,B,C Config */
  11. gpio_init_struct.gpio_pins = pin;
  12. gpio_init(gpiox, &gpio_init_struct);
  13. }
  14. static void _io_init(void) {
  15. crm_periph_clock_enable(ENC_A_RCU, TRUE);
  16. crm_periph_clock_enable(ENC_B_RCU, TRUE);
  17. crm_periph_clock_enable(ENC_PWM_RCU, TRUE);
  18. crm_periph_clock_enable(ENC_I_RCU, TRUE);
  19. #ifdef TIMER2_PB4_PB5_REMAP
  20. gpio_pin_remap_config(TIMER2_PB4_PB5_REMAP, TRUE);
  21. #endif
  22. #ifdef TIMER1_PA15_REMAP
  23. gpio_pin_remap_config(TIMER1_PA15_REMAP, TRUE);
  24. #endif
  25. enc_gpio_init(ENC_A_GROUP, ENC_A_MODE, ENC_A_PIN);
  26. enc_gpio_init(ENC_B_GROUP, ENC_B_MODE, ENC_B_PIN);
  27. enc_gpio_init(ENC_PWM_GROUP, ENC_PWM_MODE, ENC_PWM_PIN);
  28. enc_gpio_init(ENC_I_GROUP, ENC_I_MODE, ENC_I_PIN);
  29. }
  30. static void _io_init_irq(void) {
  31. exint_init_type exint_init_struct;
  32. gpio_exint_line_config(ENC_I_EXIT_SRC_GROUP, ENC_I_EXIT_SRC_PIN);
  33. exint_default_para_init(&exint_init_struct);
  34. exint_init_struct.line_enable = TRUE;
  35. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  36. exint_init_struct.line_select = ENC_I_EXTI;
  37. exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  38. exint_init(&exint_init_struct);
  39. nvic_irq_enable(ENC_I_IRQ, ENC_I_EXIT_IRQ_PRIORITY, 0U);
  40. }
  41. void enc_intf_init(u32 rate) {
  42. _io_init();
  43. enc_intf_pwm_counter();
  44. enc_intf_quadrature_init(rate);
  45. _io_init_irq();
  46. }
  47. void enc_intf_quadrature_init(u32 rate) {
  48. tmr_type* timer = ENC_TIMER;
  49. crm_periph_clock_enable(ENC_TIMER_RCU, TRUE);
  50. tmr_reset(timer);
  51. tmr_base_init(timer, rate -1 , 0);
  52. tmr_cnt_dir_set(timer, TMR_COUNT_UP);
  53. tmr_input_channel_filter_set(timer, TMR_SELECT_CHANNEL_1, ENC_FILTER_NR);
  54. tmr_input_channel_filter_set(timer, TMR_SELECT_CHANNEL_2, ENC_FILTER_NR);
  55. /* config encoder mode */
  56. tmr_encoder_mode_config(timer, TMR_ENCODER_MODE_C, TMR_INPUT_FALLING_EDGE, TMR_INPUT_RISING_EDGE);
  57. /* enable tmr2 */
  58. tmr_counter_enable(timer, TRUE);
  59. }
  60. void enc_intf_pwm_counter(void) {
  61. tmr_input_config_type tmr_ic_init_structure;
  62. tmr_type * timer = ENC_PWM_TIMER;
  63. crm_periph_clock_enable(ENC_PWM_TIMER_RCU, TRUE);
  64. tmr_reset(timer);
  65. tmr_input_default_para_init(&tmr_ic_init_structure);
  66. tmr_ic_init_structure.input_filter_value = ENC_FILTER_NR;
  67. tmr_ic_init_structure.input_channel_select = ENC_PWM_TIMER_CHAN;
  68. tmr_ic_init_structure.input_mapped_select = TMR_CC_CHANNEL_MAPPED_DIRECT;
  69. tmr_ic_init_structure.input_polarity_select = TMR_INPUT_RISING_EDGE;
  70. tmr_pwm_input_config(timer, &tmr_ic_init_structure, TMR_CHANNEL_INPUT_DIV_1);
  71. /* select the tmr3 input trigger: c2df2 */
  72. tmr_trigger_input_select(timer, TMR_SUB_INPUT_SEL_C1DF1);
  73. /* select the sub mode: reset mode */
  74. tmr_sub_mode_select(timer, TMR_SUB_RESET_MODE);
  75. /* enable the sub sync mode */
  76. tmr_sub_sync_mode_set(timer, TRUE);
  77. /* tmr enable counter */
  78. tmr_counter_enable(timer, TRUE);
  79. /* enable the c2 interrupt request */
  80. tmr_interrupt_enable(timer, ENC_PWM_TIMER_IRQ_CH, TRUE);
  81. nvic_irq_enable(ENC_PWM_TIMER_IRQ, ENC_PWM_IRQ_PRIORITY, 0);
  82. }
  83. __weak void ENC_TIMER_Overflow(void) {
  84. }
  85. void ENC_TIMER_IRQHandler(void) {
  86. if (SET == tmr_flag_get(ENC_TIMER, TMR_OVF_FLAG)) {
  87. tmr_flag_clear(ENC_TIMER, TMR_OVF_FLAG);
  88. ENC_TIMER_Overflow();
  89. }
  90. }
  91. __weak void ENC_ABI_IRQHandler(void) {
  92. }
  93. void ABI_I_IRQHandler(void) {
  94. ENC_ABI_IRQHandler();
  95. }
  96. __weak void ENC_PWM_Duty_Handler(float t, float d) {
  97. }
  98. static float pwm_freq = 0.0f;
  99. void ENC_PWM_IRQHandler(void) {
  100. if(SET == tmr_flag_get(ENC_PWM_TIMER, ENC_PWM_TIMER_INT_FLG)){
  101. /* clear channel 0 interrupt bit */
  102. tmr_flag_clear(ENC_PWM_TIMER, ENC_PWM_TIMER_INT_FLG);
  103. /* read channel 0 capture value */
  104. u32 ic0value = tmr_channel_value_get(ENC_PWM_TIMER, TMR_SELECT_CHANNEL_1) + 1;
  105. u32 ic1value = tmr_channel_value_get(ENC_PWM_TIMER, TMR_SELECT_CHANNEL_2) + 1;
  106. float p_calc = ENC_PWM_Calc_P(ic0value);
  107. float d_calc = ENC_PWM_Calc_P(ic1value);
  108. ENC_PWM_Duty_Handler(p_calc, d_calc);
  109. pwm_freq = (float)PWM_TIME_CLK/((float)ic0value);
  110. }
  111. }
  112. float enc_get_pwm_freq(void) {
  113. return pwm_freq;
  114. }