Open vkottler opened 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
Does your code from the previous project work ? If yes , we can just modify it to match ours.
We will probably just change the GitHub library source to match our project.
Yeah it works, that sounds like the best approach
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
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.
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.
Breakout board from SparkFun.
I would suggest beginning by converting the library code to work with just
C
instead ofCPP
and changing I2C read and write function names to work with ours