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