| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
norflash.c
Go to the documentation of this file.
1 /*
2 * @brief This example shows how to opertate on external flash operations.
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #include "board.h"
33 #include "string.h"
34
65 /*****************************************************************************
66 * Private types/enumerations/variables
67 ****************************************************************************/
68
69 /* BUFFER size */
70 #define BUFFER_SIZE 0x40
71
72 /* Tag for checking if a string already exists in Flash */
73 #define CHKTAG "NxP"
74 #define CHKTAG_SIZE 3
75
76 /* ASCII ESC character code */
77 #define ESC_CHAR 27
78
79 /* Read/write buffer (32-bit aligned) */
80 uint32_t buffer[BUFFER_SIZE / sizeof(uint32_t)];
81
82 /*****************************************************************************
83 * Public types/enumerations/variables
84 ****************************************************************************/
85
86 /*****************************************************************************
87 * Private functions
88 ****************************************************************************/
89
90 /* Show current string stored in UART */
91 static void ShowString(char *str) {
92 int stSize;
93
94 /* Is data tagged with check pattern? */
95 if (strncmp(str, CHKTAG, CHKTAG_SIZE) == 0) {
96 /* Get next byte, which is the string size in bytes */
97 stSize = (uint32_t) str[3];
98 if (stSize > 32) {
99 stSize = 32;
100 }
101
102 /* Add terminator */
103 str[4 + stSize] = '\0';
104
105 /* Show string stored in Flash */
106 DEBUGSTR("Stored string found in Flash\r\n");
107 DEBUGSTR("-->");
108 DEBUGSTR((char *) &str[4]);
109 DEBUGSTR("<--\r\n");
110 }
111 else {
112 DEBUGSTR("No string stored in the Flash\r\n");
113 }
114 }
115
116 /* Get a string to save from the UART */
117 static uint32_t MakeString(uint8_t *str)
118 {
119 int index, byte;
120 char strOut[2];
121
122 /* Get a string up to 32 bytes to write into Flash */
123 DEBUGSTR("\r\nEnter a string to write into Flash\r\n");
124 DEBUGSTR("Up to 32 bytes in length, press ESC to accept\r\n");
125
126 /* Setup header */
127 strncpy((char *) str, CHKTAG, CHKTAG_SIZE);
128
129 /* Read until escape, but cap at 32 characters */
130 index = 0;
131 strOut[1] = '\0';
132 #if defined(DEBUG_ENABLE)
133 byte = DEBUGIN();
134 while ((index < 32) && (byte != ESC_CHAR)) {
135 if (byte != EOF) {
136 strOut[0] = str[4 + index] = (uint8_t) byte;
137 DEBUGSTR(strOut);
138 index++;
139 }
140
141 byte = DEBUGIN();
142 }
143 #else
144 /* Suppress warnings */
145 (void) byte;
146 (void) strOut;
147
148 /* Debug input not enabled, so use a pre-setup string */
149 strncpy((char *) &str[4], "12345678", 8);
150 index = 8;
151 #endif
152
153 str[3] = (uint8_t) index;
154
155 return (uint32_t) index;
156 }
157
158 /* Function for reading NOR FLASH - this is overkill, since NOR FLASH is
159 is memory mapped and can be directly accessed. */
160 static void ReadFlash(uint32_t addr, uint16_t *buffer, uint32_t size)
161 {
162 uint32_t i = 0;
163 for (i = 0; i < BUFFER_SIZE; i += 2) {
164 *buffer = lpc_norflash_read_word(addr + i);
165 buffer++;
166 }
167 }
168
169 static void WriteFlash(uint32_t addr, uint16_t *buffer, uint32_t size)
170 {
171 lpc_norflash_write_buffer(addr, buffer, size);
172 }
173
174 /*****************************************************************************
175 * Public functions
176 ****************************************************************************/
177
182 int main(void)
183 {
184 uint32_t addr;
185 uint16_t manufacturerID, deviceID;
186 uint32_t sector_count, flash_size;
187 uint32_t stSize;
188 volatile int loop = 1; /* For debug message only */
189
190 Board_Init();
191
192 /* Read flash information */
193 lpc_norflash_get_size(&flash_size, &sector_count);
194 lpc_norflash_get_id(&manufacturerID, &deviceID);
195 DEBUGOUT(" Flash Information: \r\n");
196 DEBUGOUT(" Manufacturer ID: 0x%04x\r\n", manufacturerID);
197 DEBUGOUT(" Device ID: 0x%04x\r\n", deviceID);
198 DEBUGOUT(" Size: %08dKB\r\n", flash_size / 1024);
199 DEBUGOUT(" Sector Count: %08d\r\n", sector_count);
200
201 /* Read data */
202 addr = lpc_norflash_get_sector_offset(8);
203 ReadFlash(addr, (uint16_t *) buffer, BUFFER_SIZE);
204
205 /* Check and display string if it exists */
206 ShowString((char *) buffer);
207
208 /* Get a string to save */
209 stSize = MakeString((uint8_t *) buffer);
210
211 /* Erase flash */
212 lpc_norflash_erase_sector(addr);
213 /* Wait TSE (~25ms) */
214 for (loop = 0; loop < 25 * 40000; loop++) {}
215
216 /* Write header + size + data to page */
217 DEBUGSTR("\r\nWrite to flash...\r\n");
218 WriteFlash(addr, (uint16_t *) buffer, (4 + stSize));
219
220 DEBUGSTR("Reading back string...\r\n");
221
222 /* Read all data from flash */
223 ReadFlash(addr, (uint16_t *) buffer, BUFFER_SIZE);
224
225 /* Check and display string if it exists */
226 ShowString((char *) buffer);
227
228 /* Wait forever */
229 while (loop) {}
230
231 return 0;
232 }
233