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.
12 /* Breathing Sleep LED brighness(PWM On period) table
13 * (64[steps] * 4[duration]) / 64[PWM periods/s] = 4 second breath cycle
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 }
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
25 /* LP Timer interrupt handler */
26 OSAL_IRQ_HANDLER(KINETIS_LPTMR0_IRQ_VECTOR) {
30 * timer:1111 1111 1111 1111
31 * \_____/\/ \_______/____ count(0-255)
32 * \ \______________ duration of step(4)
33 * \__________________ index of step table(0-63)
36 // this works for cca 65536 irqs/sec
44 } timer = { .row = 0 };
49 if (timer.pwm.count == 0) {
50 led_set(1<<USB_LED_CAPS_LOCK);
53 if (timer.pwm.count == breathing_table[timer.pwm.index]) {
57 /* Reset the counter */
58 LPTMR0->CSR |= LPTMRx_CSR_TCF;
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 */
69 /* Work around inconsistencies in Freescale naming */
70 #if !defined(SIM_SCGC5_LPTMR)
71 #define SIM_SCGC5_LPTMR SIM_SCGC5_LPTIMER
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 */
80 /* Set the compare value */
81 LPTMR0->CMR = 1; // trigger on counter value (i.e. every time)
82 /* Set up clock source and prescaler */
85 * | ON |___OFF___| ON |___OFF___| ....
86 * |<-------------->|<-------------->|<- ....
87 * PWM period PWM period
89 * R interrupts/period[resolution]
90 * F periods/second[frequency]
91 * R * F interrupts/second
93 /* === OPTION 1 === */
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;
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
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;
120 void sleep_led_enable(void) {
121 /* Enable the timer */
122 LPTMR0->CSR |= LPTMRx_CSR_TEN;
125 void sleep_led_disable(void) {
126 /* Disable the timer */
127 LPTMR0->CSR &= ~LPTMRx_CSR_TEN;
130 void sleep_led_toggle(void) {
131 /* Toggle the timer */
132 LPTMR0->CSR ^= LPTMRx_CSR_TEN;
135 #else /* platform selection: not on familiar Kinetis chips */
137 void sleep_led_init(void) {
140 void sleep_led_enable(void) {
141 led_set(1<<USB_LED_CAPS_LOCK);
144 void sleep_led_disable(void) {
148 void sleep_led_toggle(void) {
152 #endif /* platform selection */