/**************************************************************************************************** * @fn I2C_Master_Initialise * Call this function to set up the I2C master to its initial standby state. * Remember to enable interrupts from the main application after initializing the I2C. * * @param none * * @return none * ***************************************************************************************************/ void I2C_Master_Initialise(void) { I2C_InitTypeDef I2C_InitStructure; /* Configure the I2C interface */ I2C_Cmd(I2C_SENSOR_BUS, ENABLE); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0; //Don't care in master mode I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SENSOR_BUS_CLOCK; I2C_Init(I2C_SENSOR_BUS, &I2C_InitStructure); asyncXfer.i2c_txrx_status = I2C_TXRX_STATUS_PASSED; //initialize last comm status /* Note: I2C bus event and error interrupts are enabled when tx is started */ }
/** * @brief Initializes the LM75_I2C. * @param None * @retval None */ void LM75_Init(void) { LM75_LowLevel_Init(); /* I2C DeInit */ I2C_DeInit(LM75_I2C); /* I2C configuration */ I2C_Init(LM75_I2C, LM75_I2C_SPEED, 0x00, I2C_Mode_SMBusHost, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); /*!< Enable SMBus Alert interrupt */ I2C_ITConfig(LM75_I2C, I2C_IT_ERR, ENABLE); /*!< LM75_I2C Init */ I2C_Cmd(LM75_I2C, ENABLE); }
/* * 函数名:I2C_Mode_Config * 描述 :I2C 工作模式配置 * 输入 :无 * 输出 :无 * 调用 :内部调用 */ static void I2C_Mode_Config(void) { /* Initialize the I2C1 according to the I2C_InitStructure members */ I2C_InitTypeDef I2C_InitStructure; /* I2C 配置 */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ; //I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //I2C_InitStructure.I2C_OwnAddress1 = SlaveAddress; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 350000; /* I2C1 初始化 */ I2C_Init(I2C1, &I2C_InitStructure); /* 使能 I2C1 */ I2C_Cmd(I2C1, ENABLE); /*允许应答模式*/ I2C_AcknowledgeConfig(I2C1, ENABLE); }
//-------------------------------------------------------------- // interne Funktion // Init der I2C-Schnittstelle //-------------------------------------------------------------- void P_I2C3_InitI2C(void) { I2C_InitTypeDef I2C_InitStructure; // I2C-Konfiguration 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 = I2C3_CLOCK_FRQ; // I2C enable I2C_Cmd(I2C3, ENABLE); // Init Struktur I2C_Init(I2C3, &I2C_InitStructure); }
/* I2C Functions */ u32 platform_i2c_setup( unsigned id, u32 speed ) { GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(i2c_rcc[id], ENABLE); // enable clock for SCL and SDA pins RCC_AHB1PeriphClockCmd(i2c_port_rcc[id], ENABLE); I2C_DeInit(i2c[id]); /* setup SCL and SDA pins * You can connect I2C1 to two different * pairs of pins: * 1. SCL on PB6 and SDA on PB7 * 2. SCL on PB8 and SDA on PB9 */ GPIO_InitStruct.GPIO_Pin = i2c_scl_pin[id] | i2c_sda_pin[id]; // pins to use GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // set pins to alternate function GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors GPIO_Init(i2c_port[id], &GPIO_InitStruct); // init GPIO // Connect I2C1 pins to AF GPIO_PinAFConfig(i2c_port[id], i2c_scl_pinsource[id], i2c_af[id]); // SCL GPIO_PinAFConfig(i2c_port[id], i2c_sda_pinsource[id], i2c_af[id]); // SDA // configure I2C1 I2C_StructInit(&I2C_InitStruct); I2C_InitStruct.I2C_ClockSpeed = speed; //set speed (100kHz or 400kHz) I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on) I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(i2c[id], &I2C_InitStruct); // init I2C1 // enable I2C1 I2C_Cmd(i2c[id], ENABLE); return speed; }
void i2cInit(I2C_TypeDef *I2C) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; // Init pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); I2Cx = I2C; // clock out stuff to make sure slaves arent stuck i2cUnstick(); // Init I2C I2C_DeInit(I2Cx); I2C_StructInit(&I2C_InitStructure); I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); //Enable EVT and ERR interrupts - they are enabled by the first request I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 400000; I2C_Cmd(I2Cx, ENABLE); I2C_Init(I2Cx, &I2C_InitStructure); NVIC_PriorityGroupConfig(0x500); // I2C ER Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // I2C EV Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C2_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); }
u8 Temp_Read(void) { u8 MSB,LSB; //等待I2C閒置 while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)); I2C_AcknowledgeConfig(I2C1,ENABLE); //啟動訊號 I2C_GenerateSTART(I2C1,ENABLE); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS); //發送裝置位置 I2C_Send7bitAddress(I2C1,0xF0,I2C_Direction_Transmitter); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS); I2C_Cmd(I2C1,ENABLE); //發送記憶體位置 I2C_SendData(I2C1,0xE5); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS); //啟動訊號 I2C_GenerateSTART(I2C1,ENABLE); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS); //發送裝置位置 I2C_Send7bitAddress(I2C1,0xF0,I2C_Direction_Receiver); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)!=SUCCESS); //I2C_AcknowledgeConfig(I2C1,DISABLE); //ACK 關閉 MSB=I2C_ReceiveData(I2C1); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)!=SUCCESS); LSB=I2C_ReceiveData(I2C1); while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)!=SUCCESS); //停止訊號 I2C_GenerateSTOP(I2C1,ENABLE); USART_SendData(USART1,MSB); }
/** * @brief DeInitializes peripherals used by the I2C EEPROM driver. * @param None * @retval None */ void sEE_LowLevel_DeInit(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* sEE_I2C Peripheral Disable */ I2C_Cmd(sEE_I2C, DISABLE); /* sEE_I2C DeInit */ I2C_DeInit(sEE_I2C); /*!< sEE_I2C Periph clock disable */ RCC_APB1PeriphClockCmd(sEE_I2C_CLK, DISABLE); /*!< GPIO configuration */ /*!< Configure sEE_I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); /*!< Configure sEE_I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN; GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); /* Configure and enable I2C DMA TX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); /* Configure and enable I2C DMA RX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_RX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO; NVIC_Init(&NVIC_InitStructure); /* Disable and Deinitialize the DMA channels */ DMA_Cmd(sEE_I2C_DMA_CHANNEL_TX, DISABLE); DMA_Cmd(sEE_I2C_DMA_CHANNEL_RX, DISABLE); DMA_DeInit(sEE_I2C_DMA_CHANNEL_TX); DMA_DeInit(sEE_I2C_DMA_CHANNEL_RX); }
/** * @brief Initializes peripherals used by the I2C EEPROM driver. * @param None * @retval None */ void sEE_Init(void) { sEE_LowLevel_Init(); /*!< I2C configuration */ /* sEE_I2C Peripheral Enable */ I2C_Cmd(sEE_I2C, ENABLE); /* sEE_I2C configuration after enabling it */ I2C_Init(sEE_I2C, I2C_SPEED, I2C_SLAVE_ADDRESS7, I2C_Mode_I2C, I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); /* Enable the sEE_I2C peripheral DMA requests */ I2C_DMACmd(sEE_I2C, ENABLE); #if defined (sEE_M24C64_32) /*!< Select the EEPROM address according to the state of E0, E1, E2 pins */ sEEAddress = sEE_HW_ADDRESS; #endif /*!< sEE_M24C64_32 */ }
void ade7880_i2c_cfg(void) { I2C_InitTypeDef I2C_InitStructure; I2C_DeInit(I2C2); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x01; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 50000; I2C_Init(I2C2, &I2C_InitStructure); I2C_Cmd(I2C2, DISABLE); return; }
/** * @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); /* 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); I2C_Init(CODEC_I2C, &I2C_InitStructure); }
void I2C_Initialise() { I2C_InitTypeDef I2C_InitStruct; RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_ClockSpeed = 100000; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_OwnAddress1 = 0x00; I2C_Init(I2C1, &I2C_InitStruct); I2C_Cmd(I2C1, ENABLE); }
void i2cInit(I2C_TypeDef *I2C) { NVIC_InitTypeDef nvic; I2C_InitTypeDef i2c; gpio_config_t gpio; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 | RCC_APB2Periph_GPIOB, ENABLE); // Init pins gpio.pin = Pin_10 | Pin_11; gpio.speed = Speed_2MHz; gpio.mode = Mode_AF_OD; gpioInit(GPIOB, &gpio); I2Cx = I2C; // clock out stuff to make sure slaves arent stuck i2cUnstick(); // Init I2C I2C_DeInit(I2Cx); I2C_StructInit(&i2c); I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Enable EVT and ERR interrupts - they are enabled by the first request i2c.I2C_Mode = I2C_Mode_I2C; i2c.I2C_DutyCycle = I2C_DutyCycle_2; i2c.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; i2c.I2C_ClockSpeed = 400000; I2C_Cmd(I2Cx, ENABLE); I2C_Init(I2Cx, &i2c); // I2C ER Interrupt nvic.NVIC_IRQChannel = I2C2_ER_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = I2C2_EV_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&nvic); }
void i2cInit(I2C_TypeDef* I2Cx) { chSemObjectInit(&i2c1_semI, 1); chSemObjectInit(&i2c1_semS, 1); I2C_InitTypeDef I2C_InitStructure; I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable; 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 = 0x0070D8FF; I2C_Init(I2Cx, &I2C_InitStructure); I2C_Cmd(I2Cx, ENABLE); }
void i2c_frequency(i2c_t *obj, int hz) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_InitTypeDef I2C_InitStructure; if ((hz != 0) && (hz <= 400000)) { I2C_DeInit(i2c); // I2C configuration I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = hz; I2C_Init(i2c, &I2C_InitStructure); I2C_Cmd(i2c, ENABLE); } }
void I2C1_Configuration(void){ I2C_InitTypeDef I2C_InitStructure; I2C_DeInit(I2C1); /* I2C1 configuration ------------------------------------------------------*/ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C2_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_CLOCK; I2C_Init(I2C1, &I2C_InitStructure); I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE); I2C_Cmd(I2C1, ENABLE); }
/** * @brief DeInitializes the LM75_I2C. * @param None * @retval None */ void LM75_LowLevel_DeInit(void) { /*!< Disable LM75_I2C */ I2C_Cmd(LM75_I2C, DISABLE); /*!< DeInitializes the LM75_I2C */ I2C_DeInit(LM75_I2C); /*!< LM75_I2C Periph clock disable */ CLK_PeripheralClockConfig(LM75_I2C_CLK, DISABLE); /*!< Configure LM75_I2C pins: SCL */ GPIO_Init(LM75_I2C_SCL_GPIO_PORT, LM75_I2C_SCL_PIN, GPIO_Mode_In_PU_No_IT); /*!< Configure LM75_I2C pins: SDA */ GPIO_Init(LM75_I2C_SDA_GPIO_PORT, LM75_I2C_SDA_PIN, GPIO_Mode_In_PU_No_IT); /*!< Configure LM75_I2C pin: SMBUS ALERT */ GPIO_Init(LM75_I2C_SMBUSALERT_GPIO_PORT, LM75_I2C_SMBUSALERT_PIN, GPIO_Mode_In_FL_No_IT); }
void I2C_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; //Enable the i2c RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE); //Reset the Peripheral RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, DISABLE); //Enable the GPIOs for the SCL/SDA Pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SCL | RCC_AHB1Periph_GPIO_SDA, ENABLE); //Configure and initialize the GPIOs GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SCL; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIO_SCL, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SDA; GPIO_Init(GPIO_SDA, &GPIO_InitStructure); //Connect GPIO pins to peripheral GPIO_PinAFConfig(GPIO_SCL, GPIO_PinSource_SCL, GPIO_AF_I2Cx); GPIO_PinAFConfig(GPIO_SDA, GPIO_PinSource_SDA, GPIO_AF_I2Cx); //Configure and Initialize the I2C I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x00; //We are the master. We don't need this I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 50000; //400kHz (Fast Mode) ( //Initialize the Peripheral I2C_Init(I2Cx, &I2C_InitStructure); // I2C Peripheral Enable I2C_Cmd(I2Cx, ENABLE); return; }
static void I2C_Local_Config(void) { I2C_InitTypeDef I2C_InitStructure; /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C_OWN_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* I2C Peripheral Enable */ I2C_Cmd(I2C_PORT, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C_PORT, &I2C_InitStructure); }
/********************************************* * Function Name : I2C_EE_INIT * Description : init module I2C EEPROM *********************************************/ void I2C_EE_INIT(void){ GPIO_InitTypeDef GPIO_InitStruct; // this is for the GPIO pins used as I2C_SDA and I2C_SCL I2C_InitTypeDef I2C_InitStruct; // this is for the I2C initilization /* enable APB1 peripheral clock for I2C*/ RCC_APB1PeriphClockCmd(I2C_EE_RCC , ENABLE); RCC_AHB1PeriphClockCmd(I2C_EE_RCC_PORT , ENABLE); GPIO_PinAFConfig(I2C_EE_PORT, I2C_EE_SDA_SOURCE, I2C_EE_GPIO_AF); // GPIO_PinAFConfig(I2C_EE_PORT, I2C_EE_SCL_SOURCE, I2C_EE_GPIO_AF); GPIO_InitStruct.GPIO_Pin = GPIO_EE_Pin_SCL | GPIO_EE_Pin_SDA ; // Pins (I2C_SCL) and (I2C_SDA) GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;// this defines the IO speed and has nothing to do with the baudrate! GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;// this defines the output type as open drain GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// this activates the pullup resistors on the IO pins GPIO_Init(I2C_EE_PORT, &GPIO_InitStruct);// now all the values are passed to the GPIO_Init() /* Configure I2C */ //I2C_DeInit(I2C); /* Enable the I2C peripheral */ /* Set the I2C structure parameters */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0xA0; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_ClockSpeed = 100000; /* Initialize the I2C peripheral w/ selected parameters */ /* I2C_Init(I2C_EE,&I2C_InitStruct); I2C_Cmd(I2C_EE, ENABLE); */ I2C_Cmd(I2C_EE, ENABLE); I2C_Init(I2C_EE, &I2C_InitStruct); }
void I2C_LowLevel_Init(){ GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; //Enable I2C Peripheral Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE); //Reset RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2Cx, DISABLE); //Enable I2C GPIO Clocks RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SCL | RCC_AHB1Periph_GPIO_SDA , ENABLE); //GPIO Alternate Function config GPIO_PinAFConfig(GPIO_SCL, GPIO_PinSource_SCL, GPIO_AF_I2Cx); GPIO_PinAFConfig(GPIO_SDA, GPIO_PinSource_SDA, GPIO_AF_I2Cx); //I2C Pin Configuration GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Set pins to alternate function GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //Set Output type to open drain (pulled low, not driven high) GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Enable pull up resistors GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SCL; GPIO_Init(GPIO_SCL, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SDA; GPIO_Init(GPIO_SDA, &GPIO_InitStructure); //I2C Configuration //I2C_DeInit(I2C); I2C_InitStructure.I2C_ClockSpeed = I2Cx_ClockSpeed; //5,50,100,400kHz I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //I2C Mode I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //Standard 50% duty cycle I2C_InitStructure.I2C_OwnAddress1 = 0x00; //Own address not relevant in master mode. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //Disable acknowledge when reading, change later on? I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2Cx, &I2C_InitStructure); //Enable I2C port I2C_Cmd(I2Cx, ENABLE); }
/******************************************************************************* * Function Name : I2C2_EV_IRQHandler * Description : This function handles I2C2 Event interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void I2C2_EV_IRQHandler(void) { switch (I2C_GetLastEvent(I2C2)) { /* Slave Transmitter ---------------------------------------------------*/ case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: /* EV1 */ /* Transmit I2C2 data */ I2C_SendData(I2C2, I2C2_Buffer_Tx[Tx2_Idx++]); break; case I2C_EVENT_SLAVE_BYTE_TRANSMITTED: /* EV3 */ /* Transmit I2C2 data */ I2C_SendData(I2C2, I2C2_Buffer_Tx[Tx2_Idx++]); break; /* Slave Receiver ------------------------------------------------------*/ case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: /* EV1 */ break; case I2C_EVENT_SLAVE_BYTE_RECEIVED: /* EV2 */ /* Store I2C2 received data */ I2C2_Buffer_Rx[Rx2_Idx++] = I2C_ReceiveData(I2C2); if(Rx2_Idx == Tx2BufferSize) { I2C_TransmitPEC(I2C2, ENABLE); Direction = Receiver; } break; case I2C_EVENT_SLAVE_STOP_DETECTED: /* EV4 */ /* Clear I2C2 STOPF flag: read of I2C_SR1 followed by a write on I2C_CR1 */ (void)(I2C_GetITStatus(I2C2, I2C_IT_STOPF)); I2C_Cmd(I2C2, ENABLE); break; default: break; } }
/* * The lightsensor is connected to I2C1 (PB6 and PB7) */ void init_lightsensor_component() { I2C_DeInit(LIGHTSENSOR_I2C); uint16_t data = 0; GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(LIGHTSENSOR_RCC_I2C_CLOCK, ENABLE); // enable clock for SCL and SDA pins RCC_AHB1PeriphClockCmd(LIGHTSENSOR_RCC_PORT_CLOCK, ENABLE); /* * 1. SCL on PB6 and SDA on PB7 * 2. SCL on PB8 and SDA on PB9 */ GPIO_InitStruct.GPIO_Pin = LIGHTSENSOR_SCL_PIN | LIGHTSENSOR_SDA_PIN; // we are going to use PB6 and PB7 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // set pins to alternate function GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // set GPIO speed GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(LIGHTSENSOR_PORT, &GPIO_InitStruct); // init the port // Connect I2C1 pins to AF GPIO_PinAFConfig(LIGHTSENSOR_PORT, LIGHTSENSOR_SCL_PINSOURCE, LIGHTSENSOR_AF); // SCL GPIO_PinAFConfig(LIGHTSENSOR_PORT, LIGHTSENSOR_SDA_PINSOURCE, LIGHTSENSOR_AF); // SDA // configure I2C I2C_InitStruct.I2C_ClockSpeed = LIGHTSENSOR_I2C_CLKSPEED; // 400kHz bus frequency I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0x00; I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(LIGHTSENSOR_I2C, &I2C_InitStruct); // enable I2C1 I2C_Cmd(LIGHTSENSOR_I2C, ENABLE); initLightSensorI2C(); }
void f3d_i2c1_init() { // I2C GPIO Initialization and Alternate Function Selection /******************vvvvvvvvvvvvvYOUR CODE GOES HEREvvvvvvvvvvvvvv***********************/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //initilzation stuff for alternate function pin GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB,&GPIO_InitStructure); //set alternate functions GPIO_PinAFConfig(GPIOB,6,GPIO_AF_4); GPIO_PinAFConfig(GPIOB,7,GPIO_AF_4); /******************^^^^^^^^^^^^^YOUR CODE GOES HERE^^^^^^^^^^^^^^**********************/ // Section 4.0 I2C Configuration I2C_InitTypeDef I2C_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); // Enable the clock to the I2C peripheral 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 = 0x00902025; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); }
/** * @brief DeInitializes peripherals used by the I2C EEPROM driver. * @param None * @retval None */ void I2C_LowLevel_DeInit(i2c_dev *dev) { GPIO_InitTypeDef GPIO_InitStructure; /* sEE_I2C Peripheral Disable */ I2C_Cmd(dev->I2Cx, DISABLE); /* sEE_I2C DeInit */ I2C_DeInit(dev->I2Cx); /*!< sEE_I2C Periph clock disable */ dev->clkcmd(dev->clk, DISABLE); /*!< GPIO configuration */ /*!< Configure I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = BIT(dev->scl_pin); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(dev->gpio_port->GPIOx, &GPIO_InitStructure); /*!< Configure I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = BIT(dev->sda_pin); GPIO_Init(dev->gpio_port->GPIOx, &GPIO_InitStructure); /* Configure and enable I2C DMA TX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = dev->dma_tx_irq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C_DMA_SUBPRIO; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); /* Configure and enable I2C DMA RX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = dev->dma_rx_irq; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = I2C_DMA_SUBPRIO; NVIC_Init(&NVIC_InitStructure); /* Disable and Deinitialize the DMA channels */ DMA_Cmd(dev->dma_tx_chan, DISABLE); DMA_Cmd(dev->dma_rx_chan, DISABLE); DMA_DeInit(dev->dma_tx_chan); DMA_DeInit(dev->dma_rx_chan); }
/* *-------------------------------------------------------------------------------- * BOOL I2C_ByteWrite(U16_T addrOfDev, U16_T addrOfMem, U8_T byteData, U8_T endCond) * Purpose : i2c master send a packet for write one data to a device * Params : addrOfDev : id address of a device * addrOfMem : address for accessing in a device * byteData : data for writing * endCond : packet condition after transmitting * Returns : TRUE : this accessing is successful * FALSE: this accessing is failed * Note : *-------------------------------------------------------------------------------- */ BOOL I2C_ByteWrite(U16_T addrOfDev, U16_T addrOfMem, U8_T byteData, U8_T endCond) { I2C_BUF *ptTxPkt = NULL; U8_T addrMode = 0; /* Get buffer of this packet */ ptTxPkt = (I2C_BUF *)GetPktBuf(); /* The end condition after transfer complete */ ptTxPkt->I2cEnd = endCond; /* Indicate the packet's direction to master transmit */ ptTxPkt->I2cDir = I2C_MST_XMIT; /* Data length exclude device address */ ptTxPkt->DataLen = 0x02; /* Device Address with 10-bit or 7-bit */ I2C_Cmd(SI_RD, I2CCTL, &addrMode); if (addrMode & I2C_10BIT) { ptTxPkt->I2cAddr.TenBitAddr = (U16_T)(addrOfDev | ((addrOfMem & 0x0700) >> 8)); }
/* * 函数名:I2C_Configuration * 描述 :I2C 工作模式配置 * 输入 :无 * 输出 :无 * 调用 :内部调用 */ static void I2C_Mode_Configu(void) { I2C_InitTypeDef I2C_InitStructure; /* I2C 配置 */ 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; /* I2C2 初始化 */ I2C_Init(I2C2, &I2C_InitStructure); /* 使能 I2C2 */ I2C_Cmd(I2C2, ENABLE); /*允许1字节1应答模式*/ I2C_AcknowledgeConfig(I2C2, ENABLE); }
void I2C::config(uint32_t speed) { _speed = speed; I2C_InitTypeDef I2C_InitStructure; /* I2C 配置 */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ; //I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //I2C_InitStructure.I2C_OwnAddress1 = SlaveAddress; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = _speed; I2C_Init(_I2Cx, &I2C_InitStructure); /* 使能 I2C1 */ I2C_Cmd (_I2Cx,ENABLE); /*允许应答模式*/ I2C_AcknowledgeConfig(_I2Cx, ENABLE); }
//--------------------------------------------------------------------------------------- void I2C_Config() { /* SCL and SDA I2C pins configuration */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_Init(GPIO0, &GPIO_InitStructure); /* I2C configuration */ I2C_InitTypeDef I2C_InitStructure; I2C_InitStructure.I2C_GeneralCall = I2C_GeneralCall_Disable; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_CLKSpeed = 100000; I2C_InitStructure.I2C_OwnAddress = 0xA0; /* I2C Peripheral Enable */ I2C_Cmd(ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(&I2C_InitStructure); }
void I2C_init() { PINSEL_CFG_Type PinCfg; // declare data struct with param members PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Funcnum = 3; PinCfg.Portnum = 0; PinCfg.Pinnum = 0; PINSEL_ConfigPin(&PinCfg); // configure pin 0 of port0 PinCfg.Pinnum = 1; PINSEL_ConfigPin(&PinCfg); // configure pin 1 of port0 I2C_Init(LPC_I2C1, 100000); // Initialize I2C peripheral I2C_Cmd(LPC_I2C1, ENABLE); // Enable I2C1 operation //NVIC_EnableIRQ(I2C1_IRQn); }