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.

425 lines
15 KiB

  1. #include "quantum.h"
  2. #include "backlight.h"
  3. #include "backlight_driver_common.h"
  4. #include "debug.h"
  5. // This logic is a bit complex, we support 3 setups:
  6. //
  7. // 1. Hardware PWM when backlight is wired to a PWM pin.
  8. // Depending on this pin, we use a different output compare unit.
  9. // 2. Software PWM with hardware timers, but the used timer
  10. // depends on the Audio setup (Audio wins over Backlight).
  11. // 3. Full software PWM, driven by the matrix scan, if both timers are used by Audio.
  12. #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)
  13. # define HARDWARE_PWM
  14. # define ICRx ICR1
  15. # define TCCRxA TCCR1A
  16. # define TCCRxB TCCR1B
  17. # define TIMERx_OVF_vect TIMER1_OVF_vect
  18. # define TIMSKx TIMSK1
  19. # define TOIEx TOIE1
  20. # if BACKLIGHT_PIN == B5
  21. # define COMxx0 COM1A0
  22. # define COMxx1 COM1A1
  23. # define OCRxx OCR1A
  24. # elif BACKLIGHT_PIN == B6
  25. # define COMxx0 COM1B0
  26. # define COMxx1 COM1B1
  27. # define OCRxx OCR1B
  28. # elif BACKLIGHT_PIN == B7
  29. # define COMxx0 COM1C0
  30. # define COMxx1 COM1C1
  31. # define OCRxx OCR1C
  32. # endif
  33. #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)
  34. # define HARDWARE_PWM
  35. # define ICRx ICR3
  36. # define TCCRxA TCCR3A
  37. # define TCCRxB TCCR3B
  38. # define TIMERx_OVF_vect TIMER3_OVF_vect
  39. # define TIMSKx TIMSK3
  40. # define TOIEx TOIE3
  41. # if BACKLIGHT_PIN == C4
  42. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  43. # error This MCU has no C4 pin!
  44. # else
  45. # define COMxx0 COM3C0
  46. # define COMxx1 COM3C1
  47. # define OCRxx OCR3C
  48. # endif
  49. # elif BACKLIGHT_PIN == C5
  50. # if (defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
  51. # error This MCU has no C5 pin!
  52. # else
  53. # define COMxx0 COM3B0
  54. # define COMxx1 COM3B1
  55. # define OCRxx OCR3B
  56. # endif
  57. # elif BACKLIGHT_PIN == C6
  58. # define COMxx0 COM3A0
  59. # define COMxx1 COM3A1
  60. # define OCRxx OCR3A
  61. # endif
  62. #elif (defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega32U2__)) && (BACKLIGHT_PIN == B7 || BACKLIGHT_PIN == C5 || BACKLIGHT_PIN == C6)
  63. # define HARDWARE_PWM
  64. # define ICRx ICR1
  65. # define TCCRxA TCCR1A
  66. # define TCCRxB TCCR1B
  67. # define TIMERx_OVF_vect TIMER1_OVF_vect
  68. # define TIMSKx TIMSK1
  69. # define TOIEx TOIE1
  70. # if BACKLIGHT_PIN == B7
  71. # define COMxx0 COM1C0
  72. # define COMxx1 COM1C1
  73. # define OCRxx OCR1C
  74. # elif BACKLIGHT_PIN == C5
  75. # define COMxx0 COM1B0
  76. # define COMxx1 COM1B1
  77. # define OCRxx OCR1B
  78. # elif BACKLIGHT_PIN == C6
  79. # define COMxx0 COM1A0
  80. # define COMxx1 COM1A1
  81. # define OCRxx OCR1A
  82. # endif
  83. #elif defined(__AVR_ATmega32A__) && (BACKLIGHT_PIN == D4 || BACKLIGHT_PIN == D5)
  84. # define HARDWARE_PWM
  85. # define ICRx ICR1
  86. # define TCCRxA TCCR1A
  87. # define TCCRxB TCCR1B
  88. # define TIMERx_OVF_vect TIMER1_OVF_vect
  89. # define TIMSKx TIMSK
  90. # define TOIEx TOIE1
  91. # if BACKLIGHT_PIN == D4
  92. # define COMxx0 COM1B0
  93. # define COMxx1 COM1B1
  94. # define OCRxx OCR1B
  95. # elif BACKLIGHT_PIN == D5
  96. # define COMxx0 COM1A0
  97. # define COMxx1 COM1A1
  98. # define OCRxx OCR1A
  99. # endif
  100. #elif defined(__AVR_ATmega328P__) && (BACKLIGHT_PIN == B1 || BACKLIGHT_PIN == B2)
  101. # define HARDWARE_PWM
  102. # define ICRx ICR1
  103. # define TCCRxA TCCR1A
  104. # define TCCRxB TCCR1B
  105. # define TIMERx_OVF_vect TIMER1_OVF_vect
  106. # define TIMSKx TIMSK1
  107. # define TOIEx TOIE1
  108. # if BACKLIGHT_PIN == B1
  109. # define COMxx0 COM1A0
  110. # define COMxx1 COM1A1
  111. # define OCRxx OCR1A
  112. # elif BACKLIGHT_PIN == B2
  113. # define COMxx0 COM1B0
  114. # define COMxx1 COM1B1
  115. # define OCRxx OCR1B
  116. # endif
  117. #elif !defined(B5_AUDIO) && !defined(B6_AUDIO) && !defined(B7_AUDIO)
  118. // Timer 1 is not in use by Audio feature, Backlight can use it
  119. # pragma message "Using hardware timer 1 with software PWM"
  120. # define HARDWARE_PWM
  121. # define BACKLIGHT_PWM_TIMER
  122. # define ICRx ICR1
  123. # define TCCRxA TCCR1A
  124. # define TCCRxB TCCR1B
  125. # define TIMERx_COMPA_vect TIMER1_COMPA_vect
  126. # define TIMERx_OVF_vect TIMER1_OVF_vect
  127. # if defined(__AVR_ATmega32A__) // This MCU has only one TIMSK register
  128. # define TIMSKx TIMSK
  129. # else
  130. # define TIMSKx TIMSK1
  131. # endif
  132. # define TOIEx TOIE1
  133. # define OCIExA OCIE1A
  134. # define OCRxx OCR1A
  135. #elif !defined(C6_AUDIO) && !defined(C5_AUDIO) && !defined(C4_AUDIO)
  136. # pragma message "Using hardware timer 3 with software PWM"
  137. // Timer 3 is not in use by Audio feature, Backlight can use it
  138. # define HARDWARE_PWM
  139. # define BACKLIGHT_PWM_TIMER
  140. # define ICRx ICR1
  141. # define TCCRxA TCCR3A
  142. # define TCCRxB TCCR3B
  143. # define TIMERx_COMPA_vect TIMER3_COMPA_vect
  144. # define TIMERx_OVF_vect TIMER3_OVF_vect
  145. # define TIMSKx TIMSK3
  146. # define TOIEx TOIE3
  147. # define OCIExA OCIE3A
  148. # define OCRxx OCR3A
  149. #elif defined(BACKLIGHT_CUSTOM_DRIVER)
  150. error("Please set 'BACKLIGHT_DRIVER = custom' within rules.mk")
  151. #else
  152. error("Please set 'BACKLIGHT_DRIVER = software' within rules.mk")
  153. #endif
  154. #ifndef BACKLIGHT_PWM_TIMER // pwm through software
  155. static inline void enable_pwm(void) {
  156. # if BACKLIGHT_ON_STATE == 1
  157. TCCRxA |= _BV(COMxx1);
  158. # else
  159. TCCRxA |= _BV(COMxx1) | _BV(COMxx0);
  160. # endif
  161. }
  162. static inline void disable_pwm(void) {
  163. # if BACKLIGHT_ON_STATE == 1
  164. TCCRxA &= ~(_BV(COMxx1));
  165. # else
  166. TCCRxA &= ~(_BV(COMxx1) | _BV(COMxx0));
  167. # endif
  168. }
  169. #endif
  170. #ifdef BACKLIGHT_PWM_TIMER
  171. // The idea of software PWM assisted by hardware timers is the following
  172. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  173. // instead of letting the Output Match Comparator control the led pin
  174. // (which is not possible since the backlight is not wired to PWM pins on the
  175. // CPU), we do the LED on/off by oursleves.
  176. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  177. // register to the current 16bits backlight level (after CIE correction).
  178. // This means the CPU will trigger a compare match interrupt when the counter
  179. // reaches the backlight level, where we turn off the LEDs,
  180. // but also an overflow interrupt when the counter rolls back to 0,
  181. // in which we're going to turn on the LEDs.
  182. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  183. // Triggered when the counter reaches the OCRx value
  184. ISR(TIMERx_COMPA_vect) { backlight_pins_off(); }
  185. // Triggered when the counter reaches the TOP value
  186. // this one triggers at F_CPU/65536 =~ 244 Hz
  187. ISR(TIMERx_OVF_vect) {
  188. # ifdef BACKLIGHT_BREATHING
  189. if (is_breathing()) {
  190. breathing_task();
  191. }
  192. # endif
  193. // for very small values of OCRxx (or backlight level)
  194. // we can't guarantee this whole code won't execute
  195. // at the same time as the compare match interrupt
  196. // which means that we might turn on the leds while
  197. // trying to turn them off, leading to flickering
  198. // artifacts (especially while breathing, because breathing_task
  199. // takes many computation cycles).
  200. // so better not turn them on while the counter TOP is very low.
  201. if (OCRxx > 256) {
  202. backlight_pins_on();
  203. }
  204. }
  205. #endif
  206. #define TIMER_TOP 0xFFFFU
  207. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  208. static uint16_t cie_lightness(uint16_t v) {
  209. if (v <= 5243) // if below 8% of max
  210. return v / 9; // same as dividing by 900%
  211. else {
  212. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  213. // to get a useful result with integer division, we shift left in the expression above
  214. // and revert what we've done again after squaring.
  215. y = y * y * y >> 8;
  216. if (y > 0xFFFFUL) // prevent overflow
  217. return 0xFFFFU;
  218. else
  219. return (uint16_t)y;
  220. }
  221. }
  222. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  223. static inline void set_pwm(uint16_t val) { OCRxx = val; }
  224. void backlight_set(uint8_t level) {
  225. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  226. if (level == 0) {
  227. #ifdef BACKLIGHT_PWM_TIMER
  228. if (OCRxx) {
  229. TIMSKx &= ~(_BV(OCIExA));
  230. TIMSKx &= ~(_BV(TOIEx));
  231. }
  232. #else
  233. // Turn off PWM control on backlight pin
  234. disable_pwm();
  235. #endif
  236. backlight_pins_off();
  237. } else {
  238. #ifdef BACKLIGHT_PWM_TIMER
  239. if (!OCRxx) {
  240. TIMSKx |= _BV(OCIExA);
  241. TIMSKx |= _BV(TOIEx);
  242. }
  243. #else
  244. // Turn on PWM control of backlight pin
  245. enable_pwm();
  246. #endif
  247. }
  248. // Set the brightness
  249. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  250. }
  251. void backlight_task(void) {}
  252. #ifdef BACKLIGHT_BREATHING
  253. # define BREATHING_NO_HALT 0
  254. # define BREATHING_HALT_OFF 1
  255. # define BREATHING_HALT_ON 2
  256. # define BREATHING_STEPS 128
  257. static uint8_t breathing_halt = BREATHING_NO_HALT;
  258. static uint16_t breathing_counter = 0;
  259. # ifdef BACKLIGHT_PWM_TIMER
  260. static bool breathing = false;
  261. bool is_breathing(void) { return breathing; }
  262. # define breathing_interrupt_enable() \
  263. do { \
  264. breathing = true; \
  265. } while (0)
  266. # define breathing_interrupt_disable() \
  267. do { \
  268. breathing = false; \
  269. } while (0)
  270. # else
  271. bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
  272. # define breathing_interrupt_enable() \
  273. do { \
  274. TIMSKx |= _BV(TOIEx); \
  275. } while (0)
  276. # define breathing_interrupt_disable() \
  277. do { \
  278. TIMSKx &= ~_BV(TOIEx); \
  279. } while (0)
  280. # endif
  281. # define breathing_min() \
  282. do { \
  283. breathing_counter = 0; \
  284. } while (0)
  285. # define breathing_max() \
  286. do { \
  287. breathing_counter = get_breathing_period() * 244 / 2; \
  288. } while (0)
  289. void breathing_enable(void) {
  290. breathing_counter = 0;
  291. breathing_halt = BREATHING_NO_HALT;
  292. breathing_interrupt_enable();
  293. }
  294. void breathing_pulse(void) {
  295. if (get_backlight_level() == 0)
  296. breathing_min();
  297. else
  298. breathing_max();
  299. breathing_halt = BREATHING_HALT_ON;
  300. breathing_interrupt_enable();
  301. }
  302. void breathing_disable(void) {
  303. breathing_interrupt_disable();
  304. // Restore backlight level
  305. backlight_set(get_backlight_level());
  306. }
  307. void breathing_self_disable(void) {
  308. if (get_backlight_level() == 0)
  309. breathing_halt = BREATHING_HALT_OFF;
  310. else
  311. breathing_halt = BREATHING_HALT_ON;
  312. }
  313. /* To generate breathing curve in python:
  314. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  315. */
  316. 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};
  317. // Use this before the cie_lightness function.
  318. static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
  319. # ifdef BACKLIGHT_PWM_TIMER
  320. void breathing_task(void)
  321. # else
  322. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  323. * about 244 times per second.
  324. */
  325. ISR(TIMERx_OVF_vect)
  326. # endif
  327. {
  328. uint8_t breathing_period = get_breathing_period();
  329. uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
  330. // resetting after one period to prevent ugly reset at overflow.
  331. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  332. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  333. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  334. breathing_interrupt_disable();
  335. }
  336. set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  337. }
  338. #endif // BACKLIGHT_BREATHING
  339. void backlight_init_ports(void) {
  340. // Setup backlight pin as output and output to on state.
  341. backlight_pins_init();
  342. // I could write a wall of text here to explain... but TL;DW
  343. // Go read the ATmega32u4 datasheet.
  344. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  345. #ifdef BACKLIGHT_PWM_TIMER
  346. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  347. TCCRxA = _BV(WGM11); // = 0b00000010;
  348. // clock select clk/1
  349. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  350. #else // hardware PWM
  351. // Pin PB7 = OCR1C (Timer 1, Channel C)
  352. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  353. // (i.e. start high, go low when counter matches.)
  354. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  355. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  356. /*
  357. 14.8.3:
  358. "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 [..]."
  359. "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)."
  360. */
  361. # if BACKLIGHT_ON_STATE == 1
  362. TCCRxA = _BV(COMxx1) | _BV(WGM11);
  363. # else
  364. TCCRxA = _BV(COMxx1) | _BV(COMxx0) | _BV(WGM11);
  365. # endif
  366. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
  367. #endif
  368. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  369. ICRx = TIMER_TOP;
  370. backlight_init();
  371. #ifdef BACKLIGHT_BREATHING
  372. if (is_backlight_breathing()) {
  373. breathing_enable();
  374. }
  375. #endif
  376. }