From 7c37ef0b4795342ea0d673ba32a22369151f7e1e Mon Sep 17 00:00:00 2001 From: Lennart Eriksson Date: Thu, 22 Sep 2016 08:31:51 +0200 Subject: [PATCH] temp checkin --- UAV-ControlSystem/inc/drivers/I2C.h | 19 ++++++++++ UAV-ControlSystem/src/drivers/I2C.c | 56 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 UAV-ControlSystem/inc/drivers/I2C.h create mode 100644 UAV-ControlSystem/src/drivers/I2C.c diff --git a/UAV-ControlSystem/inc/drivers/I2C.h b/UAV-ControlSystem/inc/drivers/I2C.h new file mode 100644 index 0000000..aed20ad --- /dev/null +++ b/UAV-ControlSystem/inc/drivers/I2C.h @@ -0,0 +1,19 @@ +/* + * I2C.h + * + * Created on: 21 sep. 2016 + * Author: len12007 + */ + +#ifndef DRIVERS_I2C_H_ +#define DRIVERS_I2C_H_ + +#include "stm32f4xx.h" + + +void i2c_configure(void); + + + + +#endif /* DRIVERS_I2C_H_ */ diff --git a/UAV-ControlSystem/src/drivers/I2C.c b/UAV-ControlSystem/src/drivers/I2C.c new file mode 100644 index 0000000..c2216ec --- /dev/null +++ b/UAV-ControlSystem/src/drivers/I2C.c @@ -0,0 +1,56 @@ +/* + * I2C.c + * + * Created on: 21 sep. 2016 + * Author: len12007 + */ + +#include "drivers/I2C.h" + + + + +void i2c_configure(uint32_t channel) +{ + + GPIO_InitTypeDef GPIO_InitStruct; + I2C_InitTypeDef I2C_InitStruct; + + // enable APB1 peripheral clock for I2C1 + RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); + // enable clock for SCL and SDA pins + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); + + /* 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.Pin = GPIO_PIN_6 | GPIO_PIN_7; // we are going to use PB6 and PB7 + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // set pins to alternate function + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; // 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.Pull = GPIO_PULLUP; // enable pull up resistors + GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; + GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB + + I2C_InitStruct.ClockSpeed = 100000; + I2C_InitStruct.DualAddressMode = + + // Connect I2C1 pins to AF + GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL + GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA + + // configure I2C1 + I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz + 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(I2C1, &I2C_InitStruct); // init I2C1 + + // enable I2C1 + I2C_Cmd(I2C1, ENABLE); +}