mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_NXP/TARGET_LPC81X/us_ticker.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 304:89b9c3a9a045
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 46:bebbbd80dd87 | 1 | /* mbed Microcontroller Library |
mbed_official | 46:bebbbd80dd87 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 46:bebbbd80dd87 | 3 | * |
mbed_official | 46:bebbbd80dd87 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 46:bebbbd80dd87 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 46:bebbbd80dd87 | 6 | * You may obtain a copy of the License at |
mbed_official | 46:bebbbd80dd87 | 7 | * |
mbed_official | 46:bebbbd80dd87 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 46:bebbbd80dd87 | 9 | * |
mbed_official | 46:bebbbd80dd87 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 46:bebbbd80dd87 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 46:bebbbd80dd87 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 46:bebbbd80dd87 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 46:bebbbd80dd87 | 14 | * limitations under the License. |
mbed_official | 46:bebbbd80dd87 | 15 | */ |
mbed_official | 46:bebbbd80dd87 | 16 | #include <stddef.h> |
mbed_official | 46:bebbbd80dd87 | 17 | #include "us_ticker_api.h" |
mbed_official | 46:bebbbd80dd87 | 18 | #include "PeripheralNames.h" |
mbed_official | 46:bebbbd80dd87 | 19 | |
mbed_official | 46:bebbbd80dd87 | 20 | #define US_TICKER_TIMER_IRQn SCT_IRQn |
mbed_official | 46:bebbbd80dd87 | 21 | |
mbed_official | 46:bebbbd80dd87 | 22 | int us_ticker_inited = 0; |
mbed_official | 46:bebbbd80dd87 | 23 | |
mbed_official | 46:bebbbd80dd87 | 24 | void us_ticker_init(void) { |
mbed_official | 46:bebbbd80dd87 | 25 | if (us_ticker_inited) return; |
mbed_official | 46:bebbbd80dd87 | 26 | us_ticker_inited = 1; |
mbed_official | 46:bebbbd80dd87 | 27 | |
mbed_official | 46:bebbbd80dd87 | 28 | // Enable the SCT clock |
mbed_official | 46:bebbbd80dd87 | 29 | LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 8); |
mbed_official | 46:bebbbd80dd87 | 30 | |
mbed_official | 46:bebbbd80dd87 | 31 | // Clear peripheral reset the SCT: |
mbed_official | 46:bebbbd80dd87 | 32 | LPC_SYSCON->PRESETCTRL |= (1 << 8); |
mbed_official | 46:bebbbd80dd87 | 33 | |
mbed_official | 46:bebbbd80dd87 | 34 | // Unified counter (32 bits) |
mbed_official | 46:bebbbd80dd87 | 35 | LPC_SCT->CONFIG |= 1; |
mbed_official | 46:bebbbd80dd87 | 36 | |
mbed_official | 46:bebbbd80dd87 | 37 | // halt and clear the counter |
mbed_official | 46:bebbbd80dd87 | 38 | LPC_SCT->CTRL_L |= (1 << 2) | (1 << 3); |
mbed_official | 46:bebbbd80dd87 | 39 | |
mbed_official | 46:bebbbd80dd87 | 40 | // System Clock (12)MHz -> us_ticker (1)MHz |
mbed_official | 46:bebbbd80dd87 | 41 | LPC_SCT->CTRL_L |= ((SystemCoreClock/1000000 - 1) << 5); |
mbed_official | 46:bebbbd80dd87 | 42 | |
mbed_official | 46:bebbbd80dd87 | 43 | // unhalt the counter: |
mbed_official | 46:bebbbd80dd87 | 44 | // - clearing bit 2 of the CTRL register |
mbed_official | 46:bebbbd80dd87 | 45 | LPC_SCT->CTRL_L &= ~(1 << 2); |
mbed_official | 46:bebbbd80dd87 | 46 | |
mbed_official | 46:bebbbd80dd87 | 47 | NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler); |
mbed_official | 46:bebbbd80dd87 | 48 | NVIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
mbed_official | 46:bebbbd80dd87 | 49 | } |
mbed_official | 46:bebbbd80dd87 | 50 | |
mbed_official | 46:bebbbd80dd87 | 51 | uint32_t us_ticker_read() { |
mbed_official | 46:bebbbd80dd87 | 52 | if (!us_ticker_inited) |
mbed_official | 46:bebbbd80dd87 | 53 | us_ticker_init(); |
mbed_official | 46:bebbbd80dd87 | 54 | |
mbed_official | 46:bebbbd80dd87 | 55 | return LPC_SCT->COUNT_U; |
mbed_official | 46:bebbbd80dd87 | 56 | } |
mbed_official | 46:bebbbd80dd87 | 57 | |
mbed_official | 304:89b9c3a9a045 | 58 | void us_ticker_set_interrupt(timestamp_t timestamp) { |
mbed_official | 46:bebbbd80dd87 | 59 | // halt the counter: |
mbed_official | 46:bebbbd80dd87 | 60 | // - setting bit 2 of the CTRL register |
mbed_official | 46:bebbbd80dd87 | 61 | LPC_SCT->CTRL_L |= (1 << 2); |
mbed_official | 46:bebbbd80dd87 | 62 | |
mbed_official | 46:bebbbd80dd87 | 63 | // set timestamp in compare register |
mbed_official | 304:89b9c3a9a045 | 64 | LPC_SCT->MATCH[0].U = (uint32_t)timestamp; |
mbed_official | 46:bebbbd80dd87 | 65 | |
mbed_official | 46:bebbbd80dd87 | 66 | // unhalt the counter: |
mbed_official | 46:bebbbd80dd87 | 67 | // - clearing bit 2 of the CTRL register |
mbed_official | 46:bebbbd80dd87 | 68 | LPC_SCT->CTRL_L &= ~(1 << 2); |
mbed_official | 46:bebbbd80dd87 | 69 | |
mbed_official | 46:bebbbd80dd87 | 70 | // if events are not enabled, enable them |
mbed_official | 46:bebbbd80dd87 | 71 | if (!(LPC_SCT->EVEN & 0x01)) { |
mbed_official | 46:bebbbd80dd87 | 72 | |
mbed_official | 46:bebbbd80dd87 | 73 | // comb mode = match only |
mbed_official | 46:bebbbd80dd87 | 74 | LPC_SCT->EVENT[0].CTRL = (1 << 12); |
mbed_official | 46:bebbbd80dd87 | 75 | |
mbed_official | 46:bebbbd80dd87 | 76 | // ref manual: |
mbed_official | 46:bebbbd80dd87 | 77 | // In simple applications that do not |
mbed_official | 46:bebbbd80dd87 | 78 | // use states, write 0x01 to this |
mbed_official | 46:bebbbd80dd87 | 79 | // register to enable an event |
mbed_official | 46:bebbbd80dd87 | 80 | LPC_SCT->EVENT[0].STATE |= 0x1; |
mbed_official | 46:bebbbd80dd87 | 81 | |
mbed_official | 46:bebbbd80dd87 | 82 | // enable events |
mbed_official | 46:bebbbd80dd87 | 83 | LPC_SCT->EVEN |= 0x1; |
mbed_official | 46:bebbbd80dd87 | 84 | } |
mbed_official | 46:bebbbd80dd87 | 85 | } |
mbed_official | 46:bebbbd80dd87 | 86 | |
mbed_official | 46:bebbbd80dd87 | 87 | void us_ticker_disable_interrupt(void) { |
mbed_official | 46:bebbbd80dd87 | 88 | LPC_SCT->EVEN &= ~1; |
mbed_official | 46:bebbbd80dd87 | 89 | } |
mbed_official | 46:bebbbd80dd87 | 90 | |
mbed_official | 46:bebbbd80dd87 | 91 | void us_ticker_clear_interrupt(void) { |
mbed_official | 46:bebbbd80dd87 | 92 | LPC_SCT->EVFLAG = 1; |
mbed_official | 46:bebbbd80dd87 | 93 | } |