luat_spi_air105.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_spi.h"
  23. #include "luat_lcd.h"
  24. #include "ff.h" /* Obtains integer types */
  25. #include "diskio.h" /* Declarations of disk functions */
  26. #include "app_interface.h"
  27. #define LUAT_LOG_TAG "luat.spi"
  28. #include "luat_log.h"
  29. typedef struct
  30. {
  31. uint8_t id;
  32. uint8_t mark;
  33. uint8_t mode;//spi模式
  34. }Spi_Struct;
  35. static Spi_Struct luat_spi[6] ={0};
  36. int32_t luat_spi_cb(void *pData, void *pParam){
  37. // LLOGD("luat_spi_cb pData:%d pParam:%d ",(int)pData,(int)pParam);
  38. switch ((int)pData){
  39. case 0:
  40. luat_spi[5].mark = 0;
  41. break;
  42. case 1:
  43. luat_spi[0].mark = 0;
  44. break;
  45. case 2:
  46. luat_spi[1].mark = 0;
  47. break;
  48. case 3:
  49. luat_spi[2].mark = 0;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. int luat_spi_device_config(luat_spi_device_t* spi_dev) {
  56. uint8_t spi_mode = SPI_MODE_0;
  57. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  58. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  59. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  60. SPI_SetNewConfig(luat_spi[spi_dev->bus_id].id, spi_dev->spi_config.bandrate, spi_mode);
  61. return 0;
  62. }
  63. int luat_spi_bus_setup(luat_spi_device_t* spi_dev){
  64. int bus_id = spi_dev->bus_id;
  65. if (bus_id == 0) {
  66. luat_spi[bus_id].id=SPI_ID0;
  67. GPIO_Iomux(GPIOB_12, 0);
  68. GPIO_Iomux(GPIOB_14, 0);
  69. GPIO_Iomux(GPIOB_15, 0);
  70. }
  71. else if (bus_id == 1) {
  72. luat_spi[bus_id].id=SPI_ID1;
  73. GPIO_Iomux(GPIOA_06,3);
  74. GPIO_Iomux(GPIOA_08,3);
  75. GPIO_Iomux(GPIOA_09,3);
  76. }
  77. else if (bus_id == 2) {
  78. luat_spi[bus_id].id=SPI_ID2;
  79. GPIO_Iomux(GPIOB_02,0);
  80. GPIO_Iomux(GPIOB_04,0);
  81. GPIO_Iomux(GPIOB_05,0);
  82. }
  83. else if (bus_id == 5) {
  84. luat_spi[bus_id].id=HSPI_ID0;
  85. GPIO_Iomux(GPIOC_12,3);
  86. GPIO_Iomux(GPIOC_13,3);
  87. GPIO_Iomux(GPIOC_15,3);
  88. }
  89. else {
  90. return -1;
  91. }
  92. uint8_t spi_mode = SPI_MODE_0;
  93. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  94. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  95. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  96. luat_spi[bus_id].mode=spi_dev->spi_config.mode;
  97. // LLOGD("SPI_MasterInit luat_bus_%d:%d dataw:%d spi_mode:%d bandrate:%d ",bus_id,luat_spi[bus_id].id, spi_dev->spi_config.dataw, spi_mode, spi_dev->spi_config.bandrate);
  98. SPI_MasterInit(luat_spi[bus_id].id, spi_dev->spi_config.dataw, spi_mode, spi_dev->spi_config.bandrate, luat_spi_cb, NULL);
  99. return 0;
  100. }
  101. //初始化配置SPI各项参数,并打开SPI
  102. //成功返回0
  103. int luat_spi_setup(luat_spi_t* spi) {
  104. uint8_t spi_id = spi->id;
  105. if (spi_id == 0) {
  106. luat_spi[spi_id].id=SPI_ID0;
  107. if (spi_id == 0 || spi->cs == GPIOB_13)
  108. GPIO_Iomux(GPIOB_13, 0);
  109. GPIO_Iomux(GPIOB_12, 0);
  110. GPIO_Iomux(GPIOB_14, 0);
  111. GPIO_Iomux(GPIOB_15, 0);
  112. }
  113. else if (spi_id == 1) {
  114. luat_spi[spi_id].id=SPI_ID1;
  115. if (spi_id == 1 || spi->cs == GPIOA_07)
  116. GPIO_Iomux(GPIOA_07,3);
  117. GPIO_Iomux(GPIOA_06,3);
  118. GPIO_Iomux(GPIOA_08,3);
  119. GPIO_Iomux(GPIOA_09,3);
  120. }
  121. else if (spi_id == 2) {
  122. luat_spi[spi_id].id=SPI_ID2;
  123. if (spi_id == 2 || spi->cs == GPIOB_03)
  124. GPIO_Iomux(GPIOB_03,0);
  125. GPIO_Iomux(GPIOB_02,0);
  126. GPIO_Iomux(GPIOB_04,0);
  127. GPIO_Iomux(GPIOB_05,0);
  128. }
  129. else if (spi_id == 5) {
  130. luat_spi[spi_id].id=HSPI_ID0;
  131. if (spi_id == 5 || spi->cs == GPIOC_14)
  132. GPIO_Iomux(GPIOC_14,3);
  133. GPIO_Iomux(GPIOC_12,3);
  134. GPIO_Iomux(GPIOC_13,3);
  135. GPIO_Iomux(GPIOC_15,3);
  136. }
  137. else {
  138. return -1;
  139. }
  140. uint8_t spi_mode = SPI_MODE_0;
  141. if(spi->CPHA&&spi->CPOL)spi_mode = SPI_MODE_3;
  142. else if(spi->CPOL)spi_mode = SPI_MODE_2;
  143. else if(spi->CPHA)spi_mode = SPI_MODE_1;
  144. luat_spi[spi_id].mode=spi->mode;
  145. // LLOGD("SPI_MasterInit luat_spi%d:%d dataw:%d spi_mode:%d bandrate:%d ",spi_id,luat_spi[spi_id].id, spi->dataw, spi_mode, spi->bandrate);
  146. SPI_MasterInit(luat_spi[spi_id].id, spi->dataw, spi_mode, spi->bandrate, luat_spi_cb, NULL);
  147. return 0;
  148. }
  149. int luat_spi_config_dma(int spi_id, uint32_t tx_channel, uint32_t rx_channel)
  150. {
  151. // if (luat_spi[spi_id].id != HSPI_ID0) {
  152. // SPI_DMATxInit(luat_spi[spi_id].id, (tx_channel >= 8)?ETH_SPI_TX_DMA_STREAM:tx_channel, 0);
  153. // SPI_DMARxInit(luat_spi[spi_id].id, (rx_channel >= 8)?ETH_SPI_RX_DMA_STREAM:rx_channel, 0);
  154. // }
  155. // else
  156. // {
  157. // SPI_DMATxInit(luat_spi[spi_id].id, (tx_channel >= 8)?LCD_SPI_TX_DMA_STREAM:tx_channel, 0);
  158. // SPI_DMARxInit(luat_spi[spi_id].id, (rx_channel >= 8)?LCD_SPI_RX_DMA_STREAM:rx_channel, 0);
  159. // }
  160. }
  161. //关闭SPI,成功返回0
  162. int luat_spi_close(int spi_id) {
  163. return 0;
  164. }
  165. //收发SPI数据,返回接收字节数
  166. int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
  167. // LLOGD("SPI_MasterInit luat_spi%d:%d send_buf:%x recv_buf:%x length:%d ",spi_id,luat_spi[spi_id], *send_buf, *recv_buf, length);
  168. // while(luat_spi[spi_id].mark)
  169. luat_spi[spi_id].mark = 1;
  170. if(luat_spi[spi_id].mode==0)
  171. SPI_FlashBlockTransfer(luat_spi[spi_id].id, send_buf, send_length, recv_buf, recv_length);
  172. else
  173. SPI_BlockTransfer(luat_spi[spi_id].id, send_buf, recv_buf, recv_length);
  174. return recv_length;
  175. }
  176. //收SPI数据,返回接收字节数
  177. int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
  178. // LLOGD("SPI_MasterInit luat_spi%d:%d recv_buf:%x length:%d ",spi_id,luat_spi[spi_id], *recv_buf, length);
  179. // while(luat_spi[spi_id].mark)
  180. luat_spi[spi_id].mark = 1;
  181. SPI_BlockTransfer(luat_spi[spi_id].id, recv_buf, recv_buf, length);
  182. return length;
  183. }
  184. //发SPI数据,返回发送字节数
  185. int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
  186. // LLOGD("luat_spi_send luat_spi%d:%d send_buf:%x length:%d ",spi_id,luat_spi[spi_id], *send_buf, length);
  187. // while(luat_spi[spi_id].mark)
  188. luat_spi[spi_id].mark = 1;
  189. SPI_BlockTransfer(luat_spi[spi_id].id, send_buf, NULL, length);
  190. return length;
  191. }
  192. int luat_lcd_draw_no_block(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color, uint8_t last_flush)
  193. {
  194. uint32_t retry_cnt = 0;
  195. uint32_t cache_len = Core_LCDDrawCacheLen();
  196. // if (last_flush)
  197. // {
  198. // LLOGD("lcd flush done!");
  199. // }
  200. if (conf->port == LUAT_LCD_SPI_DEVICE){
  201. while (Core_LCDDrawCacheLen() > (conf->buffer_size))
  202. {
  203. retry_cnt++;
  204. luat_timer_mdelay(1);
  205. }
  206. if (retry_cnt)
  207. {
  208. LLOGD("lcd flush delay %ums, status %u,%u,%u,%d", retry_cnt, cache_len, x2-x1+1, y2-y1+1, last_flush);
  209. }
  210. LCD_DrawStruct *draw = OS_Zalloc(sizeof(LCD_DrawStruct));
  211. if (!draw)
  212. {
  213. LLOGE("lcd flush no memory");
  214. return -1;
  215. }
  216. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  217. int spi_id = spi_dev->bus_id;
  218. uint8_t spi_mode = SPI_MODE_0;
  219. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  220. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  221. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  222. draw->DCDelay = conf->dc_delay_us;
  223. draw->Mode = spi_mode;
  224. draw->Speed = spi_dev->spi_config.bandrate;
  225. draw->SpiID = luat_spi[spi_id].id;
  226. draw->CSPin = spi_dev->spi_config.cs;
  227. draw->DCPin = conf->pin_dc;
  228. draw->x1 = x1;
  229. draw->x2 = x2;
  230. draw->y1 = y1;
  231. draw->y2 = y2;
  232. draw->xoffset = conf->xoffset;
  233. draw->yoffset = conf->yoffset;
  234. draw->Size = (draw->x2 - draw->x1 + 1) * (draw->y2 - draw->y1 + 1) * 2;
  235. draw->ColorMode = COLOR_MODE_RGB_565;
  236. draw->Data = OS_Malloc(draw->Size);
  237. if (!draw->Data)
  238. {
  239. LLOGE("lcd flush data no memory");
  240. OS_Free(draw);
  241. return -1;
  242. }
  243. memcpy(draw->Data, color, draw->Size);
  244. Core_LCDDraw(draw);
  245. return 0;
  246. }
  247. else
  248. {
  249. return -1;
  250. }
  251. }
  252. int luat_spi_get_hw_bus(int spi_id)
  253. {
  254. return luat_spi[spi_id].id;
  255. }
  256. void luat_lcd_draw_block(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color, uint8_t last_flush)
  257. {
  258. LCD_DrawStruct draw;
  259. if (conf->port == LUAT_LCD_SPI_DEVICE){
  260. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  261. int spi_id = spi_dev->bus_id;
  262. uint8_t spi_mode = SPI_MODE_0;
  263. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  264. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  265. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  266. draw.DCDelay = conf->dc_delay_us;
  267. draw.Mode = spi_mode;
  268. draw.Speed = spi_dev->spi_config.bandrate;
  269. draw.SpiID = luat_spi[spi_id].id;
  270. draw.CSPin = spi_dev->spi_config.cs;
  271. draw.DCPin = conf->pin_dc;
  272. draw.x1 = x1;
  273. draw.x2 = x2;
  274. draw.y1 = y1;
  275. draw.y2 = y2;
  276. draw.xoffset = conf->xoffset;
  277. draw.yoffset = conf->yoffset;
  278. draw.Size = (draw.x2 - draw.x1 + 1) * (draw.y2 - draw.y1 + 1) * 2;
  279. draw.ColorMode = COLOR_MODE_RGB_565;
  280. draw.Data = color;
  281. Core_LCDDrawBlock(&draw);
  282. }
  283. }
  284. #ifdef LUAT_USE_LCD_CUSTOM_DRAW
  285. int luat_lcd_flush(luat_lcd_conf_t* conf) {
  286. if (conf->buff == NULL) {
  287. return 0;
  288. }
  289. //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
  290. if (conf->flush_y_max < conf->flush_y_min) {
  291. // 没有需要刷新的内容,直接跳过
  292. //LLOGD("luat_lcd_flush no need");
  293. return 0;
  294. }
  295. if (conf->is_init_done) {
  296. luat_lcd_draw_no_block(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max, &conf->buff[conf->flush_y_min * conf->w], 0);
  297. }
  298. else {
  299. uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
  300. luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
  301. const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
  302. if (conf->port == LUAT_LCD_SPI_DEVICE){
  303. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
  304. }else{
  305. luat_spi_send(conf->port, tmp, size);
  306. }
  307. }
  308. // 重置为不需要刷新的状态
  309. conf->flush_y_max = 0;
  310. conf->flush_y_min = conf->h;
  311. return 0;
  312. }
  313. int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color) {
  314. // 直接刷屏模式
  315. if (conf->buff == NULL) {
  316. if (conf->is_init_done)
  317. {
  318. return luat_lcd_draw_no_block(conf, x1, y1, x2, y2, color, 0);
  319. }
  320. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
  321. luat_lcd_set_address(conf, x1, y1, x2, y2);
  322. if (conf->port == LUAT_LCD_SPI_DEVICE){
  323. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size);
  324. }else{
  325. luat_spi_send(conf->port, (const char*)color, size);
  326. }
  327. return 0;
  328. }
  329. // buff模式
  330. if (x1 > conf->w || x2 > conf->w || y1 > conf->h || y2 > conf->h) {
  331. //LLOGW("out of lcd range");
  332. return -1;
  333. }
  334. luat_color_t* dst = (conf->buff + x1 + conf->w * y1);
  335. luat_color_t* src = (color);
  336. size_t lsize = (x2 - x1 + 1);
  337. for (size_t i = 0; i < (y2 - y1 + 1); i++) {
  338. memcpy(dst, src, lsize * sizeof(luat_color_t));
  339. dst += conf->w; // 移动到下一行
  340. src += lsize; // 移动数据
  341. }
  342. // 存储需要刷新的区域
  343. if (y1 < conf->flush_y_min)
  344. conf->flush_y_min = y1;
  345. if (y2 > conf->flush_y_max)
  346. conf->flush_y_max = y2;
  347. return 0;
  348. }
  349. #endif
  350. static void *luat_fatfs_spi_ctrl;
  351. static HANDLE luat_fatfs_locker;
  352. static void sdhc_spi_check(luat_fatfs_spi_t* userdata)
  353. {
  354. if (!luat_fatfs_spi_ctrl)
  355. {
  356. if(userdata->type == 1){
  357. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_device->bus_id].id, userdata->spi_device->spi_config.cs);
  358. luat_spi_device_config(userdata->spi_device);
  359. }else{
  360. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_id].id, userdata->spi_cs);
  361. }
  362. }
  363. }
  364. static DSTATUS sdhc_spi_initialize(luat_fatfs_spi_t* userdata)
  365. {
  366. int i;
  367. if (!luat_fatfs_locker)
  368. {
  369. luat_fatfs_locker = OS_MutexCreateUnlock();
  370. }
  371. OS_MutexLock(luat_fatfs_locker);
  372. if (luat_fatfs_spi_ctrl)
  373. {
  374. for(i = 0; i < USB_MAX; i++)
  375. {
  376. Core_UDiskDetachSDHC(i, luat_fatfs_spi_ctrl);
  377. }
  378. SDHC_SpiRelease(luat_fatfs_spi_ctrl);
  379. free(luat_fatfs_spi_ctrl);
  380. luat_fatfs_spi_ctrl = NULL;
  381. }
  382. sdhc_spi_check(userdata);
  383. SDHC_SpiInitCard(luat_fatfs_spi_ctrl);
  384. if(userdata->type == 1){
  385. userdata->spi_device->spi_config.bandrate = userdata->fast_speed;
  386. luat_spi_device_config(userdata->spi_device);
  387. }else{
  388. SPI_SetNewConfig(luat_spi[userdata->spi_id].id, userdata->fast_speed, 0);
  389. }
  390. if (SDHC_IsReady(luat_fatfs_spi_ctrl))
  391. {
  392. for(i = 0; i < USB_MAX; i++)
  393. {
  394. Core_UDiskAttachSDHCRecovery(i, luat_fatfs_spi_ctrl);
  395. }
  396. }
  397. OS_MutexRelease(luat_fatfs_locker);
  398. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  399. }
  400. static DSTATUS sdhc_spi_status(luat_fatfs_spi_t* userdata)
  401. {
  402. OS_MutexLock(luat_fatfs_locker);
  403. sdhc_spi_check(userdata);
  404. OS_MutexRelease(luat_fatfs_locker);
  405. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  406. }
  407. static DRESULT sdhc_spi_read(luat_fatfs_spi_t* userdata, uint8_t* buff, uint32_t sector, uint32_t count)
  408. {
  409. OS_MutexLock(luat_fatfs_locker);
  410. sdhc_spi_check(userdata);
  411. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  412. {
  413. OS_MutexRelease(luat_fatfs_locker);
  414. return RES_NOTRDY;
  415. }
  416. SDHC_SpiReadBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  417. OS_MutexRelease(luat_fatfs_locker);
  418. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  419. }
  420. static DRESULT sdhc_spi_write(luat_fatfs_spi_t* userdata, const uint8_t* buff, uint32_t sector, uint32_t count)
  421. {
  422. OS_MutexLock(luat_fatfs_locker);
  423. sdhc_spi_check(userdata);
  424. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  425. {
  426. OS_MutexRelease(luat_fatfs_locker);
  427. return RES_NOTRDY;
  428. }
  429. SDHC_SpiWriteBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  430. OS_MutexRelease(luat_fatfs_locker);
  431. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  432. }
  433. static DRESULT sdhc_spi_ioctl(luat_fatfs_spi_t* userdata, uint8_t ctrl, void* buff)
  434. {
  435. OS_MutexLock(luat_fatfs_locker);
  436. sdhc_spi_check(userdata);
  437. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  438. {
  439. OS_MutexRelease(luat_fatfs_locker);
  440. return RES_NOTRDY;
  441. }
  442. SDHC_SpiReadCardConfig(luat_fatfs_spi_ctrl);
  443. OS_MutexRelease(luat_fatfs_locker);
  444. switch (ctrl) {
  445. case CTRL_SYNC : /* Make sure that no pending write process */
  446. return RES_OK;
  447. break;
  448. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  449. *(uint32_t*)buff = SDHC_GetLogBlockNbr(luat_fatfs_spi_ctrl);
  450. return RES_OK;
  451. break;
  452. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  453. *(uint32_t*)buff = 128;
  454. return RES_OK;
  455. break;
  456. default:
  457. return RES_PARERR;
  458. }
  459. return RES_PARERR;
  460. }
  461. static const block_disk_opts_t sdhc_spi_disk_opts = {
  462. .initialize = sdhc_spi_initialize,
  463. .status = sdhc_spi_status,
  464. .read = sdhc_spi_read,
  465. .write = sdhc_spi_write,
  466. .ioctl = sdhc_spi_ioctl,
  467. };
  468. void luat_spi_set_sdhc_ctrl(block_disk_t *disk)
  469. {
  470. disk->opts = &sdhc_spi_disk_opts;
  471. }
  472. void *luat_spi_get_sdhc_ctrl(void)
  473. {
  474. return luat_fatfs_spi_ctrl;
  475. }