Private
Public Access
2
0

MAA version 0.2.0 moves to a standard C API

* Removed all C++ code and renamed all .cxx extensions to .c
* All functions are renamed to maa_ and modules are for example called maa_pwm
* Cmake can now 'make doc' using a Doxyfile.in to create documentation
* examples/ have been updated but swig generated API is untested

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-04-27 21:17:54 +01:00
parent 5a270191b5
commit a5a407e4b5
23 changed files with 389 additions and 736 deletions

View File

@@ -20,10 +20,6 @@
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct gpio_struct
{
int pin;
@@ -43,7 +39,3 @@ void gpio_dir(gpio_t *gpio, gpio_dir_t dir);
void gpio_close(gpio_t *gpio);
int gpio_read(gpio_t *gpio);
void gpio_write(gpio_t *gpio, int value);
#ifdef __cplusplus
}
#endif

169
api/i2c.h
View File

@@ -18,119 +18,84 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "gpio.h"
#include "smbus.hpp"
typedef struct {
int hz;
int fh;
int addr;
gpio_t gpio;
} i2c_t;
namespace maa {
int maa_i2c_init(i2c_t* dev);
/** An I2C Master, used for communicating with I2C slave devices
/** Set the frequency of the I2C interface
*
* Example:
* @code
* // Read from I2C slave at address 0x62
*
* #include "maa.h"
*
* I2C i2c(p28, p27);
*
* int main() {
* int address = 0x62;
* char data[2];
* i2c.read(address, data, 2);
* }
* @endcode
* @param hz The bus frequency in hertz
*/
class I2C {
void maa_i2c_frequency(i2c_t* dev, int hz);
public:
enum RxStatus {
NoData,
MasterGeneralCall,
MasterWrite,
MasterRead
};
/** Checks to see if this I2C Slave has been addressed.
*
* @returns
* A status indicating if the device has been addressed, and how
* - NoData - the slave has not been addressed
* - ReadAddressed - the master has requested a read from this slave
* - WriteAddressed - the master is writing to this slave
* - WriteGeneral - the master is writing to all slave
*/
int maa_i2c_receive(i2c_t* dev);
enum Acknowledge {
NoACK = 0,
ACK = 1
};
/** Read from an I2C master.
*
* @param data pointer to the byte array to read data in to
* @param length maximum number of bytes to read
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int maa_i2c_read(i2c_t* dev, char *data, int length);
/** Create an I2C Master interface, connected to the specified pins
*
* @param sda I2C data line pin
* @param scl I2C clock line pin
*/
I2C(unsigned int sda, unsigned int scl);
/** Read a single byte from an I2C master.
*
* @returns
* the byte read
*/
int maa_i2c_read_byte(i2c_t* dev);
/** Set the frequency of the I2C interface
*
* @param hz The bus frequency in hertz
*/
void frequency(int hz);
/** Write to an I2C master.
*
* @param data pointer to the byte array to be transmitted
* @param length the number of bytes to transmite
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int maa_i2c_write(i2c_t* dev, const char *data, int length);
/** Read from an I2C slave
*
* Performs a complete read transaction. The bottom bit of
* the address is forced to 1 to indicate a read.
*
* @param address 8-bit I2C slave address [ addr | 1 ]
* @param data Pointer to the byte-array to read data in to
* @param length Number of bytes to read
* @param repeated Repeated start, true - don't send stop at end
*
* @returns
* 0 on success (ack),
* non-0 on failure (nack)
*/
int read(int address, char *data, int length, bool repeated = false);
/** Write a single byte to an I2C master.
*
* @data the byte to write
*
* @returns
* '1' if an ACK was received,
* '0' otherwise
*/
int maa_i2c_write_byte(i2c_t* dev, int data);
/** Read a single byte from the I2C bus
*
* @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
*
* @returns
* the byte read
*/
int read(int ack);
/** Sets the I2C slave address.
*
* @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.
*/
void maa_i2c_address(i2c_t* dev, int address);
/** Write to an I2C slave
*
* Performs a complete write transaction. The bottom bit of
* the address is forced to 0 to indicate a write.
*
* @param address 8-bit I2C slave address [ addr | 0 ]
* @param data Pointer to the byte-array data to send
* @param length Number of bytes to send
* @param repeated Repeated start, true - do not send stop at end
*
* @returns
* 0 on success (ack),
* non-0 on failure (nack)
*/
int write(int address, const char *data, int length, bool repeated = false);
/** Write single byte out on the I2C bus
* @param data data to write out on bus
*
* @returns
* '1' if an ACK was received,
* '0' otherwise
*/
int write(int data);
/** Creates a start condition on the I2C bus
*/
void start(void);
/** Creates a stop condition on the I2C bus
*/
void stop(void);
protected:
void aquire();
int _hz;
int i2c_handle;
};
}
/** De-inits an i2c_t device
*/
void maa_i2c_stop(i2c_t* dev);

View File

