diskio_spitf.c 29 KB

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