SPI problem - MC9S08AW60

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SPI problem - MC9S08AW60

4,709 Views
alive
Contributor I
hi everybody,
 
i am now trying to commnicate the microcontroller, MC9S08Aw60, with the external flash memory chip, M25PE16 using SPI interface. I am getting so desperate with it. I can transmit data but no receive at all. My SPI_SRF never gets set. My code is like this:
bye SendReceive_data(byte data)
{
  SPID=data;
  while (!SPIS_SPRF);
  data=SPID;
  return data;
}
Pls fix my program and any suggestion is much appreciated. pls reply me ASAP. I attached my program.
Do I need to put some delays and the way I use to reset the chip is correct or not?
Regards,
mizu
 
 
Added p/n to subject.


Message Edited by NLFSJ on 2008-07-24 07:25 AM
Labels (1)
0 Kudos
Reply
10 Replies

1,342 Views
bigmac
Specialist III
Hello mizu,
 
I suspect your problem might be because you do not seem to have an initial read of the SPIS register, as described in the following thread -
 
The initial read may occur within the SPI initialisation, or alternatively within your SPI data transfer function, that might be modified as follows.
 
byte SendReceive_data(byte data)
{
  (void)SPIS;
  SPID = data;
  while (!SPIS_SPRF);
  return SPID;
}
 
Regards,
Mac
 
0 Kudos
Reply

1,342 Views
alive
Contributor I
Hi Mac,
 
           I found that my codes do not work. Nothing is received. I think something's wrong. Could you find out more for me? I have not much time left. Even if I dont check the transmit buffer, I cannot transmit the data I like to. Instead, I can find only 0x19 on the transmit buffer whatever I send. When I check the transmit buffer before I transmit, I can transmit the data I want to. But cannot still receive. When I debug this function, it stops at the third line. It means that read buffer never set to 1.
Now I try like this>>>
 
byte SendReceive_data(byte data)
{
  while(SPI1S_SPTEF==0)__RESET_WATCHDOG();
  SPI1D=data;
  while (!SPI1S_SPRF);
  return SPI1D;
}
 
Pls reply me ASAP. I feel desperate about it.
 
With Regards,
mizu
 
 
0 Kudos
Reply

1,342 Views
bigmac
Specialist III
Hello mizu,
 
Are you are attempting to display the values of SPI1S and SPI1D within a debugger window?  If so, it is likely that this process will clear the SPRF flag before the code can sense that the flag is set.  See whether the function will complete when the registers are not monitored, and without single-stepping through the function.
 
I also think there may be some misunderstanding about the process of reading the SPI1D register.  When a value is written to the register, this is the value that is sent by the SPI module.  However, when the same register is read, the value will always be the returned receive value, as a result of the previous send.
 
For the serial flash device, after sending the command byte and the address bytes, you will likely need to send dummy byte(s) to read the contents.
 
Regards,
Mac
 
0 Kudos
Reply

1,342 Views
alive
Contributor I
Hi Mac,
 
               Thanks for your reply.  The problem cannot be solved yet. My way is like this. I send the instruction byte first and then I should receive some bytes as the result of the previous send. Then I send the dummy bytes(0xff) to receive the read chip ID which I want. Am I right? But No data received entirely. What am I wrong? Pls check my program again. And pls show me the way. I am losing my way.
 
 
 
With Regards,
mizu
0 Kudos
Reply

1,342 Views
peg
Senior Contributor IV
Hello mizu,

I think you need to make your problem a little clearer for us.

With SPI, you ALWAYS recieve something when you send something and also you must send something in order to recieve something. This is Rule No.1 and the one most newbies trip up on.

Even if you recieve something that is not what you expect you still recieve something, 00 and FF will occur when the MISO is stuck high or low but this is exactly what happens when all is working perfectly for these two bytes.

Mac has already covered one reason why you may not be getting proper indication that you have recieved something.

0 Kudos
Reply

1,342 Views
alive
Contributor I
Hi peg,
 
 
           I'm sorry I was not clear to mention my problem. Per your speaking that I always receive something when I send something, I receive only 0x00 whatever I send. If so, the MISO seems always stuck at low, does it? That is my point. And then, how can i get the correct data............................ When I test with the scope, I found that the voltage just rises up from 0 V to 0.8V on MISO channel.
 
 Pls reply me ASAP. Thanks a lot for supporting me.
I went through the documents and the forum. My understanding is the same. I cannot get the new idea.  I cannot solve this yet.
 
With Regards,
Mizu
0 Kudos
Reply

1,342 Views
bigmac
Specialist III
Hello Mizu,
 
Does the SendReceive_data() function now successfully exit?  Assuming this is so, another possible problem with your code is that within the SPI_flash_init() function you have the following line
SPI_CHIP_SELECT = LOW;
This is initialising the chip to an active state, rather than inactive.
 
Finally, if you think that there may be a hardware problem, connect an external pullup resistor to the MISO line.  You should then see the line at a high state whenever SS is inactive.  The  resistor is probably a good idea anyway, to ensure that the line is not at an intermediate voltage level when the flash device is inactive.
 
Regards,
Mac
 


Message Edited by bigmac on 2008-07-22 09:06 PM
0 Kudos
Reply

1,342 Views
alive
Contributor I
Hi Mac,
 
             Could you help me pls find out the problem source? I cannot troubleshoot my circuit board yet. I used the external pullup resistor to MISO pin. But still cannot receive the correct code. I found all connections ok. When I connect with MISO and MOSI directly, it is ok. Sending and Receiving are the same. But with the memory chip , I receive only 0x00 or 0xff. Then I reset the memory chip. But nothing changed. I use the microcontroller board which has no external oscillator and so I use the internal clock generator. Although I changed the chip, it remains the same. Pls reply me ASAP. I have to solve this problem.
 
 
With Regards,
mizu
0 Kudos
Reply

1,342 Views
alive
Contributor I
Hi ,
 
          Special thanks to everybody! Now I got the correct id of the chip :smileyvery-happy: But I used another board.  My code is Ok. Now I am getting back to my old board and troubleshooting it. I cannot find out yet. I use external pull up resistor to MISO. This time it just sending 0xff. Still stuck at 1.
 
With Regards,
mizu
 
 
0 Kudos
Reply

1,342 Views
Denn
Contributor I
Hello,
 
Have you solved this?
0 Kudos
Reply