SPI fail

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

I have a real simple SPI function that reads by sending a dummy byte. I also have the uart up and running for debug work. This function comes from the Fatfs demo for anyone who wonders.

Anyway, the init function calls rcvr_spi 10 times to wake up the card or something, but it always hangs. I added the uart output to see what was happening and the best case so far is 5 transmissions with an average of 2-3 before freezing.

Why would the SPIF bit fail to set? I have no SPI interrupts enabled (I'm in master mode).


#define DD_MOSI   2
#define DD_SCK   1
#define DDR_SPI   DDRB
#define DD_SS   4

.....SPI init code


	DDR_SPI = (1<<DD_MOSI)|(1<<DD_SCK)| (1<<DD_SS);
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1); /* Initialize SPI port (Mode 0) */

.....


//SPI receive via dummy send
static
BYTE rcvr_spi (void)
{
	SPDR = 0xFF;
	xputs(PSTR("SPI get\n"));
	loop_until_bit_is_set(SPSR, SPIF);
	return SPDR;
}

EDIT: also tried to go with a while loop in place of the loop_until_bit_is_set() macro thingy, no dice.

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

I can never understand why people have to complicate SPI. You only need a single function. You always have an in and an out. You just 'out' a dummy if you only want the 'in'.

unsigned char spi(unsigned char data)
{
    SPDR = data;
    while ((SPSR & (1<<SPIF)) == 0) ;
    return SPDR;
}

The FatFS code should work straight out of the box. Remember to implement the Card-Detect and Write-protect.

And of course wire the lines correctly.

David.

p.s. I am well aware that ARM SPI can do some very clever things. The AVR can't. So why not keep it simple?

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

david.prentice wrote:

The FatFS code should work straight out of the box. Remember to implement the Card-Detect and Write-protect.

... Yeah it SHOULD work straight out of the box but it doesn't and that's why I am posting. The SPI function freezes after a few uses. I am running the demo on a mega64 and have all the connections done up properly.

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

Well, the code is written for a mega64. It DOES work out of the box.

Re-install the ZIP file and start again.

David.

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

hahah... I can kill ANYTHING that makes sense in 2 lines of code or less!

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Quote:

I can never understand why people have to complicate SPI. You only need a single function.

In fairness to the OP, the fatfs stuff has the twin routines built. (And they work OK for me.)

Note that these are used along with a timeout. Have you set up the timing functions?

It is strange, as the wakeup is 80 dummy clocks. And master SPI should work fine.

You don't have a floating /SS by any chance?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.