drv_spi.c 7.6 KB

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