madison-embedded / gcc-builds

For projects built with the GNU toolchain.
GNU General Public License v3.0
11 stars 8 forks source link

Device Driver: MPU9250 (I2C) #35

Open vkottler opened 7 years ago

vkottler commented 7 years ago

Breakout board from SparkFun.

I would suggest beginning by converting the library code to work with just C instead of CPP and changing I2C read and write function names to work with ours

vkottler commented 7 years ago

FYI here's a previous driver I had been working on: https://github.com/vkottler/badgerloopControls/blob/master/Pic32/peripherals/C/MPU9250.c

chroro commented 7 years ago

Does your code from the previous project work ? If yes , we can just modify it to match ours.

chroro commented 7 years ago

We will probably just change the GitHub library source to match our project.

vkottler commented 7 years ago

Yeah it works, that sounds like the best approach

vkottler commented 7 years ago
uint8_t readByte(uint8_t address, uint8_t subAddress)
{

 uint8_t *buffer =  (uint8_t *) malloc(sizeof(uint8_t));
 *buffer = subAddress;
 I2C_setSlaveAddr(address,  MPU9250_config.I2C_BASE);
 I2C_Master_Transmit_Data( MPU9250_config.I2C_BASE, 1, buffer);
 I2C_Master_Receive_Data( MPU9250_config.I2C_BASE, 1, buffer);
 I2C_Read_Data_Ready( MPU9250_config.I2C_BASE);

 return *buffer; 

}

Isn't uint8_t *buffer = (uint8_t *) malloc(sizeof(uint8_t)); the exact same as uint8_t buffer; and then passing &buffer to the function?

Consider the significant overhead in making a memory allocation library call

chroro commented 7 years ago

We were using &buffer before we noticed a bug in writeByte function. Interestingly, since we don't busy wait in writeByte function until data is transmitted, next call to write function overwrites the data in the stack. Since stack contains the data (&buffer ) to send via I2C next writeByte call may overwrite the data to transmit from the first writeByte call. Therefore, we had to malloc memory, to not overwrite any data.

chroro commented 7 years ago

We also did not busy wait in readBytes. So we decided to call malloc again. I believe in the end we decided to busy wait in readBytes function so that we will return with the proper value in the passed buffer argument. Since we are busy waiting in readBytes and readByte we can use &buffer. we can change that )) good catch.