Commit 04b86b80 authored by Jonathan Karras's avatar Jonathan Karras Committed by Marcel Stör
Browse files

Fix open drain level issues when _not_ using parasitic power mode. (#1291)

Port of @pstolarz work on PaulStoffregen/OneWire#8
parent e14dd7ab
...@@ -107,7 +107,6 @@ uint8_t onewire_reset(uint8_t pin) ...@@ -107,7 +107,6 @@ uint8_t onewire_reset(uint8_t pin)
noInterrupts(); noInterrupts();
DIRECT_WRITE_LOW(pin); DIRECT_WRITE_LOW(pin);
DIRECT_MODE_OUTPUT(pin); // drive output low
interrupts(); interrupts();
delayMicroseconds(480); delayMicroseconds(480);
noInterrupts(); noInterrupts();
...@@ -126,19 +125,12 @@ uint8_t onewire_reset(uint8_t pin) ...@@ -126,19 +125,12 @@ uint8_t onewire_reset(uint8_t pin)
static void onewire_write_bit(uint8_t pin, uint8_t v) static void onewire_write_bit(uint8_t pin, uint8_t v)
{ {
if (v & 1) { if (v & 1) {
noInterrupts(); onewire_read_bit(pin);
DIRECT_WRITE_LOW(pin);
DIRECT_MODE_OUTPUT(pin); // drive output low
delayMicroseconds(10);
DIRECT_WRITE_HIGH(pin); // drive output high
interrupts();
delayMicroseconds(55);
} else { } else {
noInterrupts(); noInterrupts();
DIRECT_WRITE_LOW(pin); DIRECT_WRITE_LOW(pin);
DIRECT_MODE_OUTPUT(pin); // drive output low
delayMicroseconds(65); delayMicroseconds(65);
DIRECT_WRITE_HIGH(pin); // drive output high DIRECT_MODE_INPUT(pin); // drive output high by the pull-up
interrupts(); interrupts();
delayMicroseconds(5); delayMicroseconds(5);
} }
...@@ -153,14 +145,14 @@ static uint8_t onewire_read_bit(uint8_t pin) ...@@ -153,14 +145,14 @@ static uint8_t onewire_read_bit(uint8_t pin)
uint8_t r; uint8_t r;
noInterrupts(); noInterrupts();
DIRECT_MODE_OUTPUT(pin);
DIRECT_WRITE_LOW(pin); DIRECT_WRITE_LOW(pin);
delayMicroseconds(3);
delayMicroseconds(5);
DIRECT_MODE_INPUT(pin); // let pin float, pull up will raise DIRECT_MODE_INPUT(pin); // let pin float, pull up will raise
delayMicroseconds(10); delayMicroseconds(8);
r = DIRECT_READ(pin); r = DIRECT_READ(pin);
interrupts(); interrupts();
delayMicroseconds(53); delayMicroseconds(52);
return r; return r;
} }
...@@ -177,10 +169,10 @@ void onewire_write(uint8_t pin, uint8_t v, uint8_t power /* = 0 */) { ...@@ -177,10 +169,10 @@ void onewire_write(uint8_t pin, uint8_t v, uint8_t power /* = 0 */) {
for (bitMask = 0x01; bitMask; bitMask <<= 1) { for (bitMask = 0x01; bitMask; bitMask <<= 1) {
onewire_write_bit(pin, (bitMask & v)?1:0); onewire_write_bit(pin, (bitMask & v)?1:0);
} }
if ( !power) { if ( power ) {
noInterrupts(); noInterrupts();
DIRECT_MODE_INPUT(pin); DIRECT_WRITE_HIGH(pin);
DIRECT_WRITE_LOW(pin);
interrupts(); interrupts();
} }
} }
...@@ -189,10 +181,10 @@ void onewire_write_bytes(uint8_t pin, const uint8_t *buf, uint16_t count, bool p ...@@ -189,10 +181,10 @@ void onewire_write_bytes(uint8_t pin, const uint8_t *buf, uint16_t count, bool p
uint16_t i; uint16_t i;
for (i = 0 ; i < count ; i++) for (i = 0 ; i < count ; i++)
onewire_write(pin, buf[i], owDefaultPower); onewire_write(pin, buf[i], owDefaultPower);
if (!power) { if ( power ) {
noInterrupts(); noInterrupts();
DIRECT_MODE_INPUT(pin); DIRECT_WRITE_HIGH(pin);
DIRECT_WRITE_LOW(pin);
interrupts(); interrupts();
} }
} }
...@@ -474,7 +466,7 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len) ...@@ -474,7 +466,7 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len)
uint8_t onewire_crc8(const uint8_t *addr, uint8_t len) uint8_t onewire_crc8(const uint8_t *addr, uint8_t len)
{ {
uint8_t crc = 0; uint8_t crc = 0;
while (len--) { while (len--) {
uint8_t inbyte = *addr++; uint8_t inbyte = *addr++;
uint8_t i; uint8_t i;
...@@ -501,8 +493,8 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len) ...@@ -501,8 +493,8 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len)
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
// if (!CheckCRC16(buf, 11, &buf[11])) { // if (!CheckCRC16(buf, 11, &buf[11])) {
// // Handle error. // // Handle error.
// } // }
// //
// @param input - Array of bytes to checksum. // @param input - Array of bytes to checksum.
// @param len - How many bytes to use. // @param len - How many bytes to use.
// @param inverted_crc - The two CRC16 bytes in the received data. // @param inverted_crc - The two CRC16 bytes in the received data.
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
// Platform specific I/O definitions // Platform specific I/O definitions
#define DIRECT_READ(pin) (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin]))) #define DIRECT_READ(pin) (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin])))
#define DIRECT_MODE_INPUT(pin) GPIO_DIS_OUTPUT(pin_num[pin]) #define DIRECT_MODE_INPUT(pin) GPIO_DIS_OUTPUT(GPIO_ID_PIN(pin_num[pin]))
#define DIRECT_MODE_OUTPUT(pin) #define DIRECT_MODE_OUTPUT(pin)
#define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0)) #define DIRECT_WRITE_LOW(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0))
#define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1)) #define DIRECT_WRITE_HIGH(pin) (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1))
...@@ -74,10 +74,10 @@ void onewire_read_bytes(uint8_t pin, uint8_t *buf, uint16_t count); ...@@ -74,10 +74,10 @@ void onewire_read_bytes(uint8_t pin, uint8_t *buf, uint16_t count);
// Write a bit. The bus is always left powered at the end, see // Write a bit. The bus is always left powered at the end, see
// note in write() about that. // note in write() about that.
// void onewire_write_bit(uint8_t pin, uint8_t v); static void onewire_write_bit(uint8_t pin, uint8_t v);
// Read a bit. // Read a bit.
// uint8_t onewire_read_bit(uint8_t pin); static uint8_t onewire_read_bit(uint8_t pin);
// Stop forcing power onto the bus. You only need to do this if // Stop forcing power onto the bus. You only need to do this if
// you used the 'power' flag to write() or used a write_bit() call // you used the 'power' flag to write() or used a write_bit() call
...@@ -120,8 +120,8 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len); ...@@ -120,8 +120,8 @@ uint8_t onewire_crc8(const uint8_t *addr, uint8_t len);
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16 // ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
// if (!CheckCRC16(buf, 11, &buf[11])) { // if (!CheckCRC16(buf, 11, &buf[11])) {
// // Handle error. // // Handle error.
// } // }
// //
// @param input - Array of bytes to checksum. // @param input - Array of bytes to checksum.
// @param len - How many bytes to use. // @param len - How many bytes to use.
// @param inverted_crc - The two CRC16 bytes in the received data. // @param inverted_crc - The two CRC16 bytes in the received data.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment