Browse Source

ROM commands, read/write bytes (wip)

timer1
Youen Toupin 9 years ago
parent
commit
b5fa47249d
  1. 104
      OneWireSlave.cpp
  2. 22
      OneWireSlave.h

104
OneWireSlave.cpp

@ -16,7 +16,7 @@ namespace
const unsigned long PresenceWaitDuration = 30;
const unsigned long PresenceDuration = 300;
const unsigned long ReadBitSamplingTime = 30;
const unsigned long ReadBitSamplingTime = 25;
const unsigned long SendBitDuration = 35;
@ -28,6 +28,7 @@ namespace
OneWireSlave OneWire;
byte OneWireSlave::rom_[8];
byte OneWireSlave::scratchpad_[8];
Pin OneWireSlave::pin_;
byte OneWireSlave::tccr1bEnable_;
@ -45,6 +46,10 @@ byte OneWireSlave::searchRomBytePos_;
byte OneWireSlave::searchRomBitPos_;
bool OneWireSlave::searchRomInverse_;
byte* OneWireSlave::receiveBytesBuffer_;
short OneWireSlave::receiveBytesLength_;
void(*OneWireSlave::receiveBytesCallback_)(bool error);
ISR(TIMER1_COMPA_vect) // timer1 interrupt
{
TCCR1B = 0; // disable clock
@ -364,7 +369,8 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
if (error)
{
error_("Invalid bit");
beginWaitReset_();
if (receiveTarget_ >= 0)
receiveBytesCallback_(true);
return;
}
@ -378,23 +384,39 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
#endif
if (receiveTarget_ == ReceiveCommand)
{
if (receivingByte_ == 0xF0)
receiveTarget_ = 0;
switch (receivingByte_)
{
case 0xF0: // SEARCH ROM
beginSearchRom_();
return;
}
else
{
// TODO: send command to client code
beginWaitReset_();
case 0x33: // READ ROM
beginWriteBytes_(rom_, 8, &OneWireSlave::noOpCallback_);
return;
case 0x55: // MATCH ROM
beginReceiveBytes_(scratchpad_, 8, &OneWireSlave::matchRomBytesReceived_);
return;
case 0xCC: // SKIP ROM
// emulate a match rom operation
memcpy(scratchpad_, rom_, 8);
matchRomBytesReceived_(false);
return;
default:
error_("Unknown command");
return;
}
}
else
{
// TODO: add byte in receive buffer
beginWaitReset_();
return;
receiveBytesBuffer_[receiveTarget_++] = receivingByte_;
receivingByte_ = 0;
receivingBitPos_ = 0;
if (receiveTarget_ == receiveBytesLength_)
{
beginWaitReset_();
receiveBytesCallback_(false);
return;
}
}
}
@ -424,7 +446,6 @@ void OneWireSlave::continueSearchRom_(bool error)
if (error)
{
error_("Failed to send bit");
beginWaitReset_();
return;
}
@ -444,7 +465,6 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
if (error)
{
error_("Bit read error during ROM search");
beginWaitReset_();
return;
}
@ -480,3 +500,61 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
beginWaitReset_();
}
}
void OneWireSlave::beginWriteBytes_(byte* data, short numBytes, void(*complete)(bool error))
{
// TODO
beginWaitReset_();
}
void OneWireSlave::beginReceiveBytes_(byte* buffer, short numBytes, void(*complete)(bool error))
{
receiveBytesBuffer_ = buffer;
receiveBytesLength_ = numBytes;
receiveTarget_ = 0;
receiveBytesCallback_ = complete;
beginReceive_();
}
void OneWireSlave::noOpCallback_(bool error)
{
if (error)
error_("error during an internal 1-wire operation");
}
void OneWireSlave::matchRomBytesReceived_(bool error)
{
if (error)
{
error_("error receiving match rom bytes");
return;
}
if (memcmp(rom_, scratchpad_, 8) == 0)
{
#ifdef DEBUG_LOG
debug.SC_APPEND_STR("ROM matched");
#endif
beginReceiveBytes_(scratchpad_, 1, &OneWireSlave::notifyClientByteReceived_);
}
else
{
#ifdef DEBUG_LOG
debug.SC_APPEND_STR("ROM not matched");
#endif
beginWaitReset_();
}
}
void OneWireSlave::notifyClientByteReceived_(bool error)
{
if (error)
{
error_("error receiving device specific bytes");
return;
}
beginWaitReset_();
// TODO
}

