backlight_avr.c 16 KB

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