SZS 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 "adc.h" // ADC routines
82 #include "uart.h" // Clock routines
83 #include "led.h" // Led control routines
84 #include "but.h" // Button control routines
85 #include "rep.h" // Report control routines
86 #include "im.h" // Interruption management
87 /*
88  * ======== Doxygen ========
89  */
98 /*
99  * ======== macros ========
100  */
101 
102 // Task schedule
103 #define MAIN_TASK_SCH_SPI main_taskSchedule[0]// SPI
104 #define MAIN_TASK_SCH_CCAI main_taskSchedule[1]// CC2480
105 #define MAIN_TASK_SCH_NWK main_taskSchedule[2]// Network
106 #define MAIN_TASK_SCH_ADC main_taskSchedule[3]// ADC
107 #define MAIN_TASK_SCH_UART main_taskSchedule[4]// UART
108 #define MAIN_TASK_SCH_LED main_taskSchedule[5]// Led
109 #define MAIN_TASK_SCH_BUT main_taskSchedule[6]// Button
110 #define MAIN_TASK_SCH_REP main_taskSchedule[7]// Report
111 #define MAIN_TASK_SCH_WDT main_taskSchedule[8]// Watch Dog
112 
115 #define MAIN_TASK_COUNT 9
116 
120 #define MAIN_FLAG_WDT_BIT 1
121 
122 /*
123  * ======== variables ========
124  */
125 
130 
142 void main() {
143  UInt16 main_runCounter = 0;
144  Uint8 main_flags = 0;
145  UInt16 i;
146 
147  Grace_init(); // Activate Grace-generated configuration
148 
149 // >>>>> Fill-in user code here <<<<<
150 
151  // Check if WDT+ caused the prior reset condition
152  if (IFG1 & WDTIFG) {
153  // WDT+ initiated the prior reset condition
154  IFG1 &= ~WDTIFG; // Software clear WDTIFG
155  main_flags |= MAIN_FLAG_WDT_BIT;
156  }
157  //WDTCTL = WDTPW | WDTHOLD; // turn off Watch dog (for testing)
158 
159  clock_init();
160  spi_init();
161  ccai_init();
162  nwk_init();
163  adc_init();
164  uart_init();
165  led_init();
166  but_init();
167  rep_init();
168 
169  // Report the reset
170  if (uart_startFrame(1, 0xE1, 0)) {
171  if (main_flags & MAIN_FLAG_WDT_BIT) {
172  uart_snd(1, "\x01"); // Reset by WDT+
173  } else {
174  uart_snd(1, "\x00"); // Normal initialization
175  }
176  uart_endFrame();
177  }
178 
179  // Hello message
180  rep_hello();
181 
182  // If all tasks run at the same 1ms cycle, they may take more than 1ms to finish, and then
183  // the next cycle will be delayed in units of ms. A better practice would be define delays
184  // from one task to another here, for example main_taskSchedule[i] = 0x0001 << i; but
185  // defining all tasks to start at the same time can be a good test to the tolerance of the
186  // system to delays on the execution of his tasks.
187  i = MAIN_TASK_COUNT;
188  while (i) {
189  i--;
190  main_taskSchedule[i] = 0;
191  }
192 
193  // Delay report task 200ms on first run
194  MAIN_TASK_SCH_REP= 200;
195 
196  // Main loop
197  for (;;) {
198 
199  // SPI routines
200  if (MAIN_TASK_SCH_SPI== main_runCounter)
201  MAIN_TASK_SCH_SPI = main_runCounter + spi_proccess();
202 
203  // CC2480 routines
204  if (MAIN_TASK_SCH_CCAI == main_runCounter)
205  MAIN_TASK_SCH_CCAI = main_runCounter + ccai_proccess();
206 
207  // Network routines
208  if (MAIN_TASK_SCH_NWK == main_runCounter)
209  MAIN_TASK_SCH_NWK = main_runCounter + nwk_process();
210 
211  // ADC routines
212  if (MAIN_TASK_SCH_ADC == main_runCounter)
213  MAIN_TASK_SCH_ADC = main_runCounter + adc_proccess();
214 
215  // UART routines
216  if (MAIN_TASK_SCH_UART == main_runCounter)
217  MAIN_TASK_SCH_UART = main_runCounter + uart_proccess();
218 
219  // Led routines
220  if (MAIN_TASK_SCH_LED == main_runCounter)
221  MAIN_TASK_SCH_LED = main_runCounter + led_proccess();
222 
223  // Button routines
224  if (MAIN_TASK_SCH_BUT == main_runCounter)
225  MAIN_TASK_SCH_BUT = main_runCounter + but_proccess();
226 
227  // Report status
228  if (MAIN_TASK_SCH_REP == main_runCounter)
229  MAIN_TASK_SCH_REP = main_runCounter + rep_proccess();
230 
231  // Watch dog routines
232  if (MAIN_TASK_SCH_WDT == main_runCounter) {
233  MAIN_TASK_SCH_WDT = main_runCounter + 2000;
234  // Say "I'm here!" to watch dog.
235  WDTCTL = WDTPW | WDTCNTCL | WDTSSEL;// Set WDT+ counter Clear and SourceSel bits (instruction clears other bits)
236  }
237 
238  // Put device on LPM and wait next time to run main loop
239  __bis_SR_register(LPM3_bits + GIE);
240 
241  // Increment main_runCounter
242  main_runCounter++;
243  }
244 }
245 
255 UInt8 TMRB0_ISR() {
256  Bool result = 0;
257  static Uint8 main_timerCount = 0;
258 
259  __disable_interrupt();
260 
261  // run on each timer event
262  clock_tick();
263 
264  main_timerCount++;
265 
266  if (!(main_timerCount & 0x03)) { // each 4 timer events (~1ms)
267  result = 1; // Exit LPM by returning 1
268  }
269 
270  __enable_interrupt();
271 
272  return result;
273 }
274 
UInt16 rep_proccess()
Report process.
Definition: rep.c:89
Network functions interface.
UART interface.
#define MAIN_TASK_COUNT
Definition: main.c:115
UInt8 TMRB0_ISR()
Tiner B0 ISR.
Definition: main.c:255
Interruption management (enable/disable) interface.
void clock_tick()
Clock tick.
Definition: clock.c:171
void adc_init()
This function initializes the ADC variables.
Definition: adc.c:171
UInt16 uart_proccess()
UART process.
Definition: uart.c:122
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.
void main()
Main function of the program.
Definition: main.c:142
void rep_init()
Report initialization.
Definition: rep.c:78
void clock_init()
Clock Initialization.
Definition: clock.c:148
SPI interface.
UInt8 uart_startFrame(UInt8 len, UInt8 cmd0, UInt8 cmd1)
Start frame.
Definition: uart.c:140
UInt16 main_taskSchedule[MAIN_TASK_COUNT]
Task schedule for each task.
Definition: main.c:129
void nwk_init()
Network initialization.
Definition: nwk.c:276
UInt16 nwk_process()
Network process.
Definition: nwk.c:310
#define MAIN_FLAG_WDT_BIT
Definition: main.c:120
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:181
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:120
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:105
void uart_endFrame()
End frame.
Definition: uart.c:164
void spi_init()
SPI initialization.
Definition: spi.c:287
Button interface.
UInt16 adc_proccess()
This function processes the tasks of the ADC.
Definition: adc.c:218
Report interface.