Go Down

Topic: ATMEGA 644 won´t work with WIZNET5100 WIZ812MJ (Read 4 times) previous topic - next topic

WanaGo

Did you have any luck with this in the end?

I have just received the prototype boards back from the printer and have populated them, and am now just attempting to get some test code working for the Wiznet module.

I dont have the exact computer setup I had when I did my first test, so I have started again with IDE 22.
I have downloaded the arduino-extras zip file that Mark S produced on his website (www.avr-developers.com) as that has the core files for the 644P.
I compared them against the Sanguino files and there are decrepencies. I couldnt even get the Sanguino core files to compile the Ethernet library.

I then looked in the pins_arduino.h (and .c) files, and found there was no reference to the MISO, MOSI, CSK, SS for the 644P configuration, as it looks like the SPI library uses these variable declarations.

here are some examples:

In the SPI library, it has the following code:

Code: [Select]
void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);
 
  digitalWrite(SCK, LOW);
  digitalWrite(MOSI, LOW);
  digitalWrite(SS, HIGH);


It refers to the SCK, MOSI and SS variables.
I then looked in the pins_arduino files in the modified core files from Mark S, and found this entry in the pins_arduino.h file:

Code: [Select]
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
const static uint8_t SS   = 53;
const static uint8_t MOSI = 51;
const static uint8_t MISO = 50;
const static uint8_t SCK  = 52;
#else
const static uint8_t SS   = 10;
const static uint8_t MOSI = 11;
const static uint8_t MISO = 12;
const static uint8_t SCK  = 13;
#endif


It didn't have any listing for the 644P.
I added in this part, however still no joy:

Code: [Select]
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
const static uint8_t SS   = 30;
const static uint8_t MOSI = 5;
const static uint8_t MISO = 6;
const static uint8_t SCK  = 7;


SS = 30 above is how I have the Ethernet Board wired in my setup. Very crude way to do it, as all SPI devices would therefore use this, however I dont see the SPI library as being very accomidating for this anyway, as the SS is hardcoded inside the library. I am not experienced enough to know the best method to make the SPI library suitable for using multiple SPI devices at once.
Either way, I have hard coded my SS to be 30 at the moment, so I am guessing that the SPI library will therefore use this value of 30.

But as mentioned, still no joy yet.

I really cannot recall how I got it working when I did my first testing, and am now actually starting to doubt if I did infact carry out this testing on the 644P...
My memory is quite poor.

If anyone has any input I would love to hear.
I have already emailed Mark to see if he can assist.

Thanks

WanaGo

I have had a bit more of a look, trying to understand how this works.

In the Ethernet Library, there is the W5100 files that refer to the SPI.h file. So I am assuming this is the link that joins the Ethernet library to the SPI library.

Inside this file, near the bottom are the following lines, and I think these may be related to the problem, if not the cause (additional to the already mentioned).

Code: [Select]
private:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  inline static void initSS()    { DDRB  |=  _BV(4); };
  inline static void setSS()     { PORTB &= ~_BV(4); };
  inline static void resetSS()   { PORTB |=  _BV(4); };
#else
  inline static void initSS()    { DDRB  |=  _BV(2); };
  inline static void setSS()     { PORTB &= ~_BV(2); };
  inline static void resetSS()   { PORTB |=  _BV(2); };
#endif


I am not yet sure what to put here for the 644 and 644P, however I will give it some thought. I think its currently trying to do something incorrect with the SS bit, so a new number needs to be inserted instead of 2 or 4 above in a new #elif statement. I am not sure what that is at this point though, not code that I have had to try and interpret before.

WanaGo

#17
Apr 18, 2011, 08:14 am Last Edit: Apr 21, 2011, 12:41 am by WanaGo Reason: 1
Actually, I think the value in your case should be 16.
In my case, because I am not using a standard SS pin (using Digital 30, or PA1), mine should be 2 - but of PortA.

16 in Binary when mapped to Port B, is PB4 which is the SS pin.
2 in Binary when mapped to Port A, is PA1 which is my custom SS pin.


INCORRECT ABOVE SO EDITED:

Digital 30 for me is PA1, which is _BV(1), not 2 as mentioned above.
PB4 for you, which is _BV(4), not 16 as mentioned above.

I will test this once I have given my son his bath etc - :) Hopefully this is all that is required.

So you maybe need to do this:

EDITED BELOW TOO

Code: [Select]
private:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
 inline static void initSS()    { DDRB  |=  _BV(4); };
 inline static void setSS()     { PORTB &= ~_BV(4); };
 inline static void resetSS()   { PORTB |=  _BV(4); };
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
 inline static void initSS()    { DDRB  |=  _BV(4); };
 inline static void setSS()     { PORTB &= ~_BV(4); };
 inline static void resetSS()   { PORTB |=  _BV(4); };
#else
 inline static void initSS()    { DDRB  |=  _BV(2); };
 inline static void setSS()     { PORTB &= ~_BV(2); };
 inline static void resetSS()   { PORTB |=  _BV(2); };
#endif


and I need to try this:

Code: [Select]
private:
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
 inline static void initSS()    { DDRB  |=  _BV(4); };
 inline static void setSS()     { PORTB &= ~_BV(4); };
 inline static void resetSS()   { PORTB |=  _BV(4); };
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
 inline static void initSS()    { DDRA  |=  _BV(1); };
 inline static void setSS()     { PORTA &= ~_BV(1); };
 inline static void resetSS()   { PORTA |=  _BV(1); };
#else
 inline static void initSS()    { DDRB  |=  _BV(2); };
 inline static void setSS()     { PORTB &= ~_BV(2); };
 inline static void resetSS()   { PORTB |=  _BV(2); };
#endif


Time will tell

Cheers
James

WanaGo


westfw

Quote
I think the value in your case should be 16.

The _BV(n) syntax converts a "bit number" to a binary value, and since the AVR is an 8 bit architecture, it won't ever be more than 7 for accessing bits in an IO port.   The actual definition is:
Code: [Select]
#define _BV(n) (1<<(n))

So for PB4 (which is a bit number as well), you should try:
Code: [Select]
inline static void initSS()    { DDRB  |=  _BV(PB4); };
and for PA1 you can use
Code: [Select]
inline static void initSS()    { DDRA  |=  _BV(PA1); };

Go Up