From c46c8c208575510d2d227d40bdd0efa5fcbb5b42 Mon Sep 17 00:00:00 2001 From: Bertrand SIMONNET Date: Mon, 7 Mar 2016 14:28:38 -0800 Subject: Expose SPI in the developer interface. This CL defines and implements the C API used by the developer to access the SPI interface. Bug: 27530022 Change-Id: Id3380191fa8d81ff749e3db9d208dff25deed3fb --- .../peripheralmanager/peripheral_manager_client.h | 4 + include/peripheralmanager/spi_device.h | 112 +++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 include/peripheralmanager/spi_device.h (limited to 'include') diff --git a/include/peripheralmanager/peripheral_manager_client.h b/include/peripheralmanager/peripheral_manager_client.h index 48273ca..b394fb8 100644 --- a/include/peripheralmanager/peripheral_manager_client.h +++ b/include/peripheralmanager/peripheral_manager_client.h @@ -21,6 +21,7 @@ #include "peripheralmanager/errors.h" #include "peripheralmanager/gpio.h" +#include "peripheralmanager/spi_device.h" __BEGIN_DECLS @@ -66,6 +67,9 @@ int BPeripheralManagerClient_openGpio(const BPeripheralManagerClient* client, const char* name, BGpio** gpio); +int BPeripheralManagerClient_openSpiDevice( + const BPeripheralManagerClient* client, const char* name, BSpiDevice** dev); + // Creates a new client. // // Returns: diff --git a/include/peripheralmanager/spi_device.h b/include/peripheralmanager/spi_device.h new file mode 100644 index 0000000..3c65d28 --- /dev/null +++ b/include/peripheralmanager/spi_device.h @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_ +#define SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_ + +#include +#include + +__BEGIN_DECLS + +/// @defgroup Spi Spi device interface +/// @brief Functions to control an SPI device. +/// +/// These functions can be used to control an SPI device. +/// @{ + +/// Endianness. +enum { + SPI_LSB_FIRST, /**< Least significant bits first */ + SPI_MSB_FIRST /**< Most significant bits first */ +}; + +/// SPI phase modes. +enum { + SPI_CPHA = 0x01, /**< Clock phase */ + SPI_CPOL = 0x02 /**< Clock polarity */ +}; + +/// SPI modes (similar to the Linux kernel's modes). +enum { + SPI_MODE0 = 0, /**< CPHA=0, CPOL=0 */ + SPI_MODE1 = SPI_CPHA, /**< CPHA=1, CPOL=0 */ + SPI_MODE2 = SPI_CPOL, /**< CPHA=0, CPOL=1 */ + SPI_MODE3 = SPI_CPHA + SPI_CPOL /**< CPHA=1, CPOL=1 */ +}; + +typedef struct BSpiDevice BSpiDevice; + +/// Writes a byte to the device. +/// @param device Pointer to the BSpiDevice struct. +/// @param val Value to write. +/// @return Error code. +int BSpiDevice_writeByte(const BSpiDevice* device, uint8_t val); + +/// Writes a buffer to the device. +/// @param device Pointer to the BSpiDevice struct. +/// @param data Buffer to write. +/// @param len Length of the buffer. +/// @return Error code. +int BSpiDevice_writeBuffer(const BSpiDevice* device, + const void* data, + size_t len); + +/// Transfer data to the device. +/// @param device Pointer to the BSpiDevice struct. +/// @param tx_data Buffer to write. If NULL, no data will be written. +/// @param rx_data Buffer to read data in. If NULL, no data will be read. +/// @param len Length of the buffers. +/// @return Error code. +int BSpiDevice_transfer(const BSpiDevice* device, + const void* tx_data, + void* rx_data, + size_t len); + +/// Sets the frequency in Hertz. +/// @param device Pointer to the BSpiDevice struct. +/// @param freq_hz Frequency to set. +/// @return Error code. +int BSpiDevice_setFrequency(const BSpiDevice* device, uint32_t freq_hz); + +/// Sets the SPI mode. +/// @param device Pointer to the BSpiDevice struct. +/// @param mode Mode to use. One of SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3. +/// @return Error code. +int BSpiDevice_setMode(const BSpiDevice* device, int mode); + +/// Sets the bit justification. +/// @param device Pointer to the BSpiDevice struct. +/// @param bit_justification One of SPI_LSB_FIRST OR SPI_MSB_FIRST. +/// @return Error code. +int BSpiDevice_setBitJustification(const BSpiDevice* device, + int bit_justification); + +/// Sets the number of bits per words. +/// @param device Pointer to the BSpiDevice struct. +/// @param bits_per_word Number of bits per word. +/// @return Error code. +int BSpiDevice_setBitsPerWord(const BSpiDevice* device, uint32_t bits_per_word); + +/// Destroys a BSpiDevice struct. +/// @param device Pointer to the BSpiDevice struct. +void BSpiDevice_delete(BSpiDevice* device); + +/// @} + +__END_DECLS + +#endif // SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_ -- cgit v1.2.3