luat_spi_air105.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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_spi_no_block_transfer(int spi_id, uint8_t *tx_buff, uint8_t *rx_buff, size_t len, void *CB, void *pParam)
  193. {
  194. if (SPI_IsTransferBusy(luat_spi[spi_id].id)) return -1;
  195. SPI_SetCallbackFun(luat_spi[spi_id].id, CB, pParam);
  196. SPI_SetNoBlock(luat_spi[spi_id].id);
  197. return SPI_Transfer(luat_spi[spi_id].id, tx_buff, rx_buff, len, 0);
  198. }
  199. 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)
  200. {
  201. uint32_t retry_cnt = 0;
  202. uint32_t cache_len = Core_LCDDrawCacheLen();
  203. // if (last_flush)
  204. // {
  205. // LLOGD("lcd flush done!");
  206. // }
  207. if (conf->port == LUAT_LCD_SPI_DEVICE){
  208. while (Core_LCDDrawCacheLen() > (conf->buffer_size))
  209. {
  210. retry_cnt++;
  211. luat_timer_mdelay(1);
  212. }
  213. if (retry_cnt)
  214. {
  215. LLOGD("lcd flush delay %ums, status %u,%u,%u,%d", retry_cnt, cache_len, x2-x1+1, y2-y1+1, last_flush);
  216. }
  217. LCD_DrawStruct *draw = zalloc(sizeof(LCD_DrawStruct));
  218. if (!draw)
  219. {
  220. LLOGE("lcd flush no memory");
  221. return -1;
  222. }
  223. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  224. int spi_id = spi_dev->bus_id;
  225. uint8_t spi_mode = SPI_MODE_0;
  226. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  227. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  228. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  229. draw->DCDelay = conf->dc_delay_us;
  230. draw->Mode = spi_mode;
  231. draw->Speed = spi_dev->spi_config.bandrate;
  232. draw->SpiID = luat_spi[spi_id].id;
  233. draw->CSPin = spi_dev->spi_config.cs;
  234. draw->DCPin = conf->pin_dc;
  235. draw->x1 = x1;
  236. draw->x2 = x2;
  237. draw->y1 = y1;
  238. draw->y2 = y2;
  239. draw->xoffset = conf->xoffset;
  240. draw->yoffset = conf->yoffset;
  241. draw->Size = (draw->x2 - draw->x1 + 1) * (draw->y2 - draw->y1 + 1) * 2;
  242. draw->ColorMode = COLOR_MODE_RGB_565;
  243. draw->Data = malloc(draw->Size);
  244. if (!draw->Data)
  245. {
  246. LLOGE("lcd flush data no memory");
  247. free(draw);
  248. return -1;
  249. }
  250. memcpy(draw->Data, color, draw->Size);
  251. Core_LCDDraw(draw);
  252. return 0;
  253. }
  254. else
  255. {
  256. return -1;
  257. }
  258. }
  259. int luat_spi_get_hw_bus(int spi_id)
  260. {
  261. return luat_spi[spi_id].id;
  262. }
  263. 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)
  264. {
  265. LCD_DrawStruct draw;
  266. if (conf->port == LUAT_LCD_SPI_DEVICE){
  267. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  268. int spi_id = spi_dev->bus_id;
  269. uint8_t spi_mode = SPI_MODE_0;
  270. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  271. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  272. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  273. draw.DCDelay = conf->dc_delay_us;
  274. draw.Mode = spi_mode;
  275. draw.Speed = spi_dev->spi_config.bandrate;
  276. draw.SpiID = luat_spi[spi_id].id;
  277. draw.CSPin = spi_dev->spi_config.cs;
  278. draw.DCPin = conf->pin_dc;
  279. draw.x1 = x1;
  280. draw.x2 = x2;
  281. draw.y1 = y1;
  282. draw.y2 = y2;
  283. draw.xoffset = conf->xoffset;
  284. draw.yoffset = conf->yoffset;
  285. draw.Size = (draw.x2 - draw.x1 + 1) * (draw.y2 - draw.y1 + 1) * 2;
  286. draw.ColorMode = COLOR_MODE_RGB_565;
  287. draw.Data = color;
  288. Core_LCDDrawBlock(&draw);
  289. }
  290. }
  291. #ifdef LUAT_USE_LCD_CUSTOM_DRAW
  292. int luat_lcd_flush(luat_lcd_conf_t* conf) {
  293. if (conf->buff == NULL) {
  294. return 0;
  295. }
  296. //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
  297. if (conf->flush_y_max < conf->flush_y_min) {
  298. // 没有需要刷新的内容,直接跳过
  299. //LLOGD("luat_lcd_flush no need");
  300. return 0;
  301. }
  302. if (conf->is_init_done) {
  303. 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);
  304. }
  305. else {
  306. uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
  307. luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
  308. const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
  309. if (conf->port == LUAT_LCD_SPI_DEVICE){
  310. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
  311. }else{
  312. luat_spi_send(conf->port, tmp, size);
  313. }
  314. }
  315. // 重置为不需要刷新的状态
  316. conf->flush_y_max = 0;
  317. conf->flush_y_min = conf->h;
  318. return 0;
  319. }
  320. 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) {
  321. // 直接刷屏模式
  322. if (conf->buff == NULL) {
  323. if (conf->is_init_done)
  324. {
  325. return luat_lcd_draw_no_block(conf, x1, y1, x2, y2, color, 0);
  326. }
  327. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
  328. luat_lcd_set_address(conf, x1, y1, x2, y2);
  329. if (conf->port == LUAT_LCD_SPI_DEVICE){
  330. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size);
  331. }else{
  332. luat_spi_send(conf->port, (const char*)color, size);
  333. }
  334. return 0;
  335. }
  336. // buff模式
  337. if (x1 > conf->w || y1 > conf->h) {
  338. LLOGE("out of lcd range");
  339. return -1;
  340. }
  341. uint16_t x_end = x2 > conf->w?conf->w:x2;
  342. uint16_t y_end = y2 > conf->h?conf->h:y2;
  343. luat_color_t* dst = (conf->buff + x1 + conf->w * y1);
  344. luat_color_t* src = (color);
  345. size_t lsize = (x_end - x1 + 1);
  346. for (size_t i = y1; i <= y_end; i++) {
  347. memcpy(dst, src, lsize * sizeof(luat_color_t));
  348. dst += conf->w; // 移动到下一行
  349. src += lsize; // 移动数据
  350. if (x2 > conf->w){
  351. src+=x2 - conf->w;
  352. }
  353. }
  354. // 存储需要刷新的区域
  355. if (y1 < conf->flush_y_min)
  356. conf->flush_y_min = y1;
  357. if (y_end > conf->flush_y_max)
  358. conf->flush_y_max = y_end;
  359. return 0;
  360. }
  361. #endif
  362. static void *luat_fatfs_spi_ctrl;
  363. static HANDLE luat_fatfs_locker;
  364. static void sdhc_spi_check(luat_fatfs_spi_t* userdata)
  365. {
  366. if (!luat_fatfs_spi_ctrl)
  367. {
  368. if(userdata->type == 1){
  369. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_device->bus_id].id, userdata->spi_device->spi_config.cs);
  370. luat_spi_device_config(userdata->spi_device);
  371. }else{
  372. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_id].id, userdata->spi_cs);
  373. }
  374. }
  375. }
  376. static DSTATUS sdhc_spi_initialize(luat_fatfs_spi_t* userdata)
  377. {
  378. int i;
  379. if (!luat_fatfs_locker)
  380. {
  381. luat_fatfs_locker = OS_MutexCreateUnlock();
  382. }
  383. OS_MutexLock(luat_fatfs_locker);
  384. if (luat_fatfs_spi_ctrl)
  385. {
  386. for(i = 0; i < USB_MAX; i++)
  387. {
  388. Core_UDiskDetachSDHC(i, luat_fatfs_spi_ctrl);
  389. }
  390. SDHC_SpiRelease(luat_fatfs_spi_ctrl);
  391. free(luat_fatfs_spi_ctrl);
  392. luat_fatfs_spi_ctrl = NULL;
  393. }
  394. sdhc_spi_check(userdata);
  395. SDHC_SpiInitCard(luat_fatfs_spi_ctrl);
  396. if(userdata->type == 1){
  397. userdata->spi_device->spi_config.bandrate = userdata->fast_speed;
  398. luat_spi_device_config(userdata->spi_device);
  399. }else{
  400. SPI_SetNewConfig(luat_spi[userdata->spi_id].id, userdata->fast_speed, 0);
  401. }
  402. if (SDHC_IsReady(luat_fatfs_spi_ctrl))
  403. {
  404. for(i = 0; i < USB_MAX; i++)
  405. {
  406. Core_UDiskAttachSDHCRecovery(i, luat_fatfs_spi_ctrl);
  407. }
  408. }
  409. OS_MutexRelease(luat_fatfs_locker);
  410. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  411. }
  412. static DSTATUS sdhc_spi_status(luat_fatfs_spi_t* userdata)
  413. {
  414. OS_MutexLock(luat_fatfs_locker);
  415. sdhc_spi_check(userdata);
  416. OS_MutexRelease(luat_fatfs_locker);
  417. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  418. }
  419. static DRESULT sdhc_spi_read(luat_fatfs_spi_t* userdata, uint8_t* buff, uint32_t sector, uint32_t count)
  420. {
  421. OS_MutexLock(luat_fatfs_locker);
  422. sdhc_spi_check(userdata);
  423. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  424. {
  425. OS_MutexRelease(luat_fatfs_locker);
  426. return RES_NOTRDY;
  427. }
  428. SDHC_SpiReadBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  429. OS_MutexRelease(luat_fatfs_locker);
  430. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  431. }
  432. static DRESULT sdhc_spi_write(luat_fatfs_spi_t* userdata, const uint8_t* buff, uint32_t sector, uint32_t count)
  433. {
  434. OS_MutexLock(luat_fatfs_locker);
  435. sdhc_spi_check(userdata);
  436. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  437. {
  438. OS_MutexRelease(luat_fatfs_locker);
  439. return RES_NOTRDY;
  440. }
  441. SDHC_SpiWriteBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  442. OS_MutexRelease(luat_fatfs_locker);
  443. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  444. }
  445. static DRESULT sdhc_spi_ioctl(luat_fatfs_spi_t* userdata, uint8_t ctrl, void* buff)
  446. {
  447. OS_MutexLock(luat_fatfs_locker);
  448. sdhc_spi_check(userdata);
  449. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  450. {
  451. OS_MutexRelease(luat_fatfs_locker);
  452. return RES_NOTRDY;
  453. }
  454. SDHC_SpiReadCardConfig(luat_fatfs_spi_ctrl);
  455. OS_MutexRelease(luat_fatfs_locker);
  456. switch (ctrl) {
  457. case CTRL_SYNC : /* Make sure that no pending write process */
  458. return RES_OK;
  459. break;
  460. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  461. *(uint32_t*)buff = SDHC_GetLogBlockNbr(luat_fatfs_spi_ctrl);
  462. return RES_OK;
  463. break;
  464. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  465. *(uint32_t*)buff = 128;
  466. return RES_OK;
  467. break;
  468. default:
  469. return RES_PARERR;
  470. }
  471. return RES_PARERR;
  472. }
  473. static const block_disk_opts_t sdhc_spi_disk_opts = {
  474. .initialize = sdhc_spi_initialize,
  475. .status = sdhc_spi_status,
  476. .read = sdhc_spi_read,
  477. .write = sdhc_spi_write,
  478. .ioctl = sdhc_spi_ioctl,
  479. };
  480. void luat_spi_set_sdhc_ctrl(block_disk_t *disk)
  481. {
  482. disk->opts = &sdhc_spi_disk_opts;
  483. }
  484. void *luat_spi_get_sdhc_ctrl(void)
  485. {
  486. return luat_fatfs_spi_ctrl;
  487. }