Lehrer Busch / LCD_i2C_JDS

Dependents:   120_robot_H_Bridge8835_UserButton_DIR_PWM_I2C_LCD 140_robot_H_Bridge8835_DIR_PWM_US_LCD xxx_Uhr_I2C_LCD_Delay_Bua xxx_Uhr_I2C_LCD_Delay_BuaV ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SoftwareI2C.cpp Source File

SoftwareI2C.cpp

00001 /* 
00002  * mbed Library to use a software master i2c interface on any GPIO pins
00003  * Copyright (c) 2012 Christopher Pepper
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 #include "SoftwareI2C.h"
00008 
00009 /**
00010  * @brief Initializes interface
00011  * @param sda GPIO pin to use as I2C SDA
00012  * @param scl GPIO pin to use as I2C SCL
00013  */
00014 
00015 SoftwareI2C::SoftwareI2C(PinName sda, PinName scl) : _sda(sda) , _scl(scl) {
00016     _scl.output();
00017     _scl.mode(OpenDrain);
00018     _sda.output();
00019     _sda.mode(OpenDrain);
00020 
00021     _device_address = 0;
00022     _frequency_delay = 20;//3;
00023 
00024     initialise();
00025 }
00026 
00027 SoftwareI2C::~SoftwareI2C() {
00028 
00029 }
00030 
00031 /**
00032  * @brief Read 1 or more bytes from the I2C slave
00033  * @param device_address The address of the device to read from
00034  * @param data An allocated array to read the data into
00035  * @param data_bytes Number of bytes to read (must be equal to or less then the allocated memory in data)
00036  */
00037 void SoftwareI2C::read(uint8_t device_address, uint8_t* data, uint8_t data_bytes) {
00038     if (data == 0 || data_bytes == 0) return;
00039     __disable_irq();
00040     device_address = device_address | 0x01;
00041     start();
00042     putByte(device_address);
00043     getAck();
00044     for (int x = 0; x < data_bytes; ++x) {
00045         data[x] = getByte();
00046         if ( x < (data_bytes -1)) { //ack all but the final byte
00047             giveAck();
00048         }
00049     }
00050     stop();
00051     __enable_irq();
00052 }
00053 
00054 /**
00055  * @brief Write 1 or more bytes to the I2C slave
00056  * @param device_address The address of the device to write to
00057  * @param data An array to write the data from
00058  * @param data_bytes Number of bytes to write from array
00059  */
00060 void SoftwareI2C::write(uint8_t device_address, uint8_t* data,  uint8_t data_bytes) {
00061     if (data == 0 || data_bytes == 0) return;
00062     __disable_irq();
00063     device_address = device_address & 0xFE;
00064     start();
00065     putByte(device_address);
00066     getAck();
00067     for ( int x = 0; x < data_bytes; ++x ) {
00068         putByte(data[x]);
00069         getAck();
00070     }
00071     stop();
00072     __enable_irq();
00073 }
00074 
00075 /**
00076  * @brief Write 1 byte to the I2C slave
00077  * @param device_address The address of the device to write to
00078  * @param byte The data to write
00079  */
00080 void SoftwareI2C::write(uint8_t device_address, uint8_t byte) {
00081     device_address = device_address & 0xFE;
00082     start();
00083     putByte(device_address);
00084     getAck();
00085     putByte(byte);
00086     getAck();
00087     stop();
00088 }
00089 
00090 /**
00091  * @brief Read 1 or more bytes from the I2C slave at the specified memory address
00092  * @param device_address The address of the device to read from
00093  * @param start_address The memory address to read from
00094  * @param data The allocated array to read into
00095  * @param data_bytes The number of bytes to read
00096  */
00097 void SoftwareI2C::randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) {
00098     if (data == 0 || data_bytes == 0) return;
00099 
00100     device_address = device_address & 0xFE;
00101     start();
00102     putByte(device_address);
00103     if (!getAck()) {
00104         return;
00105     }
00106     putByte(start_address);
00107     if (!getAck()) {
00108         return;
00109     }
00110 
00111     device_address=device_address | 0x01;
00112     start();
00113     putByte(device_address);
00114     if (!getAck()) {
00115         return;
00116     }
00117     for ( int x = 0; x < data_bytes; ++x) {
00118         data[x] = getByte();
00119         if (x != (data_bytes - 1)) giveAck();
00120     }
00121     stop();
00122 }
00123 
00124 /**
00125  * @brief Write 1 byte to the I2C slave at the specified memory address
00126  * @param device_address The address of the device to write to
00127  * @param start_address The memory address to write to
00128  * @param byte The data to write
00129  */
00130 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte) {
00131     device_address = device_address & 0xFE;
00132     start();
00133     putByte(device_address);
00134     getAck();
00135     putByte(start_address);
00136     getAck();
00137     putByte(byte);
00138     getAck();
00139     stop();
00140 }
00141 
00142 /**
00143  * @brief Write 1 or more bytes to the I2C slave at the specified memory address
00144  * @param device_address The address of the device to write to
00145  * @param start_address The memory address to write to
00146  * @param data The data to write
00147  * @param data_bytes The number of bytes to write
00148  */
00149 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) {
00150     if (data == 0 || data_bytes == 0) return;
00151 
00152     device_address = device_address & 0xFE;
00153     start();
00154     putByte(device_address);
00155     getAck();
00156     putByte(start_address);
00157     getAck();
00158     for ( int x = 0; x <= data_bytes; ++x ) {
00159         putByte(data[x]);
00160         getAck();
00161     }
00162     stop();
00163 }
00164 
00165 /**
00166  * @brief Read 2 bytes from the I2C slave at the specified memory address and return them as an 16bit unsigned integer
00167  * @param device_address The address of the device to read from
00168  * @param start_address The memory address to read from
00169  * @return MSB 16bit unsigned integer
00170  */
00171 uint16_t SoftwareI2C::read16(uint8_t device_address, uint8_t start_address) {
00172     uint8_t short_array[2] = {0, 0};
00173     randomRead(device_address, start_address, short_array, 2 );
00174     uint16_t value = 0;
00175     value = short_array[0] << 8;
00176     value |= short_array[1];
00177 
00178     return value;
00179 }
00180 
00181 /**
00182  * @brief Read 3 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer
00183  * @param device_address The address of the device to read from
00184  * @param start_address The memory address to read from
00185  * @return MSB 32bit unsigned integer 
00186  */
00187 uint32_t SoftwareI2C::read24(uint8_t device_address, uint8_t start_address) {
00188     uint8_t value_array[4] = {0, 0, 0};
00189     randomRead(device_address, start_address, value_array, 3 );
00190     uint32_t value = 0;
00191     value = value_array[0] << 16;
00192     value |= value_array[1] << 8;
00193     value |= value_array[2];
00194 
00195     return value;
00196 }
00197 
00198 /**
00199  * @brief Read 4 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer
00200  * @param device_address The address of the device to read from
00201  * @param start_address The memory address to read from
00202  * @return MSB 32bit unsigned integer
00203  */
00204 uint32_t SoftwareI2C::read32(uint8_t device_address, uint8_t start_address) {
00205     uint8_t value_array[4] = {0, 0, 0, 0};
00206     randomRead(device_address, start_address, value_array, 4 );
00207     uint32_t value = 0;
00208     value = value_array[0] << 24;
00209     value |= value_array[1] << 16;
00210     value |= value_array[2] << 8;
00211     value |= value_array[3];
00212 
00213     return value;
00214 }