SPI slave on ATmega162: transferring data
CASTalk.com Forum Index CASTalk.com
Discussion of DSP, FPGA, storage and embedded system.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web castalk.com
SPI slave on ATmega162: transferring data
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CASTalk.com Forum Index -> Embedded System
Author Message
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 3:19 pm    Post subject: SPI slave on ATmega162: transferring data Reply with quote

Hello, All!

I keep fighting with atmega162 and its SPI :)

So now I configured slave mode. The purpose is to send 8 bytes one after
another. I tested and debugged all code in simulator hundreds of times, I
watched MCU registers, SPI registers and etc., everything seems to be fine,
but after burning in chip - some weird problem.

After receiving data from me, master is logging them onto the screen, so I
can watch what's going on. Indeed it happenes, data got by master are
strange and not what I sent.

My chip is ATmega162, frequency 8MHz. Here is code:

char *data = "123456780";
....

/*
SPI init
Mode: slave
MISO: output
MOSI: input
SS: input
SCK: input
*/
void SPI_SlaveInit(void)
{
DDRB = (1 << PB6);
SPCR = (1 << SPIE) | (1 << SPE); // slave and enable SPI

PORTB |= (1 << PB6);
}

void SPI_SendByte(char *data)
{
SPDR = *data;
while ( !(SPSR & (1 << SPIF)) )
;
}

SIGNAL(SIG_SPI)
{
char *t;

t = data;
while ( *data != 0 ) {
SPI_SendByte((char *)data);
data++;
}

data = t;

LED0_blink(); // for debug: flush LED
}


int main(void)
{
sei();
SPI_SlaveInit();
.........
return 1;
}

And now I don't know, where is my problem has hidden.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Meindert Sprang
Guest





Posted: Thu Dec 09, 2004 3:55 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

"Roman Mashak" <mrv@tusur.ru> wrote in message
news:cp98r7$2ocg$1@mpeks.tomsk.su...
Quote:
Hello, All!

I keep fighting with atmega162 and its SPI :)

So now I configured slave mode. The purpose is to send 8 bytes one after
another. I tested and debugged all code in simulator hundreds of times, I
watched MCU registers, SPI registers and etc., everything seems to be
fine,
but after burning in chip - some weird problem.

After receiving data from me, master is logging them onto the screen, so I
can watch what's going on. Indeed it happenes, data got by master are
strange and not what I sent.

What data is received, what do you see on the screen?

Meindert
Back to top
Gary Kato
Guest





Posted: Thu Dec 09, 2004 4:40 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Quote:
So now I configured slave mode.

I have a feeling that the problem is that you aren't thinking how a slave
should react. You are still thinking as if it is a master. A slave only
responds to a request from a master. A slave does not initiate transfers.

If you really want to see just your data come across, you need to setup the
first data byte on initialization. When the master starts clocking, so will the
slave. I don't know what triggers your SIG_SPI, maybe the toggling of SS? If
so, then you have only the amount of time from the changing of SS to the Master
starting to clock in order to get your data into the data register if it wasn't
put there previously. I'm not sure what happens if you change the data register
while the shifting is happening. Maybe the 162 will save it away in a buffer to
be written after the transfer or maybe it won't allow the change, or maybe it
will allow the change and the master will get some bits from the old data and
some from the new data.

And remember that the data register gets overwritten by watever the master
sent.

What you probably want to do in this specific case is:

on slave init: setup for SPI Slave, then write first byte of data into SPDR.

after transmission complete: get next data byte and write into SPDR for next
transmission.

Quote:
char *t;

t = data;
while ( *data != 0 ) {
SPI_SendByte((char *)data);
data++;
}
data = t;

Quite frankly, I also don't like this. You change the global variable "data"
and then restore it. Why not just change the temporary "t" and do away with the
restore? Only change a global if that side effect is required.
Back to top
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 5:26 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Gary!
You wrote on 09 Dec 2004 11:40:57 GMT:

