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.
Dependents: WebserverGSOE_Schnaiter HC05__TI4_RP5_Schnaiter
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 if (HAL_GetDEVID () ==0x437) _frequency_delay = 20;//3; 00023 else _frequency_delay = 60;//3; 00024 00025 initialise(); 00026 } 00027 00028 SoftwareI2C::~SoftwareI2C() { 00029 00030 } 00031 00032 /** 00033 * @brief Read 1 or more bytes from the I2C slave 00034 * @param device_address The address of the device to read from 00035 * @param data An allocated array to read the data into 00036 * @param data_bytes Number of bytes to read (must be equal to or less then the allocated memory in data) 00037 */ 00038 void SoftwareI2C::read(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { 00039 if (data == 0 || data_bytes == 0) return; 00040 __disable_irq(); 00041 device_address = device_address | 0x01; 00042 start(); 00043 putByte(device_address); 00044 getAck(); 00045 for (int x = 0; x < data_bytes; ++x) { 00046 data[x] = getByte(); 00047 if ( x < (data_bytes -1)) { //ack all but the final byte 00048 giveAck(); 00049 } 00050 } 00051 stop(); 00052 __enable_irq(); 00053 } 00054 00055 /** 00056 * @brief Write 1 or more bytes to the I2C slave 00057 * @param device_address The address of the device to write to 00058 * @param data An array to write the data from 00059 * @param data_bytes Number of bytes to write from array 00060 */ 00061 void SoftwareI2C::write(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { 00062 if (data == 0 || data_bytes == 0) return; 00063 __disable_irq(); 00064 device_address = device_address & 0xFE; 00065 start(); 00066 putByte(device_address); 00067 getAck(); 00068 for ( int x = 0; x < data_bytes; ++x ) { 00069 putByte(data[x]); 00070 getAck(); 00071 } 00072 stop(); 00073 __enable_irq(); 00074 } 00075 00076 /** 00077 * @brief Write 1 byte to the I2C slave 00078 * @param device_address The address of the device to write to 00079 * @param byte The data to write 00080 */ 00081 void SoftwareI2C::write(uint8_t device_address, uint8_t byte) { 00082 device_address = device_address & 0xFE; 00083 start(); 00084 putByte(device_address); 00085 getAck(); 00086 putByte(byte); 00087 getAck(); 00088 stop(); 00089 } 00090 00091 /** 00092 * @brief Read 1 or more bytes from the I2C slave at the specified memory address 00093 * @param device_address The address of the device to read from 00094 * @param start_address The memory address to read from 00095 * @param data The allocated array to read into 00096 * @param data_bytes The number of bytes to read 00097 */ 00098 void SoftwareI2C::randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { 00099 if (data == 0 || data_bytes == 0) return; 00100 00101 device_address = device_address & 0xFE; 00102 start(); 00103 putByte(device_address); 00104 if (!getAck()) { 00105 return; 00106 } 00107 putByte(start_address); 00108 if (!getAck()) { 00109 return; 00110 } 00111 00112 device_address=device_address | 0x01; 00113 start(); 00114 putByte(device_address); 00115 if (!getAck()) { 00116 return; 00117 } 00118 for ( int x = 0; x < data_bytes; ++x) { 00119 data[x] = getByte(); 00120 if (x != (data_bytes - 1)) giveAck(); 00121 } 00122 stop(); 00123 } 00124 00125 /** 00126 * @brief Write 1 byte to the I2C slave at the specified memory address 00127 * @param device_address The address of the device to write to 00128 * @param start_address The memory address to write to 00129 * @param byte The data to write 00130 */ 00131 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte) { 00132 device_address = device_address & 0xFE; 00133 start(); 00134 putByte(device_address); 00135 getAck(); 00136 putByte(start_address); 00137 getAck(); 00138 putByte(byte); 00139 getAck(); 00140 stop(); 00141 } 00142 00143 /** 00144 * @brief Write 1 or more bytes to the I2C slave at the specified memory address 00145 * @param device_address The address of the device to write to 00146 * @param start_address The memory address to write to 00147 * @param data The data to write 00148 * @param data_bytes The number of bytes to write 00149 */ 00150 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { 00151 if (data == 0 || data_bytes == 0) return; 00152 00153 device_address = device_address & 0xFE; 00154 start(); 00155 putByte(device_address); 00156 getAck(); 00157 putByte(start_address); 00158 getAck(); 00159 for ( int x = 0; x <= data_bytes; ++x ) { 00160 putByte(data[x]); 00161 getAck(); 00162 } 00163 stop(); 00164 } 00165 00166 /** 00167 * @brief Read 2 bytes from the I2C slave at the specified memory address and return them as an 16bit unsigned integer 00168 * @param device_address The address of the device to read from 00169 * @param start_address The memory address to read from 00170 * @return MSB 16bit unsigned integer 00171 */ 00172 uint16_t SoftwareI2C::read16(uint8_t device_address, uint8_t start_address) { 00173 uint8_t short_array[2] = {0, 0}; 00174 randomRead(device_address, start_address, short_array, 2 ); 00175 uint16_t value = 0; 00176 value = short_array[0] << 8; 00177 value |= short_array[1]; 00178 00179 return value; 00180 } 00181 00182 /** 00183 * @brief Read 3 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer 00184 * @param device_address The address of the device to read from 00185 * @param start_address The memory address to read from 00186 * @return MSB 32bit unsigned integer 00187 */ 00188 uint32_t SoftwareI2C::read24(uint8_t device_address, uint8_t start_address) { 00189 uint8_t value_array[4] = {0, 0, 0}; 00190 randomRead(device_address, start_address, value_array, 3 ); 00191 uint32_t value = 0; 00192 value = value_array[0] << 16; 00193 value |= value_array[1] << 8; 00194 value |= value_array[2]; 00195 00196 return value; 00197 } 00198 00199 /** 00200 * @brief Read 4 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer 00201 * @param device_address The address of the device to read from 00202 * @param start_address The memory address to read from 00203 * @return MSB 32bit unsigned integer 00204 */ 00205 uint32_t SoftwareI2C::read32(uint8_t device_address, uint8_t start_address) { 00206 uint8_t value_array[4] = {0, 0, 0, 0}; 00207 randomRead(device_address, start_address, value_array, 4 ); 00208 uint32_t value = 0; 00209 value = value_array[0] << 24; 00210 value |= value_array[1] << 16; 00211 value |= value_array[2] << 8; 00212 value |= value_array[3]; 00213 00214 return value; 00215 }
Generated on Mon Aug 29 2022 12:21:52 by
1.7.2