spibus0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/spibus0.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: spibus0.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. #ifndef AVR32_SPI0_ADDRESS
  57. #define AVR32_SPI0_ADDRESS AVR32_SPI_ADDRESS
  58. #endif
  59. #ifndef AVR32_SPI0
  60. #define AVR32_SPI0 AVR32_SPI
  61. #endif
  62. #if defined(AVR32_SPI0_SCK_0_0_PIN)
  63. #define AVR32_SPI0_SCK_PIN AVR32_SPI0_SCK_0_0_PIN
  64. #define AVR32_SPI0_SCK_FUNCTION AVR32_SPI0_SCK_0_0_FUNCTION
  65. #define AVR32_SPI0_MISO_PIN AVR32_SPI0_MISO_0_0_PIN
  66. #define AVR32_SPI0_MISO_FUNCTION AVR32_SPI0_MISO_0_0_FUNCTION
  67. #define AVR32_SPI0_MOSI_PIN AVR32_SPI0_MOSI_0_0_PIN
  68. #define AVR32_SPI0_MOSI_FUNCTION AVR32_SPI0_MOSI_0_0_FUNCTION
  69. #define AVR32_SPI0_NPCS_PIN AVR32_SPI0_NPCS_0_0_PIN
  70. #define AVR32_SPI0_NPCS_FUNCTION AVR32_SPI0_NPCS_0_0_FUNCTION
  71. #elif defined(AVR32_SPI0_SCK_0_PIN)
  72. #define AVR32_SPI0_SCK_PIN AVR32_SPI0_SCK_0_PIN
  73. #define AVR32_SPI0_SCK_FUNCTION AVR32_SPI0_SCK_0_FUNCTION
  74. #define AVR32_SPI0_MISO_PIN AVR32_SPI0_MISO_0_PIN
  75. #define AVR32_SPI0_MISO_FUNCTION AVR32_SPI0_MISO_0_FUNCTION
  76. #define AVR32_SPI0_MOSI_PIN AVR32_SPI0_MOSI_0_PIN
  77. #define AVR32_SPI0_MOSI_FUNCTION AVR32_SPI0_MOSI_0_FUNCTION
  78. #define AVR32_SPI0_NPCS_PIN AVR32_SPI0_NPCS_0_PIN
  79. #define AVR32_SPI0_NPCS_FUNCTION AVR32_SPI0_NPCS_0_FUNCTION
  80. #elif defined(AVR32_SPI_SCK_0_0_PIN)
  81. #define AVR32_SPI0_SCK_PIN AVR32_SPI_SCK_0_0_PIN
  82. #define AVR32_SPI0_SCK_FUNCTION AVR32_SPI_SCK_0_0_FUNCTION
  83. #define AVR32_SPI0_MISO_PIN AVR32_SPI_MISO_0_0_PIN
  84. #define AVR32_SPI0_MISO_FUNCTION AVR32_SPI_MISO_0_0_FUNCTION
  85. #define AVR32_SPI0_MOSI_PIN AVR32_SPI_MOSI_0_0_PIN
  86. #define AVR32_SPI0_MOSI_FUNCTION AVR32_SPI_MOSI_0_0_FUNCTION
  87. #define AVR32_SPI0_NPCS_PIN AVR32_SPI_NPCS_0_0_PIN
  88. #define AVR32_SPI0_NPCS_FUNCTION AVR32_SPI_NPCS_0_0_FUNCTION
  89. #endif
  90. #if defined(SPI0_CS0_PIO_BIT)
  91. #if defined(SPI0_CS0_PIO_ID)
  92. #undef GPIO_ID
  93. #define GPIO_ID SPI0_CS0_PIO_ID
  94. #include <cfg/arch/porttran.h>
  95. static INLINE void SPI0_CS0_LO(void)
  96. {
  97. GPIO_SET_LO(SPI0_CS0_PIO_BIT);
  98. }
  99. static INLINE void SPI0_CS0_HI(void)
  100. {
  101. GPIO_SET_HI(SPI0_CS0_PIO_BIT);
  102. }
  103. static INLINE void SPI0_CS0_SO(void)
  104. {
  105. GPIO_ENABLE(SPI0_CS0_PIO_BIT);
  106. GPIO_OUTPUT(SPI0_CS0_PIO_BIT);
  107. }
  108. #else
  109. #define SPI0_CS0_LO()
  110. #define SPI0_CS0_HI()
  111. #define SPI0_CS0_SO()
  112. #endif
  113. #endif
  114. #if defined(SPI0_CS1_PIO_BIT)
  115. #if defined(SPI0_CS1_PIO_ID)
  116. #undef GPIO_ID
  117. #define GPIO_ID SPI0_CS1_PIO_ID
  118. #include <cfg/arch/porttran.h>
  119. static INLINE void SPI0_CS1_LO(void)
  120. {
  121. GPIO_SET_LO(SPI0_CS1_PIO_BIT);
  122. }
  123. static INLINE void SPI0_CS1_HI(void)
  124. {
  125. GPIO_SET_HI(SPI0_CS1_PIO_BIT);
  126. }
  127. static INLINE void SPI0_CS1_SO(void)
  128. {
  129. GPIO_ENABLE(SPI0_CS1_PIO_BIT);
  130. GPIO_OUTPUT(SPI0_CS1_PIO_BIT);
  131. }
  132. #else
  133. #define SPI0_CS1_LO()
  134. #define SPI0_CS1_HI()
  135. #define SPI0_CS1_SO()
  136. #endif
  137. #endif
  138. #if defined(SPI0_CS2_PIO_BIT)
  139. #if defined(SPI0_CS2_PIO_ID)
  140. #undef GPIO_ID
  141. #define GPIO_ID SPI0_CS2_PIO_ID
  142. #include <cfg/arch/porttran.h>
  143. static INLINE void SPI0_CS2_LO(void)
  144. {
  145. GPIO_SET_LO(SPI0_CS2_PIO_BIT);
  146. }
  147. static INLINE void SPI0_CS2_HI(void)
  148. {
  149. GPIO_SET_HI(SPI0_CS2_PIO_BIT);
  150. }
  151. static INLINE void SPI0_CS2_SO(void)
  152. {
  153. GPIO_ENABLE(SPI0_CS2_PIO_BIT);
  154. GPIO_OUTPUT(SPI0_CS2_PIO_BIT);
  155. }
  156. #else
  157. #define SPI0_CS2_LO()
  158. #define SPI0_CS2_HI()
  159. #define SPI0_CS2_SO()
  160. #endif
  161. #endif
  162. #if defined(SPI0_CS3_PIO_BIT)
  163. #if defined(SPI0_CS3_PIO_ID)
  164. #undef GPIO_ID
  165. #define GPIO_ID SPI0_CS3_PIO_ID
  166. #include <cfg/arch/porttran.h>
  167. static INLINE void SPI0_CS3_LO(void)
  168. {
  169. GPIO_SET_LO(SPI0_CS3_PIO_BIT);
  170. }
  171. static INLINE void SPI0_CS3_HI(void)
  172. {
  173. GPIO_SET_HI(SPI0_CS3_PIO_BIT);
  174. }
  175. static INLINE void SPI0_CS3_SO(void)
  176. {
  177. GPIO_ENABLE(SPI0_CS3_PIO_BIT);
  178. GPIO_OUTPUT(SPI0_CS3_PIO_BIT);
  179. }
  180. #else
  181. #define SPI0_CS3_LO()
  182. #define SPI0_CS3_HI()
  183. #define SPI0_CS3_SO()
  184. #endif
  185. #endif
  186. /*!
  187. * \brief Set the specified chip select to a given level.
  188. */
  189. int Avr32Spi0ChipSelect(uint_fast8_t cs, uint_fast8_t hi)
  190. {
  191. int rc = 0;
  192. switch (cs) {
  193. #if defined(SPI0_CS0_PIO_BIT)
  194. case 0:
  195. if (hi) {
  196. SPI0_CS0_HI();
  197. } else {
  198. SPI0_CS0_LO();
  199. }
  200. SPI0_CS0_SO();
  201. break;
  202. #endif
  203. #if defined(SPI0_CS1_PIO_BIT)
  204. case 1:
  205. if (hi) {
  206. SPI0_CS1_HI();
  207. } else {
  208. SPI0_CS1_LO();
  209. }
  210. SPI0_CS1_SO();
  211. break;
  212. #endif
  213. #if defined(SPI0_CS2_PIO_BIT)
  214. case 2:
  215. if (hi) {
  216. SPI0_CS2_HI();
  217. } else {
  218. SPI0_CS2_LO();
  219. }
  220. SPI0_CS2_SO();
  221. break;
  222. #endif
  223. #if defined(SPI0_CS3_PIO_BIT)
  224. case 3:
  225. if (hi) {
  226. SPI0_CS3_HI();
  227. } else {
  228. SPI0_CS3_LO();
  229. }
  230. SPI0_CS3_SO();
  231. break;
  232. #endif
  233. default:
  234. errno = EIO;
  235. rc = -1;
  236. break;
  237. }
  238. return rc;
  239. }
  240. /*! \brief Select a device on the first SPI bus.
  241. *
  242. * Locks and activates the bus for the specified node.
  243. *
  244. * \param node Specifies the SPI bus node.
  245. * \param tmo Timeout in milliseconds. To disable timeout, set this
  246. * parameter to NUT_WAIT_INFINITE.
  247. *
  248. * \return 0 on success. In case of an error, -1 is returned and the bus
  249. * is not locked.
  250. */
  251. int Avr32SpiBus0Select(NUTSPINODE * node, uint32_t tmo)
  252. {
  253. int rc;
  254. /* Sanity check. */
  255. NUTASSERT(node != NULL);
  256. NUTASSERT(node->node_bus != NULL);
  257. NUTASSERT(node->node_stat != NULL);
  258. /* Allocate the bus. */
  259. rc = NutEventWait(&node->node_bus->bus_mutex, tmo);
  260. if (rc) {
  261. errno = EIO;
  262. } else {
  263. AVR32SPIREG *spireg = node->node_stat;
  264. /* Enable SPI peripherals and clock. */
  265. gpio_enable_module_pin(AVR32_SPI0_SCK_PIN, AVR32_SPI0_SCK_FUNCTION);
  266. gpio_enable_module_pin(AVR32_SPI0_MISO_PIN, AVR32_SPI0_MISO_FUNCTION);
  267. gpio_enable_module_pin(AVR32_SPI0_MOSI_PIN, AVR32_SPI0_MOSI_FUNCTION);
  268. gpio_enable_module_pin(AVR32_SPI0_NPCS_PIN, AVR32_SPI0_NPCS_FUNCTION);
  269. /* If the mode update bit is set, then update our registers. */
  270. if (node->node_mode & SPI_MODE_UPDATE) {
  271. Avr32SpiSetup(node);
  272. }
  273. /* Set SPI mode. */
  274. outr(AVR32_SPI0_ADDRESS + AVR32_SPI_MR, spireg->mr);
  275. //outr(AVR32_SPI0_ADDRESS + AVR32_SPI_CSR0 + (node->node_cs * (AVR32_SPI_CSR0 - AVR32_SPI_CSR0)), spireg->csr);
  276. outr(AVR32_SPI0_ADDRESS + AVR32_SPI_CSR0, spireg->csr);
  277. /* Enable SPI. */
  278. AVR32_SPI0.cr |= AVR32_SPI_CR_SPIEN_MASK;
  279. /* Finally activate the node's chip select. */
  280. rc = Avr32Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) != 0);
  281. if (rc) {
  282. /* Release the bus in case of an error. */
  283. NutEventPost(&node->node_bus->bus_mutex);
  284. }
  285. }
  286. return rc;
  287. }
  288. /*! \brief Deselect a device on the first SPI bus.
  289. *
  290. * Deactivates the chip select and unlocks the bus.
  291. *
  292. * \param node Specifies the SPI bus node.
  293. *
  294. * \return Always 0.
  295. */
  296. int Avr32SpiBus0Deselect(NUTSPINODE * node)
  297. {
  298. /* Sanity check. */
  299. NUTASSERT(node != NULL);
  300. NUTASSERT(node->node_bus != NULL);
  301. #ifdef SPIBUS0_DOUBLE_BUFFER
  302. Avr32SpiBusWait(node, NUT_WAIT_INFINITE);
  303. #endif
  304. /* Deactivate the node's chip select. */
  305. Avr32Spi0ChipSelect(node->node_cs, (node->node_mode & SPI_MODE_CSHIGH) == 0);
  306. /* Release the bus. */
  307. NutEventPost(&node->node_bus->bus_mutex);
  308. return 0;
  309. }
  310. #if !defined(SPIBUS0_POLLING_MODE) || !defined(SPIBUS0_DOUBLE_BUFFER)
  311. static uint8_t *volatile spi0_txp;
  312. static uint8_t *volatile spi0_rxp;
  313. static volatile size_t spi0_xc;
  314. void Avr32SpiBus0Interrupt(void *arg)
  315. {
  316. uint8_t b;
  317. /* Get the received byte. */
  318. b = (uint8_t) inr(AVR32_SPI0_ADDRESS + AVR32_SPI_RDR) >> AVR32_SPI_RDR_RD_OFFSET;
  319. if (spi0_xc) {
  320. if (spi0_rxp) {
  321. *spi0_rxp++ = b;
  322. }
  323. spi0_xc--;
  324. }
  325. if (spi0_xc) {
  326. if (spi0_txp) {
  327. b = *spi0_txp++;
  328. }
  329. outr(AVR32_SPI0_ADDRESS + AVR32_SPI_TDR, (b << AVR32_SPI_TDR_TD_OFFSET));
  330. } else {
  331. NutEventPostFromIrq((void **) arg);
  332. }
  333. }
  334. /*!
  335. * \brief Transfer data on the SPI bus using single buffered interrupt mode.
  336. *
  337. * A device must have been selected by calling Avr32SpiSelect().
  338. *
  339. * \param node Specifies the SPI bus node.
  340. * \param txbuf Pointer to the transmit buffer. If NULL, undetermined
  341. * byte values are transmitted.
  342. * \param rxbuf Pointer to the receive buffer. If NULL, then incoming
  343. * data is discarded.
  344. * \param xlen Number of bytes to transfer.
  345. *
  346. * \return Always 0.
  347. */
  348. int Avr32SpiBus0Transfer(NUTSPINODE * node, const void *txbuf, void *rxbuf, int xlen)
  349. {
  350. uint16_t b = 0xff;
  351. uintptr_t base;
  352. /* Sanity check. */
  353. NUTASSERT(node != NULL);
  354. NUTASSERT(node->node_bus != NULL);
  355. NUTASSERT(node->node_bus->bus_base != 0);
  356. base = node->node_bus->bus_base;
  357. if (xlen) {
  358. if ( txbuf ) {
  359. if ( node->node_bits == 16 ) {
  360. b = *(uint16_t *) txbuf;
  361. }
  362. else if ( node->node_bits == 8 ) {
  363. b = *(uint8_t *)txbuf;
  364. }
  365. }
  366. /* Enable and kick interrupts. */
  367. outr(base + AVR32_SPI_IER, AVR32_SPI_IER_RDRF_MASK);
  368. if (node->node_bits == 8) {
  369. outr(base + AVR32_SPI_TDR, (b << AVR32_SPI_TDR_TD_OFFSET));
  370. }
  371. else if (node->node_bits == 16) {
  372. outr(base + AVR32_SPI_TDR, b);
  373. }
  374. /* Wait until transfer has finished. */
  375. NutEventWait(&node->node_bus->bus_ready, NUT_WAIT_INFINITE);
  376. /* Wait for data is send */
  377. while ((inr(base + AVR32_SPI_SR) & AVR32_SPI_TXEMPTY_MASK) == 0);
  378. /* Read incoming data. */
  379. if (node->node_bits == 8) {
  380. b = (uint8_t) inr(base + AVR32_SPI_RDR) >> AVR32_SPI_RDR_RD_OFFSET;
  381. }
  382. else if (node->node_bits == 16) {
  383. b = (uint16_t) inr(base + AVR32_SPI_RDR);
  384. }
  385. if (rxbuf) {
  386. if (node->node_bits == 8) {
  387. *(uint8_t *)rxbuf++ = b;
  388. }
  389. else if ( node->node_bits == 16) {
  390. *(uint16_t *)rxbuf++ = b;
  391. }
  392. }
  393. outr(base + AVR32_SPI_IDR, (unsigned int) -1);
  394. }
  395. return 0;
  396. }
  397. #endif
  398. /*!
  399. * \brief Avr32 SPI bus driver implementation structure.
  400. */
  401. NUTSPIBUS spiBus0Avr32 = {
  402. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  403. NULL, /*!< Bus ready signal (bus_ready). */
  404. AVR32_SPI0_ADDRESS, /*!< Bus base address (bus_base). */
  405. &sig_SPI0, /*!< Bus interrupt handler (bus_sig). */
  406. Avr32SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  407. Avr32SpiBus0Select, /*!< Select the specified device (bus_alloc). */
  408. Avr32SpiBus0Deselect, /*!< Deselect the specified device (bus_release). */
  409. #if defined(SPIBUS0_POLLING_MODE)
  410. Avr32SpiBusPollTransfer, /*!< Transfer data to and from a specified node (bus_transfer). */
  411. #elif defined(SPIBUS0_DOUBLE_BUFFER)
  412. Avr32SpiBusDblBufTransfer,
  413. #else
  414. Avr32SpiBus0Transfer,
  415. #endif
  416. #ifdef SPIBUS0_DOUBLE_BUFFER
  417. Avr32SpiBusWait,
  418. #else
  419. NutSpiBusWait, /*!< Wait for bus transfer ready (bus_wait). */
  420. #endif
  421. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  422. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  423. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  424. };