diskio_spitf.c 30 KB

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