Private
Public Access
2
0

android-things: Prep for peripheralmanager support

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2016-12-23 15:29:25 +00:00
committed by Noel Eck
parent d7fb6c5af9
commit 4c7616fe91
25 changed files with 2501 additions and 9 deletions

View File

@@ -38,7 +38,9 @@ extern "C" {
extern mraa_board_t* plat;
extern char* platform_name;
#if !defined(PERIPHERALMAN)
extern mraa_iio_info_t* plat_iio;
#endif
extern mraa_lang_func_t* lang_func;
/**

View File

@@ -25,10 +25,16 @@
#pragma once
#ifdef PERIPHERALMAN
#include <peripheralmanager/peripheral_manager_client.h>
#endif
#include "common.h"
#include "mraa.h"
#include "mraa_adv_func.h"
#if !defined(PERIPHERALMAN)
#include "iio.h"
#endif
// Bionic does not implement pthread cancellation API
#ifndef __BIONIC__
@@ -124,6 +130,9 @@ struct _gpio {
int mock_state; /**< mock state of the pin */
#endif
/*@}*/
#ifdef PERIPHERALMAN
BGpio *bgpio;
#endif
};
/**
@@ -143,6 +152,10 @@ struct _i2c {
uint8_t* mock_dev_data; /**< mock device data register block contents */
#endif
/*@}*/
#ifdef PERIPHERALMAN
BI2cDevice *bi2c;
char bus_name[256];
#endif
};
/**
@@ -157,6 +170,9 @@ struct _spi {
unsigned int bpw; /**< Bits per word */
mraa_adv_func_t* advance_func; /**< override function table */
/*@}*/
#ifdef PERIPHERALMAN
BSpiDevice *bspi;
#endif
};
/**
@@ -195,8 +211,12 @@ struct _uart {
int fd; /**< file descriptor for device. */
mraa_adv_func_t* advance_func; /**< override function table */
/*@}*/
#if defined(PERIPHERALMAN)
struct BUartDevice *buart;
#endif
};
#if !defined(PERIPHERALMAN)
/**
* A structure representing an IIO device
*/
@@ -215,6 +235,7 @@ struct _iio {
mraa_iio_event* events;
int datasize;
};
#endif
/**
* A bitfield representing the capabilities of a pin.
@@ -377,7 +398,9 @@ typedef struct _board_t {
/*@}*/
} mraa_board_t;
#if !defined(PERIPHERALMAN)
typedef struct {
struct _iio* iio_devices; /**< Pointer to IIO devices */
uint8_t iio_device_count; /**< IIO device count */
} mraa_iio_info_t;
#endif

38
include/peripheralman.h Normal file
View File

