ft32f0xx_div.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. ******************************************************************************
  3. * @file FT32f0xx_div.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the dividor peripheral
  7. * applicable only on FT32F072xB devices:
  8. * + Comparators configuration
  9. * + Window mode control
  10. * @version V1.0.0
  11. * @data 2021-12-01
  12. ******************************************************************************
  13. */
  14. /* Includes ------------------------------------------------------------------*/
  15. #include "ft32f0xx_div.h"
  16. /**
  17. * @brief Enable or disable the OPA peripheral.
  18. * @note If the selected comparator is locked, enable/disable can't be performed.
  19. * To unlock the configuration, perform a system reset.
  20. * @param OPA_Selection: the selected comparator.
  21. * This parameter can be one of the following values:
  22. * @arg NOPA_Selection_OPA: OPA1 selected
  23. * @arg POPA_Selection_OPA: OPA2 selected
  24. * @param NewState: new state of the OPA peripheral.
  25. * This parameter can be: ENABLE or DISABLE.
  26. * @note When enabled, the comparator compares the non inverting input with
  27. * the inverting input and the comparison result is available on comparator output.
  28. * @note When disabled, the comparator doesn't perform comparison and the
  29. * output level is low.
  30. * @retval None
  31. */
  32. DIV_Status DivS32ByS16(DIV_ResultTypeDef* pResult,int32_t divedent,int16_t dividor)
  33. {
  34. DIV_Status status = DIV_COMPLETE;
  35. DIV->DID = divedent;
  36. DIV->DIS = dividor;
  37. while(DIV_GetFlagStatus(DIV_FLAG_BUSY) == SET);
  38. if(DIV_GetFlagStatus(DIV_FLAG_DIV0ERR) == SET)
  39. {
  40. status = DIV_ERROR_DIV0ERR;
  41. }
  42. else if(DIV_GetFlagStatus(DIV_FLAG_DIVOV) == SET)
  43. {
  44. status = DIV_ERROR_DIV0V;
  45. }
  46. else
  47. {
  48. pResult -> DIV_quotient = DIV-> QUO;
  49. pResult -> DIV_remainder = DIV-> REM;
  50. }
  51. return status;
  52. }
  53. /** @defgroup DIV Interrupts and flags management functions
  54. * @brief Interrupts and flags management functions.
  55. *
  56. @verbatim
  57. ===============================================================================
  58. ##### Interrupts and flags management functions #####
  59. ===============================================================================
  60. [..] This section provides functions allowing to configure the DIV Interrupts
  61. and get the status and clear flags and Interrupts pending bits.
  62. *** Flags for DIV status ***
  63. ======================================================
  64. [..]
  65. (+)Flags :
  66. (##) DIV_FLAG_DIV0ERR : This flag is set after the ADC has been enabled (bit ADEN=1)
  67. and when the ADC reaches a state where it is ready to accept conversion requests
  68. (##) DIV_FLAG_DIVOV : This flag is set by software to enable the ADC.
  69. The DIV will be effectively ready to operate once the ADRDY flag has been set.
  70. (##) DIV_FLAG_BUSY : This flag is cleared once the ADC is effectively
  71. disabled.
  72. (+)Interrupts
  73. (##) DIV_IT_DIV0ERR : specifies the interrupt source for ADC ready event.
  74. (##) DIV_IT_DIVOV : specifies the interrupt source for ADC ready event.
  75. [..] The user should identify which mode will be used in his application to
  76. manage the ADC controller events: Polling mode or Interrupt mode.
  77. [..] In the Polling Mode it is advised to use the following functions:
  78. (+) DIV_GetFlagStatus() : to check if flags events occur.
  79. (+) DIV_ClearFlag() : to clear the flags events.
  80. [..] In the Interrupt Mode it is advised to use the following functions:
  81. (+) DIV_ITConfig() : to enable or disable the interrupt source.
  82. (+) DIV_GetITStatus() : to check if Interrupt occurs.
  83. (+) DIV_ClearITPendingBit() : to clear the Interrupt pending Bit
  84. (corresponding Flag).
  85. @endverbatim
  86. * @{
  87. */
  88. /**
  89. * @brief Enables or disables the specified DIV interrupts.
  90. * @param DIV_IT: specifies the DIV interrupt sources to be enabled or disabled.
  91. * This parameter can be one of the following values:
  92. * @arg DIV_IT_DIV0ERR: Divide By Zero Exception
  93. * @arg DIV_IT_DIVOV: Overflow interrupt
  94. * @param NewState: new state of the specified DIV interrupts.
  95. * This parameter can be: ENABLE or DISABLE.
  96. * @retval None
  97. */
  98. void DIV_ITConfig(uint32_t DIV_IT, FunctionalState NewState)
  99. {
  100. /* Check the parameters */
  101. assert_param(IS_FUNCTIONAL_STATE(NewState));
  102. assert_param(IS_DIV_CONFIG_IT(DIV_IT));
  103. if (NewState != DISABLE)
  104. {
  105. /* Enable the selected DIV interrupts */
  106. DIV->SC |= DIV_IT;
  107. }
  108. else
  109. {
  110. /* Disable the selected DIV interrupts */
  111. DIV->SC &= (~(uint32_t)DIV_IT);
  112. }
  113. }
  114. /**
  115. * @brief Checks whether the specified DIV flag is set or not.
  116. * @param DIV_FLAG: specifies the flag to check.
  117. * This parameter can be one of the following values:
  118. * @arg DIV_FLAG_DIV0ERR: Divide By Zero Exception flag
  119. * @arg DIV_FLAG_DIVOV: Overflow flag
  120. * @arg DIV_FLAG_BUSY: Busy flag
  121. * @retval The new state of DIV_FLAG (SET or RESET).
  122. */
  123. FlagStatus DIV_GetFlagStatus(uint32_t DIV_FLAG)
  124. {
  125. FlagStatus bitstatus = RESET;
  126. /* Check the parameters */
  127. assert_param(IS_DIV_GET_FLAG(DIV_FLAG));
  128. /* Check the status of the specified DIV flag */
  129. if ((DIV->SC & DIV_FLAG) != (uint32_t)RESET)
  130. {
  131. /* DIV_FLAG is set */
  132. bitstatus = SET;
  133. }
  134. else
  135. {
  136. /* DIV_FLAG is reset */
  137. bitstatus = RESET;
  138. }
  139. /* Return the DIV_FLAG status */
  140. return bitstatus;
  141. }
  142. /**
  143. * @brief Clears the DIV's pending flags.
  144. * @param DIV_FLAG: specifies the flag to clear.
  145. * This parameter can be any combination of the following values:
  146. * @arg DIV_FLAG_DIV0ERRC: Divide By Zero Exception flag
  147. * @arg DIV_FLAG_DIVOVC: Overflow flag
  148. * @retval None
  149. */
  150. void DIV_ClearFlag(uint32_t DIV_FLAG)
  151. {
  152. /* Check the parameters */
  153. assert_param(IS_DIV_CLEAR_FLAG(DIV_FLAG));
  154. /* Clear the selected DIV flags */
  155. DIV->SC |= (uint32_t)(DIV_FLAG<<8);
  156. }
  157. /**
  158. * @brief Checks whether the specified DIV interrupt has occurred or not.
  159. * @param DIV_IT: specifies the DIV interrupt source to check.
  160. * This parameter can be one of the following values:
  161. * @arg DIV_IT_DIV0ERR: Divide By Zero Exception
  162. * @arg DIV_IT_DIVOV: Overflow interrupt
  163. * @retval The new state of DIV_IT (SET or RESET).
  164. */
  165. ITStatus DIV_GetITStatus(uint32_t DIV_IT)
  166. {
  167. ITStatus bitstatus = RESET;
  168. uint32_t enablestatus = 0;
  169. /* Check the parameters */
  170. assert_param(IS_DIV_GET_IT(DIV_IT));
  171. /* Get the DIV_IT enable bit status */
  172. enablestatus = (uint32_t)((DIV->SC>>1) & DIV_IT);
  173. /* Check the status of the specified DIV interrupt */
  174. if (((uint32_t)(DIV->SC & DIV_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  175. {
  176. /* DIV_IT is set */
  177. bitstatus = SET;
  178. }
  179. else
  180. {
  181. /* DIV_IT is reset */
  182. bitstatus = RESET;
  183. }
  184. /* Return the DIV_IT status */
  185. return bitstatus;
  186. }
  187. /**
  188. * @brief Clears the DIV's interrupt pending bits.
  189. * @param DIV: where x can be 1 to select the DIV1 peripheral.
  190. * @param DIV_IT: specifies the DIV interrupt pending bit to clear.
  191. * This parameter can be one of the following values:
  192. * @arg DIV_IT_DIV0ERR: Divide By Zero Exception
  193. * @arg DIV_IT_DIVOV: Overflow interrupt
  194. * @retval None
  195. */
  196. void DIV_ClearITPendingBit(uint32_t DIV_IT)
  197. {
  198. /* Check the parameters */
  199. assert_param(IS_DIV_CLEAR_IT(DIV_IT));
  200. /* Clear the selected DIV interrupt pending bits */
  201. DIV->SC |= (uint32_t)(DIV_IT<<8);
  202. }
  203. /**
  204. * @}
  205. */
  206. /************************ (C) COPYRIGHT FMD *****END OF FILE****/