SZSPTP 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 "ptp.h"
31 #include "uart.h"
32 #include "nwk.h"
33 #include "clock.h"
34 
35 /*
36  * ======== Constants ========
37  */
38 
39 /*
40  * ======== Macros ========
41  */
42 
43 /*
44  * ======== Types ========
45  */
46 
47 /*
48  * ======== Global Variables ========
49  */
50 
51 /*
52  * ======== Local Variables ========
53  */
54 
55 /*
56  * ======== Local Functions ========
57  */
58 
59 UInt8 rep_startFrame(UInt8 len, UInt8 reportType);
60 UInt8 rep_snd(UInt8 len, void *pBuf);
61 void rep_endFrame(UInt8 forwardToSerial);
62 UInt8 rep_sndUInt8(UInt8 v);
63 UInt8 rep_sndUInt16(UInt16 v);
64 UInt8 rep_sndUInt32(UInt32 *p);
65 UInt8 rep_sndUInt48(uint64_t *p);
67 UInt8 rep_sndUInt64(uint64_t *p);
68 UInt8 rep_sndInt64(int64_t *p);
69 
70 /*
71  * ================
72  */
73 
79 void rep_init() {
80 
81 }
82 
90 UInt16 rep_proccess() {
91  UInt16 v16 = 0;
92  clock_timeStamp time;
93  UInt8 portIndex;
94 
95  if (nwk_isBound() || nwk_isSink()) {
96  if (rep_startFrame(76, 1)) {
97  rep_sndUInt64(nwk_getPIeeeAddress()); // 8 bytes
98  adc_getTemp(&v16, &time); // get temperature/timestamp
99  rep_sndUInt16(v16); // 2 bytes
100  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
101  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
102  adc_getVolt(&v16, &time); // get voltage/timestamp
103  rep_sndUInt16(v16); // 2 bytes
104  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
105  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
106  adc_getLux(&v16, &time); // get luminescence/timestamp
107  rep_sndUInt16(v16); // 2 bytes
108  rep_sndUInt48sc(&(time.seconds)); // 6 bytes
109  rep_sndUInt32(&(time.nanoSeconds)); // 4 bytes
110  // clock adjusting information:
111  portIndex = ptpClock.defaultDS.numberPorts;
112  while (portIndex) {
113  portIndex--;
114  if (ptpClock.parentDS.parentPortIdentity.clockIdentity
115  == &(ptp_foreignMaster[portIndex][0].clockIdentity)) {
116  rep_sndUInt16(nwk_neighbors[portIndex].foreignAddress);
117  portIndex = 0;
118  } else if (!portIndex) {
119  rep_snd(2, "\xFF\xFF");
120  }
121  } // 2 bytes
122  rep_sndUInt64(&ptp_meanDelayAtLastAdjust); // 8 bytes
123  rep_sndInt64(&ptp_lastAdjust); // 8 bytes
124  rep_sndUInt48sc((void*) &(ptp_lastAdjustTime.secondsField)); // 6 bytes
125  rep_sndUInt32(&(ptp_lastAdjustTime.nanoSecondsField)); // 4 bytes
126  rep_sndUInt32(&clock_tickSize); // 4 bytes
127  rep_endFrame(TRUE); // If true, the sending command is forwarded to serial port
128  } else
129  return 5; // If no buffer space, try again in 5ms
130  }
131  return (REP_SENSORS_REPORT_PERIOD * 1000);
132 }
133 
139 void rep_hello() {
140  // delay loop, until 85 bytes are free on TX buffer.
141  while (uart_txBufFree() < 85)
142  __bis_SR_register(LPM3_bits + GIE);
143 
144  if (uart_startFrame(85, 0xE1, 2)) { // Send text message
145  uart_snd(31, "******************************\n");
146  uart_snd(16, " Sensor network\n");
147  uart_snd(29, " with timestamp synchronized\n");
148  uart_snd(9, " by PTP.\n");
149  uart_endFrame();
150  }
151 
152  // delay loop, until 81 bytes are free on TX buffer.
153  while (uart_txBufFree() < 81)
154  __bis_SR_register(LPM3_bits + GIE);
155 
156  if (uart_startFrame(81, 0xE1, 2)) { // Send text message
157  uart_snd(9, "\n Autor:\n");
158  uart_snd(27, " Fernando Biazi Nascimento\n");
159  uart_snd(1, "\n");
160  uart_snd(13, " Orientador:\n");
161  uart_snd(31, " Prof. Dr. Paulo Batista Lopes\n");
162  uart_endFrame();
163  }
164 
165  // delay loop, until 30 bytes are free on TX buffer.
166  while (uart_txBufFree() < 30)
167  __bis_SR_register(LPM3_bits + GIE);
168 
169  if (uart_startFrame(30, 0xE1, 2)) { // Send text message
170  uart_snd(30, "******************************");
171  uart_endFrame();
172  }
173 
174  // delay loop, until 100 bytes are free on TX buffer.
175  while (uart_txBufFree() < 100)
176  __bis_SR_register(LPM3_bits + GIE);
177 }
178 
190 UInt8 rep_startFrame(UInt8 len, UInt8 reportType) {
191  if (nwk_isBound()) {
192  if (reportType == 1) // Only sensor report goes to the network
193  return nwk_startFrame(0xFFFE, NWK_CMDIDRPT, NWK_DEFRADIUS, len);
194  }
195  if (nwk_isSink()) {
196  return uart_startFrame(len, 0xE1, reportType);
197  }
198  return 0;
199 }
200 
213 UInt8 rep_snd(UInt8 len, void *pBuf) {
214  if (nwk_isBound()) {
215  return nwk_snd(len, pBuf);
216 
217  }
218  if (nwk_isSink()) {
219  return uart_snd(len, pBuf);
220  }
221  return 0;
222 }
223 
231 void rep_endFrame(UInt8 forwardToSerial) {
232  if (nwk_isBound()) {
233  nwk_endFrame(forwardToSerial);
234  } else if (nwk_isSink()) {
235  uart_endFrame();
236  }
237 }
238 
248 UInt8 rep_sndUInt8(UInt8 v) {
249  if (nwk_isBound()) {
250  return nwk_sndUInt8(v);
251  }
252  if (nwk_isSink()) {
253  return uart_sndUInt8(v);
254  }
255  return 0;
256 }
257 
267 UInt8 rep_sndUInt16(UInt16 v) {
268  if (nwk_isBound()) {
269  return nwk_sndUInt16(v);
270  }
271  if (nwk_isSink()) {
272  return uart_sndUInt16(v);
273  }
274  return 0;
275 }
276 
286 UInt8 rep_sndUInt32(UInt32 *p) {
287  if (nwk_isBound()) {
288  return nwk_sndUInt32(p);
289  }
290  if (nwk_isSink()) {
291  return uart_sndUInt32(p);
292  }
293  return 0;
294 }
295 
305 UInt8 rep_sndUInt48(uint64_t *p) {
306  if (nwk_isBound()) {
307  return nwk_sndUInt48(p);
308  }
309  if (nwk_isSink()) {
310  return uart_sndUInt48(p);
311  }
312  return 0;
313 }
314 
325  if (nwk_isBound()) {
326  if (nwk_sndUInt32(&(p->secondsLow)))
327  return nwk_sndUInt16(p->secondsHigh);
328  else
329  return 0;
330  }
331  if (nwk_isSink()) {
332  if (uart_sndUInt32(&(p->secondsLow)))
333  return uart_sndUInt16(p->secondsHigh);
334  else
335  return 0;
336  }
337  return 0;
338 }
339 
349 UInt8 rep_sndUInt64(uint64_t *p) {
350  if (nwk_isBound()) {
351  return nwk_sndUInt64(p);
352  }
353  if (nwk_isSink()) {
354  return uart_sndUInt64(p);
355  }
356  return 0;
357 }
358 
368 UInt8 rep_sndInt64(int64_t *p) {
369  if (nwk_isBound()) {
370  return nwk_sndInt64(p);
371  }
372  if (nwk_isSink()) {
373  return uart_sndInt64(p);
374  }
375  return 0;
376 }
UInt8 nwk_isSink()
Is sink.
Definition: nwk.c:1170
UInt16 rep_proccess()
Report process.
Definition: rep.c:90
Network functions interface.
uint64_t * nwk_getPIeeeAddress()
Get pointer to the IEEE address.
Definition: nwk.c:1245
UART interface.
UInt8 nwk_sndInt64(int64_t *p)
Send signed 64 bits integer.
Definition: nwk.c:1394
UInt8 uart_sndUInt8(UInt8 v)
Send UInt8.
Definition: uart.c:411
UInt8 rep_sndUInt48sc(clock_secondsCount *p)
Send seconds count value.
Definition: rep.c:324
UInt8 nwk_sndUInt64(uint64_t *p)
Send unsigned 64 bits integer.
Definition: nwk.c:1380
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:213
void rep_endFrame(UInt8 forwardToSerial)
End frame.
Definition: rep.c:231
UInt8 uart_txBufFree()
TX buffer free.
Definition: uart.c:294
UInt8 nwk_sndUInt48(uint64_t *p)
Send unsigned 48 bits integer.
Definition: nwk.c:1366
UInt32 clock_tickSize
Tick size (ns)
Definition: clock.c:102
Time stamp.
Definition: clock.h:75
Clock interface.
nwk_neighbor nwk_neighbors[NWK_NEIGHBORMAX]
Neighbors information.
Definition: nwk.c:159
UInt8 nwk_sndUInt32(UInt32 *p)
Send unsigned 32 bits integer.
Definition: nwk.c:1349
ADC10 connected sensors reading interface.
UInt8 nwk_isBound()
Is bound.
Definition: nwk.c:1182
UInt8 rep_sndUInt8(UInt8 v)
Send 8 bits unsigned integer.
Definition: rep.c:248
UInt8 rep_sndUInt64(uint64_t *p)
Send 64 bits unsigned integer.
Definition: rep.c:349
UInt8 rep_sndUInt32(UInt32 *p)
Send 32 bits unsigned integer.
Definition: rep.c:286
void rep_init()
Report initialization.
Definition: rep.c:79
UInt8 nwk_sndUInt16(UInt16 v)
Send unsigned 16 bits integer.
Definition: nwk.c:1333
PTP interface.
UInt8 uart_startFrame(UInt8 len, UInt8 cmd0, UInt8 cmd1)
Start frame.
Definition: uart.c:141
UInt8 uart_sndUInt16(UInt16 v)
Send UInt16.
Definition: uart.c:396
UInt8 nwk_sndUInt8(UInt8 v)
Send unsigned 8 bits integer.
Definition: nwk.c:1317
UInt8 uart_sndUInt32(UInt32 *p)
Send UInt32.
Definition: uart.c:381
#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:305
UInt8 uart_sndInt64(int64_t *p)
Send Int64.
Definition: uart.c:336
UInt8 uart_snd(UInt8 len, void *pBuf)
Send.
Definition: uart.c:182
#define NWK_DEFRADIUS
Default radius. Default value for maximum hops before a transmitted packet is dropped.
Definition: nwk.h:200
UInt8 rep_sndInt64(int64_t *p)
Send 64 bits integer.
Definition: rep.c:368
UInt8 rep_sndUInt16(UInt16 v)
Send 16 bits unsigned integer.
Definition: rep.c:267
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:1264
void rep_hello()
Hello message.
Definition: rep.c:139
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:1285
void uart_endFrame()
End frame.
Definition: uart.c:165
void nwk_endFrame(UInt8 forwardToSerial)
End frame.
Definition: nwk.c:1300
UInt8 uart_sndUInt48(uint64_t *p)
Send UInt48.
Definition: uart.c:366
#define REP_SENSORS_REPORT_PERIOD
Definition: rep.h:49
UInt8 rep_startFrame(UInt8 len, UInt8 reportType)
Start frame.
Definition: rep.c:190
UInt8 uart_sndUInt64(uint64_t *p)
Send UInt64.
Definition: uart.c:351
Report interface.