hal_exti.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_exti.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE EXTI FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_EXTI_C_
  19. // Files includes
  20. #include "hal_exti.h"
  21. ////////////////////////////////////////////////////////////////////////////////
  22. /// @addtogroup MM32_Hardware_Abstract_Layer
  23. /// @{
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// @addtogroup EXTI_HAL
  26. /// @{
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// @addtogroup EXTI_Exported_Functions
  29. /// @{
  30. ////////////////////////////////////////////////////////////////////////////////
  31. /// @brief Deinitializes the EXTI peripheral registers to their default reset
  32. /// values.
  33. /// @param None.
  34. /// @retval None.
  35. ////////////////////////////////////////////////////////////////////////////////
  36. ////////////////////////////////////////////////////////////////////////////////
  37. /// @brief Deinitializes the EXTI registers to their default reset values.
  38. /// @param None.
  39. /// @retval None.
  40. /// @note MEM_MODE bits are not affected by APB reset.
  41. /// @note MEM_MODE bits took the value from the user option bytes.
  42. /// @note CFGR2 register is not affected by APB reset.
  43. /// @note CLABBB configuration bits are locked when set.
  44. /// @note To unlock the configuration, perform a system reset.
  45. ////////////////////////////////////////////////////////////////////////////////
  46. void EXTI_DeInit(void)
  47. {
  48. u16 i;
  49. // Clear all
  50. exEXTI_LineDisable(~0x00000000);
  51. // rc_w1
  52. EXTI->PR = EXTI->PR;
  53. // Set EXTI_CFGR1 register to reset value without affecting MEM_MODE bits
  54. EXTI->CFGR &= EXTI_CFGR_MEMMODE;
  55. // Set EXTICRx registers to reset value
  56. for (i = 0; i < 4; i++) {
  57. EXTI->CR[i] = 0;
  58. }
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. /// @brief Selects the GPIO pin used as EXTI Line.
  62. /// @param port_source_gpio: selects the GPIO port to be used as source for EXTI lines .
  63. /// @param pin_source: specifies the EXTI line to be configured.
  64. /// @note This parameter can be pin_source where x can be:
  65. /// For MCU: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOD.
  66. /// @retval None.
  67. ////////////////////////////////////////////////////////////////////////////////
  68. void EXTI_LineConfig(u8 port_source_gpio, u8 pin_source)
  69. {
  70. EXTI->CR[pin_source >> 0x02] &= ~(0x0F << (0x04 * (pin_source & 0x03)));
  71. EXTI->CR[pin_source >> 0x02] |= ((port_source_gpio) << (0x04 * (pin_source & 0x03)));
  72. }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. /// @brief Initializes the EXTI peripheral according to the specified
  75. /// parameters in the init_struct.
  76. /// @param init_struct: pointer to a EXTI_InitTypeDef structure that
  77. /// contains the configuration information for the EXTI peripheral.
  78. /// @retval None.
  79. ////////////////////////////////////////////////////////////////////////////////
  80. void EXTI_Init(EXTI_InitTypeDef* init_struct)
  81. {
  82. if (init_struct->EXTI_LineCmd != DISABLE) {
  83. EXTI->IMR &= ~init_struct->EXTI_Line;
  84. EXTI->EMR &= ~init_struct->EXTI_Line;
  85. if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
  86. EXTI->IMR |= init_struct->EXTI_Line;
  87. }
  88. else {
  89. EXTI->EMR |= init_struct->EXTI_Line;
  90. }
  91. EXTI->RTSR &= ~init_struct->EXTI_Line;
  92. EXTI->FTSR &= ~init_struct->EXTI_Line;
  93. if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising_Falling) {
  94. EXTI->RTSR |= init_struct->EXTI_Line;
  95. EXTI->FTSR |= init_struct->EXTI_Line; // Rising and Faling afio
  96. }
  97. else if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising) {
  98. EXTI->RTSR |= init_struct->EXTI_Line;
  99. }
  100. else {
  101. EXTI->FTSR |= init_struct->EXTI_Line;
  102. }
  103. }
  104. else {
  105. if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
  106. EXTI->IMR &= ~init_struct->EXTI_Line;
  107. }
  108. else {
  109. EXTI->EMR &= ~init_struct->EXTI_Line;
  110. }
  111. }
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. /// @brief Fills each init_struct member with its reset value.
  115. /// @param init_struct: pointer to a EXTI_InitTypeDef structure which will
  116. /// be initialized.
  117. /// @retval None.
  118. ////////////////////////////////////////////////////////////////////////////////
  119. void EXTI_StructInit(EXTI_InitTypeDef* init_struct)
  120. {
  121. init_struct->EXTI_Line = EXTI_LineNone;
  122. init_struct->EXTI_Mode = EXTI_Mode_Interrupt;
  123. init_struct->EXTI_Trigger = EXTI_Trigger_Falling;
  124. init_struct->EXTI_LineCmd = DISABLE;
  125. }
  126. ////////////////////////////////////////////////////////////////////////////////
  127. /// @brief Generates a Software interrupt on selected EXTI line.
  128. /// @param line: specifies the EXTI line on which the software interrupt
  129. /// will be generated.
  130. /// @retval None.
  131. ////////////////////////////////////////////////////////////////////////////////
  132. void EXTI_GenerateSWInterrupt(u32 line)
  133. {
  134. EXTI->SWIER |= line;
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. /// @brief Checks whether the specified EXTI line flag is set or not.
  138. /// @param line: specifies the EXTI line flag to check.
  139. /// @retval The new state of line (SET or RESET).
  140. ////////////////////////////////////////////////////////////////////////////////
  141. FlagStatus EXTI_GetFlagStatus(u32 line)
  142. {
  143. return (EXTI->PR & line) ? SET : RESET;
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////
  146. /// @brief Clears the EXTI's line pending flags.
  147. /// @param line: specifies the EXTI lines flags to clear.
  148. /// @retval None.
  149. ////////////////////////////////////////////////////////////////////////////////
  150. void EXTI_ClearFlag(u32 line)
  151. {
  152. EXTI->PR = line;
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////
  155. /// @brief Checks whether the specified EXTI line is asserted or not.
  156. /// @param line: specifies the EXTI line to check.
  157. /// @retval The new state of line (SET or RESET).
  158. ////////////////////////////////////////////////////////////////////////////////
  159. ITStatus EXTI_GetITStatus(u32 line)
  160. {
  161. return ((EXTI->PR & line) && (EXTI->IMR & line)) ? SET : RESET;
  162. }
  163. ////////////////////////////////////////////////////////////////////////////////
  164. /// @brief Clears the EXTI's line pending bits.
  165. /// @param line: specifies the EXTI lines to clear.
  166. /// @retval None.
  167. ////////////////////////////////////////////////////////////////////////////////
  168. void EXTI_ClearITPendingBit(u32 line)
  169. {
  170. EXTI->PR = line;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////
  173. /// @brief EXTI Line Disable
  174. /// @param line: specifies the EXTI lines to clear.
  175. /// @retval None.
  176. ////////////////////////////////////////////////////////////////////////////////
  177. void exEXTI_LineDisable(u32 line)
  178. {
  179. EXTI->IMR &= ~line;
  180. EXTI->EMR &= ~line;
  181. EXTI->RTSR &= ~line;
  182. EXTI->FTSR &= ~line;
  183. }
  184. ////////////////////////////////////////////////////////////////////////////////
  185. /// @brief Clears the EXTI's line all pending bits.
  186. /// @param None.
  187. /// @retval None.
  188. ////////////////////////////////////////////////////////////////////////////////
  189. u32 exEXTI_GetAllFlagStatus(void)
  190. {
  191. return EXTI->PR;
  192. }
  193. /// @}
  194. /// @}
  195. /// @}