spibus0avr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2008-2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/avr/dev/spibus0avr.c
  36. * \brief AVR SPI bus 0 driver.
  37. *
  38. * May be configured as an interrupt driven or polling driver. The
  39. * interrupt driven version may use single or double buffering.
  40. *
  41. * Single buffer interrupt mode is the default.
  42. *
  43. * Polling mode requires about 300 bytes less code and is roughly 10%
  44. * faster. However, it blocks the CPU during the whole transfer.
  45. *
  46. * Double buffering additionally requires about 200 bytes of code space
  47. * and is only slightly faster (less than 1%).
  48. *
  49. * \verbatim
  50. * $Id: spibus0avr.c 4937 2013-01-22 11:38:42Z haraldkipp $
  51. * \endverbatim
  52. */
  53. #include <cfg/spi.h>
  54. #include <cfg/arch/gpio.h>
  55. #include <dev/spibus_avr.h>
  56. #include <dev/irqreg.h>
  57. #include <sys/event.h>
  58. #include <sys/nutdebug.h>
  59. #include <errno.h>
  60. #include <stdlib.h>
  61. #include <memdebug.h>
  62. #if defined(SPI0_CS0_PIO_BIT)
  63. #if defined(SPI0_CS0_PIO_ID)
  64. #undef GPIO_ID
  65. #define GPIO_ID SPI0_CS0_PIO_ID
  66. #include <cfg/arch/porttran.h>
  67. static INLINE void SPI0_CS0_LO(void) { GPIO_SET_LO(SPI0_CS0_PIO_BIT); }
  68. static INLINE void SPI0_CS0_HI(void) { GPIO_SET_HI(SPI0_CS0_PIO_BIT); }
  69. static INLINE void SPI0_CS0_SO(void) { GPIO_OUTPUT(SPI0_CS0_PIO_BIT); }
  70. #else
  71. #define SPI0_CS0_LO()
  72. #define SPI0_CS0_HI()
  73. #define SPI0_CS0_SO()
  74. #endif
  75. #endif
  76. #if defined(SPI0_CS1_PIO_BIT)
  77. #if defined(SPI0_CS1_PIO_ID)
  78. #undef GPIO_ID
  79. #define GPIO_ID SPI0_CS1_PIO_ID
  80. #include <cfg/arch/porttran.h>
  81. static INLINE void SPI0_CS1_LO(void) { GPIO_SET_LO(SPI0_CS1_PIO_BIT); }
  82. static INLINE void SPI0_CS1_HI(void) { GPIO_SET_HI(SPI0_CS1_PIO_BIT); }
  83. static INLINE void SPI0_CS1_SO(void) { GPIO_OUTPUT(SPI0_CS1_PIO_BIT); }
  84. #else
  85. #define SPI0_CS1_LO()
  86. #define SPI0_CS1_HI()
  87. #define SPI0_CS1_SO()
  88. #endif
  89. #endif
  90. #if defined(SPI0_CS2_PIO_BIT)
  91. #if defined(SPI0_CS2_PIO_ID)
  92. #undef GPIO_ID
  93. #define GPIO_ID SPI0_CS2_PIO_ID
  94. #include <cfg/arch/porttran.h>
  95. static INLINE void SPI0_CS2_LO(void) { GPIO_SET_LO(SPI0_CS2_PIO_BIT); }
  96. static INLINE void SPI0_CS2_HI(void) { GPIO_SET_HI(SPI0_CS2_PIO_BIT); }
  97. static INLINE void SPI0_CS2_SO(void) { GPIO_OUTPUT(SPI0_CS2_PIO_BIT); }
  98. #else
  99. #define SPI0_CS2_LO()
  100. #define SPI0_CS2_HI()
  101. #define SPI0_CS2_SO()
  102. #endif
  103. #endif
  104. #if defined(SPI0_CS3_PIO_BIT)
  105. #if defined(SPI0_CS3_PIO_ID)
  106. #undef GPIO_ID
  107. #define GPIO_ID SPI0_CS3_PIO_ID
  108. #include <cfg/arch/porttran.h>
  109. static INLINE void SPI0_CS3_LO(void) { GPIO_SET_LO(SPI0_CS3_PIO_BIT); }
  110. static INLINE void SPI0_CS3_HI(void) { GPIO_SET_HI(SPI0_CS3_PIO_BIT); }
  111. static INLINE void SPI0_CS3_SO(void) { GPIO_OUTPUT(SPI0_CS3_PIO_BIT); }
  112. #else
  113. #define SPI0_CS3_LO()
  114. #define SPI0_CS3_HI()
  115. #define SPI0_CS4_SO()
  116. #endif
  117. #endif
  118. /*!
  119. * \brief Set the specified chip select to a given level.
  120. */
  121. static int AvrSpi0ChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  122. {
  123. int rc = 0;
  124. switch (cs) {
  125. #if defined(SPI0_CS0_PIO_BIT)
  126. case 0:
  127. if (hi) {
  128. SPI0_CS0_HI();
  129. } else {
  130. SPI0_CS0_LO();
  131. }
  132. SPI0_CS0_SO();
  133. break;
  134. #endif
  135. #if defined(SPI0_CS1_PIO_BIT)
  136. case 1:
  137. if (hi) {
  138. SPI0_CS1_HI();
  139. } else {
  140. SPI0_CS1_LO();
  141. }
  142. SPI0_CS1_SO();
  143. break;
  144. #endif
  145. #if defined(SPI0_CS2_PIO_BIT)
  146. case 2:
  147. if (hi) {
  148. SPI0_CS2_HI();
  149. } else {
  150. SPI0_CS2_LO();
  151. }
  152. SPI0_CS2_SO();
  153. break;
  154. #endif
  155. #if defined(SPI0_CS3_PIO_BIT)
  156. case 3:
  157. if (hi) {
  158. SPI0_CS3_HI();
  159. } else {
  160. SPI0_CS3_LO();
  161. }
  162. SPI0_CS3_SO();
  163. break;
  164. #endif
  165. default:
  166. errno = EIO;
  167. rc = -1;
  168. break;
  169. }
  170. return rc;
  171. }
  172. static uint8_t * volatile spi0_txp;
  173. static uint8_t * volatile spi0_rxp;
  174. #ifndef SPIBUS0_POLLING_MODE
  175. static HANDLE spi0_que;
  176. static volatile size_t spi0_xc;
  177. #ifdef SPIBUS0_DOUBLE_BUFFER
  178. /* Buffers used for double buffering. */
  179. static uint8_t * volatile spi0_ntxp;
  180. static uint8_t * volatile spi0_nrxp;
  181. static volatile size_t spi0_nxc;
  182. #endif
  183. /*!
  184. * \brief AVR SPI interrupt handler.
  185. */
  186. static void AvrSpi0Interrupt(void *arg)
  187. {
  188. uint8_t b;
  189. /* Get the received byte. */
  190. b = inb(SPDR);
  191. if (spi0_xc) {
  192. if (spi0_rxp) {
  193. *spi0_rxp++ = b;
  194. }
  195. spi0_xc--;
  196. }
  197. if (spi0_xc == 0) {
  198. #ifdef SPIBUS0_DOUBLE_BUFFER
  199. if (spi0_nxc) {
  200. /* More data in secondary buffer. */
  201. spi0_txp = spi0_ntxp;
  202. spi0_rxp = spi0_nrxp;
  203. spi0_xc = spi0_nxc;
  204. spi0_nxc = 0;
  205. }
  206. #endif
  207. NutEventPostFromIrq(&spi0_que);
  208. }
  209. if (spi0_xc) {
  210. if (spi0_txp) {
  211. b = *spi0_txp++;
  212. }
  213. outb(SPDR, b);
  214. }
  215. }
  216. #endif /* SPIBUS0_POLLING_MODE */
  217. /*!
  218. * \brief Transfer data on the SPI bus.
  219. *
  220. * A device must have been selected by calling AvrSpi0Select().
  221. *
  222. * Depending on the configuration, this routine implemets polling or
  223. * interrupt mode. For the latter either single or double buffering
  224. * may have been selected.
  225. *
  226. * When using double buffered interrupt mode, then the transfer may
  227. * be still in progress when returning from this function.
  228. *
  229. * \param node Specifies the SPI bus node.
  230. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  231. * byte values are transmitted.
  232. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  233. * data is discarded.
  234. * \param xlen Number of bytes to transfer.
  235. *
  236. * \return Always 0.
  237. */
  238. int AvrSpiBus0Transfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  239. {
  240. uint8_t b = 0xff;
  241. /* Sanity check. */
  242. NUTASSERT(node != NULL);
  243. #ifdef SPIBUS0_POLLING_MODE
  244. /*
  245. * Polling mode.
  246. */
  247. spi0_txp = (uint8_t *) txbuf;
  248. spi0_rxp = (uint8_t *) rxbuf;
  249. while (xlen--) {
  250. if (spi0_txp) {
  251. b = *spi0_txp++;
  252. }
  253. outb(SPDR, b);
  254. while ((inb(SPSR) & 0x80) == 0);
  255. b = inb(SPDR);
  256. if (spi0_rxp) {
  257. *spi0_rxp++ = b;
  258. }
  259. }
  260. #else /* SPIBUS0_POLLING_MODE */
  261. if (xlen) {
  262. #ifdef SPIBUS0_DOUBLE_BUFFER
  263. /*
  264. * Double buffer interrupt mode.
  265. */
  266. cbi(SPCR, SPIE);
  267. /* Wait until secondary buffer is available. */
  268. while (spi0_nxc) {
  269. sbi(SPCR, SPIE);
  270. NutEventWait(&spi0_que, NUT_WAIT_INFINITE);
  271. cbi(SPCR, SPIE);
  272. }
  273. if (spi0_xc) {
  274. /* Primary buffer in use. Prepare secondary buffer. */
  275. spi0_ntxp = (uint8_t *) txbuf;
  276. spi0_nrxp = (uint8_t *) rxbuf;
  277. spi0_nxc = (size_t) xlen;
  278. sbi(SPCR, SPIE);
  279. } else {
  280. spi0_txp = (uint8_t *) txbuf;
  281. spi0_rxp = (uint8_t *) rxbuf;
  282. spi0_xc = (size_t) xlen;
  283. if (spi0_txp) {
  284. b = *spi0_txp++;
  285. }
  286. /* Enable and kick interrupts. */
  287. sbi(SPCR, SPIE);
  288. outb(SPDR, b);
  289. }
  290. #else /* SPIBUS0_DOUBLE_BUFFER */
  291. /*
  292. * Single buffer interrupt mode.
  293. */
  294. spi0_txp = (uint8_t *) txbuf;
  295. spi0_rxp = (uint8_t *) rxbuf;
  296. spi0_xc = (size_t) xlen;
  297. if (spi0_txp) {
  298. b = *spi0_txp++;
  299. }
  300. /* Enable and kick interrupts. */
  301. sbi(SPCR, SPIE);
  302. outb(SPDR, b);
  303. /* Wait until transfer has finished. */
  304. NutEventWait(&spi0_que, NUT_WAIT_INFINITE);
  305. cbi(SPCR, SPIE);
  306. #endif /* SPIBUS0_DOUBLE_BUFFER */
  307. }
  308. #endif /* SPIBUS0_POLLING_MODE */
  309. return 0;
  310. }
  311. #ifdef SPIBUS0_DOUBLE_BUFFER
  312. /*!
  313. * \brief Wait until all SPI bus transfers are done.
  314. *
  315. * \param node Specifies the SPI bus node.
  316. * \param tmo Timeout in milliseconds. To disable timeout, set this
  317. * parameter to NUT_WAIT_INFINITE.
  318. *
  319. * \return Always 0.
  320. */
  321. int AvrSpiBus0Wait(NUTSPINODE * node, uint32_t tmo)
  322. {
  323. cbi(SPCR, SPIE);
  324. while (spi0_xc) {
  325. sbi(SPCR, SPIE);
  326. if (NutEventWait(&spi0_que, tmo)) {
  327. return -1;
  328. }
  329. cbi(SPCR, SPIE);
  330. }
  331. return 0;
  332. }
  333. #endif /* SPIBUS0_DOUBLE_BUFFER */
  334. /*!
  335. * \brief Initialize an SPI bus node.
  336. *
  337. * This routine is called for each SPI node, which is registered via
  338. * NutRegisterSpiDevice().
  339. *
  340. * \param node Specifies the SPI bus node.
  341. *
  342. * \return 0 on success or -1 if there is no valid chip select.
  343. */
  344. int AvrSpiBus0NodeInit(NUTSPINODE * node)
  345. {
  346. int rc;
  347. /* Sanity check. */
  348. NUTASSERT(node != NULL);
  349. /* Try to deactivate the node's chip select. */
  350. rc = AvrSpi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  351. /* It should not hurt us being called more than once. Thus, we
  352. ** check wether any initialization had been taken place already. */
  353. if (rc == 0 && node->node_stat == NULL) {
  354. /* Allocate our shadow registers. */
  355. node->node_stat = malloc(sizeof(AVRSPIREG));
  356. if (node->node_stat) {
  357. AvrSpiSetup(node);
  358. #ifndef SPIBUS0_POLLING_MODE
  359. /* Register and enable SPI interrupt handler. */
  360. NutRegisterIrqHandler(node->node_bus->bus_sig, AvrSpi0Interrupt, NULL);
  361. #endif
  362. } else {
  363. /* Out of memory? */
  364. rc = -1;
  365. }
  366. }
  367. return rc;
  368. }
  369. /*! \brief Select a device on the SPI bus.
  370. *
  371. * Locks and activates the bus for the specified node.
  372. *
  373. * \param node Specifies the SPI bus node.
  374. * \param tmo Timeout in milliseconds. To disable timeout, set this
  375. * parameter to NUT_WAIT_INFINITE.
  376. *
  377. * \return 0 on success. In case of an error, -1 is returned and the bus
  378. * is not locked.
  379. */
  380. int AvrSpiBus0Select(NUTSPINODE * node, uint32_t tmo)
  381. {
  382. int rc;
  383. /* Sanity check. */
  384. NUTASSERT(node != NULL);
  385. NUTASSERT(node->node_stat != NULL);
  386. /* Allocate the bus. */
  387. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  388. if (rc) {
  389. errno = EIO;
  390. } else {
  391. AVRSPIREG *spireg = node->node_stat;
  392. /* If the mode update bit is set, then update our shadow registers. */
  393. if (node->node_mode & SPI_MODE_UPDATE) {
  394. AvrSpiSetup(node);
  395. }
  396. /* Even when set to master mode, the SCK pin is not automatically
  397. ** switched to output. Do it manually, maintaining the polarity. */
  398. if (spireg->avrspi_spcr & _BV(CPOL)) {
  399. cbi(PORTB, 1);
  400. } else {
  401. sbi(PORTB, 1);
  402. }
  403. sbi(DDRB, 1);
  404. /* Also MOSI requires manual setting. */
  405. cbi(PORTB, 2);
  406. sbi(DDRB, 2);
  407. /* When SS is configured as input, we may be forced into slave
  408. ** mode if this pin goes low. Enable the pull-up. */
  409. if (bit_is_clear(DDRB, 0)) {
  410. sbi(PORTB, 0);
  411. }
  412. /* Enable MISO pull-up to avoid floating. */
  413. sbi(PORTB, 3);
  414. /* Set SPI mode for this device, using the shadow registers. */
  415. outb(SPCR, spireg->avrspi_spcr);
  416. #if defined(SPI2X)
  417. outb(SPSR, spireg->avrspi_spsr);
  418. #endif
  419. /* Clean-up the status. */
  420. inb(SPSR);
  421. inb(SPDR);
  422. /* Finally activate the node's chip select. */
  423. rc = AvrSpi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  424. if (rc) {
  425. /* Release the bus in case of an error. */
  426. NutEventPost(&node->node_bus->bus_mutex);
  427. }
  428. }
  429. return rc;
  430. }
  431. /*! \brief Deselect a device on the SPI bus.
  432. *
  433. * Deactivates the chip select and unlocks the bus.
  434. *
  435. * \param node Specifies the SPI bus node.
  436. *
  437. * \return Always 0.
  438. */
  439. int AvrSpiBus0Deselect(NUTSPINODE * node)
  440. {
  441. /* Sanity check. */
  442. NUTASSERT(node != NULL);
  443. NUTASSERT(node->node_bus != NULL);
  444. #ifdef SPIBUS0_DOUBLE_BUFFER
  445. AvrSpiBus0Wait(node, NUT_WAIT_INFINITE);
  446. #endif
  447. /* Deactivate the node's chip select. */
  448. AvrSpi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  449. /* Release the bus. */
  450. NutEventPost(&node->node_bus->bus_mutex);
  451. return 0;
  452. }
  453. /*!
  454. * \brief AVR SPI bus driver implementation structure.
  455. */
  456. NUTSPIBUS spiBus0Avr = {
  457. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  458. NULL, /*!< Bus ready signal (bus_ready). */
  459. 0, /*!< Unused bus base address (bus_base). */
  460. &sig_SPI, /*!< Bus interrupt handler (bus_sig). */
  461. AvrSpiBus0NodeInit, /*!< Initialize the bus (bus_initnode). */
  462. AvrSpiBus0Select, /*!< Select the specified device (bus_alloc). */
  463. AvrSpiBus0Deselect, /*!< Deselect the specified device (bus_release). */
  464. AvrSpiBus0Transfer, /*!< Transfer data to and from a specified device (bus_transfer). */
  465. #ifdef SPIBUS0_DOUBLE_BUFFER
  466. AvrSpiBus0Wait,
  467. #else
  468. NutSpiBusWait, /*!< Wait for bus transfer ready (bus_wait). */
  469. #endif
  470. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  471. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  472. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  473. };