LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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
3  * This example show how to use the EEPROM interface
4  *
5  * @note
6  * Copyright(C) NXP Semiconductors, 2012
7  * All rights reserved.
8  *
9  * @par
10  * Software that is described herein is for illustrative purposes only
11  * which provides customers with programming information regarding the
12  * LPC products. This software is supplied "AS IS" without any warranties of
13  * any kind, and NXP Semiconductors and its licensor disclaim any and
14  * all warranties, express or implied, including all implied warranties of
15  * merchantability, fitness for a particular purpose and non-infringement of
16  * intellectual property rights. NXP Semiconductors assumes no responsibility
17  * or liability for the use of the software, conveys no license or rights under any
18  * patent, copyright, mask work right, or any other intellectual property rights in
19  * or to any products. NXP Semiconductors reserves the right to make changes
20  * in the software without notification. NXP Semiconductors also makes no
21  * representation or warranty that such application will be suitable for the
22  * specified use without further testing or modification.
23  *
24  * @par
25  * Permission to use, copy, modify, and distribute this software and its
26  * documentation is hereby granted, under NXP Semiconductors' and its
27  * licensor's relevant copyrights in the software, without fee, provided that it
28  * is used in conjunction with NXP Semiconductors microcontrollers. This
29  * copyright, permission, and disclaimer notice must appear in all copies of
30  * this code.
31  */
32 
33 #include "board.h"
34 #include "string.h"
35 
63 /*****************************************************************************
64  * Private types/enumerations/variables
65  ****************************************************************************/
66 
67 /* EEPROM Address used for storage */
68 #define EEPROM_ADDR 0x40
69 
70 /* BUFFER size */
71 #define BUFFER_SIZE 0x40
72 
73 /* Tag for checking if a string already exists in EEPROM */
74 #define CHKTAG "NxP"
75 #define CHKTAG_SIZE 3
76 
77 /* ASCII ESC character code */
78 #define ESC_CHAR 27
79 
80 /* Read/write buffer (32-bit aligned) */
82 
83 /*****************************************************************************
84  * Public types/enumerations/variables
85  ****************************************************************************/
86 
87 /* Show current string stored in UART */
88 static void ShowString(char *str) {
89  int stSize;
90 
91  /* Is data tagged with check pattern? */
92  if (strncmp(str, CHKTAG, CHKTAG_SIZE) == 0) {
93  /* Get next byte, which is the string size in bytes */
94  stSize = (uint32_t) str[3];
95  if (stSize > 32) {
96  stSize = 32;
97  }
98 
99  /* Add terminator */
100  str[4 + stSize] = '\0';
101 
102  /* Show string stored in EEPROM */
103  DEBUGSTR("Stored string found in EEEPROM\r\n");
104  DEBUGSTR("-->");
105  DEBUGSTR((char *) &str[4]);
106  DEBUGSTR("<--\r\n");
107  }
108  else {
109  DEBUGSTR("No string stored in the EEPROM\r\n");
110  }
111 }
112 
113 /* Get a string to save from the UART */
114 static uint32_t MakeString(uint8_t *str)
115 {
116  int index, byte;
117  char strOut[2];
118 
119  /* Get a string up to 32 bytes to write into EEPROM */
120  DEBUGSTR("\r\nEnter a string to write into EEPROM\r\n");
121  DEBUGSTR("Up to 32 bytes in length, press ESC to accept\r\n");
122 
123  /* Setup header */
124  strncpy((char *) str, CHKTAG, CHKTAG_SIZE);
125 
126  /* Read until escape, but cap at 32 characters */
127  index = 0;
128  strOut[1] = '\0';
129 #if defined(DEBUG_ENABLE)
130  byte = DEBUGIN();
131  while ((index < 32) && (byte != ESC_CHAR)) {
132  if (byte != EOF) {
133  strOut[0] = str[4 + index] = (uint8_t) byte;
134  DEBUGSTR(strOut);
135  index++;
136  }
137 
138  byte = DEBUGIN();
139  }
140 #else
141  /* Suppress warnings */
142  (void) byte;
143  (void) strOut;
144 
145  /* Debug input not enabled, so use a pre-setup string */
146  strncpy((char *) &str[4], "12345678", 8);
147  index = 8;
148 #endif
149 
150  str[3] = (uint8_t) index;
151 
152  return (uint32_t) index;
153 }
154 
155 /*****************************************************************************
156  * Private functions
157  ****************************************************************************/
158 
163 int main(void)
164 {
165  EEPROM_READ_COMMAND_T rCommand;
166  EEPROM_READ_OUTPUT_T rOutput;
167  EEPROM_WRITE_COMMAND_T wCommand;
168  EEPROM_WRITE_OUTPUT_T wOutput;
169 
170  uint32_t stSize;
171  uint8_t *ptr = (uint8_t *) buffer;
172  volatile int loop = 1; /* For debug message only */
173 
174  Board_Init();
175 
176  /* Read all data from EEPROM page */
177  rCommand.cmd = FLASH_EEPROM_READ;
178  rCommand.eepromAddr = EEPROM_ADDR;
179  rCommand.ramAddr = (uint32_t) ptr;
180  rCommand.byteNum = BUFFER_SIZE;
181  rCommand.cclk = Chip_Clock_GetSystemClockRate() / 1000;
182  Chip_EEPROM_Read(&rCommand, &rOutput);
183  if (rOutput.status != CMD_SUCCESS) {
184  DEBUGSTR("EEPROM read failed\r\n");
185  }
186 
187  /* Check and display string if it exists */
188  ShowString((char *) ptr);
189 
190  /* Get a string to save */
191  stSize = MakeString(ptr);
192 
193  /* Write header + size + data to page */
194  wCommand.cmd = FLASH_EEPROM_WRITE;
195  wCommand.eepromAddr = EEPROM_ADDR;
196  wCommand.ramAddr = (uint32_t) ptr;
197  wCommand.byteNum = (4 + stSize);
198  wCommand.cclk = Chip_Clock_GetSystemClockRate() / 1000;
199  Chip_EEPROM_Write(&wCommand, &wOutput);
200  if (wOutput.status == CMD_SUCCESS) {
201  DEBUGSTR("EEPROM write passed\r\n");
202  DEBUGSTR("Reading back string...\r\n");
203 
204  /* Clear buffer */
205  for (loop = 0; loop < BUFFER_SIZE / sizeof(uint32_t); loop++) {
206  buffer[loop] = 0;
207  }
208  /* Read all data from EEPROM page */
209  Chip_EEPROM_Read(&rCommand, &rOutput);
210 
211  /* Check and display string if it exists */
212  ShowString((char *) ptr);
213  }
214  else {
215  DEBUGSTR("EEPROM write failed\r\n");
216  }
217 
218  /* Wait forever */
219  while (loop) {}
220 
221  return 0;
222 }
223