-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_init.c
159 lines (127 loc) · 4.84 KB
/
mac_init.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
154
155
156
157
158
159
/* Copyright (c) 2008 ATMEL Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
$Id: mac.c,v 1.2 2009/05/28 19:45:34 bleverett Exp $
*/
// Includes
//..
#include <stdlib.h>
#include <stddef.h>
#include "hal.h"
#include "mac.h"
#include "data_types.h"
#include "radio.h"
#include "mac_scan.h"
#include "timer.h"
#include "mac_associate.h"
#include "EE_prom.h"
#include "mac_data.h"
// Globals
#if DEBUG
/// Debugging temporary string, only used if DEBUG is non-zero
char debugStr[100];
char dbg_buff[40];
#endif
/// MAC config struct, contains state info for the MAC
macConfig_t macConfig;
// Macros & Defines
/**
A global buffer to hold frames. This area is used by both
send an receive functions to hold radio frame data. Sometimes the
first byte is the frame length byte, and sometimes the first byte
is the first byte of the frame.
*/
u8 mac_buffer_tx[sizeof(rx_frame_t)]; ///< Global MAC buffer (transmit)
u8 mac_buffer_rx[sizeof(rx_frame_t)]; ///< Global MAC buffer (receive)
// Implementation
/**
Init the mac, which includes initializing the radio chip.
param: Channel Sets the channel to use for the MAC. Use 0xff for
non-coordinator nodes.
*/
void macInit(u8 Channel) // this gets called from multiple places to init/reinit the MAC and radio
{
macConfig.currentChannel = Channel;
macConfig.panId = BROADCASTPANID;
macConfig.shortAddress = BROADCASTADDR;
macConfig.associated = false;
macConfig.parentShortAddress = BROADCASTADDR;
macConfig.lastRoute = BROADCASTADDR;
macConfig.hopsToCoord = 0;
macConfig.busy = 0;
// TODO: This should be under control of the COORD node. Currently COORD can only turn off sleeping, but not on
// Current consumption of a sleeping node is ~4.8ma, Non sleeping rx node ~18ma and transmitting node about 20ma
macConfig.sleeping = NODE_SLEEP;
// Setup the address of this device by reading the stored address from eeprom.
halGetMacAddr((u8*)&macConfig.longAddr);
radioSetup();
# if (NODE_TYPE == COORD)
// Initialize the array of nodes (coordinator only)
macInitNodes();
#endif
#if (DEMO)
debugMsgStr("\r\nMAC & Radio Init in DEMO mode - reduced TX-output, reduced RX-sens, RSSI only for association ");
#endif
}
/**
Set the radio's operating channel, and saves that channel to the
MAC's global data structure.
param: channel The channel number to use for radio communication.
*/
void
macSetOperatingChannel(u8 channel)
{
// Set the channel
macConfig.currentChannel = channel;
radioSetOperatingChannel(channel);
}
#if (NODE_TYPE == COORD)
/**
This function will cause a device to become a PAN
Coordinator. This function must be run by app code on at least one
device in the network.
@ingroup mac
*/
void macStartCoord(void)
{
// Local vars.
u16 panid;
// If PanID is not set yet the create a random one -- Creates a new network
// and all the children have to bind again
if((panid = EE_GetPanID()) == BROADCASTPANID)
{
panid = radioRandom(8);
panid <<= 8;
panid |= radioRandom(8);
EE_SetPanID(panid); // save it
}
radioSetPanId(panid);
macConfig.panId = panid;
radioSetShortAddress(DEFAULT_COORD_ADDR); // Coordinator is always 0000
macConfig.shortAddress = DEFAULT_COORD_ADDR;
}
#endif
/** @} */