modified example accordingto latest version of irext-core

This commit is contained in:
2017-12-03 16:45:37 +08:00
parent b4e25a9967
commit e86bede45f
48 changed files with 2479 additions and 349 deletions

View File

@@ -0,0 +1,107 @@
/*
* 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_GPIO_H_
#define SYSTEM_PERIPHERALMANAGER_GPIO_H_
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/// @defgroup Gpio Gpio Interface
/// @brief Functions to control GPIO pins.
///
/// These functions can be used to control GPIO.
/// @{
/// Edge trigger types.
typedef enum AGpioEdge {
AGPIO_EDGE_NONE = 0, /**< None */
AGPIO_EDGE_RISING = 1, /**< Rising edge */
AGPIO_EDGE_FALLING = 2, /**< Falling edge */
AGPIO_EDGE_BOTH = 3 /**< Both edges */
} AGpioEdge;
/// GPIO direction types.
typedef enum AGpioDirection {
AGPIO_DIRECTION_IN = 0, /**< Input mode */
AGPIO_DIRECTION_OUT_INITIALLY_HIGH = 1, /**< Output mode, initially high */
AGPIO_DIRECTION_OUT_INITIALLY_LOW = 2 /**< Output mode, initially low */
} AGpioDirection;
/// Possible active types.
typedef enum AGpioActiveType {
AGPIO_ACTIVE_LOW = 0, /**< Active Low */
AGPIO_ACTIVE_HIGH = 1 /**< Active High */
} AGpioActiveType;
typedef struct AGpio AGpio;
/// Sets the GPIO direction to output.
/// @param gpio Pointer to the AGpio struct
/// @param direction One of DIRECTION_IN,
/// DIRECTION_OUT_INITIALLY_HIGH, DIRECTION_OUT_INITIALLY_LOW.
/// @return 0 on success, errno on error.
int AGpio_setDirection(const AGpio* gpio, AGpioDirection direction);
/// Sets the interrupt edge trigger type.
/// @param gpio Pointer to the AGpio struct
/// @param type One of NONE_EDGE, RISING_EDGE, FALLING_EDGE or BOTH_EDGE.
/// @return 0 on success, errno on error.
int AGpio_setEdgeTriggerType(const AGpio* gpio, AGpioEdge type);
/// Sets the GPIOs active low/high status.
/// @param gpio Pointer to the AGpio struct.
/// @param type One of ACTIVE_HIGH, ACTIVE_LOW.
/// @return 0 on success, errno on error.
int AGpio_setActiveType(const AGpio* gpio, AGpioActiveType type);
/// Sets the GPIO value (for output GPIO only).
/// @param gpio Pointer to the AGpio struct.
/// @param value Value to set.
/// @return 0 on success, errno on error.
int AGpio_setValue(const AGpio* gpio, int value);
/// Gets the GPIO value (for input GPIO only).
/// @param gpio Pointer to the AGpio struct.
/// @param value Output pointer to the value of the GPIO.
/// @return 0 on success, errno on error.
int AGpio_getValue(const AGpio* gpio, int* value);
/// Returns a file descriptor that can be used to poll on new data.
/// Can be passed to select/epoll to wait for data to become available.
/// @param gpio Pointer to the AGpio struct.
/// @param fd Output pointer to the file descriptor number.
/// @return 0 on success, errno on error.
int AGpio_getPollingFd(const AGpio* gpio, int* fd);
/// Acknowledges the interrupt and resets the file descriptor.
/// This must be called after each event triggers in order to be able to
/// poll/select for another event.
/// @param fd Polling file descriptor to reset.
/// @return 0 on success, errno on error.
int AGpio_ackInterruptEvent(int fd);
/// Destroys a AGpio struct.
/// @param gpio Pointer to the AGpio struct.
void AGpio_delete(AGpio* gpio);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_GPIO_H_

View File

