mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Aug 27 08:45:06 2014 +0100
Revision:
300:55638feb26a4
Parent:
270:e2babe29baf8
Child:
304:89b9c3a9a045
Synchronized with git revision 90467175c04ad81c20cdf7b2ddb86c54f5f3dcbb

Full URL: https://github.com/mbedmicro/mbed/commit/90467175c04ad81c20cdf7b2ddb86c54f5f3dcbb/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 85:e1a8e879a6a9 1 /* mbed Microcontroller Library
mbed_official 104:a6a92e2e5a92 2 * Copyright (c) 2013 Nordic Semiconductor
mbed_official 85:e1a8e879a6a9 3 *
mbed_official 85:e1a8e879a6a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 85:e1a8e879a6a9 5 * you may not use this file except in compliance with the License.
mbed_official 85:e1a8e879a6a9 6 * You may obtain a copy of the License at
mbed_official 85:e1a8e879a6a9 7 *
mbed_official 85:e1a8e879a6a9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 85:e1a8e879a6a9 9 *
mbed_official 85:e1a8e879a6a9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 85:e1a8e879a6a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 85:e1a8e879a6a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 85:e1a8e879a6a9 13 * See the License for the specific language governing permissions and
mbed_official 85:e1a8e879a6a9 14 * limitations under the License.
mbed_official 85:e1a8e879a6a9 15 */
mbed_official 85:e1a8e879a6a9 16 #include <stddef.h>
mbed_official 85:e1a8e879a6a9 17 #include "us_ticker_api.h"
mbed_official 85:e1a8e879a6a9 18 #include "cmsis.h"
mbed_official 85:e1a8e879a6a9 19 #include "PeripheralNames.h"
mbed_official 85:e1a8e879a6a9 20
mbed_official 85:e1a8e879a6a9 21 #define US_TICKER_TIMER NRF_TIMER1
mbed_official 85:e1a8e879a6a9 22 #define US_TICKER_TIMER_IRQn TIMER1_IRQn
mbed_official 85:e1a8e879a6a9 23
mbed_official 300:55638feb26a4 24 int us_ticker_inited = 0;
mbed_official 300:55638feb26a4 25 volatile uint16_t overflow = 0; //overflow value that forms the upper 16 bits of the counter
mbed_official 300:55638feb26a4 26 volatile uint16_t timeStamp = 0;
mbed_official 85:e1a8e879a6a9 27
mbed_official 85:e1a8e879a6a9 28 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 29 extern "C" {
mbed_official 300:55638feb26a4 30 #endif
mbed_official 127:ce7cebc0511f 31
mbed_official 300:55638feb26a4 32 void TIMER1_IRQHandler(void)
mbed_official 300:55638feb26a4 33 {
mbed_official 300:55638feb26a4 34 if ((US_TICKER_TIMER->EVENTS_COMPARE[1] != 0) &&
mbed_official 300:55638feb26a4 35 ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0)) {
mbed_official 300:55638feb26a4 36 US_TICKER_TIMER->EVENTS_COMPARE[1] = 0;
mbed_official 300:55638feb26a4 37 overflow++;
mbed_official 300:55638feb26a4 38 US_TICKER_TIMER->CC[1] = 0xFFFF;
mbed_official 300:55638feb26a4 39 if (timeStamp>0) {
mbed_official 300:55638feb26a4 40 timeStamp--;
mbed_official 300:55638feb26a4 41 if (timeStamp==0) {
mbed_official 300:55638feb26a4 42 us_ticker_clear_interrupt();
mbed_official 300:55638feb26a4 43 us_ticker_disable_interrupt();
mbed_official 300:55638feb26a4 44 us_ticker_irq_handler();
mbed_official 300:55638feb26a4 45 return;
mbed_official 300:55638feb26a4 46 }
mbed_official 300:55638feb26a4 47 }
mbed_official 85:e1a8e879a6a9 48 }
mbed_official 300:55638feb26a4 49 if ((US_TICKER_TIMER->EVENTS_COMPARE[0] != 0) &&
mbed_official 300:55638feb26a4 50 ((US_TICKER_TIMER->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) {
mbed_official 300:55638feb26a4 51 us_ticker_clear_interrupt();
mbed_official 300:55638feb26a4 52 us_ticker_disable_interrupt();
mbed_official 300:55638feb26a4 53 if (timeStamp==0) {
mbed_official 300:55638feb26a4 54 us_ticker_irq_handler();
mbed_official 300:55638feb26a4 55 }
mbed_official 85:e1a8e879a6a9 56 }
mbed_official 85:e1a8e879a6a9 57 }
mbed_official 300:55638feb26a4 58
mbed_official 85:e1a8e879a6a9 59 #ifdef __cplusplus
mbed_official 85:e1a8e879a6a9 60 }
mbed_official 300:55638feb26a4 61 #endif
mbed_official 300:55638feb26a4 62 void us_ticker_init(void)
mbed_official 300:55638feb26a4 63 {
mbed_official 300:55638feb26a4 64 if (us_ticker_inited && US_TICKER_TIMER->POWER) {
mbed_official 85:e1a8e879a6a9 65 return;
mbed_official 85:e1a8e879a6a9 66 }
mbed_official 300:55638feb26a4 67
mbed_official 85:e1a8e879a6a9 68 us_ticker_inited = 1;
mbed_official 300:55638feb26a4 69
mbed_official 85:e1a8e879a6a9 70 US_TICKER_TIMER->POWER = 0;
mbed_official 85:e1a8e879a6a9 71 US_TICKER_TIMER->POWER = 1;
mbed_official 300:55638feb26a4 72
mbed_official 85:e1a8e879a6a9 73 US_TICKER_TIMER->MODE = TIMER_MODE_MODE_Timer;
mbed_official 300:55638feb26a4 74
mbed_official 300:55638feb26a4 75 US_TICKER_TIMER->PRESCALER = 4;
mbed_official 300:55638feb26a4 76 US_TICKER_TIMER->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
mbed_official 300:55638feb26a4 77 US_TICKER_TIMER->TASKS_CLEAR = 1;
mbed_official 300:55638feb26a4 78 US_TICKER_TIMER->CC[1] = 0xFFFF;
mbed_official 300:55638feb26a4 79 US_TICKER_TIMER->INTENSET = TIMER_INTENSET_COMPARE1_Set << TIMER_INTENSET_COMPARE1_Pos;
mbed_official 300:55638feb26a4 80
mbed_official 224:e7c230c6cb31 81 NVIC_SetPriority(US_TICKER_TIMER_IRQn, 3);
mbed_official 85:e1a8e879a6a9 82 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
mbed_official 300:55638feb26a4 83
mbed_official 85:e1a8e879a6a9 84 US_TICKER_TIMER->TASKS_START = 0x01;
mbed_official 85:e1a8e879a6a9 85 }
mbed_official 85:e1a8e879a6a9 86
mbed_official 300:55638feb26a4 87 uint32_t us_ticker_read()
mbed_official 300:55638feb26a4 88 {
mbed_official 300:55638feb26a4 89 if (!us_ticker_inited || (US_TICKER_TIMER->POWER==0)) {
mbed_official 85:e1a8e879a6a9 90 us_ticker_init();
mbed_official 85:e1a8e879a6a9 91 }
mbed_official 300:55638feb26a4 92
mbed_official 300:55638feb26a4 93 uint16_t bufferedOverFlow = overflow;
mbed_official 300:55638feb26a4 94 US_TICKER_TIMER->TASKS_CAPTURE[2] = 1;
mbed_official 300:55638feb26a4 95
mbed_official 300:55638feb26a4 96 if (overflow!=bufferedOverFlow) {
mbed_official 300:55638feb26a4 97 bufferedOverFlow = overflow;
mbed_official 300:55638feb26a4 98 US_TICKER_TIMER->TASKS_CAPTURE[2] = 1;
mbed_official 300:55638feb26a4 99 }
mbed_official 300:55638feb26a4 100 return (((uint32_t)bufferedOverFlow << 16) | US_TICKER_TIMER->CC[2]);
mbed_official 85:e1a8e879a6a9 101 }
mbed_official 85:e1a8e879a6a9 102
mbed_official 300:55638feb26a4 103 void us_ticker_set_interrupt(unsigned int timestamp)
mbed_official 300:55638feb26a4 104 {
mbed_official 300:55638feb26a4 105 if (!us_ticker_inited || (US_TICKER_TIMER->POWER == 0)) {
mbed_official 85:e1a8e879a6a9 106 us_ticker_init();
mbed_official 300:55638feb26a4 107 }
mbed_official 300:55638feb26a4 108
mbed_official 300:55638feb26a4 109 US_TICKER_TIMER->TASKS_CAPTURE[0] = 1;
mbed_official 300:55638feb26a4 110 uint16_t tsUpper16 = (uint16_t)((timestamp - us_ticker_read()) >> 16);
mbed_official 300:55638feb26a4 111 if (tsUpper16>0) {
mbed_official 300:55638feb26a4 112 if ((timeStamp ==0) || (timeStamp> tsUpper16)) {
mbed_official 300:55638feb26a4 113 timeStamp = tsUpper16;
mbed_official 300:55638feb26a4 114 }
mbed_official 300:55638feb26a4 115 } else {
mbed_official 300:55638feb26a4 116 US_TICKER_TIMER->INTENSET |= TIMER_INTENSET_COMPARE0_Set << TIMER_INTENSET_COMPARE0_Pos;
mbed_official 300:55638feb26a4 117 US_TICKER_TIMER->CC[0] += timestamp - us_ticker_read();
mbed_official 300:55638feb26a4 118 }
mbed_official 85:e1a8e879a6a9 119 }
mbed_official 85:e1a8e879a6a9 120
mbed_official 300:55638feb26a4 121 void us_ticker_disable_interrupt(void)
mbed_official 300:55638feb26a4 122 {
mbed_official 85:e1a8e879a6a9 123 US_TICKER_TIMER->INTENCLR = TIMER_INTENCLR_COMPARE0_Clear << TIMER_INTENCLR_COMPARE0_Pos;
mbed_official 85:e1a8e879a6a9 124 }
mbed_official 300:55638feb26a4 125
mbed_official 300:55638feb26a4 126 void us_ticker_clear_interrupt(void)
mbed_official 300:55638feb26a4 127 {
mbed_official 85:e1a8e879a6a9 128 US_TICKER_TIMER->EVENTS_COMPARE[0] = 0;
mbed_official 85:e1a8e879a6a9 129 }