GK> If you really want to see just your data come across, you need to setup
GK> the first data byte on initialization. When the master starts clocking,
Do you mean, I should write byte into SPDR at my SPI_SlaveInit function? If
so, should I also wait for transfer complete or at that stage it's quite
enought just store value in the SPDR register?
But if slave doesn't initiate a transfer it can silently wait for a moment
when master starts transfer. I don't quite understand why should I write
byte at slave init?
GK> so will the slave. I don't know what triggers your SIG_SPI, maybe the
GK> toggling of SS? If so, then you have only the amount of time from the
As I understood datasheet, interrupt will be invoked as soon as transfer of
byte will be completed.
GK> changing of SS to the Master starting to clock in order to get your
GK> data into the data register if it wasn't put there previously. I'm not

[skip]

GK> What you probably want to do in this specific case is:

GK> on slave init: setup for SPI Slave, then write first byte of data into
GK> SPDR.

GK> after transmission complete: get next data byte and write into SPDR for
GK> next transmission.
ok, I'll follow your advice
??>> char *t;
??>>
??>> t = data;
??>> while ( *data != 0 ) {
??>> SPI_SendByte((char *)data);
??>> data++;
??>> }
??>> data = t;

GK> Quite frankly, I also don't like this. You change the global variable
"data"
GK> and then restore it. Why not just change the temporary "t" and do away
with the
GK> restore? Only change a global if that side effect is required.
In this case, may be it's better to declare 'char *t' as global volatile
variable and use it, or it's rather useless ?

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 5:34 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Meindert!
You wrote on Thu, 9 Dec 2004 11:55:22 +0100:

??>> After receiving data from me, master is logging them onto the screen,
??>> so I can watch what's going on. Indeed it happenes, data got by master
??>> are strange and not what I sent.

MS> What data is received, what do you see on the screen?
on the screen I see like this:
0x01 0x02 0x01 0x00 0x04 0x03 0x02 0x01

ans so on

and from time to time the order of data is changed.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 5:47 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Gary!
You wrote on 09 Dec 2004 11:40:57 GMT:


GK> And remember that the data register gets overwritten by watever the
GK> master sent.

GK> What you probably want to do in this specific case is:

GK> on slave init: setup for SPI Slave, then write first byte of data into
GK> SPDR.

GK> after transmission complete: get next data byte and write into SPDR for
GK> next transmission.
I don't quite understand if I should check SPIF flag in case of interrupt
driven code?

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Meindert Sprang
Guest





Posted: Thu Dec 09, 2004 5:58 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

"Roman Mashak" <mrv@tusur.ru> wrote in message
news:cp98r7$2ocg$1@mpeks.tomsk.su...
Quote:
Hello, All!

I keep fighting with atmega162 and its SPI :)

Below, you pass a pointer to a character:

Quote:
void SPI_SendByte(char *data)
{
SPDR = *data;
while ( !(SPSR & (1 << SPIF)) )
;
}

