forked from jrowberg/i2cdevlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
// I2Cdev library collection - Main I2C device class | ||
// Abstracts bit and byte I2C R/W functions into a convenient class | ||
// | ||
// Nordic Semiconductors nrf51 code by August Bering <[email protected]>, tested with MPU6050 only | ||
// readTimeout not implemented. | ||
|
||
// Based on Arduino's I2Cdev by Jeff Rowberg <[email protected]> | ||
// | ||
|
||
|
||
/* ============================================ | ||
I2Cdev device library code is placed under the MIT license | ||
Copyright (c) 2015 Jeff Rowberg, Nicolas Baldeck | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
=============================================== | ||
*/ | ||
extern "C"{ | ||
#include "nrf_drv_twi.h" | ||
#include "app_util_platform.h" | ||
} | ||
#include "I2Cdev.h" | ||
static nrf_drv_twi_t m_twi=NRF_DRV_TWI_INSTANCE(0); | ||
/** Default constructor. | ||
*/ | ||
I2Cdev::I2Cdev() { | ||
} | ||
|
||
#define SCL_PIN 7 | ||
#define SDA_PIN 30 | ||
|
||
uint16_t I2Cdev::readTimeout=I2CDEV_DEFAULT_READ_TIMEOUT; | ||
/** Initialize I2C0 | ||
*/ | ||
void I2Cdev::initialize() { | ||
ret_code_t err_code; | ||
|
||
const nrf_drv_twi_config_t config = { | ||
.scl = SCL_PIN, | ||
.sda = SDA_PIN, | ||
.frequency = NRF_TWI_FREQ_400K, | ||
.interrupt_priority = APP_IRQ_PRIORITY_HIGH | ||
}; | ||
|
||
|
||
err_code = nrf_drv_twi_init(&m_twi, &config,NULL, NULL); | ||
APP_ERROR_CHECK(err_code); | ||
} | ||
|
||
/** Enable or disable I2C | ||
* @param isEnabled true = enable, false = disable | ||
*/ | ||
void I2Cdev::enable(bool isEnabled) { | ||
|
||
if (isEnabled) | ||
nrf_drv_twi_enable(&m_twi); | ||
else | ||
nrf_drv_twi_disable(&m_twi); | ||
|
||
} | ||
|
||
/** Read a single bit from an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register regAddr to read from | ||
* @param bitNum Bit position to read (0-7) | ||
* @param data Container for single bit value | ||
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) | ||
* @return Status of read operation (true = success) | ||
*/ | ||
int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) { | ||
uint8_t b; | ||
uint8_t count = readByte(devAddr, regAddr, &b, timeout); | ||
*data = b & (1 << bitNum); | ||
return count; | ||
} | ||
|
||
/** Read multiple bits from an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register regAddr to read from | ||
* @param bitStart First bit position to read (0-7) | ||
* @param length Number of bits to read (not more than 8) | ||
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) | ||
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) | ||
* @return Status of read operation (true = success) | ||
*/ | ||
int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) { | ||
// 01101001 read byte | ||
// 76543210 bit numbers | ||
// xxx args: bitStart=4, length=3 | ||
// 010 masked | ||
// -> 010 shifted | ||
uint8_t count, b; | ||
if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { | ||
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); | ||
b &= mask; | ||
b >>= (bitStart - length + 1); | ||
*data = b; | ||
} | ||
return count; | ||
} | ||
|
||
/** Read single byte from an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register regAddr to read from | ||
* @param data Container for byte value read from device | ||
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) | ||
* @return Status of read operation (true = success) | ||
*/ | ||
int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) { | ||
return readBytes(devAddr, regAddr, 1, data, timeout); | ||
} | ||
|
||
/** Read multiple bytes from an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr First register regAddr to read from | ||
* @param length Number of bytes to read | ||
* @param data Buffer to store read data in | ||
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) | ||
* @return I2C_TransferReturn_TypeDef http://downloads.energymicro.com/documentation/doxygen/group__I2C.html | ||
*/ | ||
int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) { | ||
|
||
//bool transfer_succeeded; | ||
nrf_drv_twi_tx(&m_twi,devAddr,®Addr,1,true); | ||
// transfer_succeeded = twi_master_transfer(m_device_address, ®ister_address, 1, TWI_DONT_ISSUE_STOP); | ||
|
||
ret_code_t r= nrf_drv_twi_rx(&m_twi,devAddr,data,length,false); | ||
|
||
//transfer_succeeded &= twi_master_transfer(m_device_address|TWI_READ_BIT, destination, number_of_bytes, TWI_ISSUE_STOP); | ||
return r==NRF_SUCCESS; | ||
|
||
} | ||
|
||
/** write a single bit in an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register regAddr to write to | ||
* @param bitNum Bit position to write (0-7) | ||
* @param value New bit value to write | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) { | ||
uint8_t b; | ||
readByte(devAddr, regAddr, &b); | ||
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum)); | ||
return writeByte(devAddr, regAddr, b); | ||
} | ||
|
||
/** Write multiple bits in an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register regAddr to write to | ||
* @param bitStart First bit position to write (0-7) | ||
* @param length Number of bits to write (not more than 8) | ||
* @param data Right-aligned value to write | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) { | ||
// 010 value to write | ||
// 76543210 bit numbers | ||
// xxx args: bitStart=4, length=3 | ||
// 00011100 mask byte | ||
// 10101111 original value (sample) | ||
// 10100011 original & ~mask | ||
// 10101011 masked | value | ||
uint8_t b; | ||
if (readByte(devAddr, regAddr, &b) != 0) { | ||
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); | ||
data <<= (bitStart - length + 1); // shift data into correct position | ||
data &= mask; // zero all non-important bits in data | ||
b &= ~(mask); // zero all important bits in existing byte | ||
b |= data; // combine data with existing byte | ||
return writeByte(devAddr, regAddr, b); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** Write single byte to an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register address to write to | ||
* @param data New byte value to write | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) { | ||
uint8_t w2_data[2]; | ||
|
||
w2_data[0] = regAddr; | ||
w2_data[1] = data; | ||
return NRF_SUCCESS==nrf_drv_twi_tx(&m_twi,devAddr,w2_data,2,false); | ||
} | ||
|
||
/** Write multiple bytes to an 8-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr First register address to write to | ||
* @param length Number of bytes to write | ||
* @param data Buffer to copy new data from | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data) { | ||
if (NRF_SUCCESS!=nrf_drv_twi_tx(&m_twi,devAddr,®Addr,1,true)) | ||
return false; | ||
return NRF_SUCCESS==nrf_drv_twi_tx(&m_twi,devAddr,data,length,false); | ||
} | ||
/** Write single word to a 16-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr Register address to write to | ||
* @param data New word value to write | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) { | ||
return writeWords(devAddr, regAddr, 1, &data); | ||
} | ||
/** Write multiple words to a 16-bit device register. | ||
* @param devAddr I2C slave device address | ||
* @param regAddr First register address to write to | ||
* @param length Number of words to write | ||
* @param data Buffer to copy new data from | ||
* @return Status of operation (true = success) | ||
*/ | ||
bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data){ | ||
if (NRF_SUCCESS!=nrf_drv_twi_tx(&m_twi,devAddr,®Addr,1,true)) | ||
return false; | ||
|
||
for (int i=0;i<length;i++){ | ||
uint8_t d[2]; | ||
d[0]=data[i]>>8; | ||
d[1]=data[i]&0xff; | ||
bool pending = i < length - 1; | ||
if (NRF_SUCCESS != nrf_drv_twi_tx(&m_twi, devAddr, d, 2, pending)) | ||
return false; | ||
} | ||
return true; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// I2Cdev library collection - Main I2C device class | ||
// Abstracts bit and byte I2C R/W functions into a convenient class | ||
// EFM32 stub port by Nicolas Baldeck <[email protected]> | ||
// Based on Arduino's I2Cdev by Jeff Rowberg <[email protected]> | ||
// | ||
// Changelog: | ||
// 2015-01-02 - Initial release | ||
|
||
|
||
/* ============================================ | ||
I2Cdev device library code is placed under the MIT license | ||
Copyright (c) 2015 Jeff Rowberg, Nicolas Baldeck | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
=============================================== | ||
*/ | ||
|
||
#ifndef _I2CDEV_H_ | ||
#define _I2CDEV_H_ | ||
#include <stdint.h> | ||
#include "compiler_abstraction.h" | ||
#include "nrf.h" | ||
#ifdef SOFTDEVICE_PRESENT | ||
#include "nrf_soc.h" | ||
#include "app_error.h" | ||
#endif | ||
|
||
|
||
#define I2C_SDA_PORT gpioPortA | ||
#define I2C_SDA_PIN 0 | ||
#define I2C_SDA_MODE gpioModeWiredAnd | ||
#define I2C_SDA_DOUT 1 | ||
|
||
#define I2C_SCL_PORT gpioPortA | ||
#define I2C_SCL_PIN 1 | ||
#define I2C_SCL_MODE gpioModeWiredAnd | ||
#define I2C_SCL_DOUT 1 | ||
|
||
#define I2CDEV_DEFAULT_READ_TIMEOUT 0 | ||
|
||
class I2Cdev { | ||
public: | ||
I2Cdev(); | ||
|
||
static void initialize(); | ||
static void enable(bool isEnabled); | ||
|
||
static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
//TODO static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
//TODO static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
//TODO static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
//TODO static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout); | ||
|
||
static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data); | ||
//TODO static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data); | ||
static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data); | ||
//TODO static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data); | ||
static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data); | ||
static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data); | ||
static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data); | ||
static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data); | ||
|
||
static uint16_t readTimeout; | ||
}; | ||
|
||
#endif /* _I2CDEV_H_ */ |