Jack Hansdampf / SoftwareI2C
Committer:
jack1930
Date:
Tue Apr 14 12:49:03 2020 +0000
Revision:
4:07b9f0e0424c
Parent:
2:8670e78c4b63
wait_us ersetzt

Who changed what in which revision?

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