diskio_spitf.c 32 KB

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