stm32_spi6.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Nikolaj Zamotaev. All rights reserved.
  4. * Copyright (C) 2014 by Uwe Bonnes.
  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. /*
  36. * \verbatim
  37. * $Id: stm32_spi6.c 5716 2014-05-23 14:55:45Z u_bonnes $
  38. * \endverbatim
  39. */
  40. #include <arch/cm3.h>
  41. #include <sys/timer.h>
  42. #include <cfg/spi.h>
  43. #include <cfg/arch/gpio.h>
  44. #include <dev/spibus.h>
  45. #include <dev/gpio.h>
  46. #include <arch/cm3/stm/stm32_gpio.h>
  47. #include <arch/cm3/stm/stm32_spi.h>
  48. #include <dev/irqreg.h>
  49. #include <sys/event.h>
  50. #include <sys/nutdebug.h>
  51. #include <stdlib.h>
  52. #include <errno.h>
  53. /* Handle the PIN remap possibilities
  54. * F4
  55. * NSS: PG8
  56. * SCK: PG13
  57. * MISO: PG12
  58. * MOSI: PG14
  59. *
  60. * For function pins, we use PG8/12/13/14 as default
  61. */
  62. #if !defined( SPIBUS6_NO_CS)
  63. #if !defined(SPIBUS6_CS0_PORT) && !defined(SPIBUS6_CS0_PIN)
  64. #define SPIBUS_CS0_PORT NUTGPIO_PORTG
  65. #define SPIBUS_CS0_PIN 8
  66. #elif !defined(SPIBUS6_CS0_PORT) || !defined(SPIBUS6_CS0_PIN)
  67. #warning "SPIBUS6 uncomplete chip select"
  68. #else
  69. #define SPIBUS_CS0_PORT SPIBUS6_CS0_PORT
  70. #define SPIBUS_CS0_PIN SPIBUS6_CS0_PIN
  71. #endif
  72. #if defined(SPIBUS6_CS1_PORT)
  73. #define SPIBUS_CS1_PORT SPIBUS6_CS1_PORT
  74. #endif
  75. #if defined(SPIBUS6_CS2_PORT)
  76. #define SPIBUS_CS2_PORT SPIBUS6_CS2_PORT
  77. #endif
  78. #if defined(SPIBUS6_CS3_PORT)
  79. #define SPIBUS_CS3_PORT SPIBUS6_CS3_PORT
  80. #endif
  81. #if defined(SPIBUS6_CS1_PIN)
  82. #define SPIBUS_CS1_PIN SPIBUS6_CS1_PIN
  83. #endif
  84. #if defined(SPIBUS6_CS2_PIN)
  85. #define SPIBUS_CS2_PIN SPIBUS6_CS2_PIN
  86. #endif
  87. #if defined(PIBUS6_CS2_PIN)
  88. #define SPIBUS_CS3_PIN SPIBUS6_CS3_PIN
  89. #endif
  90. #endif
  91. #define SPIBUS_SCK_PORT NUTGPIO_PORTG
  92. #define SPIBUS_SCK_PIN 13
  93. #define SPIBUS_MISO_PORT NUTGPIO_PORTG
  94. #define SPIBUS_MISO_PIN 12
  95. #define SPIBUS_MOSI_PORT NUTGPIO_PORTG
  96. #define SPIBUS_MOSI_PIN 14
  97. #define SPI_GPIO_AF GPIO_AF_SPI6
  98. #define SPI_ENABLE_CLK_SET() CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_SPI6EN))
  99. #define SPI_ENABLE_CLK_GET() CM3BBGET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_SPI6EN))
  100. #define sig_SPI sig_SPI6
  101. #define SPI_BASE SPI6_BASE
  102. #if !defined(SPIBUS6_MODE)
  103. #define SPIBUS_MODE IRQ_MODE
  104. #else
  105. #define SPIBUS_MODE SPIBUS6_MODE
  106. #endif
  107. #if SPIBUS_MODE == DMA_MODE
  108. #define SPI_DMA_TX_CHANNEL SPI6_TX_DMA
  109. #define sig_SPI_DMA_TX SPI6_TX_DMA_IRQ
  110. #define SPI_DMA_RX_CHANNEL SPI6_RX_DMA
  111. #define sig_SPI_DMA_RX SPI6_RX_DMA_IRQ
  112. #endif
  113. NUTSPIBUS spiBus6Stm32 = {
  114. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  115. NULL, /*!< Bus ready signal (bus_ready). */
  116. SPI6_BASE, /*!< Bus base address (bus_base). */
  117. NULL, /*!< Bus interrupt handler (bus_sig). */
  118. Stm32SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  119. Stm32SpiBusSelect, /*!< Select the specified device (bus_alloc). */
  120. Stm32SpiBusDeselect, /*!< Deselect the specified device (bus_release). */
  121. Stm32SpiBusTransfer,
  122. NutSpiBusWait,
  123. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  124. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  125. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  126. };
  127. #include "stm32_spi.c"