1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
#ifndef trace_LEVEL
|
31 |
|
|
#define trace_LEVEL trace_INFO
|
32 |
|
|
#endif
|
33 |
|
|
|
34 |
|
|
//------------------------------------------------------------------------------
|
35 |
|
|
// Headers
|
36 |
|
|
//------------------------------------------------------------------------------
|
37 |
|
|
|
38 |
|
|
#include "eefc.h"
|
39 |
|
|
|
40 |
|
|
#ifdef BOARD_FLASH_EEFC
|
41 |
|
|
|
42 |
|
|
#include <utility/assert.h>
|
43 |
|
|
#include <utility/trace.h>
|
44 |
|
|
|
45 |
|
|
//------------------------------------------------------------------------------
|
46 |
|
|
// Global functions
|
47 |
|
|
//------------------------------------------------------------------------------
|
48 |
|
|
|
49 |
|
|
//------------------------------------------------------------------------------
|
50 |
|
|
/// Enables the flash ready interrupt source on the EEFC peripheral.
|
51 |
|
|
//------------------------------------------------------------------------------
|
52 |
|
|
void EFC_EnableFrdyIt(void)
|
53 |
|
|
{
|
54 |
|
|
AT91C_BASE_EFC->EFC_FMR |= AT91C_EFC_FRDY;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
//------------------------------------------------------------------------------
|
58 |
|
|
/// Disables the flash ready interrupt source on the EEFC peripheral.
|
59 |
|
|
//------------------------------------------------------------------------------
|
60 |
|
|
void EFC_DisableFrdyIt(void)
|
61 |
|
|
{
|
62 |
|
|
AT91C_BASE_EFC->EFC_FMR &= ~AT91C_EFC_FRDY;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
//------------------------------------------------------------------------------
|
66 |
|
|
/// Translates the given address page and offset values. The resulting
|
67 |
|
|
/// values are stored in the provided variables if they are not null.
|
68 |
|
|
/// \param address Address to translate.
|
69 |
|
|
/// \param pPage First page accessed.
|
70 |
|
|
/// \param pOffset Byte offset in first page.
|
71 |
|
|
//------------------------------------------------------------------------------
|
72 |
|
|
void EFC_TranslateAddress(
|
73 |
|
|
unsigned int address,
|
74 |
|
|
unsigned short *pPage,
|
75 |
|
|
unsigned short *pOffset)
|
76 |
|
|
{
|
77 |
|
|
unsigned short page;
|
78 |
|
|
unsigned short offset;
|
79 |
|
|
|
80 |
|
|
SANITY_CHECK(address >= AT91C_IFLASH);
|
81 |
|
|
SANITY_CHECK(address <= (AT91C_IFLASH + AT91C_IFLASH_SIZE));
|
82 |
|
|
|
83 |
|
|
// Calculate page & offset
|
84 |
|
|
page = (address - AT91C_IFLASH) / AT91C_IFLASH_PAGE_SIZE;
|
85 |
|
|
offset = (address - AT91C_IFLASH) % AT91C_IFLASH_PAGE_SIZE;
|
86 |
|
|
trace_LOG(trace_DEBUG,
|
87 |
|
|
"-D- Translated 0x%08X to page=%d and offset=%d\n\r",
|
88 |
|
|
address, page, offset);
|
89 |
|
|
|
90 |
|
|
// Store values
|
91 |
|
|
if (pPage) {
|
92 |
|
|
|
93 |
|
|
*pPage = page;
|
94 |
|
|
}
|
95 |
|
|
if (pOffset) {
|
96 |
|
|
|
97 |
|
|
*pOffset = offset;
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
//------------------------------------------------------------------------------
|
102 |
|
|
/// Computes the address of a flash access given the page and offset.
|
103 |
|
|
/// \param page Page number.
|
104 |
|
|
/// \param offset Byte offset inside page.
|
105 |
|
|
/// \param pAddress Computed address (optional).
|
106 |
|
|
//------------------------------------------------------------------------------
|
107 |
|
|
void EFC_ComputeAddress(
|
108 |
|
|
unsigned short page,
|
109 |
|
|
unsigned short offset,
|
110 |
|
|
unsigned int *pAddress)
|
111 |
|
|
{
|
112 |
|
|
unsigned int address;
|
113 |
|
|
|
114 |
|
|
SANITY_CHECK(page <= AT91C_IFLASH_NB_OF_PAGES);
|
115 |
|
|
SANITY_CHECK(offset < AT91C_IFLASH_PAGE_SIZE);
|
116 |
|
|
|
117 |
|
|
// Compute address
|
118 |
|
|
address = AT91C_IFLASH + page * AT91C_IFLASH_PAGE_SIZE + offset;
|
119 |
|
|
|
120 |
|
|
// Store result
|
121 |
|
|
if (pAddress) {
|
122 |
|
|
|
123 |
|
|
*pAddress = address;
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
//------------------------------------------------------------------------------
|
128 |
|
|
/// Starts the executing the given command on the EEFC. This function returns
|
129 |
|
|
/// as soon as the command is started. It does NOT set the FMCN field automatically.
|
130 |
|
|
/// \param command Command to execute.
|
131 |
|
|
/// \param argument Command argument (should be 0 if not used).
|
132 |
|
|
//------------------------------------------------------------------------------
|
133 |
|
|
void EFC_StartCommand(unsigned char command, unsigned short argument)
|
134 |
|
|
{
|
135 |
|
|
// Check command & argument
|
136 |
|
|
switch (command) {
|
137 |
|
|
|
138 |
|
|
case AT91C_EFC_FCMD_WP:
|
139 |
|
|
case AT91C_EFC_FCMD_WPL:
|
140 |
|
|
case AT91C_EFC_FCMD_EWP:
|
141 |
|
|
case AT91C_EFC_FCMD_EWPL:
|
142 |
|
|
case AT91C_EFC_FCMD_EPL:
|
143 |
|
|
case AT91C_EFC_FCMD_EPA:
|
144 |
|
|
case AT91C_EFC_FCMD_SLB:
|
145 |
|
|
case AT91C_EFC_FCMD_CLB:
|
146 |
|
|
ASSERT(argument < AT91C_IFLASH_NB_OF_PAGES,
|
147 |
|
|
"-F- Embedded flash has only %d pages\n\r",
|
148 |
|
|
AT91C_IFLASH_NB_OF_PAGES);
|
149 |
|
|
break;
|
150 |
|
|
|
151 |
|
|
case AT91C_EFC_FCMD_SFB:
|
152 |
|
|
case AT91C_EFC_FCMD_CFB:
|
153 |
|
|
ASSERT(argument < EFC_NUM_GPNVMS, "-F- Embedded flash has only %d GPNVMs\n\r", EFC_NUM_GPNVMS);
|
154 |
|
|
break;
|
155 |
|
|
|
156 |
|
|
case AT91C_EFC_FCMD_GETD:
|
157 |
|
|
case AT91C_EFC_FCMD_EA:
|
158 |
|
|
case AT91C_EFC_FCMD_GLB:
|
159 |
|
|
case AT91C_EFC_FCMD_GFB:
|
160 |
|
|
ASSERT(argument == 0, "-F- Argument is meaningless for the given command.\n\r");
|
161 |
|
|
break;
|
162 |
|
|
|
163 |
|
|
default: ASSERT(0, "-F- Unknown command %d\n\r", command);
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
// Start commandEmbedded flash
|
167 |
|
|
ASSERT((AT91C_BASE_EFC->EFC_FSR & AT91C_EFC_FRDY) == AT91C_EFC_FRDY, "-F- EEFC is not ready\n\r");
|
168 |
|
|
AT91C_BASE_EFC->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
//------------------------------------------------------------------------------
|
172 |
|
|
/// Performs the given command and wait until its completion (or an error).
|
173 |
|
|
/// Returns 0 if successful; otherwise returns an error code.
|
174 |
|
|
/// \param command Command to perform.
|
175 |
|
|
/// \param argument Optional command argument.
|
176 |
|
|
//------------------------------------------------------------------------------
|
177 |
|
|
#ifdef __ICCARM__
|
178 |
|
|
__ramfunc
|
179 |
|
|
#else
|
180 |
|
|
__attribute__ ((section (".ramfunc")))
|
181 |
|
|
#endif
|
182 |
|
|
unsigned char EFC_PerformCommand(unsigned char command, unsigned short argument)
|
183 |
|
|
{
|
184 |
|
|
unsigned int status;
|
185 |
|
|
|
186 |
|
|
#ifdef BOARD_FLASH_IAP_ADDRESS
|
187 |
|
|
// Pointer on IAP function in ROM
|
188 |
|
|
static void (*IAP_PerformCommand)(unsigned int);
|
189 |
|
|
IAP_PerformCommand = (void (*)(unsigned int)) *((unsigned int *) BOARD_FLASH_IAP_ADDRESS);
|
190 |
|
|
|
191 |
|
|
// Check if IAP function is implemented (opcode in SWI != 'b' or 'ldr') */
|
192 |
|
|
if ((((((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xEA) &&
|
193 |
|
|
(((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xE5)) {
|
194 |
|
|
|
195 |
|
|
IAP_PerformCommand((0x5A << 24) | (argument << 8) | command);
|
196 |
|
|
return (AT91C_BASE_EFC->EFC_FSR & (AT91C_EFC_LOCKE | AT91C_EFC_FCMDE));
|
197 |
|
|
}
|
198 |
|
|
#endif
|
199 |
|
|
|
200 |
|
|
AT91C_BASE_EFC->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
|
201 |
|
|
do {
|
202 |
|
|
|
203 |
|
|
status = AT91C_BASE_EFC->EFC_FSR;
|
204 |
|
|
}
|
205 |
|
|
while ((status & AT91C_EFC_FRDY) != AT91C_EFC_FRDY);
|
206 |
|
|
|
207 |
|
|
return (status & (AT91C_EFC_LOCKE | AT91C_EFC_FCMDE));
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
//------------------------------------------------------------------------------
|
211 |
|
|
/// Returns the current status of the EEFC. Keep in mind that this function clears
|
212 |
|
|
/// the value of some status bits (LOCKE, PROGE).
|
213 |
|
|
//------------------------------------------------------------------------------
|
214 |
|
|
unsigned int EFC_GetStatus(void)
|
215 |
|
|
{
|
216 |
|
|
return AT91C_BASE_EFC->EFC_FSR;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
//------------------------------------------------------------------------------
|
220 |
|
|
/// Returns the result of the last executed command.
|
221 |
|
|
//------------------------------------------------------------------------------
|
222 |
|
|
unsigned int EFC_GetResult(void) {
|
223 |
|
|
|
224 |
|
|
return AT91C_BASE_EFC->EFC_FRR;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
#endif //#ifdef BOARD_FLASH_EEFC
|
228 |
|
|
|