backlight_avr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. 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. uint8_t backlight_tick = 0;
  197. # ifndef BACKLIGHT_CUSTOM_DRIVER
  198. void backlight_task(void) {
  199. if ((0xFFFF >> ((BACKLIGHT_LEVELS - get_backlight_level()) * ((BACKLIGHT_LEVELS + 1) / 2))) & (1 << backlight_tick)) {
  200. FOR_EACH_LED(backlight_on(backlight_pin);)
  201. } else {
  202. FOR_EACH_LED(backlight_off(backlight_pin);)
  203. }
  204. backlight_tick = (backlight_tick + 1) % 16;
  205. }
  206. # endif
  207. # ifdef BACKLIGHT_BREATHING
  208. # ifndef BACKLIGHT_CUSTOM_DRIVER
  209. # error "Backlight breathing only available with hardware PWM. Please disable."
  210. # endif
  211. # endif
  212. # else // hardware pwm through timer
  213. # ifdef BACKLIGHT_PWM_TIMER
  214. // The idea of software PWM assisted by hardware timers is the following
  215. // we use the hardware timer in fast PWM mode like for hardware PWM, but
  216. // instead of letting the Output Match Comparator control the led pin
  217. // (which is not possible since the backlight is not wired to PWM pins on the
  218. // CPU), we do the LED on/off by oursleves.
  219. // The timer is setup to count up to 0xFFFF, and we set the Output Compare
  220. // register to the current 16bits backlight level (after CIE correction).
  221. // This means the CPU will trigger a compare match interrupt when the counter
  222. // reaches the backlight level, where we turn off the LEDs,
  223. // but also an overflow interrupt when the counter rolls back to 0,
  224. // in which we're going to turn on the LEDs.
  225. // The LED will then be on for OCRxx/0xFFFF time, adjusted every 244Hz.
  226. // Triggered when the counter reaches the OCRx value
  227. ISR(TIMERx_COMPA_vect) { FOR_EACH_LED(backlight_off(backlight_pin);) }
  228. // Triggered when the counter reaches the TOP value
  229. // this one triggers at F_CPU/65536 =~ 244 Hz
  230. ISR(TIMERx_OVF_vect) {
  231. # ifdef BACKLIGHT_BREATHING
  232. if (is_breathing()) {
  233. breathing_task();
  234. }
  235. # endif
  236. // for very small values of OCRxx (or backlight level)
  237. // we can't guarantee this whole code won't execute
  238. // at the same time as the compare match interrupt
  239. // which means that we might turn on the leds while
  240. // trying to turn them off, leading to flickering
  241. // artifacts (especially while breathing, because breathing_task
  242. // takes many computation cycles).
  243. // so better not turn them on while the counter TOP is very low.
  244. if (OCRxx > 256) {
  245. FOR_EACH_LED(backlight_on(backlight_pin);)
  246. }
  247. }
  248. # endif
  249. # define TIMER_TOP 0xFFFFU
  250. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  251. static uint16_t cie_lightness(uint16_t v) {
  252. if (v <= 5243) // if below 8% of max
  253. return v / 9; // same as dividing by 900%
  254. else {
  255. uint32_t y = (((uint32_t)v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  256. // to get a useful result with integer division, we shift left in the expression above
  257. // and revert what we've done again after squaring.
  258. y = y * y * y >> 8;
  259. if (y > 0xFFFFUL) // prevent overflow
  260. return 0xFFFFU;
  261. else
  262. return (uint16_t)y;
  263. }
  264. }
  265. // range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
  266. static inline void set_pwm(uint16_t val) { OCRxx = val; }
  267. # ifndef BACKLIGHT_CUSTOM_DRIVER
  268. void backlight_set(uint8_t level) {
  269. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  270. if (level == 0) {
  271. # ifdef BACKLIGHT_PWM_TIMER
  272. if (OCRxx) {
  273. TIMSKx &= ~(_BV(OCIExA));
  274. TIMSKx &= ~(_BV(TOIEx));
  275. FOR_EACH_LED(backlight_off(backlight_pin);)
  276. }
  277. # else
  278. // Turn off PWM control on backlight pin
  279. TCCRxA &= ~(_BV(COMxx1));
  280. # endif
  281. } else {
  282. # ifdef BACKLIGHT_PWM_TIMER
  283. if (!OCRxx) {
  284. TIMSKx |= _BV(OCIExA);
  285. TIMSKx |= _BV(TOIEx);
  286. }
  287. # else
  288. // Turn on PWM control of backlight pin
  289. TCCRxA |= _BV(COMxx1);
  290. # endif
  291. }
  292. // Set the brightness
  293. set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
  294. }
  295. void backlight_task(void) {}
  296. # endif // BACKLIGHT_CUSTOM_DRIVER
  297. # ifdef BACKLIGHT_BREATHING
  298. # define BREATHING_NO_HALT 0
  299. # define BREATHING_HALT_OFF 1
  300. # define BREATHING_HALT_ON 2
  301. # define BREATHING_STEPS 128
  302. static uint8_t breathing_halt = BREATHING_NO_HALT;
  303. static uint16_t breathing_counter = 0;
  304. # ifdef BACKLIGHT_PWM_TIMER
  305. static bool breathing = false;
  306. bool is_breathing(void) { return breathing; }
  307. # define breathing_interrupt_enable() \
  308. do { \
  309. breathing = true; \
  310. } while (0)
  311. # define breathing_interrupt_disable() \
  312. do { \
  313. breathing = false; \
  314. } while (0)
  315. # else
  316. bool is_breathing(void) { return !!(TIMSKx & _BV(TOIEx)); }
  317. # define breathing_interrupt_enable() \
  318. do { \
  319. TIMSKx |= _BV(TOIEx); \
  320. } while (0)
  321. # define breathing_interrupt_disable() \
  322. do { \
  323. TIMSKx &= ~_BV(TOIEx); \
  324. } while (0)
  325. # endif
  326. # define breathing_min() \
  327. do { \
  328. breathing_counter = 0; \
  329. } while (0)
  330. # define breathing_max() \
  331. do { \
  332. breathing_counter = get_breathing_period() * 244 / 2; \
  333. } while (0)
  334. void breathing_enable(void) {
  335. breathing_counter = 0;
  336. breathing_halt = BREATHING_NO_HALT;
  337. breathing_interrupt_enable();
  338. }
  339. void breathing_pulse(void) {
  340. if (get_backlight_level() == 0)
  341. breathing_min();
  342. else
  343. breathing_max();
  344. breathing_halt = BREATHING_HALT_ON;
  345. breathing_interrupt_enable();
  346. }
  347. void breathing_disable(void) {
  348. breathing_interrupt_disable();
  349. // Restore backlight level
  350. backlight_set(get_backlight_level());
  351. }
  352. void breathing_self_disable(void) {
  353. if (get_backlight_level() == 0)
  354. breathing_halt = BREATHING_HALT_OFF;
  355. else
  356. breathing_halt = BREATHING_HALT_ON;
  357. }
  358. void breathing_toggle(void) {
  359. if (is_breathing())
  360. breathing_disable();
  361. else
  362. breathing_enable();
  363. }
  364. /* To generate breathing curve in python:
  365. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  366. */
  367. 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};
  368. // Use this before the cie_lightness function.
  369. static inline uint16_t scale_backlight(uint16_t v) { return v / BACKLIGHT_LEVELS * get_backlight_level(); }
  370. # ifdef BACKLIGHT_PWM_TIMER
  371. void breathing_task(void)
  372. # else
  373. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  374. * about 244 times per second.
  375. */
  376. ISR(TIMERx_OVF_vect)
  377. # endif
  378. {
  379. uint8_t breathing_period = get_breathing_period();
  380. uint16_t interval = (uint16_t)breathing_period * 244 / BREATHING_STEPS;
  381. // resetting after one period to prevent ugly reset at overflow.
  382. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  383. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  384. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  385. breathing_interrupt_disable();
  386. }
  387. set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  388. }
  389. # endif // BACKLIGHT_BREATHING
  390. void backlight_init_ports(void) {
  391. // Setup backlight pin as output and output to on state.
  392. FOR_EACH_LED(setPinOutput(backlight_pin); backlight_on(backlight_pin);)
  393. // I could write a wall of text here to explain... but TL;DW
  394. // Go read the ATmega32u4 datasheet.
  395. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  396. # ifdef BACKLIGHT_PWM_TIMER
  397. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  398. TCCRxA = _BV(WGM11); // = 0b00000010;
  399. // clock select clk/1
  400. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  401. # else // hardware PWM
  402. // Pin PB7 = OCR1C (Timer 1, Channel C)
  403. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  404. // (i.e. start high, go low when counter matches.)
  405. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  406. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  407. /*
  408. 14.8.3:
  409. "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 [..]."
  410. "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)."
  411. */
  412. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  413. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  414. # endif
  415. // Use full 16-bit resolution. Counter counts to ICR1 before reset to 0.
  416. ICRx = TIMER_TOP;
  417. backlight_init();
  418. # ifdef BACKLIGHT_BREATHING
  419. if (is_backlight_breathing()) {
  420. breathing_enable();
  421. }
  422. # endif
  423. }
  424. # endif // hardware backlight
  425. #endif // backlight