From 176fff61776dbd44a99906e3f61d76269494c0c5 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Mon, 19 May 2014 00:10:58 +0000 Subject: [PATCH] --- Project/applications/smartcities/i2c.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Project/applications/smartcities/include/i2c.h | 7 +++++++ Project/applications/smartcities/include/sensors.h | 9 ++++++--- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/Project/applications/smartcities/i2c.c b/Project/applications/smartcities/i2c.c index 078cb59..ec1d47c 100644 --- a/Project/applications/smartcities/i2c.c +++ b/Project/applications/smartcities/i2c.c @@ -1,5 +1,4 @@ #include "i2c.h" -#include "libwismart.h" void I2C_init(void) { @@ -110,6 +109,58 @@ void I2C_stop(I2C_TypeDef* I2Cx) } } +uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) +{ + printf("I2C busy...\r\n"); + while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)) + { + // wait until I2C1 is not busy any more + } + printf("I2C ready...\r\n"); + I2C_GenerateSTART(I2Cx, ENABLE); + printf("START RELEASED...\r\n"); + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) + { + // wait for I2C1 EV5 --> Slave has acknowledged start condition + } + printf("Probing address %x ...\r\n",address); + I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter); + chThdSleepMilliseconds(SCAN_TIMEOUT); + if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + printf("I2C address not detected: %x\r\n", address); + return 1; + } + printf("I2C address detected: %x\r\n", address); + I2C_write(I2C1,0x00); + I2C_stop(I2C1); + return 0; + /*while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) + { + // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode + }*/ +} + +void I2C_scan(uint8_t *addresses) +{ + uint8_t sensors[TOTAL_SENSORS] = {DISTANCE_ADDR,LIGHT_ADDR,PRESSURE_ADDR,HUMIDITY_TEMP_ADDR}; + uint8_t i, j = 0; + for(i = 0; i < TOTAL_SENSORS; i++) + { + if(!I2C_check(I2C1,sensors[i] << 1)) + { + printf("adding...\r\n"); + addresses[j++] = sensors[i]; + } + else + { + I2C_SoftwareResetCmd(I2C1, ENABLE); + I2C_DeInit(I2C1); + I2C_init(); + } + } +} + /* SAMPLE CODE BMP-085 * uint16_t received_data; diff --git a/Project/applications/smartcities/include/i2c.h b/Project/applications/smartcities/include/i2c.h index 130c837..b448ade 100644 --- a/Project/applications/smartcities/include/i2c.h +++ b/Project/applications/smartcities/include/i2c.h @@ -4,6 +4,10 @@ #include "stm32f10x.h" #include "stm32f10x_i2c.h" #include "stm32f10x_gpio.h" +#include "sensors.h" +#include "libwismart.h" + +#define SCAN_TIMEOUT 100 void I2C_init(void); void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction); @@ -11,6 +15,9 @@ void I2C_stop(I2C_TypeDef* I2Cx); void I2C_write(I2C_TypeDef* I2Cx, uint8_t data); uint8_t I2C_read_ack(I2C_TypeDef* I2Cx); uint8_t I2C_read_nack(I2C_TypeDef* I2Cx); +void I2C_scan(uint8_t *addresses); +uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address); + #endif diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index 6f09c80..8818c27 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -8,9 +8,12 @@ #include "module.h" //#include "json.h" -#define LIGHT_ADDR 0x39 -#define DISTANCE_ADDR 0x01 -#define PRESSURE_ADDR 0x77 +#define LIGHT_ADDR 0x39 +#define DISTANCE_ADDR 0x01 +#define PRESSURE_ADDR 0x77 +#define HUMIDITY_TEMP_ADDR 0x00 +//REMEMBER TO UPDATE I2C_scan ROUTINE WITH EACH NEW SENSOR!!!! +#define TOTAL_SENSORS 4 typedef struct { uint8_t ID; -- libgit2 0.22.2