From 36268163fe19db2e49ef4a408f0b2be1960518bd Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Fri, 6 Jun 2014 03:46:32 +0000 Subject: [PATCH] --- Project/applications/smartcities/i2c.c | 48 ++++++++++++++++++++++++------------------------ Project/applications/smartcities/json.c | 6 +++--- Project/applications/smartcities/main.c | 1 + Project/applications/smartcities/sensors.c | 26 +++++++------------------- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/Project/applications/smartcities/i2c.c b/Project/applications/smartcities/i2c.c index 4580224..c0e2c7a 100644 --- a/Project/applications/smartcities/i2c.c +++ b/Project/applications/smartcities/i2c.c @@ -3,7 +3,7 @@ void I2C_init(void) { - DBG_I2C("Initializing I2C...\r\n"); + //DBG_I2C("Initializing I2C...\r\n"); GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; @@ -30,21 +30,21 @@ void I2C_init(void) void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { - DBG_I2C("Sending I2C Start...\r\n"); + //DBG_I2C("Sending I2C Start...\r\n"); while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)) { // wait until I2C1 is not busy any more } - DBG_I2C("I2C ready\r\n"); + //DBG_I2C("I2C ready\r\n"); I2C_GenerateSTART(I2Cx, ENABLE); - DBG_I2C("Start sent\r\n"); + //DBG_I2C("Start sent\r\n"); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) { // wait for I2C1 EV5 --> Slave has acknowledged start condition } - DBG_I2C("Start ACK\r\n"); + //DBG_I2C("Start ACK\r\n"); I2C_Send7bitAddress(I2Cx, address, direction); - DBG_I2C("Master -> Slave Address and R/W sent\r\n"); + //DBG_I2C("Master -> Slave Address and R/W sent\r\n"); if(direction == I2C_Direction_Transmitter) { while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) @@ -59,21 +59,21 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode } } - DBG_I2C("Slave -> Master ACK\r\n"); + //DBG_I2C("Slave -> Master ACK\r\n"); } void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) { - DBG_I2C("Sending repeated I2C Start...\r\n"); + //DBG_I2C("Sending repeated I2C Start...\r\n"); I2C_GenerateSTART(I2Cx, ENABLE); - DBG_I2C("Start sent\r\n"); + //DBG_I2C("Start sent\r\n"); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) { // wait for I2C1 EV5 --> Slave has acknowledged start condition } - DBG_I2C("Start ACK\r\n"); + //DBG_I2C("Start ACK\r\n"); I2C_Send7bitAddress(I2Cx, address, direction); - DBG_I2C("Master -> Slave Address and R/W sent\r\n"); + //DBG_I2C("Master -> Slave Address and R/W sent\r\n"); if(direction == I2C_Direction_Transmitter) { while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) @@ -88,51 +88,51 @@ void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode } } - DBG_I2C("Slave -> Master ACK\r\n"); + //DBG_I2C("Slave -> Master ACK\r\n"); } void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) { - DBG_I2C("Sending I2C byte %02x...\r\n",data); + //DBG_I2C("Sending I2C byte %02x...\r\n",data); I2C_SendData(I2Cx, data); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { // wait for I2C1 EV8_2 --> last byte is transmitted } - DBG_I2C("Data sent\r\n"); + //DBG_I2C("Data sent\r\n"); } uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) { - DBG_I2C("Listening for byte in I2C (ACK)\r\n"); + //DBG_I2C("Listening for byte in I2C (ACK)\r\n"); I2C_AcknowledgeConfig(I2Cx, ENABLE); while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) { // wait until one byte has been received } uint8_t data = I2C_ReceiveData(I2Cx); - DBG_I2C("Received byte: %02x\r\n",data); + //DBG_I2C("Received byte: %02x\r\n",data); return data; } uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) { - DBG_I2C("Listening for byte in I2C (NACK)\r\n"); + //DBG_I2C("Listening for byte in I2C (NACK)\r\n"); I2C_AcknowledgeConfig(I2Cx, DISABLE); while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) { // wait until one byte has been received } uint8_t data = I2C_ReceiveData(I2Cx); - DBG_I2C("Received byte: %02x\r\n",data); + //DBG_I2C("Received byte: %02x\r\n",data); return data; } void I2C_stop(I2C_TypeDef* I2Cx) { - DBG_I2C("Sending I2C Stop...\r\n"); + //DBG_I2C("Sending I2C Stop...\r\n"); I2C_GenerateSTOP(I2Cx, ENABLE); - DBG_I2C("Stop sent\r\n"); + //DBG_I2C("Stop sent\r\n"); } uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) @@ -146,14 +146,14 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) { // wait for I2C1 EV5 --> Slave has acknowledged start condition } - DBG_I2C("Probing address %x ...\r\n",address); + //DBG_I2C("Probing address %x ...\r\n",address); I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter); chThdSleepMilliseconds(I2C_TIMEOUT); if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { return 1; } - DBG_I2C("I2C address detected: %x\r\n", address); + //DBG_I2C("I2C address detected: %x\r\n", address); I2C_write(I2Cx,0x00); I2C_stop(I2Cx); return 0; @@ -161,7 +161,7 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) void I2C_reset(I2C_TypeDef* I2Cx) { - DBG_I2C("Resetting I2C...\r\n"); + //DBG_I2C("Resetting I2C...\r\n"); I2C_SoftwareResetCmd(I2Cx, ENABLE); I2C_DeInit(I2Cx); I2C_init(); @@ -170,7 +170,7 @@ void I2C_reset(I2C_TypeDef* I2Cx) void I2C_scan(I2C_TypeDef* I2Cx,uint8_t *addresses) { - DBG_I2C("Scanning sensors...\r\n"); + //DBG_I2C("Scanning sensors...\r\n"); uint8_t i, j = 0; I2C_init(); for(i = 0; i < TOTAL_SENSORS; i++) diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 61100bc..92584b9 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -10,7 +10,6 @@ uint8_t register_sensor(sensor sens) DBG_JSON("Putting additional info for pressure sensor...\r\n"); bmp085_callibration_t calib_data; get_pressure_callibration_data(&calib_data); - DBG_JSON("Got callibration data\r\n"); char *callibration_data = callibration_pressure_data_csv(&calib_data); DBG_JSON("callibration_data is: %s\r\n",callibration_data); statement = prepare_json_register_statement(mod,sens,callibration_data); @@ -97,13 +96,14 @@ char* prepare_json_register_statement(module mod, sensor sens, char *additional_ char* prepare_observation(char* observation, uint32_t length) { + DBG_JSON("Preparing: %s with length %d\r\n",observation,length); char *json_data, *str_aux, *str_aux2; int value_length = find_next_index(observation,length,','); while(1) { - if(find_next_index(observation+value_length+1,length,',')) + if(find_next_index(observation+value_length+1,length-value_length,',')) { - value_length += find_next_index(observation+value_length+1,length,',')+1; + value_length += find_next_index(observation+value_length+1,length-value_length,',')+1; } else { diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index d6025e1..b998fda 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -235,6 +235,7 @@ int main(void) } DBG("%c[1;35m--------------WI-SENSE STARTED-------------%c[1;00m\r\n",0x1B,0x1B); + wifi_connect(); check_memory(); diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c index 8674fab..771727a 100644 --- a/Project/applications/smartcities/sensors.c +++ b/Project/applications/smartcities/sensors.c @@ -114,22 +114,16 @@ uint16_t get_light_ch0(void) uint16_t data = 0; I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x8C); + I2C_write(I2C1, 0xAC); DBG_SENSORS("[LIGHT] Sent fetch command for CH0\r\n"); I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); - data = I2C_read_nack(I2C1); - I2C_stop(I2C1); + data = I2C_read_ack(I2C1); DBG_SENSORS("[LIGHT] Got low byte for CH0\r\n"); - - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x8D); - - I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); data = data | (I2C_read_nack(I2C1) << 8); - I2C_stop(I2C1); DBG_SENSORS("[LIGHT] Got high byte for CH0\r\n"); - + I2C_stop(I2C1); + return data; } uint16_t get_light_ch1(void) @@ -141,18 +135,12 @@ uint16_t get_light_ch1(void) DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n"); I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); - data = I2C_read_nack(I2C1); - I2C_stop(I2C1); + data = I2C_read_ack(I2C1); DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n"); - - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); - I2C_write(I2C1, 0x8F); - - I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); data = data | (I2C_read_nack(I2C1) << 8); - I2C_stop(I2C1); DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n"); - + I2C_stop(I2C1); + return data; } -- libgit2 0.22.2