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.

327 lines
11 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. #include "backlight.h"
  2. #include "gpio.h"
  3. #include "progmem.h"
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. // Maximum duty cycle limit
  7. #ifndef BACKLIGHT_LIMIT_VAL
  8. # define BACKLIGHT_LIMIT_VAL 255
  9. #endif
  10. #if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == B5 || BACKLIGHT_PIN == B6 || BACKLIGHT_PIN == B7)
  11. # define ICRx ICR1
  12. # define TCCRxA TCCR1A
  13. # define TCCRxB TCCR1B
  14. # define TIMERx_OVF_vect TIMER1_OVF_vect
  15. # define TIMSKx TIMSK1
  16. # define TOIEx TOIE1
  17. # if BACKLIGHT_PIN == B5
  18. # define COMxx0 COM1A0
  19. # define COMxx1 COM1A1
  20. # define OCRxx OCR1A
  21. # elif BACKLIGHT_PIN == B6
  22. # define COMxx0 COM1B0
  23. # define COMxx1 COM1B1
  24. # define OCRxx OCR1B
  25. # elif BACKLIGHT_PIN == B7
  26. # define COMxx0 COM1C0
  27. # define COMxx1 COM1C1
  28. # define OCRxx OCR1C
  29. # endif
  30. #elif (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__)) && (BACKLIGHT_PIN == C4 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  31. # define ICRx ICR3
  32. # define TCCRxA TCCR3A
  33. # define TCCRxB TCCR3B
  34. # define TIMERx_OVF_vect TIMER3_OVF_vect
  35. # define TIMSKx TIMSK3
  36. # define TOIEx TOIE3
  37. # if BACKLIGHT_PIN == C4
  38. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  39. # error This MCU has no C4 pin!
  40. # else
  41. # define COMxx0 COM3C0
  42. # define COMxx1 COM3C1
  43. # define OCRxx OCR3C
  44. # endif
  45. # elif BACKLIGHT_PIN == C5
  46. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  47. # error This MCU has no C5 pin!
  48. # else
  49. # define COMxx0 COM3B0
  50. # define COMxx1 COM3B1
  51. # define OCRxx OCR3B
  52. # endif
  53. # elif BACKLIGHT_PIN == C6
  54. # define COMxx0 COM3A0
  55. # define COMxx1 COM3A1
  56. # define OCRxx OCR3A
  57. # endif
  58. #elif (defined(__AVR_AT90USB162__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  59. # define ICRx ICR1
  60. # define TCCRxA TCCR1A
  61. # define TCCRxB TCCR1B
  62. # define TIMERx_OVF_vect TIMER1_OVF_vect
  63. # define TIMSKx TIMSK1
  64. # define TOIEx TOIE1
  65. # if BACKLIGHT_PIN == B7
  66. # define COMxx0 COM1C0
  67. # define COMxx1 COM1C1
  68. # define OCRxx OCR1C
  69. # elif BACKLIGHT_PIN == C5
  70. # define COMxx0 COM1B0
  71. # define COMxx1 COM1B1
  72. # define OCRxx OCR1B
  73. # elif BACKLIGHT_PIN == C6
  74. # define COMxx0 COM1A0
  75. # define COMxx1 COM1A1
  76. # define OCRxx OCR1A
  77. # endif
  78. #elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
  79. # define ICRx ICR1
  80. # define TCCRxA TCCR1A
  81. # define TCCRxB TCCR1B
  82. # define TIMERx_OVF_vect TIMER1_OVF_vect
  83. # define TIMSKx TIMSK
  84. # define TOIEx TOIE1
  85. # if BACKLIGHT_PIN == D4
  86. # define COMxx0 COM1B0
  87. # define COMxx1 COM1B1
  88. # define OCRxx OCR1B
  89. # elif BACKLIGHT_PIN == D5
  90. # define COMxx0 COM1A0
  91. # define COMxx1 COM1A1
  92. # define OCRxx OCR1A
  93. # endif
  94. #elif (defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
  95. # define ICRx ICR1
  96. # define TCCRxA TCCR1A
  97. # define TCCRxB TCCR1B
  98. # define TIMERx_OVF_vect TIMER1_OVF_vect
  99. # define TIMSKx TIMSK1
  100. # define TOIEx TOIE1
  101. # if BACKLIGHT_PIN == B1
  102. # define COMxx0 COM1A0
  103. # define COMxx1 COM1A1
  104. # define OCRxx OCR1A
  105. # elif BACKLIGHT_PIN == B2
  106. # define COMxx0 COM1B0
  107. # define COMxx1 COM1B1
  108. # define OCRxx OCR1B
  109. # endif
  110. #endif
  111. #ifndef BACKLIGHT_RESOLUTION
  112. # define BACKLIGHT_RESOLUTION 0xFFFFU
  113. #endif
  114. #if (BACKLIGHT_RESOLUTION > 0xFFFF || BACKLIGHT_RESOLUTION < 0x00FF)
  115. # error "Backlight resolution must be between 0x00FF and 0xFFFF"
  116. #endif
  117. #define BREATHING_SCALE_FACTOR F_CPU / BACKLIGHT_RESOLUTION / 120
  118. static inline void enable_pwm(void) {
  119. #if BACKLIGHT_ON_STATE == 1
  120. TCCRxA |= _BV(COMxx1);
  121. #else
  122. TCCRxA |= _BV(COMxx1) | _BV(COMxx0);
  123. #endif
  124. }
  125. static inline void disable_pwm(void) {
  126. #if BACKLIGHT_ON_STATE == 1
  127. TCCRxA &= ~(_BV(COMxx1));
  128. #else
  129. TCCRxA &= ~(_BV(COMxx1) | _BV(COMxx0));
  130. #endif
  131. }
  132. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  133. static uint16_t cie_lightness(uint16_t v) {
  134. if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max
  135. {
  136. return v / 9; // Same as dividing by 900%
  137. } else {
  138. // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
  139. uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max)
  140. uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
  141. if (out > ICRx) // Avoid overflows
  142. {
  143. out = ICRx;
  144. }
  145. return (uint16_t)out;
  146. }
  147. }
  148. // rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
  149. static uint32_t rescale_limit_val(uint32_t val) {
  150. return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
  151. }
  152. // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
  153. static inline void set_pwm(uint16_t val) {
  154. OCRxx = val;
  155. }
  156. void backlight_set(uint8_t level) {
  157. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  158. if (level == 0) {
  159. // Turn off PWM control on backlight pin
  160. disable_pwm();
  161. } else {
  162. // Turn on PWM control of backlight pin
  163. enable_pwm();
  164. }
  165. // Set the brightness
  166. set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS)));
  167. }
  168. void backlight_task(void) {}
  169. #ifdef BACKLIGHT_BREATHING
  170. # define BREATHING_NO_HALT 0
  171. # define BREATHING_HALT_OFF 1
  172. # define BREATHING_HALT_ON 2
  173. # define BREATHING_STEPS 128
  174. static uint8_t breathing_halt = BREATHING_NO_HALT;
  175. static uint16_t breathing_counter = 0;
  176. static uint8_t breath_scale_counter = 1;
  177. /* Run the breathing loop at ~120Hz*/
  178. const uint8_t breathing_ISR_frequency = 120;
  179. bool is_breathing(void) {
  180. return !!(TIMSKx & _BV(TOIEx));
  181. }
  182. # define breathing_interrupt_enable() \
  183. do { \
  184. TIMSKx |= _BV(TOIEx); \
  185. } while (0)
  186. # define breathing_interrupt_disable() \
  187. do { \
  188. TIMSKx &= ~_BV(TOIEx); \
  189. } while (0)
  190. # define breathing_min() \
  191. do { \
  192. breathing_counter = 0; \
  193. } while (0)
  194. # define breathing_max() \
  195. do { \
  196. breathing_counter = get_breathing_period() * breathing_ISR_frequency / 2; \
  197. } while (0)
  198. void breathing_enable(void) {
  199. breathing_counter = 0;
  200. breathing_halt = BREATHING_NO_HALT;
  201. breathing_interrupt_enable();
  202. }
  203. void breathing_pulse(void) {
  204. if (get_backlight_level() == 0)
  205. breathing_min();
  206. else
  207. breathing_max();
  208. breathing_halt = BREATHING_HALT_ON;
  209. breathing_interrupt_enable();
  210. }
  211. void breathing_disable(void) {
  212. breathing_interrupt_disable();
  213. // Restore backlight level
  214. backlight_set(get_backlight_level());
  215. }
  216. void breathing_self_disable(void) {
  217. if (get_backlight_level() == 0)
  218. breathing_halt = BREATHING_HALT_OFF;
  219. else
  220. breathing_halt = BREATHING_HALT_ON;
  221. }
  222. /* To generate breathing curve in python:
  223. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  224. */
  225. static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  226. // Use this before the cie_lightness function.
  227. static inline uint16_t scale_backlight(uint16_t v) {
  228. return v / BACKLIGHT_LEVELS * get_backlight_level();
  229. }
  230. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  231. * about 244 times per second.
  232. *
  233. * The following ISR runs at F_CPU/ISRx. With a 16MHz clock and default pwm resolution, that means 244Hz
  234. */
  235. ISR(TIMERx_OVF_vect) {
  236. // Only run this ISR at ~120 Hz
  237. if (breath_scale_counter++ == BREATHING_SCALE_FACTOR) {
  238. breath_scale_counter = 1;
  239. } else {
  240. return;
  241. }
  242. uint16_t interval = (uint16_t)get_breathing_period() * breathing_ISR_frequency / BREATHING_STEPS;
  243. // resetting after one period to prevent ugly reset at overflow.
  244. breathing_counter = (breathing_counter + 1) % (get_breathing_period() * breathing_ISR_frequency);
  245. uint8_t index = breathing_counter / interval;
  246. // limit index to max step value
  247. if (index >= BREATHING_STEPS) {
  248. index = BREATHING_STEPS - 1;
  249. }
  250. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  251. breathing_interrupt_disable();
  252. }
  253. // Set PWM to a brightnessvalue scaled to the configured resolution
  254. set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint32_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255))));
  255. }
  256. #endif // BACKLIGHT_BREATHING
  257. void backlight_init_ports(void) {
  258. setPinOutput(BACKLIGHT_PIN);
  259. #if BACKLIGHT_ON_STATE == 1
  260. writePinLow(BACKLIGHT_PIN);
  261. #else
  262. writePinHigh(BACKLIGHT_PIN);
  263. #endif
  264. // I could write a wall of text here to explain... but TL;DW
  265. // Go read the ATmega32u4 datasheet.
  266. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  267. // Pin PB7 = OCR1C (Timer 1, Channel C)
  268. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  269. // (i.e. start high, go low when counter matches.)
  270. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  271. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  272. /*
  273. 14.8.3:
  274. "In fast PWM mode, the compare units allow generation of PWM waveforms on the OCnx pins. Setting the COMnx1:0 bits to two will produce a non-inverted PWM [..]."
  275. "In fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGMn3:0 = 5, 6, or 7), the value in ICRn (WGMn3:0 = 14), or the value in OCRnA (WGMn3:0 = 15)."
  276. */
  277. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  278. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  279. ICRx = BACKLIGHT_RESOLUTION;
  280. backlight_init();
  281. #ifdef BACKLIGHT_BREATHING
  282. if (is_backlight_breathing()) {
  283. breathing_enable();
  284. }
  285. #endif
  286. }