diff options
Diffstat (limited to 'lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/PWM/main.c')
-rw-r--r-- | lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/PWM/main.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/PWM/main.c b/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/PWM/main.c new file mode 100644 index 000000000..32d80209b --- /dev/null +++ b/lib/chibios-contrib/testhal/KINETIS/FRDM-KL25Z/PWM/main.c | |||
@@ -0,0 +1,128 @@ | |||
1 | /* | ||
2 | * (c) 2015 flabbergast <[email protected]> | ||
3 | * Based on ChibiOS 3.0.1 demo code, license below. | ||
4 | * Licensed under the Apache License, Version 2.0. | ||
5 | */ | ||
6 | |||
7 | /* | ||
8 | * ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio | ||
9 | * | ||
10 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
11 | * you may not use this file except in compliance with the License. | ||
12 | * You may obtain a copy of the License at | ||
13 | * | ||
14 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
15 | * | ||
16 | * Unless required by applicable law or agreed to in writing, software | ||
17 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
19 | * See the License for the specific language governing permissions and | ||
20 | * limitations under the License. | ||
21 | */ | ||
22 | |||
23 | #include "ch.h" | ||
24 | #include "hal.h" | ||
25 | |||
26 | /* | ||
27 | * on FRDM-KL25Z: | ||
28 | * red LED on PTB18/TPM2_CH0 (AF3) | ||
29 | * green LED on PTB19/TPM2_CH1 (AF3) | ||
30 | */ | ||
31 | |||
32 | #define PWM_DRIVER PWMD3 | ||
33 | |||
34 | /* PWM config structure */ | ||
35 | /* Note: the PWM clock frequency must be so that | ||
36 | * SYSCLK / FREQ is a power of 2 between 1 and 128. | ||
37 | */ | ||
38 | static const PWMConfig pwmcfg = { | ||
39 | 750000, /* 750kHz PWM clock frequency. */ | ||
40 | 1000, /* PWM period is 1000 cycles. */ | ||
41 | /* meaning PWM resolution is 750 */ | ||
42 | NULL, /* no callback */ | ||
43 | { | ||
44 | {PWM_OUTPUT_ACTIVE_LOW, NULL}, /* ch0: mode, no callback */ | ||
45 | {PWM_OUTPUT_ACTIVE_LOW, NULL}, /* ch1: mode, no callback */ | ||
46 | {PWM_OUTPUT_DISABLED, NULL}, /* ch2: mode, no callback */ | ||
47 | {PWM_OUTPUT_DISABLED, NULL}, /* ch3: mode, no callback */ | ||
48 | {PWM_OUTPUT_DISABLED, NULL}, /* ch4: mode, no callback */ | ||
49 | {PWM_OUTPUT_DISABLED, NULL} /* ch5: mode, no callback */ | ||
50 | }, | ||
51 | }; | ||
52 | |||
53 | #define BREATHE_STEP 16 /* ms; = 4000ms/TABLE_SIZE */ | ||
54 | |||
55 | /* Breathing Sleep LED brighness(PWM On period) table | ||
56 | * | ||
57 | * http://www.wolframalpha.com/input/?i=%28sin%28+x%2F64*pi%29**8+*+255%2C+x%3D0+to+63 | ||
58 | * (0..63).each {|x| p ((sin(x/64.0*PI)**8)*255).to_i } | ||
59 | */ | ||
60 | /* ruby -e "a = ((0..255).map{|x| Math.exp(Math.cos(Math::PI+(2*x*(Math::PI)/255)))-Math.exp(-1) }); m = a.max; a.map\!{|x| (10000*x/m).to_i}; p a" */ | ||
61 | #define TABLE_SIZE 256 | ||
62 | static const uint16_t breathing_table[TABLE_SIZE] = { | ||
63 | 0, 0, 1, 4, 7, 11, 17, 23, 30, 38, 47, 58, 69, 81, 94, 109, 124, 141, 159, 177, 197, 218, 241, 264, 289, 315, 343, 372, 402, 433, 466, 501, 537, 574, 613, 654, 696, 741, 786, 834, 883, 935, 988, 1043, 1100, 1159, 1220, 1283, 1349, 1416, 1486, 1558, 1632, 1709, 1788, 1870, 1954, 2040, 2129, 2220, 2314, 2411, 2510, 2611, 2715, 2822, 2932, 3044, 3158, 3275, 3395, 3517, 3641, 3768, 3897, 4028, 4162, 4298, 4436, 4576, 4717, 4861, 5006, 5152, 5300, 5449, 5600, 5751, 5903, 6055, 6208, 6361, 6513, 6666, 6818, 6970, 7120, 7269, 7417, 7563, 7708, 7850, 7990, 8127, 8261, 8391, 8519, 8643, 8762, 8878, 8989, 9095, 9196, 9293, 9383, 9469, 9548, 9622, 9689, 9750, 9805, 9853, 9895, 9930, 9957, 9978, 9992, 9999, 10000, 9992, 9978, 9957, 9930, 9895, 9853, 9805, 9750, 9689, 9622, 9548, 9469, 9383, 9293, 9196, 9095, 8989, 8878, 8762, 8643, 8519, 8391, 8261, 8127, 7990, 7850, 7708, 7563, 7417, 7269, 7120, 6970, 6818, 6666, 6513, 6361, 6208, 6055, 5903, 5751, 5600, 5449, 5300, 5152, 5006, 4861, 4717, 4576, 4436, 4298, 4162, 4028, 3897, 3768, 3641, 3517, 3395, 3275, 3158, 3044, 2932, 2822, 2715, 2611, 2510, 2411, 2314, 2220, 2129, 2040, 1954, 1870, 1788, 1709, 1632, 1558, 1486, 1416, 1349, 1283, 1220, 1159, 1100, 1043, 988, 935, 883, 834, 786, 741, 696, 654, 613, 574, 537, 501, 466, 433, 402, 372, 343, 315, 289, 264, 241, 218, 197, 177, 159, 141, 124, 109, 94, 81, 69, 58, 47, 38, 30, 23, 17, 11, 7, 4, 1, 0, 0 | ||
64 | }; | ||
65 | |||
66 | uint16_t table_pos = 0; | ||
67 | uint8_t active_led = 0; | ||
68 | |||
69 | static THD_WORKING_AREA(waBreatheThread, 128); | ||
70 | static THD_FUNCTION(BreatheThread, arg) { | ||
71 | (void)arg; | ||
72 | chRegSetThreadName("breatheThread"); | ||
73 | |||
74 | while(true) { | ||
75 | pwmEnableChannel(&PWM_DRIVER, active_led, PWM_PERCENTAGE_TO_WIDTH(&PWM_DRIVER,breathing_table[table_pos])); | ||
76 | table_pos++; | ||
77 | if(table_pos == TABLE_SIZE) { | ||
78 | table_pos = 0; | ||
79 | active_led = (active_led+1) % 2; | ||
80 | } | ||
81 | chThdSleepMilliseconds(BREATHE_STEP); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | /* | ||
86 | * Application entry point. | ||
87 | */ | ||
88 | int main(void) { | ||
89 | /* | ||
90 | * System initializations. | ||
91 | * - HAL initialization, this also initializes the configured device drivers | ||
92 | * and performs the board-specific initializations. | ||
93 | * - Kernel initialization, the main() function becomes a thread and the | ||
94 | * RTOS is active. | ||
95 | */ | ||
96 | halInit(); | ||
97 | chSysInit(); | ||
98 | |||
99 | /* | ||
100 | * Turn off the RGB LED. | ||
101 | */ | ||
102 | palSetLine(LINE_LED_RED); /* red */ | ||
103 | palSetLine(LINE_LED_GREEN); /* green */ | ||
104 | palSetLine(LINE_LED_BLUE); /* blue */ | ||
105 | |||
106 | /* | ||
107 | * Start the PWM driver, route TPM2 output to PTB18, PTB19. | ||
108 | * Enable channels now to avoid a blink later. | ||
109 | */ | ||
110 | pwmStart(&PWM_DRIVER, &pwmcfg); | ||
111 | palSetLineMode(LINE_LED_RED, PAL_MODE_ALTERNATIVE_3); | ||
112 | palSetLineMode(LINE_LED_GREEN, PAL_MODE_ALTERNATIVE_3); | ||
113 | pwmEnableChannel(&PWM_DRIVER, 0, 0); | ||
114 | pwmEnableChannel(&PWM_DRIVER, 1, 0); | ||
115 | |||
116 | /* | ||
117 | * Create the breathe thread. | ||
118 | */ | ||
119 | chThdCreateStatic(waBreatheThread, sizeof(waBreatheThread), NORMALPRIO, BreatheThread, NULL); | ||
120 | |||
121 | /* | ||
122 | * Normal main() thread activity, in this demo it does nothing except | ||
123 | * sleeping in a loop. | ||
124 | */ | ||
125 | while(true) { | ||
126 | chThdSleepMilliseconds(500); | ||
127 | } | ||
128 | } | ||