gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "bsp/bsp_driver.h"
  2. #include "libs/utils.h"
  3. /*
  4. * gpio.c
  5. * all pins used as gpio(in/out/irq) must be init&accessed here
  6. */
  7. void gpio_beep(u32 ms) {
  8. }
  9. void at32_gpio_init(gpio_type *gpiox, gpio_mode_type mode, gpio_output_type otype, gpio_pull_type pull, u32 pin) {
  10. gpio_init_type gpio_init_struct = {0};
  11. gpio_default_para_init(&gpio_init_struct);
  12. gpio_init_struct.gpio_mode = mode;
  13. gpio_init_struct.gpio_out_type = otype;
  14. gpio_init_struct.gpio_pull = pull;
  15. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  16. /* High-side, Phase A,B,C Config */
  17. gpio_init_struct.gpio_pins = pin;
  18. gpio_init(gpiox, &gpio_init_struct);
  19. }
  20. void gpio_pin_init(void){
  21. crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  22. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  23. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  24. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  25. crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
  26. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  27. gpio_bits_write(STATUS3_LED_PORT, STATUS3_LED_GPIO_PIN, TRUE);
  28. at32_gpio_init(STATUS3_LED_PORT, GPIO_MODE_OUTPUT, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, STATUS3_LED_GPIO_PIN);
  29. }
  30. void gpio_mc_brk_init(void) {
  31. #ifdef GPIO_BRAKE_IN_GROUP
  32. crm_periph_clock_enable(GPIO_BRAKE_IN_RCU, TRUE);
  33. #ifdef GPIO_BRAKE_PIN_REMAP
  34. gpio_pin_remap_config(GPIO_BRAKE_PIN_REMAP, TRUE);
  35. #endif
  36. at32_gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_BRAKE_IN_PIN);
  37. exint_init_type exint_init_struct;
  38. gpio_exint_line_config(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  39. exint_default_para_init(&exint_init_struct);
  40. exint_init_struct.line_enable = TRUE;
  41. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  42. exint_init_struct.line_select = GPIO_BRAKE_EXTI;
  43. exint_init_struct.line_polarity = EXINT_TRIGGER_BOTH_EDGE;
  44. exint_init(&exint_init_struct);
  45. exint_flag_clear(GPIO_BRAKE_EXTI);
  46. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  47. #endif
  48. }
  49. void gpio_mlock_init(void) {
  50. #ifdef GPIO_MLOCK_IN_GROUP
  51. crm_periph_clock_enable(GPIO_MLOCK_IN_RCU, TRUE);
  52. at32_gpio_init(GPIO_MLOCK_IN_GROUP, GPIO_MLOCK_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_MLOCK_IN_PIN);
  53. #endif
  54. }
  55. void gpio_fan_det_init(void) {
  56. #ifdef GPIO_FAN1_IN_GROUP
  57. crm_periph_clock_enable(GPIO_FAN1_IN_RCU, TRUE);
  58. at32_gpio_init(GPIO_FAN1_IN_GROUP, GPIO_FAN1_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_FAN1_IN_PIN);
  59. exint_init_type exint_init_struct;
  60. gpio_exint_line_config(GPIO_FAN1_EXIT_SRC_GROUP, GPIO_FAN1_EXIT_SRC_PIN);
  61. exint_default_para_init(&exint_init_struct);
  62. exint_init_struct.line_enable = TRUE;
  63. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  64. exint_init_struct.line_select = GPIO_FAN1_EXTI;
  65. exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  66. exint_init(&exint_init_struct);
  67. exint_flag_clear(GPIO_FAN1_EXTI);
  68. nvic_irq_enable(GPIO_FAN1_IRQ, ENC_OTHER_IRQ_PRIORITY, 0U);
  69. #endif
  70. }
  71. void gpio_phase_u_detect(bool enable) {
  72. #ifdef GPIO_UDEC_OUT_GROUP
  73. if (enable) {
  74. at32_gpio_init(GPIO_UDEC_OUT_GROUP, GPIO_UDEC_OUT_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_UDEC_OUT_PIN);
  75. #ifdef GPIO_UDEC_OUT_REMAP_DISABLE
  76. gpio_pin_remap_config(GPIO_UDEC_OUT_REMAP_DISABLE, ENABLE);
  77. #endif
  78. gpio_bits_write(GPIO_UDEC_OUT_GROUP, GPIO_UDEC_OUT_PIN, TRUE);
  79. }else {
  80. at32_gpio_init(GPIO_UDEC_OUT_GROUP, GPIO_MODE_INPUT, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_UDEC_OUT_PIN);
  81. #ifdef GPIO_UDEC_OUT_REMAP_ENABLE
  82. gpio_pin_remap_config(GPIO_UDEC_OUT_REMAP_ENABLE, ENABLE);
  83. #endif
  84. }
  85. #endif
  86. }
  87. void gpio_led_init(void) {
  88. #ifdef GPIO_LED_OUT_GROUP
  89. crm_periph_clock_enable(GPIO_LED_OUT_RCU, TRUE);
  90. at32_gpio_init(GPIO_LED_OUT_GROUP, GPIO_LED_OUT_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_LED_OUT_PIN);
  91. gpio_bits_reset(GPIO_LED_OUT_GROUP, GPIO_LED_OUT_PIN);
  92. #endif
  93. }
  94. void mc_gpio_init(void) {
  95. gpio_led_init();
  96. }
  97. u8 gpio_board_id(void) {
  98. return 0xFF;
  99. }
  100. void gpio_led_enable(bool enable) {
  101. #ifdef GPIO_LED_OUT_GROUP
  102. gpio_bits_write(GPIO_BRAKE_LIGHT_OUT_GROUP, GPIO_BRAKE_LIGHT_OUT_PIN, enable?TRUE:FALSE);
  103. #endif
  104. }
  105. void gpio_brk_light_enable(bool enable) {
  106. }
  107. bool mc_get_gpio_brake(void) {
  108. return false;
  109. }
  110. bool mc_get_gpio_brake1(void) {
  111. return mc_get_gpio_brake();
  112. }
  113. bool gpio_motor_locked(void) {
  114. return false;
  115. }