You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.7 KiB
49 lines
1.7 KiB
5 years ago
|
#include <stm32f10x_i2c.h>
|
||
|
|
||
|
void initI2C(I2C_TypeDef* i2cx , uint32_t speed, uint16_t own_address)
|
||
|
{
|
||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||
|
I2C_InitTypeDef I2C_InitStructure;
|
||
|
// Enable GPIOB clocks
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||
|
// Configure I2C clock and GPIO
|
||
|
GPIO_StructInit (& GPIO_InitStructure);
|
||
|
if (i2cx == I2C1){
|
||
|
// I2C1 clock enable
|
||
|
RCC_APB1PeriphClockCmd (RCC_APB1Periph_I2C1 , ENABLE);
|
||
|
// I2C1 SDA and SCL configuration
|
||
|
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 );
|
||
|
// I2C1 Reset
|
||
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
|
||
|
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
|
||
|
}
|
||
|
else { // I2C2 ...
|
||
|
}
|
||
|
// I2C configuration
|
||
|
I2C_StructInit(&I2C_InitStructure);
|
||
|
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
|
||
|
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
|
||
|
I2C_InitStructure.I2C_OwnAddress1 = own_address;
|
||
|
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
|
||
|
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
|
||
|
I2C_InitStructure.I2C_ClockSpeed = speed;
|
||
|
I2C_Init(i2cx, &I2C_InitStructure);
|
||
|
I2C_Cmd(i2cx, ENABLE);
|
||
|
}
|
||
|
|
||
|
//uint8_t pingI2C (I2C_TypeDef* i2cx, uint8_t SlaveAddress)
|
||
|
//{
|
||
|
// __IO uint32_t Timeout = 0;
|
||
|
// Timed ( I2C_GetFlagStatus (I2Cx , I2C_FLAG_BUSY ));
|
||
|
// // Intiate Start Sequence
|
||
|
// I2C_GenerateSTART (I2Cx , ENABLE );
|
||
|
// Timed (! I2C_CheckEvent (I2Cx , I2C_EVENT_MASTER_MODE_SELECT));
|
||
|
// // Send Address EV5
|
||
|
// I2C_Send7bitAddress (I2Cx , SlaveAddress, I2C_Direction_Transmitter);
|
||
|
// Timed (! I2C_CheckEvent (I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
|
||
|
// return 0;
|
||
|
//}
|