gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_pin_init(void){
  8. crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  9. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  10. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  11. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  12. crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
  13. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  14. }
  15. void gpio_beep(u32 ms) {
  16. }
  17. void at32_gpio_init(gpio_type *gpiox, gpio_mode_type mode, gpio_output_type otype, gpio_pull_type pull, u32 pin) {
  18. gpio_init_type gpio_init_struct = {0};
  19. gpio_default_para_init(&gpio_init_struct);
  20. gpio_init_struct.gpio_mode = mode;
  21. gpio_init_struct.gpio_out_type = otype;
  22. gpio_init_struct.gpio_pull = pull;
  23. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  24. /* High-side, Phase A,B,C Config */
  25. gpio_init_struct.gpio_pins = pin;
  26. gpio_init(gpiox, &gpio_init_struct);
  27. }
  28. void gpio_mc_brk_init(void) {
  29. #ifdef GPIO_BRAKE_IN_GROUP
  30. crm_periph_clock_enable(GPIO_BRAKE_IN_RCU, TRUE);
  31. #ifdef GPIO_BRAKE_PIN_REMAP
  32. gpio_pin_remap_config(GPIO_BRAKE_PIN_REMAP, TRUE);
  33. #endif
  34. at32_gpio_init(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_BRAKE_IN_PIN);
  35. exint_init_type exint_init_struct;
  36. gpio_exint_line_config(GPIO_BRAKE_EXIT_SRC_GROUP, GPIO_BRAKE_EXIT_SRC_PIN);
  37. exint_default_para_init(&exint_init_struct);
  38. exint_init_struct.line_enable = TRUE;
  39. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  40. exint_init_struct.line_select = GPIO_BRAKE_EXTI;
  41. exint_init_struct.line_polarity = EXINT_TRIGGER_BOTH_EDGE;
  42. exint_init(&exint_init_struct);
  43. exint_flag_clear(GPIO_BRAKE_EXTI);
  44. nvic_irq_enable(GPIO_BRAKE_IRQ, EBREAK_IRQ_PRIORITY, 0U);
  45. #endif
  46. }
  47. void gpio_mlock_init(void) {
  48. #ifdef GPIO_MLOCK_IN_GROUP
  49. crm_periph_clock_enable(GPIO_MLOCK_IN_RCU, TRUE);
  50. at32_gpio_init(GPIO_MLOCK_IN_GROUP, GPIO_MLOCK_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_MLOCK_IN_PIN);
  51. #endif
  52. }
  53. void gpio_fan_det_init(void) {
  54. #ifdef GPIO_FAN1_IN_GROUP
  55. crm_periph_clock_enable(GPIO_FAN1_IN_RCU, TRUE);
  56. at32_gpio_init(GPIO_FAN1_IN_GROUP, GPIO_FAN1_IN_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_FAN1_IN_PIN);
  57. exint_init_type exint_init_struct;
  58. gpio_exint_line_config(GPIO_FAN1_EXIT_SRC_GROUP, GPIO_FAN1_EXIT_SRC_PIN);
  59. exint_default_para_init(&exint_init_struct);
  60. exint_init_struct.line_enable = TRUE;
  61. exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
  62. exint_init_struct.line_select = GPIO_FAN1_EXTI;
  63. exint_init_struct.line_polarity = EXINT_TRIGGER_RISING_EDGE;
  64. exint_init(&exint_init_struct);
  65. exint_flag_clear(GPIO_FAN1_EXTI);
  66. nvic_irq_enable(GPIO_FAN1_IRQ, ENC_OTHER_IRQ_PRIORITY, 0U);
  67. #endif
  68. }
  69. void gpio_phase_u_detect(bool enable) {
  70. #ifdef GPIO_UDEC_OUT_GROUP
  71. if (enable) {
  72. at32_gpio_init(GPIO_UDEC_OUT_GROUP, GPIO_UDEC_OUT_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_UDEC_OUT_PIN);
  73. #ifdef GPIO_UDEC_OUT_REMAP_DISABLE
  74. gpio_pin_remap_config(GPIO_UDEC_OUT_REMAP_DISABLE, ENABLE);
  75. #endif
  76. gpio_bits_write(GPIO_UDEC_OUT_GROUP, GPIO_UDEC_OUT_PIN, TRUE);
  77. }else {
  78. at32_gpio_init(GPIO_UDEC_OUT_GROUP, GPIO_MODE_INPUT, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_UDEC_OUT_PIN);
  79. #ifdef GPIO_UDEC_OUT_REMAP_ENABLE
  80. gpio_pin_remap_config(GPIO_UDEC_OUT_REMAP_ENABLE, ENABLE);
  81. #endif
  82. }
  83. #endif
  84. }
  85. void gpio_led_init(void) {
  86. #ifdef GPIO_LED_OUT_GROUP
  87. crm_periph_clock_enable(GPIO_LED_OUT_RCU, TRUE);
  88. at32_gpio_init(GPIO_LED_OUT_GROUP, GPIO_LED_OUT_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_LED_OUT_PIN);
  89. gpio_bits_reset(GPIO_LED_OUT_GROUP, GPIO_LED_OUT_PIN);
  90. #endif
  91. }
  92. void gpio_brk_light_init(void) {
  93. #ifdef GPIO_BRAKE_LIGHT_OUT_GROUP
  94. crm_periph_clock_enable(GPIO_BRAKE_LIGHT_OUT_RCU, TRUE);
  95. at32_gpio_init(GPIO_BRAKE_LIGHT_OUT_GROUP, GPIO_BRAKE_LIGHT_OUT_MODE, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, GPIO_BRAKE_LIGHT_OUT_PIN);
  96. gpio_bits_reset(GPIO_BRAKE_LIGHT_OUT_GROUP, GPIO_BRAKE_LIGHT_OUT_PIN);
  97. #endif
  98. }
  99. void gpio_board_id_init(void) {
  100. #ifdef BOOT_PIN_0_GROUP
  101. at32_gpio_init(BOOT_PIN_0_GROUP, GPIO_MODE_INPUT, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, BOOT_PIN_0_PIN);
  102. at32_gpio_init(BOOT_PIN_1_GROUP, GPIO_MODE_INPUT, GPIO_OUTPUT_PUSH_PULL, GPIO_PULL_NONE, BOOT_PIN_1_PIN);
  103. #endif
  104. }
  105. u8 gpio_hw_board_id(void) {
  106. u8 id = BOARD_105_VERSION_4;
  107. #ifdef BOOT_PIN_0_GROUP
  108. u8 b0 = gpio_input_data_bit_read(BOOT_PIN_0_GROUP, BOOT_PIN_0_PIN);
  109. u8 b1 = gpio_input_data_bit_read(BOOT_PIN_1_GROUP, BOOT_PIN_1_PIN);
  110. id = ((b1 << 1) | b0);
  111. #endif
  112. return id;
  113. }
  114. static u8 _board_id = BOARD_105_VERSION_4;
  115. void mc_gpio_init(void) {
  116. gpio_mlock_init();
  117. gpio_mc_brk_init();
  118. gpio_fan_det_init();
  119. gpio_led_init();
  120. gpio_brk_light_init();
  121. gpio_board_id_init();
  122. int count = 10;
  123. do {
  124. delay_ms(5);
  125. _board_id = gpio_hw_board_id();
  126. if (_board_id == BOARD_105_VERSION_3 || _board_id == BOARD_105_VERSION_4) {
  127. break;
  128. }
  129. }while(count-- > 0);
  130. }
  131. u8 gpio_board_id(void) {
  132. return _board_id;
  133. }
  134. void gpio_led_enable(bool enable) {
  135. #ifdef GPIO_LED_OUT_GROUP
  136. gpio_bits_write(GPIO_BRAKE_LIGHT_OUT_GROUP, GPIO_BRAKE_LIGHT_OUT_PIN, enable?TRUE:FALSE);
  137. #endif
  138. }
  139. void gpio_brk_light_enable(bool enable) {
  140. #ifdef GPIO_BRAKE_LIGHT_OUT_GROUP
  141. gpio_bits_write(GPIO_BRAKE_LIGHT_OUT_GROUP, GPIO_BRAKE_LIGHT_OUT_PIN, enable?TRUE:FALSE);
  142. #endif
  143. }
  144. bool mc_get_gpio_brake(void) {
  145. return gpio_input_data_bit_read(GPIO_BRAKE_IN_GROUP, GPIO_BRAKE_IN_PIN) == SET;
  146. }
  147. bool mc_get_gpio_brake1(void) {
  148. #ifdef GPIO_BRAKE1_IN_GROUP
  149. return gpio_input_data_bit_read(GPIO_BRAKE1_IN_GROUP, GPIO_BRAKE1_IN_PIN) == SET;
  150. #else
  151. return mc_get_gpio_brake();
  152. #endif
  153. }
  154. bool gpio_motor_locked(void) {
  155. #ifdef GPIO_MLOCK_IN_GROUP
  156. return gpio_input_data_bit_read(GPIO_MLOCK_IN_GROUP, GPIO_MLOCK_IN_PIN) == RESET;
  157. #else
  158. return false;
  159. #endif
  160. }