My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctrl_access.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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-2015 Atmel Corporation. All rights reserved.
  19. *
  20. * \asf_license_start
  21. *
  22. * \page License
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions are met:
  26. *
  27. * 1. Redistributions of source code must retain the above copyright notice,
  28. * this list of conditions and the following disclaimer.
  29. *
  30. * 2. Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. *
  34. * 3. The name of Atmel may not be used to endorse or promote products derived
  35. * from this software without specific prior written permission.
  36. *
  37. * 4. This software may only be redistributed and used in connection with an
  38. * Atmel microcontroller product.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  41. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  42. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  43. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  44. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  49. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  50. * POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. * \asf_license_stop
  53. *
  54. ******************************************************************************/
  55. /*
  56. * Support and FAQ: visit <a href="https://www.atmel.com/design-support/">Atmel Support</a>
  57. */
  58. #ifndef _CTRL_ACCESS_H_
  59. #define _CTRL_ACCESS_H_
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**
  64. * \defgroup group_common_services_storage_ctrl_access Memory Control Access
  65. *
  66. * Common abstraction layer for memory interfaces. It provides interfaces between:
  67. * Memory and USB, Memory and RAM, Memory and Memory. Common API for XMEGA and UC3.
  68. *
  69. * \{
  70. */
  71. #include "compiler.h"
  72. #include "conf_access.h"
  73. #ifndef SECTOR_SIZE
  74. #define SECTOR_SIZE 512
  75. #endif
  76. //! Status returned by CTRL_ACCESS interfaces.
  77. typedef enum
  78. {
  79. CTRL_GOOD = PASS, //!< Success, memory ready.
  80. CTRL_FAIL = FAIL, //!< An error occurred.
  81. CTRL_NO_PRESENT = FAIL + 1, //!< Memory unplugged.
  82. CTRL_BUSY = FAIL + 2 //!< Memory not initialized or changed.
  83. } Ctrl_status;
  84. // FYI: Each Logical Unit Number (LUN) corresponds to a memory.
  85. // Check LUN defines.
  86. #ifndef LUN_0
  87. #error LUN_0 must be defined as ENABLE or DISABLE in conf_access.h
  88. #endif
  89. #ifndef LUN_1
  90. #error LUN_1 must be defined as ENABLE or DISABLE in conf_access.h
  91. #endif
  92. #ifndef LUN_2
  93. #error LUN_2 must be defined as ENABLE or DISABLE in conf_access.h
  94. #endif
  95. #ifndef LUN_3
  96. #error LUN_3 must be defined as ENABLE or DISABLE in conf_access.h
  97. #endif
  98. #ifndef LUN_4
  99. #error LUN_4 must be defined as ENABLE or DISABLE in conf_access.h
  100. #endif
  101. #ifndef LUN_5
  102. #error LUN_5 must be defined as ENABLE or DISABLE in conf_access.h
  103. #endif
  104. #ifndef LUN_6
  105. #error LUN_6 must be defined as ENABLE or DISABLE in conf_access.h
  106. #endif
  107. #ifndef LUN_7
  108. #error LUN_7 must be defined as ENABLE or DISABLE in conf_access.h
  109. #endif
  110. #ifndef LUN_USB
  111. #error LUN_USB must be defined as ENABLE or DISABLE in conf_access.h
  112. #endif
  113. /*! \name LUN IDs
  114. */
  115. //! @{
  116. #define LUN_ID_0 (0) //!< First static LUN.
  117. #define LUN_ID_1 (LUN_ID_0 + LUN_0)
  118. #define LUN_ID_2 (LUN_ID_1 + LUN_1)
  119. #define LUN_ID_3 (LUN_ID_2 + LUN_2)
  120. #define LUN_ID_4 (LUN_ID_3 + LUN_3)
  121. #define LUN_ID_5 (LUN_ID_4 + LUN_4)
  122. #define LUN_ID_6 (LUN_ID_5 + LUN_5)
  123. #define LUN_ID_7 (LUN_ID_6 + LUN_6)
  124. #define MAX_LUN (LUN_ID_7 + LUN_7) //!< Number of static LUNs.
  125. #define LUN_ID_USB (MAX_LUN) //!< First dynamic LUN (USB host mass storage).
  126. //! @}
  127. // Include LUN header files.
  128. #if LUN_0 == ENABLE
  129. #include LUN_0_INCLUDE
  130. #endif
  131. #if LUN_1 == ENABLE
  132. #include LUN_1_INCLUDE
  133. #endif
  134. #if LUN_2 == ENABLE
  135. #include LUN_2_INCLUDE
  136. #endif
  137. #if LUN_3 == ENABLE
  138. #include LUN_3_INCLUDE
  139. #endif
  140. #if LUN_4 == ENABLE
  141. #include LUN_4_INCLUDE
  142. #endif
  143. #if LUN_5 == ENABLE
  144. #include LUN_5_INCLUDE
  145. #endif
  146. #if LUN_6 == ENABLE
  147. #include LUN_6_INCLUDE
  148. #endif
  149. #if LUN_7 == ENABLE
  150. #include LUN_7_INCLUDE
  151. #endif
  152. #if LUN_USB == ENABLE
  153. #include LUN_USB_INCLUDE
  154. #endif
  155. // Check the configuration of write protection in conf_access.h.
  156. #ifndef GLOBAL_WR_PROTECT
  157. #error GLOBAL_WR_PROTECT must be defined as true or false in conf_access.h
  158. #endif
  159. #if GLOBAL_WR_PROTECT == true
  160. //! Write protect.
  161. extern bool g_wr_protect;
  162. #endif
  163. /*! \name Control Interface
  164. */
  165. //! @{
  166. #ifdef FREERTOS_USED
  167. /*! \brief Initializes the LUN access locker.
  168. *
  169. * \return \c true if the locker was successfully initialized, else \c false.
  170. */
  171. extern bool ctrl_access_init(void);
  172. #endif // FREERTOS_USED
  173. /*! \brief Returns the number of LUNs.
  174. *
  175. * \return Number of LUNs in the system.
  176. */
  177. extern U8 get_nb_lun(void);
  178. /*! \brief Returns the current LUN.
  179. *
  180. * \return Current LUN.
  181. *
  182. * \todo Implement.
  183. */
  184. extern U8 get_cur_lun(void);
  185. /*! \brief Tests the memory state and initializes the memory if required.
  186. *
  187. * The TEST UNIT READY SCSI primary command allows an application client to poll
  188. * a LUN until it is ready without having to allocate memory for returned data.
  189. *
  190. * This command may be used to check the media status of LUNs with removable
  191. * media.
  192. *
  193. * \param lun Logical Unit Number.
  194. *
  195. * \return Status.
  196. */
  197. extern Ctrl_status mem_test_unit_ready(U8 lun);
  198. /*! \brief Returns the address of the last valid sector (512 bytes) in the
  199. * memory.
  200. *
  201. * \param lun Logical Unit Number.
  202. * \param u32_nb_sector Pointer to the address of the last valid sector.
  203. *
  204. * \return Status.
  205. */
  206. extern Ctrl_status mem_read_capacity(U8 lun, U32 *u32_nb_sector);
  207. /*! \brief Returns the size of the physical sector.
  208. *
  209. * \param lun Logical Unit Number.
  210. *
  211. * \return Sector size (unit: 512 bytes).
  212. */
  213. extern U8 mem_sector_size(U8 lun);
  214. /*! \brief Unload/load the medium.
  215. *
  216. * \param lun Logical Unit Number.
  217. * \param unload \c true to unload the medium, \c false to load the medium.
  218. *
  219. * \return \c true if unload/load success, else \c false.
  220. */
  221. extern bool mem_unload(U8 lun, bool unload);
  222. /*! \brief Returns the write-protection state of the memory.
  223. *
  224. * \param lun Logical Unit Number.
  225. *
  226. * \return \c true if the memory is write-protected, else \c false.
  227. *
  228. * \note Only used by removable memories with hardware-specific write
  229. * protection.
  230. */
  231. extern bool mem_wr_protect(U8 lun);
  232. /*! \brief Tells whether the memory is removable.
  233. *
  234. * \param lun Logical Unit Number.
  235. *
  236. * \return \c true if the memory is removable, else \c false.
  237. */
  238. extern bool mem_removal(U8 lun);
  239. /*! \brief Returns a pointer to the LUN name.
  240. *
  241. * \param lun Logical Unit Number.
  242. *
  243. * \return Pointer to the LUN name string.
  244. */
  245. extern const char *mem_name(U8 lun);
  246. //! @}
  247. #if ACCESS_USB == true
  248. /*! \name MEM <-> USB Interface
  249. */
  250. //! @{
  251. /*! \brief Transfers data from the memory to USB.
  252. *
  253. * \param lun Logical Unit Number.
  254. * \param addr Address of first memory sector to read.
  255. * \param nb_sector Number of sectors to transfer.
  256. *
  257. * \return Status.
  258. */
  259. extern Ctrl_status memory_2_usb(U8 lun, U32 addr, U16 nb_sector);
  260. /*! \brief Transfers data from USB to the memory.
  261. *
  262. * \param lun Logical Unit Number.
  263. * \param addr Address of first memory sector to write.
  264. * \param nb_sector Number of sectors to transfer.
  265. *
  266. * \return Status.
  267. */
  268. extern Ctrl_status usb_2_memory(U8 lun, U32 addr, U16 nb_sector);
  269. //! @}
  270. #endif // ACCESS_USB == true
  271. #if ACCESS_MEM_TO_RAM == true
  272. /*! \name MEM <-> RAM Interface
  273. */
  274. //! @{
  275. /*! \brief Copies 1 data sector from the memory to RAM.
  276. *
  277. * \param lun Logical Unit Number.
  278. * \param addr Address of first memory sector to read.
  279. * \param ram Pointer to RAM buffer to write.
  280. *
  281. * \return Status.
  282. */
  283. extern Ctrl_status memory_2_ram(U8 lun, U32 addr, void *ram);
  284. /*! \brief Copies 1 data sector from RAM to the memory.
  285. *
  286. * \param lun Logical Unit Number.
  287. * \param addr Address of first memory sector to write.
  288. * \param ram Pointer to RAM buffer to read.
  289. *
  290. * \return Status.
  291. */
  292. extern Ctrl_status ram_2_memory(U8 lun, U32 addr, const void *ram);
  293. //! @}
  294. #endif // ACCESS_MEM_TO_RAM == true
  295. #if ACCESS_STREAM == true
  296. /*! \name Streaming MEM <-> MEM Interface
  297. */
  298. //! @{
  299. //! Erroneous streaming data transfer ID.
  300. #define ID_STREAM_ERR 0xFF
  301. #if ACCESS_MEM_TO_MEM == true
  302. /*! \brief Copies data from one memory to another.
  303. *
  304. * \param src_lun Source Logical Unit Number.
  305. * \param src_addr Source address of first memory sector to read.
  306. * \param dest_lun Destination Logical Unit Number.
  307. * \param dest_addr Destination address of first memory sector to write.
  308. * \param nb_sector Number of sectors to copy.
  309. *
  310. * \return Status.
  311. */
  312. extern Ctrl_status stream_mem_to_mem(U8 src_lun, U32 src_addr, U8 dest_lun, U32 dest_addr, U16 nb_sector);
  313. #endif // ACCESS_MEM_TO_MEM == true
  314. /*! \brief Returns the state of a streaming data transfer.
  315. *
  316. * \param id Transfer ID.
  317. *
  318. * \return Status.
  319. *
  320. * \todo Implement.
  321. */
  322. extern Ctrl_status stream_state(U8 id);
  323. /*! \brief Stops a streaming data transfer.
  324. *
  325. * \param id Transfer ID.
  326. *
  327. * \return Number of remaining sectors.
  328. *
  329. * \todo Implement.
  330. */
  331. extern U16 stream_stop(U8 id);
  332. //! @}
  333. #endif // ACCESS_STREAM == true
  334. /**
  335. * \}
  336. */
  337. #ifdef __cplusplus
  338. }
  339. #endif
  340. #endif // _CTRL_ACCESS_H_