123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include "driver.h"
- #include <linux/types.h>
- #include <stdio.h>
- #include <string.h>
- #include "com_gpio.h"
- #include "hal_interface_api.h"
- #include "linux/fcntl.h"
- #define DEV_NAME "/dev/gpio"
- /* Demon
- // Set PD_2 output
- GPIO_InitTypeDef GPIO_InitStruct;
- GPIO_InitStruct.Pin = GPIO_PIN_12;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- stm32_gpio_init(GPIOD, &GPIO_InitStruct);
- stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
- // Set PI_8 input
- GPIO_InitStruct.Pin = GPIO_PIN_8;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- stm32_gpio_init(GPIOI, &GPIO_InitStruct);
- //Set PD_2 low
- stm32_gpio_write(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
- //Get PI_8 value
- printf("PI_8: %d\n", stm32_gpio_read(GPIOI, GPIO_PIN_8));
- */
- void stm32_gpio_init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
- {
- int fd;
- int ret;
- gpio_t gpio_arg;
- fd = open(DEV_NAME, O_RDWR);
- if(fd == -1)
- {
- printf("Open %s failed!\n", DEV_NAME);
- }
- gpio_arg.GPIOx = GPIOx;
- gpio_arg.GPIO_pin = GPIO_Init->Pin;
- memcpy(&gpio_arg.GPIO_Init, GPIO_Init, sizeof(GPIO_InitTypeDef));
- ret = ioctl(fd, GPIO_INIT, &gpio_arg);
- if(ret == -1)
- {
- printf("Init gpio failed!\n");
- }
- close(fd);
- }
- void stm32_gpio_write(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
- {
- int fd;
- int ret;
- gpio_t gpio_arg;
- fd = open(DEV_NAME, O_RDWR);
- if(fd == -1)
- {
- printf("Open %s failed!\n", DEV_NAME);
- }
- gpio_arg.GPIOx = GPIOx;
- gpio_arg.GPIO_pin = GPIO_Pin;
- gpio_arg.Data = PinState;
- ret = ioctl(fd, GPIO_WRITE_PIN, &gpio_arg);
- if(ret == -1)
- {
- printf("Write gpio failed!\n");
- }
- close(fd);
- }
- GPIO_PinState stm32_gpio_read(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
- {
- int fd;
- int ret;
- gpio_t gpio_arg;
- fd = open(DEV_NAME, O_RDWR);
- if(fd == -1)
- {
- printf("Open %s failed!\n", DEV_NAME);
- }
- gpio_arg.GPIOx = GPIOx;
- gpio_arg.GPIO_pin = GPIO_Pin;
- ret = ioctl(fd, GPIO_READ_PIN, &gpio_arg);
- if(ret == -1)
- {
- printf("Write gpio failed!\n");
- }
- close(fd);
- return gpio_arg.Data;
- }
|