Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Read and Write to Flash Memory using SPI

Status
Not open for further replies.

Thota Suresh Avionics

Member level 2
Joined
Jul 15, 2013
Messages
45
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Activity points
345
continuing thread https://www.edaboard.com/threads/329948/

PIC24EP512GP806
MPLAB8.8V

Interfacing s25FL032P flash memory with my pic through SPI.

Are there any examples how to read and write data to flash memory using SPI ?


How do I control the mode of SPI for SCK remains at 0 for (CPOL = 0, CPHA = 0 Mode 0)
in PIC SPI register I dont find any ?


so far with some suggestions I am using SPI2 , it is not remappable.
so
Code:
#define PAGESIZE 	256
#define SCK			TRISGbits.TRISG6
#define SDO			TRISGbits.TRISG7
#define SDI			TRISGbits.TRISG8
#define WP			TRISEbits.TRISE5
//#define HOLD		        TRISEbits.TRISE6
#define CS			        TRISEbits.TRISE7

// For Reading Data
#define SPI_READ		0x03		// Read Data Bytes

// For Write Control
#define SPI_WREN		0x60		// Write Enable
#define SPI_WRDIS		0x04		// Write Disable

// For Erase	
#define SPI_P4E			0x20		// 4KB Parameter Sector Erase
#define SPI_P8E			0x40		// 8KB(two 4KB) Parameter Sector Erase
#define SPI_SE			0xD8		// 64KB Sector Erase
#define SPI_BE			0xC7		// Bulk Erase

// For Program	
#define SPI_PP			0x02		// Page Program
#define SPI_QPP		0x32		// Quad Page Program

// For Status and Configuration Register 
#define SPI_RDSR		0x05		// Read Status Register
#define SPI_WRR		0x01		// Write Register
#define SPI_RCR		0x35		// Read Configuration Register
#define SPI_CLSR		0x30		// Reset the Erase and Program Fail Flag

void Config_SPI(void)
{
	SCK 	= 0; // SCK as output
	SDO 	= 0; // SDO as output
	SDI 	= 1; // SDI as input
	WP 		= 0; // WP as O/P
	//HOLD 	= 0; // HOLD as O/P
	CS		= 0; // CHIP SELECT as O/P
}

void Int_SPI(void)
{
       Config_SPI();
       SPI2BUF     = 0;  // clearing Buffer
       SPI2STAT   = 0x0000;  // 
       SPI2CON    = 0x023B;  // 8 bit mode, SPRE = 6(1:2), PPRE = 3(1:1)
       SPI2STATbits.SPIEN		= 1;	// Enable the SPI Module.
       /*
           Fsck = Fcy /(Primary Prescalar * Secondary Prescalar)
           Fsck = 40Mhz.
       */
}
 
Last edited:

can you pls be specific, do you need example or need to check any problem you are facing
If you need example kindly refer to this site
And if you have any problem pls explain in detail to look at.
 

The Microchip MCUs do not use the CPOL and CPHA names for the SPI peripherals. YOu may find the web site https://stevethemoose.blogspot.com.au/2009/04/microchip-pic-spi-cke-and-cpe-vs-cpol.html of use.
I always compare the waveforms in the various data sheets and double-check that the PIC is set up the way the other device expects.
A personal comment: I think you are in for a world of pain by defining the symbols such as SDO and SDI, and in particular CS, to be the TRIS register bits. I will bet someone (possibly you) in a few days/weeks/months/years time will try to toggle the 'CS' symbol to select the slave device and wonder why it doesn't work.
Moving to the SCK frequency: you need to read the data sheet, and in particular the AC electrical characteristics. As you are using SPI2, you need to look at Table 32-41 where you will see that, for SMP=1, CKE = 0 and CKP = 0 (which I think is the setting you are using (BTW setting the whole register like that makes it hard to write in the first place and debug later - comments can very easily be wrong!) the maximum SCK frequency is 10MHz.
You are trying to set the SCK to 20MHz which is twice the valid maximum. Do not expect the SPI peripheral to work with those settings.
Susan
 

I will corrections in my calculations,
but I need to interface S25FL032P, the connections in my board are as given. In data sheet of S25FL032P it is mentioned as 32-Mbit CMOS 3.0 Volt Flash Memory
with 104-MHz SPI (Serial Peripheral Interface) Multi I/O Bus. Range is 40Mhz to 104Mhz. If maximum SCK frequency is 10 Mhz then I will not be able to operate even the Normal Read mode which require a 40Mhz clock.

**broken link removed**

- - - Updated - - -

Please suggest a modification where I can use S25FL032P for reading and writing purpose as the maximum range of SPI data rate/clock is 10 Mhz for the processor that I am using now!
 
Last edited:

Look at the data sheet for the S25FL032P, in particular section 18 ("AC Characteristics") and the timings for 'Fr' and 'Fc' - the minimum SCK frequency is "DC".
From a quick scan of the rest of the data sheet, the only reference I can see to the 40MHz SCK is that it is the maximum clock rate for the 'READ' command and is probably the "typical" clock frequency for many of the operations.
If the MINIMUM SCK frequency of the device really is 40MHz then you will simply not be able to use the PIC24EP512GP806 to drive it. However I do not believe that is the case and the PIC will work (at 10MHz SCK).
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top