TUKS MCU Introductory course / TUKS-COURSE-TIMER
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elmot 1:d0dfbce63a89 1 /* mbed Microcontroller Library
elmot 1:d0dfbce63a89 2 *******************************************************************************
elmot 1:d0dfbce63a89 3 * Copyright (c) 2016, STMicroelectronics
elmot 1:d0dfbce63a89 4 * All rights reserved.
elmot 1:d0dfbce63a89 5 *
elmot 1:d0dfbce63a89 6 * Redistribution and use in source and binary forms, with or without
elmot 1:d0dfbce63a89 7 * modification, are permitted provided that the following conditions are met:
elmot 1:d0dfbce63a89 8 *
elmot 1:d0dfbce63a89 9 * 1. Redistributions of source code must retain the above copyright notice,
elmot 1:d0dfbce63a89 10 * this list of conditions and the following disclaimer.
elmot 1:d0dfbce63a89 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
elmot 1:d0dfbce63a89 12 * this list of conditions and the following disclaimer in the documentation
elmot 1:d0dfbce63a89 13 * and/or other materials provided with the distribution.
elmot 1:d0dfbce63a89 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
elmot 1:d0dfbce63a89 15 * may be used to endorse or promote products derived from this software
elmot 1:d0dfbce63a89 16 * without specific prior written permission.
elmot 1:d0dfbce63a89 17 *
elmot 1:d0dfbce63a89 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elmot 1:d0dfbce63a89 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elmot 1:d0dfbce63a89 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elmot 1:d0dfbce63a89 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
elmot 1:d0dfbce63a89 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
elmot 1:d0dfbce63a89 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
elmot 1:d0dfbce63a89 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
elmot 1:d0dfbce63a89 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
elmot 1:d0dfbce63a89 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
elmot 1:d0dfbce63a89 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elmot 1:d0dfbce63a89 28 *******************************************************************************
elmot 1:d0dfbce63a89 29 */
elmot 1:d0dfbce63a89 30 #include "device.h"
elmot 1:d0dfbce63a89 31
elmot 1:d0dfbce63a89 32 #if DEVICE_LOWPOWERTIMER
elmot 1:d0dfbce63a89 33
elmot 1:d0dfbce63a89 34 #include "ticker_api.h"
elmot 1:d0dfbce63a89 35 #include "lp_ticker_api.h"
elmot 1:d0dfbce63a89 36 #include "rtc_api.h"
elmot 1:d0dfbce63a89 37 #include "rtc_api_hal.h"
elmot 1:d0dfbce63a89 38
elmot 1:d0dfbce63a89 39 static uint8_t lp_ticker_inited = 0;
elmot 1:d0dfbce63a89 40
elmot 1:d0dfbce63a89 41 void lp_ticker_init(void)
elmot 1:d0dfbce63a89 42 {
elmot 1:d0dfbce63a89 43 if (lp_ticker_inited) return;
elmot 1:d0dfbce63a89 44 lp_ticker_inited = 1;
elmot 1:d0dfbce63a89 45
elmot 1:d0dfbce63a89 46 rtc_init();
elmot 1:d0dfbce63a89 47 rtc_set_irq_handler((uint32_t) lp_ticker_irq_handler);
elmot 1:d0dfbce63a89 48 }
elmot 1:d0dfbce63a89 49
elmot 1:d0dfbce63a89 50 uint32_t lp_ticker_read(void)
elmot 1:d0dfbce63a89 51 {
elmot 1:d0dfbce63a89 52 uint32_t usecs = 0;
elmot 1:d0dfbce63a89 53 time_t time = 0;
elmot 1:d0dfbce63a89 54
elmot 1:d0dfbce63a89 55 lp_ticker_init();
elmot 1:d0dfbce63a89 56
elmot 1:d0dfbce63a89 57 do {
elmot 1:d0dfbce63a89 58 time = rtc_read();
elmot 1:d0dfbce63a89 59 usecs = rtc_read_subseconds();
elmot 1:d0dfbce63a89 60 } while (time != rtc_read());
elmot 1:d0dfbce63a89 61
elmot 1:d0dfbce63a89 62 return (time * 1000000) + usecs;
elmot 1:d0dfbce63a89 63 }
elmot 1:d0dfbce63a89 64
elmot 1:d0dfbce63a89 65 void lp_ticker_set_interrupt(timestamp_t timestamp)
elmot 1:d0dfbce63a89 66 {
elmot 1:d0dfbce63a89 67 uint32_t delta;
elmot 1:d0dfbce63a89 68
elmot 1:d0dfbce63a89 69 delta = timestamp - lp_ticker_read();
elmot 1:d0dfbce63a89 70 rtc_set_wake_up_timer(delta);
elmot 1:d0dfbce63a89 71 }
elmot 1:d0dfbce63a89 72
elmot 1:d0dfbce63a89 73 void lp_ticker_disable_interrupt(void)
elmot 1:d0dfbce63a89 74 {
elmot 1:d0dfbce63a89 75 rtc_deactivate_wake_up_timer();
elmot 1:d0dfbce63a89 76 }
elmot 1:d0dfbce63a89 77
elmot 1:d0dfbce63a89 78 void lp_ticker_clear_interrupt(void)
elmot 1:d0dfbce63a89 79 {
elmot 1:d0dfbce63a89 80
elmot 1:d0dfbce63a89 81 }
elmot 1:d0dfbce63a89 82
elmot 1:d0dfbce63a89 83 #endif