Private
Public Access
2
0

maa: add global maa.h and maa.hpp and stop using /usr/include/maa/

* pkg-config now only sees maa.h & maa.hpp
* usage of maa/*.{h,hpp} is still allowed
* examples updated to use maa.{h,hpp}

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-06-10 16:33:32 +01:00
parent f1a0e9825e
commit faedfce419
28 changed files with 61 additions and 15 deletions

82
api/maa/aio.h Normal file
View File

@@ -0,0 +1,82 @@
/*
* Author: Nandkishor Sonar
* Copyright (c) 2014 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
/**
* @file
* @brief Analog input/output
*
* AIO is the anlog input & output interface to libmaa. It is used to read or
* set the voltage applied to an AIO pin.
*
* @snippet analogin_a0.c Interesting
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include "maa.h"
#include "gpio.h"
#define ADC_RAW_RESOLUTION_BITS (12)
#define ADC_SUPPORTED_RESOLUTION_BITS (10)
/**
* Opaque pointer definition to the internal struct _aio. This context refers
* to one single AIO pin on the board.
*/
typedef struct _aio* maa_aio_context;
/**
* Initialise an Analog input device, connected to the specified pin
*
* @param pin Channel number to read ADC inputs
* @returns aio context or NULL
*/
maa_aio_context maa_aio_init(unsigned int pin);
/**
* Read the input voltage
*
* @param dev The AIO context
* @returns The current input voltage, normalised to a 16-bit value
*/
uint16_t maa_aio_read(maa_aio_context dev);
/**
* Close the analog input context, this will free the memory for the context
*
* @param dev The AIO context
* @return Result of operation
*/
maa_result_t maa_aio_close(maa_aio_context dev);
#ifdef __cplusplus
}
#endif

69
api/maa/aio.hpp Normal file
View File

@@ -0,0 +1,69 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
#include "aio.h"
namespace maa {
/**
* @brief C++ API to Analog IO
*
* This file defines the aio C++ interface for libmaa
*
* @snippet examples/c++/AioA0.cpp Interesting
*/
class Aio {
public:
/**
* Aio Constructor, takes a pin number which will map directly to the
* board number
*
* @param pin channel number to read ADC inputs
*/
Aio(unsigned int pin) {
m_aio = maa_aio_init(pin);
}
/**
* Aio destructor
*/
~Aio() {
maa_aio_close(m_aio);
}
/**
* Read a value from the AIO pin. Note this value can never be outside
* of the bounds of an unsigned short
*
* @returns The current input voltage, normalised to a 16-bit value
*/
int read() {
// Use basic types to make swig code generation simpler
return (int) maa_aio_read(m_aio);
}
private:
maa_aio_context m_aio;
};
}

195
api/maa/gpio.h Normal file
View File

