EmbeddedRelated.com
Forums

SPI code in trouble

Started by el_lopo06 July 11, 2009
Hi,

I try to use a LPC2148 with a ADIS16209 inclinometer.

I try to get some codes and essentially the app . note from NXP
(AN10406).

The ADIS is wired like this :

LPC ADIS
SCK1 (P0.17) SCLK
SSEL1(P0.20) /CS
MISO1 (P0.18) DOUT
MOSI1 (P0.19) DIN

The Code I use is the following, I use a Serial monitor to receive infos
from the LPC and see what happen, but dummy data (0x00) is sent.

Can someone tell me what is wrong ?

PS :(I spent many time to test before posting, and used a JTAG to try to
debug)
int main (void)
{
PINSEL0 = 0;
PINSEL1 = 0;
PINSEL2 &= 0x0000000C;
PINSEL2 |= 0x00000030;
DelayProc(0.2 * CCLOCK);
UART0_init(38400/*bps*/, 15000/*kHz*/, length_8_bit, stop_bit_1,
parity_disable, parity_odd);
SPI_Init();
while(1)
{
ADIS_ReadStatus() ;
ADIS_ReadXincl();
DelayProc(0.2 * CCLOCK);
}

-----

#include /* LPC214x definitions */

#include "spi_fonx.h"
#include "spi_adis16209.h"

#include "Serial.h" // For debug only

/* Send a byte cmd to ADIS */
WORD ADIS_Write( BYTE reg )
{
BYTE dataMSB, dataLSB ;
long tempo ;
WORD Return_mask = 0 ;

SPI_SendByte( reg);
SPI_SendByte( 0x00);
for (tempo = 0; tempo< 1000000;tempo++); // bad 100 uS wait

dataMSB = SPI_ReceiveByte();
*Message2= dataMSB ;
UART0_sendchar (dataMSB); //send debug to UART0
dataLSB = SPI_ReceiveByte();
*Message2= dataLSB ;
UART0_sendchar (dataLSB); //send debug to UART0
Return_mask = dataMSB | dataLSB ; // MSB + LSB concatenation
return(Return_mask);
}

-----------------

/*----------------------------------
* Name: SPI_Fonx.c
* Purpose: SPI interface driver functions
* Version: V1.0
*
*---------------------------------*/

#include /* LPC214x definitions */
#include "spi_fonx.h" /* SPI functions header */

/* SPI_Init

Configure PIN connect block :

- bit 32, 54, 76 are 0x10, bit 98 are 0x00
- port 0 bits : 17, 18, 19, 20 ==> are SSP port SCK1, MISO1, MOSI1
and SSEL1 set SSEL to GPIO pin that you will have the total
freedom to set/reset the SPI chip-select pin

CPOL and CPHA are set to 1 for the ADIS16209
*---------------------------------*/

#include "Serial.h" // For debug only

void SPI_Init( void )
{
DWORD portConfig;
BYTE i, Dummy;

SSPCR1 = 0x00; /* SSP master (off) in
normal mode */

// UART0_sendstring ("Dans spiiniT");
portConfig = PINSEL1;
PINSEL1 = 0x00A8 ; /* cf comments upward */
IODIR0 = SPI_SEL; /* SSEL is output (GPIO
P0.20) */
// IOSET0 = SPI_SEL; /* set SSEL to high */

VPBDIV = 0x02; /* Set PCLK 1/2 of CCLK */

/* Set data to 8-bit, Frame format SPI, CPOL = 1, CPHA = 1, and SCR is
15 */
SSPCR0 = 0x07C7;

/* SSPCPSR clock prescale register, master mode, minimum divisor is
0x02*/
SSPCPSR = 0x2;

/* Device select as master, SSP Enabled, normal operational mode */
SSPCR1 = 0x02;

for ( i = 0; i < 8; i++ )
{
Dummy = SSPDR; /* clear the RxFIFO */
}
return;

}

/* SPI Send a block of data based on the length */

void SPI_SendByte( BYTE data )
{
BYTE Dummy;

/* as long as TNF bit is set, TxFIFO is not full, I can write */
while ( !(SSPSR & 0x02) );
SSPDR = data;
/* Wait until the Busy bit is cleared */
while ( !(SSPSR & 0x04) );
Dummy = SSPDR; /* Flush the RxFIFO */
}

/* SPI receives a block of data based on the length */

void SPI_Receive( BYTE *buf, DWORD Length )
{
DWORD i;

for ( i = 0; i < Length; i++ )
{
*buf = SPI_ReceiveByte();
buf++;
}
return;
}

/* SPI Receive Byte, receive one byte only, return Data byte
* used a lot to check the status.
*/

BYTE SPI_ReceiveByte( void )
{
BYTE data;

/* wrtie dummy byte out to generate clock, then read data from MISO
*/
SSPDR = 0xFF;

while ( SSPSR & 0x10 ); /* Wait until the Busy bit is cleared */
data = SSPDR;
DelayProc(0.2 * 12000000);
return ( data );
}
WORD ADIS_ReadStatus(void)
{
WORD retour = 0 ;
retour = ADIS_Write(ADIS_STATUS);
return(retour);
}

WORD ADIS_ReadXincl(void)
{
WORD retour = 0 ;
retour = ADIS_Write(ADIS_XINCL_OUT);
return (retour);
}


An Engineer's Guide to the LPC2100 Series