source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifi_dnld/src/SOFTWARE_FRAMEWORK/SERVICES/MEMORY/CTRL_ACCESS/ctrl_access.c @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 11.8 KB
Line 
1/* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2
3/*This file is prepared for Doxygen automatic documentation generation.*/
4/*! \file *********************************************************************
5 *
6 * \brief Abstraction layer for memory interfaces.
7 *
8 * This module contains the interfaces:
9 *   - MEM <-> USB;
10 *   - MEM <-> RAM;
11 *   - MEM <-> MEM.
12 *
13 * This module may be configured and expanded to support the following features:
14 *   - write-protected globals;
15 *   - password-protected data;
16 *   - specific features;
17 *   - etc.
18 *
19 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
20 * - Supported devices:  All AVR32 devices can be used.
21 * - AppNote:
22 *
23 * \author               Atmel Corporation: http://www.atmel.com \n
24 *                       Support and FAQ: http://support.atmel.no/
25 *
26 ******************************************************************************/
27
28/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
32 *
33 * 1. Redistributions of source code must retain the above copyright notice, this
34 * list of conditions and the following disclaimer.
35 *
36 * 2. Redistributions in binary form must reproduce the above copyright notice,
37 * this list of conditions and the following disclaimer in the documentation
38 * and/or other materials provided with the distribution.
39 *
40 * 3. The name of Atmel may not be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * 4. This software may only be redistributed and used in connection with an Atmel
44 * AVR product.
45 *
46 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
47 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
49 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
50 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
56 *
57 */
58
59//_____ I N C L U D E S ____________________________________________________
60
61#include "compiler.h"
62#include "preprocessor.h"
63#ifdef FREERTOS_USED
64#include "FreeRTOS.h"
65#include "semphr.h"
66#endif
67#include "ctrl_access.h"
68
69
70//_____ D E F I N I T I O N S ______________________________________________
71
72#ifdef FREERTOS_USED
73
74/*! \name LUN Access Protection Macros
75 */
76//! @{
77
78/*! \brief Locks accesses to LUNs.
79 *
80 * \return \c TRUE if the access was successfully locked, else \c FALSE.
81 */
82#define Ctrl_access_lock()    ctrl_access_lock()
83
84/*! \brief Unlocks accesses to LUNs.
85 */
86#define Ctrl_access_unlock()  xSemaphoreGive(ctrl_access_semphr)
87
88//! @}
89
90//! Handle to the semaphore protecting accesses to LUNs.
91static xSemaphoreHandle ctrl_access_semphr = NULL;
92
93#else
94
95/*! \name LUN Access Protection Macros
96 */
97//! @{
98
99/*! \brief Locks accesses to LUNs.
100 *
101 * \return \c TRUE if the access was successfully locked, else \c FALSE.
102 */
103#define Ctrl_access_lock()    TRUE
104
105/*! \brief Unlocks accesses to LUNs.
106 */
107#define Ctrl_access_unlock()
108
109//! @}
110
111#endif  // FREERTOS_USED
112
113
114#if MAX_LUN
115
116/*! \brief Initializes an entry of the LUN descriptor table.
117 *
118 * \param lun Logical Unit Number.
119 *
120 * \return LUN descriptor table entry initializer.
121 */
122#if ACCESS_USB == ENABLED && ACCESS_MEM_TO_RAM == ENABLED
123#define Lun_desc_entry(lun) \
124  {\
125    TPASTE3(Lun_, lun, _test_unit_ready),\
126    TPASTE3(Lun_, lun, _read_capacity),\
127    TPASTE3(Lun_, lun, _wr_protect),\
128    TPASTE3(Lun_, lun, _removal),\
129    TPASTE3(Lun_, lun, _usb_read_10),\
130    TPASTE3(Lun_, lun, _usb_write_10),\
131    TPASTE3(Lun_, lun, _mem_2_ram),\
132    TPASTE3(Lun_, lun, _ram_2_mem),\
133    TPASTE3(LUN_, lun, _NAME)\
134  }
135#elif ACCESS_USB == ENABLED
136#define Lun_desc_entry(lun) \
137  {\
138    TPASTE3(Lun_, lun, _test_unit_ready),\
139    TPASTE3(Lun_, lun, _read_capacity),\
140    TPASTE3(Lun_, lun, _wr_protect),\
141    TPASTE3(Lun_, lun, _removal),\
142    TPASTE3(Lun_, lun, _usb_read_10),\
143    TPASTE3(Lun_, lun, _usb_write_10),\
144    TPASTE3(LUN_, lun, _NAME)\
145  }
146#elif ACCESS_MEM_TO_RAM == ENABLED
147#define Lun_desc_entry(lun) \
148  {\
149    TPASTE3(Lun_, lun, _test_unit_ready),\
150    TPASTE3(Lun_, lun, _read_capacity),\
151    TPASTE3(Lun_, lun, _wr_protect),\
152    TPASTE3(Lun_, lun, _removal),\
153    TPASTE3(Lun_, lun, _mem_2_ram),\
154    TPASTE3(Lun_, lun, _ram_2_mem),\
155    TPASTE3(LUN_, lun, _NAME)\
156  }
157#else
158#define Lun_desc_entry(lun) \
159  {\
160    TPASTE3(Lun_, lun, _test_unit_ready),\
161    TPASTE3(Lun_, lun, _read_capacity),\
162    TPASTE3(Lun_, lun, _wr_protect),\
163    TPASTE3(Lun_, lun, _removal),\
164    TPASTE3(LUN_, lun, _NAME)\
165  }
166#endif
167
168//! LUN descriptor table.
169static const struct
170{
171  Ctrl_status (*test_unit_ready)(void);
172  Ctrl_status (*read_capacity)(U32 *);
173  Bool (*wr_protect)(void);
174  Bool (*removal)(void);
175#if ACCESS_USB == ENABLED
176  Ctrl_status (*usb_read_10)(U32, U16);
177  Ctrl_status (*usb_write_10)(U32, U16);
178#endif
179#if ACCESS_MEM_TO_RAM == ENABLED
180  Ctrl_status (*mem_2_ram)(U32, void *);
181  Ctrl_status (*ram_2_mem)(U32, const void *);
182#endif
183  const char *name;
184} lun_desc[MAX_LUN] =
185{
186#if LUN_0 == ENABLE
187  Lun_desc_entry(0),
188#endif
189#if LUN_1 == ENABLE
190  Lun_desc_entry(1),
191#endif
192#if LUN_2 == ENABLE
193  Lun_desc_entry(2),
194#endif
195#if LUN_3 == ENABLE
196  Lun_desc_entry(3),
197#endif
198#if LUN_4 == ENABLE
199  Lun_desc_entry(4),
200#endif
201#if LUN_5 == ENABLE
202  Lun_desc_entry(5),
203#endif
204#if LUN_6 == ENABLE
205  Lun_desc_entry(6),
206#endif
207#if LUN_7 == ENABLE
208  Lun_desc_entry(7)
209#endif
210};
211
212#endif
213
214
215#if GLOBAL_WR_PROTECT == ENABLED
216Bool g_wr_protect;
217#endif
218
219
220/*! \name Control Interface
221 */
222//! @{
223
224
225#ifdef FREERTOS_USED
226
227Bool ctrl_access_init(void)
228{
229  // If the handle to the protecting semaphore is not valid,
230  if (!ctrl_access_semphr)
231  {
232    // try to create the semaphore.
233    vSemaphoreCreateBinary(ctrl_access_semphr);
234
235    // If the semaphore could not be created, there is no backup solution.
236    if (!ctrl_access_semphr) return FALSE;
237  }
238
239  return TRUE;
240}
241
242
243/*! \brief Locks accesses to LUNs.
244 *
245 * \return \c TRUE if the access was successfully locked, else \c FALSE.
246 */
247static Bool ctrl_access_lock(void)
248{
249  // If the semaphore could not be created, there is no backup solution.
250  if (!ctrl_access_semphr) return FALSE;
251
252  // Wait for the semaphore.
253  while (!xSemaphoreTake(ctrl_access_semphr, portMAX_DELAY));
254
255  return TRUE;
256}
257
258#endif  // FREERTOS_USED
259
260
261U8 get_nb_lun(void)
262{
263#if MEM_USB == ENABLE
264  U8 nb_lun;
265
266  if (!Ctrl_access_lock()) return MAX_LUN;
267
268  nb_lun = MAX_LUN + host_get_lun();
269
270  Ctrl_access_unlock();
271
272  return nb_lun;
273#else
274  return MAX_LUN;
275#endif
276}
277
278
279U8 get_cur_lun(void)
280{
281  return LUN_ID_0;
282}
283
284
285Ctrl_status mem_test_unit_ready(U8 lun)
286{
287  Ctrl_status status;
288
289  if (!Ctrl_access_lock()) return CTRL_FAIL;
290
291  status =
292#if MAX_LUN
293           (lun < MAX_LUN) ? lun_desc[lun].test_unit_ready() :
294#endif
295#if LUN_USB == ENABLE
296                             Lun_usb_test_unit_ready(lun - LUN_ID_USB);
297#else
298                             CTRL_FAIL;
299#endif
300
301  Ctrl_access_unlock();
302
303  return status;
304}
305
306
307Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector)
308{
309  Ctrl_status status;
310
311  if (!Ctrl_access_lock()) return CTRL_FAIL;
312
313  status =
314#if MAX_LUN
315           (lun < MAX_LUN) ? lun_desc[lun].read_capacity(u32_nb_sector) :
316#endif
317#if LUN_USB == ENABLE
318                             Lun_usb_read_capacity(lun - LUN_ID_USB, u32_nb_sector);
319#else
320                             CTRL_FAIL;
321#endif
322
323  Ctrl_access_unlock();
324
325  return status;
326}
327
328
329U8 mem_sector_size(U8 lun)
330{
331  U8 sector_size;
332
333  if (!Ctrl_access_lock()) return 0;
334
335  sector_size =
336#if MAX_LUN
337                (lun < MAX_LUN) ? 1 :
338#endif
339#if LUN_USB == ENABLE
340                                  Lun_usb_read_sector_size(lun - LUN_ID_USB);
341#else
342                                  0;
343#endif
344
345  Ctrl_access_unlock();
346
347  return sector_size;
348}
349
350
351Bool mem_wr_protect(U8 lun)
352{
353  Bool wr_protect;
354
355  if (!Ctrl_access_lock()) return TRUE;
356
357  wr_protect =
358#if MAX_LUN
359               (lun < MAX_LUN) ? lun_desc[lun].wr_protect() :
360#endif
361#if LUN_USB == ENABLE
362                                 Lun_usb_wr_protect(lun - LUN_ID_USB);
363#else
364                                 TRUE;
365#endif
366
367  Ctrl_access_unlock();
368
369  return wr_protect;
370}
371
372
373Bool mem_removal(U8 lun)
374{
375  Bool removal;
376
377  if (!Ctrl_access_lock()) return TRUE;
378
379  removal =
380#if MAX_LUN
381            (lun < MAX_LUN) ? lun_desc[lun].removal() :
382#endif
383#if LUN_USB == ENABLE
384                              Lun_usb_removal();
385#else
386                              TRUE;
387#endif
388
389  Ctrl_access_unlock();
390
391  return removal;
392}
393
394
395const char *mem_name(U8 lun)
396{
397  return
398#if MAX_LUN
399         (lun < MAX_LUN) ? lun_desc[lun].name :
400#endif
401#if LUN_USB == ENABLE
402                           LUN_USB_NAME;
403#else
404                           NULL;
405#endif
406}
407
408
409//! @}
410
411
412#if ACCESS_USB == ENABLED
413
414/*! \name MEM <-> USB Interface
415 */
416//! @{
417
418
419Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector)
420{
421  Ctrl_status status;
422
423  if (!Ctrl_access_lock()) return CTRL_FAIL;
424
425  memory_start_read_action(nb_sector);
426  status =
427#if MAX_LUN
428           (lun < MAX_LUN) ? lun_desc[lun].usb_read_10(addr, nb_sector) :
429#endif
430                             CTRL_FAIL;
431  memory_stop_read_action();
432
433  Ctrl_access_unlock();
434
435  return status;
436}
437
438
439Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector)
440{
441  Ctrl_status status;
442
443  if (!Ctrl_access_lock()) return CTRL_FAIL;
444
445  memory_start_write_action(nb_sector);
446  status =
447#if MAX_LUN
448           (lun < MAX_LUN) ? lun_desc[lun].usb_write_10(addr, nb_sector) :
449#endif
450                             CTRL_FAIL;
451  memory_stop_write_action();
452
453  Ctrl_access_unlock();
454
455  return status;
456}
457
458
459//! @}
460
461#endif  // ACCESS_USB == ENABLED
462
463
464#if ACCESS_MEM_TO_RAM == ENABLED
465
466/*! \name MEM <-> RAM Interface
467 */
468//! @{
469
470
471Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram)
472{
473  Ctrl_status status;
474
475  if (!Ctrl_access_lock()) return CTRL_FAIL;
476
477  memory_start_read_action(1);
478  status =
479#if MAX_LUN
480           (lun < MAX_LUN) ? lun_desc[lun].mem_2_ram(addr, ram) :
481#endif
482#if LUN_USB == ENABLE
483                             Lun_usb_mem_2_ram(addr, ram);
484#else
485                             CTRL_FAIL;
486#endif
487  memory_stop_read_action();
488
489  Ctrl_access_unlock();
490
491  return status;
492}
493
494
495Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram)
496{
497  Ctrl_status status;
498
499  if (!Ctrl_access_lock()) return CTRL_FAIL;
500
501  memory_start_write_action(1);
502  status =
503#if MAX_LUN
504           (lun < MAX_LUN) ? lun_desc[lun].ram_2_mem(addr, ram) :
505#endif
506#if LUN_USB == ENABLE
507                             Lun_usb_ram_2_mem(addr, ram);
508#else
509                             CTRL_FAIL;
510#endif
511  memory_stop_write_action();
512
513  Ctrl_access_unlock();
514
515  return status;
516}
517
518
519//! @}
520
521#endif  // ACCESS_MEM_TO_RAM == ENABLED
522
523
524#if ACCESS_STREAM == ENABLED
525
526/*! \name Streaming MEM <-> MEM Interface
527 */
528//! @{
529
530
531  #if ACCESS_MEM_TO_MEM == ENABLED
532
533#include "fat.h"
534
535Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector)
536{
537#if (defined __GNUC__) && (defined __AVR32__)
538  __attribute__((__aligned__(4)))
539#elif (defined __ICCAVR32__)
540  #pragma data_alignment = 4
541#endif
542  static U8 sector_buf[FS_512B];
543  Ctrl_status status = CTRL_GOOD;
544
545  while (nb_sector--)
546  {
547    if ((status = memory_2_ram(src_lun, src_addr++, sector_buf)) != CTRL_GOOD) break;
548    if ((status = ram_2_memory(dest_lun, dest_addr++, sector_buf)) != CTRL_GOOD) break;
549  }
550
551  return status;
552}
553
554  #endif  // ACCESS_MEM_TO_MEM == ENABLED
555
556
557Ctrl_status stream_state(U8 id)
558{
559  return CTRL_GOOD;
560}
561
562
563U16 stream_stop(U8 id)
564{
565  return 0;
566}
567
568
569//! @}
570
571#endif  // ACCESS_STREAM == ENABLED
Note: See TracBrowser for help on using the repository browser.