backlight_avr.c 18 KB

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