Commit b3f997a0600a0d7ce6cd2a390c37cc85d97ae578
1 parent
e08f5763
--no commit message
Showing
7 changed files
with
145 additions
and
115 deletions
Project/applications/smartcities/i2c.c
@@ -27,6 +27,9 @@ void I2C_init(void) | @@ -27,6 +27,9 @@ void I2C_init(void) | ||
27 | I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses | 27 | I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses |
28 | I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 | 28 | I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 |
29 | 29 | ||
30 | + //Enable clock stretching | ||
31 | + I2C_StretchClockCmd(I2C1, ENABLE); | ||
32 | + | ||
30 | // enable I2C1 | 33 | // enable I2C1 |
31 | I2C_Cmd(I2C1, ENABLE); //sets PE bit in CR1, at end` | 34 | I2C_Cmd(I2C1, ENABLE); //sets PE bit in CR1, at end` |
32 | } | 35 | } |
@@ -38,18 +41,53 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) | @@ -38,18 +41,53 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) | ||
38 | { | 41 | { |
39 | // wait until I2C1 is not busy any more | 42 | // wait until I2C1 is not busy any more |
40 | } | 43 | } |
44 | + printf("I2C ready\r\n"); | ||
45 | + I2C_GenerateSTART(I2Cx, ENABLE); | ||
46 | + printf("Start sent\r\n"); | ||
47 | + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) | ||
48 | + { | ||
49 | + // wait for I2C1 EV5 --> Slave has acknowledged start condition | ||
50 | + } | ||
51 | + printf("Start ACK\r\n"); | ||
52 | + I2C_Send7bitAddress(I2Cx, address, direction); | ||
53 | + printf("Master -> Slave Address and R/W sent\r\n"); | ||
54 | + if(direction == I2C_Direction_Transmitter) | ||
55 | + { | ||
56 | + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) | ||
57 | + { | ||
58 | + // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode | ||
59 | + } | ||
60 | + printf("Slave -> Master ACK\r\n"); | ||
61 | + } | ||
62 | + else if(direction == I2C_Direction_Receiver) | ||
63 | + { | ||
64 | + while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) | ||
65 | + { | ||
66 | + // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode | ||
67 | + } | ||
68 | + printf("Slave -> Master ACK\r\n"); | ||
69 | + } | ||
70 | +} | ||
71 | + | ||
72 | +void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) | ||
73 | +{ | ||
74 | + printf("Sending repeated I2C Start...\r\n"); | ||
41 | I2C_GenerateSTART(I2Cx, ENABLE); | 75 | I2C_GenerateSTART(I2Cx, ENABLE); |
76 | + printf("Start sent\r\n"); | ||
42 | while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) | 77 | while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)) |
43 | { | 78 | { |
44 | // wait for I2C1 EV5 --> Slave has acknowledged start condition | 79 | // wait for I2C1 EV5 --> Slave has acknowledged start condition |
45 | } | 80 | } |
81 | + printf("Start ACK\r\n"); | ||
46 | I2C_Send7bitAddress(I2Cx, address, direction); | 82 | I2C_Send7bitAddress(I2Cx, address, direction); |
83 | + printf("Master -> Slave Address and R/W sent\r\n"); | ||
47 | if(direction == I2C_Direction_Transmitter) | 84 | if(direction == I2C_Direction_Transmitter) |
48 | { | 85 | { |
49 | while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) | 86 | while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) |
50 | { | 87 | { |
51 | // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode | 88 | // wait for I2Cx EV6, check if Slave has acknowledged Master transmitter mode |
52 | } | 89 | } |
90 | + printf("Slave -> Master ACK\r\n"); | ||
53 | } | 91 | } |
54 | else if(direction == I2C_Direction_Receiver) | 92 | else if(direction == I2C_Direction_Receiver) |
55 | { | 93 | { |
@@ -57,6 +95,7 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) | @@ -57,6 +95,7 @@ void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction) | ||
57 | { | 95 | { |
58 | // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode | 96 | // wait for I2Cx EV6, check if Slave has acknowledged Master receiver mode |
59 | } | 97 | } |
98 | + printf("Slave -> Master ACK\r\n"); | ||
60 | } | 99 | } |
61 | } | 100 | } |
62 | 101 | ||
@@ -68,6 +107,7 @@ void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) | @@ -68,6 +107,7 @@ void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) | ||
68 | { | 107 | { |
69 | // wait for I2C1 EV8_2 --> last byte is transmitted | 108 | // wait for I2C1 EV8_2 --> last byte is transmitted |
70 | } | 109 | } |
110 | + printf("Data sent\r\n"); | ||
71 | } | 111 | } |
72 | 112 | ||
73 | uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) | 113 | uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) |
@@ -105,6 +145,7 @@ void I2C_stop(I2C_TypeDef* I2Cx) | @@ -105,6 +145,7 @@ void I2C_stop(I2C_TypeDef* I2Cx) | ||
105 | { | 145 | { |
106 | // wait for I2C1 EV8_2 --> byte has been transmitted | 146 | // wait for I2C1 EV8_2 --> byte has been transmitted |
107 | }*/ | 147 | }*/ |
148 | + printf("Stop sent\r\n"); | ||
108 | } | 149 | } |
109 | 150 | ||
110 | uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) | 151 | uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) |
@@ -126,34 +167,34 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) | @@ -126,34 +167,34 @@ uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address) | ||
126 | return 1; | 167 | return 1; |
127 | } | 168 | } |
128 | printf("I2C address detected: %x\r\n", address); | 169 | printf("I2C address detected: %x\r\n", address); |
129 | - I2C_write(I2C1,0x00); | ||
130 | - I2C_stop(I2C1); | 170 | + I2C_write(I2Cx,0x00); |
171 | + I2C_stop(I2Cx); | ||
131 | return 0; | 172 | return 0; |
132 | } | 173 | } |
133 | 174 | ||
134 | -void I2C_reset() | 175 | +void I2C_reset(I2C_TypeDef* I2Cx) |
135 | { | 176 | { |
136 | printf("Resetting I2C...\r\n"); | 177 | printf("Resetting I2C...\r\n"); |
137 | - I2C_SoftwareResetCmd(I2C1, ENABLE); | ||
138 | - I2C_DeInit(I2C1); | 178 | + I2C_SoftwareResetCmd(I2Cx, ENABLE); |
179 | + I2C_DeInit(I2Cx); | ||
139 | I2C_init(); | 180 | I2C_init(); |
140 | 181 | ||
141 | } | 182 | } |
142 | 183 | ||
143 | -void I2C_scan(uint8_t *addresses) | 184 | +void I2C_scan(I2C_TypeDef* I2Cx,uint8_t *addresses) |
144 | { | 185 | { |
145 | uint8_t i, j = 0; | 186 | uint8_t i, j = 0; |
146 | I2C_init(); | 187 | I2C_init(); |
147 | for(i = 0; i < TOTAL_SENSORS; i++) | 188 | for(i = 0; i < TOTAL_SENSORS; i++) |
148 | { | 189 | { |
149 | - if(!I2C_check(I2C1,sensors[i]->ID << 1)) | 190 | + if(!I2C_check(I2Cx,sensors[i]->ID << 1)) |
150 | { | 191 | { |
151 | addresses[j++] = sensors[i]->ID; | 192 | addresses[j++] = sensors[i]->ID; |
152 | register_sensor(*sensors[i]); | 193 | register_sensor(*sensors[i]); |
153 | } | 194 | } |
154 | else | 195 | else |
155 | { | 196 | { |
156 | - I2C_reset(); | 197 | + I2C_reset(I2Cx); |
157 | } | 198 | } |
158 | } | 199 | } |
159 | register_sensor(battery); | 200 | register_sensor(battery); |
Project/applications/smartcities/include/i2c.h
@@ -15,9 +15,10 @@ void I2C_stop(I2C_TypeDef* I2Cx); | @@ -15,9 +15,10 @@ void I2C_stop(I2C_TypeDef* I2Cx); | ||
15 | void I2C_write(I2C_TypeDef* I2Cx, uint8_t data); | 15 | void I2C_write(I2C_TypeDef* I2Cx, uint8_t data); |
16 | uint8_t I2C_read_ack(I2C_TypeDef* I2Cx); | 16 | uint8_t I2C_read_ack(I2C_TypeDef* I2Cx); |
17 | uint8_t I2C_read_nack(I2C_TypeDef* I2Cx); | 17 | uint8_t I2C_read_nack(I2C_TypeDef* I2Cx); |
18 | -void I2C_scan(uint8_t *addresses); | 18 | +void I2C_scan(I2C_TypeDef* I2Cx,uint8_t *addresses); |
19 | uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address); | 19 | uint8_t I2C_check(I2C_TypeDef* I2Cx, uint8_t address); |
20 | -void I2C_reset(void); | 20 | +void I2C_reset(I2C_TypeDef* I2Cx); |
21 | +void I2C_restart(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction); | ||
21 | 22 | ||
22 | 23 | ||
23 | #endif | 24 | #endif |
Project/applications/smartcities/include/json.h
@@ -20,7 +20,7 @@ | @@ -20,7 +20,7 @@ | ||
20 | 20 | ||
21 | uint8_t register_sensor(sensor sens); | 21 | uint8_t register_sensor(sensor sens); |
22 | char* prepare_json_observation_statement(char** data, uint32_t nObservations); | 22 | char* prepare_json_observation_statement(char** data, uint32_t nObservations); |
23 | -char* prepare_json_register_statement(module mod, sensor sens); | 23 | +char* prepare_json_register_statement(module mod, sensor sens, char *additional_info); |
24 | char* prepare_observation(char* observation, uint32_t length); | 24 | char* prepare_observation(char* observation, uint32_t length); |
25 | uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); | 25 | uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); |
26 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); | 26 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); |
Project/applications/smartcities/include/sensors.h
@@ -24,7 +24,6 @@ typedef struct { | @@ -24,7 +24,6 @@ typedef struct { | ||
24 | char* description; | 24 | char* description; |
25 | char* type; | 25 | char* type; |
26 | char* unit; | 26 | char* unit; |
27 | - char* additional_info; | ||
28 | } sensor; | 27 | } sensor; |
29 | 28 | ||
30 | void wakeup_sensors(uint8_t logic_address); | 29 | void wakeup_sensors(uint8_t logic_address); |
@@ -50,8 +49,6 @@ char* light_value(uint32_t light); | @@ -50,8 +49,6 @@ char* light_value(uint32_t light); | ||
50 | //ULTRASONIC SENSOR | 49 | //ULTRASONIC SENSOR |
51 | uint16_t get_distance_data(void); | 50 | uint16_t get_distance_data(void); |
52 | void init_ultrasound(void); | 51 | void init_ultrasound(void); |
53 | -uint8_t get_distance_low(void); | ||
54 | -uint8_t get_distance_high(void); | ||
55 | char* distance_value(uint16_t distance); | 52 | char* distance_value(uint16_t distance); |
56 | 53 | ||
57 | 54 |
Project/applications/smartcities/json.c
@@ -7,14 +7,15 @@ uint8_t register_sensor(sensor sens) | @@ -7,14 +7,15 @@ uint8_t register_sensor(sensor sens) | ||
7 | char *statement; | 7 | char *statement; |
8 | if(sens.ID == PRESSURE_ADDR) | 8 | if(sens.ID == PRESSURE_ADDR) |
9 | { | 9 | { |
10 | - sens.additional_info = callibration_pressure_data_csv(get_pressure_callibration_data()); | ||
11 | - statement = prepare_json_register_statement(mod,sens); | ||
12 | - chHeapFree(sens.additional_info); | ||
13 | - sens.additional_info = NULL; | 10 | + printf("Putting additional info for pressure sensor...\r\n"); |
11 | + char *callibration_data = callibration_pressure_data_csv(get_pressure_callibration_data()); | ||
12 | + printf("callibration_data is: %s\r\n",callibration_data); | ||
13 | + statement = prepare_json_register_statement(mod,sens,callibration_data); | ||
14 | + chHeapFree(callibration_data); | ||
14 | } | 15 | } |
15 | else | 16 | else |
16 | { | 17 | { |
17 | - statement = prepare_json_register_statement(mod,sens); | 18 | + statement = prepare_json_register_statement(mod,sens,NULL); |
18 | } | 19 | } |
19 | printf("%s\r\n",statement); | 20 | printf("%s\r\n",statement); |
20 | char sensor_ID[3]; | 21 | char sensor_ID[3]; |
@@ -44,7 +45,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | @@ -44,7 +45,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | ||
44 | return json_statement; | 45 | return json_statement; |
45 | } | 46 | } |
46 | 47 | ||
47 | -char* prepare_json_register_statement(module mod, sensor sens) | 48 | +char* prepare_json_register_statement(module mod, sensor sens, char *additional_info) |
48 | { | 49 | { |
49 | printf("Registering sensor: %s in: %s\r\n",sens.description,mod.geoloc); | 50 | printf("Registering sensor: %s in: %s\r\n",sens.description,mod.geoloc); |
50 | unsigned int length; | 51 | unsigned int length; |
@@ -79,11 +80,15 @@ char* prepare_json_register_statement(module mod, sensor sens) | @@ -79,11 +80,15 @@ char* prepare_json_register_statement(module mod, sensor sens) | ||
79 | str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); | 80 | str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); |
80 | chHeapFree(str_aux2); | 81 | chHeapFree(str_aux2); |
81 | length += strlen(mod.geoloc); | 82 | length += strlen(mod.geoloc); |
82 | - if(sens.additional_info != NULL) | 83 | + if(additional_info != NULL) |
83 | { | 84 | { |
84 | - str_aux2 = join_strings(str_aux,sens.additional_info,length,strlen(sens.additional_info),JOIN_NO_FREE); | 85 | + printf("In prepare_json_register_statement: Detected additional_info\r\n"); |
86 | + str_aux2 = join_strings(str_aux,"\",\"additionalInfo\":\"",length,20,JOIN_NO_FREE); | ||
87 | + chHeapFree(str_aux); | ||
88 | + length += 20; | ||
89 | + str_aux = join_strings(str_aux2,additional_info,length,strlen(additional_info),JOIN_NO_FREE); | ||
85 | chHeapFree(str_aux2); | 90 | chHeapFree(str_aux2); |
86 | - length += strlen(sens.additional_info); | 91 | + length += strlen(additional_info); |
87 | } | 92 | } |
88 | json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); | 93 | json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); |
89 | chHeapFree(str_aux); | 94 | chHeapFree(str_aux); |
Project/applications/smartcities/main2.c
@@ -140,10 +140,10 @@ void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, | @@ -140,10 +140,10 @@ void put_buffers(char** buffers[],uint32_t ind[],uint32_t sizes[],char** cooked, | ||
140 | printf("For %d and %d\n\r", ind[i],sizes[i]); | 140 | printf("For %d and %d\n\r", ind[i],sizes[i]); |
141 | printf("Putting data: %s\n\r",cooked[i]); | 141 | printf("Putting data: %s\n\r",cooked[i]); |
142 | buffers[i] = put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); | 142 | buffers[i] = put_message(cooked[i], buffers[i] ,&ind[i],&sizes[i]); |
143 | - printf("Message put: %s\r\n",buffers[0][ind[0]-1]); | 143 | + printf("Message put: %s\r\n",buffers[i][ind[i]-1]); |
144 | chHeapFree(cooked[i]); | 144 | chHeapFree(cooked[i]); |
145 | printf("Memory freed\n\r"); | 145 | printf("Memory freed\n\r"); |
146 | - printf("Message put (assertion): %s\r\n",buffers[0][ind[0]-1]); | 146 | + printf("Message put (assertion): %s with length %d\r\n",buffers[i][ind[i]-1] ,strlen(buffers[i][ind[i]-1])); |
147 | } | 147 | } |
148 | 148 | ||
149 | } | 149 | } |
@@ -238,14 +238,10 @@ int main(void) | @@ -238,14 +238,10 @@ int main(void) | ||
238 | printf("Scanning sensors...\r\n"); | 238 | printf("Scanning sensors...\r\n"); |
239 | //Escanea los sensores -> retorna un vector con las direcciones en cada posiciรณn del vector, si la posiciรณn del vector retorna un cero -> no existe el sensor | 239 | //Escanea los sensores -> retorna un vector con las direcciones en cada posiciรณn del vector, si la posiciรณn del vector retorna un cero -> no existe el sensor |
240 | //Escanea y registra | 240 | //Escanea y registra |
241 | - I2C_scan(sensors); | 241 | + I2C_scan(I2C1,sensors); |
242 | sensors_length=strlen((char*)sensors); | 242 | sensors_length=strlen((char*)sensors); |
243 | printf("%d sensor detected...\r\n",sensors_length); | 243 | printf("%d sensor detected...\r\n",sensors_length); |
244 | 244 | ||
245 | - I2C_init(); | ||
246 | - printf("%s\r\n",light_value(get_light_data())); | ||
247 | - printf("%s\r\n",light_value(get_light_data())); | ||
248 | - | ||
249 | unsigned long time; | 245 | unsigned long time; |
250 | update_time(&time); | 246 | update_time(&time); |
251 | //desconectarwifi | 247 | //desconectarwifi |
@@ -262,15 +258,26 @@ int main(void) | @@ -262,15 +258,26 @@ int main(void) | ||
262 | int i = 1; | 258 | int i = 1; |
263 | 259 | ||
264 | while(1){ | 260 | while(1){ |
261 | + if(ind[0]) | ||
262 | + { | ||
263 | + printf("Data stored in buffer: %s with index %d and size %d\r\n",buffers[0][ind[0]-1], ind[0],sizes[0]); | ||
264 | + } | ||
265 | time += getElapsedTime(delay); | 265 | time += getElapsedTime(delay); |
266 | printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); | 266 | printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); |
267 | 267 | ||
268 | delay = getSystemTime(); | 268 | delay = getSystemTime(); |
269 | /* Collect data from sensors */ | 269 | /* Collect data from sensors */ |
270 | + if(ind[0]) | ||
271 | + { | ||
272 | + printf("Data stored in buffer: %s with index %d and size %d\r\n",buffers[0][ind[0]-1], ind[0],sizes[0]); | ||
273 | + } | ||
270 | printf("Collecting data...\r\n"); | 274 | printf("Collecting data...\r\n"); |
271 | collectData(valueSensors, sensors); | 275 | collectData(valueSensors, sensors); |
272 | printf("Data collected...\r\n"); | 276 | printf("Data collected...\r\n"); |
273 | - | 277 | + if(ind[0]) |
278 | + { | ||
279 | + printf("Data stored in buffer: %s with index %d and size %d\r\n",buffers[0][ind[0]-1], ind[0],sizes[0]); | ||
280 | + } | ||
274 | time += getElapsedTime(delay); | 281 | time += getElapsedTime(delay); |
275 | printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); | 282 | printf("time (absolute):\t%ul\r\ntime mod LONG_PERIOD:\t%ul\r\ntime mod SHORT_PERIOD:\t%ul\r\n",time,time%LONG_PERIOD,time%SHORT_PERIOD); |
276 | timestamp = time; | 283 | timestamp = time; |
@@ -363,7 +370,6 @@ int main(void) | @@ -363,7 +370,6 @@ int main(void) | ||
363 | /* | 370 | /* |
364 | * TO-DO | 371 | * TO-DO |
365 | * | 372 | * |
366 | - * - I2C issues | ||
367 | * - Test all sensors | 373 | * - Test all sensors |
368 | * - Test ADC | 374 | * - Test ADC |
369 | * - Test register, data send and battery send | 375 | * - Test register, data send and battery send |
Project/applications/smartcities/sensors.c
1 | #include "sensors.h" | 1 | #include "sensors.h" |
2 | 2 | ||
3 | -sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux", NULL}; | ||
4 | -sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm", NULL}; | ||
5 | -sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa", NULL}; | ||
6 | -sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Temperature and humidity sensor", "temperature,humidity", "ยบC,RH",NULL}; | ||
7 | -sensor sound_sensor = {SOUND_ADDR, "Sound sensor", "sound", "mV", NULL}; | ||
8 | -sensor battery = {BATTERY_ADDR, "Battery Level", "power", "mV", NULL}; | 3 | +sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux"}; |
4 | +sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm"}; | ||
5 | +sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa"}; | ||
6 | +sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Temperature and humidity sensor", "temperature,humidity", "ยบC,RH"}; | ||
7 | +sensor sound_sensor = {SOUND_ADDR, "Sound sensor", "sound", "mV"}; | ||
8 | +sensor battery = {BATTERY_ADDR, "Battery Level", "power", "mV"}; | ||
9 | 9 | ||
10 | sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery}; | 10 | sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery}; |
11 | 11 | ||
@@ -92,13 +92,12 @@ uint16_t get_light_ch0(void) | @@ -92,13 +92,12 @@ uint16_t get_light_ch0(void) | ||
92 | 92 | ||
93 | I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | 93 | I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); |
94 | I2C_write(I2C1, 0x0C); | 94 | I2C_write(I2C1, 0x0C); |
95 | - I2C_stop(I2C1); | ||
96 | printf("LIGHT: Sent fetch command for CH1\r\n"); | 95 | printf("LIGHT: Sent fetch command for CH1\r\n"); |
97 | 96 | ||
98 | - I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | 97 | + I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); |
99 | data = I2C_read_ack(I2C1); | 98 | data = I2C_read_ack(I2C1); |
100 | printf("LIGHT: Got low byte for CH1\r\n"); | 99 | printf("LIGHT: Got low byte for CH1\r\n"); |
101 | - data = data | (I2C_read_ack(I2C1) << 8); | 100 | + data = data | (I2C_read_nack(I2C1) << 8); |
102 | printf("LIGHT: Got high byte for CH1\r\n"); | 101 | printf("LIGHT: Got high byte for CH1\r\n"); |
103 | I2C_stop(I2C1); | 102 | I2C_stop(I2C1); |
104 | 103 | ||
@@ -110,13 +109,12 @@ uint16_t get_light_ch1(void) | @@ -110,13 +109,12 @@ uint16_t get_light_ch1(void) | ||
110 | 109 | ||
111 | I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); | 110 | I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter); |
112 | I2C_write(I2C1, 0x0E); | 111 | I2C_write(I2C1, 0x0E); |
113 | - I2C_stop(I2C1); | ||
114 | printf("LIGHT: Sent fetch command for CH1\r\n"); | 112 | printf("LIGHT: Sent fetch command for CH1\r\n"); |
115 | 113 | ||
116 | - I2C_start(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); | 114 | + I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver); |
117 | data = I2C_read_ack(I2C1); | 115 | data = I2C_read_ack(I2C1); |
118 | printf("LIGHT: Got low byte for CH1\r\n"); | 116 | printf("LIGHT: Got low byte for CH1\r\n"); |
119 | - data = data | (I2C_read_ack(I2C1) << 8); | 117 | + data = data | (I2C_read_nack(I2C1) << 8); |
120 | printf("LIGHT: Got high byte for CH1\r\n"); | 118 | printf("LIGHT: Got high byte for CH1\r\n"); |
121 | I2C_stop(I2C1); | 119 | I2C_stop(I2C1); |
122 | 120 | ||
@@ -127,48 +125,29 @@ uint16_t get_distance_data(void) | @@ -127,48 +125,29 @@ uint16_t get_distance_data(void) | ||
127 | { | 125 | { |
128 | init_ultrasound(); | 126 | init_ultrasound(); |
129 | printf("DISTANCE: Initialized\r\n"); | 127 | printf("DISTANCE: Initialized\r\n"); |
130 | - uint16_t data = get_distance_high(); | ||
131 | - printf("Got distance high byte\r\n"); | ||
132 | - data = data << 8; | ||
133 | - data = data | get_distance_low(); | ||
134 | - printf("Got distance low byte\r\n"); | ||
135 | - return data; | ||
136 | -} | ||
137 | -void init_ultrasound(void) | ||
138 | -{ | ||
139 | - I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | ||
140 | - I2C_write(I2C1, 0x00); | ||
141 | - I2C_stop(I2C1); | ||
142 | 128 | ||
129 | + uint16_t data; | ||
143 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | 130 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); |
144 | - I2C_write(I2C1, 0x50); | ||
145 | - I2C_stop(I2C1); | 131 | + I2C_write(I2C1, 0x02); |
146 | 132 | ||
147 | - chThdSleepMilliseconds(70); | ||
148 | -} | ||
149 | -uint8_t get_distance_low(void) | ||
150 | -{ | ||
151 | - uint8_t data; | ||
152 | - I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | ||
153 | - I2C_write(I2C1, 0x03); | ||
154 | - I2C_stop(I2C1); | ||
155 | - | ||
156 | - I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | 133 | + I2C_restart(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); |
157 | data = I2C_read_ack(I2C1); | 134 | data = I2C_read_ack(I2C1); |
135 | + printf("DISTANCE: Got high byte\r\n"); | ||
136 | + data = data << 8; | ||
137 | + data = data | I2C_read_nack(I2C1); | ||
138 | + printf("DISTANCE: Got low byte\r\n"); | ||
158 | I2C_stop(I2C1); | 139 | I2C_stop(I2C1); |
159 | return data; | 140 | return data; |
160 | } | 141 | } |
161 | -uint8_t get_distance_high(void) | 142 | + |
143 | +void init_ultrasound(void) | ||
162 | { | 144 | { |
163 | - uint8_t data; | ||
164 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | 145 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); |
165 | - I2C_write(I2C1, 0x02); | 146 | + I2C_write(I2C1, 0x00); |
147 | + I2C_write(I2C1, 0x50); | ||
166 | I2C_stop(I2C1); | 148 | I2C_stop(I2C1); |
167 | - | ||
168 | - I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | ||
169 | - data = I2C_read_ack(I2C1); | ||
170 | - I2C_stop(I2C1); | ||
171 | - return data; | 149 | + |
150 | + chThdSleepMilliseconds(70); | ||
172 | } | 151 | } |
173 | 152 | ||
174 | uint16_t get_pressure_data(void) | 153 | uint16_t get_pressure_data(void) |
@@ -178,9 +157,8 @@ uint16_t get_pressure_data(void) | @@ -178,9 +157,8 @@ uint16_t get_pressure_data(void) | ||
178 | printf("PRESSURE: Initialized\r\n"); | 157 | printf("PRESSURE: Initialized\r\n"); |
179 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | 158 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); |
180 | I2C_write(I2C1, 0xF6); | 159 | I2C_write(I2C1, 0xF6); |
181 | - I2C_stop(I2C1); | ||
182 | - | ||
183 | - I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | 160 | + |
161 | + I2C_restart(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | ||
184 | data = I2C_read_ack(I2C1); | 162 | data = I2C_read_ack(I2C1); |
185 | printf("PRESSURE: Got high byte\r\n"); | 163 | printf("PRESSURE: Got high byte\r\n"); |
186 | data = data << 8; | 164 | data = data << 8; |
@@ -196,9 +174,8 @@ uint16_t get_pressure_temperature_data(void) | @@ -196,9 +174,8 @@ uint16_t get_pressure_temperature_data(void) | ||
196 | printf("PRESSURE: Initialized temperature\r\n"); | 174 | printf("PRESSURE: Initialized temperature\r\n"); |
197 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); | 175 | I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter); |
198 | I2C_write(I2C1, 0xF6); | 176 | I2C_write(I2C1, 0xF6); |
199 | - I2C_stop(I2C1); | ||
200 | 177 | ||
201 | - I2C_start(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); | 178 | + I2C_restart(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver); |
202 | data = I2C_read_ack(I2C1); | 179 | data = I2C_read_ack(I2C1); |
203 | printf("PRESSURE: Got temperature high byte\r\n"); | 180 | printf("PRESSURE: Got temperature high byte\r\n"); |
204 | data = data << 8; | 181 | data = data << 8; |
@@ -214,87 +191,88 @@ bmp085_callibration get_pressure_callibration_data(void) | @@ -214,87 +191,88 @@ bmp085_callibration get_pressure_callibration_data(void) | ||
214 | 191 | ||
215 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 192 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
216 | I2C_write(I2C1, 0xAA); | 193 | I2C_write(I2C1, 0xAA); |
217 | - I2C_stop(I2C1); | ||
218 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
219 | - calib_data.AC1 = I2C_read_ack(I2C1); | 194 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
195 | + calib_data.AC1 = (I2C_read_ack(I2C1) << 8); | ||
196 | + calib_data.AC1 |= I2C_read_nack(I2C1); | ||
220 | I2C_stop(I2C1); | 197 | I2C_stop(I2C1); |
221 | 198 | ||
222 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 199 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
223 | I2C_write(I2C1, 0xAC); | 200 | I2C_write(I2C1, 0xAC); |
224 | - I2C_stop(I2C1); | ||
225 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
226 | - calib_data.AC2 = I2C_read_ack(I2C1); | 201 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
202 | + calib_data.AC2 = (I2C_read_ack(I2C1) << 8); | ||
203 | + calib_data.AC2 |= I2C_read_nack(I2C1); | ||
227 | I2C_stop(I2C1); | 204 | I2C_stop(I2C1); |
228 | 205 | ||
229 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 206 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
230 | I2C_write(I2C1, 0xAE); | 207 | I2C_write(I2C1, 0xAE); |
231 | - I2C_stop(I2C1); | ||
232 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
233 | - calib_data.AC3 = I2C_read_ack(I2C1); | 208 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
209 | + calib_data.AC3 = (I2C_read_ack(I2C1) << 8); | ||
210 | + calib_data.AC3 |= I2C_read_nack(I2C1); | ||
234 | I2C_stop(I2C1); | 211 | I2C_stop(I2C1); |
235 | 212 | ||
236 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 213 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
237 | I2C_write(I2C1, 0xB0); | 214 | I2C_write(I2C1, 0xB0); |
238 | - I2C_stop(I2C1); | ||
239 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
240 | - calib_data.AC4 = I2C_read_ack(I2C1); | 215 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
216 | + calib_data.AC4 = (I2C_read_ack(I2C1) << 8); | ||
217 | + calib_data.AC4 |= I2C_read_nack(I2C1); | ||
241 | I2C_stop(I2C1); | 218 | I2C_stop(I2C1); |
242 | 219 | ||
243 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 220 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
244 | I2C_write(I2C1, 0xB2); | 221 | I2C_write(I2C1, 0xB2); |
245 | - I2C_stop(I2C1); | ||
246 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
247 | - calib_data.AC5 = I2C_read_ack(I2C1); | 222 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
223 | + calib_data.AC5 = (I2C_read_ack(I2C1) << 8); | ||
224 | + calib_data.AC5 |= I2C_read_nack(I2C1); | ||
248 | I2C_stop(I2C1); | 225 | I2C_stop(I2C1); |
249 | 226 | ||
250 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 227 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
251 | I2C_write(I2C1, 0xB4); | 228 | I2C_write(I2C1, 0xB4); |
252 | - I2C_stop(I2C1); | ||
253 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
254 | - calib_data.AC6 = I2C_read_ack(I2C1); | 229 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
230 | + calib_data.AC6 = (I2C_read_ack(I2C1) << 8); | ||
231 | + calib_data.AC6 |= I2C_read_nack(I2C1); | ||
255 | I2C_stop(I2C1); | 232 | I2C_stop(I2C1); |
256 | 233 | ||
257 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 234 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
258 | I2C_write(I2C1, 0xB6); | 235 | I2C_write(I2C1, 0xB6); |
259 | - I2C_stop(I2C1); | ||
260 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
261 | - calib_data.B1 = I2C_read_ack(I2C1); | 236 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
237 | + calib_data.B1 = (I2C_read_ack(I2C1) << 8); | ||
238 | + calib_data.B1 |= I2C_read_nack(I2C1); | ||
262 | I2C_stop(I2C1); | 239 | I2C_stop(I2C1); |
263 | 240 | ||
264 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 241 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
265 | I2C_write(I2C1, 0xB8); | 242 | I2C_write(I2C1, 0xB8); |
266 | - I2C_stop(I2C1); | ||
267 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
268 | - calib_data.B2 = I2C_read_ack(I2C1); | 243 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
244 | + calib_data.B2 = (I2C_read_ack(I2C1) << 8); | ||
245 | + calib_data.B2 |= I2C_read_nack(I2C1); | ||
269 | I2C_stop(I2C1); | 246 | I2C_stop(I2C1); |
270 | 247 | ||
271 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 248 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
272 | I2C_write(I2C1, 0xBA); | 249 | I2C_write(I2C1, 0xBA); |
273 | - I2C_stop(I2C1); | ||
274 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
275 | - calib_data.MB = I2C_read_ack(I2C1); | 250 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
251 | + calib_data.MB = (I2C_read_ack(I2C1) << 8); | ||
252 | + calib_data.MB |= I2C_read_nack(I2C1); | ||
276 | I2C_stop(I2C1); | 253 | I2C_stop(I2C1); |
277 | 254 | ||
278 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 255 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
279 | I2C_write(I2C1, 0xBC); | 256 | I2C_write(I2C1, 0xBC); |
280 | - I2C_stop(I2C1); | ||
281 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
282 | - calib_data.MC = I2C_read_ack(I2C1); | 257 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
258 | + calib_data.MC = (I2C_read_ack(I2C1) << 8); | ||
259 | + calib_data.MC |= I2C_read_nack(I2C1); | ||
283 | I2C_stop(I2C1); | 260 | I2C_stop(I2C1); |
284 | 261 | ||
285 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); | 262 | I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter); |
286 | I2C_write(I2C1, 0xBE); | 263 | I2C_write(I2C1, 0xBE); |
287 | - I2C_stop(I2C1); | ||
288 | - I2C_start(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); | ||
289 | - calib_data.MD = I2C_read_ack(I2C1); | 264 | + I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver); |
265 | + calib_data.MD = (I2C_read_ack(I2C1) << 8); | ||
266 | + calib_data.MD |= I2C_read_nack(I2C1); | ||
290 | I2C_stop(I2C1); | 267 | I2C_stop(I2C1); |
268 | + | ||
291 | printf("PRESSURE: Got callibration data\r\n"); | 269 | printf("PRESSURE: Got callibration data\r\n"); |
292 | return calib_data; | 270 | return calib_data; |
293 | } | 271 | } |
294 | char* callibration_pressure_data_csv(bmp085_callibration parameters) | 272 | char* callibration_pressure_data_csv(bmp085_callibration parameters) |
295 | { | 273 | { |
296 | char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1)); | 274 | char *str = chHeapAlloc(NULL,sizeof(char)*(11*11-1)); |
297 | - memset(str,0x00,sizeof(char)*(11*11-1)); | 275 | + memset(str,0x00,sizeof(char)*(11*11)); |
298 | sprintf(str + strlen(str),"%d,",parameters.AC1); | 276 | sprintf(str + strlen(str),"%d,",parameters.AC1); |
299 | sprintf(str + strlen(str),"%d,",parameters.AC2); | 277 | sprintf(str + strlen(str),"%d,",parameters.AC2); |
300 | sprintf(str + strlen(str),"%d,",parameters.AC3); | 278 | sprintf(str + strlen(str),"%d,",parameters.AC3); |
@@ -331,8 +309,10 @@ void init_pressure_temperature(void) | @@ -331,8 +309,10 @@ void init_pressure_temperature(void) | ||
331 | 309 | ||
332 | void init_humidity_temp(void) | 310 | void init_humidity_temp(void) |
333 | { | 311 | { |
312 | + printf("HUMIDITY: Initializing sensor\r\n"); | ||
334 | I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter); | 313 | I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter); |
335 | I2C_stop(I2C1); | 314 | I2C_stop(I2C1); |
315 | + //Reset I2C? | ||
336 | } | 316 | } |
337 | uint16_t get_humidity_data(void) | 317 | uint16_t get_humidity_data(void) |
338 | { | 318 | { |