22
OneWireSlave.h

@ -7,19 +7,19 @@
class OneWireSlave
{
public:
///! Starts listening for the 1-wire master, on the specified pin, as a virtual slave device identified by the specified ROM (7 bytes, starting from the family code, CRC will be computed internally). Reset, Presence, SearchRom and MatchRom are handled automatically. The library will use the external interrupt on the specified pin (note that this is usually not possible with all pins, depending on the board), as well as one hardware timer. Blocking interrupts (either by disabling them explicitely with sei/cli, or by spending time in another interrupt) can lead to malfunction of the library, due to tight timing for some 1-wire operations.
//! Starts listening for the 1-wire master, on the specified pin, as a virtual slave device identified by the specified ROM (7 bytes, starting from the family code, CRC will be computed internally). Reset, Presence, SearchRom and MatchRom are handled automatically. The library will use the external interrupt on the specified pin (note that this is usually not possible with all pins, depending on the board), as well as one hardware timer. Blocking interrupts (either by disabling them explicitely with sei/cli, or by spending time in another interrupt) can lead to malfunction of the library, due to tight timing for some 1-wire operations.
void begin(byte* rom, byte pinNumber);
///! Stops all 1-wire activities, which frees hardware resources for other purposes.
//! Stops all 1-wire activities, which frees hardware resources for other purposes.
void end();
///! Pops one byte from the receive buffer, or returns false if no byte has been received.
//! Pops one byte from the receive buffer, or returns false if no byte has been received.
bool read(byte& b);
///! Sets (or replaces) a function to be called when at least one byte has been received. Callbacks are executed from interrupts and should be as short as possible.
//! Sets (or replaces) a function to be called when at least one byte has been received. Callbacks are executed from interrupts and should be as short as possible.
void setReceiveCallback(void(*callback)());
///! Enqueues the specified bytes in the send buffer. They will be sent in the background. The optional callback is used to notify when the bytes are sent, or if an error occured. Callbacks are executed from interrupts and should be as short as possible.
//! Enqueues the specified bytes in the send buffer. They will be sent in the background. The optional callback is used to notify when the bytes are sent, or if an error occured. Callbacks are executed from interrupts and should be as short as possible.
void write(byte* bytes, short numBytes, void(*complete)(bool error));
private:
@ -52,6 +52,13 @@ private:
static void continueSearchRom_(bool error);
static void searchRomOnBitReceived_(bool bit, bool error);
static void beginWriteBytes_(byte* data, short numBytes, void(*complete)(bool error));
static void beginReceiveBytes_(byte* buffer, short numBytes, void(*complete)(bool error));
static void noOpCallback_(bool error);
static void matchRomBytesReceived_(bool error);
static void notifyClientByteReceived_(bool error);
// interrupt handlers
static void waitReset_();
static void beginPresence_();
@ -65,6 +72,7 @@ private:
private:
static byte rom_[8];
static byte scratchpad_[8];
static Pin pin_;
static byte tccr1bEnable_;
@ -81,6 +89,10 @@ private:
static byte searchRomBytePos_;
static byte searchRomBitPos_;
static bool searchRomInverse_;
static byte* receiveBytesBuffer_;
static short receiveBytesLength_;
static void(*receiveBytesCallback_)(bool error);
};
extern OneWireSlave OneWire;

Loading…
Cancel
Save