SPI configuration problem

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

SPI configuration problem

4,652 次查看
Argus
Contributor I
Hi, first allow me to apologize if my english is not really good, it is not my native language.
I am trying to use the SPI in my MC68HC908GP32CP, I used the processor expert SPI bean initialization
Here is the init code that it generates:
void SPI1_Init(void)
{
  /* SPCR: SPRIE=0,??=0,SPMSTR=0,CPOL=0,CPHA=0,SPWOM=0,SPE=0,SPTIE=0 */
  setReg8(SPCR, 0x00);                 /* Disable the SPI module */
  (void)getReg8(SPSCR);                /* Dummy read of the SPSCR registr to clear flags */
  (void)getReg8(SPDR);                 /* Dummy read of the SPDR registr to clear flags */
  /* SPSCR: SPRF=0,ERRIE=0,OVRF=0,MODF=0,SPTE=0,MODFEN=0,SPR1=1,SPR0=1 */
  setReg8(SPSCR, 0x03);                
  /* SPCR: SPRIE=0,??=0,SPMSTR=1,CPOL=1,CPHA=1,SPWOM=0,SPE=0,SPTIE=0 */
  setReg8(SPCR, 0x38);                 
}

 My problems start here, when I try to do the simulation, it show me an error in the code line
(void)getReg8(SPDR);                 /* Dummy read of the SPDR registr to clear flags */

The error said :
Error: At location 0012 -
Error: Attempt to use invalid or uninitialized memory, i don't know what to do.

I am posting all the code that I am using to comunicate with the slave device, it is a MMA745xL Digital Accelerometer, almost all the code here I copied from the application note 3468 (AN3468), because this is my first try with SPI and this kind of devices.
Thank you, I will really appreciate any help I can get.
PD. I already try searching on the forum for any problem similar to mine, but didn't find any.
Andres

/** ###################################################################
**     Filename  : Project_9.C
**     Project   : Project_9
**     Processor : MC68HC908GP32CP
**     Version   : Driver 01.15
**     Compiler  : CodeWarrior HC08 C Compiler
**     Date/Time : 25/10/2008, 12:18 a.m.
**     Abstract  :
**         Main module.
**         This module contains user's application code.
**     Settings  :
**     Contents  :
**         No public methods
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2008
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/
/* MODULE Project_9 */


