S3c2440(ARM9) spi_read_write Flash Memory

时光毁灭记忆、已成空白 提交于 2020-01-06 13:04:50

问题


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!!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/30136634/s3c2440arm9-spi-read-write-flash-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!