stm32_twi2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Rittal GmbH & Co. KG. All rights reserved.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*
  36. * \verbatim
  37. * $Id: stm32_twi2.c 5169 2013-05-20 20:14:10Z u_bonnes $
  38. * \endverbatim
  39. */
  40. /*!
  41. * \file arch/cm3/dev/stm/stm32_twi2.c
  42. * \brief STM32F I2C bus 1 initialization
  43. */
  44. #include <cfg/os.h>
  45. #include <cfg/clock.h>
  46. #include <cfg/arch.h>
  47. #include <cfg/twi.h>
  48. #include <cfg/arch/gpio.h>
  49. #include <string.h>
  50. #include <sys/atom.h>
  51. #include <sys/event.h>
  52. #include <sys/timer.h>
  53. #include <dev/irqreg.h>
  54. #include <dev/gpio.h>
  55. #include <dev/twif.h>
  56. #include <arch/cm3/stm/stm32xxxx.h>
  57. #include <arch/cm3/stm/stm32_gpio.h>
  58. #if defined(I2CBUS2_USE_DMA)
  59. #if defined(MCU_STM32F1)
  60. #include <arch/cm3/stm/stm32f1_dma.h>
  61. #else
  62. #warning "Unhandled STM32 family"
  63. #endif
  64. #endif
  65. #include <arch/cm3/stm/stm32_twi.h>
  66. /*!
  67. * \brief I2CBUS2 GPIO configuartion and assignment.
  68. *
  69. * F1: SMBA PB12
  70. * SCL PB10
  71. * SDA PB11
  72. * L1: SMBA PB12
  73. * SCL PB10
  74. * SDA PB11
  75. * F2/F4L SMBA PB12 PF2 PH6
  76. * SCL PB10 PF1 PH4
  77. * SDA PB11 PF0 PH5
  78. */
  79. #if defined(MCU_STM32F1)
  80. #define I2CBUS2_SCL_PORT NUTGPIO_PORTB
  81. #define I2CBUS2_SCL_PIN 10
  82. #define I2CBUS2_SDA_PORT NUTGPIO_PORTB
  83. #define I2CBUS2_SDA_PIN 11
  84. #else
  85. #if !defined(I2CBUS2_SCL_PIN)
  86. #define I2CBUS2_SCL_PORT NUTGPIO_PORTB
  87. #define I2CBUS2_SCL_PIN 10
  88. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SCL_PIN == 1
  89. #define I2CBUS2_SCL_PORT NUTGPIO_PORTF
  90. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SCL_PIN == 4
  91. #define I2CBUS2_SCL_PORT NUTGPIO_PORTH
  92. #else
  93. #warning "Illegal I2C2 SCL pin assignement"
  94. #endif
  95. #if !defined(I2CBUS2_SDA_PIN)
  96. #define I2CBUS2_SDA_PORT NUTGPIO_PORTB
  97. #define I2CBUS2_SDA_PIN 11
  98. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SDA_PIN == 0
  99. #define I2CBUS2_SDA_PORT NUTGPIO_PORTF
  100. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SDA_PIN == 5
  101. #define I2CBUS2_SDA_PORT NUTGPIO_PORTH
  102. #else
  103. #warning "Illegal I2C2 SCL pin assignement"
  104. #endif
  105. #endif
  106. #ifdef I2CBUS2_MODE_SMBUS
  107. #if defined(MCU_STM32F1)
  108. #define I2CBUS2_SMBA_PORT NUTGPIO_PORTB
  109. #define I2CBUS2_SMBA_PIN 11
  110. #endif
  111. #else
  112. #ifndef I2CBUS2_SMBA_PIN
  113. #define I2CBUS2_SMBA_PORT NUTGPIO_PORTB
  114. #define I2CBUS2_SMBA_PIN 11
  115. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SMBA_PIN == 2
  116. #define I2CBUS2_SMBA_PORT NUTGPIO_PORTF
  117. #elif (defined(MCU_STM32F2) || defined(MCU_STM32F2)) && I2CBUS2_SMBA_PIN == 6
  118. #define I2CBUS2_SMBA_PORT NUTGPIO_PORTH
  119. #else
  120. #warning "Illegal I2C2 SMBA pin assignement"
  121. #endif
  122. #endif
  123. #ifdef I2CBUS2_DEFAULT_SPEED
  124. #define I2C_DEFAULT_SPEED
  125. #else
  126. #endif
  127. int Stm32I2cBus2Recover( void)
  128. {
  129. uint_fast8_t i;
  130. /* Handle pins as GPIOs, set SCL low */
  131. GpioPinConfigSet( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN, GPIO_CFG_OUTPUT|GPIO_CFG_MULTIDRIVE);
  132. GpioPinConfigSet( I2CBUS2_SDA_PORT, I2CBUS2_SDA_PIN, GPIO_CFG_OUTPUT|GPIO_CFG_MULTIDRIVE);
  133. GpioPinSetLow( I2CBUS2_SDA_PORT, I2CBUS2_SDA_PIN);
  134. NutMicroDelay(10);
  135. /* Run sequence of 8 SCL clocks */
  136. for( i=0; i<8; i++) {
  137. GpioPinSetLow( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN);
  138. NutMicroDelay(10);
  139. GpioPinSetHigh( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN);
  140. NutMicroDelay(10);
  141. }
  142. /* Issue Stop condition on the bus */
  143. GpioPinSetHigh( I2CBUS2_SDA_PORT, I2CBUS2_SDA_PIN);
  144. NutMicroDelay(10);
  145. GpioPinSetHigh( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN);
  146. GpioPinConfigSet( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|GPIO_CFG_MULTIDRIVE);
  147. GpioPinConfigSet( I2CBUS2_SDA_PORT, I2CBUS2_SDA_PIN, GPIO_CFG_OUTPUT| GPIO_CFG_PERIPHAL|GPIO_CFG_MULTIDRIVE);
  148. return 0;
  149. }
  150. /*!
  151. * \brief Processor specific Hardware Initiliaization
  152. *
  153. */
  154. int Stm32I2cBus2Init(void)
  155. {
  156. /* Enable I2C Bus 2 peripheral clock. */
  157. RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
  158. /* Reset I2C Bus 2 IP */
  159. RCC->APB1RSTR |= RCC_APB1RSTR_I2C2RST;
  160. RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C2RST;
  161. /* Setup Related GPIOs.
  162. * On non-F1 parts, pins may be mapped to different ports!
  163. */
  164. GpioPinConfigSet( I2CBUS2_SCL_PORT, I2CBUS2_SCL_PIN,
  165. GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL |
  166. GPIO_CFG_MULTIDRIVE | GPIO_CFG_INIT_HIGH);
  167. GpioPinConfigSet( I2CBUS2_SDA_PORT, I2CBUS2_SDA_PIN,
  168. GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL |
  169. GPIO_CFG_MULTIDRIVE | GPIO_CFG_INIT_HIGH);
  170. #ifdef I2CBUS2_MODE_SMBUS
  171. GpioPinConfigSet( I2CBUS2_SMBA_PORT, I2CBUS2_SMBA_PIN,
  172. GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL |
  173. GPIO_CFG_MULTIDRIVE | GPIO_CFG_INIT_HIGH);
  174. GPIO_PinAFConfig((GPIO_TypeDef*) I2CBUS2_SMBA_PORT,
  175. I2CBUS2_SMBA_PIN, GPIO_AF_I2C2);
  176. #endif
  177. #if defined (MCU_STM32L1) || defined (MCU_STM32F2) || defined (MCU_STM32F4)
  178. GPIO_PinAFConfig((GPIO_TypeDef*) I2CBUS2_SDA_PORT,
  179. I2CBUS2_SDA_PIN, GPIO_AF_I2C2);
  180. GPIO_PinAFConfig((GPIO_TypeDef*) I2CBUS2_SCL_PORT,
  181. I2CBUS2_SCL_PIN, GPIO_AF_I2C2);
  182. #endif
  183. NVIC_SetPriorityGrouping(4);
  184. NVIC_SetPriority( I2C2_EV_IRQn, 0);
  185. NVIC_SetPriority( I2C2_ER_IRQn, 1);
  186. #if defined(I2CBUS2_USE_DMA)
  187. DMA_Init();
  188. DMA_Disable(I2C2_DMA_CHANNEL_TX);
  189. DMA_Disable(I2C2_DMA_CHANNEL_RX);
  190. #endif
  191. return 0;
  192. }
  193. /*!
  194. * \brief TWI/I2C bus structure.
  195. */
  196. NUTTWIBUS Stm32TwiBus_2 = {
  197. /*.bus_base = */ I2C2_BASE, /* Bus base address. */
  198. /*.bus_sig_ev = */ &sig_TWI2_EV, /* Bus data and event interrupt handler. */
  199. /*.bus_sig_er = */ &sig_TWI2_ER, /* Bus error interrupt handler. */
  200. /*.bus_mutex = */ NULL, /* Bus lock queue. */
  201. /*.bus_icb = */ NULL, /* Bus Runtime Data Pointer */
  202. #if defined(I2CBUS1_USE_DMA)
  203. /*.bus_dma_tx = */ I2C2_DMA_CHANNEL_TX, /* DMA channel for TX direction. */
  204. /*.bus_dma_rx = */ I2C2_DMA_CHANNEL_RX, /* DMA channel for RX direction. */
  205. #else
  206. /*.bus_dma_tx = */ 0,
  207. /*.bus_dma_rx = */ 0,
  208. #endif
  209. /*.bus_initbus = */ Stm32I2cBus2Init, /* Initialize bus controller. */
  210. /*.bus_recover = */ Stm32I2cBus2Recover, /* Recover bus in case a slave hangs with SCL low */
  211. };