Helpful ReplyHot!Problem with SPI clock

Page: 12 > Showing page 1 of 2
Author
JosepI
Starting Member
  • Total Posts : 54
  • Reward points : 0
  • Joined: 2013/12/19 21:51:53
  • Location: 0
  • Status: offline
2018/10/11 05:06:59 (permalink)
0

Problem with SPI clock

Hello there!
 
I am using a PIC16(L)F1847 and I want to communicate via SPI with a RFID TAG, the EM4325. The problem I am having is very simple, but I am struggling to find a solution. My clock (SCK) is currenty working in the following way. It does 8 clock cycles and then it stops, as it shows in the osciloscope screenshot. When it works this way the slave has no clock cycles to send its response, thus I cannot communicate with him. I would like to have consecutive clock cycles (just like a normal clock) so that my SPI slave can answer me. 
 
I've tried activating clock out enable (CLKOUTEN) bit from the CONFIG1 register. This activates a constant clock but on a diferent pin than the SCK, that would make me change all the pin configuration (I am using a printed board so it is not easy for me). It also does not care about the chip select or any other SPI protol, it is always on.
 
So my question is: Is there any way to make the SCK work as shown in the second picture?
 
In the first image you can see is an osciloscope screenshot of my current clock and on the second image you can see what type of clock I would like to have.
 
Thank you very much for your time!! :)

Attached Image(s)

#1
Nikolay_Po
Super Member
  • Total Posts : 1691
  • Reward points : 0
  • Joined: 2012/04/01 13:49:27
  • Location: Russia, Novorossiysk
  • Status: offline
Re: Problem with SPI clock 2018/10/11 05:24:55 (permalink) ☄ Helpfulby Jim Nickerson 2018/10/11 05:40:41
+4 (4)
Usually you need write into SPI to make the clock signal even in case you need only reading.
#2
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Problem with SPI clock 2018/10/11 05:34:11 (permalink) ☄ Helpfulby Jim Nickerson 2018/10/11 06:59:53
+4 (4)
As the answer of Nikolay implies, you have a (common) wrong understanding of how SPI communication works.
You can google for  e.g. "spi read" or "microchip spi read" or of course you could start some basic reading .
 
This is just one of many discussions about this
https://www.microchip.com/forums/m741951.aspx
#3
Jim Nickerson
User 452
  • Total Posts : 5280
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Problem with SPI clock 2018/10/11 05:44:23 (permalink)
+2 (2)
Nikolay is being kind.
To get the SCK out to toggle you must write some data to the SSPxBUF register
#4
RamonRomeu
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/06/05 07:50:46
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/11 06:54:27 (permalink)
0
Hello, I am Ramon and I am working with Josepl, the autor of the thread. What we want is to write to the SPI slave. In order to do this we are using the following function:
 
void SPI_write(unsigned char data) 
{
    RB0 = 0; //Select chip
    unsigned char dummy_data; 
    dummy_data = SSP1BUF; //Clears BF 
    PIR1bits.SSP1IF = 0; //Clear interrupt flag 
    SSP1CON1bits.WCOL = 0; //Clear any previous write collision
    
    SSP1BUF = data; //clock out data 
    while(!SSP1STATbits.BF); //Wait until transmission completed 

    PIR1bits.SSP1IF = 0; //Clear interrupt flag bit
    RB0 = 1; //Disselect chip
}

 
I guess that the code is correrct because the data shown on the MOSI line is the wished one. The problem we have is with the clock, that we want it to be as the image in the main thread, taken from the datasheet of the slave.
 
(We asked to the EM4325 support team and they said that the error was in the clock, it sould be working as the one in the image).
 
Thank you!
 
Ramon
 
 
#5
Jim Nickerson
User 452
  • Total Posts : 5280
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Problem with SPI clock 2018/10/11 07:03:45 (permalink)
+4 (4)
As already stated you have yet to gain enough knowledge about how SPI works.
What happens if you try this code ?

while(1)
{
SPI_write(0);
}

