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 int peripheral_interface_spi_close(peripheral_spi_h spi)
24 int ret = close(spi->fd);
25 CHECK_ERROR(ret != 0);
30 int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
32 int ret = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode);
33 CHECK_ERROR(ret != 0);
38 int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order)
40 int ret = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
41 CHECK_ERROR(ret != 0);
46 int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits)
48 int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
49 CHECK_ERROR(ret != 0);
54 int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq)
56 int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
57 CHECK_ERROR(ret != 0);
62 int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t length)
65 struct spi_ioc_transfer xfer;
67 memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
68 xfer.rx_buf = (unsigned long)rxbuf;
71 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
72 CHECK_ERROR(ret != 0);
77 int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_t length)
80 struct spi_ioc_transfer xfer;
82 memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
83 xfer.tx_buf = (unsigned long)txbuf;
86 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
87 CHECK_ERROR(ret != 0);
92 int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint8_t *rxbuf, uint32_t length)
95 struct spi_ioc_transfer xfer;
97 if (!txbuf || !rxbuf) return -EINVAL;
99 memset(&xfer, 0, sizeof(xfer));
100 xfer.tx_buf = (unsigned long)txbuf;
101 xfer.rx_buf = (unsigned long)rxbuf;
104 ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
105 CHECK_ERROR(ret != 0);