@@ -0,0 +1,195 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 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
/**
* @file
* @brief General Purpose IO
*
* Gpio is the General Purpose IO interface to libmaa. It's features depends on
* the board type used, it can use gpiolibs (exported via a kernel module
* through sysfs), or memory mapped IO via a /dev/uio device or /dev/mem
* depending again on the board configuratio, or memory mapped IO via a
* /dev/uio device or /dev/mem depending again on the board configuration
*
* @snippet gpio_read6.c Interesting
*/
#ifdef __cplusplus
extern "C" {
#endif
#ifdef SWIGPYTHON
#include <Python.h>
#endif
#include <stdio.h>
#include <pthread.h>
#include "maa.h"
/**
* Opaque pointer definition to the internal struct _gpio
*/
typedef struct _gpio* maa_gpio_context;
/**
* Gpio Output modes
*/
typedef enum {
MAA_GPIO_STRONG = 0, /**< Default. Strong high and low */
MAA_GPIO_PULLUP = 1, /**< Resistive High */
MAA_GPIO_PULLDOWN = 2, /**< Resistive Low */
MAA_GPIO_HIZ = 3 /**< High Z State */
} gpio_mode_t;
/**
* Gpio Direction options
*/
typedef enum {
MAA_GPIO_OUT = 0, /**< Output. A Mode can also be set */
MAA_GPIO_IN = 1 /**< Input */
} gpio_dir_t;
/**
* Gpio Edge types for interupts
*/
typedef enum {
MAA_GPIO_EDGE_NONE = 0, /**< No interrupt on Gpio */
MAA_GPIO_EDGE_BOTH = 1, /**< Interupt on rising & falling */
MAA_GPIO_EDGE_RISING = 2, /**< Interupt on rising only */
MAA_GPIO_EDGE_FALLING = 3 /**< Interupt on falling only */
} gpio_edge_t;
/**
* Initialise gpio_context, based on board number
*
* @param pin Pin number read from the board, i.e IO3 is 3
* @returns gpio context or NULL
*/
maa_gpio_context maa_gpio_init(int pin);
/**
* Initialise gpio context without any mapping to a pin
*
* @param gpiopin gpio pin as listed in SYSFS
* @return gpio context or NULL
*/
maa_gpio_context maa_gpio_init_raw(int gpiopin);
/**
* Set the edge mode on the gpio
*
* @param dev The Gpio context
* @param mode The edge mode to set the gpio into
* @return Result of operation
*/
maa_result_t maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode);
/**
* Set an interupt on pin
*
* @param dev The Gpio context
* @param edge The edge mode to set the gpio into
* @param fptr Function pointer to function to be called when interupt is
* triggered
* @return Result of operation
*/
maa_result_t maa_gpio_isr(maa_gpio_context dev, gpio_edge_t edge, void (*fptr)(void));
/**
* Stop the current interupt watcher on this Gpio, and set the Gpio edge mode
* to MAA_GPIO_EDGE_NONE
*
* @param dev The Gpio context
* @return Result of operation
*/
maa_result_t maa_gpio_isr_exit(maa_gpio_context dev);
/**
* Set Gpio Output Mode,
*
* @param dev The Gpio context
* @param mode The Gpio Output Mode
* @return Result of operation
*/
maa_result_t maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode);
/**
* Set Gpio direction
*
* @param dev The Gpio context
* @param dir The direction of the Gpio
* @return Result of operation
*/
maa_result_t maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir);
/**
* Close the Gpio context
* - Will free the memory for the context and unexport the Gpio
*
* @param dev The Gpio context
* @return Result of operation
*/
maa_result_t maa_gpio_close(maa_gpio_context dev);
/**
* Read the Gpio value.
*
* @param dev The Gpio context
* @return Result of operation
*/
int maa_gpio_read(maa_gpio_context dev);
/**
* Write to the Gpio Value.
*
* @param dev The Gpio context
* @param value Integer value to write
* @return Result of operation
*/
maa_result_t maa_gpio_write(maa_gpio_context dev, int value);
/**
* Change ownership of the context.
*
* @param dev The Gpio context
* @param owner Does this context own the pin
* @return Result of operation
*/
maa_result_t maa_gpio_owner(maa_gpio_context dev, maa_boolean_t owner);
/**
* Enable using memory mapped io instead of sysfs
*
* @param dev The Gpio context
* @param mmap Use mmap instead of sysfs
* @return Result of operation
*/
maa_result_t maa_gpio_use_mmaped(maa_gpio_context dev, maa_boolean_t mmap);
#ifdef __cplusplus
}
#endif

168
api/maa/gpio.hpp Normal file
View File