#6
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Problem with SPI clock 2018/10/11 07:30:58 (permalink)
+2 (2)
I've tried activating clock out enable (CLKOUTEN) bit from the CONFIG1 register. This activates a constant clock but on a diferent pin than the SCK,

 
We asked to the EM4325 support team and they said that the error was in the clock, it sould be working as the one in the image

 
You seem to think that you just need to "shape" the clock like that and it will work.
 
Try Jim's code like this

 
for (i=0;i<3;++i){
SPI_write(0xaa);
}

 
This will give you the clock you want (with much smaller gaps between the bytes).
But a clock just "looking" like that or that in the image doesn't mean that it solves your problem with the communication. 
 
Do you understand that for each byte you want to read from your EM4325 device, you have to write some dummy data ? With SPI you are always writing and reading at the same time. After 8 clock pulses you have shifted out one byte, and at the same time received one byte. 
Your EM4325 chip manual shows e.g. in "SPI Master Example #3" that you would use your function SPI_write to write 3 valid bytes in sequence to the device.  Then you would write another two dummy bytes to SPI to "read" the data (answer) from the device.
 
You need to read the SSP1BUF register for each transfer. Your SPI_write function is not suited for that.
You will find examples with functions typically called SPI_transfer, which write and receive at the same time.

The above mentioned link also explains this, it appears you did not take the time to read.
 
 
post edited by rodims - 2018/10/11 10:17:27
#7
Jim Nickerson
User 452
  • Total Posts : 5280
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Problem with SPI clock 2018/10/11 08:23:22 (permalink)
+2 (2)
Interesting, after 5 years as EE student...
Rolf, I wonder which code he will try (if any )?
#8
qhb
Superb Member
  • Total Posts : 7156
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Problem with SPI clock 2018/10/11 12:47:04 (permalink)
+2 (2)
A lot of the confusion comes from the EM4325 datasheet itself.
It can act as an SPI Master or Slave.
In SPI Master mode, it conforms to normal SPI timing.
In SPI Slave mode, it's just not SPI!
As you can see from the timing diagram in the first post, it wants a continuous SCLK signal, and the data response starts a variable time later, signified by an initial one bit on MISO. They're lying when they call this SPI.
The PIC will have to send continual dummy zero bytes, and when data starts coming back, it will have to do software rotation to re-align the data to get the first received "1" bit into the correct bit position.
 
http://www.emmicroelectronic.com/sites/default/files/public/products/datasheets/4325-ds_0.pdf
 

Worst forum problems are now fixed, but the damn firewall is still there.
#9
jack@kksound
code tags!
  • Total Posts : 2761
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/11 13:27:45 (permalink)
+2 (2)
Sounds as if it may be better to simply bit-bang the receive.
#10
qhb
Superb Member
  • Total Posts : 7156
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Problem with SPI clock 2018/10/11 14:30:57 (permalink)
+2 (2)
jack@kksound
Sounds as if it may be better to simply bit-bang the receive.

Indeed, that crossed my mind too.
Particularly challenging when you're a student, and not familiar with SPI to start with...

Worst forum problems are now fixed, but the damn firewall is still there.
#11
jack@kksound
code tags!
  • Total Posts : 2761
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/11 14:41:16 (permalink)
+2 (2)
Always surprises me that SPI, a relatively simple protocol, should result in so many problems. I guess it is the full duplex nature that throws so many off...
#12
qhb
Superb Member
  • Total Posts : 7156
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Problem with SPI clock 2018/10/11 14:43:02 (permalink)
+2 (2)
So many people think it is just USART with a clock...
 

Worst forum problems are now fixed, but the damn firewall is still there.
#13
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Problem with SPI clock 2018/10/12 03:30:35 (permalink)
+1 (1)
So far communication with the two OPs is completely one-way.  I would assume SPI-master, but why should we guess ...
#14
RamonRomeu
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/06/05 07:50:46
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/16 01:42:41 (permalink)
+1 (1)
Hello! And sorry for my late response, I couldn't work on if for a couple of days. Thank you very much for all your answers.
 
