Sensorian  1.0
C API Reference Guide Library
main.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 <stdio.h>
29 #include <unistd.h>
30 #include "S25FL204K.h"
31 #include "SPI.h"
32 #include "Utilities.h"
33 
34 unsigned char pattern1[] =
35 {
36 0x55,0x55,0x55,0x65,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
37 0x55,0x55,0x55,0x65,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
38 0x55,0x55,0x55,0x65,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
39 0x55,0x55,0x55,0x65,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
40 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
41 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
42 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
43 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
44 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
45 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
46 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
47 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
48 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
49 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
50 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
51 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
52 };
53 
54 unsigned char pattern2[] =
55 {
56 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
57 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
58 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
59 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
60 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
61 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
62 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
63 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
64 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
65 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
66 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
67 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
68 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
69 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
70 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g',
71 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g'
72 };
73 
74 unsigned char buffer[256] = {0};
75 unsigned int len = 256;
76 unsigned long address = 0x000000;
77 
78 void readMemory(unsigned long address,unsigned int page_count);
79 
80 int main(void)
81 {
82  SPI_Initialize(); //Initialize SPI peripheral
83  S25FL_Setup(); //Setup flash memory
84  S25FL_SReg_Write(0x00); //Remove protection from all blocks writes to SR1
85 
86  printf("Flash memory example.\r\n");
87  printf("This demo read and writes pages from flash memory.\r\n");
88 
89  /*****************Read pages *************************/
90 
91  printf("Read first page\r\n");
92  readMemory(address,1);
93  delay_ms(200);
94  printf("\r\n Writing on first page \r\n");
96 
97  printf("\r\nRead new sectors\r\n");
98  unsigned long dst2 = 0x000000;
99  readMemory(dst2,2);
100 
101  printf("\r\n Erasing data \r\n");
102  S25FL_SectorErase(0x000000);
103  printf("\r\n Done erase \r\n");
104  delay_ms(500);
105 
106  dst2 = 0x000000;
107  printf("\r\n Veryfing erased data \r\n");
108  readMemory(dst2,2);
109 
110  delay_ms(500);
111  SPI_Close();
112 
113 return 0;
114 }
115 
116 
117 void readMemory(unsigned long address,unsigned int page_count)
118 {
119  unsigned int i = 0;
120  unsigned char page = 0;
121 
122  for(page =0 ; page < page_count;page++)
123  {
124  S25FL_ReadArray(address,buffer,len);
125  usleep(35000);
126  printf("\r\n\r\nPage %d.", page);
127  usleep(35000);
128 
129  for(i=0;i<256;i++)
130  {
131  if (i%64 == 0)
132  {
133  printf("\r\n");
134  }
135  printf("0x%X ",buffer[i]);
136  }
137  address += 0x100;
138  }
139 }
unsigned char pattern1[]
Definition: main.c:34
unsigned char buffer[256]
Definition: main.c:74
SPI driver header.
void S25FL_Setup(void)
Setup SPI and IOs connected to Serial FLASH.
Definition: S25FL204K.c:39
unsigned char pattern2[]
Definition: main.c:54
void S25FL_SectorErase(unsigned long address)
This function erases a 4Kb sector (it erase a page actually)
Definition: S25FL204K.c:211
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
void SPI_Initialize(void)
Initializes the SPI peripheral.
Definition: SPI.c:35
void readMemory(unsigned long address, unsigned int page_count)
Definition: main.c:117
unsigned int len
Definition: main.c:75
void SPI_Close(void)
Close SPI bus, should not be called if another SPI device is being used.
Definition: SPI.c:95
void S25FL_SReg_Write(unsigned char statReg)
This procedure writes a byte to the Status Register.
Definition: S25FL204K.c:279
int main(int argc, char **argv)
Definition: main.c:34
void delay_ms(unsigned int ms)
Delay in ms.
Definition: Utilities.c:39
Utilities driver header.
S25FL204K driver header.
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
unsigned long address
Definition: main.c:76