-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
153 lines (127 loc) · 3.49 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* \file main.c
* \brief Main control code and interrupt definitions for sensor node
*
* Initializes both the MSP430 controller and the attached CC110l radio.
* After completing the initialization process the controller goes into LPM3 and waits.
*/
#include <msp430.h> // Base header files
#include <stdint.h> // pull in standard datatypes
#include "pinDefs.h" // Pin defines
#include "CC110l.h" // Literals for helping with the radio
#include "MSP_Init.h" // Code to set initial board state
#include "Radio.h"
#include "SPI_Library.h" // SPI control for the radio
#include "sleep_timer.h" // Sleep code for controller
#include "states.h" // Enum for the possible machine states.
#include "Clustering.h" //Clustering
#include "Wait_For_Start.h"
#include "SPI_Pins.h"
#include "RandomGen.h"
#include "States.h"
#include "Message_Prep.h"
#include "CH_TDMA_Assign.h"
#include "TDMA_Assign.h"
#include "CH_Sense.h"
#include "ND_Sense.h"
// Globals
volatile uint16_t Timer_Rollover_Count = 0; // check if leave uint16_t
volatile uint8_t Break_Sleep = False;
uint8_t _MyAddress = 0x05; // user defined address for each node
uint8_t _ChildCount;
uint8_t _ChildList[20];
uint8_t __MyHead;
Machine_State __State = Waiting_For_Start;
uint32_t curr_time = 0;
uint32_t start = 0;
int32_t GT_Adjust = 0;
uint32_t tdma_sleep_cycles = 0;
/**
* \brief Main control sequence for sensor node
* @return Constant 0, but it has nowhere to go.
*/
int main(void) {
Board_Init();
Timer_Init();
SPI_Init(); // Start SPI
Radio_Init(); // Prep the radio
Radio_Sleep();
LPM3;
SPI_Send(ADDR, _MyAddress); // Lode the nodes address into the radio for packet filtering
SPI_Send(PKTCTRL1, 0b00001111); // Enable address filtering with 0xFF and 0x00 as broadcast
P1DIR |= LED1; // Set LED to output
while (True) {
if (__State == Waiting_For_Start)
__State = Wait_For_Start();
if (__State == CH_TDMA_Assignment) {
__State = CH_TDMA_Assign(_MyAddress, _ChildList, _ChildCount);
}
if (__State == CH_Sensing) {
__State = CH_Sense(_MyAddress, _ChildList, _ChildCount);
}
if (__State == Clustering) {
__State = Cluster_Choose();
}
if (__State == TDMA_Assignment) {
__State = TDMA_Assign(&tdma_sleep_cycles, __MyHead);
}
if (__State == Sensing) {
__State = ND_Sense(tdma_sleep_cycles, _MyAddress, __MyHead);
}
if (__State == CH_Sensing) {
__State = CH_Sense(_MyAddress, _ChildList, _ChildCount);
}
if (__State == CH_Associate) {
__State = CH_Cluster_Associate();
}
if (__State == Associate) {
__State = Child_Associate();
}
if (__State == broken) {// Should never hit this step, infinite loop for debugging purposes
while (1)
;
}
}
}
/**
* \brief Interrupt service routine for slow timer
*/
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TimerA_0_ISR(void)
{
switch (TA0IV)
{
case (TA0IV_TAIFG):
Timer_Rollover_Count++;
break;
case (TA0IV_TACCR1):
LPM3_EXIT; // Wake up if CCR1 is hit, used for sleeping function
break;
case (TA0IV_TACCR2):
break;
default:
break;
}
}
/**
* \brief Interrupt service routine for packet received pin from CC100l
*/
#pragma vector=PORT1_VECTOR
__interrupt void MSP_RX_ISR (void)
{
if (__State == Sensing)
{
curr_time = Get_Time();
GT_Adjust = curr_time - start - GT;
if (GT_Adjust > (int32_t)tdma_sleep_cycles)
GT_Adjust = GT;
Break_Sleep = True;
LPM3_EXIT; // Wake up on interrupt
}
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC_ISR (void)
{
ADC10CTL0 &= ~ADC10IFG; // Clear flag
LPM0_EXIT;
}