hal_gpio_interface.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #define DEV_NAME "/dev/gpio"
  9. /* Demon
  10. // Set PD_2 output
  11. GPIO_InitTypeDef GPIO_InitStruct;
  12. GPIO_InitStruct.Pin = GPIO_PIN_12;
  13. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  14. GPIO_InitStruct.Pull = GPIO_NOPULL;
  15. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  16. stm32_gpio_init(GPIOD, &GPIO_InitStruct);
  17. stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
  18. // Set PI_8 input
  19. GPIO_InitStruct.Pin = GPIO_PIN_8;
  20. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  21. GPIO_InitStruct.Pull = GPIO_NOPULL;
  22. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  23. stm32_gpio_init(GPIOI, &GPIO_InitStruct);
  24. //Set PD_2 low
  25. stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
  26. //Get PI_8 value
  27. printf("PI_8: %d\n", stm32_gpio_read(GPIOI, GPIO_PIN_8));
  28. */
  29. void stm32_gpio_init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  30. {
  31. int fd;
  32. int ret;
  33. gpio_t gpio_arg;
  34. fd = open(DEV_NAME, O_RDWR);
  35. if(fd == -1)
  36. {
  37. printf("Open %s failed!\n", DEV_NAME);
  38. }
  39. gpio_arg.GPIOx = GPIOx;
  40. gpio_arg.GPIO_pin = GPIO_Init->Pin;
  41. memcpy(&gpio_arg.GPIO_Init, GPIO_Init, sizeof(GPIO_InitTypeDef));
  42. ret = ioctl(fd, GPIO_INIT, &gpio_arg);
  43. if(ret == -1)
  44. {
  45. printf("Init gpio failed!\n");
  46. }
  47. close(fd);
  48. }
  49. void stm32_gpio_write(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  50. {
  51. int fd;
  52. int ret;
  53. gpio_t gpio_arg;
  54. fd = open(DEV_NAME, O_RDWR);
  55. if(fd == -1)
  56. {
  57. printf("Open %s failed!\n", DEV_NAME);
  58. }
  59. gpio_arg.GPIOx = GPIOx;
  60. gpio_arg.GPIO_pin = GPIO_Pin;
  61. gpio_arg.Data = PinState;
  62. ret = ioctl(fd, GPIO_WRITE_PIN, &gpio_arg);
  63. if(ret == -1)
  64. {
  65. printf("Write gpio failed!\n");
  66. }
  67. close(fd);
  68. }
  69. GPIO_PinState stm32_gpio_read(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
  70. {
  71. int fd;
  72. int ret;
  73. gpio_t gpio_arg;
  74. fd = open(DEV_NAME, O_RDWR);
  75. if(fd == -1)
  76. {
  77. printf("Open %s failed!\n", DEV_NAME);
  78. }
  79. gpio_arg.GPIOx = GPIOx;
  80. gpio_arg.GPIO_pin = GPIO_Pin;
  81. ret = ioctl(fd, GPIO_READ_PIN, &gpio_arg);
  82. if(ret == -1)
  83. {
  84. printf("Write gpio failed!\n");
  85. }
  86. close(fd);
  87. return gpio_arg.Data;
  88. }