Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-RtosI2cDriver by
Diff: I2CMasterRtos.h
- Revision:
- 3:967dde37e712
- Child:
- 5:8a418c89e515
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CMasterRtos.h Fri Apr 19 21:33:29 2013 +0000 @@ -0,0 +1,127 @@ +#ifndef I2CMASTERRTOS_H +#define I2CMASTERRTOS_H + +#include "I2CDriver.h" + +namespace mbed +{ + +/// I2C master interface to the RTOS-I2CDriver. +/// The interface is compatible to the original mbed I2C class. +/// Provides an additonal read from register function. +class I2CMasterRtos +{ + I2CDriver m_drv; + +public: + /** Create an I2C Master interface, connected to the specified pins + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + */ + I2CMasterRtos(PinName sda, PinName scl, int freq=100000):m_drv(sda,scl,freq) {} + + /** Set the frequency of the I2C interface + * + * @param hz The bus frequency in hertz + */ + void frequency(int hz) { + m_drv.frequency(hz); + } + + /** 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) { + return m_drv.readMaster( address, data, length, repeated); + } + + /** Read from a given I2C slave register + * + * Performs a complete write-register-read-data-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 _register 8-bit regster address + * @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, uint8_t _register, char *data, int length, bool repeated = false) { + return m_drv.readMaster( address, _register, data, length, repeated); + } + + /** 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) { + return m_drv.readMaster(ack); + } + + /** 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) { + return m_drv.writeMaster(address, data, length, repeated); + } + + /** 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) { + return m_drv.writeMaster(data); + } + + /** Creates a start condition on the I2C bus + */ + + void startMaster(void) { + m_drv.startMaster(); + } + + /** Creates a stop condition on the I2C bus + */ + void stop(void) { + m_drv.stopMaster(); + } + +}; +} + + +#endif \ No newline at end of file