Contiki 3.x
serial-timeout.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 #include "dev/serial-timeout.h"
33 #include "dev/protobuf-handler.h"
34 #include <string.h> /* for memcpy() */
35 #include <stdio.h>
36 #include "lib/ringbuf.h"
37 #include "contiki.h"
38 #include "contiki-conf.h"
39 
40 //#define SERIAL_TIMEOUT_DEBUG
41 
42 #ifdef SERIAL_TIMEOUT_DEBUG
43 #define PRINTF(...) printf(__VA_ARGS__)
44 #else
45 #define PRINTF(...) do {} while (0)
46 #endif
47 
48 
49 #ifdef SERIAL_TIMEOUT_CONF_BUFSIZE
50 #define SER_BUFSIZE SERIAL_TIMEOUT_CONF_BUFSIZE
51 #else /* SERIAL_LINE_CONF_BUFSIZE */
52 #define SER_BUFSIZE 128
53 #endif /* SERIAL_LINE_CONF_BUFSIZE */
54 
55 #if (SER_BUFSIZE & (SER_BUFSIZE - 1)) != 0
56 #error SERIAL_TIMEOUT_CONF_BUFSIZE must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
57 #error Change SERIAL_TIMEOUT_CONF_BUFSIZE in contiki-conf.h.
58 #endif
59 
60 
61 static uint8_t rxbuf_data[SER_BUFSIZE];
62 static rtimer_clock_t ser_timer;
63 static uint8_t rxbytes;
64 
65 PROCESS(serial_timeout_process, "Serial timeout driver");
66 
67 extern process_event_t serial_timeout_event_message;
68 
69 /*---------------------------------------------------------------------------*/
70 int
71 serial_timeout_input_byte(unsigned char c)
72 {
73  ser_timer = RTIMER_NOW(); /*Reset the timeout timer */
74 // PRINTF("Byte recieved...");
75  if(rxbytes ==0){
76  /*This was the first in a potential batch */
77 // PRINTF("first\n");
78  rxbuf_data[rxbytes++] = c; /* Store byte in buffer */
79  }else{ /*We're still in the timeout period from another byte */
80 // PRINTF("not first\n");
81  if(rxbytes >= SER_BUFSIZE){
82  /* Overflow */
83 // PRINTF("OVERFLOW\n");
84  //TODO handle this
85  return 0;
86  }else{
87 // PRINTF("Storing rxbytes in %i\n", rxbytes);
88  rxbuf_data[rxbytes++] = c;
89 
90  }
91  }
92 
93  /* Wake up consumer process */
94  if(rxbytes > 0){
95  process_poll(&serial_timeout_process);
96  return 1;
97  }
98  return 2;
99 }
100 /*---------------------------------------------------------------------------*/
101 PROCESS_THREAD(serial_timeout_process, ev, data)
102 {
103  PROCESS_BEGIN();
104  printf("Serial timeout process started\n");
105  static uint8_t buf[SER_BUFSIZE];
106  static uint8_t bytes;
107  while (1){
108  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
109  if(rxbytes == 0){
110  /* Stops spurios polls that seem to come in for no known reason */
111  PRINTF("Rx bytes = 0\n");
112  continue;
113  }
114 
115  while (RTIMER_CLOCK_LT(RTIMER_NOW(), (ser_timer + SERIAL_TIMEOUT_VALUE)));
116  if (rxbytes > SER_BUFSIZE){
117  PRINTF("Serial recieve overflow");
118  }else{
119 #ifdef SERIAL_TIMEOUT_DEBUG
120  printf("Timeout reached\n");
121  printf("Recieved Bytes: %i\n", rxbytes);
122  int i = 0;
123  while (i < SER_BUFSIZE){
124  printf("%i:", (int)rxbuf_data[i++]);
125  }
126  printf("\n");
127 #endif
128  memcpy( buf, rxbuf_data, rxbytes);
129  bytes = rxbytes;
130  rxbytes = 0;
131  memset(rxbuf_data, 0, SER_BUFSIZE); /*Reset buffer*/
132  if (bytes != 0){
133  PRINTF("CALLING PROTOBUG\n");
134  protobuf_process_message(buf, bytes);
135  }
136  }
137  }
138  PROCESS_END();
139 }
140 /*---------------------------------------------------------------------------*/
141 void
142 serial_timeout_init(void)
143 {
144  rxbytes = 0; /*Intially no bytes recieved */
145  memset(rxbuf_data, 0, SER_BUFSIZE); /*Set buffer to 0 */
146  process_start(&serial_timeout_process, NULL);
147 }
148 /*---------------------------------------------------------------------------*/
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define RTIMER_NOW()
Get the current clock time.
Definition: rtimer.h:135
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
int serial_timeout_input_byte(unsigned char c)
Get one byte of input from the serial driver.
#define NULL
The null pointer.
process_event_t serial_timeout_event_message
Event posted when a timeout has expired after the end of data recieving.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
Generic serial I/O process header filer waits for timeout.
Header file for the ring buffer library
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120