/ports/stm8/stm8s-periphs/stm8s_tim1.c

https://github.com/sasikanthbabu/atomthreads · C · 184 lines · 80 code · 20 blank · 84 comment · 4 complexity · 91efc467bf76d28d5732d5b53f6fafea MD5 · raw file

  1. /**
  2. ******************************************************************************
  3. * @file stm8s_tim1.c
  4. * @brief This file contains all the functions for the TIM1 peripheral.
  5. * @author STMicroelectronics - MCD Application Team
  6. * @version V1.1.1
  7. * @date 06/05/2009
  8. ******************************************************************************
  9. *
  10. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  11. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  12. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  13. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  14. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  15. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  16. *
  17. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  18. * @image html logo.bmp
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm8s_tim1.h"
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. /* Private function prototypes -----------------------------------------------*/
  28. /**
  29. * @addtogroup TIM1_Public_Functions
  30. * @{
  31. */
  32. /**
  33. * @brief Deinitializes the TIM1 peripheral registers to their default reset values.
  34. * @par Parameters:
  35. * None
  36. * @retval None
  37. */
  38. void TIM1_DeInit(void)
  39. {
  40. TIM1->CR1 = TIM1_CR1_RESET_VALUE;
  41. TIM1->CR2 = TIM1_CR2_RESET_VALUE;
  42. TIM1->SMCR = TIM1_SMCR_RESET_VALUE;
  43. TIM1->ETR = TIM1_ETR_RESET_VALUE;
  44. TIM1->IER = TIM1_IER_RESET_VALUE;
  45. TIM1->SR2 = TIM1_SR2_RESET_VALUE;
  46. /* Disable channels */
  47. TIM1->CCER1 = TIM1_CCER1_RESET_VALUE;
  48. TIM1->CCER2 = TIM1_CCER2_RESET_VALUE;
  49. /* Configure channels as inputs: it is necessary if lock level is equal to 2 or 3 */
  50. TIM1->CCMR1 = 0x01;
  51. TIM1->CCMR2 = 0x01;
  52. TIM1->CCMR3 = 0x01;
  53. TIM1->CCMR4 = 0x01;
  54. /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
  55. TIM1->CCER1 = TIM1_CCER1_RESET_VALUE;
  56. TIM1->CCER2 = TIM1_CCER2_RESET_VALUE;
  57. TIM1->CCMR1 = TIM1_CCMR1_RESET_VALUE;
  58. TIM1->CCMR2 = TIM1_CCMR2_RESET_VALUE;
  59. TIM1->CCMR3 = TIM1_CCMR3_RESET_VALUE;
  60. TIM1->CCMR4 = TIM1_CCMR4_RESET_VALUE;
  61. TIM1->CNTRH = TIM1_CNTRH_RESET_VALUE;
  62. TIM1->CNTRL = TIM1_CNTRL_RESET_VALUE;
  63. TIM1->PSCRH = TIM1_PSCRH_RESET_VALUE;
  64. TIM1->PSCRL = TIM1_PSCRL_RESET_VALUE;
  65. TIM1->ARRH = TIM1_ARRH_RESET_VALUE;
  66. TIM1->ARRL = TIM1_ARRL_RESET_VALUE;
  67. TIM1->CCR1H = TIM1_CCR1H_RESET_VALUE;
  68. TIM1->CCR1L = TIM1_CCR1L_RESET_VALUE;
  69. TIM1->CCR2H = TIM1_CCR2H_RESET_VALUE;
  70. TIM1->CCR2L = TIM1_CCR2L_RESET_VALUE;
  71. TIM1->CCR3H = TIM1_CCR3H_RESET_VALUE;
  72. TIM1->CCR3L = TIM1_CCR3L_RESET_VALUE;
  73. TIM1->CCR4H = TIM1_CCR4H_RESET_VALUE;
  74. TIM1->CCR4L = TIM1_CCR4L_RESET_VALUE;
  75. TIM1->OISR = TIM1_OISR_RESET_VALUE;
  76. TIM1->EGR = 0x01; /* TIM1_EGR_UG */
  77. TIM1->DTR = TIM1_DTR_RESET_VALUE;
  78. TIM1->BKR = TIM1_BKR_RESET_VALUE;
  79. TIM1->RCR = TIM1_RCR_RESET_VALUE;
  80. TIM1->SR1 = TIM1_SR1_RESET_VALUE;
  81. }
  82. /**
  83. * @brief Initializes the TIM1 Time Base Unit according to the specified parameters.
  84. * @param[in] TIM1_Prescaler specifies the Prescaler value.
  85. * @param[in] TIM1_CounterMode specifies the counter mode from @ref TIM1_CounterMode_TypeDef .
  86. * @param[in] TIM1_Period specifies the Period value.
  87. * @param[in] TIM1_RepetitionCounter specifies the Repetition counter value
  88. * @retval None
  89. */
  90. void TIM1_TimeBaseInit(u16 TIM1_Prescaler,
  91. TIM1_CounterMode_TypeDef TIM1_CounterMode,
  92. u16 TIM1_Period,
  93. u8 TIM1_RepetitionCounter)
  94. {
  95. /* Check parameters */
  96. assert_param(IS_TIM1_COUNTER_MODE_OK(TIM1_CounterMode));
  97. /* Set the Autoreload value */
  98. TIM1->ARRH = (u8)(TIM1_Period >> 8);
  99. TIM1->ARRL = (u8)(TIM1_Period);
  100. /* Set the Prescaler value */
  101. TIM1->PSCRH = (u8)(TIM1_Prescaler >> 8);
  102. TIM1->PSCRL = (u8)(TIM1_Prescaler);
  103. /* Select the Counter Mode */
  104. TIM1->CR1 = (u8)(((TIM1->CR1) & (u8)(~(TIM1_CR1_CMS | TIM1_CR1_DIR))) | (u8)(TIM1_CounterMode));
  105. /* Set the Repetition Counter value */
  106. TIM1->RCR = TIM1_RepetitionCounter;
  107. }
  108. /**
  109. * @brief Enables or disables the TIM1 peripheral.
  110. * @param[in] NewState new state of the TIM1 peripheral.
  111. * This parameter can be ENABLE or DISABLE.
  112. * @retval None
  113. */
  114. void TIM1_Cmd(FunctionalState NewState)
  115. {
  116. /* Check the parameters */
  117. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  118. /* set or Reset the CEN Bit */
  119. if (NewState != DISABLE)
  120. {
  121. TIM1->CR1 |= TIM1_CR1_CEN;
  122. }
  123. else
  124. {
  125. TIM1->CR1 &= (u8)(~TIM1_CR1_CEN);
  126. }
  127. }
  128. /**
  129. * @brief Enables or disables the specified TIM1 interrupts.
  130. * @param[in] NewState new state of the TIM1 peripheral.
  131. * This parameter can be: ENABLE or DISABLE.
  132. * @param[in] TIM1_IT specifies the TIM1 interrupts sources to be enabled or disabled.
  133. * This parameter can be any combination of the following values:
  134. * - TIM1_IT_UPDATE: TIM1 update Interrupt source
  135. * - TIM1_IT_CC1: TIM1 Capture Compare 1 Interrupt source
  136. * - TIM1_IT_CC2: TIM1 Capture Compare 2 Interrupt source
  137. * - TIM1_IT_CC3: TIM1 Capture Compare 3 Interrupt source
  138. * - TIM1_IT_CC4: TIM1 Capture Compare 4 Interrupt source
  139. * - TIM1_IT_CCUpdate: TIM1 Capture Compare Update Interrupt source
  140. * - TIM1_IT_TRIGGER: TIM1 Trigger Interrupt source
  141. * - TIM1_IT_BREAK: TIM1 Break Interrupt source
  142. * @param[in] NewState new state of the TIM1 peripheral.
  143. * @retval None
  144. */
  145. void TIM1_ITConfig(TIM1_IT_TypeDef TIM1_IT, FunctionalState NewState)
  146. {
  147. /* Check the parameters */
  148. assert_param(IS_TIM1_IT_OK(TIM1_IT));
  149. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  150. if (NewState != DISABLE)
  151. {
  152. /* Enable the Interrupt sources */
  153. TIM1->IER |= (u8)TIM1_IT;
  154. }
  155. else
  156. {
  157. /* Disable the Interrupt sources */
  158. TIM1->IER &= (u8)(~(u8)TIM1_IT);
  159. }
  160. }
  161. /**
  162. * @}
  163. */
  164. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/