Contiki 3.x
contiki-main.c
1 /*
2  * Copyright (c) 2014, Analog Devices, Inc.
3  * All rights reserved.
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  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \author Ian Martin <martini@redwirellc.com>
33  */
34 
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include "contiki.h"
40 #include "net/netstack.h"
41 
42 #include "dev/serial-line.h"
43 
44 #include "net/ip/uip.h"
45 
46 #include "dev/button-sensor.h"
47 
48 #if NETSTACK_CONF_WITH_IPV6
49 #include "net/ipv6/uip-ds6.h"
50 #endif /* NETSTACK_CONF_WITH_IPV6 */
51 
52 #include "net/rime/rime.h"
53 #include "uart0.h"
54 #include "contiki-uart.h"
55 #include "watchdog.h"
56 #include "slip-arch.h"
57 
58 #if __GNUC__
59 #include "write.h"
60 #endif
61 
62 SENSORS(&button_sensor);
63 
64 #ifndef SERIAL_ID
65 #define SERIAL_ID { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }
66 #endif
67 
68 static uint8_t serial_id[] = SERIAL_ID;
69 #if !NETSTACK_CONF_WITH_IPV6
70 static uint16_t node_id = 0x0102;
71 #endif /* !NETSTACK_CONF_WITH_IPV6 */
72 
73 /*---------------------------------------------------------------------------*/
74 static void
75 set_rime_addr(void)
76 {
77  linkaddr_t addr;
78  int i;
79 
80  memset(&addr, 0, sizeof(linkaddr_t));
81 #if NETSTACK_CONF_WITH_IPV6
82  memcpy(addr.u8, serial_id, sizeof(addr.u8));
83 #else
84  if(node_id == 0) {
85  for(i = 0; i < sizeof(linkaddr_t); ++i) {
86  addr.u8[i] = serial_id[7 - i];
87  }
88  } else {
89  addr.u8[0] = node_id & 0xff;
90  addr.u8[1] = node_id >> 8;
91  }
92 #endif
94  printf("Rime started with address ");
95  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
96  printf("%d.", addr.u8[i]);
97  }
98  printf("%d" NEWLINE, addr.u8[i]);
99 }
100 /*---------------------------------------------------------------------------*/
101 int contiki_argc = 0;
102 char **contiki_argv;
103 
104 static void
105 delay_1sec(void)
106 {
107  /* Delay 1 second */
108  register unsigned long int i;
109  for(i = 0x000FFFFFUL; i; --i) {
110  asm ("nop");
111  }
112 }
113 int
114 main(int argc, char **argv)
115 {
116  bool flip_flop = false;
117 
118  asm ("di");
119  /* Setup clocks */
120  CMC = 0x11U; /* Enable XT1, disable X1 */
121  CSC = 0x80U; /* Start XT1 and HOCO, stop X1 */
122  CKC = 0x00U;
123  delay_1sec();
124  OSMC = 0x00; /* Supply fsub to peripherals, including Interval Timer */
125  uart0_init();
126 
127 #if __GNUC__
128  /* Force linking of custom write() function: */
129  write(1, NULL, 0);
130 #endif
131 
132  /* Setup 12-bit interval timer */
133  RTCEN = 1; /* Enable 12-bit interval timer and RTC */
134  ITMK = 1; /* Disable IT interrupt */
135  ITPR0 = 0; /* Set interrupt priority - highest */
136  ITPR1 = 0;
137  ITMC = 0x8FFFU; /* Set maximum period 4096/32768Hz = 1/8 s, and start timer */
138  ITIF = 0; /* Clear interrupt request flag */
139  ITMK = 0; /* Enable IT interrupt */
140  /* asm ("ei"); / * Enable interrupts * / */
141 
142  /* Disable analog inputs because they can conflict with the SPI buses: */
143  ADPC = 0x01; /* Configure all analog pins as digital I/O. */
144  PMC0 &= 0xF0; /* Disable analog inputs. */
145 
146  clock_init();
147 
148  /* Initialize Joystick Inputs: */
149  PM5 |= BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1); /* Set pins as inputs. */
150  PU5 |= BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1); /* Enable internal pull-up resistors. */
151 
152  /* Initialize LED outputs: */
153 #define BIT(n) (1 << (n))
154  PM12 &= ~BIT(0); /* LED1 */
155  PM4 &= ~BIT(3); /* LED2 */
156  PM1 &= ~BIT(6); /* LED3 */
157  PM1 &= ~BIT(5); /* LED4 */
158  PM0 &= ~BIT(6); /* LED5 */
159  PM0 &= ~BIT(5); /* LED6 */
160  PM3 &= ~BIT(0); /* LED7 */
161  PM5 &= ~BIT(0); /* LED8 */
162 
163 #if NETSTACK_CONF_WITH_IPV6
164 #if UIP_CONF_IPV6_RPL
165  printf(CONTIKI_VERSION_STRING " started with IPV6, RPL" NEWLINE);
166 #else
167  printf(CONTIKI_VERSION_STRING " started with IPV6" NEWLINE);
168 #endif
169 #else
170  printf(CONTIKI_VERSION_STRING " started" NEWLINE);
171 #endif
172 
173  /* crappy way of remembering and accessing argc/v */
174  contiki_argc = argc;
175  contiki_argv = argv;
176 
177  process_init();
178  process_start(&etimer_process, NULL);
179  ctimer_init();
180 
181  set_rime_addr();
182 
183  queuebuf_init();
184 
185  netstack_init();
186  printf("MAC %s RDC %s NETWORK %s" NEWLINE, NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name);
187 
188 #if NETSTACK_CONF_WITH_IPV6
189  memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
190 
191  process_start(&tcpip_process, NULL);
192  printf("Tentative link-local IPv6 address ");
193  {
194  uip_ds6_addr_t *lladdr;
195  int i;
196  lladdr = uip_ds6_get_link_local(-1);
197  for(i = 0; i < 7; ++i) {
198  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
199  lladdr->ipaddr.u8[i * 2 + 1]);
200  }
201  /* make it hardcoded... */
202  lladdr->state = ADDR_AUTOCONF;
203 
204  printf("%02x%02x" NEWLINE, lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
205  }
206 #elif NETSTACK_CONF_WITH_IPV4
207  process_start(&tcpip_process, NULL);
208 #endif
209 
210  serial_line_init();
211 
212  autostart_start(autostart_processes);
213 
214  while(1) {
216 
217  if(NETSTACK_RADIO.pending_packet()) {
218  int len;
219  packetbuf_clear();
220  len = NETSTACK_RADIO.read(packetbuf_dataptr(), PACKETBUF_SIZE);
221  if(len > 0) {
223  NETSTACK_RDC.input();
224  }
225  }
226 
227  while(uart0_can_getchar()) {
228  char c;
229  UART_RX_LED = 1;
230  c = uart0_getchar();
231  if(uart0_input_handler) {
232  uart0_input_handler(c);
233  }
234  }
235  UART_RX_LED = 0;
236 
237  process_run();
238 
240 
241  HEARTBEAT_LED1 = flip_flop;
242  flip_flop = !flip_flop;
243  HEARTBEAT_LED2 = flip_flop;
244  }
245 
246  return 0;
247 }
248 /*---------------------------------------------------------------------------*/
249 void
250 log_message(char *m1, char *m2)
251 {
252  printf("%s%s" NEWLINE, m1, m2);
253 }
254 /*---------------------------------------------------------------------------*/
255 void
256 uip_log(char *m)
257 {
258  printf("%s" NEWLINE, m);
259 }
260 /*---------------------------------------------------------------------------*/
void uip_log(char *m)
Print out a uIP log message.
Definition: contiki-main.c:187
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:143
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
Header file for IPv6-related data structures.
#define BIT(x)
Useful to reference a single bit of a byte.
void etimer_request_poll(void)
Make the event timer aware that the clock has changed.
Definition: etimer.c:145
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:76
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:66
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int process_run(void)
Run the system once - call poll handlers and process one event.
Definition: process.c:302
Unicast address structure.
Definition: uip-ds6.h:202
SENSORS & button_sensor
Copyright (c) 2014, Analog Devices, Inc.
Definition: contiki-main.c:61
Header file for the Rime stack
#define NULL
The null pointer.
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
Header file for the uIP TCP/IP stack.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:151
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
int main(void)
This is main...
Definition: contiki-main.c:456
Generic serial I/O process header filer.
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118
void process_init(void)
Initialize the process module.
Definition: process.c:208
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
Include file for the Contiki low-layer network stack (NETSTACK)