[solved] Teensy 4.1 call setClock AFTER mcp.begin to increase SCL-Cock-frequency

Hi everybody,

I started testing a MCP2307-IO-expansion-board. I want to measure what is the practical upper limit of toggling / reading in an IO-expansion-pin.

I2C-Scanner found adress0x27

using the Adafruit_MCP23017.h-library I'm able to toggle an IO-Pin on the expansionboard with a very simple testcode?

I have adjusted the Arduino-IE to Teensy 4.1 board and I'm able to upload code.
The pin is toggling. I measured the SCL-clock-frequency with my DSO and found 98.6 kHz which is aprox. 100 kHz.

Then I tried to increase the I2C-SCL-Clock-frequency with the command

  Wire.setClock(400000); 

But the SCL-Clock-frequency stays the same.
I have connected the IO-pins 18,19 as SDA/SCL according to this Pinout of the Teensy 4.1


Which is called SDA/SCL with no additional number (like SDA1/SCL1 etc.)

So what do I have to change in hardware or software to increase the SCL-clock-frequency?

Should I use a different MCP2307-library?
Should I use a different Teensy 4.1 I2C-hardware-interface (like SDA1, SDA2)?

Would I have do modify the wire-library itself to access higher SCL-frequencies?

#include <Wire.h>
#include "Adafruit_MCP23017.h"

Adafruit_MCP23017 mcp;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

  Wire.begin();
  Wire.setClock(400000); 
  mcp.begin(0x27);      

  mcp.pinMode(7, OUTPUT);
  Serial.println("IO-pin 7 as OUTPUT");
  Serial.println("start toggling now...");
}

void loop() {
  mcp.digitalWrite(7, HIGH);
  //delay(100);
  mcp.digitalWrite(7, LOW);
}

best regards Stefan

So after re-thinking about possabilities what might cause the not-change of the I2C-Bus-clock-frequency

I tried to wire.setClock after the call of mcp.begin.

see the modified demo-code with comments

#include <Wire.h>
#include "Adafruit_MCP23017.h"

Adafruit_MCP23017 mcp;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

  Wire.begin();
  mcp.begin(0x27);        // first call mcp.begin     
  Wire.setClock(1700000); // after the mcp.begin setClock

  mcp.pinMode(7, OUTPUT);
  Serial.println("IO-pin 7 as OUTPUT");
  Serial.println("start toggling now...");
}

void loop() {
  mcp.digitalWrite(7, HIGH);
  //delay(100);
  mcp.digitalWrite(7, LOW);
}

And that worked. Though the frequencies my DSO measures deviate greatly from the value
used in the setClock-command

  Wire.setClock(1700000); // after the mcp.begin setClock

which means 1,7 MHz like the specs of the MCP2307 say
but with my DSO I measured only 788 kHz.
@Paul Stoffregen: what causes this big deviation?

Is there a way to fix this?

best regards Stefan

1 Like

That sounds like it could be I2C High-Speed Mode. The processor on the T4.1 does support it, but it appears to require different configuration of the various control registers. So, you should start by reading the I2C chapter of the processor's datasheet. Then open up the T4.1's implementation of the Wire library and see if it accommodates High-Speed Mode.

Or, if you really want to go fast, just use the SPI interface - 10MHz max on MCP23017.

Somewhere I have read a posting that the the frequencies 100 kHz, 400 kHz and 1,7 MHz are implenented in the wire-library for the I2C-bus.

If this is right why did they not implement it properly?
best regards Stefan

"Somewhere" is not a very authoritative source. Like I said, grab the datasheet, open the library source code and dig in. Maybe you'll find an implementation error in the code.

EDIT:
You might get more help posting your question on the Teensy Forum if you haven't already.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.