@@ -0,0 +1,109 @@
/*
* 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_I2C_DEVICE_H_
#define SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/// @defgroup I2c I2c device interface
/// @brief Functions to control an I2C device.
///
/// These functions can be used to control an I2C device.
/// @{
typedef struct AI2cDevice AI2cDevice;
/// Reads from the device.
/// @param device Pointer to the AI2cDevice struct.
/// @param data Output buffer to write the data to.
/// @param len Number of bytes to read.
/// @return 0 on success, errno on error
int AI2cDevice_read(const AI2cDevice* device, void* data, uint32_t len);
/// Reads a byte from an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int AI2cDevice_readRegByte(const AI2cDevice* device, uint8_t reg, uint8_t* val);
/// Reads a word from an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int AI2cDevice_readRegWord(const AI2cDevice* device,
uint8_t reg,
uint16_t* val);
/// Reads from an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to read from.
/// @param data Output buffer to write the data to.
/// @param len Number of bytes to read.
/// @return 0 on success, errno on error
int AI2cDevice_readRegBuffer(const AI2cDevice* device,
uint8_t reg,
void* data,
uint32_t len);
/// Writes to the device.
/// @param device Pointer to the AI2cDevice struct.
/// @param data Buffer to write.
/// @param len Number of bytes to write.
/// @return 0 on success, errno on error
int AI2cDevice_write(const AI2cDevice* device, const void* data, uint32_t len);
/// Writes a byte to an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int AI2cDevice_writeRegByte(const AI2cDevice* device, uint8_t reg, uint8_t val);
/// Writes a word to an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int AI2cDevice_writeRegWord(const AI2cDevice* device,
uint8_t reg,
uint16_t val);
/// Writes to an I2C register.
/// @param device Pointer to the AI2cDevice struct.
/// @param reg Register to write to.
/// @param data Data to write.
/// @param len Number of bytes to write.
/// @return 0 on success, errno on error
int AI2cDevice_writeRegBuffer(const AI2cDevice* device,
uint8_t reg,
const void* data,
uint32_t len);
/// Destroys a AI2cDevice struct.
/// @param device Pointer to the AI2cDevice struct.
void AI2cDevice_delete(AI2cDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2017 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_I2S_H_
#define SYSTEM_PERIPHERALMANAGER_I2S_H_
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/// @defgroup I2s I2s Interface
/// @brief Functions to control I2S pins.
///
/// These functions can be used to control I2S.
/// @{
/// Possible encodings
typedef enum AI2sEncoding {
AI2S_ENCODING_PCM_8_BIT,
AI2S_ENCODING_PCM_16_BIT,
AI2S_ENCODING_PCM_24_BIT,
AI2S_ENCODING_PCM_32_BIT
} AI2sEncoding;
/// Flags to specify I2s bus direction.
typedef enum AI2sFlags {
AI2S_FLAG_DIRECTION_IN = 1 << 0,
AI2S_FLAG_DIRECTION_OUT = 1 << 1
} AI2sFlags;
typedef struct AI2sDevice AI2sDevice;
/// Writes raw data to the I2S device. Multi-channel audio data is interleaved.
/// @param i2s Pointer to the AI2s struct.
/// @param data Data to write.
/// @param offset Offset to first byte in data.
/// @param size Number of bytes to write.
/// @param bytes_written Number of bytes written.
/// @return 0 on success, errno on error.
int AI2sDevice_write(const AI2sDevice* i2s,
const void* data,
int offset,
int size,
int* bytes_written);
/// Reads raw data from the I2S device. Multi-channel audio data is interleaved.
/// @param i2s Pointer to the AI2s struct.
/// @param data Buffer to fill with data read.
/// @param offset Offset to first byte in data.
/// @param size Number of bytes to read.
/// @param bytes_read Number of bytes read.
/// @return 0 on success, errno on error.
int AI2sDevice_read(
const AI2sDevice* i2s, void* data, int offset, int size, int* bytes_read);
/// Gets the timestamp when a specific sample entered the kernel.
/// @param i2s Pointer to the AI2s struct.
/// @param frame_position Output indicating number of frames read.
/// @param nano_time Output indicating time (ns) when the frame was read.
/// @param success Output indicating success (1) or failure (0).
/// @return 0 on success, errno on error. This will only be nonzero on a fatal
/// error such as the I2S device couldn't be found; in the normal case
/// that a timestamp isn't available the success param will be used.
int AI2sDevice_getInputTimestamp(const AI2sDevice* i2s,
int64_t* frame_position,
int64_t* nano_time,
int* success);
/// Gets the timestamp when a specific sample exited the kernel.
/// @param i2s Pointer to the AI2s struct.
/// @param frame_position Output indicating number of frames written.
/// @param nano_time Output indicating time (ns) when the frame was written.
/// @param success Output indicating success (1) or failure (0).
/// @return 0 on success, errno on error. This will only be nonzero on a fatal
/// error such as the I2S device couldn't be found; in the normal case
/// that a timestamp isn't available the success param will be used.
int AI2sDevice_getOutputTimestamp(const AI2sDevice* i2s,
int64_t* frame_position,
int64_t* nano_time,
int* success);
/// Destroys an AI2s struct.
/// @param i2s Pointer to the AI2s struct.
void AI2sDevice_delete(AI2sDevice* i2s);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_I2S_H_

View File

@@ -0,0 +1,169 @@
/*
* Copyright (C) 2015 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_PERIPHERAL_MANAGER_CLIENT_H_
#define SYSTEM_PERIPHERALMANAGER_PERIPHERAL_MANAGER_CLIENT_H_
#include <sys/cdefs.h>
#include "gpio.h"
#include "i2c_device.h"
#include "i2s_device.h"
#include "pwm.h"
#include "spi_device.h"
#include "uart_device.h"
__BEGIN_DECLS
/// @defgroup PeripheralManagerClient Peripheral client functions
/// @brief Functions to access embedded peripherals
/// @{
typedef struct APeripheralManagerClient APeripheralManagerClient;
/// Returns the list of GPIOs.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_gpio Output pointer to the number of elements in the list.
/// @return The list of gpios.
char** APeripheralManagerClient_listGpio(const APeripheralManagerClient* client,
int* num_gpio);
/// Opens a GPIO and takes ownership of it.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the GPIO.
/// @param gpio Output pointer to the AGpio struct. Empty on error.
/// @return 0 on success, errno on error.
int APeripheralManagerClient_openGpio(const APeripheralManagerClient* client,
const char* name,
AGpio** gpio);
/// Returns the list of PWMs.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_gpio Output pointer to the number of elements in the list.
/// @return The list of pwms.
char** APeripheralManagerClient_listPwm(const APeripheralManagerClient* client,
int* num_pwm);
/// Opens a PWM and takes ownership of it.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the PWM.
/// @param gpio Output pointer to the AGpio struct. Empty on error.
/// @return 0 on success, errno on error.
int APeripheralManagerClient_openPwm(const APeripheralManagerClient* client,
const char* name,
APwm** pwm);
/// Returns the list of SPI buses.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_spi_buses Output pointer to the number of elements in the list.
/// @return The list of spi buses.
char** APeripheralManagerClient_listSpiBuses(
const APeripheralManagerClient* client, int* num_spi_buses);
/// Opens a SPI device and takes ownership of it.
/// @oaram client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the SPI device.
/// @param dev Output pointer to the ASpiDevice struct. Empty on error.
/// @return 0 on success, errno on error.
int APeripheralManagerClient_openSpiDevice(
const APeripheralManagerClient* client, const char* name, ASpiDevice** dev);
/// Returns the list of I2C buses.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_i2c_buses Output pointer to the number of elements in the list.
/// @return The list of i2c buses.
char** APeripheralManagerClient_listI2cBuses(
const APeripheralManagerClient* client, int* num_i2c_buses);
/// Opens an I2C device and takes ownership of it.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the I2C bus.
/// @param address Address of the I2C device.
/// @param dev Output pointer to the AI2cDevice struct. Empty on error.
/// @return 0 on success, errno on error
int APeripheralManagerClient_openI2cDevice(
const APeripheralManagerClient* client,
const char* name,
uint32_t address,
AI2cDevice** dev);
/// Returns the list of UART buses.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_uart_buses Output pointer to the number of elements in the list.
/// @return The list of uart buses.
char** APeripheralManagerClient_listUartDevices(
const APeripheralManagerClient* client, int* num_uart_buses);
/// Opens an UART device and takes ownership of it.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the UART device.
/// @param dev Output pointer to the AUartDevice struct. Empty on error.
/// @return 0 on success, errno on error
int APeripheralManagerClient_openUartDevice(
const APeripheralManagerClient* client,
const char* name,
AUartDevice** dev);
/// Returns the list of I2S buses.
/// This does not take ownership into account.
/// The list must be freed by the caller.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param num_i2s_buses Output pointer to the number of elements in the list.
/// @return The list of I2S buses.
char** APeripheralManagerClient_listI2sDevices(
const APeripheralManagerClient* client, int* num_i2s_buses);
/// Opens an I2S device and takes ownership of it.
/// @param client Pointer to the APeripheralManagerClient struct.
/// @param name Name of the I2S device.
/// @param encoding Device pcm encoding.
/// @param channels Number of channels.
/// @param rate Device rate in Hz.
/// @param flags Specify device supporting input, output or both.
/// @param dev Output pointer to the AI2sDevice struct. Empty on error.
/// @return 0 on success, errno on error
int APeripheralManagerClient_openI2sDevice(
const APeripheralManagerClient* client,
const char* name,
AI2sEncoding encoding,
int channels,
int rate,
int flags,
AI2sDevice** dev);
/// Creates a new client.
/// @return A pointer to the created client. nullptr on errors.
APeripheralManagerClient* APeripheralManagerClient_new();
/// Destroys the peripheral manager client.
/// @param client Pointer to the APeripheralManagerClient struct.
void APeripheralManagerClient_delete(APeripheralManagerClient* client);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_PERIPHERAL_MANAGER_CLIENT_H_

View File

@@ -0,0 +1,59 @@
/*
* 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_PWM_H_
#define SYSTEM_PERIPHERALMANAGER_PWM_H_
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/// @defgroup Pwm Pwm Interface
/// @brief Functions to control PWM pins.
///
/// These functions can be used to control PWM.
/// @{
typedef struct APwm APwm;
/// Sets the PWM duty cycle.
/// @param gpio Pointer to the APwm struct.
/// @param duty_cycle Double between 0 and 100 inclusive.
/// @return 0 on success, errno on error.
int APwm_setDutyCycle(const APwm* pwm, double duty_cycle);
/// Sets the PWM frequency.
/// @param gpio Pointer to the APwm struct.
/// @param freq Double denoting the frequency in Hz.
/// @return 0 on success, errno on error.
int APwm_setFrequencyHz(const APwm* pwm, double frequency);
/// Enables the PWM.
/// @param gpio Pointer to the APwm struct.
/// @param enabled Non-zero to enable.
/// @return 0 on success, errno on error.
int APwm_setEnabled(const APwm* pwm, int enabled);
/// Destroys a APwm struct.
/// @param pwm Pointer to the APwm struct.
void APwm_delete(APwm* pwm);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_PWM_H_

View File

@@ -0,0 +1,119 @@
/*
* 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 <sys/cdefs.h>
#include <sys/types.h>
__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.
typedef enum ASpiBitJustification {
ASPI_LSB_FIRST = 0, /**< Least significant bits first */
ASPI_MSB_FIRST = 1 /**< Most significant bits first */
} ASpiBitJustification;
/// SPI modes (similar to the Linux kernel's modes).
typedef enum ASpiMode {
ASPI_MODE0 = 0, /**< CPHA=0, CPOL=0 */
ASPI_MODE1 = 1, /**< CPHA=1, CPOL=0 */
ASPI_MODE2 = 2, /**< CPHA=0, CPOL=1 */
ASPI_MODE3 = 3 /**< CPHA=1, CPOL=1 */
} ASpiMode;
typedef struct ASpiDevice ASpiDevice;
/// Writes a buffer to the device.
/// @param device Pointer to the ASpiDevice struct.
/// @param data Buffer to write.
/// @param len Length of the buffer.
/// @return 0 on success, errno on error.
int ASpiDevice_writeBuffer(const ASpiDevice* device,
const void* data,
size_t len);
/// Reads a buffer from the device.
/// @param device Pointer to the ASpiDevice struct.
/// @param data Buffer to read into.
/// @param len Length of the buffer.
/// @return 0 on success, errno on error.
int ASpiDevice_readBuffer(const ASpiDevice* device, void* data, size_t len);
/// Transfer data to the device.
/// @param device Pointer to the ASpiDevice struct.
/// @param tx_data Buffer to write.
/// @param rx_data Buffer to read data in. If NULL, no data will be read.
/// @param len Length of the buffers.
/// @return 0 on success, errno on error.
int ASpiDevice_transfer(const ASpiDevice* device,
const void* tx_data,
void* rx_data,
size_t len);
/// Sets the frequency in Hertz.
/// @param device Pointer to the ASpiDevice struct.
/// @param freq_hz Frequency to set.
/// @return 0 on success, errno on error.
int ASpiDevice_setFrequency(const ASpiDevice* device, uint32_t freq_hz);
/// Sets the SPI mode.
/// @param device Pointer to the ASpiDevice struct.
/// @param mode Mode to use. One of SPI_MODE0, SPI_MODE1, SPI_MODE2, SPI_MODE3.
/// @return 0 on success, errno on error.
int ASpiDevice_setMode(const ASpiDevice* device, ASpiMode mode);
/// Sets the bit justification.
/// @param device Pointer to the ASpiDevice struct.
/// @param bit_justification One of SPI_LSB_FIRST OR SPI_MSB_FIRST.
/// @return 0 on success, errno on error.
int ASpiDevice_setBitJustification(const ASpiDevice* device,
ASpiBitJustification bit_justification);
/// Sets the number of bits per words.
/// @param device Pointer to the ASpiDevice struct.
/// @param bits_per_word Number of bits per word.
/// @return 0 on success, errno on error.
int ASpiDevice_setBitsPerWord(const ASpiDevice* device, uint8_t bits_per_word);
/// Sets the delay to wait after each transfer.
/// @param device Pointer to the ASpiDevice struct.
/// @param delay_usecs Delay in microseconds.
/// @return 0 on success, errno on error.
int ASpiDevice_setDelay(const ASpiDevice* device, uint16_t delay_usecs);
/// Sets the chip select behavior after each transfer.
/// @param device Pointer to the ASpiDevice struct.
/// @param change If set, cs will be active between transfers.
/// @return 0 on success, errno on error.
int ASpiDevice_setCsChange(const ASpiDevice* device, int change);
/// Destroys a ASpiDevice struct.
/// @param device Pointer to the ASpiDevice struct.
void ASpiDevice_delete(ASpiDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_

View File

@@ -0,0 +1,178 @@
/*
* 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_UART_DEVICE_H_
#define SYSTEM_PERIPHERALMANAGER_UART_DEVICE_H_
#include <sys/cdefs.h>
#include <sys/types.h>
__BEGIN_DECLS
/// @defgroup Uart Uart device interface
/// @brief Functions to control an UART device.
///
/// These functions can be used to control an UART device.
/// @{
/// UART Parity
typedef enum AUartParity {
AUART_PARITY_NONE = 0, /**< No parity */
AUART_PARITY_EVEN = 1, /**< Even parity */
AUART_PARITY_ODD = 2, /**< Odd parity */
AUART_PARITY_MARK = 3, /**< Mark parity, always 1 */
AUART_PARITY_SPACE = 4 /**< Space parity, always 0 */
} AUartParity;
/// Modem control lines.
typedef enum AUartModemControlLine {
AUART_MODEM_CONTROL_LE = 1 << 0, /**< Data set ready/Line enable */
AUART_MODEM_CONTROL_DTR = 1 << 1, /**< Data terminal ready */
AUART_MODEM_CONTROL_RTS = 1 << 2, /**< Request to send */
AUART_MODEM_CONTROL_ST = 1 << 3, /**< Secondary TXD */
AUART_MODEM_CONTROL_SR = 1 << 4, /**< Secondary RXD */
AUART_MODEM_CONTROL_CTS = 1 << 5, /**< Clear to send */
AUART_MODEM_CONTROL_CD = 1 << 6, /**< Data carrier detect */
AUART_MODEM_CONTROL_RI = 1 << 7, /**< Ring */
AUART_MODEM_CONTROL_DSR = 1 << 8 /**< Data set ready */
} AUartModemControlLine;
// Hardware Flow Control
typedef enum AUartHardwareFlowControl {
AUART_HARDWARE_FLOW_CONTROL_NONE = 0, /**< No hardware flow control */
AUART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS = 1 /**< Auto RTS/CTS */
} AUartHardwareFlowControl;
/// Flush queue selection
typedef enum AUartFlushDirection {
AUART_FLUSH_IN = 0, /**< Flushes data received but not read */
AUART_FLUSH_OUT = 1, /**< Flushes data written but not transmitted */
AUART_FLUSH_IN_OUT = 2 /**< Flushes both in and out */
} AUartFlushDirection;
typedef struct AUartDevice AUartDevice;
/// Writes to a UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param data Data to write.
/// @param len Size of the data to write.
/// @param bytes_written Output pointer to the number of bytes written.
/// @return 0 on success, errno on error.
int AUartDevice_write(const AUartDevice* device,
const void* data,
uint32_t len,
uint32_t* bytes_written);
/// Reads from a UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param data Buffer to read the data into.
/// @param len Number of bytes to read.
/// @param bytes_read Output pointer to the number of bytes read.
/// @return 0 on success, errno on error.
int AUartDevice_read(const AUartDevice* device,
void* data,
uint32_t len,
uint32_t* bytes_read);
/// Sets the input and output speed of a UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param baudrate Speed in baud.
/// @return 0 on success, errno on error.
int AUartDevice_setBaudrate(const AUartDevice* device, uint32_t baudrate);
/// Sets number of stop bits for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param stop_bits Number of stop bits. Typically 1 or 2.
/// @return 0 on success, errno on error.
int AUartDevice_setStopBits(const AUartDevice* device, uint32_t stop_bits);
/// Sets the data size of a character for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param data_size Number of bits per character. Typically between 5 and 8.
/// @return 0 on success, errno on error.
int AUartDevice_setDataSize(const AUartDevice* device, uint32_t data_size);
/// Sets the parity mode for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param mode Parity mode.
/// @return 0 on success, errno on error.
int AUartDevice_setParity(const AUartDevice* device, AUartParity mode);
/// Sets the hardware flow control mode for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param mode Flow control mode.
/// @return 0 on success, errno on error.
int AUartDevice_setHardwareFlowControl(const AUartDevice* device,
AUartHardwareFlowControl mode);
/// Sets the modem control bits for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param lines Lines to set. AUartModemControlLine values OR'ed together.
/// @return 0 on success, errno on error.
int AUartDevice_setModemControl(const AUartDevice* device, uint32_t lines);
/// Clears the modem control bits for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param lines Lines to clear. AUartModemControlLine values OR'ed together.
/// @return 0 on success, errno on error.
int AUartDevice_clearModemControl(const AUartDevice* device, uint32_t lines);
/// Sends a break to the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param duration Duration of break transmission in milliseconds. If 0,
/// transmits zero-valued bits for at least 0.25 seconds, and not more
/// than 0.5 seconds.
/// @return 0 on success, errno on error.
int AUartDevice_sendBreak(const AUartDevice* device, uint32_t duration_msecs);
/// Flushes specified queue for the UART device.
/// @param device Pointer to the AUartDevice struct.
/// @param direction Direction to flush.
/// @return 0 on success, errno on error.
int AUartDevice_flush(const AUartDevice* device, AUartFlushDirection direction);
/// Gets a file descriptor to be notified when data can be read.
///
/// You can use this file descriptor to poll on incoming data instead of
/// actively reading for new data.
///
/// @param device Pointer to the AUartDevice struct.
/// @param fd Output pointer to the file descriptor.
/// @return 0 on success, errno on error.
int AUartDevice_getPollingFd(const AUartDevice* device, int* fd);
/// Acknowledges an input event.
///
/// This must be called after receiving an event notification on the polling
/// file descriptor.
/// If you don't acknowledge an event, peripheral manager will assume you are
/// still processing it and you will not receive any more events.
/// If you acknowledge an event before reading the data from the device, you
/// will receive an event immediately as there will still be data available.
///
/// @param fd File descriptor to acknowledge the event on.
/// @return 0 on success, errno on error.
int AUartDevice_ackInputEvent(int fd);
/// Destroys a AUartDevice struct.
/// @param device Pointer to the AUartDevice struct.
void AUartDevice_delete(AUartDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_UART_DEVICE_H_