stm32_can2.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 by 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_can2.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/arch.h>
  44. #include <cfg/can_dev.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/stm/stm32xxxx.h>
  53. #include <arch/cm3/stm/stm32_gpio.h>
  54. #ifndef CANBUS2_REMAP_CAN
  55. #define CANBUS2_REMAP_CAN 0
  56. #endif
  57. /*!
  58. * \brief CANBUS2 GPIO configuartion and assignment.
  59. */
  60. #define CAN2_GPIO_PORT NUTGPIO_PORTB
  61. #if defined(MCU_STM32F1)
  62. #if (CANBUS2_REMAP_CAN == 1)
  63. #define CAN2RX_GPIO_PIN 5
  64. #define CAN2TX_GPIO_PIN 6
  65. #else
  66. #define CAN2RX_GPIO_PIN 12
  67. #define CAN2TX_GPIO_PIN 13
  68. #endif
  69. #else /*L1/F2/F4*/
  70. #if !defined(CANBUS2TX_PIN)
  71. #if (CANBUS2_REMAP_CAN == 0)
  72. #define CAN2TX_GPIO_PIN 13
  73. #elif (CANBUS2_REMAP_CAN == 1)
  74. #define CAN2TX_GPIO_PIN 6
  75. #else
  76. #error "Illegal CANBUS2_REMAP_CAN value"
  77. #endif
  78. #elif (CANBUS2TX_PIN == 13)
  79. #define CAN2TX_GPIO_PIN 13
  80. #elif (CANBUS2TX_PIN == 6)
  81. #define CAN2TX_GPIO_PIN 6
  82. #else
  83. #errror "Illegal CAN2 TX value"
  84. #endif
  85. #if !defined(CANBUS2RX_PIN)
  86. #if (CANBUS2_REMAP_CAN == 0)
  87. #define CAN2RX_GPIO_PIN 12
  88. #elif (CANBUS2_REMAP_CAN == 1)
  89. #define CAN2RX_GPIO_PIN 5
  90. #else
  91. #error "Illegal CANBUS2_REMAP_CAN value"
  92. #endif
  93. #elif (CANBUS2RX_PIN == 12)
  94. #define CAN2RX_GPIO_PIN 12
  95. #elif (CANBUS2RX_PIN == 5)
  96. #define CAN2RX_GPIO_PIN 5
  97. #else
  98. #errror "Illegal CAN2 RX value"
  99. #endif
  100. #endif
  101. /*!
  102. * \brief Processor specific Hardware Initiliaization
  103. *
  104. */
  105. int Stm32CanHw2Init(void)
  106. {
  107. #if defined (CAN2_ACCEPTANCE_FILTERS)
  108. uint32_t rc;
  109. #endif
  110. if (!CM3BBGET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_CAN1EN)))
  111. {
  112. /* CAN1 has no clock, unconditionally clock CAN 1 */
  113. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_CAN1EN));
  114. /* Reset CAN Bus 1 IP */
  115. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN1RST));
  116. CM3BBCLR(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN1RST));
  117. #if defined (CAN2_ACCEPTANCE_FILTERS)
  118. /* Set the CAN1/CAN2 Filter split */
  119. rc =CAN1->FMR;
  120. rc &= ~0x3f00;
  121. rc |= CAN2_ACCEPTANCE_FILTERS<<8;
  122. CAN1->FMR = rc;
  123. #endif
  124. }
  125. /* Enable CAN Bus 2 peripheral clock. */
  126. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1ENR, _BI32(RCC_APB1ENR_CAN2EN));
  127. /* Reset CAN Bus 2 IP */
  128. CM3BBSET(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN2RST));
  129. CM3BBCLR(RCC_BASE, RCC_TypeDef, APB1RSTR, _BI32(RCC_APB1RSTR_CAN2RST));
  130. /* Setup Related GPIOs. */
  131. GpioPinConfigSet(CAN2_GPIO_PORT, CAN2RX_GPIO_PIN, GPIO_CFG_PULLUP|GPIO_CFG_PERIPHAL);
  132. GpioPinConfigSet(CAN2_GPIO_PORT, CAN2TX_GPIO_PIN, GPIO_CFG_OUTPUT|GPIO_CFG_PERIPHAL);
  133. #if defined (MCU_STM32F1)
  134. #if (CANBUS2_REMAP_CAN == 1)
  135. CM3BBSET(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_CAN2_REMAP));
  136. #else
  137. CM3BBCLR(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_CAN2_REMAP));
  138. #endif
  139. #else
  140. GPIO_PinAFConfig((GPIO_TypeDef*) CAN2_GPIO_PORT, CAN2RX_GPIO_PIN, GPIO_AF_CAN2);
  141. GPIO_PinAFConfig((GPIO_TypeDef*) CAN2_GPIO_PORT, CAN2TX_GPIO_PIN, GPIO_AF_CAN2);
  142. #endif
  143. return 0;
  144. }
  145. NUTCANBUS Stm32CanBus2 = {
  146. CAN2_BASE,
  147. CM3BB_BASE(CAN2_BASE),
  148. &sig_CAN2_RX0,
  149. &sig_CAN2_TX,
  150. &sig_CAN2_SCE,
  151. 0,
  152. Stm32CanHw2Init,
  153. };
  154. NUTCANBUS Stm32CanBus2C = {
  155. CAN2_BASE,
  156. CM3BB_BASE(CAN2_BASE),
  157. &sig_CAN2_RX1,
  158. 0,
  159. 0,
  160. 0,
  161. 0,
  162. };