SIGNAL(SIG_SPI)
{
char *t;

t = data;
while ( *data != 0 ) {
SPI_SendByte((char *)data);
And here, you pass a character to SPI_SendByte, while it expects a

pointer...
Can't be right.

Meindert
Back to top
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 6:16 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Meindert!
You wrote on Thu, 9 Dec 2004 13:58:24 +0100:

MS> Below, you pass a pointer to a character:
I changed the code and simplified a little, now I don't use pointers and
send only one byte at all:

....
char *t_data = "1";

void SPI_SendByte(char data)
{
SPDR = data;
while ( !(SPSR & (1 << SPIF)) )
;
}

SIGNAL(SIG_SPI)
{
SPI_SendByte(*t_data);
......
}

nt main(void)
{
Timer1_Init();
sei(); // enable global interrupt
SPI_SlaveInit();

// write first byte outside ISR to trigger interrupt
SPI_SendByte(*t_data);

while (1)
{
...
}
}

But still the same issue...

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Meindert Sprang
Guest





Posted: Thu Dec 09, 2004 6:40 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

"Roman Mashak" <mrv@tusur.ru> wrote in message
news:cp9j6u$6tp$1@mpeks.tomsk.su...
Quote:
Hello, Meindert!
You wrote on Thu, 9 Dec 2004 13:58:24 +0100:

MS> Below, you pass a pointer to a character:
I changed the code and simplified a little, now I don't use pointers and
send only one byte at all:

...
char *t_data = "1";

void SPI_SendByte(char data)
{
SPDR = data;
while ( !(SPSR & (1 << SPIF)) )
;
}

SIGNAL(SIG_SPI)
{
SPI_SendByte(*t_data);
.....
}

nt main(void)
{
Timer1_Init();
sei(); // enable global interrupt
SPI_SlaveInit();

// write first byte outside ISR to trigger interrupt
SPI_SendByte(*t_data);

while (1)
{
...
}
}

But still the same issue...

The problem with the code above (I assume it is the code for the master) is
that you call spi_sendbyte from within the interrupt handler. Spi_sendbyte
waits for the SPIF flag. The moment this flag is set, another interrupt is
issued while you are still inside spi_sendbyte. You can safely remove the
while loop in spi_sendbyte because you only call it once at the start of the
communication. After that it is called from the interrupt handler only when
the shifter is empty. So, no need to wait for the SPIF flag. And when the
while loop is removed, you'll that the only line left in spi_sendbyte can be
put in the interrupt handler to save a pair of unnecessary call/return.

Meindert
Back to top
Roman Mashak
Guest





Posted: Thu Dec 09, 2004 7:10 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Meindert!
You wrote on Thu, 9 Dec 2004 14:40:44 +0100:

MS> The problem with the code above (I assume it is the code for the
MS> master) is that you call spi_sendbyte from within the interrupt
this is code for slave
MS> handler. Spi_sendbyte waits for the SPIF flag. The moment this flag is
MS> set, another interrupt is issued while you are still inside
MS> spi_sendbyte. You can safely remove the while loop in spi_sendbyte
Alright, I understand you. Here is changed version, I hope is correct :)
MS> because you only call it once at the start of the communication. After
MS> that it is called from the interrupt handler only when the shifter is
MS> empty. So, no need to wait for the SPIF flag. And when the while loop
MS> is removed, you'll that the only line left in spi_sendbyte can be put
MS> in the interrupt handler to save a pair of unnecessary call/return.

char *t_data = "1";
....

SIGNAL(SIG_SPI)
{
SPDR = *t_data;
LED0_blink(); // debugging feature
}

void SPI_SlaveInit(void)
{
DDRB = (1 << PB6);
SPCR = (1 << SPIE) | (1 << SPE); // select slave and enable SPI

PORTB |= (1 << PB6);
}

int main(void)
{
sei();
SPI_SlaveInit();
SPDR = *t_data; //write byte to trigger interrupt

while (1)
{
.....
}

return 1;
}

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
Meindert Sprang
Guest





Posted: Thu Dec 09, 2004 7:27 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

"Roman Mashak" <mrv@tusur.ru> wrote in message
news:cp9mce$bj5$1@mpeks.tomsk.su...
Quote:
char *t_data = "1";
...

SIGNAL(SIG_SPI)
{
SPDR = *t_data;
LED0_blink(); // debugging feature
}

void SPI_SlaveInit(void)
{
DDRB = (1 << PB6);
SPCR = (1 << SPIE) | (1 << SPE); // select slave and enable SPI

PORTB |= (1 << PB6);
}


Almost. You do not detect whether you are at the end of the array or ready
sending data.

Meindert
Back to top
Mark Borgerson
Guest





Posted: Thu Dec 09, 2004 8:26 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

In article <cp98r7$2ocg$1@mpeks.tomsk.su>, mrv@tusur.ru says...
Quote:
Hello, All!

I keep fighting with atmega162 and its SPI :)

So now I configured slave mode. The purpose is to send 8 bytes one after
another. I tested and debugged all code in simulator hundreds of times, I
watched MCU registers, SPI registers and etc., everything seems to be fine,
but after burning in chip - some weird problem.

What is the frequency of the SCK signal from the master? Sometimes
simulators will respond to clock signals too fast for the actual
hardware.
Quote:

After receiving data from me, master is logging them onto the screen, so I
can watch what's going on. Indeed it happenes, data got by master are
strange and not what I sent.


