1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<meta http-equiv="X-UA-Compatible" content="IE=9"/>
6<meta name="generator" content="Doxygen 1.8.13"/>
7<meta name="viewport" content="width=device-width, initial-scale=1"/>
8<title>ISSDK: drivers/systick/systick_utils.c Source File</title>
9<link href="tabs.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="dynsections.js"></script>
12<link href="issdk_stylesheet.css" rel="stylesheet" type="text/css" />
13</head>
14<body>
15<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
16<div id="titlearea">
17<table cellspacing="0" cellpadding="0">
18 <tbody>
19 <tr style="height: 56px;">
20  <td id="projectlogo"><img alt="Logo" src="nxp_logo_small.png"/></td>
21  <td id="projectalign" style="padding-left: 0.5em;">
22   <div id="projectname">ISSDK
23   &#160;<span id="projectnumber">1.8</span>
24   </div>
25   <div id="projectbrief">IoT Sensing Software Development Kit</div>
26  </td>
27 </tr>
28 </tbody>
29</table>
30</div>
31<!-- end header part -->
32<!-- Generated by Doxygen 1.8.13 -->
33<script type="text/javascript" src="menudata.js"></script>
34<script type="text/javascript" src="menu.js"></script>
35<script type="text/javascript">
36$(function() {
37  initMenu('',false,false,'search.php','Search');
38});
39</script>
40<div id="main-nav"></div>
41<div id="nav-path" class="navpath">
42  <ul>
43<li class="navelem"><a class="el" href="dir_14bc92f4b96c8519b376567118ac28b3.html">drivers</a></li><li class="navelem"><a class="el" href="dir_f670b913935809da779923b5e12832cc.html">systick</a></li>  </ul>
44</div>
45</div><!-- top -->
46<div class="header">
47  <div class="headertitle">
48<div class="title">systick_utils.c</div>  </div>
49</div><!--header-->
50<div class="contents">
51<a href="a00530.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright (c) 2016, Freescale Semiconductor, Inc.</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> * Copyright 2016-2017 NXP</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * All rights reserved.</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> *</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * SPDX-License-Identifier: BSD-3-Clause</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> */</span></div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"></span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment">/**</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * @file  systick_utils.c</span></div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * @brief Encapsulates the ARM sysTick counter, which is used for computing delays.</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> *         ARM-core specific function that enables the ARM systick timer on Kinetis uCs.</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *         the timer is 24 bit so allows measurement of intervals up to 2^24/CORE_SYSTICK_HZ secs=0.35s for a 48MHz uC.</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment">*/</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="preprocessor">#include &quot;issdk_hal.h&quot;</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment">// SysTick register definitions based on CMSIS definitions.</span></div><div class="line"><a name="l00019"></a><span class="lineno"><a class="line" href="a00530.html#ab26b3fc75982181f81b185b206e897f6">   19</a></span>&#160;<span class="preprocessor">#define SYST_CSR SysTick-&gt;CTRL // SysTick Control &amp; Status Register</span></div><div class="line"><a name="l00020"></a><span class="lineno"><a class="line" href="a00530.html#a4e8efcc1f2b551dbf3cb0aae1231e380">   20</a></span>&#160;<span class="preprocessor">#define SYST_RVR SysTick-&gt;LOAD // SysTick Reload Value Register</span></div><div class="line"><a name="l00021"></a><span class="lineno"><a class="line" href="a00530.html#ae3dc4d2dbfdf38c593a5581415fecfed">   21</a></span>&#160;<span class="preprocessor">#define SYST_CVR SysTick-&gt;VAL  // SysTick Current Value Register</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="a00530.html#ac2d41df1a7ed0204bf5ff703fe298d0d">   23</a></span>&#160;uint32_t <a class="code" href="a00530.html#ac2d41df1a7ed0204bf5ff703fe298d0d">g_ovf_stamp</a>;</div><div class="line"><a name="l00024"></a><span class="lineno"><a class="line" href="a00530.html#a9f513763df42a1ba7bb6955361e29707">   24</a></span>&#160;<span class="keyword">volatile</span> uint32_t <a class="code" href="a00530.html#a9f513763df42a1ba7bb6955361e29707">g_ovf_counter</a> = 0;</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="preprocessor">#ifndef SDK_OS_FREE_RTOS</span></div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="comment">// SDK specific SysTick Interrupt Handler</span></div><div class="line"><a name="l00028"></a><span class="lineno"><a class="line" href="a00530.html#ab5e09814056d617c521549e542639b7e">   28</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="a00530.html#ab5e09814056d617c521549e542639b7e">SysTick_Handler</a>(<span class="keywordtype">void</span>)</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;{</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;    <a class="code" href="a00530.html#a9f513763df42a1ba7bb6955361e29707">g_ovf_counter</a> += 1;</div><div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;}</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;<span class="comment">// ARM-core specific function to enable systicks.</span></div><div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="a00533.html#ad43629969994e92bd3620e630bda24c8">   35</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="a00530.html#ad43629969994e92bd3620e630bda24c8">BOARD_SystickEnable</a>(<span class="keywordtype">void</span>)</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;{</div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    <a class="code" href="a00530.html#ab26b3fc75982181f81b185b206e897f6">SYST_CSR</a> = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk |</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;               SysTick_CTRL_ENABLE_Msk; <span class="comment">// Enable systick from internal clock with Interrupts.</span></div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;    <a class="code" href="a00530.html#a4e8efcc1f2b551dbf3cb0aae1231e380">SYST_RVR</a> = 0x00FFFFFFu;             <span class="comment">// Set reload to maximum 24 bit value.</span></div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    <span class="keywordflow">return</span>;</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;}</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="comment">// ARM-core specific function to store the current systick timer ticks.</span></div><div class="line"><a name="l00044"></a><span class="lineno"><a class="line" href="a00533.html#ac4471deeef0c2a93b7d4263259b0ed63">   44</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="a00530.html#ac4471deeef0c2a93b7d4263259b0ed63">BOARD_SystickStart</a>(<a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> *pStart)</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;{</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;    <span class="comment">// Store the 24 bit systick timer.</span></div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;    <a class="code" href="a00530.html#ac2d41df1a7ed0204bf5ff703fe298d0d">g_ovf_stamp</a> = <a class="code" href="a00530.html#a9f513763df42a1ba7bb6955361e29707">g_ovf_counter</a>;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    *pStart = <a class="code" href="a00530.html#ae3dc4d2dbfdf38c593a5581415fecfed">SYST_CVR</a> &amp; 0x00FFFFFF;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;}</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;<span class="comment">// ARM-core specific function to compute the elapsed systick timer ticks.</span></div><div class="line"><a name="l00052"></a><span class="lineno"><a class="line" href="a00533.html#a5526698540030255f601fc77e6e80bda">   52</a></span>&#160;<a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> <a class="code" href="a00530.html#a5526698540030255f601fc77e6e80bda">BOARD_SystickElapsedTicks</a>(<a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> *pStart)</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;{</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    <a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> elapsed;</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;    <span class="comment">// Subtract the stored start ticks and check for wraparound down through zero.</span></div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    elapsed = *pStart - (<a class="code" href="a00530.html#ae3dc4d2dbfdf38c593a5581415fecfed">SYST_CVR</a> &amp; 0x00FFFFFF);</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;    elapsed += <a class="code" href="a00530.html#a4e8efcc1f2b551dbf3cb0aae1231e380">SYST_RVR</a> * (<a class="code" href="a00530.html#a9f513763df42a1ba7bb6955361e29707">g_ovf_counter</a> - <a class="code" href="a00530.html#ac2d41df1a7ed0204bf5ff703fe298d0d">g_ovf_stamp</a>);</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;    <span class="keywordflow">return</span> elapsed;</div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;}</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="comment">// ARM-core specific function to compute the elapsed time in micro seconds.</span></div><div class="line"><a name="l00064"></a><span class="lineno"><a class="line" href="a00533.html#a85a32737b730fbb713ec910e1ad17c74">   64</a></span>&#160;uint32_t <a class="code" href="a00530.html#a85a32737b730fbb713ec910e1ad17c74">BOARD_SystickElapsedTime_us</a>(<a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> *pStart)</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;{</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    uint32_t time_us, elapsed;</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;    uint32_t systemCoreClock;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    elapsed = <a class="code" href="a00530.html#a5526698540030255f601fc77e6e80bda">BOARD_SystickElapsedTicks</a>(pStart);</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    systemCoreClock = CLOCK_GetFreq(kCLOCK_CoreSysClk);</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;    time_us = COUNT_TO_USEC(elapsed, systemCoreClock);</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    <span class="comment">// Update the 24 bit systick timer.</span></div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;    <a class="code" href="a00530.html#ac4471deeef0c2a93b7d4263259b0ed63">BOARD_SystickStart</a>(pStart);</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    <span class="keywordflow">return</span> time_us;</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;}</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;<span class="comment">// ARM-core specific function to insert delays in milli seconds.</span></div><div class="line"><a name="l00081"></a><span class="lineno"><a class="line" href="a00533.html#ab052b802081fd87e1bda8d56e2c0db42">   81</a></span>&#160;<span class="keywordtype">void</span> <a class="code" href="a00530.html#ab052b802081fd87e1bda8d56e2c0db42">BOARD_DELAY_ms</a>(uint32_t delay_ms)</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;{</div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    <a class="code" href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a> start, elapsed;</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;    uint32_t systemCoreClock = CLOCK_GetFreq(kCLOCK_CoreSysClk);</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;    <a class="code" href="a00530.html#ac4471deeef0c2a93b7d4263259b0ed63">BOARD_SystickStart</a>(&amp;start);</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;    <span class="keywordflow">do</span> <span class="comment">// Loop for requested number of ms.</span></div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    {</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;        elapsed = <a class="code" href="a00530.html#a5526698540030255f601fc77e6e80bda">BOARD_SystickElapsedTicks</a>(&amp;start);</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    } <span class="keywordflow">while</span>(COUNT_TO_MSEC(elapsed, systemCoreClock) &lt; delay_ms);</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;}</div><div class="ttc" id="a00530_html_ab052b802081fd87e1bda8d56e2c0db42"><div class="ttname"><a href="a00530.html#ab052b802081fd87e1bda8d56e2c0db42">BOARD_DELAY_ms</a></div><div class="ttdeci">void BOARD_DELAY_ms(uint32_t delay_ms)</div><div class="ttdoc">Function to insert delays. </div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00081">systick_utils.c:81</a></div></div>
52<div class="ttc" id="a00521_html_a5d7069350a7d7e65686c77a8038f462f"><div class="ttname"><a href="a00521.html#a5d7069350a7d7e65686c77a8038f462f">int32_t</a></div><div class="ttdeci">typedef int32_t(DATA_FORMAT_Append_t))(void *pData</div><div class="ttdoc">The interface function to append the data on the formated stream. </div></div>
53<div class="ttc" id="a00530_html_ae3dc4d2dbfdf38c593a5581415fecfed"><div class="ttname"><a href="a00530.html#ae3dc4d2dbfdf38c593a5581415fecfed">SYST_CVR</a></div><div class="ttdeci">#define SYST_CVR</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00021">systick_utils.c:21</a></div></div>
54<div class="ttc" id="a00530_html_a4e8efcc1f2b551dbf3cb0aae1231e380"><div class="ttname"><a href="a00530.html#a4e8efcc1f2b551dbf3cb0aae1231e380">SYST_RVR</a></div><div class="ttdeci">#define SYST_RVR</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00020">systick_utils.c:20</a></div></div>
55<div class="ttc" id="a00530_html_ac4471deeef0c2a93b7d4263259b0ed63"><div class="ttname"><a href="a00530.html#ac4471deeef0c2a93b7d4263259b0ed63">BOARD_SystickStart</a></div><div class="ttdeci">void BOARD_SystickStart(int32_t *pStart)</div><div class="ttdoc">Function to Record the Start systick. </div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00044">systick_utils.c:44</a></div></div>
56<div class="ttc" id="a00530_html_ac2d41df1a7ed0204bf5ff703fe298d0d"><div class="ttname"><a href="a00530.html#ac2d41df1a7ed0204bf5ff703fe298d0d">g_ovf_stamp</a></div><div class="ttdeci">uint32_t g_ovf_stamp</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00023">systick_utils.c:23</a></div></div>
57<div class="ttc" id="a00530_html_a85a32737b730fbb713ec910e1ad17c74"><div class="ttname"><a href="a00530.html#a85a32737b730fbb713ec910e1ad17c74">BOARD_SystickElapsedTime_us</a></div><div class="ttdeci">uint32_t BOARD_SystickElapsedTime_us(int32_t *pStart)</div><div class="ttdoc">Function to compute the Elapsed Time. </div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00064">systick_utils.c:64</a></div></div>
58<div class="ttc" id="a00530_html_ab26b3fc75982181f81b185b206e897f6"><div class="ttname"><a href="a00530.html#ab26b3fc75982181f81b185b206e897f6">SYST_CSR</a></div><div class="ttdeci">#define SYST_CSR</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00019">systick_utils.c:19</a></div></div>
59<div class="ttc" id="a00530_html_a9f513763df42a1ba7bb6955361e29707"><div class="ttname"><a href="a00530.html#a9f513763df42a1ba7bb6955361e29707">g_ovf_counter</a></div><div class="ttdeci">volatile uint32_t g_ovf_counter</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00024">systick_utils.c:24</a></div></div>
60<div class="ttc" id="a00530_html_ab5e09814056d617c521549e542639b7e"><div class="ttname"><a href="a00530.html#ab5e09814056d617c521549e542639b7e">SysTick_Handler</a></div><div class="ttdeci">void SysTick_Handler(void)</div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00028">systick_utils.c:28</a></div></div>
61<div class="ttc" id="a00530_html_ad43629969994e92bd3620e630bda24c8"><div class="ttname"><a href="a00530.html#ad43629969994e92bd3620e630bda24c8">BOARD_SystickEnable</a></div><div class="ttdeci">void BOARD_SystickEnable(void)</div><div class="ttdoc">Function to enable systicks framework. </div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00035">systick_utils.c:35</a></div></div>
62<div class="ttc" id="a00530_html_a5526698540030255f601fc77e6e80bda"><div class="ttname"><a href="a00530.html#a5526698540030255f601fc77e6e80bda">BOARD_SystickElapsedTicks</a></div><div class="ttdeci">int32_t BOARD_SystickElapsedTicks(int32_t *pStart)</div><div class="ttdoc">Function to compute the Elapsed systicks. </div><div class="ttdef"><b>Definition:</b> <a href="a00530_source.html#l00052">systick_utils.c:52</a></div></div>
63</div><!-- fragment --></div><!-- contents -->
64
65<hr class="footer"/><address class="footer"><small>
66&copy; Copyright 2016-2022 NXP. All Rights Reserved. SPDX-License-Identifier: BSD-3-Clause
67</small></address>
68</body>
69</html>
70