]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/KL25Z/pit/main.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / KL25Z / pit / main.cpp
1 #include "mbed.h"
2
3 extern "C" {
4 volatile uint32_t msTicks;
5
6 void SysTick_Handler(void) {
7     msTicks++;
8 }
9
10 void Delay(uint32_t dlyTicks) {
11     uint32_t curTicks;
12
13     curTicks = msTicks;
14     while ((msTicks - curTicks) < dlyTicks);
15 }
16 }
17
18 int main() {
19     SysTick_Config(SystemCoreClock / 1000);
20
21     SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;   // Clock PIT
22     PIT->MCR = 0;                       // Enable PIT
23
24     // Timer 1
25     PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF;
26     PIT->CHANNEL[1].TCTRL = 0x0;                 // Disable Interrupts
27     PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_CHN_MASK; // Chain to timer 0
28     PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 1
29
30     // Timer 2
31     PIT->CHANNEL[0].LDVAL = 0xFFFFFFFF;
32     PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK;  // Start timer 0, disable interrupts
33
34     DigitalOut led(LED_BLUE);
35     while (true) {
36         Delay(1000);
37         led = !led;
38
39         uint64_t ticks = (uint64_t)PIT->LTMR64H << 32;
40         ticks         |= (uint64_t)PIT->LTMR64L;
41         printf("ticks: 0x%x%x\n", (uint32_t)(ticks>>32), (uint32_t)(ticks & 0xFFFFFFFF));
42
43         ticks = (~ticks) / 24;
44         uint32_t us = (uint32_t)(0xFFFFFFFF & ticks);
45
46         printf("us   : 0x%x\n", us);
47     }
48 }