Contiki-Inga 3.x
raven-lcd.c
Go to the documentation of this file.
1 /* Copyright (c) 2008, Swedish Institute of Computer Science
2  * All rights reserved.
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of the copyright holders nor the names of
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31 */
32 
33 /**
34  * \brief This module contains code to interface a Contiki-based
35  * project on the AVR Raven platform's ATMega1284P chip to the LCD
36  * driver chip (ATMega3290P) on the Raven.
37  *
38  * \author Blake Leverett <bleverett@gmail.com>
39  *
40 */
41 
42 /** \addtogroup raven
43  * @{
44  */
45 
46 /**
47  * \defgroup ravenserial Serial interface between Raven processors
48  * @{
49  */
50 /**
51  * \file
52  * This file contains code to connect the two AVR Raven processors via a serial connection.
53  *
54  */
55 
56 #define DEBUG 0 //Making this 1 will slightly alter command timings
57 #if DEBUG
58 #define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
59 #else
60 #define PRINTF(...)
61 #endif
62 #define DEBUGSERIAL 0 //Making this 1 will significantly alter command timings
63 
64 #include "contiki.h"
65 #include "contiki-lib.h"
66 #include "contiki-net.h"
67 #include "webserver-nogui.h"
68 #include "httpd-cgi.h"
69 #include "raven-lcd.h"
70 
71 #include <string.h>
72 #include <stdio.h>
73 #include <avr/pgmspace.h>
74 #include <avr/eeprom.h>
75 #include <avr/sleep.h>
76 #include <dev/watchdog.h>
77 
78 static uint8_t count = 0;
79 static uint8_t seqno;
80 uip_ipaddr_t dest_addr;
81 
82 #define MAX_CMD_LEN 20
83 static struct{
84  uint8_t frame[MAX_CMD_LEN];
85  uint8_t ndx;
86  uint8_t len;
87  uint8_t cmd;
88  uint8_t done;
89 } cmd;
90 
91 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
92 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
93 #define PING6_DATALEN 16
94 
95 void rs232_send(uint8_t port, unsigned char c);
96 
97 /*---------------------------------------------------------------------------*/
98 /* Sends a ping packet out the radio */
99 /* Useful for debugging so allow external calls */
100 void
101 raven_ping6(void)
102 {
103 #if UIP_CONF_IPV6_RPL||1
104 /* No default router, so pick on someone else */
105 #define PING_GOOGLE 1
106 seqno++;
107 #endif
108 
109  UIP_IP_BUF->vtc = 0x60;
110  UIP_IP_BUF->tcflow = 1;
111  UIP_IP_BUF->flow = 0;
112  UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
113  UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
114 #if PING_GOOGLE
115  if (seqno==1) {
116  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, uip_ds6_defrt_choose()); //the default router
117  } else if (seqno==2) {
118  uip_ip6addr(&UIP_IP_BUF->destipaddr,0x2001,0x4860,0x800f,0x0000,0x0000,0x0000,0x0000,0x0093); //ipv6.google.com
119  } else if (seqno==3) {
120  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, uip_ds6_defrt_choose()); //the default router
121  } else {
122 // uip_ip6addr(&UIP_IP_BUF->destipaddr,0x2001,0x0420,0x5FFF,0x007D,0x02D0,0xB7FF,0xFE23,0xE6DB); //?.cisco.com
123  uip_ip6addr(&UIP_IP_BUF->destipaddr,0x2001,0x0420,0x0000,0x0010,0x0250,0x8bff,0xfee8,0xf800); //six.cisco.com
124  }
125 #else
126  uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, uip_ds6_defrt_choose()); //the default router
127 #endif
128 
129  uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
131  UIP_ICMP_BUF->icode = 0;
132  /* set identifier and sequence number to 0 */
133  memset((void *)UIP_ICMP_BUF + UIP_ICMPH_LEN, 0, 4);
134  /* put one byte of data */
135  memset((void *)UIP_ICMP_BUF + UIP_ICMPH_LEN + UIP_ICMP6_ECHO_REQUEST_LEN,
136  count, PING6_DATALEN);
137 
138 
139  uip_len = UIP_ICMPH_LEN + UIP_ICMP6_ECHO_REQUEST_LEN + UIP_IPH_LEN + PING6_DATALEN;
140  UIP_IP_BUF->len[0] = (uint8_t)((uip_len - 40) >> 8);
141  UIP_IP_BUF->len[1] = (uint8_t)((uip_len - 40) & 0x00FF);
142 
143  UIP_ICMP_BUF->icmpchksum = 0;
144  UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
145 
146 
148 }
149 #if defined(__AVR_ATmega128RFA1__)
150 /* No raven 3290p to talk to but the lcd process still needed for ping reporting */
151 /* Just dummy out send_frame */
152 static void
153 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
154 {
155 }
156 #else
157 /*---------------------------------------------------------------------------*/
158 /* Send a serial command frame to the ATMega3290 Processsor on Raven via serial port */
159 static void
160 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
161 {
162  uint8_t i;
163 
164  rs232_send(0, SOF_CHAR); /* Start of Frame */
165  rs232_send(0, len);
166  rs232_send(0, cmd);
167  for (i=0;i<len;i++)
168  rs232_send(0,*payload++);
169  rs232_send(0, EOF_CHAR);
170 }
171 #endif
172 char serial_char_received;
173 /*---------------------------------------------------------------------------*/
174 /* Sleep for howlong seconds, or until UART interrupt if howlong==0.
175  * Uses TIMER2 with external 32768 Hz crystal to sleep in 1 second multiples.
176  * TIMER2 may have already been set up for 125 ticks/second in clock.c
177 
178  *
179  * Until someone figures out how to get UART to wake from powerdown,
180  * a three second powersave cycle is used with exit based on any character received.
181 
182  * The system clock is adjusted to reflect the sleep time.
183  */
184 
185 void micro_sleep(uint8_t howlong)
186 {
187  uint8_t saved_sreg = SREG, saved_howlong = howlong;
188 #if AVR_CONF_USE32KCRYSTAL
189 /* Save TIMER2 configuration if clock.c is using it */
190  uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
191 #endif
192 
193 // if (howlong==0) {
194 // set_sleep_mode(SLEEP_MODE_PWR_DOWN); // UART can't wake from powerdown
195 // } else {
196  set_sleep_mode(SLEEP_MODE_PWR_SAVE); // Sleep for howlong seconds
197  if (howlong==0) howlong=3; // 3*32/(32768/1024) = 3 second sleep cycle if not specified
198  cli(); // Disable interrupts for the present
199  ASSR |= (1 << AS2); // Set TIMER2 asyncronous from external crystal
200  TCCR2A =(1<<WGM21); // CTC mode
201  TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));// Prescale by 1024 = 32 ticks/sec
202 // while(ASSR & (1 << TCR2BUB)); // Wait for TCNT2 write to finish.
203  OCR2A = howlong*32; // Set TIMER2 output compare register
204 // while(ASSR & (1 << OCR2AUB)); // Wait for OCR2 write to finish.
205  SREG = saved_sreg; // Restore interrupt state.
206 // UCSR(USART,B)&= ~(1<<RXCIE(USART)) // Disable the RX Complete interrupt;
207 // UCSR0B|=(1<<RXCIE0); // Enable UART0 RX complete interrupt
208 // UCSR1B|=(1<<RXCIE1); // Enable UART1 RX complete interrupt
209 // TCNT2 = 0; // Reset TIMER2 timer counter value.
210 // while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to finish before entering sleep.
211 // TIMSK2 |= (1 << OCIE2A); // Enable TIMER2 output compare interrupt.
212 // }
213 
214  TCNT2 = 0; // Reset timer
215  watchdog_stop(); // Silence annoying distractions
216  while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to (which assures TCCR2x and OCR2A are finished!)
217  TIMSK2 |= (1 << OCIE2A); // Enable TIMER2 output compare interrupt
218  SMCR |= (1 << SE); // Enable sleep mode.
219  while (1) {
220 // TCNT2 = 0; // Cleared automatically in CTC mode
221 // while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to finish before entering sleep.
222  serial_char_received=0; // Set when chars received by UART
223  sleep_mode(); // Sleep
224 
225  /* Adjust clock.c for the time spent sleeping */
226  extern void clock_adjust_ticks(uint16_t howmany);
227  clock_adjust_ticks(howlong * CLOCK_SECOND);
228 
229 // if (TIMSK2&(1<<OCIE2A)) break; // Exit sleep if not awakened by TIMER2
230  PRINTF(".");
231  if (saved_howlong) break; // Exit sleep if nonzero time specified
232  PRINTF("%d",serial_char_received);
233  if (serial_char_received) break;
234  }
235 
236  SMCR &= ~(1 << SE); //Disable sleep mode after wakeup
237 
238 #if AVR_CONF_USE32KCRYSTAL
239 /* Restore clock.c configuration */
240 // OCRSetup();
241  cli();
242  TCCR2A = savedTCCR2A;
243  TCCR2B = savedTCCR2B;
244  OCR2A = savedOCR2A;
245  TCNT2 = savedTCNT2;
246  sei();
247 #else
248  TIMSK2 &= ~(1 << OCIE2A); //Disable TIMER2 interrupt
249 #endif
250 
251  watchdog_start();
252 }
253 #if !AVR_CONF_USE32KCRYSTAL
254 /*---------------------------------------------------------------------------*/
255 /* TIMER2 Interrupt service */
256 
257 ISR(TIMER2_COMPA_vect)
258 {
259 // TIMSK2 &= ~(1 << OCIE2A); //Just one interrupt needed for waking
260 }
261 #endif /* !AVR_CONF_USE32KCRYSTAL */
262 
263 #if DEBUGSERIAL
264 uint8_t serialcount;
265 char dbuf[30];
266 #endif
267 
268 /*---------------------------------------------------------------------------*/
269 extern uint16_t ledtimer;
270 static uint8_t
271 raven_gui_loop(process_event_t ev, process_data_t data)
272 {
273  uint8_t i,activeconnections,radio_state;
274 
275 // PRINTF("\nevent %d ",ev);
276 #if DEBUGSERIAL
277  printf_P(PSTR("Buffer [%d]="),serialcount);
278  serialcount=0;
279  for (i=0;i<30;i++) {
280  printf_P(PSTR(" %d"),dbuf[i]);
281  dbuf[i]=0;
282  }
283 #endif
284  if(ev == tcpip_icmp6_event) switch(*((uint8_t *)data)) {
285 
286 // case ICMP6_NS:
287  /*Tell the 3290 we are being solicited. */
288 // send_frame(REPORT_NS,...);
289 // break; //fall through for beep
290 // case ICMP6_RA:
291  /*Tell the 3290 we have a router. */
292 // send_frame(REPORT_NA,...);
293 // break; //fall through for beep
294  case ICMP6_ECHO_REQUEST:
295  /* We have received a ping request over the air. Tell the 3290 */
296  // send_frame(REPORT_PING_BEEP, 0, 0);
297 #if RF230BB_CONF_LEDONPORTE1
298  PORTE|=(1<<PE1);ledtimer=1000; //turn on led, set counter for turnoff
299 #endif
300  break;
301  case ICMP6_ECHO_REPLY:
302  /* We have received a ping reply over the air. Send frame back to 3290 */
303  // send_frame(REPORT_PING, 1, &seqno);
304  break;
305 
306  } else switch (ev) {
307  case SERIAL_CMD:
308  /* Check for command from serial port, execute it. */
309  /* Note cmd frame is written in an interrupt - delays here can cause overwriting by next command */
310  PRINTF("\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
311  if (cmd.done){
312  /* Execute the waiting command */
313  switch (cmd.cmd){
314  case SEND_PING:
315  /* Send ping request over the air */
316  seqno = cmd.frame[0];
317  raven_ping6();
318  break;
319  case SEND_TEMP:
320  /* Set temperature string in web server */
321  // web_set_temp((char *)cmd.frame);
322  break;
323  case SEND_ADC2:
324  /* Set ext voltage string in web server */
325  // web_set_voltage((char *)cmd.frame);
326  break;
327  case SEND_SLEEP:
328  /* Sleep radio and 1284p. */
329  if (cmd.frame[0]==0) { //Time to sleep in seconds
330  /* Unconditional sleep. Don't wake until a serial interrupt. */
331  } else {
332  /* Sleep specified number of seconds (3290p "DOZE" mode) */
333  /* It sleeps a bit longer so we will be always be awake for the next sleep command. */
334  /* Only sleep this cycle if no active TCP/IP connections */
335  activeconnections=0;
336  for(i = 0; i < UIP_CONNS; ++i) {
337  if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
338  }
339  if (activeconnections) {
340  PRINTF("\nWaiting for %d connections",activeconnections);
341  break;
342  }
343  }
344  radio_state = NETSTACK_RADIO.off();
345  PRINTF ("\nsleep %d radio state %d...",cmd.frame[0],radio_state);
346 
347  /*Sleep for specified time*/
348  PRINTF("\nSleeping...");
349  micro_sleep(cmd.frame[0]);
350 
351  radio_state = NETSTACK_RADIO.on();
352  if (radio_state > 0) {
353  PRINTF("Awake!");
354  } else {
355  PRINTF("Radio wake error %d\n",radio_state);
356  }
357  break;
358  case SEND_WAKE:
359  /* 3290p requests return message showing awake status */
360  send_frame(REPORT_WAKE, 0, 0);
361  break;
362  default:
363  break;
364  }
365  /* Reset command done flag. */
366  cmd.done = 0;
367  }
368  break;
369  default:
370  break;
371  }
372  return 0;
373 }
374 
375 /*---------------------------------------------------------------------------*/
376 /* Process an input character from serial port.
377  * ** This is called from an ISR!!
378 */
379 
380 int raven_lcd_serial_input(unsigned char ch)
381 {
382  /* Tell sleep routine if a reception occurred */
383  /* Random nulls occur for some reason, so ignore those */
384  if (ch) serial_char_received++;
385 #if DEBUGSERIAL
386  if (serialcount<25) dbuf[serialcount]=ch;
387  serialcount++;
388 #endif
389  /* Don't overwrite an unprocessed command */
390 // if (cmd.done) return 0;
391 
392  /* Parse frame, */
393  switch (cmd.ndx){
394  case 0:
395  /* first byte, must be 0x01 */
396  if (ch == 0x01){
397 // cmd.done = false;
398  } else {
399 #if DEBUGSERIAL
400  dbuf[25]++;
401 #endif
402  return 0;
403  }
404  break;
405  case 1:
406  /* Second byte, length of payload */
407  cmd.len = ch;
408  break;
409  case 2:
410  /* Third byte, command byte */
411  cmd.cmd = ch;
412  break;
413  default:
414  /* Payload and ETX */
415  if (cmd.ndx >= (MAX_CMD_LEN+3)) { //buffer overflow!
416  cmd.ndx=0;
417 #if DEBUGSERIAL
418  dbuf[26]++;
419 #endif
420  return 0;
421  }
422  if (cmd.ndx >= cmd.len+3){
423  /* all done, check ETX */
424  if (ch == 0x04){
425  cmd.done = 1;
426 #if DEBUGSERIAL
427  dbuf[27]++;
428 #endif
429  process_post(&raven_lcd_process, SERIAL_CMD, 0);
430  } else {
431  /* Failed ETX */
432 #if DEBUGSERIAL
433  dbuf[28]++;
434 #endif
435  }
436  cmd.ndx=0; //set up for next command
437  return 0;
438  } else {
439  /* Just grab and store payload */
440  cmd.frame[cmd.ndx - 3] = ch;
441  }
442  break;
443  }
444 
445  cmd.ndx++;
446  return 0;
447 }
448 
449 /*---------------------------------------------------------------------------*/
450 void
451 raven_lcd_show_text(char *text) {
452  uint8_t textlen=strlen(text)+1;
453  if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
454  send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
455 }
456 
457 #if WEBSERVER
458 static void
459 lcd_show_servername(void) {
460 
461 //extern uint8_t mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
462 extern uint8_t server_name[16];
463 //extern uint8_t domain_name[30];
464 char buf[sizeof(server_name)+1];
465  eeprom_read_block (buf,server_name, sizeof(server_name));
466  buf[sizeof(server_name)]=0;
467  raven_lcd_show_text(buf); //must fit in all the buffers or it will be truncated!
468 }
469 #endif
470 /*---------------------------------------------------------------------------*/
471 PROCESS(raven_lcd_process, "Raven LCD interface process");
472 PROCESS_THREAD(raven_lcd_process, ev, data)
473 {
474 
475  PROCESS_BEGIN();
476 
477 #if WEBSERVER
478  lcd_show_servername();
479 #endif
480 
481  /* Get ICMP6 callbacks from uip6 stack, perform 3290p action on pings, responses, etc. */
482  if(icmp6_new(NULL) == 0) {
483 
484  while(1) {
485  PROCESS_YIELD();
486 // if (ev != ?) //trap frequent strobes?
487  raven_gui_loop(ev, data);
488  }
489  }
490  PROCESS_END();
491 }
492 
493 /** @} */