temp checkin

This commit is contained in:
Lennart Eriksson 2016-09-22 08:31:51 +02:00
parent a89e3dc4e9
commit 7c37ef0b47
2 changed files with 75 additions and 0 deletions

View File

@ -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_ */

View File

@ -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);
}