led.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2023 Daniele Lacamera <root@danielinux.net>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdint.h>
  18. #include "led.h"
  19. #include "system.h"
  20. #include "unicore-mx/stm32/gpio.h"
  21. #include "unicore-mx/stm32/rcc.h"
  22. /* A15, G7, B4, G6, C6, C7 */
  23. void uled_init(void)
  24. {
  25. rcc_periph_clock_enable(RCC_GPIOA);
  26. rcc_periph_clock_enable(RCC_GPIOB);
  27. rcc_periph_clock_enable(RCC_GPIOC);
  28. rcc_periph_clock_enable(RCC_GPIOG);
  29. gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LD0);
  30. gpio_set(GPIOA, LD0);
  31. gpio_mode_setup(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, LD1 | LD3);
  32. gpio_set(GPIOG, LD1 | LD3);
  33. gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, LD2);
  34. gpio_set(GPIOB, LD2);
  35. gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, LD4 | LD5);
  36. gpio_set(GPIOC, LD4 | LD5);
  37. }
  38. void uled_on(int led)
  39. {
  40. switch(led)
  41. {
  42. case 0:
  43. gpio_set(GPIOA, LD0);
  44. break;
  45. case 1:
  46. gpio_set(GPIOG, LD1);
  47. break;
  48. case 2:
  49. gpio_set(GPIOB, LD2);
  50. break;
  51. case 3:
  52. gpio_set(GPIOG, LD3);
  53. break;
  54. case 4:
  55. gpio_set(GPIOC, LD4);
  56. break;
  57. case 5:
  58. gpio_set(GPIOC, LD5);
  59. break;
  60. }
  61. }
  62. void uled_off(int led)
  63. {
  64. switch(led)
  65. {
  66. case 0:
  67. gpio_clear(GPIOA, LD0);
  68. break;
  69. case 1:
  70. gpio_clear(GPIOG, LD1);
  71. break;
  72. case 2:
  73. gpio_clear(GPIOB, LD2);
  74. break;
  75. case 3:
  76. gpio_clear(GPIOG, LD3);
  77. break;
  78. case 4:
  79. gpio_clear(GPIOC, LD4);
  80. break;
  81. case 5:
  82. gpio_clear(GPIOC, LD5);
  83. break;
  84. }
  85. }
  86. void uled_bincount(uint8_t x)
  87. {
  88. int a,b,c,d;
  89. a = (x & 0x1);
  90. b = (x & 0x2) >> 1;
  91. c = (x & 0x4) >> 2;
  92. d = (x & 0x8) >> 3;
  93. if (a)
  94. gpio_set(GPIOA, LD0);
  95. else
  96. gpio_clear(GPIOA, LD0);
  97. if (b)
  98. gpio_set(GPIOG, LD1);
  99. else
  100. gpio_clear(GPIOG, LD1);
  101. if (c)
  102. gpio_set(GPIOB, LD2);
  103. else
  104. gpio_clear(GPIOB, LD2);
  105. if (d)
  106. gpio_set(GPIOG, LD3);
  107. else
  108. gpio_clear(GPIOG, LD3);
  109. }