| 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_001.c
Go to the documentation of this file.
1 /*
2 * @brief EEPROM driver functions
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 "eeprom_001.h"
33
34 /*****************************************************************************
35 * Private types/enumerations/variables
36 ****************************************************************************/
37
38 /*****************************************************************************
39 * Public types/enumerations/variables
40 ****************************************************************************/
41
42 /*****************************************************************************
43 * Private functions
44 ****************************************************************************/
45
46 /*****************************************************************************
47 * Public functions
48 ****************************************************************************/
49
50 /* Initializes EEPROM */
51 void IP_EEPROM_Init(IP_EEPROM_001_T *pEEPROM, uint32_t div)
52 {
53 /* Disable EEPROM power down mode */
54 IP_EEPROM_DisablePowerDown(pEEPROM);
55
56 /* Set EEPROM clock divide value*/
57 pEEPROM->CLKDIV = div;
58 }
59
60 /* Wait for interrupt */
61 void IP_EEPROM_WaitForIntStatus(IP_EEPROM_001_T *pEEPROM, uint32_t mask)
62 {
63 uint32_t status;
64 while (1) {
65 status = IP_EEPROM_GetIntStatus(pEEPROM);
66 if ((status & mask) == mask) {
67 break;
68 }
69 }
70 IP_EEPROM_ClearIntStatus(pEEPROM, mask);
71 }
72
73 /* Erase data in page register */
74 void IP_EEPROM_ErasePageRegister(IP_EEPROM_001_T *pEEPROM)
75 {
76 uint32_t i = 0;
77
78 IP_EEPROM_ClearIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
79
80 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_32BITS_WRITE);
81
82 IP_EEPROM_SetAddr(pEEPROM, 0, 0);
83
84 for (i = 0; i < EEPROM_PAGE_SIZE; i += 4) {
85 IP_EEPROM_WriteData(pEEPROM, 0);
86 IP_EEPROM_WaitForIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
87 }
88
89 }
90
91 /* Write data to page register */
92 uint32_t IP_EEPROM_WritePageRegister(IP_EEPROM_001_T *pEEPROM, uint16_t pageOffset,
93 uint8_t *pData, uint8_t wsize, uint32_t byteNum)
94 {
95 uint32_t i = 0;
96 uint32_t mask = (1 << (8 * wsize)) - 1;
97
98 IP_EEPROM_ClearIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
99
100 if (wsize == 1) {
101 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_8BITS_WRITE);
102 }
103 else if (wsize == 2) {
104 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_16BITS_WRITE);
105 }
106 else {
107 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_32BITS_WRITE);
108 }
109
110 IP_EEPROM_SetAddr(pEEPROM, 0, pageOffset);
111
112 for (i = 0; i < byteNum; i += wsize) {
113 IP_EEPROM_WriteData(pEEPROM, (*(uint32_t *) (&pData[i])) & mask);
114 IP_EEPROM_WaitForIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
115 }
116
117 return i;
118 }
119
120 /* Write data from page register to non-volatile memory */
121 void IP_EEPROM_EraseProgramPage(IP_EEPROM_001_T *pEEPROM, uint16_t pageAddr)
122 {
123 IP_EEPROM_ClearIntStatus(pEEPROM, EEPROM_CMD_ERASE_PRG_PAGE);
124 IP_EEPROM_SetAddr(pEEPROM, pageAddr, 0);
125 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_ERASE_PRG_PAGE);
126 IP_EEPROM_WaitForIntStatus(pEEPROM, EEPROM_INT_ENDOFPROG);
127 }
128
129 /* Read data from non-volatile memory */
130 uint32_t IP_EEPROM_ReadPage(IP_EEPROM_001_T *pEEPROM,
131 uint16_t pageOffset,
132 uint16_t pageAddr,
133 uint8_t *pData,
134 uint8_t rsize,
135 uint32_t byteNum)
136 {
137 uint32_t i;
138 uint32_t mask = (1 << (8 * rsize)) - 1;
139
140 IP_EEPROM_ClearIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
141 IP_EEPROM_SetAddr(pEEPROM, pageAddr, pageOffset);
142
143 if (rsize == 1) {
144 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_8BITS_READ | EEPROM_CMD_RDPREFETCH);
145 }
146 else if (rsize == 2) {
147 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_16BITS_READ | EEPROM_CMD_RDPREFETCH);
148 }
149 else {
150 IP_EEPROM_SetCmd(pEEPROM, EEPROM_CMD_32BITS_READ | EEPROM_CMD_RDPREFETCH);
151 }
152
153 /* read and store data in buffer */
154 for (i = 0; i < byteNum; i += rsize) {
155 (*(uint32_t *) (&pData[i]) ) &= ~mask;
156 (*(uint32_t *) (&pData[i]) ) |= (IP_EEPROM_ReadData(pEEPROM) & mask);
157 IP_EEPROM_WaitForIntStatus(pEEPROM, EEPROM_INT_ENDOFRW);
158 }
159 return i;
160 }
161
162 /* Write data to EEPROM at specific address */
163 Status IP_EEPROM_Write(IP_EEPROM_001_T *pEEPROM,
164 uint16_t pageOffset,
165 uint16_t pageAddr,
166 void *pData,
167 IP_EEPROM_RWSIZE_T wsize,
168 uint32_t byteNum)
169 {
170 uint32_t wTotalByteNum = 0;
171 uint32_t wOffset = (pageOffset & (EEPROM_PAGE_SIZE - 1));
172 uint32_t wByteNum = EEPROM_PAGE_SIZE - wOffset;
173 while (byteNum) {
174 if (wByteNum > byteNum) {
175 wByteNum = byteNum;
176 }
177 /* update data to page register */
178 IP_EEPROM_WritePageRegister(pEEPROM, wOffset,
179 &((uint8_t *) pData)[wTotalByteNum], (uint8_t) wsize, wByteNum);
180 IP_EEPROM_EraseProgramPage(pEEPROM, pageAddr);
181 wTotalByteNum += wByteNum;
182 byteNum -= wByteNum;
183
184 /* Change to next page */
185 pageAddr++;
186 wOffset = 0;
187 wByteNum = EEPROM_PAGE_SIZE;
188 }
189 return SUCCESS;
190 }
191
192 /* Read data to EEPROM at specific address */
193 Status IP_EEPROM_Read(IP_EEPROM_001_T *pEEPROM,
194 uint16_t pageOffset,
195 uint16_t pageAddr,
196 void *pData,
197 IP_EEPROM_RWSIZE_T rsize,
198 uint32_t byteNum)
199 {
200 uint32_t rTotalByteNum = 0;
201 uint32_t rOffset = (pageOffset & (EEPROM_PAGE_SIZE - 1));
202 uint32_t rByteNum = EEPROM_PAGE_SIZE - rOffset;
203 /* read and store data in buffer */
204 while (byteNum) {
205 if (rByteNum > byteNum) {
206 rByteNum = byteNum;
207 }
208 /* update data to page register */
209 IP_EEPROM_ReadPage(pEEPROM, rOffset, pageAddr,
210 &((uint8_t *) pData)[rTotalByteNum], (uint8_t) rsize, rByteNum);
211 rTotalByteNum += rByteNum;
212 byteNum -= rByteNum;
213
214 /* Change to next page */
215 pageAddr++;
216 rOffset = 0;
217 rByteNum = EEPROM_PAGE_SIZE;
218 }
219 return SUCCESS;
220 }
221
222 /* Erase a page at the specific address */
223 void IP_EEPROM_Erase(IP_EEPROM_001_T *pEEPROM, uint16_t pageAddr)
224 {
225 IP_EEPROM_ErasePageRegister(pEEPROM);
226
227 IP_EEPROM_EraseProgramPage(pEEPROM, pageAddr);
228 }