osd-contiki/cpu/cc26xx-cc13xx/dev/soc-rtc.c

168 lines
5.9 KiB
C
Raw Normal View History

2015-02-25 13:09:56 +01:00
/*
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*---------------------------------------------------------------------------*/
2015-02-25 13:09:56 +01:00
/**
* \addtogroup cc13xx-cc26xx-rtc
2015-02-25 13:09:56 +01:00
* @{
*
* \file
* Implementation of the CC13xx/CC26xx AON RTC driver
2015-02-25 13:09:56 +01:00
*/
/*---------------------------------------------------------------------------*/
2015-02-25 13:09:56 +01:00
#include "contiki.h"
#include "sys/energest.h"
#include "rtimer.h"
#include "lpm.h"
#include "ti-lib.h"
#include <stdint.h>
2015-05-01 17:15:33 +02:00
#include <stdbool.h>
2015-02-25 13:09:56 +01:00
/*---------------------------------------------------------------------------*/
#define soc_rtc_isr(...) AONRTCIntHandler(__VA_ARGS__)
2015-02-25 13:09:56 +01:00
/*---------------------------------------------------------------------------*/
/* Prototype of a function in clock.c. Called every time the handler fires */
void clock_update(void);
/*---------------------------------------------------------------------------*/
#define COMPARE_INCREMENT (RTIMER_SECOND / CLOCK_SECOND)
#define MULTIPLE_512_MASK 0xFFFFFE00
/*---------------------------------------------------------------------------*/
/*
* Used to test timer wraparounds.
*
* Set to 0xFFFFFFFA to test AON RTC second counter wraparound
* Set to 0xFFFA to test AON RTC 16.16 format wraparound
*/
#ifdef SOC_RTC_CONF_START_TICK_COUNT
#define SOC_RTC_START_TICK_COUNT SOC_RTC_CONF_START_TICK_COUNT
#else
#define SOC_RTC_START_TICK_COUNT 0
#endif
/*---------------------------------------------------------------------------*/
2015-02-25 13:09:56 +01:00
void
soc_rtc_init(void)
2015-02-25 13:09:56 +01:00
{
2015-05-01 17:15:33 +02:00
bool interrupts_disabled;
uint32_t next;
2015-02-25 13:09:56 +01:00
/* Disable and clear interrupts */
2015-05-01 17:15:33 +02:00
interrupts_disabled = ti_lib_int_master_disable();
2015-02-25 13:09:56 +01:00
ti_lib_aon_rtc_disable();
ti_lib_aon_rtc_event_clear(AON_RTC_CH0);
ti_lib_aon_rtc_event_clear(AON_RTC_CH1);
2015-02-25 13:09:56 +01:00
/* Setup the wakeup event */
ti_lib_aon_event_mcu_wake_up_set(AON_EVENT_MCU_WU0, AON_EVENT_RTC_CH0);
ti_lib_aon_event_mcu_wake_up_set(AON_EVENT_MCU_WU1, AON_EVENT_RTC_CH1);
ti_lib_aon_rtc_combined_event_config(AON_RTC_CH0 | AON_RTC_CH1);
HWREG(AON_RTC_BASE + AON_RTC_O_SEC) = SOC_RTC_START_TICK_COUNT;
next = ti_lib_aon_rtc_current_compare_value_get() + COMPARE_INCREMENT;
/* Configure channel 1 to start generating clock ticks. First tick at 512 */
ti_lib_aon_rtc_compare_value_set(AON_RTC_CH1, next);
/* Enable channel 1 and the RTC */
ti_lib_aon_rtc_channel_enable(AON_RTC_CH1);
2015-02-25 13:09:56 +01:00
ti_lib_aon_rtc_enable();
ti_lib_int_enable(INT_AON_RTC);
2015-05-01 17:15:33 +02:00
/* Re-enable interrupts */
if(!interrupts_disabled) {
ti_lib_int_master_enable();
}
2015-02-25 13:09:56 +01:00
}
/*---------------------------------------------------------------------------*/
rtimer_clock_t
soc_rtc_get_next_trigger()
2015-02-25 13:09:56 +01:00
{
rtimer_clock_t ch1 = ti_lib_aon_rtc_compare_value_get(AON_RTC_CH1);
2015-02-25 13:09:56 +01:00
if(HWREG(AON_RTC_BASE + AON_RTC_O_CHCTL) & AON_RTC_CHCTL_CH0_EN) {
rtimer_clock_t ch0 = ti_lib_aon_rtc_compare_value_get(AON_RTC_CH0);
2015-02-25 13:09:56 +01:00
return RTIMER_CLOCK_LT(ch0, ch1) ? ch0 : ch1;
2015-02-25 13:09:56 +01:00
}
return ch1;
2015-02-25 13:09:56 +01:00
}
/*---------------------------------------------------------------------------*/
void
soc_rtc_schedule_one_shot(uint32_t channel, uint32_t ticks)
2015-02-25 13:09:56 +01:00
{
if((channel != AON_RTC_CH0) && (channel != AON_RTC_CH1)) {
return;
}
2015-02-25 13:09:56 +01:00
/* Set the channel to fire a one-shot compare event at time==ticks */
ti_lib_aon_rtc_compare_value_set(channel, ticks);
ti_lib_aon_rtc_channel_enable(channel);
2015-02-25 13:09:56 +01:00
}
/*---------------------------------------------------------------------------*/
/* The AON RTC interrupt handler */
void
soc_rtc_isr(void)
2015-02-25 13:09:56 +01:00
{
uint32_t now, next;
2015-02-25 13:09:56 +01:00
ENERGEST_ON(ENERGEST_TYPE_IRQ);
now = ti_lib_aon_rtc_current_compare_value_get();
/* Adjust the s/w tick counter irrespective of which event trigger this */
clock_update();
if(ti_lib_aon_rtc_event_get(AON_RTC_CH1)) {
HWREG(AON_RTC_BASE + AON_RTC_O_EVFLAGS) = AON_RTC_EVFLAGS_CH1;
/*
* We need to keep ticking while we are awake, so we schedule the next
* event on the next 512 tick boundary. If we drop to deep sleep before it
* happens, lpm_drop() will reschedule us in the 'distant' future
*/
next = (now + COMPARE_INCREMENT) & MULTIPLE_512_MASK;
ti_lib_aon_rtc_compare_value_set(AON_RTC_CH1, next);
2015-02-25 13:09:56 +01:00
}
if(ti_lib_aon_rtc_event_get(AON_RTC_CH0)) {
ti_lib_aon_rtc_channel_disable(AON_RTC_CH0);
HWREG(AON_RTC_BASE + AON_RTC_O_EVFLAGS) = AON_RTC_EVFLAGS_CH0;
rtimer_run_next();
2015-02-25 13:09:56 +01:00
}
ENERGEST_OFF(ENERGEST_TYPE_IRQ);
}
/*---------------------------------------------------------------------------*/
/** @} */