Contiki 2.5
uart.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in
12  * the documentation and/or other materials provided with the
13  * distribution.
14  * * Neither the name of the copyright holders nor the names of
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /**
31  * \file
32  *
33  * \brief
34  * Handles the control of the USART for communication with the ATmega1284p
35  * for sending commands.
36  *
37  * \author
38  * Mike Vidales mavida404@gmail.com
39  *
40  */
41 
42 #include "uart.h"
43 #include "lcd.h"
44 #include "main.h"
45 #include "menu.h"
46 #include "beep.h"
47 
48 /**
49  * \addtogroup lcd
50  * \{
51 */
52 
53 #define TIMEOUT (0xff)
54 
55 /** \brief The RX circular buffer, for storing characters from serial port. */
57 
58 /*---------------------------------------------------------------------------*/
59 
60 /**
61  * \brief This will intialize the circular buffer head and tail of tcirc_buf struct.
62  *
63  * \param cbuf Pointer to buffer to initialize.
64 */
65 void
67 {
68  cbuf->head = cbuf->tail = 0;
69 }
70 
71 /*---------------------------------------------------------------------------*/
72 
73 /**
74  * \brief This will add a new character to the circular buffer.
75  *
76  * \param cbuf Pointer to buffer where character will be stored.
77  * \param ch Character to store into buffer.
78 */
79 void
80 uart_add_to_circ_buf(tcirc_buf *cbuf, uint8_t ch)
81 {
82  /* Add char to buffer */
83  uint8_t newhead = cbuf->head;
84  newhead++;
85  if (newhead >= BUFSIZE){
86  newhead = 0;
87  }
88  if (newhead == cbuf->tail){
89  /* Buffer full, quit it */
90  return;
91  }
92 
93  cbuf->buf[cbuf->head] = ch;
94  cbuf->head = newhead;
95 }
96 
97 /*---------------------------------------------------------------------------*/
98 
99 /**
100  * \brief This will get a character from the buffer requested.
101  *
102  * \param cbuf Pointer to buffer to get character from.
103  *
104  * \return retval Return character from buffer.
105 */
106 uint8_t
108 {
109  /* Get char from buffer. */
110  /* Be sure to check first that there is a char in buffer. */
111  uint8_t newtail = cbuf->tail;
112  uint8_t retval = cbuf->buf[newtail];
113 
114  newtail++;
115  if (newtail >= BUFSIZE){
116  /* Rollover */
117  newtail = 0;
118  }
119  cbuf->tail = newtail;
120 
121  return retval;
122 }
123 
124 /*---------------------------------------------------------------------------*/
125 
126 /**
127  * \brief This will clear the RX buffer.
128 */
129 void
131 {
132  rxbuf.tail = rxbuf.head = 0;
133 }
134 
135 /**
136  * \brief This will check for a character in the requested buffer.
137  *
138  * \param cbuf Pointer to buffer to check for any characters.
139  *
140  * \return True if buffer empty.
141 */
142 uint8_t
144 {
145  /* Return true if buffer empty */
146  return (cbuf->head != cbuf->tail);
147 }
148 
149 /*---------------------------------------------------------------------------*/
150 
151 /**
152  * \brief This will convert a nibble to a hex value.
153  *
154  * \param val Value to convert to hex.
155  *
156  * \return val Converted hex value
157 */
158 uint8_t
159 uip_ntohex(uint8_t val)
160 {
161  /* Convert nibble to hex */
162  if (val > 9){
163  return val + 'A' - 10;
164  }
165  else{
166  return val + '0';
167  }
168 }
169 
170 /*---------------------------------------------------------------------------*/
171 
172 /**
173  * \brief Convert integer to hex value.
174  *
175  * \param val Value to convert to hex.
176  * \param str Location to store converted value.
177 */
178 void
179 itohex(uint8_t val,char *str)
180 {
181  *str++ = uip_ntohex(val >> 8);
182  *str = uip_ntohex(val & 0x0f);
183 }
184 
185 /*---------------------------------------------------------------------------*/
186 
187 /**
188  * \brief This will wait for a new character from the ATmega1284p and timeout
189  * if no new character is received.
190  *
191  * \retval TIMEOUT Returns if timeout has occured.
192  * \return retval Character returned upon seeing rx_char_ready()
193 */
194 uint8_t
196 {
197  /* Gets a serial char, and waits for timeout */
198  uint32_t timex = 5000000;
199  uint8_t retval;
200 
201  while (!rx_char_ready()){
202  if (!timex--){
203  /* Timeout, return timeout */
204  return TIMEOUT;
205  }
206  }
207 
208  retval = uart_get_from_circ_buf(&rxbuf);
209  return retval;
210 }
211 
212 /*---------------------------------------------------------------------------*/
213 
214 /**
215  * \brief Initialize UART to 38400 Baud Rate and only
216  * enable UART for transmission.
217 */
218 void
219 uart_init(void)
220 {
221  /* For Mega3290P, enable the uart peripheral */
222  PRR &= ~(1 << PRUSART0);
223 
225  /* 38400 baud @ 8 MHz internal RC oscillator (error = 0.2%) */
226  UBRR0 = BAUD_RATE_38400;
227 
228  /* 8 bit character size, 1 stop bit and no parity mode */
229  UCSR0C = ( 3 << UCSZ00);
230 
231  /* Enable RX,TX and RX interrupt on USART */
232  UCSR0B = (1 << RXEN0)|(1 << TXEN0)|(1 << RXCIE0);
233 }
234 
235 /*---------------------------------------------------------------------------*/
236 
237 /**
238  * \brief Turn off UART for sleep mode.
239 */
240 void
242 {
243  /* Disable RX,TX and RX interrupt on USART */
244  UCSR0B = 0;
245 
246  /* for Mega3290P, disable the uart peripheral */
247  PRR |= (1 << PRUSART0);
248 }
249 
250 /*---------------------------------------------------------------------------*/
251 
252 /**
253  * \brief Send one byte over the uart. This is called to send binary commands.
254  *
255  * \param byte The byte of data to send out the uart.
256 */
257 void
258 uart_send_byte(uint8_t byte)
259 {
260  /* Wait for last char to be gone... */
261  while(!(UCSR0A & (1 << UDRE0)))
262  ;
263  UDR0 = byte;
264 
265  /* Clear the TXC bit to allow transmit complete test before sleep*/
266  UCSR0A |=(1 << TXC0);
267 }
268 
269 /*---------------------------------------------------------------------------*/
270 
271 /**
272  * \brief This is the USART RX complete interrupt.
273 */
274 ISR
275 (USART_RX_vect)
276 {
277  /* Get byte from serial port, put in Rx Buffer. */
278  uint8_t retval;
279 
280  retval = UDR0;
281  uart_add_to_circ_buf(&rxbuf, retval);
282 }
283 
284 /*---------------------------------------------------------------------------*/
285 
286 /**
287  * \brief This function builds and sends a binary command frame to the
288  * ATmega1284p.
289  *
290  * \param cmd Command to send.
291  * \param payload_length Length of data to be sent with command.
292  * \param payload Pointer to data to send.
293 */
294 void
295 uart_serial_send_frame(uint8_t cmd, uint8_t payload_length, uint8_t *payload)
296 {
297  /* Send a frame to 1284p */
298  int8_t i;
299 
301  uart_send_byte(payload_length);
302  uart_send_byte(cmd);
303  for (i=0;i<=payload_length-1;i++){
304  uart_send_byte(payload[i]);
305  }
307 }
308 
309 /*---------------------------------------------------------------------------*/
310 
311 /**
312  * \brief This displays a time out message to the user based on the parameter
313  * reason x.
314  *
315  * \param x Reason for USART time out.
316 */
317 void
319 {
320  char str[20] = "TO ";
321 
322  dectoascii(x, str+3);
323  lcd_puts(str);
324 }
325 
326 /*---------------------------------------------------------------------------*/
327 
328 /**
329  * \brief This will receive a frame from the ATmega1284p and parse the incoming
330  * data.
331  *
332  * If the incoming data is a binary command acknowledgement, then this will not
333  * parse any data. If the incoming data is test reports, the menu will store the
334  * data for end of test metrics.
335  *
336  * \param wait_for_ack Flag used to wait for acknowledgement when receving a serial
337  * frame.
338 */
339 void
340 uart_serial_rcv_frame(uint8_t wait_for_ack)
341 {
342  /* Gets a serial frame, if any, and displays data appropriately */
343  /* If wait_for_ack is true, this funtion will wait for serial chars. */
344  volatile uint8_t ch;
345  volatile uint8_t length;
346  volatile uint8_t cmd;
347  volatile uint8_t payload[20];
348  uint16_t i;
349 
350  if (!wait_for_ack && !rx_char_ready()){
351  return;
352  }
353 
354  /* Check for SOF */
355  ch = uart_get_char_rx();
356  if (ch != SOF_CHAR){
357  return uart_timeout_msg(1);
358  }
359 
360  /* Turn on nose LED for activity indicator */
361  led_on();
362 
363  /* Get length byte */
364  ch = uart_get_char_rx();
365  if (ch == TIMEOUT){
366  return uart_timeout_msg(2);
367  }
368  /* Check for ACK Frame */
369  if (ch >= 0x80){
370  /* This is an ack frame, just get it and go away. */
371  ch = uart_get_char_rx();
372  if (ch != EOF_CHAR){
373  uart_timeout_msg(3);
374  }
375  led_off();
376  return;
377  }
378 
379  length = ch;
380  if (length > sizeof(payload)){
381  /* invalid length */
382  return;
383  }
384 
385  /* Get cmd byte */
386  ch = uart_get_char_rx();
387  if (ch == TIMEOUT){
388  return uart_timeout_msg(5);
389  }
390  cmd = ch;
391 
392  /* Get payload */
393  for (i=0;i<length;i++){
394  ch = uart_get_char_rx();
395  if (ch == TIMEOUT){
396  return uart_timeout_msg(i);
397  }
398  /* Save payload */
399  payload[i] = ch;
400  }
401 
402  /* Get EOF */
403  ch = uart_get_char_rx();
404  if (ch != EOF_CHAR){
405  return uart_timeout_msg(7);
406  }
407 
408  /* Process the received command */
409  switch (cmd){
410  case REPORT_PING:
411  /*
412  * This will update the lcd with the current ping status.
413  * Store the sequence number away.
414  */
415  ping_response = payload[0];
416 
417  if(ping_response == 1){
418  lcd_single_print_dig(ping_response, 3);
419  }
420  else if(ping_response == 2){
421  lcd_single_print_dig(ping_response, 2);
422  }
423  else if(ping_response == 3){
424  lcd_single_print_dig(ping_response, 1);
425  }
426  else if(ping_response == 4){
427  lcd_single_print_dig(ping_response, 0);
428  }
429 
430  timeout_flag = false;
431 
432  /* Beep on successful ping response. */
433  lcd_symbol_set(LCD_SYMBOL_BELL);
434  beep();
435  lcd_symbol_clr(LCD_SYMBOL_BELL);
436  break;
437  case REPORT_TEXT_MSG:
438  /* Copy text message to menu buffer and play ringtone */
439  /* Prezero in case no string terminator in command */
440  for (i=0;i<sizeof(top_menu_text);i++) top_menu_text[i]=0;
441  memcpy(&top_menu_text,(char *)payload,sizeof(top_menu_text)-1); //leave zero byte at end
442  play_ringtone();
443  break;
444  case REPORT_PING_BEEP:
445  lcd_symbol_set(LCD_SYMBOL_BELL);
446  beep();
447  lcd_symbol_clr(LCD_SYMBOL_BELL);
448  break;
449  case REPORT_WAKE:
450  /* Indicates 1284 is awake*/
451  break;
452  default:
453  break;
454  }
455  led_off();
456 }
457 
458 /** \} */