SZS ez430-RF2480 1.0

ZigBee Sensor Network with synchronized time and time-stamped measurements.
rep.c
Go to the documentation of this file.
1 /*
2  * rep.c
3  *
4  * Date: 19/06/2014
5  * Author: Fernando Biazi Nascimento
6  * Copyright © 2014 Fernando Biazi Nascimento. All rights reserved.
7  *
8  * License of use and copy on file license.txt
9  *
10  */
11 
20 /*
21  * ======== Standard MSP430 includes ========
22  */
23 #include <msp430.h>
24 
25 /*
26  * ======== Includes ========
27  */
28 #include "rep.h"
29 #include "adc.h"
30 #include "uart.h"
31 #include "nwk.h"
32 #include "clock.h"
33 
34 /*
35  * ======== Constants ========
36  */
37 
38 /*
39  * ======== Macros ========
40  */
41 
42 /*
43  * ======== Types ========
44  */
45 
46 /*
47  * ======== Global Variables ========
48  */
49 
50 /*
51  * ======== Local Variables ========
52  */
53 
54 /*
55  * ======== Local Functions ========
56  */
57 
58 UInt8 rep_startFrame(UInt8 len, UInt8 reportType);
59 UInt8 rep_snd(UInt8 len, void *pBuf);
60 void rep_endFrame(UInt8 forwardToSerial);
61 UInt8 rep_sndUInt8(UInt8 v);
62 UInt8 rep_sndUInt16(UInt16 v);
63 UInt8 rep_sndUInt32(UInt32 *p);
64 UInt8 rep_sndUInt48(uint64_t *p);
66 UInt8 rep_sndUInt64(uint64_t *p);
67 UInt8 rep_sndInt64(int64_t *p);
68 
69 /*
70  * ================
71  */
72 
78 void rep_init() {
79 
80 }
81 
89 UInt16 rep_proccess() {
90  UInt16 v16 = 0;
91  clock_timeStamp time;
92 
93  if (nwk_isBound() || nwk_isSink()) {
94  if (rep_startFrame(44, 1)) {
95  rep_sndUInt64(nwk_getPIeeeAddress()); // 8 bytes
96  adc_getTemp(&v16, &time); // get temperature/timestamp
97  rep_sndUInt16(v16); // 2 bytes
98  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
99  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
100  adc_getVolt(&v16, &time); // get voltage/timestamp
101  rep_sndUInt16(v16); // 2 bytes
102  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
103  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
104  adc_getLux(&v16, &time); // get luminescence/timestamp
105  rep_sndUInt16(v16); // 2 bytes
106  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
107  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
108  rep_endFrame(TRUE); // If true, the sending command is forwarded to serial port
109  } else
110  return 5; // If no buffer space, try again in 5ms
111  }
112  return (REP_SENSORS_REPORT_PERIOD * 1000);
113 }
114 
120 void rep_hello() {
121  // delay loop, until 85 bytes are free on TX buffer.
122  while (uart_txBufFree() < 85)
123  __bis_SR_register(LPM3_bits + GIE);
124 
125  if (uart_startFrame(64, 0xE1, 2)) { // Send text message
126  uart_snd(31, "******************************\n");
127  uart_snd(16, " Sensor network\n");
128  uart_snd(17, " with timestamp.\n");
129  uart_endFrame();
130  }
131 
132  // delay loop, until 81 bytes are free on TX buffer.
133  while (uart_txBufFree() < 81)
134  __bis_SR_register(LPM3_bits + GIE);
135 
136  if (uart_startFrame(81, 0xE1, 2)) { // Send text message
137  uart_snd(9, "\n Autor:\n");
138  uart_snd(27, " Fernando Biazi Nascimento\n");
139  uart_snd(1, "\n");
140  uart_snd(13, " Orientador:\n");
141  uart_snd(31, " Prof. Dr. Paulo Batista Lopes\n");
142  uart_endFrame();
143  }
144 
145  // delay loop, until 30 bytes are free on TX buffer.
146  while (uart_txBufFree() < 30)
147  __bis_SR_register(LPM3_bits + GIE);
148 
149  if (uart_startFrame(30, 0xE1, 2)) { // Send text message
150  uart_snd(30, "******************************");
151  uart_endFrame();
152  }
153 
154  // delay loop, until 100 bytes are free on TX buffer.
155  while (uart_txBufFree() < 100)
156  __bis_SR_register(LPM3_bits + GIE);
157 }
158 
170 UInt8 rep_startFrame(UInt8 len, UInt8 reportType) {
171  if (nwk_isBound()) {
172  if (reportType == 1) // Only sensor report goes to the network
173  return nwk_startFrame(0xFFFE, NWK_CMDIDRPT, NWK_DEFRADIUS, len);
174  }
175  if (nwk_isSink()) {
176  return uart_startFrame(len, 0xE1, reportType);
177  }
178  return 0;
179 }
180 
193 UInt8 rep_snd(UInt8 len, void *pBuf) {
194  if (nwk_isBound()) {
195  return nwk_snd(len, pBuf);
196 
197  }
198  if (nwk_isSink()) {
199  return uart_snd(len, pBuf);
200  }
201  return 0;
202 }
203 
211 void rep_endFrame(UInt8 forwardToSerial) {
212  if (nwk_isBound()) {
213  nwk_endFrame(forwardToSerial);
214  } else if (nwk_isSink()) {
215  uart_endFrame();
216  }
217 }
218 
228 UInt8 rep_sndUInt8(UInt8 v) {
229  if (nwk_isBound()) {
230  return nwk_sndUInt8(v);
231  }
232  if (nwk_isSink()) {
233  return uart_sndUInt8(v);
234  }
235  return 0;
236 }
237 
247 UInt8 rep_sndUInt16(UInt16 v) {
248  if (nwk_isBound()) {
249  return nwk_sndUInt16(v);
250  }
251  if (nwk_isSink()) {
252  return uart_sndUInt16(v);
253  }
254  return 0;
255 }
256 
266 UInt8 rep_sndUInt32(UInt32 *p) {
267  if (nwk_isBound()) {
268  return nwk_sndUInt32(p);
269  }
270  if (nwk_isSink()) {
271  return uart_sndUInt32(p);
272  }
273  return 0;
274 }
275 
285 UInt8 rep_sndUInt48(uint64_t *p) {
286  if (nwk_isBound()) {
287  return nwk_sndUInt48(p);
288  }
289  if (nwk_isSink()) {
290  return uart_sndUInt48(p);
291  }
292  return 0;
293 }
294 
305  if (nwk_isBound()) {
306  if (nwk_sndUInt32(&(p->secondsLow)))
307  return nwk_sndUInt16(p->secondsHigh);
308  else
309  return 0;
310  }
311  if (nwk_isSink()) {
312  if (uart_sndUInt32(&(p->secondsLow)))
313  return uart_sndUInt16(p->secondsHigh);
314  else
315  return 0;
316  }
317  return 0;
318 }
319 
329 UInt8 rep_sndUInt64(uint64_t *p) {
330  if (nwk_isBound()) {
331  return nwk_sndUInt64(p);
332  }
333  if (nwk_isSink()) {
334  return uart_sndUInt64(p);
335  }
336  return 0;
337 }
338 
348 UInt8 rep_sndInt64(int64_t *p) {
349  if (nwk_isBound()) {
350  return nwk_sndInt64(p);
351  }
352  if (nwk_isSink()) {
353  return uart_sndInt64(p);
354  }
355  return 0;
356 }
UInt8 nwk_isSink()
Is sink.
Definition: nwk.c:1167
UInt16 rep_proccess()
Report process.
Definition: rep.c:89
Network functions interface.
uint64_t * nwk_getPIeeeAddress()
Get pointer to the IEEE address.
Definition: nwk.c:1242
UART interface.
UInt8 nwk_sndInt64(int64_t *p)
Send signed 64 bits integer.
Definition: nwk.c:1391
UInt8 uart_sndUInt8(UInt8 v)
Send UInt8.
Definition: uart.c:410
UInt8 rep_sndUInt48sc(clock_secondsCount *p)
Send seconds count value.
Definition: rep.c:304
UInt8 nwk_sndUInt64(uint64_t *p)
Send unsigned 64 bits integer.
Definition: nwk.c:1377
Seconds count.
Definition: clock.h:63
void adc_getLux(UInt16 *lux, clock_timeStamp *luxTime)
This function get current stored luminescence.
Definition: adc.c:324
UInt8 rep_snd(UInt8 len, void *pBuf)
Send.
Definition: rep.c:193
void rep_endFrame(UInt8 forwardToSerial)
End frame.
Definition: rep.c:211
UInt8 uart_txBufFree()
TX buffer free.
Definition: uart.c:293
UInt8 nwk_sndUInt48(uint64_t *p)
Send unsigned 48 bits integer.
Definition: nwk.c:1363
Time stamp.
Definition: clock.h:75
Clock interface.
UInt8 nwk_sndUInt32(UInt32 *p)
Send unsigned 32 bits integer.
Definition: nwk.c:1346
ADC10 connected sensors reading interface.
UInt8 nwk_isBound()
Is bound.
Definition: nwk.c:1179
UInt8 rep_sndUInt8(UInt8 v)
Send 8 bits unsigned integer.
Definition: rep.c:228
UInt8 rep_sndUInt64(uint64_t *p)
Send 64 bits unsigned integer.
Definition: rep.c:329
UInt8 rep_sndUInt32(UInt32 *p)
Send 32 bits unsigned integer.
Definition: rep.c:266
void rep_init()
Report initialization.
Definition: rep.c:78
UInt8 nwk_sndUInt16(UInt16 v)
Send unsigned 16 bits integer.
Definition: nwk.c:1330
UInt8 uart_startFrame(UInt8 len, UInt8 cmd0, UInt8 cmd1)
Start frame.
Definition: uart.c:140
UInt8 uart_sndUInt16(UInt16 v)
Send UInt16.
Definition: uart.c:395
UInt8 nwk_sndUInt8(UInt8 v)
Send unsigned 8 bits integer.
Definition: nwk.c:1314
UInt8 uart_sndUInt32(UInt32 *p)
Send UInt32.
Definition: uart.c:380
#define NWK_CMDIDRPT
Command id for report.
Definition: nwk.h:163
UInt8 rep_sndUInt48(uint64_t *p)
Send 48 bits unsigned integer.
Definition: rep.c:285
UInt8 uart_sndInt64(int64_t *p)
Send Int64.
Definition: uart.c:335
UInt8 uart_snd(UInt8 len, void *pBuf)
Send.
Definition: uart.c:181
#define NWK_DEFRADIUS
Default radius. Default value for maximum hops before a transmitted packet is dropped.
Definition: nwk.h:194
UInt8 rep_sndInt64(int64_t *p)
Send 64 bits integer.
Definition: rep.c:348
UInt8 rep_sndUInt16(UInt16 v)
Send 16 bits unsigned integer.
Definition: rep.c:247
void adc_getVolt(UInt16 *volt, clock_timeStamp *voltTime)
This function get current stored voltage.
Definition: adc.c:309
UInt8 nwk_startFrame(UInt16 destination, UInt16 cmdId, UInt8 radius, UInt8 len)
Start Frame.
Definition: nwk.c:1261
void rep_hello()
Hello message.
Definition: rep.c:120
void adc_getTemp(UInt16 *temp, clock_timeStamp *tempTime)
This function get current stored temperature.
Definition: adc.c:294
UInt8 nwk_snd(UInt8 len, void *pBuf)
Send data.
Definition: nwk.c:1282
void uart_endFrame()
End frame.
Definition: uart.c:164
void nwk_endFrame(UInt8 forwardToSerial)
End frame.
Definition: nwk.c:1297
UInt8 uart_sndUInt48(uint64_t *p)
Send UInt48.
Definition: uart.c:365
#define REP_SENSORS_REPORT_PERIOD
Definition: rep.h:49
UInt8 rep_startFrame(UInt8 len, UInt8 reportType)
Start frame.
Definition: rep.c:170
UInt8 uart_sndUInt64(uint64_t *p)
Send UInt64.
Definition: uart.c:350
Report interface.