hi
the context here is that i'm trying to read and write data to a FM3316 device, which is a FRAM, using 2 SPORT.
timing sequence you can find online. i'll just give a brief intro.
to write ,Sport send 0x02 + 16 bit address + 8bit data
to read ,send 0x03+16bit address+8bit don't care and read from incoming pin.
my code is given below.
/*****************************************************************************
* NewProject.c
*****************************************************************************/
#include <21489.h>
#include <def21489.h>
#include <Cdef21489.h>
#include <sru21489.h>
#include <signal.h>
#include <stdio.h>
#define FRAM_READ_COMMAND (0x03<<24)
#define FRAM_WRITE_COMMAND (0x02<<24)
#define FRAM_WRITE_ENABLE (0x06<<24)
#define FRAM_CLOCK_DIV (49)
#define FRAM_FRAME_BITWIDTH (33)
#define SPCTL0_SET ( \
SPEN_A | \
/* DTYPE0 | */ \
/* DTYPE1 | */ \
/* DTYPE2 | */ \
/* DTYPE3 | */ \
/* LSBF | */ \
/* SLEN3 | */ \
/* SLEN4 | */ \
/* SLEN5 | */ \
/* SLEN6 | */ \
/* SLEN7 | */ \
/* SLEN8 | */ \
/* SLEN9 | */ \
/* SLEN10 | */ \
/* SLEN11 | */ \
/* SLEN12 | */ \
/* SLEN13 | */ \
/* SLEN14 | */ \
/* SLEN15 | */ \
/* SLEN16 | */ \
/* SLEN17 | */ \
/* SLEN18 | */ \
/* SLEN19 | */ \
/* SLEN20 | */ \
/* SLEN21 | */ \
/* SLEN22 | */ \
/* SLEN23 | */ \
/* SLEN24 | */ \
/* SLEN25 | */ \
/* SLEN26 | */ \
/* SLEN27 | */ \
/* SLEN28 | */ \
/* SLEN29 | */ \
/* SLEN30 | */ \
/* SLEN31 | */ \
SLEN32 | \
/* PACK | */ \
/* MSTR | */ \
ICLK | \
/* OPMODE | */ \
CKRE | \
FSR | \
IFS | \
/* IMFS | */ \
/* IRFS | */ \
/* DITFS | */ \
/* DIFS | */ \
/* LFS | */ \
/* LRFS | */ \
/* LTDV | */ \
/* LMFS | */ \
/* L_FIRST | */ \
/* LAFS | */ \
/* SDEN_A | */ \
/* SCHEN_A | */ \
/* SDEN_B | */ \
/* SCHEN_B | */ \
/* FS_BOTH | */ \
/* BHD | */ \
/* SPEN_B | */ \
SPTRAN | \
/* DERR_B | */ \
/* TUVF_B | */ \
/* DXS0_B | */ \
/* DXS1_B | */ \
/* TXS0_B | */ \
/* TXS1_B | */ \
/* DERR_A | */ \
/* TUVF_A | */ \
/* ROVF_A | */ \
/* DXS0_A | */ \
/* DXS1_A | */ \
/* RXS0_A | */ \
/* RXS1_A | */ \
/* TXS0_A | */ \
/* TXS1_A | */ \
0 )
static void SRU_DA(void)
{
SRU(SPORT1_CLK_PBEN_O,DAI_PBEN06_I);
SRU(SPORT1_CLK_O,DAI_PB06_I);
SRU(SPORT1_DA_PBEN_O,DAI_PBEN05_I);
SRU(SPORT1_DA_O,DAI_PB05_I);
SRU(SPORT1_FS_PBEN_O,DAI_PBEN04_I);
SRU(SPORT1_FS_O,DAI_PB04_I);
SRU(LOW,DAI_PBEN07_I);
// SRU (SPORT1_CLK_O, SPORT2_CLK_I);
// SRU (SPORT1_FS_O, SPORT2_FS_I);
SRU (DAI_PB07_O, SPORT2_DA_I);
SRU (SPORT2_CLK_O, SPORT2_CLK_I);
SRU (SPORT2_FS_O, SPORT2_FS_I);
//SRU()
// SRU(HIGH, DAI_PBEN18_I);
}
static SPI_Init(void)
{
int clkdiv,fsdiv;
//Clear out SPORT 0 registers
*pSPCTL1 = 0;
//Fastest serial clock speed is fine for the minimum timings
clkdiv= FRAM_CLOCK_DIV;
fsdiv = FRAM_FRAME_BITWIDTH;
// Divisor register for FS and CLK
*pDIV1 = (fsdiv<<16)|(clkdiv<<1);
*pSPCTL1 |= SPCTL0_SET; //SP1 as transmitting master
*pSPCTL2 = 0;
*pDIV2 = (fsdiv<<16)|(clkdiv<<1);
*pSPCTL2 |= SPCTL0_SET;
*pSPCTL2 &= ~(SPTRAN);//~(ICLK | IFS | SPTRAN);
//*pSPCTL2 &= ~CKRE; //SP2 as receiving slave
// *pSPCTL1 &= ~CKRE;
}
void SendMessageSPIA(int Data)
{
while((*pSPCTL1 & (DXS0_A |DXS1_A))!= 0);
*pTXSP1A = Data;
}
void SendData(unsigned int Data,unsigned int Address)
{
int i = FRAM_WRITE_COMMAND | Address<<8 | Data;
SendMessageSPIA(FRAM_WRITE_ENABLE);
SendMessageSPIA(i);
i = *pRXSP2A;
i = *pRXSP2A;
}
int ReadData(int address)
{
int i = FRAM_READ_COMMAND | address<<8;
SendMessageSPIA(i);
//while((*pSPCTL1 & (RXS0_A |RXS1_A))!= 0);
i = *pRXSP2A;
return i;
}
void ADI_FRAM_Write(unsigned int Data,unsigned int Address)
{
unsigned int i,tempdata = 0;
unsigned int mask = 0xff;
for(i = 0;i<4;i++)
{
tempdata = (Data & mask)>>(i<<3);
SendData(tempdata,(Address<<2)+i);
mask = mask<<8;
}
}
unsigned int ADI_FRAM_Read(unsigned int Address)
{
unsigned int tempdata,i,result=0;
for(i=0;i<4;i++)
{
tempdata = ReadData((Address<<2)+i)&0xFF;
tempdata = tempdata << (i<<3);
result += tempdata;
}
return result;
}
int main(void)
{
SRU_DA();
SPI_Init();
unsigned int databack,i;
ADI_FRAM_Write(0x12341234,0);
printf("0x%x\n",databack); //this cause write to fail
databack = ADI_FRAM_Read(0);
printf("0x%x\n",databack);
}
now in the last part, the line marked red, if it is there the the write will fail and some random data will be fed to the outgoing line.
now i've tried several instructions and only the printf() can cause this.
i'm not sure if this is a bug or something i did wrong.is there a solution for this problem?
bty i‘m using vdsp++ 5.0 up 10