Contiki-Inga 3.x
main.c
1 /*
2  * Copyright (c) 2012, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org) and Contiki.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include <string.h>
34 
35 /* debug */
36 #define DEBUG DEBUG_FULL
37 #include "net/ip/uip-debug.h"
38 
39 /* contiki */
40 #include "contiki.h"
41 #include "dev/button-sensor.h"
42 #include "net/linkaddr.h"
43 #include "net/netstack.h"
44 
45 /* mc1322x */
46 #include "mc1322x.h"
47 #include "config.h"
48 #include "contiki-uart.h"
49 
50 /* econotag */
51 #include "platform_prints.h"
52 
53 SENSORS(&button_sensor);
54 
55 #ifndef M12_CONF_SERIAL
56 #define M12_SERIAL 0x000000
57 #else
58 #define M12_SERIAL M12_CONF_SERIAL
59 #endif
60 
61 int main(void) {
62 
63  mc1322x_init();
64 
65  /* m12_init() flips the mux switch */
66 
67  /* trims the main crystal load capacitance */
68  if (!FORCE_ECONOTAG_I && CRM->SYS_CNTLbits.XTAL32_EXISTS) {
69  /* M12 based econotag */
70  PRINTF("trim xtal for M12\n\r");
71  CRM->XTAL_CNTLbits.XTAL_CTUNE = (M12_CTUNE_4PF << 4) | M12_CTUNE;
72  CRM->XTAL_CNTLbits.XTAL_FTUNE = M12_FTUNE;
73 
74  /* configure pullups for low power */
75  GPIO->FUNC_SEL.GPIO_63 = 3;
76  GPIO->PAD_PU_SEL.GPIO_63 = 0;
77  GPIO->FUNC_SEL.SS = 3;
78  GPIO->PAD_PU_SEL.SS = 1;
79  GPIO->FUNC_SEL.VREF2H = 3;
80  GPIO->PAD_PU_SEL.VREF2H = 1;
81  GPIO->FUNC_SEL.U1RTS = 3;
82  GPIO->PAD_PU_SEL.U1RTS = 1;
83 
84  } else {
85  /* econotag I */
86  PRINTF("trim xtal for Econotag I\n\r");
87  CRM->XTAL_CNTLbits.XTAL_CTUNE = (ECONOTAG_CTUNE_4PF << 4) | ECONOTAG_CTUNE;
88  CRM->XTAL_CNTLbits.XTAL_FTUNE = ECONOTAG_FTUNE;
89  }
90 
91  /* create mac address if blank*/
92  if (mc1322x_config.eui == 0) {
93  /* mac address is blank */
94  /* construct a new mac address based on IAB or OUI definitions */
95 
96  /* if an M12_SERIAL number is not defined */
97  /* generate a random extension in the Redwire experimental IAB */
98  /* The Redwire IAB (for development only) is: */
99  /* OUI: 0x0050C2 IAB: 0xA8C */
100  /* plus a random 24-bit extension */
101  /* Otherwise, construct a mac based on the M12_SERIAL */
102  /* Owners of an Econotag I (not M12 based) can request a serial number from Redwire */
103  /* to use here */
104 
105  /* M12 mac is of the form "EC473C4D12000000" */
106  /* Redwire's OUI: EC473C */
107  /* M12: 4D12 */
108  /* next six nibbles are the M12 serial number as hex */
109  /* e.g. if the barcode reads: "12440021" = BDD1D5 */
110  /* full mac is EC473C4D12BDD1D5 */
111 
112 #if (M12_SERIAL == 0)
113  /* use random mac from experimental range */
114  mc1322x_config.eui = (0x0050C2A8Cull << 24) | (*MACA_RANDOM & (0xffffff));
115 #else
116  /* construct mac from serial number */
117  mc1322x_config.eui = (0xEC473C4D12ull << 24) | M12_SERIAL;
118 #endif
119  mc1322x_config_save(&mc1322x_config);
120  }
121 
122  /* configure address on maca hardware and RIME */
123  contiki_maca_set_mac_address(mc1322x_config.eui);
124 
125 #if WITH_UIP6
126  memcpy(&uip_lladdr.addr, &linkaddr_node_addr.u8, sizeof(uip_lladdr.addr));
127  queuebuf_init();
128  NETSTACK_RDC.init();
129  NETSTACK_MAC.init();
130  NETSTACK_NETWORK.init();
131  #if DEBUG_ANNOTATE
132  print_netstack();
133  #endif
134  process_start(&tcpip_process, NULL);
135  #if DEBUG_ANNOTATE
136  print_lladdrs();
137  #endif
138 #endif /* endif WITH_UIP6 */
139 
140  process_start(&sensors_process, NULL);
141 
142  print_processes(autostart_processes);
143  autostart_start(autostart_processes);
144 
145  /* Main scheduler loop */
146  while(1) {
147  check_maca();
148 
149  if(uart1_input_handler != NULL) {
150  if(uart1_can_get()) {
151  uart1_input_handler(uart1_getc());
152  }
153  }
154 
155  process_run();
156  }
157 
158  return 0;
159 }