Page 1 of 1

SPI bus Char and String variables?

Posted: Sun Mar 24, 2019 9:11 pm
by fotios
Hi everyone.
First of all, I try to implement an SPI bus for the first time.
I have a project with an 8-bit P16F18877 that retrieves data from a slave ATM90E32 through SPI.
The addresses of registers of ATM90 are fixed, 15-bit wide and they follow the 16th bit, which defines the Read = 1 or Write = 0, states.
However, the higher register address is FF and only the 10 lower bits are decoded, the higher 5 bits are ignored by default.
The registers data are 16-bit wide.
So for a "Write to a register" cycle, the master must send a 32-bit word to the slave
The SPI component of FC7 can send or receive Character type or String type variables.
Which variable type could I use in this instance to send/receive data to or from the ATM90E32?

Update: just now I tried to insert a variable in Ulong format as "Sent Char" of SPI component and does not show error.
What means the type Byte? Does it mean "Byte oriented"?

Thanks

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 11:09 am
by fotios
I have another question:

In FC7 SPI Call Component Macro, to receive data I suppose you have to select "GetChar" which returns a variable. Where I could place the register address of which I want to read the data?

Thanks

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 12:16 pm
by LeighM
With Flowcode 7 I suggest you do this ..
Write data...

Code: Select all

CS low
SendChar(Address >> 8)
SendChar(Address)
SendChar(Data >> 8)
SendChar(Data)
CS high
Read data ...

Code: Select all

CS low
SendChar((Address >> 8)|0x80)
SendChar(Address)
HighByte = GetChar()
LowByte = GetChar()
Data = (HighByte << 8) + (LowByte)
CS high
Where Address and Data are 16 bit unsigned integers

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 10:09 pm
by fotios
Thanks, Leigh
I tried your C code as you suggest but during compiling I receive errors regarding the Uint variables Address, Data, HighByte, LowByte: are not defined
Something is missing and I'm not an expert in C.

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 11:08 pm
by medelec35
Hi Fotis,
You don't need to use C.
For example in my flowchart that communicates via SPI:
Typical SPI commands.png
(35.84 KiB) Downloaded 2437 times
Just make sure address variables are assigned of course. :)

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 11:10 pm
by fotios
null

Re: SPI bus Char and String variables?

Posted: Mon Mar 25, 2019 11:27 pm
by fotios
medelec35 wrote:Hi Fotis,
You don't need to use C.
For example in my flowchart that communicates via SPI:Typical SPI commands.png
Just make sure address variables are assigned of course. :)
Hi Martin
Glad to hear you after so long :D
I'm not sure that understand, but I've read for the tricky dummy byte on the web. It pushes the slave to send data to master?
For writing a register the thing is quite simple yet using a Ulong variable with the FC7 SPI component.
As the register address and data of the slave are 16+16 = 32 bit wide, you could send any combination.
The problem arises in the reading of the register data, you have to first send the address (Write) and (if remember well) a dummy word.
I couldn't find a way to Read with the FC7 SPI component.

Thanks a lot

Re: SPI bus Char and String variables?

Posted: Tue Mar 26, 2019 8:41 am
by LeighM
Hi Fotis,
Sorry, I should have said that was not C code, but pseudo code.
Just giving you the macros to use from the Flowcode component, SendChar and GetChar
They will send or receive 8 bit data bytes, so you need 4 of them to read or write the 32 bits
Also use the chip select macro at start an end of the sequences
Hope you follow that.
Alternatively you could use the CAL component MasterByte function as in Martin's example.

Leigh

Re: SPI bus Char and String variables?

Posted: Tue Mar 26, 2019 9:01 pm
by fotios
Hi everyone

I tried Martin's solution without success.
However, I couldn't be sure that I did the correct selections. I've just followed the logic of Martin as much as I could understand.

The SPI bus format of ATM90E32 is as usual:
1. Register Address = 16-bit wide, of which the MSB (16th) should be "0" for Write and "1" for Read, and the following 5 bits are ignored.
2. Register Data = 16-bit wide.
3. The acceptable bitrate is from 400Kbps up to 1100Kbps. In my project the Fosc=8MHz and so the instruction cycle=2MHz (T=0.5usec). For the SPI clock, I have selected a prescaler Fosc/16=500KHz (T=2usec) in order to be within the given specification and to can check it with my relatively slow oscilloscope.

I have a question:
Both in Leigh's and Martin's code, the SPI component transmits and receives sequential bytes. The "SENDCHAR" and "GETCHAR" commands are repeated per each byte. Why this? It is because the SPI component cannot transmit a whole Unsigned integer at once?
It would not be a problem for the slave to scan the bits that receive? I suppose that each time a "SENDCHAR" command is executed to transmit the Address High Byte, then again executed to transmit the Address Low byte, and then again to transmit the Data High byte, and then again to transmit the Data Low Byte, the SCLK should be enabled - disabled sequentially 4 times?

I checked my hardware with the oscilloscope and I could see the CS, MOSI, and SCLK signals while the MISO, not at all. The MISO is always high.
I also noticed that the MOSI and MISO are in High idle state but I suppose that is normal.

Thanks

Re: SPI bus Char and String variables?

Posted: Wed Mar 27, 2019 12:55 pm
by fotios
I think have good news :D

I tried with 8-bit variables: Add_H, Add_L, DatIN_H, DatIN_L, DatOUT_H, DatOUT_L
I did the same as in Martin's example by transmitting the High - Low bytes separately and in sequence.
Now I can see on the LCD the two bytes of the Read-only "LastSPI" Data register of ATM90E32, that are the same with the two data bytes I had sent previously, 0x00 and 0x0F as decimals.
To be sure, I checked the SPI lines with the oscilloscope and this time I have signal and in MISO.

To summarize, the problem was that I was trying to send - get Uint type variables at once, without breaking them in two bytes. While the FC SPI component transmits and receives only bytes.
Because the register data could be some times 16-bit wide, and the reception is broken in two bytes, there is the need for a Uint type variable in which the received High Byte and the Low Byte will be shifted to form the original data word. That explains why in Martin's and Leigh's code the Shift operation is used.

Thanks a lot