|
@@ -0,0 +1,561 @@
|
|
|
+
|
|
|
+ twi.c - TWI/I2C library for Wiring & Arduino
|
|
|
+ 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
|
|
|
+*/
|
|
|
+
|
|
|
+#include <math.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <inttypes.h>
|
|
|
+#include <avr/io.h>
|
|
|
+#include <avr/interrupt.h>
|
|
|
+#include <compat/twi.h>
|
|
|
+#include "Arduino.h"
|
|
|
+
|
|
|
+#ifndef cbi
|
|
|
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifndef sbi
|
|
|
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
|
|
+#endif
|
|
|
+
|
|
|
+#include "pins_arduino.h"
|
|
|
+#include "twi.h"
|
|
|
+
|
|
|
+static volatile uint8_t twi_state;
|
|
|
+static volatile uint8_t twi_slarw;
|
|
|
+static volatile uint8_t twi_sendStop;
|
|
|
+static volatile uint8_t twi_inRepStart;
|
|
|
+
|
|
|
+static void (*twi_onSlaveTransmit)(void);
|
|
|
+static void (*twi_onSlaveReceive)(uint8_t*, int);
|
|
|
+
|
|
|
+static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
|
|
|
+static volatile uint8_t twi_masterBufferIndex;
|
|
|
+static volatile uint8_t twi_masterBufferLength;
|
|
|
+
|
|
|
+static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
|
|
|
+static volatile uint8_t twi_txBufferIndex;
|
|
|
+static volatile uint8_t twi_txBufferLength;
|
|
|
+
|
|
|
+static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
|
|
|
+static volatile uint8_t twi_rxBufferIndex;
|
|
|
+
|
|
|
+static volatile uint8_t twi_error;
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_init
|
|
|
+ * Desc readys twi pins and sets twi bitrate
|
|
|
+ * Input none
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_init(void)
|
|
|
+{
|
|
|
+
|
|
|
+ twi_state = TWI_READY;
|
|
|
+ twi_sendStop = true;
|
|
|
+ twi_inRepStart = false;
|
|
|
+
|
|
|
+
|
|
|
+ digitalWrite(SDA, 1);
|
|
|
+ digitalWrite(SCL, 1);
|
|
|
+
|
|
|
+
|
|
|
+ cbi(TWSR, TWPS0);
|
|
|
+ cbi(TWSR, TWPS1);
|
|
|
+ TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
|
|
|
+
|
|
|
+
|
|
|
+ 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 */
|
|
|
+
|
|
|
+
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_disable
|
|
|
+ * Desc disables twi pins
|
|
|
+ * Input none
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_disable(void)
|
|
|
+{
|
|
|
+
|
|
|
+ TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
|
|
|
+
|
|
|
+
|
|
|
+ digitalWrite(SDA, 0);
|
|
|
+ digitalWrite(SCL, 0);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_slaveInit
|
|
|
+ * Desc sets slave address and enables interrupt
|
|
|
+ * Input none
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_setAddress(uint8_t address)
|
|
|
+{
|
|
|
+
|
|
|
+ TWAR = address << 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_setClock
|
|
|
+ * Desc sets twi bit rate
|
|
|
+ * Input Clock Frequency
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_setFrequency(uint32_t frequency)
|
|
|
+{
|
|
|
+ TWBR = ((F_CPU / frequency) - 16) / 2;
|
|
|
+
|
|
|
+
|
|
|
+ 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 */
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_readFrom
|
|
|
+ * Desc attempts to become twi bus master and read a
|
|
|
+ * series of bytes from a device on the bus
|
|
|
+ * Input address: 7bit i2c device address
|
|
|
+ * data: pointer to byte array
|
|
|
+ * length: number of bytes to read into array
|
|
|
+ * sendStop: Boolean indicating whether to send a stop at the end
|
|
|
+ * Output number of bytes read
|
|
|
+ */
|
|
|
+uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
|
|
|
+{
|
|
|
+ uint8_t i;
|
|
|
+
|
|
|
+
|
|
|
+ if(TWI_BUFFER_LENGTH < length){
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ while(TWI_READY != twi_state){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ twi_state = TWI_MRX;
|
|
|
+ twi_sendStop = sendStop;
|
|
|
+
|
|
|
+ twi_error = 0xFF;
|
|
|
+
|
|
|
+
|
|
|
+ twi_masterBufferIndex = 0;
|
|
|
+ twi_masterBufferLength = length-1;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ twi_slarw = TW_READ;
|
|
|
+ twi_slarw |= address << 1;
|
|
|
+
|
|
|
+ if (true == twi_inRepStart) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ twi_inRepStart = false;
|
|
|
+ do {
|
|
|
+ TWDR = twi_slarw;
|
|
|
+ } while(TWCR & _BV(TWWC));
|
|
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE);
|
|
|
+ }
|
|
|
+ else
|
|
|
+
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
|
|
|
+
|
|
|
+
|
|
|
+ while(TWI_MRX == twi_state){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (twi_masterBufferIndex < length)
|
|
|
+ length = twi_masterBufferIndex;
|
|
|
+
|
|
|
+
|
|
|
+ for(i = 0; i < length; ++i){
|
|
|
+ data[i] = twi_masterBuffer[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ return length;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_writeTo
|
|
|
+ * Desc attempts to become twi bus master and write a
|
|
|
+ * series of bytes to a device on the bus
|
|
|
+ * Input address: 7bit i2c device address
|
|
|
+ * data: pointer to byte array
|
|
|
+ * length: number of bytes in array
|
|
|
+ * wait: boolean indicating to wait for write or not
|
|
|
+ * sendStop: boolean indicating whether or not to send a stop at the end
|
|
|
+ * Output 0 .. success
|
|
|
+ * 1 .. length to long for buffer
|
|
|
+ * 2 .. address send, NACK received
|
|
|
+ * 3 .. data send, NACK received
|
|
|
+ * 4 .. other twi error (lost bus arbitration, bus error, ..)
|
|
|
+ */
|
|
|
+uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
|
|
|
+{
|
|
|
+ uint8_t i;
|
|
|
+
|
|
|
+
|
|
|
+ if(TWI_BUFFER_LENGTH < length){
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ while(TWI_READY != twi_state){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ twi_state = TWI_MTX;
|
|
|
+ twi_sendStop = sendStop;
|
|
|
+
|
|
|
+ twi_error = 0xFF;
|
|
|
+
|
|
|
+
|
|
|
+ twi_masterBufferIndex = 0;
|
|
|
+ twi_masterBufferLength = length;
|
|
|
+
|
|
|
+
|
|
|
+ for(i = 0; i < length; ++i){
|
|
|
+ twi_masterBuffer[i] = data[i];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ twi_slarw = TW_WRITE;
|
|
|
+ twi_slarw |= address << 1;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (true == twi_inRepStart) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ twi_inRepStart = false;
|
|
|
+ do {
|
|
|
+ TWDR = twi_slarw;
|
|
|
+ } while(TWCR & _BV(TWWC));
|
|
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE);
|
|
|
+ }
|
|
|
+ else
|
|
|
+
|
|
|
+ TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA);
|
|
|
+
|
|
|
+
|
|
|
+ while(wait && (TWI_MTX == twi_state)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (twi_error == 0xFF)
|
|
|
+ return 0;
|
|
|
+ else if (twi_error == TW_MT_SLA_NACK)
|
|
|
+ return 2;
|
|
|
+ else if (twi_error == TW_MT_DATA_NACK)
|
|
|
+ return 3;
|
|
|
+ else
|
|
|
+ return 4;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_transmit
|
|
|
+ * Desc fills slave tx buffer with data
|
|
|
+ * must be called in slave tx event callback
|
|
|
+ * Input data: pointer to byte array
|
|
|
+ * length: number of bytes in array
|
|
|
+ * Output 1 length too long for buffer
|
|
|
+ * 2 not slave transmitter
|
|
|
+ * 0 ok
|
|
|
+ */
|
|
|
+uint8_t twi_transmit(const uint8_t* data, uint8_t length)
|
|
|
+{
|
|
|
+ uint8_t i;
|
|
|
+
|
|
|
+
|
|
|
+ if(TWI_BUFFER_LENGTH < (twi_txBufferLength+length)){
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(TWI_STX != twi_state){
|
|
|
+ return 2;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for(i = 0; i < length; ++i){
|
|
|
+ twi_txBuffer[twi_txBufferLength+i] = data[i];
|
|
|
+ }
|
|
|
+ twi_txBufferLength += length;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_attachSlaveRxEvent
|
|
|
+ * Desc sets function called before a slave read operation
|
|
|
+ * Input function: callback function to use
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
|
|
|
+{
|
|
|
+ twi_onSlaveReceive = function;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_attachSlaveTxEvent
|
|
|
+ * Desc sets function called before a slave write operation
|
|
|
+ * Input function: callback function to use
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_attachSlaveTxEvent( void (*function)(void) )
|
|
|
+{
|
|
|
+ twi_onSlaveTransmit = function;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_reply
|
|
|
+ * Desc sends byte or readys receive line
|
|
|
+ * Input ack: byte indicating to ack or to nack
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_reply(uint8_t ack)
|
|
|
+{
|
|
|
+
|
|
|
+ if(ack){
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
|
|
|
+ }else{
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_stop
|
|
|
+ * Desc relinquishes bus master status
|
|
|
+ * Input none
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_stop(void)
|
|
|
+{
|
|
|
+
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ while(TWCR & _BV(TWSTO)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ twi_state = TWI_READY;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+ * Function twi_releaseBus
|
|
|
+ * Desc releases bus control
|
|
|
+ * Input none
|
|
|
+ * Output none
|
|
|
+ */
|
|
|
+void twi_releaseBus(void)
|
|
|
+{
|
|
|
+
|
|
|
+ TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
|
|
|
+
|
|
|
+
|
|
|
+ twi_state = TWI_READY;
|
|
|
+}
|
|
|
+
|
|
|
+ISR(TWI_vect)
|
|
|
+{
|
|
|
+ switch(TW_STATUS){
|
|
|
+
|
|
|
+ case TW_START:
|
|
|
+ case TW_REP_START:
|
|
|
+
|
|
|
+ TWDR = twi_slarw;
|
|
|
+ twi_reply(1);
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case TW_MT_SLA_ACK:
|
|
|
+ case TW_MT_DATA_ACK:
|
|
|
+
|
|
|
+ if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
|
+
|
|
|
+ TWDR = twi_masterBuffer[twi_masterBufferIndex++];
|
|
|
+ twi_reply(1);
|
|
|
+ }else{
|
|
|
+ if (twi_sendStop)
|
|
|
+ twi_stop();
|
|
|
+ else {
|
|
|
+ twi_inRepStart = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
|
+ twi_state = TWI_READY;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TW_MT_SLA_NACK:
|
|
|
+ twi_error = TW_MT_SLA_NACK;
|
|
|
+ twi_stop();
|
|
|
+ break;
|
|
|
+ case TW_MT_DATA_NACK:
|
|
|
+ twi_error = TW_MT_DATA_NACK;
|
|
|
+ twi_stop();
|
|
|
+ break;
|
|
|
+ case TW_MT_ARB_LOST:
|
|
|
+ twi_error = TW_MT_ARB_LOST;
|
|
|
+ twi_releaseBus();
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case TW_MR_DATA_ACK:
|
|
|
+
|
|
|
+ twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
|
+ case TW_MR_SLA_ACK:
|
|
|
+
|
|
|
+ if(twi_masterBufferIndex < twi_masterBufferLength){
|
|
|
+ twi_reply(1);
|
|
|
+ }else{
|
|
|
+ twi_reply(0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TW_MR_DATA_NACK:
|
|
|
+
|
|
|
+ twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
|
|
+ if (twi_sendStop)
|
|
|
+ twi_stop();
|
|
|
+ else {
|
|
|
+ twi_inRepStart = true;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
|
|
|
+ twi_state = TWI_READY;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TW_MR_SLA_NACK:
|
|
|
+ twi_stop();
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ case TW_SR_SLA_ACK:
|
|
|
+ case TW_SR_GCALL_ACK:
|
|
|
+ case TW_SR_ARB_LOST_SLA_ACK:
|
|
|
+ case TW_SR_ARB_LOST_GCALL_ACK:
|
|
|
+
|
|
|
+ twi_state = TWI_SRX;
|
|
|
+
|
|
|
+ twi_rxBufferIndex = 0;
|
|
|
+ twi_reply(1);
|
|
|
+ break;
|
|
|
+ case TW_SR_DATA_ACK:
|
|
|
+ case TW_SR_GCALL_DATA_ACK:
|
|
|
+
|
|
|
+ if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
|
+
|
|
|
+ twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
|
|
|
+ twi_reply(1);
|
|
|
+ }else{
|
|
|
+
|
|
|
+ twi_reply(0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TW_SR_STOP:
|
|
|
+
|
|
|
+ twi_releaseBus();
|
|
|
+
|
|
|
+ if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
|
|
+ twi_rxBuffer[twi_rxBufferIndex] = '\0';
|
|
|
+ }
|
|
|
+
|
|
|
+ twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
|
|
|
+
|
|
|
+ twi_rxBufferIndex = 0;
|
|
|
+ break;
|
|
|
+ case TW_SR_DATA_NACK:
|
|
|
+ case TW_SR_GCALL_DATA_NACK:
|
|
|
+
|
|
|
+ twi_reply(0);
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case TW_ST_SLA_ACK:
|
|
|
+ case TW_ST_ARB_LOST_SLA_ACK:
|
|
|
+
|
|
|
+ twi_state = TWI_STX;
|
|
|
+
|
|
|
+ twi_txBufferIndex = 0;
|
|
|
+
|
|
|
+ twi_txBufferLength = 0;
|
|
|
+
|
|
|
+
|
|
|
+ twi_onSlaveTransmit();
|
|
|
+
|
|
|
+ if(0 == twi_txBufferLength){
|
|
|
+ twi_txBufferLength = 1;
|
|
|
+ twi_txBuffer[0] = 0x00;
|
|
|
+ }
|
|
|
+
|
|
|
+ case TW_ST_DATA_ACK:
|
|
|
+
|
|
|
+ TWDR = twi_txBuffer[twi_txBufferIndex++];
|
|
|
+
|
|
|
+ if(twi_txBufferIndex < twi_txBufferLength){
|
|
|
+ twi_reply(1);
|
|
|
+ }else{
|
|
|
+ twi_reply(0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case TW_ST_DATA_NACK:
|
|
|
+ case TW_ST_LAST_DATA:
|
|
|
+
|
|
|
+ twi_reply(1);
|
|
|
+
|
|
|
+ twi_state = TWI_READY;
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ case TW_NO_INFO:
|
|
|
+ break;
|
|
|
+ case TW_BUS_ERROR:
|
|
|
+ twi_error = TW_BUS_ERROR;
|
|
|
+ twi_stop();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|