Contiki 2.5
per.c
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of libmc1322x: see http://mc1322x.devl.org
31  * for details.
32  *
33  *
34  */
35 
36 #include <mc1322x.h>
37 #include <board.h>
38 #include <stdio.h>
39 
40 #include "tests.h"
41 #include "config.h"
42 
43 /* This program communicates with itself and determines the packet */
44 /* error rate (PER) under a variety of powers and packet sizes */
45 /* Each test the packets are sent and received as fast as possible */
46 
47 /* The program first scans on channel 11 and attempts to open a test */
48 /* session with a node. After opening a session, the nodes begin the */
49 /* test sequence */
50 
51 /* how long to wait between session requests */
52 #define SESSION_REQ_TIMEOUT 10000 /* phony seconds */
53 
54 enum STATES {
55  SCANNING,
56  MAX_STATE
57 };
58 
59 typedef uint32_t ptype_t;
60 enum PACKET_TYPE {
61  PACKET_SESS_REQ,
62  MAX_PACKET_TYPE
63 };
64 /* get protocol level packet type */
65 /* this is not 802.15.4 packet type */
66 ptype_t get_packet_type(packet_t * p __attribute__((unused))) {
67  return MAX_PACKET_TYPE;
68 }
69 
70 typedef uint32_t session_id_t;
71 
72 
73 
74 /* phony get_time */
75 uint32_t get_time(void) {
76  static volatile int32_t cur_time = 0;
77  cur_time++;
78  return cur_time;
79 }
80 
81 
82 #define random_short_addr() (*MACA_RANDOM & ones(sizeof(uint16_t)*8))
83 
84 void build_session_req(volatile packet_t *p) {
85  static uint8_t count = 0;
86  p->length = 4; p->offset = 0;
87  p->data[0] = 0xff;
88  p->data[1] = 0x01;
89  p->data[2] = 0x02;
90  p->data[3] = count++;
91  return;
92 }
93 
94 void session_req(uint16_t addr __attribute__((unused))) {
95  static volatile int time = 0;
96  volatile packet_t *p;
97 
98  if((get_time() - time) > SESSION_REQ_TIMEOUT) {
99  time = get_time();
100  if((p = get_free_packet())) {
101  build_session_req(p);
102  tx_packet(p);
103  }
104  }
105  return;
106 }
107 
108 session_id_t open_session(uint16_t addr __attribute((unused))) { return 0; }
109 
110 void main(void) {
111  uint32_t state;
112  volatile packet_t *p;
113  session_id_t sesid;
114  ptype_t type;
115  uint16_t addr, my_addr;
116 
117  /* trim the reference osc. to 24MHz */
118  pack_XTAL_CNTL(CTUNE_4PF, CTUNE, FTUNE, IBIAS);
119 
120  uart_init(INC, MOD, SAMP);
121 
122  vreg_init();
123 
124  maca_init();
125 
126  set_power(0x0f); /* 0dbm */
127  set_channel(0); /* channel 11 */
128 
129  /* generate a random short address */
130  my_addr = random_short_addr();
131 
132  /* sets up tx_on, should be a board specific item */
133  *GPIO_FUNC_SEL2 = (0x01 << ((44-16*2)*2));
134  gpio_pad_dir_set( 1ULL << 44 );
135 
136  print_welcome("Packet error test");
137 
138  state = SCANNING;
139  while(1) {
140 
141  switch(state) {
142  case SCANNING:
143  if((p = rx_packet())) {
144  /* extract what we need and free the packet */
145  printf("Recv: ");
146  print_packet(p);
147  type = get_packet_type((packet_t *) p);
148  addr = 0; /* FIXME */
149  free_packet(p);
150  /* pick a new address if someone else is using ours */
151  if(addr == my_addr) {
152  my_addr = random_short_addr();
153  printf("DUP addr received, changing to new addr 0x%x02\n\r",my_addr);
154  }
155  /* if we have a packet */
156  /* check if it's a session request beacon */
157  if(type == PACKET_SESS_REQ) {
158  /* try to start a session */
159  sesid = open_session(addr);
160  }
161  } else {
162  session_req(my_addr);
163  }
164  break;
165  default:
166  break;
167  }
168 
169 
170  }
171 
172 }
173