hw_io_pin.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "hw_io_pin.h"
  2. #include "board.h"
  3. typedef struct
  4. {
  5. unsigned long gpio; //蝞∟����沌ORT
  6. unsigned short pin; //蝞∟����沌IN
  7. unsigned short level; //蝞∟��㗇��萄像 �芾��?�𤥁�?
  8. unsigned long pupd; //銝𠹺��西挽蝵?
  9. } io_pin_t;
  10. static const io_pin_t io_input_map[IO_PIN_INPUT_NUMBER] = {
  11. { GPIOB, 9, 0, GPIO_PUPD_PULLUP }, //0
  12. { GPIOB, 7, 0, GPIO_PUPD_PULLUP }, //1
  13. { GPIOB, 6, 0, GPIO_PUPD_PULLUP }, //2
  14. { GPIOB, 5, 0, GPIO_PUPD_PULLUP }, //3
  15. { GPIOB, 4, 0, GPIO_PUPD_PULLUP }, //4
  16. { GPIOB, 3, 0, GPIO_PUPD_PULLUP }, //5
  17. { GPIOA, 15, 0, GPIO_PUPD_PULLUP }, //6
  18. { GPIOA, 12, 0, GPIO_PUPD_PULLUP } //7
  19. };
  20. static const io_pin_t io_output_map[IO_PIN_OUTPUT_NUMBER] = {
  21. { GPIOA, 4, 0, GPIO_PUPD_NONE }, //0
  22. { GPIOA, 5, 0, GPIO_PUPD_NONE }, //1
  23. { GPIOA, 6, 0, GPIO_PUPD_NONE }, //2
  24. { GPIOA, 7, 0, GPIO_PUPD_NONE }, //3
  25. { GPIOB, 0, 0, GPIO_PUPD_NONE }, //4
  26. { GPIOB, 1, 0, GPIO_PUPD_NONE } //5
  27. };
  28. #define OUTPUT_ENABLE_PORT GPIOB
  29. #define OUTPUT_ENABLE_PIN 12
  30. #define OUTPUT_ENABLE() GPIO_BOP(OUTPUT_ENABLE_PORT) = 1 << (OUTPUT_ENABLE_PIN)
  31. #define OUTPUT_DISABLE() GPIO_BOP(OUTPUT_ENABLE_PORT) = 1 << (OUTPUT_ENABLE_PIN + 16)
  32. #define OUTPUT_LED_PORT GPIOA
  33. #define OUTPUT_LED_PIN 15
  34. #define OUTPUT_LED_ENABLE() GPIO_BOP(OUTPUT_LED_PORT) = 1 << (OUTPUT_LED_PIN + 16)
  35. #define OUTPUT_LED_DISABLE() GPIO_BOP(OUTPUT_LED_PORT) = 1 << (OUTPUT_LED_PIN)
  36. void hw_io_pin_init(void)
  37. {
  38. int i = 0;
  39. unsigned long port;
  40. unsigned short pin;
  41. /* unsigned long pupd;
  42. int in_size = sizeof(io_input_map) / sizeof(io_pin_t);
  43. int out_size = sizeof(io_output_map) / sizeof(io_pin_t);
  44. if (IO_PIN_INPUT_NUMBER != in_size || IO_PIN_OUTPUT_NUMBER != out_size)
  45. {
  46. while (1)
  47. {}
  48. }
  49. for (i = 0; i < in_size; i++)
  50. {
  51. port = (uint32_t)io_input_map[i].gpio;
  52. pin = 1 << io_input_map[i].pin;
  53. pupd = io_input_map[i].pupd;
  54. if (port != 0) {
  55. //雿輯��園�
  56. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  57. //�滨蔭IO銝箄���
  58. gpio_mode_set(port, GPIO_MODE_INPUT, pupd, pin);
  59. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  60. }
  61. //io_input_map[i].level *= pin;
  62. }
  63. for (i = 0; i < out_size; i++)
  64. {
  65. port = (uint32_t)io_output_map[i].gpio;
  66. pin = 1 << io_output_map[i].pin;
  67. pupd = io_output_map[i].pupd;
  68. if (io_output_map[i].level)
  69. {
  70. GPIO_BOP(port) = pin<<16;
  71. } else
  72. {
  73. GPIO_BOP(port) = pin;
  74. }
  75. if (port != 0) {
  76. //雿輯��園�
  77. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  78. //�滨蔭IO銝箄���
  79. gpio_mode_set(port, GPIO_MODE_OUTPUT, pupd, pin);
  80. gpio_output_options_set(port, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, pin);
  81. }
  82. }
  83. */
  84. //雿輯�颲枏枂
  85. {
  86. port = OUTPUT_ENABLE_PORT;
  87. pin = 1 << OUTPUT_ENABLE_PIN;
  88. if (port != 0) {
  89. //雿輯��園�
  90. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  91. //�滨蔭IO銝箄���
  92. gpio_mode_set(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, pin);
  93. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  94. //OUTPUT_ENABLE();
  95. }
  96. }
  97. //雿輯�颲枏枂
  98. {
  99. port = OUTPUT_LED_PORT;
  100. pin = 1 << OUTPUT_LED_PIN;
  101. if (port != 0) {
  102. //雿輯��園�
  103. rcu_periph_clock_enable(rcu_periph_clock_bit(port));
  104. //�滨蔭IO銝箄��?
  105. gpio_mode_set(port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, pin);
  106. gpio_output_options_set(port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, pin);
  107. //OUTPUT_ENABLE();
  108. }
  109. }
  110. OUTPUT_DISABLE();
  111. OUTPUT_LED_DISABLE();
  112. }
  113. /**
  114. * 颲枏枂�萄像
  115. *
  116. * @author lxz
  117. *
  118. * @param index
  119. * @param sta
  120. */
  121. void hw_io_pin_output(unsigned short index, int sta)
  122. {
  123. if (index < IO_PIN_OUTPUT_NUMBER)
  124. {
  125. if (sta)
  126. {
  127. if (io_output_map[index].level)
  128. {
  129. GPIO_BOP(io_output_map[index].gpio) = 1 << (io_output_map[index].pin);
  130. } else
  131. {
  132. GPIO_BOP(io_output_map[index].gpio) = 1 << (io_output_map[index].pin+16);
  133. }
  134. } else
  135. {
  136. if (io_output_map[index].level)
  137. {
  138. GPIO_BOP(io_output_map[index].gpio) = 1 << (io_output_map[index].pin+16);
  139. } else
  140. {
  141. GPIO_BOP(io_output_map[index].gpio) = 1 << (io_output_map[index].pin);
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * 颲枏��萄像
  148. *
  149. * @author lxz
  150. *
  151. * @param index
  152. *
  153. * @return int
  154. */
  155. int hw_io_pin_input(unsigned short index)
  156. {
  157. if (index < IO_PIN_INPUT_NUMBER)
  158. {
  159. return (gpio_input_bit_get(io_input_map[index].gpio, (1<<io_input_map[index].pin))==
  160. (io_input_map[index].level));
  161. }
  162. return 0;
  163. }
  164. /**
  165. * 颲枏枂雿輯�
  166. *
  167. * @author lxz (2019/5/29/�其�)
  168. */
  169. void hw_io_output_enable(void)
  170. {
  171. OUTPUT_ENABLE();
  172. OUTPUT_LED_ENABLE();
  173. }
  174. /**
  175. * 颲枏枂蝳�迫
  176. *
  177. * @author lxz (2019/5/29/�其�)
  178. */
  179. void hw_io_output_disable(void)
  180. {
  181. OUTPUT_DISABLE();
  182. OUTPUT_LED_DISABLE();
  183. }
  184. /**
  185. * �瑕�IO颲枏�撘閗���𠶖���摮睃膥
  186. *
  187. * @author LXZ (062820)
  188. *
  189. * @param index
  190. *
  191. * @return void*
  192. */
  193. int hw_io_get_input_pin_infomation(unsigned short index, hw_io_infomation_t * info){
  194. if (index < IO_PIN_INPUT_NUMBER) {
  195. info->io_register = (void *)&(GPIO_ISTAT(io_input_map[index].gpio));
  196. info->pin_mask = 1 << io_input_map[index].pin;
  197. info->valid_value = io_input_map[index].level;
  198. return 1;
  199. }
  200. return 0;
  201. }