Skip to content

GIGA: I2C setClock does not appear to do anything #23

Closed
@mjs513

Description

@mjs513

Using a MPU9250 as my I2c test device I tested using SCL/SDA and SCL1/SDA1. Seems as though i2c clock speed is hardcoded someplace.

SCL/SDA - i2c2
In the overlay default clock is set to fast or 400khz

Setting the clock to 100Khz using setClock(100000) or setClock(i2c_standard) shows an actual clock of 400khz
Image

Setting it 400khz shows same clock
Image

SCL1/SDA1 - i2c4
No default clock is set and seeing 400khz when I do a setclock of 100khz.
Image

EDIT> finally poked around a bit and appears setclock may not be implemented at least I can not find where?

Activity

mjs513

mjs513 commented on Dec 26, 2024

@mjs513
Author

To resolve the setClock issue I implemented this piece of code for set Clock in wire.cpp

void arduino::ZephyrI2C::setClock(uint32_t freq) {
    uint8_t speed = 	I2C_SPEED_STANDARD;
	if(freq >  0x06u ) {
		if(freq == 100000) {
		  speed = I2C_SPEED_STANDARD;
		} else if(freq == 400000) {
		  speed = I2C_SPEED_FAST;
		} else if(freq == 1000000) {
		  speed = I2C_SPEED_FAST_PLUS;
		} else {
		  speed = I2C_SPEED_STANDARD;
		}
	} else {
		speed = (uint8_t) freq;
	}
	uint32_t i2c_cfg = I2C_MODE_CONTROLLER |
					I2C_SPEED_SET(speed);

	if (i2c_configure(i2c_dev, i2c_cfg)) {
		//Serial.println("Failed to configure i2c interface.");
	}
}

also had to add this to llext_exports.c

FORCE_EXPORT_SYM(__aeabi_ldivmod);

seems to be working for 100khz and 400khz. Tried 1Mhz but my LA read 111khz?? Tested on Wire and Wire1 with the following sketch:

/* Blink inbuilt LED example */

#include <Arduino.h>
#include <LibPrintf.h>
#include "Wire.h"

void setup()
{
	printf("\n\nSetup begins\n");
	Wire1.begin();
  Wire1.setClock(400000);
	// initialize the LED pin as an output:

	// initialize the pushbutton pin as an input:
	// pinMode(buttonPin, INPUT);
	// pinMode(ledPin, OUTPUT);
	Wire1.beginTransmission(0x53);
	Wire1.write(0x2C);
	Wire1.write(0x08);
	Wire1.endTransmission();

	Wire1.beginTransmission(0x53);
	Wire1.write(0x31);
	Wire1.write(0x08);
	Wire1.endTransmission();

	Wire1.beginTransmission(0x53);
	Wire1.write(0x2D);
	Wire1.write(0x08);
	Wire1.endTransmission();
	printf("\n\nSetup COMPLETE\n\n\n");
}

void loop()
{
	Wire1.beginTransmission(0x53);
Wire1.write(0x32); 
Wire1.endTransmission();
// printf("\n\nrequesting from 53\n\n\n");
Wire1.requestFrom(0x53, 1);
byte x0 = Wire1.read();

Wire1.beginTransmission(0x53);
Wire1.write(0x33); 
Wire1.endTransmission();
Wire1.requestFrom(0x53, 1);
byte x1 = Wire1.read();
x1 = x1 & 0x03;

uint16_t x = (x1 << 8) + x0;
int16_t xf = x;
if(xf > 511)
{
xf = xf - 1024;
}
float xa = xf * 0.004;
printf("\n\nX = %f\n",xa);
// Serial.print("X = "); 
// Serial.print(xa);
// Serial.print(" g"); 
// Serial.println(); 


Wire1.beginTransmission(0x53);
Wire1.write(0x34); 
Wire1.endTransmission();
Wire1.requestFrom(0x53, 1);
byte y0 = Wire1.read();

Wire1.beginTransmission(0x53);
Wire1.write(0x35); 
Wire1.endTransmission();
Wire1.requestFrom(0x53, 1);
byte y1 = Wire1.read();
y1 = y1 & 0x03;

uint16_t y = (y1 << 8) + y0;
int16_t yf = y;
if(yf > 511)
{
yf = yf - 1024;
}
float ya = yf * 0.004;
// printk("Y = %f\n",ya);
printf("Y = %f\n",ya);
// printf("\n\nYa = %f\n\n\n",ya);
// Serial.print("Y = "); 
// Serial.print(ya);
// Serial.print(" g"); 
// Serial.println(); 

Wire1.beginTransmission(0x53);
Wire1.write(0x36); 
Wire1.endTransmission();
Wire1.requestFrom(0x53, 1);
byte z0 = Wire1.read();

Wire1.beginTransmission(0x53);
Wire1.write(0x37); 
Wire1.endTransmission();
Wire1.requestFrom(0x53, 1);
byte z1 = Wire1.read();
z1 = z1 & 0x03;

uint16_t z = (z1 << 8) + z0;
int16_t zf = z;
if(zf > 511)
{
zf = zf - 1024;
}
float za = zf * 0.004;
printf("Z = %f\n\n",za);
// Serial.print("Z = "); 
// Serial.print(za);
// Serial.print(" g"); 
// Serial.println(); 
// Serial.println(); 
delay(2);

}
changed the title [-]setClock does not appear to do anything on the Arduino Giga[/-] [+]I2C setClock does not appear to do anything on the Arduino Giga[/+] on Dec 27, 2024
changed the title [-]I2C setClock does not appear to do anything on the Arduino Giga[/-] [+]GIGA: I2C setClock does not appear to do anything[/+] on Dec 30, 2024
facchinm

facchinm commented on Jan 8, 2025

@facchinm
Member

Hi Mike, thanks for the investigation! Can I ask you to prepare a PR for the setClock implementation? As usual, to preserve proper attribution 🙂 Thank you!

mjs513

mjs513 commented on Jan 8, 2025

@mjs513
Author

Done. If you want to close not an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mjs513@facchinm

        Issue actions

          GIGA: I2C setClock does not appear to do anything · Issue #23 · arduino/ArduinoCore-zephyr