yc_gpio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. File Name : yc_gpio.c
  3. Author : Yichip
  4. Version : V1.0
  5. Date : 2019/12/03
  6. Description : gpio encapsulation.
  7. */
  8. #include "yc_gpio.h"
  9. void GPIO_Config(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, GPIO_FunTypeDef function)
  10. {
  11. _ASSERT(ISGPIOGROUP(GPIOx));
  12. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  13. int i;
  14. for (i = 0; i < GPIO_PIN_NUM; i++)
  15. {
  16. if (GPIO_Pin & 1 << i)
  17. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = function;
  18. }
  19. }
  20. void GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
  21. {
  22. _ASSERT(ISGPIOGROUP(GPIOx));
  23. _ASSERT(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
  24. _ASSERT(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  25. int i;
  26. switch (GPIO_InitStruct->GPIO_Mode)
  27. {
  28. case GPIO_Mode_IN_FLOATING:
  29. for (i = 0; i < GPIO_PIN_NUM; i++)
  30. {
  31. if (GPIO_InitStruct->GPIO_Pin & 1 << i)
  32. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x00;
  33. }
  34. break;
  35. case GPIO_Mode_IPU:
  36. for (i = 0; i < GPIO_PIN_NUM; i++)
  37. {
  38. if (GPIO_InitStruct->GPIO_Pin & 1 << i)
  39. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x40;
  40. }
  41. break;
  42. case GPIO_Mode_IPD:
  43. for (i = 0; i < GPIO_PIN_NUM; i++)
  44. {
  45. if (GPIO_InitStruct->GPIO_Pin & 1 << i)
  46. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x80;
  47. }
  48. break;
  49. case GPIO_Mode_AIN:
  50. for (i = 0; i < GPIO_PIN_NUM; i++)
  51. {
  52. if (GPIO_InitStruct->GPIO_Pin & 1 << i)
  53. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0xC0;
  54. }
  55. break;
  56. case GPIO_Mode_Out_PP:
  57. for (i = 0; i < GPIO_PIN_NUM; i++)
  58. {
  59. if (GPIO_InitStruct->GPIO_Pin & 1 << i)
  60. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = 0x3E;
  61. }
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. void GPIO_PullUpCmd(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, FunctionalState NewState)
  68. {
  69. _ASSERT(ISGPIOGROUP(GPIOx));
  70. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  71. int i;
  72. for (i = 0; i < GPIO_PIN_NUM; i++)
  73. {
  74. if (GPIO_Pin & 1 << i)
  75. {
  76. if (NewState == ENABLE)
  77. {
  78. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) |= 1 << 6;
  79. }
  80. else if (NewState == DISABLE)
  81. {
  82. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) &= ~(1 << 6);
  83. }
  84. }
  85. }
  86. }
  87. uint16_t GPIO_ReadInputData(GPIO_TypeDef GPIOx)
  88. {
  89. _ASSERT(ISGPIOGROUP(GPIOx));
  90. return GPIO_IN(GPIOx);
  91. }
  92. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  93. {
  94. _ASSERT(ISGPIOGROUP(GPIOx));
  95. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  96. if (GPIO_IN(GPIOx) & GPIO_Pin)
  97. {
  98. return (uint8_t)0x01;
  99. }
  100. else
  101. {
  102. return (uint8_t)0x00;
  103. }
  104. }
  105. uint16_t GPIO_ReadOutputData(GPIO_TypeDef GPIOx)
  106. {
  107. _ASSERT(ISGPIOGROUP(GPIOx));
  108. return GPIO_IN(GPIOx);
  109. }
  110. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  111. {
  112. _ASSERT(ISGPIOGROUP(GPIOx));
  113. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  114. if (GPIO_IN(GPIOx) & GPIO_Pin)
  115. {
  116. return (uint8_t)0x01;
  117. }
  118. else
  119. {
  120. return (uint8_t)0x00;
  121. }
  122. }
  123. void GPIO_ResetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  124. {
  125. _ASSERT(ISGPIOGROUP(GPIOx));
  126. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  127. int i;
  128. uint8_t Temp;
  129. for (i = 0; i < GPIO_PIN_NUM; i++)
  130. {
  131. if (GPIO_Pin & 1 << i)
  132. {
  133. Temp = GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i);
  134. Temp |= 0x3F; //00111111
  135. Temp &= 0xFE; //11111110
  136. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) = Temp;
  137. }
  138. }
  139. }
  140. void GPIO_SetBits(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin)
  141. {
  142. _ASSERT(ISGPIOGROUP(GPIOx));
  143. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  144. int i;
  145. for (i = 0; i < GPIO_PIN_NUM; i++)
  146. {
  147. if (GPIO_Pin & 1 << i)
  148. GPIO_CONFIG(GPIOx * GPIO_PIN_NUM + i) |= 0x3F; //00111111
  149. }
  150. }
  151. void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
  152. {
  153. GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
  154. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
  155. }
  156. void GPIO_Write(GPIO_TypeDef GPIOx, uint16_t value)
  157. {
  158. _ASSERT(ISGPIOGROUP(GPIOx));
  159. int i;
  160. for (i = 0; i < GPIO_PIN_NUM; i++)
  161. {
  162. if (BIT_GET(value, i))
  163. GPIO_SetBits(GPIOx, 1 << i);
  164. else
  165. GPIO_ResetBits(GPIOx, 1 << i);
  166. }
  167. }
  168. void GPIO_WriteBit(GPIO_TypeDef GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
  169. {
  170. _ASSERT(ISGPIOGROUP(GPIOx));
  171. _ASSERT(IS_GET_GPIO_PIN(GPIO_Pin));
  172. if (BitVal == Bit_SET)
  173. GPIO_SetBits(GPIOx, GPIO_Pin);
  174. else if (BitVal == Bit_RESET)
  175. GPIO_ResetBits(GPIOx, GPIO_Pin);
  176. }