drv_spi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-11-02 勤为本 first version
  9. * 2018-06-09 zhuangwei add spi0 cs0 support,remove msd_init
  10. */
  11. #include <rtthread.h>
  12. #include <drivers/spi.h>
  13. #include <rthw.h>
  14. #include "drv_spi.h"
  15. #include "ls1c_public.h"
  16. #include "spi_msd.h"
  17. #include "dfs_fs.h"
  18. #include "../libraries/ls1c_pin.h"
  19. #ifdef RT_USING_SPI
  20. #ifdef DEBUG
  21. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  22. #else
  23. #define DEBUG_PRINTF(...)
  24. #endif
  25. static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  26. static rt_uint32_t xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  27. static struct rt_spi_ops ls1c_spi_ops =
  28. {
  29. .configure = configure,
  30. .xfer = xfer
  31. };
  32. static rt_err_t configure(struct rt_spi_device *device,
  33. struct rt_spi_configuration *configuration)
  34. {
  35. struct rt_spi_bus *spi_bus = NULL;
  36. struct ls1c_spi *ls1c_spi = NULL;
  37. unsigned char SPIx = 0;
  38. void *spi_base = NULL;
  39. unsigned char cpol = 0;
  40. unsigned char cpha = 0;
  41. unsigned char val = 0;
  42. RT_ASSERT(NULL != device);
  43. RT_ASSERT(NULL != configuration);
  44. spi_bus = device->bus;
  45. ls1c_spi = (struct ls1c_spi *)spi_bus->parent.user_data;
  46. SPIx = ls1c_spi->SPIx;
  47. spi_base = ls1c_spi_get_base(SPIx);
  48. {
  49. // 使能SPI控制器,master模式,关闭中断
  50. reg_write_8(0x53, spi_base + LS1C_SPI_SPCR_OFFSET);
  51. // 清空状态寄存器
  52. reg_write_8(0xc0, spi_base + LS1C_SPI_SPSR_OFFSET);
  53. // 1字节产生中断,采样(读)与发送(写)时机同时
  54. reg_write_8(0x03, spi_base + LS1C_SPI_SPER_OFFSET);
  55. // 关闭SPI flash
  56. val = reg_read_8(spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
  57. val &= 0xfe;
  58. reg_write_8(val, spi_base + LS1C_SPI_SFC_PARAM_OFFSET);
  59. // spi flash时序控制寄存器
  60. reg_write_8(0x05, spi_base + LS1C_SPI_SFC_TIMING_OFFSET);
  61. }
  62. // baudrate
  63. ls1c_spi_set_clock(spi_base, configuration->max_hz);
  64. // 设置通信模式(时钟极性和相位)
  65. if (configuration->mode & RT_SPI_CPOL) // cpol
  66. {
  67. cpol = SPI_CPOL_1;
  68. }
  69. else
  70. {
  71. cpol = SPI_CPOL_0;
  72. }
  73. if (configuration->mode & RT_SPI_CPHA) // cpha
  74. {
  75. cpha = SPI_CPHA_1;
  76. }
  77. else
  78. {
  79. cpha = SPI_CPHA_0;
  80. }
  81. ls1c_spi_set_mode(spi_base, cpol, cpha);
  82. DEBUG_PRINTF("ls1c spi%d configuration\n", SPIx);
  83. return RT_EOK;
  84. }
  85. static rt_uint32_t xfer(struct rt_spi_device *device,
  86. struct rt_spi_message *message)
  87. {
  88. struct rt_spi_bus *spi_bus = NULL;
  89. struct ls1c_spi *ls1c_spi = NULL;
  90. void *spi_base = NULL;
  91. unsigned char SPIx = 0;
  92. struct ls1c_spi_cs *ls1c_spi_cs = NULL;
  93. unsigned char cs = 0;
  94. rt_uint32_t size = 0;
  95. const rt_uint8_t *send_ptr = NULL;
  96. rt_uint8_t *recv_ptr = NULL;
  97. rt_uint8_t data = 0;
  98. RT_ASSERT(NULL != device);
  99. RT_ASSERT(NULL != message);
  100. spi_bus = device->bus;
  101. ls1c_spi = spi_bus->parent.user_data;
  102. SPIx = ls1c_spi->SPIx;
  103. spi_base = ls1c_spi_get_base(SPIx);
  104. ls1c_spi_cs = device->parent.user_data;
  105. cs = ls1c_spi_cs->cs;
  106. size = message->length;
  107. DEBUG_PRINTF("[%s] SPIx=%d, cs=%d\n", __FUNCTION__, SPIx, cs);
  108. // take cs
  109. if (message->cs_take)
  110. {
  111. ls1c_spi_set_cs(spi_base, cs, 0);
  112. }
  113. // 收发数据
  114. send_ptr = message->send_buf;
  115. recv_ptr = message->recv_buf;
  116. while (size--)
  117. {
  118. data = 0xFF;
  119. if (NULL != send_ptr)
  120. {
  121. data = *send_ptr++;
  122. }
  123. if (NULL != recv_ptr)
  124. {
  125. *recv_ptr++ = ls1c_spi_txrx_byte(spi_base, data);
  126. }
  127. else
  128. {
  129. ls1c_spi_txrx_byte(spi_base, data);
  130. }
  131. }
  132. // release cs
  133. if (message->cs_release)
  134. {
  135. ls1c_spi_set_cs(spi_base, cs, 1);
  136. }
  137. return message->length;
  138. }
  139. #ifdef RT_USING_SPI0
  140. struct ls1c_spi ls1c_spi0 =
  141. {
  142. .SPIx = LS1C_SPI_0,
  143. };
  144. static struct rt_spi_bus spi0_bus;
  145. #endif
  146. #ifdef RT_USING_SPI1
  147. struct ls1c_spi ls1c_spi1 =
  148. {
  149. .SPIx = LS1C_SPI_1,
  150. };
  151. static struct rt_spi_bus spi1_bus;
  152. #endif
  153. /*
  154. * 初始化并注册龙芯1c的spi总线
  155. * @SPI SPI总线,比如LS1C_SPI_0, LS1C_SPI_1
  156. * @spi_bus_name 总线名字
  157. * @ret
  158. */
  159. rt_err_t ls1c_spi_bus_register(rt_uint8_t SPI, const char *spi_bus_name)
  160. {
  161. struct rt_spi_bus *spi_bus = NULL;
  162. #ifdef RT_USING_SPI0
  163. if (LS1C_SPI_0 == SPI)
  164. {
  165. spi_bus = &spi0_bus;
  166. spi_bus->parent.user_data = &ls1c_spi0;
  167. }
  168. #endif
  169. #ifdef RT_USING_SPI1
  170. if (LS1C_SPI_1 == SPI)
  171. {
  172. spi_bus = &spi1_bus;
  173. spi_bus->parent.user_data = &ls1c_spi1;
  174. }
  175. #endif
  176. return rt_spi_bus_register(spi_bus, spi_bus_name, &ls1c_spi_ops);
  177. }
  178. int ls1c_hw_spi_init(void)
  179. {
  180. #ifdef RT_USING_SPI0
  181. pin_set_purpose(78, PIN_PURPOSE_OTHER);
  182. pin_set_purpose(79, PIN_PURPOSE_OTHER);
  183. pin_set_purpose(80, PIN_PURPOSE_OTHER);
  184. pin_set_purpose(83, PIN_PURPOSE_OTHER);//cs2 - SD card
  185. pin_set_purpose(82, PIN_PURPOSE_OTHER);//cs1
  186. pin_set_purpose(81, PIN_PURPOSE_OTHER);//cs0
  187. pin_set_remap(78, PIN_REMAP_DEFAULT);
  188. pin_set_remap(79, PIN_REMAP_DEFAULT);
  189. pin_set_remap(80, PIN_REMAP_DEFAULT);
  190. pin_set_remap(83, PIN_REMAP_DEFAULT);//cs2 - SD card
  191. pin_set_remap(82, PIN_REMAP_DEFAULT);//CS1
  192. pin_set_remap(81, PIN_REMAP_DEFAULT);//cs0
  193. ls1c_spi_bus_register(LS1C_SPI_0, "spi0");
  194. #endif
  195. #ifdef RT_USING_SPI1
  196. pin_set_purpose(46, PIN_PURPOSE_OTHER);
  197. pin_set_purpose(47, PIN_PURPOSE_OTHER);
  198. pin_set_purpose(48, PIN_PURPOSE_OTHER);
  199. pin_set_purpose(49, PIN_PURPOSE_OTHER);//CS0 - touch screen
  200. pin_set_remap(46, PIN_REMAP_THIRD);
  201. pin_set_remap(47, PIN_REMAP_THIRD);
  202. pin_set_remap(48, PIN_REMAP_THIRD);
  203. pin_set_remap(49, PIN_REMAP_THIRD);//CS0 - touch screen
  204. ls1c_spi_bus_register(LS1C_SPI_1, "spi1");
  205. #endif
  206. #ifdef RT_USING_SPI0
  207. /* attach cs */
  208. {
  209. static struct rt_spi_device spi_device0;
  210. static struct rt_spi_device spi_device1;
  211. static struct rt_spi_device spi_device2;
  212. static struct rt_spi_device spi_device3;
  213. static struct ls1c_spi_cs spi_cs0;
  214. static struct ls1c_spi_cs spi_cs1;
  215. static struct ls1c_spi_cs spi_cs2;
  216. static struct ls1c_spi_cs spi_cs3;
  217. /* spi02: CS2 SD Card*/
  218. spi_cs2.cs = LS1C_SPI_CS_2;
  219. rt_spi_bus_attach_device(&spi_device2, "spi02", "spi0", (void *)&spi_cs2);
  220. spi_cs1.cs = LS1C_SPI_CS_1;
  221. rt_spi_bus_attach_device(&spi_device1, "spi01", "spi0", (void *)&spi_cs1);
  222. spi_cs0.cs = LS1C_SPI_CS_0;
  223. rt_spi_bus_attach_device(&spi_device0, "spi00", "spi0", (void *)&spi_cs0);
  224. spi_cs3.cs = LS1C_SPI_CS_3;
  225. rt_spi_bus_attach_device(&spi_device3, "spi03", "spi0", (void*)&spi_cs3);
  226. msd_init("sd0", "spi02");
  227. }
  228. #endif
  229. #ifdef RT_USING_SPI1
  230. {
  231. static struct rt_spi_device spi_device;
  232. static struct ls1c_spi_cs spi_cs;
  233. /* spi10: CS0 Touch*/
  234. spi_cs.cs = LS1C_SPI_CS_0;
  235. rt_spi_bus_attach_device(&spi_device, "spi10", "spi1", (void *)&spi_cs);
  236. }
  237. #endif
  238. }
  239. INIT_BOARD_EXPORT(ls1c_hw_spi_init);
  240. static int board_sd_init(void)
  241. {
  242. #if defined(RT_USING_DFS) && defined(RT_USING_DFS_ELMFAT)
  243. /* mount sd card fat partition 1 as root directory */
  244. if( dfs_mount("sd0", "/", "elm", 0, 0) == 0)
  245. {
  246. rt_kprintf("File System initialized!\n");
  247. }
  248. else
  249. {
  250. rt_kprintf("File System initialzation failed!\n");
  251. }
  252. #endif /* RT_USING_DFS && RT_USING_DFS_ELMFAT */
  253. }
  254. INIT_APP_EXPORT(board_sd_init);
  255. #endif