 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Ttelmah Guest
|
|
Posted: Tue Apr 12, 2005 2:31 am |
|
|
You can 'write with the slave, and read with the master', _but_ the slave cannot 'initiate' the transaction. This is the whole point/difference between the master and the slave. Data is transferred across the bus, when the _master_ clocks it.
Are you using your chips hardware serial port?. If not, this represents by far the easiest way to do what you want.
The second way is to add one extra wire, from an output on the 'slave', to an interrupt input on the master, and when this is operated by the slave, then have the master initiate a transaction. The third method is to use I2C. Here you program both devices to stay as 'slaves'. Then to send, you reprogram the device that want to send to be 'master', and transmit the data. With I2C, because the lines are driven as 'open collector' drives (pull down only), there is no risk of damage, if both chips try to drive at once. You obviously have to implement bus collision detection to cover the situation where both systems try to send at once (alternatively, just have a clock running at something like 100Hz, fed to both chips, and allow chip 1 to only transmit when this goes high, and chip 2 to only transmit when this goes low - this is simple to implement, and gives relatively rapid communication in both directions).
Best Wishes |
|
 |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Tue Apr 12, 2005 3:01 am |
|
|
Ok then,
this project must use SPI It is out of my control.
You said i can write with the slave and read with the master but could you give me some code example ?
Or could you help me with a code example to use interrupts doing what i want to do ?
Many thanks for your help, i am pretty much lost with all this ...
Thanks again ! |
|
 |
Ttelmah Guest
|
|
Posted: Tue Apr 12, 2005 10:08 am |
|
|
Every time the master writes a byte, it _at the same time_, receives back the byte waiting in the slave. This again is a key part of SPI.
In most (all?) cases, the SPI lines, and the I2C lines are the same signals from the chip, so you are not forced to use SPI, unless it is too late to just shift the interconnection of the lines, and add a couple of resistors.
You are going to need just as much rewiring, to get 'slave triggered' SPI, since you are going to have to add a wire from the slave to the master, and require another input, which is preferably an interrupt.
Doing a bidirectional transfer in the past (where the master 'polls' the slave every few mSec, sending it one block of data, and receiving back another), I used this:
| Code: |
//This code, shows how an SPI slave, can respond with a block of data.
#DEFINE READ_SSP() (SSPBUF)
#DEFINE WAIT_FOR_SSP() while((SSPSTAT & 1)==0)
#DEFINE WRITE_SSP(chr) SSPBUF=(chr)
//Need to define SSPSTAT, and SSPBUF to suit your chip
#define READ (1)
#define WRITE (64)
//Slave code
int8 buffer[10];
int8 obuffer[10];
#INT_SSP
void SSPsub(void) {
/* This is the code to handle the SPI requests. */
/* The entire transfer is done inside the subroutine. In the event of
a problem this will force a watchdog recovery, and give much higher
speeds. Downside though is that the interrupt is therefore slow,
and could interfere with other operations... */
int *ptr;
int *optr;
unsigned int chr;
int ochr;
/* I arrive here, because the 'main' unit has clocked a transfer. Initially,
my output register would be empty, so it will receive garbage, and I
will receive the 'marker' byte sent. */
/* I use my own SPI routines to avoid the oddities of the standard
routines. */
chr=READ_SSP();
if (chr!=0xAA) return;
WRITE_SSP(0x55);
/* Here I have to wait for transfer */
WAIT_FOR_SSP();
ptr=buffer;
optr=obuffer;
ochr=0;
while (ochr<10) {
WRITE_SSP(*(optr));
++optr;
++ochr;
WAIT_FOR_SSP();
*(ptr++)=READ_SSP();
}
}
//Master code
int8 obuffer[10];
int8 buffer[10];
void transfer(void) {
/* this routine sends a block of ten bytes to the slave, and receives ten
bytes back at the same time. The blocks of data are in 'obuffer', and
'buffer', and after the transfer, the contents of the blocks a both ends
are swapped... */
int *ptr;
int *optr;
unsigned int chr;
int count;
while (true) {
WRITE_SSP(0xAA);
WAIT_FOR_SSP();
/* Here I have clocked a byte to the slave, and now need to wait long
enough for it to actually be inside the interrupt routine */
delay_cycles(60);
/* The value needed here will depend on the worst case interrupt
latency of the slave (what else it is doing... */
WRITE_SSP(0);
chr=READ_SSP();
/* Here the slave should have responded with '55' if it is in the handler.
If not, I need to retry. */
if (chr==55) break;
}
/* Now I need to clock out ten characters, and the returns.
ptr=buffer;
optr=obuffer;
count=0;
while (count<10) {
WRITE_SSP(*(optr));
++optr;
++count;
WAIT_FOR_SSP();
*(ptr++)=READ_SSP();
}
}
|
Best Wishes |
|
 |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Wed Apr 13, 2005 3:49 am |
|
|
Hello, i worked it out pretty much like i wanted for basicly do what i want to do. Thank you very much for your help !
I may nee some help later on my interrupts !
| Code: | /*******************************************************************************
************************************** main ************************************
*******************************************************************************/
void main(void)
{
spi_master_write();
//spi_slave_read();
}
/*******************************************************************************
************************************ MASTER ************************************
*******************************************************************************/
void spi_master_write(void)
{
int8 byte1;
int8 byte2;
int8 byte3;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_64);
while(1)
{
//if (SPI_DATA_IS_IN())
//{
spi_write(byte1); //to wake up the slave
byte1 = spi_read(); //to read in what the slave has to say
printf("\nDATA = %X", byte1);
delay_ms(1); //delay for slave vectoring
byte2 = spi_read(byte2); //start swapping bytes
printf("\nDATA = %X", byte2);
delay_ms(1); //way more than I needed
byte3 = spi_read(byte3);
printf("\nDATA = %X", byte3);
delay_ms(1);
delay_ms(1000);
//}
}
}
/*******************************************************************************
************************************ SLAVE *************************************
*******************************************************************************/
void spi_slave_read(void)
{
int8 byte1;
int8 byte2;
int8 byte3;
setup_spi(SPI_SLAVE|SPI_SS_DISABLED);
while(1)
{
spi_write(0x01); //the trick was to load the buffer ahead of time
if(SPI_DATA_IS_IN()) //becareful with this, it sucks up clocks
{
output_low(PIN_SS);
byte1 = spi_read();
byte2 = spi_read(0x02);
byte3 = spi_read(0x03);
output_high(PIN_SS);
//if (spi_read()==0) {
//The master now knows that the slave has seen the byte
//and can do something here...
//}
}
}
}
|
|
|
 |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|