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