drv_gpio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "drv_gpio.h"
  2. #include "pin.h"
  3. #include "stm32f4xx_gpio.h"
  4. #define PIN_NUM(port, no) (((((port)&0xFu) << 4) | ((no)&0xFu)))
  5. #define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
  6. #define PIN_NO(pin) ((uint8_t)((pin)&0xFu))
  7. #define PIN_STPORT(pin) ((GPIO_TypeDef *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
  8. #define PIN_STPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
  9. #define PIN_STPORT_MAX 16
  10. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  11. rt_size_t rt_strlen(const char *s)
  12. {
  13. const char *sc;
  14. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  15. ;
  16. return sc - s;
  17. }
  18. rt_base_t stm32_pin_get(const char *name)
  19. {
  20. rt_base_t pin = 0;
  21. int hw_port_num, hw_pin_num = 0;
  22. int i, name_len;
  23. name_len = rt_strlen(name);
  24. if ((name_len < 4) || (name_len >= 6))
  25. {
  26. return -RT_EINVAL;
  27. }
  28. if ((name[0] != 'P') || (name[2] != '.'))
  29. {
  30. return -RT_EINVAL;
  31. }
  32. if ((name[1] >= 'A') && (name[1] <= 'Z'))
  33. {
  34. hw_port_num = (int)(name[1] - 'A');
  35. }
  36. else
  37. {
  38. return -RT_EINVAL;
  39. }
  40. for (i = 3; i < name_len; i++)
  41. {
  42. hw_pin_num *= 10;
  43. hw_pin_num += name[i] - '0';
  44. }
  45. pin = PIN_NUM(hw_port_num, hw_pin_num);
  46. return pin;
  47. }
  48. void rt_pin_write(rt_base_t pin, rt_base_t value)
  49. {
  50. GPIO_TypeDef *gpio_port;
  51. uint16_t gpio_pin;
  52. if (PIN_PORT(pin) < PIN_STPORT_MAX)
  53. {
  54. gpio_port = PIN_STPORT(pin);
  55. gpio_pin = PIN_STPIN(pin);
  56. GPIO_WriteBit(gpio_port, gpio_pin, value);
  57. }
  58. }
  59. int rt_pin_read(rt_base_t pin)
  60. {
  61. GPIO_TypeDef *gpio_port;
  62. uint16_t gpio_pin;
  63. int value = PIN_LOW;
  64. if (PIN_PORT(pin) < PIN_STPORT_MAX)
  65. {
  66. gpio_port = PIN_STPORT(pin);
  67. gpio_pin = PIN_STPIN(pin);
  68. value = GPIO_ReadInputDataBit(gpio_port, gpio_pin);
  69. }
  70. return value;
  71. }
  72. void rt_pin_mode(rt_base_t pin, rt_base_t mode)
  73. {
  74. GPIO_InitTypeDef GPIO_InitStruct;
  75. if (PIN_PORT(pin) >= PIN_STPORT_MAX)
  76. {
  77. return;
  78. }
  79. /* Configure GPIO_InitStructure */
  80. GPIO_InitStruct.GPIO_Pin = PIN_STPIN(pin);
  81. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  82. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  83. GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;
  84. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  85. if (mode == PIN_MODE_OUTPUT)
  86. {
  87. /* output setting */
  88. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  89. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  90. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  91. }
  92. else if (mode == PIN_MODE_INPUT)
  93. {
  94. /* input setting: not GPIO_OType. */
  95. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  96. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  97. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  98. }
  99. else if (mode == PIN_MODE_INPUT_PULLUP)
  100. {
  101. /* input setting: GPIO_OType up. */
  102. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  103. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  104. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  105. }
  106. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  107. {
  108. /* input setting: GPIO_OType down. */
  109. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  110. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  111. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;
  112. }
  113. else if (mode == PIN_MODE_OUTPUT_OD)
  114. {
  115. /* output setting: od. */
  116. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  117. GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
  118. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  119. }
  120. GPIO_Init(PIN_STPORT(pin), &GPIO_InitStruct);
  121. }
  122. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  123. {
  124. int i;
  125. for (i = 0; i < 32; i++)
  126. {
  127. if ((0x01 << i) == bit)
  128. {
  129. return i;
  130. }
  131. }
  132. return -1;
  133. }
  134. int rt_hw_pin_init(void)
  135. {
  136. #if defined(RCC_GPIOA_CLK_ENABLE)
  137. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  138. #endif
  139. #if defined(RCC_GPIOB_CLK_ENABLE)
  140. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  141. #endif
  142. #if defined(RCC_GPIOC_CLK_ENABLE)
  143. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  144. #endif
  145. #if defined(RCC_GPIOD_CLK_ENABLE)
  146. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  147. #endif
  148. #if defined(RCC_GPIOE_CLK_ENABLE)
  149. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
  150. #endif
  151. #if defined(RCC_GPIOF_CLK_ENABLE)
  152. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
  153. #endif
  154. #if defined(RCC_GPIOG_CLK_ENABLE)
  155. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  156. #endif
  157. #if defined(RCC_GPIOH_CLK_ENABLE)
  158. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
  159. #endif
  160. #if defined(RCC_GPIOI_CLK_ENABLE)
  161. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
  162. #endif
  163. return 0;
  164. }