LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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 */
52 {
53  /* Disable EEPROM power down mode */
55 
56  /* Set EEPROM clock divide value*/
57  pEEPROM->CLKDIV = div;
58 }
59 
60 /* Wait for interrupt */
62 {
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 */
75 {
76  uint32_t i = 0;
77 
79 
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);
87  }
88 
89 }
90 
91 /* Write data to page register */
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 
99 
100  if (wsize == 1) {
102  }
103  else if (wsize == 2) {
105  }
106  else {
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);
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 {
124  IP_EEPROM_SetAddr(pEEPROM, pageAddr, 0);
127 }
128 
129 /* Read data from non-volatile memory */
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 
141  IP_EEPROM_SetAddr(pEEPROM, pageAddr, pageOffset);
142 
143  if (rsize == 1) {
145  }
146  else if (rsize == 2) {
148  }
149  else {
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);
158  }
159  return i;
160 }
161 
162 /* Write data to EEPROM at specific address */
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 */
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 {
226 
227  IP_EEPROM_EraseProgramPage(pEEPROM, pageAddr);
228 }