backlight_avr.c 15 KB

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