mirror of
https://github.com/SMFSW/cI2C
synced 2024-11-22 09:54:23 +01:00
code review, changes following Wire lib updates & const for function parameters
This commit is contained in:
parent
035edd6d7f
commit
ed7030cffc
2
Doxyfile
2
Doxyfile
@ -38,7 +38,7 @@ PROJECT_NAME = "Arduino Hardware I2C for AVR MCUs (plain c)"
|
|||||||
# could be handy for archiving the generated documentation or if some version
|
# could be handy for archiving the generated documentation or if some version
|
||||||
# control system is used.
|
# control system is used.
|
||||||
|
|
||||||
PROJECT_NUMBER = 0.6
|
PROJECT_NUMBER = 1.0
|
||||||
|
|
||||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||||
# for a project that appears at the top of each page and should give viewer a
|
# for a project that appears at the top of each page and should give viewer a
|
||||||
|
@ -15,6 +15,10 @@ Feel free to share your thoughts @ xgarmanboziax@gmail.com about:
|
|||||||
------------
|
------------
|
||||||
|
|
||||||
** Actual:
|
** Actual:
|
||||||
|
v1.0 21 Nov 2017:
|
||||||
|
- Added const qualifier for function parameters
|
||||||
|
- Return from comm functions if bytes to R/W set to 0
|
||||||
|
|
||||||
v0.6 12 Jul 2017:
|
v0.6 12 Jul 2017:
|
||||||
- compliance with Arduino v1.5+ IDE source located in src subfolder
|
- compliance with Arduino v1.5+ IDE source located in src subfolder
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
This example code is in the public domain.
|
This example code is in the public domain.
|
||||||
|
|
||||||
created Jan 12 2017
|
created Jan 12 2017
|
||||||
latest mod Jan 31 2017
|
latest mod Nov 21 2017
|
||||||
by SMFSW
|
by SMFSW
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -24,8 +24,8 @@ void setup() {
|
|||||||
Serial.begin(115200); // start serial for output
|
Serial.begin(115200); // start serial for output
|
||||||
I2C_init(I2C_FM); // init with Fast Mode (400KHz)
|
I2C_init(I2C_FM); // init with Fast Mode (400KHz)
|
||||||
I2C_slave_init(&FRAM, 0x50, I2C_16B_REG);
|
I2C_slave_init(&FRAM, 0x50, I2C_16B_REG);
|
||||||
I2C_slave_set_rw_func(&FRAM, I2C_wr_advanced, I2C_WRITE);
|
I2C_slave_set_rw_func(&FRAM, (ci2c_fct_ptr) I2C_wr_advanced, I2C_WRITE);
|
||||||
I2C_slave_set_rw_func(&FRAM, I2C_rd_advanced, I2C_READ);
|
I2C_slave_set_rw_func(&FRAM, (ci2c_fct_ptr) I2C_rd_advanced, I2C_READ);
|
||||||
|
|
||||||
I2C_get_chip_id(&FRAM, &str[0]);
|
I2C_get_chip_id(&FRAM, &str[0]);
|
||||||
|
|
||||||
@ -62,8 +62,10 @@ void loop() {
|
|||||||
* \param [in] bytes - indicates how many bytes of data to write
|
* \param [in] bytes - indicates how many bytes of data to write
|
||||||
* \return Boolean indicating success/fail of write attempt
|
* \return Boolean indicating success/fail of write attempt
|
||||||
*/
|
*/
|
||||||
bool I2C_wr_advanced(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
bool I2C_wr_advanced(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes)
|
||||||
{
|
{
|
||||||
|
if (bytes == 0) { return false; }
|
||||||
|
|
||||||
slave->reg_addr = reg_addr;
|
slave->reg_addr = reg_addr;
|
||||||
|
|
||||||
if (I2C_start() == false) { return false; }
|
if (I2C_start() == false) { return false; }
|
||||||
@ -96,11 +98,11 @@ bool I2C_wr_advanced(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint1
|
|||||||
* \param [in] bytes - indicates how many bytes of data to read
|
* \param [in] bytes - indicates how many bytes of data to read
|
||||||
* \return Boolean indicating success/fail of read attempt
|
* \return Boolean indicating success/fail of read attempt
|
||||||
*/
|
*/
|
||||||
bool I2C_rd_advanced(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
bool I2C_rd_advanced(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes)
|
||||||
{
|
{
|
||||||
slave->reg_addr = reg_addr;
|
if (bytes == 0) { return false; }
|
||||||
|
|
||||||
if (bytes == 0) { bytes = 1; }
|
slave->reg_addr = reg_addr;
|
||||||
|
|
||||||
if (slave->cfg.reg_size)
|
if (slave->cfg.reg_size)
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=cI2C
|
name=cI2C
|
||||||
version=0.6
|
version=1.0
|
||||||
author=SMFSW <xgarmanboziax@gmail.com>
|
author=SMFSW <xgarmanboziax@gmail.com>
|
||||||
maintainer=SMFSW <xgarmanboziax@gmail.com>
|
maintainer=SMFSW <xgarmanboziax@gmail.com>
|
||||||
sentence=Arduino Hardware I2C for AVR (in plain c)
|
sentence=Arduino Hardware I2C for AVR (in plain c)
|
||||||
|
76
src/ci2c.c
76
src/ci2c.c
@ -1,6 +1,6 @@
|
|||||||
/*!\file ci2c.c
|
/*!\file ci2c.c
|
||||||
** \author SMFSW
|
** \author SMFSW
|
||||||
** \version 0.6
|
** \version 1.0
|
||||||
** \copyright MIT SMFSW (2017)
|
** \copyright MIT SMFSW (2017)
|
||||||
** \brief arduino master i2c in plain c code
|
** \brief arduino master i2c in plain c code
|
||||||
**/
|
**/
|
||||||
@ -53,8 +53,8 @@ static struct {
|
|||||||
|
|
||||||
|
|
||||||
// Needed prototypes
|
// Needed prototypes
|
||||||
static bool I2C_wr(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes);
|
static bool I2C_wr(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
|
||||||
static bool I2C_rd(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes);
|
static bool I2C_rd(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
|
||||||
|
|
||||||
|
|
||||||
/*! \brief Init an I2C slave structure for cMI2C communication
|
/*! \brief Init an I2C slave structure for cMI2C communication
|
||||||
@ -63,7 +63,7 @@ static bool I2C_rd(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_
|
|||||||
* \param [in] reg_sz - internal register map size
|
* \param [in] reg_sz - internal register map size
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
void I2C_slave_init(I2C_SLAVE * slave, uint8_t sl_addr, I2C_INT_SIZE reg_sz)
|
void I2C_slave_init(I2C_SLAVE * slave, const uint8_t sl_addr, const I2C_INT_SIZE reg_sz)
|
||||||
{
|
{
|
||||||
(void) I2C_slave_set_addr(slave, sl_addr);
|
(void) I2C_slave_set_addr(slave, sl_addr);
|
||||||
(void) I2C_slave_set_reg_size(slave, reg_sz);
|
(void) I2C_slave_set_reg_size(slave, reg_sz);
|
||||||
@ -79,7 +79,7 @@ void I2C_slave_init(I2C_SLAVE * slave, uint8_t sl_addr, I2C_INT_SIZE reg_sz)
|
|||||||
* \param [in] rw - 0 = write function, 1 = read function
|
* \param [in] rw - 0 = write function, 1 = read function
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
void I2C_slave_set_rw_func(I2C_SLAVE * slave, ci2c_fct_ptr func, I2C_RW rw)
|
void I2C_slave_set_rw_func(I2C_SLAVE * slave, const ci2c_fct_ptr func, const I2C_RW rw)
|
||||||
{
|
{
|
||||||
ci2c_fct_ptr * pfc = (ci2c_fct_ptr*) (rw ? &slave->cfg.rd : &slave->cfg.wr);
|
ci2c_fct_ptr * pfc = (ci2c_fct_ptr*) (rw ? &slave->cfg.rd : &slave->cfg.wr);
|
||||||
*pfc = func;
|
*pfc = func;
|
||||||
@ -90,7 +90,7 @@ void I2C_slave_set_rw_func(I2C_SLAVE * slave, ci2c_fct_ptr func, I2C_RW rw)
|
|||||||
* \param [in] sl_addr - I2C slave address
|
* \param [in] sl_addr - I2C slave address
|
||||||
* \return true if new address set (false if address is >7Fh)
|
* \return true if new address set (false if address is >7Fh)
|
||||||
*/
|
*/
|
||||||
bool I2C_slave_set_addr(I2C_SLAVE * slave, uint8_t sl_addr)
|
bool I2C_slave_set_addr(I2C_SLAVE * slave, const uint8_t sl_addr)
|
||||||
{
|
{
|
||||||
if (sl_addr > 0x7F) { return false; }
|
if (sl_addr > 0x7F) { return false; }
|
||||||
slave->cfg.addr = sl_addr;
|
slave->cfg.addr = sl_addr;
|
||||||
@ -102,7 +102,7 @@ bool I2C_slave_set_addr(I2C_SLAVE * slave, uint8_t sl_addr)
|
|||||||
* \param [in] reg_sz - internal register map size
|
* \param [in] reg_sz - internal register map size
|
||||||
* \return true if new size is correct (false otherwise and set to 16bit by default)
|
* \return true if new size is correct (false otherwise and set to 16bit by default)
|
||||||
*/
|
*/
|
||||||
bool I2C_slave_set_reg_size(I2C_SLAVE * slave, I2C_INT_SIZE reg_sz)
|
bool I2C_slave_set_reg_size(I2C_SLAVE * slave, const I2C_INT_SIZE reg_sz)
|
||||||
{
|
{
|
||||||
slave->cfg.reg_size = reg_sz > I2C_16B_REG ? I2C_16B_REG : reg_sz;
|
slave->cfg.reg_size = reg_sz > I2C_16B_REG ? I2C_16B_REG : reg_sz;
|
||||||
return !(reg_sz > I2C_16B_REG);
|
return !(reg_sz > I2C_16B_REG);
|
||||||
@ -114,10 +114,8 @@ bool I2C_slave_set_reg_size(I2C_SLAVE * slave, I2C_INT_SIZE reg_sz)
|
|||||||
* \param [in] reg_addr - register address
|
* \param [in] reg_addr - register address
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
static inline void __attribute__((__always_inline__)) I2C_slave_set_reg_addr(I2C_SLAVE * slave, uint16_t reg_addr)
|
static inline void __attribute__((__always_inline__)) I2C_slave_set_reg_addr(I2C_SLAVE * slave, const uint16_t reg_addr) {
|
||||||
{
|
slave->reg_addr = reg_addr; }
|
||||||
slave->reg_addr = reg_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -126,7 +124,7 @@ static inline void __attribute__((__always_inline__)) I2C_slave_set_reg_addr(I2C
|
|||||||
* \param [in] speed - I2C bus speed in KHz
|
* \param [in] speed - I2C bus speed in KHz
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
void I2C_init(uint16_t speed)
|
void I2C_init(const uint16_t speed)
|
||||||
{
|
{
|
||||||
// Set SDA and SCL to ports with pull-ups
|
// Set SDA and SCL to ports with pull-ups
|
||||||
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
|
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
|
||||||
@ -172,7 +170,7 @@ void I2C_reset(void)
|
|||||||
* \param [in] speed - I2C speed in kHz (max 1MHz)
|
* \param [in] speed - I2C speed in kHz (max 1MHz)
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
bool I2C_set_speed(uint16_t speed)
|
bool I2C_set_speed(const uint16_t speed)
|
||||||
{
|
{
|
||||||
i2c.cfg.speed = (I2C_SPEED) ((speed == 0) ? (uint16_t) I2C_STD : ((speed > (uint16_t) I2C_HS) ? (uint16_t) I2C_STD : speed));
|
i2c.cfg.speed = (I2C_SPEED) ((speed == 0) ? (uint16_t) I2C_STD : ((speed > (uint16_t) I2C_HS) ? (uint16_t) I2C_STD : speed));
|
||||||
|
|
||||||
@ -192,7 +190,7 @@ bool I2C_set_speed(uint16_t speed)
|
|||||||
* \param [in] timeout - I2C ack timeout (500 ms max)
|
* \param [in] timeout - I2C ack timeout (500 ms max)
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
bool I2C_set_timeout(uint16_t timeout)
|
bool I2C_set_timeout(const uint16_t timeout)
|
||||||
{
|
{
|
||||||
static const uint16_t max_timeout = 500;
|
static const uint16_t max_timeout = 500;
|
||||||
i2c.cfg.timeout = (timeout > max_timeout) ? max_timeout : timeout;
|
i2c.cfg.timeout = (timeout > max_timeout) ? max_timeout : timeout;
|
||||||
@ -203,7 +201,7 @@ bool I2C_set_timeout(uint16_t timeout)
|
|||||||
* \param [in] retries - I2C number of retries (max of 8)
|
* \param [in] retries - I2C number of retries (max of 8)
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
bool I2C_set_retries(uint8_t retries)
|
bool I2C_set_retries(const uint8_t retries)
|
||||||
{
|
{
|
||||||
static const uint16_t max_retries = 8;
|
static const uint16_t max_retries = 8;
|
||||||
i2c.cfg.retries = (retries > max_retries) ? max_retries : retries;
|
i2c.cfg.retries = (retries > max_retries) ? max_retries : retries;
|
||||||
@ -213,10 +211,8 @@ bool I2C_set_retries(uint8_t retries)
|
|||||||
/*! \brief Get I2C busy status
|
/*! \brief Get I2C busy status
|
||||||
* \return true if busy
|
* \return true if busy
|
||||||
*/
|
*/
|
||||||
bool I2C_is_busy(void)
|
bool I2C_is_busy(void) {
|
||||||
{
|
return i2c.busy; }
|
||||||
return i2c.busy;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*! \brief This function reads or writes the provided data to/from the address specified.
|
/*! \brief This function reads or writes the provided data to/from the address specified.
|
||||||
@ -229,7 +225,7 @@ bool I2C_is_busy(void)
|
|||||||
* \param [in] rw - 0 = write, 1 = read operation
|
* \param [in] rw - 0 = write, 1 = read operation
|
||||||
* \return I2C_STATUS status of write attempt
|
* \return I2C_STATUS status of write attempt
|
||||||
*/
|
*/
|
||||||
static I2C_STATUS I2C_comm(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes, I2C_RW rw)
|
static I2C_STATUS I2C_comm(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes, const I2C_RW rw)
|
||||||
{
|
{
|
||||||
uint8_t retry = i2c.cfg.retries;
|
uint8_t retry = i2c.cfg.retries;
|
||||||
bool ack = false;
|
bool ack = false;
|
||||||
@ -257,10 +253,8 @@ static I2C_STATUS I2C_comm(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data,
|
|||||||
* \param [in] bytes - indicates how many bytes of data to write
|
* \param [in] bytes - indicates how many bytes of data to write
|
||||||
* \return I2C_STATUS status of write attempt
|
* \return I2C_STATUS status of write attempt
|
||||||
*/
|
*/
|
||||||
I2C_STATUS I2C_write(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
I2C_STATUS I2C_write(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes) {
|
||||||
{
|
return I2C_comm(slave, reg_addr, data, bytes, I2C_WRITE); }
|
||||||
return I2C_comm(slave, reg_addr, data, bytes, I2C_WRITE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief This function reads data from the address specified and stores this
|
/*! \brief This function reads data from the address specified and stores this
|
||||||
* data in the area provided by the pointer.
|
* data in the area provided by the pointer.
|
||||||
@ -270,29 +264,23 @@ I2C_STATUS I2C_write(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint1
|
|||||||
* \param [in] bytes - indicates how many bytes of data to read
|
* \param [in] bytes - indicates how many bytes of data to read
|
||||||
* \return I2C_STATUS status of read attempt
|
* \return I2C_STATUS status of read attempt
|
||||||
*/
|
*/
|
||||||
I2C_STATUS I2C_read(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
I2C_STATUS I2C_read(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes) {
|
||||||
{
|
return I2C_comm(slave, reg_addr, data, bytes, I2C_READ); }
|
||||||
return I2C_comm(slave, reg_addr, data, bytes, I2C_READ);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*! \brief Start i2c_timeout timer
|
/*! \brief Start i2c_timeout timer
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
static inline void __attribute__((__always_inline__)) I2C_start_timeout(void)
|
static inline void __attribute__((__always_inline__)) I2C_start_timeout(void) {
|
||||||
{
|
i2c.start_wait = (uint16_t) millis(); }
|
||||||
i2c.start_wait = (uint16_t) millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief Test i2c_timeout
|
/*! \brief Test i2c_timeout
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
* \return true if i2c_timeout occured (false otherwise)
|
* \return true if i2c_timeout occured (false otherwise)
|
||||||
*/
|
*/
|
||||||
static inline uint8_t __attribute__((__always_inline__)) I2C_timeout(void)
|
static inline uint8_t __attribute__((__always_inline__)) I2C_timeout(void) {
|
||||||
{
|
return (((uint16_t) millis() - i2c.start_wait) >= i2c.cfg.timeout); }
|
||||||
return (((uint16_t) millis() - i2c.start_wait) >= i2c.cfg.timeout);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief Send start condition
|
/*! \brief Send start condition
|
||||||
* \return true if start condition acknowledged (false otherwise)
|
* \return true if start condition acknowledged (false otherwise)
|
||||||
@ -331,7 +319,7 @@ bool I2C_stop(void)
|
|||||||
* \param [in] dat - data to be sent
|
* \param [in] dat - data to be sent
|
||||||
* \return true if data sent acknowledged (false otherwise)
|
* \return true if data sent acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
bool I2C_wr8(uint8_t dat)
|
bool I2C_wr8(const uint8_t dat)
|
||||||
{
|
{
|
||||||
TWDR = dat;
|
TWDR = dat;
|
||||||
|
|
||||||
@ -354,7 +342,7 @@ bool I2C_wr8(uint8_t dat)
|
|||||||
* \param [in] ack - true if wait for ack
|
* \param [in] ack - true if wait for ack
|
||||||
* \return true if data reception acknowledged (false otherwise)
|
* \return true if data reception acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
uint8_t I2C_rd8(bool ack)
|
uint8_t I2C_rd8(const bool ack)
|
||||||
{
|
{
|
||||||
I2C_start_timeout();
|
I2C_start_timeout();
|
||||||
|
|
||||||
@ -374,7 +362,7 @@ uint8_t I2C_rd8(bool ack)
|
|||||||
* \param [in] rw - read/write transaction
|
* \param [in] rw - read/write transaction
|
||||||
* \return true if I2C chip address sent acknowledged (false otherwise)
|
* \return true if I2C chip address sent acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
bool I2C_sndAddr(I2C_SLAVE * slave, I2C_RW rw)
|
bool I2C_sndAddr(I2C_SLAVE * slave, const I2C_RW rw)
|
||||||
{
|
{
|
||||||
TWDR = (slave->cfg.addr << 1) | rw;
|
TWDR = (slave->cfg.addr << 1) | rw;
|
||||||
|
|
||||||
@ -401,8 +389,10 @@ bool I2C_sndAddr(I2C_SLAVE * slave, I2C_RW rw)
|
|||||||
* \param [in] bytes - indicates how many bytes of data to write
|
* \param [in] bytes - indicates how many bytes of data to write
|
||||||
* \return Boolean indicating success/fail of write attempt
|
* \return Boolean indicating success/fail of write attempt
|
||||||
*/
|
*/
|
||||||
static bool I2C_wr(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
static bool I2C_wr(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes)
|
||||||
{
|
{
|
||||||
|
if (bytes == 0) { return false; }
|
||||||
|
|
||||||
(void) I2C_slave_set_reg_addr(slave, reg_addr);
|
(void) I2C_slave_set_reg_addr(slave, reg_addr);
|
||||||
|
|
||||||
if (I2C_start() == false) { return false; }
|
if (I2C_start() == false) { return false; }
|
||||||
@ -435,11 +425,11 @@ static bool I2C_wr(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_
|
|||||||
* \param [in] bytes - indicates how many bytes of data to read
|
* \param [in] bytes - indicates how many bytes of data to read
|
||||||
* \return Boolean indicating success/fail of read attempt
|
* \return Boolean indicating success/fail of read attempt
|
||||||
*/
|
*/
|
||||||
static bool I2C_rd(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes)
|
static bool I2C_rd(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes)
|
||||||
{
|
{
|
||||||
(void) I2C_slave_set_reg_addr(slave, reg_addr);
|
if (bytes == 0) { return false; }
|
||||||
|
|
||||||
if (bytes == 0) { bytes = 1; }
|
(void) I2C_slave_set_reg_addr(slave, reg_addr);
|
||||||
|
|
||||||
if (slave->cfg.reg_size) // If start register has to be sent first
|
if (slave->cfg.reg_size) // If start register has to be sent first
|
||||||
{
|
{
|
||||||
|
76
src/ci2c.h
76
src/ci2c.h
@ -1,6 +1,6 @@
|
|||||||
/*!\file ci2c.h
|
/*!\file ci2c.h
|
||||||
** \author SMFSW
|
** \author SMFSW
|
||||||
** \version 0.6
|
** \version 1.0
|
||||||
** \copyright MIT SMFSW (2017)
|
** \copyright MIT SMFSW (2017)
|
||||||
** \brief arduino i2c in plain c declarations
|
** \brief arduino i2c in plain c declarations
|
||||||
**/
|
**/
|
||||||
@ -75,7 +75,8 @@ typedef enum __attribute__((__packed__)) enI2C_INT_SIZE {
|
|||||||
} I2C_INT_SIZE;
|
} I2C_INT_SIZE;
|
||||||
|
|
||||||
|
|
||||||
typedef bool (*ci2c_fct_ptr) (const void*, uint16_t, uint8_t*, uint16_t); //!< i2c read/write function pointer typedef
|
typedef bool (*ci2c_fct_ptr) (void*, const uint16_t, uint8_t*, const uint16_t); //!< i2c read/write function pointer typedef
|
||||||
|
|
||||||
|
|
||||||
/*! \struct StructI2CSlave
|
/*! \struct StructI2CSlave
|
||||||
* \brief ci2c slave config and control parameters
|
* \brief ci2c slave config and control parameters
|
||||||
@ -106,7 +107,7 @@ typedef struct __attribute__((__packed__)) StructI2CSlave {
|
|||||||
* \param [in] reg_sz - internal register map size
|
* \param [in] reg_sz - internal register map size
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
extern void I2C_slave_init(I2C_SLAVE * slave, uint8_t sl_addr, I2C_INT_SIZE reg_sz);
|
void I2C_slave_init(I2C_SLAVE * slave, const uint8_t sl_addr, const I2C_INT_SIZE reg_sz);
|
||||||
|
|
||||||
/*! \brief Redirect slave I2C read/write function (if needed for advanced use)
|
/*! \brief Redirect slave I2C read/write function (if needed for advanced use)
|
||||||
* \param [in] slave - pointer to the I2C slave structure to init
|
* \param [in] slave - pointer to the I2C slave structure to init
|
||||||
@ -114,51 +115,45 @@ extern void I2C_slave_init(I2C_SLAVE * slave, uint8_t sl_addr, I2C_INT_SIZE reg_
|
|||||||
* \param [in] rw - 0 = write function, 1 = read function
|
* \param [in] rw - 0 = write function, 1 = read function
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
extern void I2C_slave_set_rw_func(I2C_SLAVE * slave, ci2c_fct_ptr func, I2C_RW rw);
|
void I2C_slave_set_rw_func(I2C_SLAVE * slave, const ci2c_fct_ptr func, const I2C_RW rw);
|
||||||
|
|
||||||
/*! \brief Change I2C slave address
|
/*! \brief Change I2C slave address
|
||||||
* \param [in, out] slave - pointer to the I2C slave structure to init
|
* \param [in, out] slave - pointer to the I2C slave structure to init
|
||||||
* \param [in] sl_addr - I2C slave address
|
* \param [in] sl_addr - I2C slave address
|
||||||
* \return true if new address set (false if address is >7Fh)
|
* \return true if new address set (false if address is >7Fh)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_slave_set_addr(I2C_SLAVE * slave, uint8_t sl_addr);
|
bool I2C_slave_set_addr(I2C_SLAVE * slave, const uint8_t sl_addr);
|
||||||
|
|
||||||
/*! \brief Change I2C registers map size (for access)
|
/*! \brief Change I2C registers map size (for access)
|
||||||
* \param [in, out] slave - pointer to the I2C slave structure
|
* \param [in, out] slave - pointer to the I2C slave structure
|
||||||
* \param [in] reg_sz - internal register map size
|
* \param [in] reg_sz - internal register map size
|
||||||
* \return true if new size is correct (false otherwise and set to 16bit by default)
|
* \return true if new size is correct (false otherwise and set to 16bit by default)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_slave_set_reg_size(I2C_SLAVE * slave, I2C_INT_SIZE reg_sz);
|
bool I2C_slave_set_reg_size(I2C_SLAVE * slave, const I2C_INT_SIZE reg_sz);
|
||||||
|
|
||||||
/*! \brief Get I2C slave address
|
/*! \brief Get I2C slave address
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
* \param [in] slave - pointer to the I2C slave structure
|
* \param [in] slave - pointer to the I2C slave structure
|
||||||
* \return I2C slave address
|
* \return I2C slave address
|
||||||
*/
|
*/
|
||||||
inline uint8_t __attribute__((__always_inline__)) I2C_slave_get_addr(I2C_SLAVE * slave)
|
inline uint8_t __attribute__((__always_inline__)) I2C_slave_get_addr(const I2C_SLAVE * slave) {
|
||||||
{
|
return slave->cfg.addr; }
|
||||||
return slave->cfg.addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief Get I2C register map size (for access)
|
/*! \brief Get I2C register map size (for access)
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
* \param [in] slave - pointer to the I2C slave structure
|
* \param [in] slave - pointer to the I2C slave structure
|
||||||
* \return register map using 16bits if true (1Byte otherwise)
|
* \return register map using 16bits if true (1Byte otherwise)
|
||||||
*/
|
*/
|
||||||
inline bool __attribute__((__always_inline__)) I2C_slave_get_reg_size(I2C_SLAVE * slave)
|
inline bool __attribute__((__always_inline__)) I2C_slave_get_reg_size(const I2C_SLAVE * slave) {
|
||||||
{
|
return slave->cfg.reg_size; }
|
||||||
return slave->cfg.reg_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief Get I2C current register address (addr may passed this way in procedures if contigous accesses)
|
/*! \brief Get I2C current register address (addr may passed this way in procedures if contigous accesses)
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
* \param [in] slave - pointer to the I2C slave structure
|
* \param [in] slave - pointer to the I2C slave structure
|
||||||
* \return current register map address
|
* \return current register map address
|
||||||
*/
|
*/
|
||||||
inline uint16_t __attribute__((__always_inline__)) I2C_slave_get_reg_addr(I2C_SLAVE * slave)
|
inline uint16_t __attribute__((__always_inline__)) I2C_slave_get_reg_addr(const I2C_SLAVE * slave) {
|
||||||
{
|
return slave->reg_addr; }
|
||||||
return slave->reg_addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************/
|
/*************************/
|
||||||
/*** I2C BUS FUNCTIONS ***/
|
/*** I2C BUS FUNCTIONS ***/
|
||||||
@ -169,35 +164,35 @@ inline uint16_t __attribute__((__always_inline__)) I2C_slave_get_reg_addr(I2C_SL
|
|||||||
* \param [in] speed - I2C bus speed in KHz
|
* \param [in] speed - I2C bus speed in KHz
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
extern void I2C_init(uint16_t speed);
|
void I2C_init(const uint16_t speed);
|
||||||
|
|
||||||
/*! \brief Disable I2c module on arduino board (releasing pull-ups, and TWI control)
|
/*! \brief Disable I2c module on arduino board (releasing pull-ups, and TWI control)
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
extern void I2C_uninit();
|
void I2C_uninit();
|
||||||
|
|
||||||
/*! \brief Change I2C frequency
|
/*! \brief Change I2C frequency
|
||||||
* \param [in] speed - I2C bus speed in KHz
|
* \param [in] speed - I2C bus speed in KHz
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_set_speed(uint16_t speed);
|
bool I2C_set_speed(const uint16_t speed);
|
||||||
|
|
||||||
/*! \brief Change I2C ack timeout
|
/*! \brief Change I2C ack timeout
|
||||||
* \param [in] timeout - I2C ack timeout (500 ms max)
|
* \param [in] timeout - I2C ack timeout (500 ms max)
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_set_timeout(uint16_t timeout);
|
bool I2C_set_timeout(const uint16_t timeout);
|
||||||
|
|
||||||
/*! \brief Change I2C message retries (in case of failure)
|
/*! \brief Change I2C message retries (in case of failure)
|
||||||
* \param [in] retries - I2C number of retries (max of 8)
|
* \param [in] retries - I2C number of retries (max of 8)
|
||||||
* \return true if change is successful (false otherwise)
|
* \return true if change is successful (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_set_retries(uint8_t retries);
|
bool I2C_set_retries(const uint8_t retries);
|
||||||
|
|
||||||
/*! \brief Get I2C busy status
|
/*! \brief Get I2C busy status
|
||||||
* \return true if busy
|
* \return true if busy
|
||||||
*/
|
*/
|
||||||
extern bool I2C_is_busy(void);
|
bool I2C_is_busy(void);
|
||||||
|
|
||||||
/*! \brief This function writes the provided data to the address specified.
|
/*! \brief This function writes the provided data to the address specified.
|
||||||
* \param [in, out] slave - pointer to the I2C slave structure
|
* \param [in, out] slave - pointer to the I2C slave structure
|
||||||
@ -206,7 +201,7 @@ extern bool I2C_is_busy(void);
|
|||||||
* \param [in] bytes - indicates how many bytes of data to write
|
* \param [in] bytes - indicates how many bytes of data to write
|
||||||
* \return I2C_STATUS status of write attempt
|
* \return I2C_STATUS status of write attempt
|
||||||
*/
|
*/
|
||||||
extern I2C_STATUS I2C_write(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes);
|
I2C_STATUS I2C_write(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
|
||||||
|
|
||||||
/*! \brief This inline is a wrapper to I2C_write in case of contigous operations
|
/*! \brief This inline is a wrapper to I2C_write in case of contigous operations
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
@ -215,11 +210,9 @@ extern I2C_STATUS I2C_write(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data
|
|||||||
* \param [in] bytes - indicates how many bytes of data to write
|
* \param [in] bytes - indicates how many bytes of data to write
|
||||||
* \return I2C_STATUS status of write attempt
|
* \return I2C_STATUS status of write attempt
|
||||||
*/
|
*/
|
||||||
inline I2C_STATUS __attribute__((__always_inline__)) I2C_write_next(I2C_SLAVE * slave, uint8_t * data, uint16_t bytes)
|
inline I2C_STATUS __attribute__((__always_inline__)) I2C_write_next(I2C_SLAVE * slave, uint8_t * data, const uint16_t bytes) {
|
||||||
{
|
|
||||||
// TODO: implement read next so that it doesn't have to send start register address again
|
// TODO: implement read next so that it doesn't have to send start register address again
|
||||||
return I2C_write(slave, slave->reg_addr, data, bytes);
|
return I2C_write(slave, slave->reg_addr, data, bytes); }
|
||||||
}
|
|
||||||
|
|
||||||
/*! \brief This function reads data from the address specified and stores this
|
/*! \brief This function reads data from the address specified and stores this
|
||||||
* data in the area provided by the pointer.
|
* data in the area provided by the pointer.
|
||||||
@ -229,7 +222,7 @@ inline I2C_STATUS __attribute__((__always_inline__)) I2C_write_next(I2C_SLAVE *
|
|||||||
* \param [in] bytes - indicates how many bytes of data to read
|
* \param [in] bytes - indicates how many bytes of data to read
|
||||||
* \return I2C_STATUS status of read attempt
|
* \return I2C_STATUS status of read attempt
|
||||||
*/
|
*/
|
||||||
extern I2C_STATUS I2C_read(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data, uint16_t bytes);
|
I2C_STATUS I2C_read(I2C_SLAVE * slave, const uint16_t reg_addr, uint8_t * data, const uint16_t bytes);
|
||||||
|
|
||||||
/*! \brief This inline is a wrapper to I2C_read in case of contigous operations
|
/*! \brief This inline is a wrapper to I2C_read in case of contigous operations
|
||||||
* \attribute inline
|
* \attribute inline
|
||||||
@ -238,11 +231,9 @@ extern I2C_STATUS I2C_read(I2C_SLAVE * slave, uint16_t reg_addr, uint8_t * data,
|
|||||||
* \param [in] bytes - indicates how many bytes of data to read
|
* \param [in] bytes - indicates how many bytes of data to read
|
||||||
* \return I2C_STATUS status of read attempt
|
* \return I2C_STATUS status of read attempt
|
||||||
*/
|
*/
|
||||||
inline I2C_STATUS __attribute__((__always_inline__)) I2C_read_next(I2C_SLAVE * slave, uint8_t * data, uint16_t bytes)
|
inline I2C_STATUS __attribute__((__always_inline__)) I2C_read_next(I2C_SLAVE * slave, uint8_t * data, const uint16_t bytes) {
|
||||||
{
|
|
||||||
// TODO: implement read next so that it doesn't have to send start register address again
|
// TODO: implement read next so that it doesn't have to send start register address again
|
||||||
return I2C_read(slave, slave->reg_addr, data, bytes);
|
return I2C_read(slave, slave->reg_addr, data, bytes); }
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/***********************************/
|
/***********************************/
|
||||||
@ -252,31 +243,36 @@ inline I2C_STATUS __attribute__((__always_inline__)) I2C_read_next(I2C_SLAVE * s
|
|||||||
/*! \brief I2C bus reset (Release SCL and SDA lines and re-enable module)
|
/*! \brief I2C bus reset (Release SCL and SDA lines and re-enable module)
|
||||||
* \return nothing
|
* \return nothing
|
||||||
*/
|
*/
|
||||||
extern void I2C_reset(void);
|
void I2C_reset(void);
|
||||||
|
|
||||||
/*! \brief Send start condition
|
/*! \brief Send start condition
|
||||||
* \return true if start condition acknowledged (false otherwise)
|
* \return true if start condition acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_start(void);
|
bool I2C_start(void);
|
||||||
|
|
||||||
/*! \brief Send stop condition
|
/*! \brief Send stop condition
|
||||||
* \return true if stop condition acknowledged (false otherwise)
|
* \return true if stop condition acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_stop(void);
|
bool I2C_stop(void);
|
||||||
|
|
||||||
/*! \brief Send byte on bus
|
/*! \brief Send byte on bus
|
||||||
* \param [in] dat - data to be sent
|
* \param [in] dat - data to be sent
|
||||||
* \return true if data sent acknowledged (false otherwise)
|
* \return true if data sent acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_wr8(uint8_t dat);
|
bool I2C_wr8(const uint8_t dat);
|
||||||
|
|
||||||
/*! \brief Receive byte from bus
|
/*! \brief Receive byte from bus
|
||||||
* \param [in] ack - true if wait for ack
|
* \param [in] ack - true if wait for ack
|
||||||
* \return true if data reception acknowledged (false otherwise)
|
* \return true if data reception acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern uint8_t I2C_rd8(bool ack);
|
uint8_t I2C_rd8(const bool ack);
|
||||||
|
|
||||||
/*! \brief Send I2C address
|
/*! \brief Send I2C address
|
||||||
* \param [in] slave - pointer to the I2C slave structure
|
* \param [in] slave - pointer to the I2C slave structure
|
||||||
* \param [in] rw - read/write transaction
|
* \param [in] rw - read/write transaction
|
||||||
* \return true if I2C chip address sent acknowledged (false otherwise)
|
* \return true if I2C chip address sent acknowledged (false otherwise)
|
||||||
*/
|
*/
|
||||||
extern bool I2C_sndAddr(I2C_SLAVE * slave, I2C_RW rw);
|
bool I2C_sndAddr(I2C_SLAVE * slave, const I2C_RW rw);
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
Loading…
Reference in New Issue
Block a user