sleep_led.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. timer.row++;
  39. // LED on
  40. if (timer.pwm.count == 0) {
  41. led_set(1 << USB_LED_CAPS_LOCK);
  42. }
  43. // LED off
  44. if (timer.pwm.count == breathing_table[timer.pwm.index]) {
  45. led_set(0);
  46. }
  47. }
  48. #endif /* common parts for known platforms */
  49. #if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */
  50. /* Use Low Power Timer (LPTMR) */
  51. # define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR
  52. # define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
  53. /* LPTMR clock options */
  54. # define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */
  55. # define LPTMR_CLOCK_LPO 1 /* 1kHz clock */
  56. # define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */
  57. # define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */
  58. /* Work around inconsistencies in Freescale naming */
  59. # if !defined(SIM_SCGC5_LPTMR)
  60. # define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
  61. # endif
  62. /* interrupt handler */
  63. OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
  64. OSAL_IRQ_PROLOGUE();
  65. sleep_led_timer_callback();
  66. /* Reset the counter */
  67. RESET_COUNTER;
  68. OSAL_IRQ_EPILOGUE();
  69. }
  70. /* Initialise the timer */
  71. void sleep_led_init(void) {
  72. /* Make sure the clock to the LPTMR is enabled */
  73. SIM->SCGC5 |= SIM_SCGC5_LPTMR;
  74. /* Reset LPTMR settings */
  75. LPTMR0->CSR = 0;
  76. /* Set the compare value */
  77. LPTMR0->CMR = 0; // trigger on counter value (i.e. every time)
  78. /* Set up clock source and prescaler */
  79. /* Software PWM
  80. * ______ ______ __
  81. * | ON |___OFF___| ON |___OFF___| ....
  82. * |<-------------->|<-------------->|<- ....
  83. * PWM period PWM period
  84. *
  85. * R interrupts/period[resolution]
  86. * F periods/second[frequency]
  87. * R * F interrupts/second
  88. */
  89. /* === OPTION 1 === */
  90. # if 0
  91. // 1kHz LPO
  92. // No prescaler => 1024 irqs/sec
  93. // Note: this is too slow for a smooth breathe
  94. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_LPO)|LPTMRx_PSR_PBYP;
  95. # endif /* OPTION 1 */
  96. /* === OPTION 2 === */
  97. # if 1
  98. // nMHz IRC (n=4 on KL25Z, KL26Z and K20x; n=2 or 8 on KL27Z)
  99. MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock
  100. # if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others
  101. MCG->MC |= MCG_MC_LIRC_DIV2_DIV2;
  102. # endif /* KL27 */
  103. MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock
  104. // to work in stop mode, also MCG_C1_IREFSTEN
  105. // Divide 4MHz by 2^N (N=6) => 62500 irqs/sec =>
  106. // => approx F=61, R=256, duration = 4
  107. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK) | LPTMRx_PSR_PRESCALE(6);
  108. # endif /* OPTION 2 */
  109. /* === OPTION 3 === */
  110. # if 0
  111. // OSC output (external crystal), usually 8MHz or 16MHz
  112. OSC0->CR |= OSC_CR_ERCLKEN; // enable ext ref clock
  113. // to work in stop mode, also OSC_CR_EREFSTEN
  114. // Divide by 2^N
  115. LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_OSCERCLK)|LPTMRx_PSR_PRESCALE(7);
  116. # endif /* OPTION 3 */
  117. /* === END OPTIONS === */
  118. /* Interrupt on TCF set (compare flag) */
  119. nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority
  120. LPTMR0->CSR |= LPTMRx_CSR_TIE;
  121. }
  122. void sleep_led_enable(void) {
  123. /* Enable the timer */
  124. LPTMR0->CSR |= LPTMRx_CSR_TEN;
  125. }
  126. void sleep_led_disable(void) {
  127. /* Disable the timer */
  128. LPTMR0->CSR &= ~LPTMRx_CSR_TEN;
  129. }
  130. void sleep_led_toggle(void) {
  131. /* Toggle the timer */
  132. LPTMR0->CSR ^= LPTMRx_CSR_TEN;
  133. }
  134. #elif defined(SLEEP_LED_GPT_DRIVER)
  135. static void gptTimerCallback(GPTDriver *gptp) {
  136. (void)gptp;
  137. sleep_led_timer_callback();
  138. }
  139. static const GPTConfig gptcfg = {1000000, gptTimerCallback, 0, 0};
  140. /* Initialise the timer */
  141. void sleep_led_init(void) {
  142. gptStart(&SLEEP_LED_GPT_DRIVER, &gptcfg);
  143. }
  144. void sleep_led_enable(void) {
  145. gptStartContinuous(&SLEEP_LED_GPT_DRIVER, gptcfg.frequency / 0xFFFF);
  146. }
  147. void sleep_led_disable(void) {
  148. gptStopTimer(&SLEEP_LED_GPT_DRIVER);
  149. }
  150. void sleep_led_toggle(void) {
  151. (SLEEP_LED_GPT_DRIVER.state == GPT_READY) ? sleep_led_enable() : sleep_led_disable();
  152. }
  153. #else /* platform selection: not on familiar chips */
  154. void sleep_led_init(void) {}
  155. void sleep_led_enable(void) {
  156. led_set(1 << USB_LED_CAPS_LOCK);
  157. }
  158. void sleep_led_disable(void) {
  159. led_set(0);
  160. }
  161. void sleep_led_toggle(void) {
  162. // not implemented
  163. }
  164. #endif /* platform selection */