mbed-os
Fork of mbed-os by
features/unsupported/tests/KL25Z/pit/main.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "mbed.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | extern "C" { |
elessair | 0:f269e3021894 | 4 | volatile uint32_t msTicks; |
elessair | 0:f269e3021894 | 5 | |
elessair | 0:f269e3021894 | 6 | void SysTick_Handler(void) { |
elessair | 0:f269e3021894 | 7 | msTicks++; |
elessair | 0:f269e3021894 | 8 | } |
elessair | 0:f269e3021894 | 9 | |
elessair | 0:f269e3021894 | 10 | void Delay(uint32_t dlyTicks) { |
elessair | 0:f269e3021894 | 11 | uint32_t curTicks; |
elessair | 0:f269e3021894 | 12 | |
elessair | 0:f269e3021894 | 13 | curTicks = msTicks; |
elessair | 0:f269e3021894 | 14 | while ((msTicks - curTicks) < dlyTicks); |
elessair | 0:f269e3021894 | 15 | } |
elessair | 0:f269e3021894 | 16 | } |
elessair | 0:f269e3021894 | 17 | |
elessair | 0:f269e3021894 | 18 | int main() { |
elessair | 0:f269e3021894 | 19 | SysTick_Config(SystemCoreClock / 1000); |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | SIM->SCGC6 |= SIM_SCGC6_PIT_MASK; // Clock PIT |
elessair | 0:f269e3021894 | 22 | PIT->MCR = 0; // Enable PIT |
elessair | 0:f269e3021894 | 23 | |
elessair | 0:f269e3021894 | 24 | // Timer 1 |
elessair | 0:f269e3021894 | 25 | PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF; |
elessair | 0:f269e3021894 | 26 | PIT->CHANNEL[1].TCTRL = 0x0; // Disable Interrupts |
elessair | 0:f269e3021894 | 27 | PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_CHN_MASK; // Chain to timer 0 |
elessair | 0:f269e3021894 | 28 | PIT->CHANNEL[1].TCTRL |= PIT_TCTRL_TEN_MASK; // Start timer 1 |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | // Timer 2 |
elessair | 0:f269e3021894 | 31 | PIT->CHANNEL[0].LDVAL = 0xFFFFFFFF; |
elessair | 0:f269e3021894 | 32 | PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK; // Start timer 0, disable interrupts |
elessair | 0:f269e3021894 | 33 | |
elessair | 0:f269e3021894 | 34 | DigitalOut led(LED_BLUE); |
elessair | 0:f269e3021894 | 35 | while (true) { |
elessair | 0:f269e3021894 | 36 | Delay(1000); |
elessair | 0:f269e3021894 | 37 | led = !led; |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | uint64_t ticks = (uint64_t)PIT->LTMR64H << 32; |
elessair | 0:f269e3021894 | 40 | ticks |= (uint64_t)PIT->LTMR64L; |
elessair | 0:f269e3021894 | 41 | printf("ticks: 0x%x%x\n", (uint32_t)(ticks>>32), (uint32_t)(ticks & 0xFFFFFFFF)); |
elessair | 0:f269e3021894 | 42 | |
elessair | 0:f269e3021894 | 43 | ticks = (~ticks) / 24; |
elessair | 0:f269e3021894 | 44 | uint32_t us = (uint32_t)(0xFFFFFFFF & ticks); |
elessair | 0:f269e3021894 | 45 | |
elessair | 0:f269e3021894 | 46 | printf("us : 0x%x\n", us); |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | } |