drv_spi.c 8.3 KB

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