Author Topic: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION  (Read 37038 times)

Robert

  • Jr. Member
  • **
  • Posts: 66
  • Country: be
Re: Moteino & W5100 ethernet SPI support
« Reply #75 on: October 17, 2015, 12:20:45 PM »
Tom,

As a matter of fact I have to do it, and I am using a brand new copy of the Arduino IDE 1.6.5-r5.
I also did try the W5100.h Kiwi version with the SPI version of Paul Stoffregen (see https://github.com/PaulStoffregen/SPI), without success.

But I don't want to continue to argue, this work fine for me.

Robert





TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Moteino & W5100 ethernet SPI support
« Reply #76 on: October 17, 2015, 04:16:27 PM »
Tom,

As a matter of fact I have to do it, and I am using a brand new copy of the Arduino IDE 1.6.5-r5.
I also did try the W5100.h Kiwi version with the SPI version of Paul Stoffregen (see https://github.com/PaulStoffregen/SPI), without success.
Yes, of course you do.  I forgot that the RFM69 library hasn't implemented SPI transactions!  I have this implemented in my RFM69_ATC wrapper, which is why I don't have the problem.  Kiwi's change to W5100 was to make the SS pin variable, but he also implemented SPI transactions in his version of the RFM69 library to get it to work.
Quote
But I don't want to continue to argue, this work fine for me.
No argument, just trying to understand what the problem was and you are exactly right, disabling the RFM69 interrupt during Ethernet SPI access (or any other SPI access for that matter) is necessary.

Tom

Robert

  • Jr. Member
  • **
  • Posts: 66
  • Country: be
Re: Moteino & W5100 ethernet SPI support
« Reply #77 on: October 18, 2015, 03:52:17 AM »
Tom,

It is becoming very difficult to follow the RFM69 library variants.
I had a look at the RFM library of Kiwi
   https://github.com/kiwisincebirth/RFM69/blob/master/RFM69.cpp
which looks to integrate the change request :
  https://github.com/LowPowerLab/RFM69/pull/25/files#diff-2d1040d890d26d4ac6877f9269b3e32fR36
This is probably a fix for my issue (didn't try it yet)

However looking at the new releases of the RFM library of Felix, I don't see these changes, however your 'vitalisation' is now included.

Any ideas why the  change above was not implemented, it would greatly improve the management of all the RFM versions and not to patch standard code.

Robert

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Moteino & W5100 ethernet SPI support
« Reply #78 on: October 18, 2015, 09:27:57 AM »
Tom,

It is becoming very difficult to follow the RFM69 library variants.
I had a look at the RFM library of Kiwi
   https://github.com/kiwisincebirth/RFM69/blob/master/RFM69.cpp
which looks to integrate the change request :
  https://github.com/LowPowerLab/RFM69/pull/25/files#diff-2d1040d890d26d4ac6877f9269b3e32fR36
This is probably a fix for my issue (didn't try it yet)
Unfortunately, Kiwi's changed RFM69 might be too dated at this point to include directly.
Quote
However looking at the new releases of the RFM library of Felix, I don't see these changes, however your 'vitalisation' is now included.

Any ideas why the  change above was not implemented, it would greatly improve the management of all the RFM versions and not to patch standard code.
At the time these changes were made, I think Felix thought they weren't sufficiently tested enough and required a new (at the time) version of Arduino IDE (1.6.0).  Also, I was trying to incorporate my ATC changes into the base library.   There were also proposals being made to add enhanced security features to RFM69 and the sense was that there wasn't a single combination of these requested changes that everyone wanted.

To deal with this dilemma I proposed virtualizing the RFM69 methods so that derived classes could add features without requiring a change to the base class.  This is how the RFM69_ATC library was born. 

As you point out, the virtualization change has been made to the base class, and I'm working with Felix to resolve the ATC changes.  Frankly I had forgotten about the SPI transaction changes until you mentioned your difficulty.  I do think these changes SHOULD go into the base library and I'll work with Felix to incorporate them into the base library.  I can make the changes to a fork of the RFM69 library and post them on Github if you'd be willing to test it.  I'm sure Felix would appreciate extra help with the testing.

Tom

Robert

  • Jr. Member
  • **
  • Posts: 66
  • Country: be
Re: Moteino & W5100 ethernet SPI support
« Reply #79 on: October 18, 2015, 10:57:59 AM »
Tom,

Ok thanks to your reply, looking forward to see a new RFM69 version including all these changes, for the time being I can live with my W5100 patch.

I didn't mentioned that I am not actually using the  RFM69 library but the RFM69_SessionKey one, which also uses your visualisation, it would be nice to have a smooth way to included it the RFM69 library.

Also I have no problem and time   :) to test  any new RFM version within my limited test environment.

Notes:
Even if the RFM69_SessionKey is generating a little overhead, security (anti-replay) is for me of the utmost importance, so I see it as a nice feature of the standard RFM69 library .
(I have some comments about the RFM69_SessionKey, that I will keep for a future discussion).

Br
Robert

Robert

  • Jr. Member
  • **
  • Posts: 66
  • Country: be
Re: Moteino & W5100 ethernet SPI support
« Reply #80 on: October 23, 2015, 02:01:56 PM »
Tom,
I have modified the standard and current library with the kiwi patches (covering the Ethernet / RFM interrupts issue).
Because I am using  the RFM69_SessionKey Library I had also to modify:
1. the RFM69.h - CtlByte definition with the one used by the SessionKey library.
2. the RFM69_SessionKey.cpp library to take into account the the interrupt issue on the "receiveDone ()" function
Details can be found on: https://github.com/rrobinet/RFM69_Libary.
Modifications are surrounded by excavation points (!!!)
Hoping this may help others.
Robert

FYI
This library was tested  with an Ethernet Shield plugged on an ATMEG2560 (a gateway) together with a RFM69WH and  transmitting in RF  to a Moteino R4 Node (RFM69W)
 - The gateway connects via Ethernet to a Raspberry  server (OpenHab + MQTT broker) to publish and subscribe  Node data
 - In data mode, I use modified version of the RFM69_SessionKey (Challenge extended from a random Byte to  the system time (long integer))
 - In upgrade mode, the  Raspberry server talks to the gateway with a variant of the wirelessProgarmming (Serial connection is replaced by a TCP/IP one)  to upgrade the remote node software.

With the current library I was able to:
- run the normal data mode between Server (MQTT Broker + OpenHab Interface) - RFM gateway - RFM Mote,  without problem during several days.
- start OTA upgrade from the Server across the gateway to the remote node without problem.
 




TD22057

  • NewMember
  • *
  • Posts: 26
  • Country: us
Re: Moteino & W5100 ethernet SPI support
« Reply #81 on: February 11, 2016, 10:57:36 PM »
Hey Robert (or Kiwi),
I was wondering if you could help me understand some of the code in your transaction based RFM69 class. 

In select() (lines 461-462), it's saving SPCR and SPSR (SPI status register) and then restoring them in unselect() (lines 489-490) but I thought that the point of the SPI transaction system was that beginTransaction()/endTransaction() takes care of this.   Is this in case some other library isn't using transactions or am I confused (which I assume is the answer)?

The biggest thing I don't understand is the interrupt handling in receiveDone() (lines 380-407).  The comment by noInterrupts() (line 384) says that the interrupts will get re-enabled in unselect() but the call to interrupts() in unselect() is commented out in the transaction based code.  The original line in Kiwi's code has a comment about why SREG is manipulated but I didn't really understand it at all.  So I was wondering if you (or anyone) could explain why receiveDone() is saving/restoring SREG which wasn't necessary before and why it calls noInterrupts() but never calls interrupts()?  Every other call to interrupts()/noInterrupts() is no longer needed w/ the transaction code so it was a little confusing to me to see noInterrupts() still there on line 384.

Thanks,
TD


TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Moteino & W5100 ethernet SPI support
« Reply #82 on: February 12, 2016, 07:54:26 AM »
Hey Robert (or Kiwi),
I was wondering if you could help me understand some of the code in your transaction based RFM69 class. 

In select() (lines 461-462), it's saving SPCR and SPSR (SPI status register) and then restoring them in unselect() (lines 489-490) but I thought that the point of the SPI transaction system was that beginTransaction()/endTransaction() takes care of this.   Is this in case some other library isn't using transactions or am I confused (which I assume is the answer)?

The biggest thing I don't understand is the interrupt handling in receiveDone() (lines 380-407).  The comment by noInterrupts() (line 384) says that the interrupts will get re-enabled in unselect() but the call to interrupts() in unselect() is commented out in the transaction based code.  The original line in Kiwi's code has a comment about why SREG is manipulated but I didn't really understand it at all.  So I was wondering if you (or anyone) could explain why receiveDone() is saving/restoring SREG which wasn't necessary before and why it calls noInterrupts() but never calls interrupts()?  Every other call to interrupts()/noInterrupts() is no longer needed w/ the transaction code so it was a little confusing to me to see noInterrupts() still there on line 384.

Thanks,
TD
TD, your confusion is justified.  There were several iterations of changes to RFM69/W5100 code during the SPI Transaction conversion.  Without going into detail (I probably have forgotten most of it anyway), the RFM69 library from LowPowerLab did not implement the SPI transaction code.   I ended up putting  SPI Transaction support in the RFM69_ATC library as an extension to the base RFM69 library as overrides to the base code.

Here is a snippet from the RFM69_ATC library:
Code: [Select]
//=============================================================================
// select() - replaces base class with one supporting spiTransactions
//=============================================================================
// select the transceiver
void RFM69_ATC::select() {
  // save current SPI settings
  _SPCR = SPCR;
  _SPSR = SPSR;
  _SREG = SREG;
#ifdef SPI_HAS_TRANSACTION
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
#else
  noInterrupts();
  // set RFM69 SPI settings
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV4); // decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
#endif
  digitalWrite(_slaveSelectPin, LOW);
}


//=============================================================================
// unselect() - replaces base class with one supporting spiTransactions
//=============================================================================
// UNselect the transceiver chip
void RFM69_ATC::unselect() {
  digitalWrite(_slaveSelectPin, HIGH);
#ifdef SPI_HAS_TRANSACTION
  SPI.endTransaction();
#else 
  SREG = _SREG;
#endif
  // restore SPI settings to what they were before talking to RFM69
  SPCR = _SPCR;
  SPSR = _SPSR;
}

Tom
« Last Edit: February 29, 2016, 05:28:53 PM by Felix »

TD22057

  • NewMember
  • *
  • Posts: 26
  • Country: us
Re: Moteino & W5100 ethernet SPI support
« Reply #83 on: February 12, 2016, 10:26:14 AM »
TomWS,
Thanks for the reply.  But - the ATC library at LowPowerLab and in your own github repository doesn't contain any transaction code.  Can you update your github repository with the latest version?

And - in your example, could you comment on why the transaction code needs to save and restore SPCR, SPSR, and SREG?  My understanding (probably wrong) is that if all the SPI devices are using transactions, saving and restoring these registers shouldn't be necessary.

Thanks,
TD

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Moteino & W5100 ethernet SPI support
« Reply #84 on: February 12, 2016, 12:16:31 PM »
TomWS,
Thanks for the reply.  But - the ATC library at LowPowerLab and in your own github repository doesn't contain any transaction code.  Can you update your github repository with the latest version?

And - in your example, could you comment on why the transaction code needs to save and restore SPCR, SPSR, and SREG?  My understanding (probably wrong) is that if all the SPI devices are using transactions, saving and restoring these registers shouldn't be necessary.

Thanks,
TD
Curious... My own version of the ATC library has the code I posted a little while ago.  I'll have to take a look at the various versions, but I can't do it today.  Maybe this weekend.

Tom

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION
« Reply #85 on: February 29, 2016, 08:53:01 PM »
I would like to jumpstart this thread back to life in an effort to try to consolidate the SPI transaction support into the master RFM69 library.
To try to clarify some confusion - when I added Tom's ATC changes, it was an entirely manual change by myself, without a proper merge. I had to do that since Tom's pull included other things I was not ready to integrate at that time, including any SPI transaction support.

Now ... at this point in time I would like to add SPI transaction support, based on kiwisincebirth's pull request and also select changes from Tom's SPI transaction branch. Sorry guys, I know it's been 1 year since this stuff was pushed but I am just that slow these days.

Since these pulls and branch are too different than the current master, I had to do a manual merge of these into a new branch of my own: https://github.com/LowPowerLab/RFM69/tree/SPI_TRANSACTION-attempt
Consider this a SPI transactions branch only.
I would like to ask TomWS and kiwisincebirth (and others that wish to review/test) to please take a look at this new branch. Please let me know what you think and if I am missing something. I will try to test this but first I'd like to get a first pass of your review. One thing I still don't fully understand, and it was asked by TD22057 also, is why there's still a need to save/restore SPCR and SPSR in addition to the SPI transaction calls.

geco82

  • NewMember
  • *
  • Posts: 1
  • Country: it
Re: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION
« Reply #86 on: August 17, 2016, 04:32:10 AM »
Hi Felix,
did you managed to test the SPI support?

I'm new to moteino and I have also experienced some silly errors so I wouldn't like to break my settings...

Thanks

Giacomo

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION
« Reply #87 on: August 17, 2016, 08:00:39 AM »
Hi Felix,
did you managed to test the SPI support?
Hi Giacomo,
Unfortunately not. There has been no activity on this SPI-transaction-attempt branch linked above and I don't really use any other SPI devices on the Moteinos (like the W5100) so I don't see these issues. The master branch has since been updated with various patches so this is now behind but can probably merge the master commits.

vysocan

  • NewMember
  • *
  • Posts: 1
  • Country: cz
Re: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION
« Reply #88 on: September 20, 2016, 01:42:52 PM »
Hello Felix,

I will be happy to test SPI transaction with W5500, which I heavily use. In fact I'm using your SPI transaction attempt already, but it hangs after while. I did not give it more thoughts, as I had some hardware problem with W5500, but now I can come back to it. Using cli(); sei(); keep my code running, but I'm loosing interrupts.

For W5*00 I use official WizNet library which I think is fully equipped with SPI transaction support.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Moteino & W5100 ethernet SPI support / SPI_HAS_TRANSACTION
« Reply #89 on: September 20, 2016, 02:53:31 PM »
@vysocan,
Thanks for the offer, I am not familiar with the new W5500, only a little with the older W5100, and I don't have any of the hardware available. Either way the story is similar to any SPI device. If you find the branch can be improved you can submit pull requests with your patches, and I will try my best to assist and test.
Thanks again.