stm32_can1.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Rittal GmbH & Co. KG. All rights reserved.
  4. * Copyright (C) 2012 Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de).
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*
  37. * \verbatim
  38. * $Id: stm32_can1.c 5639 2014-04-07 13:58:53Z u_bonnes $
  39. * \endverbatim
  40. */
  41. #include <cfg/os.h>
  42. #include <cfg/clock.h>
  43. #include <cfg/can_dev.h>
  44. #include <cfg/arch.h>
  45. #include <cfg/arch/gpio.h>
  46. #include <sys/atom.h>
  47. #include <sys/event.h>
  48. #include <sys/timer.h>
  49. #include <dev/irqreg.h>
  50. #include <dev/gpio.h>
  51. #include <dev/canbus.h>
  52. #include <arch/cm3.h>
  53. #include <arch/cm3/stm/stm32xxxx.h>
  54. #include <arch/cm3/stm/stm32_gpio.h>
  55. #ifndef CANBUS1_REMAP_CAN
  56. #define CANBUS1_REMAP_CAN 0
  57. #endif
  58. /*!
  59. * \brief CANBUS1 GPIO configuartion and assignment.
  60. */
  61. #if defined(MCU_STM32F1)
  62. #if (CANBUS1_REMAP_CAN == 1)
  63. #define CANBUS_REMAP AFIO_MAPR_CAN_REMAP_REMAP2
  64. #define CAN1RX_GPIO_PORT NUTGPIO_PORTB
  65. #define CAN1TX_GPIO_PORT NUTGPIO_PORTB
  66. #define CAN1RX_GPIO_PIN 8
  67. #define CAN1TX_GPIO_PIN 9
  68. #elif (CANBUS1_REMAP_CAN == 2)
  69. #define CANBUS_REMAP AFIO_MAPR_CAN_REMAP_REMAP3
  70. #define CAN1RX_GPIO_PORT NUTGPIO_PORTD
  71. #define CAN1TX_GPIO_PORT NUTGPIO_PORTD
  72. #define CAN1RX_GPIO_PIN 0
  73. #define CAN1TX_GPIO_PIN 1
  74. #else
  75. #undef CANBUS_REMAP
  76. #define CANBUS_REMAP AFIO_MAPR_CAN_REMAP_REMAP1
  77. #define CAN1RX_GPIO_PORT NUTGPIO_PORTA
  78. #define CAN1TX_GPIO_PORT NUTGPIO_PORTA
  79. #define CAN1RX_GPIO_PIN 11
  80. #define CAN1TX_GPIO_PIN 12
  81. #endif
  82. #else /*L1/F2/F3/F4*/
  83. #if !defined(CANBUS1_TX_PIN)
  84. #if (CANBUS1_REMAP_CAN == 0)
  85. #define CAN1TX_GPIO_PORT NUTGPIO_PORTA
  86. #define CAN1TX_GPIO_PIN 12
  87. #elif (CANBUS1_REMAP_CAN == 1)
  88. #define CAN1TX_GPIO_PORT NUTGPIO_PORTB
  89. #define CAN1TX_GPIO_PIN 9
  90. #elif (CANBUS1_REMAP_CAN == 2)
  91. #define CAN1TX_GPIO_PORT NUTGPIO_PORTD
  92. #define CAN1TX_GPIO_PIN 1
  93. #else
  94. #error "Illegal CANBUS1_REMAP_CAN value"
  95. #endif
  96. #elif (CANBUS1_TX_PIN == 12)
  97. #define CAN1TX_GPIO_PORT NUTGPIO_PORTA
  98. #define CAN1TX_GPIO_PIN 12
  99. #elif (CANBUS1_TX_PIN == 9)
  100. #define CAN1TX_GPIO_PORT NUTGPIO_PORTB
  101. #define CAN1TX_GPIO_PIN 9
  102. #elif (CANBUS1_TX_PIN == 1)
  103. #define CAN1TX_GPIO_PORT NUTGPIO_PORTD
  104. #define CAN1TX_GPIO_PIN 1
  105. #else
  106. #errror "Illegal CAN1 TX value"
  107. #endif
  108. #if !defined(CANBUS1_RX_PIN)
  109. #if (CANBUS1_REMAP_CAN == 0)
  110. #define CAN1RX_GPIO_PORT NUTGPIO_PORTA
  111. #define CAN1RX_GPIO_PIN 11
  112. #elif (CANBUS1_REMAP_CAN == 1)
  113. #define CAN1RX_GPIO_PORT NUTGPIO_PORTB
  114. #define CAN1RX_GPIO_PIN 8
  115. #elif (CANBUS1_REMAP_CAN == 2)
  116. #define CAN1RX_GPIO_PORT NUTGPIO_PORTD
  117. #define CAN1RX_GPIO_PIN 0
  118. #else
  119. #error "Illegal CANBUS1_REMAP_CAN value"
  120. #endif
  121. #elif (CANBUS1_RX_PIN == 11)
  122. #define CAN1RX_GPIO_PORT NUTGPIO_PORTA
  123. #define CAN1RX_GPIO_PIN 11
  124. #elif (CANBUS1_RX_PIN == 8)
  125. #define CAN1RX_GPIO_PORT NUTGPIO_PORTB
  126. #define CAN1RX_GPIO_PIN 8
  127. #elif (CANBUS1_RX_PIN == 0)
  128. #define CAN1RX_GPIO_PORT NUTGPIO_PORTD
  129. #define CAN1RX_GPIO_PIN 0
  130. #else
  131. #errror "Illegal CAN1 RX value"
  132. #endif
  133. #endif
  134. #if defined(MCU_STM32F3)
  135. #define F3_GPIO_AF_CAN1(x) (x == NUTGPIO_PORTD)?7:9
  136. #endif
  137. /*!
  138. * \brief Processor specific Hardware Initiliaization
  139. *
  140. */
  141. int Stm32CanHw1Init(void)
  142. {
  143. #if defined (CAN2_ACCEPTANCE_FILTERS)
  144. uint32_t fmr;
  145. #endif
  146. /* Enable CAN Bus 1 peripheral clock. */
  147. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_CAN1EN));
  148. /* Reset CAN Bus 1 IP */
  149. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN1RST));
  150. CM3BBCLR(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN1RST));
  151. #if defined (CAN2_ACCEPTANCE_FILTERS)
  152. /* Set the CAN1/CAN2 Filter split */
  153. fmr = CAN1->FMR;
  154. fmr &= ~0x3f00;
  155. fmr |= CAN2_ACCEPTANCE_FILTERS<<8;
  156. CAN1->FMR = fmr;
  157. #endif
  158. /* Setup Related GPIOs. */
  159. GpioPinConfigSet(CAN1RX_GPIO_PORT, CAN1RX_GPIO_PIN, GPIO_CFG_PULLUP|GPIO_CFG_PERIPHAL);
  160. GpioPinConfigSet(CAN1TX_GPIO_PORT, CAN1TX_GPIO_PIN, GPIO_CFG_OUTPUT|GPIO_CFG_PERIPHAL);
  161. #if defined (MCU_STM32F1)
  162. AFIO->MAPR &= ~AFIO_MAPR_CAN_REMAP;
  163. AFIO->MAPR |= CANBUS_REMAP;
  164. #elif defined (MCU_STM32F3)
  165. GPIO_PinAFConfig((GPIO_TypeDef*) CAN1RX_GPIO_PORT, CAN1RX_GPIO_PIN, F3_GPIO_AF_CAN1(CAN1RX_GPIO_PORT));
  166. GPIO_PinAFConfig((GPIO_TypeDef*) CAN1TX_GPIO_PORT, CAN1TX_GPIO_PIN, F3_GPIO_AF_CAN1(CAN1RX_GPIO_PORT));
  167. #else
  168. GPIO_PinAFConfig((GPIO_TypeDef*) CAN1RX_GPIO_PORT, CAN1RX_GPIO_PIN, GPIO_AF_CAN1);
  169. GPIO_PinAFConfig((GPIO_TypeDef*) CAN1TX_GPIO_PORT, CAN1TX_GPIO_PIN, GPIO_AF_CAN1);
  170. #endif
  171. return 0;
  172. }
  173. NUTCANBUS Stm32CanBus1 = {
  174. CAN1_BASE,
  175. CM3BB_BASE(CAN1_BASE),
  176. &sig_CAN1_RX0,
  177. &sig_CAN1_TX,
  178. &sig_CAN1_SCE,
  179. 0,
  180. Stm32CanHw1Init,
  181. };
  182. NUTCANBUS Stm32CanBus1C = {
  183. CAN1_BASE,
  184. CM3BB_BASE(CAN1_BASE),
  185. &sig_CAN1_RX1,
  186. 0,
  187. 0,
  188. 0,
  189. 0,
  190. };