Contiki 2.6

per.c

00001 /*
00002  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
00003  * to the MC1322x project (http://mc1322x.devl.org)
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the Institute nor the names of its contributors
00015  *    may be used to endorse or promote products derived from this software
00016  *    without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * This file is part of libmc1322x: see http://mc1322x.devl.org
00031  * for details. 
00032  *
00033  *
00034  */
00035 
00036 #include <mc1322x.h>
00037 #include <board.h>
00038 #include <stdio.h>
00039 
00040 #include "tests.h"
00041 #include "config.h"
00042 
00043 /* This program communicates with itself and determines the packet */
00044 /* error rate (PER) under a variety of powers and packet sizes  */
00045 /* Each test the packets are sent and received as fast as possible */
00046 
00047 /* The program first scans on channel 11 and attempts to open a test */
00048 /* session with a node. After opening a session, the nodes begin the */
00049 /* test sequence  */
00050 
00051 /* how long to wait between session requests */
00052 #define SESSION_REQ_TIMEOUT 10000     /* phony seconds */
00053 
00054 enum STATES {
00055         SCANNING,
00056         MAX_STATE
00057 };
00058 
00059 typedef uint32_t ptype_t;
00060 enum PACKET_TYPE {
00061         PACKET_SESS_REQ,
00062         MAX_PACKET_TYPE
00063 };
00064 /* get protocol level packet type   */
00065 /* this is not 802.15.4 packet type */
00066 ptype_t get_packet_type(packet_t * p __attribute__((unused))) {
00067         return MAX_PACKET_TYPE;
00068 }
00069 
00070 typedef uint32_t session_id_t;
00071 
00072 
00073 
00074 /* phony get_time */
00075 uint32_t get_time(void) {
00076         static volatile int32_t cur_time = 0;
00077         cur_time++;
00078         return cur_time;
00079 }
00080 
00081 
00082 #define random_short_addr() (*MACA_RANDOM & ones(sizeof(uint16_t)*8))
00083 
00084 void build_session_req(volatile packet_t *p) {
00085         static uint8_t count = 0;
00086         p->length = 4; p->offset = 0;
00087         p->data[0] = 0xff;
00088         p->data[1] = 0x01;
00089         p->data[2] = 0x02;
00090         p->data[3] = count++;
00091         return;
00092 }
00093 
00094 void session_req(uint16_t addr __attribute__((unused))) {       
00095         static volatile int time = 0;
00096         volatile packet_t *p;
00097 
00098         if((get_time() - time) > SESSION_REQ_TIMEOUT) {
00099                 time = get_time();
00100                 if((p = get_free_packet())) {
00101                         build_session_req(p);
00102                         tx_packet(p);
00103                 }
00104         }
00105         return; 
00106 }
00107 
00108 session_id_t open_session(uint16_t addr __attribute((unused))) { return 0; }
00109 
00110 void main(void) {
00111         uint32_t state;
00112         volatile packet_t *p;
00113         session_id_t sesid;
00114         ptype_t type;
00115         uint16_t addr, my_addr;
00116 
00117         /* trim the reference osc. to 24MHz */
00118         pack_XTAL_CNTL(CTUNE_4PF, CTUNE, FTUNE, IBIAS);
00119         
00120         uart_init(INC, MOD, SAMP);
00121 
00122         vreg_init();
00123 
00124         maca_init();
00125         
00126         set_power(0x0f); /* 0dbm */
00127         set_channel(0); /* channel 11 */
00128 
00129         /* generate a random short address */
00130         my_addr = random_short_addr();
00131 
00132         /* sets up tx_on, should be a board specific item */
00133         *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2));
00134         gpio_pad_dir_set( 1ULL << 44 );
00135 
00136         print_welcome("Packet error test");
00137 
00138         state = SCANNING;
00139         while(1) { 
00140                 
00141                 switch(state) {
00142                 case SCANNING:
00143                         if((p = rx_packet())) {
00144                                 /* extract what we need and free the packet */
00145                                 printf("Recv: ");
00146                                 print_packet(p);
00147                                 type = get_packet_type((packet_t *) p);
00148                                 addr = 0; /* FIXME */
00149                                 free_packet(p);
00150                                 /* pick a new address if someone else is using ours */
00151                                 if(addr == my_addr) {
00152                                         my_addr = random_short_addr();
00153                                         printf("DUP addr received, changing to new addr 0x%x02\n\r",my_addr);
00154                                 }
00155                                 /* if we have a packet */
00156                                 /* check if it's a session request beacon */
00157                                 if(type == PACKET_SESS_REQ) {
00158                                         /* try to start a session */
00159                                         sesid = open_session(addr);
00160                                 }
00161                         }  else {
00162                                 session_req(my_addr);
00163                         }
00164                         break;
00165                 default:
00166                         break;
00167                 }
00168                                         
00169 
00170         }
00171 
00172 }
00173