35 #include "net/mac/tdma_mac.h"
50 #define DLEDS_ON(x) leds_on(x)
51 #define DLEDS_OFF(x) leds_off(x)
52 #define DLEDS_TOGGLE(x) leds_toggle(x)
53 #define PRINTF(...) printf(__VA_ARGS__)
57 #define DLEDS_TOGGLE(x)
63 #define SLOT_LENGTH (RTIMER_SECOND/3)
64 #define GUARD_PERIOD (RTIMER_SECOND/12)
66 #define MY_SLOT (node_id % NR_SLOTS)
67 #define PERIOD_LENGTH RTIMER_SECOND
71 uint8_t lastqueued = 0;
74 struct queuebuf* data[NUM_PACKETS];
81 static void (* receiver_callback)(
const struct mac_driver *);
82 static int id_counter = 0;
83 static int sent_counter = 0;
87 transmitter(
struct rtimer *t,
void *ptr)
90 rtimer_clock_t now, rest, period_start, slot_start;
94 rest = now % PERIOD_LENGTH;
95 period_start = now - rest;
96 slot_start = period_start + MY_SLOT*SLOT_LENGTH;
99 if(now < slot_start ||
100 now > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
101 PRINTF(
"TIMER We are outside our slot: %u != [%u,%u]\n", now, slot_start, slot_start + SLOT_LENGTH);
102 while(now > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
103 slot_start += PERIOD_LENGTH;
106 PRINTF(
"TIMER Rescheduling until %u\n", slot_start);
108 (
void (*)(
struct rtimer *,
void *))transmitter,
NULL);
110 PRINTF(
"TIMER Error #1: %d\n", r);
117 while(nextsend != freeslot) {
118 PRINTF(
"RADIO Transmitting packet #%i\n",
id[nextsend]);
119 if(!radio->
send(queuebuf_dataptr(data[nextsend]),
120 queuebuf_datalen(data[nextsend]))) {
122 PRINTF(
"RADIO Transmit OK for #%i, total=%i\n",
id[nextsend], sent_counter);
123 DLEDS_TOGGLE(LEDS_GREEN);
125 PRINTF(
"RADIO Transmit failed for #%i, total=%i\n",
id[nextsend], sent_counter);
126 DLEDS_TOGGLE(LEDS_RED);
129 nextsend = (nextsend + 1) % NUM_PACKETS;
132 if(
RTIMER_NOW() > slot_start + SLOT_LENGTH - GUARD_PERIOD) {
133 PRINTF(
"TIMER No more time to transmit\n");
139 slot_start += PERIOD_LENGTH;
140 PRINTF(
"TIMER Rescheduling until %u\n", slot_start);
142 (
void (*)(
struct rtimer *,
void *))transmitter,
NULL);
144 PRINTF(
"TIMER Error #2: %d\n", r);
157 while(lastqueued != nextsend) {
158 PRINTF(
"BUFFER Cleaning up packet #%i\n",
id[lastqueued]);
159 queuebuf_free(data[lastqueued]);
160 data[lastqueued] =
NULL;
162 lastqueued = (lastqueued + 1) % NUM_PACKETS;
165 if((freeslot + 1) % NUM_PACKETS == lastqueued) {
166 PRINTF(
"BUFFER Buffer full, dropping packet #%i\n", (id_counter+1));
171 data[freeslot] = queuebuf_new_from_packetbuf();
172 id[freeslot] = id_counter;
173 if(data[freeslot] ==
NULL) {
174 PRINTF(
"BUFFER Queuebuffer full, dropping packet #%i\n",
id[freeslot]);
177 PRINTF(
"BUFFER Wrote packet #%i to buffer \n",
id[freeslot]);
179 freeslot = (freeslot + 1) % NUM_PACKETS;
182 PRINTF(
"TIMER Starting timer\n");
184 (
void (*)(
struct rtimer *,
void *))transmitter,
NULL);
186 PRINTF(
"TIMER Error #3: %d\n", r);
198 receiver_callback(&tdma_mac_driver);
212 set_receive_function(
void (* recv)(
const struct mac_driver *))
214 receiver_callback = recv;
224 off(
int keep_radio_on)
237 for(i = 0; i < NUM_PACKETS; i++) {
242 radio->set_receive_function(
input);
245 return &tdma_mac_driver;
253 set_receive_function,