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 WITH_UIP6
49 #include "net/ipv6/uip-ds6.h"
50 #endif /* WITH_UIP6 */
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 static uint16_t node_id = 0x0102;
70 
71 /*---------------------------------------------------------------------------*/
72 static void
73 set_rime_addr(void)
74 {
75  linkaddr_t addr;
76  int i;
77 
78  memset(&addr, 0, sizeof(linkaddr_t));
79 #if UIP_CONF_IPV6
80  memcpy(addr.u8, serial_id, sizeof(addr.u8));
81 #else
82  if(node_id == 0) {
83  for(i = 0; i < sizeof(linkaddr_t); ++i) {
84  addr.u8[i] = serial_id[7 - i];
85  }
86  } else {
87  addr.u8[0] = node_id & 0xff;
88  addr.u8[1] = node_id >> 8;
89  }
90 #endif
92  printf("Rime started with address ");
93  for(i = 0; i < sizeof(addr.u8) - 1; i++) {
94  printf("%d.", addr.u8[i]);
95  }
96  printf("%d" NEWLINE, addr.u8[i]);
97 }
98 /*---------------------------------------------------------------------------*/
99 int contiki_argc = 0;
100 char **contiki_argv;
101 
102 static void
103 delay_1sec(void)
104 {
105  /* Delay 1 second */
106  register unsigned long int i;
107  for(i = 0x000FFFFFUL; i; --i) {
108  asm ("nop");
109  }
110 }
111 int
112 main(int argc, char **argv)
113 {
114  bool flip_flop = false;
115 
116  asm ("di");
117  /* Setup clocks */
118  CMC = 0x11U; /* Enable XT1, disable X1 */
119  CSC = 0x80U; /* Start XT1 and HOCO, stop X1 */
120  CKC = 0x00U;
121  delay_1sec();
122  OSMC = 0x00; /* Supply fsub to peripherals, including Interval Timer */
123  uart0_init();
124 
125 #if __GNUC__
126  /* Force linking of custom write() function: */
127  write(1, NULL, 0);
128 #endif
129 
130  /* Setup 12-bit interval timer */
131  RTCEN = 1; /* Enable 12-bit interval timer and RTC */
132  ITMK = 1; /* Disable IT interrupt */
133  ITPR0 = 0; /* Set interrupt priority - highest */
134  ITPR1 = 0;
135  ITMC = 0x8FFFU; /* Set maximum period 4096/32768Hz = 1/8 s, and start timer */
136  ITIF = 0; /* Clear interrupt request flag */
137  ITMK = 0; /* Enable IT interrupt */
138  /* asm ("ei"); / * Enable interrupts * / */
139 
140  /* Disable analog inputs because they can conflict with the SPI buses: */
141  ADPC = 0x01; /* Configure all analog pins as digital I/O. */
142  PMC0 &= 0xF0; /* Disable analog inputs. */
143 
144  clock_init();
145 
146  /* Initialize Joystick Inputs: */
147  PM5 |= BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1); /* Set pins as inputs. */
148  PU5 |= BIT(5) | BIT(4) | BIT(3) | BIT(2) | BIT(1); /* Enable internal pull-up resistors. */
149 
150  /* Initialize LED outputs: */
151 #define BIT(n) (1 << (n))
152  PM12 &= ~BIT(0); /* LED1 */
153  PM4 &= ~BIT(3); /* LED2 */
154  PM1 &= ~BIT(6); /* LED3 */
155  PM1 &= ~BIT(5); /* LED4 */
156  PM0 &= ~BIT(6); /* LED5 */
157  PM0 &= ~BIT(5); /* LED6 */
158  PM3 &= ~BIT(0); /* LED7 */
159  PM5 &= ~BIT(0); /* LED8 */
160 
161 #if UIP_CONF_IPV6
162 #if UIP_CONF_IPV6_RPL
163  printf(CONTIKI_VERSION_STRING " started with IPV6, RPL" NEWLINE);
164 #else
165  printf(CONTIKI_VERSION_STRING " started with IPV6" NEWLINE);
166 #endif
167 #else
168  printf(CONTIKI_VERSION_STRING " started" NEWLINE);
169 #endif
170 
171  /* crappy way of remembering and accessing argc/v */
172  contiki_argc = argc;
173  contiki_argv = argv;
174 
175  process_init();
176  process_start(&etimer_process, NULL);
177  ctimer_init();
178 
179  set_rime_addr();
180 
181  queuebuf_init();
182 
183  netstack_init();
184  printf("MAC %s RDC %s NETWORK %s" NEWLINE, NETSTACK_MAC.name, NETSTACK_RDC.name, NETSTACK_NETWORK.name);
185 
186 #if WITH_UIP6
187  memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
188 
189  process_start(&tcpip_process, NULL);
190  printf("Tentative link-local IPv6 address ");
191  {
192  uip_ds6_addr_t *lladdr;
193  int i;
194  lladdr = uip_ds6_get_link_local(-1);
195  for(i = 0; i < 7; ++i) {
196  printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
197  lladdr->ipaddr.u8[i * 2 + 1]);
198  }
199  /* make it hardcoded... */
200  lladdr->state = ADDR_AUTOCONF;
201 
202  printf("%02x%02x" NEWLINE, lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
203  }
204 #else
205  process_start(&tcpip_process, NULL);
206 #endif
207 
208  serial_line_init();
209 
210  autostart_start(autostart_processes);
211 
212  while(1) {
214 
215  if(NETSTACK_RADIO.pending_packet()) {
216  int len;
217  packetbuf_clear();
218  len = NETSTACK_RADIO.read(packetbuf_dataptr(), PACKETBUF_SIZE);
219  if(len > 0) {
221  NETSTACK_RDC.input();
222  }
223  }
224 
225  while(uart0_can_getchar()) {
226  char c;
227  UART_RX_LED = 1;
228  c = uart0_getchar();
229  if(uart0_input_handler) {
230  uart0_input_handler(c);
231  }
232  }
233  UART_RX_LED = 0;
234 
235  process_run();
236 
238 
239  HEARTBEAT_LED1 = flip_flop;
240  flip_flop = !flip_flop;
241  HEARTBEAT_LED2 = flip_flop;
242  }
243 
244  return 0;
245 }
246 /*---------------------------------------------------------------------------*/
247 void
248 log_message(char *m1, char *m2)
249 {
250  printf("%s%s" NEWLINE, m1, m2);
251 }
252 /*---------------------------------------------------------------------------*/
253 void
254 uip_log(char *m)
255 {
256  printf("%s" NEWLINE, m);
257 }
258 /*---------------------------------------------------------------------------*/
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
Unicast address structure.
Definition: uip-ds6.h:172
Header file for the uIP TCP/IP stack.
void uart0_init(unsigned long ubr)
Initalize the RS232 port.
Definition: uart0.c:111
Network interface and stateless autoconfiguration (RFC 4862)
#define NULL
The null pointer.
void uip_log(char *msg)
Print out a uIP log message.
Definition: uip-log.c:3
void clock_init(void)
Initialize the clock library.
Definition: clock.c:76
int main(void)
This is main...
Definition: ethconfig.c:49
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
void process_init(void)
Initialize the process module.
Definition: process.c:208
Header file for the Rime stack
#define BIT(x)
Useful to reference a single bit of a byte.
Generic serial I/O process header filer.
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
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:77
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
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:65
void linkaddr_set_node_addr(linkaddr_t *t)
Set the address of the current node.
Definition: linkaddr.c:72
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
Include file for the Contiki low-layer network stack (NETSTACK)