drv_spi.c 5.3 KB

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