atomic.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "atomic.h"
  2. __attribute__ ((weak))
  3. void matrix_init_user(void) {
  4. // leave this function blank - it can be defined in a keymap file
  5. };
  6. __attribute__ ((weak))
  7. void matrix_scan_user(void) {
  8. // leave this function blank - it can be defined in a keymap file
  9. }
  10. __attribute__ ((weak))
  11. void process_action_user(keyrecord_t *record) {
  12. // leave this function blank - it can be defined in a keymap file
  13. }
  14. __attribute__ ((weak))
  15. void led_set_user(uint8_t usb_led) {
  16. // leave this function blank - it can be defined in a keymap file
  17. }
  18. void matrix_init_kb(void) {
  19. // put your keyboard start-up code here
  20. // runs once when the firmware starts up
  21. MCUCR |= (1<<JTD);
  22. MCUCR |= (1<<JTD);
  23. #ifdef BACKLIGHT_ENABLE
  24. backlight_init_ports();
  25. #endif
  26. // Turn status LED on
  27. DDRE |= (1<<6);
  28. PORTE |= (1<<6);
  29. matrix_init_user();
  30. }
  31. void matrix_scan_kb(void) {
  32. // put your looping keyboard code here
  33. // runs every cycle (a lot)
  34. matrix_scan_user();
  35. }
  36. void process_action_kb(keyrecord_t *record) {
  37. // put your per-action keyboard code here
  38. // runs for every action, just before processing by the firmware
  39. process_action_user(record);
  40. }
  41. void led_set_kb(uint8_t usb_led) {
  42. // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
  43. led_set_user(usb_led);
  44. }
  45. #ifdef BACKLIGHT_ENABLE
  46. #define CHANNEL OCR1C
  47. void backlight_init_ports()
  48. {
  49. // Setup PB7 as output and output low.
  50. DDRB |= (1<<7);
  51. PORTB &= ~(1<<7);
  52. // Use full 16-bit resolution.
  53. ICR1 = 0xFFFF;
  54. // I could write a wall of text here to explain... but TL;DW
  55. // Go read the ATmega32u4 datasheet.
  56. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  57. // Pin PB7 = OCR1C (Timer 1, Channel C)
  58. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  59. // (i.e. start high, go low when counter matches.)
  60. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  61. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  62. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  63. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  64. backlight_init();
  65. }
  66. void backlight_set(uint8_t level)
  67. {
  68. if ( level == 0 )
  69. {
  70. // Turn off PWM control on PB7, revert to output low.
  71. TCCR1A &= ~(_BV(COM1C1));
  72. CHANNEL = 0x0;
  73. // Prevent backlight blink on lowest level
  74. PORTB &= ~(_BV(PORTB7));
  75. }
  76. else if ( level == BACKLIGHT_LEVELS )
  77. {
  78. // Prevent backlight blink on lowest level
  79. PORTB &= ~(_BV(PORTB7));
  80. // Turn on PWM control of PB7
  81. TCCR1A |= _BV(COM1C1);
  82. // Set the brightness
  83. CHANNEL = 0xFFFF;
  84. }
  85. else
  86. {
  87. // Prevent backlight blink on lowest level
  88. PORTB &= ~(_BV(PORTB7));
  89. // Turn on PWM control of PB7
  90. TCCR1A |= _BV(COM1C1);
  91. // Set the brightness
  92. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  93. }
  94. }
  95. #endif