mux.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "at32f403a_407.h"
  2. #include "mux.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include <stdbool.h>
  6. mux_channel_t leds[LED_NUMBER] = {
  7. {STATUS_G, {1, 1, 0}, LED_OFF, 0},
  8. {STATUS_R, {1, 1, 0}, LED_OFF, 0},
  9. {RX_G, {1, 1, 0}, LED_OFF, 0},
  10. {TX_R, {1, 1, 0}, LED_OFF, 0},
  11. };
  12. //
  13. void mux_led_init(mux_channel_t *ch)
  14. {
  15. }
  16. //
  17. void mux_gpio_init(void)
  18. {
  19. gpio_init_type gpio_initstructure;
  20. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  21. crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  22. crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
  23. // LED_COL
  24. // COL_1 - PD6
  25. // COL_2 - PD7
  26. // COL_3 - PB6
  27. // COL_4 - PB7
  28. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  29. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  30. gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
  31. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  32. gpio_initstructure.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
  33. gpio_init(GPIOB, &gpio_initstructure);
  34. gpio_initstructure.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
  35. gpio_init(GPIOD, &gpio_initstructure);
  36. gpio_bits_reset(GPIOB, GPIO_PINS_6 | GPIO_PINS_7);
  37. gpio_bits_reset(GPIOD, GPIO_PINS_6 | GPIO_PINS_7);
  38. // LED_LINE (низкий уровень на пине = высокий уровень на входе MUX)
  39. // LINE_0 - PE3
  40. // LINE_1 - PE2
  41. // LINE_2 - PB9
  42. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  43. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  44. gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
  45. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  46. gpio_initstructure.gpio_pins = GPIO_PINS_2 | GPIO_PINS_3;
  47. gpio_init(GPIOE, &gpio_initstructure);
  48. gpio_initstructure.gpio_pins = GPIO_PINS_9;
  49. gpio_init(GPIOB, &gpio_initstructure);
  50. gpio_bits_reset(GPIOE, GPIO_PINS_2 | GPIO_PINS_3);
  51. gpio_bits_reset(GPIOB, GPIO_PINS_9);
  52. }
  53. //
  54. void mux_led_proc(void)
  55. {
  56. uint8_t shift = 0;
  57. for (uint8_t i = 0; i < LED_NUMBER/4; i++)
  58. {
  59. leds[shift].line[0] ? (LINE_0_RESET) : (LINE_0_SET);
  60. leds[shift].line[1] ? (LINE_1_RESET) : (LINE_1_SET);
  61. leds[shift].line[2] ? (LINE_2_RESET) : (LINE_2_SET);
  62. leds[i*4].state == LED_ON ? (COL_1_SET) : (COL_1_RESET);
  63. leds[i*4 + 1].state == LED_ON ? (COL_2_SET) : (COL_2_RESET);
  64. leds[i*4 + 2].state == LED_ON ? (COL_3_SET) : (COL_3_RESET);
  65. leds[i*4 + 3].state == LED_ON ? (COL_4_SET) : (COL_4_RESET);
  66. if (leds[i*4].state == LED_ON || leds[i*4 + 1].state == LED_ON ||
  67. leds[i*4 + 2].state == LED_ON || leds[i*4 + 3].state == LED_ON)
  68. {
  69. vTaskDelay(1);
  70. }
  71. shift += 4;
  72. }
  73. }
  74. //
  75. void mux_led_test_init(void)
  76. {
  77. LINE_0_SET;
  78. LINE_1_SET;
  79. LINE_2_SET;
  80. }
  81. //
  82. void mux_led_test_toggle(void)
  83. {
  84. static bool flag = false;
  85. if (!flag) {
  86. COL_1_SET;
  87. flag = true;
  88. }
  89. else {
  90. COL_1_RESET;
  91. flag = false;
  92. }
  93. }
  94. //
  95. void mux_led_blink(void)
  96. {
  97. for (int i = 0; i < LED_NUMBER; i++)
  98. {
  99. leds[i].state = LED_ON;
  100. vTaskDelay(100);
  101. leds[i].state = LED_OFF;
  102. }
  103. }
  104. // true - normal
  105. // false - alarm
  106. void mux_led_status(bool state)
  107. {
  108. if (state) {
  109. leds[STATUS_G].state = LED_ON;
  110. leds[STATUS_R].state = LED_OFF;
  111. }
  112. else {
  113. leds[STATUS_G].state = LED_OFF;
  114. leds[STATUS_R].state = LED_ON;
  115. }
  116. }