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.

215 lines
5.1 KiB

  1. /**
  2. * Backlighting code for PS2AVRGB boards (ATMEGA32A)
  3. * Kenneth A. (github.com/krusli | krusli.me)
  4. Modified by Wayne K Jones (github.com/WarmCatUK) 2018
  5. */
  6. #include "backlight.h"
  7. #include "quantum.h"
  8. #include <avr/pgmspace.h>
  9. #include <avr/interrupt.h>
  10. #include "backlight_custom.h"
  11. #include "breathing_custom.h"
  12. // DEBUG
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. // Port D: digital pins of the AVR chipset
  16. //#define NUMLOCK_PORT (1 << 2) // 2nd pin of Port D (digital)
  17. #define CAPSLOCK_PORT (1 << 1) // 1st pin
  18. #define BACKLIGHT_PORT (1 << 4) // 4th pin
  19. //#define SCROLLLOCK_PORT (1 << 6) // 6th pin
  20. #define TIMER_CLK_DIV64 0x03 ///< Timer clocked at F_CPU/64
  21. #define TIMER1PRESCALE TIMER_CLK_DIV64 ///< timer 1 prescaler default
  22. #define TIMER_PRESCALE_MASK 0x07 ///< Timer Prescaler Bit-Mask
  23. #define PWM_MAX 0xFF
  24. #define TIMER_TOP 255 // 8 bit PWM
  25. extern backlight_config_t backlight_config;
  26. /**
  27. * References
  28. * Port Registers: https://www.arduino.cc/en/Reference/PortManipulation
  29. * TCCR1A: https://electronics.stackexchange.com/questions/92350/what-is-the-difference-between-tccr1a-and-tccr1b
  30. * Timers: http://www.avrbeginners.net/architecture/timers/timers.html
  31. * 16-bit timer setup: http://sculland.com/ATmega168/Interrupts-And-Timers/16-Bit-Timer-Setup/
  32. * PS2AVRGB firmware: https://github.com/showjean/ps2avrU/tree/master/firmware
  33. */
  34. // @Override
  35. // turn LEDs on and off depending on USB caps/num/scroll lock states.
  36. void led_set_user(uint8_t usb_led) {
  37. /*
  38. if (usb_led & (1 << USB_LED_NUM_LOCK)) {
  39. // turn on
  40. DDRD |= NUMLOCK_PORT;
  41. PORTD |= NUMLOCK_PORT;
  42. } else {
  43. // turn off
  44. DDRD &= ~NUMLOCK_PORT;
  45. PORTD &= ~NUMLOCK_PORT;
  46. }
  47. */
  48. if (usb_led & (1 << USB_LED_CAPS_LOCK)) {
  49. DDRD |= CAPSLOCK_PORT;
  50. PORTD |= CAPSLOCK_PORT;
  51. } else {
  52. DDRD &= ~CAPSLOCK_PORT;
  53. PORTD &= ~CAPSLOCK_PORT;
  54. }
  55. /*
  56. if (usb_led & (1 << USB_LED_SCROLL_LOCK)) {
  57. DDRD |= SCROLLLOCK_PORT;
  58. PORTD |= SCROLLLOCK_PORT;
  59. } else {
  60. DDRD &= ~SCROLLLOCK_PORT;
  61. PORTD &= ~SCROLLLOCK_PORT;
  62. }
  63. */
  64. }
  65. #ifdef BACKLIGHT_ENABLE
  66. // sets up Timer 1 for 8-bit PWM
  67. void timer1PWMSetup(void) { // NOTE ONLY CALL THIS ONCE
  68. // default 8 bit mode
  69. TCCR1A &= ~(1 << 1); // cbi(TCCR1A,PWM11); <- set PWM11 bit to HIGH
  70. TCCR1A |= (1 << 0); // sbi(TCCR1A,PWM10); <- set PWM10 bit to LOW
  71. // clear output compare value A
  72. // outb(OCR1AH, 0);
  73. // outb(OCR1AL, 0);
  74. // clear output comparator registers for B
  75. OCR1BH = 0; // outb(OCR1BH, 0);
  76. OCR1BL = 0; // outb(OCR1BL, 0);
  77. }
  78. bool is_init = false;
  79. void timer1Init(void) {
  80. // timer1SetPrescaler(TIMER1PRESCALE)
  81. // set to DIV/64
  82. (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | TIMER1PRESCALE;
  83. // reset TCNT1
  84. TCNT1H = 0; // outb(TCNT1H, 0);
  85. TCNT1L = 0; // outb(TCNT1L, 0);
  86. // TOIE1: Timer Overflow Interrupt Enable (Timer 1);
  87. TIMSK |= _BV(TOIE1); // sbi(TIMSK, TOIE1);
  88. is_init = true;
  89. }
  90. void timer1UnInit(void) {
  91. // set prescaler back to NONE
  92. (TCCR1B) = ((TCCR1B) & ~TIMER_PRESCALE_MASK) | 0x00; // TIMERRTC_CLK_STOP
  93. // disable timer overflow interrupt
  94. TIMSK &= ~_BV(TOIE1); // overflow bit?
  95. setPWM(0);
  96. is_init = false;
  97. }
  98. // handle TCNT1 overflow
  99. //! Interrupt handler for tcnt1 overflow interrupt
  100. ISR(TIMER1_OVF_vect, ISR_NOBLOCK)
  101. {
  102. // sei();
  103. // handle breathing here
  104. #ifdef BACKLIGHT_BREATHING
  105. if (is_breathing()) {
  106. custom_breathing_handler();
  107. }
  108. #endif
  109. // TODO call user defined function
  110. }
  111. // enable timer 1 PWM
  112. // timer1PWMBOn()
  113. void timer1PWMBEnable(void) {
  114. // turn on channel B (OC1B) PWM output
  115. // set OC1B as non-inverted PWM
  116. TCCR1A |= _BV(COM1B1);
  117. TCCR1A &= ~_BV(COM1B0);
  118. }
  119. // disable timer 1 PWM
  120. // timer1PWMBOff()
  121. void timer1PWMBDisable(void) {
  122. TCCR1A &= ~_BV(COM1B1);
  123. TCCR1A &= ~_BV(COM1B0);
  124. }
  125. void enableBacklight(void) {
  126. DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
  127. PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
  128. }
  129. void disableBacklight(void) {
  130. // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
  131. PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
  132. }
  133. void startPWM(void) {
  134. timer1Init();
  135. timer1PWMBEnable();
  136. enableBacklight();
  137. }
  138. void stopPWM(void) {
  139. timer1UnInit();
  140. disableBacklight();
  141. timer1PWMBDisable();
  142. }
  143. void b_led_init_ports(void) {
  144. /* turn backlight on/off depending on user preference */
  145. #if BACKLIGHT_ON_STATE == 0
  146. // DDRx register: sets the direction of Port D
  147. // DDRD &= ~BACKLIGHT_PORT; // set digital pin 4 as input
  148. PORTD &= ~BACKLIGHT_PORT; // set digital pin 4 to low
  149. #else
  150. DDRD |= BACKLIGHT_PORT; // set digital pin 4 as output
  151. PORTD |= BACKLIGHT_PORT; // set digital pin 4 to high
  152. #endif
  153. timer1PWMSetup();
  154. startPWM();
  155. #ifdef BACKLIGHT_BREATHING
  156. breathing_enable();
  157. #endif
  158. }
  159. void b_led_set(uint8_t level) {
  160. if (level > BACKLIGHT_LEVELS) {
  161. level = BACKLIGHT_LEVELS;
  162. }
  163. setPWM((int)(TIMER_TOP * (float) level / BACKLIGHT_LEVELS));
  164. }
  165. // called every matrix scan
  166. void b_led_task(void) {
  167. // do nothing for now
  168. }
  169. void setPWM(uint16_t xValue) {
  170. if (xValue > TIMER_TOP) {
  171. xValue = TIMER_TOP;
  172. }
  173. OCR1B = xValue; // timer1PWMBSet(xValue);
  174. }
  175. #endif // BACKLIGHT_ENABLE