@@ -0,0 +1,168 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
#include "gpio.h"
namespace maa {
// These enums must match the enums in gpio.h
/**
* Gpio Output modes
*/
typedef enum {
MODE_STRONG = 0, /**< No interrupt on Gpio */
MODE_PULLUP = 1, /**< Interupt on rising & falling */
MODE_PULLDOWN = 2, /**< Interupt on rising only */
MODE_HIZ = 3 /**< Interupt on falling only */
} Mode;
/**
* Gpio Direction options
*/
typedef enum {
DIR_OUT = 0, /**< Output. A Mode can also be set */
DIR_IN = 1 /**< Input */
} Dir;
/**
* Gpio Edge types for interupts
*/
typedef enum {
EDGE_NONE = 0, /**< No interrupt on Gpio */
EDGE_BOTH = 1, /**< Interupt on rising & falling */
EDGE_RISING = 2, /**< Interupt on rising only */
EDGE_FALLING = 3 /**< Interupt on falling only */
} Edge;
/**
* @brief C++ API to General Purpose IO
*
* This file defines the gpio C++ interface for libmaa
*
* @snippet Blink-IO.cpp Interesting
*/
class Gpio {
public:
/**
* Instanciates a Gpio object
*
* @param pin pin number to use
* @param owner (optional) Set pin owner, default behaviour is to 'own'
* the pin if we exported it. This means we will close it on destruct.
* Otherwise it will get left open. This is only valid in sysfs use
* cases
* @param raw (optional) Raw pins will use gpiolibs pin numbering from
* the kernel module. Note that you will not get any muxers set up for
* you so this may not always work as expected.
*/
Gpio(int pin, bool owner=true, bool raw=false) {
if (raw)
m_gpio = maa_gpio_init_raw(pin);
else
m_gpio = maa_gpio_init(pin);
if (!owner)
maa_gpio_owner(m_gpio, 0);
}
/**
* Gpio object destructor, this will only unexport the gpio if we where
* the owner
*/
~Gpio() {
maa_gpio_close(m_gpio);
}
/**
* Set the edge mode for ISR
*
* @param mode The edge mode to set
* @return Result of operation
*/
maa_result_t edge(Edge mode) {
return maa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
}
#if defined(SWIGPYTHON)
maa_result_t isr(Edge mode, PyObject *pyfunc) {
return maa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) ()) pyfunc);
}
#else
/**
* Sets a callback to be called when pin value changes
*
* @return Result of operation
*/
maa_result_t isr(Edge mode, void (*fptr)(void)) {
return maa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr);
}
#endif
/**
* Exits callback - this call will not kill the isr thread imediatlu
* but only when it is out of it's critical section
*
* @return Result of operation
*/
maa_result_t isr_exit() {
return maa_gpio_isr_exit(m_gpio);
}
/**
* Change Gpio mode
*
* @param mode The mode to change the gpio into
* @return Result of operation
*/
maa_result_t mode(Mode mode) {
return maa_gpio_mode(m_gpio, (gpio_mode_t) mode);
}
/**
* Change Gpio direction
*
* @param dir The direction to change the gpio into
* @return Result of operation
*/
maa_result_t dir(Dir dir) {
return maa_gpio_dir(m_gpio, (gpio_dir_t) dir);
}
/**
* Read value from Gpio
*
* @return Gpio value
*/
int read() {
return maa_gpio_read(m_gpio);
}
/**
* Write value to Gpio
*
* @param value Value to write to Gpio
* @return Result of operation
*/
maa_result_t write(int value) {
return maa_gpio_write(m_gpio, value);
}
private:
maa_gpio_context m_gpio;
};
}

140
api/maa/i2c.h Normal file
View File

