Browse Source

ROM commands, read/write bytes (wip)

pull/5/head
Youen Toupin 10 years ago
parent
commit
b5fa47249d
  1. 100
      OneWireSlave.cpp
  2. 22
      OneWireSlave.h

100
OneWireSlave.cpp

@ -16,7 +16,7 @@ namespace
const unsigned long PresenceWaitDuration = 30; const unsigned long PresenceWaitDuration = 30;
const unsigned long PresenceDuration = 300; const unsigned long PresenceDuration = 300;
const unsigned long ReadBitSamplingTime = 30; const unsigned long ReadBitSamplingTime = 25;
const unsigned long SendBitDuration = 35; const unsigned long SendBitDuration = 35;
@ -28,6 +28,7 @@ namespace
OneWireSlave OneWire; OneWireSlave OneWire;
byte OneWireSlave::rom_[8]; byte OneWireSlave::rom_[8];
byte OneWireSlave::scratchpad_[8];
Pin OneWireSlave::pin_; Pin OneWireSlave::pin_;
byte OneWireSlave::tccr1bEnable_; byte OneWireSlave::tccr1bEnable_;
@ -45,6 +46,10 @@ byte OneWireSlave::searchRomBytePos_;
byte OneWireSlave::searchRomBitPos_; byte OneWireSlave::searchRomBitPos_;
bool OneWireSlave::searchRomInverse_; bool OneWireSlave::searchRomInverse_;
byte* OneWireSlave::receiveBytesBuffer_;
short OneWireSlave::receiveBytesLength_;
void(*OneWireSlave::receiveBytesCallback_)(bool error);
ISR(TIMER1_COMPA_vect) // timer1 interrupt ISR(TIMER1_COMPA_vect) // timer1 interrupt
{ {
TCCR1B = 0; // disable clock TCCR1B = 0; // disable clock
@ -364,7 +369,8 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
if (error) if (error)
{ {
error_("Invalid bit"); error_("Invalid bit");
beginWaitReset_(); if (receiveTarget_ >= 0)
receiveBytesCallback_(true);
return; return;
} }
@ -378,25 +384,41 @@ void OneWireSlave::onBitReceived_(bool bit, bool error)
#endif #endif
if (receiveTarget_ == ReceiveCommand) if (receiveTarget_ == ReceiveCommand)
{ {
if (receivingByte_ == 0xF0) receiveTarget_ = 0;
switch (receivingByte_)
{ {
case 0xF0: // SEARCH ROM
beginSearchRom_(); beginSearchRom_();
return; return;
} case 0x33: // READ ROM
else beginWriteBytes_(rom_, 8, &OneWireSlave::noOpCallback_);
{ return;
// TODO: send command to client code case 0x55: // MATCH ROM
beginWaitReset_(); 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; return;
} }
} }
else else
{ {
// TODO: add byte in receive buffer receiveBytesBuffer_[receiveTarget_++] = receivingByte_;
receivingByte_ = 0;
receivingBitPos_ = 0;
if (receiveTarget_ == receiveBytesLength_)
{
beginWaitReset_(); beginWaitReset_();
receiveBytesCallback_(false);
return; return;
} }
} }
}
beginReceiveBit_(&OneWireSlave::onBitReceived_); beginReceiveBit_(&OneWireSlave::onBitReceived_);
} }
@ -424,7 +446,6 @@ void OneWireSlave::continueSearchRom_(bool error)
if (error) if (error)
{ {
error_("Failed to send bit"); error_("Failed to send bit");
beginWaitReset_();
return; return;
} }
@ -444,7 +465,6 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
if (error) if (error)
{ {
error_("Bit read error during ROM search"); error_("Bit read error during ROM search");
beginWaitReset_();
return; return;
} }
@ -480,3 +500,61 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
beginWaitReset_(); 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 class OneWireSlave
{ {
public: 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); 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(); 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); 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)()); 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)); void write(byte* bytes, short numBytes, void(*complete)(bool error));
private: private:
@ -52,6 +52,13 @@ private:
static void continueSearchRom_(bool error); static void continueSearchRom_(bool error);
static void searchRomOnBitReceived_(bool bit, 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 // interrupt handlers
static void waitReset_(); static void waitReset_();
static void beginPresence_(); static void beginPresence_();
@ -65,6 +72,7 @@ private:
private: private:
static byte rom_[8]; static byte rom_[8];
static byte scratchpad_[8];
static Pin pin_; static Pin pin_;
static byte tccr1bEnable_; static byte tccr1bEnable_;
@ -81,6 +89,10 @@ private:
static byte searchRomBytePos_; static byte searchRomBytePos_;
static byte searchRomBitPos_; static byte searchRomBitPos_;
static bool searchRomInverse_; static bool searchRomInverse_;
static byte* receiveBytesBuffer_;
static short receiveBytesLength_;
static void(*receiveBytesCallback_)(bool error);
}; };
extern OneWireSlave OneWire; extern OneWireSlave OneWire;

Loading…
Cancel
Save