Advertisement
Guest User

Untitled

a guest
Jan 4th, 2018
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. /**
  2.   * @brief  This function sets up the I2C1 hardware as mentioned in the
  3.   *         hardware description in the header file. Afterwards I2C1 is
  4.   *         enabled.
  5.     *   @note       PB6 == SCL, PB7 == SDA
  6.   * @param  None
  7.   * @retval None
  8.   */
  9. void I2C_Setup(void)
  10. {
  11.   GPIO_InitTypeDef GPIO_InitStructure;
  12.   I2C_InitTypeDef  I2C_InitStructure;
  13.  
  14.   // Set I2C1 clock to SYSCLK (see system_stm32f0.c)
  15.   RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
  16.  
  17.   //(#) Enable peripheral clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE)
  18.   //    function for I2C1 or I2C2.
  19.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  20.  
  21.   //(#) Enable SDA, SCL  and SMBA (when used) GPIO clocks using
  22.   //    RCC_AHBPeriphClockCmd() function.
  23.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  24.  
  25.   //(#) Peripherals alternate function:
  26.   //    (++) Connect the pin to the desired peripherals' Alternate
  27.   //         Function (AF) using GPIO_PinAFConfig() function.
  28.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
  29.   GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
  30.  
  31.   //    (++) Configure the desired pin in alternate function by:
  32.   //         GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  33.   GPIO_StructInit(&GPIO_InitStructure);
  34.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  35.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  36.  
  37.   //    (++) Select the type, OpenDrain and speed via  
  38.   //         GPIO_PuPd, GPIO_OType and GPIO_Speed members
  39.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  40.   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  41.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  42.  
  43.   //    (++) Call GPIO_Init() function.
  44.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  45.  
  46.   //(#) Program the Mode, Timing , Own address, Ack and Acknowledged Address
  47.   //    using the I2C_Init() function.
  48.   I2C_StructInit(&I2C_InitStructure);
  49.   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  50.   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  51.   I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  52.   I2C_InitStructure.I2C_DigitalFilter = 0;
  53.   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  54.   I2C_InitStructure.I2C_OwnAddress1 = 0;
  55. //I2C_InitStructure.I2C_Timing = 0x00310309; // ~400 kHz. @ 8 MHz (HSI) see Ref. Man. Table 91
  56. //I2C_InitStructure.I2C_Timing = 0x50330309; // ~400 kHz. @ 48 MHz (SYSCLK) see Ref. Man. Table 93
  57. // I2C_InitStructure.I2C_Timing = 0x2033030A; // =400 kHz. @ 48 MHz (SYSCLK) measured with Logic Analyzer
  58.     I2C_InitStructure.I2C_Timing = 0xB0420F13; // =100 kHz. @ 48 MHz (SYSCLK) See Table 93
  59.  
  60.   I2C_Init(I2C1, &I2C_InitStructure);
  61.  
  62.   //(#) Optionally you can enable/configure the following parameters without
  63.   //    re-initialization (i.e there is no need to call again I2C_Init() function):
  64.   //    (++) Enable the acknowledge feature using I2C_AcknowledgeConfig() function.
  65.   //    (++) Enable the dual addressing mode using I2C_DualAddressCmd() function.
  66.   //    (++) Enable the general call using the I2C_GeneralCallCmd() function.
  67.   //    (++) Enable the clock stretching using I2C_StretchClockCmd() function.
  68.   //    (++) Enable the PEC Calculation using I2C_CalculatePEC() function.
  69.   //    (++) For SMBus Mode:
  70.   //         (+++) Enable the SMBusAlert pin using I2C_SMBusAlertCmd() function.
  71.    
  72.   //(#) Enable the NVIC and the corresponding interrupt using the function
  73.   //    I2C_ITConfig() if you need to use interrupt mode.
  74.  
  75.   //(#) When using the DMA mode
  76.   //   (++) Configure the DMA using DMA_Init() function.
  77.   //   (++) Active the needed channel Request using I2C_DMACmd() function.
  78.  
  79.   //(#) Enable the I2C using the I2C_Cmd() function.
  80.   I2C_Cmd(I2C1, ENABLE);
  81.  
  82.   //(#) Enable the DMA using the DMA_Cmd() function when using DMA mode in the
  83.   //    transfers.
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement