HAL_TIMER_EX.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /***********************************************************************
  2. * Filename : hal_lpuart.c
  3. * Description : lpuart driver source file
  4. * Author(s) : xwl
  5. * version : V1.0
  6. * Modify date : 2021-04-02
  7. ***********************************************************************/
  8. #include "ACM32Fxx_HAL.h"
  9. /*********************************************************************************
  10. * Function : HAL_TIMER_ConfigBreakDeadTime
  11. * Description : configure deadtime parameter
  12. * Input : timer handler, break and deadtime configuration
  13. * Output : none
  14. * Author : xwl
  15. **********************************************************************************/
  16. void HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim, TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  17. {
  18. /* Keep this variable initialized to 0 as it is used to configure BDTR register */
  19. uint32_t break_deadtime_reg = 0U;
  20. /* Set the BDTR bits */
  21. MODIFY_REG(break_deadtime_reg, TIM_BDTR_DTG, sBreakDeadTimeConfig->DeadTime);
  22. MODIFY_REG(break_deadtime_reg, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
  23. MODIFY_REG(break_deadtime_reg, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
  24. MODIFY_REG(break_deadtime_reg, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
  25. MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
  26. MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
  27. MODIFY_REG(break_deadtime_reg, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
  28. MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  29. /* Set TIMx_BDTR */
  30. htim->Instance->BDTR = break_deadtime_reg;
  31. }
  32. /*********************************************************************************
  33. * Function : HAL_TIMEx_ConfigBreakInput
  34. * Description : Configures the break input source.
  35. * Input :
  36. htim: timer handler
  37. BreakInput: TIM_BREAKINPUT_BRK
  38. sBreakInputConfig: Break input source configuration
  39. * Output :
  40. * Author : xwl
  41. **********************************************************************************/
  42. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput,
  43. TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
  44. {
  45. uint32_t tmporx;
  46. uint32_t bkin_enable_mask;
  47. uint32_t bkin_polarity_mask;
  48. uint32_t bkin_enable_bitpos;
  49. uint32_t bkin_polarity_bitpos;
  50. switch(sBreakInputConfig->Source)
  51. {
  52. case TIM_BREAKINPUTSOURCE_BKIN:
  53. {
  54. bkin_enable_mask = TIM1_AF1_BKINE;
  55. bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
  56. bkin_polarity_mask = TIM1_AF1_BKINP;
  57. bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
  58. break;
  59. }
  60. #ifdef HAL_COMP_MODULE_ENABLED
  61. case TIM_BREAKINPUTSOURCE_COMP1:
  62. {
  63. bkin_enable_mask = TIM1_AF1_BKCMP1E;
  64. bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
  65. bkin_polarity_mask = TIM1_AF1_BKCMP1P;
  66. bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
  67. break;
  68. }
  69. case TIM_BREAKINPUTSOURCE_COMP2:
  70. {
  71. bkin_enable_mask = TIM1_AF1_BKCMP2E;
  72. bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
  73. bkin_polarity_mask = TIM1_AF1_BKCMP2P;
  74. bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
  75. break;
  76. }
  77. #endif /* COMP1 && COMP2 */
  78. default:
  79. {
  80. bkin_enable_mask = 0U;
  81. bkin_enable_bitpos = 0U;
  82. bkin_polarity_mask = 0U;
  83. bkin_polarity_bitpos = 0U;
  84. break;
  85. }
  86. }
  87. switch(BreakInput)
  88. {
  89. case TIM_BREAKINPUT_BRK:
  90. {
  91. /* Get the TIMx_AF1 register value */
  92. tmporx = htim->Instance->AF1;
  93. /* Enable the break input */
  94. tmporx &= ~bkin_enable_mask;
  95. tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
  96. /* Set the break input polarity */
  97. tmporx &= ~bkin_polarity_mask;
  98. tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
  99. /* Set TIMx_AF1 */
  100. htim->Instance->AF1 = tmporx;
  101. break;
  102. }
  103. default:
  104. break;
  105. }
  106. return HAL_OK;
  107. }
  108. /*********************************************************************************
  109. * Function : HAL_TIMEx_ConfigCommutEvent
  110. * Description : Configure the TIM commutation event sequence. This function is mandatory to use the commutation event
  111. in order to update the configuration at each commutation detection on the TRGI input of the Timer.
  112. * Input :
  113. htim: timer handler
  114. InputTrigger: TIM_TRIGGER_SOURCE_ITR0, TIM_TRIGGER_SOURCE_ITR1...
  115. CommutationSource: TIM_COMMUTATION_TRGI, TIM_COMMUTATION_SOFTWARE
  116. * Output :
  117. * Author : xwl
  118. **********************************************************************************/
  119. void HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  120. {
  121. if ((InputTrigger == TIM_TRIGGER_SOURCE_ITR0) || (InputTrigger == TIM_TRIGGER_SOURCE_ITR1) ||
  122. (InputTrigger == TIM_TRIGGER_SOURCE_ITR2) || (InputTrigger == TIM_TRIGGER_SOURCE_ITR3))
  123. {
  124. /* Select the Input trigger */
  125. htim->Instance->SMCR &= (~(BIT4|BIT5|BIT6));
  126. htim->Instance->SMCR |= InputTrigger;
  127. }
  128. /* Select the Capture Compare preload feature */
  129. htim->Instance->CR2 |= TIM_CR2_CCPC;
  130. /* Select the Commutation event source */
  131. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  132. htim->Instance->CR2 |= CommutationSource;
  133. /* Disable Commutation Interrupt */
  134. HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);
  135. /* Disable Commutation DMA request */
  136. HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);
  137. }