diskio_spitf.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be */
  5. /* attached to the FatFs via a glue function rather than modifying it. */
  6. /* This is an example of glue functions to attach various exsisting */
  7. /* storage control modules to the FatFs module with a defined API. */
  8. /*-----------------------------------------------------------------------*/
  9. #include "luat_base.h"
  10. #include "luat_spi.h"
  11. #include "luat_timer.h"
  12. #include "luat_gpio.h"
  13. #include "luat_mem.h"
  14. #ifdef __LUATOS__
  15. #include "lauxlib.h"
  16. #endif
  17. #include "ff.h" /* Obtains integer types */
  18. #include "diskio.h" /* Declarations of disk functions */
  19. #ifdef __LUATOS__
  20. #include "c_common.h"
  21. #else
  22. #if defined(LUAT_EC7XX_CSDK) || defined(CHIP_EC618)
  23. #include "bsp_common.h"
  24. #endif
  25. #endif
  26. #include "luat_rtos.h"
  27. #include "luat_mcu.h"
  28. #define LUAT_LOG_TAG "SPI_TF"
  29. #include "luat_log.h"
  30. #define __SDHC_BLOCK_LEN__ (512)
  31. #define CMD0 (0) /* GO_IDLE_STATE */
  32. #define CMD1 (1) /* SEND_OP_COND */
  33. #define CMD2 (2)
  34. #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
  35. #define CMD8 (8) /* SEND_IF_COND */
  36. #define CMD9 (9) /* SEND_CSD */
  37. #define CMD10 (10) /* SEND_CID */
  38. #define CMD12 (12) /* STOP_TRANSMISSION */
  39. #define CMD13 (13) /* SEND_STATUS */
  40. #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
  41. #define CMD16 (16) /* SET_BLOCKLEN */
  42. #define CMD17 (17) /* READ_SINGLE_BLOCK */
  43. #define CMD18 (18) /* READ_MULTIPLE_BLOCK */
  44. #define CMD23 (23) /* SET_BLOCK_COUNT */
  45. #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
  46. #define CMD24 (24) /* WRITE_BLOCK */
  47. #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
  48. #define CMD32 (32) /* ERASE_ER_BLK_START */
  49. #define CMD33 (33) /* ERASE_ER_BLK_END */
  50. #define CMD38 (38) /* ERASE */
  51. #define CMD55 (55) /* APP_CMD */
  52. #define CMD58 (58) /* READ_OCR */
  53. #define SD_CMD_GO_IDLE_STATE 0 /* CMD0 = 0x40 */
  54. #define SD_CMD_SEND_OP_COND 1 /* CMD1 = 0x41 */
  55. #define SD_CMD_SEND_IF_COND 8 /* CMD8 = 0x48 */
  56. #define SD_CMD_SEND_CSD 9 /* CMD9 = 0x49 */
  57. #define SD_CMD_SEND_CID 10 /* CMD10 = 0x4A */
  58. #define SD_CMD_STOP_TRANSMISSION 12 /* CMD12 = 0x4C */
  59. #define SD_CMD_SEND_STATUS 13 /* CMD13 = 0x4D */
  60. #define SD_CMD_SET_BLOCKLEN 16 /* CMD16 = 0x50 */
  61. #define SD_CMD_READ_SINGLE_BLOCK 17 /* CMD17 = 0x51 */
  62. #define SD_CMD_READ_MULT_BLOCK 18 /* CMD18 = 0x52 */
  63. #define SD_CMD_SET_BLOCK_COUNT 23 /* CMD23 = 0x57 */
  64. #define SD_CMD_WRITE_SINGLE_BLOCK 24 /* CMD24 = 0x58 */
  65. #define SD_CMD_WRITE_MULT_BLOCK 25 /* CMD25 = 0x59 */
  66. #define SD_CMD_PROG_CSD 27 /* CMD27 = 0x5B */
  67. #define SD_CMD_SET_WRITE_PROT 28 /* CMD28 = 0x5C */
  68. #define SD_CMD_CLR_WRITE_PROT 29 /* CMD29 = 0x5D */
  69. #define SD_CMD_SEND_WRITE_PROT 30 /* CMD30 = 0x5E */
  70. #define SD_CMD_SD_ERASE_GRP_START 32 /* CMD32 = 0x60 */
  71. #define SD_CMD_SD_ERASE_GRP_END 33 /* CMD33 = 0x61 */
  72. #define SD_CMD_UNTAG_SECTOR 34 /* CMD34 = 0x62 */
  73. #define SD_CMD_ERASE_GRP_START 35 /* CMD35 = 0x63 */
  74. #define SD_CMD_ERASE_GRP_END 36 /* CMD36 = 0x64 */
  75. #define SD_CMD_UNTAG_ERASE_GROUP 37 /* CMD37 = 0x65 */
  76. #define SD_CMD_ERASE 38 /* CMD38 = 0x66 */
  77. #define SD_CMD_SD_APP_OP_COND 41 /* CMD41 = 0x69 */
  78. #define SD_CMD_APP_CMD 55 /* CMD55 = 0x77 */
  79. #define SD_CMD_READ_OCR 58 /* CMD55 = 0x79 */
  80. #define SD_DEFAULT_BLOCK_SIZE (512)
  81. #define SPI_TF_WRITE_TO_MS (100)
  82. #define SPI_TF_READ_TO_MS (100)
  83. typedef struct
  84. {
  85. uint8_t Reserved1:2; /* Reserved */
  86. uint16_t DeviceSize:12; /* Device Size */
  87. uint8_t MaxRdCurrentVDDMin:3; /* Max. read current @ VDD min */
  88. uint8_t MaxRdCurrentVDDMax:3; /* Max. read current @ VDD max */
  89. uint8_t MaxWrCurrentVDDMin:3; /* Max. write current @ VDD min */
  90. uint8_t MaxWrCurrentVDDMax:3; /* Max. write current @ VDD max */
  91. uint8_t DeviceSizeMul:3; /* Device size multiplier */
  92. } struct_v1;
  93. typedef struct
  94. {
  95. uint8_t Reserved1:6; /* Reserved */
  96. uint32_t DeviceSize:22; /* Device Size */
  97. uint8_t Reserved2:1; /* Reserved */
  98. } struct_v2;
  99. /**
  100. * @brief Card Specific Data: CSD Register
  101. */
  102. typedef struct
  103. {
  104. /* Header part */
  105. uint8_t CSDStruct:2; /* CSD structure */
  106. uint8_t Reserved1:6; /* Reserved */
  107. uint8_t TAAC:8; /* Data read access-time 1 */
  108. uint8_t NSAC:8; /* Data read access-time 2 in CLK cycles */
  109. uint8_t MaxBusClkFrec:8; /* Max. bus clock frequency */
  110. uint16_t CardComdClasses:12; /* Card command classes */
  111. uint8_t RdBlockLen:4; /* Max. read data block length */
  112. uint8_t PartBlockRead:1; /* Partial blocks for read allowed */
  113. uint8_t WrBlockMisalign:1; /* Write block misalignment */
  114. uint8_t RdBlockMisalign:1; /* Read block misalignment */
  115. uint8_t DSRImpl:1; /* DSR implemented */
  116. /* v1 or v2 struct */
  117. union csd_version {
  118. struct_v1 v1;
  119. struct_v2 v2;
  120. } version;
  121. uint8_t EraseSingleBlockEnable:1; /* Erase single block enable */
  122. uint8_t EraseSectorSize:7; /* Erase group size multiplier */
  123. uint8_t WrProtectGrSize:7; /* Write protect group size */
  124. uint8_t WrProtectGrEnable:1; /* Write protect group enable */
  125. uint8_t Reserved2:2; /* Reserved */
  126. uint8_t WrSpeedFact:3; /* Write speed factor */
  127. uint8_t MaxWrBlockLen:4; /* Max. write data block length */
  128. uint8_t WriteBlockPartial:1; /* Partial blocks for write allowed */
  129. uint8_t Reserved3:5; /* Reserved */
  130. uint8_t FileFormatGrouop:1; /* File format group */
  131. uint8_t CopyFlag:1; /* Copy flag (OTP) */
  132. uint8_t PermWrProtect:1; /* Permanent write protection */
  133. uint8_t TempWrProtect:1; /* Temporary write protection */
  134. uint8_t FileFormat:2; /* File Format */
  135. uint8_t Reserved4:2; /* Reserved */
  136. uint8_t crc:7; /* Reserved */
  137. uint8_t Reserved5:1; /* always 1*/
  138. } SD_CSD;
  139. /**
  140. * @brief Card Identification Data: CID Register
  141. */
  142. typedef struct
  143. {
  144. uint8_t ManufacturerID; /* ManufacturerID */
  145. uint16_t OEM_AppliID; /* OEM/Application ID */
  146. uint32_t ProdName1; /* Product Name part1 */
  147. uint8_t ProdName2; /* Product Name part2*/
  148. uint8_t ProdRev; /* Product Revision */
  149. uint32_t ProdSN; /* Product Serial Number */
  150. uint8_t Reserved1; /* Reserved1 */
  151. uint16_t ManufactDate; /* Manufacturing Date */
  152. uint8_t CID_CRC; /* CID CRC */
  153. uint8_t Reserved2; /* always 1 */
  154. } SD_CID;
  155. /**
  156. * @brief SD Card information
  157. */
  158. typedef struct
  159. {
  160. SD_CSD Csd;
  161. SD_CID Cid;
  162. uint64_t CardCapacity; /*!< Card Capacity */
  163. uint32_t LogBlockNbr; /*!< Specifies the Card logical Capacity in blocks */
  164. uint32_t CardBlockSize; /*!< Card Block Size */
  165. uint32_t LogBlockSize; /*!< Specifies logical block size in bytes */
  166. } SD_CardInfo;
  167. typedef struct
  168. {
  169. SD_CardInfo *Info;
  170. Buffer_Struct DataBuf;
  171. HANDLE locker;
  172. uint32_t Size; //flash的大小KB
  173. uint32_t OCR;
  174. uint32_t SpiSpeed;
  175. uint8_t *TempData;
  176. uint16_t WriteWaitCnt;
  177. uint8_t CSPin;
  178. uint8_t SpiID;
  179. uint8_t SDHCState;
  180. uint8_t IsInitDone;
  181. uint8_t IsCRCCheck;
  182. uint8_t SDHCError;
  183. uint8_t SPIError;
  184. uint8_t ExternResult[8];
  185. uint8_t ExternLen;
  186. uint8_t SDSC;
  187. }luat_spitf_ctrl_t;
  188. #define SPI_TF_WAIT(x) luat_rtos_task_sleep(x)
  189. static luat_spitf_ctrl_t g_s_spitf;
  190. static void luat_spitf_read_config(luat_spitf_ctrl_t *spitf);
  191. static void luat_spitf_cs(luat_spitf_ctrl_t *spitf, uint8_t OnOff)
  192. {
  193. uint8_t Temp[1] = {0xff};
  194. luat_gpio_set(spitf->CSPin, !OnOff);
  195. if (!OnOff)
  196. {
  197. luat_spi_send(spitf->SpiID, (const char *)Temp, 1);
  198. }
  199. }
  200. static uint8_t CRC7(uint8_t * chr, int cnt)
  201. {
  202. int i,a;
  203. uint8_t crc,Data;
  204. crc=0;
  205. for (a=0;a<cnt;a++)
  206. {
  207. Data=chr[a];
  208. for (i=0;i<8;i++)
  209. {
  210. crc <<= 1;
  211. if ((Data & 0x80)^(crc & 0x80))
  212. crc ^=0x09;
  213. Data <<= 1;
  214. }
  215. }
  216. crc=(crc<<1)|1;
  217. return(crc);
  218. }
  219. extern void DBG_HexPrintf(void *Data, unsigned int len);
  220. static int32_t luat_spitf_cmd(luat_spitf_ctrl_t *spitf, uint8_t Cmd, uint32_t Arg, uint8_t NeedStop)
  221. {
  222. uint8_t i, TxLen, DummyLen;
  223. int32_t Result = -ERROR_OPERATION_FAILED;
  224. luat_spitf_cs(spitf, 1);
  225. spitf->TempData[0] = 0x40|Cmd;
  226. BytesPutBe32(spitf->TempData + 1, Arg);
  227. spitf->TempData[5] = CRC7(spitf->TempData, 5);
  228. memset(spitf->TempData + 6, 0xff, 8);
  229. TxLen = 14;
  230. spitf->SPIError = 0;
  231. spitf->SDHCError = 0;
  232. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  233. for(i = 7; i < TxLen; i++)
  234. {
  235. if (spitf->TempData[i] != 0xff)
  236. {
  237. spitf->SDHCState = spitf->TempData[i];
  238. if ((spitf->SDHCState == !spitf->IsInitDone) || !spitf->SDHCState)
  239. {
  240. Result = ERROR_NONE;
  241. }
  242. DummyLen = TxLen - i - 1;
  243. memcpy(spitf->ExternResult, &spitf->TempData[i + 1], DummyLen);
  244. spitf->ExternLen = DummyLen;
  245. break;
  246. }
  247. }
  248. if (NeedStop)
  249. {
  250. luat_spitf_cs(spitf, 0);
  251. }
  252. if (Result)
  253. {
  254. LLOGE("cmd %d arg %x result %d", Cmd, Arg, Result);
  255. DBG_HexPrintf(spitf->TempData, TxLen);
  256. }
  257. return Result;
  258. }
  259. static int32_t luat_spitf_read_reg(luat_spitf_ctrl_t *spitf, uint8_t *RegDataBuf, uint8_t DataLen)
  260. {
  261. uint64_t OpEndTick;
  262. int Result = ERROR_NONE;
  263. uint16_t DummyLen;
  264. uint16_t i,offset;
  265. spitf->SPIError = 0;
  266. spitf->SDHCError = 0;
  267. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS * 4;
  268. offset = 0;
  269. for(i = 0; i < spitf->ExternLen; i++)
  270. {
  271. if (spitf->ExternResult[i] != 0xff)
  272. {
  273. if (0xfe == spitf->ExternResult[i])
  274. {
  275. offset = 1;
  276. }
  277. else
  278. {
  279. LLOGD("no 0xfe find %d,%x",i,spitf->ExternResult[i]);
  280. }
  281. DummyLen = spitf->ExternLen - i - offset;
  282. memcpy(RegDataBuf, &spitf->ExternResult[i + offset], DummyLen);
  283. memset(RegDataBuf + DummyLen, 0xff, DataLen - DummyLen);
  284. luat_spi_transfer(spitf->SpiID, (const char *)(RegDataBuf + DummyLen), DataLen - DummyLen, (char *)(RegDataBuf + DummyLen), DataLen - DummyLen);
  285. goto SDHC_SPIREADREGDATA_DONE;
  286. }
  287. }
  288. while((luat_mcu_tick64_ms() < OpEndTick) && !spitf->SDHCError)
  289. {
  290. memset(spitf->TempData, 0xff, 40);
  291. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 40, (char *)spitf->TempData, 40);
  292. for(i = 0; i < 40; i++)
  293. {
  294. if (spitf->TempData[i] != 0xff)
  295. {
  296. if (0xfe == spitf->TempData[i])
  297. {
  298. offset = 1;
  299. }
  300. else
  301. {
  302. LLOGD("no 0xfe find %d,%x",i,spitf->TempData[i]);
  303. }
  304. DummyLen = 40 - i - offset;
  305. if (DummyLen >= DataLen)
  306. {
  307. memcpy(RegDataBuf, &spitf->TempData[i + offset], DataLen);
  308. goto SDHC_SPIREADREGDATA_DONE;
  309. }
  310. else
  311. {
  312. memcpy(RegDataBuf, &spitf->TempData[i + offset], DummyLen);
  313. memset(RegDataBuf + DummyLen, 0xff, DataLen - DummyLen);
  314. luat_spi_transfer(spitf->SpiID, (const char *)(RegDataBuf + DummyLen), DataLen - DummyLen, (char *)(RegDataBuf + DummyLen), DataLen - DummyLen);
  315. goto SDHC_SPIREADREGDATA_DONE;
  316. }
  317. }
  318. }
  319. SPI_TF_WAIT(1);
  320. }
  321. LLOGD("read config reg timeout!");
  322. Result = -ERROR_OPERATION_FAILED;
  323. SDHC_SPIREADREGDATA_DONE:
  324. luat_spitf_cs(spitf, 0);
  325. return Result;
  326. }
  327. static int32_t luat_spitf_write_data(luat_spitf_ctrl_t *spitf)
  328. {
  329. uint64_t OpEndTick;
  330. int Result = -ERROR_OPERATION_FAILED;
  331. uint16_t TxLen, DoneFlag, waitCnt;
  332. uint16_t i, crc16;
  333. spitf->SPIError = 0;
  334. spitf->SDHCError = 0;
  335. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  336. while( (spitf->DataBuf.Pos < spitf->DataBuf.MaxLen) && (luat_mcu_tick64_ms() < OpEndTick) )
  337. {
  338. spitf->TempData[0] = 0xff;
  339. spitf->TempData[1] = 0xff;
  340. //LLOGD("%u,%u", spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  341. spitf->TempData[2] = 0xfc;
  342. memcpy(spitf->TempData + 3, spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__);
  343. crc16 = CRC16Cal(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__, 0, CRC16_CCITT_GEN, 0);
  344. BytesPutBe16(spitf->TempData + 3 + __SDHC_BLOCK_LEN__, crc16);
  345. spitf->TempData[5 + __SDHC_BLOCK_LEN__] = 0xff;
  346. TxLen = 6 + __SDHC_BLOCK_LEN__;
  347. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  348. if ((spitf->TempData[5 + __SDHC_BLOCK_LEN__] & 0x1f) != 0x05)
  349. {
  350. LLOGD("write data error! %d %02x", spitf->DataBuf.Pos, spitf->TempData[5 + __SDHC_BLOCK_LEN__]);
  351. spitf->SDHCError = 1;
  352. goto SDHC_SPIWRITEBLOCKDATA_DONE;
  353. }
  354. DoneFlag = 0;
  355. waitCnt = 0;
  356. while( (luat_mcu_tick64_ms() < OpEndTick) && !DoneFlag )
  357. {
  358. TxLen = spitf->WriteWaitCnt?spitf->WriteWaitCnt:80;
  359. memset(spitf->TempData, 0xff, TxLen);
  360. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  361. for(i = 4; i < TxLen; i++)
  362. {
  363. if (spitf->TempData[i] == 0xff)
  364. {
  365. DoneFlag = 1;
  366. if ((i + waitCnt) < __SDHC_BLOCK_LEN__)
  367. {
  368. if ((i + waitCnt) != spitf->WriteWaitCnt)
  369. {
  370. spitf->WriteWaitCnt = i + waitCnt + 8;
  371. }
  372. }
  373. break;
  374. }
  375. }
  376. waitCnt += TxLen;
  377. }
  378. if (!DoneFlag)
  379. {
  380. LLOGD("write data timeout!");
  381. spitf->SDHCError = 1;
  382. goto SDHC_SPIWRITEBLOCKDATA_DONE;
  383. }
  384. spitf->DataBuf.Pos++;
  385. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  386. }
  387. Result = ERROR_NONE;
  388. SDHC_SPIWRITEBLOCKDATA_DONE:
  389. spitf->TempData[0] = 0xfd;
  390. luat_spi_send(spitf->SpiID, (const char *)spitf->TempData, 1);
  391. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  392. DoneFlag = 0;
  393. while( (luat_mcu_tick64_ms() < OpEndTick) && !DoneFlag )
  394. {
  395. TxLen = 512;
  396. memset(spitf->TempData, 0xff, TxLen);
  397. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  398. for(i = 4; i < TxLen; i++)
  399. {
  400. if (spitf->TempData[i] == 0xff)
  401. {
  402. DoneFlag = 1;
  403. break;
  404. }
  405. }
  406. }
  407. luat_spitf_cs(spitf, 0);
  408. return Result;
  409. }
  410. static int32_t luat_spitf_read_data(luat_spitf_ctrl_t *spitf)
  411. {
  412. uint64_t OpEndTick;
  413. int Result = -ERROR_OPERATION_FAILED;
  414. uint16_t ReadLen, DummyLen, RemainingLen;
  415. uint16_t i, crc16, crc16_check;
  416. uint8_t *pBuf;
  417. spitf->SPIError = 0;
  418. spitf->SDHCError = 0;
  419. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS;
  420. while( (spitf->DataBuf.Pos < spitf->DataBuf.MaxLen) && (luat_mcu_tick64_ms() < OpEndTick) )
  421. {
  422. DummyLen = (__SDHC_BLOCK_LEN__ >> 1);
  423. memset(spitf->TempData, 0xff, DummyLen);
  424. // LLOGD("read blocks %u,%u", spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  425. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, DummyLen, (char *)spitf->TempData, DummyLen);
  426. RemainingLen = 0;
  427. for(i = 0; i < DummyLen; i++)
  428. {
  429. if (spitf->TempData[i] == 0xfe)
  430. {
  431. ReadLen = (DummyLen - i - 1);
  432. RemainingLen = __SDHC_BLOCK_LEN__ - ReadLen;
  433. if (ReadLen)
  434. {
  435. memcpy(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, spitf->TempData + i + 1, ReadLen);
  436. }
  437. // LLOGD("read result %u,%u", ReadLen, RemainingLen);
  438. goto READ_REST_DATA;
  439. }
  440. }
  441. continue;
  442. READ_REST_DATA:
  443. pBuf = spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__ + ReadLen;
  444. memset(pBuf, 0xff, RemainingLen);
  445. luat_spi_transfer(spitf->SpiID, (const char *)pBuf, RemainingLen, (char *)pBuf, RemainingLen);
  446. memset(spitf->TempData, 0xff, 2);
  447. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 2, (char *)spitf->TempData, 2);
  448. // if (spitf->IsCRCCheck)
  449. {
  450. crc16 = CRC16Cal(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__, 0, CRC16_CCITT_GEN, 0);
  451. crc16_check = BytesGetBe16(spitf->TempData);
  452. if (crc16 != crc16_check)
  453. {
  454. LLOGD("crc16 error %04x %04x", crc16, crc16_check);
  455. Result = ERROR_NONE;
  456. goto SDHC_SPIREADBLOCKDATA_DONE;
  457. }
  458. }
  459. spitf->DataBuf.Pos++;
  460. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS;
  461. }
  462. Result = ERROR_NONE;
  463. SDHC_SPIREADBLOCKDATA_DONE:
  464. return Result;
  465. }
  466. static void luat_spitf_init(luat_spitf_ctrl_t *spitf)
  467. {
  468. uint64_t OpEndTick;
  469. if (!spitf->Info)
  470. {
  471. spitf->Info = luat_heap_malloc(sizeof(SD_CardInfo));
  472. }
  473. memset(spitf->Info, 0, sizeof(SD_CardInfo));
  474. if (!spitf->TempData)
  475. {
  476. spitf->TempData = luat_heap_malloc(__SDHC_BLOCK_LEN__ + 8);
  477. }
  478. luat_spi_change_speed(spitf->SpiID, 400000);
  479. spitf->IsInitDone = 0;
  480. spitf->SDHCState = 0xff;
  481. spitf->Info->CardCapacity = 0;
  482. spitf->WriteWaitCnt = 80;
  483. luat_gpio_set(spitf->CSPin, 0);
  484. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 40, (char *)spitf->TempData, 40);
  485. luat_gpio_set(spitf->CSPin, 1);
  486. memset(spitf->TempData, 0xff, 40);
  487. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 40, (char *)spitf->TempData, 40);
  488. spitf->SDSC = 0;
  489. if (luat_spitf_cmd(spitf, CMD0, 0, 1))
  490. {
  491. goto INIT_DONE;
  492. }
  493. OpEndTick = luat_mcu_tick64_ms() + 3000;
  494. if (luat_spitf_cmd(spitf, CMD8, 0x1aa, 1)) //只支持2G以上的SDHC卡
  495. {
  496. LLOGD("tf cmd8 not support");
  497. spitf->SDSC = 1;
  498. }
  499. WAIT_INIT_DONE:
  500. if (luat_mcu_tick64_ms() >= OpEndTick)
  501. {
  502. LLOGD("tf init timeout!");
  503. goto INIT_DONE;
  504. }
  505. if (luat_spitf_cmd(spitf, SD_CMD_APP_CMD, 0, 1))
  506. {
  507. goto INIT_DONE;
  508. }
  509. if (!spitf->SDSC)
  510. {
  511. if (luat_spitf_cmd(spitf, SD_CMD_SD_APP_OP_COND, 0x40000000, 1))
  512. {
  513. goto INIT_DONE;
  514. }
  515. }
  516. else
  517. {
  518. if (luat_spitf_cmd(spitf, SD_CMD_SD_APP_OP_COND, 0, 1))
  519. {
  520. goto INIT_DONE;
  521. }
  522. }
  523. spitf->IsInitDone = !spitf->SDHCState;
  524. if (!spitf->IsInitDone)
  525. {
  526. SPI_TF_WAIT(10);
  527. goto WAIT_INIT_DONE;
  528. }
  529. if (luat_spitf_cmd(spitf, CMD58, 0, 1))
  530. {
  531. goto INIT_DONE;
  532. }
  533. spitf->OCR = BytesGetBe32(spitf->ExternResult);
  534. luat_spi_change_speed(spitf->SpiID, spitf->SpiSpeed);
  535. luat_spitf_read_config(spitf);
  536. LLOGD("sdcard init OK OCR:0x%08x!", spitf->OCR);
  537. return;
  538. INIT_DONE:
  539. if (!spitf->IsInitDone)
  540. {
  541. LLOGD("sdcard init fail!");
  542. }
  543. return;
  544. }
  545. static void luat_spitf_read_config(luat_spitf_ctrl_t *spitf)
  546. {
  547. uint8_t CSD_Tab[18];
  548. SD_CSD* Csd = &spitf->Info->Csd;
  549. SD_CardInfo *pCardInfo = spitf->Info;
  550. uint64_t Temp;
  551. uint8_t flag_SDHC = (spitf->OCR & 0x40000000) >> 30;
  552. spitf->SDSC = !flag_SDHC;
  553. if (spitf->Info->CardCapacity) return;
  554. if (luat_spitf_cmd(spitf, CMD9, 0, 0))
  555. {
  556. goto READ_CONFIG_ERROR;
  557. }
  558. if (spitf->SDHCState)
  559. {
  560. goto READ_CONFIG_ERROR;
  561. }
  562. if (luat_spitf_read_reg(spitf, CSD_Tab, 18))
  563. {
  564. goto READ_CONFIG_ERROR;
  565. }
  566. /*************************************************************************
  567. CSD header decoding
  568. *************************************************************************/
  569. /* Byte 0 */
  570. Csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  571. Csd->Reserved1 = CSD_Tab[0] & 0x3F;
  572. /* Byte 1 */
  573. Csd->TAAC = CSD_Tab[1];
  574. /* Byte 2 */
  575. Csd->NSAC = CSD_Tab[2];
  576. /* Byte 3 */
  577. Csd->MaxBusClkFrec = CSD_Tab[3];
  578. /* Byte 4/5 */
  579. Csd->CardComdClasses = (CSD_Tab[4] << 4) | ((CSD_Tab[5] & 0xF0) >> 4);
  580. Csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  581. /* Byte 6 */
  582. Csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  583. Csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  584. Csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  585. Csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  586. /*************************************************************************
  587. CSD v1/v2 decoding
  588. *************************************************************************/
  589. if(!flag_SDHC)
  590. {
  591. Csd->version.v1.Reserved1 = ((CSD_Tab[6] & 0x0C) >> 2);
  592. Csd->version.v1.DeviceSize = ((CSD_Tab[6] & 0x03) << 10)
  593. | (CSD_Tab[7] << 2)
  594. | ((CSD_Tab[8] & 0xC0) >> 6);
  595. Csd->version.v1.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  596. Csd->version.v1.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  597. Csd->version.v1.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  598. Csd->version.v1.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  599. Csd->version.v1.DeviceSizeMul = ((CSD_Tab[9] & 0x03) << 1)
  600. |((CSD_Tab[10] & 0x80) >> 7);
  601. }
  602. else
  603. {
  604. Csd->version.v2.Reserved1 = ((CSD_Tab[6] & 0x0F) << 2) | ((CSD_Tab[7] & 0xC0) >> 6);
  605. Csd->version.v2.DeviceSize= ((CSD_Tab[7] & 0x3F) << 16) | (CSD_Tab[8] << 8) | CSD_Tab[9];
  606. Csd->version.v2.Reserved2 = ((CSD_Tab[10] & 0x80) >> 8);
  607. }
  608. Csd->EraseSingleBlockEnable = (CSD_Tab[10] & 0x40) >> 6;
  609. Csd->EraseSectorSize = ((CSD_Tab[10] & 0x3F) << 1)
  610. |((CSD_Tab[11] & 0x80) >> 7);
  611. Csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
  612. Csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  613. Csd->Reserved2 = (CSD_Tab[12] & 0x60) >> 5;
  614. Csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  615. Csd->MaxWrBlockLen = ((CSD_Tab[12] & 0x03) << 2)
  616. |((CSD_Tab[13] & 0xC0) >> 6);
  617. Csd->WriteBlockPartial = (CSD_Tab[13] & 0x20) >> 5;
  618. Csd->Reserved3 = (CSD_Tab[13] & 0x1F);
  619. Csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  620. Csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  621. Csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  622. Csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  623. Csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  624. Csd->Reserved4 = (CSD_Tab[14] & 0x03);
  625. Csd->crc = (CSD_Tab[15] & 0xFE) >> 1;
  626. Csd->Reserved5 = (CSD_Tab[15] & 0x01);
  627. #if 0
  628. if (luat_spitf_cmd(spitf, CMD10, 0, 0))
  629. {
  630. goto READ_CONFIG_ERROR;
  631. }
  632. if (spitf->SDHCState)
  633. {
  634. goto READ_CONFIG_ERROR;
  635. }
  636. if (luat_spitf_read_reg(Ctrl, CID_Tab, 18))
  637. {
  638. goto READ_CONFIG_ERROR;
  639. }
  640. /* Byte 0 */
  641. Cid->ManufacturerID = CID_Tab[0];
  642. /* Byte 1 */
  643. Cid->OEM_AppliID = CID_Tab[1] << 8;
  644. /* Byte 2 */
  645. Cid->OEM_AppliID |= CID_Tab[2];
  646. /* Byte 3 */
  647. Cid->ProdName1 = CID_Tab[3] << 24;
  648. /* Byte 4 */
  649. Cid->ProdName1 |= CID_Tab[4] << 16;
  650. /* Byte 5 */
  651. Cid->ProdName1 |= CID_Tab[5] << 8;
  652. /* Byte 6 */
  653. Cid->ProdName1 |= CID_Tab[6];
  654. /* Byte 7 */
  655. Cid->ProdName2 = CID_Tab[7];
  656. /* Byte 8 */
  657. Cid->ProdRev = CID_Tab[8];
  658. /* Byte 9 */
  659. Cid->ProdSN = CID_Tab[9] << 24;
  660. /* Byte 10 */
  661. Cid->ProdSN |= CID_Tab[10] << 16;
  662. /* Byte 11 */
  663. Cid->ProdSN |= CID_Tab[11] << 8;
  664. /* Byte 12 */
  665. Cid->ProdSN |= CID_Tab[12];
  666. /* Byte 13 */
  667. Cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  668. Cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  669. /* Byte 14 */
  670. Cid->ManufactDate |= CID_Tab[14];
  671. /* Byte 15 */
  672. Cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
  673. Cid->Reserved2 = 1;
  674. #endif
  675. if(flag_SDHC)
  676. {
  677. pCardInfo->LogBlockSize = 512;
  678. pCardInfo->CardBlockSize = 512;
  679. Temp = 1024 * pCardInfo->LogBlockSize;
  680. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v2.DeviceSize + 1) * Temp;
  681. pCardInfo->LogBlockNbr = (pCardInfo->Csd.version.v2.DeviceSize + 1) * 1024;
  682. }
  683. else
  684. {
  685. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1) ;
  686. pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.version.v1.DeviceSizeMul + 2));
  687. pCardInfo->LogBlockSize = 512;
  688. pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
  689. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  690. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  691. }
  692. LLOGD("卡容量 %lluKB", pCardInfo->CardCapacity/1024);
  693. return;
  694. READ_CONFIG_ERROR:
  695. spitf->IsInitDone = 0;
  696. spitf->SDHCError = 1;
  697. return;
  698. }
  699. static void luat_spitf_read_blocks(luat_spitf_ctrl_t *spitf, uint8_t *Buf, uint32_t StartLBA, uint32_t BlockNums)
  700. {
  701. uint8_t Retry = 0;
  702. uint8_t error = 1;
  703. uint32_t address;
  704. Buffer_StaticInit(&spitf->DataBuf, Buf, BlockNums);
  705. if (spitf->SDSC)
  706. {
  707. if (luat_spitf_cmd(spitf, CMD16, 512, 1))
  708. {
  709. goto SDHC_SPIREADBLOCKS_ERROR;
  710. }
  711. }
  712. SDHC_SPIREADBLOCKS_START:
  713. if (spitf->SDSC)
  714. {
  715. address = (StartLBA + spitf->DataBuf.Pos) * 512;
  716. }
  717. else
  718. {
  719. address = (StartLBA + spitf->DataBuf.Pos);
  720. }
  721. if (luat_spitf_cmd(spitf, CMD18, address, 0))
  722. {
  723. goto SDHC_SPIREADBLOCKS_CHECK;
  724. }
  725. if (luat_spitf_read_data(spitf))
  726. {
  727. luat_spitf_cmd(spitf, CMD12, 0, 1);
  728. goto SDHC_SPIREADBLOCKS_CHECK;
  729. }
  730. for (int i = 0; i < 3; i++)
  731. {
  732. if (!luat_spitf_cmd(spitf, CMD12, 0, 1))
  733. {
  734. error = 0;
  735. break;
  736. }
  737. else
  738. {
  739. spitf->SDHCError = 0;
  740. spitf->IsInitDone = 1;
  741. spitf->SDHCState = 0;
  742. }
  743. }
  744. SDHC_SPIREADBLOCKS_CHECK:
  745. if (error)
  746. {
  747. LLOGD("read error %x,%u,%u",spitf->SDHCState, spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  748. }
  749. if (spitf->DataBuf.Pos != spitf->DataBuf.MaxLen)
  750. {
  751. Retry++;
  752. LLOGD("read retry %d,%u,%u,%u", Retry, StartLBA, spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  753. if (Retry > 3)
  754. {
  755. spitf->SDHCError = 1;
  756. goto SDHC_SPIREADBLOCKS_ERROR;
  757. }
  758. else
  759. {
  760. spitf->SDHCError = 0;
  761. spitf->IsInitDone = 1;
  762. spitf->SDHCState = 0;
  763. }
  764. goto SDHC_SPIREADBLOCKS_START;
  765. }
  766. return;
  767. SDHC_SPIREADBLOCKS_ERROR:
  768. LLOGD("read error!");
  769. spitf->IsInitDone = 0;
  770. spitf->SDHCError = 1;
  771. return;
  772. }
  773. static void luat_spitf_write_blocks(luat_spitf_ctrl_t *spitf, const uint8_t *Buf, uint32_t StartLBA, uint32_t BlockNums)
  774. {
  775. uint8_t Retry = 0;
  776. uint32_t address;
  777. Buffer_StaticInit(&spitf->DataBuf, (void *)Buf, BlockNums);
  778. if (spitf->SDSC)
  779. {
  780. if (luat_spitf_cmd(spitf, CMD16, 512, 1))
  781. {
  782. goto SDHC_SPIWRITEBLOCKS_ERROR;
  783. }
  784. }
  785. SDHC_SPIWRITEBLOCKS_START:
  786. if (spitf->SDSC)
  787. {
  788. address = (StartLBA + spitf->DataBuf.Pos) * 512;
  789. }
  790. else
  791. {
  792. address = (StartLBA + spitf->DataBuf.Pos);
  793. }
  794. if (luat_spitf_cmd(spitf, CMD25, address, 0))
  795. {
  796. goto SDHC_SPIWRITEBLOCKS_ERROR;
  797. }
  798. if (luat_spitf_write_data(spitf))
  799. {
  800. goto SDHC_SPIWRITEBLOCKS_ERROR;
  801. }
  802. if (spitf->DataBuf.Pos != spitf->DataBuf.MaxLen)
  803. {
  804. Retry++;
  805. LLOGD("write retry %d", Retry);
  806. if (Retry > 3)
  807. {
  808. spitf->SDHCError = 1;
  809. goto SDHC_SPIWRITEBLOCKS_ERROR;
  810. }
  811. goto SDHC_SPIWRITEBLOCKS_START;
  812. }
  813. return;
  814. SDHC_SPIWRITEBLOCKS_ERROR:
  815. luat_spitf_cs(spitf, 0);
  816. LLOGD("write error!");
  817. spitf->IsInitDone = 0;
  818. spitf->SDHCError = 1;
  819. return;
  820. }
  821. static uint8_t luat_spitf_is_ready(luat_spitf_ctrl_t *spitf)
  822. {
  823. if (!spitf->SDHCState && spitf->IsInitDone)
  824. {
  825. return 1;
  826. }
  827. else
  828. {
  829. LLOGD("SDHC error, please reboot tf card");
  830. return 0;
  831. }
  832. }
  833. static DSTATUS luat_spitf_initialize(void* userdata)
  834. {
  835. luat_mutex_lock(g_s_spitf.locker);
  836. luat_spitf_init(&g_s_spitf);
  837. luat_mutex_unlock(g_s_spitf.locker);
  838. return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
  839. }
  840. static DSTATUS luat_spitf_status(void* userdata)
  841. {
  842. return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
  843. }
  844. static DRESULT luat_spitf_read(void* userdata, uint8_t* buff, LBA_t sector, UINT count)
  845. {
  846. luat_mutex_lock(g_s_spitf.locker);
  847. if (!luat_spitf_is_ready(&g_s_spitf))
  848. {
  849. luat_mutex_unlock(g_s_spitf.locker);
  850. return RES_NOTRDY;
  851. }
  852. luat_spitf_read_blocks(&g_s_spitf, buff, sector, count);
  853. luat_mutex_unlock(g_s_spitf.locker);
  854. return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
  855. }
  856. static DRESULT luat_spitf_write(void* userdata, const uint8_t* buff, LBA_t sector, UINT count)
  857. {
  858. luat_mutex_lock(g_s_spitf.locker);
  859. if (!luat_spitf_is_ready(&g_s_spitf))
  860. {
  861. luat_mutex_unlock(g_s_spitf.locker);
  862. return RES_NOTRDY;
  863. }
  864. luat_spitf_write_blocks(&g_s_spitf, buff, sector, count);
  865. luat_mutex_unlock(g_s_spitf.locker);
  866. return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
  867. }
  868. static DRESULT luat_spitf_ioctl(void* userdata, uint8_t ctrl, void* buff)
  869. {
  870. luat_mutex_lock(g_s_spitf.locker);
  871. if (!luat_spitf_is_ready(&g_s_spitf))
  872. {
  873. luat_mutex_unlock(g_s_spitf.locker);
  874. return RES_NOTRDY;
  875. }
  876. luat_spitf_read_config(&g_s_spitf);
  877. luat_mutex_unlock(g_s_spitf.locker);
  878. switch (ctrl) {
  879. case CTRL_SYNC : /* Make sure that no pending write process */
  880. return RES_OK;
  881. break;
  882. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  883. *(uint32_t*)buff = g_s_spitf.Info->LogBlockNbr;
  884. return RES_OK;
  885. break;
  886. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  887. *(uint32_t*)buff = 128;
  888. return RES_OK;
  889. break;
  890. default:
  891. return RES_PARERR;
  892. }
  893. return RES_PARERR;
  894. }
  895. const block_disk_opts_t spitf_disk_opts = {
  896. .initialize = luat_spitf_initialize,
  897. .status = luat_spitf_status,
  898. .read = luat_spitf_read,
  899. .write = luat_spitf_write,
  900. .ioctl = luat_spitf_ioctl,
  901. };
  902. void luat_spi_set_sdhc_ctrl_default(
  903. block_disk_t *disk
  904. )
  905. {
  906. luat_fatfs_spi_t* userdata = disk->userdata;
  907. if (userdata->type)
  908. {
  909. g_s_spitf.CSPin = userdata->spi_device->spi_config.cs;
  910. g_s_spitf.SpiID = userdata->spi_device->bus_id;
  911. g_s_spitf.SpiSpeed = userdata->fast_speed;
  912. }
  913. else
  914. {
  915. g_s_spitf.CSPin = userdata->spi_cs;
  916. g_s_spitf.SpiID = userdata->spi_id;
  917. g_s_spitf.SpiSpeed = userdata->fast_speed;
  918. }
  919. if (!g_s_spitf.locker)
  920. {
  921. g_s_spitf.locker = luat_mutex_create();
  922. }
  923. luat_heap_free(disk->userdata);
  924. disk->userdata = NULL;
  925. disk->opts = &spitf_disk_opts;
  926. }
  927. #ifndef LUAT_COMPILER_NOWEAK
  928. __attribute__((weak)) void luat_spi_set_sdhc_ctrl(
  929. block_disk_t *disk)
  930. {
  931. luat_spi_set_sdhc_ctrl_default(disk);
  932. }
  933. #else
  934. void luat_spi_set_sdhc_ctrl(block_disk_t *disk);
  935. #endif
  936. static block_disk_t disk = {0};
  937. DRESULT diskio_open_spitf(BYTE pdrv, luat_fatfs_spi_t* userdata) {
  938. // 暂时只支持单个fatfs实例
  939. disk.opts = &spitf_disk_opts;
  940. disk.userdata = userdata;
  941. luat_spi_set_sdhc_ctrl(&disk);
  942. return diskio_open(pdrv, &disk);
  943. }
  944. //static DWORD get_fattime() {
  945. // how to get?
  946. //}
  947. //--------------------------------------------------------------------------------------