2 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <sys/ioctl.h>
18 #include <linux/spi/spidev.h>
20 #include "peripheral_interface_spi.h"
22 #define SYSFS_SPI_DIR "/dev/spidev"
23 #define SYSFS_SPI_BUFSIZ "/sys/module/spidev/parameters/bufsiz"
24 #define SPI_BUFFER_MAX 64
25 #define MAX_ERR_LEN 255
27 int peripheral_interface_spi_close(peripheral_spi_h spi)
29 int ret = close(spi->fd);
30 CHECK_ERROR(ret != 0);
35 int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
37 int ret = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode);
38 CHECK_ERROR(ret != 0);
43 int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order)
45 int ret = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
46 CHECK_ERROR(ret != 0);
51 int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits)
53 int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
54 CHECK_ERROR(ret != 0);
59 int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq)
61 int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
62 CHECK_ERROR(ret != 0);
67 int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t length)
70 struct spi_ioc_transfer xfer;
72 memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
73 xfer.rx_buf = (unsigned long)rxbuf;
76 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
77 CHECK_ERROR(ret != 0);
82 int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_t length)
85 struct spi_ioc_transfer xfer;
87 memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
88 xfer.tx_buf = (unsigned long)txbuf;
91 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
92 CHECK_ERROR(ret != 0);
97 int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint8_t *rxbuf, uint32_t length)
100 struct spi_ioc_transfer xfer;
102 if (!txbuf || !rxbuf) return -EINVAL;
104 memset(&xfer, 0, sizeof(xfer));
105 xfer.tx_buf = (unsigned long)txbuf;
106 xfer.rx_buf = (unsigned long)rxbuf;
109 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
110 CHECK_ERROR(ret != 0);