PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
0:e8b8f36b4505
direct lcd stuff used by sensitive

Who changed what in which revision?

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