spibus1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software 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/avr32/dev/spibus1.c
  36. * \brief Primary AVR32 SPI bus controller.
  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. * \verbatim
  42. * $Id: spibus1.c,v 1.3 2009/01/30 08:56:39 haraldkipp Exp $
  43. * \endverbatim
  44. */
  45. #include <cfg/spi.h>
  46. #include <arch/avr32.h>
  47. #include <arch/avr32/gpio.h>
  48. #include <dev/spibus_avr32.h>
  49. #include <dev/irqreg.h>
  50. #include <dev/gpio.h>
  51. #include <sys/event.h>
  52. #include <sys/nutdebug.h>
  53. #include <stdlib.h>
  54. #include <errno.h>
  55. #include <avr32/io.h>
  56. #if defined(AVR32_SPI1_SCK_0_0_PIN)
  57. #define AVR32_SPI1_SCK_PIN AVR32_SPI1_SCK_0_0_PIN
  58. #define AVR32_SPI1_SCK_FUNCTION AVR32_SPI1_SCK_0_0_FUNCTION
  59. #define AVR32_SPI1_MISO_PIN AVR32_SPI1_MISO_0_0_PIN
  60. #define AVR32_SPI1_MISO_FUNCTION AVR32_SPI1_MISO_0_0_FUNCTION
  61. #define AVR32_SPI1_MOSI_PIN AVR32_SPI1_MOSI_0_0_PIN
  62. #define AVR32_SPI1_MOSI_FUNCTION AVR32_SPI1_MOSI_0_0_FUNCTION
  63. #define AVR32_SPI1_NPCS_PIN AVR32_SPI1_NPCS_0_0_PIN
  64. #define AVR32_SPI1_NPCS_FUNCTION AVR32_SPI1_NPCS_0_0_FUNCTION
  65. #elif defined(AVR32_SPI1_SCK_0_PIN)
  66. #define AVR32_SPI1_SCK_PIN AVR32_SPI1_SCK_0_PIN
  67. #define AVR32_SPI1_SCK_FUNCTION AVR32_SPI1_SCK_0_FUNCTION
  68. #define AVR32_SPI1_MISO_PIN AVR32_SPI1_MISO_0_PIN
  69. #define AVR32_SPI1_MISO_FUNCTION AVR32_SPI1_MISO_0_FUNCTION
  70. #define AVR32_SPI1_MOSI_PIN AVR32_SPI1_MOSI_0_PIN
  71. #define AVR32_SPI1_MOSI_FUNCTION AVR32_SPI1_MOSI_0_FUNCTION
  72. #define AVR32_SPI1_NPCS_PIN AVR32_SPI1_NPCS_0_PIN
  73. #define AVR32_SPI1_NPCS_FUNCTION AVR32_SPI1_NPCS_0_FUNCTION
  74. #endif
  75. #if defined(SPI1_CS0_PIO_BIT)
  76. #if defined(SPI1_CS0_PIO_ID)
  77. #undef GPIO_ID
  78. #define GPIO_ID SPI1_CS0_PIO_ID
  79. #include <cfg/arch/porttran.h>
  80. static INLINE void SPI1_CS0_LO(void)
  81. {
  82. GPIO_SET_LO(SPI1_CS0_PIO_BIT);
  83. }
  84. static INLINE void SPI1_CS0_HI(void)
  85. {
  86. GPIO_SET_HI(SPI1_CS0_PIO_BIT);
  87. }
  88. static INLINE void SPI1_CS0_SO(void)
  89. {
  90. GPIO_ENABLE(SPI1_CS0_PIO_BIT);
  91. GPIO_OUTPUT(SPI1_CS0_PIO_BIT);
  92. }
  93. #else
  94. #define SPI1_CS0_LO()
  95. #define SPI1_CS0_HI()
  96. #define SPI1_CS0_SO()
  97. #endif
  98. #endif
  99. #if defined(SPI1_CS1_PIO_BIT)
  100. #if defined(SPI1_CS1_PIO_ID)
  101. #undef GPIO_ID
  102. #define GPIO_ID SPI1_CS1_PIO_ID
  103. #include <cfg/arch/porttran.h>
  104. static INLINE void SPI1_CS1_LO(void)
  105. {
  106. GPIO_SET_LO(SPI1_CS1_PIO_BIT);
  107. }
  108. static INLINE void SPI1_CS1_HI(void)
  109. {
  110. GPIO_SET_HI(SPI1_CS1_PIO_BIT);
  111. }
  112. static INLINE void SPI1_CS1_SO(void)
  113. {
  114. GPIO_ENABLE(SPI1_CS1_PIO_BIT);
  115. GPIO_OUTPUT(SPI1_CS1_PIO_BIT);
  116. }
  117. #else
  118. #define SPI1_CS1_LO()
  119. #define SPI1_CS1_HI()
  120. #define SPI1_CS1_SO()
  121. #endif
  122. #endif
  123. #if defined(SPI1_CS2_PIO_BIT)
  124. #if defined(SPI1_CS2_PIO_ID)
  125. #undef GPIO_ID
  126. #define GPIO_ID SPI1_CS2_PIO_ID
  127. #include <cfg/arch/porttran.h>
  128. static INLINE void SPI1_CS2_LO(void)
  129. {
  130. GPIO_SET_LO(SPI1_CS2_PIO_BIT);
  131. }
  132. static INLINE void SPI1_CS2_HI(void)
  133. {
  134. GPIO_SET_HI(SPI1_CS2_PIO_BIT);
  135. }
  136. static INLINE void SPI1_CS2_SO(void)
  137. {
  138. GPIO_ENABLE(SPI1_CS2_PIO_BIT);
  139. GPIO_OUTPUT(SPI1_CS2_PIO_BIT);
  140. }
  141. #else
  142. #define SPI1_CS2_LO()
  143. #define SPI1_CS2_HI()
  144. #define SPI1_CS2_SO()
  145. #endif
  146. #endif
  147. #if defined(SPI1_CS3_PIO_BIT)
  148. #if defined(SPI1_CS3_PIO_ID)
  149. #undef GPIO_ID
  150. #define GPIO_ID SPI1_CS3_PIO_ID
  151. #include <cfg/arch/porttran.h>
  152. static INLINE void SPI1_CS3_LO(void)
  153. {
  154. GPIO_SET_LO(SPI1_CS3_PIO_BIT);
  155. }
  156. static INLINE void SPI1_CS3_HI(void)
  157. {
  158. GPIO_SET_HI(SPI1_CS3_PIO_BIT);
  159. }
  160. static INLINE void SPI1_CS3_SO(void)
  161. {
  162. GPIO_ENABLE(SPI1_CS3_PIO_BIT);
  163. GPIO_OUTPUT(SPI1_CS3_PIO_BIT);
  164. }
  165. #else
  166. #define SPI1_CS3_LO()
  167. #define SPI1_CS3_HI()
  168. #define SPI1_CS3_SO()
  169. #endif
  170. #endif
  171. /*!
  172. * \brief Set the specified chip select to a given level.
  173. */
  174. int Avr32Spi1ChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  175. {
  176. int rc = 0;
  177. switch (cs) {
  178. #if defined(SPI1_CS0_PIO_BIT)
  179. case 0:
  180. if (hi) {
  181. SPI1_CS0_HI();
  182. } else {
  183. SPI1_CS0_LO();
  184. }
  185. SPI1_CS0_SO();
  186. break;
  187. #endif
  188. #if defined(SPI1_CS1_PIO_BIT)
  189. case 1:
  190. if (hi) {
  191. SPI1_CS1_HI();
  192. } else {
  193. SPI1_CS1_LO();
  194. }
  195. SPI1_CS1_SO();
  196. break;
  197. #endif
  198. #if defined(SPI1_CS2_PIO_BIT)
  199. case 2:
  200. if (hi) {
  201. SPI1_CS2_HI();
  202. } else {
  203. SPI1_CS2_LO();
  204. }
  205. SPI1_CS2_SO();
  206. break;
  207. #endif
  208. #if defined(SPI1_CS3_PIO_BIT)
  209. case 3:
  210. if (hi) {
  211. SPI1_CS3_HI();
  212. } else {
  213. SPI1_CS3_LO();
  214. }
  215. SPI1_CS3_SO();
  216. break;
  217. #endif
  218. default:
  219. errno = EIO;
  220. rc = -1;
  221. break;
  222. }
  223. return rc;
  224. }
  225. /*! \brief Select a device on the first SPI bus.
  226. *
  227. * Locks and activates the bus for the specified node.
  228. *
  229. * \param node Specifies the SPI bus node.
  230. * \param tmo Timeout in milliseconds. To disable timeout, set this
  231. * parameter to NUT_WAIT_INFINITE.
  232. *
  233. * \return 0 on success. In case of an error, -1 is returned and the bus
  234. * is not locked.
  235. */
  236. int Avr32SpiBus1Select(NUTSPINODE * node, uint32_t tmo)
  237. {
  238. int rc;
  239. /* Sanity check. */
  240. NUTASSERT(node != NULL);
  241. NUTASSERT(node->node_bus != NULL);
  242. NUTASSERT(node->node_stat != NULL);
  243. /* Allocate the bus. */
  244. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  245. if (rc) {
  246. errno = EIO;
  247. } else {
  248. AVR32SPIREG *spireg = node->node_stat;
  249. /* Enable SPI peripherals and clock. */
  250. gpio_enable_module_pin(AVR32_SPI1_SCK_PIN, AVR32_SPI1_SCK_FUNCTION);
  251. gpio_enable_module_pin(AVR32_SPI1_MISO_PIN, AVR32_SPI1_MISO_FUNCTION);
  252. gpio_enable_module_pin(AVR32_SPI1_MOSI_PIN, AVR32_SPI1_MOSI_FUNCTION);
  253. gpio_enable_module_pin(AVR32_SPI1_NPCS_PIN, AVR32_SPI1_NPCS_FUNCTION);
  254. /* If the mode update bit is set, then update our registers. */
  255. if (node->node_mode & SPI_MODE_UPDATE) {
  256. Avr32SpiSetup(node);
  257. }
  258. /* Set SPI mode. */
  259. outr(AVR32_SPI1_ADDRESS + AVR32_SPI_MR, spireg->mr);
  260. //outr(AVR32_SPI1_ADDRESS + AVR32_SPI_CSR0 + (node->node_cs * (AVR32_SPI_CSR1 - AVR32_SPI_CSR0)), spireg->csr);
  261. outr(AVR32_SPI1_ADDRESS + AVR32_SPI_CSR0, spireg->csr);
  262. /* Enable SPI. */
  263. AVR32_SPI1.cr |= AVR32_SPI_CR_SPIEN_MASK;
  264. /* Finally activate the node's chip select. */
  265. rc = Avr32Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  266. if (rc) {
  267. /* Release the bus in case of an error. */
  268. NutEventPost(&node->node_bus->bus_mutex);
  269. }
  270. }
  271. return rc;
  272. }
  273. /*! \brief Deselect a device on the first SPI bus.
  274. *
  275. * Deactivates the chip select and unlocks the bus.
  276. *
  277. * \param node Specifies the SPI bus node.
  278. *
  279. * \return Always 0.
  280. */
  281. int Avr32SpiBus1Deselect(NUTSPINODE * node)
  282. {
  283. /* Sanity check. */
  284. NUTASSERT(node != NULL);
  285. NUTASSERT(node->node_bus != NULL);
  286. #ifdef SPIBUS1_DOUBLE_BUFFER
  287. Avr32SpiBusWait(node, NUT_WAIT_INFINITE);
  288. #endif
  289. /* Deactivate the node's chip select. */
  290. Avr32Spi1ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  291. /* Release the bus. */
  292. NutEventPost(&node->node_bus->bus_mutex);
  293. return 0;
  294. }
  295. #if !defined(SPIBUS1_POLLING_MODE) || !defined(SPIBUS1_DOUBLE_BUFFER)
  296. static uint8_t *volatile spi1_txp;
  297. static uint8_t *volatile spi1_rxp;
  298. static volatile size_t spi1_xc;
  299. void Avr32SpiBus1Interrupt(void *arg)
  300. {
  301. uint8_t b;
  302. /* Get the received byte. */
  303. b = (uint8_t) inr(AVR32_SPI1_ADDRESS + AVR32_SPI_RDR) >> AVR32_SPI_RDR_RD_OFFSET;
  304. if (spi1_xc) {
  305. if (spi1_rxp) {
  306. *spi1_rxp++ = b;
  307. }
  308. spi1_xc--;
  309. }
  310. if (spi1_xc) {
  311. if (spi1_txp) {
  312. b = *spi1_txp++;
  313. }
  314. outr(AVR32_SPI1_ADDRESS + AVR32_SPI_TDR, (b << AVR32_SPI_TDR_TD_OFFSET));
  315. } else {
  316. NutEventPostFromIrq((void **) arg);
  317. }
  318. }
  319. /*!
  320. * \brief Transfer data on the SPI bus using single buffered interrupt mode.
  321. *
  322. * A device must have been selected by calling Avr32SpiSelect().
  323. *
  324. * \param node Specifies the SPI bus node.
  325. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  326. * byte values are transmitted.
  327. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  328. * data is discarded.
  329. * \param xlen Number of bytes to transfer.
  330. *
  331. * \return Always 0.
  332. */
  333. int Avr32SpiBus1Transfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  334. {
  335. uint16_t b = 0xff;
  336. uintptr_t base;
  337. /* Sanity check. */
  338. NUTASSERT(node != NULL);
  339. NUTASSERT(node->node_bus != NULL);
  340. NUTASSERT(node->node_bus->bus_base != 0);
  341. base = node->node_bus->bus_base;
  342. if (xlen) {
  343. if ( txbuf ) {
  344. if ( node->node_bits == 16 ) {
  345. b = *(uint16_t *) txbuf;
  346. }
  347. else if ( node->node_bits == 8 ) {
  348. b = *(uint8_t *)txbuf;
  349. }
  350. }
  351. /* Enable and kick interrupts. */
  352. outr(base + AVR32_SPI_IER, AVR32_SPI_IER_RDRF_MASK);
  353. if (node->node_bits == 8) {
  354. outr(base + AVR32_SPI_TDR, (b << AVR32_SPI_TDR_TD_OFFSET));
  355. }
  356. else if (node->node_bits == 16) {
  357. outr(base + AVR32_SPI_TDR, b);
  358. }
  359. /* Wait until transfer has finished. */
  360. NutEventWait(&node->node_bus->bus_ready, NUT_WAIT_INFINITE);
  361. /* Wait for data is send */
  362. while ((inr(base + AVR32_SPI_SR) & AVR32_SPI_TXEMPTY_MASK) == 0);
  363. /* Read incoming data. */
  364. if (node->node_bits == 8) {
  365. b = (uint8_t) inr(base + AVR32_SPI_RDR) >> AVR32_SPI_RDR_RD_OFFSET;
  366. }
  367. else if (node->node_bits == 16) {
  368. b = (uint16_t) inr(base + AVR32_SPI_RDR);
  369. }
  370. if (rxbuf) {
  371. if (node->node_bits == 8) {
  372. *(uint8_t *)rxbuf++ = b;
  373. }
  374. else if ( node->node_bits == 16) {
  375. *(uint16_t *)rxbuf++ = b;
  376. }
  377. }
  378. outr(base + AVR32_SPI_IDR, (unsigned int) -1);
  379. }
  380. return 0;
  381. }
  382. #endif
  383. /*!
  384. * \brief Avr32 SPI bus driver implementation structure.
  385. */
  386. NUTSPIBUS spiBus1Avr32 = {
  387. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  388. NULL, /*!< Bus ready signal (bus_ready). */
  389. AVR32_SPI1_ADDRESS, /*!< Bus base address (bus_base). */
  390. &sig_SPI1, /*!< Bus interrupt handler (bus_sig). */
  391. Avr32SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  392. Avr32SpiBus1Select, /*!< Select the specified device (bus_alloc). */
  393. Avr32SpiBus1Deselect, /*!< Deselect the specified device (bus_release). */
  394. #if defined(SPIBUS1_POLLING_MODE)
  395. Avr32SpiBusPollTransfer, /*!< Transfer data to and from a specified node (bus_transfer). */
  396. #elif defined(SPIBUS1_DOUBLE_BUFFER)
  397. Avr32SpiBusDblBufTransfer,
  398. #else
  399. Avr32SpiBus1Transfer,
  400. #endif
  401. #ifdef SPIBUS1_DOUBLE_BUFFER
  402. Avr32SpiBusWait,
  403. #else
  404. NutSpiBusWait, /*!< Wait for bus transfer ready (bus_wait). */
  405. #endif
  406. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  407. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  408. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  409. };