]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC15XX/us_ticker.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC15XX / us_ticker.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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 #include <stddef.h>
17 #include "us_ticker_api.h"
18 #include "PeripheralNames.h"
19
20 #define US_TICKER_TIMER_IRQn RIT_IRQn
21
22 int us_ticker_inited = 0;
23
24 void us_ticker_init(void) {
25 if (us_ticker_inited) return;
26 us_ticker_inited = 1;
27
28 // Enable the RIT clock
29 LPC_SYSCON->SYSAHBCLKCTRL1 |= (1 << 1);
30
31 // Clear peripheral reset the RIT
32 LPC_SYSCON->PRESETCTRL1 |= (1 << 1);
33 LPC_SYSCON->PRESETCTRL1 &= ~(1 << 1);
34
35 LPC_RIT->MASK = 0;
36 LPC_RIT->MASK_H = 0;
37
38 LPC_RIT->COUNTER = 0;
39 LPC_RIT->COUNTER_H = 0;
40
41 LPC_RIT->COMPVAL = 0xffffffff;
42 LPC_RIT->COMPVAL_H = 0x0000ffff;
43
44 // Timer enable, enable for debug
45 LPC_RIT->CTRL = 0xC;
46
47 NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
48 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
49 }
50
51 uint32_t us_ticker_read() {
52 if (!us_ticker_inited)
53 us_ticker_init();
54
55 uint64_t temp;
56 temp = LPC_RIT->COUNTER | ((uint64_t)LPC_RIT->COUNTER_H << 32);
57 temp /= (SystemCoreClock/1000000);
58 return (uint32_t)temp;
59 }
60
61 void us_ticker_set_interrupt(timestamp_t timestamp) {
62 uint64_t temp = ((uint64_t)timestamp * (SystemCoreClock/1000000));
63 LPC_RIT->COMPVAL = (temp & 0xFFFFFFFFL);
64 LPC_RIT->COMPVAL_H = ((temp >> 32)& 0x0000FFFFL);
65 }
66
67 void us_ticker_disable_interrupt(void) {
68 LPC_RIT->CTRL |= (1 << 3);
69 }
70
71 void us_ticker_clear_interrupt(void) {
72 LPC_RIT->CTRL |= (1 << 0);
73 }
Imprint / Impressum