Sensorian  1.0
C API Reference Guide Library
S25FL204K.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
28 #include "S25FL204K.h"
29 #include "SPI.h"
30 
34 
39 void S25FL_Setup(void)
40 {
41  CE_OUTPUT(); //Set CE0 pin as output
42  CE_DESELECT(); //Deselect CE0 pin
43 }
44 
51 void S25FL_WriteByte(unsigned char data, unsigned long address)
52 {
53  CE_SELECT(); //Enable device
54  SPI_Write(CMD_WREN); //Send WREN command
55  CE_DESELECT(); //Disable device
56 
57  CE_SELECT(); //Start SPI communication
58  SPI_Write(PAGE_PROGRAM); //Program page
59  SPI_Write((unsigned char)(address >> 16) & 0x000000FF); //Send 3 address bytes
60  SPI_Write((unsigned char)(address >> 8) & 0x000000FF);
61  SPI_Write((unsigned char)(address & 0x000000FF));
62  SPI_Write(data);
63  CE_DESELECT(); //Disable device
64 
65  while ((S25FL_SReg_Read(CMD_RDSR1) & 0x01) == 0x01) //Waste time until not busy
66  {
67 
68  }
69 }
70 
76 unsigned char S25FL_ReadByte(unsigned long address)
77 {
78  unsigned char mybyte = 0;
79  CE_SELECT(); //Enable device
80  SPI_Write(CMD_READ_DATA); //Read command,Send 3 address bytes
81  SPI_Write((unsigned char)(address >> 16) & 0x000000FF);
82  SPI_Write((unsigned char)(address >> 8) & 0x000000FF);
83  SPI_Write((unsigned char)(address & 0x000000FF));
84  mybyte = SPI_Read();
85  CE_DESELECT(); //Disable device
86  return mybyte; //Return one byte read
87 }
88 
97 void S25FL_WriteArray(unsigned long address, unsigned char* pData, unsigned int arrayLength)
98 {
99  unsigned int i = 0;
100  CE_SELECT(); //Enable device
101  SPI_Write(CMD_WREN); //Send WREN command
102  CE_DESELECT(); //Disable device
103 
104  CE_SELECT(); //Enable device
105  SPI_Write(PAGE_PROGRAM); //Send Byte Program command
106  SPI_Write((unsigned char)(address >> 16) & 0xFF);
107  SPI_Write((unsigned char)(address >> 8) & 0xFF);
108  SPI_Write(0x00);
109 
110  for (i=0;i<arrayLength;i++)
111  {
112  SPI_Write(pData[i]); //Send byte to be programmed
113  }
114  CE_DESELECT(); //Disable device
115 
116  //Wait Busy
117  while ((S25FL_SReg_Read(CMD_RDSR1) & 0x01) == 0x01) //Waste time until not busy
118  {
119  }
120 }
121 
129 void S25FL_ReadArray(unsigned long address, unsigned char* pData, unsigned int arrayLength)
130 {
131  unsigned int i = 0;
132 
133  CE_SELECT(); //Enable device
134  SPI_Write(CMD_READ_DATA); //Read command
135  SPI_Write((unsigned char)(address >> 16));
136  SPI_Write((unsigned char)(address >> 8));
137  SPI_Write((unsigned char) address);
138 
139  for (i = 0; i <arrayLength; i++) //Read until no_bytes is reached
140  {
141  pData[i] = SPI_Read(); //Receive bytes
142  }
143  CE_DESELECT(); //Disable device
144 
145  //Wait Busy
146  while (S25FL_SReg_Read(CMD_RDSR1) & 0x01) //Waste time until not busy
147  {
148  }
149 }
150 
156 unsigned int S25FL_ReadID(void)
157 {
158  unsigned int deviceID = 0;
159  unsigned char man, dev = 0;
160  CE_SELECT(); //Start SPI communication
161  SPI_Write(CMD_RDID); //Send read ID command (90h or ABh)
162  SPI_Write(0x00); //Send dummy byte
163  SPI_Write(0x00); //Send dummy byte
164  SPI_Write(0x00); //Send 0x00
165  man = SPI_Read(); //Send either manufacturer ID or device ID
166  dev = SPI_Read();
167  deviceID = (man << 8) | dev; //Receive byte
168  CE_DESELECT(); //Disable device
169  return deviceID;
170 }
171 
177 unsigned long S25FL_Jedec_ID_Read(void)
178 {
179  unsigned long int jedid = 0;
180  unsigned char byte1, byte2, byte3 = 0;
181  CE_SELECT(); //Enable device
182  SPI_Write(JEDEC); //Send JEDEC ID command (9Fh)
183  byte1 = SPI_Read();
184  byte2 = SPI_Read();
185  byte3 = SPI_Read();
186  jedid = ((unsigned long)byte1 << 16)| (byte2 << 8) | byte3;
187  CE_DESELECT(); //Disable device
188  return jedid;
189 }
190 
195 void S25FL_ChipErase(void)
196 {
197  CE_SELECT(); //Enable device
198  SPI_Write(CMD_WREN); //Send WREN command
199  CE_DESELECT(); //Disable device
200 
201  CE_SELECT(); //Enable device
202  SPI_Write(ERASE_CHIP); //Send Sector Erase command
203  CE_DESELECT(); //Disable device
204 }
205 
211 void S25FL_SectorErase(unsigned long address)
212 {
213  CE_SELECT(); //Enable device
214  SPI_Write(CMD_WREN); //Send WREN command
215  CE_DESELECT(); //Disable device
216 
217  CE_SELECT(); //Enable device
218  SPI_Write(ERASE_SECTOR); //Send Sector Erase command
219  SPI_Write((address & 0x0000FFFF)>>8);
220  SPI_Write(address & 0x000000FF);
221  SPI_Write(0x00);
222  CE_DESELECT(); //Disable device
223 }
224 
230 void S25FL_BlockErase(unsigned long address)
231 {
232  CE_SELECT(); //Enable device
233  SPI_Write(CMD_WREN); //Send WREN command
234  CE_DESELECT(); //Disable device
235 
236  CE_SELECT(); //Enable device
237  SPI_Write(ERASE_BLOCK64); //Send Sector Erase command
238  SPI_Write((unsigned char)(address >> 16) & 0x000000FF);
239  SPI_Write((unsigned char)(address >> 8) & 0x000000FF);
240  SPI_Write((unsigned char)(address & 0x000000FF));
241  CE_DESELECT(); //Disable device
242 }
243 
248 unsigned char S25FL_IsWriteBusy(void)
249 {
250  unsigned char temp = 0;
251  CE_SELECT(); //Enable device
252  SPI_Write(CMD_RDSR1); //Send RDSR command
253  temp = SPI_Read();
254  CE_DESELECT(); //Disable
255  if (temp & 0x01) return 1;
256  else return 0;
257 }
258 
264 unsigned char S25FL_SReg_Read(unsigned char statReg)
265 {
266  unsigned char byte = 0;
267  CE_SELECT(); // enable device
268  SPI_Write(statReg); // send RDSR command
269  byte = SPI_Read(); // receive byte
270  CE_DESELECT(); // disable device
271  return byte;
272 }
273 
279 void S25FL_SReg_Write(unsigned char statReg)
280 {
281  CE_SELECT(); //Enable device
282  SPI_Write(CMD_WREN); //Send WREN command
283  CE_DESELECT(); //Disable device
284 
285  CE_SELECT(); // enable device
286  SPI_Write(CMD_WRSR); // select write to status register
287  SPI_Write(statReg); // data that will change the status of BPx or BPL (only bits 2,3,4,5,7 can be written)
288  CE_DESELECT(); // disable the device
289 }
290 
295 void S25FL_DeepSleep(void)
296 {
297  CE_SELECT(); // enable device
298  SPI_Write(CMD_DEEPSLP); // select write to status register
299  CE_DESELECT(); // disable the device
300 }
301 
306 void S25FL_WakeUp(void)
307 {
308  CE_SELECT(); // enable device
309  SPI_Write(CMD_RESDPD); // select write to status register
310  CE_DESELECT(); // disable the device
311 }
312 
318 void S25FL_SetBlockProtection(unsigned char prot)
319 {
320  CE_SELECT();
321  SPI_Write(CMD_EWSR); //enable write to volatile register
322  CE_DESELECT();
323 
324  CE_SELECT();
325  SPI_Write(CMD_WRSR); //write status register
326  SPI_Write((prot & 0x0f)<<2);
327  CE_DESELECT();
328 }
329 
S25FL204K driver header.
#define CMD_RESDPD
Definition: S25FL204K.h:52
void S25FL_ChipErase(void)
Erase all chip contents. Erase is slow on flash chips.
Definition: S25FL204K.c:195
#define CE_DESELECT()
Definition: S25FL204K.h:87
#define ERASE_BLOCK64
Definition: S25FL204K.h:48
#define CMD_EWSR
Definition: S25FL204K.h:38
#define JEDEC
Definition: S25FL204K.h:54
void S25FL_Setup(void)
Setup SPI and IOs connected to Serial FLASH.
Definition: S25FL204K.c:39
void S25FL_WakeUp(void)
Wakes the flash memory chip from deep sleep mode.
Definition: S25FL204K.c:306
void S25FL_SectorErase(unsigned long address)
This function erases a 4Kb sector (it erase a page actually)
Definition: S25FL204K.c:211
#define PAGE_PROGRAM
Definition: S25FL204K.h:41
void S25FL_SetBlockProtection(unsigned char prot)
Sets the block protection of the chip.
Definition: S25FL204K.c:318
void S25FL_WriteArray(unsigned long address, unsigned char *pData, unsigned int arrayLength)
Write data array at the specified address , this procedure does page programming. The destination add...
Definition: S25FL204K.c:97
unsigned char SPI_Write(unsigned char data)
Writes a byte of data to the SPI bus.
Definition: SPI.c:53
unsigned int S25FL_ReadID(void)
This function reads the Manufacturer ID code This procedure Reads the manufacturer's ID and device ID...
Definition: S25FL204K.c:156
unsigned char SPI_Read(void)
Reads a byte of data from the SPI bus.
Definition: SPI.c:62
#define CMD_WRSR
Definition: S25FL204K.h:68
#define ERASE_CHIP
Definition: S25FL204K.h:49
void S25FL_WriteByte(unsigned char data, unsigned long address)
Write a byte to the address specified (24 bit address)
Definition: S25FL204K.c:51
#define CMD_RDSR1
Definition: S25FL204K.h:34
unsigned char S25FL_IsWriteBusy(void)
This function reads status register and checks BUSY bit for a write in progress operation.It waits until the device is no longer busy.
Definition: S25FL204K.c:248
#define CMD_READ_DATA
Definition: S25FL204K.h:43
#define CE_SELECT()
Definition: S25FL204K.h:88
unsigned char S25FL_ReadByte(unsigned long address)
Read a byte from the specified 24 bit address.
Definition: S25FL204K.c:76
#define CE_OUTPUT()
Definition: S25FL204K.h:86
#define CMD_RDID
Definition: S25FL204K.h:53
void S25FL_SReg_Write(unsigned char statReg)
This procedure writes a byte to the Status Register.
Definition: S25FL204K.c:279
unsigned char S25FL_SReg_Read(unsigned char statReg)
Read specific Status Register depending on value of statReg.
Definition: S25FL204K.c:264
void S25FL_BlockErase(unsigned long address)
This function erases a 64Kb block.
Definition: S25FL204K.c:230
#define ERASE_SECTOR
Definition: S25FL204K.h:46
SPI driver header.
unsigned long S25FL_Jedec_ID_Read(void)
This procedure Reads the manufacturer's ID (BFh), memory type (25h) and device ID (4Bh)...
Definition: S25FL204K.c:177
void S25FL_ReadArray(unsigned long address, unsigned char *pData, unsigned int arrayLength)
Read an array of data from the given address.
Definition: S25FL204K.c:129
#define CMD_WREN
Definition: S25FL204K.h:37
void S25FL_DeepSleep(void)
Puts the flash chip into deep sleep mode.
Definition: S25FL204K.c:295
unsigned long address
Definition: main.c:76
#define CMD_DEEPSLP
Definition: S25FL204K.h:51