#ifndef __GPIO_H__
#define __GPIO_H__
+#include "peripheral_io.h"
+
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define GPIO_BUFFER_MAX 64
GPIO_EDGE_BOTH = 3,
} gpio_edge_e;
-int gpio_open(int gpiopin);
-int gpio_close(int gpiopin);
-int gpio_set_edge_mode(int gpiopin, gpio_edge_e edge);
-int gpio_get_edge_mode(int gpiopin, gpio_edge_e *edge);
-int gpio_set_direction(int gpiopin, gpio_direction_e dir);
-int gpio_get_direction(int gpiopin, gpio_direction_e *dir);
-int gpio_write(int gpiopin, int value);
-int gpio_read(int gpiopin, int *value);
+int gpio_close(peripheral_gpio_h gpio);
+int gpio_set_edge_mode(peripheral_gpio_h gpio, gpio_edge_e edge);
+int gpio_set_direction(peripheral_gpio_h gpio, gpio_direction_e dir);
+int gpio_write(peripheral_gpio_h gpio, int value);
+int gpio_read(peripheral_gpio_h gpio, int *value);
-int gpio_open_isr(int gpiopin);
-int gpio_close_isr(int file_hndl);
-int gpio_read_isr(void *fdset, char *rev_buf, int length);
+int gpio_open_isr(peripheral_gpio_h gpio);
+int gpio_close_isr(peripheral_gpio_h gpio);
#endif/*__GPIO_H__*/
#include <stdint.h>
+#include "peripheral_io.h"
+
#define SYSFS_I2C_DIR "/dev/i2c"
#define I2C_BUFFER_MAX 64
union i2c_smbus_data *data;
};
-int i2c_open(int bus, int *fd);
-int i2c_close(int fd);
-int i2c_set_address(int fd, int address);
-int i2c_read(int fd, unsigned char *data, int length);
-int i2c_write(int fd, const unsigned char *data, int length);
-int i2c_smbus_ioctl(int fd, struct i2c_smbus_ioctl_data *data);
+int i2c_close(peripheral_i2c_h i2c);
+int i2c_set_address(peripheral_i2c_h i2c, int address);
+int i2c_read(peripheral_i2c_h i2c, unsigned char *data, int length);
+int i2c_write(peripheral_i2c_h i2c, const unsigned char *data, int length);
+int i2c_smbus_ioctl(peripheral_i2c_h i2c, struct i2c_smbus_ioctl_data *data);
#endif/* __I2C_H__ */
#ifndef __PWM_H__
#define __PWM_H__
+#include "peripheral_io.h"
+
/**
* @brief Enumeration for Polarity
*/
PWM_POLARITY_INVERSED,
} pwm_polarity_e;
-/**
-* @brief pwm_open() init pwm pin.
-*
-* @param[in] chip pwm chip number
-* @param[in] pin pwm pin number
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int pwm_open(int chip, int pin);
/**
* @brief pwm_close() deinit pwm pin.
* @param[in] pin pwm pin number
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int pwm_close(int chip, int pin);
+int pwm_close(peripheral_pwm_h pwm);
/**
* @brief pwm_set_period() sets the pwm period.
* @param[in] period pwm period
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int pwm_set_period(int chip, int pin, int period);
-
-/**
-* @brief pwm_get_period() gets the pwm period.
-*
-* @param[in] chip pwm chip number
-* @param[in] pin pwm pin number
-* @param[out] period pwm period
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int pwm_get_period(int chip, int pin, int *period);
+int pwm_set_period(peripheral_pwm_h pwm, int period);
/**
* @brief pwm_set_duty_cycle() sets the pwm duty cycle.
* @param[in] duty_cycle pwm duty cycle
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int pwm_set_duty_cycle(int chip, int pin, int duty_cycle);
-
-/**
-* @brief pwm_get_duty_cycle() gets the pwm duty cycle.
-*
-* @param[in] chip pwm chip number
-* @param[in] pin pwm pin number
-* @param[out] duty_cycle pwm duty cycle
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int pwm_get_duty_cycle(int chip, int pin, int *duty_cycle);
+int pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle);
/**
* @brief pwm_set_polarity() sets the pwm polarity.
* @param[in] polarity pwm polarity
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int pwm_set_polarity(int chip, int pin, pwm_polarity_e polarity);
-/**
-* @brief pwm_get_polarity() gets the pwm polarity.
-*
-* @param[in] chip pwm chip number
-* @param[in] pin pwm pin number
-* @param[out] polarity pwm polarity
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int pwm_get_polarity(int chip, int pin, pwm_polarity_e *polarity);
+int pwm_set_polarity(peripheral_pwm_h pwm, pwm_polarity_e polarity);
/**
* @brief pwm_set_enable() sets the pwm state.
* @param[in] enable pwm enable/disabled state value
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int pwm_set_enable(int chip, int pin, bool enable);
-
-/**
-* @brief pwm_get_enable() checks if pwm state is enabled.
-*
-* @param[in] chip pwm chip number
-* @param[in] pin pwm pin number
-* @param[out] enable pwm enable/disabled state value
-* @return On success, 0 is returned. On failure, a negative value is returned.
-*/
-int pwm_get_enable(int chip, int pin, bool *enable);
+int pwm_set_enable(peripheral_pwm_h pwm, bool enable);
#endif /* __PWM_H__ */
#ifndef __SPI_H__
#define __SPI_H__
-int spi_open(int bus, int cs, int *fd);
-int spi_close(int fd);
-int spi_set_mode(int fd, unsigned char mode);
-int spi_set_bit_order(int fd, unsigned char lsb);
-int spi_set_bits_per_word(int fd, unsigned char bits);
-int spi_set_frequency(int fd, unsigned int freq);
-int spi_get_buffer_size(int *bufsiz);
-int spi_read(int fd, unsigned char *rxbuf, int length);
-int spi_write(int fd, unsigned char *txbuf, int length);
-int spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int length);
+#include "peripheral_io.h"
+
+int spi_close(peripheral_spi_h spi);
+int spi_set_mode(peripheral_spi_h spi, unsigned char mode);
+int spi_set_bit_order(peripheral_spi_h spi, unsigned char lsb);
+int spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits);
+int spi_set_frequency(peripheral_spi_h spi, unsigned int freq);
+int spi_read(peripheral_spi_h spi, unsigned char *rxbuf, int length);
+int spi_write(peripheral_spi_h spi, unsigned char *txbuf, int length);
+int spi_transfer(peripheral_spi_h spi, unsigned char *txbuf, unsigned char *rxbuf, int length);
#endif /* __SPI_H__ */
#ifndef __UART_H__
#define __UART_H__
+#include "peripheral_io.h"
+
#include <stdint.h>
/**
UART_STOP_BITS_2BIT
} uart_stop_bits_e;
-/**
-* @brief uart_valid_baudrate() validation check of input baudrate
-*
-* @param[in] baudrate baudrate for uart
-* @return On success, valid input. On failure, NULL is returned.
-*/
-int uart_valid_baud_rate(unsigned int baud_rate);
-
-/**
-* @brief uart_open() initializes uart port.
-*
-* @param[in] port uart port
-* @param[in] file_hndl handle of uart port
-* @return On success, handle of uart_context is returned. On failure, NULL is returned.
-*/
-int uart_open(int port, int *file_hndl);
-
/**
* @brief uart_close() closes uart port.
*
* @param[in] file_hndl handle of uart_context
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_close(int file_hndl);
+int uart_close(peripheral_uart_h uart);
/**
* @brief uart_flush() flushes uart buffer.
* @param[in] file_hndl handle of uart_context
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_flush(int file_hndl);
+int uart_flush(peripheral_uart_h uart);
/**
* @brief uart_set_baudrate() sets uart baud rate.
* @param[in] baud uart baud rate
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_baud_rate(int file_hndl, uart_baud_rate_e baud);
+int uart_set_baud_rate(peripheral_uart_h uart, uart_baud_rate_e baud);
/**
* @brief uart_set_mode() sets byte size, parity bit and stop bits.
* @param[in] stop_bits uart stop bits
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_mode(int file_hndl, uart_byte_size_e byte_size, uart_parity_e parity, uart_stop_bits_e stop_bits);
+int uart_set_mode(peripheral_uart_h uart, uart_byte_size_e byte_size, uart_parity_e parity, uart_stop_bits_e stop_bits);
/**
* @brief peripheral_bus_uart_set_byte_size() set byte size.
* @param[in] byte_size uart byte size
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_byte_size(int file_hndl, uart_byte_size_e byte_size);
+int uart_set_byte_size(peripheral_uart_h uart, uart_byte_size_e byte_size);
/**
* @brief peripheral_bus_uart_set_parity() set parity bit.
* @param[in] parity uart parity type (even/odd/none)
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_parity(int file_hndl, uart_parity_e parity);
+int uart_set_parity(peripheral_uart_h uart, uart_parity_e parity);
/**
* @brief peripheral_bus_uart_set_stop_bits() set stop bits.
* @param[in] stop_bits uart stop bits
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_stop_bits(int file_hndl, uart_stop_bits_e stop_bits);
+int uart_set_stop_bits(peripheral_uart_h uart, uart_stop_bits_e stop_bits);
/**
* @brief uart_set_flow_control() set flow control settings.
* @param[in] rtscts rts/cts
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_set_flow_control(int file_hndl, bool xonxoff, bool rtscts);
+int uart_set_flow_control(peripheral_uart_h uart, bool xonxoff, bool rtscts);
/**
* @brief uart_read() reads data over uart bus.
* @param[in] length size to read
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_read(int file_hndl, uint8_t *buf, unsigned int length);
+int uart_read(peripheral_uart_h uart, uint8_t *buf, unsigned int length);
/**
* @brief uart_write() writes data over uart bus.
* @param[in] length size to write
* @return On success, 0 is returned. On failure, a negative value is returned.
*/
-int uart_write(int file_hndl, uint8_t *buf, unsigned int length);
+int uart_write(peripheral_uart_h uart, uint8_t *buf, unsigned int length);
#endif /* __UART_H__ */
#include "gpio.h"
#include "peripheral_common.h"
+#include "peripheral_internal.h"
#define MAX_ERR_LEN 255
-int gpio_open(int gpiopin)
+int gpio_set_direction(peripheral_gpio_h gpio, gpio_direction_e dir)
{
- int fd, len, status;
- char gpio_export[GPIO_BUFFER_MAX] = {0, };
-
- _D("gpiopin : %d", gpiopin);
-
- fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/export :%s\n", errmsg);
- return -ENXIO;
- }
-
- len = snprintf(gpio_export, GPIO_BUFFER_MAX, "%d", gpiopin);
- status = write(fd, gpio_export, len);
-
- if (status != len) {
- close(fd);
- _E("Error: gpio open error \n");
- return -EIO;
- }
-
- close(fd);
-
- return 0;
-}
-
-int gpio_set_direction(int gpiopin, gpio_direction_e dir)
-{
- int fd, status;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
-
- _D("gpiopin : %d, dir : %d", gpiopin, dir);
-
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/direction", gpiopin);
- fd = open(gpio_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d/direction: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
+ int status;
if (dir == GPIO_DIRECTION_IN)
- status = write(fd, "in", strlen("in")+1);
+ status = write(gpio->fd_direction, "in", strlen("in")+1);
else if (dir == GPIO_DIRECTION_OUT_HIGH)
- status = write(fd, "high", strlen("high")+1);
+ status = write(gpio->fd_direction, "high", strlen("high")+1);
else if (dir == GPIO_DIRECTION_OUT_LOW)
- status = write(fd, "low", strlen("low")+1);
+ status = write(gpio->fd_direction, "low", strlen("low")+1);
else {
- close(fd);
_E("Error: gpio direction is wrong\n");
return -EIO;
}
if (status <= 0) {
- close(fd);
_E("Error: gpio direction set error\n");
return -EIO;
}
- close(fd);
-
return 0;
}
-int gpio_get_direction(int gpiopin, gpio_direction_e *dir)
+int gpio_set_edge_mode(peripheral_gpio_h gpio, gpio_edge_e edge)
{
- int fd, len;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
- char gpio_buf[GPIO_BUFFER_MAX] = {0, };
-
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/direction", gpiopin);
- fd = open(gpio_dev, O_RDONLY);
-
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d/direction: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
-
- len = read(fd, &gpio_buf, GPIO_BUFFER_MAX);
- if (len <= 0) {
- close(fd);
- _E("Error: gpio direction read error\n");
- return -EIO;
- }
-
- if (0 == strncmp(gpio_buf, "in", strlen("in")))
- *dir = GPIO_DIRECTION_IN;
- else if (0 == strncmp(gpio_buf, "out", strlen("out")))
- *dir = GPIO_DIRECTION_OUT_LOW;
- else {
- close(fd);
- _E("Error: gpio direction is wrong\n");
- return -EIO;
- }
-
- close(fd);
-
- return 0;
-}
-
-int gpio_set_edge_mode(int gpiopin, gpio_edge_e edge)
-{
- int fd, status;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
-
- _D("gpiopin : %d, edge : %d", gpiopin, edge);
-
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/edge", gpiopin);
- fd = open(gpio_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d/edge: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
+ int status;
if (edge == GPIO_EDGE_NONE)
- status = write(fd, "none", strlen("none")+1);
+ status = write(gpio->fd_edge, "none", strlen("none")+1);
else if (edge == GPIO_EDGE_RISING)
- status = write(fd, "rising", strlen("rising")+1);
+ status = write(gpio->fd_edge, "rising", strlen("rising")+1);
else if (edge == GPIO_EDGE_FALLING)
- status = write(fd, "falling", strlen("falling")+1);
+ status = write(gpio->fd_edge, "falling", strlen("falling")+1);
else if (edge == GPIO_EDGE_BOTH)
- status = write(fd, "both", strlen("both")+1);
+ status = write(gpio->fd_edge, "both", strlen("both")+1);
else {
- close(fd);
_E("Error: gpio edge is wrong\n");
return -EIO;
}
if (status <= 0) {
- close(fd);
_E("Error: gpio edge set error\n");
return -EIO;
}
- close(fd);
-
- return 0;
-}
-
-int gpio_get_edge_mode(int gpiopin, gpio_edge_e *edge)
-{
- int fd, len;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
- char gpio_buf[GPIO_BUFFER_MAX] = {0, };
-
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/edge", gpiopin);
- fd = open(gpio_dev, O_RDONLY);
-
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d/edge: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
-
- len = read(fd, &gpio_buf, GPIO_BUFFER_MAX);
- if (len <= 0) {
- close(fd);
- _E("Error: gpio edge read error\n");
- return -EIO;
- }
-
- if (0 == strncmp(gpio_buf, "none", strlen("none")))
- *edge = GPIO_EDGE_NONE;
- else if (0 == strncmp(gpio_buf, "both", strlen("both")))
- *edge = GPIO_EDGE_BOTH;
- else if (0 == strncmp(gpio_buf, "rising", strlen("rising")))
- *edge = GPIO_EDGE_RISING;
- else if (0 == strncmp(gpio_buf, "falling", strlen("falling")))
- *edge = GPIO_EDGE_FALLING;
- else {
- close(fd);
- _E("Error: gpio edge is wrong\n");
- return -EIO;
- }
-
- close(fd);
-
return 0;
}
-int gpio_write(int gpiopin, int value)
+int gpio_write(peripheral_gpio_h gpio, int value)
{
- int fd, status;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
-
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
- fd = open(gpio_dev, O_WRONLY);
-
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d/value: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
+ int status;
if (value == 1)
- status = write(fd, "1", strlen("1")+1);
+ status = write(gpio->fd_value, "1", strlen("1")+1);
else if (value == 0)
- status = write(fd, "0", strlen("0")+1);
+ status = write(gpio->fd_value, "0", strlen("0")+1);
else {
- close(fd);
_E("Error: gpio write value error \n");
return -EIO;
}
if (status <= 0) {
- close(fd);
_E("Error: gpio write error\n");
return -EIO;
}
- close(fd);
-
return 0;
}
-int gpio_read(int gpiopin, int *value)
+int gpio_read(peripheral_gpio_h gpio, int *value)
{
- int fd, len;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
+ int len;
char gpio_buf[GPIO_BUFFER_MAX] = {0, };
- snprintf(gpio_dev, GPIO_BUFFER_MAX, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
- fd = open(gpio_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d pin value: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
-
- len = read(fd, &gpio_buf, 1);
- close(fd);
-
+ len = read(gpio->fd_value, &gpio_buf, 1);
if (len <= 0) {
_E("Error: gpio read error \n");
return -EIO;
return 0;
}
-int gpio_close(int gpiopin)
+int gpio_close(peripheral_gpio_h gpio)
{
- int fd, len, status;
- char gpio_unexport[GPIO_BUFFER_MAX] = {0, };
-
- _D("gpiopin : %d", gpiopin);
+ int status;
- fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/unexport %s\n", errmsg);
- return -ENXIO;
+ status = close(gpio->fd_direction);
+ if (status < 0) {
+ _E("Error: gpio direction fd close error \n");
+ return -EIO;
}
- len = snprintf(gpio_unexport, GPIO_BUFFER_MAX, "%d", gpiopin);
- status = write(fd, gpio_unexport, len);
-
- if (status != len) {
- close(fd);
- _E("Error: gpio close error \n");
+ status = close(gpio->fd_edge);
+ if (status < 0) {
+ _E("Error: gpio edge fd close error \n");
return -EIO;
}
- close(fd);
+ status = close(gpio->fd_value);
+ if (status < 0) {
+ _E("Error: gpio value fd close error \n");
+ return -EIO;
+ }
return 0;
}
-int gpio_open_isr(int gpiopin)
+int gpio_open_isr(peripheral_gpio_h gpio)
{
- int fd;
- char gpio_dev[GPIO_BUFFER_MAX] = {0, };
-
- snprintf(gpio_dev, sizeof(gpio_dev)-1, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
+ // int fd;
+ // char gpio_dev[GPIO_BUFFER_MAX] = {0, };
- _D("open isr string [%s]", gpio_dev);
+ // snprintf(gpio_dev, sizeof(gpio_dev)-1, SYSFS_GPIO_DIR"/gpio%d/value", gpiopin);
- fd = open(gpio_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open /sys/class/gpio/gpio%d pin value: %s\n", gpiopin, errmsg);
- return -ENXIO;
- }
-
- return fd;
-}
+ // _D("open isr string [%s]", gpio_dev);
-int gpio_close_isr(int fd)
-{
- if (fd <= 0) return -EINVAL;
+ // fd = open(gpio_dev, O_RDONLY);
+ // if (fd < 0) {
+ // char errmsg[MAX_ERR_LEN];
+ // strerror_r(errno, errmsg, MAX_ERR_LEN);
+ // _E("Can't Open /sys/class/gpio/gpio%d pin value: %s\n", gpiopin, errmsg);
+ // return -ENXIO;
+ // }
- close(fd);
+ // return fd;
return 0;
}
-int gpio_read_isr(void *fdset, char *rev_buf, int length)
+int gpio_close_isr(peripheral_gpio_h gpio)
{
- int poll_state = 0;
- int len;
- struct pollfd poll_events;
-
- poll_events.fd = ((struct pollfd*)fdset)->fd;
- poll_events.events = POLLPRI;
- poll_events.revents = ((struct pollfd*)fdset)->revents;
-
- poll_state = poll((struct pollfd*)&poll_events, 1, -1); // 0 is going to return directly.
-
- if (poll_state < 0) {
- _E("poll() failed!\n");
- return -EIO;
- }
+// if (fd <= 0) return -EINVAL;
- if (poll_events.revents & POLLPRI) {
- lseek(poll_events.fd, 0, SEEK_SET);
- len = read(poll_events.fd, rev_buf, length);
- if (len == -1)
- return -EIO;
- }
+// close(fd);
- return poll_state;
-}
+ return 0;
+}
\ No newline at end of file
#include "i2c.h"
#include "peripheral_common.h"
+#include "peripheral_internal.h"
#define MAX_ERR_LEN 255
-int i2c_open(int bus, int *fd)
-{
- int new_fd;
- char i2c_dev[I2C_BUFFER_MAX] = {0,};
-
- _D("bus : %d", bus);
-
- snprintf(i2c_dev, sizeof(i2c_dev)-1, SYSFS_I2C_DIR"-%d", bus);
- new_fd = open(i2c_dev, O_RDWR);
- if (new_fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s : %s", i2c_dev, errmsg);
- return -ENXIO;
- }
- _D("fd : %d", new_fd);
- *fd = new_fd;
-
- return 0;
-}
-
-int i2c_close(int fd)
+int i2c_close(peripheral_i2c_h i2c)
{
int status;
- _D("fd : %d", fd);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d", i2c->fd);
+ RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd");
- status = close(fd);
+ status = close(i2c->fd);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to close fd : %d", fd);
+ _E("Failed to close fd : %d", i2c->fd);
return -EIO;
}
return 0;
}
-int i2c_set_address(int fd, int address)
+int i2c_set_address(peripheral_i2c_h i2c, int address)
{
int status;
- _D("fd : %d, slave address : 0x%x", fd, address);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d, slave address : 0x%x", i2c->fd, address);
+ RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd");
- status = ioctl(fd, I2C_SLAVE, address);
+ status = ioctl(i2c->fd, I2C_SLAVE, address);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to set slave address(%x), fd : %d, errmsg : %s", address, fd, errmsg);
+ _E("Failed to set slave address(%x), fd : %d, errmsg : %s", address, i2c->fd, errmsg);
return status;
}
return 0;
}
-int i2c_read(int fd, unsigned char *data, int length)
+int i2c_read(peripheral_i2c_h i2c, unsigned char *data, int length)
{
int status;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
- status = read(fd, data, length);
+ status = read(i2c->fd, data, length);
if (status != length) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("i2c read failed, fd : %d, errmsg : %s", fd, errmsg);
+ _E("i2c read failed, fd : %d, errmsg : %s", i2c->fd, errmsg);
return -EIO;
}
return 0;
}
-int i2c_write(int fd, const unsigned char *data, int length)
+int i2c_write(peripheral_i2c_h i2c, const unsigned char *data, int length)
{
int status;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
- status = write(fd, data, length);
+ status = write(i2c->fd, data, length);
if (status != length) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("i2c write failed fd : %d, errmsg : %s", fd, errmsg);
+ _E("i2c write failed fd : %d, errmsg : %s", i2c->fd, errmsg);
return -EIO;
}
return 0;
}
-int i2c_smbus_ioctl(int fd, struct i2c_smbus_ioctl_data *data)
+int i2c_smbus_ioctl(peripheral_i2c_h i2c, struct i2c_smbus_ioctl_data *data)
{
int status;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(i2c->fd < 0, -EINVAL, "Invalid fd : %d", i2c->fd);
- status = ioctl(fd, I2C_SMBUS, data);
+ status = ioctl(i2c->fd, I2C_SMBUS, data);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("i2c transaction failed fd : %d, errmsg : %s", fd, errmsg);
+ _E("i2c transaction failed fd : %d, errmsg : %s", i2c->fd, errmsg);
return -EIO;
}
#include "pwm.h"
#include "peripheral_common.h"
+#include "peripheral_internal.h"
#define SYSFS_PWM_PATH "/sys/class/pwm"
#define PWM_BUF_MAX 16
#define MAX_ERR_LEN 255
-int pwm_open(int chip, int pin)
+int pwm_close(peripheral_pwm_h pwm)
{
- int fd, len, status;
- char pwm_dev[PATH_BUF_MAX] = {0};
- char pwm_buf[PWM_BUF_MAX] = {0};
-
- _D("chip : %d, pin : %d", chip, pin);
+ int status;
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/export", chip);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", pin);
- status = write(fd, pwm_buf, len);
+ status = close(pwm->fd_period);
if (status < 0) {
- _E("Failed to export pwmchip%d, pwm%d", chip, pin);
- close(fd);
+ _E("Error: pwm period fd close error \n");
return -EIO;
}
- close(fd);
-
- return 0;
-}
-
-int pwm_close(int chip, int pin)
-{
- int fd, len, status;
- char pwm_dev[PATH_BUF_MAX] = {0};
- char pwm_buf[PWM_BUF_MAX] = {0};
- _D("chip : %d, pin : %d", chip, pin);
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/unexport", chip);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", pin);
- status = write(fd, pwm_buf, len);
+ status = close(pwm->fd_duty_cycle);
if (status < 0) {
- _E("Failed to unexport pwmchip%d, pwm%", chip, pin);
- close(fd);
+ _E("Error: pwm duty cycle fd close error \n");
return -EIO;
}
- close(fd);
-
- return 0;
-}
-
-int pwm_set_period(int chip, int pin, int period)
-{
- int fd, len, status;
- char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
- _D("chip : %d, pin : %d, period : %d", chip, pin, period);
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/period", chip, pin);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", period);
- status = write(fd, pwm_buf, len);
+ status = close(pwm->fd_polarity);
if (status < 0) {
- close(fd);
- _E("Failed to set period, path : %s", pwm_dev);
+ _E("Error: pwm polarity fd close error \n");
return -EIO;
}
- close(fd);
-
- return 0;
-}
-
-int pwm_get_period(int chip, int pin, int *period)
-{
- int fd, result, status;
- char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/period", chip, pin);
- fd = open(pwm_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
- status = read(fd, pwm_buf, PWM_BUF_MAX);
+ status = close(pwm->fd_enable);
if (status < 0) {
- close(fd);
- _E("Failed to get period, path : %s", pwm_dev);
+ _E("Error: pwm enable fd close error \n");
return -EIO;
}
- result = atoi(pwm_buf);
- *period = result;
- close(fd);
return 0;
}
-int pwm_set_duty_cycle(int chip, int pin, int duty_cycle)
+int pwm_set_period(peripheral_pwm_h pwm, int period)
{
- int fd, len, status;
+ int len, status;
char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
- _D("chip : %d, pin : %d, duty_cycle : %d", chip, pin, duty_cycle);
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/duty_cycle", chip, pin);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", duty_cycle);
- status = write(fd, pwm_buf, len);
+ len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", period);
+ status = write(pwm->fd_period, pwm_buf, len);
if (status < 0) {
- close(fd);
- _E("Failed to set duty cycle, path : %s", pwm_dev);
+ _E("Failed to set period");
return -EIO;
}
- close(fd);
return 0;
}
-int pwm_get_duty_cycle(int chip, int pin, int *duty_cycle)
+int pwm_set_duty_cycle(peripheral_pwm_h pwm, int duty_cycle)
{
- int fd, result, status;
+ int len, status;
char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/duty_cycle", chip, pin);
- fd = open(pwm_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
- status = read(fd, pwm_buf, PWM_BUF_MAX);
+ len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", duty_cycle);
+ status = write(pwm->fd_duty_cycle, pwm_buf, len);
if (status < 0) {
- close(fd);
- _E("Failed to get duty cycle, path : %s", pwm_dev);
+ _E("Failed to set duty cycle");
return -EIO;
}
- result = atoi(pwm_buf);
- *duty_cycle = result;
- close(fd);
return 0;
}
-int pwm_set_polarity(int chip, int pin, pwm_polarity_e polarity)
+int pwm_set_polarity(peripheral_pwm_h pwm, pwm_polarity_e polarity)
{
- int fd, status;
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- _D("chip : %d, pin : %d, polarity : %d", chip, pin, polarity);
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/polarity", chip, pin);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
+ int status;
if (polarity == PWM_POLARITY_NORMAL)
- status = write(fd, "normal", strlen("normal")+1);
+ status = write(pwm->fd_polarity, "normal", strlen("normal")+1);
else if (polarity == PWM_POLARITY_INVERSED)
- status = write(fd, "inversed", strlen("inversed")+1);
+ status = write(pwm->fd_polarity, "inversed", strlen("inversed")+1);
else {
_E("Invalid pwm polarity : %d", polarity);
- close(fd);
return -EINVAL;
}
if (status <= 0) {
- close(fd);
- _E("Failed to set polarity, path : %s", pwm_dev);
+ _E("Failed to set polarity");
return -EIO;
}
- close(fd);
return 0;
}
-int pwm_get_polarity(int chip, int pin, pwm_polarity_e *polarity)
+int pwm_set_enable(peripheral_pwm_h pwm, bool enable)
{
- int fd, status;
+ int len, status;
char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/polarity", chip, pin);
- fd = open(pwm_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- status = read(fd, pwm_buf, PWM_BUF_MAX);
- if (status < 0) {
- _E("Failed to get polarity, path : %s", pwm_dev);
- close(fd);
- return -EIO;
- }
-
- if (0 == strncmp(pwm_buf, "normal", strlen("normal")))
- *polarity = PWM_POLARITY_NORMAL;
- else if (0 == strncmp(pwm_buf, "inversed", strlen("inversed")))
- *polarity = PWM_POLARITY_INVERSED;
- else {
- close(fd);
- _E("Invalid pwm polarity : %d", pwm_buf);
- return -EIO;
- }
- close(fd);
-
- return 0;
-}
-
-int pwm_set_enable(int chip, int pin, bool enable)
-{
- int fd, len, status;
- char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- _D("chip : %d, pin : %d, enable : %d", chip, pin, enable);
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/enable", chip, pin);
- fd = open(pwm_dev, O_WRONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
len = snprintf(pwm_buf, sizeof(pwm_buf), "%d", enable);
- status = write(fd, pwm_buf, len);
- if (status < 0) {
- close(fd);
- _E("Failed to set enable, path : %s", pwm_dev);
- return -EIO;
- }
- close(fd);
-
- return 0;
-}
-
-int pwm_get_enable(int chip, int pin, bool *enable)
-{
- int fd, result, status;
- char pwm_buf[PWM_BUF_MAX] = {0};
- char pwm_dev[PATH_BUF_MAX] = {0};
-
- snprintf(pwm_dev, PATH_BUF_MAX, SYSFS_PWM_PATH "/pwmchip%d/pwm%d/enable", chip, pin);
- fd = open(pwm_dev, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", pwm_dev, errmsg);
- return -ENXIO;
- }
-
- status = read(fd, pwm_buf, PWM_BUF_MAX);
+ status = write(pwm->fd_enable, pwm_buf, len);
if (status < 0) {
- close(fd);
- _E("Failed to get enable, path : %s", pwm_dev);
+ _E("Failed to set enable");
return -EIO;
}
- result = atoi(pwm_buf);
- *enable = !!result;
- close(fd);
return 0;
}
-
#include "spi.h"
#include "peripheral_common.h"
+#include "peripheral_internal.h"
#define SYSFS_SPI_DIR "/dev/spidev"
#define SYSFS_SPI_BUFSIZ "/sys/module/spidev/parameters/bufsiz"
#define SPI_BUFFER_MAX 64
#define MAX_ERR_LEN 255
-int spi_open(int bus, int cs, int *fd)
-{
- int new_fd = 0;
- char spi_dev[SPI_BUFFER_MAX] = {0,};
-
- _D("bus : %d, cs : %d", bus, cs);
-
- snprintf(spi_dev, sizeof(spi_dev)-1, SYSFS_SPI_DIR"%d.%d", bus, cs);
-
- new_fd = open(spi_dev, O_RDWR);
- if (new_fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", spi_dev, errmsg);
- return -ENXIO;
- }
- _D("fd : %d", new_fd);
- *fd = new_fd;
-
- return 0;
-}
-
-int spi_close(int fd)
+int spi_close(peripheral_spi_h spi)
{
int status;
- _D("fd : %d", fd);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d", spi->fd);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
- status = close(fd);
+ status = close(spi->fd);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to close fd : %d", fd);
+ _E("Failed to close fd : %d", spi->fd);
return -EIO;
}
return 0;
}
-int spi_set_mode(int fd, unsigned char mode)
+int spi_set_mode(peripheral_spi_h spi, unsigned char mode)
{
int status;
- _D("fd : %d, mode : %d", fd, mode);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d, mode : %d", spi->fd, mode);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
- status = ioctl(fd, SPI_IOC_WR_MODE, &mode);
+ status = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int spi_set_bit_order(int fd, unsigned char lsb)
+int spi_set_bit_order(peripheral_spi_h spi, unsigned char lsb)
{
int status;
- _D("fd : %d, lsb : %d", fd, lsb);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d, lsb : %d", spi->fd, lsb);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
- status = ioctl(fd, SPI_IOC_WR_LSB_FIRST, &lsb);
+ status = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &lsb);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to set lsb first(%d), fd : %d, errmsg : %s", lsb, fd, errmsg);
+ _E("Failed to set lsb first(%d), fd : %d, errmsg : %s", lsb, spi->fd, errmsg);
return -EIO;
}
return 0;
}
-int spi_set_bits_per_word(int fd, unsigned char bits)
+int spi_set_bits_per_word(peripheral_spi_h spi, unsigned char bits)
{
int status;
- _D("fd : %d, bits : %d", fd, bits);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d, bits : %d", spi->fd, bits);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
- status = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
+ status = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to set bits(%d), fd : %d, errmsg : %s", bits, fd, errmsg);
+ _E("Failed to set bits(%d), fd : %d, errmsg : %s", bits, spi->fd, errmsg);
return -EIO;
}
return 0;
}
-int spi_set_frequency(int fd, unsigned int freq)
+int spi_set_frequency(peripheral_spi_h spi, unsigned int freq)
{
int status;
- _D("fd : %d, freq : %d", fd, freq);
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd");
+ _D("fd : %d, freq : %d", spi->fd, freq);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd");
- status = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
+ status = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to set frequency(%d), fd : %d, errmsg : %s", freq, fd, errmsg);
- return -EIO;
- }
-
- return 0;
-}
-
-int spi_get_buffer_size(int *bufsiz)
-{
- int fd, result, status;
- char spi_buf[SPI_BUFFER_MAX] = {0,};
-
- fd = open(SYSFS_SPI_BUFSIZ, O_RDONLY);
- if (fd < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Can't Open %s, errmsg : %s", SYSFS_SPI_BUFSIZ, errmsg);
- return -ENXIO;
- }
-
- status = read(fd, spi_buf, SPI_BUFFER_MAX);
- if (status < 0) {
- _E("Failed to get buffer size, path : %s", SYSFS_SPI_BUFSIZ);
- close(fd);
+ _E("Failed to set frequency(%d), fd : %d, errmsg : %s", freq, spi->fd, errmsg);
return -EIO;
}
- result = atoi(spi_buf);
- *bufsiz = result;
- close(fd);
return 0;
}
-int spi_read(int fd, unsigned char *rxbuf, int length)
+int spi_read(peripheral_spi_h spi, unsigned char *rxbuf, int length)
{
int status;
struct spi_ioc_transfer xfer;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd : %d", spi->fd);
memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
xfer.rx_buf = (unsigned long)rxbuf;
xfer.len = length;
- status = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
+ status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to read data, fd : %d, length : %d, errmsg : %s", fd, length, errmsg);
+ _E("Failed to read data, fd : %d, length : %d, errmsg : %s", spi->fd, length, errmsg);
return -EIO;
}
return 0;
}
-int spi_write(int fd, unsigned char *txbuf, int length)
+int spi_write(peripheral_spi_h spi, unsigned char *txbuf, int length)
{
int status;
struct spi_ioc_transfer xfer;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd : %d", spi->fd);
memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
xfer.tx_buf = (unsigned long)txbuf;
xfer.len = length;
- status = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
+ status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int spi_transfer(int fd, unsigned char *txbuf, unsigned char *rxbuf, int length)
+int spi_transfer(peripheral_spi_h spi, unsigned char *txbuf, unsigned char *rxbuf, int length)
{
int status;
struct spi_ioc_transfer xfer;
- RETVM_IF(fd < 0, -EINVAL, "Invalid fd : %d", fd);
+ RETVM_IF(spi->fd < 0, -EINVAL, "Invalid fd : %d", spi->fd);
if (!txbuf || !rxbuf || length < 0) return -EINVAL;
xfer.rx_buf = (unsigned long)rxbuf;
xfer.len = length;
- status = ioctl(fd, SPI_IOC_MESSAGE(1), &xfer);
+ status = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
#include "uart.h"
#include "peripheral_common.h"
+#include "peripheral_internal.h"
#define PATH_BUF_MAX 64
#define UART_BUF_MAX 16
static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
-int uart_open(int port, int *file_hndl)
-{
- int i, fd;
- char uart_dev[PATH_BUF_MAX] = {0};
-
- _D("port : %d", port);
-
- for (i = 0; i < ARRAY_SIZE(sysfs_uart_path); i++) {
- snprintf(uart_dev, PATH_BUF_MAX, "%s%d", sysfs_uart_path[i], port);
- if (access(uart_dev, F_OK) == 0)
- break;
- }
- _D("uart_dev : %s", uart_dev);
- if ((fd = open(uart_dev, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) {
- char errmsg[MAX_ERR_LEN];
- strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("can't open %s, errmsg : %s", uart_dev, errmsg);
- return -ENXIO;
- }
- *file_hndl = fd;
- return 0;
-}
-
-int uart_close(int file_hndl)
+int uart_close(peripheral_uart_h uart)
{
int status;
- _D("file_hndl : %d", file_hndl);
+ _D("file_hndl : %d", uart->fd);
- if (file_hndl < 0) {
+ if (uart->fd < 0) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- status = uart_flush(file_hndl);
+ status = uart_flush(uart);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to close fd : %d, errmsg : %s", file_hndl, errmsg);
+ _E("Failed to close fd : %d, errmsg : %s", uart->fd, errmsg);
return -EIO;
}
- status = close(file_hndl);
+ status = close(uart->fd);
if (status < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
- _E("Failed to close fd : %d, errmsg : %s", file_hndl, errmsg);
+ _E("Failed to close fd : %d, errmsg : %s", uart->fd, errmsg);
return -EIO;
}
return 0;
}
-int uart_flush(int file_hndl)
+int uart_flush(peripheral_uart_h uart)
{
int ret;
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = tcflush(file_hndl, TCIOFLUSH);
+ ret = tcflush(uart->fd, TCIOFLUSH);
if (ret < 0) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_baud_rate(int file_hndl, uart_baud_rate_e baud)
+int uart_set_baud_rate(peripheral_uart_h uart, uart_baud_rate_e baud)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, baud : %d", file_hndl, baud);
+ _D("file_hndl : %d, baud : %d", uart->fd, baud);
memset(&tio, 0, sizeof(tio));
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
- uart_flush(file_hndl);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ uart_flush(uart);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_mode(int file_hndl, uart_byte_size_e byte_size, uart_parity_e parity, uart_stop_bits_e stop_bits)
+int uart_set_mode(peripheral_uart_h uart, uart_byte_size_e byte_size, uart_parity_e parity, uart_stop_bits_e stop_bits)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, bytesize : %d, parity : %d, stopbits : %d", file_hndl, byte_size, parity, stop_bits);
+ _D("file_hndl : %d, bytesize : %d, parity : %d, stopbits : %d", uart->fd, byte_size, parity, stop_bits);
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return -EINVAL;
}
- uart_flush(file_hndl);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ uart_flush(uart);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_byte_size(int file_hndl, uart_byte_size_e byte_size)
+int uart_set_byte_size(peripheral_uart_h uart, uart_byte_size_e byte_size)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, bytesize : %d", file_hndl, byte_size);
+ _D("file_hndl : %d, bytesize : %d", uart->fd, byte_size);
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
tio.c_cflag |= byteinfo[byte_size];
tio.c_cflag |= (CLOCAL | CREAD);
- uart_flush(file_hndl);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ uart_flush(uart);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_parity(int file_hndl, uart_parity_e parity)
+int uart_set_parity(peripheral_uart_h uart, uart_parity_e parity)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, parity : %d", file_hndl, parity);
+ _D("file_hndl : %d, parity : %d", uart->fd, parity);
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
break;
}
- uart_flush(file_hndl);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ uart_flush(uart);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_stop_bits(int file_hndl, uart_stop_bits_e stop_bits)
+int uart_set_stop_bits(peripheral_uart_h uart, uart_stop_bits_e stop_bits)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, stopbits : %d", file_hndl, stop_bits);
+ _D("file_hndl : %d, stopbits : %d", uart->fd, stop_bits);
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return -EINVAL;
}
- uart_flush(file_hndl);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ uart_flush(uart);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_set_flow_control(int file_hndl, bool xonxoff, bool rtscts)
+int uart_set_flow_control(peripheral_uart_h uart, bool xonxoff, bool rtscts)
{
int ret;
struct termios tio;
- _D("file_hndl : %d, xonxoff : %d, rtscts : %d", file_hndl, xonxoff, rtscts);
+ _D("file_hndl : %d, xonxoff : %d, rtscts : %d", uart->fd, xonxoff, rtscts);
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = tcgetattr(file_hndl, &tio);
+ ret = tcgetattr(uart->fd, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
else
tio.c_iflag &= ~(IXON | IXOFF | IXANY);
- ret = tcsetattr(file_hndl, TCSANOW, &tio);
+ ret = tcsetattr(uart->fd, TCSANOW, &tio);
if (ret) {
char errmsg[MAX_ERR_LEN];
strerror_r(errno, errmsg, MAX_ERR_LEN);
return 0;
}
-int uart_read(int file_hndl, uint8_t *buf, unsigned int length)
+int uart_read(peripheral_uart_h uart, uint8_t *buf, unsigned int length)
{
int ret;
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = read(file_hndl, (void *)buf, length);
+ ret = read(uart->fd, (void *)buf, length);
if (ret <= 0) {
if (errno == EAGAIN)
return -EAGAIN;
return ret;
}
-int uart_write(int file_hndl, uint8_t *buf, unsigned int length)
+int uart_write(peripheral_uart_h uart, uint8_t *buf, unsigned int length)
{
int ret;
- if (!file_hndl) {
+ if (!uart->fd) {
_E("Invalid NULL parameter");
return -EINVAL;
}
- ret = write(file_hndl, buf, length);
+ ret = write(uart->fd, buf, length);
if (ret <= 0) {
if (errno == EAGAIN)
return -EAGAIN;