backlight_avr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. // or F_CPU/BACKLIGHT_CUSTOM_RESOLUTION if used.
  188. // Triggered when the counter reaches the OCRx value
  189. ISR(TIMERx_COMPA_vect) {
  190. backlight_pins_off();
  191. }
  192. // Triggered when the counter reaches the TOP value
  193. // this one triggers at F_CPU/ICRx = 16MHz/65536 =~ 244 Hz
  194. ISR(TIMERx_OVF_vect) {
  195. # ifdef BACKLIGHT_BREATHING
  196. if (is_breathing()) {
  197. breathing_task();
  198. }
  199. # endif
  200. // for very small values of OCRxx (or backlight level)
  201. // we can't guarantee this whole code won't execute
  202. // at the same time as the compare match interrupt
  203. // which means that we might turn on the leds while
  204. // trying to turn them off, leading to flickering
  205. // artifacts (especially while breathing, because breathing_task
  206. // takes many computation cycles).
  207. // so better not turn them on while the counter TOP is very low.
  208. if (OCRxx > ICRx / 250 + 5) {
  209. backlight_pins_on();
  210. }
  211. }
  212. #endif
  213. #define TIMER_TOP 0xFFFFU
  214. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  215. static uint16_t cie_lightness(uint16_t v) {
  216. if (v <= (uint32_t)ICRx / 12) // If the value is less than or equal to ~8% of max
  217. {
  218. return v / 9; // Same as dividing by 900%
  219. } else {
  220. // In the next two lines values are bit-shifted. This is to avoid loosing decimals in integer math.
  221. uint32_t y = (((uint32_t)v + (uint32_t)ICRx / 6) << 5) / ((uint32_t)ICRx / 6 + ICRx); // If above 8%, add ~16% of max, and normalize with (max + ~16% max)
  222. uint32_t out = (y * y * y * ICRx) >> 15; // Cube it and undo the bit-shifting. (which is now three times as much due to the cubing)
  223. if (out > ICRx) // Avoid overflows
  224. {
  225. out = ICRx;
  226. }
  227. return (uint16_t)out;
  228. }
  229. }
  230. // rescale the supplied backlight value to be in terms of the value limit // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
  231. static uint32_t rescale_limit_val(uint32_t val) {
  232. return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
  233. }
  234. // range for val is [0..ICRx]. PWM pin is high while the timer count is below val.
  235. static inline void set_pwm(uint16_t val) {
  236. OCRxx = val;
  237. }
  238. void backlight_set(uint8_t level) {
  239. if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
  240. if (level == 0) {
  241. #ifdef BACKLIGHT_PWM_TIMER
  242. if (OCRxx) {
  243. TIMSKx &= ~(_BV(OCIExA));
  244. TIMSKx &= ~(_BV(TOIEx));
  245. }
  246. #else
  247. // Turn off PWM control on backlight pin
  248. disable_pwm();
  249. #endif
  250. backlight_pins_off();
  251. } else {
  252. #ifdef BACKLIGHT_PWM_TIMER
  253. if (!OCRxx) {
  254. TIMSKx |= _BV(OCIExA);
  255. TIMSKx |= _BV(TOIEx);
  256. }
  257. #else
  258. // Turn on PWM control of backlight pin
  259. enable_pwm();
  260. #endif
  261. }
  262. // Set the brightness
  263. set_pwm(cie_lightness(rescale_limit_val(ICRx * (uint32_t)level / BACKLIGHT_LEVELS)));
  264. }
  265. void backlight_task(void) {}
  266. #ifdef BACKLIGHT_BREATHING
  267. # define BREATHING_NO_HALT 0
  268. # define BREATHING_HALT_OFF 1
  269. # define BREATHING_HALT_ON 2
  270. # define BREATHING_STEPS 128
  271. static uint8_t breathing_halt = BREATHING_NO_HALT;
  272. static uint16_t breathing_counter = 0;
  273. static uint8_t breath_scale_counter = 1;
  274. /* Run the breathing loop at ~120Hz*/
  275. const uint8_t breathing_ISR_frequency = 120;
  276. static uint16_t breathing_freq_scale_factor = 2;
  277. # ifdef BACKLIGHT_PWM_TIMER
  278. static bool breathing = false;
  279. bool is_breathing(void) {
  280. return breathing;
  281. }
  282. # define breathing_interrupt_enable() \
  283. do { \
  284. breathing = true; \
  285. } while (0)
  286. # define breathing_interrupt_disable() \
  287. do { \
  288. breathing = false; \
  289. } while (0)
  290. # else
  291. bool is_breathing(void) {
  292. return !!(TIMSKx & _BV(TOIEx));
  293. }
  294. # define breathing_interrupt_enable() \
  295. do { \
  296. TIMSKx |= _BV(TOIEx); \
  297. } while (0)
  298. # define breathing_interrupt_disable() \
  299. do { \
  300. TIMSKx &= ~_BV(TOIEx); \
  301. } while (0)
  302. # endif
  303. # define breathing_min() \
  304. do { \
  305. breathing_counter = 0; \
  306. } while (0)
  307. # define breathing_max() \
  308. do { \
  309. breathing_counter = get_breathing_period() * breathing_ISR_frequency / 2; \
  310. } while (0)
  311. void breathing_enable(void) {
  312. breathing_counter = 0;
  313. breathing_halt = BREATHING_NO_HALT;
  314. breathing_interrupt_enable();
  315. }
  316. void breathing_pulse(void) {
  317. if (get_backlight_level() == 0)
  318. breathing_min();
  319. else
  320. breathing_max();
  321. breathing_halt = BREATHING_HALT_ON;
  322. breathing_interrupt_enable();
  323. }
  324. void breathing_disable(void) {
  325. breathing_interrupt_disable();
  326. // Restore backlight level
  327. backlight_set(get_backlight_level());
  328. }
  329. void breathing_self_disable(void) {
  330. if (get_backlight_level() == 0)
  331. breathing_halt = BREATHING_HALT_OFF;
  332. else
  333. breathing_halt = BREATHING_HALT_ON;
  334. }
  335. /* To generate breathing curve in python:
  336. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  337. */
  338. 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};
  339. // Use this before the cie_lightness function.
  340. static inline uint16_t scale_backlight(uint16_t v) {
  341. return v / BACKLIGHT_LEVELS * get_backlight_level();
  342. }
  343. # ifdef BACKLIGHT_PWM_TIMER
  344. void breathing_task(void)
  345. # else
  346. /* Assuming a 16MHz CPU clock and a timer that resets at 64k (ICR1), the following interrupt handler will run
  347. * about 244 times per second.
  348. *
  349. * The following ISR runs at F_CPU/ISRx. With a 16MHz clock and default pwm resolution, that means 244Hz
  350. */
  351. ISR(TIMERx_OVF_vect)
  352. # endif
  353. {
  354. // Only run this ISR at ~120 Hz
  355. if (breath_scale_counter++ == breathing_freq_scale_factor) {
  356. breath_scale_counter = 1;
  357. } else {
  358. return;
  359. }
  360. uint16_t interval = (uint16_t)get_breathing_period() * breathing_ISR_frequency / BREATHING_STEPS;
  361. // resetting after one period to prevent ugly reset at overflow.
  362. breathing_counter = (breathing_counter + 1) % (get_breathing_period() * breathing_ISR_frequency);
  363. uint8_t index = breathing_counter / interval;
  364. // limit index to max step value
  365. if (index >= BREATHING_STEPS) {
  366. index = BREATHING_STEPS - 1;
  367. }
  368. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) || ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1))) {
  369. breathing_interrupt_disable();
  370. }
  371. // Set PWM to a brightnessvalue scaled to the configured resolution
  372. set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint32_t)pgm_read_byte(&breathing_table[index]) * ICRx / 255))));
  373. }
  374. #endif // BACKLIGHT_BREATHING
  375. void backlight_init_ports(void) {
  376. // Setup backlight pin as output and output to on state.
  377. backlight_pins_init();
  378. // I could write a wall of text here to explain... but TL;DW
  379. // Go read the ATmega32u4 datasheet.
  380. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  381. #ifdef BACKLIGHT_PWM_TIMER
  382. // TimerX setup, Fast PWM mode count to TOP set in ICRx
  383. TCCRxA = _BV(WGM11); // = 0b00000010;
  384. // clock select clk/1
  385. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  386. #else // hardware PWM
  387. // Pin PB7 = OCR1C (Timer 1, Channel C)
  388. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  389. // (i.e. start high, go low when counter matches.)
  390. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  391. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  392. /*
  393. 14.8.3:
  394. "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 [..]."
  395. "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)."
  396. */
  397. TCCRxA = _BV(COMxx1) | _BV(WGM11); // = 0b00001010;
  398. TCCRxB = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  399. #endif
  400. #ifdef BACKLIGHT_CUSTOM_RESOLUTION
  401. # if (BACKLIGHT_CUSTOM_RESOLUTION > 0xFFFF || BACKLIGHT_CUSTOM_RESOLUTION < 1)
  402. # error "This out of range of the timer capabilities"
  403. # elif (BACKLIGHT_CUSTOM_RESOLUTION < 0xFF)
  404. # warning "Resolution lower than 0xFF isn't recommended"
  405. # endif
  406. # ifdef BACKLIGHT_BREATHING
  407. breathing_freq_scale_factor = F_CPU / BACKLIGHT_CUSTOM_RESOLUTION / 120;
  408. # endif
  409. ICRx = BACKLIGHT_CUSTOM_RESOLUTION;
  410. #else
  411. ICRx = TIMER_TOP;
  412. #endif
  413. backlight_init();
  414. #ifdef BACKLIGHT_BREATHING
  415. if (is_backlight_breathing()) {
  416. breathing_enable();
  417. }
  418. #endif
  419. }