@@ -0,0 +1,140 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
/**
* @file
* @brief Inter-Integrated Circuit
*
* This file defines the i2c/Iic interface for libmaa. A context represents a
* bus and that bus may contain multiple addresses or i2c slaves. It is
* considered best practice to make sure the address is correct before doing
* any calls on i2c, in case another application or even thread changed the
* addres on that bus. Multiple instances of the same bus can exist.
*
* @snippet i2c_HMC5883L.c Interesting
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdint.h>
#include "maa.h"
#include "gpio.h"
/**
* Opaque pointer definition to the internal struct _i2c
*/
typedef struct _i2c* maa_i2c_context;
/**
* Initialise i2c context, using board defintions
*
* @param bus i2c bus to use
* @return i2c context or NULL
*/
maa_i2c_context maa_i2c_init(int bus);
/**
* Initialise i2c context, passing in spi bus to use.
*
* @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2"
* @return i2c context or NULL
*/
maa_i2c_context maa_i2c_init_raw(unsigned int bus);
/**
* Sets the frequency of the i2c context
*
* @param dev The i2c context
* @param hz The bus frequency in hertz
* @return Result of operation
*/
maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz);
/**
* Read from an i2c context
*
* @param dev The i2c context
* @param data pointer to the byte array to read data in to
* @param length max number of bytes to read
* @return Result of operation
*/
maa_result_t maa_i2c_read(maa_i2c_context dev, uint8_t *data, int length);
/**
* Read a single byte from the i2c context
*
* @param dev The i2c context
* @return The result of the read or -1 if failed
*/
uint8_t maa_i2c_read_byte(maa_i2c_context dev);
/**
* Write to an i2c context
*
* @param dev The i2c context
* @param data pointer to the byte array to be written
* @param length the number of bytes to transmit
* @return Result of operation
*/
maa_result_t maa_i2c_write(maa_i2c_context dev, const uint8_t *data, int length);
/**
* Write a single byte to an i2c context
*
* @param dev The i2c context
* @param data The byte to write
* @return Result of operation
*/
maa_result_t maa_i2c_write_byte(maa_i2c_context dev, const uint8_t data);
/**
* Sets the i2c context address.
*
* @param dev The i2c context
* @param address The address to set for the slave (ignoring the least
* signifcant bit). If set to 0, the slave will only respond to the
* general call address.
* @return Result of operation
*/
maa_result_t maa_i2c_address(maa_i2c_context dev, int address);
/**
* De-inits an maa_i2c_context device
*
* @param dev The i2c context
* @return Result of operation
*/
maa_result_t maa_i2c_stop(maa_i2c_context dev);
#ifdef __cplusplus
}
#endif

124
api/maa/i2c.hpp Normal file
View File

@@ -0,0 +1,124 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
#include "i2c.h"
namespace maa {
/**
* @brief C++ API to Inter-Integrated Circuit
*
* This file defines the I2c C++ interface for libmaa
*
* @snippet I2c-compass.cpp Interesting
*/
class I2c {
public:
/**
* Instantiates an i2c bus. Multiple instances of the same bus can
* exist and the bus is not guarranteed to be on the correct address
* before read/write.
*
* @param bus The i2c bus to use
* @param raw Whether to disable pinmapper for your board
*/
I2c(int bus, bool raw=false) {
if (raw)
m_i2c = maa_i2c_init_raw(bus);
else
m_i2c = maa_i2c_init(bus);
}
/**
* Closes the I2c Bus used. This does not guarrantee the bus will not
* be usable by anyone else or communicates this disconnect to any
* slaves.
*/
~I2c() {
maa_i2c_stop(m_i2c);
}
/**
* Sets the i2c Frequency for communication. Your board may not support
* the set frequency. Anyone can change this at any time and this will
* affect every slave on the bus
*
* @param hz Frequency to set the bus to in hz
* @return Result of operation
*/
maa_result_t frequency(int hz) {
return maa_i2c_frequency(m_i2c, hz);
}
/**
* Set the slave to talk to, typically called before every read/write
* operation
*
* @param address Communicate to the i2c slave on this address
* @return Result of operation
*/
maa_result_t address(int address) {
return maa_i2c_address(m_i2c, address);
}
/**
* Read exactly one byte from the bus
*
* @return Char read from the bus
*/
unsigned char read_byte() {
return (unsigned char) maa_i2c_read_byte(m_i2c);
}
/**
* Read mutliple bytes from the bus
*
* @param data Buffer to write into
* @param length Size of read
* @return Result of operation
*/
maa_result_t read(unsigned char * data, int length) {
return maa_i2c_read(m_i2c, data, length);
}
/**
* Write one byte to the bus
*
* @param data Buffer to send on the bus
* @param length Size of buffer to send
* @return Result of operation
*/
maa_result_t write(const unsigned char* data, int length) {
return maa_i2c_write(m_i2c, data, length);
}
/**
* Write multiple bytes to the bus
*
* @param data The byte to send on the bus
* @return Result of operation
*/
maa_result_t write_byte(const unsigned char data) {
return maa_i2c_write_byte(m_i2c, data);
}
private:
maa_i2c_context m_i2c;
};
}

