summaryrefslogtreecommitdiff
path: root/third_party/TouchSense/STMTouch_Driver/src/tsl_time_stm8l.c
blob: 06904ebcab76d638c572863b79da1778b652af43 (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
/**
  ******************************************************************************
  * @file    tsl_time_stm8l.c
  * @author  MCD Application Team
  * @version V1.4.4
  * @date    31-March-2014
  * @brief   This file contains all functions to manage the timing with STM8L products.
  ******************************************************************************
  * @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_stm8l.h"
#include "tsl_time.h"

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


/**
  * @brief  Initialization of the timing module.
  * @param  None
  * @retval Status Return TSL_STATUS_ERROR if the clock divider is wrong
  */
TSL_Status_enum_T TSL_tim_Init(void)
{
  // Enable TIM4 clock
#if defined(STM8L10X)
  CLK->PCKENR |= CLK_PCKENR_TIM4;
#else // STM8L15X
  CLK->PCKENR1 |= CLK_PCKENR1_TIM4;
#endif

  if (CLK->CKDIVR != 0x00) // Warning: The CPU frequency must be equal to 16 MHz
  {
    return TSL_STATUS_ERROR;
  }

  TIM4->SR1 = 0; // Clear overflow flag

#if (TSLPRM_TICK_FREQ == 2000)
  TIM4->PSCR = 6; // 16 MHz / 64 = 4 us clock
  TIM4->ARR = 124; // 125 * 4 us = 0.5 ms
#endif

#if (TSLPRM_TICK_FREQ == 1000)
  TIM4->PSCR = 6; // 16 MHz / 64 = 4 us clock
  TIM4->ARR = 249; // 250 * 4 us = 1 ms
#endif

#if (TSLPRM_TICK_FREQ == 500)
  TIM4->PSCR = 8; // 16 MHz / 256 = 16 us clock
  TIM4->ARR = 124; // 125 *  16 us = 2 ms
#endif

#if (TSLPRM_TICK_FREQ == 250)
  TIM4->PSCR = 8; // 16 MHz / 256 = 16 us clock
  TIM4->ARR = 249; // 250 *  16 us = 4 ms
#endif

#if (TSLPRM_TICK_FREQ == 125)
  TIM4->PSCR = 10; // 16 MHz / 1024 = 64 us clock
  TIM4->ARR = 124; // 125 *  64 us = 8 ms
#endif

  TIM4->IER = 0x01; // Enable interrupt
  TIM4->CR1 = 0x01; // Start timer

  return TSL_STATUS_OK;
}


/**
  * @brief  Interrupt handler for TIM4 dedicated to ECS
  * @param  None
  * @retval None
  */
#if defined(_COSMIC_)
// 'svlreg option' is added to force the saving of the virtual long register
@svlreg INTERRUPT_HANDLER(TSL_Timer_ISR, 25)
#else
INTERRUPT_HANDLER(TSL_Timer_ISR, 25)
#endif
{
  TIM4->SR1 &= (uint8_t)(~TIM4_SR1_UIF);
  TSL_tim_ProcessIT();
}

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