Skip to content

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

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
mjs513 opened this issue Dec 25, 2024 · 3 comments
Closed

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

mjs513 opened this issue Dec 25, 2024 · 3 comments

Comments

@mjs513
Copy link

mjs513 commented Dec 25, 2024

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?

@mjs513
Copy link
Author

mjs513 commented Dec 26, 2024

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);

}

@mjs513 mjs513 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 Dec 27, 2024
@mjs513 mjs513 changed the title I2C setClock does not appear to do anything on the Arduino Giga GIGA: I2C setClock does not appear to do anything Dec 30, 2024
@facchinm
Copy link
Member

facchinm commented Jan 8, 2025

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
Copy link
Author

mjs513 commented Jan 8, 2025

Done. If you want to close not an issue

@facchinm facchinm closed this as completed Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants