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:
6:5fdc4ba80160
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 #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 5:66ca6c9b15f2 59 asm(
jack1930 5:66ca6c9b15f2 60 "nop\n\t"
jack1930 5:66ca6c9b15f2 61 );
jack1930 5:66ca6c9b15f2 62 }
jack1930 5:66ca6c9b15f2 63 }
jack1930 5:66ca6c9b15f2 64 inline void start() {
jack1930 5:66ca6c9b15f2 65 _sda.output();
jack1930 5:66ca6c9b15f2 66 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 67 _scl = 1;
jack1930 5:66ca6c9b15f2 68 _sda = 1;
jack1930 5:66ca6c9b15f2 69 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 70 _sda = 0;
jack1930 5:66ca6c9b15f2 71 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 72 _scl = 0;
jack1930 5:66ca6c9b15f2 73 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 74 }
jack1930 5:66ca6c9b15f2 75
jack1930 5:66ca6c9b15f2 76 inline void stop() {
jack1930 5:66ca6c9b15f2 77 _sda.output();
jack1930 5:66ca6c9b15f2 78 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 79 _sda = 0;
jack1930 5:66ca6c9b15f2 80 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 81 _scl = 1;
jack1930 5:66ca6c9b15f2 82 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 83 _sda = 1;
jack1930 5:66ca6c9b15f2 84 }
jack1930 5:66ca6c9b15f2 85
jack1930 5:66ca6c9b15f2 86 inline void putByte(uint8_t byte) {
jack1930 5:66ca6c9b15f2 87 _sda.output();
jack1930 5:66ca6c9b15f2 88 for ( int n = 8; n > 0; --n) {
jack1930 5:66ca6c9b15f2 89 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 90 _sda = byte & (1 << (n-1));
jack1930 5:66ca6c9b15f2 91 _scl = 1;
jack1930 5:66ca6c9b15f2 92 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 93 _scl = 0;
jack1930 5:66ca6c9b15f2 94 }
jack1930 5:66ca6c9b15f2 95 _sda = 1;
jack1930 5:66ca6c9b15f2 96 }
jack1930 5:66ca6c9b15f2 97
jack1930 5:66ca6c9b15f2 98 inline uint8_t getByte() {
jack1930 5:66ca6c9b15f2 99 uint8_t byte = 0;
jack1930 5:66ca6c9b15f2 100
jack1930 5:66ca6c9b15f2 101 _sda.input(); //release the data line
jack1930 5:66ca6c9b15f2 102 _sda.mode(OpenDrain);
jack1930 5:66ca6c9b15f2 103
jack1930 5:66ca6c9b15f2 104 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 105
jack1930 5:66ca6c9b15f2 106 for ( int n = 8; n > 0; --n ) {
jack1930 5:66ca6c9b15f2 107 _scl=1; //set clock high
jack1930 5:66ca6c9b15f2 108 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 109 byte |= _sda << (n-1); //read the bit
jack1930 5:66ca6c9b15f2 110 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 111 _scl=0; //set clock low
jack1930 5:66ca6c9b15f2 112 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 113 }
jack1930 5:66ca6c9b15f2 114
jack1930 5:66ca6c9b15f2 115 _sda.output(); //take data line back
jack1930 5:66ca6c9b15f2 116
jack1930 5:66ca6c9b15f2 117 return byte;
jack1930 5:66ca6c9b15f2 118 }
jack1930 5:66ca6c9b15f2 119
jack1930 5:66ca6c9b15f2 120 inline void giveAck() {
jack1930 5:66ca6c9b15f2 121 _sda.output();
jack1930 5:66ca6c9b15f2 122 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 123 _sda = 0;
jack1930 5:66ca6c9b15f2 124 _scl = 1;
jack1930 5:66ca6c9b15f2 125 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 126 _scl = 0;
jack1930 5:66ca6c9b15f2 127 _sda = 1;
jack1930 5:66ca6c9b15f2 128
jack1930 5:66ca6c9b15f2 129 }
jack1930 5:66ca6c9b15f2 130
jack1930 5:66ca6c9b15f2 131 inline bool getAck() {
jack1930 5:66ca6c9b15f2 132 _sda.output();
jack1930 5:66ca6c9b15f2 133 _sda = 1;
jack1930 5:66ca6c9b15f2 134 _scl = 1;
jack1930 5:66ca6c9b15f2 135 _sda.input();
jack1930 5:66ca6c9b15f2 136 _sda.mode(OpenDrain);
jack1930 5:66ca6c9b15f2 137 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 138 _scl = 0;
jack1930 5:66ca6c9b15f2 139
jack1930 5:66ca6c9b15f2 140 if(_sda != 0){return false;}
jack1930 5:66ca6c9b15f2 141
jack1930 5:66ca6c9b15f2 142 warte(_frequency_delay);
jack1930 5:66ca6c9b15f2 143 return true;
jack1930 5:66ca6c9b15f2 144 }
jack1930 5:66ca6c9b15f2 145
jack1930 5:66ca6c9b15f2 146 DigitalInOut _sda;
jack1930 5:66ca6c9b15f2 147 DigitalInOut _scl;
jack1930 5:66ca6c9b15f2 148
jack1930 5:66ca6c9b15f2 149 uint8_t _device_address;
jack1930 5:66ca6c9b15f2 150 uint32_t _frequency_delay;
jack1930 5:66ca6c9b15f2 151 };
jack1930 5:66ca6c9b15f2 152
jack1930 5:66ca6c9b15f2 153 #endif