171
api/maa/pwm.h Normal file
View File

@@ -0,0 +1,171 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 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
/**
* @file
* @brief Pulse Width Modulation module
*
* PWM is the Pulse Width Modulation interface to libmaa. It allows the
* generation of a signal on a pin. Some boards may have higher or lower levels
* of resolution so make sure you check the board & pin you are using before
* hand.
*
* @snippet cycle-pwm3.c Interesting
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <fcntl.h>
#include "maa.h"
typedef struct _pwm* maa_pwm_context;
/**
* Initialise pwm_context, uses board mapping
*
* @param pin The PWM PIN
* @return pwm context or NULL
*/
maa_pwm_context maa_pwm_init(int pin);
/**
* Initialise pwm_context, raw mode
*
* @param chipid The chip inwhich the PWM is under in SYSFS
* @param pin The PWM PIN.
* @return pwm context or NULL
*/
maa_pwm_context maa_pwm_init_raw(int chipid, int pin);
/**
* Set the ouput duty-cycle percentage, as a float
*
* @param dev The Pwm context to use
* @param percentage A floating-point value representing percentage of output.
* The value should lie between 0.0f (representing on 0%) and 1.0f
* Values above or below this range will be set at either 0.0f or 1.0f
* @return Result of operation
*/
maa_result_t maa_pwm_write(maa_pwm_context dev, float percentage);
/**
* Read the ouput duty-cycle percentage, as a float
*
* @param dev The Pwm context to use
* @return percentage A floating-point value representing percentage of output.
* The value should lie between 0.0f (representing on 0%) and 1.0f
* Values above or below this range will be set at either 0.0f or 1.0f
*/
float maa_pwm_read(maa_pwm_context dev);
/**
* Set the PWM period as seconds represented in a float
*
* @param dev The Pwm context to use
* @param seconds Period represented as a float in seconds
* @return Result of operation
*/
maa_result_t maa_pwm_period(maa_pwm_context dev, float seconds);
/**
* Set period, milliseconds.
*
* @param dev The Pwm context to use
* @param ms Milliseconds for period
* @return Result of operation
*/
maa_result_t maa_pwm_period_ms(maa_pwm_context dev, int ms);
/**
* Set period, microseconds
*
* @param dev The Pwm context to use
* @param us Microseconds as period
* @return Result of operation
*/
maa_result_t maa_pwm_period_us(maa_pwm_context dev, int us);
/**
* Set pulsewidth, As represnted by seconds in a (float)
*
* @param dev The Pwm context to use
* @param seconds The duration of a pulse
* @return Result of operation
*/
maa_result_t maa_pwm_pulsewidth(maa_pwm_context dev, float seconds);
/**
* Set pulsewidth, milliseconds
*
* @param dev The Pwm context to use
* @param ms Milliseconds for pulsewidth
* @return Result of operation
*/
maa_result_t maa_pwm_pulsewidth_ms(maa_pwm_context dev, int ms);
/**
* Set pulsewidth, microseconds
*
* @param dev The Pwm context to use
* @param us Microseconds for pulsewidth
* @return Result of operation
*/
maa_result_t maa_pwm_pulsewidth_us(maa_pwm_context dev, int us);
/**
* Set the enable status of the PWM pin. None zero will assume on with output being driven.
* and 0 will disable the output.
*
* @param dev The pwm context to use
* @param enable Toggle status of pin
* @return Result of operation.
*/
maa_result_t maa_pwm_enable(maa_pwm_context dev, int enable);
/**
* Change ownership of context
*
* @param dev the context
* @param owner Ownership boolean
* @return Result of operation
*/
maa_result_t maa_pwm_owner(maa_pwm_context dev, maa_boolean_t owner);
/**
* Close and unexport the PWM pin
*
* @param dev The pwm context to use
* @return Result of operation
*/
maa_result_t maa_pwm_close(maa_pwm_context dev);
#ifdef __cplusplus
}
#endif

