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.

113 lines
3.0 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 "indicator_leds.h"
  15. #include <avr/interrupt.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #define LED_T1H 900
  19. #define LED_T1L 600
  20. #define LED_T0H 400
  21. #define LED_T0L 900
  22. void send_bit_d4(bool bitVal)
  23. {
  24. if(bitVal) {
  25. asm volatile (
  26. "sbi %[port], %[bit] \n\t"
  27. ".rept %[onCycles] \n\t"
  28. "nop \n\t"
  29. ".endr \n\t"
  30. "cbi %[port], %[bit] \n\t"
  31. ".rept %[offCycles] \n\t"
  32. "nop \n\t"
  33. ".endr \n\t"
  34. ::
  35. [port] "I" (_SFR_IO_ADDR(PORTD)),
  36. [bit] "I" (4),
  37. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  38. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  39. } else {
  40. asm volatile (
  41. "sbi %[port], %[bit] \n\t"
  42. ".rept %[onCycles] \n\t"
  43. "nop \n\t"
  44. ".endr \n\t"
  45. "cbi %[port], %[bit] \n\t"
  46. ".rept %[offCycles] \n\t"
  47. "nop \n\t"
  48. ".endr \n\t"
  49. ::
  50. [port] "I" (_SFR_IO_ADDR(PORTD)),
  51. [bit] "I" (4),
  52. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  53. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  54. }
  55. }
  56. void send_bit_d6(bool bitVal)
  57. {
  58. if(bitVal) {
  59. asm volatile (
  60. "sbi %[port], %[bit] \n\t"
  61. ".rept %[onCycles] \n\t"
  62. "nop \n\t"
  63. ".endr \n\t"
  64. "cbi %[port], %[bit] \n\t"
  65. ".rept %[offCycles] \n\t"
  66. "nop \n\t"
  67. ".endr \n\t"
  68. ::
  69. [port] "I" (_SFR_IO_ADDR(PORTD)),
  70. [bit] "I" (6),
  71. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  72. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  73. } else {
  74. asm volatile (
  75. "sbi %[port], %[bit] \n\t"
  76. ".rept %[onCycles] \n\t"
  77. "nop \n\t"
  78. ".endr \n\t"
  79. "cbi %[port], %[bit] \n\t"
  80. ".rept %[offCycles] \n\t"
  81. "nop \n\t"
  82. ".endr \n\t"
  83. ::
  84. [port] "I" (_SFR_IO_ADDR(PORTD)),
  85. [bit] "I" (6),
  86. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  87. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  88. }
  89. }
  90. void send_value(uint8_t byte, enum Device device)
  91. {
  92. for(uint8_t b = 0; b < 8; b++) {
  93. if(device == Device_STATUSLED) {
  94. send_bit_d4(byte & 0b10000000);
  95. }
  96. if(device == Device_PCBRGB) {
  97. send_bit_d6(byte & 0b10000000);
  98. }
  99. byte <<= 1;
  100. }
  101. }
  102. void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device)
  103. {
  104. send_value(r, device);
  105. send_value(g, device);
  106. send_value(b, device);
  107. }