Hi please I'm trying to reverse engineer and understand how onewire library implementation works on arduino as i'm looking forward to make a UART onewire implementation. it's been really tough almost all night trying to catch up with definitions on header files from one place to another.. so I'm asking for help if someone can please help me out understand what do these definitions mean
Onewire.h code.
//[b] Platform specific I/O definitions
#if defined(__AVR__)
#define PIN_TO_BASEREG(pin) (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin)) <=== this is the only one I understand sets the bitmask
from a specific pin from pins_arduino.h
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM asm("r30")
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
[/b]
I'm trying to understand how they work together with onewire.cpp, for instance with this piece of code at least help me understand the reset from all line initialization steps from onewire
OneWire::OneWire(uint8_t pin)
{
pinMode(pin, INPUT);
bitmask = PIN_TO_BITMASK(pin); <<<=== I know this brings up the bitmask corresponding to the arduino pin
for instance digital pin 13 brings 0b00100000. [/b]
baseReg = PIN_TO_BASEREG(pin); <<== I don't understand what this brings up to basereg
#if ONEWIRE_SEARCH
reset_search();
#endif
}
// Perform the onewire reset function. We will wait up to 250uS for
// the bus to come high, if it doesn't then it is broken or shorted
// and we return a 0;
//
// Returns 1 if a device asserted a presence pulse, 0 otherwise.
//
uint8_t OneWire::reset(void)
{
IO_REG_TYPE mask = bitmask; <<==bitmask if digital pin 13 is set here
IO_REG_TYPE is 0b00100000 ? right?
volatile IO_REG_TYPE *reg IO_REG_ASM = baseReg; <<== Lost here what does *reg mean
uint8_t r;
uint8_t retries = 125;
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); <==== lost here as well about DIRECT_MODE_INPUT and reg mask
interrupts();
// wait until the wire is high... just in case <== I know it's trying to set up the line low for a time slot of 480us but
hardly understand the syntax.
do {
if (--retries == 0) return 0;
delayMicroseconds(2);
} while ( !DIRECT_READ(reg, mask));
noInterrupts();
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
interrupts();
delayMicroseconds(480); <<=== this is setting line low to 480us right?
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
delayMicroseconds(70); <==== 70us for presence ?
r = !DIRECT_READ(reg, mask);
interrupts();
delayMicroseconds(410);
return r;
}