Keyboard firmwares for Atmel AVR and Cortex-M
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

USBMSD.h 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright (c) 2010-2011 mbed.org, MIT License
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  4. * and associated documentation files (the "Software"), to deal in the Software without
  5. * restriction, including without limitation the rights to use, copy, modify, merge, publish,
  6. * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or
  10. * substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. */
  18. #ifndef USBMSD_H
  19. #define USBMSD_H
  20. /* These headers are included for child class. */
  21. #include "USBEndpoints.h"
  22. #include "USBDescriptor.h"
  23. #include "USBDevice_Types.h"
  24. #include "USBDevice.h"
  25. /**
  26. * USBMSD class: generic class in order to use all kinds of blocks storage chip
  27. *
  28. * Introduction
  29. *
  30. * The USBMSD implements the MSD protocol. It permits to access a memory chip (flash, sdcard,...)
  31. * from a computer over USB. But this class doesn't work standalone, you need to subclass this class
  32. * and define virtual functions which are called in USBMSD.
  33. *
  34. * How to use this class with your chip ?
  35. *
  36. * You have to inherit and define some pure virtual functions (mandatory step):
  37. * - virtual int disk_read(char * data, int block): function to read a block
  38. * - virtual int disk_write(const char * data, int block): function to write a block
  39. * - virtual int disk_initialize(): function to initialize the memory
  40. * - virtual int disk_sectors(): return the number of blocks
  41. * - virtual int disk_size(): return the memory size
  42. * - virtual int disk_status(): return the status of the storage chip (0: OK, 1: not initialized, 2: no medium in the drive, 4: write protection)
  43. *
  44. * All functions names are compatible with the fat filesystem library. So you can imagine using your own class with
  45. * USBMSD and the fat filesystem library in the same program. Just be careful because there are two different parts which
  46. * will access the sd card. You can do a master/slave system using the disk_status method.
  47. *
  48. * Once these functions defined, you can call connect() (at the end of the constructor of your class for instance)
  49. * of USBMSD to connect your mass storage device. connect() will first call disk_status() to test the status of the disk.
  50. * If disk_status() returns 1 (disk not initialized), then disk_initialize() is called. After this step, connect() will collect information
  51. * such as the number of blocks and the memory size.
  52. */
  53. class USBMSD: public USBDevice {
  54. public:
  55. /**
  56. * Constructor
  57. *
  58. * @param vendor_id Your vendor_id
  59. * @param product_id Your product_id
  60. * @param product_release Your preoduct_release
  61. */
  62. USBMSD(uint16_t vendor_id = 0x0703, uint16_t product_id = 0x0104, uint16_t product_release = 0x0001);
  63. /**
  64. * Connect the USB MSD device. Establish disk initialization before really connect the device.
  65. *
  66. * @param blocking if not configured
  67. * @returns true if successful
  68. */
  69. bool connect(bool blocking = true);
  70. /**
  71. * Disconnect the USB MSD device.
  72. */
  73. void disconnect();
  74. /**
  75. * Destructor
  76. */
  77. ~USBMSD();
  78. protected:
  79. /*
  80. * read one or more blocks on a storage chip
  81. *
  82. * @param data pointer where will be stored read data
  83. * @param block starting block number
  84. * @param count number of blocks to read
  85. * @returns 0 if successful
  86. */
  87. virtual int disk_read(uint8_t* data, uint64_t block, uint8_t count) = 0;
  88. /*
  89. * write one or more blocks on a storage chip
  90. *
  91. * @param data data to write
  92. * @param block starting block number
  93. * @param count number of blocks to write
  94. * @returns 0 if successful
  95. */
  96. virtual int disk_write(const uint8_t* data, uint64_t block, uint8_t count) = 0;
  97. /*
  98. * Disk initilization
  99. */
  100. virtual int disk_initialize() = 0;
  101. /*
  102. * Return the number of blocks
  103. *
  104. * @returns number of blocks
  105. */
  106. virtual uint64_t disk_sectors() = 0;
  107. /*
  108. * Return memory size
  109. *
  110. * @returns memory size
  111. */
  112. virtual uint64_t disk_size() = 0;
  113. /*
  114. * To check the status of the storage chip
  115. *
  116. * @returns status: 0: OK, 1: disk not initialized, 2: no medium in the drive, 4: write protected
  117. */
  118. virtual int disk_status() = 0;
  119. /*
  120. * Get string product descriptor
  121. *
  122. * @returns pointer to the string product descriptor
  123. */
  124. virtual uint8_t * stringIproductDesc();
  125. /*
  126. * Get string interface descriptor
  127. *
  128. * @returns pointer to the string interface descriptor
  129. */
  130. virtual uint8_t * stringIinterfaceDesc();
  131. /*
  132. * Get configuration descriptor
  133. *
  134. * @returns pointer to the configuration descriptor
  135. */
  136. virtual uint8_t * configurationDesc();
  137. /*
  138. * Callback called when a packet is received
  139. */
  140. virtual bool EPBULK_OUT_callback();
  141. /*
  142. * Callback called when a packet has been sent
  143. */
  144. virtual bool EPBULK_IN_callback();
  145. /*
  146. * Set configuration of device. Add endpoints
  147. */
  148. virtual bool USBCallback_setConfiguration(uint8_t configuration);
  149. /*
  150. * Callback called to process class specific requests
  151. */
  152. virtual bool USBCallback_request();
  153. private:
  154. // MSC Bulk-only Stage
  155. enum Stage {
  156. READ_CBW, // wait a CBW
  157. ERROR, // error
  158. PROCESS_CBW, // process a CBW request
  159. SEND_CSW, // send a CSW
  160. WAIT_CSW, // wait that a CSW has been effectively sent
  161. };
  162. // Bulk-only CBW
  163. typedef struct {
  164. uint32_t Signature;
  165. uint32_t Tag;
  166. uint32_t DataLength;
  167. uint8_t Flags;
  168. uint8_t LUN;
  169. uint8_t CBLength;
  170. uint8_t CB[16];
  171. } PACKED CBW;
  172. // Bulk-only CSW
  173. typedef struct {
  174. uint32_t Signature;
  175. uint32_t Tag;
  176. uint32_t DataResidue;
  177. uint8_t Status;
  178. } PACKED CSW;
  179. //state of the bulk-only state machine
  180. Stage stage;
  181. // current CBW
  182. CBW cbw;
  183. // CSW which will be sent
  184. CSW csw;
  185. // addr where will be read or written data
  186. uint32_t addr;
  187. // length of a reading or writing
  188. uint32_t length;
  189. // memory OK (after a memoryVerify)
  190. bool memOK;
  191. // cache in RAM before writing in memory. Useful also to read a block.
  192. uint8_t * page;
  193. int BlockSize;
  194. uint64_t MemorySize;
  195. uint64_t BlockCount;
  196. void CBWDecode(uint8_t * buf, uint16_t size);
  197. void sendCSW (void);
  198. bool inquiryRequest (void);
  199. bool write (uint8_t * buf, uint16_t size);
  200. bool readFormatCapacity();
  201. bool readCapacity (void);
  202. bool infoTransfer (void);
  203. void memoryRead (void);
  204. bool modeSense6 (void);
  205. void testUnitReady (void);
  206. bool requestSense (void);
  207. void memoryVerify (uint8_t * buf, uint16_t size);
  208. void memoryWrite (uint8_t * buf, uint16_t size);
  209. void reset();
  210. void fail();
  211. };
  212. #endif