drv_spi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-15 JasonHu first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include "drv_spi.h"
  14. #define DBG_TAG "DRV.SPI"
  15. #define DBG_LVL DBG_LOG
  16. #include <rtdbg.h>
  17. #include <sunxi_hal_spi.h>
  18. #ifdef BSP_USING_SPI
  19. struct hw_spi_bus
  20. {
  21. struct rt_spi_bus parent;
  22. hal_spi_master_port_t port;
  23. };
  24. #define BSP_SPI_MAX_HZ SPI_MAX_FREQUENCY
  25. #if defined (BSP_USING_SPI0)
  26. #define SPI0_BUS_NAME "spi0"
  27. #define SPI0_BUS_DEVICE0_NAME "spi01"
  28. struct hw_spi_bus spi0_bus;
  29. #endif /* BSP_USING_SPI0 */
  30. #if defined (BSP_USING_SPI1)
  31. #define SPI1_BUS_NAME "spi1"
  32. #define SPI1_BUS_DEVICE0_NAME "spi11"
  33. struct hw_spi_bus spi1_bus;
  34. #endif /* BSP_USING_SPI1 */
  35. static rt_err_t hw_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  36. {
  37. RT_ASSERT(cfg != RT_NULL);
  38. RT_ASSERT(device != RT_NULL);
  39. struct hw_spi_bus *spi_dev = RT_NULL;
  40. hal_spi_master_config_t hal_cfg;
  41. LOG_D("configure mode:%x, clk:%d\n", cfg->mode, cfg->max_hz);
  42. /* trans cfg to hal cfg */
  43. spi_dev = (struct hw_spi_bus *)(device->parent.user_data);
  44. if (cfg->max_hz > BSP_SPI_MAX_HZ)
  45. cfg->max_hz = BSP_SPI_MAX_HZ;
  46. /* set default frequency */
  47. hal_cfg.clock_frequency = cfg->max_hz;
  48. hal_cfg.slave_port = HAL_SPI_MASTER_SLAVE_0; /* only support slave 0 */
  49. /* byte order */
  50. if (cfg->mode & RT_SPI_MSB)
  51. {
  52. hal_cfg.bit_order = HAL_SPI_MASTER_MSB_FIRST;
  53. }
  54. else
  55. {
  56. hal_cfg.bit_order = HAL_SPI_MASTER_LSB_FIRST;
  57. }
  58. if(cfg->mode & RT_SPI_CPOL)
  59. {
  60. hal_cfg.cpol = HAL_SPI_MASTER_CLOCK_POLARITY1;
  61. }
  62. else
  63. {
  64. hal_cfg.cpol = HAL_SPI_MASTER_CLOCK_POLARITY0;
  65. }
  66. if(cfg->mode & RT_SPI_CPHA)
  67. {
  68. hal_cfg.cpha = HAL_SPI_MASTER_CLOCK_PHASE1;
  69. }
  70. else
  71. {
  72. hal_cfg.cpha = HAL_SPI_MASTER_CLOCK_PHASE0;
  73. }
  74. if (cfg->mode & RT_SPI_NO_CS)
  75. {
  76. hal_cfg.csmode = HAL_SPI_MASTER_CS_AUTO;
  77. }
  78. else
  79. {
  80. hal_cfg.csmode = HAL_SPI_MASTER_CS_SOFT;
  81. }
  82. if (hal_spi_hw_config(spi_dev->port, &hal_cfg) != SPI_MASTER_OK)
  83. {
  84. return -RT_EIO;
  85. }
  86. return RT_EOK;
  87. }
  88. static rt_uint32_t hw_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  89. {
  90. spi_master_status_t ret = SPI_MASTER_OK;
  91. hal_spi_master_transfer_t tr;
  92. struct hw_spi_bus *spi_dev = RT_NULL;
  93. RT_ASSERT(device != RT_NULL);
  94. RT_ASSERT(device->bus != RT_NULL);
  95. RT_ASSERT(device->parent.user_data != RT_NULL);
  96. spi_dev = (struct hw_spi_bus *)(device->parent.user_data);
  97. tr.rx_buf = message->recv_buf;
  98. tr.rx_len = message->recv_buf == RT_NULL ? 0 : message->length;
  99. tr.tx_buf = message->send_buf;
  100. tr.tx_len = message->send_buf == RT_NULL ? 0 : message->length;
  101. tr.dummy_byte = 0;
  102. tr.tx_single_len = message->send_buf == RT_NULL ? 0 : message->length;
  103. tr.rx_nbits = SPI_NBITS_SINGLE;
  104. tr.tx_nbits = SPI_NBITS_SINGLE;
  105. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
  106. {
  107. if (device->config.mode & RT_SPI_CS_HIGH)
  108. {
  109. hal_spi_cs(spi_dev->port, 1);
  110. }
  111. else
  112. {
  113. hal_spi_cs(spi_dev->port, 0);
  114. }
  115. }
  116. if (message->length > 0)
  117. {
  118. ret = hal_spi_xfer(spi_dev->port, &tr);
  119. }
  120. // FIXME: GCC O3 maybe not execute.
  121. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
  122. {
  123. if (device->config.mode & RT_SPI_CS_HIGH)
  124. {
  125. hal_spi_cs(spi_dev->port, 0);
  126. }
  127. else
  128. {
  129. hal_spi_cs(spi_dev->port, 1);
  130. }
  131. }
  132. if (ret != SPI_MASTER_OK)
  133. {
  134. LOG_E("xfer err ret:%ld", ret);
  135. return -RT_EIO;
  136. }
  137. return message->length;
  138. }
  139. /**
  140. * attach a spi device on bus
  141. */
  142. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name)
  143. {
  144. rt_err_t ret = RT_EOK;
  145. struct hw_spi_bus *hw_spi = RT_NULL;
  146. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  147. RT_ASSERT(spi_device != RT_NULL);
  148. #ifdef BSP_USING_SPI0
  149. if (!rt_strcmp(bus_name, SPI0_BUS_NAME))
  150. {
  151. hw_spi = &spi0_bus;
  152. }
  153. else
  154. #endif
  155. #ifdef BSP_USING_SPI1
  156. if (!rt_strcmp(bus_name, SPI0_BUS_NAME))
  157. {
  158. hw_spi = &spi1_bus;
  159. }
  160. else
  161. #endif
  162. {
  163. return -RT_EIO;
  164. }
  165. /* TODO: attach device */
  166. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, hw_spi);
  167. return ret;
  168. }
  169. static struct rt_spi_ops hw_spi_ops =
  170. {
  171. .configure = hw_spi_configure,
  172. .xfer = hw_spi_xfer
  173. };
  174. #endif /* BSP_USING_SPI */
  175. /* used to init spi bus */
  176. static hal_spi_master_config_t default_hal_cfg = {
  177. .bit_order = HAL_SPI_MASTER_MSB_FIRST,
  178. .clock_frequency = 5000000,//SPI_MOD_CLK,
  179. .cpha = HAL_SPI_MASTER_CLOCK_PHASE0,
  180. .cpol = HAL_SPI_MASTER_CLOCK_POLARITY0,
  181. .slave_port = HAL_SPI_MASTER_SLAVE_0,
  182. .csmode = HAL_SPI_MASTER_CS_SOFT,
  183. };
  184. int rt_hw_spi_init(void)
  185. {
  186. #if defined (BSP_USING_SPI0)
  187. spi0_bus.port = HAL_SPI_MASTER_0;
  188. if (hal_spi_init(spi0_bus.port, &default_hal_cfg) != SPI_MASTER_OK)
  189. {
  190. LOG_E("hal init %s bus failed!", SPI0_BUS_NAME);
  191. return -1;
  192. }
  193. rt_spi_bus_register(&spi0_bus.parent, SPI0_BUS_NAME, &hw_spi_ops);
  194. rt_hw_spi_device_attach(SPI0_BUS_NAME, SPI0_BUS_DEVICE0_NAME);
  195. #endif /* BSP_USING_SPI0 */
  196. #if defined (BSP_USING_SPI1)
  197. spi1_bus.port = HAL_SPI_MASTER_1;
  198. if (hal_spi_init(spi1_bus.port, &default_hal_cfg) != SPI_MASTER_OK)
  199. {
  200. LOG_E("hal init %s bus failed!", SPI1_BUS_NAME);
  201. return -1;
  202. }
  203. rt_spi_bus_register(&spi1_bus.parent, SPI1_BUS_NAME, &hw_spi_ops);
  204. rt_hw_spi_device_attach(SPI1_BUS_NAME, SPI1_BUS_DEVICE0_NAME);
  205. #endif /* BSP_USING_SPI1 */
  206. return RT_EOK;
  207. }
  208. INIT_DEVICE_EXPORT(rt_hw_spi_init);
  209. #define CFG_GPIO_PORT(p) ((p) - 'A' + 1)
  210. int32_t hal_spi_gpio_cfg_count(const char *secname)
  211. {
  212. return 4;
  213. }
  214. static const user_gpio_set_t _spi_gpio_cfg[][4] = {
  215. {// spi0
  216. {
  217. .gpio_name = "spi0_sclk",
  218. .port = CFG_GPIO_PORT('C'),
  219. .port_num = 2, // PC02
  220. .mul_sel = 2,
  221. .pull = 0, // no pull
  222. .drv_level = 3,
  223. .data = 0,
  224. },
  225. {
  226. .gpio_name = "spi0_cs",
  227. .port = CFG_GPIO_PORT('C'),
  228. .port_num = 3, // PC03
  229. .mul_sel = 2,
  230. .pull = 1, // pull up
  231. .drv_level = 3,
  232. .data = 0,
  233. },
  234. {
  235. .gpio_name = "spi0_mosi",
  236. .port = CFG_GPIO_PORT('C'),
  237. .port_num = 4, // PC04
  238. .mul_sel = 2,
  239. .pull = 0, // no pull
  240. .drv_level = 3,
  241. .data = 0,
  242. },
  243. {
  244. .gpio_name = "spi0_miso",
  245. .port = CFG_GPIO_PORT('C'),
  246. .port_num = 5, // PC05
  247. .mul_sel = 2,
  248. .pull = 0, // no pull
  249. .drv_level = 3,
  250. .data = 0,
  251. }
  252. },
  253. };
  254. int hal_spi_gpio_cfg_load(user_gpio_set_t *gpio_cfg, int32_t GPIONum, int id)
  255. {
  256. int i;
  257. if (id > sizeof(_spi_gpio_cfg) / sizeof(_spi_gpio_cfg[0]))
  258. {
  259. rt_kprintf("spi id %d>%d\n", id, sizeof(_spi_gpio_cfg) / sizeof(_spi_gpio_cfg[0]));
  260. return -1;
  261. }
  262. /* twi0 */
  263. for (i = 0; i < GPIONum; i++)
  264. {
  265. memcpy(gpio_cfg, &_spi_gpio_cfg[id][i], sizeof(user_gpio_set_t));
  266. gpio_cfg++;
  267. }
  268. return 0;
  269. }
  270. /*
  271. * 程序清单:这是一个 SPI 设备使用例程
  272. * 例程导出了 spi_w25q_sample 命令到控制终端
  273. * 命令调用格式:spi_w25q_sample spi10
  274. * 命令解释:命令第二个参数是要使用的SPI设备名称,为空则使用默认的SPI设备
  275. * 程序功能:通过SPI设备读取 w25q 的 ID 数据
  276. */
  277. #include <rtthread.h>
  278. #include <rtdevice.h>
  279. #define SPI_DEVICE_NAME "spi01"
  280. static void spi_sample(int argc, char *argv[])
  281. {
  282. struct rt_spi_device *spi_dev;
  283. static char cmd[200];
  284. static char recv_data[200];
  285. rt_uint8_t id[5] = {0};
  286. int i;
  287. rt_err_t err;
  288. int type;
  289. type = atoi(argv[1]);
  290. for (i = 0; i < sizeof(cmd); i++)
  291. {
  292. cmd[i] = i + 1;
  293. }
  294. /* 查找 spi 设备获取设备句柄 */
  295. spi_dev = (struct rt_spi_device *)rt_device_find(SPI_DEVICE_NAME);
  296. if (!spi_dev)
  297. {
  298. rt_kprintf("spi sample run failed! can't find %s device!\n", SPI_DEVICE_NAME);
  299. }
  300. else
  301. {
  302. struct rt_spi_configuration spi_cfg;
  303. spi_cfg.max_hz = 5000000;
  304. spi_cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB;
  305. spi_cfg.data_width = 8;
  306. rt_spi_configure(spi_dev, &spi_cfg);
  307. if (type == 1)
  308. {
  309. err = rt_spi_transfer(spi_dev, cmd, recv_data, sizeof(recv_data));
  310. }
  311. else if (type == 2)
  312. {
  313. err = rt_spi_send_then_recv(spi_dev, cmd, sizeof(cmd), recv_data, sizeof(recv_data));
  314. }
  315. else if (type == 3)
  316. {
  317. err = rt_spi_send(spi_dev, cmd, sizeof(cmd));
  318. }
  319. else if (type == 4)
  320. {
  321. err = rt_spi_send_then_send(spi_dev, cmd, sizeof(cmd), cmd, sizeof(cmd));
  322. }
  323. else if (type == 5)
  324. {
  325. err = rt_spi_recv(spi_dev, recv_data, sizeof(recv_data));
  326. }
  327. // err = rt_spi_send(spi_dev, cmd, sizeof(cmd));
  328. if (err != sizeof(cmd))
  329. {
  330. rt_kprintf("spi send error:%d\n", err);
  331. }
  332. }
  333. }
  334. /* 导出到 msh 命令列表中 */
  335. MSH_CMD_EXPORT(spi_sample, spi w25q sample);