2015-02-25 12:09:56 +00: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.
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \addtogroup cc26xx-clocks
|
|
|
|
* @{
|
|
|
|
*
|
2015-08-16 15:30:04 +00:00
|
|
|
* \defgroup cc26xx-software-clock Software Clock
|
2015-02-25 12:09:56 +00:00
|
|
|
*
|
2015-08-16 15:30:04 +00:00
|
|
|
* Implementation of the clock module for the CC26xx and CC13xx.
|
2015-02-25 12:09:56 +00:00
|
|
|
*
|
|
|
|
* The software clock uses the facilities provided by the AON RTC driver
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2015-08-16 15:30:04 +00:00
|
|
|
* Software clock implementation for the TI CC13xx/CC26xx
|
2015-02-25 12:09:56 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
|
|
|
|
#include "ti-lib.h"
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-08-16 15:30:04 +00:00
|
|
|
static volatile uint64_t count;
|
2015-02-25 12:09:56 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
power_domain_on(void)
|
|
|
|
{
|
|
|
|
ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH);
|
|
|
|
while(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) !=
|
|
|
|
PRCM_DOMAIN_POWER_ON);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_init(void)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, we configure GPT0 Timer A, which we subsequently use in
|
|
|
|
* clock_delay_usec
|
|
|
|
*
|
2015-05-01 14:41:53 +00:00
|
|
|
* We need to access registers, so firstly power up the PD and then enable
|
|
|
|
* the clock to GPT0.
|
2015-02-25 12:09:56 +00:00
|
|
|
*/
|
2015-05-01 14:41:53 +00:00
|
|
|
if(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) !=
|
|
|
|
PRCM_DOMAIN_POWER_ON) {
|
|
|
|
power_domain_on();
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:09:56 +00:00
|
|
|
ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_TIMER0);
|
|
|
|
ti_lib_prcm_load_set();
|
|
|
|
while(!ti_lib_prcm_load_get());
|
|
|
|
|
|
|
|
/* Disable both GPT0 timers */
|
|
|
|
HWREG(GPT0_BASE + GPT_O_CTL) &= ~(GPT_CTL_TAEN | GPT_CTL_TBEN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We assume that the clock is running at 48MHz, we use GPT0 Timer A,
|
|
|
|
* one-shot, countdown, prescaled by 48 gives us 1 tick per usec
|
|
|
|
*/
|
|
|
|
ti_lib_timer_configure(GPT0_BASE,
|
|
|
|
TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT);
|
|
|
|
|
|
|
|
/* Global config: split pair (2 x 16-bit wide) */
|
|
|
|
HWREG(GPT0_BASE + GPT_O_CFG) = TIMER_CFG_SPLIT_PAIR >> 24;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pre-scale value 47 pre-scales by 48
|
|
|
|
*
|
|
|
|
* ToDo: The theoretical value here should be 47 (to provide x48 prescale)
|
|
|
|
* However, 49 seems to give results much closer to the desired delay
|
|
|
|
*/
|
|
|
|
ti_lib_timer_prescale_set(GPT0_BASE, TIMER_B, 49);
|
|
|
|
|
|
|
|
/* GPT0 / Timer B: One shot, PWM interrupt enable */
|
|
|
|
HWREG(GPT0_BASE + GPT_O_TBMR) =
|
|
|
|
((TIMER_CFG_B_ONE_SHOT >> 8) & 0xFF) | GPT_TBMR_TBPWMIE;
|
2016-04-25 15:02:26 +00:00
|
|
|
|
|
|
|
/* enable sync with radio timer */
|
|
|
|
HWREGBITW(AON_RTC_BASE + AON_RTC_O_CTL, AON_RTC_CTL_RTC_UPD_EN_BITN) = 1;
|
2015-02-25 12:09:56 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-02 10:05:47 +00:00
|
|
|
static void
|
|
|
|
update_clock_variable(void)
|
|
|
|
{
|
|
|
|
uint32_t aon_rtc_secs_now;
|
|
|
|
uint32_t aon_rtc_secs_now2;
|
|
|
|
uint16_t aon_rtc_ticks_now;
|
|
|
|
|
|
|
|
do {
|
|
|
|
aon_rtc_secs_now = HWREG(AON_RTC_BASE + AON_RTC_O_SEC);
|
|
|
|
aon_rtc_ticks_now = HWREG(AON_RTC_BASE + AON_RTC_O_SUBSEC) >> 16;
|
|
|
|
aon_rtc_secs_now2 = HWREG(AON_RTC_BASE + AON_RTC_O_SEC);
|
|
|
|
} while(aon_rtc_secs_now != aon_rtc_secs_now2);
|
|
|
|
|
|
|
|
/* Convert AON RTC ticks to clock tick counter */
|
|
|
|
count = (aon_rtc_secs_now * CLOCK_SECOND) + (aon_rtc_ticks_now >> 9);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-02-21 00:14:10 +00:00
|
|
|
clock_time_t
|
2015-02-25 12:09:56 +00:00
|
|
|
clock_time(void)
|
|
|
|
{
|
2015-09-02 10:05:47 +00:00
|
|
|
update_clock_variable();
|
|
|
|
|
2015-08-16 15:30:04 +00:00
|
|
|
return (clock_time_t)(count & 0xFFFFFFFF);
|
2015-02-25 12:09:56 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_update(void)
|
|
|
|
{
|
2015-09-02 10:05:47 +00:00
|
|
|
update_clock_variable();
|
2015-02-25 12:09:56 +00:00
|
|
|
|
2015-08-16 15:30:04 +00:00
|
|
|
if(etimer_pending()) {
|
|
|
|
etimer_request_poll();
|
2015-02-25 12:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-02-21 00:14:10 +00:00
|
|
|
unsigned long
|
2015-02-25 12:09:56 +00:00
|
|
|
clock_seconds(void)
|
|
|
|
{
|
2015-08-16 15:30:04 +00:00
|
|
|
bool interrupts_disabled;
|
|
|
|
uint32_t secs_now;
|
|
|
|
|
|
|
|
interrupts_disabled = ti_lib_int_master_disable();
|
|
|
|
|
|
|
|
secs_now = ti_lib_aon_rtc_sec_get();
|
|
|
|
|
|
|
|
/* Re-enable interrupts */
|
|
|
|
if(!interrupts_disabled) {
|
|
|
|
ti_lib_int_master_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned long)secs_now;
|
2015-02-25 12:09:56 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_wait(clock_time_t i)
|
|
|
|
{
|
|
|
|
clock_time_t start;
|
|
|
|
|
|
|
|
start = clock_time();
|
|
|
|
while(clock_time() - start < (clock_time_t)i);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
clock_delay_usec(uint16_t len)
|
|
|
|
{
|
2015-05-01 14:41:35 +00:00
|
|
|
uint32_t clock_status;
|
|
|
|
|
2015-02-25 12:09:56 +00:00
|
|
|
if(ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) !=
|
|
|
|
PRCM_DOMAIN_POWER_ON) {
|
|
|
|
power_domain_on();
|
|
|
|
}
|
|
|
|
|
2015-05-01 14:41:35 +00:00
|
|
|
clock_status = HWREG(PRCM_BASE + PRCM_O_GPTCLKGR) & PRCM_GPIOCLKGR_CLK_EN;
|
|
|
|
|
|
|
|
ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_TIMER0);
|
|
|
|
ti_lib_prcm_load_set();
|
|
|
|
while(!ti_lib_prcm_load_get());
|
|
|
|
|
2015-02-25 12:09:56 +00:00
|
|
|
ti_lib_timer_load_set(GPT0_BASE, TIMER_B, len);
|
|
|
|
ti_lib_timer_enable(GPT0_BASE, TIMER_B);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for TBEN to clear. CC26xxware does not provide us with a convenient
|
|
|
|
* function, hence the direct register access here
|
|
|
|
*/
|
Use additive offsets
OR-ing an offset to a base address instead of adding it is dangerous
because it can only work if the base address is aligned enough for the
offset.
Moreover, if the base address or the offset has a value unknown at
compile time, then the assembly instructions dedicated to 'base +
offset' addressing on most CPUs can't be emitted by the compiler because
this would require the alignment of the base address against the offset
to be known in order to optimize 'base | offset' into 'base + offset'.
In that case, the compiler has to emit more instructions in order to
compute 'base | offset' on most CPUs, e.g. on ARM, which means larger
binary size and slower execution.
Hence, replace all occurrences of 'base | offset' with 'base + offset'.
This must become a coding rule.
Here are the results for the cc2538-demo example:
- Compilation of uart_init():
* before:
REG(regs->base | UART_CC) = 0;
200b78: f446 637c orr.w r3, r6, #4032 ; 0xfc0
200b7c: f043 0308 orr.w r3, r3, #8
200b80: 2200 movs r2, #0
200b82: 601a str r2, [r3, #0]
* now:
REG(regs->base + UART_CC) = 0;
200b7a: 2300 movs r3, #0
200b7c: f8c4 3fc8 str.w r3, [r4, #4040] ; 0xfc8
- Size of the .text section:
* before: 0x4c7c
* now: 0x4c28
* saved: 84 bytes
Signed-off-by: Benoît Thébaudeau <benoit.thebaudeau.dev@gmail.com>
2015-03-28 16:16:17 +00:00
|
|
|
while(HWREG(GPT0_BASE + GPT_O_CTL) & GPT_CTL_TBEN);
|
2015-05-01 14:41:35 +00:00
|
|
|
|
|
|
|
if(clock_status == 0) {
|
|
|
|
ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_TIMER0);
|
|
|
|
ti_lib_prcm_load_set();
|
|
|
|
while(!ti_lib_prcm_load_get());
|
|
|
|
}
|
2015-02-25 12:09:56 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \brief Obsolete delay function but we implement it here since some code
|
|
|
|
* still uses it
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clock_delay(unsigned int i)
|
|
|
|
{
|
|
|
|
clock_delay_usec(i);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|