Contiki 2.5
autoack-tx.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 #define LED LED_RED
44 
45 /* 802.15.4 PSDU is 127 MAX */
46 /* 2 bytes are the FCS */
47 /* therefore 125 is the max payload length */
48 #define PAYLOAD_LEN 16
49 #define DELAY 100000
50 
51 void fill_packet(volatile packet_t *p) {
52  static volatile uint8_t count=0;
53 
54  p->length = 16;
55  p->offset = 0;
56  p->data[0] = 0x71; /* 0b 10 01 10 000 1 1 0 0 001 data, ack request, short addr */
57  p->data[1] = 0x98; /* 0b 10 01 10 000 1 1 0 0 001 data, ack request, short addr */
58  p->data[2] = count++; /* dsn */
59  p->data[3] = 0xaa; /* pan */
60  p->data[4] = 0xaa;
61  p->data[5] = 0x11; /* dest. short addr. */
62  p->data[6] = 0x11;
63  p->data[7] = 0x22; /* src. short addr. */
64  p->data[8] = 0x22;
65 
66  /* payload */
67  p->data[9] = 'a';
68  p->data[10] = 'c';
69  p->data[11] = 'k';
70  p->data[12] = 't';
71  p->data[13] = 'e';
72  p->data[14] = 's';
73  p->data[15] = 't';
74 
75 }
76 
77 void maca_tx_callback(volatile packet_t *p) {
78  switch(p->status) {
79  case 0:
80  printf("TX OK\n\r");
81  break;
82  case 3:
83  printf("CRC ERR\n\r");
84  break;
85  case 5:
86  printf("NO ACK\n\r");
87  break;
88  default:
89  printf("unknown status: %d\n", (int)p->status);
90  }
91 }
92 
93 void main(void) {
94  volatile packet_t *p;
95  char c;
96  uint16_t r=30; /* start reception 100us before ack should arrive */
97  uint16_t end=180; /* 750 us receive window*/
98 
99  /* trim the reference osc. to 24MHz */
100  trim_xtal();
101 
102  uart_init(INC, MOD, SAMP);
103 
104  vreg_init();
105 
106  maca_init();
107 
108  set_channel(0); /* channel 11 */
109 // set_power(0x0f); /* 0xf = -1dbm, see 3-22 */
110 // set_power(0x11); /* 0x11 = 3dbm, see 3-22 */
111  set_power(0x12); /* 0x12 is the highest, not documented */
112 
113  /* sets up tx_on, should be a board specific item */
114  GPIO->FUNC_SEL_44 = 1;
115  GPIO->PAD_DIR_SET_44 = 1;
116 
117  GPIO->FUNC_SEL_45 = 2;
118  GPIO->PAD_DIR_SET_45 = 1;
119 
120  *MACA_RXACKDELAY = r;
121 
122  printf("rx warmup: %d\n\r", (int)(*MACA_WARMUP & 0xfff));
123 
124  *MACA_RXEND = end;
125 
126  printf("rx end: %d\n\r", (int)(*MACA_RXEND & 0xfff));
127 
128  set_prm_mode(AUTOACK);
129 
130  print_welcome("rftest-tx");
131 
132  while(1) {
133 
134  /* call check_maca() periodically --- this works around */
135  /* a few lockup conditions */
136  check_maca();
137 
138  while((p = rx_packet())) {
139  if(p) {
140  printf("RX: ");
141  print_packet(p);
142  free_packet(p);
143  }
144  }
145 
146  if(uart1_can_get()) {
147  c = uart1_getc();
148 
149  switch(c) {
150  case 'z':
151  r++;
152  if(r > 4095) { r = 0; }
153  *MACA_RXACKDELAY = r;
154  printf("rx ack delay: %d\n\r", r);
155  break;
156  case 'x':
157  if(r == 0) { r = 4095; } else { r--; }
158  *MACA_RXACKDELAY = r;
159  printf("rx ack delay: %d\n\r", r);
160  break;
161  case 'q':
162  end++;
163  if(r > 4095) { r = 0; }
164  *MACA_RXEND = end;
165  printf("rx end: %d\n\r", end);
166  break;
167  case 'w':
168  end--;
169  if(r == 0) { r = 4095; } else { r--; }
170  *MACA_RXEND = end;
171  printf("rx end: %d\n\r", end);
172  break;
173  default:
174  p = get_free_packet();
175  if(p) {
176  fill_packet(p);
177 
178  printf("autoack-tx --- ");
179  print_packet(p);
180 
181  tx_packet(p);
182  }
183  break;
184  }
185  }
186 
187  }
188 
189 }