Port to the Atmega128 based CC2420DBK demonstration board.

This commit is contained in:
bg- 2007-02-02 14:07:34 +00:00
parent 429d41defd
commit b1dde63655
4 changed files with 579 additions and 0 deletions

122
platform/cc2420dbk/Makefile Normal file
View File

@ -0,0 +1,122 @@
# $Id: Makefile,v 1.1 2007/02/02 14:07:34 bg- Exp $
#
# This makefile requires GNU make!
LIB = libcontiki.a
KERNELS = gateway.ihex
PROGS = #loadable_prg.ko udpsend.ko udprecv.ko
TOOLS = tunslip scat codeprop
all: $(LIB) $(KERNELS) $(PROGS) $(TOOLS)
loadable_prg.ko: loadable_prg.o
udpsend.ko: udpsend.o
udprecv.ko: udprecv.o
test.ko: test.o
gateway.out: gateway.o $(LIB)
client.out: client.o $(LIB)
dhclient.out: dhclient.o $(LIB)
TOP=../..
INCDIRS = -I. -I$(TOP)/cpu/avr -I$(TOP)/core -I$(TOP)/apps
SRCDIRS = dev $(TOP)/cpu/avr $(TOP)/cpu/avr/dev ${addprefix $(TOP)/core/,sys dev net lib loader} $(TOP)/apps/codeprop
MCU=atmega128
ARCH=avr.o spi.o qleds.o rom.o xmem.o
SYSTEM=process.o procinit.o service.o clock.o etimer.o timer.o \
elfloader_compat.o cle.o symtab.o
UIP=uip.o uiplib.o tcpip.o uip-fw.o uip-fw-service.o uipbuf.o \
tcpdump.o psock.o dhcpc.o uaodv.o uaodv-rt.o uip-udp-packet.o
UIPDRIVERS=slip.o slip_uart1.o \
cc2420.o cc2420_spi.o cc2420_send_ip.o cc2420_send_uaodv.o
LIBS=memb.o list.o rand.o assert.o crtk.o
SYSAPPS=tcp_loader.o
LIBFILES=$(SYSTEM) $(ARCH) $(UIP) $(UIPDRIVERS) $(LIBS) $(SYSAPPS)
MKNMLIST=awk -f $(TOP)/tools/mknmlist-avr
CP=cp
LIBC=-lgcc -lc -lgcc
CC=avr-gcc
LD=avr-ld
AS=avr-as
NM=avr-nm
OBJCOPY=avr-objcopy
STRIP=avr-strip
AR=avr-ar
RANLIB=avr-ranlib
BSL=avrdude
CFLAGS=-mmcu=$(MCU) $(INCDIRS) -Wall -Os
LDFLAGS=-mmcu=$(MCU) -Wl,--section-start=.bootloader=$(BOOTLOADER_START)
# Check fuse high byte, bits BOOTSZ0 and BOOTSZ1.
BOOTLOADER_START=0x1fc00
# Setup directory search path for source files
vpath %.c $(SRCDIRS) ${filter-out CVS,${wildcard labs/*}}
# Set COM port if not already set.
# DOS: 1 => COM2, 2 => COM3, etc.
# Linux: /dev/ttyUSB0, /dev/ttyUSB1, etc.
# FreeBSD 6.2: /dev/cuaU0, /dev/cuaU1, etc.
ifndef COMPORT
COMPORT := /dev/ttyUSB0
endif
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $(notdir $(<:.c=.o))
# Combine several .o:s to one .ko and put COMMONs into the BSS segment
%.ko:
$(LD) -r -dc --strip-all --unresolved-symbols=ignore-in-object-files $^ -o $@
%.ihex: %.out
$(OBJCOPY) $^ -O ihex $@
#%.out:
# $(if $^,$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^,$(error "NO TARGET $@"))
# Add a namelist to the kernel
%.out: $^
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBC)
# : | $(MKNMLIST) > $@_tmp.c && mv $@_tmp.c $@_nm.c
# $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBC) $@_nm.c
# $(NM) $@ | $(MKNMLIST) > $@_tmp.c && mv $@_tmp.c $@_nm.c
# $(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBC) $@_nm.c
%.u: %.ihex
$(BSL) -p $(MCU) -P $(COMPORT) -c stk200 -U flash:w:$<
$(LIB): $(LIBFILES)
$(AR) rcf $@ $^
$(RANLIB) $@
codeprop: $(TOP)/tools/codeprop.c
cc -g -Wall $< -o $@
tunslip: $(TOP)/tools/tunslip.c
cc -g -Wall -DBAUDRATE="B38400" $< -o $@
scat: $(TOP)/tools/scat.c
cc -g -Wall -DBAUDRATE="B38400" $< -o $@
### TEST ###
test.out: test.o $(LIB)
$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^
depend: $(LIBFILES:.o=.c)
-$(CC) $(CCDEPFLAGS) -MM $(CFLAGS) $(INCDIRS) $^ *.c >Makefile.depend
tags: $(LIBFILES:.o=.c)
etags $^
clean:
rm -f *.o *.ko *~ *.core *.out *.ihex *_nm.c
rm -f $(LIB) $(KERNELS) $(TOOLS)
.PHONY: all
-include Makefile.depend

View File

@ -0,0 +1,170 @@
/* -*- C -*- */
/* @(#)$Id: contiki-conf.h,v 1.1 2007/02/02 14:07:34 bg- Exp $ */
#ifndef CONTIKI_CONF_H
#define CONTIKI_CONF_H
/* #define CB_GATEWAY */
#include <avr/interrupt.h>
#define CCIF
#define CLIF
/* This is still experimental. /bg */
#define UAODV_BAD_ROUTE
/* Helper prototypes that should go somewhere. */
void *sbrk(int);
typedef unsigned char spl_t;
static inline void splx_(spl_t s) { SREG = s; }
static inline spl_t splhigh_(void) { spl_t s = SREG; cli(); return s; }
void cpu_init(void);
#define splhigh() splhigh_()
#define splx(s) splx_((spl_t)s)
void clock_delay(unsigned int us2);
void clock_wait(int ms10);
void clock_set_seconds(unsigned long s);
unsigned long clock_seconds(void);
#define WITH_UIP 1
#define WITH_ASCII 1
#define PROCESS_CONF_FASTPOLL 4
/* Our clock resolution, this is the same as Unix HZ. */
/* Fix clock.c XXX/bg */
#define CLOCK_CONF_SECOND 62
/* CPU target speed in Hz */
#define F_CPU 8000000L
/* The +1 and divide by 2 is to achieve rounding. */
#define BAUD2UBR(baud) ((u16_t)((F_CPU/baud/8uL + 1)/2 - 1))
/* UART configs */
//#define MCU_MHZ 8
//#define MCU atmega128
//#define SLIP_PORT RS232_PORT_1
#define UIP_CONF_DHCP_LIGHT
#define UIP_CONF_LLH_LEN 0
#define UIP_CONF_BUFFER_SIZE 116
#define UIP_CONF_RECEIVE_WINDOW (UIP_CONF_BUFFER_SIZE - 40)
#define UIP_CONF_MAX_CONNECTIONS 4
#define UIP_CONF_MAX_LISTENPORTS 8
#define UIP_CONF_UDP_CONNS 6
#define UIP_CONF_FWCACHE_SIZE 5
#define UIP_CONF_BROADCAST 1
//#define UIP_ARCH_IPCHKSUM 1
#define UIP_CONF_UDP_CHECKSUMS 1
#define UIP_CONF_PINGADDRCONF 0
#define UIP_CONF_LOGGING 0
/*
* Definitions below are dictated by the hardware and not really
* changeable!
*/
#ifdef CB_GATEWAY
/* LED port E */
#define LEDS_CONF_GREEN _BV(2) /* PE.2 - Output */
#define LEDS_CONF_YELLOW _BV(3) /* PE.3 - Output */
#else
#define LEDS_ORANGE 8
/* LED port B */
#define LEDS_CONF_ORANGE _BV(4) /* PB.4 - Output */
#define LEDS_CONF_GREEN _BV(7) /* PB.7 - Output */
/* LED port E */
#define LEDS_CONF_RED _BV(3) /* PE.3 - Output */
#define LEDS_CONF_YELLOW _BV(4) /* PE.4 - Output */
#endif
typedef unsigned char u8_t;
typedef unsigned short u16_t;
typedef unsigned long u32_t;
typedef u16_t uip_stats_t;
typedef u16_t clock_time_t;
typedef u32_t off_t;
#define ROM_ERASE_UNIT_SIZE SPM_PAGESIZE
#define XMEM_ERASE_UNIT_SIZE 8
/* Use the first 64k of external flash for codeprop. */
#define EEPROMFS_ADDR_CODEPROP (128 * XMEM_ERASE_UNIT_SIZE)
#define CC2420_RADIO
/*
* SPI bus configuration for the CC2420DBK.
*/
/* SPI input/output registers. */
#define SPI_TXBUF SPDR
#define SPI_RXBUF SPDR
#define BV(bitno) _BV(bitno)
#define SPI_WAITFOREOTx() do { while (!(SPSR & BV(SPIF))); } while (0)
#define SPI_WAITFOREORx() do { while (!(SPSR & BV(SPIF))); } while (0)
#define SCK 1 /* - Output: SPI Serial Clock (SCLK) */
#define MOSI 2 /* - Output: SPI Master out - slave in (MOSI) */
#define MISO 3 /* - Input: SPI Master in - slave out (MISO) */
/*
* SPI bus - CC2420 pin configuration.
*/
#define FIFO_P 0 /* - Input: FIFOP from CC2420 */
#define FIFO 1 /* - Input: FIFO from CC2420 */
#define CCA 6 /* - Input: CCA from CC2420 */
#define SFD 4 /* - Input: SFD from CC2420 */
#define CSN 0 /* - Output: SPI Chip Select (CS_N) */
#define VREG_EN 5 /* - Output: VREG_EN to CC2420 */
#define RESET_N 6 /* - Output: RESET_N to CC2420 */
/* Pin status. */
#define FIFO_IS_1 (!!(PIND & BV(FIFO)))
#define CCA_IS_1 (!!(PIND & BV(CCA) ))
#define RESET_IS_1 (!!(PINB & BV(RESET_N)))
#define VREG_IS_1 (!!(PINB & BV(VREG_EN)))
#define FIFOP_IS_1 (!!(PIND & BV(FIFO_P)))
#define SFD_IS_1 (!!(PIND & BV(SFD)))
/* The CC2420 reset pin. */
#define SET_RESET_INACTIVE() ( PORTB |= BV(RESET_N) )
#define SET_RESET_ACTIVE() ( PORTB &= ~BV(RESET_N) )
/* CC2420 voltage regulator enable pin. */
#define SET_VREG_ACTIVE() ( PORTB |= BV(VREG_EN) )
#define SET_VREG_INACTIVE() ( PORTB &= ~BV(VREG_EN) )
/* CC2420 rising edge trigger for external interrupt 0 (FIFOP). */
#define FIFOP_INT_INIT() do {\
EICRA |= 0x03; \
CLEAR_FIFOP_INT(); \
} while (0)
/* FIFOP on external interrupt 0. */
#define ENABLE_FIFOP_INT() do { EIMSK |= 0x01; } while (0)
#define DISABLE_FIFOP_INT() do { EIMSK &= ~0x01; } while (0)
#define CLEAR_FIFOP_INT() do { EIFR = 0x01; } while (0)
/* Enables/disables CC2420 access to the SPI bus (not the bus).
*
* These guys should really be renamed but are compatible with the
* original Chipcon naming.
*
* SPI_CC2420_ENABLE/SPI_CC2420_DISABLE???
* CC2420_ENABLE_SPI/CC2420_DISABLE_SPI???
*/
#define SPI_ENABLE() ( PORTB &= ~BV(CSN) ) /* ENABLE CSn (active low) */
#define SPI_DISABLE() ( PORTB |= BV(CSN) ) /* DISABLE CSn (active low) */
#endif /* CONTIKI_CONF_H */

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2007, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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: qleds.c,v 1.1 2007/02/02 14:07:34 bg- Exp $
*/
#include "contiki.h"
#include "dev/leds.h"
#include <avr/io.h>
void
leds_init(void)
{
#ifdef CB_GATEWAY
DDRE |= LEDS_CONF_GREEN | LEDS_CONF_YELLOW;
PORTE |= LEDS_CONF_GREEN | LEDS_CONF_YELLOW; /* LEDS off */
#else
DDRB |= LEDS_CONF_ORANGE | LEDS_CONF_GREEN;
DDRE |= LEDS_CONF_RED | LEDS_CONF_YELLOW;
PORTB &= ~(LEDS_CONF_ORANGE | LEDS_CONF_GREEN);
PORTE &= ~(LEDS_CONF_RED | LEDS_CONF_YELLOW);
#endif
}
void
leds_on(unsigned char leds)
{
#ifdef CB_GATEWAY
if (leds & LEDS_GREEN)
PORTE &= ~LEDS_CONF_GREEN;
if (leds & LEDS_YELLOW)
PORTE &= ~LEDS_CONF_YELLOW;
#else
if (leds & LEDS_ORANGE)
PORTB |= LEDS_CONF_ORANGE;
if (leds & LEDS_GREEN)
PORTB |= LEDS_CONF_GREEN;
if (leds & LEDS_RED)
PORTE |= LEDS_CONF_RED;
if (leds & LEDS_YELLOW)
PORTE |= LEDS_CONF_YELLOW;
#endif
}
void
leds_off(unsigned char leds)
{
#ifdef CB_GATEWAY
if (leds & LEDS_GREEN)
PORTE |= LEDS_CONF_GREEN;
if (leds & LEDS_YELLOW)
PORTE |= LEDS_CONF_YELLOW;
#else
if (leds & LEDS_ORANGE)
PORTB &= ~LEDS_CONF_ORANGE;
if (leds & LEDS_GREEN)
PORTB &= ~LEDS_CONF_GREEN;
if (leds & LEDS_RED)
PORTE &= ~LEDS_CONF_RED;
if (leds & LEDS_YELLOW)
PORTE &= ~LEDS_CONF_YELLOW;
#endif
}
void
leds_toggle(unsigned char leds)
{
/*
* Synonym: void leds_invert(unsigned char leds);
*/
asm(".global leds_invert\nleds_invert:\n");
#ifdef CB_GATEWAY
if (leds & LEDS_GREEN)
PORTE ^= ~LEDS_CONF_GREEN;
if (leds & LEDS_YELLOW)
PORTE ^= ~LEDS_CONF_YELLOW;
#else
if (leds & LEDS_ORANGE)
PORTB ^= LEDS_CONF_ORANGE;
if (leds & LEDS_GREEN)
PORTB ^= LEDS_CONF_GREEN;
if (leds & LEDS_RED)
PORTE ^= LEDS_CONF_RED;
if (leds & LEDS_YELLOW)
PORTE ^= LEDS_CONF_YELLOW;
#endif
}

View File

@ -0,0 +1,168 @@
/*
* Copyright (c) 2007, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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: gateway.c,v 1.1 2007/02/02 14:07:34 bg- Exp $
*/
/*
* Example gateway configuration with two IP interfaces, one SLIP over
* USB and one over 802.11.4 radio.
*
* The IP address is hardcoded to 172.16.0.1 and the 172.16/16 network
* is on the radio interface.
*
* The default route is over the SLIP interface.
*
* On the SLIP host run a standard SLIP implementation or the tunslip
* program that can also view debug printfs, example:
*
* sliphost# ./tunslip 172.16.0.1 255.255.0.0
*
*
* This kernel has no application code but new modules can be uploaded
* using the codeprop program, example:
*
* datan$ ./codeprop 172.16.0.1 loadable_prg.ko
* File successfully sent (1116 bytes)
* Reply: ok
*
*/
#include <stdio.h>
#include <avr/eeprom.h>
#include "contiki.h"
/* Also IP output. */
#include "net/uip-fw-service.h"
#include "net/uaodv.h"
#include "dev/slip.h"
#include "dev/cc2420.h"
#include "dev/leds.h"
#include "dev/xmem.h"
#include "codeprop/codeprop.h"
/* We have two IP interfaces. */
struct uip_fw_netif cc2420if =
{UIP_FW_NETIF(172,16,0,1, 255,255,0,0, cc2420_send_uaodv)};
static struct uip_fw_netif slipif =
{UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
/* Radio stuff in network byte order. */
static u16_t panId = HTONS(0x2024);
#ifndef RF_CHANNEL
#define RF_CHANNEL 15
#endif
unsigned char ds2411_id[8] = { 0x00, 0x12, 0x4b, 0x00, 0x00, 0x00, 0x09, 0x04};
static void
ds2411_init(void)
{
u8_t i, t;
eeprom_read_block(ds2411_id, 0, 8);
for (i = 0; i < 4; i++) {
t = ds2411_id[i];
ds2411_id[i] = ds2411_id[7 - i];
ds2411_id[7 - i] = t;
}
}
int
main(int argc, char **argv)
{
/*
* Initalize hardware.
*/
cpu_init();
clock_init();
leds_init();
leds_toggle(LEDS_ALL);
slip_arch_init(BAUD2UBR(38400)); /* Must come before first printf */
printf("Starting %s "
"($Id: gateway.c,v 1.1 2007/02/02 14:07:34 bg- Exp $)\n", __FILE__);
ds2411_init();
cc2420_init();
xmem_init();
clock_wait(CLOCK_SECOND/10);
leds_toggle(LEDS_ALL);
/*
* Hardware initialization done!
*/
printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
ds2411_id[0], ds2411_id[1], ds2411_id[2], ds2411_id[3],
ds2411_id[4], ds2411_id[5], ds2411_id[6], ds2411_id[7]);
uip_ipaddr_copy(&uip_hostaddr, &cc2420if.ipaddr);
uip_ipaddr_copy(&uip_netmask, &cc2420if.netmask);
printf("IP %d.%d.%d.%d netmask %d.%d.%d.%d\n",
uip_ipaddr_to_quad(&uip_hostaddr),
uip_ipaddr_to_quad(&uip_netmask));
cc2420_set_chan_pan_addr(RF_CHANNEL, panId, uip_hostaddr.u16[1], ds2411_id);
/*
* Initialize Contiki and our processes.
*/
process_init();
process_start(&etimer_process, NULL);
/* Configure IP stack. */
uip_init();
uip_fw_default(&slipif); /* Point2point, no default router. */
uip_fw_register(&cc2420if);
tcpip_set_forwarding(1);
/* Start IP stack. */
process_start(&tcpip_process, NULL);
process_start(&uip_fw_process, NULL); /* Start IP output */
process_start(&slip_process, NULL);
process_start(&cc2420_process, NULL);
cc2420_on();
process_start(&uaodv_process, NULL);
//process_start(&tcp_loader_process, NULL);
/*
* This is the scheduler loop.
*/
printf("process_run()...\n");
while (1) {
do {
/* Reset watchdog. */
} while(process_run() > 0);
/* Idle! */
}
return 0;
}