LCD Baseshield GSOE GSOF

Dependents:   WebserverGSOE_Schnaiter HC05__TI4_RP5_Schnaiter

Committer:
jack1930
Date:
Thu Sep 10 14:39:09 2020 +0000
Revision:
8:3bac0faac335
Parent:
7:c3d157395c16
Child:
9:b08c2ffb9253
freq delay 20

Who changed what in which revision?

UserRevisionLine numberNew 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 8:3bac0faac335 22 _frequency_delay = 20;//3;
jack1930 5:66ca6c9b15f2 23
jack1930 5:66ca6c9b15f2 24 initialise();
jack1930 5:66ca6c9b15f2 25 }
jack1930 5:66ca6c9b15f2 26
jack1930 5:66ca6c9b15f2 27 SoftwareI2C::~SoftwareI2C() {
jack1930 5:66ca6c9b15f2 28
jack1930 5:66ca6c9b15f2 29 }
jack1930 5:66ca6c9b15f2 30
jack1930 5:66ca6c9b15f2 31 /**
jack1930 5:66ca6c9b15f2 32 * @brief Read 1 or more bytes from the I2C slave
jack1930 5:66ca6c9b15f2 33 * @param device_address The address of the device to read from
jack1930 5:66ca6c9b15f2 34 * @param data An allocated array to read the data into
jack1930 5:66ca6c9b15f2 35 * @param data_bytes Number of bytes to read (must be equal to or less then the allocated memory in data)
jack1930 5:66ca6c9b15f2 36 */
jack1930 5:66ca6c9b15f2 37 void SoftwareI2C::read(uint8_t device_address, uint8_t* data, uint8_t data_bytes) {
jack1930 5:66ca6c9b15f2 38 if (data == 0 || data_bytes == 0) return;
jack1930 7:c3d157395c16 39 __disable_irq();
jack1930 5:66ca6c9b15f2 40 device_address = device_address | 0x01;
jack1930 5:66ca6c9b15f2 41 start();
jack1930 5:66ca6c9b15f2 42 putByte(device_address);
jack1930 5:66ca6c9b15f2 43 getAck();
jack1930 5:66ca6c9b15f2 44 for (int x = 0; x < data_bytes; ++x) {
jack1930 5:66ca6c9b15f2 45 data[x] = getByte();
jack1930 5:66ca6c9b15f2 46 if ( x < (data_bytes -1)) { //ack all but the final byte
jack1930 5:66ca6c9b15f2 47 giveAck();
jack1930 5:66ca6c9b15f2 48 }
jack1930 5:66ca6c9b15f2 49 }
jack1930 5:66ca6c9b15f2 50 stop();
jack1930 7:c3d157395c16 51 __enable_irq();
jack1930 5:66ca6c9b15f2 52 }
jack1930 5:66ca6c9b15f2 53
jack1930 5:66ca6c9b15f2 54 /**
jack1930 5:66ca6c9b15f2 55 * @brief Write 1 or more bytes to the I2C slave
jack1930 5:66ca6c9b15f2 56 * @param device_address The address of the device to write to
jack1930 5:66ca6c9b15f2 57 * @param data An array to write the data from
jack1930 5:66ca6c9b15f2 58 * @param data_bytes Number of bytes to write from array
jack1930 5:66ca6c9b15f2 59 */
jack1930 5:66ca6c9b15f2 60 void SoftwareI2C::write(uint8_t device_address, uint8_t* data, uint8_t data_bytes) {
jack1930 5:66ca6c9b15f2 61 if (data == 0 || data_bytes == 0) return;
jack1930 7:c3d157395c16 62 __disable_irq();
jack1930 5:66ca6c9b15f2 63 device_address = device_address & 0xFE;
jack1930 5:66ca6c9b15f2 64 start();
jack1930 5:66ca6c9b15f2 65 putByte(device_address);
jack1930 5:66ca6c9b15f2 66 getAck();
jack1930 5:66ca6c9b15f2 67 for ( int x = 0; x < data_bytes; ++x ) {
jack1930 5:66ca6c9b15f2 68 putByte(data[x]);
jack1930 5:66ca6c9b15f2 69 getAck();
jack1930 5:66ca6c9b15f2 70 }
jack1930 5:66ca6c9b15f2 71 stop();
jack1930 7:c3d157395c16 72 __enable_irq();
jack1930 5:66ca6c9b15f2 73 }
jack1930 5:66ca6c9b15f2 74
jack1930 5:66ca6c9b15f2 75 /**
jack1930 5:66ca6c9b15f2 76 * @brief Write 1 byte to the I2C slave
jack1930 5:66ca6c9b15f2 77 * @param device_address The address of the device to write to
jack1930 5:66ca6c9b15f2 78 * @param byte The data to write
jack1930 5:66ca6c9b15f2 79 */
jack1930 5:66ca6c9b15f2 80 void SoftwareI2C::write(uint8_t device_address, uint8_t byte) {
jack1930 5:66ca6c9b15f2 81 device_address = device_address & 0xFE;
jack1930 5:66ca6c9b15f2 82 start();
jack1930 5:66ca6c9b15f2 83 putByte(device_address);
jack1930 5:66ca6c9b15f2 84 getAck();
jack1930 5:66ca6c9b15f2 85 putByte(byte);
jack1930 5:66ca6c9b15f2 86 getAck();
jack1930 5:66ca6c9b15f2 87 stop();
jack1930 5:66ca6c9b15f2 88 }
jack1930 5:66ca6c9b15f2 89
jack1930 5:66ca6c9b15f2 90 /**
jack1930 5:66ca6c9b15f2 91 * @brief Read 1 or more bytes from the I2C slave at the specified memory address
jack1930 5:66ca6c9b15f2 92 * @param device_address The address of the device to read from
jack1930 5:66ca6c9b15f2 93 * @param start_address The memory address to read from
jack1930 5:66ca6c9b15f2 94 * @param data The allocated array to read into
jack1930 5:66ca6c9b15f2 95 * @param data_bytes The number of bytes to read
jack1930 5:66ca6c9b15f2 96 */
jack1930 5:66ca6c9b15f2 97 void SoftwareI2C::randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) {
jack1930 5:66ca6c9b15f2 98 if (data == 0 || data_bytes == 0) return;
jack1930 5:66ca6c9b15f2 99
jack1930 5:66ca6c9b15f2 100 device_address = device_address & 0xFE;
jack1930 5:66ca6c9b15f2 101 start();
jack1930 5:66ca6c9b15f2 102 putByte(device_address);
jack1930 5:66ca6c9b15f2 103 if (!getAck()) {
jack1930 5:66ca6c9b15f2 104 return;
jack1930 5:66ca6c9b15f2 105 }
jack1930 5:66ca6c9b15f2 106 putByte(start_address);
jack1930 5:66ca6c9b15f2 107 if (!getAck()) {
jack1930 5:66ca6c9b15f2 108 return;
jack1930 5:66ca6c9b15f2 109 }
jack1930 5:66ca6c9b15f2 110
jack1930 5:66ca6c9b15f2 111 device_address=device_address | 0x01;
jack1930 5:66ca6c9b15f2 112 start();
jack1930 5:66ca6c9b15f2 113 putByte(device_address);
jack1930 5:66ca6c9b15f2 114 if (!getAck()) {
jack1930 5:66ca6c9b15f2 115 return;
jack1930 5:66ca6c9b15f2 116 }
jack1930 5:66ca6c9b15f2 117 for ( int x = 0; x < data_bytes; ++x) {
jack1930 5:66ca6c9b15f2 118 data[x] = getByte();
jack1930 5:66ca6c9b15f2 119 if (x != (data_bytes - 1)) giveAck();
jack1930 5:66ca6c9b15f2 120 }
jack1930 5:66ca6c9b15f2 121 stop();
jack1930 5:66ca6c9b15f2 122 }
jack1930 5:66ca6c9b15f2 123
jack1930 5:66ca6c9b15f2 124 /**
jack1930 5:66ca6c9b15f2 125 * @brief Write 1 byte to the I2C slave at the specified memory address
jack1930 5:66ca6c9b15f2 126 * @param device_address The address of the device to write to
jack1930 5:66ca6c9b15f2 127 * @param start_address The memory address to write to
jack1930 5:66ca6c9b15f2 128 * @param byte The data to write
jack1930 5:66ca6c9b15f2 129 */
jack1930 5:66ca6c9b15f2 130 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte) {
jack1930 5:66ca6c9b15f2 131 device_address = device_address & 0xFE;
jack1930 5:66ca6c9b15f2 132 start();
jack1930 5:66ca6c9b15f2 133 putByte(device_address);
jack1930 5:66ca6c9b15f2 134 getAck();
jack1930 5:66ca6c9b15f2 135 putByte(start_address);
jack1930 5:66ca6c9b15f2 136 getAck();
jack1930 5:66ca6c9b15f2 137 putByte(byte);
jack1930 5:66ca6c9b15f2 138 getAck();
jack1930 5:66ca6c9b15f2 139 stop();
jack1930 5:66ca6c9b15f2 140 }
jack1930 5:66ca6c9b15f2 141
jack1930 5:66ca6c9b15f2 142 /**
jack1930 5:66ca6c9b15f2 143 * @brief Write 1 or more bytes to the I2C slave at the specified memory address
jack1930 5:66ca6c9b15f2 144 * @param device_address The address of the device to write to
jack1930 5:66ca6c9b15f2 145 * @param start_address The memory address to write to
jack1930 5:66ca6c9b15f2 146 * @param data The data to write
jack1930 5:66ca6c9b15f2 147 * @param data_bytes The number of bytes to write
jack1930 5:66ca6c9b15f2 148 */
jack1930 5:66ca6c9b15f2 149 void SoftwareI2C::randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes) {
jack1930 5:66ca6c9b15f2 150 if (data == 0 || data_bytes == 0) return;
jack1930 5:66ca6c9b15f2 151
jack1930 5:66ca6c9b15f2 152 device_address = device_address & 0xFE;
jack1930 5:66ca6c9b15f2 153 start();
jack1930 5:66ca6c9b15f2 154 putByte(device_address);
jack1930 5:66ca6c9b15f2 155 getAck();
jack1930 5:66ca6c9b15f2 156 putByte(start_address);
jack1930 5:66ca6c9b15f2 157 getAck();
jack1930 5:66ca6c9b15f2 158 for ( int x = 0; x <= data_bytes; ++x ) {
jack1930 5:66ca6c9b15f2 159 putByte(data[x]);
jack1930 5:66ca6c9b15f2 160 getAck();
jack1930 5:66ca6c9b15f2 161 }
jack1930 5:66ca6c9b15f2 162 stop();
jack1930 5:66ca6c9b15f2 163 }
jack1930 5:66ca6c9b15f2 164
jack1930 5:66ca6c9b15f2 165 /**
jack1930 5:66ca6c9b15f2 166 * @brief Read 2 bytes from the I2C slave at the specified memory address and return them as an 16bit unsigned integer
jack1930 5:66ca6c9b15f2 167 * @param device_address The address of the device to read from
jack1930 5:66ca6c9b15f2 168 * @param start_address The memory address to read from
jack1930 5:66ca6c9b15f2 169 * @return MSB 16bit unsigned integer
jack1930 5:66ca6c9b15f2 170 */
jack1930 5:66ca6c9b15f2 171 uint16_t SoftwareI2C::read16(uint8_t device_address, uint8_t start_address) {
jack1930 5:66ca6c9b15f2 172 uint8_t short_array[2] = {0, 0};
jack1930 5:66ca6c9b15f2 173 randomRead(device_address, start_address, short_array, 2 );
jack1930 5:66ca6c9b15f2 174 uint16_t value = 0;
jack1930 5:66ca6c9b15f2 175 value = short_array[0] << 8;
jack1930 5:66ca6c9b15f2 176 value |= short_array[1];
jack1930 5:66ca6c9b15f2 177
jack1930 5:66ca6c9b15f2 178 return value;
jack1930 5:66ca6c9b15f2 179 }
jack1930 5:66ca6c9b15f2 180
jack1930 5:66ca6c9b15f2 181 /**
jack1930 5:66ca6c9b15f2 182 * @brief Read 3 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer
jack1930 5:66ca6c9b15f2 183 * @param device_address The address of the device to read from
jack1930 5:66ca6c9b15f2 184 * @param start_address The memory address to read from
jack1930 5:66ca6c9b15f2 185 * @return MSB 32bit unsigned integer
jack1930 5:66ca6c9b15f2 186 */
jack1930 5:66ca6c9b15f2 187 uint32_t SoftwareI2C::read24(uint8_t device_address, uint8_t start_address) {
jack1930 5:66ca6c9b15f2 188 uint8_t value_array[4] = {0, 0, 0};
jack1930 5:66ca6c9b15f2 189 randomRead(device_address, start_address, value_array, 3 );
jack1930 5:66ca6c9b15f2 190 uint32_t value = 0;
jack1930 5:66ca6c9b15f2 191 value = value_array[0] << 16;
jack1930 5:66ca6c9b15f2 192 value |= value_array[1] << 8;
jack1930 5:66ca6c9b15f2 193 value |= value_array[2];
jack1930 5:66ca6c9b15f2 194
jack1930 5:66ca6c9b15f2 195 return value;
jack1930 5:66ca6c9b15f2 196 }
jack1930 5:66ca6c9b15f2 197
jack1930 5:66ca6c9b15f2 198 /**
jack1930 5:66ca6c9b15f2 199 * @brief Read 4 bytes from the I2C slave at the specified memory address and return them as an 32bit unsigned integer
jack1930 5:66ca6c9b15f2 200 * @param device_address The address of the device to read from
jack1930 5:66ca6c9b15f2 201 * @param start_address The memory address to read from
jack1930 5:66ca6c9b15f2 202 * @return MSB 32bit unsigned integer
jack1930 5:66ca6c9b15f2 203 */
jack1930 5:66ca6c9b15f2 204 uint32_t SoftwareI2C::read32(uint8_t device_address, uint8_t start_address) {
jack1930 5:66ca6c9b15f2 205 uint8_t value_array[4] = {0, 0, 0, 0};
jack1930 5:66ca6c9b15f2 206 randomRead(device_address, start_address, value_array, 4 );
jack1930 5:66ca6c9b15f2 207 uint32_t value = 0;
jack1930 5:66ca6c9b15f2 208 value = value_array[0] << 24;
jack1930 5:66ca6c9b15f2 209 value |= value_array[1] << 16;
jack1930 5:66ca6c9b15f2 210 value |= value_array[2] << 8;
jack1930 5:66ca6c9b15f2 211 value |= value_array[3];
jack1930 5:66ca6c9b15f2 212
jack1930 5:66ca6c9b15f2 213 return value;
jack1930 5:66ca6c9b15f2 214 }