7 #include "derivative.h"
13 #include "contiki-conf.h"
20 #if DEFAULT_SYSTEM_CLOCK % CLOCK_SECOND
22 #error SYSCLOCK/CLOCK_SECOND gives a remainder: Accuracy of Systick is reduced.
25 #if (CLOCK_SETUP == 0)
27 #define CLOCK_TPM0_PRESCALE 0x00U
28 #define CLOCK_LPTMR_SOURCE 0x00U
29 #define CLOCK_LPTMR_PRESCALE 0x00U
31 #warning CLOCK_SETUP = 0 reduces timing accuracy!
33 #elif (CLOCK_SETUP == 1)
35 #define CLOCK_TPM0_PRESCALE 0x00U
36 #define CLOCK_LPTMR_SOURCE 0x03U
37 #define CLOCK_LPTMR_PRESCALE 0x02U
39 #elif (CLOCK_SETUP == 2)
41 #define CLOCK_TPM0_PRESCALE 0x07U
42 #define CLOCK_LPTMR_SOURCE 0x00U
43 #define CLOCK_LPTMR_PRESCALE 0x01U
45 #warning CLOCK_SETUP = 2 reduces timing accuracy!
47 #elif (CLOCK_SETUP == 3)
49 #define CLOCK_TPM0_PRESCALE 0x00U
50 #define CLOCK_LPTMR_SOURCE 0x03U
51 #define CLOCK_LPTMR_PRESCALE 0x01U
53 #elif (CLOCK_SETUP == 4)
55 #define CLOCK_TPM0_PRESCALE 0x00U
56 #define CLOCK_LPTMR_SOURCE 0x03U
57 #define CLOCK_LPTMR_PRESCALE 0x02U
60 #error Unsupported CLOCK_SETUP, cannot accurately set counter parameters.
63 #define RTIMER_CLOCK_TICK_RATIO (RTIMER_SECOND / CLOCK_SECOND)
66 static volatile uint64_t rt_ticks_startup, rt_ticks_epoch;
74 MCG_C1 |= MCG_C1_IRCLKEN_MASK | MCG_C1_IREFSTEN_MASK;
77 SIM_SOPT2 &= ~(SIM_SOPT2_TPMSRC_MASK);
78 SIM_SOPT2 |= SIM_SOPT2_TPMSRC(0x03);
79 SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK;
82 TPM0_CNT = TPM_CNT_COUNT(0x00);
84 TPM0_SC = TPM_SC_TOF_MASK | TPM_SC_PS(CLOCK_TPM0_PRESCALE);
88 TPM0_C0SC = TPM_CnSC_MSA_MASK;
89 TPM0_C0V = TPM_CnV_VAL(0x00);
101 rt_ticks_startup = 0;
104 TPM0_SC = TPM_SC_CMOD(0x01);
110 SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK;
111 LPTMR0_CSR = LPTMR_CSR_TCF_MASK;
113 #if (CLOCK_SETUP == 0)
114 LPTMR0_PSR = LPTMR_PSR_PCS(CLOCK_LPTMR_SOURCE)
115 | LPTMR_PSR_PRESCALE(CLOCK_LPTMR_PRESCALE)
116 | LPTMR_PSR_PBYP_MASK;
118 LPTMR0_PSR = LPTMR_PSR_PCS(CLOCK_LPTMR_SOURCE)
119 | LPTMR_PSR_PRESCALE(CLOCK_LPTMR_PRESCALE);
129 printf(
"ERROR: Invalid Systick Setting in clock.c\n\r");
133 port_conf_pin(SYSTICK_DEBUG_PORT, SYSTICK_DEBUG_PIN, (PORT_PCR_MUX_GPIO | PORT_PCR_ISF_MASK));
143 return rt_ticks_startup / RTIMER_CLOCK_TICK_RATIO;
149 rt_ticks_epoch = (uint64_t)sec * RTIMER_SECOND;
155 return rt_ticks_epoch / RTIMER_SECOND;
167 while(
clock_time() - start < (clock_time_t)i);
174 LPTMR0_CSR = LPTMR_CSR_TCF_MASK;
176 #if (CLOCK_SETUP == 0)
179 LPTMR0_CMR = dt / 31;
181 LPTMR0_CMR = (dt / 31) + 1;
187 LPTMR0_CSR = LPTMR_CSR_TEN_MASK;
189 while(!(LPTMR0_CSR & LPTMR_CSR_TCF_MASK));
190 LPTMR0_CSR = LPTMR_CSR_TCF_MASK;
205 uint64_t prev_rt_ticks_startup, cur_rt_ticks_startup_hi;
208 prev_rt_ticks_startup = rt_ticks_startup;
209 cur_rt_ticks_startup_hi = prev_rt_ticks_startup >> 16;
211 while(TPM0_SC & TPM_SC_TOF_MASK) {
212 TPM0_SC |= TPM_SC_TOF_MASK;
213 cur_rt_ticks_startup_hi++;
216 rt_ticks_startup = (cur_rt_ticks_startup_hi << 16) | now;
218 rt_ticks_epoch += rt_ticks_startup - prev_rt_ticks_startup;
231 void SysTick_Handler()
233 ENERGEST_ON(ENERGEST_TYPE_IRQ);
240 #if (DISABLE_WDOG == 0)
241 if(CPU_Watchdog_Disabled()) {
251 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
Header file for the real-time timer module.
static void start(void)
Start measurement.
TPM0 single interrupt vector for all sources.
#define RTIMER_NOW()
Get the current clock time.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
int etimer_pending(void)
Check if there are any non-expired event timers.
clock_time_t clock_time(void)
Get the current clock time.
Default definitions of C compiler quirk work-arounds.
void gpio_set_output(GPIO_Type *Port, uint32_t Pin_Mask)
Set pins with Pin_Mask of port with GPIOn_BASE_PTR to output.
Header file for the energy estimation mechanism
void clock_init()
Initialize the clock library.
static void update_ticks(void)
Update the software clock ticks and seconds.
__STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
Set Interrupt Priority.
#define CLOCK_SECOND
A second, measured in system clock time.
void gpio_clr_pin(GPIO_Type *Port, uint32_t Pin_Mask)
Clear pins with Pin_Mask of port with GPIOn_BASE_PTR low.
void port_conf_pin(PORT_Type *Port, uint8_t Pin, uint32_t PCR)
Configure the specified Pin of the port with PORTx_BASE_PTR with PCR.
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
void clock_wait(clock_time_t i)
Wait for a given number of ticks.
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
System Tick Configuration.
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Header file for the MKL25Z NVIC functions.
void clock_delay_usec(uint16_t usec)
Delay a given number of microseconds.
#define SCB_ICSR_PENDSTCLR_Msk
uint32_t port_pin_to_mask(uint8_t pin)
Convert a pin number (0 to 31) to a pin mask.
void clock_set_seconds(unsigned long sec)
Set the value of the platform seconds.