Contiki 2.6

autoack-tx.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 #define LED LED_RED
00044 
00045 /* 802.15.4 PSDU is 127 MAX */
00046 /* 2 bytes are the FCS */
00047 /* therefore 125 is the max payload length */
00048 #define PAYLOAD_LEN 16
00049 #define DELAY 100000
00050 
00051 void fill_packet(volatile packet_t *p) {
00052         static volatile uint8_t count=0;
00053 
00054         p->length = 16;
00055         p->offset = 0;
00056         p->data[0] = 0x71;  /* 0b 10 01 10 000 1 1 0 0 001 data, ack request, short addr */
00057         p->data[1] = 0x98;  /* 0b 10 01 10 000 1 1 0 0 001 data, ack request, short addr */
00058         p->data[2] = count++; /* dsn */
00059         p->data[3] = 0xaa;    /* pan */
00060         p->data[4] = 0xaa;
00061         p->data[5] = 0x11;    /* dest. short addr. */
00062         p->data[6] = 0x11;
00063         p->data[7] = 0x22;    /* src. short addr. */
00064         p->data[8] = 0x22;
00065 
00066         /* payload */
00067         p->data[9] = 'a';
00068         p->data[10] = 'c';
00069         p->data[11] = 'k';
00070         p->data[12] = 't';
00071         p->data[13] = 'e';
00072         p->data[14] = 's';
00073         p->data[15] = 't';
00074 
00075 }
00076 
00077 void maca_tx_callback(volatile packet_t *p) {
00078         switch(p->status) {
00079         case 0:
00080                 printf("TX OK\n\r");
00081                 break;
00082         case 3:
00083                 printf("CRC ERR\n\r");
00084                 break;
00085         case 5:
00086                 printf("NO ACK\n\r");
00087                 break;
00088         default:
00089                 printf("unknown status: %d\n", (int)p->status);
00090         }
00091 }
00092 
00093 void main(void) {
00094         volatile packet_t *p;
00095         char c;
00096         uint16_t r=30; /* start reception 100us before ack should arrive */
00097         uint16_t end=180; /* 750 us receive window*/
00098 
00099         /* trim the reference osc. to 24MHz */
00100         trim_xtal();
00101 
00102         uart_init(INC, MOD, SAMP);
00103 
00104         vreg_init();
00105 
00106         maca_init();
00107 
00108         set_channel(0); /* channel 11 */
00109 //      set_power(0x0f); /* 0xf = -1dbm, see 3-22 */
00110 //      set_power(0x11); /* 0x11 = 3dbm, see 3-22 */
00111         set_power(0x12); /* 0x12 is the highest, not documented */
00112 
00113         /* sets up tx_on, should be a board specific item */
00114         GPIO->FUNC_SEL_44 = 1;   
00115         GPIO->PAD_DIR_SET_44 = 1;        
00116 
00117         GPIO->FUNC_SEL_45 = 2;   
00118         GPIO->PAD_DIR_SET_45 = 1;        
00119 
00120         *MACA_RXACKDELAY = r;
00121         
00122         printf("rx warmup: %d\n\r", (int)(*MACA_WARMUP & 0xfff));
00123 
00124         *MACA_RXEND = end;
00125 
00126         printf("rx end: %d\n\r", (int)(*MACA_RXEND & 0xfff));
00127 
00128         set_prm_mode(AUTOACK);
00129 
00130         print_welcome("rftest-tx");
00131 
00132         while(1) {              
00133                         
00134                 /* call check_maca() periodically --- this works around */
00135                 /* a few lockup conditions */
00136                 check_maca();
00137 
00138                 while((p = rx_packet())) {
00139                         if(p) {
00140                                 printf("RX: ");
00141                                 print_packet(p);
00142                                 free_packet(p);
00143                         }
00144                 }
00145 
00146                 if(uart1_can_get()) {
00147                         c = uart1_getc();
00148 
00149                         switch(c) {
00150                         case 'z':
00151                                 r++;
00152                                 if(r > 4095) { r = 0; }
00153                                 *MACA_RXACKDELAY = r;
00154                                 printf("rx ack delay: %d\n\r", r);
00155                                 break;
00156                         case 'x':
00157                                 if(r == 0) { r = 4095; } else { r--; }
00158                                 *MACA_RXACKDELAY = r;
00159                                 printf("rx ack delay: %d\n\r", r);
00160                                 break;
00161                         case 'q':
00162                                 end++;
00163                                 if(r > 4095) { r = 0; }
00164                                 *MACA_RXEND = end;
00165                                 printf("rx end: %d\n\r", end);
00166                                 break;
00167                         case 'w':
00168                                 end--;
00169                                 if(r == 0) { r = 4095; } else { r--; }
00170                                 *MACA_RXEND = end;
00171                                 printf("rx end: %d\n\r", end);
00172                                 break;
00173                         default:
00174                                 p = get_free_packet();
00175                                 if(p) {
00176                                         fill_packet(p);
00177                                         
00178                                         printf("autoack-tx --- ");
00179                                         print_packet(p);
00180                                         
00181                                         tx_packet(p);                           
00182                                 }
00183                                 break;
00184                         }
00185                 }
00186                 
00187         }
00188 
00189 }