I've read all your answers really interested in all of them. Specially in yours qhb and jack@kksound, do you really think the solution is bit-banging the recieve. I do not know anything about it, but I can work on it if it's the solution to my problem. 
 
And Jim Nickerson and rodims i've tried your code, and this is what I get. In the following image you can see what I am getting what I send rodims code:
while(1){
 
 
 
RB0 = 0;
for (int i=0;i<3;++i){
SPI_write(0xAA);
}
RB0 = 1;
__delay_us(15);
}

The first line is the chip select, second line is the SCK, third line is the MOSI and fourth line is the MISO.

 The response I am getting in the MISO line is a 1111 0100 (0xF2). I couldn't find in the datasheet any meaning for this response. This is the first time I get a response from the EM4325 that is not all ones. I do not really understand this response from my EM4325. Why I send three times the same and the response is each time different?
 
Thank you very much, and again, sorry for the late response.
 
Ramon
 
post edited by RamonRomeu - 2018/10/16 04:55:01

Attached Image(s)

#15
jack@kksound
code tags!
  • Total Posts : 2761
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/16 08:38:11 (permalink)
0
Why are you writing a 0xAA to the EM4325? Is this a command code? I cannot find that code in the datasheet. Are you sure that is the right thing to send the EM4325?
#16
rodims
Super Member
  • Total Posts : 1353
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Problem with SPI clock 2018/10/16 09:30:42 (permalink)
0
Ehmm, that's most likely the result of my (and Jim's) post. Of course that was not meant to do a meaningful communication with the EM4325.  It was only intended to  a) verify that the PIC actually outputs to SPI  (0xaa giving an easy signal on the SDO line) and b) to show that the mere clock shape and a functional communication are two rather different things, since Ramon / OP apparently confused that.
 
Ramon should first state in which mode he uses or wants to use the EM4325 as explained by qhb in post #9.
I.e.  
PIC = SPI Master and EM4325 = slave
or
PIC = SPI Slave and EM4325 = master
 
as also configured in the System Memory I/O Control Word / SPI Config bits
http://www.emmicroelectronic.com/sites/default/files/public/products/datasheets/4325-ds.pdf
page 22
 
#17
Jim Nickerson
User 452
  • Total Posts : 5280
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: Problem with SPI clock 2018/10/16 10:09:32 (permalink)
0
My post was intended to answer the original request
I would like to have consecutive clock cycles (just like a normal clock)
, maybe that is not what the OP really wanted ...
#18
RamonRomeu
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/06/05 07:50:46
  • Location: 0
  • Status: offline
Re: Problem with SPI clock 2018/10/17 02:03:19 (permalink)
0
Hi, 
I am using my EM4325 as a slave and the PIC as the master. All the configuration bits have been checked and configured with an RFID reader. The OP was made becuse we reached the EM4325 support team because we where not able to comunicate with the TAG  via SPI and their respose was that we needed a constant clock rate, as the first image from the OP shows. That is what we asked in the OP. But maybe an other solution is posible, istead of having a constant clock rate. 
 
The SPI function is working correcly and I send the data as the datasheet says. I first send the command word, in this case a 0xE8, SPI write command, and then I send the data byte I want to write. Then I send 3 bytes of dummy data so that I get the TAGs response. This it what the datasheet says to do, but then I load all the data of the EEPROM with the reader and nothing has benn written to memory of the EM4325. I get all 0s
 
Now I do not have an oscilloscope close to show you all the signals, I will send you a capture as soon as I can.
 
Than you, 
 
Ramon
 
 
#19
qhb
Superb Member
  • Total Posts : 7156
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Problem with SPI clock 2018/10/17 02:14:32 (permalink)
0
As I said in post#9, the EM4325 in slave mode is NOT standard SPI.
The easiest way to do it would be to ignore the SPI peripheral and just bit bang the interface.
 

Worst forum problems are now fixed, but the damn firewall is still there.
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2018 APG vNext Commercial Version 4.5