stm32_twi1.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_twi1.c 5633 2014-04-07 13:58:20Z u_bonnes $
  38. * \endverbatim
  39. */
  40. /*!
  41. * \file arch/cm3/dev/stm/stm32_twi1.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 <sys/atom.h>
  50. #include <sys/event.h>
  51. #include <sys/timer.h>
  52. #include <dev/irqreg.h>
  53. #include <dev/gpio.h>
  54. #include <dev/twif.h>
  55. #include <arch/cm3/stm/stm32xxxx.h>
  56. #include <arch/cm3/stm/stm32_gpio.h>
  57. #if defined(I2CBUS1_USE_DMA)
  58. #if defined(MCU_STM32F1)
  59. #include <arch/cm3/stm/stm32f1_dma.h>
  60. #else
  61. #warning "Unhandled STM32 family"
  62. #endif
  63. #endif
  64. #include <arch/cm3/stm/stm32_twi.h>
  65. /*!
  66. * \brief I2CBUS1 GPIO configuartion and assignment.
  67. * F1/L1/F2/F4: SMBA PB5
  68. * SCL PB6 PB8
  69. * SDA PB7 PB9
  70. */
  71. #define I2C_PORT NUTGPIO_PORTB
  72. #if defined(MCU_STM32F1)
  73. #if defined(I2CBUS1_REMAP_I2C)
  74. #define I2CBUS1_SDA_PIN 9
  75. #define I2CBUS1_SCL_PIN 8
  76. #else /* I2CBUS1_REMAP_I2C */
  77. #define I2CBUS1_SDA_PIN 7
  78. #define I2CBUS1_SCL_PIN 6
  79. #endif /* I2CBUS1_REMAP_I2C */
  80. #else /*L1/F2/F4*/
  81. #if !defined(I2CBUS1_SDA_PIN)
  82. #if defined(I2CBUS1_REMAP_I2C)
  83. #define I2CBUS1_SDA_PIN 9
  84. #else
  85. #define I2CBUS1_SDA_PIN 7
  86. #endif
  87. #endif
  88. #if I2CBUS1_SDA_PIN != 7 && I2CBUS1_SDA_PIN != 9
  89. #warning "Illegal I2C1 SDA pin assignement"
  90. #endif
  91. #if !defined(I2CBUS1_SCL_PIN)
  92. #if defined(I2CBUS1_REMAP_I2C)
  93. #define I2CBUS1_SCL_PIN 8
  94. #else
  95. #define I2CBUS1_SCL_PIN 6
  96. #endif
  97. #endif
  98. #if I2CBUS1_SCL_PIN != 6 && I2CBUS1_SCL_PIN != 8
  99. #warning "Illegal I2C1 SCL pin assignement"
  100. #endif
  101. #endif
  102. #ifdef I2CBUS1_MODE_SMBUS
  103. #define SMBA_PIN 5
  104. #endif
  105. /*!
  106. * \brief Unlock a broken slave by clocking 8 SCL pulses manually.
  107. */
  108. int Stm32I2cBus1Recover( void)
  109. {
  110. uint_fast8_t i;
  111. /* Handle pins as GPIOs, set SCL low */
  112. GpioPortConfigSet( I2C_PORT, _BV(I2CBUS1_SDA_PIN) | _BV(I2CBUS1_SCL_PIN), GPIO_CFG_OUTPUT|GPIO_CFG_MULTIDRIVE);
  113. GpioPinSetLow( I2C_PORT, I2CBUS1_SDA_PIN);
  114. NutMicroDelay(10);
  115. /* Run sequence of 8 SCL clocks */
  116. for( i=0; i<9; i++) {
  117. GpioPinSetLow( I2C_PORT, I2CBUS1_SCL_PIN);
  118. NutMicroDelay(10);
  119. GpioPinSetHigh( I2C_PORT, I2CBUS1_SCL_PIN);
  120. NutMicroDelay(10);
  121. }
  122. /* Issue Stop condition on the bus */
  123. GpioPinSetHigh( I2C_PORT, I2CBUS1_SDA_PIN);
  124. NutMicroDelay(10);
  125. GpioPinSetHigh( I2C_PORT, I2CBUS1_SCL_PIN);
  126. GpioPortConfigSet(I2C_PORT, _BV(I2CBUS1_SDA_PIN) | _BV(I2CBUS1_SCL_PIN), GPIO_CFG_OUTPUT
  127. | GPIO_CFG_PERIPHAL
  128. | GPIO_CFG_MULTIDRIVE);
  129. return 0;
  130. }
  131. /*!
  132. * \brief Processor specific Hardware Initiliaization
  133. *
  134. */
  135. int Stm32I2cBus1Init(void)
  136. {
  137. uint16_t pins = _BV(I2CBUS1_SDA_PIN) | _BV(I2CBUS1_SCL_PIN);
  138. /* Enable I2C Bus 1 peripheral clock. */
  139. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
  140. /* Reset I2C Bus 1 IP */
  141. RCC->APB1RSTR |= RCC_APB1RSTR_I2C1RST;
  142. RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C1RST;
  143. /* Setup Related GPIOs. */
  144. #ifdef I2CBUS1_MODE_SMBUS
  145. pins |= _BV(SMBA_PIN);
  146. #endif
  147. GpioPortConfigSet( I2C_PORT, pins,
  148. GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL
  149. | GPIO_CFG_MULTIDRIVE |GPIO_CFG_INIT_HIGH);
  150. NVIC_SetPriorityGrouping(4);
  151. NVIC_SetPriority( I2C1_EV_IRQn, 0);
  152. NVIC_SetPriority( I2C1_ER_IRQn, 1);
  153. #ifdef I2CBUS1_MODE_SMBUS
  154. GPIO_PinAFConfig((GPIO_TypeDef*) I2C_PORT, SMBA_PIN, GPIO_AF_I2C1);
  155. #endif
  156. #if defined (MCU_STM32F1)
  157. /* Configure alternate configuration. */
  158. #if defined(I2CBUS1_REMAP_I2C)
  159. CM3BBSET(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_I2C1_REMAP));
  160. #else
  161. CM3BBCLR(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_I2C1_REMAP));
  162. #endif
  163. #elif defined (MCU_STM32L1) || defined (MCU_STM32F2) || defined (MCU_STM32F4)
  164. GPIO_PinAFConfig((GPIO_TypeDef*) I2C_PORT, I2CBUS1_SDA_PIN, GPIO_AF_I2C1);
  165. GPIO_PinAFConfig((GPIO_TypeDef*) I2C_PORT, I2CBUS1_SCL_PIN, GPIO_AF_I2C1);
  166. #else
  167. #warning "Unhandled STM32 family"
  168. #endif
  169. #if defined(I2CBUS1_USE_DMA)
  170. #if defined (MCU_STM32F1)
  171. DMA_Init();
  172. DMA_Disable(I2C1_DMA_CHANNEL_TX);
  173. DMA_Disable(I2C1_DMA_CHANNEL_RX);
  174. #else
  175. #warning "Unhandled STM32 family"
  176. #endif
  177. #endif
  178. return 0;
  179. }
  180. /*!
  181. * \brief TWI/I2C bus structure.
  182. */
  183. NUTTWIBUS Stm32TwiBus_1 = {
  184. /*.bus_base = */ I2C1_BASE, /* Bus base address. */
  185. /*.bus_sig_ev = */ &sig_TWI1_EV, /* Bus data and event interrupt handler. */
  186. /*.bus_sig_er = */ &sig_TWI1_ER, /* Bus error interrupt handler. */
  187. /*.bus_mutex = */ NULL, /* Bus lock queue. */
  188. /*.bus_icb = */ NULL, /* Bus Runtime Data Pointer */
  189. #if defined(I2CBUS1_USE_DMA)
  190. /*.bus_dma_tx = */ I2C1_DMA_CHANNEL_TX, /* DMA channel for TX direction. */
  191. /*.bus_dma_rx = */ I2C1_DMA_CHANNEL_RX, /* DMA channel for RX direction. */
  192. #else
  193. /*.bus_dma_tx = */ 0,
  194. /*.bus_dma_rx = */ 0,
  195. #endif
  196. /*.bus_initbus = */ Stm32I2cBus1Init, /* Initialize bus controller. */
  197. /*.bus_recover = */ Stm32I2cBus1Recover, /* Recover bus in case a slave hangs with SCL low */
  198. };