ATmega32U4 - RaspberryPi SPI problem

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

I'm trying to perform SPI data transfer between RaspberryPi 3 (master) and blank ATmega32U4 (slave).

 

Here are both slave/master code:

 

 

// ******  Master ********

#include "wiringPi.h"

#include "wirinPiSPI.h"

int main(void)

{

    

    wiringPiGpioSetup();

    wiringPiSPISetup(0,1000000);

 

 

    unsigned char a;

    while(1)

   {

        spiDataRaw(0,&a,1);

        delay(20);

   }

    

}

 

 

// ********* Slave *********

ISR(SPI_vect) // Interrupt function
{
    // Blink a led during transfer
    PORTF &= ~(1<<PF5);
    
    char command;
    command = SPDR;   
    SPDR = command+1;     
}

int main()
{
  DDRF = (1<<PF5);
  
  DDRB = (1<<PB3);
  SPCR &= ~(1<<MSTR);
  SPCR |= (1<<SPE) | (1<<SPIE);
  sei();

  while(1)
  {
    PORTF |= (1<<PF5); 
  }
}

 

At the execution of master code, the slave shut the led and stay "blocked". It seems that code stop after the first call of in the interrupt function.  After that, every byte from ATmega is 0.

From the things we know:

  • The ATmega chip is flashed with AVRISP from the RaspberryPi with ATmega SS pin connected on CEO pin on raspberry, i don't think hardware is the problem.
  • The first call of the interrupt slave function comes with after calling "wiringPiSPISetup". If the function is not called, then nothing happen at all. After that, trig the ATmega reset pin make the led shine again but SPI interface is not responding, interruption function is never called, even after re-run the master program.
  • I have to reupload .hex file to retry.

The ATmega32u4 fuses are    E:FB   H:D9   L:FF

 

Did I miss something ?

 

Thanks,
Adrien
 

Adrien Girod
Fleche Tech

Last Edited: Wed. Mar 7, 2018 - 12:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I'm trying to perform SPI data transfer between RaspberryPi 3 (master) and blank ATmega32U4 (slave).

blank...what are you talking about? Has the slave code you show been programmed into the chip?  If your program has been been burned into the AVR, why would you call it blank?

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps he's trying to use the RPi to program the AVR ... ?

 

EDIT

 

AHA - this seems to be a duplicate: https://www.avrfreaks.net/forum/atmega32u4-avr-isp-boot-problem

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Mar 7, 2018 - 03:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

    unsigned char a;

    while(1)

   {

        spiDataRaw(0,&a,1);

 

'a' is never assigned a value so will inherit junk (I expect your C compiler probably told you this?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I expect your C compiler probably told you this?

Maybe the compiler doesn't notice, as there is no direct reference - just taking its address ... ?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, the RaspberryPi flash the ATmega with ISP, blank means that it comes directly from factory with default settings (just disabled JTAG in hfuse). Something may be wrong with default setting that's why I tried to wrote it "clearly", sorry if I did not.

In fact, 'a' is filled with the received char from slave, so it's just an echo. Slave increments this character and send it back to the slave. In theory, it should be counting the number of char translations, right ? 
Well it doesn't, even if a is initialized, 'a' gives only zeros. If I don't call "wiringPiSPISetup", 'a' is never changed from it initial value.

Adrien Girod
Fleche Tech

Last Edited: Thu. Mar 8, 2018 - 10:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Come on guys I really need some help. What did I miss ?

Adrien Girod
Fleche Tech

Last Edited: Fri. Mar 9, 2018 - 11:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks, you think I should give up with this post ?
What did I do wrong ?
 

Adrien Girod
Fleche Tech

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Understand that people here are just other users, like you.

 

Nobody is paid to contribute here, so demanding attention is not going to get you far. If you want priority support, you'll have to pay someone for that.

 

If you're not getting replies to your question, it's probably because you haven't supplied enough clear information.

 

Remember that nobody here knows you, or your project, or your setup, or your level of experience. We can't see what you're doing, or what's happening on your bench.

 

It is entirely up to you to provide sufficient, clear information for people to be able to see what's going on.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

(it worked last time angel)

Ok thank you for your understanding, I'll learn posting.

If someone have an idea, don't hesitate

Adrien Girod
Fleche Tech

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The ATmega32U4 has on-chip debug - so have you used it to see what the chip is actually doing?

 

You say you are programming a blank chip - are you sure that the programming is actually working properly?

 

Add some diagnostics to you code to see what's happening - blinking LEDs, messages from the UART, etc, ...

 

Do you have an oscilloscope or logic analyser to see what's actually happening on the wires?

 

Post your schematic, and some good, clear, in-focus photos of your setup (see Tip #1).

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I finally got an oscillo and new answers !

First, it seems that ISP makes conflict with SPI data transfer. After flash the chip with the hex file, I have to reboot the raspberry to enable master communication. Any one knows why ? 

But there is another issue with ATmega32U4 SPI interrupt. SPIF flag is set when char is loaded, and it load the right data.  But interrupt is never called. 
Here are both interrupt/loop slave code. The loop mode works, the interrupt mode code does not.

// Interrupt mode (does NOT work)

char mess;

void setup() {

  //Spi slave
  SPDR=1;
  DDRB |= (1<<PB3);
  SPCR |= (1<<SPE);

  //enable interrupts
  SPCR |= (1<<SPIE);
  sei();
}

void loop() {

    delay(20);
}
ISR(SPI_vect)
{
    mess=SPDR;
    SPDR=mess+2;
}
// Loop mode, this actually works good
char mess;

void setup() {

  //Spi slave
  SPDR=1;
  DDRB |= (1<<PB3);
  SPCR |= (1<<SPE);

}

void loop() {
    if(SPSR & (1<<SPIF))
    {
       mess=SPDR;

       SPCR &=~(1<<SPE);
       SPDR=mess+2;
       SPCR |= (1<<SPE);
    }

    delay(200);
}

 

I cannot find anything more about interrupt in datasheet. What do you think about this ?

Adrien Girod
Fleche Tech

Last Edited: Wed. Mar 14, 2018 - 10:43 AM