Philip Asare

52210DEMO and MMA745xL QSPI Problem

Discussion created by Philip Asare on Jul 11, 2009
Latest reply on Jul 13, 2009 by Rich Testardi

Hi,

 

I'm trying to interface the 52210DEMO board with the MMA745xL accelerometer through SPI. I'm trying to read the acceleration values, but it doesn't seem to work right. I've never done this before so there's definitely a chance I'm doing something wrong.

 

Please help

 

Here's the code I'm using:

 

 


#include "support_common.h" /* include peripheral declarations and more */
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include <stdio.h>
#endif

/* Initialize SPI module on system */

void spi_init(void)
{
MCF_QSPI_QMR = 0 |
     MCF_QSPI_QMR_MSTR |   //SPI Mater Mode
     MCF_QSPI_QMR_BAUD(0x0a) | //Baud of 4MHz
     MCF_QSPI_QMR_BITS(0x08); //8 bit data transfer
   
   
/* set direction of QSPI pins _CLK, _DOUT _CS0 as outputs and _DIN as input */
MCF_GPIO_DDRQS = 0 |MCF_GPIO_PORTQS_PORTQS0 |MCF_GPIO_PORTQS_PORTQS2 |MCF_GPIO_PORTQS_PORTQS3;

/* set peripheral access control register to allow read/write access to QPSI module */
MCF_SCM_PACR4 |= MCF_SCM_PACR_ACCESS_CTRL1(0x04);

/* set all delays to default and make sure QSPI module is disabled */
MCF_QSPI_QDLYR = 0;  
MCF_QSPI_QIR = 0xd00f; //set errors
printf("QSPI initiated\n");
}


void spi_transfer(void)
{

#define SPI_READ(reg) (((reg) &0x7F)<<1)
#define SPI_WRITE(reg) ((((reg) &0x3F)<<1)|0x80)

int i=0;
MCF_QSPI_QAR = 0 |MCF_QSPI_QAR_ADDR(0x20);
for(i=0;i<5;i++)
{
/* set command ram for active low and chip select 0 */
  MCF_QSPI_QDR = 0 | MCF_QSPI_QDR_BITSE | MCF_QSPI_QDR_QSPI_CS0;
}

MCF_QSPI_QAR = 0 |MCF_QSPI_QAR_ADDR(0x00);

/* write transmit RAM of QDR */
MCF_QSPI_QDR = MCF_QSPI_QDR_DATA(SPI_WRITE(0x16)); //send write request register $16
MCF_QSPI_QDR = MCF_QSPI_QDR_DATA(0x05); //send 0x05 as data to register $16
MCF_QSPI_QDR = MCF_QSPI_QDR_DATA(SPI_READ(0x06)); //read data from register $06
MCF_QSPI_QDR = MCF_QSPI_QDR_DATA(SPI_READ(0x07)); //read data from register $07
MCF_QSPI_QDR = MCF_QSPI_QDR_DATA(SPI_READ(0x08)); //read data from register $08
MCF_QSPI_QWR = 0x1400; //set begin and end point of transmt RAM
MCF_QSPI_QDLYR = 0 | MCF_QSPI_QDLYR_SPE; //initiate transfer
printf("Initiating Trasnfer\n");
while (!(MCF_QSPI_QIR & MCF_QSPI_QIR_SPIF)) //wait for tranfer to finish
{

}
printf("Transfer Complete\n");
MCF_QSPI_QIR |= MCF_QSPI_QIR_SPIF; //clear SPIF flag
MCF_QSPI_QAR = 0 |MCF_QSPI_QAR_ADDR(0x10); // set QAR register to Receive RAM
}

int main(void)
{
vuint16 accX;
vuint16 accY;
vuint16 accZ;
spi_init();


while( 1 )
{
        // spin forever
        spi_transfer();
        accX = MCF_QSPI_QDR; //read X value
        accY = MCF_QSPI_QDR; //read Y value
        accZ = MCF_QSPI_QDR; //read Z value
        printf("Ax = %d | Ay = %d | Az = %d\n", accX, accY, accZ);
}
}

Outcomes