SZSPTP ez430-RF2480 1.0

ZigBee Sensor Network with synchronized time and time-stamped measurements.
main.c
Go to the documentation of this file.
1 /*
2  * main.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  * The contents of this file can also be found at:
10  * http://www.biazi.eng.br/mack/Mestrado/2014/SZS/license.txt
11  *
12  * Using Template: Code Composer Studio 5.3.0.00090 grace empty project
13  *
14  */
15 
61 /*
62  * ======== Standard MSP430 includes ========
63  */
64 #include <msp430.h>
65 
66 /*
67  * ======== Grace related includes ========
68  */
69 #include <ti/mcu/msp430/Grace.h>
70 
71 /*
72  * ======== user includes ========
73  */
74 
75 #include <xdc/std.h>
76 
77 #include "clock.h" // Clock routines
78 #include "spi.h" // SPI routines
79 #include "cc2480ai.h" // CC2480 routines
80 #include "nwk.h" // Network routines
81 #include "ptp.h" // PTP routines
82 #include "adc.h" // ADC routines
83 #include "uart.h" // Clock routines
84 #include "led.h" // Led control routines
85 #include "but.h" // Button control routines
86 #include "rep.h" // Report control routines
87 #include "im.h" // Interruption management
88 /*
89  * ======== Doxygen ========
90  */
99 /*
100  * ======== macros ========
101  */
102 
103 // Task schedules
104 #define MAIN_TASK_SCH_SPI main_taskSchedule[0]
105 #define MAIN_TASK_SCH_CCAI main_taskSchedule[1]
106 #define MAIN_TASK_SCH_NWK main_taskSchedule[2]
107 #define MAIN_TASK_SCH_PTPDR main_taskSchedule[3]
108 #define MAIN_TASK_SCH_PTP main_taskSchedule[4]
109 #define MAIN_TASK_SCH_ADC main_taskSchedule[5]
110 #define MAIN_TASK_SCH_UART main_taskSchedule[6]
111 #define MAIN_TASK_SCH_LED main_taskSchedule[7]
112 #define MAIN_TASK_SCH_BUT main_taskSchedule[8]
113 #define MAIN_TASK_SCH_REP main_taskSchedule[9]
114 #define MAIN_TASK_SCH_WDT main_taskSchedule[10]
115 
118 #define MAIN_TASK_COUNT 11
119 
123 #define MAIN_FLAG_WDT_BIT 1
124 
125 /*
126  * ======== variables ========
127  */
128 
133 
145 void main() {
146  UInt16 main_runCounter = 0;
147  Uint8 main_flags = 0;
148  UInt16 i;
149 
150  Grace_init(); // Activate Grace-generated configuration
151 
152 // >>>>> Fill-in user code here <<<<<
153 
154  // Check if WDT+ caused the prior reset condition
155  if (IFG1 & WDTIFG) {
156  // WDT+ initiated the prior reset condition
157  IFG1 &= ~WDTIFG; // Software clear WDTIFG
158  main_flags |= MAIN_FLAG_WDT_BIT;
159  }
160  //WDTCTL = WDTPW | WDTHOLD; // turn off Watch dog (for testing)
161 
162  clock_init();
163  spi_init();
164  ccai_init();
165  nwk_init();
166  ptp_init(); // PTP initialization is done after network is running by a call to ptp_initEnd.
167  adc_init();
168  uart_init();
169  led_init();
170  but_init();
171  rep_init();
172 
173  // Report the reset
174  if (uart_startFrame(1, 0xE1, 0)) {
175  if (main_flags & MAIN_FLAG_WDT_BIT) {
176  uart_snd(1, "\x01"); // Reset by WDT+
177  } else {
178  uart_snd(1, "\x00"); // Normal initialization
179  }
180  uart_endFrame();
181  }
182 
183  // Hello message
184  rep_hello();
185 
186  // If all tasks run at the same 1ms cycle, they may take more than 1ms to finish, and then
187  // the next cycle will be delayed in units of ms. A better practice would be define delays
188  // from one task to another here, for example main_taskSchedule[i] = 0x0001 << i; but
189  // defining all tasks to start at the same time can be a good test to the tolerance of the
190  // system to delays on the execution of his tasks.
191  i = MAIN_TASK_COUNT;
192  while (i) {
193  i--;
194  main_taskSchedule[i] = 0;
195  }
196 
197  // Delay report task 200ms on first run
198  MAIN_TASK_SCH_REP= 200;
199 
200  // Main loop
201  for (;;) {
202 
203  // SPI routines
204  if (MAIN_TASK_SCH_SPI== main_runCounter)
205  MAIN_TASK_SCH_SPI = main_runCounter + spi_proccess();
206 
207  // CC2480 routines
208  if (MAIN_TASK_SCH_CCAI == main_runCounter)
209  MAIN_TASK_SCH_CCAI = main_runCounter + ccai_proccess();
210 
211  // Network routines
212  if (MAIN_TASK_SCH_NWK == main_runCounter)
213  MAIN_TASK_SCH_NWK = main_runCounter + nwk_process();
214 
215  // PTP routines
216  if (MAIN_TASK_SCH_PTPDR == main_runCounter)
217  MAIN_TASK_SCH_PTPDR = main_runCounter + ptp_processPDelayReq();
218  if (MAIN_TASK_SCH_PTP == main_runCounter)
219  MAIN_TASK_SCH_PTP = main_runCounter + ptp_process();
220 
221  // ADC routines
222  if (MAIN_TASK_SCH_ADC == main_runCounter)
223  MAIN_TASK_SCH_ADC = main_runCounter + adc_proccess();
224 
225  // UART routines
226  if (MAIN_TASK_SCH_UART == main_runCounter)
227  MAIN_TASK_SCH_UART = main_runCounter + uart_proccess();
228 
229  // Led routines
230  if (MAIN_TASK_SCH_LED == main_runCounter)
231  MAIN_TASK_SCH_LED = main_runCounter + led_proccess();
232 
233  // Button routines
234  if (MAIN_TASK_SCH_BUT == main_runCounter)
235  MAIN_TASK_SCH_BUT = main_runCounter + but_proccess();
236 
237  // Report status
238  if (MAIN_TASK_SCH_REP == main_runCounter)
239  MAIN_TASK_SCH_REP = main_runCounter + rep_proccess();
240 
241  // Watch dog routines
242  if (MAIN_TASK_SCH_WDT == main_runCounter) {
243  MAIN_TASK_SCH_WDT = main_runCounter + 2000;
244  // Say "I'm here!" to watch dog.
245  WDTCTL = WDTPW | WDTCNTCL | WDTSSEL;// Set WDT+ counter Clear and SourceSel bits (instruction clears other bits)
246  }
247 
248  // Put device on LPM and wait next time to run main loop
249  __bis_SR_register(LPM3_bits + GIE);
250 
251  // Increment main_runCounter
252  main_runCounter++;
253  }
254 }
255 
265 UInt8 TMRB0_ISR() {
266  Bool result = 0;
267  static Uint8 main_timerCount = 0;
268 
269  __disable_interrupt();
270 
271  // run on each timer event
272  clock_tick();
273 
274  main_timerCount++;
275 
276  if (!(main_timerCount & 0x03)) { // each 4 timer events (~1ms)
277  result = 1; // Exit LPM by returning 1
278  }
279 
280  __enable_interrupt();
281 
282  return result;
283 }
284 
UInt16 rep_proccess()
Report process.
Definition: rep.c:90
Network functions interface.
UART interface.
#define MAIN_TASK_COUNT
Definition: main.c:118
#define MAIN_TASK_SCH_PTPDR
PTP delay request check task schedule.
Definition: main.c:107
UInt8 TMRB0_ISR()
Tiner B0 ISR.
Definition: main.c:265
Interruption management (enable/disable) interface.
void clock_tick()
Clock tick.
Definition: clock.c:171
#define MAIN_TASK_SCH_UART
UART task schedule.
Definition: main.c:110
void adc_init()
This function initializes the ADC variables.
Definition: adc.c:171
UInt16 uart_proccess()
UART process.
Definition: uart.c:123
UInt16 ptp_process()
PTP process.
Definition: ptp.c:145
void ptp_init()
PTP initialization.
Definition: ptp.c:95
UInt16 led_proccess()
This function processes the tasks of the LED, it controls the red LED.
Definition: led.c:158
Clock interface.
ADC10 connected sensors reading interface.
#define MAIN_TASK_SCH_PTP
PTP task schedule.
Definition: main.c:108
void main()
Main function of the program.
Definition: main.c:145
void rep_init()
Report initialization.
Definition: rep.c:79
void clock_init()
Clock Initialization.
Definition: clock.c:148
PTP interface.
SPI interface.
UInt8 uart_startFrame(UInt8 len, UInt8 cmd0, UInt8 cmd1)
Start frame.
Definition: uart.c:141
UInt16 main_taskSchedule[MAIN_TASK_COUNT]
Task schedule for each task.
Definition: main.c:132
#define MAIN_TASK_SCH_NWK
Network task schedule.
Definition: main.c:106
void nwk_init()
Network initialization.
Definition: nwk.c:277
#define MAIN_TASK_SCH_ADC
ADC task schedule.
Definition: main.c:109
UInt16 ptp_processPDelayReq()
Process PDelayReq.
Definition: ptp.c:241
#define MAIN_TASK_SCH_LED
Led task schedule.
Definition: main.c:111
#define MAIN_TASK_SCH_CCAI
CC2480 task schedule.
Definition: main.c:105
#define MAIN_TASK_SCH_REP
Report task schedule.
Definition: main.c:113
#define MAIN_TASK_SCH_BUT
Button task schedule.
Definition: main.c:112
UInt16 nwk_process()
Network process.
Definition: nwk.c:311
#define MAIN_FLAG_WDT_BIT
Definition: main.c:123
void led_init()
LED Initialization.
Definition: led.c:141
UInt16 spi_proccess()
SPI proccess.
Definition: spi.c:325
UInt8 uart_snd(UInt8 len, void *pBuf)
Send.
Definition: uart.c:182
#define MAIN_TASK_SCH_SPI
SPI task schedule.
Definition: main.c:104
CC2480 interface.
UInt16 but_proccess()
This function processes the tasks of the button control.
Definition: but.c:132
void rep_hello()
Hello message.
Definition: rep.c:139
void ccai_init()
This function initializes the CC2480 Application Interface variables.
Definition: cc2480ai.c:557
Led interface.
void but_init()
This function initializes the button control variables.
Definition: but.c:108
UInt16 ccai_proccess()
This function processes the tasks of the CC2480 Application Interface.
Definition: cc2480ai.c:573
void uart_init()
UART initialization.
Definition: uart.c:106
void uart_endFrame()
End frame.
Definition: uart.c:165
void spi_init()
SPI initialization.
Definition: spi.c:287
#define MAIN_TASK_SCH_WDT
Watch Dog task schedule.
Definition: main.c:114
Button interface.
UInt16 adc_proccess()
This function processes the tasks of the ADC.
Definition: adc.c:218
Report interface.