Reply
Posts: 60
Registered: ‎10-25-2016

spi 2 byte transfer

i am using EFM8UB11F16G controller ,i have to transfer 2 bytes of data (add+data)during write operation and during read operation i have to read data from that addr(2 bytes (1add+data(to be read)by sending 1 byte dummy data)

but during write operation when i send 1 byte of data simultaneously i am  receiving 1 byte of data so during write opeartion i am sending 2 byte of data and  i am receiving 2 byte of data,so what should i do ,i must clear receive fifo each time??

 

Posts: 3,025
Registered: ‎02-07-2002

Re: spi 2 byte transfer

I recommend to read instead of clear the receive fifo. SPI is a protocol that always writes and reads the exact same number of bytes (bits actually). At the lowest level only handle this transfer and interpret at a higher level.

Posts: 60
Registered: ‎10-25-2016

Re: spi 2 byte transfer

at present i want to check whether the slave device is working or not(si4032)

so what i did is during write operation i loaded x55(data) in x75(add)from master spi ,and during read operation i read data from same add(0x75 (add)) i must get 0x55 

//main program

{spiwrite(0x75,0x55);

spiread(0x75);}

 

void spiwrite(add,data)

{SPI0DAT = (add | 0x80); 
while (SPI0CN0_SPIF == 0)
SPI0CN0_SPIF = 0;

SPI0DAT = (data); 
while (SPI0CN0_SPIF == 0)
SPI0CN0_SPIF = 0;

}

same for read opration bus oring is not donewhen i transfer 2 byte(write opertion) i am getting 2byte in receive fifo and dur}ing read operation again i am receiving 2 byte so total i am receiving 4 byte but i want only 4th (1 byte )??

how to interpret at higher level means??

 

 

 

 

 

 

Posts: 3,025
Registered: ‎02-07-2002

Re: spi 2 byte transfer

I recommended you to write one function:

void spitransfer (uint8_t * buf, int size)

that writes out the buffer and places read data back into this buffer.

 

 

Forget about the meaning of the bytes for this particular device for now.

Posts: 60
Registered: ‎10-25-2016

Re: spi 2 byte transfer

sry i didnt get u ,what u want to tell?

during write operation 

buf[0x75,0x55] and size=2,and decrease the size and transfer the byte from SPIODAT till size is equal to zero????

during read operation how can i use" void" i must use uni8_tspi transfer because i have to return the received byte to main program???

Highlighted
Posts: 3,025
Registered: ‎02-07-2002

Re: spi 2 byte transfer

Please do not address me with u, I am not a micro. Your keyboard also contains the 'y' and 'o' key. It shows utter lazyness and assumes that other peoples time to decipher your message is much less important than your own time to type full words.

 

Now for the code. The fact that I have to spell this out makes me wonder how far you'll get with the rest of your project.

void spitransfer (uint8_t * buf, int size)
{
    int i;
    for (i=0; i<size; i++)
    {
        SPI0DAT = buf[i];
        while (SPI0CN0_SPIF == 0) { /* wait */ }
        SPI0CN0_SPIF = 0;
        buf[i] = SPI0DAT;
    }
}
Posts: 60
Registered: ‎10-25-2016

Re: spi 2 byte transfer

@vanmierlo i am sorry i will not use short cut 

and thank you  for the code

//still i have one doubt ,buf[array of 4 byte(1=addr, 2==data,3=addr(addrfrom have to read data,4=dummy value(oxFF]

i main program i must check whether buff[4]==data????

 

 

Posts: 3,025
Registered: ‎02-07-2002

Re: spi 2 byte transfer

I don't know your spi device, but I very much doubt you can combine two transfers into one operation. I definitely recommend against it. Normally you first assert the CS of a device, then do the transfer and finally release the CS for each transfer. In your case the device probably expects the first byte after asserting CS to be the address.

 

Further buff[4] will be outside the 4-byte array.

 

But yes, you should read a byte in the buffer to get the response data.

Posts: 60
Registered: ‎10-25-2016

Re: spi 2 byte transfer

i am using ISM transmitter (SI4032) slave device

My code is

{main program 

 

SPI_Byte_Write(ox75,0x55);

Itstatus(receive data)=SPI_Byte_Read(ox75);

}

 

void SPI_Byte_Write(uint8_t regAdd, uint8_t regData) {

NSS = 0;// chip select NSS  ,NSS=0 
SPI0DAT = (regAdd | 0x80); // (Msb  bit =1 }
while (SPI0CN0_SPIF == 0); // send the data
SPI0CN0_SPIF = 0;

SPI0DAT = regData; //data
while (SPI0CN0_SPIF == 0);

SPI0CN0_SPIF = 0;
NSS=1                                     //chip select NSS=1
}

 

uint8_t SPI_Byte_Read(uint8_t U8) {
NSS = 0;
SPI0DAT = U8; // addr bus MSB=0 for read operation
while (SPI0CN0_SPIF == 0); // send the data
SPI0CN0_SPIF = 0;

SPI0DAT = 0xFF;
while (SPI0CN0_SPIF == 0)
;
SPI0CN0_SPIF = 0;

NSS = 1;
return SPI0DAT; //read the receive data and send it  back to main  program

}

Posts: 60
Registered: ‎10-25-2016

Re: spi 2 byte transfer

Problem is solve ..above code is right  i have just store the receive byte after sending each 1 byte and the bytes which i required, i saved it in 1 register and neglect other bytes  and then called it from main program