@@ -1,154 +0,0 @@
/*
* Originally from mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
* Copyright (c) 2014 Intel Corporation
*
* 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.
*/
#pragma once
#include <stdio.h>
#include <fcntl.h>
#include "smbus.hpp"
#include "gpio.h"
namespace maa {
/** An I2C Slave, used for communicating with an I2C Master device
*
* Example:
* @code
* // Simple I2C responder
* #include <mbed.h>
*
* I2CSlave slave(p9, p10);
*
* int main() {
* char buf[10];
* char msg[] = "Slave!";
*
* slave.address(0xA0);
* while (1) {
* int i = slave.receive();
* switch (i) {
* case I2CSlave::ReadAddressed:
* slave.write(msg, strlen(msg) + 1); // Includes null char
* break;
* case I2CSlave::WriteGeneral:
* slave.read(buf, 10);
* printf("Read G: %s\n", buf);
* break;
* case I2CSlave::WriteAddressed:
* slave.read(buf, 10);
* printf("Read A: %s\n", buf);
* break;
* }
* for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
* }
* }
* @endcode
*/
class I2CSlave {
public:
enum RxStatus {
NoData = 0,
ReadAddressed = 1,
WriteGeneral = 2,
WriteAddressed = 3
};
/** Create an I2C Slave interface, connected to the specified pins.
*
* @param sda I2C data line pin
* @param scl I2C clock line pin
*/
I2CSlave(unsigned int sda, unsigned int scl);
/** Set the frequency of the I2C interface
*
* @param hz The bus frequency in hertz
*/
void frequency(int hz);
/** Checks to see if this I2C Slave has been addressed.
*
* @returns
* A status indicating if the device has been addressed, and how
* - NoData - the slave has not been addressed
* - ReadAddressed - the master has requested a read from this slave
* - WriteAddressed - the master is writing to this slave
* - WriteGeneral - the master is writing to all slave
*/
int receive(void);
/** Read from an I2C master.
*
* @param data pointer to the byte array to read data in to
* @param length maximum number of bytes to read
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int read(char *data, int length);
/** Read a single byte from an I2C master.
*
* @returns
* the byte read
*/
int read(void);
/** Write to an I2C master.
*
* @param data pointer to the byte array to be transmitted
* @param length the number of bytes to transmite
*
* @returns
* 0 on success,
* non-0 otherwise
*/
int write(const char *data, int length);
/** Write a single byte to an I2C master.
*
* @data the byte to write
*
* @returns
* '1' if an ACK was received,
* '0' otherwise
*/
int write(int data);
/** Sets the I2C slave address.
*
* @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.
*/
void address(int address);
/** Reset the I2C slave back into the known ready receiving state.
*/
void stop(void);
protected:
int _hz;
int i2c_handle;
int _addr;
gpio_t gpio;
};
}

View File

@@ -25,10 +25,9 @@
#pragma once
#include "i2c.h"
#include "i2cslave.h"
#include "gpio.h"
#include "pwm.h"
#define MAA_LIBRARY_VERSION 1
int get_version();
int maa_get_version();

146
api/pwm.h
View File

@@ -21,112 +21,66 @@
#include <stdio.h>
#include <fcntl.h>
namespace maa {
/** A PWM object, used for interacting with PWM output.
*
* Example:
* @code
* // Set up PWM object then cycle percentage 0-100.
*
* #include "maa.h"
*
* PWM pwm(3);
*
* int main() {
* pwm.period_us(7968750i); //Max Galileo Rev D
* pwm.enable(1);
*
* float value = 0;
* while(1) {
* pwm.write(value);
* sleep(0.5);
* if(value == 1.0) {
* value = 0;
* } else {
* value = value +0.1;
* }
* }
* }
* @endcode
*/
class PWM {
private:
typedef struct {
int chipid, pin;
FILE *duty_fp;
} pwm_t;
void write_period(int period);
void write_duty(int duty);
int setup_duty_fp();
int get_period();
int get_duty();
int maa_pwm_init(pwm_t* pwm, int chipin, int pin);
public:
/** Set the ouput 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.
*/
void maa_pwm_write(pwm_t* pwm, float percentage);
/** Create an PWM object
*
* @param chipid The chip in which the following pin is on.
* @param pin The PWM channel to operate on
*/
PWM(int chipid, int pin);
/** Read the ouput duty-cycle percentage, as a float
*
* @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(pwm_t* pwm);
/** Set the ouput 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.
*/
void write(float percentage);
/** Set the PWM period as seconds represented in a float
*
* @param seconds Peroid represented as a float in seconds.
*/
void maa_pwm_period(pwm_t* pwm, float seconds);
/** Read the ouput duty-cycle percentage, as a float
*
* @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 read();
/** Set period. milli-oseconds.
* @param ms milli-seconds for period.
*/
void maa_pwm_period_ms(pwm_t* pwm, int ms);
/** Set the PWM period as seconds represented in a float
*
* @param seconds Peroid represented as a float in seconds.
*/
void period(float seconds);
/** Set period. microseconds
* @param ns microseconds as period.
*/
void maa_pwm_period_us(pwm_t* pwm, int us);
/** Set period. milli-oseconds.
* @param ms milli-seconds for period.
*/
void period_ms(int ms);
/** Set pulsewidth, As represnted by seconds in a (float).
* @param seconds The duration of a pulse
*/
void maa_pwm_pulsewidth(pwm_t* pwm, float seconds);
/** Set period. microseconds
* @param ns microseconds as period.
*/
void period_us(int us);
/** Set pulsewidth. Milliseconds
* @param ms milliseconds for pulsewidth.
*/
void maa_pwm_pulsewidth_ms(pwm_t* pwm, int ms);
/** Set pulsewidth, As represnted by seconds in a (float).
* @param seconds The duration of a pulse
*/
void pulsewidth(float seconds);
/** Set pulsewidth, microseconds.
* @param us microseconds for pulsewidth.
*/
void maa_pwm_pulsewidth_us(pwm_t* pwm, int us);
/** Set pulsewidth. Milliseconds
* @param ms milliseconds for pulsewidth.
*/
void pulsewidth_ms(int ms);
/** 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
*/
void maa_pwm_enable(pwm_t* pwm, int enable);
/** Set pulsewidth, microseconds.
* @param us microseconds for pulsewidth.
*/
void pulsewidth_us(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 enable enable status of pin
*/
void enable(int enable);
/** Close and unexport the PWM pin.
*/
void close();
};
}
/** Close and unexport the PWM pin.
*/
void maa_pwm_close(pwm_t* pwm);