Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_ll_i2c.c
  4.   * @author  MCD Application Team
  5.   * @brief   I2C LL module driver.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. #if defined(USE_FULL_LL_DRIVER)
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f1xx_ll_i2c.h"
  23. #include "stm32f1xx_ll_bus.h"
  24. #include "stm32f1xx_ll_rcc.h"
  25. #ifdef  USE_FULL_ASSERT
  26. #include "stm32_assert.h"
  27. #else
  28. #define assert_param(expr) ((void)0U)
  29. #endif
  30.  
  31. /** @addtogroup STM32F1xx_LL_Driver
  32.   * @{
  33.   */
  34.  
  35. #if defined (I2C1) || defined (I2C2)
  36.  
  37. /** @defgroup I2C_LL I2C
  38.   * @{
  39.   */
  40.  
  41. /* Private types -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private constants ---------------------------------------------------------*/
  44. /* Private macros ------------------------------------------------------------*/
  45. /** @addtogroup I2C_LL_Private_Macros
  46.   * @{
  47.   */
  48.  
  49. #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
  50.                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
  51.                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
  52.                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
  53.  
  54. #define IS_LL_I2C_CLOCK_SPEED(__VALUE__)           (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
  55.  
  56. #define IS_LL_I2C_DUTY_CYCLE(__VALUE__)            (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
  57.                                                  ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
  58.  
  59. #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
  60.  
  61. #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
  62.                                                  ((__VALUE__) == LL_I2C_NACK))
  63.  
  64. #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
  65.                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
  66. /**
  67.   * @}
  68.   */
  69.  
  70. /* Private function prototypes -----------------------------------------------*/
  71.  
  72. /* Exported functions --------------------------------------------------------*/
  73. /** @addtogroup I2C_LL_Exported_Functions
  74.   * @{
  75.   */
  76.  
  77. /** @addtogroup I2C_LL_EF_Init
  78.   * @{
  79.   */
  80.  
  81. /**
  82.   * @brief  De-initialize the I2C registers to their default reset values.
  83.   * @param  I2Cx I2C Instance.
  84.   * @retval An ErrorStatus enumeration value:
  85.   *          - SUCCESS  I2C registers are de-initialized
  86.   *          - ERROR  I2C registers are not de-initialized
  87.   */
  88. uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
  89. {
  90.   ErrorStatus status = SUCCESS;
  91.  
  92.   /* Check the I2C Instance I2Cx */
  93.   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  94.  
  95.   if (I2Cx == I2C1)
  96.   {
  97.     /* Force reset of I2C clock */
  98.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
  99.  
  100.     /* Release reset of I2C clock */
  101.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
  102.   }
  103. #if defined(I2C2)
  104.   else if (I2Cx == I2C2)
  105.   {
  106.     /* Force reset of I2C clock */
  107.     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
  108.  
  109.     /* Release reset of I2C clock */
  110.     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
  111.  
  112.   }
  113. #endif /* I2C2 */
  114.   else
  115.   {
  116.     status = ERROR;
  117.   }
  118.  
  119.   return status;
  120. }
  121.  
  122. /**
  123.   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
  124.   * @param  I2Cx I2C Instance.
  125.   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
  126.   * @retval An ErrorStatus enumeration value:
  127.   *          - SUCCESS  I2C registers are initialized
  128.   *          - ERROR  Not applicable
  129.   */
  130. uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
  131. {
  132.   LL_RCC_ClocksTypeDef rcc_clocks;
  133.  
  134.   /* Check the I2C Instance I2Cx */
  135.   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
  136.  
  137.   /* Check the I2C parameters from I2C_InitStruct */
  138.   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
  139.   assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
  140.   assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
  141.   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
  142.   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
  143.   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
  144.  
  145.   /* Disable the selected I2Cx Peripheral */
  146.   LL_I2C_Disable(I2Cx);
  147.  
  148.   /* Retrieve Clock frequencies */
  149.   LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  150.  
  151.   /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
  152.    * Configure the SCL speed :
  153.    * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
  154.    *           and I2C_CCR_CCR[11:0] bits
  155.    * - DutyCycle: I2C_CCR_DUTY[7:0] bits
  156.    */
  157.   LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
  158.  
  159.   /*---------------------------- I2Cx OAR1 Configuration -----------------------
  160.    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
  161.    * - OwnAddress1:  I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
  162.    * - OwnAddrSize:  I2C_OAR1_ADDMODE bit
  163.    */
  164.   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
  165.  
  166.   /*---------------------------- I2Cx MODE Configuration -----------------------
  167.   * Configure I2Cx peripheral mode with parameter :
  168.    * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
  169.    */
  170.   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
  171.  
  172.   /* Enable the selected I2Cx Peripheral */
  173.   LL_I2C_Enable(I2Cx);
  174.  
  175.   /*---------------------------- I2Cx CR2 Configuration ------------------------
  176.    * Configure the ACKnowledge or Non ACKnowledge condition
  177.    * after the address receive match code or next received byte with parameter :
  178.    * - TypeAcknowledge: I2C_CR2_NACK bit
  179.    */
  180.   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
  181.  
  182.   return SUCCESS;
  183. }
  184.  
  185. /**
  186.   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
  187.   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
  188.   * @retval None
  189.   */
  190. void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
  191. {
  192.   /* Set I2C_InitStruct fields to default values */
  193.   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
  194.   I2C_InitStruct->ClockSpeed      = 5000U;
  195.   I2C_InitStruct->DutyCycle       = LL_I2C_DUTYCYCLE_2;
  196.   I2C_InitStruct->OwnAddress1     = 0U;
  197.   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
  198.   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
  199. }
  200.  
  201. /**
  202.   * @}
  203.   */
  204.  
  205. /**
  206.   * @}
  207.   */
  208.  
  209. /**
  210.   * @}
  211.   */
  212.  
  213. #endif /* I2C1 || I2C2 */
  214.  
  215. /**
  216.   * @}
  217.   */
  218.  
  219. #endif /* USE_FULL_LL_DRIVER */
  220.  
  221. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  222.