PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
Pokitto
Date:
Mon Sep 18 11:47:51 2017 +0000
Revision:
0:e8b8f36b4505
Initial;

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 #ifndef _SOFTWARE_I2C_H_
Pokitto 0:e8b8f36b4505 8 #define _SOFTWARE_I2C_H_
Pokitto 0:e8b8f36b4505 9
Pokitto 0:e8b8f36b4505 10 #include "mbed.h"
Pokitto 0:e8b8f36b4505 11
Pokitto 0:e8b8f36b4505 12 /**
Pokitto 0:e8b8f36b4505 13 * @brief SoftwareI2C class
Pokitto 0:e8b8f36b4505 14 */
Pokitto 0:e8b8f36b4505 15
Pokitto 0:e8b8f36b4505 16 class SoftwareI2C {
Pokitto 0:e8b8f36b4505 17 public:
Pokitto 0:e8b8f36b4505 18 SoftwareI2C(PinName sda, PinName scl);
Pokitto 0:e8b8f36b4505 19 ~SoftwareI2C();
Pokitto 0:e8b8f36b4505 20
Pokitto 0:e8b8f36b4505 21 void read(uint8_t device_address, uint8_t* data, uint8_t data_bytes);
Pokitto 0:e8b8f36b4505 22 void write(uint8_t device_address, uint8_t* data, uint8_t data_bytes);
Pokitto 0:e8b8f36b4505 23 void write(uint8_t device_address, uint8_t byte);
Pokitto 0:e8b8f36b4505 24 void randomRead(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes);
Pokitto 0:e8b8f36b4505 25 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t* data, uint8_t data_bytes);
Pokitto 0:e8b8f36b4505 26 void randomWrite(uint8_t device_address, uint8_t start_address, uint8_t byte);
Pokitto 0:e8b8f36b4505 27
Pokitto 0:e8b8f36b4505 28 uint8_t read8(uint8_t device_address, uint8_t start_address);
Pokitto 0:e8b8f36b4505 29 uint16_t read16(uint8_t device_address, uint8_t start_address);
Pokitto 0:e8b8f36b4505 30 uint32_t read24(uint8_t device_address, uint8_t start_address);
Pokitto 0:e8b8f36b4505 31 uint32_t read32(uint8_t device_address, uint8_t start_address);
Pokitto 0:e8b8f36b4505 32
Pokitto 0:e8b8f36b4505 33 void setDeviceAddress(uint8_t address){
Pokitto 0:e8b8f36b4505 34 _device_address = address;
Pokitto 0:e8b8f36b4505 35 }
Pokitto 0:e8b8f36b4505 36
Pokitto 0:e8b8f36b4505 37 void setFrequency(uint32_t frequency){
Pokitto 0:e8b8f36b4505 38 _frequency_delay = 1000000 / frequency;
Pokitto 0:e8b8f36b4505 39 }
Pokitto 0:e8b8f36b4505 40
Pokitto 0:e8b8f36b4505 41 inline void initialise() {
Pokitto 0:e8b8f36b4505 42 _scl.output();
Pokitto 0:e8b8f36b4505 43 _sda.output();
Pokitto 0:e8b8f36b4505 44
Pokitto 0:e8b8f36b4505 45 _sda = 1;
Pokitto 0:e8b8f36b4505 46 _scl = 0;
Pokitto 0:e8b8f36b4505 47 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 48
Pokitto 0:e8b8f36b4505 49 for ( int n = 0; n <= 3; ++n ) {
Pokitto 0:e8b8f36b4505 50 stop();
Pokitto 0:e8b8f36b4505 51 }
Pokitto 0:e8b8f36b4505 52 }
Pokitto 0:e8b8f36b4505 53
Pokitto 0:e8b8f36b4505 54 private:
Pokitto 0:e8b8f36b4505 55 inline void start() {
Pokitto 0:e8b8f36b4505 56 _sda.output();
Pokitto 0:e8b8f36b4505 57 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 58 _scl = 1;
Pokitto 0:e8b8f36b4505 59 _sda = 1;
Pokitto 0:e8b8f36b4505 60 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 61 _sda = 0;
Pokitto 0:e8b8f36b4505 62 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 63 _scl = 0;
Pokitto 0:e8b8f36b4505 64 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 65 }
Pokitto 0:e8b8f36b4505 66
Pokitto 0:e8b8f36b4505 67 inline void stop() {
Pokitto 0:e8b8f36b4505 68 _sda.output();
Pokitto 0:e8b8f36b4505 69 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 70 _sda = 0;
Pokitto 0:e8b8f36b4505 71 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 72 _scl = 1;
Pokitto 0:e8b8f36b4505 73 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 74 _sda = 1;
Pokitto 0:e8b8f36b4505 75 }
Pokitto 0:e8b8f36b4505 76
Pokitto 0:e8b8f36b4505 77 inline void putByte(uint8_t byte) {
Pokitto 0:e8b8f36b4505 78 _sda.output();
Pokitto 0:e8b8f36b4505 79 for ( int n = 8; n > 0; --n) {
Pokitto 0:e8b8f36b4505 80 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 81 _sda = byte & (1 << (n-1));
Pokitto 0:e8b8f36b4505 82 _scl = 1;
Pokitto 0:e8b8f36b4505 83 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 84 _scl = 0;
Pokitto 0:e8b8f36b4505 85 }
Pokitto 0:e8b8f36b4505 86 _sda = 1;
Pokitto 0:e8b8f36b4505 87 }
Pokitto 0:e8b8f36b4505 88
Pokitto 0:e8b8f36b4505 89 inline uint8_t getByte() {
Pokitto 0:e8b8f36b4505 90 uint8_t byte = 0;
Pokitto 0:e8b8f36b4505 91
Pokitto 0:e8b8f36b4505 92 _sda.input(); //release the data line
Pokitto 0:e8b8f36b4505 93 _sda.mode(OpenDrain);
Pokitto 0:e8b8f36b4505 94
Pokitto 0:e8b8f36b4505 95 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 96
Pokitto 0:e8b8f36b4505 97 for ( int n = 8; n > 0; --n ) {
Pokitto 0:e8b8f36b4505 98 _scl=1; //set clock high
Pokitto 0:e8b8f36b4505 99 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 100 byte |= _sda << (n-1); //read the bit
Pokitto 0:e8b8f36b4505 101 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 102 _scl=0; //set clock low
Pokitto 0:e8b8f36b4505 103 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 104 }
Pokitto 0:e8b8f36b4505 105
Pokitto 0:e8b8f36b4505 106 _sda.output(); //take data line back
Pokitto 0:e8b8f36b4505 107
Pokitto 0:e8b8f36b4505 108 return byte;
Pokitto 0:e8b8f36b4505 109 }
Pokitto 0:e8b8f36b4505 110
Pokitto 0:e8b8f36b4505 111 inline void giveAck() {
Pokitto 0:e8b8f36b4505 112 _sda.output();
Pokitto 0:e8b8f36b4505 113 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 114 _sda = 0;
Pokitto 0:e8b8f36b4505 115 _scl = 1;
Pokitto 0:e8b8f36b4505 116 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 117 _scl = 0;
Pokitto 0:e8b8f36b4505 118 _sda = 1;
Pokitto 0:e8b8f36b4505 119
Pokitto 0:e8b8f36b4505 120 }
Pokitto 0:e8b8f36b4505 121
Pokitto 0:e8b8f36b4505 122 inline bool getAck() {
Pokitto 0:e8b8f36b4505 123 _sda.output();
Pokitto 0:e8b8f36b4505 124 _sda = 1;
Pokitto 0:e8b8f36b4505 125 _scl = 1;
Pokitto 0:e8b8f36b4505 126 _sda.input();
Pokitto 0:e8b8f36b4505 127 _sda.mode(OpenDrain);
Pokitto 0:e8b8f36b4505 128 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 129 _scl = 0;
Pokitto 0:e8b8f36b4505 130
Pokitto 0:e8b8f36b4505 131 if(_sda != 0){return false;}
Pokitto 0:e8b8f36b4505 132
Pokitto 0:e8b8f36b4505 133 wait_us(_frequency_delay);
Pokitto 0:e8b8f36b4505 134 return true;
Pokitto 0:e8b8f36b4505 135 }
Pokitto 0:e8b8f36b4505 136
Pokitto 0:e8b8f36b4505 137 DigitalInOut _sda;
Pokitto 0:e8b8f36b4505 138 DigitalInOut _scl;
Pokitto 0:e8b8f36b4505 139
Pokitto 0:e8b8f36b4505 140 uint8_t _device_address;
Pokitto 0:e8b8f36b4505 141 uint32_t _frequency_delay;
Pokitto 0:e8b8f36b4505 142 };
Pokitto 0:e8b8f36b4505 143
Pokitto 0:e8b8f36b4505 144 #endif
Pokitto 0:e8b8f36b4505 145