logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

sleep_led.c (6014B)


  1. #include <ch.h>
  2. #include <hal.h>
  3. #include "led.h"
  4. #include "sleep_led.h"
  5. /* All right, we go the "software" way: timer, toggle LED in interrupt.
  6. * Based on hasu's code for AVRs.
  7. * Use LP timer on Kinetises, TIM14 on STM32F0.
  8. */
  9. #ifndef SLEEP_LED_GPT_DRIVER
  10. # if defined(STM32F0XX)
  11. # define SLEEP_LED_GPT_DRIVER GPTD14
  12. # endif
  13. #endif
  14. #if defined(KL2x) || defined(K20x) || defined(SLEEP_LED_GPT_DRIVER) /* common parts for timers/interrupts */
  15. /* Breathing Sleep LED brighness(PWM On period) table
  16. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  17. *
  18. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  19. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  20. */
  21. static const uint8_t breathing_table[64] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10, 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252, 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23, 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  22. void sleep_led_timer_callback(void) {
  23. /* Software PWM
  24. * timer:1111 1111 1111 1111
  25. * \_____/\/ \_______/____ count(0-255)
  26. * \ \______________ duration of step(4)
  27. * \__________________ index of step table(0-63)
  28. */
  29. // this works for cca 65536 irqs/sec
  30. static union {
  31. uint16_t row;
  32. struct {
  33. uint8_t count : 8;
  34. uint8_t duration : 2;
  35. uint8_t index : 6;
  36. } pwm;
  37. } timer = {.row = 0};
  38. static led_t led_state = {0};
  39. timer.row++;
  40. // LED on
  41. if (timer.pwm.count == 0) {
  42. led_state.caps_lock = true;
  43. led_set(led_state.raw);
  44. }
  45. // LED off
  46. if (timer.pwm.count == breathing_table[timer.pwm.index]) {
  47. led_state.caps_lock = false;
  48. led_set(led_state.raw);
  49. }
  50. }
  51. #endif /* common parts for known platforms */
  52. #if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */
  53. /* Use Low Power Timer (LPTMR) */
  54. # define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR
  55. # define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
  56. /* LPTMR clock options */
  57. # define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */
  58. # define LPTMR_CLOCK_LPO 1 /* 1kHz clock */
  59. # define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */
  60. # define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */
  61. /* Work around inconsistencies in Freescale naming */
  62. # if !defined(SIM_SCGC5_LPTMR)
  63. # define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
  64. # endif
  65. /* interrupt handler */
  66. OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
  67. OSAL_IRQ_PROLOGUE();
  68. sleep_led_timer_callback();
  69. /* Reset the counter */
  70. RESET_COUNTER;
  71. OSAL_IRQ_EPILOGUE();
  72. }
  73. /* Initialise the timer */
  74. void sleep_led_init(void) {
  75. /* Make sure the clock to the LPTMR is enabled */
  76. SIM->SCGC5 |= SIM_SCGC5_LPTMR;
  77. /* Reset LPTMR settings */
  78. LPTMR0->CSR = 0;
  79. /* Set the compare value */
  80. LPTMR0->CMR = 0; // trigger on counter value (i.e. every time)
  81. /* Set up clock source and prescaler */
  82. /* Software PWM
  83. * ______ ______ __
  84. * | ON |___OFF___| ON |___OFF___| ....
  85. * |<-------------->|<-------------->|<- ....
  86. * PWM period PWM period
  87. *
  88. * R interrupts/period[resolution]
  89. * F periods/second[frequency]
  90. * R * F interrupts/second
  91. */
  92. /* === OPTION 1 === */
  93. # if 0
  94. // 1kHz LPO
  95. // No prescaler => 1024 irqs/sec
  96. // Note: this is too slow for a smooth breathe
  97. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_LPO)|LPTMRx_PSR_PBYP;
  98. # endif /* OPTION 1 */
  99. /* === OPTION 2 === */
  100. # if 1
  101. // nMHz IRC (n=4 on KL25Z, KL26Z and K20x; n=2 or 8 on KL27Z)
  102. MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock
  103. # if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others
  104. MCG->MC |= MCG_MC_LIRC_DIV2_DIV2;
  105. # endif /* KL27 */
  106. MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock
  107. // to work in stop mode, also MCG_C1_IREFSTEN
  108. // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec =>
  109. // => approx F=61, R=256, duration = 4
  110. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK) | LPTMRx_PSR_PRESCALE(6);
  111. # endif /* OPTION 2 */
  112. /* === OPTION 3 === */
  113. # if 0
  114. // OSC output (external crystal), usually 8MHz or 16MHz
  115. OSC0->CR |= OSC_CR_ERCLKEN; // enable ext ref clock
  116. // to work in stop mode, also OSC_CR_EREFSTEN
  117. // Divide by 2^N
  118. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_OSCERCLK)|LPTMRx_PSR_PRESCALE(7);
  119. # endif /* OPTION 3 */
  120. /* === END OPTIONS === */
  121. /* Interrupt on TCF set (compare flag) */
  122. nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority
  123. LPTMR0->CSR |= LPTMRx_CSR_TIE;
  124. }
  125. void sleep_led_enable(void) {
  126. /* Enable the timer */
  127. LPTMR0->CSR |= LPTMRx_CSR_TEN;
  128. }
  129. void sleep_led_disable(void) {
  130. /* Disable the timer */
  131. LPTMR0->CSR &= ~LPTMRx_CSR_TEN;
  132. }
  133. void sleep_led_toggle(void) {
  134. /* Toggle the timer */
  135. LPTMR0->CSR ^= LPTMRx_CSR_TEN;
  136. }
  137. #elif defined(SLEEP_LED_GPT_DRIVER)
  138. static void gptTimerCallback(GPTDriver *gptp) {
  139. (void)gptp;
  140. sleep_led_timer_callback();
  141. }
  142. static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
  143. /* Initialise the timer */
  144. void sleep_led_init(void) {
  145. gptStart(&SLEEP_LED_GPT_DRIVER, &gptcfg);
  146. }
  147. void sleep_led_enable(void) {
  148. gptStartContinuous(&SLEEP_LED_GPT_DRIVER, gptcfg.frequency / 0xFFFF);
  149. }
  150. void sleep_led_disable(void) {
  151. gptStopTimer(&SLEEP_LED_GPT_DRIVER);
  152. }
  153. void sleep_led_toggle(void) {
  154. (SLEEP_LED_GPT_DRIVER.state == GPT_READY) ? sleep_led_enable() : sleep_led_disable();
  155. }
  156. #else /* platform selection: not on familiar chips */
  157. void sleep_led_init(void) {}
  158. void sleep_led_enable(void) {
  159. led_set(2); // Caps Lock
  160. }
  161. void sleep_led_disable(void) {
  162. led_set(0);
  163. }
  164. void sleep_led_toggle(void) {
  165. // not implemented
  166. }
  167. #endif /* platform selection */