You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.1 KiB

  1. /*
  2. Copyright 2016 Ralf Schmitt <ralf@bunkertor.net>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/interrupt.h>
  15. #include <avr/io.h>
  16. #include <stdbool.h>
  17. #include <util/delay.h>
  18. #include <stdint.h>
  19. #include "indicator_leds.h"
  20. #include "quantum.h"
  21. #define LED_T1H 900
  22. #define LED_T1L 600
  23. #define LED_T0H 400
  24. #define LED_T0L 900
  25. void send_bit_d4(bool bitVal)
  26. {
  27. if(bitVal) {
  28. asm volatile (
  29. "sbi %[port], %[bit] \n\t"
  30. ".rept %[onCycles] \n\t"
  31. "nop \n\t"
  32. ".endr \n\t"
  33. "cbi %[port], %[bit] \n\t"
  34. ".rept %[offCycles] \n\t"
  35. "nop \n\t"
  36. ".endr \n\t"
  37. ::
  38. [port] "I" (_SFR_IO_ADDR(PORTD)),
  39. [bit] "I" (4),
  40. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  41. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  42. } else {
  43. asm volatile (
  44. "sbi %[port], %[bit] \n\t"
  45. ".rept %[onCycles] \n\t"
  46. "nop \n\t"
  47. ".endr \n\t"
  48. "cbi %[port], %[bit] \n\t"
  49. ".rept %[offCycles] \n\t"
  50. "nop \n\t"
  51. ".endr \n\t"
  52. ::
  53. [port] "I" (_SFR_IO_ADDR(PORTD)),
  54. [bit] "I" (4),
  55. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  56. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  57. }
  58. }
  59. void send_bit_d6(bool bitVal)
  60. {
  61. if(bitVal) {
  62. asm volatile (
  63. "sbi %[port], %[bit] \n\t"
  64. ".rept %[onCycles] \n\t"
  65. "nop \n\t"
  66. ".endr \n\t"
  67. "cbi %[port], %[bit] \n\t"
  68. ".rept %[offCycles] \n\t"
  69. "nop \n\t"
  70. ".endr \n\t"
  71. ::
  72. [port] "I" (_SFR_IO_ADDR(PORTD)),
  73. [bit] "I" (6),
  74. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  75. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  76. } else {
  77. asm volatile (
  78. "sbi %[port], %[bit] \n\t"
  79. ".rept %[onCycles] \n\t"
  80. "nop \n\t"
  81. ".endr \n\t"
  82. "cbi %[port], %[bit] \n\t"
  83. ".rept %[offCycles] \n\t"
  84. "nop \n\t"
  85. ".endr \n\t"
  86. ::
  87. [port] "I" (_SFR_IO_ADDR(PORTD)),
  88. [bit] "I" (6),
  89. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  90. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  91. }
  92. }
  93. void send_value(uint8_t byte, enum Device device)
  94. {
  95. for(uint8_t b = 0; b < 8; b++) {
  96. if(device == Device_STATUSLED) {
  97. send_bit_d4(byte & 0b10000000);
  98. }
  99. if(device == Device_PCBRGB) {
  100. send_bit_d6(byte & 0b10000000);
  101. }
  102. byte <<= 1;
  103. }
  104. }
  105. void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device)
  106. {
  107. send_value(r, device);
  108. send_value(g, device);
  109. send_value(b, device);
  110. }