luat_spi_air105.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. //关闭SPI,成功返回0
  150. int luat_spi_close(int spi_id) {
  151. return 0;
  152. }
  153. //收发SPI数据,返回接收字节数
  154. int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
  155. // 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);
  156. // while(luat_spi[spi_id].mark)
  157. luat_spi[spi_id].mark = 1;
  158. if(luat_spi[spi_id].mode==1)
  159. SPI_FlashBlockTransfer(luat_spi[spi_id].id, send_buf, send_length, recv_buf, recv_length);
  160. else
  161. SPI_BlockTransfer(luat_spi[spi_id].id, send_buf, recv_buf, recv_length);
  162. return recv_length;
  163. }
  164. //收SPI数据,返回接收字节数
  165. int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
  166. // LLOGD("SPI_MasterInit luat_spi%d:%d recv_buf:%x length:%d ",spi_id,luat_spi[spi_id], *recv_buf, length);
  167. // while(luat_spi[spi_id].mark)
  168. luat_spi[spi_id].mark = 1;
  169. SPI_BlockTransfer(luat_spi[spi_id].id, recv_buf, recv_buf, length);
  170. return length;
  171. }
  172. //发SPI数据,返回发送字节数
  173. int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
  174. // LLOGD("luat_spi_send luat_spi%d:%d send_buf:%x length:%d ",spi_id,luat_spi[spi_id], *send_buf, length);
  175. // while(luat_spi[spi_id].mark)
  176. luat_spi[spi_id].mark = 1;
  177. SPI_BlockTransfer(luat_spi[spi_id].id, send_buf, NULL, length);
  178. return length;
  179. }
  180. 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)
  181. {
  182. uint32_t retry_cnt = 0;
  183. uint32_t cache_len = Core_LCDDrawCacheLen();
  184. // if (last_flush)
  185. // {
  186. // LLOGD("lcd flush done!");
  187. // }
  188. if (conf->port == LUAT_LCD_SPI_DEVICE){
  189. while (Core_LCDDrawCacheLen() > (conf->buffer_size))
  190. {
  191. retry_cnt++;
  192. luat_timer_mdelay(1);
  193. }
  194. if (retry_cnt)
  195. {
  196. LLOGD("lcd flush delay %ums, status %u,%u,%u,%d", retry_cnt, cache_len, x2-x1+1, y2-y1+1, last_flush);
  197. }
  198. LCD_DrawStruct *draw = OS_Zalloc(sizeof(LCD_DrawStruct));
  199. if (!draw)
  200. {
  201. LLOGE("lcd flush no memory");
  202. return -1;
  203. }
  204. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  205. int spi_id = spi_dev->bus_id;
  206. uint8_t spi_mode = SPI_MODE_0;
  207. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  208. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  209. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  210. draw->DCDelay = conf->dc_delay_us;
  211. draw->Mode = spi_mode;
  212. draw->Speed = spi_dev->spi_config.bandrate;
  213. draw->SpiID = luat_spi[spi_id].id;
  214. draw->CSPin = spi_dev->spi_config.cs;
  215. draw->DCPin = conf->pin_dc;
  216. draw->x1 = x1;
  217. draw->x2 = x2;
  218. draw->y1 = y1;
  219. draw->y2 = y2;
  220. draw->xoffset = conf->xoffset;
  221. draw->yoffset = conf->yoffset;
  222. draw->Size = (draw->x2 - draw->x1 + 1) * (draw->y2 - draw->y1 + 1) * 2;
  223. draw->ColorMode = COLOR_MODE_RGB_565;
  224. draw->Data = OS_Malloc(draw->Size);
  225. if (!draw->Data)
  226. {
  227. LLOGE("lcd flush data no memory");
  228. OS_Free(draw);
  229. return -1;
  230. }
  231. memcpy(draw->Data, color, draw->Size);
  232. Core_LCDDraw(draw);
  233. return 0;
  234. }
  235. else
  236. {
  237. return -1;
  238. }
  239. }
  240. 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)
  241. {
  242. LCD_DrawStruct draw;
  243. if (conf->port == LUAT_LCD_SPI_DEVICE){
  244. luat_spi_device_t* spi_dev = (luat_spi_device_t*)conf->lcd_spi_device;
  245. int spi_id = spi_dev->bus_id;
  246. uint8_t spi_mode = SPI_MODE_0;
  247. if(spi_dev->spi_config.CPHA&&spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_3;
  248. else if(spi_dev->spi_config.CPOL)spi_mode = SPI_MODE_2;
  249. else if(spi_dev->spi_config.CPHA)spi_mode = SPI_MODE_1;
  250. draw.DCDelay = conf->dc_delay_us;
  251. draw.Mode = spi_mode;
  252. draw.Speed = spi_dev->spi_config.bandrate;
  253. draw.SpiID = luat_spi[spi_id].id;
  254. draw.CSPin = spi_dev->spi_config.cs;
  255. draw.DCPin = conf->pin_dc;
  256. draw.x1 = x1;
  257. draw.x2 = x2;
  258. draw.y1 = y1;
  259. draw.y2 = y2;
  260. draw.xoffset = conf->xoffset;
  261. draw.yoffset = conf->yoffset;
  262. draw.Size = (draw.x2 - draw.x1 + 1) * (draw.y2 - draw.y1 + 1) * 2;
  263. draw.ColorMode = COLOR_MODE_RGB_565;
  264. draw.Data = color;
  265. Core_LCDDrawBlock(&draw);
  266. }
  267. }
  268. static void *luat_fatfs_spi_ctrl;
  269. static void sdhc_spi_check(luat_fatfs_spi_t* userdata)
  270. {
  271. if (!luat_fatfs_spi_ctrl)
  272. {
  273. if(userdata->type == 1){
  274. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_device->bus_id].id, userdata->spi_device->spi_config.cs);
  275. luat_spi_device_config(userdata->spi_device);
  276. }else{
  277. luat_fatfs_spi_ctrl = SDHC_SpiCreate(luat_spi[userdata->spi_id].id, userdata->spi_cs);
  278. }
  279. }
  280. }
  281. static DSTATUS sdhc_spi_initialize(luat_fatfs_spi_t* userdata)
  282. {
  283. if (luat_fatfs_spi_ctrl)
  284. {
  285. free(luat_fatfs_spi_ctrl);
  286. luat_fatfs_spi_ctrl = NULL;
  287. }
  288. sdhc_spi_check(userdata);
  289. SDHC_SpiInitCard(luat_fatfs_spi_ctrl);
  290. if(userdata->type == 1){
  291. userdata->spi_device->spi_config.bandrate = userdata->fast_speed;
  292. luat_spi_device_config(userdata->spi_device);
  293. }else{
  294. SPI_SetNewConfig(luat_spi[userdata->spi_id].id, userdata->fast_speed, 0);
  295. }
  296. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  297. }
  298. static DSTATUS sdhc_spi_status(luat_fatfs_spi_t* userdata)
  299. {
  300. sdhc_spi_check(userdata);
  301. return SDHC_IsReady(luat_fatfs_spi_ctrl)?0:STA_NOINIT;
  302. }
  303. static DRESULT sdhc_spi_read(luat_fatfs_spi_t* userdata, uint8_t* buff, uint32_t sector, uint32_t count)
  304. {
  305. sdhc_spi_check(userdata);
  306. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  307. {
  308. return RES_NOTRDY;
  309. }
  310. SDHC_SpiReadBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  311. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  312. }
  313. static DRESULT sdhc_spi_write(luat_fatfs_spi_t* userdata, const uint8_t* buff, uint32_t sector, uint32_t count)
  314. {
  315. sdhc_spi_check(userdata);
  316. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  317. {
  318. return RES_NOTRDY;
  319. }
  320. SDHC_SpiWriteBlocks(luat_fatfs_spi_ctrl, buff, sector, count);
  321. return SDHC_IsReady(luat_fatfs_spi_ctrl)?RES_OK:RES_ERROR;
  322. }
  323. static DRESULT sdhc_spi_ioctl(luat_fatfs_spi_t* userdata, uint8_t ctrl, void* buff)
  324. {
  325. sdhc_spi_check(userdata);
  326. if (!SDHC_IsReady(luat_fatfs_spi_ctrl))
  327. {
  328. return RES_NOTRDY;
  329. }
  330. SDHC_SpiReadCardConfig(luat_fatfs_spi_ctrl);
  331. switch (ctrl) {
  332. case CTRL_SYNC : /* Make sure that no pending write process */
  333. return RES_OK;
  334. break;
  335. case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
  336. *(uint32_t*)buff = SDHC_GetLogBlockNbr(luat_fatfs_spi_ctrl);
  337. return RES_OK;
  338. break;
  339. case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
  340. *(uint32_t*)buff = 128;
  341. return RES_OK;
  342. break;
  343. default:
  344. return RES_PARERR;
  345. }
  346. return RES_PARERR;
  347. }
  348. static const block_disk_opts_t sdhc_spi_disk_opts = {
  349. .initialize = sdhc_spi_initialize,
  350. .status = sdhc_spi_status,
  351. .read = sdhc_spi_read,
  352. .write = sdhc_spi_write,
  353. .ioctl = sdhc_spi_ioctl,
  354. };
  355. void luat_spi_set_sdhc_ctrl(block_disk_t *disk)
  356. {
  357. disk->opts = &sdhc_spi_disk_opts;
  358. }
  359. void *luat_spi_get_sdhc_ctrl(void)
  360. {
  361. return luat_fatfs_spi_ctrl;
  362. }