From 2b462a5419904e0882e936ab637a342bda78c944 Mon Sep 17 00:00:00 2001 From: Adrian Szyndela Date: Fri, 11 Jun 2021 14:59:27 +0200 Subject: [PATCH] spi: flatten structure - remove 'interface' Merge src/peripheral_spi.c with src/interface/peripheral_interface_spi.c. Change-Id: I9b45addfafe7c71e99cbd7797069df6f98fc69ec --- CMakeLists.txt | 1 - include/interface/peripheral_interface_spi.h | 31 -------- include/peripheral_handle.h | 1 - src/interface/peripheral_interface_spi.c | 105 --------------------------- src/peripheral_spi.c | 59 ++++++++++++--- 5 files changed, 50 insertions(+), 147 deletions(-) delete mode 100644 include/interface/peripheral_interface_spi.h delete mode 100644 src/interface/peripheral_interface_spi.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e5b255c..edcfbac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,6 @@ SET(SOURCES src/peripheral_gpio.c src/interface/peripheral_interface_gpio.c src/interface/peripheral_interface_pwm.c src/interface/peripheral_interface_adc.c - src/interface/peripheral_interface_spi.c src/interface/peripheral_interface_uart.c src/gdbus/peripheral_gdbus_gpio.c src/gdbus/peripheral_gdbus_pwm.c diff --git a/include/interface/peripheral_interface_spi.h b/include/interface/peripheral_interface_spi.h deleted file mode 100644 index 7667bd2..0000000 --- a/include/interface/peripheral_interface_spi.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __PERIPHERAL_INTERFACE_SPI_H__ -#define __PERIPHERAL_INTERFACE_SPI_H__ - -#include "peripheral_interface_common.h" - -void peripheral_interface_spi_close(peripheral_spi_h spi); -int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode); -int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order); -int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits); -int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq); -int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t length); -int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_t length); -int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint8_t *rxbuf, uint32_t length); - -#endif /* __PERIPHERAL_INTERFACE_SPI_H__ */ diff --git a/include/peripheral_handle.h b/include/peripheral_handle.h index b766364..bd6567e 100644 --- a/include/peripheral_handle.h +++ b/include/peripheral_handle.h @@ -88,7 +88,6 @@ struct _peripheral_uart_s { * @brief Internal struct for spi context */ struct _peripheral_spi_s { - uint handle; int fd; }; diff --git a/src/interface/peripheral_interface_spi.c b/src/interface/peripheral_interface_spi.c deleted file mode 100644 index b38436b..0000000 --- a/src/interface/peripheral_interface_spi.c +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -#include "peripheral_interface_spi.h" - -void peripheral_interface_spi_close(peripheral_spi_h spi) -{ - close(spi->fd); -} - -int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) -{ - int ret = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order) -{ - int ret = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits) -{ - int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq) -{ - int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq); - CHECK_ERROR(ret != 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t length) -{ - int ret; - struct spi_ioc_transfer xfer; - - memset(&xfer, 0, sizeof(struct spi_ioc_transfer)); - xfer.rx_buf = (unsigned long)rxbuf; - xfer.len = length; - - ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); - CHECK_ERROR(ret < 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_t length) -{ - int ret; - struct spi_ioc_transfer xfer; - - memset(&xfer, 0, sizeof(struct spi_ioc_transfer)); - xfer.tx_buf = (unsigned long)txbuf; - xfer.len = length; - - ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); - CHECK_ERROR(ret < 0); - - return PERIPHERAL_ERROR_NONE; -} - -int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint8_t *rxbuf, uint32_t length) -{ - int ret; - struct spi_ioc_transfer xfer; - - if (!txbuf || !rxbuf) return PERIPHERAL_ERROR_INVALID_PARAMETER; - - memset(&xfer, 0, sizeof(xfer)); - xfer.tx_buf = (unsigned long)txbuf; - xfer.rx_buf = (unsigned long)rxbuf; - xfer.len = length; - - ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); - CHECK_ERROR(ret < 0); - - return PERIPHERAL_ERROR_NONE; -} diff --git a/src/peripheral_spi.c b/src/peripheral_spi.c index 24dc78e..9381d00 100644 --- a/src/peripheral_spi.c +++ b/src/peripheral_spi.c @@ -15,15 +15,16 @@ */ #include +#include #include #include +#include #include #include #include #include "peripheral_io.h" -#include "peripheral_handle.h" -#include "peripheral_interface_spi.h" +#include "peripheral_interface_common.h" #include "peripheral_log.h" #define PERIPHERAL_IO_SPI_FEATURE "http://tizen.org/feature/peripheral_io.spi" @@ -112,7 +113,10 @@ int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode) RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF((mode < PERIPHERAL_SPI_MODE_0) || (mode > PERIPHERAL_SPI_MODE_3), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi mode parameter"); - return peripheral_interface_spi_set_mode(spi, mode); + int ret = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order) @@ -121,7 +125,10 @@ int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_ RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF((bit_order < PERIPHERAL_SPI_BIT_ORDER_MSB) || (bit_order > PERIPHERAL_SPI_BIT_ORDER_LSB), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid bit order parameter"); - return peripheral_interface_spi_set_bit_order(spi, bit_order); + int ret = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits) @@ -129,7 +136,10 @@ int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits) RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_interface_spi_set_bits_per_word(spi, bits); + int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz) @@ -137,7 +147,10 @@ int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz) RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported"); RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); - return peripheral_interface_spi_set_frequency(spi, freq_hz); + int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq_hz); + CHECK_ERROR(ret != 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length) @@ -146,7 +159,16 @@ int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length) RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_spi_read(spi, data, length); + int ret; + struct spi_ioc_transfer xfer = {0, }; + + xfer.rx_buf = (unsigned long)data; + xfer.len = length; + + ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); + CHECK_ERROR(ret < 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length) @@ -155,7 +177,16 @@ int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length) RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_spi_write(spi, data, length); + int ret; + struct spi_ioc_transfer xfer = {0, }; + + xfer.tx_buf = (unsigned long)data; + xfer.len = length; + + ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); + CHECK_ERROR(ret < 0); + + return PERIPHERAL_ERROR_NONE; } int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxdata, uint32_t length) @@ -164,5 +195,15 @@ int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxda RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL"); RETVM_IF(txdata == NULL || rxdata == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter"); - return peripheral_interface_spi_transfer(spi, txdata, rxdata, length); + int ret; + struct spi_ioc_transfer xfer = {0, }; + + xfer.tx_buf = (unsigned long)txdata; + xfer.rx_buf = (unsigned long)rxdata; + xfer.len = length; + + ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer); + CHECK_ERROR(ret < 0); + + return PERIPHERAL_ERROR_NONE; } -- 2.7.4