planck.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "planck.h"
  2. __attribute__ ((weak))
  3. void matrix_init_user(void) {}
  4. __attribute__ ((weak))
  5. void matrix_scan_user(void) {}
  6. __attribute__ ((weak))
  7. void process_action_user(keyrecord_t *record) {}
  8. void matrix_init_kb(void) {
  9. #ifdef BACKLIGHT_ENABLE
  10. backlight_init_ports();
  11. #endif
  12. #ifdef RGBLIGHT_ENABLE
  13. rgblight_init();
  14. #endif
  15. // Turn status LED on
  16. DDRE |= (1<<6);
  17. PORTE |= (1<<6);
  18. matrix_init_user();
  19. }
  20. void matrix_scan_kb(void) {
  21. matrix_scan_user();
  22. }
  23. void process_action_kb(keyrecord_t *record) {
  24. process_action_user(record);
  25. }
  26. #ifdef BACKLIGHT_ENABLE
  27. #define CHANNEL OCR1C
  28. void backlight_init_ports()
  29. {
  30. // Setup PB7 as output and output low.
  31. DDRB |= (1<<7);
  32. PORTB &= ~(1<<7);
  33. // Use full 16-bit resolution.
  34. ICR1 = 0xFFFF;
  35. // I could write a wall of text here to explain... but TL;DW
  36. // Go read the ATmega32u4 datasheet.
  37. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  38. // Pin PB7 = OCR1C (Timer 1, Channel C)
  39. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  40. // (i.e. start high, go low when counter matches.)
  41. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  42. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  43. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  44. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  45. backlight_init();
  46. }
  47. void backlight_set(uint8_t level)
  48. {
  49. if ( level == 0 )
  50. {
  51. // Turn off PWM control on PB7, revert to output low.
  52. TCCR1A &= ~(_BV(COM1C1));
  53. CHANNEL = 0x0;
  54. // Prevent backlight blink on lowest level
  55. PORTB &= ~(_BV(PORTB7));
  56. }
  57. else if ( level == BACKLIGHT_LEVELS )
  58. {
  59. // Prevent backlight blink on lowest level
  60. PORTB &= ~(_BV(PORTB7));
  61. // Turn on PWM control of PB7
  62. TCCR1A |= _BV(COM1C1);
  63. // Set the brightness
  64. CHANNEL = 0xFFFF;
  65. }
  66. else
  67. {
  68. // Prevent backlight blink on lowest level
  69. PORTB &= ~(_BV(PORTB7));
  70. // Turn on PWM control of PB7
  71. TCCR1A |= _BV(COM1C1);
  72. // Set the brightness
  73. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  74. }
  75. }
  76. #endif