stm32_spi2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Nikolaj Zamotaev. All rights reserved.
  4. * Copyright (C) 2012-2014 by Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de
  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_spi2.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. #if defined(MCU_STM32F1)
  48. #include <arch/cm3/stm/stm32f1_dma.h>
  49. #endif
  50. #include <arch/cm3/stm/stm32_spi.h>
  51. #include <dev/irqreg.h>
  52. #include <sys/event.h>
  53. #include <sys/nutdebug.h>
  54. #include <stdlib.h>
  55. #include <errno.h>
  56. /* Handle the PIN remap possibilities
  57. * F1: NSS: PB12
  58. * SCK: PB13
  59. * MISO: PB14
  60. * MOSI: PB15
  61. * L1: NSS: PB12/PD0
  62. * SCK: PB13/PD1
  63. * MISO: PB14/PD3
  64. * MOSI: PB15/PD4
  65. * F2/F4: NSS: PB12/PB9/PI0
  66. * SCK: PB13/PB10/PI1
  67. * MISO: PB14/PC2/PI2
  68. * MOSI: PB15/PC3/PI3
  69. * F30x: NSS: PB12/PD12
  70. * SCK: PB13/PF9/PF10
  71. * MISO: PB14
  72. * MOSI: PB15
  73. *
  74. * F373: NSS: PA11/PB9/PD6
  75. * SCK: PA8/PB8/PB10/PD7/PD8
  76. * MISO: PA9/PB14/PC2/PD3
  77. * MOSI: PA10/PB15/PC3/PD4/
  78. *
  79. * Use PB12 or on F373 PA11 as default chip select
  80. */
  81. #if !defined( SPIBUS2_NO_CS)
  82. #if !defined(SPIBUS2_CS0_PORT) && !defined(SPIBUS2_CS0_PIN)
  83. #if defined(MCU_STM32F37X)
  84. #define SPIBUS_CS0_PORT NUTGPIO_PORTA
  85. #define SPIBUS_CS0_PIN 11
  86. #else
  87. #define SPIBUS_CS0_PORT NUTGPIO_PORTB
  88. #define SPIBUS_CS0_PIN 12
  89. #endif
  90. #elif !defined(SPIBUS2_CS0_PORT) || !defined(SPIBUS2_CS0_PIN)
  91. #warning "SPIBUS2 uncomplete chip select"
  92. #else
  93. #define SPIBUS_CS0_PORT SPIBUS2_CS0_PORT
  94. #define SPIBUS_CS0_PIN SPIBUS2_CS0_PIN
  95. #endif
  96. #if defined(SPIBUS2_CS1_PORT)
  97. #define SPIBUS_CS1_PORT SPIBUS2_CS1_PORT
  98. #endif
  99. #if defined(SPIBUS2_CS2_PORT)
  100. #define SPIBUS_CS2_PORT SPIBUS2_CS2_PORT
  101. #endif
  102. #if defined(SPIBUS2_CS3_PORT)
  103. #define SPIBUS_CS3_PORT SPIBUS2_CS3_PORT
  104. #endif
  105. #if defined(SPIBUS2_CS1_PIN)
  106. #define SPIBUS_CS1_PIN SPIBUS2_CS1_PIN
  107. #endif
  108. #if defined(SPIBUS2_CS2_PIN)
  109. #define SPIBUS_CS2_PIN SPIBUS2_CS2_PIN
  110. #endif
  111. #if defined(PIBUS2_CS2_PIN)
  112. #define SPIBUS_CS3_PIN SPIBUS2_CS3_PIN
  113. #endif
  114. #endif
  115. #if defined(MCU_STM32F1)
  116. #undef SPIBUS_REMAP_BB
  117. #define SPIBUS_SCK_PIN 13
  118. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  119. #define SPIBUS_MISO_PIN 14
  120. #define SPIBUS_MISO_PORT NUTGPIO_PORTB
  121. #define SPIBUS_MOSI_PIN 15
  122. #define SPIBUS_MOSI_PORT NUTGPIO_PORTB
  123. #elif defined(MCU_STM32F37X)
  124. #if SPIBUS2_SCK_PIN == 8 || !defined(SPIBUS2_SCK_PIN)
  125. #define SPIBUS_SCK_PORT NUTGPIO_PORTA
  126. #define SPIBUS_SCK_PIN 8
  127. #elif SPIBUS2_SCK_PIN == 208
  128. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  129. #define SPIBUS_SCK_PIN 8
  130. #elif SPIBUS2_SCK_PIN == 408
  131. #define SPIBUS_SCK_PORT NUTGPIO_PORTD
  132. #define SPIBUS_SCK_PIN 8
  133. #elif SPIBUS2_SCK_PIN == 7
  134. #define SPIBUS_SCK_PORT NUTGPIO_PORTD
  135. #define SPIBUS_SCK_PIN 7
  136. #elif SPIBUS2_SCK_PIN == 10
  137. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  138. #define SPIBUS_SCK_PIN 10
  139. #else
  140. #warning "Illegal STM32F373 SPI2 SCK assignment"
  141. #endif
  142. #if SPIBUS2_MOSI_PIN == 10 || !defined(SPIBUS2_MOSI_PIN)
  143. #define SPIBUS_MOSI_PORT NUTGPIO_PORTA
  144. #define SPIBUS_MOSI_PIN 10
  145. #elif SPIBUS2_MOSI_PIN == 15
  146. #define SPIBUS_MOSI_PORT NUTGPIO_PORTB
  147. #define SPIBUS_MOSI_PIN 15
  148. #elif SPIBUS2_MOSI_PIN == 3
  149. #define SPIBUS_MOSI_PORT NUTGPIO_PORTC
  150. #define SPIBUS_MOSI_PIN 3
  151. #elif SPIBUS2_MOSI_PIN == 4
  152. #define SPIBUS_MOSI_PORT NUTGPIO_PORTB
  153. #define SPIBUS_MOSI_PIN 4
  154. #else
  155. #warning "Illegal STM32F373 SPI2 MOSI assignment"
  156. #endif
  157. #if SPIBUS2_MISO_PIN == 9 || !defined(SPIBUS2_MISO_PIN)
  158. #define SPIBUS_MISO_PORT NUTGPIO_PORTA
  159. #define SPIBUS_MISO_PIN 9
  160. #elif SPIBUS2_MISO_PIN == 14
  161. #define SPIBUS_MISO_PORT NUTGPIO_PORTB
  162. #define SPIBUS_MISO_PIN 14
  163. #elif SPIBUS2_MISO_PIN == 2
  164. #define SPIBUS_MISO_PORT NUTGPIO_PORTC
  165. #define SPIBUS_MISO_PIN 2
  166. #elif SPIBUS2_MISO_PIN == 3
  167. #define SPIBUS_MISO_PORT NUTGPIO_PORTD
  168. #define SPIBUS_MISO_PIN 3
  169. #else
  170. #warning "Illegal STM32F373 SPI2 MISO assignment"
  171. #endif
  172. #else
  173. #if !defined(SPIBUS2_SCK_PIN)
  174. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  175. #define SPIBUS_SCK_PIN 13
  176. #elif SPIBUS2_SCK_PIN == 13
  177. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  178. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_SCK_PIN == 10
  179. #define SPIBUS_SCK_PORT NUTGPIO_PORTB
  180. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_SCK_PIN == 1
  181. #define SPIBUS_SCK_PORT NUTGPIO_PORTD
  182. #elif defined(MCU_STM32L1) && SPIBUS2_SCK_PIN == 1
  183. #define SPIBUS_SCK_PORT NUTGPIO_PORTD
  184. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_SCK_PIN == 901
  185. #undef SPIBUS2_SCK_PIN
  186. #define SPIBUS_SCK_PIN 1
  187. #define SPIBUS_SCK_PORT NUTGPIO_PORTI
  188. #else
  189. #warning "Illegal I2C2 SCK pin assignement"
  190. #endif
  191. #if !defined(SPIBUS2_MISO_PIN)
  192. #define SPIBUS_MISO_PIN 14
  193. #define SPIBUS_MISO_PORT NUTGPIO_PORTB
  194. #elif SPIBUS2_MISO_PIN == 14
  195. #define SPIBUS_MISO_PORT NUTGPIO_PORTB
  196. #elif defined(MCU_STM32L1) && SPIBUS2_MISO_PIN == 3
  197. #define SPIBUS_MISO_PORT NUTGPIO_PORTD
  198. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_MISO_PIN == 2
  199. #define SPIBUS_MISO_PORT NUTGPIO_PORTC
  200. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_MISO_PIN == 902
  201. #undef SPIBUS2_MISO_PIN
  202. #define SPIBUS_MISO_PIN 2
  203. #define SPIBUS_MISO_PORT NUTGPIO_PORTI
  204. #else
  205. #warning "Illegal I2C2 MISO pin assignement"
  206. #endif
  207. #if !defined(SPIBUS2_MOSI_PIN)
  208. #define SPIBUS_MOSI_PIN 15
  209. #define SPIBUS_MOSI_PORT NUTGPIO_PORTB
  210. #elif SPIBUS2_MOSI_PIN == 15
  211. #define SPIBUS_MOSI_PORT NUTGPIO_PORTB
  212. #elif defined(MCU_STM32L1) && SPIBUS2_MOSI_PIN == 4
  213. #define SPIBUS_MOSI_PORT NUTGPIO_PORTD
  214. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_MOSI_PIN == 3
  215. #define SPIBUS_MOSI_PORT NUTGPIO_PORTC
  216. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F4)) && SPIBUS2_MOSI_PIN == 903
  217. #undef SPIBUS2_MOSI_PIN
  218. #define SPIBUS_MOSI_PIN 3
  219. #define SPIBUS_MOSI_PORT NUTGPIO_PORTI
  220. #else
  221. #warning "Illegal I2C2 MOSI pin assignement"
  222. #endif
  223. #endif
  224. #define SPI_DEV 2
  225. #define SPI_GPIO_AF GPIO_AF_SPI2
  226. #define SPI_ENABLE_CLK_SET() CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_SPI2EN))
  227. #define SPI_ENABLE_CLK_GET() CM3BBGET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_SPI2EN))
  228. #define sig_SPI sig_SPI2
  229. #define SPI_BASE SPI2_BASE
  230. #if !defined(SPIBUS2_MODE)
  231. #define SPIBUS_MODE IRQ_MODE
  232. #else
  233. #define SPIBUS_MODE SPIBUS2_MODE
  234. #endif
  235. #if SPIBUS_MODE == DMA_MODE
  236. #define SPI_DMA_TX_CHANNEL SPI2_TX_DMA
  237. #define sig_SPI_DMA_TX SPI2_TX_DMA_IRQ
  238. #define SPI_DMA_RX_CHANNEL SPI2_RX_DMA
  239. #define sig_SPI_DMA_RX SPI2_RX_DMA_IRQ
  240. #endif
  241. NUTSPIBUS spiBus2Stm32 = {
  242. NULL, /*!< Bus mutex semaphore (bus_mutex). */
  243. NULL, /*!< Bus ready signal (bus_ready). */
  244. SPI2_BASE, /*!< Bus base address (bus_base). */
  245. NULL, /*!< Bus interrupt handler (bus_sig). */
  246. Stm32SpiBusNodeInit, /*!< Initialize the bus (bus_initnode). */
  247. Stm32SpiBusSelect, /*!< Select the specified device (bus_alloc). */
  248. Stm32SpiBusDeselect, /*!< Deselect the specified device (bus_release). */
  249. Stm32SpiBusTransfer,
  250. NutSpiBusWait,
  251. NutSpiBusSetMode, /*!< Set SPI mode of a specified device (bus_set_mode). */
  252. NutSpiBusSetRate, /*!< Set clock rate of a specified device (bus_set_rate). */
  253. NutSpiBusSetBits /*!< Set number of data bits of a specified device (bus_set_bits). */
  254. };
  255. #include "stm32_spi.c"