rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes
BitbangI2c Class Reference

#include <i2c_bb.h>

Collaboration diagram for BitbangI2c:
Collaboration graph
[legend]

Public Member Functions

bool init (brain_pin_e scl, brain_pin_e sda)
 
void deinit ()
 
void write (uint8_t addr, const uint8_t *data, size_t size)
 
void read (uint8_t addr, uint8_t *data, size_t size)
 
void writeRead (uint8_t addr, const uint8_t *writeData, size_t writeSize, uint8_t *readData, size_t readSize)
 
uint8_t readRegister (uint8_t addr, uint8_t reg)
 
void writeRegister (uint8_t addr, uint8_t reg, uint8_t val)
 

Private Member Functions

bool writeByte (uint8_t data)
 
uint8_t readByte (bool ack)
 
void sda_low ()
 
void sda_high ()
 
void scl_low ()
 
void scl_high ()
 
void start ()
 
void stop ()
 
void sendBit (bool val)
 
bool readBit ()
 
void waitQuarterBit ()
 

Private Attributes

ioportid_t m_sclPort = 0
 
ioportmask_t m_sclPin = 0
 
ioportid_t m_sdaPort = 0
 
ioportmask_t m_sdaPin = 0
 

Detailed Description

Definition at line 11 of file i2c_bb.h.

Member Function Documentation

◆ deinit()

void BitbangI2c::deinit ( )

Definition at line 64 of file i2c_bb.cpp.

64 {
65#if EFI_PROD_CODE
66 if (m_sclPort) {
68 m_sclPort = NULL;
69 }
70 if (m_sdaPort) {
72 m_sdaPort = NULL;
73 }
74#endif
75}
ioportid_t m_sclPort
Definition i2c_bb.h:54
ioportmask_t m_sdaPin
Definition i2c_bb.h:57
ioportid_t m_sdaPort
Definition i2c_bb.h:56
ioportmask_t m_sclPin
Definition i2c_bb.h:55
void gpio_pin_markUnused(ioportid_t port, ioportmask_t pin)

Referenced by getBoardRevision().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init()

bool BitbangI2c::init ( brain_pin_e  scl,
brain_pin_e  sda 
)

Definition at line 37 of file i2c_bb.cpp.

37 {
38#if EFI_PROD_CODE
39 if (m_sdaPort) {
40 return false;
41 }
42
43 if (!isBrainPinValid(scl) || !isBrainPinValid(sda)) {
44 return false;
45 }
46
47 efiSetPadMode("i2c", scl, PAL_MODE_OUTPUT_OPENDRAIN); //PAL_STM32_OTYPE_OPENDRAIN
48 efiSetPadMode("i2c", sda, PAL_MODE_OUTPUT_OPENDRAIN);
49
50 m_sclPort = getHwPort("i2c", scl);
51 m_sclPin = getHwPin("i2c", scl);
52
53 m_sdaPort = getHwPort("i2c", sda);
54 m_sdaPin = getHwPin("i2c", sda);
55#endif
56
57 // Both lines idle high
58 scl_high();
59 sda_high();
60
61 return true;
62}
void efiSetPadMode(const char *msg, brain_pin_e brainPin, iomode_t mode)
void sda_high()
Definition i2c_bb.cpp:13
void scl_high()
Definition i2c_bb.cpp:25
ioportid_t getHwPort(const char *msg, brain_pin_e brainPin)
ioportmask_t getHwPin(const char *msg, brain_pin_e brainPin)
bool isBrainPinValid(brain_pin_e brainPin)

Referenced by getBoardRevision(), Ads1015::init(), Lps25::init(), and setHellen128ETBConfig().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

void BitbangI2c::read ( uint8_t  addr,
uint8_t *  data,
size_t  size 
)

Definition at line 210 of file i2c_bb.cpp.

