hal_gpio_interface.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "driver.h"
  2. #include <linux/types.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "com_gpio.h"
  6. #include "hal_interface_api.h"
  7. #include "linux/fcntl.h"
  8. #include <stdint.h>
  9. #include "stm32f429xx.h"
  10. #include "stm32_hal_legacy.h"
  11. #define DEV_NAME "/dev/gpio"
  12. /* Demon
  13. // Set PD_2 output
  14. GPIO_InitTypeDef GPIO_InitStruct;
  15. GPIO_InitStruct.Pin = GPIO_PIN_12;
  16. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  17. GPIO_InitStruct.Pull = GPIO_NOPULL;
  18. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  19. stm32_gpio_init(GPIOD, &GPIO_InitStruct);
  20. stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  21. // Set PI_8 input
  22. GPIO_InitStruct.Pin = GPIO_PIN_8;
  23. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  24. GPIO_InitStruct.Pull = GPIO_NOPULL;
  25. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  26. stm32_gpio_init(GPIOI, &GPIO_InitStruct);
  27. //Set PD_2 low
  28. stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  29. //Get PI_8 value
  30. printf("PI_8: %d\n", stm32_gpio_read(GPIOI, GPIO_PIN_8));
  31. */
  32. void stm32_gpio_init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  33. {
  34. int fd;
  35. int ret;
  36. gpio_t gpio_arg;
  37. fd = open(DEV_NAME, O_RDWR);
  38. if(fd == -1)
  39. {
  40. printf("Open %s failed!\n", DEV_NAME);
  41. }
  42. gpio_arg.GPIOx = GPIOx;
  43. gpio_arg.GPIO_pin = GPIO_Init->Pin;
  44. memcpy(&gpio_arg.GPIO_Init, GPIO_Init, sizeof(GPIO_InitTypeDef));
  45. ret = ioctl(fd, GPIO_INIT, &gpio_arg);
  46. if(ret == -1)
  47. {
  48. printf("Init gpio failed!\n");
  49. }
  50. close(fd);
  51. }
  52. void stm32_gpio_write(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  53. {
  54. if(PinState != GPIO_PIN_RESET)
  55. {
  56. GPIOx->BSRR = GPIO_Pin;
  57. }
  58. else
  59. {
  60. GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
  61. }
  62. // int fd;
  63. // int ret;
  64. // gpio_t gpio_arg;
  65. // fd = open(DEV_NAME, O_RDWR);
  66. // if(fd == -1)
  67. // {
  68. // printf("Open %s failed!\n", DEV_NAME);
  69. // }
  70. // gpio_arg.GPIOx = GPIOx;
  71. // gpio_arg.GPIO_pin = GPIO_Pin;
  72. // gpio_arg.Data = PinState;
  73. // ret = ioctl(fd, GPIO_WRITE_PIN, &gpio_arg);
  74. // if(ret == -1)
  75. // {
  76. // printf("Write gpio failed!\n");
  77. // }
  78. // close(fd);
  79. }
  80. GPIO_PinState stm32_gpio_read(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  81. {
  82. GPIO_PinState bitstatus;
  83. /* Check the parameters */
  84. //assert_param(IS_GPIO_PIN(GPIO_Pin));
  85. if((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
  86. {
  87. bitstatus = GPIO_PIN_SET;
  88. }
  89. else
  90. {
  91. bitstatus = GPIO_PIN_RESET;
  92. }
  93. return bitstatus;
  94. // int fd;
  95. // int ret;
  96. // gpio_t gpio_arg;
  97. // fd = open(DEV_NAME, O_RDWR);
  98. // if(fd == -1)
  99. // {
  100. // printf("Open %s failed!\n", DEV_NAME);
  101. // }
  102. // gpio_arg.GPIOx = GPIOx;
  103. // gpio_arg.GPIO_pin = GPIO_Pin;
  104. // ret = ioctl(fd, GPIO_READ_PIN, &gpio_arg);
  105. // if(ret == -1)
  106. // {
  107. // printf("Write gpio failed!\n");
  108. // }
  109. // close(fd);
  110. // return gpio_arg.Data;
  111. }
  112. //direct = 0: input
  113. //direct = 1: output
  114. int stm32_gpio_direct(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint8_t direct)
  115. {
  116. uint32_t temp;
  117. uint32_t ioposition;
  118. uint32_t iocurrent;
  119. uint32_t position;
  120. /* Configure the port pins */
  121. for(position = 0U; position < 16; position++)
  122. {
  123. /* Get the IO position */
  124. ioposition = 0x01U << position;
  125. /* Get the current IO position */
  126. iocurrent = (uint32_t)(GPIO_Pin) & ioposition;
  127. if(iocurrent == ioposition)
  128. {
  129. temp = GPIOx->MODER;
  130. temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
  131. temp |= (direct << (position * 2U));
  132. GPIOx->MODER = temp;
  133. }
  134. }
  135. // int fd;
  136. // int ret;
  137. // gpio_t gpio_arg;
  138. // if((direct != 0) && (direct != 1))
  139. // {
  140. // printf("Invalid direct! %d\n", direct);
  141. // return -1;
  142. // }
  143. // fd = open(DEV_NAME, O_RDWR);
  144. // if(fd == -1)
  145. // {
  146. // printf("Open %s failed!\n", DEV_NAME);
  147. // }
  148. // gpio_arg.GPIOx = GPIOx;
  149. // gpio_arg.GPIO_pin = GPIO_Pin;
  150. // gpio_arg.Direct = direct;
  151. // ret = ioctl(fd, GPIO_SET_DIR, &gpio_arg);
  152. // if(ret == -1)
  153. // {
  154. // printf("Set gpio direct failed!\n");
  155. // }
  156. // close(fd);
  157. }