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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
#include "MEDRamDisk.h"
#include <board.h>
#include <utility/trace.h>
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
/// Do copy and modify pointer
typedef void copyFunction(unsigned char **, unsigned char **, unsigned int);
//------------------------------------------------------------------------------
// Internal Functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Do copy for 8-byte aligned data
//------------------------------------------------------------------------------
static void AlignedCopy(unsigned char * *src,
unsigned char * *dst,
unsigned int len)
{
unsigned int *src32, *dst32;
src32 = (unsigned int*)*src;
dst32 = (unsigned int*)*dst;
for (;len > 0; len -= 8) {
*dst32 ++ = *src32 ++;
*dst32 ++ = *src32 ++;
}
*src = (unsigned char*)src32;
*dst = (unsigned char*)dst32;
}
//------------------------------------------------------------------------------
/// Do copy for byte-aligned data
//------------------------------------------------------------------------------
static void UnalignedCopy(unsigned char * *src,
unsigned char * *dst,
unsigned int len)
{
for (;len > 0; len --) {
*(*dst) ++ = *(*src) ++;
}
}
//------------------------------------------------------------------------------
//! \brief Reads a specified amount of data from a RAM Disk memory
//! \param media Pointer to a Media instance
//! \param address Address of the data to read
//! \param data Pointer to the buffer in which to store the retrieved
//! data
//! \param length Length of the buffer
//! \param callback Optional pointer to a callback function to invoke when
//! the operation is finished
//! \param argument Optional pointer to an argument for the callback
//! \return Operation result code
//------------------------------------------------------------------------------
static unsigned char MEDRamDisk_Read(Media *media,
unsigned int address,
void *data,
unsigned int length,
MediaCallback callback,
void *argument)
{
unsigned char *source;
unsigned char *dest;
copyFunction *pCpy;
// Check that the media is ready
if (media->state != MED_STATE_READY) {
TRACE_INFO("Media busy\n\r");
return MED_STATUS_BUSY;
}
// Check that the data to read is not too big
if ((length + address) > media->size) {
TRACE_WARNING("RamDisk_Read: Data too big: %u, 0x%08X\n\r",
length, address);
return MED_STATUS_ERROR;
}
// Enter Busy state
media->state = MED_STATE_BUSY;
// Source & Destination
source = (unsigned char *)(media->blockSize
* (address + media->baseAddress));
dest = (unsigned char *)data;
// Align/Unaligned copy
pCpy = (((unsigned int)source%4) == 0 && (media->blockSize%8) == 0)
? AlignedCopy : UnalignedCopy;
for (; length > 0; length --) {
pCpy(&source, &dest, media->blockSize);
}
// Leave the Busy state
media->state = MED_STATE_READY;
// Invoke callback
if (callback != 0) {
callback(argument, MED_STATUS_SUCCESS, 0, 0);
}
return MED_STATUS_SUCCESS;
}
//------------------------------------------------------------------------------
//! \brief Writes data on a Ram Disk media
//! \param media Pointer to a Media instance
//! \param address Address at which to write
//! \param data Pointer to the data to write
//! \param length Size of the data buffer
//! \param callback Optional pointer to a callback function to invoke when
//! the write operation terminates
//! \param argument Optional argument for the callback function
//! \return Operation result code
//! \see Media
//! \see MediaCallback
//------------------------------------------------------------------------------
static unsigned char MEDRamDisk_Write(Media *media,
unsigned int address,
void *data,
unsigned int length,
MediaCallback callback,
void *argument)
{
unsigned char *source;
unsigned char *dest;
copyFunction *pCpy;
//TRACE_DEBUG("RamDisk_Write\n\r");
// Check that the media if ready
if (media->state != MED_STATE_READY) {
TRACE_WARNING("RamDisk_Write: busy\n\r");
return MED_STATUS_BUSY;
}
// Check that the data to write is not too big
if ((length + address) > media->size) {
TRACE_WARNING("RamDisk_Write: Data too big\n\r");
return MED_STATUS_ERROR;
}
// Put the media in Busy state
media->state = MED_STATE_BUSY;
// Compute function parameters
source = (unsigned char *) data;
dest = (unsigned char *) (media->blockSize *
(media->baseAddress + address));
// Align/Unaligned copy
pCpy = (((unsigned int)source%4) == 0 && (media->blockSize%8) == 0)
? AlignedCopy : UnalignedCopy;
for (; length > 0; length --) {
pCpy(&source, &dest, media->blockSize);
}
// Leave the Busy state
media->state = MED_STATE_READY;
// Invoke the callback if it exists
if (callback != 0) {
callback(argument, MED_STATUS_SUCCESS, 0, 0);
}
return MED_STATUS_SUCCESS;
}
//------------------------------------------------------------------------------
// Exported Functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief Initializes a Media instance and the associated physical interface
//! \param media Pointer to the Media instance to initialize
//! \return 1 if initialize sucessfully, 0 if any error.
//! \see Media
//------------------------------------------------------------------------------
unsigned char MEDRamDisk_Initialize(Media *media,
unsigned int blockSize,
unsigned int baseAddress,
unsigned int size)
{
unsigned int bak_val;
unsigned int baseInBytes = baseAddress * blockSize;
unsigned int sizeInBytes = size * blockSize;
TRACE_INFO("RAM Disk init\n\r");
// If the address Initialized ?
bak_val = *((volatile unsigned int *) (baseInBytes));
*((volatile unsigned int *) (baseInBytes)) = 0xDEADBEEF;
if (*((volatile unsigned int *) baseInBytes) == 0xDEADBEEF) {
*((volatile unsigned int *) baseInBytes) = bak_val;
}
else {
TRACE_ERROR("Un-configured memory area %x ...\n\r", baseInBytes);
return 0;
}
baseInBytes += sizeInBytes - 4;
bak_val = *((volatile unsigned int *) (baseInBytes));
*((volatile unsigned int *) (baseInBytes)) = 0xAECDBE9F;
if (*((volatile unsigned int *)(baseInBytes)) == 0xAECDBE9F) {
*((volatile unsigned int *)(baseInBytes)) = bak_val;
}
else {
TRACE_ERROR("Un-configured memory area %x ...\n\r", baseInBytes);
return 0;
}
// Initialize media fields
media->write = MEDRamDisk_Write;
media->read = MEDRamDisk_Read;
media->lock = 0;
media->unlock = 0;
media->handler = 0;
media->flush = 0;
media->blockSize = blockSize;
media->baseAddress = baseAddress;
media->size = size;
media->mappedRD = 0;
media->mappedWR = 0;
media->protected = 0;
media->removable = 0;
media->state = MED_STATE_READY;
media->transfer.data = 0;
media->transfer.address = 0;
media->transfer.length = 0;
media->transfer.callback = 0;
media->transfer.argument = 0;
return 1;
}
|