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:
Mon Jan 26 14:15:07 2015 +0000
Revision:
452:a2b30f7d1bc5
Parent:
361:56c2a6244bba
Child:
453:a290c6acf95e
Synchronized with git revision e979bd60eb6de65d0db5993b547adbd692b91e9f

Full URL: https://github.com/mbedmicro/mbed/commit/e979bd60eb6de65d0db5993b547adbd692b91e9f/

switching timestamp_t back to 32-bits.

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 304:89b9c3a9a045 20 #include "app_timer.h"
mbed_official 452:a2b30f7d1bc5 21 #include "projectconfig.h"
mbed_official 127:ce7cebc0511f 22
mbed_official 304:89b9c3a9a045 23 static bool us_ticker_inited = false;
mbed_official 304:89b9c3a9a045 24 static volatile bool us_ticker_appTimerRunning = false;
mbed_official 304:89b9c3a9a045 25 static app_timer_id_t us_ticker_appTimerID = TIMER_NULL;
mbed_official 300:55638feb26a4 26
mbed_official 300:55638feb26a4 27 void us_ticker_init(void)
mbed_official 300:55638feb26a4 28 {
mbed_official 304:89b9c3a9a045 29 if (us_ticker_inited) {
mbed_official 85:e1a8e879a6a9 30 return;
mbed_official 85:e1a8e879a6a9 31 }
mbed_official 300:55638feb26a4 32
mbed_official 452:a2b30f7d1bc5 33 APP_TIMER_INIT(CFG_TIMER_PRESCALER, CFG_TIMER_MAX_INSTANCE, CFG_TIMER_OPERATION_QUEUE_SIZE, CFG_SCHEDULER_ENABLE);
mbed_official 300:55638feb26a4 34
mbed_official 304:89b9c3a9a045 35 us_ticker_inited = true;
mbed_official 85:e1a8e879a6a9 36 }
mbed_official 85:e1a8e879a6a9 37
mbed_official 300:55638feb26a4 38 uint32_t us_ticker_read()
mbed_official 300:55638feb26a4 39 {
mbed_official 304:89b9c3a9a045 40 if (!us_ticker_inited) {
mbed_official 304:89b9c3a9a045 41 us_ticker_init();
mbed_official 304:89b9c3a9a045 42 }
mbed_official 304:89b9c3a9a045 43
mbed_official 452:a2b30f7d1bc5 44 uint64_t value;
mbed_official 304:89b9c3a9a045 45 app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */
mbed_official 361:56c2a6244bba 46 return ((value * 1000000) / (uint32_t)APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value.
mbed_official 304:89b9c3a9a045 47 * This is only as precise as the 32khz low-freq
mbed_official 304:89b9c3a9a045 48 * clock source, but could be adequate.*/
mbed_official 304:89b9c3a9a045 49 }
mbed_official 304:89b9c3a9a045 50
mbed_official 304:89b9c3a9a045 51 /* An adaptor to interface us_ticker_irq_handler with the app_timer callback.
mbed_official 304:89b9c3a9a045 52 * Needed because the irq_handler() doesn't take any parameter.*/
mbed_official 304:89b9c3a9a045 53 static void us_ticker_app_timer_callback(void *context)
mbed_official 304:89b9c3a9a045 54 {
mbed_official 304:89b9c3a9a045 55 us_ticker_appTimerRunning = false;
mbed_official 304:89b9c3a9a045 56 us_ticker_irq_handler();
mbed_official 304:89b9c3a9a045 57 }
mbed_official 304:89b9c3a9a045 58
mbed_official 304:89b9c3a9a045 59 void us_ticker_set_interrupt(timestamp_t timestamp)
mbed_official 304:89b9c3a9a045 60 {
mbed_official 304:89b9c3a9a045 61 if (!us_ticker_inited) {
mbed_official 85:e1a8e879a6a9 62 us_ticker_init();
mbed_official 85:e1a8e879a6a9 63 }
mbed_official 300:55638feb26a4 64
mbed_official 304:89b9c3a9a045 65 if (us_ticker_appTimerID == TIMER_NULL) {
mbed_official 304:89b9c3a9a045 66 if (app_timer_create(&us_ticker_appTimerID, APP_TIMER_MODE_SINGLE_SHOT, us_ticker_app_timer_callback) != NRF_SUCCESS) {
mbed_official 304:89b9c3a9a045 67 /* placeholder to do something to recover from error */
mbed_official 304:89b9c3a9a045 68 return;
mbed_official 304:89b9c3a9a045 69 }
mbed_official 300:55638feb26a4 70 }
mbed_official 85:e1a8e879a6a9 71
mbed_official 304:89b9c3a9a045 72 if (us_ticker_appTimerRunning) {
mbed_official 304:89b9c3a9a045 73 return;
mbed_official 300:55638feb26a4 74 }
mbed_official 300:55638feb26a4 75
mbed_official 452:a2b30f7d1bc5 76 uint64_t currentCounter64;
mbed_official 304:89b9c3a9a045 77 app_timer_cnt_get(&currentCounter64);
mbed_official 304:89b9c3a9a045 78 uint32_t currentCounter = currentCounter64 & MAX_RTC_COUNTER_VAL;
mbed_official 304:89b9c3a9a045 79 uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL;
mbed_official 304:89b9c3a9a045 80 uint32_t ticksToCount = (targetCounter >= currentCounter) ?
mbed_official 304:89b9c3a9a045 81 (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter);
mbed_official 361:56c2a6244bba 82 if (ticksToCount < APP_TIMER_MIN_TIMEOUT_TICKS) { /* Honour the minimum value of the timeout_ticks parameter of app_timer_start() */
mbed_official 361:56c2a6244bba 83 ticksToCount = APP_TIMER_MIN_TIMEOUT_TICKS;
mbed_official 300:55638feb26a4 84 }
mbed_official 361:56c2a6244bba 85
mbed_official 361:56c2a6244bba 86 uint32_t rc;
mbed_official 361:56c2a6244bba 87 rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/);
mbed_official 361:56c2a6244bba 88 if (rc != NRF_SUCCESS) {
mbed_official 361:56c2a6244bba 89 /* placeholder to do something to recover from error */
mbed_official 361:56c2a6244bba 90 return;
mbed_official 361:56c2a6244bba 91 }
mbed_official 361:56c2a6244bba 92 us_ticker_appTimerRunning = true;
mbed_official 85:e1a8e879a6a9 93 }
mbed_official 85:e1a8e879a6a9 94
mbed_official 300:55638feb26a4 95 void us_ticker_disable_interrupt(void)
mbed_official 300:55638feb26a4 96 {
mbed_official 304:89b9c3a9a045 97 if (us_ticker_appTimerRunning) {
mbed_official 452:a2b30f7d1bc5 98 if (app_timer_stop(us_ticker_appTimerID) == NRF_SUCCESS) {
mbed_official 452:a2b30f7d1bc5 99 us_ticker_appTimerRunning = false;
mbed_official 452:a2b30f7d1bc5 100 }
mbed_official 304:89b9c3a9a045 101 }
mbed_official 85:e1a8e879a6a9 102 }
mbed_official 300:55638feb26a4 103
mbed_official 300:55638feb26a4 104 void us_ticker_clear_interrupt(void)
mbed_official 300:55638feb26a4 105 {
mbed_official 304:89b9c3a9a045 106 if (us_ticker_appTimerRunning) {
mbed_official 452:a2b30f7d1bc5 107 if (app_timer_stop(us_ticker_appTimerID) == NRF_SUCCESS) {
mbed_official 452:a2b30f7d1bc5 108 us_ticker_appTimerRunning = false;
mbed_official 452:a2b30f7d1bc5 109 }
mbed_official 304:89b9c3a9a045 110 }
mbed_official 85:e1a8e879a6a9 111 }