Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LSM6DS3 by
Diff: LSM6DS3.cpp
- Revision:
- 1:cc2caaf5536c
- Parent:
- 0:301b2cb34ead
- Child:
- 2:c19d384b2896
--- a/LSM6DS3.cpp Fri Feb 19 09:50:40 2016 +0000 +++ b/LSM6DS3.cpp Tue Feb 23 07:26:58 2016 +0000 @@ -32,8 +32,8 @@ #include "stdint.h" #include "math.h" -I2C i2c(D14,D15); -SPI spi(D11,D12,D13); +//I2C i2c(D14,D15); +//SPI spi(D11,D12,D13); //****************************************************************************// // // LSM6DS3Core functions. @@ -54,7 +54,7 @@ //================================= //****************************************************************************// -LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg) : commInterface(SPI_MODE), I2CAddress(0x6B), chipSelectPin(D10) +LSM6DS3Core::LSM6DS3Core( uint8_t busType, uint8_t inputArg) : commInterface(SPI_MODE), I2CAddress(0x6B), chipSelectPin(PB_6), spi(D11,D12,D13), i2c(D14,D15), cs((PinName)chipSelectPin) { commInterface = busType; if( commInterface == I2C_MODE ) { @@ -80,7 +80,7 @@ case SPI_MODE: // start the SPI library: - DigitalOut cs(chipSelectPin); + //DigitalOut cs((PinName)chipSelectPin); // Maximum SPI frequency is 10MHz, could divide by 2 here: spi.frequency(5000000); // Data is read and written MSb first. @@ -153,7 +153,7 @@ switch (commInterface) { case I2C_MODE: - +/* i2c.write(offset); if( i2c.stop() != 0 ) { returnError = IMU_HW_ERROR; @@ -167,15 +167,16 @@ i++; } } + */ break; case SPI_MODE: // take the chip select low to select the device: - digitalWrite(chipSelectPin, LOW); + cs = 0; // send the device the register you want to read: - SPI.transfer(offset | 0x80); //Ored with "read request" bit + spi.write(offset | 0x80); //Ored with "read request" bit while ( i < length ) { // slave may send less than requested - c = SPI.transfer(0x00); // receive a byte as character + c = spi.write(0x00); // receive a byte as character if( c == 0xFF ) { //May have problem tempFFCounter++; @@ -189,7 +190,7 @@ returnError = IMU_ALL_ONES_WARNING; } // take the chip select high to de-select: - digitalWrite(chipSelectPin, HIGH); + cs = 1; //digitalWrite(chipSelectPin, HIGH); break; default: @@ -218,7 +219,7 @@ switch (commInterface) { case I2C_MODE: - Wire.beginTransmission(I2CAddress); + /* Wire.beginTransmission(I2CAddress); Wire.write(offset); if( Wire.endTransmission() != 0 ) { returnError = IMU_HW_ERROR; @@ -227,17 +228,18 @@ while ( Wire.available() ) { // slave may send less than requested result = Wire.read(); // receive a byte as a proper uint8_t } + */ break; case SPI_MODE: // take the chip select low to select the device: - digitalWrite(chipSelectPin, LOW); + cs = 0; //digitalWrite(chipSelectPin, LOW); // send the device the register you want to read: - SPI.transfer(offset | 0x80); //Ored with "read request" bit + spi.write(offset | 0x80); //Ored with "read request" bit // send a value of 0 to read the first byte returned: - result = SPI.transfer(0x00); + result = spi.write(0x00); // take the chip select high to de-select: - digitalWrite(chipSelectPin, HIGH); + cs = 1; //digitalWrite(chipSelectPin, HIGH); if( result == 0xFF ) { //we've recieved all ones, report @@ -286,25 +288,25 @@ status_t returnError = IMU_SUCCESS; switch (commInterface) { case I2C_MODE: - //Write the byte +/* //Write the byte Wire.beginTransmission(I2CAddress); Wire.write(offset); Wire.write(dataToWrite); if( Wire.endTransmission() != 0 ) { returnError = IMU_HW_ERROR; } - break; + */ break; case SPI_MODE: // take the chip select low to select the device: - digitalWrite(chipSelectPin, LOW); + cs = 0; //digitalWrite(chipSelectPin, LOW); // send the device the register you want to read: - SPI.transfer(offset); + spi.write(offset); // send a value of 0 to read the first byte returned: - SPI.transfer(dataToWrite); + spi.write(dataToWrite); // decrement the number of bytes left to read: // take the chip select high to de-select: - digitalWrite(chipSelectPin, HIGH); + cs = 1;//digitalWrite(chipSelectPin, HIGH); break; //No way to check error on this write (Except to read back but that's not reliable)