Commit b044cc29d94ab610e9c044e9c8b3e68ce58c4aac

Authored by Imanol-Mikel Barba Sabariego
1 parent 1ee7640c

I2C Completed and tested

Project/applications/smartcities/i2c.c
1 #include "i2c.h" 1 #include "i2c.h"
  2 +#include "libwismart.h"
2 3
3 void I2C_init(void) 4 void I2C_init(void)
4 { 5 {
@@ -13,8 +14,7 @@ void I2C_init(void) @@ -13,8 +14,7 @@ void I2C_init(void)
13 * 2. SDA on PB7 or PB9 14 * 2. SDA on PB7 or PB9
14 */ 15 */
15 16
16 - GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);  
17 - GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9; 17 + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
18 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD; 18 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
19 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 19 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
20 GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB 20 GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB
@@ -44,13 +44,11 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) @@ -44,13 +44,11 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
44 // wait until I2C1 is not busy any more 44 // wait until I2C1 is not busy any more
45 } 45 }
46 I2C_GenerateSTART(I2Cx, ENABLE); 46 I2C_GenerateSTART(I2Cx, ENABLE);
47 -  
48 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) 47 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
49 { 48 {
50 // wait for I2C1 EV5 --> Slave has acknowledged start condition 49 // wait for I2C1 EV5 --> Slave has acknowledged start condition
51 } 50 }
52 I2C_Send7bitAddress(I2Cx, address, direction); 51 I2C_Send7bitAddress(I2Cx, address, direction);
53 -  
54 if(direction == I2C_Direction_Transmitter) 52 if(direction == I2C_Direction_Transmitter)
55 { 53 {
56 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 54 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
@@ -112,19 +110,43 @@ void I2C_stop(I2C_TypeDef* I2Cx) @@ -112,19 +110,43 @@ void I2C_stop(I2C_TypeDef* I2Cx)
112 } 110 }
113 } 111 }
114 112
115 -/* SAMPLE CODE 113 +/* SAMPLE CODE BMP-085
116 * 114 *
117 - * int main(void)  
118 - * {  
119 - * uint8_t received_data;  
120 - * I2C1_init(); // initialize I2C peripheral  
121 - * I2C_start(I2C1, SLAVE_ADDRESS<<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode  
122 - * I2C_write(I2C1, 0x01); // write one byte to the slave  
123 - * I2C_stop(I2C1); // stop the transmission  
124 - * I2C_start(I2C1, SLAVE_ADDRESS<<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode  
125 - * received_data = I2C_read_nack(I2C1); // read one byte and don't request another byte, stop transmission  
126 - * while(1);  
127 - * return 0;  
128 - * } 115 + uint16_t received_data;
  116 + char answer[10];
  117 + int32_t UT, X1, X2, B5; // following ds convention
  118 + float t;
  119 + bmp085_calib_data _bmp085_coeffs;
  120 +
  121 + printf("Hello\r\n");
  122 + I2C_init();
  123 +
  124 + printf("I2C Initialized\r\n");
  125 + I2C_start(I2C1,SLAVE_ADDRESS << 1, I2C_Direction_Transmitter);
  126 + printf("I2C First write\r\n");
  127 + I2C_write(I2C1, 0xF4);
  128 + printf("I2C Second write\r\n");
  129 + I2C_write(I2C1, 0x2E);
  130 + printf("I2C stop\r\n");
  131 + I2C_stop(I2C1);
  132 + printf("I2C Temperature measurment started\r\n");
  133 +
  134 + I2C_start(I2C1,SLAVE_ADDRESS << 1, I2C_Direction_Transmitter);
  135 + printf("I2C First write\r\n");
  136 + I2C_write(I2C1, 0xF6);
  137 + printf("I2C stop\r\n");
  138 + I2C_stop(I2C1);
  139 + printf("I2C Temperature measurment request sent\r\n");
  140 +
  141 + I2C_start(I2C1, SLAVE_ADDRESS<<1, I2C_Direction_Receiver);
  142 + printf("I2C Read request sent\r\n");
  143 + received_data = I2C_read_ack(I2C1);
  144 + received_data = received_data << 8;
  145 + received_data = received_data | I2C_read_ack(I2C1);
  146 + printf("I2C data received\r\n");
  147 +
  148 + sprintf(answer,"%d",received_data);
  149 + printf("Data: %s\r\n",answer);
  150 +
129 * --Imanol 151 * --Imanol
130 */ 152 */