From 26e25a4d58ff1a970d4790e5ccc3a5f4c3dea398 Mon Sep 17 00:00:00 2001
From: Paolo Paolucci
Date: Mon, 7 Mar 2016 18:40:09 +0100
Subject: [PATCH] Fix Wire register
Move TWBR register from Wire.cpp to twi.c file.
---
hardware/arduino/avr/libraries/Wire/src/Wire.cpp | 4 ++--
.../arduino/avr/libraries/Wire/src/utility/twi.c | 16 ++++++++++++++++
.../arduino/avr/libraries/Wire/src/utility/twi.h | 1 +
3 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/hardware/arduino/avr/libraries/Wire/src/Wire.cpp b/hardware/arduino/avr/libraries/Wire/src/Wire.cpp
index 86696e93f..d2146f70f 100644
--- a/hardware/arduino/avr/libraries/Wire/src/Wire.cpp
+++ b/hardware/arduino/avr/libraries/Wire/src/Wire.cpp
@@ -80,9 +80,9 @@ void TwoWire::end(void)
twi_disable();
}
-void TwoWire::setClock(uint32_t frequency)
+void TwoWire::setClock(uint32_t clock)
{
- TWBR = ((F_CPU / frequency) - 16) / 2;
+ twi_setFrequency(clock);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
diff --git a/hardware/arduino/avr/libraries/Wire/src/utility/twi.c b/hardware/arduino/avr/libraries/Wire/src/utility/twi.c
index 7efcc5a47..f5d7d5b05 100644
--- a/hardware/arduino/avr/libraries/Wire/src/utility/twi.c
+++ b/hardware/arduino/avr/libraries/Wire/src/utility/twi.c
@@ -118,6 +118,22 @@ 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;
+
+ /* 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 */
+}
+
/*
* Function twi_readFrom
* Desc attempts to become twi bus master and read a
diff --git a/hardware/arduino/avr/libraries/Wire/src/utility/twi.h b/hardware/arduino/avr/libraries/Wire/src/utility/twi.h
index 4c52bc5a0..d27325ea7 100644
--- a/hardware/arduino/avr/libraries/Wire/src/utility/twi.h
+++ b/hardware/arduino/avr/libraries/Wire/src/utility/twi.h
@@ -41,6 +41,7 @@
void twi_init(void);
void twi_disable(void);
void twi_setAddress(uint8_t);
+ void twi_setFrequency(uint32_t);
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t);
uint8_t twi_transmit(const uint8_t*, uint8_t);