Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c@304:89b9c3a9a045, 2014-08-29 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 29 17:15:07 2014 +0100
- Revision:
- 304:89b9c3a9a045
- Parent:
- 300:55638feb26a4
- Child:
- 361:56c2a6244bba
Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea
Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/
1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.
2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.
I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 127:ce7cebc0511f | 21 | |
| mbed_official | 304:89b9c3a9a045 | 22 | static bool us_ticker_inited = false; |
| mbed_official | 304:89b9c3a9a045 | 23 | static volatile bool us_ticker_appTimerRunning = false; |
| mbed_official | 304:89b9c3a9a045 | 24 | static app_timer_id_t us_ticker_appTimerID = TIMER_NULL; |
| mbed_official | 300:55638feb26a4 | 25 | |
| mbed_official | 300:55638feb26a4 | 26 | void us_ticker_init(void) |
| mbed_official | 300:55638feb26a4 | 27 | { |
| mbed_official | 304:89b9c3a9a045 | 28 | if (us_ticker_inited) { |
| mbed_official | 85:e1a8e879a6a9 | 29 | return; |
| mbed_official | 85:e1a8e879a6a9 | 30 | } |
| mbed_official | 300:55638feb26a4 | 31 | |
| mbed_official | 304:89b9c3a9a045 | 32 | APP_TIMER_INIT(0 /*CFG_TIMER_PRESCALER*/ , 1 /*CFG_TIMER_MAX_INSTANCE*/, 1 /*CFG_TIMER_OPERATION_QUEUE_SIZE*/, false /*CFG_SCHEDULER_ENABLE*/); |
| mbed_official | 300:55638feb26a4 | 33 | |
| mbed_official | 304:89b9c3a9a045 | 34 | us_ticker_inited = true; |
| mbed_official | 85:e1a8e879a6a9 | 35 | } |
| mbed_official | 85:e1a8e879a6a9 | 36 | |
| mbed_official | 300:55638feb26a4 | 37 | uint32_t us_ticker_read() |
| mbed_official | 300:55638feb26a4 | 38 | { |
| mbed_official | 304:89b9c3a9a045 | 39 | if (!us_ticker_inited) { |
| mbed_official | 304:89b9c3a9a045 | 40 | us_ticker_init(); |
| mbed_official | 304:89b9c3a9a045 | 41 | } |
| mbed_official | 304:89b9c3a9a045 | 42 | |
| mbed_official | 304:89b9c3a9a045 | 43 | timestamp_t value; |
| mbed_official | 304:89b9c3a9a045 | 44 | app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */ |
| mbed_official | 304:89b9c3a9a045 | 45 | return (uint32_t)((value * 1000000) / APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value. |
| mbed_official | 304:89b9c3a9a045 | 46 | * This is only as precise as the 32khz low-freq |
| mbed_official | 304:89b9c3a9a045 | 47 | * clock source, but could be adequate.*/ |
| mbed_official | 304:89b9c3a9a045 | 48 | } |
| mbed_official | 304:89b9c3a9a045 | 49 | |
| mbed_official | 304:89b9c3a9a045 | 50 | /* An adaptor to interface us_ticker_irq_handler with the app_timer callback. |
| mbed_official | 304:89b9c3a9a045 | 51 | * Needed because the irq_handler() doesn't take any parameter.*/ |
| mbed_official | 304:89b9c3a9a045 | 52 | static void us_ticker_app_timer_callback(void *context) |
| mbed_official | 304:89b9c3a9a045 | 53 | { |
| mbed_official | 304:89b9c3a9a045 | 54 | us_ticker_appTimerRunning = false; |
| mbed_official | 304:89b9c3a9a045 | 55 | us_ticker_irq_handler(); |
| mbed_official | 304:89b9c3a9a045 | 56 | } |
| mbed_official | 304:89b9c3a9a045 | 57 | |
| mbed_official | 304:89b9c3a9a045 | 58 | void us_ticker_set_interrupt(timestamp_t timestamp) |
| mbed_official | 304:89b9c3a9a045 | 59 | { |
| mbed_official | 304:89b9c3a9a045 | 60 | if (!us_ticker_inited) { |
| mbed_official | 85:e1a8e879a6a9 | 61 | us_ticker_init(); |
| mbed_official | 85:e1a8e879a6a9 | 62 | } |
| mbed_official | 300:55638feb26a4 | 63 | |
| mbed_official | 304:89b9c3a9a045 | 64 | if (us_ticker_appTimerID == TIMER_NULL) { |
| mbed_official | 304:89b9c3a9a045 | 65 | if (app_timer_create(&us_ticker_appTimerID, APP_TIMER_MODE_SINGLE_SHOT, us_ticker_app_timer_callback) != NRF_SUCCESS) { |
| mbed_official | 304:89b9c3a9a045 | 66 | /* placeholder to do something to recover from error */ |
| mbed_official | 304:89b9c3a9a045 | 67 | return; |
| mbed_official | 304:89b9c3a9a045 | 68 | } |
| mbed_official | 300:55638feb26a4 | 69 | } |
| mbed_official | 85:e1a8e879a6a9 | 70 | |
| mbed_official | 304:89b9c3a9a045 | 71 | if (us_ticker_appTimerRunning) { |
| mbed_official | 304:89b9c3a9a045 | 72 | return; |
| mbed_official | 300:55638feb26a4 | 73 | } |
| mbed_official | 300:55638feb26a4 | 74 | |
| mbed_official | 304:89b9c3a9a045 | 75 | timestamp_t currentCounter64; |
| mbed_official | 304:89b9c3a9a045 | 76 | app_timer_cnt_get(¤tCounter64); |
| mbed_official | 304:89b9c3a9a045 | 77 | uint32_t currentCounter = currentCounter64 & MAX_RTC_COUNTER_VAL; |
| mbed_official | 304:89b9c3a9a045 | 78 | uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL; |
| mbed_official | 304:89b9c3a9a045 | 79 | uint32_t ticksToCount = (targetCounter >= currentCounter) ? |
| mbed_official | 304:89b9c3a9a045 | 80 | (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter); |
| mbed_official | 304:89b9c3a9a045 | 81 | if (ticksToCount > 0) { |
| mbed_official | 304:89b9c3a9a045 | 82 | uint32_t rc; |
| mbed_official | 304:89b9c3a9a045 | 83 | rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); |
| mbed_official | 304:89b9c3a9a045 | 84 | if (rc != NRF_SUCCESS) { |
| mbed_official | 304:89b9c3a9a045 | 85 | /* placeholder to do something to recover from error */ |
| mbed_official | 304:89b9c3a9a045 | 86 | return; |
| mbed_official | 300:55638feb26a4 | 87 | } |
| mbed_official | 304:89b9c3a9a045 | 88 | us_ticker_appTimerRunning = true; |
| mbed_official | 300:55638feb26a4 | 89 | } |
| mbed_official | 85:e1a8e879a6a9 | 90 | } |
| mbed_official | 85:e1a8e879a6a9 | 91 | |
| mbed_official | 300:55638feb26a4 | 92 | void us_ticker_disable_interrupt(void) |
| mbed_official | 300:55638feb26a4 | 93 | { |
| mbed_official | 304:89b9c3a9a045 | 94 | if (us_ticker_appTimerRunning) { |
| mbed_official | 304:89b9c3a9a045 | 95 | app_timer_stop(us_ticker_appTimerID); |
| mbed_official | 304:89b9c3a9a045 | 96 | } |
| mbed_official | 85:e1a8e879a6a9 | 97 | } |
| mbed_official | 300:55638feb26a4 | 98 | |
| mbed_official | 300:55638feb26a4 | 99 | void us_ticker_clear_interrupt(void) |
| mbed_official | 300:55638feb26a4 | 100 | { |
| mbed_official | 304:89b9c3a9a045 | 101 | if (us_ticker_appTimerRunning) { |
| mbed_official | 304:89b9c3a9a045 | 102 | app_timer_stop(us_ticker_appTimerID); |
| mbed_official | 304:89b9c3a9a045 | 103 | } |
| mbed_official | 85:e1a8e879a6a9 | 104 | } |
