Jurica Resetar / aconno_I2C

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aconno_i2c.cpp Source File

aconno_i2c.cpp

00001 /*
00002  *  I2C library made by Jurica Resetar @ aconno
00003  *  2017
00004  *  More info @ aconno.de
00005  *  jurica_resetar@yahoo.com
00006  *  All right reserved
00007  *
00008  */
00009 
00010 #include "aconno_i2c.h"
00011 
00012 /**
00013  * [aconno_i2c::aconno_i2c description]
00014  * @param i2c     [description]
00015  * @param address [description]
00016  */
00017 aconno_i2c::aconno_i2c(I2C *i2c, char address){
00018     this->i2c = i2c;
00019     i2cAddress = address;
00020 }
00021 
00022 /**
00023  * Method for changing specific bits within register
00024  * @param  regAddress: 8bit address
00025  * @param  regSize:  register size in B
00026  * @param  newValue  new value for [numOfBits] bits within the register
00027  * @param  numOfBits  sizeof[newValue] in b
00028  * @param  offset     newValue offset within the register
00029  * @return            returns new value of the register
00030  */
00031 uint16_t aconno_i2c::changeRegBits(char regAddress, uint8_t regSize,
00032     uint16_t newValue, uint16_t numOfBits, uint16_t offset)
00033 {
00034     uint16_t mask;
00035     uint16_t regData = 0;
00036     // Read old configuration
00037     readFromReg(regAddress, (char*)&regData, regSize);
00038     mask = 0 | newValue << offset;
00039     // Clear bits
00040     regData &= ~(((1 << numOfBits)-1) << offset);
00041     regData |= mask; // Set/clear bits as required
00042     writeToReg(regAddress, (char*)&regData, regSize);
00043     return regData;
00044 }
00045 
00046 /**
00047  * [aconno_i2c::writeToReg description]
00048  * @param  regAddress [description]
00049  * @param  data       [description]
00050  * @param  len        [description]
00051  * @return            [description]
00052  */
00053 uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){
00054     uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
00055     char dataToSend[len+1];
00056 
00057     dataToSend[0] = regAddress;
00058     memcpy(dataToSend+1, data, len);
00059     // R/W bit is set low for a write command
00060     success = i2c->write(i2cAddress & 0xFE, dataToSend, len + 1);
00061     return success;
00062 }
00063 
00064 /**
00065  * [aconno_i2c::readFromReg description]
00066  * @param  regAddress [description]
00067  * @param  dataBuffer [description]
00068  * @param  len        [description]
00069  * @return            [description]
00070  */
00071 uint8_t aconno_i2c::readFromReg(char regAddress, char *dataBuffer, int len){
00072     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
00073     char regAddr = regAddress;
00074 
00075     i2c->write(i2cAddress & 0xFE, &regAddr, 1);               // R/W bit is set low for a write command
00076     success = i2c->read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
00077     return success;
00078 }
00079 
00080 /**
00081  * This method is used with sendCommand(char *command, uint8_t len) method when delay between command
00082  * and response from the slave is required
00083  * @param  dataBuffer [description]
00084  * @param  len        [description]
00085  * @return            [description]
00086  */
00087 uint8_t aconno_i2c::readBus(char *dataBuffer, int len){
00088     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
00089 
00090     success = i2c->read(i2cAddress | 0x01, dataBuffer, len);     // R/W bit is set high for a read command
00091     return success;
00092 }
00093 
00094 /**
00095  *  This method is used to send commands to I2C Device.
00096  *  Ex. send 2B command to Si7006 to get Device ID.
00097  * @param  command     [description]
00098  * @param  len         [description]
00099  * @param  response    [description]
00100  * @param  responseLen [description]
00101  * @param  repeated    [description]
00102  * @return 0 on success (ack), non-0 on failure (nack)
00103  */
00104 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response,
00105     uint8_t responseLen, bool repeated)
00106     {
00107     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
00108 
00109     // R/W bit is set low for a write command
00110     i2c->write(i2cAddress & 0xFE, command, len, repeated);
00111     // R/W bit is set high for a read command
00112     success = i2c->read(i2cAddress | 0x01, response, responseLen);
00113     return success;
00114 }
00115 
00116 /**
00117  * [aconno_i2c::sendCommand description]
00118  * @param  command [description]
00119  * @param  len     [description]
00120  * @return         [description]
00121  */
00122 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){
00123     uint8_t success;    /* 0 on success (ack), non-0 on failure (nack) */
00124 
00125     success = i2c->write(i2cAddress & 0xFE, command, len);
00126     return success;
00127 }