mbed library sources. Supersedes mbed-src.

Fork of mbed by teralytic

Committer:
rodriguise
Date:
Mon Oct 17 18:47:01 2016 +0000
Revision:
148:4802eb17e82b
Parent:
147:30b64687e01f
backup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /**
<> 144:ef7eb2e8f9f7 2 *******************************************************************************
<> 144:ef7eb2e8f9f7 3 * @file lpticker.c
<> 144:ef7eb2e8f9f7 4 * @brief Implementation of a Low Power Ticker functionality based on RTC
<> 144:ef7eb2e8f9f7 5 * @internal
<> 144:ef7eb2e8f9f7 6 * @author ON Semiconductor
<> 144:ef7eb2e8f9f7 7 * $Rev: $
<> 144:ef7eb2e8f9f7 8 * $Date: $
<> 144:ef7eb2e8f9f7 9 ******************************************************************************
<> 147:30b64687e01f 10 * Copyright 2016 Semiconductor Components Industries LLC (d/b/a “ON Semiconductor”).
<> 147:30b64687e01f 11 * All rights reserved. This software and/or documentation is licensed by ON Semiconductor
<> 147:30b64687e01f 12 * under limited terms and conditions. The terms and conditions pertaining to the software
<> 147:30b64687e01f 13 * and/or documentation are available at http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf
<> 147:30b64687e01f 14 * (“ON Semiconductor Standard Terms and Conditions of Sale, Section 8 Software”) and
<> 147:30b64687e01f 15 * if applicable the software license agreement. Do not use this software and/or
<> 147:30b64687e01f 16 * documentation unless you have carefully read and you agree to the limited terms and
<> 147:30b64687e01f 17 * conditions. By using this software and/or documentation, you agree to the limited
<> 147:30b64687e01f 18 * terms and conditions.
<> 144:ef7eb2e8f9f7 19 *
<> 144:ef7eb2e8f9f7 20 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
<> 144:ef7eb2e8f9f7 21 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
<> 144:ef7eb2e8f9f7 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
<> 144:ef7eb2e8f9f7 23 * ON SEMICONDUCTOR SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL,
<> 144:ef7eb2e8f9f7 24 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
<> 144:ef7eb2e8f9f7 25 * @endinternal
<> 144:ef7eb2e8f9f7 26 *
<> 144:ef7eb2e8f9f7 27 * @ingroup lpticker
<> 144:ef7eb2e8f9f7 28 *
<> 144:ef7eb2e8f9f7 29 * @details
<> 144:ef7eb2e8f9f7 30 * This is Low Power ticker derived from RTC
<> 144:ef7eb2e8f9f7 31 *
<> 144:ef7eb2e8f9f7 32 */
<> 144:ef7eb2e8f9f7 33
<> 144:ef7eb2e8f9f7 34 #include "device.h"
<> 144:ef7eb2e8f9f7 35 #if DEVICE_LOWPOWERTIMER
<> 144:ef7eb2e8f9f7 36
<> 144:ef7eb2e8f9f7 37 #include "sleep_api.h"
<> 144:ef7eb2e8f9f7 38 #include "cmsis_nvic.h"
<> 144:ef7eb2e8f9f7 39 #include "lp_ticker_api.h"
<> 144:ef7eb2e8f9f7 40 #include "rtc.h"
<> 144:ef7eb2e8f9f7 41 #include "rtc_map.h"
<> 144:ef7eb2e8f9f7 42 #include "sleep.h"
<> 144:ef7eb2e8f9f7 43
<> 144:ef7eb2e8f9f7 44 /* Initialize the RTC for low power ticker */
<> 144:ef7eb2e8f9f7 45 void lp_ticker_init()
<> 144:ef7eb2e8f9f7 46 {
<> 144:ef7eb2e8f9f7 47 fRtcInit();
<> 144:ef7eb2e8f9f7 48 }
<> 144:ef7eb2e8f9f7 49
<> 144:ef7eb2e8f9f7 50 /* Return the current RTC counter value in us */
<> 144:ef7eb2e8f9f7 51 uint32_t lp_ticker_read()
<> 144:ef7eb2e8f9f7 52 {
<> 144:ef7eb2e8f9f7 53 return (uint32_t)(fRtcRead() & 0xFFFFFFFF); /* TODO Truncating 64 bit value to 32 bit */
<> 144:ef7eb2e8f9f7 54 }
<> 144:ef7eb2e8f9f7 55
<> 144:ef7eb2e8f9f7 56 /* Set interrupt for specified time */
<> 144:ef7eb2e8f9f7 57 void lp_ticker_set_interrupt(timestamp_t timestamp)
<> 144:ef7eb2e8f9f7 58 {
<> 144:ef7eb2e8f9f7 59 /* The RTC Match register needs to be Set to the RTC alarm value */
<> 144:ef7eb2e8f9f7 60 fRtcSetInterrupt(timestamp);
<> 144:ef7eb2e8f9f7 61 }
<> 144:ef7eb2e8f9f7 62
<> 144:ef7eb2e8f9f7 63 /*Return the time that gets cut off when you return just a 32 bit us resolution number */
<> 144:ef7eb2e8f9f7 64 uint32_t lp_ticker_get_overflows_counter(void)
<> 144:ef7eb2e8f9f7 65 {
<> 144:ef7eb2e8f9f7 66 /* To check; do we need an counter in software in RTC to find overflows */
<> 144:ef7eb2e8f9f7 67 uint64_t now = fRtcRead();
<> 144:ef7eb2e8f9f7 68 uint32_t overflow = (now & 0xFFFFFFFF00000000) >> 32;
<> 144:ef7eb2e8f9f7 69 return overflow;
<> 144:ef7eb2e8f9f7 70 }
<> 144:ef7eb2e8f9f7 71
<> 144:ef7eb2e8f9f7 72 /* Return the RTC Match counter contents */
<> 144:ef7eb2e8f9f7 73 uint32_t lp_ticker_get_compare_match()
<> 144:ef7eb2e8f9f7 74 {
<> 144:ef7eb2e8f9f7 75 /* read the alarms and convert to us */
<> 144:ef7eb2e8f9f7 76 uint16_t sub_second_alarm = RTCREG->SUB_SECOND_ALARM;
<> 144:ef7eb2e8f9f7 77 uint32_t second_alarm = RTCREG->SECOND_ALARM;
<> 144:ef7eb2e8f9f7 78 uint64_t alarm_us = (uint64_t)((((float)sub_second_alarm / RTC_CLOCK_HZ) * RTC_SEC_TO_US) +
<> 144:ef7eb2e8f9f7 79 (second_alarm * RTC_SEC_TO_US));
<> 144:ef7eb2e8f9f7 80 /* TODO truncating to 32 bits */
<> 144:ef7eb2e8f9f7 81 return (uint32_t)(alarm_us & 0xFFFFFFFF);
<> 144:ef7eb2e8f9f7 82 }
<> 144:ef7eb2e8f9f7 83
<> 144:ef7eb2e8f9f7 84 /* sleep until alarm */
<> 144:ef7eb2e8f9f7 85 void lp_ticker_sleep_until(uint32_t now, uint32_t time)
<> 144:ef7eb2e8f9f7 86 {
<> 144:ef7eb2e8f9f7 87 /* Set the interrupt */
<> 144:ef7eb2e8f9f7 88 lp_ticker_set_interrupt(time);
<> 144:ef7eb2e8f9f7 89
<> 144:ef7eb2e8f9f7 90 /* Go to sleep */
<> 144:ef7eb2e8f9f7 91 sleep_t obj;
<> 144:ef7eb2e8f9f7 92 obj.SleepType = SLEEP_TYPE_NONE;
<> 144:ef7eb2e8f9f7 93 obj.timeToSleep = time - now;
<> 144:ef7eb2e8f9f7 94
<> 144:ef7eb2e8f9f7 95 mbed_enter_sleep(&obj);
<> 144:ef7eb2e8f9f7 96 /* TBD: This is dummy exit for now; once the entered sleep it should be
<> 144:ef7eb2e8f9f7 97 removed and sleep exit should happen through interrupt */
<> 144:ef7eb2e8f9f7 98 mbed_exit_sleep(&obj);
<> 144:ef7eb2e8f9f7 99 }
<> 144:ef7eb2e8f9f7 100
<> 144:ef7eb2e8f9f7 101 /** Disable low power ticker interrupt
<> 144:ef7eb2e8f9f7 102 *
<> 144:ef7eb2e8f9f7 103 */
<> 144:ef7eb2e8f9f7 104 void lp_ticker_disable_interrupt(void)
<> 144:ef7eb2e8f9f7 105 {
<> 144:ef7eb2e8f9f7 106 /* TODO : This is an empty implementation for now */
<> 144:ef7eb2e8f9f7 107 }
<> 144:ef7eb2e8f9f7 108
<> 144:ef7eb2e8f9f7 109 /** Clear the low power ticker interrupt
<> 144:ef7eb2e8f9f7 110 *
<> 144:ef7eb2e8f9f7 111 */
<> 144:ef7eb2e8f9f7 112 void lp_ticker_clear_interrupt(void)
<> 144:ef7eb2e8f9f7 113 {
<> 144:ef7eb2e8f9f7 114 /* TODO : This is an empty implementation for now */
<> 144:ef7eb2e8f9f7 115 }
<> 144:ef7eb2e8f9f7 116
<> 144:ef7eb2e8f9f7 117 #endif /* DEVICE_LOWPOWERTIMER */