2020-08-20 10:36:01 +00:00
|
|
|
/*
|
2020-08-20 13:29:57 +00:00
|
|
|
twi.c - Stripped-down TWI/I2C library
|
2020-08-20 10:36:01 +00:00
|
|
|
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2021-01-29 16:51:38 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include "config.h"
|
|
|
|
#include "fastio.h"
|
2020-08-20 10:36:01 +00:00
|
|
|
#include "twi.h"
|
2022-02-16 23:05:09 +00:00
|
|
|
#include "Timer.h"
|
2020-08-20 10:36:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void twi_init(void)
|
|
|
|
{
|
|
|
|
// activate internal pullups for twi.
|
2021-01-29 16:51:38 +00:00
|
|
|
WRITE(SDA_PIN, 1);
|
|
|
|
WRITE(SCL_PIN, 1);
|
2020-08-20 10:36:01 +00:00
|
|
|
|
|
|
|
// initialize twi prescaler and bit rate
|
2020-09-27 14:42:20 +00:00
|
|
|
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
|
2020-08-20 10:36:01 +00:00
|
|
|
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
|
|
|
|
|
|
|
|
/* twi bit rate formula from atmega128 manual pg 204
|
|
|
|
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
|
|
|
|
note: TWBR should be 10 or higher for master mode
|
|
|
|
It is 72 for a 16mhz Wiring board with 100kHz TWI */
|
|
|
|
}
|
|
|
|
|
|
|
|
void twi_disable(void)
|
|
|
|
{
|
2022-02-16 23:05:31 +00:00
|
|
|
// disable TWI hardware.
|
|
|
|
TWCR = 0;
|
|
|
|
|
2020-08-20 10:36:01 +00:00
|
|
|
// deactivate internal pullups for twi.
|
2021-01-29 16:51:38 +00:00
|
|
|
WRITE(SDA_PIN, 0);
|
|
|
|
WRITE(SCL_PIN, 0);
|
2020-08-20 10:36:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
|
|
|
|
static void twi_stop()
|
2020-08-20 10:36:01 +00:00
|
|
|
{
|
2020-09-28 18:21:07 +00:00
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
|
2020-08-20 10:36:01 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
|
|
|
|
static uint8_t twi_wait(uint8_t status)
|
|
|
|
{
|
2022-02-16 23:05:09 +00:00
|
|
|
static ShortTimer timmy;
|
|
|
|
timmy.start();
|
|
|
|
while(!(TWCR & _BV(TWINT))) {
|
|
|
|
if (timmy.expired(TWI_TIMEOUT_MS)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 18:21:07 +00:00
|
|
|
if(TW_STATUS != status)
|
|
|
|
{
|
|
|
|
twi_stop();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t twi_start(uint8_t address, uint8_t reg)
|
2020-08-20 10:36:01 +00:00
|
|
|
{
|
2020-08-20 13:29:57 +00:00
|
|
|
// send start condition
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
|
2020-09-28 18:21:07 +00:00
|
|
|
if(twi_wait(TW_START))
|
2020-08-20 13:29:57 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// send address
|
2020-09-28 18:21:07 +00:00
|
|
|
TWDR = TW_WRITE | (address << 1);
|
2020-08-20 13:29:57 +00:00
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT);
|
2020-09-28 18:21:07 +00:00
|
|
|
if(twi_wait(TW_MT_SLA_ACK))
|
|
|
|
return 2;
|
2020-08-20 13:29:57 +00:00
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
// send register
|
|
|
|
TWDR = reg;
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT);
|
|
|
|
if(twi_wait(TW_MT_DATA_ACK))
|
|
|
|
return 3;
|
2020-08-25 09:58:48 +00:00
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-25 09:58:48 +00:00
|
|
|
|
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data)
|
|
|
|
{
|
|
|
|
if(twi_start(address, reg))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// repeat start
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
|
|
|
|
if(twi_wait(TW_REP_START))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// start receiving
|
|
|
|
TWDR = TW_READ | (address << 1);
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT);
|
|
|
|
if(twi_wait(TW_MR_SLA_ACK))
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
// receive data
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT);
|
|
|
|
if(twi_wait(TW_MR_DATA_NACK))
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
*data = TWDR;
|
2020-08-20 13:29:57 +00:00
|
|
|
|
|
|
|
// send stop
|
2020-09-28 18:21:07 +00:00
|
|
|
twi_stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:36:01 +00:00
|
|
|
|
2020-09-28 18:21:07 +00:00
|
|
|
uint8_t twi_w8(uint8_t address, uint8_t reg, uint8_t data)
|
|
|
|
{
|
|
|
|
if(twi_start(address, reg))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// send data
|
|
|
|
TWDR = data;
|
|
|
|
TWCR = _BV(TWEN) | _BV(TWINT);
|
|
|
|
if(twi_wait(TW_MT_DATA_ACK))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// send stop
|
|
|
|
twi_stop();
|
2020-08-20 10:36:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|