Skip to content

Commit 9be7604

Browse files
bigdinotechcalvinatintel
authored andcommitted
I2C - add functionality to change i2c clock speed
-adds functionality to change clock speed -clock speed is set/changed when Wire.begin() is called -default is standard mode -use Wire.begin() for standard speed (100k) -use Wire.begin(I2C_SPEED_FAST) for full speed (400k) -use Wire.setClock(I2C_SPEED_FAST) for full speed (400k)
1 parent 8682149 commit 9be7604

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

cores/arduino/i2c.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void ss_i2c_err(uint32_t dev_id)
4949
}
5050

5151
static int wait_rx_or_err(bool no_stop){
52-
uint64_t timeout = TIMEOUT_MS * 20;
52+
uint64_t timeout = TIMEOUT_MS * 200;
5353
while(timeout--) {
5454
if (i2c_err_detect) {
5555
if (i2c_err_source & I2C_ABRT_7B_ADDR_NOACK )
@@ -70,7 +70,7 @@ static int wait_rx_or_err(bool no_stop){
7070
return I2C_OK;
7171
}
7272
}
73-
delayMicroseconds(50);
73+
delayMicroseconds(10);
7474
}
7575
if (!no_stop)
7676
return I2C_TIMEOUT;
@@ -79,7 +79,7 @@ static int wait_rx_or_err(bool no_stop){
7979
}
8080

8181
static int wait_tx_or_err(bool no_stop){
82-
uint64_t timeout = TIMEOUT_MS * 20;
82+
uint64_t timeout = TIMEOUT_MS * 200;
8383
while(timeout--) {
8484
if (i2c_err_detect) {
8585
if (i2c_err_source & I2C_ABRT_7B_ADDR_NOACK )
@@ -100,7 +100,7 @@ static int wait_tx_or_err(bool no_stop){
100100
return I2C_OK;
101101
}
102102
}
103-
delayMicroseconds(50);
103+
delayMicroseconds(10);
104104
}
105105
if (!no_stop)
106106
return I2C_TIMEOUT;
@@ -109,15 +109,15 @@ static int wait_tx_or_err(bool no_stop){
109109
}
110110

111111
static int wait_dev_ready(I2C_CONTROLLER controller_id, bool no_stop){
112-
uint64_t timeout = TIMEOUT_MS * 20;
112+
uint64_t timeout = TIMEOUT_MS * 200;
113113
int ret = 0;
114114
while(timeout--) {
115115
ret = ss_i2c_status(controller_id, no_stop);
116116
if (ret == I2C_OK) {
117117
return I2C_OK;
118118
}
119119
if (ret == I2C_BUSY) {
120-
delayMicroseconds(50);
120+
delayMicroseconds(10);
121121
}
122122
}
123123
return I2C_TIMEOUT - ret;
@@ -128,7 +128,7 @@ int i2c_openadapter(void)
128128
{
129129
int ret;
130130

131-
SET_PIN_MODE(24, I2C_MUX_MODE); // Rdx SOC PIN (Arduino header pin 18)
131+
SET_PIN_MODE(24, I2C_MUX_MODE); // Rxd SOC PIN (Arduino header pin 18)
132132
SET_PIN_MODE(25, I2C_MUX_MODE); // Txd SOC PIN (Arduino header pin 19)
133133

134134
SET_PIN_PULLUP(24, 1);
@@ -156,6 +156,38 @@ int i2c_openadapter(void)
156156
return ret;
157157
}
158158

159+
int i2c_openadapter_speed(int i2c_speed)
160+
{
161+
int ret;
162+
163+
SET_PIN_MODE(24, I2C_MUX_MODE); // Rxd SOC PIN (Arduino header pin 18)
164+
SET_PIN_MODE(25, I2C_MUX_MODE); // Txd SOC PIN (Arduino header pin 19)
165+
166+
SET_PIN_PULLUP(24, 1);
167+
SET_PIN_PULLUP(25, 1);
168+
169+
i2c_cfg_data_t i2c_cfg;
170+
memset(&i2c_cfg, 0, sizeof(i2c_cfg_data_t));
171+
172+
i2c_cfg.speed = i2c_speed;
173+
i2c_cfg.addressing_mode = I2C_7_Bit;
174+
i2c_cfg.mode_type = I2C_MASTER;
175+
i2c_cfg.cb_tx = ss_i2c_tx;
176+
i2c_cfg.cb_rx = ss_i2c_rx;
177+
i2c_cfg.cb_err = ss_i2c_err;
178+
179+
i2c_tx_complete = 0;
180+
i2c_rx_complete = 0;
181+
i2c_err_detect = 0;
182+
183+
ss_i2c_set_config(I2C_SENSING_0, &i2c_cfg);
184+
ss_i2c_clock_enable(I2C_SENSING_0);
185+
ret = wait_dev_ready(I2C_SENSING_0, false);
186+
187+
return ret;
188+
}
189+
190+
159191
void i2c_setslave(uint8_t addr)
160192
{
161193
i2c_slave = addr;

cores/arduino/i2c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C"{
3838
#define I2C_ABRT_TXDATA_NOACK (1 << 3)
3939

4040
int i2c_openadapter(void);
41+
int i2c_openadapter_speed(int);
4142
void i2c_setslave(uint8_t addr);
4243
int i2c_writebytes(uint8_t *bytes, uint8_t length, bool no_stop);
4344
int i2c_readbytes(uint8_t *buf, int length, bool no_stop);

libraries/Wire/src/Wire.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ void TwoWire::begin(void)
4141
init_status = i2c_openadapter();
4242
}
4343

44+
void TwoWire::begin(int i2c_speed)
45+
{
46+
init_status = i2c_openadapter_speed(i2c_speed);
47+
}
48+
49+
void TwoWire::setClock(long speed)
50+
{
51+
if(speed == 400000L) {
52+
init_status = i2c_openadapter_speed(I2C_SPEED_FAST);
53+
} else if(speed == 100000L) {
54+
init_status = i2c_openadapter_speed(I2C_SPEED_SLOW);
55+
} else if(speed == I2C_SPEED_FAST) {
56+
init_status = i2c_openadapter_speed(I2C_SPEED_FAST);
57+
}else {
58+
init_status = i2c_openadapter();
59+
}
60+
}
61+
4462
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
4563
{
4664
int ret;

libraries/Wire/src/Wire.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
#include "Stream.h"
2525
#include "variant.h"
2626

27-
#define BUFFER_LENGTH 32
27+
#define BUFFER_LENGTH 32
28+
#define I2C_SPEED_SLOW 1
29+
#define I2C_SPEED_FAST 2
2830

2931
class TwoWire : public Stream {
3032
public:
3133
TwoWire(void);
3234
void begin();
35+
void begin(int);
36+
void setClock(long speed);
3337
void beginTransmission(uint8_t);
3438
void beginTransmission(int);
3539
uint8_t endTransmission(void);

0 commit comments

Comments
 (0)