0

I am working on SPI communication.Trying to communicate SST25VF032B(32 MB microchip SPI Flash). When I am reading the Manufacturer Id it shows MF_ID =>4A25BF but originally it is MF_ID =>BF254A. I am getting it simply reverse, means first bite in 3rd and 3rd byte in first.

What could be the possible reason for that? My SPI Init function is here:

//Enable clock control register CLKCON 18 Bit enables SPI  
CLKCON |= (0x01 << 18);//0x40000;  
printk("s3c2440_clkcon=%08ld\n",CLKCON);
//Enable GPG2 Corresponding NSS port
GPGCON =0x1011;//010000 00 01 00 01
printk("s3c2440_GPGCON=%08ld\n",GPGCON);
SPNSS0_ENABLE(); 
//Enable GPE 11,12,13,Corresponding MISO0,MOSI0,SCK0 = 11 0x0000FC00
GPECON &= ~((3 << 22) | (3 << 24) | (3 << 26));  
GPECON |=  ((2 << 22) | (2 << 24) | (2 << 26));  
//GPEUP Set; all disable  
GPGUP &= ~(0x07 << 2);    
GPEUP |=  (0x07 << 11);
//SPI Register section
//SPI Prescaler register settings,  
//Baud Rate=PCLK/2/(Prescaler value+1)  
SPPRE0 = 0x18;       //freq = 1M 
printk("SPPRE0=%02X\n",SPPRE0);  

//polling,en-sck,master,low,format A,nomal = 0 | TAGD = 1  
SPCON0 = (0<<5)|(1<<4)|(1<<3)|(0<<2)|(0<<1)|(0<<0);  
printk("SPCON1=%02ld\n",SPCON0);  

//Multi-host error detection is enabled 
SPPIN0 = (0 << 2) | (1 << 1) | (0 << 0);  
printk("SPPIN1=%02X\n",SPPIN0);  

//Initialization procedure
SPTDAT0 = 0xff;

My spi_read_write function as follows:

static char spi_read_write (unsigned char outb) 
{
   // Write and Read a byte on SPI interface.  

   int j = 0;  
   unsigned char inb;
   SPTDAT0 = outb;  
   while(!SPI_TXRX_READY) for(j = 0; j < 0xFF; j++);  
   SPTDAT0 = outb;
   //SPTDAT0 = 0xff;  
   while(!SPI_TXRX_READY) for(j = 0; j < 0xFF; j++);   
   inb = SPRDAT0; 
   return (inb);   
}

My Calling function is:

   MEM_1_CS(0);
   spi_read_write(0x9F);
   m1 = spi_read_write(0x00);
   m2 = spi_read_write(0x00);
   m3 = spi_read_write(0x00);
   MEM_1_CS(1); 
   printk("\n\rMF_ID =>%02X-%02X-%02X",m1,m2,m3); 

Please guide me what to do? Thanks in Advance!!

RFK
  • 83
  • 11

2 Answers2

0

There's no apparent problem with the SPI function. The problem is with your printing function. Arm is little endian processor. it keeps the bytes reversed in memory. You need to print it reverse order and you'll be fine.

stdcall
  • 27,613
  • 18
  • 81
  • 125
  • My spi_read_write function is unsigned char & I keep the return value in three different variable. so, it should print first value first which return from spi_read_write function. Suppose, my function return integer and I am getting 0x11223344 insted of 0x44332211 that can be explained by `little endian` or `big endian`. – RFK May 09 '15 at 07:14
  • I used previous controller that is Cortex-M3. I got MF_ID =>BF254A there.My calling function is same as previous, only `spi_read_write` and `spi_init` function are different. But, here I am getting MF_ID =>4A25BF. I mean sequence of command are same as previous then why I am getting it in reverse order? – RFK May 09 '15 at 07:19
0

I was banging my head on this from last couple of days and finally I find the solution. All I needed to change my spi_read_write function as follows.

static char spi_read_write (unsigned char outb) 
{
   int j = 0;  
   unsigned char inb;   
   while(!SPI_TXRX_READY) for(j = 0; j < 0xFF; j++); 
   SPTDAT0 = outb;   
   while(!SPI_TXRX_READY) for(j = 0; j < 0xFF; j++);   
   inb = SPRDAT0;
   return (inb);       
}

CHANGES MADE:

First of all we have to check whether the SPI_TXRX_READY then fill the register with the value SPTDAT0 = outb;.

Thanks all for your kind support.

RFK
  • 83
  • 11
  • This doesn't make sense. This couldn't cause you're bytes read the other way around. – stdcall May 09 '15 at 17:28
  • but in datasheet says you have to fill `SPTDAT0 = 0xff;` before reading the SPI Register. Anyway thanks for your comment. There is also dummy read concept what is that? – RFK May 14 '15 at 14:00