Tiny26 and Mega162 SPI communication [solved]

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

I try to make a tiny26 and a mega162 to communicate using the spi protocol. For the mega I use the hardware spi controller and for the tiny I ported the assembly code from the datasheet for using USI as SPI slave. When master wants the communication to be initiated, pulls the SS line low which triggers an interrupt to the slave, and in the ISR the spi_comm routine is called.

The init and comm routines for master are the following:

void spi_init_ms(void)
{
	DDRB |=0xb0;						        //SCK,MOSI,~SS as outputs
	cbi(DDRB,6);						         //MISO as input
	sbi(PORTB,6);                 //MISO pull-up enabled
	SPCR = ((1<<SPE)+(1<<MSTR)+(1<<SPR1));	//SCK freq = fosc/64
};

unsigned char spi_comm(char tosl)
{
	unsigned char fromsl;
	cbi(PORTB,4);					    //pull ~SS low
	_delay_loop_2(200);				//wait 
	SPDR = tosl;
	while(!(SPSR&(1<<SPIF)));		    //wait for end of transmition
	fromsl = SPDR;
	sbi(PORTB,4);
	return fromsl;
};

and for tha slave:

void spi_init_sl(void)
{
	DDRB = 0x02;
	PORTB= 0x45;
	USICR = (1<<USIWM0)+(1<<USICS1);	//3-wire , shift reg clock ext., pos.edge
	GIMSK = 0x40;
	MCUCR = (1<<ISC01);				   //ext interrupt on falling edge
};

unsigned char spi_comm(unsigned char tomst)
{
	unsigned char frommst;
	
	USIDR = tomst;
	USISR = (1<<USIOIF);
	while(!(USISR&(1<<USIOIF)));	//wait for 4bit counter overflow
	frommst = USIDR;
	return(frommst);
};

The problem is that when i initiate the transmition from the master with the slave configured to send always a 0x3c byte, the data i receive in the master look like 0x00 0xf0 0x3c 0x3c 0x27 0x9e 0x1e 0x3c (real received data).

Anybody has any clue for what the prob is? I have spent quite a long time trying different settings and this is the best i could make (much better than receiving only 0xff).

P.S. I am new in c. I used to write code in assembly.

Last Edited: Tue. Dec 14, 2004 - 04:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have now confirmed that the problem is on the tiny26 side. I tried the same code on the master Mega162 with another Mega162 configured as slave and it worked.

So, has anybody ever made a mcu with usi work as spi slave? Any help would be greatly appreciated. I am stuck.

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

A search for "usi spi"

Comes up with a lot of links in this site.

Also this one:

https://www.avrfreaks.net/phpBB2/...

/Bingo

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

Solved already. My functions are absolutely working. It was a hardware problem. Thanks anyway

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

Hi,

What was the hardware problem? Please remember that other people may search these forums with similar (or the same) problems. It is very frustrating to read through an entire thread thinking the answer is lurking...only to end up with a statement such as yours.

Regards,
Steve

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

I didn't mention the exact problem because it was just a case of bad crystal.
I am actually thinking of writing a how-to on SPI slave for USI when i'll find the time. I tried to find documentation on this subject but i only found for master SPI and I'd like to help someone with the same problem as i get help many times.

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

Thanks for posting the solution :!: