dev_spi_bit_ops.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-11 kyle first version
  9. */
  10. #include <dev_spi_bit_ops.h>
  11. #include <rtdevice.h>
  12. #define DBG_TAG "SPI"
  13. #ifdef RT_SPI_BITOPS_DEBUG
  14. #define DBG_LVL DBG_LOG
  15. #else
  16. #define DBG_LVL DBG_ERROR
  17. #endif
  18. #include <rtdbg.h>
  19. #define TOG_SCLK(ops) ops->tog_sclk(ops->data)
  20. #define SET_SCLK(ops, val) ops->set_sclk(ops->data, val)
  21. #define SET_MOSI(ops, val) ops->set_mosi(ops->data, val)
  22. #define SET_MISO(ops, val) ops->set_miso(ops->data, val)
  23. #define GET_SCLK(ops) ops->get_sclk(ops->data)
  24. #define GET_MOSI(ops) ops->get_mosi(ops->data)
  25. #define GET_MISO(ops) ops->get_miso(ops->data)
  26. #define DIR_MOSI(ops, val) ops->dir_mosi(ops->data, val)
  27. #define DIR_MISO(ops, val) ops->dir_miso(ops->data, val)
  28. rt_inline void spi_delay(struct rt_spi_bit_ops *ops)
  29. {
  30. ops->udelay((ops->delay_us + 1) >> 1);
  31. }
  32. rt_inline void spi_delay2(struct rt_spi_bit_ops *ops)
  33. {
  34. ops->udelay(ops->delay_us);
  35. }
  36. #define SCLK_H(ops) SET_SCLK(ops, 1)
  37. #define SCLK_L(ops) SET_SCLK(ops, 0)
  38. #define MOSI_H(ops) SET_MOSI(ops, 1)
  39. #define MOSI_L(ops) SET_MOSI(ops, 0)
  40. #define MOSI_IN(ops) DIR_MOSI(ops, 1)
  41. #define MOSI_OUT(ops) DIR_MOSI(ops, 0)
  42. #define MISO_IN(ops) DIR_MISO(ops, 1)
  43. #define MISO_OUT(ops) DIR_MISO(ops, 0)
  44. rt_inline rt_ssize_t spi_xfer_4line_data8(struct rt_spi_bit_ops *ops,
  45. struct rt_spi_configuration *config,
  46. const void *send_buf,
  47. void *recv_buf,
  48. rt_size_t length)
  49. {
  50. int i = 0;
  51. RT_ASSERT(ops != RT_NULL);
  52. RT_ASSERT(length != 0);
  53. {
  54. const rt_uint8_t *send_ptr = send_buf;
  55. rt_uint8_t *recv_ptr = recv_buf;
  56. rt_uint32_t size = length;
  57. while (size--)
  58. {
  59. rt_uint8_t tx_data = 0xFF;
  60. rt_uint8_t rx_data = 0xFF;
  61. rt_uint8_t bit = 0;
  62. if (send_buf != RT_NULL)
  63. {
  64. tx_data = *send_ptr++;
  65. }
  66. for (i = 0; i < 8; i++)
  67. {
  68. if (config->mode & RT_SPI_MSB) { bit = tx_data & (0x1 << (7 - i)); }
  69. else { bit = tx_data & (0x1 << i); }
  70. if (bit) MOSI_H(ops);
  71. else MOSI_L(ops);
  72. spi_delay2(ops);
  73. TOG_SCLK(ops);
  74. if (config->mode & RT_SPI_MSB)
  75. {
  76. rx_data <<= 1; bit = 0x01;
  77. }
  78. else
  79. {
  80. rx_data >>= 1; bit = 0x80;
  81. }
  82. if (GET_MISO(ops))
  83. {
  84. rx_data |= bit;
  85. }
  86. else
  87. {
  88. rx_data &= ~bit;
  89. }
  90. spi_delay2(ops);
  91. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 7))
  92. {
  93. TOG_SCLK(ops);
  94. }
  95. }
  96. if (recv_buf != RT_NULL)
  97. {
  98. *recv_ptr++ = rx_data;
  99. }
  100. }
  101. }
  102. return length;
  103. }
  104. rt_inline rt_ssize_t spi_xfer_4line_data16(struct rt_spi_bit_ops *ops,
  105. struct rt_spi_configuration *config,
  106. const void *send_buf,
  107. void *recv_buf,
  108. rt_size_t length)
  109. {
  110. int i = 0;
  111. RT_ASSERT(ops != RT_NULL);
  112. RT_ASSERT(length != 0);
  113. {
  114. const rt_uint16_t *send_ptr = send_buf;
  115. rt_uint16_t *recv_ptr = recv_buf;
  116. rt_uint32_t size = length;
  117. while (size--)
  118. {
  119. rt_uint16_t tx_data = 0xFFFF;
  120. rt_uint16_t rx_data = 0xFFFF;
  121. rt_uint16_t bit = 0;
  122. if (send_buf != RT_NULL)
  123. {
  124. tx_data = *send_ptr++;
  125. }
  126. for (i = 0; i < 16; i++)
  127. {
  128. if (config->mode & RT_SPI_MSB) { bit = tx_data & (0x1 << (15 - i)); }
  129. else { bit = tx_data & (0x1 << i); }
  130. if (bit) MOSI_H(ops);
  131. else MOSI_L(ops);
  132. spi_delay2(ops);
  133. TOG_SCLK(ops);
  134. if (config->mode & RT_SPI_MSB)
  135. {
  136. rx_data <<= 1; bit = 0x0001;
  137. }
  138. else
  139. {
  140. rx_data >>= 1; bit = 0x8000;
  141. }
  142. if (GET_MISO(ops))
  143. {
  144. rx_data |= bit;
  145. }
  146. else
  147. {
  148. rx_data &= ~bit;
  149. }
  150. spi_delay2(ops);
  151. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 15))
  152. {
  153. TOG_SCLK(ops);
  154. }
  155. }
  156. if (recv_buf != RT_NULL)
  157. {
  158. *recv_ptr++ = rx_data;
  159. }
  160. }
  161. }
  162. return length;
  163. }
  164. rt_inline rt_ssize_t spi_xfer_3line_data8(struct rt_spi_bit_ops *ops,
  165. struct rt_spi_configuration *config,
  166. const void *send_buf,
  167. void *recv_buf,
  168. rt_size_t length)
  169. {
  170. int i = 0;
  171. RT_ASSERT(ops != RT_NULL);
  172. RT_ASSERT(length != 0);
  173. {
  174. const rt_uint8_t *send_ptr = send_buf;
  175. rt_uint8_t *recv_ptr = recv_buf;
  176. rt_uint32_t size = length;
  177. rt_uint8_t send_flg = 0;
  178. if ((send_buf != RT_NULL) || (recv_buf == RT_NULL))
  179. {
  180. MOSI_OUT(ops);
  181. send_flg = 1;
  182. }
  183. else
  184. {
  185. MOSI_IN(ops);
  186. }
  187. while (size--)
  188. {
  189. rt_uint8_t tx_data = 0xFF;
  190. rt_uint8_t rx_data = 0xFF;
  191. rt_uint8_t bit = 0;
  192. if (send_buf != RT_NULL)
  193. {
  194. tx_data = *send_ptr++;
  195. }
  196. if (send_flg)
  197. {
  198. for (i = 0; i < 8; i++)
  199. {
  200. if (config->mode & RT_SPI_MSB) { bit = tx_data & (0x1 << (7 - i)); }
  201. else { bit = tx_data & (0x1 << i); }
  202. if (bit) MOSI_H(ops);
  203. else MOSI_L(ops);
  204. spi_delay2(ops);
  205. TOG_SCLK(ops);
  206. spi_delay2(ops);
  207. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 7))
  208. {
  209. TOG_SCLK(ops);
  210. }
  211. }
  212. rx_data = tx_data;
  213. }
  214. else
  215. {
  216. for (i = 0; i < 8; i++)
  217. {
  218. spi_delay2(ops);
  219. TOG_SCLK(ops);
  220. if (config->mode & RT_SPI_MSB)
  221. {
  222. rx_data <<= 1; bit = 0x01;
  223. }
  224. else
  225. {
  226. rx_data >>= 1; bit = 0x80;
  227. }
  228. if (GET_MOSI(ops))
  229. {
  230. rx_data |= bit;
  231. }
  232. else
  233. {
  234. rx_data &= ~bit;
  235. }
  236. spi_delay2(ops);
  237. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 7))
  238. {
  239. TOG_SCLK(ops);
  240. }
  241. }
  242. }
  243. if (recv_buf != RT_NULL)
  244. {
  245. *recv_ptr++ = rx_data;
  246. }
  247. }
  248. if (!send_flg)
  249. {
  250. MOSI_OUT(ops);
  251. }
  252. }
  253. return length;
  254. }
  255. rt_inline rt_ssize_t spi_xfer_3line_data16(struct rt_spi_bit_ops *ops,
  256. struct rt_spi_configuration *config,
  257. const void *send_buf,
  258. void *recv_buf,
  259. rt_size_t length)
  260. {
  261. int i = 0;
  262. RT_ASSERT(ops != RT_NULL);
  263. RT_ASSERT(length != 0);
  264. {
  265. const rt_uint16_t *send_ptr = send_buf;
  266. rt_uint16_t *recv_ptr = recv_buf;
  267. rt_uint32_t size = length;
  268. rt_uint8_t send_flg = 0;
  269. if ((send_buf != RT_NULL) || (recv_buf == RT_NULL))
  270. {
  271. MOSI_OUT(ops);
  272. send_flg = 1;
  273. }
  274. else
  275. {
  276. MOSI_IN(ops);
  277. }
  278. while (size--)
  279. {
  280. rt_uint16_t tx_data = 0xFFFF;
  281. rt_uint16_t rx_data = 0xFFFF;
  282. rt_uint16_t bit = 0;
  283. if (send_buf != RT_NULL)
  284. {
  285. tx_data = *send_ptr++;
  286. }
  287. if (send_flg)
  288. {
  289. for (i = 0; i < 16; i++)
  290. {
  291. if (config->mode & RT_SPI_MSB) { bit = tx_data & (0x1 << (15 - i)); }
  292. else { bit = tx_data & (0x1 << i); }
  293. if (bit) MOSI_H(ops);
  294. else MOSI_L(ops);
  295. spi_delay2(ops);
  296. TOG_SCLK(ops);
  297. spi_delay2(ops);
  298. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 15))
  299. {
  300. TOG_SCLK(ops);
  301. }
  302. }
  303. rx_data = tx_data;
  304. }
  305. else
  306. {
  307. for (i = 0; i < 16; i++)
  308. {
  309. spi_delay2(ops);
  310. TOG_SCLK(ops);
  311. if (config->mode & RT_SPI_MSB)
  312. {
  313. rx_data <<= 1; bit = 0x0001;
  314. }
  315. else
  316. {
  317. rx_data >>= 1; bit = 0x8000;
  318. }
  319. if (GET_MOSI(ops))
  320. {
  321. rx_data |= bit;
  322. }
  323. else
  324. {
  325. rx_data &= ~bit;
  326. }
  327. spi_delay2(ops);
  328. if (!(config->mode & RT_SPI_CPHA) || (size != 0) || (i < 15))
  329. {
  330. TOG_SCLK(ops);
  331. }
  332. }
  333. }
  334. if (recv_buf != RT_NULL)
  335. {
  336. *recv_ptr++ = rx_data;
  337. }
  338. }
  339. if (!send_flg)
  340. {
  341. MOSI_OUT(ops);
  342. }
  343. }
  344. return length;
  345. }
  346. rt_err_t spi_bit_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
  347. {
  348. struct rt_spi_bit_obj *obj = rt_container_of(device->bus, struct rt_spi_bit_obj, bus);
  349. struct rt_spi_bit_ops *ops = obj->ops;
  350. RT_ASSERT(device != RT_NULL);
  351. RT_ASSERT(configuration != RT_NULL);
  352. if(ops->pin_init != RT_NULL)
  353. {
  354. ops->pin_init();
  355. }
  356. if (configuration->mode & RT_SPI_SLAVE)
  357. {
  358. return -RT_EIO;
  359. }
  360. if (configuration->mode & RT_SPI_CPOL)
  361. {
  362. SCLK_H(ops);
  363. }
  364. else
  365. {
  366. SCLK_L(ops);
  367. }
  368. if (configuration->max_hz < 200000)
  369. {
  370. ops->delay_us = 1;
  371. }
  372. else
  373. {
  374. ops->delay_us = 0;
  375. }
  376. rt_memcpy(&obj->config, configuration, sizeof(struct rt_spi_configuration));
  377. return RT_EOK;
  378. }
  379. rt_ssize_t spi_bit_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  380. {
  381. struct rt_spi_bit_obj *obj = rt_container_of(device->bus, struct rt_spi_bit_obj, bus);
  382. struct rt_spi_bit_ops *ops = obj->ops;
  383. struct rt_spi_configuration *config = &obj->config;
  384. rt_base_t cs_pin = device->cs_pin;
  385. rt_ssize_t length = 0;
  386. RT_ASSERT(device != NULL);
  387. RT_ASSERT(message != NULL);
  388. #ifdef RT_SPI_BITOPS_DEBUG
  389. if (!ops->tog_sclk || !ops->set_sclk || !ops->get_sclk)
  390. {
  391. LOG_E("SPI bus error, SCLK line not defined");
  392. }
  393. if (!ops->set_mosi || !ops->get_mosi)
  394. {
  395. LOG_E("SPI bus error, MOSI line not defined");
  396. }
  397. if (!ops->set_miso || !ops->get_miso)
  398. {
  399. LOG_E("SPI bus error, MISO line not defined");
  400. }
  401. #endif
  402. /* take CS */
  403. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE))
  404. {
  405. LOG_I("spi take cs\n");
  406. if (device->config.mode & RT_SPI_CS_HIGH)
  407. {
  408. rt_pin_write(cs_pin, PIN_HIGH);
  409. }
  410. else
  411. {
  412. rt_pin_write(cs_pin, PIN_LOW);
  413. }
  414. spi_delay(ops);
  415. }
  416. /* spi phase */
  417. if ((config->mode & RT_SPI_CPHA))
  418. {
  419. spi_delay(ops);
  420. TOG_SCLK(ops);
  421. }
  422. if (config->mode & RT_SPI_3WIRE)
  423. {
  424. if (config->data_width <= 8)
  425. {
  426. length = spi_xfer_3line_data8(ops, config, message->send_buf, message->recv_buf, message->length);
  427. }
  428. else if (config->data_width <= 16)
  429. {
  430. length = spi_xfer_3line_data16(ops, config, message->send_buf, message->recv_buf, message->length);
  431. }
  432. }
  433. else
  434. {
  435. if (config->data_width <= 8)
  436. {
  437. length = spi_xfer_4line_data8(ops, config, message->send_buf, message->recv_buf, message->length);
  438. }
  439. else if (config->data_width <= 16)
  440. {
  441. length = spi_xfer_4line_data16(ops, config, message->send_buf, message->recv_buf, message->length);
  442. }
  443. }
  444. /* release CS */
  445. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (cs_pin != PIN_NONE))
  446. {
  447. if ((config->mode & RT_SPI_CPOL) && !GET_SCLK(ops))
  448. {
  449. spi_delay(ops);
  450. TOG_SCLK(ops);
  451. }
  452. spi_delay(ops);
  453. if (device->config.mode & RT_SPI_CS_HIGH)
  454. {
  455. rt_pin_write(cs_pin, PIN_LOW);
  456. }
  457. else
  458. {
  459. rt_pin_write(cs_pin, PIN_HIGH);
  460. }
  461. LOG_I("spi release cs\n");
  462. }
  463. return length;
  464. }
  465. static const struct rt_spi_ops spi_bit_bus_ops =
  466. {
  467. .configure = spi_bit_configure,
  468. .xfer = spi_bit_xfer,
  469. };
  470. rt_err_t rt_spi_bit_add_bus(struct rt_spi_bit_obj *obj,
  471. const char *bus_name,
  472. struct rt_spi_bit_ops *ops)
  473. {
  474. obj->ops = ops;
  475. obj->config.data_width = 8;
  476. obj->config.max_hz = 1 * 1000 * 1000;
  477. obj->config.mode = RT_SPI_MASTER | RT_SPI_MSB | RT_SPI_MODE_0;
  478. return rt_spi_bus_register(&obj->bus, bus_name, &spi_bit_bus_ops);
  479. }