hal_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_gpio.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE GPIO 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_GPIO_C_
  19. // Files includes
  20. #include "reg_exti.h"
  21. #include "hal_rcc.h"
  22. #include "hal_gpio.h"
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// @addtogroup MM32_Hardware_Abstract_Layer
  25. /// @{
  26. ////////////////////////////////////////////////////////////////////////////////
  27. /// @addtogroup GPIO_HAL
  28. /// @{
  29. ////////////////////////////////////////////////////////////////////////////////
  30. /// @addtogroup GPIO_Exported_Functions
  31. /// @{
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// @brief Deinitializes the gpio peripheral registers to their default reset
  34. /// values.
  35. /// @param gpio: select the GPIO peripheral.
  36. /// @retval None.
  37. ////////////////////////////////////////////////////////////////////////////////
  38. void GPIO_DeInit(GPIO_TypeDef* gpio)
  39. {
  40. switch (*(vu32*)&gpio) {
  41. case (u32)GPIOA:
  42. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, DISABLE);
  43. break;
  44. case (u32)GPIOB:
  45. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, DISABLE);
  46. break;
  47. case (u32)GPIOC:
  48. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOC, DISABLE);
  49. break;
  50. case (u32)GPIOD:
  51. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD, DISABLE);
  52. break;
  53. case (u32)GPIOE:
  54. RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOE, DISABLE);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. /// @brief Deinitializes the Alternate Functions (remap, event control
  62. /// and EXTI configuration) registers to their default reset values.
  63. /// @param None.
  64. /// @retval None.
  65. ////////////////////////////////////////////////////////////////////////////////
  66. void GPIO_AFIODeInit()
  67. {
  68. GPIOA->AFRL = 0xFFFFFFFF;
  69. GPIOA->AFRH = 0xF00FFFFF; // PA14:SWCLK, PA13:PSWDIO
  70. GPIOB->AFRL = 0xFFFFFFFF;
  71. GPIOB->AFRH = 0xFFFFFFFF;
  72. GPIOC->AFRL = 0xFFFFFFFF;
  73. GPIOC->AFRH = 0xFFFFFFFF;
  74. GPIOD->AFRL = 0xFFFFFFFF;
  75. GPIOD->AFRH = 0xFFFFFFFF;
  76. GPIOE->AFRL = 0xFFFFFFFF;
  77. GPIOE->AFRH = 0xFFFFFFFF;
  78. }
  79. ////////////////////////////////////////////////////////////////////////////////
  80. /// @brief Initializes the gpio peripheral according to the specified
  81. /// parameters in the init_struct.
  82. /// @param gpio: select the GPIO peripheral.
  83. /// @param init_struct: pointer to a GPIO_InitTypeDef structure that
  84. /// contains the configuration information for the specified GPIO
  85. /// peripheral.
  86. /// @retval None.
  87. ////////////////////////////////////////////////////////////////////////////////
  88. void GPIO_Init(GPIO_TypeDef* gpio, GPIO_InitTypeDef* init_struct)
  89. {
  90. u8 idx;
  91. u8 i;
  92. u32 tmp;
  93. __IO u32* reg ;
  94. // 1x
  95. u32 dat = init_struct->GPIO_Mode & 0x0F;
  96. if (init_struct->GPIO_Mode & 0x10)
  97. dat |= init_struct->GPIO_Speed;
  98. // 0x
  99. reg = &gpio->CRL;
  100. for (i = 0; i < 8; i++) {
  101. idx = i * 4;
  102. if ((init_struct->GPIO_Pin) & (1 << i)) {
  103. *reg = (*reg & ~(0xF << idx)) | (dat << idx);
  104. }
  105. }
  106. reg = &gpio->CRH;
  107. tmp = init_struct->GPIO_Pin >> 8;
  108. for (i = 0; i < 8; i++) {
  109. idx = i * 4;
  110. if (tmp & (1 << i)) {
  111. *reg = (*reg & ~(0xF << idx)) | (dat << idx);
  112. }
  113. }
  114. // 2x,4x
  115. if (init_struct->GPIO_Mode == GPIO_Mode_IPD)
  116. gpio->BRR |= init_struct->GPIO_Pin;
  117. else if (init_struct->GPIO_Mode == GPIO_Mode_IPU)
  118. gpio->BSRR |= init_struct->GPIO_Pin;
  119. }
  120. ////////////////////////////////////////////////////////////////////////////////
  121. /// @brief Fills each init_struct member with its default value.
  122. /// @param init_struct : pointer to a GPIO_InitTypeDef structure
  123. /// which will be initialized.
  124. /// @retval : None
  125. ////////////////////////////////////////////////////////////////////////////////
  126. void GPIO_StructInit(GPIO_InitTypeDef* init_struct)
  127. {
  128. // Reset GPIO init structure parameters values
  129. init_struct->GPIO_Pin = GPIO_Pin_All;
  130. init_struct->GPIO_Speed = GPIO_Speed_2MHz;
  131. init_struct->GPIO_Mode = GPIO_Mode_FLOATING;
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////
  134. /// @brief Reads the input data of specified GPIO port pin.
  135. /// @param gpio: select the GPIO peripheral.
  136. /// @param pin: specifies the port pin to be read.
  137. /// This parameter can be GPIO_Pin_x where x can be (0..15).
  138. /// @retval The input port pin value.
  139. ////////////////////////////////////////////////////////////////////////////////
  140. bool GPIO_ReadInputDataBit(GPIO_TypeDef* gpio, u16 pin)
  141. {
  142. return ((gpio->IDR & pin)) ? Bit_SET : Bit_RESET;
  143. }
  144. ////////////////////////////////////////////////////////////////////////////////
  145. /// @brief Reads all GPIO port pins input data.
  146. /// @param gpio: select the GPIO peripheral.
  147. /// @retval GPIO port input data value.
  148. ////////////////////////////////////////////////////////////////////////////////
  149. u16 GPIO_ReadInputData(GPIO_TypeDef* gpio)
  150. {
  151. return gpio->IDR;
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. /// @brief Reads the output data of specified GPIO port pin.
  155. /// @param gpio: select the GPIO peripheral.
  156. /// @param pin: specifies the port bit to be read.
  157. /// This parameter can be GPIO_Pin_x where x can be (0..15).
  158. /// @retval The output port pin value.
  159. ////////////////////////////////////////////////////////////////////////////////
  160. bool GPIO_ReadOutputDataBit(GPIO_TypeDef* gpio, u16 pin)
  161. {
  162. return (gpio->ODR & pin) ? Bit_SET : Bit_RESET;
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. /// @brief Reads all GPIO port pins output data.
  166. /// @param gpio: select the GPIO peripheral.
  167. /// @retval GPIO port output data value.
  168. ////////////////////////////////////////////////////////////////////////////////
  169. u16 GPIO_ReadOutputData(GPIO_TypeDef* gpio)
  170. {
  171. return gpio->ODR;
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. /// @brief Sets the selected GPIO port pin.
  175. /// @param gpio: where x can be (A..D) to select the GPIO peripheral.
  176. /// @param pin: specifies the port pins to be written.
  177. /// This parameter can be any combination of GPIO_Pin_x where x can be
  178. /// (0..15).
  179. /// @retval None.
  180. ////////////////////////////////////////////////////////////////////////////////
  181. void GPIO_SetBits(GPIO_TypeDef* gpio, u16 pin)
  182. {
  183. gpio->BSRR = pin;
  184. }
  185. ////////////////////////////////////////////////////////////////////////////////
  186. /// @brief Clears the selected GPIO port bit.
  187. /// @param gpio: where x can be (A..D) to select the GPIO peripheral.
  188. /// @param pin: specifies the port pins to be written.
  189. /// This parameter can be any combination of GPIO_Pin_x where x can be
  190. /// (0..15).
  191. /// @retval None.
  192. ////////////////////////////////////////////////////////////////////////////////
  193. void GPIO_ResetBits(GPIO_TypeDef* gpio, u16 pin)
  194. {
  195. gpio->BRR = pin;
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////
  198. /// @brief Sets or clears the selected GPIO port pin.
  199. /// @param gpio: select the GPIO peripheral.
  200. /// @param pin: specifies the port bit to be written.
  201. /// This parameter can be one of GPIO_Pin_x where x can be (0..15).
  202. /// @param value: specifies the value to be written to the selected bit.
  203. /// This parameter can be one of the BitAction enum values:
  204. /// @arg Bit_RESET: to clear the port pin
  205. /// @arg Bit_SET: to set the port pin
  206. /// @retval None.
  207. ////////////////////////////////////////////////////////////////////////////////
  208. void GPIO_WriteBit(GPIO_TypeDef* gpio, u16 pin, BitAction value)
  209. {
  210. (value) ? (gpio->BSRR = pin) : (gpio->BRR = pin);
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////
  213. /// @brief Writes data to all GPIO port pins.
  214. /// @param gpio: where x can be (A..D) to select the GPIO peripheral.
  215. /// @param value: specifies the value to be written to the port output data
  216. /// register.
  217. /// @retval None.
  218. ////////////////////////////////////////////////////////////////////////////////
  219. void GPIO_Write(GPIO_TypeDef* gpio, u16 value)
  220. {
  221. gpio->ODR = value;
  222. }
  223. ////////////////////////////////////////////////////////////////////////////////
  224. /// @brief Locks GPIO Pins configuration.
  225. /// @param gpio: to select the GPIO peripheral.
  226. /// @param pin: specifies the port bit to be written.
  227. /// This parameter can be any combination of GPIO_Pin_x where x can be
  228. /// (0..15).
  229. /// @param state: new lock state of the port pin.
  230. /// This parameter can be: ENABLE or DISABLE.
  231. /// @retval None.
  232. ////////////////////////////////////////////////////////////////////////////////
  233. void GPIO_PinLock(GPIO_TypeDef* gpio, u16 pin, FunctionalState state)
  234. {
  235. (state) ? (gpio->LCKR |= pin) : (gpio->LCKR &= ~pin);
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////
  238. /// @brief Locks GPIO Pins configuration registers until next system reset.
  239. /// @param gpio: to select the GPIO peripheral.
  240. /// @param pin: specifies the port bit to be written.
  241. /// This parameter can be any combination of GPIO_Pin_x where x can be
  242. /// (0..15).
  243. /// @retval None.
  244. ////////////////////////////////////////////////////////////////////////////////
  245. void GPIO_PinLockConfig(GPIO_TypeDef* gpio, u16 pin)
  246. {
  247. gpio->LCKR = GPIO_LCKR_LCKK | pin;
  248. gpio->LCKR = pin;
  249. gpio->LCKR = GPIO_LCKR_LCKK | pin;
  250. gpio->LCKR;
  251. gpio->LCKR;
  252. }
  253. ////////////////////////////////////////////////////////////////////////////////
  254. /// @brief Enables or disables the port pin remapping.
  255. /// @param remap: selects the pin to remap.
  256. /// @param mask: the corresponding remapping mask of the remapping pin.
  257. /// @param state: new state of the port pin remapping.
  258. /// This parameter can be: ENABLE or DISABLE.
  259. /// @retval None.
  260. ////////////////////////////////////////////////////////////////////////////////
  261. ////////////////////////////////////////////////////////////////////////////////
  262. /// @brief Writes data to the specified GPIO data port.
  263. /// @param gpio: select the GPIO peripheral.
  264. /// @param pin: specifies the pin for the Alternate function.
  265. /// This parameter can be GPIO_PinSourcex where x can be (0..15) for
  266. /// GPIOA, GPIOB, GPIOD and (0..12) for GPIOC .
  267. /// @param alternate_function: selects the pin to used as Alternate function.
  268. /// This parameter can be the GPIO_AF_x where x can be (0..7).
  269. /// @note The pin should be used for Digital IP.
  270. /// @retval None.
  271. ////////////////////////////////////////////////////////////////////////////////
  272. void GPIO_PinAFConfig(GPIO_TypeDef* gpio, u8 pin, u8 alternate_function)
  273. {
  274. u8 shift = (pin & 0x07) * 4;
  275. u32* ptr = (pin < 8) ? (u32*)&gpio->AFRL : (u32*)&gpio->AFRH;
  276. *ptr = (*ptr & ~(0x0F << shift)) | (alternate_function << shift);
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////
  279. /// @brief Set the remap function and AF function of the GPIO pin.
  280. /// @param gpio:select the GPIO peripheral.
  281. /// @param pin: specifies the pin for the Alternate function.
  282. /// This parameter can be GPIO_Pin_x where x can be (0..15) for
  283. /// GPIOA, GPIOB, GPIOD and (0..12) for GPIOC .
  284. /// @param remap: selects the pin to remap.
  285. /// @param alternate_function: selects the pin to used as Alternate function.
  286. /// This parameter can be the GPIO_AF_x where x can be (0..7).
  287. /// @note The pin should be used for Digital IP.
  288. /// @retval None.
  289. ////////////////////////////////////////////////////////////////////////////////
  290. void exGPIO_PinAFConfig(GPIO_TypeDef* gpio, u16 pin, s32 remap, s8 alternate_function)
  291. {
  292. u8 i;
  293. u8 shift;
  294. u32* ptr;
  295. if (alternate_function >= 0) {
  296. for (i = 0; i < 32; i++) {
  297. if (pin & 0x01) {
  298. pin = i;
  299. break;
  300. }
  301. pin >>= 1;
  302. }
  303. shift = (pin & 0x07) * 4;
  304. ptr = (pin < 8) ? (u32*)&gpio->AFRL : (u32*)&gpio->AFRH;
  305. *ptr = (*ptr & ~(0x0F << shift)) | (alternate_function << shift);
  306. }
  307. }
  308. /// @}
  309. /// @}
  310. /// @}