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 
66 #if AVR_WEBSERVER
67 #include "webserver-nogui.h"
68 #include "httpd-cgi.h"
69 #endif
70 
71 #include "raven-lcd.h"
72 
73 #include <string.h>
74 #include <stdio.h>
75 #include <avr/pgmspace.h>
76 #include <avr/eeprom.h>
77 #include <avr/sleep.h>
78 #include <dev/watchdog.h>
79 
80 static uint8_t count = 0;
81 static uint8_t seqno;
82 uip_ipaddr_t dest_addr;
83 
84 #define MAX_CMD_LEN 20
85 static struct{
86  uint8_t frame[MAX_CMD_LEN];
87  uint8_t ndx;
88  uint8_t len;
89  uint8_t cmd;
90  uint8_t done;
91 } cmd;
92 
93 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
94 #define UIP_ICMP_BUF ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
95 #define PING6_DATALEN 16
96 
97 void rs232_send(uint8_t port, unsigned char c);
98 
99 /*---------------------------------------------------------------------------*/
100 /* Sends a ping packet out the radio */
101 /* Useful for debugging so allow external calls */
102 void
103 raven_ping6(void)
104 {
105 #define PING_GOOGLE 0
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 
148 /*---------------------------------------------------------------------------*/
149 /* Send a serial command frame to the ATMega3290 Processsor on Raven via serial port */
150 static void
151 send_frame(uint8_t cmd, uint8_t len, uint8_t *payload)
152 {
153  uint8_t i;
154 
155  rs232_send(0, SOF_CHAR); /* Start of Frame */
156  rs232_send(0, len);
157  rs232_send(0, cmd);
158  for (i=0;i<len;i++)
159  rs232_send(0,*payload++);
160  rs232_send(0, EOF_CHAR);
161 }
162 char serial_char_received;
163 /*---------------------------------------------------------------------------*/
164 /* Sleep for howlong seconds, or until UART interrupt if howlong==0.
165  * Uses TIMER2 with external 32768 Hz crystal to sleep in 1 second multiples.
166  * TIMER2 may have already been set up for CLOCK_CONF_SECOND ticks/second in clock.c
167 
168  *
169  * Until someone figures out how to get UART to wake from powerdown,
170  * a three second powersave cycle is used with exit based on any character received.
171 
172  * The system clock is adjusted to reflect the sleep time.
173  */
174 
175 void micro_sleep(uint8_t howlong)
176 {
177  uint8_t saved_sreg = SREG, saved_howlong = howlong;
178 #if AVR_CONF_USE32KCRYSTAL
179 /* Save TIMER2 configuration if clock.c is using it */
180  uint8_t savedTCNT2=TCNT2, savedTCCR2A=TCCR2A, savedTCCR2B = TCCR2B, savedOCR2A = OCR2A;
181 #endif
182 
183 // if (howlong==0) {
184 // set_sleep_mode(SLEEP_MODE_PWR_DOWN); // UART can't wake from powerdown
185 // } else {
186  set_sleep_mode(SLEEP_MODE_PWR_SAVE); // Sleep for howlong seconds
187  if (howlong==0) howlong=3; // 3*32/(32768/1024) = 3 second sleep cycle if not specified
188  cli(); // Disable interrupts for the present
189  ASSR |= (1 << AS2); // Set TIMER2 asyncronous from external crystal
190  TCCR2A =(1<<WGM21); // CTC mode
191  TCCR2B =((1<<CS22)|(1<<CS21)|(1<<CS20));// Prescale by 1024 = 32 ticks/sec
192 // while(ASSR & (1 << TCR2BUB)); // Wait for TCNT2 write to finish.
193  OCR2A = howlong*32; // Set TIMER2 output compare register
194 // while(ASSR & (1 << OCR2AUB)); // Wait for OCR2 write to finish.
195  SREG = saved_sreg; // Restore interrupt state.
196 // UCSR(USART,B)&= ~(1<<RXCIE(USART)) // Disable the RX Complete interrupt;
197 // UCSR0B|=(1<<RXCIE0); // Enable UART0 RX complete interrupt
198 // UCSR1B|=(1<<RXCIE1); // Enable UART1 RX complete interrupt
199 // TCNT2 = 0; // Reset TIMER2 timer counter value.
200 // while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to finish before entering sleep.
201 // TIMSK2 |= (1 << OCIE2A); // Enable TIMER2 output compare interrupt.
202 // }
203 
204  TCNT2 = 0; // Reset timer
205  watchdog_stop(); // Silence annoying distractions
206  while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to (which assures TCCR2x and OCR2A are finished!)
207  TIMSK2 |= (1 << OCIE2A); // Enable TIMER2 output compare interrupt
208  SMCR |= (1 << SE); // Enable sleep mode.
209  while (1) {
210 // TCNT2 = 0; // Cleared automatically in CTC mode
211 // while(ASSR & (1 << TCN2UB)); // Wait for TCNT2 write to finish before entering sleep.
212  serial_char_received=0; // Set when chars received by UART
213  sleep_mode(); // Sleep
214 
215  /* Adjust clock.c for the time spent sleeping */
216  clock_adjust_ticks(howlong * CLOCK_SECOND);
217 
218 // if (TIMSK2&(1<<OCIE2A)) break; // Exit sleep if not awakened by TIMER2
219  PRINTF(".");
220  if (saved_howlong) break; // Exit sleep if nonzero time specified
221 // PRINTF("%d",serial_char_received);
222  if (serial_char_received) break;
223  }
224 
225  SMCR &= ~(1 << SE); //Disable sleep mode after wakeup
226 
227 #if AVR_CONF_USE32KCRYSTAL
228 /* Restore clock.c configuration */
229 // OCRSetup();
230  cli();
231  TCCR2A = savedTCCR2A;
232  TCCR2B = savedTCCR2B;
233  OCR2A = savedOCR2A;
234  TCNT2 = savedTCNT2;
235  sei();
236 #else
237  TIMSK2 &= ~(1 << OCIE2A); //Disable TIMER2 interrupt
238 #endif
239 
240  watchdog_start();
241 }
242 #if !AVR_CONF_USE32KCRYSTAL
243 /*---------------------------------------------------------------------------*/
244 /* TIMER2 Interrupt service */
245 
246 ISR(TIMER2_COMPA_vect)
247 {
248 // TIMSK2 &= ~(1 << OCIE2A); //Just one interrupt needed for waking
249 }
250 #endif /* !AVR_CONF_USE32KCRYSTAL */
251 
252 #if DEBUGSERIAL
253 uint8_t serialcount;
254 char dbuf[30];
255 #endif
256 
257 /*---------------------------------------------------------------------------*/
258 static uint8_t
259 raven_gui_loop(process_event_t ev, process_data_t data)
260 {
261  uint8_t i,activeconnections,radio_state;
262 
263 // PRINTF("\nevent %d ",ev);
264 #if DEBUGSERIAL
265  printf_P(PSTR("Buffer [%d]="),serialcount);
266  serialcount=0;
267  for (i=0;i<30;i++) {
268  printf_P(PSTR(" %d"),dbuf[i]);
269  dbuf[i]=0;
270  }
271 #endif
272  if(ev == tcpip_icmp6_event) switch(*((uint8_t *)data)) {
273 
274 // case ICMP6_NS:
275  /*Tell the 3290 we are being solicited. */
276 // send_frame(REPORT_NS,...);
277 // break; //fall through for beep
278 // case ICMP6_RA:
279  /*Tell the 3290 we have a router. */
280 // send_frame(REPORT_NA,...);
281 // break; //fall through for beep
282  case ICMP6_ECHO_REQUEST:
283  /* We have received a ping request over the air. Tell the 3290 */
284  send_frame(REPORT_PING_BEEP, 0, 0);
285  break;
286  case ICMP6_ECHO_REPLY:
287  /* We have received a ping reply over the air. Send frame back to 3290 */
288  send_frame(REPORT_PING, 1, &seqno);
289  break;
290 
291  } else switch (ev) {
292  case SERIAL_CMD:
293  /* Check for command from serial port, execute it. */
294  /* Note cmd frame is written in an interrupt - delays here can cause overwriting by next command */
295  PRINTF("\nCommand %d length %d done %d",cmd.cmd,cmd.len,cmd.done);
296  if (cmd.done){
297  /* Execute the waiting command */
298  switch (cmd.cmd){
299  case SEND_PING:
300  /* Send ping request over the air */
301  seqno = cmd.frame[0];
302  raven_ping6();
303  break;
304  case SEND_TEMP:
305 #if AVR_WEBSERVER
306  /* Set temperature string in web server */
307  web_set_temp((char *)cmd.frame);
308 #endif
309  break;
310  case SEND_ADC2:
311 #if AVR_WEBSERVER
312  /* Set ext voltage string in web server */
313  web_set_voltage((char *)cmd.frame);
314 #endif
315  break;
316  case SEND_SLEEP:
317  /* Sleep radio and 1284p. */
318  if (cmd.frame[0]==0) { //Time to sleep in seconds
319  /* Unconditional sleep. Don't wake until a serial interrupt. */
320  } else {
321  /* Sleep specified number of seconds (3290p "DOZE" mode) */
322  /* It sleeps a bit longer so we will be always be awake for the next sleep command. */
323 #if UIP_CONF_TCP
324  /* Only sleep this cycle if no active TCP/IP connections, for fast browser responsed */
325  activeconnections=0;
326  for(i = 0; i < UIP_CONNS; ++i) {
327  if((uip_conns[i].tcpstateflags & UIP_TS_MASK) != UIP_CLOSED) activeconnections++;
328  }
329  if (activeconnections) {
330  PRINTF("\nWaiting for %d connections",activeconnections);
331  break;
332  }
333 #endif
334  }
335  radio_state = NETSTACK_RADIO.off();
336  PRINTF ("\nsleep %d radio state %d...",cmd.frame[0],radio_state);
337 
338  /*Sleep for specified time*/
339  PRINTF("\nSleeping...");
340  micro_sleep(cmd.frame[0]);
341 
342  radio_state = NETSTACK_RADIO.on();
343  if (radio_state > 0) {
344  PRINTF("Awake!");
345  } else {
346  PRINTF("Radio wake error %d\n",radio_state);
347  }
348  break;
349  case SEND_WAKE:
350  /* 3290p requests return message showing awake status */
351  send_frame(REPORT_WAKE, 0, 0);
352  break;
353  default:
354  break;
355  }
356  /* Reset command done flag. */
357  cmd.done = 0;
358  }
359  break;
360  default:
361  break;
362  }
363  return 0;
364 }
365 
366 /*---------------------------------------------------------------------------*/
367 /* Process an input character from serial port.
368  * ** This is called from an ISR!!
369 */
370 
371 int raven_lcd_serial_input(unsigned char ch)
372 {
373  /* Tell sleep routine if a reception occurred */
374  /* Random nulls occur for some reason, so ignore those */
375  if (ch) serial_char_received++;
376 #if DEBUGSERIAL
377  if (serialcount<25) dbuf[serialcount]=ch;
378  serialcount++;
379 #endif
380  /* Don't overwrite an unprocessed command */
381 // if (cmd.done) return 0;
382 
383  /* Parse frame, */
384  switch (cmd.ndx){
385  case 0:
386  /* first byte, must be 0x01 */
387  if (ch == 0x01){
388 // cmd.done = false;
389  } else {
390 #if DEBUGSERIAL
391  dbuf[25]++;
392 #endif
393  return 0;
394  }
395  break;
396  case 1:
397  /* Second byte, length of payload */
398  cmd.len = ch;
399  break;
400  case 2:
401  /* Third byte, command byte */
402  cmd.cmd = ch;
403  break;
404  default:
405  /* Payload and ETX */
406  if (cmd.ndx >= (MAX_CMD_LEN+3)) { //buffer overflow!
407  cmd.ndx=0;
408 #if DEBUGSERIAL
409  dbuf[26]++;
410 #endif
411  return 0;
412  }
413  if (cmd.ndx >= cmd.len+3){
414  /* all done, check ETX */
415  if (ch == 0x04){
416  cmd.done = 1;
417 #if DEBUGSERIAL
418  dbuf[27]++;
419 #endif
420  process_post(&raven_lcd_process, SERIAL_CMD, 0);
421  } else {
422  /* Failed ETX */
423 #if DEBUGSERIAL
424  dbuf[28]++;
425 #endif
426  }
427  cmd.ndx=0; //set up for next command
428  return 0;
429  } else {
430  /* Just grab and store payload */
431  cmd.frame[cmd.ndx - 3] = ch;
432  }
433  break;
434  }
435 
436  cmd.ndx++;
437  return 0;
438 }
439 
440 /*---------------------------------------------------------------------------*/
441 void
442 raven_lcd_show_text(char *text) {
443  uint8_t textlen=strlen(text)+1;
444  if (textlen > MAX_CMD_LEN) textlen=MAX_CMD_LEN;
445  send_frame(REPORT_TEXT_MSG, textlen, (uint8_t *) text);
446 }
447 
448 #if AVR_WEBSERVER
449 static void
450 lcd_show_servername(void) {
451 
452 //extern uint8_t eemem_mac_address[8]; //These are defined in httpd-fsdata.c via makefsdata.h
453 extern uint8_t eemem_server_name[16];
454 //extern uint8_t eemem_domain_name[30];
455 char buf[sizeof(eemem_server_name)+1];
456  eeprom_read_block (buf,eemem_server_name, sizeof(eemem_server_name));
457  buf[sizeof(eemem_server_name)]=0;
458  raven_lcd_show_text(buf); //must fit in all the buffers or it will be truncated!
459 }
460 #endif
461 /*---------------------------------------------------------------------------*/
462 PROCESS(raven_lcd_process, "Raven LCD interface process");
463 PROCESS_THREAD(raven_lcd_process, ev, data)
464 {
465 
466  PROCESS_BEGIN();
467 
468 #if AVR_WEBSERVER
469  lcd_show_servername();
470 #endif
471 
472  /* Get ICMP6 callbacks from uip6 stack, perform 3290p action on pings, responses, etc. */
473  if(icmp6_new(NULL) == 0) {
474 
475  while(1) {
476  PROCESS_YIELD();
477 // if (ev != ?) //trap frequent strobes?
478  raven_gui_loop(ev, data);
479  }
480  }
481  PROCESS_END();
482 }
483 
484 /** @} */
485 /** @} */
#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 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