summaryrefslogtreecommitdiff
path: root/third_party/TouchSense/STMTouch_Driver/src/tsl_time.c
blob: d27457577c2c75fd99bf5fc68c4d6ca97deb6b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
  ******************************************************************************
  * @file    tsl_time.c
  * @author  MCD Application Team
  * @version V1.4.4
  * @date    31-March-2014
  * @brief   This file contains all functions to manage the timings in general.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tsl_time.h"
#include "tsl_globals.h"

/* Private typedefs ----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions prototype -----------------------------------------------*/

/**
  * @brief  Management of the timing module interrupt service routine.
  * @param  None
  * @retval None
  */
void TSL_tim_ProcessIT(void)
{
  static TSL_tTick_ms_T count_1s = 0;

  // Count 1 global tick every xxx ms (defined by TSLPRM_TICK_FREQ parameter)
  TSL_Globals.Tick_ms++;

  // Check if 1 second has elapsed
  count_1s++;
  if (count_1s > (TSLPRM_TICK_FREQ - 1))
  {
    TSL_Globals.Tick_sec++; // 1 global tick every second
    if (TSL_Globals.Tick_sec > 63)  // Due to DTO counter on 6 bits...
    {
      TSL_Globals.Tick_sec = 0;
    }
    count_1s = 0;
  }

// Callback function
#if TSLPRM_USE_TIMER_CALLBACK > 0
  TSL_CallBack_TimerTick();
#endif

}


/**
  * @brief  Check if a delay (in ms) has elapsed.
  * This function must be called regularly due to counter Roll-over only managed one time.
  * @param[in] delay_ms  Delay in ms
  * @param[in] last_tick Variable holding the last tick value
  * @retval Status
  */
TSL_Status_enum_T TSL_tim_CheckDelay_ms(TSL_tTick_ms_T delay_ms, __IO TSL_tTick_ms_T *last_tick)
{
  TSL_tTick_ms_T tick;
  TSL_tTick_ms_T diff;

  disableInterrupts();

  tick = TSL_Globals.Tick_ms;

  if (delay_ms == 0)
  {
    enableInterrupts();
    return TSL_STATUS_ERROR;
  }

  // Counter Roll-over management
  if (tick >= *last_tick)
  {
    diff = tick - *last_tick;
  }
  else
  {
    diff = (0xFFFF - *last_tick) + tick + 1;
  }

#if (TSLPRM_TICK_FREQ == 125)
  if (diff >= (TSL_tTick_ms_T)(delay_ms >> 3)) // Divide by 8 for 8ms tick
#endif
#if (TSLPRM_TICK_FREQ == 250)
  if (diff >= (TSL_tTick_ms_T)(delay_ms >> 2)) // Divide by 4 for 4ms tick
#endif
#if (TSLPRM_TICK_FREQ == 500)
  if (diff >= (TSL_tTick_ms_T)(delay_ms >> 1)) // Divide by 2 for 2ms tick
#endif
#if (TSLPRM_TICK_FREQ == 1000)
  if (diff >= (TSL_tTick_ms_T)delay_ms) // Direct value for 1ms tick
#endif
#if (TSLPRM_TICK_FREQ == 2000)
  if (diff >= (TSL_tTick_ms_T)(delay_ms << 1)) // Multiply by 2 for 0.5ms tick
#endif
  {
    // Save current time
    *last_tick = tick;
    enableInterrupts();
    return TSL_STATUS_OK;
  }

  enableInterrupts();
  return TSL_STATUS_BUSY;

}


/**
  * @brief  Check if a delay (in s) has elapsed.
  * @param[in] delay_sec  Delay in seconds
  * @param[in] last_tick Variable holding the last tick value
  * @retval Status
  */
TSL_Status_enum_T TSL_tim_CheckDelay_sec(TSL_tTick_sec_T delay_sec, __IO TSL_tTick_sec_T *last_tick)
{
  TSL_tTick_sec_T tick;
  TSL_tTick_sec_T diff;

  disableInterrupts();

  tick = TSL_Globals.Tick_sec;

  if (delay_sec == 0)
  {
    enableInterrupts();
    return TSL_STATUS_ERROR;
  }

  // Counter Roll-over management
  if (tick >= *last_tick)
  {
    diff = (TSL_tTick_sec_T)(tick - *last_tick);
  }
  else
  {
    diff = (TSL_tTick_sec_T)((63 - *last_tick) + tick + 1); // DTO counter is on 6 bits
  }

  if (diff >= delay_sec)
  {
    // Save current time
    *last_tick = tick;
    enableInterrupts();
    return TSL_STATUS_OK;
  }

  enableInterrupts();
  return TSL_STATUS_BUSY;

}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/