rau cha / mbed-src-I2CWaitFix

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers us_ticker.c Source File

us_ticker.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include <stddef.h>
00017 #include "us_ticker_api.h"
00018 #include "PeripheralNames.h"
00019 
00020 #define US_TICKER_TIMER          ((LPC_CTxxBx_Type *)LPC_CT32B1_BASE)
00021 #define US_TICKER_TIMER_IRQn     TIMER_32_1_IRQn
00022 
00023 int us_ticker_inited = 0;
00024 
00025 void us_ticker_init(void) {
00026     if (us_ticker_inited) return;
00027     us_ticker_inited = 1;
00028     
00029     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); // Clock TIMER_1
00030     uint32_t PCLK = SystemCoreClock;
00031     
00032     US_TICKER_TIMER->TCR = 0x2;  // reset
00033     
00034     uint32_t prescale = PCLK / 1000000; // default to 1MHz (1 us ticks)
00035     US_TICKER_TIMER->PR = prescale - 1;
00036     US_TICKER_TIMER->TCR = 1; // enable = 1, reset = 0
00037     
00038     NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
00039     NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
00040 }
00041 
00042 uint32_t us_ticker_read() {
00043     if (!us_ticker_inited)
00044         us_ticker_init();
00045     
00046     return US_TICKER_TIMER->TC;
00047 }
00048 
00049 void us_ticker_set_interrupt(unsigned int timestamp) {
00050     // set match value
00051     US_TICKER_TIMER->MR0 = timestamp;
00052     // enable match interrupt
00053     US_TICKER_TIMER->MCR |= 1;
00054 }
00055 
00056 void us_ticker_disable_interrupt(void) {
00057     US_TICKER_TIMER->MCR &= ~1;
00058 }
00059 
00060 void us_ticker_clear_interrupt(void) {
00061     US_TICKER_TIMER->IR = 1;
00062 }