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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Wed Dec 25 23:59:52 2019 +0000
Revision:
71:531419862202
Parent:
31:f4b9b85c7b62
Changed Mode2 C++ refresh code (graphical errors)

Who changed what in which revision?

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