simple tdma rtimer implemetation
This commit is contained in:
parent
19b3035e36
commit
aa99049af0
|
@ -28,169 +28,185 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: tdma_mac.c,v 1.1 2007/08/31 13:42:22 fros4943 Exp $
|
* $Id: tdma_mac.c,v 1.2 2007/09/18 10:37:17 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "contiki.h"
|
#include "contiki.h"
|
||||||
#include "net/mac/tdma_mac.h"
|
#include "net/mac/tdma_mac.h"
|
||||||
#include "net/rime/rimebuf.h"
|
#include "net/rime/rimebuf.h"
|
||||||
#include "net/uip-fw.h"
|
#include "net/uip-fw.h"
|
||||||
|
#include "sys/rtimer.h"
|
||||||
|
#include "net/rime.h"
|
||||||
#include "lib/memb.h"
|
#include "lib/memb.h"
|
||||||
#include "lib/list.h"
|
#include "lib/list.h"
|
||||||
|
#include "dev/leds.h"
|
||||||
#include "node-id.h"
|
#include "node-id.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
static const struct radio_driver *radio;
|
|
||||||
static void (* receiver_callback)(const struct mac_driver *);
|
|
||||||
static int id_counter = 0;
|
|
||||||
|
|
||||||
#define DEBUG 1
|
#define DEBUG 1
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
/*#include "printf2log.h"*/ /* XXX COOJA specifics */
|
#define DLEDS_ON(x) leds_on(x)
|
||||||
|
#define DLEDS_OFF(x) leds_off(x)
|
||||||
|
#define DLEDS_TOGGLE(x) leds_toggle(x)
|
||||||
#define PRINTF(...) printf(__VA_ARGS__)
|
#define PRINTF(...) printf(__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
|
#define DLEDS_ON(x)
|
||||||
|
#define DLEDS_OFF(x)
|
||||||
|
#define DLEDS_TOGGLE(x)
|
||||||
#define PRINTF(...)
|
#define PRINTF(...)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* TDMA configuration */
|
/* TDMA configuration */
|
||||||
#define MAX_BUFFERED_PACKETS 6
|
#define NR_SLOTS 3
|
||||||
|
#define SLOT_LENGTH (RTIMER_SECOND/3)
|
||||||
#define NR_SLOTS 10
|
#define GUARD_PERIOD (RTIMER_SECOND/12)
|
||||||
#define SLOT_LENGTH (CLOCK_SECOND) /* Slot length */
|
|
||||||
#define GUARD_PERIOD (CLOCK_SECOND / 2) /* Approx. packet transmission time */
|
|
||||||
|
|
||||||
#define MY_SLOT (node_id % NR_SLOTS)
|
#define MY_SLOT (node_id % NR_SLOTS)
|
||||||
#define PERIOD_LENGTH (SLOT_LENGTH*NR_SLOTS)
|
#define PERIOD_LENGTH RTIMER_SECOND
|
||||||
|
|
||||||
enum {
|
/* Buffers */
|
||||||
EVENT_PACKET_BUFFERED
|
#define NUM_PACKETS 8
|
||||||
};
|
u8_t lastqueued = 0;
|
||||||
|
u8_t nextsend = 0;
|
||||||
|
u8_t freeslot = 0;
|
||||||
|
struct queuebuf* data[NUM_PACKETS];
|
||||||
|
int id[NUM_PACKETS];
|
||||||
|
|
||||||
struct buf_packet {
|
static struct rtimer rtimer;
|
||||||
struct buf_packet *next;
|
u8_t timer_on = 0;
|
||||||
struct queuebuf *queued_packet;
|
|
||||||
int id;
|
|
||||||
struct etimer etimer;
|
|
||||||
};
|
|
||||||
|
|
||||||
LIST(buf_packet_list);
|
static const struct radio_driver *radio;
|
||||||
MEMB(buf_packet_mem, struct buf_packet, MAX_BUFFERED_PACKETS);
|
static void (* receiver_callback)(const struct mac_driver *);
|
||||||
|
static int id_counter = 0;
|
||||||
PROCESS(tdma_process, "TDMA process");
|
static int sent_counter = 0;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
PROCESS_THREAD(tdma_process, ev, data)
|
static char
|
||||||
|
transmitter(struct rtimer *t, void *ptr)
|
||||||
{
|
{
|
||||||
PROCESS_BEGIN();
|
int r;
|
||||||
|
rtimer_clock_t now, rest, period_start, slot_start;
|
||||||
|
|
||||||
while(1) {
|
/* Calculate slot start time */
|
||||||
PROCESS_YIELD();
|
now = RTIMER_NOW();
|
||||||
|
rest = now % PERIOD_LENGTH;
|
||||||
|
period_start = now - rest;
|
||||||
|
slot_start = period_start + MY_SLOT*SLOT_LENGTH;
|
||||||
|
|
||||||
if(ev == EVENT_PACKET_BUFFERED || ev == PROCESS_EVENT_TIMER) {
|
/* Check if we are inside our slot */
|
||||||
struct buf_packet *packet;
|
if (now < slot_start ||
|
||||||
|
now > slot_start + SLOT_LENGTH - GUARD_PERIOD)
|
||||||
|
{
|
||||||
|
PRINTF("TIMER We are outside our slot: %u != [%u,%u]\n", now, slot_start, slot_start + SLOT_LENGTH);
|
||||||
|
while (now > slot_start + SLOT_LENGTH - GUARD_PERIOD)
|
||||||
|
{
|
||||||
|
slot_start += PERIOD_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
/* Locate packet */
|
PRINTF("TIMER Rescheduling until %u\n", slot_start);
|
||||||
for(packet = list_head(buf_packet_list); packet != NULL; packet = packet->next) {
|
r = rtimer_set(&rtimer, slot_start, 1,
|
||||||
if (etimer_expired(&packet->etimer)) {
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
||||||
int ret, rest, period_start, my_next_slot;
|
if(r)
|
||||||
clock_time_t now;
|
{
|
||||||
|
PRINTF("TIMER Error #1: %d\n", r);
|
||||||
|
}
|
||||||
|
|
||||||
/* Calculate time of our next slot */
|
return 1;
|
||||||
now = clock_time();
|
}
|
||||||
rest = now % PERIOD_LENGTH;
|
|
||||||
period_start = now - rest;
|
|
||||||
my_next_slot = period_start + MY_SLOT*SLOT_LENGTH;
|
|
||||||
|
|
||||||
/* Transmit if inside our slot, with enough time left */
|
/* Transmit queued packets */
|
||||||
if ((now == my_next_slot) ||
|
while (nextsend != freeslot)
|
||||||
(now > my_next_slot && (now - my_next_slot) < (SLOT_LENGTH-GUARD_PERIOD)))
|
{
|
||||||
{
|
PRINTF("RADIO Transmitting packet #%i\n", id[nextsend]);
|
||||||
PRINTF("SCHEDULE We can deliver right now (%i), slot = (%i <-> %i)\n", now, my_next_slot, my_next_slot + SLOT_LENGTH);
|
if(!radio->send(
|
||||||
}
|
queuebuf_dataptr(data[nextsend]),
|
||||||
else
|
queuebuf_datalen(data[nextsend])))
|
||||||
{
|
{
|
||||||
if (now > my_next_slot) {
|
sent_counter++;
|
||||||
my_next_slot += PERIOD_LENGTH;
|
PRINTF("RADIO Transmit OK for #%i, total=%i\n", id[nextsend], sent_counter);
|
||||||
}
|
DLEDS_TOGGLE(LEDS_GREEN);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PRINTF("RADIO Transmit failed for #%i, total=%i\n", id[nextsend], sent_counter);
|
||||||
|
DLEDS_TOGGLE(LEDS_RED);
|
||||||
|
}
|
||||||
|
|
||||||
int distance = my_next_slot - now;
|
nextsend = (nextsend + 1) % NUM_PACKETS;
|
||||||
etimer_set(&packet->etimer, distance);
|
|
||||||
PRINTF("SCHEDULE Packet #%i rescheduled at %i to %i\n", packet->id, clock_time(), my_next_slot);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Clear Rime buffer */
|
/* Recalculate new slot */
|
||||||
rimebuf_clear();
|
if (RTIMER_NOW() > slot_start + SLOT_LENGTH - GUARD_PERIOD)
|
||||||
|
{
|
||||||
/* Restore packet from buffer */
|
PRINTF("TIMER No more time to transmit\n");
|
||||||
if(packet->queued_packet != NULL) {
|
break;
|
||||||
queuebuf_to_rimebuf(packet->queued_packet);
|
|
||||||
queuebuf_free(packet->queued_packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Send packet */
|
|
||||||
PRINTF("TDMA_PROC Packet #%i send starting at %i\n", packet->id, clock_time());
|
|
||||||
ret = radio->send(rimebuf_hdrptr(), rimebuf_totlen());
|
|
||||||
|
|
||||||
|
|
||||||
if (ret == UIP_FW_OK) {
|
|
||||||
/* Remove transmitted packet */
|
|
||||||
list_remove(buf_packet_list, packet);
|
|
||||||
memb_free(&buf_packet_mem, packet);
|
|
||||||
PRINTF("BUFFER Packet removed, memory freed\n");
|
|
||||||
PRINTF("TDMA_PROC Packet #%i sent at %i\n", packet->id, clock_time());
|
|
||||||
} else {
|
|
||||||
/* Reschedule packet */
|
|
||||||
etimer_set(&packet->etimer, SLOT_LENGTH);
|
|
||||||
PRINTF("TDMA_PROC Packet #%i forced rescheduled\n", packet->id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PROCESS_END();
|
|
||||||
|
/* Calculate time of our next slot */
|
||||||
|
slot_start += PERIOD_LENGTH;
|
||||||
|
PRINTF("TIMER Rescheduling until %u\n", slot_start);
|
||||||
|
r = rtimer_set(&rtimer, slot_start, 1,
|
||||||
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
||||||
|
if(r)
|
||||||
|
{
|
||||||
|
PRINTF("TIMER Error #2: %d\n", r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static int
|
static int
|
||||||
send(void)
|
send(void)
|
||||||
{
|
{
|
||||||
struct buf_packet *packet;
|
int r;
|
||||||
|
id_counter++;
|
||||||
|
|
||||||
packet = (struct buf_packet *)memb_alloc(&buf_packet_mem);
|
/* Clean up already sent packets */
|
||||||
if (packet == NULL) {
|
while (lastqueued != nextsend)
|
||||||
PRINTF("BUFFER No memory available, packet dropped\n");
|
{
|
||||||
|
PRINTF("BUFFER Cleaning up packet #%i\n", id[lastqueued]);
|
||||||
|
queuebuf_free(data[lastqueued]);
|
||||||
|
data[lastqueued] = NULL;
|
||||||
|
|
||||||
|
lastqueued = (lastqueued + 1) % NUM_PACKETS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((freeslot + 1) % NUM_PACKETS == lastqueued)
|
||||||
|
{
|
||||||
|
PRINTF("BUFFER Buffer full, dropping packet #%i\n", (id_counter+1));
|
||||||
return UIP_FW_DROPPED;
|
return UIP_FW_DROPPED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add to buffered packets list */
|
/* Allocate queue buf for packet */
|
||||||
list_add(buf_packet_list, packet);
|
data[freeslot] = queuebuf_new_from_rimebuf();
|
||||||
PRINTF("BUFFER Packet added, memory allocated\n");
|
id[freeslot] = id_counter;
|
||||||
|
if (data[freeslot] == NULL)
|
||||||
/* Store packet data */
|
{
|
||||||
packet->queued_packet = queuebuf_new_from_rimebuf();
|
PRINTF("BUFFER Queuebuffer full, dropping packet #%i\n", id[freeslot]);
|
||||||
if (packet->queued_packet == NULL) {
|
|
||||||
list_remove(buf_packet_list, packet);
|
|
||||||
memb_free(&buf_packet_mem, packet);
|
|
||||||
PRINTF("BUFFER No queue memory available, packet dropped\n");
|
|
||||||
return UIP_FW_DROPPED;
|
return UIP_FW_DROPPED;
|
||||||
}
|
}
|
||||||
|
PRINTF("BUFFER Wrote packet #%i to buffer \n", id[freeslot]);
|
||||||
|
|
||||||
PROCESS_CONTEXT_BEGIN(&tdma_process);
|
freeslot = (freeslot + 1) % NUM_PACKETS;
|
||||||
etimer_set(&packet->etimer, 0);
|
|
||||||
PROCESS_CONTEXT_END(&tdma_process);
|
|
||||||
|
|
||||||
/* TODO ID only used for debugging */
|
if (!timer_on)
|
||||||
packet->id = id_counter++;
|
{
|
||||||
|
PRINTF("TIMER Starting timer\n");
|
||||||
/* Clear Rime buffer */
|
r = rtimer_set(&rtimer, RTIMER_NOW() + RTIMER_SECOND, 1,
|
||||||
rimebuf_clear();
|
(void (*)(struct rtimer *, void *))transmitter, NULL);
|
||||||
|
if(r)
|
||||||
process_post(&tdma_process, EVENT_PACKET_BUFFERED, NULL);
|
{
|
||||||
|
PRINTF("TIMER Error #3: %d\n", r);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
timer_on = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return UIP_FW_OK; /* TODO Return what? */
|
return UIP_FW_OK; /* TODO Return what? */
|
||||||
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
|
@ -230,9 +246,11 @@ off(void)
|
||||||
void
|
void
|
||||||
tdma_mac_init(const struct radio_driver *d)
|
tdma_mac_init(const struct radio_driver *d)
|
||||||
{
|
{
|
||||||
memb_init(&buf_packet_mem);
|
int i;
|
||||||
list_init(buf_packet_list);
|
for (i=0; i < NUM_PACKETS; i++)
|
||||||
process_start(&tdma_process, NULL);
|
{
|
||||||
|
data[i] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
radio = d;
|
radio = d;
|
||||||
radio->set_receive_function(input);
|
radio->set_receive_function(input);
|
||||||
|
|
Loading…
Reference in a new issue