ISSDK  1.7
IoT Sensing Software Development Kit
systick_utils.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /**
36  * @file systick_utils.c
37  * @brief Encapsulates the ARM sysTick counter, which is used for computing delays.
38  * ARM-core specific function that enables the ARM systick timer on Kinetis uCs.
39  * the timer is 24 bit so allows measurement of intervals up to 2^24/CORE_SYSTICK_HZ secs=0.35s for a 48MHz uC.
40 */
41 
42 #include "issdk_hal.h"
43 
44 // SysTick register definitions based on CMSIS definitions.
45 #define SYST_CSR SysTick->CTRL // SysTick Control & Status Register
46 #define SYST_RVR SysTick->LOAD // SysTick Reload Value Register
47 #define SYST_CVR SysTick->VAL // SysTick Current Value Register
48 
49 uint32_t g_ovf_stamp;
50 volatile uint32_t g_ovf_counter = 0;
51 
52 #if !defined(FSL_RTOS_FREE_RTOS) && !defined(ISSDK_USE_BLE_STACK)
53 // SDK specific SysTick Interrupt Handler
54 void SysTick_Handler(void)
55 {
56  g_ovf_counter += 1;
57 }
58 #endif
59 
60 uint32_t SysTick_GetOVFCount(void)
61 {
62 #ifdef ISSDK_USE_BLE_STACK
63  return OSA_TimeGetMsec();
64 #else
65  return g_ovf_counter;
66 #endif
67 }
68 
69 // ARM-core specific function to enable systicks.
71 {
72  SYST_CSR = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk |
73  SysTick_CTRL_ENABLE_Msk; // Enable systick from internal clock with Interrupts.
74  SYST_RVR = 0x00FFFFFFu; // Set reload to maximum 24 bit value.
75  return;
76 }
77 
78 // ARM-core specific function to store the current systick timer ticks.
80 {
81  // Store the 24 bit systick timer.
83  *pStart = SYST_CVR & 0x00FFFFFF;
84 }
85 
86 // ARM-core specific function to compute the elapsed systick timer ticks.
88 {
89  int32_t elapsed;
90 
91  // Subtract the stored start ticks and check for wraparound down through zero.
92  elapsed = *pStart - (SYST_CVR & 0x00FFFFFF);
93  elapsed += SYST_RVR * (SysTick_GetOVFCount() - g_ovf_stamp);
94 
95  return elapsed;
96 }
97 
98 // ARM-core specific function to compute the elapsed time in micro seconds.
100 {
101  uint32_t time_us, elapsed;
102  uint32_t systemCoreClock;
103 
104  elapsed = BOARD_SystickElapsedTicks(pStart);
105  systemCoreClock = CLOCK_GetFreq(kCLOCK_CoreSysClk);
106 
107  time_us = COUNT_TO_USEC(elapsed, systemCoreClock);
108 
109  // Update the 24 bit systick timer.
110  BOARD_SystickStart(pStart);
111 
112  return time_us;
113 }
114 
115 // ARM-core specific function to insert delays in milli seconds.
116 void BOARD_DELAY_ms(uint32_t delay_ms)
117 {
118  int32_t start, elapsed;
119  uint32_t systemCoreClock;
120 
121  BOARD_SystickStart(&start);
122  do // Loop for requested number of ms.
123  {
124  elapsed = BOARD_SystickElapsedTicks(&start);
125  systemCoreClock = CLOCK_GetFreq(kCLOCK_CoreSysClk);
126  } while(COUNT_TO_MSEC(elapsed, systemCoreClock) < delay_ms);
127 }
uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)
Function to compute the Elapsed Time.
Definition: systick_utils.c:99
#define SYST_CSR
Definition: systick_utils.c:45
int32_t BOARD_SystickElapsedTicks(int32_t *pStart)
Function to compute the Elapsed systicks.
Definition: systick_utils.c:87
uint32_t SysTick_GetOVFCount(void)
Definition: systick_utils.c:60
void SysTick_Handler(void)
Definition: systick_utils.c:54
void BOARD_DELAY_ms(uint32_t delay_ms)
Function to insert delays.
uint32_t g_ovf_stamp
Definition: systick_utils.c:49
void BOARD_SystickEnable(void)
Function to enable systicks framework.
Definition: systick_utils.c:70
volatile uint32_t g_ovf_counter
Definition: systick_utils.c:50
typedef int32_t(DATA_FORMAT_Append_t))(void *pData
The interface function to append the data on the formated stream.
void BOARD_SystickStart(int32_t *pStart)
Function to Record the Start systick.
Definition: systick_utils.c:79
#define SYST_RVR
Definition: systick_utils.c:46
#define SYST_CVR
Definition: systick_utils.c:47