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 
64 /*****************************************************************************
65  * Private types/enumerations/variables
66  ****************************************************************************/
67 
68 /* Page used for storage */
69 #define PAGE_ADDR 0x01/* Page number */
70 
71 /* Tag for checking if a string already exists in EEPROM */
72 #define CHKTAG "NxP"
73 #define CHKTAG_SIZE 3
74 
75 /* ASCII ESC character code */
76 #define ESC_CHAR 27
77 
78 /* Read/write buffer (32-bit aligned) */
80 
81 /*****************************************************************************
82  * Public types/enumerations/variables
83  ****************************************************************************/
84 
85 /* Show current string stored in UART */
86 static void ShowString(char *str) {
87  int stSize;
88 
89  /* Is data tagged with check pattern? */
90  if (strncmp(str, CHKTAG, CHKTAG_SIZE) == 0) {
91  /* Get next byte, which is the string size in bytes */
92  stSize = (uint32_t) str[3];
93  if (stSize > 32) {
94  stSize = 32;
95  }
96 
97  /* Add terminator */
98  str[4 + stSize] = '\0';
99 
100  /* Show string stored in EEPROM */
101  DEBUGSTR("Stored string found in EEEPROM\r\n");
102  DEBUGSTR("-->");
103  DEBUGSTR((char *) &str[4]);
104  DEBUGSTR("<--\r\n");
105  }
106  else {
107  DEBUGSTR("No string stored in the EEPROM\r\n");
108  }
109 }
110 
111 /* Get a string to save from the UART */
112 static uint32_t MakeString(uint8_t *str)
113 {
114  int index, byte;
115  char strOut[2];
116 
117  /* Get a string up to 32 bytes to write into EEPROM */
118  DEBUGSTR("\r\nEnter a string to write into EEPROM\r\n");
119  DEBUGSTR("Up to 32 bytes in length, press ESC to accept\r\n");
120 
121  /* Setup header */
122  strncpy((char *) str, CHKTAG, CHKTAG_SIZE);
123 
124  /* Read until escape, but cap at 32 characters */
125  index = 0;
126  strOut[1] = '\0';
127  byte = DEBUGIN();
128  while ((index < 32) && (byte != ESC_CHAR)) {
129  if (byte != EOF) {
130  strOut[0] = str[4 + index] = (uint8_t) byte;
131  DEBUGSTR(strOut);
132  index++;
133  }
134 
135  byte = DEBUGIN();
136  }
137 
138  str[3] = (uint8_t) index;
139 
140  return (uint32_t) index;
141 }
142 
143 /*****************************************************************************
144  * Private functions
145  ****************************************************************************/
146 
151 int main(void)
152 {
153  uint32_t stSize;
154  uint8_t *ptr = (uint8_t *) buffer;
155  volatile int loop = 1; /* For debug message only */
156 
157  Board_Init();
158 
159  /* Init EEPROM */
161 
162  /* Read all data from EEPROM page */
164 
165  /* Check and display string if it exists */
166  ShowString((char *) ptr);
167 
168  /* Get a string to save */
169  stSize = MakeString(ptr);
170 
171  /* Erase page */
173 
174  /* Write header + size + data to page */
175  if (Chip_EEPROM_Write(LPC_EEPROM, 0, PAGE_ADDR, ptr, EEPROM_RWSIZE_8BITS, (4 + stSize)) == SUCCESS) {
176  DEBUGSTR("EEPROM write passed\r\n");
177  DEBUGSTR("Reading back string...\r\n");
178 
179  /* Read all data from EEPROM page */
181 
182  /* Check and display string if it exists */
183  ShowString((char *) ptr);
184  }
185  else {
186  DEBUGSTR("EEPROM write failed\r\n");
187  }
188 
189  /* Wait forever */
190  while (loop) {}
191 
192  return 0;
193 }
194