Most of the SPI systems I've worked with use a separate signal as
a chip select. An SPI slave device will generally require a
low-going chip select from the master device to inform it that
a transfer is pending. In your case, you could use that
incoming low-going CS signal to start the transfers. However,
you will need to ensure that there is enough time between the
CS signal and the start of the transfer. As noted in another
post, you may have to 'pre load' the SPI data register
so that the hardware can send the byte as soon as the
master starts SCK. The CS interrupt service routine then simply
loads the NEXT byte when the first transfer is done and
polls the SPIF bit to determine when to load subsequent
bytes.

If you rely on an SPIF interrupt, you need VERY fast
interrupt service to get the next byte into the output
register before clocking starts, if the master is
requesting bytes one after the other in continuous
fashion.
Quote:
My chip is ATmega162, frequency 8MHz. Here is code:

char *data = "123456780";
...

/*
SPI init
Mode: slave
MISO: output
MOSI: input
SS: input
SCK: input
*/
void SPI_SlaveInit(void)
{
DDRB = (1 << PB6);
SPCR = (1 << SPIE) | (1 << SPE); // slave and enable SPI

PORTB |= (1 << PB6);
}

void SPI_SendByte(char *data)
{
SPDR = *data;
while ( !(SPSR & (1 << SPIF)) )
;
}

SIGNAL(SIG_SPI)
{
char *t;

t = data;
while ( *data != 0 ) {
SPI_SendByte((char *)data);
data++;
}

data = t;

LED0_blink(); // for debug: flush LED
}


int main(void)
{
sei();
SPI_SlaveInit();
........
return 1;
}

And now I don't know, where is my problem has hidden.

Mark Borgerson
Back to top
Mark Borgerson
Guest





Posted: Thu Dec 09, 2004 8:35 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

In article <10rgo4fm7tdtfa4@corp.supernews.com>,
mhsprang@NOcustomSPAMware.nl says...
Quote:
"Roman Mashak" <mrv@tusur.ru> wrote in message
news:cp9mce$bj5$1@mpeks.tomsk.su...
char *t_data = "1";
...

SIGNAL(SIG_SPI)
{
SPDR = *t_data;
LED0_blink(); // debugging feature
}

void SPI_SlaveInit(void)
{
DDRB = (1 << PB6);
SPCR = (1 << SPIE) | (1 << SPE); // select slave and enable SPI

PORTB |= (1 << PB6);
}


Almost. You do not detect whether you are at the end of the array or ready
sending data.


And if the LED0_blink() takes longer than 8 SCK cycles to execute,
you're going to have problems with continuous data. If LED0_Blink()
simply sets a flag so that the main routine can blink the LED,
you may be OK.

You haven't said what SCK rate is being used by the master. If
the slave is being implemented on an 8MHz machine, I suspect it
may not like SCK signals in excess of 2MHz. If the SCK is
2Mhz, You don't have a lot of machine cycles to respond
to the interrupts.


I would suggest starting with a clock rate in the 100KHz region,
with perhaps a millisecond between transfers at the host end.
When that works, you can start increasing the SCK rate.
One of the nice things about being an SPI slave is that you
don't really have to worry about slow clock rates.


Mark Borgerson
Back to top
CBFalconer
Guest





Posted: Thu Dec 09, 2004 11:49 pm    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Roman Mashak wrote:
Quote:

.... snip ...

void SPI_SendByte(char data)
{
SPDR = data;
while ( !(SPSR & (1 << SPIF)) )
;
}

It is more efficient to do other things while the transmitter
becomes ready, i.e. use:

while (!(SPSR % (1 << SPIF))) continue;
SPDR = data;

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Back to top
Roman Mashak
Guest





Posted: Fri Dec 10, 2004 5:53 am    Post subject: Re: SPI slave on ATmega162: transferring data Reply with quote

Hello, Meindert!
You wrote on Thu, 9 Dec 2004 15:27:20 +0100:

??>>
MS> Almost. You do not detect whether you are at the end of the array or
MS> ready sending data.
I don't have any array, I'm sending just 1 byte.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru
Back to top
 
Post new topic   Reply to topic    CASTalk.com Forum Index -> Embedded System All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




VoIP Electronics Powered by phpBB