SPI SCK problem

03 Jun 2009 . Edited: 03 Jun 2009

Hi Guys,

I am having some problem with SPI interface, somehow SCK clock only activates once, anybody has any idea what has happened? or anyone has been having the similar problem before?

Thanks a lot.

SHF

03 Jun 2009 . Edited: 03 Jun 2009

Hi,

You might need to give a little more detail than that. Can you give an example of what you mean?

e.g. If you run...

#include "mbed.h"

SPI spi(5, 6, 7);

int main() {
    spi.write(0xFF);
}

are you saying the SCK pin is not transitioning multiple times to clock out the byte correctly?

What are you using to monitor the output? An oscilloscope?

Thanks!

Simon

04 Jun 2009

Hi,

Thank you for reply!

yes ,I run the program what you said ,and use oscilloscope to monitor the output, through the oscilloscope can see the SCK clock only activates once.

 

Thanks!

SHF

04 Jun 2009

Hi,

zhao juntao wrote:

yes ,I run the program what you said ,and use oscilloscope to monitor the output, through the oscilloscope can see the SCK clock only activates once.

Ok, strange. I assume by "activates once" you mean it only transistions once low-high-low.

Just as a sanity check, perhaps try:

#include "mbed.h"

SPI spi(5, 6, 7); // mosi, miso, sclk
DigitalOut cs(8); // cs

int main() {
    while(1) {
        cs = 0;
        spi.write(0x53); // 01010011
        cs = 1;
        wait_us(2);
    }  
}

This adds an example of what would naturally be the chip-select signal (active low), and also make sure the data has lots of transitions rather than being all 1's, and repeats it all.

This should also give you a clear frame to work with on the oscilloscope; use the high-low chip select as the trigger, and frame it so you see the cs returning high, and then you should be able to probe the mosi (data) and sclk (clock) signals with another channel and see what is going on.

Report back on what you find out (take a photo of the oscilloscope if you want!), and if that still doesn't work, i'll get mine out too to do some testing! 

Cheers,

Simon

04 Jun 2009

Hi,

Thank you for reply! The SCK can work,but there have another problem:the datas read form buffer1 are not same as what write before.The details are follow: 

I have try to run the program:

#include "mbed.h"

Serial pc (USBTX,USBRX);

DigitalOut _ncs(8);
SPI _spi (5,6,7);

int main() {

  _ncs = 1;
  _spi.format(8,0,0);
  _spi.frequency(5000000);

  _ncs = 0;
  _spi.write(0x9f);
  int ID  = _spi.write(0x00) << 8; 
  ID |= _spi.write(0x00);          
  _ncs = 1;
  pc.printf("ID register is 0x%X\n",ID);
  wait(1.0);
  
  pc.printf("Writing to SRAM buffer #1\n");
  
  _ncs = 0;
  _spi.write(0x84); // Buffer 1 write command
  _spi.write(0x0);  // address 0
  _spi.write(0x0);  // address 0
  _spi.write(0x0);  // address 0
  
  // Write 8 bytes of data
  for(int i = 0 ; i < 8 ; i++){
    _spi.write(i);
  }
  _ncs = 1;
  
  wait(1.0);
  
  pc.printf("Reading from SRAM buffer #1\n");
  
  _ncs = 0;
  _spi.write(0xd4); // Buffer 1 write command
  _spi.write(0x0);  // address 0
  _spi.write(0x0);  // address 0
  _spi.write(0x0);  // address 0

  _spi.write(0x0);  // dont care byte
  
  // read 8 bytes of data
  for(int i = 0 ; i < 8 ; i++){
    pc.printf("Data at %d is %d\n",i,_spi.write(0));
  }
  _ncs = 1;




}


It Output :

ID register is 0x1F25
Writing to SRAM buffer #1
Reading from SRAM buffer #1
Data at 0 is 0
Data at 1 is 1
Data at 2 is 0
Data at 3 is 0
Data at 4 is 3
Data at 5 is 4
Data at 6 is 5
Data at 7 is 6
 or output:

ID register is 0x1F25
Writing to SRAM buffer #1
Reading from SRAM buffer #1
Data at 0 is 255
Data at 1 is 255
Data at 2 is 255
Data at 3 is 255
Data at 4 is 255
Data at 5 is 255
Data at 6 is 255
Data at 7 is 255

or anther datas.

what happened about this? It has relate to  SCK??

Thanks a lot!

SHF

 

04 Jun 2009

Hi,

I have copied the code above into my compiler, and when I run the resulting binary, i get :

ID register is 0x1F25
Writing to SRAM buffer #1
Reading from SRAM buffer #1
Data at 0 is 0
Data at 1 is 1
Data at 2 is 2
Data at 3 is 3
Data at 4 is 4
Data at 5 is 5
Data at 6 is 6
Data at 7 is 7

The fact that you can read back the part ID as 0x1F25  (I assume this project is for the AT45DB081D SPI Flash?!?) makes me think that you have wired it up correctly.

When you gect back 255 or other data, what is different? Are you simply pressing reset and re-running the program and getting different results?

Is is possible you have a loose wire or bad connection on MISO, meaning some time the data is not properly recieved?

I  have added the working binary I have to the AT45 page in the cookbook :

http://mbed.co.uk/projects/cookbook/svn/AT45/examples/SRAMAccess.bin

If this doesnt work on your setup, it sounds like it is the hardware....

Cheers,
Chris

 

 

05 Jun 2009

hi,

yes,the project is for the AT45DB081D SPI flash. Because it get back 255 or other data, I simply re-running the program and getting different results,sometimes it can get the right data,but just sometimes.

 

Thanks!

SHF