Commit 176fff61776dbd44a99906e3f61d76269494c0c5

Authored by Imanol-Mikel Barba Sabariego
1 parent 954a88db

--no commit message

Project/applications/smartcities/i2c.c
1 1 #include "i2c.h"
2   -#include "libwismart.h"
3 2  
4 3 void I2C_init(void)
5 4 {
... ... @@ -110,6 +109,58 @@ void I2C_stop(I2C_TypeDef* I2Cx)
110 109 }
111 110 }
112 111  
  112 +uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
  113 +{
  114 + printf("I2C busy...\r\n");
  115 + while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
  116 + {
  117 + // wait until I2C1 is not busy any more
  118 + }
  119 + printf("I2C ready...\r\n");
  120 + I2C_GenerateSTART(I2Cx, ENABLE);
  121 + printf("START RELEASED...\r\n");
  122 + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
  123 + {
  124 + // wait for I2C1 EV5 --> Slave has acknowledged start condition
  125 + }
  126 + printf("Probing address %x ...\r\n",address);
  127 + I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter);
  128 + chThdSleepMilliseconds(SCAN_TIMEOUT);
  129 + if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  130 + {
  131 + printf("I2C address not detected: %x\r\n", address);
  132 + return 1;
  133 + }
  134 + printf("I2C address detected: %x\r\n", address);
  135 + I2C_write(I2C1,0x00);
  136 + I2C_stop(I2C1);
  137 + return 0;
  138 + /*while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
  139 + {
  140 + // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode
  141 + }*/
  142 +}
  143 +
  144 +void I2C_scan(uint8_t *addresses)
  145 +{
  146 + uint8_t sensors[TOTAL_SENSORS] = {DISTANCE_ADDR,LIGHT_ADDR,PRESSURE_ADDR,HUMIDITY_TEMP_ADDR};
  147 + uint8_t i, j = 0;
  148 + for(i = 0; i < TOTAL_SENSORS; i++)
  149 + {
  150 + if(!I2C_check(I2C1,sensors[i] << 1))
  151 + {
  152 + printf("adding...\r\n");
  153 + addresses[j++] = sensors[i];
  154 + }
  155 + else
  156 + {
  157 + I2C_SoftwareResetCmd(I2C1, ENABLE);
  158 + I2C_DeInit(I2C1);
  159 + I2C_init();
  160 + }
  161 + }
  162 +}
  163 +
113 164 /* SAMPLE CODE BMP-085
114 165 *
115 166 uint16_t received_data;
... ...
Project/applications/smartcities/include/i2c.h
... ... @@ -4,6 +4,10 @@
4 4 #include "stm32f10x.h"
5 5 #include "stm32f10x_i2c.h"
6 6 #include "stm32f10x_gpio.h"
  7 +#include "sensors.h"
  8 +#include "libwismart.h"
  9 +
  10 +#define SCAN_TIMEOUT 100
7 11  
8 12 void I2C_init(void);
9 13 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
... ... @@ -11,6 +15,9 @@ void I2C_stop(I2C_TypeDef* I2Cx);
11 15 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data);
12 16 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx);
13 17 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx);
  18 +void I2C_scan(uint8_t *addresses);
  19 +uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address);
  20 +
14 21  
15 22 #endif
16 23  
... ...
Project/applications/smartcities/include/sensors.h
... ... @@ -8,9 +8,12 @@
8 8 #include "module.h"
9 9 //#include "json.h"
10 10  
11   -#define LIGHT_ADDR 0x39
12   -#define DISTANCE_ADDR 0x01
13   -#define PRESSURE_ADDR 0x77
  11 +#define LIGHT_ADDR 0x39
  12 +#define DISTANCE_ADDR 0x01
  13 +#define PRESSURE_ADDR 0x77
  14 +#define HUMIDITY_TEMP_ADDR 0x00
  15 +//REMEMBER TO UPDATE I2C_scan ROUTINE WITH EACH NEW SENSOR!!!!
  16 +#define TOTAL_SENSORS 4
14 17  
15 18 typedef struct {
16 19 uint8_t ID;
... ...