1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/**********************************************************************
* $Id$ lpc_eeprom.c 2011-06-02
*//**
* @file lpc_eeprom.c
* @brief Contains all functions support for EEPROM firmware library on
* LPC
* @version 1.0
* @date 02. June. 2011
* @author NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers. This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/
/* Peripheral group ----------------------------------------------------------- */
/** @addtogroup EEPROM
* @{
*/
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _EEPROM
/* Includes ------------------------------------------------------------------- */
#include "lpc_eeprom.h"
#include "lpc_clkpwr.h"
/* Public Functions ----------------------------------------------------------- */
/*********************************************************************//**
* @brief Initial EEPROM
* @param[in] None
* @return None
**********************************************************************/
void EEPROM_Init(void)
{
uint32_t val, cclk;
LPC_EEPROM->PWRDWN = 0x0;
/* EEPROM is automate turn on after reset */
/* Setting clock:
* EEPROM required a 375kHz. This clock is generated by dividing the
* system bus clock.
*/
cclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU);
val = (cclk/375000)-1;
LPC_EEPROM->CLKDIV = val;
/* Setting wait state */
val = ((((cclk / 1000000) * 15) / 1000) + 1);
val |= (((((cclk / 1000000) * 55) / 1000) + 1) << 8);
val |= (((((cclk / 1000000) * 35) / 1000) + 1) << 16);
LPC_EEPROM->WSTATE = val;
}
/*********************************************************************//**
* @brief Write data to EEPROM at specific address
* @param[in] page_offset offset of data in page register(0 - 63)
* page_address page address (0-62)
* mode Write mode, should be:
* - MODE_8_BIT : write 8 bit mode
* - MODE_16_BIT : write 16 bit mode
* - MODE_32_BIT : write 32 bit mode
* data buffer that contain data that will be written to buffer
* count number written data
* @return None
* @note This function actually write data into EEPROM memory and automatically
* write into next page if current page is overflowed
**********************************************************************/
void EEPROM_Write(uint16_t page_offset, uint16_t page_address, void* data, EEPROM_Mode_Type mode, uint32_t count)
{
uint32_t i;
uint8_t *tmp8 = (uint8_t *)data;
uint16_t *tmp16 = (uint16_t *)data;
uint32_t *tmp32 = (uint32_t *)data;
LPC_EEPROM->INT_CLR_STATUS = ((1 << EEPROM_ENDOF_RW)|(1 << EEPROM_ENDOF_PROG));
//check page_offset
if(mode == MODE_16_BIT){
if((page_offset & 0x01)!=0) while(1);
}
else if(mode == MODE_32_BIT){
if((page_offset & 0x03)!=0) while(1);
}
LPC_EEPROM->ADDR = EEPROM_PAGE_OFFSET(page_offset);
for(i=0;i<count;i++)
{
//update data to page register
if(mode == MODE_8_BIT){
LPC_EEPROM->CMD = EEPROM_CMD_8_BIT_WRITE;
LPC_EEPROM -> WDATA = *tmp8;
tmp8++;
page_offset +=1;
}
else if(mode == MODE_16_BIT){
LPC_EEPROM->CMD = EEPROM_CMD_16_BIT_WRITE;
LPC_EEPROM -> WDATA = *tmp16;
tmp16++;
page_offset +=2;
}
else{
LPC_EEPROM->CMD = EEPROM_CMD_32_BIT_WRITE;
LPC_EEPROM -> WDATA = *tmp32;
tmp32++;
page_offset +=4;
}
while(!((LPC_EEPROM->INT_STATUS >> EEPROM_ENDOF_RW)&0x01));
LPC_EEPROM->INT_CLR_STATUS = (1 << EEPROM_ENDOF_RW);
if((page_offset >= EEPROM_PAGE_SIZE)|(i==count-1)){
//update to EEPROM memory
LPC_EEPROM->INT_CLR_STATUS = (0x1 << EEPROM_ENDOF_PROG);
LPC_EEPROM->ADDR = EEPROM_PAGE_ADRESS(page_address);
LPC_EEPROM->CMD = EEPROM_CMD_ERASE_PRG_PAGE;
while(!((LPC_EEPROM->INT_STATUS >> EEPROM_ENDOF_PROG)&0x01));
LPC_EEPROM->INT_CLR_STATUS = (1 << EEPROM_ENDOF_PROG);
}
if(page_offset >= EEPROM_PAGE_SIZE)
{
page_offset = 0;
page_address +=1;
LPC_EEPROM->ADDR =0;
if(page_address > EEPROM_PAGE_NUM - 1) page_address = 0;
}
}
}
/*********************************************************************//**
* @brief Read data to EEPROM at specific address
* @param[in]
* data buffer that contain data that will be written to buffer
* mode Read mode, should be:
* - MODE_8_BIT : read 8 bit mode
* - MODE_16_BIT : read 16 bit mode
* - MODE_32_BIT : read 32 bit mode
* count number read data (bytes)
* @return data buffer that contain data that will be read to buffer
**********************************************************************/
void EEPROM_Read(uint16_t page_offset, uint16_t page_address, void* data, EEPROM_Mode_Type mode, uint32_t count)
{
uint32_t i;
uint8_t *tmp8 = (uint8_t *)data;
uint16_t *tmp16 = (uint16_t *)data;
uint32_t *tmp32 = (uint32_t *)data;
LPC_EEPROM->INT_CLR_STATUS = ((1 << EEPROM_ENDOF_RW)|(1 << EEPROM_ENDOF_PROG));
LPC_EEPROM->ADDR = EEPROM_PAGE_ADRESS(page_address)|EEPROM_PAGE_OFFSET(page_offset);
if(mode == MODE_8_BIT)
LPC_EEPROM->CMD = EEPROM_CMD_8_BIT_READ|EEPROM_CMD_RDPREFETCH;
else if(mode == MODE_16_BIT){
LPC_EEPROM->CMD = EEPROM_CMD_16_BIT_READ|EEPROM_CMD_RDPREFETCH;
//check page_offset
if((page_offset &0x01)!=0)
return;
}
else{
LPC_EEPROM->CMD = EEPROM_CMD_32_BIT_READ|EEPROM_CMD_RDPREFETCH;
//page_offset must be a multiple of 0x04
if((page_offset & 0x03)!=0)
return;
}
//read and store data in buffer
for(i=0;i<count;i++){
if(mode == MODE_8_BIT){
*tmp8 = (uint8_t)(LPC_EEPROM -> RDATA);
tmp8++;
page_offset +=1;
}
else if (mode == MODE_16_BIT)
{
*tmp16 = (uint16_t)(LPC_EEPROM -> RDATA);
tmp16++;
page_offset +=2;
}
else{
*tmp32 = (uint32_t)(LPC_EEPROM ->RDATA);
tmp32++;
page_offset +=4;
}
while(!((LPC_EEPROM->INT_STATUS >> EEPROM_ENDOF_RW)&0x01));
LPC_EEPROM->INT_CLR_STATUS = (1 << EEPROM_ENDOF_RW);
if((page_offset >= EEPROM_PAGE_SIZE) && (i < count - 1)) {
page_offset = 0;
page_address++;
LPC_EEPROM->ADDR = EEPROM_PAGE_ADRESS(page_address)|EEPROM_PAGE_OFFSET(page_offset);
if(mode == MODE_8_BIT)
LPC_EEPROM->CMD = EEPROM_CMD_8_BIT_READ|EEPROM_CMD_RDPREFETCH;
else if(mode == MODE_16_BIT)
LPC_EEPROM->CMD = EEPROM_CMD_16_BIT_READ|EEPROM_CMD_RDPREFETCH;
else
LPC_EEPROM->CMD = EEPROM_CMD_32_BIT_READ|EEPROM_CMD_RDPREFETCH;
}
}
}
/*********************************************************************//**
* @brief Erase a page at the specific address
* @param[in] address EEPROM page address (0-62)
* @return data buffer that contain data that will be read to buffer
**********************************************************************/
void EEPROM_Erase(uint16_t page_address)
{
uint32_t i;
uint32_t count = EEPROM_PAGE_SIZE/4;
LPC_EEPROM->INT_CLR_STATUS = ((1 << EEPROM_ENDOF_RW)|(1 << EEPROM_ENDOF_PROG));
//clear page register
LPC_EEPROM->ADDR = EEPROM_PAGE_OFFSET(0);
LPC_EEPROM->CMD = EEPROM_CMD_32_BIT_WRITE;
for(i=0;i<count;i++)
{
LPC_EEPROM->WDATA = 0;
while(!((LPC_EEPROM->INT_STATUS >> EEPROM_ENDOF_RW)&0x01));
LPC_EEPROM->INT_CLR_STATUS = (1 << EEPROM_ENDOF_RW);
}
LPC_EEPROM->INT_CLR_STATUS = (0x1 << EEPROM_ENDOF_PROG);
LPC_EEPROM->ADDR = EEPROM_PAGE_ADRESS(page_address);
LPC_EEPROM->CMD = EEPROM_CMD_ERASE_PRG_PAGE;
while(!((LPC_EEPROM->INT_STATUS >> EEPROM_ENDOF_PROG)&0x01));
LPC_EEPROM->INT_CLR_STATUS = (1 << EEPROM_ENDOF_PROG);
}
/*********************************************************************//**
* @brief Enable/Disable EEPROM power down mdoe
* @param[in] NewState PowerDown mode state, should be:
* - ENABLE: Enable power down mode
* - DISABLE: Disable power down mode
* @return None
**********************************************************************/
void EEPROM_PowerDown(FunctionalState NewState)
{
if(NewState == ENABLE)
LPC_EEPROM->PWRDWN = 0x1;
else
LPC_EEPROM->PWRDWN = 0x0;
}
#endif /*_EEPROM*/
/**
* @}
*/
/* --------------------------------- End Of File ------------------------------ */
|