1 /*
2 * @brief EEPROM example
3 * This example show how to use the EEPROM interface
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2013
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
36 /*****************************************************************************
37 * Private types/enumerations/variables
38 ****************************************************************************/
39
40 /* Systick interrupt rate */
41 #define TICKRATE_HZ (10) /* 10 ticks per second */
42
43 /* EEPROM Address used for storage */
44 #define EEPROM_ADDRESS 0x00000100
45
46 /* Write count */
47 #define IAP_NUM_BYTES_TO_READ_WRITE 32
48
49 /* Tag for checking if a string already exists in EEPROM */
50 #define CHKTAG "NxP"
51 #define CHKTAG_SIZE 3
52
53 /* ASCII ESC character code */
54 #define ESC_CHAR 27
55
56 /* Read/write buffer (32-bit aligned) */
57 uint32_t buffer[IAP_NUM_BYTES_TO_READ_WRITE / sizeof(uint32_t)];
58
59 /* Test string for no DEBUG */
60 #define TESTSTRING "12345678"
61
62 /*****************************************************************************
63 * Public types/enumerations/variables
64 ****************************************************************************/
65
66 /*****************************************************************************
67 * Private functions
68 ****************************************************************************/
69
70 /* Show current string stored in UART */
ShowString(char * str)71 static void ShowString(char *str) {
72 int stSize;
73
74 /* Is data tagged with check pattern? */
75 if (strncmp(str, CHKTAG, CHKTAG_SIZE) == 0) {
76 /* Get next byte, which is the string size in bytes */
77 stSize = (uint32_t) str[3];
78 if (stSize > 32) {
79 stSize = 32;
80 }
81
82 /* Add terminator */
83 str[4 + stSize] = '\0';
84
85 /* Show string stored in EEPROM */
86 DEBUGSTR("Stored string found in EEEPROM\r\n");
87 DEBUGSTR("-->");
88 DEBUGSTR((char *) &str[4]);
89 DEBUGSTR("<--\r\n");
90 }
91 else {
92 DEBUGSTR("No string stored in the EEPROM\r\n");
93 }
94 }
95
96 /* Get a string to save from the UART */
MakeString(uint8_t * str)97 static uint32_t MakeString(uint8_t *str)
98 {
99 int index, byte;
100 char strOut[2];
101
102 /* Get a string up to 32 bytes to write into EEPROM */
103 DEBUGSTR("\r\nEnter a string to write into EEPROM\r\n");
104 DEBUGSTR("Up to 32 bytes in length, press ESC to accept\r\n");
105
106 /* Setup header */
107 strncpy((char *) str, CHKTAG, CHKTAG_SIZE);
108
109 #if defined(DEBUG_ENABLE)
110 /* Read until escape, but cap at 32 characters */
111 index = 0;
112 strOut[1] = '\0';
113 byte = DEBUGIN();
114 while ((index < 32) && (byte != ESC_CHAR)) {
115 if (byte != EOF) {
116 strOut[0] = str[4 + index] = (uint8_t) byte;
117 DEBUGSTR(strOut);
118 index++;
119 }
120
121 byte = DEBUGIN();
122 }
123 #else
124 /* Suppress warnings */
125 (void) byte;
126 (void) strOut;
127
128 /* Debug input not enabled, so use a pre-setup string */
129 strcpy((char *) &str[4], TESTSTRING);
130 index = strlen(TESTSTRING);
131 #endif
132
133 str[3] = (uint8_t) index;
134
135 return (uint32_t) index;
136 }
137
138 /*****************************************************************************
139 * Public functions
140 ****************************************************************************/
141
142 /**
143 * @brief Handle interrupt from SysTick timer
144 * @return Nothing
145 */
SysTick_Handler(void)146 void SysTick_Handler(void)
147 {
148 Board_LED_Toggle(0);
149 }
150
151 /**
152 * @brief main routine for EEPROM example
153 * @return Always returns 0
154 */
main(void)155 int main(void)
156 {
157 uint8_t *ptr = (uint8_t *) buffer;
158 uint8_t ret_code;
159
160 /* Generic Initialization */
161 SystemCoreClockUpdate();
162 Board_Init();
163
164 /* Enable SysTick Timer */
165 SysTick_Config(SystemCoreClock / TICKRATE_HZ);
166
167 /* Enable EEPROM clock and reset EEPROM controller */
168 Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_EEPROM);
169 Chip_SYSCTL_PeriphReset(RESET_EEPROM);
170
171 /* Data to be read from EEPROM */
172 ret_code = Chip_EEPROM_Read(EEPROM_ADDRESS, ptr, IAP_NUM_BYTES_TO_READ_WRITE);
173
174 /* Error checking */
175 if (ret_code != IAP_CMD_SUCCESS) {
176 DEBUGOUT("Command failed to execute, return code is: %x\r\n", ret_code);
177 }
178
179 /* Check and display string if it exists */
180 ShowString((char *) ptr);
181
182 /* Get a string to save */
183 MakeString(ptr);
184
185 /* Data to be written to EEPROM */
186 ret_code = Chip_EEPROM_Write(EEPROM_ADDRESS, ptr, IAP_NUM_BYTES_TO_READ_WRITE);
187
188 /* Error checking */
189 if (ret_code == IAP_CMD_SUCCESS) {
190 DEBUGSTR("EEPROM write passed\r\n");
191 }
192 else {
193 DEBUGOUT("EEPROM write failed, return code is: %x\r\n", ret_code);
194 }
195
196 return 0;
197 }
198