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); }
void fdi_i2c_initialize(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 /* SCL */| GPIO_Pin_8 /* SDA */; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8 /* SCL */, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7 /* SDA */, GPIO_AF_I2C1); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); I2C_InitTypeDef I2C_InitStruct; I2C_StructInit(&I2C_InitStruct); I2C_InitStruct.I2C_ClockSpeed = 100000; 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(I2C1, &I2C_InitStruct); I2C_Cmd(I2C1, ENABLE); }
void i2c_init(i2c_dev *dev, uint16_t address, uint32_t speed) { I2C_InitTypeDef I2C_InitStructure; i2c_lowLevel_init(dev); /* I2C configuration */ I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = address; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = speed; I2C_ITConfig(dev->I2Cx, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, DISABLE); /* I2C Peripheral Enable */ I2C_Cmd(dev->I2Cx, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(dev->I2Cx, &I2C_InitStructure); sEEAddress = sEE_HW_ADDRESS; I2C_BLOCKED = 0; }
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); }
//---------------------------------------------------------- // I2Cnの設定 void Init_i2c(I2C_TypeDef *use_i2c){ I2C_InitTypeDef I2C_InitStructure; if(use_i2c == I2C1){ Init_af_port(I2C1_PORT,I2C1_SCL_PIN,GPIO_AF_I2C1,PORT_OD|PORT_PULL_UP); Init_af_port(I2C1_PORT,I2C1_SDA_PIN,GPIO_AF_I2C1,PORT_OD|PORT_PULL_UP); }else if(use_i2c == I2C2){ Init_af_port(I2C2_PORT,I2C2_SCL_PIN,GPIO_AF_I2C2,PORT_OD|PORT_PULL_UP); Init_af_port(I2C2_PORT,I2C2_SDA_PIN,GPIO_AF_I2C2,PORT_OD|PORT_PULL_UP); }else if(use_i2c == I2C3){ Init_af_port(I2C3_SCL_PORT,I2C3_SCL_PIN,GPIO_AF_I2C3,PORT_OD|PORT_PULL_UP); Init_af_port(I2C3_SDA_PORT,I2C3_SDA_PIN,GPIO_AF_I2C3,PORT_OD|PORT_PULL_UP); } I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_ClockSpeed = 50000; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(use_i2c,&I2C_InitStructure); I2C_Cmd(use_i2c,ENABLE); }
void i2c_init() { RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); // PB10 SCL // PB11 SDA GPIO_InitTypeDef gpioInitStruct; GPIO_StructInit(&gpioInitStruct); gpioInitStruct.GPIO_Pin = GPIO_Pin_10 + GPIO_Pin_11; gpioInitStruct.GPIO_Mode = GPIO_Mode_AF_OD; gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &gpioInitStruct); I2C_InitTypeDef i2cInitStruct; I2C_StructInit(&i2cInitStruct); i2cInitStruct.I2C_Mode = I2C_Mode_I2C; i2cInitStruct.I2C_DutyCycle = I2C_DutyCycle_2; i2cInitStruct.I2C_OwnAddress1 = 0x38; i2cInitStruct.I2C_Ack = I2C_Ack_Disable; i2cInitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; i2cInitStruct.I2C_ClockSpeed = 100000; // I2C_DeInit(I2C2); I2C_Init(I2C2, &i2cInitStruct); I2C_Cmd(I2C2, ENABLE); }
void i2cInitPort(I2CDevice bus) { I2C_TypeDef *I2Cx = i2cHardwareMap[bus].dev; GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; RCC_AHBPeriphClockCmd(i2cHardwareMap[bus].scl_periph | i2cHardwareMap[bus].sda_periph, ENABLE); RCC_APB1PeriphClockCmd(i2cHardwareMap[bus].peripheral, ENABLE); RCC_I2CCLKConfig(i2cHardwareMap[bus].clock); //i2cUnstick(I2Cx); // Clock out stuff to make sure slaves arent stuck GPIO_PinAFConfig(i2cHardwareMap[bus].gpioscl, i2cHardwareMap[bus].scl_source, i2cHardwareMap[bus].scl_af); GPIO_PinAFConfig(i2cHardwareMap[bus].gpiosda, i2cHardwareMap[bus].sda_source, i2cHardwareMap[bus].sda_af); GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); // Init pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = i2cHardwareMap[bus].scl; GPIO_Init(i2cHardwareMap[bus].gpioscl, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = i2cHardwareMap[bus].sda; GPIO_Init(i2cHardwareMap[bus].gpiosda, &GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); 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 = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10. //I2C_InitStructure.I2C_Timing = 0x8000050B; I2C_Init(I2Cx, &I2C_InitStructure); I2C_Cmd(I2Cx, ENABLE); }
void i2cInit(I2C_TypeDef *I2C) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2); // 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; GPIO_InitStructure.GPIO_OType = GPIO_OType_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); }
void i2cInit(I2CDevice index) { NVIC_InitTypeDef nvic; I2C_InitTypeDef i2c; if (index > I2CDEV_MAX) index = I2CDEV_MAX; // Turn on peripheral clock, save device and index I2Cx = i2cHardwareMap[index].dev; I2Cx_index = index; RCC_APB1PeriphClockCmd(i2cHardwareMap[index].peripheral, ENABLE); // diable I2C interrrupts first to avoid ER handler triggering I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // clock out stuff to make sure slaves arent stuck // This will also configure GPIO as AF_OD at the end i2cUnstick(); // Init I2C peripheral I2C_DeInit(I2Cx); I2C_StructInit(&i2c); I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Disable 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; // Overclocking i2c, test results // Default speed, conform specs is 400000 (400 kHz) // 2.0* : 800kHz - worked without errors // 3.0* : 1200kHz - worked without errors // 3.5* : 1400kHz - failed, hangup, bootpin recovery needed // 4.0* : 1600kHz - failed, hangup, bootpin recovery needed i2c.I2C_ClockSpeed = CLOCKSPEED; I2C_Cmd(I2Cx, ENABLE); I2C_Init(I2Cx, &i2c); // I2C ER Interrupt nvic.NVIC_IRQChannel = i2cHardwareMap[index].er_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = i2cHardwareMap[index].ev_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_EV); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_EV); NVIC_Init(&nvic); }
void i2cInit(I2CDevice index) { NVIC_InitTypeDef nvic; I2C_InitTypeDef i2c; if (index > I2CDEV_MAX) index = I2CDEV_MAX; // Turn on peripheral clock, save device and index I2Cx = i2cHardwareMap[index].dev; I2Cx_index = index; RCC_APB1PeriphClockCmd(i2cHardwareMap[index].peripheral, ENABLE); // diable I2C interrrupts first to avoid ER handler triggering I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // clock out stuff to make sure slaves arent stuck // This will also configure GPIO as AF_OD at the end i2cUnstick(); // Init I2C peripheral I2C_DeInit(I2Cx); I2C_StructInit(&i2c); I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Disable 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; if (i2cOverClock) { i2c.I2C_ClockSpeed = 800000; // 800khz Maximum speed tested on various boards without issues } else { i2c.I2C_ClockSpeed = 400000; // 400khz Operation according specs } I2C_Cmd(I2Cx, ENABLE); I2C_Init(I2Cx, &i2c); // I2C ER Interrupt nvic.NVIC_IRQChannel = i2cHardwareMap[index].er_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = i2cHardwareMap[index].ev_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_EV); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_EV); NVIC_Init(&nvic); }
void Sccb::I2CInit(void) { GPIO_InitTypeDef GPIO_InitStructure; // this is for the GPIO pins used as I2C1SDA and I2C1SCL I2C_InitTypeDef I2C_InitStructure; // this is for the I2C1 initilization /* enable APB1 peripheral clock for I2C1*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* enable the peripheral clock for the pins used by PB6 for I2C SCL and PB9 for I2C1_SDL*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* This sequence sets up the I2C1SDA and I2C1SCL pins * so they work correctly with the I2C1 peripheral */ GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // Pins 10(I2C1_SCL) and 11(I2C1_SDA) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // this defines the IO speed and has nothing to do with the baudrate! GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; // this defines the output type as open drain GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // this activates the pullup resistors on the IO pins GPIO_Init(GPIOB, &GPIO_InitStructure); // now all the values are passed to the GPIO_Init() /* The I2C1_SCL and I2C1_SDA pins are now connected to their AF * so that the I2C1 can take over control of the * pins */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); /* Configure I2C1 */ I2C_StructInit(&I2C_InitStructure); I2C_DeInit(I2C1); /* Enable the I2C peripheral */ I2C_Cmd(I2C1, ENABLE); /* Set the I2C structure parameters */ 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 Peripheral Enable */ I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); /* Initialize the I2C peripheral w/ selected parameters */ I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); }
void I2C_Setup(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* I2C1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* I2C1 SDA and SCL configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); //SCL is pin06 and SDA is pin 07 for I2C1 /*enable I2C*/ I2C_Cmd(I2C1,ENABLE); I2C_DeInit(I2C1); I2C_StructInit(&I2C_InitStructure); /* I2C1 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 = I2C_SPEED; I2C_Init(I2C1, &I2C_InitStructure); //I2C_AcknowledgeConfig(I2C1, ENABLE); /*!< Enable SMBus Alert interrupt */ //I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); //dont think this is necessary }
//______________________________________________________________________________________ _i2c *Initialize_I2C(int addr, int speed) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; _i2c *p=calloc(1,sizeof(_i2c)); if(addr) p->addr=addr; if(speed) p->speed=speed; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); I2C_DeInit(I2C1); I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost; 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 = speed; I2C_Init(I2C1, &I2C_InitStructure); I2C_StretchClockCmd(I2C1, ENABLE); I2C_ARPCmd(I2C1, ENABLE); I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_Cmd(I2C1, ENABLE); I2C_ITConfig(I2C1, I2C_IT_ERR , ENABLE); return p; }
void I2C_SetFreq(I2C_TypeDef* I2Cx, uint32_t freq, uint8_t i2c_master_slave){ I2C_InitTypeDef i2c_structure; uint16_t timeout; if(freq > 400000) freq = 400000; timeout = LONG_TIMEOUT; while((I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY) != RESET) && (timeout-- != 0)); I2C_StructInit(&i2c_structure); i2c_structure.I2C_Mode = I2C_Mode_I2C; i2c_structure.I2C_ClockSpeed = freq; i2c_structure.I2C_DutyCycle = I2C_DutyCycle_2; i2c_structure.I2C_OwnAddress1 = 0; I2C_Init(I2Cx, &i2c_structure); if(i2c_master_slave) I2Cx->CR1 |= I2C_CR1_ACK; /* Enable Address Acknowledge */ }
void i2cInit(I2C_TypeDef *I2C) { NVIC_InitTypeDef NVIC_InitStructure; I2C_InitTypeDef I2C_InitStructure; gpio_config_t gpio; // 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_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); }
/* 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* 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 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(I2CDevice index) { NVIC_InitTypeDef nvic; I2C_InitTypeDef i2c; if (index > I2CDEV_MAX) index = I2CDEV_MAX; // Turn on peripheral clock, save device and index I2Cx = i2cHardwareMap[index].dev; I2Cx_index = index; RCC_APB1PeriphClockCmd(i2cHardwareMap[index].peripheral, ENABLE); // clock out stuff to make sure slaves arent stuck // This will also configure GPIO as AF_OD at the end i2cUnstick(); // Init I2C peripheral 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 = i2cHardwareMap[index].er_irq; nvic.NVIC_IRQChannelPreemptionPriority = 0; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = i2cHardwareMap[index].ev_irq; nvic.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&nvic); }
/******************************************************************************* * Function Name : i2c_open * Description : Open the I2C port communication * Input : - ClockSpeed: I2C clock frequency (<= 400000) * Output : None * Return : -1 if an error occurred *******************************************************************************/ int i2c_open(u32 ClockSpeed) { GPIO_InitTypeDef gpio_init_structure; I2C_InitTypeDef i2c_init_structure; #ifdef _I2C_AUTO_CHECK_STATUS /* Check for I2C bus status */ if (i2c_check_status() == -1) return(-1); #endif #ifdef _I2C_OS_SUPPORT /* Wait for the I2C mutex to become available */ sys_mut_wait(I2c_Mut, T_NO_TIMEOUT); #endif /* AHB peripheral clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* SCL-SDA GPIO */ gpio_init_structure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &gpio_init_structure); /* I2C configuration */ I2C_DeInit(I2C1); I2C_StructInit(&i2c_init_structure); i2c_init_structure.I2C_Ack = I2C_Ack_Enable; i2c_init_structure.I2C_ClockSpeed = I2C_MAX_CLOCK_SPEED(ClockSpeed); I2C_Init(I2C1, &i2c_init_structure); /* Enable I2C */ I2C_Cmd(I2C1, ENABLE); #ifdef _I2C_BUS_ERROR_RESET /* Reset system if bus is busy (happens if SDL or SDA is stuck low) */ if (I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)) NVIC_SystemReset(); #endif return(0); }
void I2C_Configuration(void) { /* Define variable ------------------------------------------------------*/ /* Define InitTypeDef ---------------------------------------------------*/ GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; /* initialize InitTypeDef -----------------------------------------------*/ I2C_StructInit(&I2C_InitStructure); /* Supply clock source --------------------------------------------------*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD , ENABLE); /* Define gpio_config ---------------------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; 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_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);//PB8 SCL GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);//PB9 SDA /* Set up I2C_function --------------------------------------------------*/ I2C_DeInit(I2C1); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 50000; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); #ifdef USE_INTERRUPT_I2C1 I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE); #endif }
int I2C_config(i2c_bus *bus, u32_t clock) { I2C_InitTypeDef I2C_InitStruct; I2C_StructInit(&I2C_InitStruct); I2C_Cmd(I2C_HW(bus), DISABLE); I2C_DeInit(I2C_HW(bus)); I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0x00; // never mind, will be master I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_ClockSpeed = clock; I2C_Init(I2C_HW(bus), &I2C_InitStruct); I2C_AcknowledgeConfig(I2C_HW(bus), ENABLE); I2C_Cmd(I2C_HW(bus), ENABLE); I2C_StretchClockCmd(I2C_HW(bus), ENABLE); return I2C_OK; }
/* * ===================================================================================== * * Filename: my_i2c.c * * Description: * * Version: 1.0 * Created: 2014/6/15 10:48:31 * Revision: none * Compiler: gcc * * Author: YOUR NAME (), * Organization: * * ===================================================================================== */ #include "my_i2c.h" #include "IICtools.h" #include "my_sensor.h" #include "my_misc.h" void I2C_IO_Config(void)/*{{{*/ { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); }/*}}}*/ void I2C_Config(void)/*{{{*/ { I2C_InitTypeDef I2C_InitStructure; I2C_StructInit(&I2C_InitStructure); I2C_IO_Config(); RCC_APB1PeriphClockCmd(I2C_Sensor_Clk, ENABLE); /* I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0xAA; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 350000; //I2C_InitStructure.I2C_ClockSpeed = 100000; /* I2C Peripheral Enable */ I2C_Cmd(I2C_Sensor, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C_Sensor, &I2C_InitStructure); I2C_ITConfig(I2C_Sensor,I2C_IT_EVT,ENABLE); }/*}}}*/
void CS43L22_Config() { // configure STM32F4Discovery I2S pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_WS_PIN; GPIO_Init(STM32F4D_I2S_WS_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(STM32F4D_I2S_WS_PORT, STM32F4D_I2S_WS_PINSRC, GPIO_AF_SPI3); GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_CK_PIN; GPIO_Init(STM32F4D_I2S_CK_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(STM32F4D_I2S_CK_PORT, STM32F4D_I2S_CK_PINSRC, GPIO_AF_SPI3); GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_SD_PIN; GPIO_Init(STM32F4D_I2S_SD_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(STM32F4D_I2S_SD_PORT, STM32F4D_I2S_SD_PINSRC, GPIO_AF_SPI3); GPIO_InitStructure.GPIO_Pin = STM32F4D_I2S_MCLK_PIN; GPIO_Init(STM32F4D_I2S_MCLK_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(STM32F4D_I2S_MCLK_PORT, STM32F4D_I2S_MCLK_PINSRC, GPIO_AF_SPI3); // configure I2C pins to access the CS43L22 configuration registers GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = CODEC_I2C_SCL_PIN; GPIO_Init(CODEC_I2C_SCL_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(CODEC_I2C_SCL_PORT, GPIO_PinSource6, GPIO_AF_I2C1); GPIO_InitStructure.GPIO_Pin = CODEC_I2C_SDA_PIN; GPIO_Init(CODEC_I2C_SDA_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(CODEC_I2C_SDA_PORT, GPIO_PinSource9, GPIO_AF_I2C1); // CS43L22 reset pin GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = CODEC_RESET_PIN; GPIO_Init(CODEC_RESET_PORT, &GPIO_InitStructure); GPIO_ResetBits(CODEC_RESET_PORT, CODEC_RESET_PIN); // activate reset // I2S initialisation RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); RCC_PLLI2SCmd(ENABLE); // new for STM32F4: enable I2S PLL SPI_I2S_DeInit(SPI3); I2S_InitTypeDef I2S_InitStructure; I2S_StructInit(&I2S_InitStructure); I2S_InitStructure.I2S_Standard = STM32F4D_I2S_STANDARD; I2S_InitStructure.I2S_DataFormat = STM32F4D_I2S_DATA_FORMAT; I2S_InitStructure.I2S_MCLKOutput = STM32F4D_I2S_MCLK_ENABLE ? I2S_MCLKOutput_Enable : I2S_MCLKOutput_Disable; I2S_InitStructure.I2S_AudioFreq = (u16)(STM32F4D_I2S_AUDIO_FREQ); I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low; // configuration required as well? I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx; I2S_Init(SPI3, &I2S_InitStructure); I2S_Cmd(SPI3, ENABLE); // DMA Configuration for SPI Tx Event RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); DMA_Cmd(DMA1_Stream5, DISABLE); DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5 | DMA_FLAG_TEIF5 | DMA_FLAG_HTIF5 | DMA_FLAG_FEIF5); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&SPI3->DR; // DMA_InitStructure.DMA_MemoryBaseAddr = ...; // configured in CS43L22_Start DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // DMA_InitStructure.DMA_BufferSize = ...; // configured in CS43L22_Start DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream5, &DMA_InitStructure); // DMA_Cmd(DMA1_Stream5, ENABLE); // done on CS43L22_Start DMA_ITConfig(DMA1_Stream5, DMA_IT_TE | DMA_IT_FE, DISABLE); // trigger interrupt when transfer half complete/complete DMA_ITConfig(DMA1_Stream5, DMA_IT_HT | DMA_IT_TC, ENABLE); // enable SPI interrupts to DMA SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE); // Configure and enable DMA interrupt /* Configure the DMA IRQ handler priority */ NVIC_SetPriority(DMA1_Stream5_IRQn, 0x0); NVIC_EnableIRQ(DMA1_Stream5_IRQn); // configure I2C RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); I2C_DeInit(CODEC_I2C); I2C_InitTypeDef I2C_InitStructure; I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_ClockSpeed = 100000; I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_OwnAddress1 = CORE_I2C_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_Cmd(CODEC_I2C, ENABLE); I2C_Init(CODEC_I2C, &I2C_InitStructure); codec_init(); }
int main(void) { GPIO_InitTypeDef GPIO_InitStructure; //structure for GPIO setup //TouchInitStr touch_init; I2C_InitTypeDef I2C_InitStructure; /* SysTick end of count event each 1ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); touch_coords.processed=1; /* Initialize LEDs, Key Button, LCD and COM port(USART) available on STM32303C-EVAL board *****************************************************/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED9); //init now the TW88 chip on I2C 1 //I2C init ...on PB6 And PB7 /* Configure the I2C clock source. The clock is derived from the SYSCLK */ RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_4); // SCL GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4); // SDA GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_6); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_7); GPIO_Init(GPIOB, &GPIO_InitStructure); //init our serial port on UART2 SerialPort_Init(115200,1); printf("\n\r ***** COEN490/ELEC490 Capstone Project Team 14 Welcome! ***** \n\r"); I2C_DeInit(I2C1); I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode=I2C_Mode_I2C; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStructure.I2C_DigitalFilter = 0x00; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_OwnAddress1 = 0x00; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //I2C_InitStructure.I2C_Timing = 0x50330309; //0xB0420F13; ///0x00902025; // 0xc062121f I2C_InitStructure.I2C_Timing = 0x00310309; I2C_Init(I2C1,&I2C_InitStructure); I2C_Cmd(I2C1,ENABLE); Delay(1); //init TW88 TW88_Init(); //add the osd TW88_AddOSD_Win(0,0,0x3F,1); //touch_init.onTouchEvent = onTouchScreenEvent; AR1100Init(); /* Infinite loop */ while (1) { if (rx_counter>0) { printf("Recv: %d",rx_buffer[0]); memset(rx_buffer, 0, RX_BUFFER_LENGTH); rx_counter=0; } if (touch_coords.processed==0) { touch_coords.processed=1; onTouchScreenEvent(touch_coords); } /* Toggle LD1 */ STM_EVAL_LEDToggle(LED1); /* Insert 50 ms delay */ Delay(50); /* Toggle LD2 */ STM_EVAL_LEDToggle(LED2); /* Insert 50 ms delay */ Delay(50); /* Toggle LD3 */ STM_EVAL_LEDToggle(LED3); /* Insert 50 ms delay */ Delay(50); /* Toggle LD4 */ //STM_EVAL_LEDToggle(LED4); /* Insert 50 ms delay */ //Delay(50); } }
void i2cInit(I2CDevice device) { if (device == I2CINVALID) return; i2cDevice_t *i2c; i2c = &(i2cHardwareMap[device]); NVIC_InitTypeDef nvic; I2C_InitTypeDef i2cInit; IO_t scl = IOGetByTag(i2c->scl); IO_t sda = IOGetByTag(i2c->sda); IOInit(scl, OWNER_I2C_SCL, RESOURCE_INDEX(device)); IOInit(sda, OWNER_I2C_SDA, RESOURCE_INDEX(device)); // Enable RCC RCC_ClockCmd(i2c->rcc, ENABLE); I2C_ITConfig(i2c->dev, I2C_IT_EVT | I2C_IT_ERR, DISABLE); i2cUnstick(scl, sda); // Init pins #ifdef STM32F4 IOConfigGPIOAF(scl, IOCFG_I2C, GPIO_AF_I2C); IOConfigGPIOAF(sda, IOCFG_I2C, GPIO_AF_I2C); #else IOConfigGPIO(scl, IOCFG_I2C); IOConfigGPIO(sda, IOCFG_I2C); #endif I2C_DeInit(i2c->dev); I2C_StructInit(&i2cInit); I2C_ITConfig(i2c->dev, I2C_IT_EVT | I2C_IT_ERR, DISABLE); // Disable EVT and ERR interrupts - they are enabled by the first request i2cInit.I2C_Mode = I2C_Mode_I2C; i2cInit.I2C_DutyCycle = I2C_DutyCycle_2; i2cInit.I2C_OwnAddress1 = 0; i2cInit.I2C_Ack = I2C_Ack_Enable; i2cInit.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; if (i2c->overClock) { i2cInit.I2C_ClockSpeed = 800000; // 800khz Maximum speed tested on various boards without issues } else { i2cInit.I2C_ClockSpeed = 400000; // 400khz Operation according specs } I2C_Cmd(i2c->dev, ENABLE); I2C_Init(i2c->dev, &i2cInit); I2C_StretchClockCmd(i2c->dev, ENABLE); // I2C ER Interrupt nvic.NVIC_IRQChannel = i2c->er_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_ER); nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); // I2C EV Interrupt nvic.NVIC_IRQChannel = i2c->ev_irq; nvic.NVIC_IRQChannelPreemptionPriority = NVIC_PRIORITY_BASE(NVIC_PRIO_I2C_EV); nvic.NVIC_IRQChannelSubPriority = NVIC_PRIORITY_SUB(NVIC_PRIO_I2C_EV); NVIC_Init(&nvic); }
void i2c_init(I2C_TypeDef * I2C) { NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; uint16_t I2C_SCL_PIN,I2C_SDA_PIN; GPIO_TypeDef* I2C_PORT; I2C_context_t* context; if(I2C == I2C1){ I2C_SCL_PIN = I2C1_SCL_PIN; I2C_SDA_PIN = I2C1_SDA_PIN; I2C_PORT = I2C1_GPIO_PORT; context = &i2c1_context; }else if(I2C == I2C2){ I2C_SCL_PIN = I2C2_SCL_PIN; I2C_SDA_PIN = I2C2_SDA_PIN; I2C_PORT = I2C2_GPIO_PORT; context = &i2c2_context; }else{ I2C_SCL_PIN = I2C1_SCL_PIN; I2C_SDA_PIN = I2C1_SDA_PIN; I2C_PORT = I2C1_GPIO_PORT; context = &i2c1_context; } if(context->init) return; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); i2cUnstick(I2C); // clock out stuff to make sure slaves arent stuck // SCL PB6 // SDA PB7 GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(I2C_PORT, &GPIO_InitStructure); // Init I2C I2C_DeInit(I2C); I2C_StructInit(&I2C_InitStructure); I2C_ITConfig(I2C, I2C_IT_EVT | I2C_IT_ERR, DISABLE); //Disable 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_Init(I2C, &I2C_InitStructure); I2C_Cmd(I2C, ENABLE); if(I2C == I2C1){ // I2C ER Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C1_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 = I2C1_EV_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_Init(&NVIC_InitStructure); }else if(I2C == I2C2){ // 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); } context->init = 1; }
void i2cInit(I2C_TypeDef *I2C) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /////////////////////////////////// if (I2C == I2C1) { i2cUnstick(I2C); // Clock out stuff to make sure slaves arent stuck I2C_StructInit(&I2C_InitStructure); GPIO_PinAFConfig(I2C1_GPIO, I2C1_SCL_PIN_SOURCE, GPIO_AF_I2C1); GPIO_PinAFConfig(I2C1_GPIO, I2C1_SDA_PIN_SOURCE, GPIO_AF_I2C1); // Init pins GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN | I2C1_SDA_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(I2C1_GPIO, &GPIO_InitStructure); // Init I2C I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_ERR, DISABLE); I2C_DeInit(I2C1); I2C_InitStructure.I2C_ClockSpeed = 400000; 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_Disable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); // I2C ER Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C1_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 = I2C1_EV_IRQn; //NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /////////////////////////////////// if (I2C == I2C2) { i2cUnstick(I2C); // Clock out stuff to make sure slaves arent stuck GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); GPIO_PinAFConfig(I2C2_GPIO, I2C2_SCL_PIN_SOURCE, GPIO_AF_I2C2); GPIO_PinAFConfig(I2C2_GPIO, I2C2_SDA_PIN_SOURCE, GPIO_AF_I2C2); // Init pins GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN | I2C2_SDA_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(I2C2_GPIO, &GPIO_InitStructure); // Init I2C I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_ERR, DISABLE); I2C_DeInit(I2C2); I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_ClockSpeed = 400000; 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_Disable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C2, &I2C_InitStructure); I2C_Cmd(I2C2, ENABLE); // 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_InitStructure.NVIC_IRQChannelSubPriority = 0; //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /////////////////////////////////// }
void i2cInitPort(I2C_TypeDef *I2Cx) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; if (I2Cx == I2C1) { RCC_AHBPeriphClockCmd(I2C1_SCL_CLK_SOURCE | I2C1_SDA_CLK_SOURCE, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); //i2cUnstick(I2Cx); // Clock out stuff to make sure slaves arent stuck GPIO_PinAFConfig(I2C1_SCL_GPIO, I2C1_SCL_PIN_SOURCE, I2C1_SCL_GPIO_AF); GPIO_PinAFConfig(I2C1_SDA_GPIO, I2C1_SDA_PIN_SOURCE, I2C1_SDA_GPIO_AF); GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); // Init pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN; GPIO_Init(I2C1_SCL_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN; GPIO_Init(I2C1_SDA_GPIO, &GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); 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; if (i2cOverClock) { I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4. } else { I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10 } //I2C_InitStructure.I2C_Timing = 0x8000050B; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); } if (I2Cx == I2C2) { RCC_AHBPeriphClockCmd(I2C2_SCL_CLK_SOURCE | I2C2_SDA_CLK_SOURCE, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK); //i2cUnstick(I2Cx); // Clock out stuff to make sure slaves arent stuck GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PIN_SOURCE, I2C2_SCL_GPIO_AF); GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PIN_SOURCE, I2C2_SDA_GPIO_AF); GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); // Init pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN; GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C2_SDA_PIN; GPIO_Init(I2C2_SDA_GPIO, &GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); 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; // FIXME timing is board specific //I2C_InitStructure.I2C_Timing = 0x00310309; // //400kHz I2C @ 8MHz input -> PRESC=0x0, SCLDEL=0x3, SDADEL=0x1, SCLH=0x03, SCLL=0x09 - value from TauLabs/Sparky // ^ when using this setting and after a few seconds of a scope probe being attached to the I2C bus it was observed that the bus enters // a busy state and does not recover. if (i2cOverClock) { I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4. } else { I2C_InitStructure.I2C_Timing = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Rise 100, Fall 10 } //I2C_InitStructure.I2C_Timing = 0x8000050B; I2C_Init(I2C2, &I2C_InitStructure); I2C_Cmd(I2C2, ENABLE); } }
void i2cInitPort(I2C_TypeDef *I2Cx) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; if (I2Cx == I2C1) { RCC_AHBPeriphClockCmd(I2C1_SCL_CLK_SOURCE | I2C1_SDA_CLK_SOURCE, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); //i2cUnstick(I2Cx); // Clock out stuff to make sure slaves arent stuck GPIO_PinAFConfig(I2C1_SCL_GPIO, I2C1_SCL_PIN_SOURCE, I2C1_SCL_GPIO_AF); GPIO_PinAFConfig(I2C1_SDA_GPIO, I2C1_SDA_PIN_SOURCE, I2C1_SDA_GPIO_AF); GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); // Init pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = I2C1_SCL_PIN; GPIO_Init(I2C1_SCL_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C1_SDA_PIN; GPIO_Init(I2C1_SDA_GPIO, &GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); 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 = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 100, Hold 10. //I2C_InitStructure.I2C_Timing = 0x0070123D; // 800 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 50, Hold 5. I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4. I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE); } if (I2Cx == I2C2) { RCC_AHBPeriphClockCmd(I2C2_SCL_CLK_SOURCE | I2C2_SDA_CLK_SOURCE, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK); //i2cUnstick(I2Cx); // Clock out stuff to make sure slaves arent stuck GPIO_PinAFConfig(I2C2_SCL_GPIO, I2C2_SCL_PIN_SOURCE, I2C2_SCL_GPIO_AF); GPIO_PinAFConfig(I2C2_SDA_GPIO, I2C2_SDA_PIN_SOURCE, I2C2_SDA_GPIO_AF); GPIO_StructInit(&GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); // Init pins GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Pin = I2C2_SCL_PIN; GPIO_Init(I2C2_SCL_GPIO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C2_SDA_PIN; GPIO_Init(I2C2_SDA_GPIO, &GPIO_InitStructure); I2C_StructInit(&I2C_InitStructure); 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 = 0x00E0257A; // 400 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 100, Hold 10. //I2C_InitStructure.I2C_Timing = 0x0070123D; // 800 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 50, Hold 5. I2C_InitStructure.I2C_Timing = 0x00500E30; // 1000 Khz, 72Mhz Clock, Analog Filter Delay ON, Setup 40, Hold 4. I2C_Init(I2C2, &I2C_InitStructure); I2C_Cmd(I2C2, ENABLE); } }