Skip to content

Commit

Permalink
Make software I2C bitrate and waveform more accurate
Browse files Browse the repository at this point in the history
            Move default I2C bitrate to 100kbit/sec
  • Loading branch information
gfwilliams committed May 17, 2018
1 parent 972584b commit 3739ee5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -18,6 +18,8 @@
ESP32: Fix accidental initialisation of UART3 when switching to Telnet (fix #1362)
nRF52: Added `NRF.setAddress` to allow the MAC address to be changed
Added Graphics.setFontAlign for font alignment and rotation
Make software I2C bitrate and waveform more accurate
Move default I2C bitrate to 100kbit/sec

1v97 : nRF52: fix NRF.on('connect',...) issue
STM32: Fix setDeviceClockCmd error for USB.setConsole()
Expand Down
2 changes: 1 addition & 1 deletion src/jshardware_common.c
Expand Up @@ -39,6 +39,6 @@ void jshSPIInitInfo(JshSPIInfo *inf) {
void jshI2CInitInfo(JshI2CInfo *inf) {
inf->pinSCL = PIN_UNDEFINED;
inf->pinSDA = PIN_UNDEFINED;
inf->bitrate = 50000; // Is what we used - shouldn't it be 100k?
inf->bitrate = 100000;
inf->started = false;
}
8 changes: 5 additions & 3 deletions src/jsi2c.c
Expand Up @@ -45,8 +45,7 @@ bool jsi2cPopulateI2CInfo(
const int I2C_TIMEOUT = 100000;

static void dly(i2cInfo *inf) {
volatile int i;
for (i=inf->delay;i>0;i--);
if (inf->delay) jshDelayMicroseconds(inf->delay);
}

static void err(const char *s) {
Expand Down Expand Up @@ -92,6 +91,7 @@ static void i2c_wr_bit(i2cInfo *inf, bool b) {
dly(inf);
jshPinSetValue(inf->pinSCL, 1); // stop forcing SCL
dly(inf);
dly(inf);
int timeout = I2C_TIMEOUT;
while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
if (!timeout) err("Timeout (wr)");
Expand All @@ -103,6 +103,7 @@ static bool i2c_rd_bit(i2cInfo *inf) {
jshPinSetValue(inf->pinSDA, 1); // stop forcing SDA
dly(inf);
jshPinSetValue(inf->pinSCL, 1); // stop forcing SCL
dly(inf);
int timeout = I2C_TIMEOUT;
while (!jshPinGetValue(inf->pinSCL) && --timeout); // clock stretch
if (!timeout) err("Timeout (rd)");
Expand Down Expand Up @@ -138,7 +139,8 @@ static void i2c_initstruct(i2cInfo *inf, JshI2CInfo *i) {
inf->pinSDA = i->pinSDA;
inf->pinSCL = i->pinSCL;
inf->started = i->started;
inf->delay = 4000000/i->bitrate;
inf->delay = 250000/i->bitrate;
if (inf->delay<2) inf->delay=0;
}

// ----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/jswrap_spi_i2c.c
Expand Up @@ -91,6 +91,8 @@ May return undefined if no device can be found.
]
}
Set up this SPI port as an SPI Master.
**Note:** When using Software SPI, the baud rate will be ignored - data is sent as fast as possible.
*/
void jswrap_spi_setup(
JsVar *parent, //!< The variable that is the class instance of this function.
Expand Down

0 comments on commit 3739ee5

Please sign in to comment.