diskio.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /*!
  3. @file diskio.c
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. #include "tusb.h"
  33. #if CFG_TUSB_HOST_MSC
  34. //--------------------------------------------------------------------+
  35. // INCLUDE
  36. //--------------------------------------------------------------------+
  37. #include "ffconf.h"
  38. #include "diskio.h"
  39. //--------------------------------------------------------------------+
  40. // MACRO CONSTANT TYPEDEF
  41. //--------------------------------------------------------------------+
  42. // TODO change it to portable init
  43. static DSTATUS disk_state[CFG_TUSB_HOST_DEVICE_MAX];
  44. //--------------------------------------------------------------------+
  45. // INTERNAL OBJECT & FUNCTION DECLARATION
  46. //--------------------------------------------------------------------+
  47. //--------------------------------------------------------------------+
  48. // IMPLEMENTATION
  49. //--------------------------------------------------------------------+
  50. static DRESULT wait_for_io_complete(uint8_t usb_addr)
  51. {
  52. // TODO with RTOS, this should use semaphore instead of blocking
  53. while ( tuh_msc_is_busy(usb_addr) )
  54. {
  55. // TODO should have timeout here
  56. #if CFG_TUSB_OS != OPT_OS_NONE
  57. osal_task_delay(10);
  58. #endif
  59. }
  60. return RES_OK;
  61. }
  62. void diskio_init(void)
  63. {
  64. memset(disk_state, STA_NOINIT, CFG_TUSB_HOST_DEVICE_MAX);
  65. }
  66. //pdrv Specifies the physical drive number.
  67. DSTATUS disk_initialize ( BYTE pdrv )
  68. {
  69. disk_state[pdrv] &= (~STA_NOINIT); // clear NOINIT bit
  70. return disk_state[pdrv];
  71. }
  72. void disk_deinitialize ( BYTE pdrv )
  73. {
  74. disk_state[pdrv] |= STA_NOINIT; // set NOINIT bit
  75. }
  76. DSTATUS disk_status (BYTE pdrv)
  77. {
  78. return disk_state[pdrv];
  79. }
  80. //pdrv
  81. // Specifies the physical drive number --> == dev_addr-1
  82. //buff
  83. // Pointer to the byte array to store the read data. The size of buffer must be in sector size * sector count.
  84. //sector
  85. // Specifies the start sector number in logical block address (LBA).
  86. //count
  87. // Specifies number of sectors to read. The value can be 1 to 128. Generally, a multiple sector transfer request
  88. // must not be split into single sector transactions to the device, or you may not get good read performance.
  89. DRESULT disk_read (BYTE pdrv, BYTE*buff, DWORD sector, BYTE count)
  90. {
  91. uint8_t usb_addr = pdrv+1;
  92. if ( TUSB_ERROR_NONE != tuh_msc_read10(usb_addr, 0, buff, sector, count) ) return RES_ERROR;
  93. return wait_for_io_complete(usb_addr);
  94. }
  95. DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, BYTE count)
  96. {
  97. uint8_t usb_addr = pdrv+1;
  98. if ( TUSB_ERROR_NONE != tuh_msc_write10(usb_addr, 0, buff, sector, count) ) return RES_ERROR;
  99. return wait_for_io_complete(usb_addr);
  100. }
  101. /* [IN] Drive number */
  102. /* [IN] Control command code */
  103. /* [I/O] Parameter and data buffer */
  104. DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff)
  105. {
  106. (void) buff; (void) pdrv; // compiler warnings
  107. if (cmd != CTRL_SYNC) return RES_ERROR;
  108. return RES_OK;
  109. }
  110. static inline uint8_t month2number(char* p_ch) ATTR_PURE ATTR_ALWAYS_INLINE;
  111. static inline uint8_t month2number(char* p_ch)
  112. {
  113. char const * const month_str[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  114. for(uint8_t i=0; i<12; i++)
  115. {
  116. if ( strncmp(p_ch, month_str[i], 3) == 0 ) return i+1;
  117. }
  118. return 1;
  119. }
  120. static inline uint8_t c2i(char ch) ATTR_CONST ATTR_ALWAYS_INLINE;
  121. static inline uint8_t c2i(char ch)
  122. {
  123. return ch - '0';
  124. }
  125. DWORD get_fattime (void)
  126. {
  127. union {
  128. struct {
  129. DWORD second : 5;
  130. DWORD minute : 6;
  131. DWORD hour : 5;
  132. DWORD day_in_month : 5;
  133. DWORD month : 4;
  134. DWORD year : 7;
  135. };
  136. DWORD value;
  137. } timestamp;
  138. //------------- Date is compiled date-------------//
  139. char compile_date[] = __DATE__; // eg. "Sep 26 2013"
  140. char* p_ch;
  141. p_ch = strtok (compile_date, " ");
  142. timestamp.month = month2number(p_ch);
  143. p_ch = strtok (NULL, " ");
  144. timestamp.day_in_month = 10*c2i(p_ch[0])+ c2i(p_ch[1]);
  145. p_ch = strtok (NULL, " ");
  146. timestamp.year = 1000*c2i(p_ch[0]) + 100*c2i(p_ch[1]) + 10*c2i(p_ch[2]) + c2i(p_ch[3]) - 1980;
  147. //------------- Time each time this function call --> sec ++ -------------//
  148. static uint8_t sec = 0;
  149. static uint8_t min = 0;
  150. static uint8_t hour = 0;
  151. if (++sec >= 60)
  152. {
  153. sec = 0;
  154. if (++min >= 60)
  155. {
  156. min = 0;
  157. if (++hour >= 24)
  158. {
  159. hour = 0; // assume demo wont call this function more than 24*60*60 times
  160. }
  161. }
  162. }
  163. timestamp.hour = hour;
  164. timestamp.minute = min;
  165. timestamp.second = sec;
  166. return timestamp.value;
  167. }
  168. #endif // CFG_TUSB_HOST_MSC