210 {
211 start();
212
213 // Address + read
214 writeByte(addr << 1 | 1);
215
216 for (size_t i = 0; i < readSize - 1; i++) {
217 // All but the last byte send ACK to indicate we're still reading
218 readData[i] = readByte(true);
219 }
220
221 // last byte sends NAK to indicate we're done reading
222 readData[readSize - 1] = readByte(false);
223
224 stop();
225}
constexpr uint8_t addr
Definition ads1015.cpp:14
bool writeByte(uint8_t data)
Definition i2c_bb.cpp:143
void start()
Definition i2c_bb.cpp:77
void stop()
Definition i2c_bb.cpp:91
uint8_t readByte(bool ack)
Definition i2c_bb.cpp:163

Referenced by Ads1015::readReg(), setHellen128ETBConfig(), and writeRead().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBit()

bool BitbangI2c::readBit ( )
private

Definition at line 122 of file i2c_bb.cpp.

122 {
124
125 scl_high();
126
129
130#if EFI_PROD_CODE
131 // Read just before we set the clock low (ie, as late as possible)
132 bool val = palReadPad(m_sdaPort, m_sdaPin);
133#else
134 bool val = false;
135#endif
136
137 scl_low();
139
140 return val;
141}
void waitQuarterBit()
Definition i2c_bb.cpp:183
void scl_low()
Definition i2c_bb.cpp:31

Referenced by readByte(), and writeByte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readByte()

uint8_t BitbangI2c::readByte ( bool  ack)
private

Definition at line 163 of file i2c_bb.cpp.

163 {
164 uint8_t result = 0;
165
166 // Read in 8 data bits
167 for (size_t i = 0; i < 8; i++) {
168 result = result << 1;
169
170 result |= readBit() ? 1 : 0;
171 }
172
173 // 0 -> ack
174 // 1 -> nack
175 sendBit(!ack);
176
177 // release SDA
178 sda_high();
179
180 return result;
181}
bool readBit()
Definition i2c_bb.cpp:122
void sendBit(bool val)
Definition i2c_bb.cpp:102

Referenced by read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readRegister()

uint8_t BitbangI2c::readRegister ( uint8_t  addr,
uint8_t  reg 
)

Definition at line 227 of file i2c_bb.cpp.

227 {
228 uint8_t retval;
229
230 writeRead(addr, &reg, 1, &retval, 1);
231
232 return retval;
233}
void writeRead(uint8_t addr, const uint8_t *writeData, size_t writeSize, uint8_t *readData, size_t readSize)
Definition i2c_bb.cpp:204

Referenced by Lps25::init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ scl_high()

void BitbangI2c::scl_high ( )
private

Definition at line 25 of file i2c_bb.cpp.

25 {
26#if EFI_PROD_CODE
27 palSetPad(m_sclPort, m_sclPin);
28#endif
29}

Referenced by init(), readBit(), sendBit(), start(), and stop().

Here is the caller graph for this function:

◆ scl_low()

void BitbangI2c::scl_low ( )
private

Definition at line 31 of file i2c_bb.cpp.

31 {
32#if EFI_PROD_CODE
33 palClearPad(m_sclPort, m_sclPin);
34#endif
35}

Referenced by readBit(), sendBit(), start(), and stop().

Here is the caller graph for this function:

◆ sda_high()

void BitbangI2c::sda_high ( )
private

Definition at line 13 of file i2c_bb.cpp.

13 {
14#if EFI_PROD_CODE
15 palSetPad(m_sdaPort, m_sdaPin);
16#endif
17}

Referenced by init(), readByte(), sendBit(), start(), stop(), and writeByte().

Here is the caller graph for this function:

◆ sda_low()

void BitbangI2c::sda_low ( )
private

Definition at line 19 of file i2c_bb.cpp.

19 {
20#if EFI_PROD_CODE
21 palClearPad(m_sdaPort, m_sdaPin);
22#endif
23}

Referenced by sendBit(), start(), and stop().

Here is the caller graph for this function:

◆ sendBit()

void BitbangI2c::sendBit ( bool  val)
private

Definition at line 102 of file i2c_bb.cpp.

102 {
104
105 // Write the bit (write while SCL is low)
106 if (val) {
107 sda_high();
108 } else {
109 sda_low();
110 }
111
112 // Data setup time (~100ns min)
114
115 // Strobe the clock
116 scl_high();
118 scl_low();
120}
void sda_low()
Definition i2c_bb.cpp:19

