Commit 4983f340feb7152828cae2d2d746f59f0a7cbb79

Authored by Imanol-Mikel Barba Sabariego
1 parent 1648f85c

I2C Compiled and ready to test

Project/applications/smartcities/Makefile
... ... @@ -10,7 +10,7 @@
10 10 SDK_ROOT = ../../
11 11 PROJECT_OUT = smartcities
12 12  
13   -USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c json.c
  13 +USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c json.c i2c.c
14 14 USER_INCDIR = include/
15 15  
16 16 # if you need to add build Defines options add to USER_DEFS define
... ...
Project/applications/smartcities/i2c.c
1   -#include <stm32f10x.h>
2   -#include <stm32f10x_i2c.h>
  1 +#include "i2c.h"
3 2  
4   -void I2C1_init()
  3 +void I2C_init(void)
5 4 {
6 5 GPIO_InitTypeDef GPIO_InitStruct;
7 6 I2C_InitTypeDef I2C_InitStruct;
8 7  
9 8 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
10   - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  9 + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
11 10  
12 11 /*
13 12 * 1. SCL on PB6 or PB8
14 13 * 2. SDA on PB7 or PB9
15 14 */
16 15  
  16 + GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);
17 17 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;
18   - GPIO_InitStruct.GPIO_Mode = GPIO_Mode_A
  18 + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
19 19 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
20   - GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain
21   - GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors
22 20 GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB
23 21  
24 22 /* WARNING
... ... @@ -28,10 +26,6 @@ void I2C1_init()
28 26 * --Imanol
29 27 */
30 28  
31   - // Connect I2C1 pins to AF
32   - GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
33   - GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); // SDA
34   -
35 29 // configure I2C1
36 30 I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz - STANDARD MODE
37 31 I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
... ...
Project/applications/smartcities/include/i2c.h
1 1 #ifndef I2C_H
2 2 #define I2C_H
3 3  
4   -void I2C1_init();
  4 +#include "stm32f10x.h"
  5 +#include "stm32f10x_i2c.h"
  6 +#include "stm32f10x_gpio.h"
  7 +
  8 +void I2C_init(void);
5 9 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
6 10 void I2C_stop(I2C_TypeDef* I2Cx);
7 11 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data);
... ...