Commit 36268163fe19db2e49ef4a408f0b2be1960518bd

Authored by Imanol-Mikel Barba Sabariego
1 parent 4115cac3

--no commit message

Project/applications/smartcities/i2c.c
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 3
4 void I2C_init(void) 4 void I2C_init(void)
5 { 5 {
6 - DBG_I2C("Initializing I2C...\r\n"); 6 + //DBG_I2C("Initializing I2C...\r\n");
7 GPIO_InitTypeDef GPIO_InitStruct; 7 GPIO_InitTypeDef GPIO_InitStruct;
8 I2C_InitTypeDef I2C_InitStruct; 8 I2C_InitTypeDef I2C_InitStruct;
9 9
@@ -30,21 +30,21 @@ void I2C_init(void) @@ -30,21 +30,21 @@ void I2C_init(void)
30 30
31 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) 31 void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
32 { 32 {
33 - DBG_I2C("Sending I2C Start...\r\n"); 33 + //DBG_I2C("Sending I2C Start...\r\n");
34 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)) 34 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
35 { 35 {
36 // wait until I2C1 is not busy any more 36 // wait until I2C1 is not busy any more
37 } 37 }
38 - DBG_I2C("I2C ready\r\n"); 38 + //DBG_I2C("I2C ready\r\n");
39 I2C_GenerateSTART(I2Cx, ENABLE); 39 I2C_GenerateSTART(I2Cx, ENABLE);
40 - DBG_I2C("Start sent\r\n"); 40 + //DBG_I2C("Start sent\r\n");
41 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) 41 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
42 { 42 {
43 // wait for I2C1 EV5 --> Slave has acknowledged start condition 43 // wait for I2C1 EV5 --> Slave has acknowledged start condition
44 } 44 }
45 - DBG_I2C("Start ACK\r\n"); 45 + //DBG_I2C("Start ACK\r\n");
46 I2C_Send7bitAddress(I2Cx, address, direction); 46 I2C_Send7bitAddress(I2Cx, address, direction);
47 - DBG_I2C("Master -> Slave Address and R/W sent\r\n"); 47 + //DBG_I2C("Master -> Slave Address and R/W sent\r\n");
48 if(direction == I2C_Direction_Transmitter) 48 if(direction == I2C_Direction_Transmitter)
49 { 49 {
50 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 50 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) @@ -59,21 +59,21 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
59 // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode 59 // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode
60 } 60 }
61 } 61 }
62 - DBG_I2C("Slave -> Master ACK\r\n"); 62 + //DBG_I2C("Slave -> Master ACK\r\n");
63 } 63 }
64 64
65 void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) 65 void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
66 { 66 {
67 - DBG_I2C("Sending repeated I2C Start...\r\n"); 67 + //DBG_I2C("Sending repeated I2C Start...\r\n");
68 I2C_GenerateSTART(I2Cx, ENABLE); 68 I2C_GenerateSTART(I2Cx, ENABLE);
69 - DBG_I2C("Start sent\r\n"); 69 + //DBG_I2C("Start sent\r\n");
70 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) 70 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
71 { 71 {
72 // wait for I2C1 EV5 --> Slave has acknowledged start condition 72 // wait for I2C1 EV5 --> Slave has acknowledged start condition
73 } 73 }
74 - DBG_I2C("Start ACK\r\n"); 74 + //DBG_I2C("Start ACK\r\n");
75 I2C_Send7bitAddress(I2Cx, address, direction); 75 I2C_Send7bitAddress(I2Cx, address, direction);
76 - DBG_I2C("Master -> Slave Address and R/W sent\r\n"); 76 + //DBG_I2C("Master -> Slave Address and R/W sent\r\n");
77 if(direction == I2C_Direction_Transmitter) 77 if(direction == I2C_Direction_Transmitter)
78 { 78 {
79 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 79 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) @@ -88,51 +88,51 @@ void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
88 // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode 88 // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode
89 } 89 }
90 } 90 }
91 - DBG_I2C("Slave -> Master ACK\r\n"); 91 + //DBG_I2C("Slave -> Master ACK\r\n");
92 } 92 }
93 93
94 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) 94 void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
95 { 95 {
96 - DBG_I2C("Sending I2C byte %02x...\r\n",data); 96 + //DBG_I2C("Sending I2C byte %02x...\r\n",data);
97 I2C_SendData(I2Cx, data); 97 I2C_SendData(I2Cx, data);
98 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) 98 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
99 { 99 {
100 // wait for I2C1 EV8_2 --> last byte is transmitted 100 // wait for I2C1 EV8_2 --> last byte is transmitted
101 } 101 }
102 - DBG_I2C("Data sent\r\n"); 102 + //DBG_I2C("Data sent\r\n");
103 } 103 }
104 104
105 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) 105 uint8_t I2C_read_ack(I2C_TypeDef* I2Cx)
106 { 106 {
107 - DBG_I2C("Listening for byte in I2C (ACK)\r\n"); 107 + //DBG_I2C("Listening for byte in I2C (ACK)\r\n");
108 I2C_AcknowledgeConfig(I2Cx, ENABLE); 108 I2C_AcknowledgeConfig(I2Cx, ENABLE);
109 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) 109 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
110 { 110 {
111 // wait until one byte has been received 111 // wait until one byte has been received
112 } 112 }
113 uint8_t data = I2C_ReceiveData(I2Cx); 113 uint8_t data = I2C_ReceiveData(I2Cx);
114 - DBG_I2C("Received byte: %02x\r\n",data); 114 + //DBG_I2C("Received byte: %02x\r\n",data);
115 return data; 115 return data;
116 } 116 }
117 117
118 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) 118 uint8_t I2C_read_nack(I2C_TypeDef* I2Cx)
119 { 119 {
120 - DBG_I2C("Listening for byte in I2C (NACK)\r\n"); 120 + //DBG_I2C("Listening for byte in I2C (NACK)\r\n");
121 I2C_AcknowledgeConfig(I2Cx, DISABLE); 121 I2C_AcknowledgeConfig(I2Cx, DISABLE);
122 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) 122 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))
123 { 123 {
124 // wait until one byte has been received 124 // wait until one byte has been received
125 } 125 }
126 uint8_t data = I2C_ReceiveData(I2Cx); 126 uint8_t data = I2C_ReceiveData(I2Cx);
127 - DBG_I2C("Received byte: %02x\r\n",data); 127 + //DBG_I2C("Received byte: %02x\r\n",data);
128 return data; 128 return data;
129 } 129 }
130 130
131 void I2C_stop(I2C_TypeDef* I2Cx) 131 void I2C_stop(I2C_TypeDef* I2Cx)
132 { 132 {
133 - DBG_I2C("Sending I2C Stop...\r\n"); 133 + //DBG_I2C("Sending I2C Stop...\r\n");
134 I2C_GenerateSTOP(I2Cx, ENABLE); 134 I2C_GenerateSTOP(I2Cx, ENABLE);
135 - DBG_I2C("Stop sent\r\n"); 135 + //DBG_I2C("Stop sent\r\n");
136 } 136 }
137 137
138 uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) 138 uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
@@ -146,14 +146,14 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) @@ -146,14 +146,14 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
146 { 146 {
147 // wait for I2C1 EV5 --> Slave has acknowledged start condition 147 // wait for I2C1 EV5 --> Slave has acknowledged start condition
148 } 148 }
149 - DBG_I2C("Probing address %x ...\r\n",address); 149 + //DBG_I2C("Probing address %x ...\r\n",address);
150 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter); 150 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter);
151 chThdSleepMilliseconds(I2C_TIMEOUT); 151 chThdSleepMilliseconds(I2C_TIMEOUT);
152 if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) 152 if(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
153 { 153 {
154 return 1; 154 return 1;
155 } 155 }
156 - DBG_I2C("I2C address detected: %x\r\n", address); 156 + //DBG_I2C("I2C address detected: %x\r\n", address);
157 I2C_write(I2Cx,0x00); 157 I2C_write(I2Cx,0x00);
158 I2C_stop(I2Cx); 158 I2C_stop(I2Cx);
159 return 0; 159 return 0;
@@ -161,7 +161,7 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) @@ -161,7 +161,7 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address)
161 161
162 void I2C_reset(I2C_TypeDef* I2Cx) 162 void I2C_reset(I2C_TypeDef* I2Cx)
163 { 163 {
164 - DBG_I2C("Resetting I2C...\r\n"); 164 + //DBG_I2C("Resetting I2C...\r\n");
165 I2C_SoftwareResetCmd(I2Cx, ENABLE); 165 I2C_SoftwareResetCmd(I2Cx, ENABLE);
166 I2C_DeInit(I2Cx); 166 I2C_DeInit(I2Cx);
167 I2C_init(); 167 I2C_init();
@@ -170,7 +170,7 @@ void I2C_reset(I2C_TypeDef* I2Cx) @@ -170,7 +170,7 @@ void I2C_reset(I2C_TypeDef* I2Cx)
170 170
171 void I2C_scan(I2C_TypeDef* I2Cx,uint8_t *addresses) 171 void I2C_scan(I2C_TypeDef* I2Cx,uint8_t *addresses)
172 { 172 {
173 - DBG_I2C("Scanning sensors...\r\n"); 173 + //DBG_I2C("Scanning sensors...\r\n");
174 uint8_t i, j = 0; 174 uint8_t i, j = 0;
175 I2C_init(); 175 I2C_init();
176 for(i = 0; i < TOTAL_SENSORS; i++) 176 for(i = 0; i < TOTAL_SENSORS; i++)
Project/applications/smartcities/json.c
@@ -10,7 +10,6 @@ uint8_t register_sensor(sensor sens) @@ -10,7 +10,6 @@ uint8_t register_sensor(sensor sens)
10 DBG_JSON("Putting additional info for pressure sensor...\r\n"); 10 DBG_JSON("Putting additional info for pressure sensor...\r\n");
11 bmp085_callibration_t calib_data; 11 bmp085_callibration_t calib_data;
12 get_pressure_callibration_data(&calib_data); 12 get_pressure_callibration_data(&calib_data);
13 - DBG_JSON("Got callibration data\r\n");  
14 char *callibration_data = callibration_pressure_data_csv(&calib_data); 13 char *callibration_data = callibration_pressure_data_csv(&calib_data);
15 DBG_JSON("callibration_data is: %s\r\n",callibration_data); 14 DBG_JSON("callibration_data is: %s\r\n",callibration_data);
16 statement = prepare_json_register_statement(mod,sens,callibration_data); 15 statement = prepare_json_register_statement(mod,sens,callibration_data);
@@ -97,13 +96,14 @@ char* prepare_json_register_statement(module mod, sensor sens, char *additional_ @@ -97,13 +96,14 @@ char* prepare_json_register_statement(module mod, sensor sens, char *additional_
97 96
98 char* prepare_observation(char* observation, uint32_t length) 97 char* prepare_observation(char* observation, uint32_t length)
99 { 98 {
  99 + DBG_JSON("Preparing: %s with length %d\r\n",observation,length);
100 char *json_data, *str_aux, *str_aux2; 100 char *json_data, *str_aux, *str_aux2;
101 int value_length = find_next_index(observation,length,','); 101 int value_length = find_next_index(observation,length,',');
102 while(1) 102 while(1)
103 { 103 {
104 - if(find_next_index(observation+value_length+1,length,',')) 104 + if(find_next_index(observation+value_length+1,length-value_length,','))
105 { 105 {
106 - value_length += find_next_index(observation+value_length+1,length,',')+1; 106 + value_length += find_next_index(observation+value_length+1,length-value_length,',')+1;
107 } 107 }
108 else 108 else
109 { 109 {
Project/applications/smartcities/main.c
@@ -235,6 +235,7 @@ int main(void) @@ -235,6 +235,7 @@ int main(void)
235 } 235 }
236 236
237 DBG("%c[1;35m--------------WI-SENSE STARTED-------------%c[1;00m\r\n",0x1B,0x1B); 237 DBG("%c[1;35m--------------WI-SENSE STARTED-------------%c[1;00m\r\n",0x1B,0x1B);
  238 +
238 wifi_connect(); 239 wifi_connect();
239 check_memory(); 240 check_memory();
240 241
Project/applications/smartcities/sensors.c
@@ -114,22 +114,16 @@ uint16_t get_light_ch0(void) @@ -114,22 +114,16 @@ uint16_t get_light_ch0(void)
114 uint16_t data = 0; 114 uint16_t data = 0;
115 115
116 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); 116 I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
117 - I2C_write(I2C1, 0x8C); 117 + I2C_write(I2C1, 0xAC);
118 DBG_SENSORS("[LIGHT] Sent fetch command for CH0\r\n"); 118 DBG_SENSORS("[LIGHT] Sent fetch command for CH0\r\n");
119 119
120 I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); 120 I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
121 - data = I2C_read_nack(I2C1);  
122 - I2C_stop(I2C1); 121 + data = I2C_read_ack(I2C1);
123 DBG_SENSORS("[LIGHT] Got low byte for CH0\r\n"); 122 DBG_SENSORS("[LIGHT] Got low byte for CH0\r\n");
124 -  
125 - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);  
126 - I2C_write(I2C1, 0x8D);  
127 -  
128 - I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);  
129 data = data | (I2C_read_nack(I2C1) << 8); 123 data = data | (I2C_read_nack(I2C1) << 8);
130 - I2C_stop(I2C1);  
131 DBG_SENSORS("[LIGHT] Got high byte for CH0\r\n"); 124 DBG_SENSORS("[LIGHT] Got high byte for CH0\r\n");
132 - 125 + I2C_stop(I2C1);
  126 +
133 return data; 127 return data;
134 } 128 }
135 uint16_t get_light_ch1(void) 129 uint16_t get_light_ch1(void)
@@ -141,18 +135,12 @@ uint16_t get_light_ch1(void) @@ -141,18 +135,12 @@ uint16_t get_light_ch1(void)
141 DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n"); 135 DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n");
142 136
143 I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); 137 I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
144 - data = I2C_read_nack(I2C1);  
145 - I2C_stop(I2C1); 138 + data = I2C_read_ack(I2C1);
146 DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n"); 139 DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n");
147 -  
148 - I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);  
149 - I2C_write(I2C1, 0x8F);  
150 -  
151 - I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);  
152 data = data | (I2C_read_nack(I2C1) << 8); 140 data = data | (I2C_read_nack(I2C1) << 8);
153 - I2C_stop(I2C1);  
154 DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n"); 141 DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n");
155 - 142 + I2C_stop(I2C1);
  143 +
156 return data; 144 return data;
157 } 145 }
158 146