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