Skip to content
Snippets Groups Projects
Commit 507bb3a1 authored by Branden Sammons's avatar Branden Sammons
Browse files

Added SPI Communication for LSM9DS0.

Using the 2 chip select lines we now have a object that can handle all the SPI communication at a low level.

Features added :
 - write safety features
 - read safety features
parent 262088e3
Branches master
No related tags found
No related merge requests found
/*
* @author Branden Sammons
*/
#include "LSM9DS0.h"
/**
* Constructor for the LSM9DS0 class.
* Sets up the right SPI channels, frequency, mode, and registers.
*/
LSM9DS0::LSM9DS0() {
// set up the mraa library
mraa_init();
// set the SPI channels
SPI_1 = mraa_spi_init(1);
SPI_2 = mraa_spi_init(0);
// set the frequency ( 10Mhz )
mraa_spi_frequency(SPI_1, 10000000);
mraa_spi_frequency(SPI_2, 10000000);
// set the mode
mraa_spi_mode(SPI_1, MRAA_SPI_MODE1);
mraa_spi_mode(SPI_2, MRAA_SPI_MODE1);
// set up registers
// place holder
}
/**
* Deconstructor for the LSM9DS0 class.
* Realeases the SPI channels.
*/
LSM9DS0::~LSM9DS0() {
// place holder
}
/**
* Read command for the gyro on board the LSM9DS0
*
* Input : Address to read from
* Returns : Results from chip
*/
uint8_t LSM9DS0::read_G(uint8_t address) {
// lets set the first two bits to 0
address = address & 0x3F;
// lets not read reserved or write only registers
if( address <= 0x1F ||
address == 0x26 ||
address >= 0x39) {
// Note: register 0x0F is read only so we will just use it
// WHO_AM_I_G
address = 0x0F;
}
return this::read(SPI_1, address);
}
/**
* Write command for the gyro on board the LSM9DS0
* WARNING : Writing to a reserved register can cause harm to the device.
* Please read documentation before proceeding.
*
* Input : Address to write to, data to write
*/
void LSM9DS0::write_G(uint8_t address, uint8_t data) {
// lets set the first two bits to 0
address = address & 0x3F;
// definently not allowing writing to read only or reserved registers.
if( address <= 0x1F ||
( address >= 0x26 && address <= 0x2D ) ||
address == 0x2F ||
address == 0x31 ||
address >= 0x39) {
return;
}
this::write(SPI_1, address, data);
}
/**
* Read command for the accelerometer and magnetometer on board the LSM9DS0
*
* Input : Address to read from
* Returns : Results from chip
*/
uint8_t read_XM(uint8_t address) {
// lets set the first two bits to 0
address = address & 0x3F;
// lets not read reserved or write only registers
if( address <= 0x04 ||
address == 0x0E ||
address == 0x10 ||
address == 0x11) {
// Note: register 0x0F is read only so we will just use it
// WHO_AM_I_XM
address = 0x0F;
}
return this::read(SPI_2, address);
}
/**
* Write command for the accelerometer and magnetometer on board the LSM9DS0
* WARNING : Writing to a reserved register can cause harm to the device.
* Please read documentation before proceeding.
*
* Input : Address to write to, data to write
*/
void write_XM(uint8_t address, uint8_t data) {
// lets set the first two bits to 0
address = address & 0x3F;
// definently not allowing writing to read only or reserved registers.
if( address <= 0x11 ||
address == 0x13 ||
( address >= 0x27 && address <= 0x2D ) ||
address == 0x2F ||
address == 0x31 ||
address == 0x35 ||
address == 0x39) {
return;
}
this::write(SPI_2, address, data);
}
/* Private methods below */
/**
* Read command for the LSM9DS0
* Note : First two bits need to be 10
*
* Input : SPI context, address to read from
* Returns : Results from chip
*/
uint8_t LSM9DS0::read(mraa_spi_context SPI, uint8_t address) {
uint16_t send_data = address << 8;
// need start to be bits 10
send_data = send_data | 0x8000;
send_data = send_data & 0xBFFF;
// send the data down the channel and get the result
uint8_t result = mraa_spi_write_word(SPI, send_data);
return result;
}
/**
* Write command for the LSM9DS0
* WARNING : Writing to a reserved register can cause harm to the device.
* Please read documentation before proceeding.
*
* Input : SPI context, address to write to, data to write
*/
void LSM9DS0::write(mraa_spi_context SPI, uint8_t address, uint8_t data) {
uint16_t send_data = address << 8;
// need start to be bits 00
send_data = send_data & 0x3FFF;
// add the data to be sent
send_data = send_data | data;
// send the data down the channel
mraa_spi_write_word(SPI, send_data);
}
/*
* @author Branden Sammons
*/
#include "mraa.h"
#include <unistd.h>
#include <stdint.h>
class LSM9DS0 {
public:
LSM9DS0();
~LSM9DS0();
uint8_t read_G(uint8_t address);
void write_G(uint8_t address, uint8_t data);
uint8_t read_XM(uint8_t address);
void write_XM(uint8_t address, uint8_t data);
private:
mraa_spi_context SPI_1;
mraa_spi_context SPI_2;
uint8_t read(mraa_spi_context SPI, uint8_t address);
void write(mraa_spi_context SPI, uint8_t address, uint8_t data);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment