diff options
Diffstat (limited to 'lib/chibios-contrib/testhal/NRF51/NRF51822/GPT/main.c')
-rw-r--r-- | lib/chibios-contrib/testhal/NRF51/NRF51822/GPT/main.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/lib/chibios-contrib/testhal/NRF51/NRF51822/GPT/main.c b/lib/chibios-contrib/testhal/NRF51/NRF51822/GPT/main.c new file mode 100644 index 000000000..bda2b188c --- /dev/null +++ b/lib/chibios-contrib/testhal/NRF51/NRF51822/GPT/main.c | |||
@@ -0,0 +1,124 @@ | |||
1 | /* | ||
2 | Copyright (C) 2015 Stephen Caudle | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | |||
17 | #include "ch.h" | ||
18 | #include "hal.h" | ||
19 | |||
20 | /* | ||
21 | * GPT callback for GPTD2. | ||
22 | */ | ||
23 | static void gptcallback1(GPTDriver *gptp) { | ||
24 | |||
25 | (void)gptp; | ||
26 | palTogglePad(IOPORT1, LED1); | ||
27 | |||
28 | /* | ||
29 | * Start a one-shot timer (@ 250ms) | ||
30 | */ | ||
31 | chSysLockFromISR(); | ||
32 | gptStartOneShotI(&GPTD3, 15625); | ||
33 | chSysUnlockFromISR(); | ||
34 | } | ||
35 | |||
36 | /* | ||
37 | * GPT callback for GPTD3. | ||
38 | */ | ||
39 | static void gptcallback2(GPTDriver *gptp) { | ||
40 | |||
41 | (void)gptp; | ||
42 | palTogglePad(IOPORT1, LED2); | ||
43 | } | ||
44 | |||
45 | /* | ||
46 | * GPT configuration | ||
47 | * Frequency: 31250Hz (32us period) | ||
48 | * Resolution: 16 bits | ||
49 | */ | ||
50 | static const GPTConfig gptcfg1 = { | ||
51 | 31250, | ||
52 | gptcallback1, | ||
53 | 16, | ||
54 | }; | ||
55 | |||
56 | /* | ||
57 | * GPT configuration | ||
58 | * Frequency: 62500Hz (16us period) | ||
59 | * Resolution: 16 bits | ||
60 | */ | ||
61 | static const GPTConfig gptcfg2 = { | ||
62 | 62500, | ||
63 | gptcallback2, | ||
64 | 16, | ||
65 | }; | ||
66 | |||
67 | /* | ||
68 | * LED blinker thread, times are in milliseconds. | ||
69 | */ | ||
70 | static THD_WORKING_AREA(waThread1, 128); | ||
71 | static THD_FUNCTION(Thread1, arg) { | ||
72 | |||
73 | (void)arg; | ||
74 | chRegSetThreadName("blinker"); | ||
75 | while (true) { | ||
76 | palTogglePad(IOPORT1, LED0); | ||
77 | chThdSleepMilliseconds(500); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | /* | ||
82 | * Application entry point. | ||
83 | */ | ||
84 | int main(void) { | ||
85 | |||
86 | /* | ||
87 | * System initializations. | ||
88 | * - HAL initialization, this also initializes the configured device drivers | ||
89 | * and performs the board-specific initializations. | ||
90 | * - Kernel initialization, the main() function becomes a thread and the | ||
91 | * RTOS is active. | ||
92 | */ | ||
93 | halInit(); | ||
94 | chSysInit(); | ||
95 | |||
96 | /* | ||
97 | * Creates the blinker thread. | ||
98 | */ | ||
99 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); | ||
100 | |||
101 | /* | ||
102 | * Sets up the GPT timers | ||
103 | */ | ||
104 | gptStart(&GPTD2, &gptcfg1); | ||
105 | gptStart(&GPTD3, &gptcfg2); | ||
106 | |||
107 | /* | ||
108 | * Start a continuous timer | ||
109 | */ | ||
110 | gptStartContinuous(&GPTD2, 15625); | ||
111 | |||
112 | /* | ||
113 | * Normal main() thread activity, in this demo it does nothing. | ||
114 | */ | ||
115 | while (true) { | ||
116 | if (palReadPad(IOPORT1, KEY1) == 0) { | ||
117 | gptStopTimer(&GPTD2); | ||
118 | gptStop(&GPTD2); | ||
119 | gptStopTimer(&GPTD3); | ||
120 | gptStop(&GPTD3); | ||
121 | } | ||
122 | chThdSleepMilliseconds(500); | ||
123 | } | ||
124 | } | ||