nes-proj/arch/cpu/simplelink-cc13xx-cc26xx/dev/rtimer-arch.c

191 lines
6.6 KiB
C
Raw Normal View History

2018-02-06 12:58:17 +00:00
/*
* Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
2018-02-06 12:58:17 +00:00
* 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.
*/
/**
* \addtogroup cc13xx-cc26xx-rtimer
2018-02-06 12:58:17 +00:00
* @{
*
* \file
* Implementation of the rtimer driver for CC13xx/CC26xx.
* \author
* Edvard Pettersen <e.pettersen@ti.com>
2018-02-06 12:58:17 +00:00
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
/*---------------------------------------------------------------------------*/
2018-05-08 19:39:20 +00:00
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/aon_event.h)
#include DeviceFamily_constructPath(driverlib/aon_rtc.h)
#include DeviceFamily_constructPath(driverlib/interrupt.h)
2018-02-06 12:58:17 +00:00
2018-05-08 19:39:20 +00:00
#include <ti/drivers/dpl/ClockP.h>
#include <ti/drivers/dpl/HwiP.h>
2018-05-08 19:39:20 +00:00
/*---------------------------------------------------------------------------*/
#include <stddef.h>
2018-02-06 12:58:17 +00:00
#include <stdint.h>
2018-05-08 19:39:20 +00:00
/*---------------------------------------------------------------------------*/
#define HWIP_RTC_CH AON_RTC_CH0
#define RTIMER_RTC_CH AON_RTC_CH1
/*---------------------------------------------------------------------------*/
typedef void (*isr_fxn_t)(void);
typedef void (*hwi_dispatch_fxn_t)(void);
2018-02-06 12:58:17 +00:00
static hwi_dispatch_fxn_t hwi_dispatch_fxn;
/*---------------------------------------------------------------------------*/
/**
* \brief Stub function used when creating the dummy clock object.
*/
2018-07-25 16:16:27 +00:00
static void
rtimer_clock_stub(uintptr_t unused)
{
(void)unused;
/* do nothing */
}
/*---------------------------------------------------------------------------*/
/**
* \brief The Man-in-the-Middle ISR hook for the HWI dispatch ISR. This
* will be the ISR dispatched when INT_AON_RTC_COMB is triggered,
* and will either dispatch the interrupt to the rtimer driver or
* the HWI driver, depening on which event triggered the interrupt.
*/
2018-02-06 12:58:17 +00:00
static void
rtimer_isr_hook(void)
{
2018-07-24 10:16:53 +00:00
if(AONRTCEventGet(RTIMER_RTC_CH)) {
2018-02-06 12:58:17 +00:00
AONRTCEventClear(RTIMER_RTC_CH);
AONRTCChannelDisable(RTIMER_RTC_CH);
rtimer_run_next();
}
/*
* HWI Dispatch clears the interrupt. If HWI wasn't triggered, clear
* the interrupt manually.
*/
2018-07-24 10:16:53 +00:00
if(AONRTCEventGet(HWIP_RTC_CH)) {
hwi_dispatch_fxn();
2018-07-24 10:16:53 +00:00
} else {
2018-02-06 12:58:17 +00:00
IntPendClear(INT_AON_RTC_COMB);
}
}
/*---------------------------------------------------------------------------*/
/**
* \brief TODO
*/
void
rtimer_arch_init(void)
{
uintptr_t key;
ClockP_Struct clk_object;
ClockP_Params clk_params;
volatile isr_fxn_t *ramvec_table;
key = HwiP_disable();
2018-05-08 19:39:20 +00:00
/*
* Create a dummy clock to guarantee the RAM vector table is initialized.
*
* Creating a dummy clock will trigger initialization of TimerP, which
* subsequently initializes the driverlib/interrupt.h module. It is the
* interrupt module that initializes the RAM vector table.
*
* It is safe to destruct the Clock object immediately afterwards.
*/
ClockP_Params_init(&clk_params);
ClockP_construct(&clk_object, rtimer_clock_stub, 0, &clk_params);
ClockP_destruct(&clk_object);
2018-02-06 12:58:17 +00:00
/* Try to access the RAM vector table. */
2018-07-24 10:16:53 +00:00
ramvec_table = (isr_fxn_t *)HWREG(NVIC_VTABLE);
if(!ramvec_table) {
/*
* Unable to find the RAM vector table is a serious fault.
* Spin-lock forever.
*/
for(;;) { /* hang */ }
2018-02-06 12:58:17 +00:00
}
/*
* The HWI Dispatch ISR is located at interrupt number INT_AON_RTC_COMB
* in the RAM vector table. Fetch and store it.
*/
hwi_dispatch_fxn = (hwi_dispatch_fxn_t)ramvec_table[INT_AON_RTC_COMB];
2018-07-24 10:16:53 +00:00
if(!hwi_dispatch_fxn) {
/*
* Unable to find the HWI dispatch ISR in the RAM vector table is
* a serious fault. Spin-lock forever.
*/
for(;;) { /* hang */ }
2018-02-06 12:58:17 +00:00
}
2018-02-08 10:45:47 +00:00
/*
* Override the INT_AON_RTC_COMB interrupt number with our own ISR hook,
* which will act as a man-in-the-middle ISR for the HWI dispatch.
*/
2018-02-06 12:58:17 +00:00
IntRegister(INT_AON_RTC_COMB, rtimer_isr_hook);
AONEventMcuWakeUpSet(AON_EVENT_MCU_WU1, AON_EVENT_RTC_CH1);
2018-05-08 19:39:20 +00:00
AONRTCCombinedEventConfig(HWIP_RTC_CH | RTIMER_RTC_CH);
HwiP_restore(key);
2018-02-06 12:58:17 +00:00
}
/*---------------------------------------------------------------------------*/
/**
* \brief Schedules an rtimer task to be triggered at time \p t.
* \param t The time when the task will need executed.
2018-02-06 12:58:17 +00:00
*
* \p t is an absolute time, in other words the task will be
* executed AT time \p t, not IN \p t rtimer ticks.
2018-02-06 12:58:17 +00:00
*
* This function schedules a one-shot event with the AON RTC.
2018-02-06 12:58:17 +00:00
*
* This functions converts \p t to a value suitable for the AON RTC.
2018-02-06 12:58:17 +00:00
*/
void
rtimer_arch_schedule(rtimer_clock_t t)
{
/* Convert the rtimer tick value to a value suitable for the AON RTC */
AONRTCCompareValueSet(RTIMER_RTC_CH, (uint32_t)t);
AONRTCChannelEnable(RTIMER_RTC_CH);
}
/*---------------------------------------------------------------------------*/
/**
* \brief Returns the current real-time clock time.
* \return The current rtimer time in ticks.
2018-02-06 12:58:17 +00:00
*
* The value is read from the AON RTC counter and converted to a
* number of rtimer ticks.
2018-02-06 12:58:17 +00:00
*/
rtimer_clock_t
rtimer_arch_now()
{
return (rtimer_clock_t)AONRTCCurrentCompareValueGet();
2018-02-06 12:58:17 +00:00
}
/*---------------------------------------------------------------------------*/
/** @} */