Contiki-Inga 3.x
contiki-main.c
1 // #define __PROG_TYPES_COMPAT__
2 
3 
4 
5 #include "contiki.h"
6 
7 #include "contiki-conf.h"
8 
9 #include <avr/pgmspace.h>
10 #include <avr/fuse.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <dev/watchdog.h>
14 #include <stdbool.h>
15 
16 //#include <dev/leds.h>
17 
18 //#include <util/delay.h>
19 
20 #include <dev/rs232.h>
21 #include "dev/serial-line.h"
22 #include "dev/slip.h"
23 
24 // settings manager
25 #include "settings.h"
26 
27 #include <autostart.h>
28 #include "rtimer-arch.h"
29 
30 
31 // Radio
32 #if RF230BB
33  #include "radio/rf230bb/rf230bb.h"
34  #include "net/mac/frame802154.h"
35  #include "net/mac/framer-802154.h"
36  #include "net/sicslowpan.h"
37 #else
38  #error No Xmega radio driver available
39 #endif
40 
41 #include "contiki-net.h"
42 #include "contiki-lib.h"
43 #include "net/rime.h"
44 
45 // NodeID
46 // #include "dev/nodeid.h"
47 
48 // Apps
49 #if defined(APP_SETTINGS_DELETE)
50  #include "settings_delete.h"
51 #elif defined(APP_SETTINGS_SET)
52  #include "settings_set.h"
53 #endif
54 
55 void platform_radio_init(void)
56 {
57  // Using default or project value as default value
58  // NOTE: These variables will always be overwritten when having an eeprom value
59 
60  uint8_t radio_tx_power = RADIO_TX_POWER;
61  uint8_t radio_channel = RADIO_CHANNEL;
62  uint16_t pan_id = RADIO_PAN_ID;
63  uint16_t pan_addr = NODE_ID;
64  uint8_t ieee_addr[8] = {0,0,0,0,0,0,0,0};
65 
66  printf("default_channel: %d\n", RADIO_CHANNEL);
67 
68  // Start radio and radio receive process
69  NETSTACK_RADIO.init();
70 
71  // NODE_ID
73  {
74  pan_addr = settings_get_uint16(SETTINGS_KEY_PAN_ADDR, 0);
75  }
76  else
77  {
78  printf("NodeID/PanAddr not in EEPROM - using default\n");
79  }
80 
81  // TX_POWER
82  if(settings_check(SETTINGS_KEY_TXPOWER, 0) == true)
83  {
84  radio_tx_power = settings_get_uint8(SETTINGS_KEY_TXPOWER, 0);
85  }
86  else
87  {
88  printf("Radio TXPower not in EEPROM - using default\n");
89  }
90 
91  // CHANNEL
92  if(settings_check(SETTINGS_KEY_CHANNEL, 0) == true)
93  {
94  radio_channel = settings_get_uint8(SETTINGS_KEY_CHANNEL, 0);
95  }
96  else
97  {
98  printf("Radio Channel not in EEPROM - using default\n");
99  }
100 
101  // IEEE ADDR
102  // if setting not set or invalid data - generate ieee_addr from node_id
103  if(settings_check(SETTINGS_KEY_EUI64, 0) != true || settings_get(SETTINGS_KEY_EUI64, 0, (void*)&ieee_addr, (size_t*) sizeof(ieee_addr)) != SETTINGS_STATUS_OK)
104  {
105  ieee_addr[0] = pan_addr & 0xFF;
106  ieee_addr[1] = (pan_addr >> 8) & 0xFF;
107  ieee_addr[2] = 0;
108  ieee_addr[3] = 0;
109  ieee_addr[4] = 0;
110  ieee_addr[5] = 0;
111  ieee_addr[6] = 0;
112  ieee_addr[7] = 0;
113 
114  printf("Radio IEEE Addr not in EEPROM - using default\n");
115  }
116 
117  printf("network_id(pan_id): 0x%X\n", pan_addr);
118  printf("node_id(pan_addr): 0x%X\n", pan_addr);
119  printf("radio_tx_power: 0x%X\n", radio_tx_power);
120  printf("radio_channel: 0x%X\n", radio_channel);
121  printf("ieee_addr: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", ieee_addr[0], ieee_addr[1], ieee_addr[2], ieee_addr[3], ieee_addr[4], ieee_addr[5], ieee_addr[6], ieee_addr[7]);
122 
123  rimeaddr_t addr = {{(pan_addr >> 8) & 0xFF, pan_addr & 0xFF}};
124  rimeaddr_set_node_addr(&addr);
125 
126  rf230_set_pan_addr(
127  pan_id, // Network address 2 byte // get_panid_from_eeprom()
128  pan_addr, // PAN ADD 2 Byte // get_panaddr_from_eeprom()
129  (uint8_t *) &ieee_addr // MAC ADDRESS 8 byte
130  );
131 
132  rf230_set_channel(radio_channel);
133  rf230_set_txpower(radio_tx_power);
134 
135  // Copy NodeID to the link local address
136  #if UIP_CONF_IPV6
137  memcpy(&uip_lladdr.addr, &addr.u8, sizeof(rimeaddr_t));
138  #endif
139 
140  queuebuf_init();
141  NETSTACK_RDC.init();
142  NETSTACK_MAC.init();
143  NETSTACK_NETWORK.init();
144 }
145 
146 /* End code from INGA*/
147 
148 void init(void)
149 {
150  cli();
151 
152  // Init here because we might disable some PR afterwards
153  xmega_powerreduction_enable();
154 
155  // enable High, Med and Low interrupts
156  xmega_interrupt_enable(PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm);
157 
158  // init clock
159  xmega_clock_init();
160 
161  // init RS232
162  // Disable PR for Port first
163  // PORT0 = E, PORT1 = F
164  PR.PRPF &= (~PR_USART0_bm);
165  // actual init
166  rs232_init(RS232_PORT_1, USART_BAUD_115200, USART_PARITY_NONE | USART_STOP_BITS_1 | USART_DATA_BITS_8);
167  rs232_redirect_stdout(RS232_PORT_1);
168 
169  // init the timer only when we have a batterybackup system, else we use 1kHz from RTC
170  #ifdef RTC32
171  PR.PRPC &= (~PR_TC0_bm);
172  clock_init();
173  #endif
174 
175  // watchdog
176  watchdog_init();
177  watchdog_start();
178 
179  // rtimer (init only when RTC is available) if so we use it as etimer source
180  #ifdef RTC
181  rtimer_init();
182  #endif
183 
184  // Event System used for Timer
185  #if defined(XMEGA_TIMER_RTC) && XMEGA_TIMER_RTC == 1
186  PR.PRGEN &= (~PR_EVSYS_bm);
187  EVSYS.CH0MUX = (1 << 3); // Use RTC overflow interrupt as Channel 0 Source
188  #endif
189 
190  // Initialize process subsystem
191  process_init();
192 
193  // etimers must be started before ctimer_init
194  process_start(&etimer_process, NULL);
195 
196  ctimer_init();
197 
198  //printf("RST.STATUS: %X\n", RST.STATUS);
199 
200  #if PLATFORM_RADIO
201  // Disable Power Reduction for the SPI
202  PR.PRPC &= (~PR_SPI_bm);
203  // Init radio
204  platform_radio_init();
205  #endif
206 
207  // Enable Non Volatile Memory Power Reduction after we read from EEPROM
208  #if POWERREDUCTION_NVM
209  xmega_pr_nvm_enable();
210  #endif
211 
212  // printf("Welcome to Contiki.XMega | F_CPU = %d\n", (unsigned long) F_CPU);
213 
214  // autostart processes
215  autostart_start(autostart_processes);
216 
217  #if defined(APP_SETTINGS_SET)
218  process_start(&settings_set_process, NULL);
219  #endif
220 
221  #if defined(APP_SETTINGS_DELETE)
222  process_start(&settings_delete_process, NULL);
223  #endif
224 
225  #if UIP_CONF_IPV6
226  process_start(&tcpip_process, NULL);
227  #endif
228 
229 
230 
231 
232 
233 
234  sei();
235 
236  printf("Welcome to Contiki.XMega\n");
237 }
238 
239 int main(void)
240 {
241  init();
242 
243  while(1)
244  {
246  process_run();
247  }
248 
249  return 0;
250 }