convert code from arduino

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
dwiw_
Level 1
Level 1
50 sign-ins 25 sign-ins 10 sign-ins

Hi,

I'm using CY8CKIT-044, I want to get data from MPU9250. I have already make code (based on github) in arduino Mega and the program running nicely, I got data from accelerometer, gyroscope and magnetometer very well. 

I want to convert code from arduino to PSoC creator, but I can't get even raw data, so i think my code isn't right, first i want to check if my 'i2c read/write functions' for the MPU9250 is right.

Hope can find the solution from this community.

note : first row is arduino code and second row is converted code.

#1

uint8_t readByte(uint8_t addressuint8_t subAddress)
{
uint8_t data; // `data` will store the register data
     Wire.beginTransmission(address);         // Initialize the Tx buffer
     Wire.write(subAddress);                  // Put slave register address in Tx buffer
     Wire.endTransmission(false);             // Send the Tx buffer, but send a restart to keep connection alive
     Wire.requestFrom(address, (size_t1);   // Read one byte from slave register address
     data = Wire.read();                      // Fill Rx buffer with result
     return data;                             // Return data read from slave register
}
 

uint8_t i2c_readByte(uint8_t address, uint8_t reg)
{
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendRestart(address, 1, I2C_TimeOut);
     uint8_t data = I2C_MPU9250_I2CMasterReadByte(address, 0,I2C_TimeOut); //store the register data
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
     return data;
}


 
#2
void readBytes(uint8_t addressuint8_t subAddressuint8_t countuint8_t * dest)
{
     Wire.beginTransmission(address);   // Initialize the Tx buffer
     Wire.write(subAddress);            // Put slave register address in Tx buffer
     Wire.endTransmission(false);       // Send the Tx buffer, but send a restart to keep connection alive
     uint8_t i = 0;
     Wire.requestFrom(address, (size_t) count);  // Read bytes from slave register address
     while (Wire.available()) {
          dest[i++] = Wire.read();
     }         // Put read results in the Rx buffer
}
void i2c_readBytes(uint8_t address, uint8_t reg, uint8_t byteCount, uint8_t * destinationBuffer)
{
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendRestart(address, 1, I2C_TimeOut);
     while (byteCount--) {
          if (!byteCount) {
               /* Last byte */
               I2C_MPU9250_I2CMasterReadByte(0,destinationBuffer++,I2C_TimeOut);
          } else {
               I2C_MPU9250_I2CMasterReadByte(1,destinationBuffer++,I2C_TimeOut);
          }
     }
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
}
 
#3
void writeByte(uint8_t addressuint8_t subAddressuint8_t data)
{
    Wire.beginTransmission(address);  // Initialize the Tx buffer
    Wire.write(subAddress);           // Put slave register address in Tx buffer
    Wire.write(data);                 // Put data in Tx buffer
    Wire.endTransmission();           // Send the Tx buffer
}
void i2c_writeByte(uint8_t address, uint8_t reg, uint8_t data) {
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(data, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
}
 
Thanks,
 
Dwi
0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Although it may not be the direct answer to your question,

I have written and posted the following sample code.

https://community.cypress.com/t5/Code-Examples/MCU-Tester-a-Swiss-Army-Knife-for-PSoC-CY8CKIT-044-ve...

Please test if you can read register value(s) from MPU9250 using the sample and if it works, please refer to the source code of i2c_utils.h and i2c_utils.c.

moto

View solution in original post

0 Likes
1 Reply
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Although it may not be the direct answer to your question,

I have written and posted the following sample code.

https://community.cypress.com/t5/Code-Examples/MCU-Tester-a-Swiss-Army-Knife-for-PSoC-CY8CKIT-044-ve...

Please test if you can read register value(s) from MPU9250 using the sample and if it works, please refer to the source code of i2c_utils.h and i2c_utils.c.

moto

0 Likes