backlight_avr.c 15 KB

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