timing_delay.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. ******************************************************************************
  3. * @file timing_delay.c
  4. * @author MCD Application Team
  5. * @version V2.1.3
  6. * @date 28-June-2013
  7. * @brief This file contains a set of functions needed to generate 1ms time
  8. * base delay using TIM2 update interrupt.
  9. * TimingDelay_Init() function, should be called in the main.c file to
  10. * ensure TIM2 initialization and configuration.
  11. * The timing accuracy is based on the use of the external low speed
  12. * clock source(LSE).
  13. * Delay() function should be called in main.c file to specify the
  14. * duration of the desired delay in ms.
  15. * Counter decrementation is performed in TIM2 Update interrupt Handler
  16. * by the mean of TimingDelay_Decrement() function.
  17. * This function should be called in the stm8l15x_it.c file in the
  18. * TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler handler.
  19. *
  20. * @note TimingDelay_Init() function should be tailored in case user
  21. * wants to use a different clock source.
  22. *
  23. * For more details on timing_delay driver use, you can refer to
  24. * CLK_SYSCLKSwitch example in the STM8L15x_StdPeriph_Lib package.
  25. ******************************************************************************
  26. *
  27. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  28. * You may not use this file except in compliance with the License.
  29. * You may obtain a copy of the License at:
  30. *
  31. * http://www.st.com/software_license_agreement_liberty_v2
  32. *
  33. * Unless required by applicable law or agreed to in writing, software
  34. * distributed under the License is distributed on an "AS IS" BASIS,
  35. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36. * See the License for the specific language governing permissions and
  37. * limitations under the License.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "timing_delay.h"
  43. /** @addtogroup Utilities
  44. * @{
  45. */
  46. /** @addtogroup Misc
  47. * @{
  48. */
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. #define TIM2_PERIOD (uint8_t) 7
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. static __IO uint32_t TimingDelay;
  55. /* Private function prototypes -----------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /**
  58. * @addtogroup TIMING_DELAY_Functions
  59. * @{
  60. */
  61. /**
  62. * @brief timing delay init:to generate 1 ms time base using TIM2 update interrupt
  63. * @note The external low speed clock (LSE) is used to ensure timing accuracy.
  64. * This function should be updated in case of use of other clock source.
  65. * @param None
  66. * @retval None
  67. */
  68. void TimingDelay_Init(void)
  69. {
  70. /* Enable TIM2 clock */
  71. CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE);
  72. /* Remap TIM2 ETR to LSE: TIM2 external trigger becomes controlled by LSE clock */
  73. SYSCFG_REMAPPinConfig(REMAP_Pin_TIM2TRIGLSE, ENABLE);
  74. /* Enable LSE clock */
  75. CLK_LSEConfig(CLK_LSE_ON);
  76. /* Wait for LSERDY flag to be reset */
  77. while (CLK_GetFlagStatus(CLK_FLAG_LSERDY) == RESET);
  78. /* TIM2 configuration:
  79. - TIM2 ETR is mapped to LSE
  80. - TIM2 counter is clocked by LSE div 4
  81. so the TIM2 counter clock used is LSE / 4 = 32.768 / 4 = 8.192 KHz
  82. TIM2 Channel1 output frequency = TIM2CLK / (TIM2 Prescaler * (TIM2_PERIOD + 1))
  83. = 8192 / (1 * 8) = 1024 Hz */
  84. /* Time Base configuration */
  85. TIM2_TimeBaseInit(TIM2_Prescaler_1, TIM2_CounterMode_Up, TIM2_PERIOD);
  86. TIM2_ETRClockMode2Config(TIM2_ExtTRGPSC_DIV4, TIM2_ExtTRGPolarity_NonInverted, 0);
  87. TIM2_UpdateRequestConfig(TIM2_UpdateSource_Global);
  88. /* Clear TIM2 update flag */
  89. TIM2_ClearFlag(TIM2_FLAG_Update);
  90. /* Enable update interrupt */
  91. TIM2_ITConfig(TIM2_IT_Update, ENABLE);
  92. TIM2_Cmd(ENABLE);
  93. }
  94. /**
  95. * @brief Inserts a delay time.
  96. * @param nTime: specifies the delay time length, in milliseconds.
  97. * @retval None
  98. */
  99. void Delay(__IO uint32_t nTime)
  100. {
  101. TimingDelay = nTime;
  102. while (TimingDelay != 0);
  103. }
  104. /**
  105. * @brief Decrements the TimingDelay variable.
  106. * @note This function should be called in the
  107. * TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler in the stm8l15x_it.c file.
  108. *
  109. * // INTERRUPT_HANDLER(TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler, 19)
  110. * // {
  111. * // TimingDelay_Decrement();
  112. * // TIM2_ClearITPendingBit(TIM2_IT_Update);
  113. *
  114. * // }
  115. * @param None
  116. * @retval None
  117. */
  118. void TimingDelay_Decrement(void)
  119. {
  120. if (TimingDelay != 0x00)
  121. {
  122. TimingDelay--;
  123. }
  124. }
  125. /**
  126. * @}
  127. */
  128. /**
  129. * @}
  130. */
  131. /**
  132. * @}
  133. */
  134. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/