spi_bitbang.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /****************************************************************************
  2. * include/nuttx/spi/spi_bitbang.c
  3. *
  4. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <nuttx/spi/spi_bitbang.h>
  40. /****************************************************************************
  41. * Pre-processor Definitions
  42. ****************************************************************************/
  43. /* Usage ********************************************************************/
  44. /* To use this logic, you should provide a C file that does the following:
  45. *
  46. * - Defines SPI_SETSCK and SPI_CLRSCK to set and clear the SCK signal
  47. * - Defines SPI_SETMOSI and SPI_CLRMOSI to set and clear the MISO signal
  48. * - Defines SPI_GETMISO to sample the MISO state
  49. * - Defines SPI_PERBIT_NSEC which is the minimum time to transfer one bit.
  50. * This determines the maximum frequency.
  51. * - Other configuration options:
  52. * SPI_BITBANG_LOOPSPERMSEC - Delay loop calibration
  53. * SPI_BITBANG_DISABLEMODE0 - Define to disable Mode 0 support
  54. * SPI_BITBANG_DISABLEMODE1 - Define to disable Mode 1 support
  55. * SPI_BITBANG_DISABLEMODE2 - Define to disable Mode 2 support
  56. * SPI_BITBANG_DISABLEMODE3 - Define to disable Mode 3 support
  57. * - Provide implementations of spi_select(), spi_status() and spi_cmddata().
  58. * - Then include this file
  59. * - Provide an initialization function that initializes the GPIO pins used
  60. * in the bit bang interface and calls spi_create_bitbang().
  61. */
  62. /****************************************************************************
  63. * Private Function Prototypes
  64. ****************************************************************************/
  65. static void spi_delay(uint32_t holdtime);
  66. static void spi_select(FAR struct spi_bitbang_s *priv,
  67. uint32_t devid, bool selected);
  68. static uint32_t spi_setfrequency(FAR struct spi_bitbang_s *priv,
  69. uint32_t frequency);
  70. static void spi_setmode(FAR struct spi_bitbang_s *priv,
  71. enum spi_mode_e mode);
  72. #ifndef SPI_BITBANG_DISABLEMODE0
  73. static uint16_t spi_bitexchange0(uint16_t dataout, uint32_t holdtime);
  74. #endif
  75. #ifndef SPI_BITBANG_DISABLEMODE1
  76. static uint16_t spi_bitexchange1(uint16_t dataout, uint32_t holdtime);
  77. #endif
  78. #ifndef SPI_BITBANG_DISABLEMODE2
  79. static uint16_t spi_bitexchange2(uint16_t dataout, uint32_t holdtime);
  80. #endif
  81. #ifndef SPI_BITBANG_DISABLEMODE3
  82. static uint16_t spi_bitexchange3(uint16_t dataout, uint32_t holdtime);
  83. #endif
  84. static uint16_t spi_exchange(FAR struct spi_bitbang_s *priv,
  85. uint16_t dataout);
  86. static uint8_t spi_status(FAR struct spi_bitbang_s *priv,
  87. uint32_t devid);
  88. #ifdef CONFIG_SPI_CMDDATA
  89. static int spi_cmddata(FAR struct spi_bitbang_s *priv,
  90. uint32_t devid, bool cmd);
  91. #endif
  92. /****************************************************************************
  93. * Private Data
  94. ****************************************************************************/
  95. static const struct spi_bitbang_ops_s g_spiops =
  96. {
  97. spi_select, /* select */
  98. spi_setfrequency, /* setfrequency */
  99. spi_setmode, /* setmode */
  100. spi_exchange, /* exchange */
  101. spi_status, /* status */
  102. #ifdef CONFIG_SPI_CMDDATA
  103. spi_cmddata, /* cmddata */
  104. #endif
  105. };
  106. /****************************************************************************
  107. * Private Functions
  108. ****************************************************************************/
  109. /****************************************************************************
  110. * Name: spi_delay
  111. *
  112. * Description:
  113. * Delay for a specified number of loops
  114. *
  115. * Input Parameters:
  116. * holdtime - The number of loops
  117. *
  118. * Returned Value:
  119. * None.
  120. *
  121. ****************************************************************************/
  122. static void spi_delay(uint32_t holdtime)
  123. {
  124. volatile int i;
  125. for (i = 0; i < holdtime; i++);
  126. }
  127. /****************************************************************************
  128. * Name: spi_setfrequency
  129. *
  130. * Description:
  131. * Set the SPI frequency.
  132. *
  133. * Input Parameters:
  134. * priv - Device-specific state data
  135. * frequency - The SPI frequency requested
  136. *
  137. * Returned Value:
  138. * Returns the actual frequency selected
  139. *
  140. ****************************************************************************/
  141. static uint32_t spi_setfrequency(FAR struct spi_bitbang_s *priv,
  142. uint32_t frequency)
  143. {
  144. uint32_t pnsec;
  145. /* SPI frequency cannot be precisely controlled with a bit-bang interface.
  146. * Frequency corresponds to delay in toggle the SPI clock line: Set high,
  147. * wait, set low, wait, set high, wait, etc.
  148. *
  149. * Here we calcalute the half period of the frequency in nanoseconds (i.e.,
  150. * the amount of time that the clock should remain in the high or low
  151. * state).
  152. *
  153. * frequency = psec / 1 sec
  154. * psec = full period in seconds
  155. * psec = 1 sec / frequency
  156. * pnsec = 1000000000 nsec / (2 * frequency)
  157. * pnsec = full period in nsec
  158. *
  159. * As examples:
  160. * 1) frequency = 400KHz; SPI_PERBIT_NSEC = 100
  161. * pnsec = (2500 - 100) / 2 = 1200
  162. * 2) frequency = 20MHz; SPI_PERBIT_NSEC = 100
  163. * pnsec = (50 - 100( / 2 -> 0
  164. */
  165. pnsec = (1000000000ul + (frequency >> 1)) / frequency;
  166. /* Minus the bit transfer overhead */
  167. if (pnsec > SPI_PERBIT_NSEC)
  168. {
  169. pnsec -= SPI_PERBIT_NSEC;
  170. }
  171. else
  172. {
  173. pnsec = 0;
  174. }
  175. /* The hold time in nanoseconds is then half this */
  176. pnsec = (pnsec + 1) >> 1;
  177. /* But what we really want is the hold time in loop counts. We know that
  178. * SPI_BITBANG_LOOPSPERMSEC is the number of times through a delay loop
  179. * to get 1 millisecond.
  180. *
  181. * SPI_BITBANG_LOOPSPERMSEC / 1000000 is then the number of counts
  182. * to get 1 nanosecond. In reality, this is a number less than zero. But
  183. * then we can use this to calculate:
  184. *
  185. * holdtime loops/hold = pnsec nsec/hold * (SPI_BITBANG_LOOPSPERMSEC /
  186. * 1000000) loops/nsec
  187. *
  188. * As examples:
  189. * 1) frequency = 400KHz; SPI_PERBIT_NSEC = 100; pnsec = 1200;
  190. * SPI_BITBANG_LOOPSPERMSEC = 5000
  191. * holdtime = (1200 * 5000 + 500000) / 1000000 = 6
  192. * 2) frequency = 20MHz; SPI_PERBIT_NSEC = 100; pnsec = 0;
  193. * SPI_BITBANG_LOOPSPERMSEC = 5000
  194. * holdtime = (0 * 5000 + 500000) / 1000000 = 0
  195. */
  196. priv->holdtime = (pnsec * SPI_BITBANG_LOOPSPERMSEC + 500000) / 1000000;
  197. /* Let's do our best to calculate the actual frequency
  198. *
  199. * As examples:
  200. * 1) frequency = 400KHz; SPI_PERBIT_NSEC = 100;
  201. * SPI_BITBANG_LOOPSPERMSEC = 5000; holdtime = 6
  202. * pnsec = 2 * 1000000 * 6 / 5000 + 100 = 2500 nsec
  203. * frequency = 400KHz
  204. * 2) frequency = 20MHz; SPI_PERBIT_NSEC = 100; holdtime = 0
  205. * SPI_BITBANG_LOOPSPERMSEC = 5000; holdtime = 0
  206. * pnsec = 2 * 0 * 6 / 5000 + 100 = 100 nsec
  207. * frequency = 10MHz
  208. */
  209. pnsec = 2 * 1000000 * priv->holdtime / SPI_BITBANG_LOOPSPERMSEC +
  210. SPI_PERBIT_NSEC;
  211. frequency = 1000000000ul / pnsec;
  212. return frequency;
  213. }
  214. /****************************************************************************
  215. * Name: spi_setmode
  216. *
  217. * Description:
  218. * Select the current SPI mode
  219. *
  220. * Input Parameters:
  221. * priv - Device-specific state data
  222. * mode - The new SPI mode
  223. *
  224. * Returned Value:
  225. * None
  226. *
  227. ****************************************************************************/
  228. static void spi_setmode(FAR struct spi_bitbang_s *priv,
  229. enum spi_mode_e mode)
  230. {
  231. spiinfo("mode=%d\n", mode);
  232. switch (mode)
  233. {
  234. case SPIDEV_MODE0: /* CPOL=0; CPHA=0 */
  235. #ifndef SPI_BITBANG_DISABLEMODE0
  236. SPI_CLRSCK; /* Resting level of the clock is low */
  237. priv->exchange = spi_bitexchange0;
  238. #else
  239. DEBUGPANIC();
  240. #endif
  241. break;
  242. case SPIDEV_MODE1: /* CPOL=0; CPHA=1 */
  243. #ifndef SPI_BITBANG_DISABLEMODE1
  244. SPI_CLRSCK; /* Resting level of the clock is low */
  245. priv->exchange = spi_bitexchange1;
  246. #else
  247. DEBUGPANIC();
  248. #endif
  249. break;
  250. case SPIDEV_MODE2: /* CPOL=1; CPHA=0 */
  251. #ifndef SPI_BITBANG_DISABLEMODE2
  252. SPI_SETSCK; /* Resting level of the clock is high */
  253. priv->exchange = spi_bitexchange2;
  254. #else
  255. DEBUGPANIC();
  256. #endif
  257. break;
  258. case SPIDEV_MODE3: /* CPOL=1; CPHA=1 */
  259. #ifndef SPI_BITBANG_DISABLEMODE3
  260. SPI_SETSCK; /* Resting level of the clock is high */
  261. priv->exchange = spi_bitexchange3;
  262. #else
  263. DEBUGPANIC();
  264. #endif
  265. break;
  266. default:
  267. DEBUGPANIC();
  268. break;
  269. }
  270. }
  271. /****************************************************************************
  272. * Name: spi_bitexchange0
  273. *
  274. * Description:
  275. * Exchange one bit in mode 0
  276. *
  277. * MODE 0: CPOL=0 and CPHA = 0
  278. * The base value of the clock is zero. Data is captured on the clock's
  279. * rising edge and data is propagated on the falling edge (high->low
  280. * transition).
  281. *
  282. * /CS --+
  283. * |
  284. * +-----------------------------------------------------------------
  285. * <-hold time-> <-hold time-><-hold time-><-hold time-><-hold time->
  286. * +------------+ +------------+
  287. * | | | |
  288. * SCLK ---------------+ +------------+ +------------
  289. * `- Set MOSI | `- Set MOSI | `- Set MOSI
  290. * `- Sample MISO `- Sample MISO
  291. *
  292. * MISO /-------------X-----------\/------------X-------------\/-----------
  293. * MOSO \-------------X-----------/\------------X-------------/\-----------
  294. *
  295. * Input Parameters:
  296. * dev - Device-specific state data
  297. * lock - true: Lock spi bus, false: unlock SPI bus
  298. *
  299. * Returned Value:
  300. * None
  301. *
  302. ****************************************************************************/
  303. #ifndef SPI_BITBANG_DISABLEMODE0
  304. static uint16_t spi_bitexchange0(uint16_t dataout, uint32_t holdtime)
  305. {
  306. uint16_t datain;
  307. /* Here the clock is is in the resting set (low). Set MOSI output and wait
  308. * for the hold time
  309. */
  310. if (dataout != 0)
  311. {
  312. SPI_SETMOSI;
  313. }
  314. else
  315. {
  316. SPI_CLRMOSI;
  317. }
  318. spi_delay(holdtime);
  319. /* Set the clock high and sample MISO */
  320. SPI_SETSCK;
  321. datain = (uint16_t)SPI_GETMISO;
  322. /* Wait the required amount of hold time then put the clock back in the
  323. * resting state.
  324. */
  325. spi_delay(holdtime);
  326. SPI_CLRSCK;
  327. return datain;
  328. }
  329. #endif
  330. /****************************************************************************
  331. * Name: spi_bitexchange1
  332. *
  333. * Description:
  334. * Exchange one bit in mode 1
  335. *
  336. * MODE 1: CPOL=0 and CPHA = 1
  337. * The base value of the clock is zero. Data is captured on the clock's
  338. * falling edge and data is propagated on the rising edge
  339. *
  340. * /CS --+
  341. * |
  342. * +-----------------------------------------------------------------
  343. * <-hold time-> <-hold time-><-hold time-><-hold time-><-hold time->
  344. * +------------+ +------------+
  345. * | | | |
  346. * SCLK -+ +------------+ +------------
  347. * `- Set MOSI | `- Set MOSI |
  348. * `- Sample MISO `- Sample MISO
  349. *
  350. * MISO /-----------X------------\/-----------X-------------\/------------
  351. * MOSO \-----------X------------/\-----------X-------------/\------------
  352. *
  353. * Input Parameters:
  354. * dev - Device-specific state data
  355. * lock - true: Lock spi bus, false: unlock SPI bus
  356. *
  357. * Returned Value:
  358. * None
  359. *
  360. ****************************************************************************/
  361. #ifndef SPI_BITBANG_DISABLEMODE1
  362. static uint16_t spi_bitexchange1(uint16_t dataout, uint32_t holdtime)
  363. {
  364. uint16_t datain;
  365. /* The clock should be in the resting state (low). Set the clock to the
  366. * high state and set MOSI.
  367. */
  368. SPI_SETSCK;
  369. if (dataout != 0)
  370. {
  371. SPI_SETMOSI;
  372. }
  373. else
  374. {
  375. SPI_CLRMOSI;
  376. }
  377. /* Wait for the required hold time then put the clock back into the
  378. * resting (low) state.
  379. */
  380. spi_delay(holdtime);
  381. SPI_CLRSCK;
  382. /* Sample MISO on the falling edge and wait for the hold time again */
  383. datain = (uint16_t)SPI_GETMISO;
  384. spi_delay(holdtime);
  385. return datain;
  386. }
  387. #endif
  388. /****************************************************************************
  389. * Name: spi_bitexchange2
  390. *
  391. * Description:
  392. * Exchange one bit in mode 2
  393. *
  394. * MODE 2: CPOL=1 and CPHA = 0
  395. * The base value of the clock is one. Data is captured on the clock's
  396. * falling edge and data is propagated on the rising edge.
  397. *
  398. * /CS --+
  399. * |
  400. * +-----------------------------------------------------------------
  401. * <-hold time-> <-hold time-><-hold time-><-hold time-><-hold time->
  402. * ---------------+ +------------+ +------------
  403. * | | | |
  404. * SCLK +------------+ +------------+
  405. * `- Set MOSI | `- Set MOSI | `- Set MOSI
  406. * `- Sample MISO `- Sample MISO
  407. *
  408. * MISO /-------------X------------\/-----------X-------------\/-----------
  409. * MOSO \-------------X------------/\-----------X-------------/\-----------
  410. *
  411. * Input Parameters:
  412. * dev - Device-specific state data
  413. * lock - true: Lock spi bus, false: unlock SPI bus
  414. *
  415. * Returned Value:
  416. * None
  417. *
  418. ****************************************************************************/
  419. #ifndef SPI_BITBANG_DISABLEMODE2
  420. static uint16_t spi_bitexchange2(uint16_t dataout, uint32_t holdtime)
  421. {
  422. uint16_t datain;
  423. /* Here the clock is is in the resting set (high). Set MOSI output and
  424. * wait for the hold time
  425. */
  426. if (dataout != 0)
  427. {
  428. SPI_SETMOSI;
  429. }
  430. else
  431. {
  432. SPI_CLRMOSI;
  433. }
  434. spi_delay(holdtime);
  435. /* Set the clock low and sample MISO */
  436. SPI_CLRSCK;
  437. datain = (uint16_t)SPI_GETMISO;
  438. /* Wait the required amount of hold time then put the clock back in the
  439. * resting state (high).
  440. */
  441. spi_delay(holdtime);
  442. SPI_SETSCK;
  443. return datain;
  444. }
  445. #endif
  446. /****************************************************************************
  447. * Name: spi_bitexchange3
  448. *
  449. * Description:
  450. * Exchange one bit in mode 3
  451. *
  452. * MODE 3: CPOL=1 and CPHA = 1
  453. * The base value of the clock is one. Data is captured on the clock's
  454. * rising edge and data is propagated on the falling edge.
  455. *
  456. * /CS --+
  457. * |
  458. * +-----------------------------------------------------------------
  459. * <-hold time-> <-hold time-><-hold time-><-hold time-><-hold time->
  460. * -+ +------------+ +------------
  461. * | | | |
  462. * SCLK +------------+ +------------+
  463. * ` Set MOSI | `- Set MOSI |
  464. * `- Sample MISO `- Sample MISO
  465. *
  466. * MISO /-----------X------------\/-----------X-------------\/------------
  467. * MOSO \-----------X------------/\-----------X-------------/\------------
  468. *
  469. * Input Parameters:
  470. * dev - Device-specific state data
  471. * lock - true: Lock spi bus, false: unlock SPI bus
  472. *
  473. * Returned Value:
  474. * None
  475. *
  476. ****************************************************************************/
  477. #ifndef SPI_BITBANG_DISABLEMODE3
  478. static uint16_t spi_bitexchange3(uint16_t dataout, uint32_t holdtime)
  479. {
  480. uint16_t datain;
  481. /* The clock should be in the resting state (high). Set the clock to the
  482. * low state and set MOSI.
  483. */
  484. SPI_CLRSCK; /* Clock transition before setting MOSI */
  485. if (dataout != 0)
  486. {
  487. SPI_SETMOSI; /* Set MOSI if the bit is set */
  488. }
  489. else
  490. {
  491. SPI_CLRMOSI; /* Clear MOSI if the bit is not set */
  492. }
  493. /* Wait for the required hold time then put the clock back into the
  494. * resting (high) state.
  495. */
  496. spi_delay(holdtime);
  497. SPI_SETSCK;
  498. /* Sample MISO on the rising edge and wait for the hold time again */
  499. datain = (uint16_t)SPI_GETMISO;
  500. spi_delay(holdtime);
  501. return datain;
  502. }
  503. #endif
  504. /****************************************************************************
  505. * Name: spi_exchange
  506. *
  507. * Description:
  508. * Exahange one word of data on SPI
  509. *
  510. * Input Parameters:
  511. * priv - Device-specific state data
  512. * data - The TX data to be exchanged with the slave
  513. *
  514. * Returned Value:
  515. * The RX data received from the slave
  516. *
  517. ****************************************************************************/
  518. #ifdef CONFIG_SPI_BITBANG_VARWIDTH
  519. static uint16_t spi_exchange(FAR struct spi_bitbang_s *priv,
  520. uint16_t dataout)
  521. {
  522. bitexchange_t exchange = priv->exchange;
  523. uint32_t holdtime = priv->holdtime;
  524. uint16_t datain;
  525. uint16_t bit;
  526. int shift;
  527. /* Transfer each bit. This might be better done with straight-line
  528. * logic because the loop overhead will limit our maximum transfer
  529. * rate.
  530. */
  531. shift = priv->nbits - 1;
  532. for (bit = 1 << shift; bit != 0; bit >>= 1)
  533. {
  534. /* Shift to make space for the next, less significant bit.
  535. * Then exchange bits with the slave an OR in the new, returned
  536. * bit.
  537. */
  538. datain <<= 1;
  539. datain |= exchange(dataout & bit, holdtime);
  540. }
  541. return datain;
  542. }
  543. #else
  544. static uint16_t spi_exchange(FAR struct spi_bitbang_s *priv,
  545. uint16_t dataout)
  546. {
  547. bitexchange_t exchange = priv->exchange;
  548. uint32_t holdtime = priv->holdtime;
  549. uint8_t datain;
  550. /* Transfer each bit. This is better done with straight-line logic
  551. * when possible because the loop overhead will limit our maximum transfer
  552. * rate.
  553. */
  554. /* Exchange bit 7 with the slave */
  555. datain = priv->exchange(dataout & (1 << 7), holdtime);
  556. /* Exchange bit 6 with the slave */
  557. datain <<= 1;
  558. datain |= priv->exchange(dataout & (1 << 6), holdtime);
  559. /* Exchange bit 5 with the slave */
  560. datain <<= 1;
  561. datain |= priv->exchange(dataout & (1 << 5), holdtime);
  562. /* Exchange bit 4 with the slave */
  563. datain <<= 1;
  564. datain |= priv->exchange(dataout & (1 << 4), holdtime);
  565. /* Exchange bit 3 with the slave */
  566. datain <<= 1;
  567. datain |= priv->exchange(dataout & (1 << 3), holdtime);
  568. /* Exchange bit 2 with the slave */
  569. datain <<= 1;
  570. datain |= priv->exchange(dataout & (1 << 2), holdtime);
  571. /* Exchange bit 1 with the slave */
  572. datain <<= 1;
  573. datain |= priv->exchange(dataout & (1 << 1), holdtime);
  574. /* Exchange bit 0 with the slave */
  575. datain <<= 1;
  576. datain |= priv->exchange(dataout & (1 << 0), holdtime);
  577. return datain;
  578. }
  579. #endif