Created
February 21, 2018 08:26
-
-
Save BST-Github-Admin/cdaa6aa244335388d914de7f59443860 to your computer and use it in GitHub Desktop.
Connecting the API to the Arduino environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @brief Task that delays for a ms period of time | |
* @param period : Period of time in ms | |
*/ | |
void delay_ms(uint32_t period) | |
{ | |
// Wait for a period amount of ms | |
// The system may simply idle, sleep or even perform background tasks | |
delay(period); | |
} | |
/** | |
* @brief Callback function for reading registers over I2C | |
* @param devId : Library agnostic parameter to identify the device to communicate with | |
* @param regAddr : Register address | |
* @param regData : Pointer to the array containing the data to be read | |
* @param length : Length of the array of data | |
* @return Zero for success, non-zero otherwise | |
*/ | |
int8_t i2cRead(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) | |
{ | |
uint16_t i; | |
int8_t rslt = 0; | |
Wire.beginTransmission(devId); | |
Wire.write(regAddr); | |
rslt = Wire.endTransmission(); | |
Wire.requestFrom((int) devId, (int) length); | |
for (i = 0; (i < length) && Wire.available(); i++) { | |
regData[i] = Wire.read(); | |
} | |
return rslt; | |
} | |
/** | |
* @brief Callback function for writing registers over I2C | |
* @param devId : Library agnostic parameter to identify the device to communicate with | |
* @param regAddr : Register address | |
* @param regData : Pointer to the array containing the data to be written | |
* @param length : Length of the array of data | |
* @return Zero for success, non-zero otherwise | |
*/ | |
int8_t i2cWrite(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) | |
{ | |
uint16_t i; | |
int8_t rslt = 0; | |
Wire.beginTransmission(devId); | |
Wire.write(regAddr); | |
for (i = 0; i < length; i++) { | |
Wire.write(regData[i]); | |
} | |
rslt = Wire.endTransmission(); | |
return rslt; | |
} | |
/** | |
* @brief Callback function for reading and writing registers over SPI | |
* @param devId : Library agnostic parameter to identify the device to communicate with | |
* @param regAddr : Register address | |
* @param regData : Pointer to the array containing the data to be read or written | |
* @param length : Length of the array of data | |
* @return Zero for success, non-zero otherwise | |
*/ | |
int8_t spiTransfer(uint8_t devId, uint8_t regAddr, uint8_t *regData, uint16_t length) | |
{ | |
int8_t rslt = 0; | |
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); // Can be upto 10MHz | |
digitalWrite(devId, LOW); | |
SPI.transfer(regAddr); // Write the register address, ignore the return | |
for (uint16_t i = 0; i < length; i++) | |
regData[i] = SPI.transfer(regData[i]); | |
digitalWrite(devId, HIGH); | |
SPI.endTransaction(); | |
return rslt;; | |
} | |
/** | |
* Example sketch configuration | |
*/ | |
/* | |
#include "bmi160.h" | |
struct bmi160_dev bmiI2c, bmiSpi; | |
#define BMI160_CS 10 // Digital pin 10 | |
void setup(void) | |
{ | |
// Configure the sensor for I2C communication | |
bmiI2c.id = BMI160_I2C_ADDR; | |
bmiI2c.interface = BMI160_I2C_INTF; | |
bmiI2c.read = i2cRead; | |
bmiI2c.write = i2cWrite; | |
bmiI2c.delay_ms = delay_ms; | |
// Configure the sensor for SPI communication | |
bmiSpi.id = BMI160_CS; // Here define the chip select that you are using. | |
bmiSpi.interface = BMI160_SPI_INTF; | |
// In SPI, the register address determines the read/write intention and is handled within the API | |
bmiSpi.read = spiTransfer; | |
bmiSpi.write = spiTransfer; | |
bmiSpi.delay_ms = delay_ms; | |
Wire.begin(); | |
SPI.begin(); | |
pinMode(BMI160_CS, OUTPUT); | |
Serial.begin(115200); // For debugging | |
if(bmi160_init(&bmiI2c) == BMI160_OK) | |
Serial.println("BMI160 connected over I2C!"); | |
else if(bmi160_init(&bmiSpi) == BMI160_OK) | |
Serial.println("BMI160 connected over SPI!"); | |
else | |
Serial.println("BMI160 not found."); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment