]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - common/chibios/sleep_led.c
Squashed 'tmk_core/' changes from 57d27a8..08ce4c3
[max/tmk_keyboard.git] / 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 /* Low Power 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 /* external 32kHz crystal */
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 = 0;  // trigger on counter value (i.e. every time)
82
83     /* Set up clock source and prescaler */
84     /* Software PWM
85     *  ______           ______           __
86     * |  ON  |___OFF___|  ON  |___OFF___|   ....
87     * |<-------------->|<-------------->|<- ....
88     *     PWM period       PWM period
89     *
90     * R                interrupts/period[resolution]
91     * F                periods/second[frequency]
92     * R * F            interrupts/second
93     */
94
95     /* === OPTION 1 === */
96     #if 0
97     //  1kHz LPO
98     //  No prescaler => 1024 irqs/sec
99     //  Note: this is too slow for a smooth breathe
100     LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_LPO)|LPTMRx_PSR_PBYP;
101     #endif /* OPTION 1 */
102
103     /* === OPTION 2 === */
104     #if 1
105     //  nMHz IRC (n=4 on KL25Z, KL26Z and K20x; n=2 or 8 on KL27Z)
106     MCG->C2 |= MCG_C2_IRCS; // fast (4MHz) internal ref clock
107     #if defined(KL27) // divide the 8MHz IRC by 2, to have the same MCGIRCLK speed as others
108     MCG->MC |= MCG_MC_LIRC_DIV2_DIV2;
109     #endif /* KL27 */
110     MCG->C1 |= MCG_C1_IRCLKEN; // enable internal ref clock
111     //  to work in stop mode, also MCG_C1_IREFSTEN
112     //  Divide 4MHz by 2^N (N=6) => 62500 irqs/sec =>
113     //  => approx F=61, R=256, duration = 4
114     LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_MCGIRCLK)|LPTMRx_PSR_PRESCALE(6);
115     #endif /* OPTION 2 */
116
117     /* === OPTION 3 === */
118     #if 0
119     //  OSC output (external crystal), usually 8MHz or 16MHz
120     OSC0->CR |= OSC_CR_ERCLKEN; // enable ext ref clock
121     //  to work in stop mode, also OSC_CR_EREFSTEN
122     //  Divide by 2^N
123     LPTMR0->PSR = LPTMRx_PSR_PCS(LPTMR_CLOCK_OSCERCLK)|LPTMRx_PSR_PRESCALE(7);
124     #endif /* OPTION 3 */
125     /* === END OPTIONS === */
126
127     /* Interrupt on TCF set (compare flag) */
128     nvicEnableVector(LPTMR0_IRQn, 2); // vector, priority
129     LPTMR0->CSR |= LPTMRx_CSR_TIE;
130 }
131
132 void sleep_led_enable(void) {
133     /* Enable the timer */
134     LPTMR0->CSR |= LPTMRx_CSR_TEN;
135 }
136
137 void sleep_led_disable(void) {
138     /* Disable the timer */
139     LPTMR0->CSR &= ~LPTMRx_CSR_TEN;
140 }
141
142 void sleep_led_toggle(void) {
143     /* Toggle the timer */
144     LPTMR0->CSR ^= LPTMRx_CSR_TEN;
145 }
146
147 #else /* platform selection: not on familiar Kinetis chips */
148
149 void sleep_led_init(void) {
150 }
151  
152 void sleep_led_enable(void) {
153     led_set(1<<USB_LED_CAPS_LOCK);
154 }
155  
156 void sleep_led_disable(void) {
157     led_set(0);
158 }
159  
160 void sleep_led_toggle(void) {
161     // not implemented
162 }
163
164 #endif /* platform selection */