Mike James

MC9S08QG8 SPI Problem

Discussion created by Mike James on Jan 17, 2007
Latest reply on Jan 18, 2007 by bigmac
I'm developing microcode for the MC9S08QG8 to act as a SPI slave device. I developed an extremely simple test application to test the robustness of my SPI communications (e.g. to see if communications break down at a particular baud rate). The test involves a MC9S08QG8 acting as a master and a MC9S08QG8 acting as a slave - connected appropriately.

Note that SSOE=1 and MODFEN=1, so /SS details are handled by the SPI module.

************************Problem Master Code******************************void setSPID( byte value ) {while ( !SPIS_SPTEF );SPID = value;}byte getSPID() {while ( !SPIS_SPRF );return SPID;}void main() {// setup for SCI here for serial port communication// setup SPI here; interrupts off// clear out anything in the slave bufferssetSPID( 0xff );getSPID();setSPID( 0xff );getSPID();setSPID( 0xff );getSPID();setSPID( 0x0a );getSPID();while ( true ) {byte i, b;do {SendMsg( "X" ); // SCI communicationsetSPID( 0x0a );b = getSPID(); // should always be a value between 1 and 6} while ( b == 0 );setSPID( 0x0 );b = getSPID(); // should always be a value between 1 and 6for ( i = 0; i != b; i++ ) {if ( i+1 == b )setSPID( 0xa );elsesetSPID( 0x0 );showVal( getSPID() ); // Should ALWAYS be 0. SCI communication.}// for pretty printingv[ 0 ] = '\r'; // SCISendMsg( v ); // SCIv[ 0 ] = '\n'; // SCISendMsg( v ); // SCI}}


************************Slave Code******************************void main() {byte z1=0;while ( 1 ) {byte val;WATCHDOG;val = getSPID();if ( val == 0xff ) continue; // ignore 0xff'sif ( val == 0x0 ) {setSPID( 0x0 );} else if ( val == 0xa ) {z1 = (z1 + 1) % 6;setSPID( z1 + 1 );} else {setSPID( val ); // this line never executes as a part of the test}} /* loop forever */}



To run the test, the master and slave are hooked together and a laptop is connected to th e serial port of the master. Hyperterminal is used on the laptop to display the SCI output from master. The master and slave should communicate back and forth indefinitely and the master should send SCI communication like:

What I found is that if I let this run for a few minutes, the master and slave get out of synch resulting in SCI communications like

The problem is that the master and slave are getting out of synch. The way the code is written, this should obviously never happen. What I eventually found was that when I altered the master's getSPID and setSPID to include a very small delay, the test no longer gets out of synch (even after hours).

**************************New Master getSID and setSPID*************void setSPID( byte value ) {byte x;while (!SPIS_SPTEF );SPID = value;// MINOR DELAYx = 0;do {x++;}while ( x != 20 );}byte getSPID() {byte x;while ( !SPIS_SPRF );// MINOR DELAYx = 0;do {x++;}while ( x != 20 );return SPID;}


This is obviously very concerning and I can find no reason in the documentation to expect that such a delay should be necessary. It happens at even the slowest baud rate. Any insight someone could provide would be greatly appreciated!

Message Edited by Alban on 2007-01-17 09:09 PM