Skip to content

Commit bd8ef73

Browse files
Merge pull request #302 from bmarquismarkail/rcmclone
Adding i2C repeated start condition from @bmarquismarkail
2 parents e4f5986 + 5404459 commit bd8ef73

6 files changed

Lines changed: 41 additions & 6 deletions

File tree

STM32F1/libraries/Wire/HardWire.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#include "HardWire.h"
4040

41-
uint8 HardWire::process() {
41+
uint8 HardWire::process(uint8 stop) {
4242
int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0);
4343
if (res == I2C_ERROR_PROTOCOL) {
4444
if (sel_hard->error_flags & I2C_SR1_AF) { /* NACK */
@@ -55,6 +55,10 @@ uint8 HardWire::process() {
5555
return res;
5656
}
5757

58+
uint8 HardWire::process(){
59+
return process(true);
60+
}
61+
5862
// TODO: Add in Error Handling if devsel is out of range for other Maples
5963
HardWire::HardWire(uint8 dev_sel, uint8 flags) {
6064
if (dev_sel == 1) {

STM32F1/libraries/Wire/HardWire.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class HardWire : public WireBase {
5252
* Processes the incoming I2C message defined by WireBase to the
5353
* hardware. If an error occured, restart the I2C device.
5454
*/
55+
uint8 process(uint8);
5556
uint8 process();
5657
public:
5758
/*

STM32F1/libraries/Wire/Wire.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
* Updated by Roger Clark. 20141111. Fixed issue when process() returned because of missing ACK (often caused by invalid device address being used), caused SCL to be left
4141
* LOW so that in the next call to process() , the first clock pulse was not sent, because SCL was LOW when it should have been high.
4242
*/
43+
/*
44+
* Updated by Brandon Green. 20172306. Implementing the repeated start functionality.
45+
*/
4346

4447
#include "Wire.h"
4548

@@ -76,6 +79,12 @@ void TwoWire::i2c_stop() {
7679
set_sda(HIGH);
7780
}
7881

82+
void TwoWire::i2c_repeated_start() {
83+
set_sda(HIGH);
84+
set_scl(HIGH);
85+
set_sda(LOW);
86+
}
87+
7988
bool TwoWire::i2c_get_ack() {
8089
set_scl(LOW);
8190
set_sda(HIGH);
@@ -121,7 +130,8 @@ void TwoWire::i2c_shift_out(uint8 val) {
121130
}
122131
}
123132

124-
uint8 TwoWire::process() {
133+
//process needs to be updated for repeated start.
134+
uint8 TwoWire::process(uint8 stop) {
125135
itc_msg.xferred = 0;
126136

127137
uint8 sla_addr = (itc_msg.addr << 1);
@@ -162,10 +172,18 @@ uint8 TwoWire::process() {
162172
itc_msg.xferred++;
163173
}
164174
}
165-
i2c_stop();
175+
if(stop == true)
176+
i2c_stop();
177+
else i2c_repeated_start();
178+
166179
return SUCCESS;
167180
}
168181

182+
// For compatibility with legacy code
183+
uint8 TwoWire::process(){
184+
return process(true);
185+
}
186+
169187
// TODO: Add in Error Handling if pins is out of range for other Maples
170188
// TODO: Make delays more capable
171189
TwoWire::TwoWire(uint8 scl, uint8 sda, uint8 delay) : i2c_delay(delay) {

STM32F1/libraries/Wire/Wire.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ class TwoWire : public WireBase {
8686
* Creates a Stop condition on the bus
8787
*/
8888
void i2c_stop();
89-
89+
90+
/*
91+
* Created a Repeated Start condition on the bus
92+
*/
93+
void i2c_repeated_start();
94+
9095
/*
9196
* Gets an ACK condition from a slave device on the bus
9297
*/
@@ -119,6 +124,7 @@ class TwoWire : public WireBase {
119124
/*
120125
* Processes the incoming I2C message defined by WireBase
121126
*/
127+
uint8 process(uint8);
122128
uint8 process();
123129
public:
124130
/*

STM32F1/libraries/Wire/WireBase.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,21 @@ void WireBase::beginTransmission(int slave_address) {
5959
beginTransmission((uint8)slave_address);
6060
}
6161

62-
uint8 WireBase::endTransmission(void) {
62+
uint8 WireBase::endTransmission(uint8 stop) {
6363
uint8 retVal;
6464
if (tx_buf_overflow) {
6565
return EDATA;
6666
}
67-
retVal = process();// Changed so that the return value from process is returned by this function see also the return line below
67+
retVal = process(stop);// Changed so that the return value from process is returned by this function see also the return line below
6868
tx_buf_idx = 0;
6969
tx_buf_overflow = false;
7070
return retVal;//SUCCESS;
7171
}
7272

73+
uint8 WireBase::endTransmission(){
74+
endTransmission(true);
75+
}
76+
7377
//TODO: Add the ability to queue messages (adding a boolean to end of function
7478
// call, allows for the Arduino style to stay while also giving the flexibility
7579
// to bulk send

STM32F1/libraries/Wire/WireBase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class WireBase { // Abstraction is awesome!
6565
boolean tx_buf_overflow;
6666

6767
// Force derived classes to define process function
68+
virtual uint8 process(uint8) = 0;
6869
virtual uint8 process() = 0;
6970
public:
7071
WireBase() {}
@@ -90,6 +91,7 @@ class WireBase { // Abstraction is awesome!
9091
* Call the process function to process the message if the TX
9192
* buffer has not overflowed.
9293
*/
94+
uint8 endTransmission(uint8);
9395
uint8 endTransmission(void);
9496

9597
/*

0 commit comments

Comments
 (0)