/** * @brief Codec_TIMEOUT_UserCallback * @param None * @retval None */ uint32_t Codec_TIMEOUT_UserCallback(void) { #if 0 I2C_InitTypeDef I2C_InitStructure; LCD_ErrLog("> I2C Timeout error (CS43L22)\n"); I2C_GenerateSTOP(I2C1, ENABLE); I2C_SoftwareResetCmd(I2C1, ENABLE); I2C_SoftwareResetCmd(I2C1, DISABLE); I2C_DeInit(I2C1); /* CODEC_I2C peripheral configuration */ I2C_DeInit(I2C1); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x33; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* Enable the I2C peripheral */ I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, &I2C_InitStructure); LCD_UsrLog("> I2C error recovered.\n"); #endif return 0; }
void cpu_deinit_all() { //** ADC_DeInit(LPC_ADC); CAN_DeInit(LPC_CAN1); CAN_DeInit(LPC_CAN2); EMAC_DeInit(); EXTI_DeInit(); I2C_DeInit(LPC_I2C0); I2C_DeInit(LPC_I2C1); I2C_DeInit(LPC_I2C2); I2S_DeInit(LPC_I2S); NVIC_DeInit(); NVIC_SCBDeInit(); PWM_DeInit(LPC_PWM1); QEI_DeInit(LPC_QEI); //** RIT_DeInit(LPC_RIT); RTC_DeInit(LPC_RTC); SPI_DeInit(LPC_SPI); SSP_DeInit(LPC_SSP0); SSP_DeInit(LPC_SSP1); UART_DeInit(LPC_UART0); UART_DeInit((LPC_UART_TypeDef *) LPC_UART1); //** UART_DeInit(LPC_UART2); //** UART_DeInit(LPC_UART3); TIM_DeInit(LPC_TIM0); TIM_DeInit(LPC_TIM1); //** TIM_DeInit(LPC_TIM2); //** TIM_DeInit(LPC_TIM3); }
void Init_I2C(void){ I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable I2C and GPIO clocks */ RCC_APB1PeriphClockCmd(I2C_RCC_Periph, ENABLE); RCC_AHB1PeriphClockCmd(I2C_RCC_Port, ENABLE); /* Configure I2C pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = I2C_SCL_Pin | I2C_SDA_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType=GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_Init(I2C_Port, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); I2C_DeInit(I2C1); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x68<<1; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; /* Apply I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStructure); /* I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); }
/************************************************* 名称:iic_init(void) 功能:iic外设2初始化(管脚配置 总线速度 数据位宽) 输入参数:无 输出参数:无 返回值: 无 **************************************************/ void iic_init(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); /* PB6,7 SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C1); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x30; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 300000; //200K I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, &I2C_InitStructure); /*允许1字节1应答模式*/ I2C_AcknowledgeConfig(I2C1, ENABLE); }
void I2CHW_Reset(void) { __IO uint32_t Timeout; //uint8_t i; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C1); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, DISABLE); // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_Init(GPIOB, &GPIO_InitStructure); // // GPIO_SetBits(GPIOB,GPIO_Pin_9); // GPIO_SetBits(GPIOB,GPIO_Pin_8); // Timeout=0x1ff; // while(Timeout--); I2C_Configuration(); Timeout=0x1ff; while(Timeout--); }
void i2cSetup(uint32_t speed) { I2C_InitTypeDef i2cConfig; // GPIO Init RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); GPIO_Init(GPIOB, &(GPIO_InitTypeDef){GPIO_Pin_6, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_NOPULL}); GPIO_Init(GPIOB, &(GPIO_InitTypeDef){GPIO_Pin_9, GPIO_Mode_AF, GPIO_Speed_50MHz, GPIO_OType_OD, GPIO_PuPd_NOPULL}); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // I2C init I2C_StructInit(&i2cConfig); // TODO - Figure out why the speed isn't being set porperly i2cConfig.I2C_ClockSpeed = speed; i2cConfig.I2C_DutyCycle = I2C_DutyCycle_16_9; I2C_DeInit(I2C1); I2C_Init(I2C1, &i2cConfig); I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); NVIC_EnableIRQ(I2C1_ER_IRQn); I2C_Cmd(I2C1, ENABLE); }
void init_i2c(void) { I2C_InitTypeDef I2C_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // SCL,SDA GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStruct); I2C_SoftwareResetCmd(I2C2, ENABLE); I2C_ITConfig(I2C2,I2C_IT_ERR,ENABLE); I2C_StructInit(&I2C_InitStruct); I2C_DeInit(I2C2); I2C_InitStruct.I2C_ClockSpeed = 100000; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0x00; I2C_Init(I2C2, &I2C_InitStruct); I2C_Cmd(I2C2, ENABLE); }
/** * @brief DeInitializes the LM75_I2C. * @param None * @retval None */ void LM75_LowLevel_DeInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /*!< Disable LM75_I2C */ I2C_Cmd(LM75_I2C, DISABLE); /*!< DeInitializes the LM75_I2C */ I2C_DeInit(LM75_I2C); /*!< LM75_I2C Periph clock disable */ RCC_APB1PeriphClockCmd(LM75_I2C_CLK, DISABLE); /*!< Configure LM75_I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = LM75_I2C_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(LM75_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); /*!< Configure LM75_I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = LM75_I2C_SDA_PIN; GPIO_Init(LM75_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); /*!< Configure LM75_I2C pin: SMBUS ALERT */ GPIO_InitStructure.GPIO_Pin = LM75_I2C_SMBUSALERT_PIN; GPIO_Init(LM75_I2C_SMBUSALERT_GPIO_PORT, &GPIO_InitStructure); }
/** * @brief DeInitializes the TS751_I2C. * @param None * @retval None */ void TS751_LowLevel_DeInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Disable TS751_I2C */ I2C_Cmd(TS751_I2C, DISABLE); /* DeInitializes the TS751_I2C */ I2C_DeInit(TS751_I2C); /* TS751_I2C Periph clock disable */ RCC_APB1PeriphClockCmd(TS751_I2C_CLK, DISABLE); /* Configure TS751_I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = TS751_I2C_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(TS751_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); /* Configure TS751_I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = TS751_I2C_SDA_PIN; GPIO_Init(TS751_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); /* Configure TS751_I2C pin: SMBUS ALERT */ GPIO_InitStructure.GPIO_Pin = TS751_I2C_SMBUSALERT_PIN; GPIO_Init(TS751_I2C_SMBUSALERT_GPIO_PORT, &GPIO_InitStructure); }
/******************************************************************************* * Function Name : Mems_Init * Description : It inits mems * Input : None * Output : status * Return : None *******************************************************************************/ int8u Mems_Init(void) { int8u ret = 0; // GPIO assignments // PA1: SC2SDA (Serial Data) // PA2: SC2SCL (Serial Clock) //-----SC2 I2C Master GPIO configuration TIM2_CCER &= 0xFFFFEEEE; SC2_MODE = SC2_MODE_I2C; GPIO_PACFGL &= 0xFFFFF00F; GPIO_PACFGL |= 0x00000DD0; SC2_RATELIN = 14; // generates standard 100kbps or 400kbps SC2_RATEEXP = 1; // 3 yields 100kbps; 1 yields 400kbps SC2_TWICTRL1 = 0; // start from a clean state SC2_TWICTRL2 = 0; // start from a clean state ret = I2C_MEMS_Init(); fullscale_state = MEMS_LOW_RANGE; //Add later if really needed #ifdef ST_DBG if (!ret) I2C_DeInit(MEMS_I2C); #endif return ret; }/* end Mems_Init */
void I2cInit( void ) { I2C_DeInit( I2C_INTERFACE ); I2cGpioConfig( ); I2cConfig( ); }
void vhI2C_initI2C1(void) { I2C_InitTypeDef I2C_InitStruct; /* I2C1 * setup SCL and SDA pins * SCL = PB8 * SDA = PB9 */ /* Deinit and reset the I2C to avoid it locking up */ I2C_DeInit(I2C1 ); I2C_SoftwareResetCmd(I2C1, ENABLE); I2C_SoftwareResetCmd(I2C1, DISABLE); I2C_InitStruct.I2C_ClockSpeed = I2C1_clockSpeed; // I2C1_clockSpeed [Hz] I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = I2C1_ownAddress; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 I2C_Cmd(I2C1, ENABLE); }
/******************************************************************************* * Function Name : I2C_Configuration * Description : I2C Configuration * Input : None * Output : None * Return : None *******************************************************************************/ void I2C_Configuration(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Configure I2C1 pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; I2C_DeInit(I2C1); /* I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStructure); }
void i2c_frequency(i2c_t *obj, int hz) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; // Disable the Fast Mode Plus capability RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE); SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE); /* Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) * Standard mode (up to 100 kHz) * Fast Mode (up to 400 kHz) * Fast Mode Plus (up to 1 MHz) Below values obtained with: - I2C clock source = 8 MHz (HSI clock per default) - Analog filter delay = ON - Digital filter coefficient = 0 - Rise time = 100 ns - Fall time = 10ns */ switch (hz) { case 100000: tim = 0x00201D2B; // Standard mode break; case 200000: tim = 0x0010021E; // Fast Mode break; case 400000: tim = 0x0010020A; // Fast Mode break; case 1000000: tim = 0x00100001; // Fast Mode Plus // Enable the Fast Mode Plus capability if (obj->i2c == I2C_1) { SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); } if (obj->i2c == I2C_2) { SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); } break; default: error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); break; } // I2C configuration I2C_DeInit(i2c); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStructure.I2C_DigitalFilter = 0x00; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_Timing = tim; I2C_Init(i2c, &I2C_InitStructure); I2C_Cmd(i2c, ENABLE); }
//*---------------------------------------------------------------------------- //* Function Name : mchf_hw_i2c2_init //* Object : //* Object : //* Input Parameters : //* Output Parameters : //* Functions called : //*---------------------------------------------------------------------------- void mchf_hw_i2c2_init(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; // CODEC_I2C SCL and SDA pins configuration GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN|I2C2_SDA_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // we also have ext pullups GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure); // Connect pins to I2C peripheral GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PINSRC, CODEC_I2C_GPIO_AF); GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PINSRC, CODEC_I2C_GPIO_AF); // Enable the CODEC_I2C peripheral clock RCC_APB1PeriphClockCmd(I2C2_CLK, ENABLE); // CODEC_I2C peripheral configuration I2C_DeInit(CODEC_I2C); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x33; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C2_SPEED; // Enable the I2C peripheral I2C_Cmd (CODEC_I2C, ENABLE); I2C_Init(CODEC_I2C, &I2C_InitStructure); }
/******************************************************************************* * Function Name : I2Cx_GPIO_Config * Description : * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void CODEC_I2C_Configuration(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* If the I2C peripheral is already enabled, don't reconfigure it */ RCC_AHB1PeriphClockCmd(I2C_x_SDA_GPIO_CLK | I2C_x_SCL_GPIO_CLK,ENABLE); RCC_APB1PeriphClockCmd(I2C_x_CLK,ENABLE); GPIO_PinAFConfig(I2C_x_SDA_GPIO_PORT, I2C_x_SDA_SOURCE, I2C_x_SDA_AF); GPIO_PinAFConfig(I2C_x_SCL_GPIO_PORT, I2C_x_SCL_SOURCE, I2C_x_SCL_AF); GPIO_InitStructure.GPIO_Pin = I2C_x_SDA_PIN | I2C_x_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(I2C_x_SDA_GPIO_PORT, &GPIO_InitStructure); I2C_DeInit(I2C_x); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2Cx_SLAVE_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2Cx_SPEED; I2C_Init(I2C_x, &I2C_InitStructure); //I2C_AcknowledgeConfig(I2C_x, ENABLE); I2C_Cmd(I2C_x, ENABLE); }
/** * @fn void System_I2CConfig(void) * @brief * @li I2C 설정한다. * @remarks * @param void * @return void */ void System_I2CConfig(void) { GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStructure; /* 클럭 활성화 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* I2C1 포트 초기화 */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // SDA/SCL : Alternate function Open drain GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStruct); /* I2C 포트 초기화 */ /* I2C_InitStructure 초기화 */ I2C_DeInit(I2C1); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 100000; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1,ENABLE); // I2C_SendData(I2C1, 0x34); }
/** * @brief Initializes the Audio Codec control interface (I2C). * @param None. * @retval None. */ static void Codec_CtrlInterface_Init(void) { I2C_InitTypeDef I2C_InitStructure; /* Enable the CODEC_I2C peripheral clock */ RCC_APB1PeriphClockCmd(CODEC_I2C_CLK, ENABLE); /* If the I2C peripheral is already enabled, don't reconfigure it */ if (CODEC_I2C->CR1 & I2C_CR1_PE) { /* CODEC_I2C peripheral configuration */ I2C_DeInit(CODEC_I2C); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x33; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* Enable the I2C peripheral */ I2C_Cmd(CODEC_I2C, ENABLE); /* Initialize the I2C peripheral */ I2C_Init(CODEC_I2C, &I2C_InitStructure); } }
void i2c_frequency(i2c_t *obj, int hz) { MBED_ASSERT((hz == 100000) || (hz == 200000) || (hz == 400000) || (hz == 1000000)); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; uint32_t tim = 0; // Disable the Fast Mode Plus capability RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); // Enable SYSCFG clock SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, DISABLE); SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, DISABLE); /* Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) * Standard mode (up to 100 kHz) * Fast Mode (up to 400 kHz) * Fast Mode Plus (up to 1 MHz) Below values obtained with: - I2C clock source = 48 MHz (System Clock) - Analog filter delay = ON - Digital filter coefficient = 0 - Rise time = 100 ns - Fall time = 10ns */ switch (hz) { case 100000: tim = 0x10805E89; // Standard mode break; case 200000: tim = 0x00905E82; // Fast Mode break; case 400000: tim = 0x00901850; // Fast Mode break; case 1000000: tim = 0x00700818; // Fast Mode Plus // Enable the Fast Mode Plus capability if (obj->i2c == I2C_1) { SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C1, ENABLE); } if (obj->i2c == I2C_2) { SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_I2C2, ENABLE); } break; default: break; } // I2C configuration I2C_DeInit(i2c); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStructure.I2C_DigitalFilter = 0x00; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_Timing = tim; I2C_Init(i2c, &I2C_InitStructure); I2C_Cmd(i2c, ENABLE); }
static void i2c_configuration( void ) { GPIO_InitTypeDef gpio_init_structure; I2C_InitTypeDef i2c_init_structure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE); /* PB6-I2C1_SCL、PB7-I2C1_SDA*/ gpio_init_structure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; gpio_init_structure.GPIO_Speed = GPIO_Speed_10MHz; gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_OD; // 开漏输出; GPIO_Init( GPIOB, &gpio_init_structure); /*********** I2C periphral configuration **********/ I2C_DeInit(I2C1); i2c_init_structure.I2C_Mode = I2C_Mode_I2C; i2c_init_structure.I2C_DutyCycle = I2C_DutyCycle_2;//快速模式时的占空比; i2c_init_structure.I2C_OwnAddress1 = I2C1_MPU6050; i2c_init_structure.I2C_Ack = I2C_Ack_Enable; i2c_init_structure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; i2c_init_structure.I2C_ClockSpeed = I2C_SPEED; I2C_Cmd(I2C1, ENABLE); I2C_Init( I2C1, &i2c_init_structure); I2C_AcknowledgeConfig( I2C1, ENABLE); /************** I2C NVIC configuration *************************/ }
void MPU9250_I2C_Init(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable I2C and GPIO clocks */ RCC_APB1PeriphClockCmd(MPU9250_I2C_RCC_Periph, ENABLE); RCC_APB2PeriphClockCmd(MPU9250_I2C_RCC_Port, ENABLE); /* Configure I2C pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = MPU9250_I2C_SCL_Pin | MPU9250_I2C_SDA_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(MPU9250_I2C_Port, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = MPU9250_INT_PIN; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // GPIO_Init(MPU9250_INT_PORT, &GPIO_InitStructure); I2C_DeInit(MPU9250_I2C); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0xA0; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = MPU9250_I2C_Speed; /* Apply I2C configuration after enabling it */ I2C_Init(MPU9250_I2C, &I2C_InitStructure); /* I2C Peripheral Enable */ I2C_Cmd(MPU9250_I2C, ENABLE); }
void I2C_Configuration(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); /*STM32F103C8T6芯片的硬件I2C: PB6 -- SCL; PB7 -- SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//I2C必须开漏输出 GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C1);//使用I2C1 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x30;//主机的I2C地址,随便写的 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 400000;//400K I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, &I2C_InitStructure); }
/** * @brief Main program. * @param None * @retval None */ void main(void) { /* I2C clock Enable*/ CLK_PeripheralClockConfig(CLK_Peripheral_I2C, ENABLE); /* system_clock / 1 */ CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1); /* Initialize LEDs mounted on the EVAL board */ STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); I2C_DeInit(); /* Initialize I2C peripheral */ #ifdef I2C_slave_7Bits_Address I2C_Init(100000, SLAVE_ADDRESS, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); #else I2C_Init(100000, SLAVE_ADDRESS, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_10bit); #endif /* Enable Error Interrupt*/ I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE); /* Enable general interrupts */ enableInterrupts(); /*Main Loop */ while (1) { /* infinite loop */ } }
// ================================ Init & Add Cmd ============================= void i2cMgr_t::Init() { // Init GPIO - SCL & SDA klGpioSetupByMsk(GPIOB, GPIO_Pin_6 | GPIO_Pin_7, GPIO_Mode_AF_OD); // I2C RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // I2C clock I2C_DeInit(I2C1); I2C_InitTypeDef I2C_InitStructure; I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x30; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_CLOCK_FREQ; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); #ifndef I2C_POLL_ONLY // ==== Init manager ==== CmdToWrite = &Commands[0]; CmdToRead = &Commands[0]; // Reset stage of all commands for (uint8_t i=0; i<I2C_CMD_QUEUE_LENGTH; i++) Commands[i].State = CmdSucceded; // ==== Init DMA ==== RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); IsError = false; #endif }
void TwoWire::begin(uint8_t address) { if (onBeginCallback) onBeginCallback(); status = SLAVE_IDLE; if (twi==I2C1) { RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); pinMode(SDA, ALTERNATE); pinMode(SCL, ALTERNATE); } I2C_DeInit(twi); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStructure.I2C_DigitalFilter = 0x00; I2C_InitStructure.I2C_OwnAddress1 = address; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_Timing = 0x00E0D3FF; I2C_Init(twi, &I2C_InitStructure); NVIC_SetPriority(I2C1_IRQn, 0); NVIC_EnableIRQ(I2C1_IRQn); I2C_Cmd(twi, ENABLE); I2C_ITConfig(twi, I2C_IT_ADDRI, ENABLE); }
void AT24Cxx_init(struct AT24Cxx_init_struct* init) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Configure clocks */ RCC_APB1PeriphClockCmd(init->I2C_clock, ENABLE); RCC_APB2PeriphClockCmd(init->GPIO_clock | RCC_APB2Periph_AFIO , ENABLE); /* Configure I2C pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = init->GPIO_SCL_pin | init->GPIO_SDA_pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(init->GPIO_peripheral, &GPIO_InitStructure); /* Configure I2C */ I2C_DeInit(init->I2C_peripheral); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = init->I2C_master_address; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 10000; I2C_Cmd(init->I2C_peripheral, ENABLE); I2C_Init(init->I2C_peripheral, &I2C_InitStructure); I2C_AcknowledgeConfig(init->I2C_peripheral, ENABLE); }
void i2c1_hw_init(void) { i2c1.reg_addr = I2C1; i2c1.init_struct = &I2C1_InitStruct; i2c1.scl_pin = GPIO_Pin_6; i2c1.sda_pin = GPIO_Pin_7; i2c1.errors = &i2c1_errors; /* zeros error counter */ ZEROS_ERR_COUNTER(i2c1_errors); // Extra #ifdef I2C_DEBUG_LED LED_INIT(); #else /* reset peripheral to default state ( sometimes not achieved on reset :( ) */ //I2C_DeInit(I2C1); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitTypeDef NVIC_InitStructure; /* Configure and enable I2C1 event interrupt --------------------------------*/ NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure and enable I2C1 err interrupt ----------------------------------*/ NVIC_InitStructure.NVIC_IRQChannel = I2C1_ER_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable peripheral clocks -------------------------------------------------*/ /* Enable I2C1 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* Enable GPIOB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = i2c1.scl_pin | i2c1.sda_pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C1); // enable peripheral I2C_Cmd(I2C1, ENABLE); I2C_Init(I2C1, i2c1.init_struct); // enable error interrupts I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); #endif }
/** * @brief IOE_TIMEOUT_UserCallback * @param None * @retval 0 */ uint8_t IOE_TimeoutUserCallback(void) { I2C_InitTypeDef I2C_InitStructure; I2C_GenerateSTOP(IOE_I2C, ENABLE); I2C_SoftwareResetCmd(IOE_I2C, ENABLE); I2C_SoftwareResetCmd(IOE_I2C, DISABLE); IOE_GPIO_Config(); /* CODEC_I2C peripheral configuration */ I2C_DeInit(IOE_I2C); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* Enable the I2C peripheral */ I2C_Cmd(IOE_I2C, ENABLE); I2C_Init(IOE_I2C, &I2C_InitStructure); return 0; }
void I2cMcuDeInit( I2c_t *obj ) { I2C_DeInit( obj->I2c ); GpioInit( &obj->Scl, obj->Scl.pin, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 ); GpioInit( &obj->Sda, obj->Sda.pin, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 ); }
/** * @brief Initializes the I2C peripheral used to drive the MPU6050 * @param None * @return None */ void MPU6050_I2C_Init() { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; I2C_DeInit( I2C2 ); /* Enable I2C and GPIO clocks */ RCC_APB1PeriphClockCmd( MPU6050_I2C_RCC_Periph, ENABLE ); RCC_APB2PeriphClockCmd( MPU6050_I2C_RCC_Port, ENABLE ); /* Configure I2C pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = MPU6050_I2C_SCL_Pin | MPU6050_I2C_SDA_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init( MPU6050_I2C_Port, &GPIO_InitStructure ); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = MPU6050_DEFAULT_ADDRESS; /* MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0; */ I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = MPU6050_I2C_Speed; /* Apply I2C configuration after enabling it */ I2C_Init( MPU6050_I2C, &I2C_InitStructure ); /* I2C Peripheral Enable */ I2C_Cmd( MPU6050_I2C, ENABLE ); }