Newer
Older
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*
* Description: Communications Device Class ACM (Serial Port) over USB
* $Id: descriptors.h 31172 2017-10-05 19:05:57Z zach.metzinger $
*
*******************************************************************************
*/
/**
* @file mscmem.h
* @brief Memory routines used by the USB Mass Storage Class example.
* See the msc_mem_t structure in msc.h for function details.
* @details Functions are provided for using the internal RAM of the
* device or the external SPI flash memory. Use the SPIXF_DISK
* and RAM_DISK defines to select the desired memory at compile
* time.
*/
#include "mscmem.h"
#include <string.h>
#include <stdio.h>
#include "mx25.h"
/***** Definitions *****/
#define SPIXF_DISK 1
#define RAM_DISK 0
#define LBA_SIZE 512 /* Size of "logical blocks" in bytes */
#define LBA_SIZE_SHIFT 9 /* The shift value used to convert between addresses and block numbers */
/***** Global Data *****/
/***** File Scope Variables *****/
static int initialized = 0;
static int running = 0;
#if SPIXF_DISK
#define MX25_BAUD 5000000 /* SPI clock rate to communicate with the MX25 */
#define MX25_SECTOR_SIZE 4096 /* Number of bytes in one sector of the MX25 */
#define MX25_SECTOR_SIZE_SHIFT 12 /* The shift value used to convert between addresses and block numbers */
#define MX25_NUM_SECTORS 2048 /* Total number of sectors in the MX25 */
#define LBA_PER_SECTOR (MX25_SECTOR_SIZE >> LBA_SIZE_SHIFT)
#define INVALID_SECTOR MX25_NUM_SECTORS /* Use a sector number past the end of memory to indicate invalid */
/***** File Scope Variables *****/
static uint32_t sectorNum = INVALID_SECTOR;
static uint8_t sector[MX25_SECTOR_SIZE];
static int sectorDirty = 0;
/***** Function Prototypes *****/
static uint32_t getSectorNum(uint32_t lba);
static uint32_t getSectorAddr(uint32_t lba);
static uint32_t getSector(uint32_t num);
/******************************************************************************/
static uint32_t getSectorNum(uint32_t lba)
{
/* Absolute_address = lba * LBA_SIZE */
/* Sector_num = Absolute_address / MX25_SECTOR_SIZE */
/* Sector_num = lba * 512 / 4096 */
return lba >> (MX25_SECTOR_SIZE_SHIFT - LBA_SIZE_SHIFT);
}
/******************************************************************************/
static uint32_t getSectorAddr(uint32_t lba)
{
/* eight 512 byte blocks in each sector */
return (lba & (LBA_PER_SECTOR - 1)) << LBA_SIZE_SHIFT;
}
/******************************************************************************/
static uint32_t getSector(uint32_t num)
{
/* New sector requested? */
if(sectorNum != num) {
/* Is the current sector real? */
if(sectorNum != INVALID_SECTOR) {
/* Was it written to after it was read from memory? */
if(sectorDirty) {
/* Erase the old data. */
MX25_Erase(sectorNum << MX25_SECTOR_SIZE_SHIFT, MX25_Erase_4K);
/* Write the new */
MX25_Program_Page(sectorNum << MX25_SECTOR_SIZE_SHIFT, sector, MX25_SECTOR_SIZE, SPIXFC_WIDTH_4);
/* Mark data as clean */
sectorDirty = 0;
}
}
/* Requesting a new valid sector? */
if(num != INVALID_SECTOR) {
MX25_Read(num << MX25_SECTOR_SIZE_SHIFT, sector, MX25_SECTOR_SIZE, SPIXFC_WIDTH_4);
sectorDirty = 0;
sectorNum = num;
}
}
return 0;
}
/******************************************************************************/
int mscmem_init()
{
if(!initialized) {
MX25_Init();
MX25_Reset();
MX25_Quad(1);
initialized = 1;
}
return 0;
}
/******************************************************************************/
uint32_t mscmem_size(void)
{
/* Get number of 512 byte chunks the MX25 contains. */
return (MX25_SECTOR_SIZE >> LBA_SIZE_SHIFT) * MX25_NUM_SECTORS;
}
/******************************************************************************/
int mscmem_read(uint32_t lba, uint8_t* buffer)
{
uint32_t addr;
/* Convert to MX25 sector number. */
uint32_t sNum = getSectorNum(lba);
if(getSector(sNum)) {
/* Failed to write/read from MX25 */
return 1;
}
/* Get the offset into the current sector */
addr = getSectorAddr(lba);
memcpy(buffer, sector + addr, LBA_SIZE);
return 0;
}
/******************************************************************************/
int mscmem_write(uint32_t lba, uint8_t* buffer)
{
uint32_t addr;
/* Convert to MX25 sector number. */
uint32_t sNum = getSectorNum(lba);
if(getSector(sNum)) {
/* Failed to write/read from MX25 */
return 1;
}
/* Get the offset into the current sector */
addr = getSectorAddr(lba);
memcpy(sector + addr, buffer, LBA_SIZE);
sectorDirty = 1;
return 0;
}
/******************************************************************************/
int mscmem_start()
{
/* Turn on the MX25 if it is not already. */
if(!initialized) {
mscmem_init();
}
/* Check if the initialization succeeded. If it has, start running. */
if(initialized) {
running = 1;
}
/* Start should return fail (non-zero) if the memory cannot be initialized. */
return !initialized;
}
/******************************************************************************/
int mscmem_stop()
{
/* TODO - could shut down XIPF interface here. */
/* Flush the currently cached sector if necessary. */
if(getSector(INVALID_SECTOR)) {
return 1;
}
running = 0;
return 0;
}
/******************************************************************************/
int mscmem_ready()
{
return running;
}
#elif RAM_DISK
#define NUM_PAGES 0x100
static uint8_t mem[NUM_PAGES][LBA_SIZE];
/******************************************************************************/
int mscmem_init()
{
if(!initialized) {
initialized = 1;
#if (ERASE_MEMORY_ON_INIT)
memset(mem, 0, sizeof(mem));
#endif
}
return 0;
}
/******************************************************************************/
uint32_t mscmem_size(void)
{
return NUM_PAGES;
}
/******************************************************************************/
int mscmem_read(uint32_t lba, uint8_t* buffer)
{
if(lba >= NUM_PAGES) {
return 1;
}
memcpy(buffer, mem[lba], LBA_SIZE);
return 0;
}
/******************************************************************************/
int mscmem_write(uint32_t lba, uint8_t* buffer)
{
if(lba >= NUM_PAGES) {
return 1;
}
memcpy(mem[lba], buffer, LBA_SIZE);
return 0;
}
/******************************************************************************/
int mscmem_start()
{
/* Not much to do for this implementation. The RAM is always ready. */
if(!initialized) {
mscmem_init();
}
/* Check if the RAM has been initialized. If it has, start running. */
if(initialized) {
running = 1;
}
/* Start should return fail (non-zero) if the memory cannot be initialized. */
return !initialized;
}
/******************************************************************************/
int mscmem_stop()
{
/* Nothing to do for this implementation. All data is written as it is */
/* received so there are no pending writes that need to be flushed. */
running = 0;
return 0;
}
/******************************************************************************/
int mscmem_ready()
{
return running;
}
#else
#error "You must assign either RAM_DISK or SPIXF_DISK to 1."
#endif