You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

sleep_led.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #if defined(KL2x) || defined(K20x)
  10. /* Use Low Power Timer (LPTMR) */
  11. #define TIMER_INTERRUPT_VECTOR KINETIS_LPTMR0_IRQ_VECTOR
  12. #define RESET_COUNTER LPTMR0->CSR |= LPTMRx_CSR_TCF
  13. #elif defined(STM32F0XX)
  14. /* Use TIM14 manually */
  15. #define TIMER_INTERRUPT_VECTOR STM32_TIM14_HANDLER
  16. #define RESET_COUNTER STM32_TIM14->SR &= ~STM32_TIM_SR_UIF
  17. #endif
  18. #if defined(KL2x) || defined(K20x) || defined(STM32F0XX) /* common parts for timers/interrupts */
  19. /* Breathing Sleep LED brighness(PWM On period) table
  20. * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
  21. *
  22. * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63
  23. * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i }
  24. */
  25. static const uint8_t breathing_table[64] = {
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 6, 10,
  27. 15, 23, 32, 44, 58, 74, 93, 113, 135, 157, 179, 199, 218, 233, 245, 252,
  28. 255, 252, 245, 233, 218, 199, 179, 157, 135, 113, 93, 74, 58, 44, 32, 23,
  29. 15, 10, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  30. };
  31. /* interrupt handler */
  32. OSAL_IRQ_HANDLER(TIMER_INTERRUPT_VECTOR) {
  33. OSAL_IRQ_PROLOGUE();
  34. /* Software PWM
  35. * timer:1111 1111 1111 1111
  36. * \_____/\/ \_______/____ count(0-255)
  37. * \ \______________ duration of step(4)
  38. * \__________________ index of step table(0-63)
  39. */
  40. // this works for cca 65536 irqs/sec
  41. static union {
  42. uint16_t row;
  43. struct {
  44. uint8_t count:8;
  45. uint8_t duration:2;
  46. uint8_t index:6;
  47. } pwm;
  48. } timer = { .row = 0 };
  49. timer.row++;
  50. // LED on
  51. if (timer.pwm.count == 0) {
  52. led_set(1<<USB_LED_CAPS_LOCK);
  53. }
  54. // LED off
  55. if (timer.pwm.count == breathing_table[timer.pwm.index]) {
  56. led_set(0);
  57. }
  58. /* Reset the counter */
  59. RESET_COUNTER;
  60. OSAL_IRQ_EPILOGUE();
  61. }
  62. #endif /* common parts for known platforms */
  63. #if defined(KL2x) || defined(K20x) /* platform selection: familiar Kinetis chips */
  64. /* LPTMR clock options */
  65. #define LPTMR_CLOCK_MCGIRCLK 0 /* 4MHz clock */
  66. #define LPTMR_CLOCK_LPO 1 /* 1kHz clock */
  67. #define LPTMR_CLOCK_ERCLK32K 2 /* external 32kHz crystal */
  68. #define LPTMR_CLOCK_OSCERCLK 3 /* output from OSC */
  69. /* Work around inconsistencies in Freescale naming */
  70. #if !defined(SIM_SCGC5_LPTMR)
  71. #define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
  72. #endif
  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(STM32F0XX) /* platform selection: STM32F0XX */
  138. /* Initialise the timer */
  139. void sleep_led_init(void) {
  140. /* enable clock */
  141. rccEnableTIM14(FALSE); /* low power enable = FALSE */
  142. rccResetTIM14();
  143. /* prescale */
  144. /* Assuming 48MHz internal clock */
  145. /* getting cca 65484 irqs/sec */
  146. STM32_TIM14->PSC = 733;
  147. /* auto-reload */
  148. /* 0 => interrupt every time */
  149. STM32_TIM14->ARR = 3;
  150. /* enable counter update event interrupt */
  151. STM32_TIM14->DIER |= STM32_TIM_DIER_UIE;
  152. /* register interrupt vector */
  153. nvicEnableVector(STM32_TIM14_NUMBER, 2); /* vector, priority */
  154. }
  155. void sleep_led_enable(void) {
  156. /* Enable the timer */
  157. STM32_TIM14->CR1 = STM32_TIM_CR1_CEN | STM32_TIM_CR1_URS;
  158. /* URS => update event only on overflow; setting UG bit disabled */
  159. }
  160. void sleep_led_disable(void) {
  161. /* Disable the timer */
  162. STM32_TIM14->CR1 = 0;
  163. }
  164. void sleep_led_toggle(void) {
  165. /* Toggle the timer */
  166. STM32_TIM14->CR1 ^= STM32_TIM_CR1_CEN;
  167. }
  168. #else /* platform selection: not on familiar chips */
  169. void sleep_led_init(void) {
  170. }
  171. void sleep_led_enable(void) {
  172. led_set(1<<USB_LED_CAPS_LOCK);
  173. }
  174. void sleep_led_disable(void) {
  175. led_set(0);
  176. }
  177. void sleep_led_toggle(void) {
  178. // not implemented
  179. }
  180. #endif /* platform selection */