I2C not yet integrated
Tested working with single and differential voltages.
Connect SCL (pin 11) to D15 Connect SDA (pin 10) to D14 Connect pin 16 to +5v Connect pin 9 to gnd
Diff: LT_I2C.h
- Revision:
- 2:c9e727dcd00e
- Parent:
- 0:1473318f27b6
--- a/LT_I2C.h Tue Nov 29 03:20:35 2016 +0000 +++ b/LT_I2C.h Tue Dec 06 18:34:46 2016 +0000 @@ -99,290 +99,44 @@ #define I2C_8ADDR(address) (address >> 1) //!< Use to convert an 8-bit I2C address to 7 bits. //! @} -//! Read a byte, store in "value". -//! @return 0 on success, 1 on failure -int8_t i2c_read_byte(uint8_t address, //!< 7-bit I2C address - uint8_t *value //!< Byte to be read - ); - -//! Write "value" byte to device at "address" -//! @return 0 on success, 1 on failure -int8_t i2c_write_byte(uint8_t address, //!< 7-bit I2C address - uint8_t value //!< Byte to be written - ); - -//! Read a byte of data at register specified by "command", store in "value" -//! @return 0 on success, 1 on failure -int8_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint8_t *value //!< Byte to be read - ); - -//! Write a byte of data to register specified by "command" -//! @return 0 on success, 1 on failure -int8_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint8_t value //!< Byte to be written - ); - -//! Read a 16-bit word of data from register specified by "command" -//! @return 0 on success, 1 on failure -int8_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint16_t *value //!< Word to be read - ); - -//! Write a 16-bit word of data to register specified by "command" -//! @return 0 on success, 1 on failure -int8_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint16_t value //!< Word to be written - ); - -//! Read a block of data, starting at register specified by "command" and ending at (command + length - 1) -//! @return 0 on success, 1 on failure -int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint8_t length, //!< Length of array - uint8_t *values //!< Byte array to be read - ); - -//! Read a block of data, no command byte, reads length number of bytes and stores it in values. -//! @return 0 on success, 1 on failure -int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address - uint8_t length, //!< Length of array - uint8_t *values //!< Byte array to be read - ); - +#include "mbed.h" -//! Write a block of data, starting at register specified by "command" and ending at (command + length - 1) -//! @return 0 on success, 1 on failure -int8_t i2c_write_block_data(uint8_t address, //!< 7-bit I2C address - uint8_t command, //!< Command byte - uint8_t length, //!< Length of array - uint8_t *values //!< Byte array to be written - ); - -//! Write a two command bytes, then receive a block of data -//! @return 0 on success, 1 on failure -int8_t i2c_two_byte_command_read_block(uint8_t address, //!< 7-bit I2C address - uint16_t command, //!< Command word - uint8_t length, //!< Length of array - uint8_t *values //!< Byte array to be read - ); - -//! Initializes Linduino I2C port. -//! Before communicating to the I2C port through the QuikEval connector, you must also run -//! quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the -//! QuikEval MUX (and disconnect SPI). -void quikeval_I2C_init(void); - -//! Switch MUX to connect I2C pins to QuikEval connector. -//! This will disconnect SPI pins. -void quikeval_I2C_connect(void); - -//! i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines. -void i2c_enable(void); - -//! Write start bit to the hardware I2C port -//! @return 0 if successful, 1 if not successful -int8_t i2c_start(); - -//! Write a repeat start bit to the hardware I2C port -//! @return 0 if successful, 1 if not successful -int8_t i2c_repeated_start(); - -//! Write stop bit to the hardware I2C port -void i2c_stop(); - -//! Send a data byte to hardware I2C port -//! @return 0 if successful, 1 if not successful -int8_t i2c_write(uint8_t data //!< byte that will be written to hardware I2C port. - ); - -//! Read a data byte from the hardware I2C port. -//! If ack is 0 then an acknowledge (ACK) is generated, else a NACK is generated. -//! @return the data byte read. -uint8_t i2c_read(int8_t ack //!< If ACK is 0 then an acknowledge is generated, else a NACK is generated. - ); - -//! Poll the I2C port and look for an acknowledge -//! @return Returns 0 if successful, 1 if not successful -int8_t i2c_poll(uint8_t i2c_address //!< i2c_address is the address of the slave being polled. - ); - +class LT_I2C { +private: + I2C *i2c; + PinName sda; + PinName scl; -// //! Read a byte, store in "value". -// //! @return -1 if failed or value if it succeeds -// int32_t i2c_read_byte(uint8_t address, //!< 7-bit I2C address -// uint8_t *value //!< Byte to be read -// ); -// -// //! Write "value" byte to device at "address" -// //! @return -1 if failed or 0 if it succeeds -// int32_t i2c_write_byte(uint8_t address, //!< 7-bit I2C address -// uint8_t value //!< Byte to be written -// ); -// -// //! Read a byte of data at register specified by "command", store in "value" -// //! @return -1 if failed or value if it succeeds -// int32_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint8_t *value //!< Byte to be read -// ); -// -// //! Write a byte of data to register specified by "command" -// //! @return -1 if failed or 0 if it succeeds -// int32_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint8_t value //!< Byte to be written -// ); -// -// //! Read a 16-bit word of data from register specified by "command" -// //! @return -1 if failed or value if it succeeds -// int32_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint16_t *value //!< Word to be read -// ); -// -// //! Write a 16-bit word of data to register specified by "command" -// //! @return -1 if failed or 0 if it succeeds -// int32_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint16_t value //!< Word to be written -// ); -// -// -// //! Read a block of data, starting at register specified by "command" and ending at (command + length - 1) -// //! @return Byte count -// int32_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint8_t length, //!< Length of array -// uint8_t *values //!< Byte array to be read -// ); -// -// //! Write a block of data, starting at register specified by "command" and ending at (command + length - 1) -// //! @return Byte count -// int32_t i2c_write_block_data(uint8_t address, //!< 7-bit I2C address -// uint8_t command, //!< Command byte -// uint8_t length, //!< Length of array -// uint8_t *values //!< Byte array to be written -// ); -// -// //! Write a two command bytes, then receive a block of data -// //! @return Byte count -// int32_t i2c_two_byte_command_read_block(uint8_t address, //!< 7-bit I2C address -// uint16_t command, //!< Command word -// uint8_t length, //!< Length of array -// uint8_t *values //!< Byte array to be read -// ); -// -// //! Initializes Linduino I2C port. -// //! Before communicating to the I2C port through the QuikEval connector, you must also run -// //! quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the -// //! QuikEval MUX (and disconnect SPI). -// void quikeval_I2C_init(void); -// -// //! Switch MUX to connect I2C pins to QuikEval connector -// //! This will disconnect SPI pins. -// void quikeval_I2C_connect(void); -// -// //! Setup and enable the hardware I2C interface. -// //! Must be called before using any of the other routines. -// // 100kHz example: -// // uint8 bit_rate = 18; -// // hw_i2c_enable(bit_rate, HARDWARE_I2C_PRESCALER_4); -// void i2c_enable(uint8_t bit_rate, //!< I2C Bit Rate -// uint8_t prescaler //!< I2C prescaler -// ); - - -// LT I2C functions to emulate Linux SMBus functions for E-Z porting. There does not appear to be -// a single source of the "correct" way to implement these functions. - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////// -//// From http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/i2c/dev-interface -/////////////////////////////////////////////////////////////////////////////////////////////////////////////// -//// You can do plain I2C transactions by using read(2) and write(2) calls. -//// You do not need to pass the address byte; instead, set it through -//// ioctl I2C_SLAVE before you try to access the device. -//// -//// You can do SMBus level transactions (see documentation file smbus-protocol -//// for details) through the following functions: -//// __s32 i2c_smbus_write_quick(int file, __u8 value); -//// __s32 i2c_smbus_read_byte(int file); -//// __s32 i2c_smbus_write_byte(int file, __u8 value); -//// __s32 i2c_smbus_read_byte_data(int file, __u8 command); -//// __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); -//// __s32 i2c_smbus_read_word_data(int file, __u8 command); -//// __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); -//// __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); -//// __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); -//// __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, -//// // __u8 *values); -//// All these transactions return -1 on failure; you can read errno to see -//// what happened. The 'write' transactions return 0 on success; the -//// 'read' transactions return the read value, except for read_block, which -//// returns the number of values read. The block buffers need not be longer -//// than 32 bytes. -/////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - -/* - -// Begin "LT-ified" versions of these functions: - -// Original Linux prototype: __s32 i2c_smbus_write_quick(int file, __u8 value); -// Write a single bit in the R/W bit of the address. Note - we are using 8-bit addresses, -// we should probably AND with 0xFE before appending the "value" bit. Also, should we obligate the user -// to send a value with a "1" in the LSB, or any nonzero value to assert a 1? -int8_t LT_i2c_write_quick(uint8_t address, uint8_t value); - -// Original Linux prototype: __s32 i2c_smbus_read_byte(int file); -// Read a byte, store in "value". -int8_t LT_i2c_read_byte(uint8_t address, uint8_t *value); - -// Original Linux prototype: __s32 i2c_smbus_write_byte(int file, __u8 value); -// Write "value" byte to device at "address" -int8_t LT_i2c_write_byte(uint8_t address, uint8_t value); - -// Original Linux prototype: __s32 i2c_smbus_read_byte_data(int file, __u8 command); -// Read a byte of data at register specified by "command", store in "value" -int8_t LT_i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value); - -// Original Linux prototype: __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value); -// Write a byte of data to register specified by "command" -int8_t LT_i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value); - -// Original Linux prototype: __s32 i2c_smbus_read_word_data(int file, __u8 command); -// Read a 16-bit word of data from register specified by "command" -int8_t LT_i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value); - -// Original Linux prototype: __s32 i2c_smbus_write_word_data(int file, __u8 command, __u16 value); -// Write a 16-bit word of data to register specified by "command" -int8_t LT_i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value); - -// Original Linux prototype: __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value); -int8_t LT_i2c_process_call(uint8_t address, uint8_t command, uint16_t value); - -// For block read / write, this explains the lack of length parameter in some versions: -// "Until kernel 2.6.22, the length is hard coded to 32 bytes. If you -// ask for less than 32 bytes, your code will only work with kernels -// 2.6.23 and later." - -// Original Linux prototype: __s32 i2c_smbus_read_block_data(int file, __u8 command, __u8 *values); -// Read a block of data, starting at register specified by "command" and ending at (command + length - 1) -int8_t LT_i2c_read_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values); - -// Original Linux prototype: __s32 i2c_smbus_write_block_data(int file, __u8 command, __u8 length, __u8 *values); -// Write a block of data, starting at register specified by "command" and ending at (command + length - 1) -int8_t LT_i2c_write_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values); - -// Proposed additional function to handle cases like the LTC2495 / 97 / 99 family, which require -// a 16-bit command, and can return either 3 or 4 bytes of data. -// Write a two command bytes, then receive a block of data -int8_t LT_i2c_two_byte_command_read_block(uint8_t address, uint16_t command, uint8_t length, uint8_t *values); - -*/ - +public: + LT_I2C(); + LT_I2C(PinName sda_, PinName scl_); + + //! Read a byte of data at register specified by "command", store in "value" + //! @return 0 on success, 1 on failure + int8_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address + uint8_t command, //!< Command byte + uint8_t *value //!< Byte to be read + ); + + //! Write a byte of data to register specified by "command" + //! @return 0 on success, 1 on failure + int8_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address + uint8_t command, //!< Command byte + uint8_t value //!< Byte to be written + ); + + //! Read a 16-bit word of data from register specified by "command" + //! @return 0 on success, 1 on failure + int8_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address + uint8_t command, //!< Command byte + uint16_t *value //!< Word to be read + ); + + //! Write a 16-bit word of data to register specified by "command" + //! @return 0 on success, 1 on failure + int8_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address + uint8_t command, //!< Command byte + uint16_t value //!< Word to be written + ); +}; #endif // LT_I2C_H