| 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
eeprom.c
Go to the documentation of this file.
1 /*
2 * @brief Eeprom example using a timer and interrupt
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
63 /*****************************************************************************
64 * Private types/enumerations/variables
65 ****************************************************************************/
66 /* Auto programming on/off */
67 #define AUTOPROG_ON 1
68
69 /* Page used for storage */
70 #define PAGE_ADDR 0x01/* Page number */
71
72 /* Tag for checking if a string already exists in EEPROM */
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[EEPROM_PAGE_SIZE / sizeof(uint32_t)];
81
82 /*****************************************************************************
83 * Public types/enumerations/variables
84 ****************************************************************************/
85
86 /* Show current string stored in UART */
87 static void ShowString(char *str) {
88 int stSize;
89
90 /* Is data tagged with check pattern? */
91 if (strncmp(str, CHKTAG, CHKTAG_SIZE) == 0) {
92 /* Get next byte, which is the string size in bytes */
93 stSize = (uint32_t) str[3];
94 if (stSize > 32) {
95 stSize = 32;
96 }
97
98 /* Add terminator */
99 str[4 + stSize] = '\0';
100
101 /* Show string stored in EEPROM */
102 DEBUGSTR("Stored string found in EEEPROM\r\n");
103 DEBUGSTR("-->");
104 DEBUGSTR((char *) &str[4]);
105 DEBUGSTR("<--\r\n");
106 }
107 else {
108 DEBUGSTR("No string stored in the EEPROM\r\n");
109 }
110 }
111
112 /* Get a string to save from the UART */
113 static uint32_t MakeString(uint8_t *str)
114 {
115 int index, byte;
116 char strOut[2];
117
118 /* Get a string up to 32 bytes to write into EEPROM */
119 DEBUGSTR("\r\nEnter a string to write into EEPROM\r\n");
120 DEBUGSTR("Up to 32 bytes in length, press ESC to accept\r\n");
121
122 /* Setup header */
123 strncpy((char *) str, CHKTAG, CHKTAG_SIZE);
124
125 /* Read until escape, but cap at 32 characters */
126 index = 0;
127 strOut[1] = '\0';
128 byte = DEBUGIN();
129 while ((index < 32) && (byte != ESC_CHAR)) {
130 if (byte != EOF) {
131 strOut[0] = str[4 + index] = (uint8_t) byte;
132 DEBUGSTR(strOut);
133 index++;
134 }
135
136 byte = DEBUGIN();
137 }
138
139 str[3] = (uint8_t) index;
140
141 return (uint32_t) index;
142 }
143
144 /* Read data from EEPROM */
145 /* size must be multiple of 4 bytes */
146 STATIC void EEPROM_Read(uint32_t pageOffset, uint32_t pageAddr, uint32_t* ptr, uint32_t size)
147 {
148 uint32_t i = 0;
149 uint32_t *pEepromMem = (uint32_t*)EEPROM_ADDRESS(pageAddr,pageOffset);
150 for(i = 0; i < size/4; i++) {
151 ptr[i] = pEepromMem[i];
152 }
153 }
154
155 /* Erase a page in EEPROM */
156 STATIC void EEPROM_Erase(uint32_t pageAddr)
157 {
158 uint32_t i = 0;
159 uint32_t *pEepromMem = (uint32_t*)EEPROM_ADDRESS(pageAddr,0);
160 for(i = 0; i < EEPROM_PAGE_SIZE/4; i++) {
161 pEepromMem[i] = 0;
162 #if AUTOPROG_ON
163 Chip_EEPROM_WaitForIntStatus(LPC_EEPROM, EEPROM_INT_ENDOFPROG);
164 #endif
165 }
166 #if (AUTOPROG_ON == 0)
167 Chip_EEPROM_EraseProgramPage(LPC_EEPROM);
168 #endif
169 }
170
171 /* Write data to a page in EEPROM */
172 /* size must be multiple of 4 bytes */
173 STATIC void EEPROM_Write(uint32_t pageOffset, uint32_t pageAddr, uint32_t* ptr, uint32_t size)
174 {
175 uint32_t i = 0;
176 uint32_t *pEepromMem = (uint32_t*)EEPROM_ADDRESS(pageAddr,pageOffset);
177
178 if(size > EEPROM_PAGE_SIZE - pageOffset)
179 size = EEPROM_PAGE_SIZE - pageOffset;
180
181 for(i = 0; i < size/4; i++) {
182 pEepromMem[i] = ptr[i];
183 #if AUTOPROG_ON
184 Chip_EEPROM_WaitForIntStatus(LPC_EEPROM, EEPROM_INT_ENDOFPROG);
185 #endif
186 }
187
188 #if (AUTOPROG_ON == 0)
189 Chip_EEPROM_EraseProgramPage(LPC_EEPROM);
190 #endif
191 }
192
193 /*****************************************************************************
194 * Private functions
195 ****************************************************************************/
196
201 int main(void)
202 {
203 uint32_t stSize;
204 uint8_t *ptr = (uint8_t *) buffer;
205 volatile int loop = 1; /* For debug message only */
206
207 Board_Init();
208
209 /* Init EEPROM */
210 Chip_EEPROM_Init(LPC_EEPROM);
211
212 #if AUTOPROG_ON
213 /* Set Auto Programming mode */
214 Chip_EEPROM_SetAutoProg(LPC_EEPROM,EEPROM_AUTOPROG_AFT_1WORDWRITTEN);
215 #else
216 /* Set Auto Programming mode */
217 Chip_EEPROM_SetAutoProg(LPC_EEPROM,EEPROM_AUTOPROG_OFF);
218 #endif /*AUTOPROG_ON*/
219
220 /* Read all data from EEPROM page */
221 EEPROM_Read(0, PAGE_ADDR, (uint32_t*)ptr, EEPROM_PAGE_SIZE);
222
223 /* Check and display string if it exists */
224 ShowString((char *) ptr);
225
226 /* Get a string to save */
227 stSize = MakeString(ptr);
228 if(stSize % 4)
229 stSize = (stSize/4 + 1)*4;
230
231 /* Erase page */
232 EEPROM_Erase(PAGE_ADDR);
233
234 /* Write header + size + data to page */
235 DEBUGSTR("\r\nEEPROM write...\r\n");
236 EEPROM_Write(0, PAGE_ADDR, (uint32_t*)ptr, (4 + stSize));
237 DEBUGSTR("Reading back string...\r\n");
238
239 /* Read all data from EEPROM page */
240 EEPROM_Read(0, PAGE_ADDR, (uint32_t*)ptr, EEPROM_PAGE_SIZE);
241
242 /* Check and display string if it exists */
243 ShowString((char *) ptr);
244
245 /* Wait forever */
246 while (loop) {}
247
248 return 0;
249 }
250