@@ -0,0 +1,38 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "mraa_internal.h"
mraa_platform_t
mraa_peripheralman_platform();
#ifdef __cplusplus
}
#endif

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.
enum {
NONE_EDGE, /**< None */
RISING_EDGE, /**< Rising edge */
FALLING_EDGE, /**< Falling edge */
BOTH_EDGE /**< Both edges */
};
/// GPIO direction types.
enum {
DIRECTION_IN, /**< Input mode */
DIRECTION_OUT_INITIALLY_HIGH, /**< Output mode, initially set to high */
DIRECTION_OUT_INITIALLY_LOW /**< Output mode, initially set to low */
};
/// Possible active types.
enum {
ACTIVE_LOW, /**< Active Low */
ACTIVE_HIGH /**< Active High */
};
typedef struct BGpio BGpio;
/// Sets the GPIO direction to output.
/// @param gpio Pointer to the BGpio struct
/// @param direction One of DIRECTION_IN,
/// DIRECTION_OUT_INITIALLY_HIGH, DIRECTION_OUT_INITIALLY_LOW.
/// @return 0 on success, errno on error.
int BGpio_setDirection(const BGpio* gpio, int direction);
/// Sets the interrupt edge trigger type.
/// @param gpio Pointer to the BGpio struct
/// @param type One of NONE_EDGE, RISING_EDGE, FALLING_EDGE or BOTH_EDGE.
/// @return 0 on success, errno on error.
int BGpio_setEdgeTriggerType(const BGpio* gpio, int type);
/// Sets the GPIOs active low/high status.
/// @param gpio Pointer to the BGpio struct.
/// @param type One of ACTIVE_HIGH, ACTIVE_LOW.
/// @return 0 on success, errno on error.
int BGpio_setActiveType(const BGpio* gpio, int type);
/// Sets the GPIO value (for output GPIO only).
/// @param gpio Pointer to the BGpio struct.
/// @param value Value to set.
/// @return 0 on success, errno on error.
int BGpio_setValue(const BGpio* gpio, int value);
/// Gets the GPIO value (for input GPIO only).
/// @param gpio Pointer to the BGpio struct.
/// @param value Output pointer to the value of the GPIO.
/// @return 0 on success, errno on error.
int BGpio_getValue(const BGpio* 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 BGpio struct.
/// @param fd Output pointer to the file descriptor number.
/// @return 0 on success, errno on error.
int BGpio_getPollingFd(const BGpio* 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 BGpio_ackInterruptEvent(int fd);
/// Destroys a BGpio struct.
/// @param gpio Pointer to the BGpio struct.
void BGpio_delete(BGpio* 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 BI2cDevice BI2cDevice;
/// Reads from the device.
/// @param device Pointer to the BI2cDevice 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 BI2cDevice_read(const BI2cDevice* device, void* data, uint32_t len);
/// Reads a byte from an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int BI2cDevice_readRegByte(const BI2cDevice* device, uint8_t reg, uint8_t* val);
/// Reads a word from an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to read from.
/// @param val Output pointer to value to read.
/// @return 0 on success, errno on error
int BI2cDevice_readRegWord(const BI2cDevice* device,
uint8_t reg,
uint16_t* val);
/// Reads from an I2C register.
/// @param device Pointer to the BI2cDevice 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 BI2cDevice_readRegBuffer(const BI2cDevice* device,
uint8_t reg,
void* data,
uint32_t len);
/// Writes to the device.
/// @param device Pointer to the BI2cDevice struct.
/// @param data Buffer to write.
/// @param len Number of bytes to write.
/// @return 0 on success, errno on error
int BI2cDevice_write(const BI2cDevice* device, const void* data, uint32_t len);
/// Writes a byte to an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int BI2cDevice_writeRegByte(const BI2cDevice* device, uint8_t reg, uint8_t val);
/// Writes a word to an I2C register.
/// @param device Pointer to the BI2cDevice struct.
/// @param reg Register to write to.
/// @param val Value to write.
/// @return 0 on success, errno on error
int BI2cDevice_writeRegWord(const BI2cDevice* device,
uint8_t reg,
uint16_t val);
/// Writes to an I2C register.
/// @param device Pointer to the BI2cDevice 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 BI2cDevice_writeRegBuffer(const BI2cDevice* device,
uint8_t reg,
const void* data,
uint32_t len);
/// Destroys a BI2cDevice struct.
/// @param device Pointer to the BI2cDevice struct.
void BI2cDevice_delete(BI2cDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_I2C_DEVICE_H_

View File

@@ -0,0 +1,145 @@
/*
* 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 "peripheralmanager/gpio.h"
#include "peripheralmanager/i2c_device.h"
#include "peripheralmanager/pwm.h"
#include "peripheralmanager/spi_device.h"
#include "peripheralmanager/uart_device.h"
__BEGIN_DECLS
/// @defgroup PeripheralManagerClient Peripheral client functions
/// @brief Functions to access embedded peripherals
/// @{
typedef struct BPeripheralManagerClient BPeripheralManagerClient;
/// 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 BPeripheralManagerClient struct.
/// @param num_gpio Output pointer to the number of elements in the list.
/// @return The list of gpios.
char** BPeripheralManagerClient_listGpio(const BPeripheralManagerClient* client,
int* num_gpio);
/// Opens a GPIO and takes ownership of it.
/// @param client Pointer to the BPeripheralManagerClient struct.
/// @param name Name of the GPIO.
/// @param gpio Output pointer to the BGpio struct. Empty on error.
/// @return 0 on success, errno on error.
int BPeripheralManagerClient_openGpio(const BPeripheralManagerClient* client,
const char* name,
BGpio** 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 BPeripheralManagerClient struct.
/// @param num_gpio Output pointer to the number of elements in the list.
/// @return The list of pwms.
char** BPeripheralManagerClient_listPwm(const BPeripheralManagerClient* client,
int* num_pwm);
/// Opens a PWM and takes ownership of it.
/// @param client Pointer to the BPeripheralManagerClient struct.
/// @param name Name of the PWM.
/// @param gpio Output pointer to the BGpio struct. Empty on error.
/// @return 0 on success, errno on error.
int BPeripheralManagerClient_openPwm(const BPeripheralManagerClient* client,
const char* name,
BPwm** 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 BPeripheralManagerClient struct.
/// @param num_spi_buses Output pointer to the number of elements in the list.
/// @return The list of spi buses.
char** BPeripheralManagerClient_listSpiBuses(
const BPeripheralManagerClient* client,
int* num_spi_buses);
/// Opens a SPI device and takes ownership of it.
/// @oaram client Pointer to the BPeripheralManagerClient struct.
/// @param name Name of the SPI device.
/// @param dev Output pointer to the BSpiDevice struct. Empty on error.
/// @return 0 on success, errno on error.
int BPeripheralManagerClient_openSpiDevice(
const BPeripheralManagerClient* client,
const char* name,
BSpiDevice** 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 BPeripheralManagerClient struct.
/// @param num_i2c_buses Output pointer to the number of elements in the list.
/// @return The list of i2c buses.
char** BPeripheralManagerClient_listI2cBuses(
const BPeripheralManagerClient* client,
int* num_i2c_buses);
/// Opens an I2C device and takes ownership of it.
/// @param client Pointer to the BPeripheralManagerClient struct.
/// @param name Name of the I2C bus.
/// @param address Address of the I2C device.
/// @param dev Output pointer to the BI2cDevice struct. Empty on error.
/// @return 0 on success, errno on error
int BPeripheralManagerClient_openI2cDevice(
const BPeripheralManagerClient* client,
const char* name,
uint32_t address,
BI2cDevice** 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 BPeripheralManagerClient struct.
/// @param num_uart_buses Output pointer to the number of elements in the list.
/// @return The list of uart buses.
char** BPeripheralManagerClient_listUartDevices(
const BPeripheralManagerClient* client, int* num_uart_buses);
/// Opens an UART device and takes ownership of it.
/// @param client Pointer to the BPeripheralManagerClient struct.
/// @param name Name of the UART device.
/// @param dev Output pointer to the BUartDevice struct. Empty on error.
/// @return 0 on success, errno on error
int BPeripheralManagerClient_openUartDevice(
const BPeripheralManagerClient* client,
const char* name,
BUartDevice** dev);
/// Creates a new client.
/// @return A pointer to the created client. nullptr on errors.
BPeripheralManagerClient* BPeripheralManagerClient_new();
/// Destroys the peripheral manager client.
/// @param client Pointer to the BPeripheralManagerClient struct.
void BPeripheralManagerClient_delete(BPeripheralManagerClient* client);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_PERIPHERAL_MANAGER_CLIENT_H_

View File

@@ -0,0 +1,63 @@
/*
* 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 BPwm BPwm;
/// Sets the PWM duty cycle.
/// @param gpio Pointer to the BPwm struct
/// @param duty_cycle Double between 0 and 100 inclusive.
/// @return 0 on success, errno on error.
int BPwm_setDutyCycle(const BPwm* pwm, double duty_cycle);
/// Sets the PWM frequency.
/// @param gpio Pointer to the BPwm struct
/// @param freq Double denoting the frequency in Hz.
/// @return 0 on success, errno on error.
int BPwm_setFrequencyHz(const BPwm* pwm, double frequency);
/// Enables the PWM.
/// @param gpio Pointer to the BPwm struct
/// @return 0 on success, errno on error.
int BPwm_enable(const BPwm* pwm);
/// Disables the PWM.
/// @param gpio Pointer to the BPwm struct
/// @return 0 on success, errno on error.
int BPwm_disable(const BPwm* pwm);
/// Destroys a BPwm struct.
/// @param pwm Pointer to the BPwm struct.
void BPwm_delete(BPwm* pwm);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_PWM_H_

View File

@@ -0,0 +1,127 @@
/*
* 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.
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 buffer to the device.
/// @param device Pointer to the BSpiDevice struct.
/// @param data Buffer to write.
/// @param len Length of the buffer.
/// @return 0 on success, errno on error.
int BSpiDevice_writeBuffer(const BSpiDevice* device,
const void* data,
size_t len);
/// Reads a buffer from the device.
/// @param device Pointer to the BSpiDevice struct.
/// @param data Buffer to read into.
/// @param len Length of the buffer.
/// @return 0 on success, errno on error.
int BSpiDevice_readBuffer(const BSpiDevice* device,
void* data,
size_t len);
/// Transfer data to the device.
/// @param device Pointer to the BSpiDevice 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 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 0 on success, errno on error.
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 0 on success, errno on error.
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 0 on success, errno on error.
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 0 on success, errno on error.
int BSpiDevice_setBitsPerWord(const BSpiDevice* device, uint8_t bits_per_word);
/// Sets the delay to wait after each transfer.
/// @param device Pointer to the BSpiDevice struct.
/// @param delay_usecs Delay in microseconds.
/// @return 0 on success, errno on error.
int BSpiDevice_setDelay(const BSpiDevice* device, uint16_t delay_usecs);
/// Sets the chip select behavior after each transfer.
/// @param device Pointer to the BSpiDevice struct.
/// @param change If set, cs will be active between transfers.
/// @return 0 on success, errno on error.
int BSpiDevice_setCsChange(const BSpiDevice* device, int change);
/// Destroys a BSpiDevice struct.
/// @param device Pointer to the BSpiDevice struct.
void BSpiDevice_delete(BSpiDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_SPI_DEVICE_H_

View File

@@ -0,0 +1,183 @@
/*
* 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>
#include <termios.h>
#include <unistd.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
enum UartParity {
UART_PARITY_NONE, /**< No parity */
UART_PARITY_EVEN, /**< Even parity */
UART_PARITY_ODD, /**< Odd parity */
UART_PARITY_MARK, /**< Mark parity, always 1 */
UART_PARITY_SPACE /**< Space parity, always 0 */
};
/// Modem control Bits
enum UartModemControlBits {
UART_MC_LE = TIOCM_LE, /**< Data set ready/Line enable */
UART_MC_DTR = TIOCM_DTR, /**< Data terminal ready */
UART_MC_RTS = TIOCM_RTS, /**< Request to send */
UART_MC_ST = TIOCM_ST, /**< Secondary TXD */
UART_MC_SR = TIOCM_SR, /**< Secondary RXD */
UART_MC_CTS = TIOCM_CTS, /**< Clear to send */
UART_MC_CD = TIOCM_CAR, /**< Data carrier detect */
UART_MC_RI = TIOCM_RI, /**< Ring */
UART_MC_DSR = TIOCM_DSR /**< Data set ready */
};
// Hardware Flow Control
enum UartHardwareFlowControlType {
UART_HW_FLOW_NONE, /**< No hardware flow control */
UART_HW_FLOW_AUTO_RTSCTS /**< Auto RTS/CTS */
};
/// Flush queue selection
enum UartFlushQueueSelection {
UART_FLUSH_IN = TCIFLUSH, /**< Flushes data received but not read */
UART_FLUSH_OUT = TCOFLUSH, /**< Flushes data written but not transmitted */
UART_FLUSH_IN_OUT = TCIOFLUSH /**< Flushes both in and out */
};
typedef struct BUartDevice BUartDevice;
/// Writes to a UART device.
/// @param device Pointer to the BUartDevice 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 BUartDevice_write(const BUartDevice* device,
const void* data,
uint32_t len,
uint32_t* bytes_written);
/// Reads from a UART device.
/// @param device Pointer to the BUartDevice 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 BUartDevice_read(const BUartDevice* 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 BUartDevice struct.
/// @param baudrate Speed in baud.
/// @return 0 on success, errno on error.
int BUartDevice_setBaudrate(const BUartDevice* device, uint32_t baudrate);
/// Sets number of stop bits for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param stop_bits Number of stop bits. Typically 1 or 2.
/// @return 0 on success, errno on error.
int BUartDevice_setStopBits(const BUartDevice* device, uint32_t stop_bits);
/// Sets the data size of a character for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param data_size Number of bits per character. Typically between 5 and 8.
/// @return 0 on success, errno on error.
int BUartDevice_setDataSize(const BUartDevice* device, uint32_t data_size);
/// Sets the parity mode for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param mode Parity mode. One of UART_PARITY_NONE, UART_PARITY_EVEN,
/// UART_PARITY_ODD, UART_PARITY_MARK, UART_PARITY_SPACE.
/// @return 0 on success, errno on error.
int BUartDevice_setParity(const BUartDevice* device, uint32_t mode);
/// Sets the hardware flow control mode for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param mode Flow control mode. Either UART_HW_FLOW_NONE or
/// UART_HW_FLOW_AUTO_RTSCTS.
/// @return 0 on success, errno on error.
int BUartDevice_setHardwareFlowControl(const BUartDevice* device,
uint32_t mode);
/// Sets the modem control bits for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param bits Modem control bits to set.
/// @return 0 on success, errno on error.
int BUartDevice_setModemControl(const BUartDevice* device, uint32_t bits);
/// Clears the modem control bits for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param bits Modem control bits to clear.
/// @return 0 on success, errno on error.
int BUartDevice_clearModemControl(const BUartDevice* device, uint32_t bits);
/// Sends a break to the UART device.
/// @param device Pointer to the BUartDevice 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 BUartDevice_sendBreak(const BUartDevice* device, uint32_t duration_msecs);
/// Flushes specified queue for the UART device.
/// @param device Pointer to the BUartDevice struct.
/// @param queue Queue to flush. One of UART_FLUSH_IN, UART_FLUSH_OUT,
/// UART_FLUSH_IN_OUT.
/// @return 0 on success, errno on error.
int BUartDevice_flush(const BUartDevice* device, uint32_t queue);
/// 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 BUartDevice struct.
/// @param fd Output pointer to the file descriptor.
/// @return 0 on success, errno on error.
int BUartDevice_getPollingFd(const BUartDevice* 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 BUartDevice_ackInputEvent(int fd);
/// Destroys a BUartDevice struct.
/// @param device Pointer to the BUartDevice struct.
void BUartDevice_delete(BUartDevice* device);
/// @}
__END_DECLS
#endif // SYSTEM_PERIPHERALMANAGER_UART_DEVICE_H_