LCD Baseshield GSOE GSOF

Dependents:   WebserverGSOE_Schnaiter HC05__TI4_RP5_Schnaiter

Committer:
jack1930
Date:
Mon Apr 27 19:59:58 2020 +0000
Revision:
6:5fdc4ba80160
Parent:
5:66ca6c9b15f2
warte verbessert

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