diskio_spitf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 ((uint64_t)FATFS_WRITE_TO)
  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 SDHCError;
  184. uint8_t SPIError;
  185. uint8_t ExternResult[64];
  186. uint8_t ExternLen;
  187. uint8_t SDSC;
  188. uint8_t ResetCnt;
  189. uint8_t CmdCnt;
  190. }luat_spitf_ctrl_t;
  191. #define SPI_TF_WAIT(x) luat_rtos_task_sleep(x)
  192. extern uint8_t FATFS_NO_CRC_CHECK;
  193. extern uint16_t FATFS_WRITE_TO;
  194. static luat_spitf_ctrl_t g_s_spitf;
  195. static void luat_spitf_read_config(luat_spitf_ctrl_t *spitf);
  196. static void luat_spitf_cs(luat_spitf_ctrl_t *spitf, uint8_t OnOff)
  197. {
  198. uint8_t Temp[1] = {0xff};
  199. luat_gpio_set(spitf->CSPin, !OnOff);
  200. if (!OnOff)
  201. {
  202. luat_spi_send(spitf->SpiID, (const char *)Temp, 1);
  203. }
  204. }
  205. static uint8_t CRC7(uint8_t * chr, int cnt)
  206. {
  207. int i,a;
  208. uint8_t crc,Data;
  209. crc=0;
  210. for (a=0;a<cnt;a++)
  211. {
  212. Data=chr[a];
  213. for (i=0;i<8;i++)
  214. {
  215. crc <<= 1;
  216. if ((Data & 0x80)^(crc & 0x80))
  217. crc ^=0x09;
  218. Data <<= 1;
  219. }
  220. }
  221. crc=(crc<<1)|1;
  222. return(crc);
  223. }
  224. extern void DBG_HexPrintf(void *Data, unsigned int len);
  225. static int32_t luat_spitf_cmd(luat_spitf_ctrl_t *spitf, uint8_t Cmd, uint32_t Arg, uint8_t NeedStop)
  226. {
  227. uint8_t i, TxLen, DummyLen;
  228. int32_t Result = -ERROR_OPERATION_FAILED;
  229. luat_spitf_cs(spitf, 1);
  230. spitf->TempData[0] = 0x40|Cmd;
  231. BytesPutBe32(spitf->TempData + 1, Arg);
  232. spitf->TempData[5] = CRC7(spitf->TempData, 5);
  233. TxLen = 6 + spitf->CmdCnt;
  234. memset(spitf->TempData + 6, 0xff, TxLen - 6);
  235. spitf->SPIError = 0;
  236. spitf->SDHCError = 0;
  237. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  238. for(i = 7; i < TxLen; i++)
  239. {
  240. if (spitf->TempData[i] != 0xff)
  241. {
  242. // LLOGE("find answer byte in %d", i);
  243. if (spitf->CmdCnt != (i + 1))
  244. {
  245. spitf->CmdCnt = i + 1;
  246. // LLOGI("find answer byte in %d", i);
  247. }
  248. spitf->SDHCState = spitf->TempData[i];
  249. if ((spitf->SDHCState == !spitf->IsInitDone) || !spitf->SDHCState)
  250. {
  251. Result = ERROR_NONE;
  252. }
  253. DummyLen = TxLen - i - 1;
  254. memcpy(spitf->ExternResult, &spitf->TempData[i + 1], DummyLen);
  255. spitf->ExternLen = DummyLen;
  256. break;
  257. }
  258. }
  259. if (NeedStop)
  260. {
  261. luat_spitf_cs(spitf, 0);
  262. }
  263. if (Result)
  264. {
  265. LLOGE("cmd %d arg %x result %d", Cmd, Arg, Result);
  266. DBG_HexPrintf(spitf->TempData, TxLen);
  267. }
  268. return Result;
  269. }
  270. static int32_t luat_spitf_read_reg(luat_spitf_ctrl_t *spitf, uint8_t *RegDataBuf, uint8_t DataLen)
  271. {
  272. uint64_t OpEndTick;
  273. int Result = ERROR_NONE;
  274. uint16_t DummyLen;
  275. uint16_t i,offset;
  276. spitf->SPIError = 0;
  277. spitf->SDHCError = 0;
  278. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS * 4;
  279. offset = 0;
  280. for(i = 0; i < spitf->ExternLen; i++)
  281. {
  282. if (spitf->ExternResult[i] != 0xff)
  283. {
  284. if (0xfe == spitf->ExternResult[i])
  285. {
  286. offset = 1;
  287. }
  288. else
  289. {
  290. LLOGD("no 0xfe find %d,%x",i,spitf->ExternResult[i]);
  291. }
  292. DummyLen = spitf->ExternLen - i - offset;
  293. memcpy(RegDataBuf, &spitf->ExternResult[i + offset], DummyLen);
  294. memset(RegDataBuf + DummyLen, 0xff, DataLen - DummyLen);
  295. luat_spi_transfer(spitf->SpiID, (const char *)(RegDataBuf + DummyLen), DataLen - DummyLen, (char *)(RegDataBuf + DummyLen), DataLen - DummyLen);
  296. goto SDHC_SPIREADREGDATA_DONE;
  297. }
  298. }
  299. while((luat_mcu_tick64_ms() < OpEndTick) && !spitf->SDHCError)
  300. {
  301. memset(spitf->TempData, 0xff, 40);
  302. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 40, (char *)spitf->TempData, 40);
  303. for(i = 0; i < 40; i++)
  304. {
  305. if (spitf->TempData[i] != 0xff)
  306. {
  307. if (0xfe == spitf->TempData[i])
  308. {
  309. offset = 1;
  310. }
  311. else
  312. {
  313. LLOGD("no 0xfe find %d,%x",i,spitf->TempData[i]);
  314. }
  315. DummyLen = 40 - i - offset;
  316. if (DummyLen >= DataLen)
  317. {
  318. memcpy(RegDataBuf, &spitf->TempData[i + offset], DataLen);
  319. goto SDHC_SPIREADREGDATA_DONE;
  320. }
  321. else
  322. {
  323. memcpy(RegDataBuf, &spitf->TempData[i + offset], DummyLen);
  324. memset(RegDataBuf + DummyLen, 0xff, DataLen - DummyLen);
  325. luat_spi_transfer(spitf->SpiID, (const char *)(RegDataBuf + DummyLen), DataLen - DummyLen, (char *)(RegDataBuf + DummyLen), DataLen - DummyLen);
  326. goto SDHC_SPIREADREGDATA_DONE;
  327. }
  328. }
  329. }
  330. SPI_TF_WAIT(1);
  331. }
  332. LLOGD("read config reg timeout!");
  333. Result = -ERROR_OPERATION_FAILED;
  334. SDHC_SPIREADREGDATA_DONE:
  335. luat_spitf_cs(spitf, 0);
  336. return Result;
  337. }
  338. static int32_t luat_spitf_write_data(luat_spitf_ctrl_t *spitf)
  339. {
  340. uint64_t OpEndTick;
  341. int Result = -ERROR_OPERATION_FAILED;
  342. uint16_t TxLen, DoneFlag, waitCnt;
  343. uint16_t i, crc16;
  344. spitf->SPIError = 0;
  345. spitf->SDHCError = 0;
  346. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  347. while( (spitf->DataBuf.Pos < spitf->DataBuf.MaxLen) && (luat_mcu_tick64_ms() < OpEndTick) )
  348. {
  349. spitf->TempData[0] = 0xff;
  350. spitf->TempData[1] = 0xff;
  351. //LLOGD("%u,%u", spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  352. spitf->TempData[2] = 0xfc;
  353. memcpy(spitf->TempData + 3, spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__);
  354. crc16 = CRC16Cal(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__, 0, CRC16_CCITT_GEN, 0);
  355. BytesPutBe16(spitf->TempData + 3 + __SDHC_BLOCK_LEN__, crc16);
  356. spitf->TempData[5 + __SDHC_BLOCK_LEN__] = 0xff;
  357. TxLen = 6 + __SDHC_BLOCK_LEN__;
  358. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  359. if ((spitf->TempData[5 + __SDHC_BLOCK_LEN__] & 0x1f) != 0x05)
  360. {
  361. LLOGD("write data error! %d %02x", spitf->DataBuf.Pos, spitf->TempData[5 + __SDHC_BLOCK_LEN__]);
  362. spitf->SDHCError = 1;
  363. goto SDHC_SPIWRITEBLOCKDATA_DONE;
  364. }
  365. DoneFlag = 0;
  366. waitCnt = 0;
  367. while( (luat_mcu_tick64_ms() < OpEndTick) && !DoneFlag )
  368. {
  369. TxLen = spitf->WriteWaitCnt?spitf->WriteWaitCnt:80;
  370. memset(spitf->TempData, 0xff, TxLen);
  371. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  372. for(i = 4; i < TxLen; i++)
  373. {
  374. if (spitf->TempData[i] == 0xff)
  375. {
  376. DoneFlag = 1;
  377. if ((i + waitCnt) < __SDHC_BLOCK_LEN__)
  378. {
  379. if ((i + waitCnt) != spitf->WriteWaitCnt)
  380. {
  381. spitf->WriteWaitCnt = i + waitCnt + 8;
  382. }
  383. }
  384. break;
  385. }
  386. }
  387. waitCnt += TxLen;
  388. }
  389. if (!DoneFlag)
  390. {
  391. LLOGD("write data timeout!");
  392. spitf->SDHCError = 1;
  393. goto SDHC_SPIWRITEBLOCKDATA_DONE;
  394. }
  395. spitf->DataBuf.Pos++;
  396. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  397. }
  398. Result = ERROR_NONE;
  399. SDHC_SPIWRITEBLOCKDATA_DONE:
  400. spitf->TempData[0] = 0xfd;
  401. luat_spi_send(spitf->SpiID, (const char *)spitf->TempData, 1);
  402. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_WRITE_TO_MS;
  403. DoneFlag = 0;
  404. while( (luat_mcu_tick64_ms() < OpEndTick) && !DoneFlag )
  405. {
  406. TxLen = 512;
  407. memset(spitf->TempData, 0xff, TxLen);
  408. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, TxLen, (char *)spitf->TempData, TxLen);
  409. for(i = 4; i < TxLen; i++)
  410. {
  411. if (spitf->TempData[i] == 0xff)
  412. {
  413. DoneFlag = 1;
  414. break;
  415. }
  416. }
  417. }
  418. luat_spitf_cs(spitf, 0);
  419. return Result;
  420. }
  421. static int32_t luat_spitf_read_data(luat_spitf_ctrl_t *spitf)
  422. {
  423. uint64_t OpEndTick;
  424. int Result = -ERROR_OPERATION_FAILED;
  425. uint16_t ReadLen, DummyLen, RemainingLen;
  426. uint16_t i, crc16, crc16_check;
  427. uint8_t *pBuf;
  428. spitf->SPIError = 0;
  429. spitf->SDHCError = 0;
  430. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS;
  431. while( (spitf->DataBuf.Pos < spitf->DataBuf.MaxLen) && (luat_mcu_tick64_ms() < OpEndTick) )
  432. {
  433. DummyLen = (__SDHC_BLOCK_LEN__ >> 1);
  434. memset(spitf->TempData, 0xff, DummyLen);
  435. // LLOGD("read blocks %u,%u", spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  436. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, DummyLen, (char *)spitf->TempData, DummyLen);
  437. RemainingLen = 0;
  438. for(i = 0; i < DummyLen; i++)
  439. {
  440. if (spitf->TempData[i] == 0xfe)
  441. {
  442. ReadLen = (DummyLen - i - 1);
  443. RemainingLen = __SDHC_BLOCK_LEN__ - ReadLen;
  444. if (ReadLen)
  445. {
  446. memcpy(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, spitf->TempData + i + 1, ReadLen);
  447. }
  448. // LLOGD("read result %u,%u", ReadLen, RemainingLen);
  449. goto READ_REST_DATA;
  450. }
  451. }
  452. continue;
  453. READ_REST_DATA:
  454. pBuf = spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__ + ReadLen;
  455. memset(pBuf, 0xff, RemainingLen);
  456. luat_spi_transfer(spitf->SpiID, (const char *)pBuf, RemainingLen, (char *)pBuf, RemainingLen);
  457. memset(spitf->TempData, 0xff, 2);
  458. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, 2, (char *)spitf->TempData, 2);
  459. if (FATFS_NO_CRC_CHECK)
  460. {
  461. }
  462. else
  463. {
  464. crc16 = CRC16Cal(spitf->DataBuf.Data + spitf->DataBuf.Pos * __SDHC_BLOCK_LEN__, __SDHC_BLOCK_LEN__, 0, CRC16_CCITT_GEN, 0);
  465. crc16_check = BytesGetBe16(spitf->TempData);
  466. if (crc16 != crc16_check)
  467. {
  468. LLOGD("crc16 error %04x %04x", crc16, crc16_check);
  469. Result = ERROR_NONE;
  470. goto SDHC_SPIREADBLOCKDATA_DONE;
  471. }
  472. }
  473. spitf->DataBuf.Pos++;
  474. OpEndTick = luat_mcu_tick64_ms() + SPI_TF_READ_TO_MS;
  475. }
  476. Result = ERROR_NONE;
  477. SDHC_SPIREADBLOCKDATA_DONE:
  478. return Result;
  479. }
  480. static void luat_spitf_init(luat_spitf_ctrl_t *spitf)
  481. {
  482. uint64_t OpEndTick;
  483. if (!spitf->Info)
  484. {
  485. spitf->Info = luat_heap_malloc(sizeof(SD_CardInfo));
  486. }
  487. memset(spitf->Info, 0, sizeof(SD_CardInfo));
  488. if (!spitf->TempData)
  489. {
  490. spitf->TempData = luat_heap_malloc(__SDHC_BLOCK_LEN__ + 8);
  491. }
  492. luat_spi_change_speed(spitf->SpiID, 400000);
  493. spitf->IsInitDone = 0;
  494. spitf->SDHCState = 0xff;
  495. spitf->Info->CardCapacity = 0;
  496. spitf->WriteWaitCnt = 80;
  497. spitf->ResetCnt = 80;
  498. spitf->CmdCnt = 40;
  499. memset(spitf->TempData, 0xff, spitf->ResetCnt);
  500. luat_gpio_set(spitf->CSPin, 0);
  501. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, spitf->ResetCnt, (char *)spitf->TempData, spitf->ResetCnt);
  502. luat_gpio_set(spitf->CSPin, 1);
  503. luat_spi_transfer(spitf->SpiID, (const char *)spitf->TempData, spitf->ResetCnt, (char *)spitf->TempData, spitf->ResetCnt);
  504. spitf->SDSC = 0;
  505. if (luat_spitf_cmd(spitf, CMD0, 0, 1))
  506. {
  507. goto INIT_DONE;
  508. }
  509. OpEndTick = luat_mcu_tick64_ms() + 3000;
  510. if (luat_spitf_cmd(spitf, CMD8, 0x1aa, 1)) //只支持2G以上的SDHC卡
  511. {
  512. LLOGD("tf cmd8 not support");
  513. spitf->SDSC = 1;
  514. }
  515. WAIT_INIT_DONE:
  516. if (luat_mcu_tick64_ms() >= OpEndTick)
  517. {
  518. LLOGD("tf init timeout!");
  519. goto INIT_DONE;
  520. }
  521. if (luat_spitf_cmd(spitf, SD_CMD_APP_CMD, 0, 1))
  522. {
  523. goto INIT_DONE;
  524. }
  525. if (!spitf->SDSC)
  526. {
  527. if (luat_spitf_cmd(spitf, SD_CMD_SD_APP_OP_COND, 0x40000000, 1))
  528. {
  529. goto INIT_DONE;
  530. }
  531. }
  532. else
  533. {
  534. if (luat_spitf_cmd(spitf, SD_CMD_SD_APP_OP_COND, 0, 1))
  535. {
  536. goto INIT_DONE;
  537. }
  538. }
  539. spitf->IsInitDone = !spitf->SDHCState;
  540. if (!spitf->IsInitDone)
  541. {
  542. SPI_TF_WAIT(10);
  543. goto WAIT_INIT_DONE;
  544. }
  545. if (luat_spitf_cmd(spitf, CMD58, 0, 1))
  546. {
  547. goto INIT_DONE;
  548. }
  549. spitf->OCR = BytesGetBe32(spitf->ExternResult);
  550. luat_spi_change_speed(spitf->SpiID, spitf->SpiSpeed);
  551. luat_spitf_read_config(spitf);
  552. LLOGD("sdcard init OK OCR:0x%08x!", spitf->OCR);
  553. return;
  554. INIT_DONE:
  555. if (!spitf->IsInitDone)
  556. {
  557. LLOGD("sdcard init fail!");
  558. }
  559. return;
  560. }
  561. static void luat_spitf_read_config(luat_spitf_ctrl_t *spitf)
  562. {
  563. uint8_t CSD_Tab[18];
  564. SD_CSD* Csd = &spitf->Info->Csd;
  565. SD_CardInfo *pCardInfo = spitf->Info;
  566. uint64_t Temp;
  567. uint8_t flag_SDHC = (spitf->OCR & 0x40000000) >> 30;
  568. spitf->SDSC = !flag_SDHC;
  569. if (spitf->Info->CardCapacity) return;
  570. if (luat_spitf_cmd(spitf, CMD9, 0, 0))
  571. {
  572. goto READ_CONFIG_ERROR;
  573. }
  574. if (spitf->SDHCState)
  575. {
  576. goto READ_CONFIG_ERROR;
  577. }
  578. if (luat_spitf_read_reg(spitf, CSD_Tab, 18))
  579. {
  580. goto READ_CONFIG_ERROR;
  581. }
  582. /*************************************************************************
  583. CSD header decoding
  584. *************************************************************************/
  585. /* Byte 0 */
  586. Csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
  587. Csd->Reserved1 = CSD_Tab[0] & 0x3F;
  588. /* Byte 1 */
  589. Csd->TAAC = CSD_Tab[1];
  590. /* Byte 2 */
  591. Csd->NSAC = CSD_Tab[2];
  592. /* Byte 3 */
  593. Csd->MaxBusClkFrec = CSD_Tab[3];
  594. /* Byte 4/5 */
  595. Csd->CardComdClasses = (CSD_Tab[4] << 4) | ((CSD_Tab[5] & 0xF0) >> 4);
  596. Csd->RdBlockLen = CSD_Tab[5] & 0x0F;
  597. /* Byte 6 */
  598. Csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
  599. Csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
  600. Csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
  601. Csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
  602. /*************************************************************************
  603. CSD v1/v2 decoding
  604. *************************************************************************/
  605. if(!flag_SDHC)
  606. {
  607. Csd->version.v1.Reserved1 = ((CSD_Tab[6] & 0x0C) >> 2);
  608. Csd->version.v1.DeviceSize = ((CSD_Tab[6] & 0x03) << 10)
  609. | (CSD_Tab[7] << 2)
  610. | ((CSD_Tab[8] & 0xC0) >> 6);
  611. Csd->version.v1.MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
  612. Csd->version.v1.MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
  613. Csd->version.v1.MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
  614. Csd->version.v1.MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
  615. Csd->version.v1.DeviceSizeMul = ((CSD_Tab[9] & 0x03) << 1)
  616. |((CSD_Tab[10] & 0x80) >> 7);
  617. }
  618. else
  619. {
  620. Csd->version.v2.Reserved1 = ((CSD_Tab[6] & 0x0F) << 2) | ((CSD_Tab[7] & 0xC0) >> 6);
  621. Csd->version.v2.DeviceSize= ((CSD_Tab[7] & 0x3F) << 16) | (CSD_Tab[8] << 8) | CSD_Tab[9];
  622. Csd->version.v2.Reserved2 = ((CSD_Tab[10] & 0x80) >> 8);
  623. }
  624. Csd->EraseSingleBlockEnable = (CSD_Tab[10] & 0x40) >> 6;
  625. Csd->EraseSectorSize = ((CSD_Tab[10] & 0x3F) << 1)
  626. |((CSD_Tab[11] & 0x80) >> 7);
  627. Csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
  628. Csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
  629. Csd->Reserved2 = (CSD_Tab[12] & 0x60) >> 5;
  630. Csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
  631. Csd->MaxWrBlockLen = ((CSD_Tab[12] & 0x03) << 2)
  632. |((CSD_Tab[13] & 0xC0) >> 6);
  633. Csd->WriteBlockPartial = (CSD_Tab[13] & 0x20) >> 5;
  634. Csd->Reserved3 = (CSD_Tab[13] & 0x1F);
  635. Csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
  636. Csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
  637. Csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
  638. Csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
  639. Csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
  640. Csd->Reserved4 = (CSD_Tab[14] & 0x03);
  641. Csd->crc = (CSD_Tab[15] & 0xFE) >> 1;
  642. Csd->Reserved5 = (CSD_Tab[15] & 0x01);
  643. #if 0
  644. if (luat_spitf_cmd(spitf, CMD10, 0, 0))
  645. {
  646. goto READ_CONFIG_ERROR;
  647. }
  648. if (spitf->SDHCState)
  649. {
  650. goto READ_CONFIG_ERROR;
  651. }
  652. if (luat_spitf_read_reg(Ctrl, CID_Tab, 18))
  653. {
  654. goto READ_CONFIG_ERROR;
  655. }
  656. /* Byte 0 */
  657. Cid->ManufacturerID = CID_Tab[0];
  658. /* Byte 1 */
  659. Cid->OEM_AppliID = CID_Tab[1] << 8;
  660. /* Byte 2 */
  661. Cid->OEM_AppliID |= CID_Tab[2];
  662. /* Byte 3 */
  663. Cid->ProdName1 = CID_Tab[3] << 24;
  664. /* Byte 4 */
  665. Cid->ProdName1 |= CID_Tab[4] << 16;
  666. /* Byte 5 */
  667. Cid->ProdName1 |= CID_Tab[5] << 8;
  668. /* Byte 6 */
  669. Cid->ProdName1 |= CID_Tab[6];
  670. /* Byte 7 */
  671. Cid->ProdName2 = CID_Tab[7];
  672. /* Byte 8 */
  673. Cid->ProdRev = CID_Tab[8];
  674. /* Byte 9 */
  675. Cid->ProdSN = CID_Tab[9] << 24;
  676. /* Byte 10 */
  677. Cid->ProdSN |= CID_Tab[10] << 16;
  678. /* Byte 11 */
  679. Cid->ProdSN |= CID_Tab[11] << 8;
  680. /* Byte 12 */
  681. Cid->ProdSN |= CID_Tab[12];
  682. /* Byte 13 */
  683. Cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
  684. Cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
  685. /* Byte 14 */
  686. Cid->ManufactDate |= CID_Tab[14];
  687. /* Byte 15 */
  688. Cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
  689. Cid->Reserved2 = 1;
  690. #endif
  691. if(flag_SDHC)
  692. {
  693. pCardInfo->LogBlockSize = 512;
  694. pCardInfo->CardBlockSize = 512;
  695. Temp = 1024 * pCardInfo->LogBlockSize;
  696. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v2.DeviceSize + 1) * Temp;
  697. pCardInfo->LogBlockNbr = (pCardInfo->Csd.version.v2.DeviceSize + 1) * 1024;
  698. }
  699. else
  700. {
  701. pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1) ;
  702. pCardInfo->CardCapacity *= (1 << (pCardInfo->Csd.version.v1.DeviceSizeMul + 2));
  703. pCardInfo->LogBlockSize = 512;
  704. pCardInfo->CardBlockSize = 1 << (pCardInfo->Csd.RdBlockLen);
  705. pCardInfo->CardCapacity *= pCardInfo->CardBlockSize;
  706. pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
  707. }
  708. LLOGD("卡容量 %lluKB", pCardInfo->CardCapacity/1024);
  709. return;
  710. READ_CONFIG_ERROR:
  711. spitf->IsInitDone = 0;
  712. spitf->SDHCError = 1;
  713. return;
  714. }
  715. static void luat_spitf_read_blocks(luat_spitf_ctrl_t *spitf, uint8_t *Buf, uint32_t StartLBA, uint32_t BlockNums)
  716. {
  717. uint8_t Retry = 0;
  718. uint8_t error = 1;
  719. uint32_t address;
  720. Buffer_StaticInit(&spitf->DataBuf, Buf, BlockNums);
  721. if (spitf->SDSC)
  722. {
  723. if (luat_spitf_cmd(spitf, CMD16, 512, 1))
  724. {
  725. goto SDHC_SPIREADBLOCKS_ERROR;
  726. }
  727. }
  728. SDHC_SPIREADBLOCKS_START:
  729. if (spitf->SDSC)
  730. {
  731. address = (StartLBA + spitf->DataBuf.Pos) * 512;
  732. }
  733. else
  734. {
  735. address = (StartLBA + spitf->DataBuf.Pos);
  736. }
  737. if (luat_spitf_cmd(spitf, CMD18, address, 0))
  738. {
  739. goto SDHC_SPIREADBLOCKS_CHECK;
  740. }
  741. if (luat_spitf_read_data(spitf))
  742. {
  743. luat_spitf_cmd(spitf, CMD12, 0, 1);
  744. goto SDHC_SPIREADBLOCKS_CHECK;
  745. }
  746. for (int i = 0; i < 3; i++)
  747. {
  748. if (!luat_spitf_cmd(spitf, CMD12, 0, 1))
  749. {
  750. error = 0;
  751. break;
  752. }
  753. else
  754. {
  755. spitf->SDHCError = 0;
  756. spitf->IsInitDone = 1;
  757. spitf->SDHCState = 0;
  758. }
  759. }
  760. SDHC_SPIREADBLOCKS_CHECK:
  761. if (error)
  762. {
  763. LLOGD("read error %x,%u,%u",spitf->SDHCState, spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  764. }
  765. if (spitf->DataBuf.Pos != spitf->DataBuf.MaxLen)
  766. {
  767. Retry++;
  768. LLOGD("read retry %d,%u,%u,%u", Retry, StartLBA, spitf->DataBuf.Pos, spitf->DataBuf.MaxLen);
  769. if (Retry > 3)
  770. {
  771. spitf->SDHCError = 1;
  772. goto SDHC_SPIREADBLOCKS_ERROR;
  773. }
  774. else
  775. {
  776. spitf->SDHCError = 0;
  777. spitf->IsInitDone = 1;
  778. spitf->SDHCState = 0;
  779. }
  780. goto SDHC_SPIREADBLOCKS_START;
  781. }
  782. return;
  783. SDHC_SPIREADBLOCKS_ERROR:
  784. LLOGD("read error!");
  785. spitf->IsInitDone = 0;
  786. spitf->SDHCError = 1;
  787. return;
  788. }
  789. static void luat_spitf_write_blocks(luat_spitf_ctrl_t *spitf, const uint8_t *Buf, uint32_t StartLBA, uint32_t BlockNums)
  790. {
  791. uint8_t Retry = 0;
  792. uint32_t address;
  793. Buffer_StaticInit(&spitf->DataBuf, (void *)Buf, BlockNums);
  794. if (spitf->SDSC)
  795. {
  796. if (luat_spitf_cmd(spitf, CMD16, 512, 1))
  797. {
  798. goto SDHC_SPIWRITEBLOCKS_ERROR;
  799. }
  800. }
  801. SDHC_SPIWRITEBLOCKS_START:
  802. if (spitf->SDSC)
  803. {
  804. address = (StartLBA + spitf->DataBuf.Pos) * 512;
  805. }
  806. else
  807. {
  808. address = (StartLBA + spitf->DataBuf.Pos);
  809. }
  810. if (luat_spitf_cmd(spitf, CMD25, address, 0))
  811. {
  812. goto SDHC_SPIWRITEBLOCKS_ERROR;
  813. }
  814. if (luat_spitf_write_data(spitf))
  815. {
  816. goto SDHC_SPIWRITEBLOCKS_ERROR;
  817. }
  818. if (spitf->DataBuf.Pos != spitf->DataBuf.MaxLen)
  819. {
  820. Retry++;
  821. LLOGD("write retry %d", Retry);
  822. if (Retry > 3)
  823. {
  824. spitf->SDHCError = 1;
  825. goto SDHC_SPIWRITEBLOCKS_ERROR;
  826. }
  827. goto SDHC_SPIWRITEBLOCKS_START;
  828. }
  829. return;
  830. SDHC_SPIWRITEBLOCKS_ERROR:
  831. luat_spitf_cs(spitf, 0);
  832. LLOGD("write error!");
  833. spitf->IsInitDone = 0;
  834. spitf->SDHCError = 1;
  835. return;
  836. }
  837. static uint8_t luat_spitf_is_ready(luat_spitf_ctrl_t *spitf)
  838. {
  839. if (!spitf->SDHCState && spitf->IsInitDone)
  840. {
  841. return 1;
  842. }
  843. else
  844. {
  845. LLOGD("SDHC error, please reboot tf card");
  846. return 0;
  847. }
  848. }
  849. static DSTATUS luat_spitf_initialize(void* userdata)
  850. {
  851. luat_mutex_lock(g_s_spitf.locker);
  852. luat_spi_lock(g_s_spitf.SpiID);
  853. luat_spitf_init(&g_s_spitf);
  854. luat_spi_unlock(g_s_spitf.SpiID);
  855. luat_mutex_unlock(g_s_spitf.locker);
  856. return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
  857. }
  858. static DSTATUS luat_spitf_status(void* userdata)
  859. {
  860. return luat_spitf_is_ready(&g_s_spitf)?0:STA_NOINIT;
  861. }
  862. static DRESULT luat_spitf_read(void* userdata, uint8_t* buff, LBA_t sector, UINT count)
  863. {
  864. luat_mutex_lock(g_s_spitf.locker);
  865. if (!luat_spitf_is_ready(&g_s_spitf))
  866. {
  867. luat_mutex_unlock(g_s_spitf.locker);
  868. return RES_NOTRDY;
  869. }
  870. luat_spi_lock(g_s_spitf.SpiID);
  871. luat_spitf_read_blocks(&g_s_spitf, buff, sector, count);
  872. luat_spi_unlock(g_s_spitf.SpiID);
  873. luat_mutex_unlock(g_s_spitf.locker);
  874. return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
  875. }
  876. static DRESULT luat_spitf_write(void* userdata, const uint8_t* buff, LBA_t sector, UINT count)
  877. {
  878. luat_mutex_lock(g_s_spitf.locker);
  879. if (!luat_spitf_is_ready(&g_s_spitf))
  880. {
  881. luat_mutex_unlock(g_s_spitf.locker);
  882. return RES_NOTRDY;
  883. }
  884. luat_spi_lock(g_s_spitf.SpiID);
  885. luat_spitf_write_blocks(&g_s_spitf, buff, sector, count);
  886. luat_spi_unlock(g_s_spitf.SpiID);
  887. luat_mutex_unlock(g_s_spitf.locker);
  888. return luat_spitf_is_ready(&g_s_spitf)?RES_OK:RES_ERROR;
  889. }
  890. static DRESULT luat_spitf_ioctl(void* userdata, uint8_t ctrl, void* buff)
  891. {
  892. luat_mutex_lock(g_s_spitf.locker);
  893. if (!luat_spitf_is_ready(&g_s_spitf))
  894. {
  895. luat_mutex_unlock(g_s_spitf.locker);
  896. return RES_NOTRDY;
  897. }
  898. luat_spi_lock(g_s_spitf.SpiID);
  899. luat_spitf_read_config(&g_s_spitf);
  900. luat_spi_unlock(g_s_spitf.SpiID);
  901. luat_mutex_unlock(g_s_spitf.locker);
  902. switch (ctrl) {
  903. case CTRL_SYNC : /* Make sure that no pending write process */
  904. return RES_OK;
  905. break;
  906. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  907. *(uint32_t*)buff = g_s_spitf.Info->LogBlockNbr;
  908. return RES_OK;
  909. break;
  910. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  911. *(uint32_t*)buff = 128;
  912. return RES_OK;
  913. break;
  914. default:
  915. return RES_PARERR;
  916. }
  917. return RES_PARERR;
  918. }
  919. const block_disk_opts_t spitf_disk_opts = {
  920. .initialize = luat_spitf_initialize,
  921. .status = luat_spitf_status,
  922. .read = luat_spitf_read,
  923. .write = luat_spitf_write,
  924. .ioctl = luat_spitf_ioctl,
  925. };
  926. void luat_spi_set_sdhc_ctrl_default(
  927. block_disk_t *disk
  928. )
  929. {
  930. luat_fatfs_spi_t* userdata = disk->userdata;
  931. if (userdata->type)
  932. {
  933. g_s_spitf.CSPin = userdata->spi_device->spi_config.cs;
  934. g_s_spitf.SpiID = userdata->spi_device->bus_id;
  935. g_s_spitf.SpiSpeed = userdata->fast_speed;
  936. }
  937. else
  938. {
  939. g_s_spitf.CSPin = userdata->spi_cs;
  940. g_s_spitf.SpiID = userdata->spi_id;
  941. g_s_spitf.SpiSpeed = userdata->fast_speed;
  942. }
  943. if (!g_s_spitf.locker)
  944. {
  945. g_s_spitf.locker = luat_mutex_create();
  946. }
  947. luat_heap_free(disk->userdata);
  948. disk->userdata = NULL;
  949. disk->opts = &spitf_disk_opts;
  950. }
  951. #ifndef LUAT_COMPILER_NOWEAK
  952. __attribute__((weak)) void luat_spi_set_sdhc_ctrl(
  953. block_disk_t *disk)
  954. {
  955. luat_spi_set_sdhc_ctrl_default(disk);
  956. }
  957. #else
  958. void luat_spi_set_sdhc_ctrl(block_disk_t *disk);
  959. #endif
  960. static block_disk_t disk = {0};
  961. DRESULT diskio_open_spitf(BYTE pdrv, luat_fatfs_spi_t* userdata) {
  962. // 暂时只支持单个fatfs实例
  963. disk.opts = &spitf_disk_opts;
  964. disk.userdata = userdata;
  965. luat_spi_set_sdhc_ctrl(&disk);
  966. return diskio_open(pdrv, &disk);
  967. }
  968. //static DWORD get_fattime() {
  969. // how to get?
  970. //}
  971. //--------------------------------------------------------------------------------------