157
api/maa/pwm.hpp Normal file
View File

@@ -0,0 +1,157 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
#include "pwm.h"
namespace maa {
/**
* @brief C++ API to Pulse Width Modulation
*
* This file defines the PWM C++ interface for libmaa
*
* @snippet Pwm3-cycle.cpp Interesting
*/
class Pwm {
public:
/**
* instanciates a PWM object on a pin
*
* @param pin the pin number used on your board
* @param chipid the pwmchip to use, use only in raw mode
* @param owner if you are the owner of the pin the destructor will
* unexport the pin from sysfs, default behaviour is you are the owner
* if the pinmapper exported it
*/
Pwm(int pin, int chipid=-1, bool owner = true) {
if (chipid == -1)
m_pwm = maa_pwm_init(pin);
else
m_pwm = maa_pwm_init_raw(pin, chipid);
if (!owner)
maa_pwm_owner(m_pwm, 0);
}
/**
* Pwm destructor
*/
~Pwm() {
maa_pwm_close(m_pwm);
}
/**
* Set the output duty-cycle percentage, as a float
*
* @param percentage A floating-point value representing percentage of
* output. The value should lie between 0.0f (representing on 0%) and
* 1.0f Values above or below this range will be set at either 0.0f or
* 1.0f
* @return Result of operation
*/
maa_result_t write(float percentage) {
return maa_pwm_write(m_pwm, percentage);
}
/**
* Read the ouput duty-cycle percentage, as a float
*
* @return A floating-point value representing percentage of
* output. The value should lie between 0.0f (representing on 0%) and
* 1.0f Values above or below this range will be set at either 0.0f or
* 1.0f
*/
float read() {
return maa_pwm_read(m_pwm);
}
/**
* Set the PWM period as seconds represented in a float
*
* @param period Period represented as a float in seconds
* @return Result of operation
*/
maa_result_t period(float period) {
return maa_pwm_period(m_pwm, period);
}
/**
* Set period, milliseconds
*
* @param ms milliseconds for period
* @return Result of operation
*/
maa_result_t period_ms(int ms) {
return maa_pwm_period_ms(m_pwm, ms);
}
/**
* Set period, microseconds
*
* @param us microseconds as period
* @return Result of operation
*/
maa_result_t period_us(int us) {
return maa_pwm_period_us(m_pwm, us);
}
/**
* Set pulsewidth, As represnted by seconds in a (float)
*
* @param seconds The duration of a pulse
* @return Result of operation
*/
maa_result_t pulsewidth(float seconds) {
return maa_pwm_pulsewidth(m_pwm, seconds);
}
/**
* Set pulsewidth, milliseconds
*
* @param ms milliseconds for pulsewidth
* @return Result of operation
*/
maa_result_t pulsewidth_ms(int ms) {
return maa_pwm_pulsewidth_ms(m_pwm, ms);
}
/**
* The pulsewidth, microseconds
*
* @param us microseconds for pulsewidth
* @return Result of operation
*/
maa_result_t pulsewidth_us(int us) {
return maa_pwm_pulsewidth_us(m_pwm, us);
}
/**
* Set the enable status of the PWM pin. None zero will assume on with
* output being driven and 0 will disable the output
*
* @param enable enable status of pin
* @return Result of operation
*/
maa_result_t enable(bool enable) {
if (enable)
return maa_pwm_enable(m_pwm, 1);
else
return maa_pwm_enable(m_pwm, 0);
}
private:
maa_pwm_context m_pwm;
};
}

121
api/maa/spi.h Normal file
View File

