fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088

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_TIM_TypeDef *)LPC_TIM3_BASE)
00021 #define US_TICKER_TIMER_IRQn TIMER3_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_SC->PCONP |= 1 << 23; // Clock TIMER_3
00030     
00031     US_TICKER_TIMER->CTCR = 0x0; // timer mode
00032     uint32_t PCLK = SystemCoreClock / 4;
00033     
00034     US_TICKER_TIMER->TCR = 0x2;  // reset
00035     
00036     uint32_t prescale = PCLK / 1000000; // default to 1MHz (1 us ticks)
00037     US_TICKER_TIMER->PR = prescale - 1;
00038     US_TICKER_TIMER->TCR = 1; // enable = 1, reset = 0
00039     
00040     NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
00041     NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
00042 }
00043 
00044 uint32_t us_ticker_read() {
00045     if (!us_ticker_inited)
00046         us_ticker_init();
00047     
00048     return US_TICKER_TIMER->TC;
00049 }
00050 
00051 void us_ticker_set_interrupt(unsigned int timestamp) {
00052     // set match value
00053     US_TICKER_TIMER->MR0 = timestamp;
00054     // enable match interrupt
00055     US_TICKER_TIMER->MCR |= 1;
00056 }
00057 
00058 void us_ticker_disable_interrupt(void) {
00059     US_TICKER_TIMER->MCR &= ~1;
00060 }
00061 
00062 void us_ticker_clear_interrupt(void) {
00063     US_TICKER_TIMER->IR = 1;
00064 }