ctrl_access.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * \file
4  *
5  * \brief Abstraction layer for memory interfaces.
6  *
7  * This module contains the interfaces:
8  * - MEM <-> USB;
9  * - MEM <-> RAM;
10  * - MEM <-> MEM.
11  *
12  * This module may be configured and expanded to support the following features:
13  * - write-protected globals;
14  * - password-protected data;
15  * - specific features;
16  * - etc.
17  *
18  * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
19  *
20  * \asf_license_start
21  *
22  * \page License
23  *
24  * Subject to your compliance with these terms, you may use Microchip
25  * software and any derivatives exclusively with Microchip products.
26  * It is your responsibility to comply with third party license terms applicable
27  * to your use of third party software (including open source software) that
28  * may accompany Microchip software.
29  *
30  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
31  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
32  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
33  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
34  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
35  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
36  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
37  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
38  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
39  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
40  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
41  *
42  * \asf_license_stop
43  *
44  ******************************************************************************/
45 /*
46  * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
47  */
48 
49 
50 //_____ I N C L U D E S ____________________________________________________
51 
52 #include "compiler.h"
53 #include "preprocessor.h"
54 #ifdef FREERTOS_USED
55 #include "FreeRTOS.h"
56 #include "semphr.h"
57 #endif
58 #include "ctrl_access.h"
59 
60 
61 //_____ D E F I N I T I O N S ______________________________________________
62 
63 #ifdef FREERTOS_USED
64 
67 
73 #define Ctrl_access_lock() ctrl_access_lock()
74 
77 #define Ctrl_access_unlock() xSemaphoreGive(ctrl_access_semphr)
78 
80 
82 static xSemaphoreHandle ctrl_access_semphr = NULL;
83 
84 #else
85 
88 
94 #define Ctrl_access_lock() true
95 
98 #define Ctrl_access_unlock()
99 
101 
102 #endif // FREERTOS_USED
103 
104 
105 #if MAX_LUN
106 
113 #if ACCESS_USB == true && ACCESS_MEM_TO_RAM == true
114 #define Lun_desc_entry(lun) \
115  {\
116  TPASTE3(Lun_, lun, _test_unit_ready),\
117  TPASTE3(Lun_, lun, _read_capacity),\
118  TPASTE3(Lun_, lun, _unload),\
119  TPASTE3(Lun_, lun, _wr_protect),\
120  TPASTE3(Lun_, lun, _removal),\
121  TPASTE3(Lun_, lun, _usb_read_10),\
122  TPASTE3(Lun_, lun, _usb_write_10),\
123  TPASTE3(Lun_, lun, _mem_2_ram),\
124  TPASTE3(Lun_, lun, _ram_2_mem),\
125  TPASTE3(LUN_, lun, _NAME)\
126  }
127 #elif ACCESS_USB == true
128 #define Lun_desc_entry(lun) \
129  {\
130  TPASTE3(Lun_, lun, _test_unit_ready),\
131  TPASTE3(Lun_, lun, _read_capacity),\
132  TPASTE3(Lun_, lun, _unload),\
133  TPASTE3(Lun_, lun, _wr_protect),\
134  TPASTE3(Lun_, lun, _removal),\
135  TPASTE3(Lun_, lun, _usb_read_10),\
136  TPASTE3(Lun_, lun, _usb_write_10),\
137  TPASTE3(LUN_, lun, _NAME)\
138  }
139 #elif ACCESS_MEM_TO_RAM == true
140 #define Lun_desc_entry(lun) \
141  {\
142  TPASTE3(Lun_, lun, _test_unit_ready),\
143  TPASTE3(Lun_, lun, _read_capacity),\
144  TPASTE3(Lun_, lun, _unload),\
145  TPASTE3(Lun_, lun, _wr_protect),\
146  TPASTE3(Lun_, lun, _removal),\
147  TPASTE3(Lun_, lun, _mem_2_ram),\
148  TPASTE3(Lun_, lun, _ram_2_mem),\
149  TPASTE3(LUN_, lun, _NAME)\
150  }
151 #else
152 #define Lun_desc_entry(lun) \
153  {\
154  TPASTE3(Lun_, lun, _test_unit_ready),\
155  TPASTE3(Lun_, lun, _read_capacity),\
156  TPASTE3(Lun_, lun, _unload),\
157  TPASTE3(Lun_, lun, _wr_protect),\
158  TPASTE3(Lun_, lun, _removal),\
159  TPASTE3(LUN_, lun, _NAME)\
160  }
161 #endif
162 
164 static const struct
165 {
166  Ctrl_status (*test_unit_ready)(void);
167  Ctrl_status (*read_capacity)(U32 *);
168  bool (*unload)(bool);
169  bool (*wr_protect)(void);
170  bool (*removal)(void);
171 #if ACCESS_USB == true
172  Ctrl_status (*usb_read_10)(U32, U16);
173  Ctrl_status (*usb_write_10)(U32, U16);
174 #endif
175 #if ACCESS_MEM_TO_RAM == true
176  Ctrl_status (*mem_2_ram)(U32, void *);
177  Ctrl_status (*ram_2_mem)(U32, const void *);
178 #endif
179  const char *name;
180 } lun_desc[MAX_LUN] =
181 {
182 #if LUN_0 == ENABLE
183 # ifndef Lun_0_unload
184 # define Lun_0_unload NULL
185 # endif
186  Lun_desc_entry(0),
187 #endif
188 #if LUN_1 == ENABLE
189 # ifndef Lun_1_unload
190 # define Lun_1_unload NULL
191 # endif
192  Lun_desc_entry(1),
193 #endif
194 #if LUN_2 == ENABLE
195 # ifndef Lun_2_unload
196 # define Lun_2_unload NULL
197 # endif
198  Lun_desc_entry(2),
199 #endif
200 #if LUN_3 == ENABLE
201 # ifndef Lun_3_unload
202 # define Lun_3_unload NULL
203 # endif
204  Lun_desc_entry(3),
205 #endif
206 #if LUN_4 == ENABLE
207 # ifndef Lun_4_unload
208 # define Lun_4_unload NULL
209 # endif
210  Lun_desc_entry(4),
211 #endif
212 #if LUN_5 == ENABLE
213 # ifndef Lun_5_unload
214 # define Lun_5_unload NULL
215 # endif
216  Lun_desc_entry(5),
217 #endif
218 #if LUN_6 == ENABLE
219 # ifndef Lun_6_unload
220 # define Lun_6_unload NULL
221 # endif
222  Lun_desc_entry(6),
223 #endif
224 #if LUN_7 == ENABLE
225 # ifndef Lun_7_unload
226 # define Lun_7_unload NULL
227 # endif
228  Lun_desc_entry(7)
229 #endif
230 };
231 
232 #endif
233 
234 
235 #if GLOBAL_WR_PROTECT == true
237 #endif
238 
239 
242 
244 
245 #ifdef FREERTOS_USED
246 
247 bool ctrl_access_init(void)
248 {
249  // If the handle to the protecting semaphore is not valid,
250  if (!ctrl_access_semphr)
251  {
252  // try to create the semaphore.
253  vSemaphoreCreateBinary(ctrl_access_semphr);
254 
255  // If the semaphore could not be created, there is no backup solution.
256  if (!ctrl_access_semphr) return false;
257  }
258 
259  return true;
260 }
261 
262 
267 static bool ctrl_access_lock(void)
268 {
269  // If the semaphore could not be created, there is no backup solution.
270  if (!ctrl_access_semphr) return false;
271 
272  // Wait for the semaphore.
273  while (!xSemaphoreTake(ctrl_access_semphr, portMAX_DELAY));
274 
275  return true;
276 }
277 
278 #endif // FREERTOS_USED
279 
280 
282 {
283 #if MEM_USB == ENABLE
284 # ifndef Lun_usb_get_lun
285 # define Lun_usb_get_lun() host_get_lun()
286 # endif
287  U8 nb_lun;
288 
289  if (!Ctrl_access_lock()) return MAX_LUN;
290 
291  nb_lun = MAX_LUN + Lun_usb_get_lun();
292 
294 
295  return nb_lun;
296 #else
297  return MAX_LUN;
298 #endif
299 }
300 
301 
303 {
304  return LUN_ID_0;
305 }
306 
307 
309 {
310  Ctrl_status status;
311 
312  if (!Ctrl_access_lock()) return CTRL_FAIL;
313 
314  status =
315 #if MAX_LUN
316  (lun < MAX_LUN) ? lun_desc[lun].test_unit_ready() :
317 #endif
318 #if LUN_USB == ENABLE
320 #else
321  CTRL_FAIL;
322 #endif
323 
325 
326  return status;
327 }
328 
329 
330 Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector)
331 {
332  Ctrl_status status;
333 
334  if (!Ctrl_access_lock()) return CTRL_FAIL;
335 
336  status =
337 #if MAX_LUN
338  (lun < MAX_LUN) ? lun_desc[lun].read_capacity(u32_nb_sector) :
339 #endif
340 #if LUN_USB == ENABLE
341  Lun_usb_read_capacity(lun - LUN_ID_USB, u32_nb_sector);
342 #else
343  CTRL_FAIL;
344 #endif
345 
347 
348  return status;
349 }
350 
351 
353 {
354  U8 sector_size;
355 
356  if (!Ctrl_access_lock()) return 0;
357 
358  sector_size =
359 #if MAX_LUN
360  (lun < MAX_LUN) ? 1 :
361 #endif
362 #if LUN_USB == ENABLE
364 #else
365  0;
366 #endif
367 
369 
370  return sector_size;
371 }
372 
373 
374 bool mem_unload(U8 lun, bool unload)
375 {
376  bool unloaded;
377 #if !MAX_LUN || !defined(Lun_usb_unload)
378  UNUSED(lun);
379 #endif
380 
381  if (!Ctrl_access_lock()) return false;
382 
383  unloaded =
384 #if MAX_LUN
385  (lun < MAX_LUN) ?
386  (lun_desc[lun].unload ?
387  lun_desc[lun].unload(unload) : !unload) :
388 #endif
389 #if LUN_USB == ENABLE
390 # if defined(Lun_usb_unload)
391  Lun_usb_unload(lun - LUN_ID_USB, unload);
392 # else
393  !unload; /* Can not unload: load success, unload fail */
394 # endif
395 #else
396  false; /* No mem, unload/load fail */
397 #endif
398 
400 
401  return unloaded;
402 }
403 
405 {
406  bool wr_protect;
407 
408  if (!Ctrl_access_lock()) return true;
409 
410  wr_protect =
411 #if MAX_LUN
412  (lun < MAX_LUN) ? lun_desc[lun].wr_protect() :
413 #endif
414 #if LUN_USB == ENABLE
416 #else
417  true;
418 #endif
419 
421 
422  return wr_protect;
423 }
424 
425 
426 bool mem_removal(U8 lun)
427 {
428  bool removal;
429 #if MAX_LUN==0
430  UNUSED(lun);
431 #endif
432 
433  if (!Ctrl_access_lock()) return true;
434 
435  removal =
436 #if MAX_LUN
437  (lun < MAX_LUN) ? lun_desc[lun].removal() :
438 #endif
439 #if LUN_USB == ENABLE
440  Lun_usb_removal();
441 #else
442  true;
443 #endif
444 
446 
447  return removal;
448 }
449 
450 
451 const char *mem_name(U8 lun)
452 {
453 #if MAX_LUN==0
454  UNUSED(lun);
455 #endif
456  return
457 #if MAX_LUN
458  (lun < MAX_LUN) ? lun_desc[lun].name :
459 #endif
460 #if LUN_USB == ENABLE
461  LUN_USB_NAME;
462 #else
463  NULL;
464 #endif
465 }
466 
467 
469 
470 
471 #if ACCESS_USB == true
472 
475 
477 
478 Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector)
479 {
480  Ctrl_status status;
481 
482  if (!Ctrl_access_lock()) return CTRL_FAIL;
483 
484  memory_start_read_action(nb_sector);
485  status =
486 #if MAX_LUN
487  (lun < MAX_LUN) ? lun_desc[lun].usb_read_10(addr, nb_sector) :
488 #endif
489  CTRL_FAIL;
491 
493 
494  return status;
495 }
496 
497 
498 Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector)
499 {
500  Ctrl_status status;
501 
502  if (!Ctrl_access_lock()) return CTRL_FAIL;
503 
504  memory_start_write_action(nb_sector);
505  status =
506 #if MAX_LUN
507  (lun < MAX_LUN) ? lun_desc[lun].usb_write_10(addr, nb_sector) :
508 #endif
509  CTRL_FAIL;
511 
513 
514  return status;
515 }
516 
517 
519 
520 #endif // ACCESS_USB == true
521 
522 
523 #if ACCESS_MEM_TO_RAM == true
524 
527 
529 
530 Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram)
531 {
532  Ctrl_status status;
533 #if MAX_LUN==0
534  UNUSED(lun);
535 #endif
536 
537  if (!Ctrl_access_lock()) return CTRL_FAIL;
538 
540  status =
541 #if MAX_LUN
542  (lun < MAX_LUN) ? lun_desc[lun].mem_2_ram(addr, ram) :
543 #endif
544 #if LUN_USB == ENABLE
545  Lun_usb_mem_2_ram(addr, ram);
546 #else
547  CTRL_FAIL;
548 #endif
550 
552 
553  return status;
554 }
555 
556 
557 Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram)
558 {
559  Ctrl_status status;
560 #if MAX_LUN==0
561  UNUSED(lun);
562 #endif
563 
564  if (!Ctrl_access_lock()) return CTRL_FAIL;
565 
567  status =
568 #if MAX_LUN
569  (lun < MAX_LUN) ? lun_desc[lun].ram_2_mem(addr, ram) :
570 #endif
571 #if LUN_USB == ENABLE
572  Lun_usb_ram_2_mem(addr, ram);
573 #else
574  CTRL_FAIL;
575 #endif
577 
579 
580  return status;
581 }
582 
583 
585 
586 #endif // ACCESS_MEM_TO_RAM == true
587 
588 
589 #if ACCESS_STREAM == true
590 
593 
595 
596  #if ACCESS_MEM_TO_MEM == true
597 
598 #include "fat.h"
599 
600 Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector)
601 {
603  static U8 sector_buf[FS_512B];
604  Ctrl_status status = CTRL_GOOD;
605 
606  while (nb_sector--)
607  {
608  if ((status = memory_2_ram(src_lun, src_addr++, sector_buf)) != CTRL_GOOD) break;
609  if ((status = ram_2_memory(dest_lun, dest_addr++, sector_buf)) != CTRL_GOOD) break;
610  }
611 
612  return status;
613 }
614 
615  #endif // ACCESS_MEM_TO_MEM == true
616 
617 
619 {
620  UNUSED(id);
621  return CTRL_GOOD;
622 }
623 
624 
626 {
627  UNUSED(id);
628  return 0;
629 }
630 
631 
633 
634 #endif // ACCESS_STREAM == true
#define portMAX_DELAY
Definition: portmacro.h:65
U8 get_cur_lun(void)
Returns the current LUN.
Definition: ctrl_access.c:302
#define UNUSED(v)
Marking v as a unused parameter or value.
Definition: compiler.h:86
#define Lun_usb_removal()
Definition: conf_access.h:221
#define Lun_usb_unload
Definition: conf_access.h:219
uint16_t U16
16-bit unsigned integer.
Definition: compiler.h:249
#define Lun_usb_get_lun()
Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram)
Copies 1 data sector from RAM to the memory.
Definition: ctrl_access.c:557
Ctrl_status mem_test_unit_ready(U8 lun)
Tests the memory state and initializes the memory if required.
Definition: ctrl_access.c:308
Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector)
Transfers data from USB to the memory.
Definition: ctrl_access.c:498
#define NULL
Definition: nm_bsp.h:52
An error occurred.
Definition: ctrl_access.h:77
#define Lun_usb_test_unit_ready(lun)
Definition: conf_access.h:216
U8 mem_sector_size(U8 lun)
Returns the size of the physical sector.
Definition: ctrl_access.c:352
#define memory_start_write_action(nb_sectors)
Definition: conf_access.h:236
U8 get_nb_lun(void)
Returns the number of LUNs.
Definition: ctrl_access.c:281
Commonly used includes, types and macros.
Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram)
Copies 1 data sector from the memory to RAM.
Definition: ctrl_access.c:530
#define Lun_usb_read_sector_size(lun)
Definition: conf_access.h:218
COMPILER_ALIGNED(32)
Buffer to receive data.
Definition: udi_cdc.c:233
uint32_t U32
32-bit unsigned integer.
Definition: compiler.h:253
Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector)
Transfers data from the memory to USB.
Definition: ctrl_access.c:478
#define memory_stop_write_action()
Definition: conf_access.h:237
#define LUN_USB_NAME
Definition: conf_access.h:224
#define MAX_LUN
Number of static LUNs.
Definition: ctrl_access.h:125
#define Lun_usb_ram_2_mem(addr, ram)
Definition: conf_access.h:223
#define LUN_USB
Disable Host Mass-Storage Memory.
Definition: conf_access.h:96
#define Ctrl_access_unlock()
Unlocks accesses to LUNs.
Definition: ctrl_access.c:98
#define memory_stop_read_action()
Definition: conf_access.h:235
#define Lun_usb_wr_protect(lun)
Definition: conf_access.h:220
#define xSemaphoreTake(xSemaphore, xBlockTime)
Definition: semphr.h:290
Ctrl_status
Status returned by CTRL_ACCESS interfaces.
Definition: ctrl_access.h:74
Success, memory ready.
Definition: ctrl_access.h:76
unsigned short U16
unsigned char bool
Boolean.
Definition: compiler.h:243
Preprocessor utils.
bool mem_wr_protect(U8 lun)
Returns the write-protection state of the memory.
Definition: ctrl_access.c:404
bool mem_unload(U8 lun, bool unload)
Unload/load the medium.
Definition: ctrl_access.c:374
uint8_t U8
8-bit unsigned integer.
Definition: compiler.h:247
#define Lun_usb_read_capacity(lun, nb_sect)
Definition: conf_access.h:217
bool g_wr_protect
Write protect.
Definition: ctrl_access.c:236
Ctrl_status stream_state(U8 id)
Returns the state of a streaming data transfer.
Definition: ctrl_access.c:618
#define LUN_ID_USB
Definition: ctrl_access.h:126
#define Lun_usb_mem_2_ram(addr, ram)
Definition: conf_access.h:222
#define xSemaphoreHandle
Definition: FreeRTOS.h:903
bool mem_removal(U8 lun)
Tells whether the memory is removable.
Definition: ctrl_access.c:426
unsigned int U32
Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector)
Copies data from one memory to another.
Definition: ctrl_access.c:600
#define Ctrl_access_lock()
Locks accesses to LUNs.
Definition: ctrl_access.c:94
const char * mem_name(U8 lun)
Returns a pointer to the LUN name.
Definition: ctrl_access.c:451
#define memory_start_read_action(nb_sectors)
Definition: conf_access.h:234
U16 stream_stop(U8 id)
Stops a streaming data transfer.
Definition: ctrl_access.c:625
#define ENABLE
Definition: compiler.h:421
Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector)
Returns the address of the last valid sector (512 bytes) in the memory.
Definition: ctrl_access.c:330
#define LUN_ID_0
First static LUN.
Definition: ctrl_access.h:117


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04