Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
mbed-src/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/us_ticker.c@10:22480ac31879, 2014-11-27 (annotated)
- Committer:
- yihui
- Date:
- Thu Nov 27 09:30:36 2014 +0000
- Revision:
- 10:22480ac31879
- Parent:
- 9:05f0b5a3a70a
change to new revision hardware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 9:05f0b5a3a70a | 1 | /* mbed Microcontroller Library |
yihui | 9:05f0b5a3a70a | 2 | * Copyright (c) 2013 Nordic Semiconductor |
yihui | 9:05f0b5a3a70a | 3 | * |
yihui | 9:05f0b5a3a70a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 9:05f0b5a3a70a | 5 | * you may not use this file except in compliance with the License. |
yihui | 9:05f0b5a3a70a | 6 | * You may obtain a copy of the License at |
yihui | 9:05f0b5a3a70a | 7 | * |
yihui | 9:05f0b5a3a70a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 9:05f0b5a3a70a | 9 | * |
yihui | 9:05f0b5a3a70a | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 9:05f0b5a3a70a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 9:05f0b5a3a70a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 9:05f0b5a3a70a | 13 | * See the License for the specific language governing permissions and |
yihui | 9:05f0b5a3a70a | 14 | * limitations under the License. |
yihui | 9:05f0b5a3a70a | 15 | */ |
yihui | 9:05f0b5a3a70a | 16 | #include <stddef.h> |
yihui | 9:05f0b5a3a70a | 17 | #include "us_ticker_api.h" |
yihui | 9:05f0b5a3a70a | 18 | #include "cmsis.h" |
yihui | 9:05f0b5a3a70a | 19 | #include "PeripheralNames.h" |
yihui | 9:05f0b5a3a70a | 20 | #include "app_timer.h" |
yihui | 9:05f0b5a3a70a | 21 | |
yihui | 9:05f0b5a3a70a | 22 | static bool us_ticker_inited = false; |
yihui | 9:05f0b5a3a70a | 23 | static volatile bool us_ticker_appTimerRunning = false; |
yihui | 9:05f0b5a3a70a | 24 | static app_timer_id_t us_ticker_appTimerID = TIMER_NULL; |
yihui | 9:05f0b5a3a70a | 25 | |
yihui | 9:05f0b5a3a70a | 26 | void us_ticker_init(void) |
yihui | 9:05f0b5a3a70a | 27 | { |
yihui | 9:05f0b5a3a70a | 28 | if (us_ticker_inited) { |
yihui | 9:05f0b5a3a70a | 29 | return; |
yihui | 9:05f0b5a3a70a | 30 | } |
yihui | 9:05f0b5a3a70a | 31 | |
yihui | 9:05f0b5a3a70a | 32 | APP_TIMER_INIT(0 /*CFG_TIMER_PRESCALER*/ , 1 /*CFG_TIMER_MAX_INSTANCE*/, 1 /*CFG_TIMER_OPERATION_QUEUE_SIZE*/, false /*CFG_SCHEDULER_ENABLE*/); |
yihui | 9:05f0b5a3a70a | 33 | |
yihui | 9:05f0b5a3a70a | 34 | us_ticker_inited = true; |
yihui | 9:05f0b5a3a70a | 35 | } |
yihui | 9:05f0b5a3a70a | 36 | |
yihui | 9:05f0b5a3a70a | 37 | uint32_t us_ticker_read() |
yihui | 9:05f0b5a3a70a | 38 | { |
yihui | 9:05f0b5a3a70a | 39 | if (!us_ticker_inited) { |
yihui | 9:05f0b5a3a70a | 40 | us_ticker_init(); |
yihui | 9:05f0b5a3a70a | 41 | } |
yihui | 9:05f0b5a3a70a | 42 | |
yihui | 9:05f0b5a3a70a | 43 | timestamp_t value; |
yihui | 9:05f0b5a3a70a | 44 | app_timer_cnt_get(&value); /* This returns the RTC counter (which is fed by the 32khz crystal clock source) */ |
yihui | 9:05f0b5a3a70a | 45 | return ((value * 1000000) / (uint32_t)APP_TIMER_CLOCK_FREQ); /* Return a pseudo microsecond counter value. |
yihui | 9:05f0b5a3a70a | 46 | * This is only as precise as the 32khz low-freq |
yihui | 9:05f0b5a3a70a | 47 | * clock source, but could be adequate.*/ |
yihui | 9:05f0b5a3a70a | 48 | } |
yihui | 9:05f0b5a3a70a | 49 | |
yihui | 9:05f0b5a3a70a | 50 | /* An adaptor to interface us_ticker_irq_handler with the app_timer callback. |
yihui | 9:05f0b5a3a70a | 51 | * Needed because the irq_handler() doesn't take any parameter.*/ |
yihui | 9:05f0b5a3a70a | 52 | static void us_ticker_app_timer_callback(void *context) |
yihui | 9:05f0b5a3a70a | 53 | { |
yihui | 9:05f0b5a3a70a | 54 | us_ticker_appTimerRunning = false; |
yihui | 9:05f0b5a3a70a | 55 | us_ticker_irq_handler(); |
yihui | 9:05f0b5a3a70a | 56 | } |
yihui | 9:05f0b5a3a70a | 57 | |
yihui | 9:05f0b5a3a70a | 58 | void us_ticker_set_interrupt(timestamp_t timestamp) |
yihui | 9:05f0b5a3a70a | 59 | { |
yihui | 9:05f0b5a3a70a | 60 | if (!us_ticker_inited) { |
yihui | 9:05f0b5a3a70a | 61 | us_ticker_init(); |
yihui | 9:05f0b5a3a70a | 62 | } |
yihui | 9:05f0b5a3a70a | 63 | |
yihui | 9:05f0b5a3a70a | 64 | if (us_ticker_appTimerID == TIMER_NULL) { |
yihui | 9:05f0b5a3a70a | 65 | if (app_timer_create(&us_ticker_appTimerID, APP_TIMER_MODE_SINGLE_SHOT, us_ticker_app_timer_callback) != NRF_SUCCESS) { |
yihui | 9:05f0b5a3a70a | 66 | /* placeholder to do something to recover from error */ |
yihui | 9:05f0b5a3a70a | 67 | return; |
yihui | 9:05f0b5a3a70a | 68 | } |
yihui | 9:05f0b5a3a70a | 69 | } |
yihui | 9:05f0b5a3a70a | 70 | |
yihui | 9:05f0b5a3a70a | 71 | if (us_ticker_appTimerRunning) { |
yihui | 9:05f0b5a3a70a | 72 | return; |
yihui | 9:05f0b5a3a70a | 73 | } |
yihui | 9:05f0b5a3a70a | 74 | |
yihui | 9:05f0b5a3a70a | 75 | timestamp_t currentCounter64; |
yihui | 9:05f0b5a3a70a | 76 | app_timer_cnt_get(¤tCounter64); |
yihui | 9:05f0b5a3a70a | 77 | uint32_t currentCounter = currentCounter64 & MAX_RTC_COUNTER_VAL; |
yihui | 9:05f0b5a3a70a | 78 | uint32_t targetCounter = ((uint32_t)((timestamp * (uint64_t)APP_TIMER_CLOCK_FREQ) / 1000000) + 1) & MAX_RTC_COUNTER_VAL; |
yihui | 9:05f0b5a3a70a | 79 | uint32_t ticksToCount = (targetCounter >= currentCounter) ? |
yihui | 9:05f0b5a3a70a | 80 | (targetCounter - currentCounter) : (MAX_RTC_COUNTER_VAL + 1) - (currentCounter - targetCounter); |
yihui | 9:05f0b5a3a70a | 81 | if (ticksToCount < APP_TIMER_MIN_TIMEOUT_TICKS) { /* Honour the minimum value of the timeout_ticks parameter of app_timer_start() */ |
yihui | 9:05f0b5a3a70a | 82 | ticksToCount = APP_TIMER_MIN_TIMEOUT_TICKS; |
yihui | 9:05f0b5a3a70a | 83 | } |
yihui | 9:05f0b5a3a70a | 84 | |
yihui | 9:05f0b5a3a70a | 85 | uint32_t rc; |
yihui | 9:05f0b5a3a70a | 86 | rc = app_timer_start(us_ticker_appTimerID, ticksToCount, NULL /*p_context*/); |
yihui | 9:05f0b5a3a70a | 87 | if (rc != NRF_SUCCESS) { |
yihui | 9:05f0b5a3a70a | 88 | /* placeholder to do something to recover from error */ |
yihui | 9:05f0b5a3a70a | 89 | return; |
yihui | 9:05f0b5a3a70a | 90 | } |
yihui | 9:05f0b5a3a70a | 91 | us_ticker_appTimerRunning = true; |
yihui | 9:05f0b5a3a70a | 92 | } |
yihui | 9:05f0b5a3a70a | 93 | |
yihui | 9:05f0b5a3a70a | 94 | void us_ticker_disable_interrupt(void) |
yihui | 9:05f0b5a3a70a | 95 | { |
yihui | 9:05f0b5a3a70a | 96 | if (us_ticker_appTimerRunning) { |
yihui | 9:05f0b5a3a70a | 97 | app_timer_stop(us_ticker_appTimerID); |
yihui | 9:05f0b5a3a70a | 98 | } |
yihui | 9:05f0b5a3a70a | 99 | } |
yihui | 9:05f0b5a3a70a | 100 | |
yihui | 9:05f0b5a3a70a | 101 | void us_ticker_clear_interrupt(void) |
yihui | 9:05f0b5a3a70a | 102 | { |
yihui | 9:05f0b5a3a70a | 103 | if (us_ticker_appTimerRunning) { |
yihui | 9:05f0b5a3a70a | 104 | app_timer_stop(us_ticker_appTimerID); |
yihui | 9:05f0b5a3a70a | 105 | } |
yihui | 9:05f0b5a3a70a | 106 | } |