55// ====== and temperature data
66// ==============================================================================
77
8- MPU9250::MPU9250 ( int8_t csPin, SPIClass &spiInterface )
8+ MPU9250::MPU9250 ( int8_t csPin, SPIClass &spiInterface, uint32_t spi_freq )
99{
1010// Use hardware SPI communication
1111 // If used with sparkfun breakout board
@@ -17,6 +17,8 @@ MPU9250::MPU9250( int8_t csPin, SPIClass &spiInterface )
1717_spi = &spiInterface;
1818_wire = NULL ;
1919
20+ _interfaceSpeed = spi_freq;
21+
2022 _spi->begin ();
2123 pinMode (_csPin, OUTPUT);
2224 deselect ();
@@ -27,10 +29,12 @@ MPU9250::MPU9250( uint8_t address, TwoWire &wirePort, uint32_t clock_frequency )
2729_wire = &wirePort;
2830_spi = NULL ;
2931
32+ _interfaceSpeed = clock_frequency;
33+
3034_csPin = NOT_SPI;// Used to tell the library that the sensor is using I2C
3135
3236_wire->begin ();
33- _wire->setClock (clock_frequency );
37+ _wire->setClock (_interfaceSpeed );
3438}
3539
3640void MPU9250::getMres ()
@@ -612,8 +616,8 @@ void MPU9250::magCalMPU9250(float * bias_dest, float * scale_dest)
612616 uint16_t ii = 0 , sample_count = 0 ;
613617 int32_t mag_bias[3 ] = {0 , 0 , 0 },
614618 mag_scale[3 ] = {0 , 0 , 0 };
615- int16_t mag_max[3 ] = {0x8000 , 0x8000 , 0x8000 },
616- mag_min[3 ] = {0x7FFF , 0x7FFF , 0x7FFF },
619+ int16_t mag_max[3 ] = {- 32768 , - 32768 , - 32768 }, // Wrote out decimal (signed) values to remove a conversion warning
620+ mag_min[3 ] = {32767 , 32767 , 32767 },
617621 mag_temp[3 ] = {0 , 0 , 0 };
618622
619623 // Make sure resolution has been calculated
@@ -715,7 +719,7 @@ uint8_t MPU9250::writeByteSPI(uint8_t registerAddress, uint8_t writeData)
715719{
716720 uint8_t returnVal;
717721
718- _spi->beginTransaction (SPISettings (SPI_DATA_RATE , MSBFIRST, SPI_MODE));
722+ _spi->beginTransaction (SPISettings (_interfaceSpeed , MSBFIRST, SPI_MODE));
719723 select ();
720724
721725 _spi->transfer (registerAddress);
@@ -733,12 +737,14 @@ uint8_t MPU9250::writeByteSPI(uint8_t registerAddress, uint8_t writeData)
733737uint8_t MPU9250::writeByteWire (uint8_t deviceAddress, uint8_t registerAddress,
734738 uint8_t data)
735739{
736- _wire->beginTransmission (deviceAddress); // Initialize the Tx buffer
737- _wire->write (registerAddress); // Put slave register address in Tx buffer
738- _wire->write (data); // Put data in Tx buffer
739- _wire->endTransmission (); // Send the Tx buffer
740- // TODO: Fix this to return something meaningful
741- return NULL ;
740+ _wire->setClock (_interfaceSpeed);// Reset to the desired speed, in case other devices required a slowdown
741+ _wire->beginTransmission (deviceAddress); // Initialize the Tx buffer
742+ _wire->write (registerAddress); // Put slave register address in Tx buffer
743+ _wire->write (data); // Put data in Tx buffer
744+ _wire->endTransmission (); // Send the Tx buffer
745+ // TODO: Fix this to return something meaningful
746+ // return NULL; // In the meantime fix it to return the right type
747+ return 0 ;
742748}
743749
744750// Read a byte from given register on device. Calls necessary SPI or I2C
0 commit comments