LCD Baseshield GSOE GSOF

Dependents:   WebserverGSOE_Schnaiter HC05__TI4_RP5_Schnaiter

Committer:
jack1930
Date:
Tue Apr 14 12:55:29 2020 +0000
Revision:
5:66ca6c9b15f2
Child:
7:c3d157395c16
wait_us entfernt

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