/* Including used modules for compiling procedure */
#include "Cpu.h"
#include "Events.h"
#include "SPI1.h"
#include "AS1.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
byte Xdata,Ydata,Zdata,CTLReg,X;
void spi_write(byte reg, byte data);
byte spi_read(byte reg);
void main(void)
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/
/*** End of Processor Expert internal initialization.                    ***/
 
 

  PTC_PTC4=1;
  DDRC_DDRC4=1;
  Xdata=0;
  CTLReg=0;
  spi_write(0x16,0x05); //4 Wire Mode Set up in the Accelerometer
  CTLReg=spi_read(0x16);
  while(AS1_SendChar(CTLReg)!=ERR_OK){}

  /* Write your code here */
  /* For example: for(;; ) { } */
 
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;; ){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END Project_9 */
/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 3.03 [04.07]
**     for the Freescale HC08 series of microcontrollers.
**
** ###################################################################
*/

// Function Definitions for Read and Write
byte spi_read(byte reg){
byte x;
PTC_PTC4=0;
x=SPSCR;
x=SPDR;
while (!SPSCR_SPTE);
SPDR= ((reg &0x3F)<<1); // write in the register address with the read command
while(!SPSCR_SPRF){} //wait for transfer
x=SPDR;
SPDR = 0x00; // send 2nd byte
while (!SPSCR_SPRF){} // wait transfer done
x = SPDR;
PTC_PTC4=1;
return (x);
}


//Write function SPI 3 Wire or SPI 4 Wire Mode
void spi_write(byte reg, byte data){
byte x;
PTC_PTC4=0;
SPDR=(((reg &0x3F)<<1)|0x80);
while (!SPSCR_SPRF){} //wait for transmission complete
x=SPDR; //dummy read
SPDR=data;
while (!SPSCR_SPRF){} //wait for transmission complete
x=SPDR;
PTC_PTC4=1;
}
 
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/



标签 (1)
0 项奖励
回复
7 回复数

1,173 次查看
Argus
Contributor I
Well, I have finally been able to solve the problem of communication problem with the acelerometer, I solved by putting  a 0.1ms delay just after I activate the device by putting de CS to 0, I tought about this when Mac told me about de 1ms delay after power up of the device.

But I have just one more question, I know that to be able to receive data from the slave device, the master device has to sent a byte, I am sending 0x00 just like application note AN3468 suggested, but I read somewhere else, that it can be done by sending 0xFF, so I tried this, but in each case i receive different data, I wonder if somebody could tell me about it.

Thank you
Andres
0 项奖励
回复

1,173 次查看
peg
Senior Contributor IV
Hi Andres,

As long as you are using seperate MOSI/MISO connections it does not matter what you send from the master in order to retrieve the response from the slave. What you must be careful of is that the slave does not try to interpret these bytes as part of the previous command or the start of a new one. This maybe slave device specific.
The reason you need to send something is to generate the clock that will clock the response back into the master.
0 项奖励
回复

1,173 次查看
Argus
Contributor I
Peg:
What exactly doesn't work?
I wasn't able to simulate the code but finally i was able to.

Do you have a scope on the SPI lines?
No, right now I am not able to scope on the spi lines, but I will on Monday.

Is the SPI mode correct for the device at the other end?

I dont exactly know what do you want to said with this, the other end device is an accelerometer MMA7455 , I change the configuration according to  what Mac said, but I am still unable to communicate with it.

In your post you also talk about AsynchroSerial, this is for the SCI isn't it?
Yes I was refering to the SCI.


You can do a test by tying MISO high and see if you receive 0xFF and then low and see if you get0x00.
I didn't thought of that test, so I did it, and this way I was able to use the spi, so if I put an 0xFF in the MISO, I was able to read it in hyperterminal, and the same for the 0x00

But I am still unable to communicate to the accelerometer.

Mac:
Thank you about the 1 millisecond stabilisation period after power-up I didn't know about it, or maybe didn't pay attention to it while I was reading.
I  changed CPOL=0 and CPHA=0, but I can get to work yet.

Thank you, I really appreciate the help, I will keep trying to see if I can get it to work.
0 项奖励
回复

1,173 次查看
erooll
Contributor II
Don't worry about this message, this instruction is used to clear some flags in the module, try to set manually the value of this register through window of memory map, just before to run this line of code; really this error not pass if you run in the physically microcontroller
0 项奖励
回复

1,173 次查看
Argus
Contributor I
Hi erooll, I did that, and its ok to skip that problem, but what happens is that I can't access de SPDR, so in the code for example when I have to do SPDR=(((reg &0x3F)<<1)|0x80); it wont write on it. So is just like if I have to set the value in the memory map.
I programmed the HC08 with the code I tought that if it really was a problem it wouldn't do any thing, so I just programmed the initialization of the SPI, and try to send a char with the AsynchroSerial, and it did work, but then I put the complete code to try to read from the accelerometer and nothing happened.
I will really appreciate any help on this.
0 项奖励
回复

1,173 次查看
peg
Senior Contributor IV
Hi Argus,

What exactly doesn't work?
Do you have a scope on the SPI lines?
Is the SPI mode correct for the device at the other end?
In your post you also talk about AsynchroSerial, this is for the SCI isn't it?
You can do a test by tying MISO high and see if you receive 0xFF and then low and see if you get0x00.

0 项奖励
回复

1,173 次查看
bigmac
Specialist III
Hello,
 
It would seem that the accelerometer device requires SPI mode of CPOL = 0, CPHA = 0.  This differs from your SPI initialisation code.
 
I also note that the device requires minimum 1 millisecond stabilisation period after power-up.  Is this condition met before writing to and reading from the device?
 
Regards,
Mac
 
0 项奖励
回复