Referenced by readByte(), and writeByte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start()

void BitbangI2c::start ( )
private

Definition at line 77 of file i2c_bb.cpp.

77 {
78 // Start with both lines high (bus idle)
79 sda_high();
81 scl_high();
83
84 // SDA goes low while SCL is high
85 sda_low();
87 scl_low();
89}

Referenced by read(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void BitbangI2c::stop ( )
private

Definition at line 91 of file i2c_bb.cpp.

91 {
92 scl_low();
94 sda_low();
96 scl_high();
98 // SDA goes high while SCL is high
99 sda_high();
100}

Referenced by read(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ waitQuarterBit()

void BitbangI2c::waitQuarterBit ( )
private

Definition at line 183 of file i2c_bb.cpp.

183 {
184 // This yields a bitrate of about 320khz on a 168MHz F4
185 for (size_t i = 0; i < 30; i++) {
186 __asm__ volatile ("nop");
187 }
188}

Referenced by readBit(), sendBit(), start(), and stop().

Here is the caller graph for this function:

◆ write()

void BitbangI2c::write ( uint8_t  addr,
const uint8_t *  data,
size_t  size 
)

Definition at line 190 of file i2c_bb.cpp.

190 {
191 start();
192
193 // Address + write
194 writeByte(addr << 1 | 0);
195
196 // Write outbound bytes
197 for (size_t i = 0; i < writeSize; i++) {
198 writeByte(writeData[i]);
199 }
200
201 stop();
202}

Referenced by getBoardRevision(), Ads1015::readReg(), writeRead(), Ads1015::writeReg(), and writeRegister().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeByte()

bool BitbangI2c::writeByte ( uint8_t  data)
private

Definition at line 143 of file i2c_bb.cpp.

143 {
144 // write out 8 data bits
145 for (size_t i = 0; i < 8; i++) {
146 // Send the MSB
147 sendBit((data & 0x80) != 0);
148
149 data = data << 1;
150 }
151
152 // Force a release of the data line so the slave can ACK
153 sda_high();
154
155 // Read the ack bit
156 bool ackBit = readBit();
157
158 // 0 -> ack
159 // 1 -> nack
160 return !ackBit;
161}

Referenced by read(), and write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeRead()

void BitbangI2c::writeRead ( uint8_t  addr,
const uint8_t *  writeData,
size_t  writeSize,
uint8_t *  readData,
size_t  readSize 
)

Definition at line 204 of file i2c_bb.cpp.

204 {
205 write(addr, writeData, writeSize);
206
207 read(addr, readData, readSize);
208}
void read(uint8_t addr, uint8_t *data, size_t size)
Definition i2c_bb.cpp:210
void write(uint8_t addr, const uint8_t *data, size_t size)
Definition i2c_bb.cpp:190

Referenced by getBoardRevision(), Lps25::readPressureKpa(), and readRegister().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeRegister()

void BitbangI2c::writeRegister ( uint8_t  addr,
uint8_t  reg,
uint8_t  val 
)

Definition at line 235 of file i2c_bb.cpp.

235 {
236 uint8_t buf[2];
237 buf[0] = reg;
238 buf[1] = val;
239
240 write(addr, buf, 2);
241}

Referenced by Lps25::init().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ m_sclPin

ioportmask_t BitbangI2c::m_sclPin = 0
private

Definition at line 55 of file i2c_bb.h.

Referenced by deinit(), init(), scl_high(), and scl_low().

◆ m_sclPort

ioportid_t BitbangI2c::m_sclPort = 0
private

Definition at line 54 of file i2c_bb.h.

Referenced by deinit(), init(), scl_high(), and scl_low().

◆ m_sdaPin

ioportmask_t BitbangI2c::m_sdaPin = 0
private

Definition at line 57 of file i2c_bb.h.

Referenced by deinit(), init(), readBit(), sda_high(), and sda_low().

◆ m_sdaPort

ioportid_t BitbangI2c::m_sdaPort = 0
private

Definition at line 56 of file i2c_bb.h.

Referenced by deinit(), init(), readBit(), sda_high(), and sda_low().


The documentation for this class was generated from the following files: