Skip to content

I2C - add functionality to change i2c clock speed #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion cores/arduino/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int i2c_openadapter(void)
{
int ret;

SET_PIN_MODE(24, I2C_MUX_MODE); // Rdx SOC PIN (Arduino header pin 18)
SET_PIN_MODE(24, I2C_MUX_MODE); // Rxd SOC PIN (Arduino header pin 18)
SET_PIN_MODE(25, I2C_MUX_MODE); // Txd SOC PIN (Arduino header pin 19)

SET_PIN_PULLUP(24, 1);
Expand All @@ -130,6 +130,38 @@ int i2c_openadapter(void)
return ret;
}

int i2c_openadapter_speed(int i2c_speed)
{
int ret;

SET_PIN_MODE(24, I2C_MUX_MODE); // Rxd SOC PIN (Arduino header pin 18)
SET_PIN_MODE(25, I2C_MUX_MODE); // Txd SOC PIN (Arduino header pin 19)

SET_PIN_PULLUP(24, 1);
SET_PIN_PULLUP(25, 1);

i2c_cfg_data_t i2c_cfg;
memset(&i2c_cfg, 0, sizeof(i2c_cfg_data_t));

i2c_cfg.speed = i2c_speed;
i2c_cfg.addressing_mode = I2C_7_Bit;
i2c_cfg.mode_type = I2C_MASTER;
i2c_cfg.cb_tx = ss_i2c_tx;
i2c_cfg.cb_rx = ss_i2c_rx;
i2c_cfg.cb_err = ss_i2c_err;

i2c_tx_complete = 0;
i2c_rx_complete = 0;
i2c_err_detect = 0;

ss_i2c_set_config(I2C_SENSING_0, &i2c_cfg);
ss_i2c_clock_enable(I2C_SENSING_0);
ret = wait_dev_ready(I2C_SENSING_0, false);

return ret;
}


void i2c_setslave(uint8_t addr)
{
i2c_slave = addr;
Expand Down
1 change: 1 addition & 0 deletions cores/arduino/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C"{
#define I2C_ERROR -2

int i2c_openadapter(void);
int i2c_openadapter_speed(int);
void i2c_setslave(uint8_t addr);
int i2c_writebytes(uint8_t *bytes, uint8_t length, bool no_stop);
int i2c_readbytes(uint8_t *buf, int length, bool no_stop);
Expand Down
5 changes: 5 additions & 0 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void TwoWire::begin(void)
init_status = i2c_openadapter();
}

void TwoWire::begin(int i2c_speed)
{
init_status = i2c_openadapter_speed(i2c_speed);
}

uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
int ret;
Expand Down
5 changes: 4 additions & 1 deletion libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
#include "Stream.h"
#include "variant.h"

#define BUFFER_LENGTH 32
#define BUFFER_LENGTH 32
#define I2C_SPEED_SLOW 1
#define I2C_SPEED_FAST 2

class TwoWire : public Stream {
public:
TwoWire(void);
void begin();
void begin(int);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation mismatch- perhaps 4 spaces were used instead of a tab?

void beginTransmission(uint8_t);
void beginTransmission(int);
uint8_t endTransmission(void);
Expand Down