LCD Baseshield GSOE GSOF
Dependents: WebserverGSOE_Schnaiter HC05__TI4_RP5_Schnaiter
SoftwareI2C.cpp@9:b08c2ffb9253, 2021-03-12 (annotated)
- Committer:
- jack1930
- Date:
- Fri Mar 12 09:14:20 2021 +0000
- Revision:
- 9:b08c2ffb9253
- Parent:
- 8:3bac0faac335
L152+F411
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jack1930 | 5:66ca6c9b15f2 | 1 | /* |
jack1930 | 5:66ca6c9b15f2 | 2 | * mbed Library to use a software master i2c interface on any GPIO pins |
jack1930 | 5:66ca6c9b15f2 | 3 | * Copyright (c) 2012 Christopher Pepper |
jack1930 | 5:66ca6c9b15f2 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
jack1930 | 5:66ca6c9b15f2 | 5 | */ |
jack1930 | 5:66ca6c9b15f2 | 6 | |
jack1930 | 5:66ca6c9b15f2 | 7 | #include "SoftwareI2C.h" |
jack1930 | 5:66ca6c9b15f2 | 8 | |
jack1930 | 5:66ca6c9b15f2 | 9 | /** |
jack1930 | 5:66ca6c9b15f2 | 10 | * @brief Initializes interface |
jack1930 | 5:66ca6c9b15f2 | 11 | * @param sda GPIO pin to use as I2C SDA |
jack1930 | 5:66ca6c9b15f2 | 12 | * @param scl GPIO pin to use as I2C SCL |
jack1930 | 5:66ca6c9b15f2 | 13 | */ |
jack1930 | 5:66ca6c9b15f2 | 14 | |
jack1930 | 5:66ca6c9b15f2 | 15 | SoftwareI2C::SoftwareI2C(PinName sda, PinName scl) : _sda(sda) , _scl(scl) { |
jack1930 | 5:66ca6c9b15f2 | 16 | _scl.output(); |
jack1930 | 5:66ca6c9b15f2 | 17 | _scl.mode(OpenDrain); |
jack1930 | 5:66ca6c9b15f2 | 18 | _sda.output(); |
jack1930 | 5:66ca6c9b15f2 | 19 | _sda.mode(OpenDrain); |
jack1930 | 5:66ca6c9b15f2 | 20 | |
jack1930 | 5:66ca6c9b15f2 | 21 | _device_address = 0; |
jack1930 | 9:b08c2ffb9253 | 22 | if (HAL_GetDEVID () ==0x437) _frequency_delay = 20;//3; |
jack1930 | 9:b08c2ffb9253 | 23 | else _frequency_delay = 60;//3; |
jack1930 | 5:66ca6c9b15f2 | 24 | |
jack1930 | 5:66ca6c9b15f2 | 25 | initialise(); |
jack1930 | 5:66ca6c9b15f2 | 26 | } |
jack1930 | 5:66ca6c9b15f2 | 27 | |
jack1930 | 5:66ca6c9b15f2 | 28 | SoftwareI2C::~SoftwareI2C() { |
jack1930 | 5:66ca6c9b15f2 | 29 | |
jack1930 | 5:66ca6c9b15f2 | 30 | } |
jack1930 | 5:66ca6c9b15f2 | 31 | |
jack1930 | 5:66ca6c9b15f2 | 32 | /** |
jack1930 | 5:66ca6c9b15f2 | 33 | * @brief Read 1 or more bytes from the I2C slave |
jack1930 | 5:66ca6c9b15f2 | 34 | * @param device_address The address of the device to read from |
jack1930 | 5:66ca6c9b15f2 | 35 | * @param data An allocated array to read the data into |
jack1930 | 5:66ca6c9b15f2 | 36 | * @param data_bytes Number of bytes to read (must be equal to or less then the allocated memory in data) |
jack1930 | 5:66ca6c9b15f2 | 37 | */ |
jack1930 | 5:66ca6c9b15f2 | 38 | void SoftwareI2C::read(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { |
jack1930 | 5:66ca6c9b15f2 | 39 | if (data == 0 || data_bytes == 0) return; |
jack1930 | 7:c3d157395c16 | 40 | __disable_irq(); |
jack1930 | 5:66ca6c9b15f2 | 41 | device_address = device_address | 0x01; |
jack1930 | 5:66ca6c9b15f2 | 42 | start(); |
jack1930 | 5:66ca6c9b15f2 | 43 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 44 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 45 | for (int x = 0; x < data_bytes; ++x) { |
jack1930 | 5:66ca6c9b15f2 | 46 | data[x] = getByte(); |
jack1930 | 5:66ca6c9b15f2 | 47 | if ( x < (data_bytes -1)) { //ack all but the final byte |
jack1930 | 5:66ca6c9b15f2 | 48 | giveAck(); |
jack1930 | 5:66ca6c9b15f2 | 49 | } |
jack1930 | 5:66ca6c9b15f2 | 50 | } |
jack1930 | 5:66ca6c9b15f2 | 51 | stop(); |
jack1930 | 7:c3d157395c16 | 52 | __enable_irq(); |
jack1930 | 5:66ca6c9b15f2 | 53 | } |
jack1930 | 5:66ca6c9b15f2 | 54 | |
jack1930 | 5:66ca6c9b15f2 | 55 | /** |
jack1930 | 5:66ca6c9b15f2 | 56 | * @brief Write 1 or more bytes to the I2C slave |
jack1930 | 5:66ca6c9b15f2 | 57 | * @param device_address The address of the device to write to |
jack1930 | 5:66ca6c9b15f2 | 58 | * @param data An array to write the data from |
jack1930 | 5:66ca6c9b15f2 | 59 | * @param data_bytes Number of bytes to write from array |
jack1930 | 5:66ca6c9b15f2 | 60 | */ |
jack1930 | 5:66ca6c9b15f2 | 61 | void SoftwareI2C::write(uint8_t device_address, uint8_t* data, uint8_t data_bytes) { |
jack1930 | 5:66ca6c9b15f2 | 62 | if (data == 0 || data_bytes == 0) return; |
jack1930 | 7:c3d157395c16 | 63 | __disable_irq(); |
jack1930 | 5:66ca6c9b15f2 | 64 | device_address = device_address & 0xFE; |
jack1930 | 5:66ca6c9b15f2 | 65 | start(); |
jack1930 | 5:66ca6c9b15f2 | 66 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 67 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 68 | for ( int x = 0; x < data_bytes; ++x ) { |
jack1930 | 5:66ca6c9b15f2 | 69 | putByte(data[x]); |
jack1930 | 5:66ca6c9b15f2 | 70 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 71 | } |
jack1930 | 5:66ca6c9b15f2 | 72 | stop(); |
jack1930 | 7:c3d157395c16 | 73 | __enable_irq(); |
jack1930 | 5:66ca6c9b15f2 | 74 | } |
jack1930 | 5:66ca6c9b15f2 | 75 | |
jack1930 | 5:66ca6c9b15f2 | 76 | /** |
jack1930 | 5:66ca6c9b15f2 | 77 | * @brief Write 1 byte to the I2C slave |
jack1930 | 5:66ca6c9b15f2 | 78 | * @param device_address The address of the device to write to |
jack1930 | 5:66ca6c9b15f2 | 79 | * @param byte The data to write |
jack1930 | 5:66ca6c9b15f2 | 80 | */ |
jack1930 | 5:66ca6c9b15f2 | 81 | void SoftwareI2C::write(uint8_t device_address, uint8_t byte) { |
jack1930 | 5:66ca6c9b15f2 | 82 | device_address = device_address & 0xFE; |
jack1930 | 5:66ca6c9b15f2 | 83 | start(); |
jack1930 | 5:66ca6c9b15f2 | 84 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 85 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 86 | putByte(byte); |
jack1930 | 5:66ca6c9b15f2 | 87 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 88 | stop(); |
jack1930 | 5:66ca6c9b15f2 | 89 | } |
jack1930 | 5:66ca6c9b15f2 | 90 | |
jack1930 | 5:66ca6c9b15f2 | 91 | /** |
jack1930 | 5:66ca6c9b15f2 | 92 | * @brief Read 1 or more bytes from the I2C slave at the specified memory address |
jack1930 | 5:66ca6c9b15f2 | 93 | * @param device_address The address of the device to read from |
jack1930 | 5:66ca6c9b15f2 | 94 | * @param start_address The memory address to read from |
jack1930 | 5:66ca6c9b15f2 | 95 | * @param data The allocated array to read into |
jack1930 | 5:66ca6c9b15f2 | 96 | * @param data_bytes The number of bytes to read |
jack1930 | 5:66ca6c9b15f2 | 97 | */ |
jack1930 | 5:66ca6c9b15f2 | 98 | void SoftwareI2C::randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { |
jack1930 | 5:66ca6c9b15f2 | 99 | if (data == 0 || data_bytes == 0) return; |
jack1930 | 5:66ca6c9b15f2 | 100 | |
jack1930 | 5:66ca6c9b15f2 | 101 | device_address = device_address & 0xFE; |
jack1930 | 5:66ca6c9b15f2 | 102 | start(); |
jack1930 | 5:66ca6c9b15f2 | 103 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 104 | if (!getAck()) { |
jack1930 | 5:66ca6c9b15f2 | 105 | return; |
jack1930 | 5:66ca6c9b15f2 | 106 | } |
jack1930 | 5:66ca6c9b15f2 | 107 | putByte(start_address); |
jack1930 | 5:66ca6c9b15f2 | 108 | if (!getAck()) { |
jack1930 | 5:66ca6c9b15f2 | 109 | return; |
jack1930 | 5:66ca6c9b15f2 | 110 | } |
jack1930 | 5:66ca6c9b15f2 | 111 | |
jack1930 | 5:66ca6c9b15f2 | 112 | device_address=device_address | 0x01; |
jack1930 | 5:66ca6c9b15f2 | 113 | start(); |
jack1930 | 5:66ca6c9b15f2 | 114 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 115 | if (!getAck()) { |
jack1930 | 5:66ca6c9b15f2 | 116 | return; |
jack1930 | 5:66ca6c9b15f2 | 117 | } |
jack1930 | 5:66ca6c9b15f2 | 118 | for ( int x = 0; x < data_bytes; ++x) { |
jack1930 | 5:66ca6c9b15f2 | 119 | data[x] = getByte(); |
jack1930 | 5:66ca6c9b15f2 | 120 | if (x != (data_bytes - 1)) giveAck(); |
jack1930 | 5:66ca6c9b15f2 | 121 | } |
jack1930 | 5:66ca6c9b15f2 | 122 | stop(); |
jack1930 | 5:66ca6c9b15f2 | 123 | } |
jack1930 | 5:66ca6c9b15f2 | 124 | |
jack1930 | 5:66ca6c9b15f2 | 125 | /** |
jack1930 | 5:66ca6c9b15f2 | 126 | * @brief Write 1 byte to the I2C slave at the specified memory address |
jack1930 | 5:66ca6c9b15f2 | 127 | * @param device_address The address of the device to write to |
jack1930 | 5:66ca6c9b15f2 | 128 | * @param start_address The memory address to write to |
jack1930 | 5:66ca6c9b15f2 | 129 | * @param byte The data to write |
jack1930 | 5:66ca6c9b15f2 | 130 | */ |
jack1930 | 5:66ca6c9b15f2 | 131 | void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte) { |
jack1930 | 5:66ca6c9b15f2 | 132 | device_address = device_address & 0xFE; |
jack1930 | 5:66ca6c9b15f2 | 133 | start(); |
jack1930 | 5:66ca6c9b15f2 | 134 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 135 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 136 | putByte(start_address); |
jack1930 | 5:66ca6c9b15f2 | 137 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 138 | putByte(byte); |
jack1930 | 5:66ca6c9b15f2 | 139 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 140 | stop(); |
jack1930 | 5:66ca6c9b15f2 | 141 | } |
jack1930 | 5:66ca6c9b15f2 | 142 | |
jack1930 | 5:66ca6c9b15f2 | 143 | /** |
jack1930 | 5:66ca6c9b15f2 | 144 | * @brief Write 1 or more bytes to the I2C slave at the specified memory address |
jack1930 | 5:66ca6c9b15f2 | 145 | * @param device_address The address of the device to write to |
jack1930 | 5:66ca6c9b15f2 | 146 | * @param start_address The memory address to write to |
jack1930 | 5:66ca6c9b15f2 | 147 | * @param data The data to write |
jack1930 | 5:66ca6c9b15f2 | 148 | * @param data_bytes The number of bytes to write |
jack1930 | 5:66ca6c9b15f2 | 149 | */ |
jack1930 | 5:66ca6c9b15f2 | 150 | void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) { |
jack1930 | 5:66ca6c9b15f2 | 151 | if (data == 0 || data_bytes == 0) return; |
jack1930 | 5:66ca6c9b15f2 | 152 | |
jack1930 | 5:66ca6c9b15f2 | 153 | device_address = device_address & 0xFE; |
jack1930 | 5:66ca6c9b15f2 | 154 | start(); |
jack1930 | 5:66ca6c9b15f2 | 155 | putByte(device_address); |
jack1930 | 5:66ca6c9b15f2 | 156 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 157 | putByte(start_address); |
jack1930 | 5:66ca6c9b15f2 | 158 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 159 | for ( int x = 0; x <= data_bytes; ++x ) { |
jack1930 | 5:66ca6c9b15f2 | 160 | putByte(data[x]); |
jack1930 | 5:66ca6c9b15f2 | 161 | getAck(); |
jack1930 | 5:66ca6c9b15f2 | 162 | } |
jack1930 | 5:66ca6c9b15f2 | 163 | stop(); |
jack1930 | 5:66ca6c9b15f2 | 164 | } |
jack1930 | 5:66ca6c9b15f2 | 165 | |
jack1930 | 5:66ca6c9b15f2 | 166 | /** |
jack1930 | 5:66ca6c9b15f2 | 167 | * @brief Read 2 bytes from the I2C slave at the specified memory address and return them as an 16bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 168 | * @param device_address The address of the device to read from |
jack1930 | 5:66ca6c9b15f2 | 169 | * @param start_address The memory address to read from |
jack1930 | 5:66ca6c9b15f2 | 170 | * @return MSB 16bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 171 | */ |
jack1930 | 5:66ca6c9b15f2 | 172 | uint16_t SoftwareI2C::read16(uint8_t device_address, uint8_t start_address) { |
jack1930 | 5:66ca6c9b15f2 | 173 | uint8_t short_array[2] = {0, 0}; |
jack1930 | 5:66ca6c9b15f2 | 174 | randomRead(device_address, start_address, short_array, 2 ); |
jack1930 | 5:66ca6c9b15f2 | 175 | uint16_t value = 0; |
jack1930 | 5:66ca6c9b15f2 | 176 | value = short_array[0] << 8; |
jack1930 | 5:66ca6c9b15f2 | 177 | value |= short_array[1]; |
jack1930 | 5:66ca6c9b15f2 | 178 | |
jack1930 | 5:66ca6c9b15f2 | 179 | return value; |
jack1930 | 5:66ca6c9b15f2 | 180 | } |
jack1930 | 5:66ca6c9b15f2 | 181 | |
jack1930 | 5:66ca6c9b15f2 | 182 | /** |
jack1930 | 5:66ca6c9b15f2 | 183 | * @brief Read 3 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 184 | * @param device_address The address of the device to read from |
jack1930 | 5:66ca6c9b15f2 | 185 | * @param start_address The memory address to read from |
jack1930 | 5:66ca6c9b15f2 | 186 | * @return MSB 32bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 187 | */ |
jack1930 | 5:66ca6c9b15f2 | 188 | uint32_t SoftwareI2C::read24(uint8_t device_address, uint8_t start_address) { |
jack1930 | 5:66ca6c9b15f2 | 189 | uint8_t value_array[4] = {0, 0, 0}; |
jack1930 | 5:66ca6c9b15f2 | 190 | randomRead(device_address, start_address, value_array, 3 ); |
jack1930 | 5:66ca6c9b15f2 | 191 | uint32_t value = 0; |
jack1930 | 5:66ca6c9b15f2 | 192 | value = value_array[0] << 16; |
jack1930 | 5:66ca6c9b15f2 | 193 | value |= value_array[1] << 8; |
jack1930 | 5:66ca6c9b15f2 | 194 | value |= value_array[2]; |
jack1930 | 5:66ca6c9b15f2 | 195 | |
jack1930 | 5:66ca6c9b15f2 | 196 | return value; |
jack1930 | 5:66ca6c9b15f2 | 197 | } |
jack1930 | 5:66ca6c9b15f2 | 198 | |
jack1930 | 5:66ca6c9b15f2 | 199 | /** |
jack1930 | 5:66ca6c9b15f2 | 200 | * @brief Read 4 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 201 | * @param device_address The address of the device to read from |
jack1930 | 5:66ca6c9b15f2 | 202 | * @param start_address The memory address to read from |
jack1930 | 5:66ca6c9b15f2 | 203 | * @return MSB 32bit unsigned integer |
jack1930 | 5:66ca6c9b15f2 | 204 | */ |
jack1930 | 5:66ca6c9b15f2 | 205 | uint32_t SoftwareI2C::read32(uint8_t device_address, uint8_t start_address) { |
jack1930 | 5:66ca6c9b15f2 | 206 | uint8_t value_array[4] = {0, 0, 0, 0}; |
jack1930 | 5:66ca6c9b15f2 | 207 | randomRead(device_address, start_address, value_array, 4 ); |
jack1930 | 5:66ca6c9b15f2 | 208 | uint32_t value = 0; |
jack1930 | 5:66ca6c9b15f2 | 209 | value = value_array[0] << 24; |
jack1930 | 5:66ca6c9b15f2 | 210 | value |= value_array[1] << 16; |
jack1930 | 5:66ca6c9b15f2 | 211 | value |= value_array[2] << 8; |
jack1930 | 5:66ca6c9b15f2 | 212 | value |= value_array[3]; |
jack1930 | 5:66ca6c9b15f2 | 213 | |
jack1930 | 5:66ca6c9b15f2 | 214 | return value; |
jack1930 | 5:66ca6c9b15f2 | 215 | } |