drv_spi.c 7.2 KB

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