Contiki 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 #ifndef OWN_SENSORS_DEFINITION
54 SENSORS(&button_sensor, &button_sensor2);
55 #endif
56 
57 #ifndef M12_CONF_SERIAL
58 #define M12_SERIAL 0x000000
59 #else
60 #define M12_SERIAL M12_CONF_SERIAL
61 #endif
62 
63 int main(void) {
64 
65  mc1322x_init();
66 
67  /* m12_init() flips the mux switch */
68 
69  /* trims the main crystal load capacitance */
70  if (!FORCE_ECONOTAG_I && CRM->SYS_CNTLbits.XTAL32_EXISTS) {
71  /* M12 based econotag */
72  PRINTF("trim xtal for M12\n\r");
73  CRM->XTAL_CNTLbits.XTAL_CTUNE = (M12_CTUNE_4PF << 4) | M12_CTUNE;
74  CRM->XTAL_CNTLbits.XTAL_FTUNE = M12_FTUNE;
75 
76  /* configure pullups for low power */
77  GPIO->FUNC_SEL.GPIO_63 = 3;
78  GPIO->PAD_PU_SEL.GPIO_63 = 0;
79  GPIO->FUNC_SEL.SS = 3;
80  GPIO->PAD_PU_SEL.SS = 1;
81  GPIO->FUNC_SEL.VREF2H = 3;
82  GPIO->PAD_PU_SEL.VREF2H = 1;
83  GPIO->FUNC_SEL.U1RTS = 3;
84  GPIO->PAD_PU_SEL.U1RTS = 1;
85 
86  } else {
87  /* econotag I */
88  PRINTF("trim xtal for Econotag I\n\r");
89  CRM->XTAL_CNTLbits.XTAL_CTUNE = (ECONOTAG_CTUNE_4PF << 4) | ECONOTAG_CTUNE;
90  CRM->XTAL_CNTLbits.XTAL_FTUNE = ECONOTAG_FTUNE;
91  }
92 
93  /* create mac address if blank*/
94  if (mc1322x_config.eui == 0) {
95  /* mac address is blank */
96  /* construct a new mac address based on IAB or OUI definitions */
97 
98  /* if an M12_SERIAL number is not defined */
99  /* generate a random extension in the Redwire experimental IAB */
100  /* The Redwire IAB (for development only) is: */
101  /* OUI: 0x0050C2 IAB: 0xA8C */
102  /* plus a random 24-bit extension */
103  /* Otherwise, construct a mac based on the M12_SERIAL */
104  /* Owners of an Econotag I (not M12 based) can request a serial number from Redwire */
105  /* to use here */
106 
107  /* M12 mac is of the form "EC473C4D12000000" */
108  /* Redwire's OUI: EC473C */
109  /* M12: 4D12 */
110  /* next six nibbles are the M12 serial number as hex */
111  /* e.g. if the barcode reads: "12440021" = BDD1D5 */
112  /* full mac is EC473C4D12BDD1D5 */
113 
114 #if (M12_SERIAL == 0)
115  /* use random mac from experimental range */
116  mc1322x_config.eui = (0x0050C2A8Cull << 24) | (*MACA_RANDOM & (0xffffff));
117 #else
118  /* construct mac from serial number */
119  mc1322x_config.eui = (0xEC473C4D12ull << 24) | M12_SERIAL;
120 #endif
121  mc1322x_config_save(&mc1322x_config);
122  }
123 
124  /* configure address on maca hardware and RIME */
125  contiki_maca_set_mac_address(mc1322x_config.eui);
126 
127 #if WITH_UIP6
128  memcpy(&uip_lladdr.addr, &linkaddr_node_addr.u8, sizeof(uip_lladdr.addr));
129  queuebuf_init();
130  NETSTACK_RDC.init();
131  NETSTACK_MAC.init();
132  NETSTACK_NETWORK.init();
133  #if DEBUG_ANNOTATE
134  print_netstack();
135  #endif
136  process_start(&tcpip_process, NULL);
137  #if DEBUG_ANNOTATE
138  print_lladdrs();
139  #endif
140 #endif /* endif WITH_UIP6 */
141 
142  process_start(&sensors_process, NULL);
143 
144  print_processes(autostart_processes);
145  autostart_start(autostart_processes);
146 
147  /* Main scheduler loop */
148  while(1) {
149  check_maca();
150 
151  if(uart1_input_handler != NULL) {
152  if(uart1_can_get()) {
153  uart1_input_handler(uart1_getc());
154  }
155  }
156 
157  process_run();
158  }
159 
160  return 0;
161 }
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
#define NULL
The null pointer.
int main(void)
This is main...
Definition: ethconfig.c:49
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
A set of debugging macros.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Rime address representation