2 * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3 * to the MC1322x project (http://mc1322x.devl.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 * This file is part of libmc1322x: see http://mc1322x.devl.org
33 * $Id: per.c,v 1.1 2010/06/10 14:55:39 maralvira Exp $
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 */
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 */
51 /* how long to wait between session requests */
52 #define SESSION_REQ_TIMEOUT 10000 /* phony seconds */
59 typedef uint32_t ptype_t
;
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
;
70 typedef uint32_t session_id_t
;
75 uint32_t get_time(void) {
76 static volatile int32_t cur_time
= 0;
82 #define random_short_addr() (*MACA_RANDOM & ones(sizeof(uint16_t)*8))
84 void build_session_req(volatile packet_t
*p
) {
85 static uint8_t count
= 0;
86 p
->length
= 4; p
->offset
= 0;
94 void session_req(uint16_t addr
__attribute__((unused
))) {
95 static volatile int time
= 0;
98 if((get_time() - time
) > SESSION_REQ_TIMEOUT
) {
100 if((p
= get_free_packet())) {
101 build_session_req(p
);
108 session_id_t
open_session(uint16_t addr
__attribute((unused
))) { return 0; }
112 volatile packet_t
*p
;
115 uint16_t addr
, my_addr
;
117 /* trim the reference osc. to 24MHz */
118 pack_XTAL_CNTL(CTUNE_4PF
, CTUNE
, FTUNE
, IBIAS
);
120 uart_init(INC
, MOD
, SAMP
);
126 set_power(0x0f); /* 0dbm */
127 set_channel(0); /* channel 11 */
129 /* generate a random short address */
130 my_addr
= random_short_addr();
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 );
136 print_welcome("Packet error test");
143 if((p
= rx_packet())) {
144 /* extract what we need and free the packet */
147 type
= get_packet_type((packet_t
*) p
);
148 addr
= 0; /* FIXME */
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
);
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
);
162 session_req(my_addr
);