yc_gpio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2006-2020, YICHIP Development Team
  3. * @file yc_gpio.c
  4. * @brief source file for setting gpio
  5. *
  6. * Change Logs:
  7. * Date Author Version Notes
  8. * 2021-01-20 yangzhengfeng V1.0.2 Update library function
  9. * 2021-07-29 xubo V1.0.3 Update library function
  10. */
  11. #include "yc_gpio.h"
  12. uint8_t const UnMapTb[256] = {
  13. 0u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x00 to 0x0F */
  14. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x10 to 0x1F */
  15. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x20 to 0x2F */
  16. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x30 to 0x3F */
  17. 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x40 to 0x4F */
  18. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x50 to 0x5F */
  19. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x60 to 0x6F */
  20. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x70 to 0x7F */
  21. 7u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x80 to 0x8F */
  22. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0x90 to 0x9F */
  23. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xA0 to 0xAF */
  24. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xB0 to 0xBF */
  25. 6u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xC0 to 0xCF */
  26. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xD0 to 0xDF */
  27. 5u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, /* 0xE0 to 0xEF */
  28. 4u, 0u, 1u, 0u, 2u, 0u, 1u, 0u, 3u, 0u, 1u, 0u, 2u, 0u, 1u, 0u /* 0xF0 to 0xFF */
  29. };
  30. uint8_t UnMap(uint16_t x)
  31. {
  32. uint8_t lx = x;
  33. uint8_t hx = x >> 8;
  34. if(lx)
  35. {
  36. return UnMapTb[lx];
  37. }
  38. else
  39. {
  40. return UnMapTb[hx] + 8;
  41. }
  42. }
  43. /**
  44. * @method GPIO_Config
  45. * @brief config gpio function(Only one can be configured at a time)
  46. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  47. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)(Only one can be configured at a time)
  48. * @param function:gpio function
  49. * @retval none
  50. */
  51. void GPIO_Config(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, GPIO_FUN_TYPEDEF function)
  52. {
  53. _ASSERT(IS_GPIO_PORT(GPIOx));
  54. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  55. _ASSERT(IS_GPIO_FUN(function));
  56. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = function;
  57. }
  58. /**
  59. * @method GPIO_Init
  60. * @brief gpio mode Init
  61. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  62. * @param GPIO_InitStruct:GPIO_InitStruct
  63. * @retval none
  64. */
  65. void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  66. {
  67. _ASSERT(IS_GPIO_PORT(GPIOx));
  68. _ASSERT(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  69. _ASSERT(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  70. for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
  71. {
  72. if(GPIO_InitStruct->GPIO_Pin & (BIT0<<i))
  73. {
  74. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = GPIO_InitStruct->GPIO_Mode << 6;
  75. }
  76. }
  77. }
  78. /**
  79. * @method GPIO_PullUpCmd
  80. * @brief gpio pull up
  81. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  82. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_7)
  83. * @param NewState: new state of the port pin Pull Up.(ENABLE or DISABLE)
  84. * @retval none
  85. */
  86. void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, FunctionalState NewState)
  87. {
  88. _ASSERT(IS_GPIO_PORT(GPIOx));
  89. _ASSERT(IS_GPIO_PIN(GPIO_Pin));
  90. _ASSERT(IS_FUNCTIONAL_STATE(NewState));
  91. uint8_t i = 0;
  92. if (ENABLE == NewState)
  93. {
  94. for(i = 0; i<GPIO_PIN_NUM; i++)
  95. {
  96. MGPIO->CTRL.bit[GPIO_GetNum(GPIOx,(GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IPU;
  97. }
  98. }
  99. else if (DISABLE == NewState)
  100. {
  101. for(i = 0; i<GPIO_PIN_NUM; i++)
  102. {
  103. MGPIO->CTRL.bit[GPIO_GetNum(GPIOx, (GPIO_Pin_TypeDef)(BIT0 << i))].MODE = GPIO_Mode_IN_FLOATING;
  104. }
  105. }
  106. }
  107. /**
  108. * @method GPIO_ReadInputData
  109. * @brief Reads the GPIO input data for 2byte.
  110. * @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
  111. * @retval GPIO input data.
  112. */
  113. uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx)
  114. {
  115. _ASSERT(IS_GPIO_PORT(GPIOx));
  116. return MGPIO->IN_LEVEL.reg[GPIOx];
  117. }
  118. /**
  119. * @method GPIO_ReadInputDataBit
  120. * @brief Reads the GPIO input data(status) for bit.
  121. * @param GPIOx_IN: where x can be (GPIOA_IN...GPIOF_IN) to select the GPIO group.
  122. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  123. * @retval The input bit
  124. */
  125. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
  126. {
  127. _ASSERT(IS_GPIO_PORT(GPIOx));
  128. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  129. if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
  130. {
  131. return (uint8_t)Bit_SET;
  132. }
  133. return (uint8_t)Bit_RESET;
  134. }
  135. /**
  136. * @method GPIO_ReadOutputData
  137. * @brief Reads the GPIO output data(status) for byte.
  138. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  139. * @retval GPIO output data(status).
  140. */
  141. uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx)
  142. {
  143. _ASSERT(IS_GPIO_PORT(GPIOx));
  144. return MGPIO->IN_LEVEL.reg[GPIOx];
  145. }
  146. /**
  147. * @method GPIO_ReadOutputDataBit
  148. * @brief Reads the GPIO output data(status) for bit.
  149. * @param GPIOx: where x can be (GPIOA...GPIOF) to select the GPIO group.
  150. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  151. * @retval The output status
  152. */
  153. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
  154. {
  155. _ASSERT(IS_GPIO_PORT(GPIOx));
  156. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  157. if (((MGPIO->IN_LEVEL.reg[GPIOx]) & GPIO_Pin) != (uint32_t)Bit_RESET)
  158. {
  159. return (uint8_t)Bit_SET;
  160. }
  161. return (uint8_t)Bit_RESET;
  162. }
  163. /**
  164. * @method GPIO_ResetBit
  165. * @brief Reset the GPIO bit data(status) for bit.
  166. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  167. * @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
  168. * @retval none
  169. */
  170. void GPIO_ResetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
  171. {
  172. _ASSERT(IS_GPIO_PORT(GPIOx));
  173. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  174. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_LOW;
  175. }
  176. /**
  177. * @method GPIO_ResetBits
  178. * @brief Reset the GPIO bit data(status) for bit.
  179. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  180. * @param GPIO_Pin: select the pin to reset.(GPIO_Pin_0...GPIO_Pin_15)
  181. * @retval none
  182. */
  183. void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  184. {
  185. _ASSERT(IS_GPIO_PORT(GPIOx));
  186. _ASSERT(IS_GPIO_PIN(GPIO_Pin));
  187. for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
  188. {
  189. if(GPIO_Pin & (BIT0<<i))
  190. {
  191. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_LOW;
  192. }
  193. }
  194. }
  195. /**
  196. * @method GPIO_SetBit
  197. * @brief Set the GPIO bit data(status) for bit.
  198. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  199. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  200. * @retval none
  201. */
  202. void GPIO_SetBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
  203. {
  204. _ASSERT(IS_GPIO_PORT(GPIOx));
  205. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  206. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, GPIO_Pin)] = OUTPUT_HIGH;
  207. }
  208. /**
  209. * @method GPIO_SetBits
  210. * @brief Set the GPIO bit data(status) for bit.
  211. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  212. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  213. * @retval none
  214. */
  215. void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  216. {
  217. _ASSERT(IS_GPIO_PORT(GPIOx));
  218. _ASSERT(IS_GPIO_PIN(GPIO_Pin));
  219. for(uint8_t i = 0; i < GPIO_PIN_NUM; i++)
  220. {
  221. if(GPIO_Pin & (BIT0<<i))
  222. {
  223. MGPIO->CTRL.reg[GPIO_GetNum(GPIOx, (BIT0<<i))] = OUTPUT_HIGH;
  224. }
  225. }
  226. }
  227. /**
  228. * @method GPIO_Write
  229. * @brief Write the GPIO group data(status) for bit.
  230. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  231. * @param value: select the value to read.(0 or 1)
  232. * @retval none
  233. */
  234. void GPIO_Write(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin)
  235. {
  236. _ASSERT(IS_GPIO_PORT(GPIOx));
  237. _ASSERT(IS_GPIO_PIN(GPIO_Pin));
  238. GPIO_SetBits(GPIOx, GPIO_Pin);
  239. GPIO_ResetBits(GPIOx, (GPIO_Pin_TypeDef)(~GPIO_Pin));
  240. }
  241. /**
  242. * @method GPIO_WriteBit
  243. * @brief Write the GPIO bit data(status) for bit.
  244. * @param GPIOx: where x can be (GPIOA...GPIOE) to select the GPIO group.
  245. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  246. * @param BitVal: select the value to read.(0 or 1)
  247. * @retval none
  248. */
  249. void GPIO_WriteBit(GPIO_TypeDef GPIOx, GPIO_Pin_TypeDef GPIO_Pin, BitAction BitVal)
  250. {
  251. _ASSERT(IS_GPIO_PORT(GPIOx));
  252. _ASSERT(IS_GPIO_PIN_SINGLE(GPIO_Pin));
  253. if (BitVal == Bit_SET)
  254. GPIO_SetBit(GPIOx, GPIO_Pin);
  255. else if (BitVal == Bit_RESET)
  256. GPIO_ResetBit(GPIOx, GPIO_Pin);
  257. }
  258. /**
  259. * @method GPIO_ODSet
  260. * @brief Set the GPIO OD MODE
  261. * @param GPIOx_Drv: where x can be (GPIOA_Drv...GPIOE_Drv) to select the GPIO_Drv group.
  262. * @param GPIO_Pin: select the pin to read.(GPIO_Pin_0...GPIO_Pin_15)
  263. * @param Drvtype: select the value to set DRV value.(0x00....0x11)
  264. * @retval none
  265. */
  266. void GPIO_ODSet(GPIO_OD_TypeDef GPIOx_OD, GPIO_ODTypeDef GPIO_OD_Set)
  267. {
  268. _ASSERT(IS_GPIO_OD(GPIOx_OD));
  269. _ASSERT(IS_GPIO_MODE_OUT(GPIO_OD_Set));
  270. (MGPIO->OD_CTRL.reg) |= (GPIO_OD_Set << GPIOx_OD);
  271. }
  272. /************************ (C) COPYRIGHT Yichip Microelectronics *****END OF FILE****/