@@ -0,0 +1,121 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Copyright (c) 2014 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
/**
* @file
* @brief System Packet Interface
*
* This file defines the spi interface for libmaa
*
* @snippet spi_mcp4261.c Interesting
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <fcntl.h>
#include <stdint.h>
#include "maa.h"
/**
* Opaque pointer definition to the internal struct _spi
*/
typedef struct _spi* maa_spi_context;
/**
* Initialise SPI_context, uses board mapping. Sets the muxes
*
* @param bus Bus to use, as listed in platform definition, normally 0
* @return Spi context or NULL
*/
maa_spi_context maa_spi_init(int bus);
/**
* Set the SPI device mode. see spidev 0-3.
*
* @param dev The Spi context
* @param mode The SPI mode, See Linux spidev
* @return Spi context or NULL
*/
maa_result_t maa_spi_mode(maa_spi_context dev,unsigned short mode);
/** Set the SPI device operating clock frequency.
*
* @param dev the Spi context
* @param hz the frequency in hz
* @return maa_spi_context The returned initialised SPI context
*/
maa_result_t maa_spi_frequency(maa_spi_context dev, int hz);
/** Write Single Byte to the SPI device.
*
* @param dev The Spi context
* @param data Data to send
* @return Data received on the miso line
*/
uint8_t maa_spi_write(maa_spi_context dev, uint8_t data);
/** Write Buffer of bytes to the SPI device.
*
* @param dev The Spi context
* @param data to send
* @param length elements within buffer, Max 4096
* @return Data received on the miso line, same length as passed in
*/
uint8_t* maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length);
/**
* Change the SPI lsb mode
*
* @param dev The Spi context
* @param lsb Use least significant bit transmission. 0 for msbi
* @return Result of operation
*/
maa_result_t maa_spi_lsbmode(maa_spi_context dev, maa_boolean_t lsb);
/**
* Set bits per mode on transaction, defaults at 8
*
* @param dev The Spi context
* @param bits bits per word
* @return Result of operation
*/
maa_result_t maa_spi_bit_per_word(maa_spi_context dev, unsigned int bits);
/**
* De-inits an maa_spi_context device
*
* @param dev The Spi context
* @return Result of operation
*/
maa_result_t maa_spi_stop(maa_spi_context dev);
#ifdef __cplusplus
}
#endif

112
api/maa/spi.hpp Normal file
View File

@@ -0,0 +1,112 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
#include "spi.h"
namespace maa {
/**
* @brief C++ API to System Packet Interface
*
* This file defines the SPI C++ interface for libmaa
*
* @snippet Spi-pot.cpp Interesting
*/
class Spi {
public:
/**
* Initialise SPI object using the board mapping to set muxes
*
* @param bus to use, as listed in the platform definition, normally 0
*/
Spi(int bus) {
m_spi = maa_spi_init(bus);
}
/**
* Closes spi bus
*/
~Spi() {
maa_spi_stop(m_spi);
}
/**
* Set the SPI device mode. see spidev0-3
*
* @param mode the mode. See Linux spidev doc
* @return Result of operation
*/
maa_result_t mode(unsigned short mode) {
return maa_spi_mode(m_spi, mode);
}
/**
* Set the SPI device operating clock frequency
*
* @param hz the frequency to set in hz
* @return Result of operation
*/
maa_result_t frequency(int hz) {
return maa_spi_frequency(m_spi, hz);
}
/**
* Write single byte to the SPI device
*
* @param data the byte to send
* @return data received on the miso line
*/
unsigned char write(uint8_t data) {
return (unsigned char) maa_spi_write(m_spi, data);
}
/**
* Write buffer of bytes to SPI device
*
* @param data buffer to send
* @param length size of buffer to send
* @return char* data received on the miso line. Same length as passed in
*/
unsigned char* write_buf(uint8_t* data, int length) {
return (unsigned char*) maa_spi_write_buf(m_spi, data, length);
}
/**
* Change the SPI lsb mode
*
* @param lsb Use least significant bit transmission - 0 for msbi
* @return Result of operation
*/
maa_result_t lsbmode(bool lsb) {
return maa_spi_lsbmode(m_spi, (maa_boolean_t) lsb);
}
/**
* Set bits per mode on transaction, default is 8
*
* @param bits bits per word
* @return Result of operation
*/
maa_result_t bit_per_word(unsigned int bits) {
return maa_spi_bit_per_word(m_spi, bits);
}
private:
maa_spi_context m_spi;
};
}