Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

tonokip

By: a guest on May 9th, 2010  |  syntax: None  |  size: 4.34 KB  |  hits: 221  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Modified /arduino-0017/hardware/cores/sanguino/wiring_serial.c
  2. /*
  3.   wiring_serial.c - serial functions.
  4.   Part of Arduino - http://www.arduino.cc/
  5.  
  6.   Copyright (c) 2005-2006 David A. Mellis
  7.   Modified 29 January 2009, Marius Kintel for Sanguino - http://www.sanguino.cc/
  8.  
  9.   This library is free software; you can redistribute it and/or
  10.   modify it under the terms of the GNU Lesser General Public
  11.   License as published by the Free Software Foundation; either
  12.   version 2.1 of the License, or (at your option) any later version.
  13.  
  14.   This library is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   Lesser General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU Lesser General
  20.   Public License along with this library; if not, write to the
  21.   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  22.   Boston, MA  02111-1307  USA
  23.  
  24.   $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  25. */
  26.  
  27.  
  28. #include "wiring_private.h"
  29.  
  30. // Define constants and variables for buffering incoming serial data.  We're
  31. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  32. // location to which to write the next incoming character and rx_buffer_tail
  33. // is the index of the location from which to read.
  34. #define RX_BUFFER_SIZE 128
  35. #define RX_BUFFER_MASK (RX_BUFFER_SIZE - 1)
  36.  
  37. #if defined(__AVR_ATmega644P__)
  38. unsigned char rx_buffer[2][RX_BUFFER_SIZE];
  39. unsigned char rx_buffer_head[2] = {0, 0};
  40. unsigned char rx_buffer_tail[2] = {0, 0};
  41. #else
  42. unsigned char rx_buffer[1][RX_BUFFER_SIZE];
  43. unsigned char rx_buffer_head[1] = {0};
  44. unsigned char rx_buffer_tail[1] = {0};
  45. #endif
  46.  
  47.  
  48. #define BEGIN_SERIAL(uart_, baud_) \
  49. { \
  50.     UBRR##uart_##H = ((F_CPU / 16 + baud / 2) / baud - 1) >> 8; \
  51.     UBRR##uart_##L = ((F_CPU / 16 + baud / 2) / baud - 1); \
  52.     \
  53.     /* reset config for UART */ \
  54.     UCSR##uart_##A = 0; \
  55.     UCSR##uart_##B = 0; \
  56.     UCSR##uart_##C = 0; \
  57.     \
  58.     /* enable rx and tx */ \
  59.     sbi(UCSR##uart_##B, RXEN##uart_);\
  60.     sbi(UCSR##uart_##B, TXEN##uart_);\
  61.     \
  62.     /* enable interrupt on complete reception of a byte */ \
  63.     sbi(UCSR##uart_##B, RXCIE##uart_); \
  64.     UCSR##uart_##C = _BV(UCSZ##uart_##1)|_BV(UCSZ##uart_##0); \
  65.     /* defaults to 8-bit, no parity, 1 stop bit */ \
  66. }
  67.  
  68. void beginSerial(uint8_t uart, long baud)
  69. {
  70.   if (uart == 0) BEGIN_SERIAL(0, baud)
  71. #if defined(__AVR_ATmega644P__)
  72.   else BEGIN_SERIAL(1, baud)
  73. #endif
  74. }
  75.  
  76. #define SERIAL_WRITE(uart_, c_) \
  77.     while (!(UCSR##uart_##A & (1 << UDRE##uart_))) \
  78.       ; \
  79.     UDR##uart_ = c
  80.  
  81. void serialWrite(uint8_t uart, unsigned char c)
  82. {
  83.   if (uart == 0) {
  84.     SERIAL_WRITE(0, c);
  85.   }
  86. #if defined(__AVR_ATmega644P__)
  87.   else {
  88.     SERIAL_WRITE(1, c);
  89.   }
  90. #endif
  91. }
  92.  
  93. int serialAvailable(uint8_t uart)
  94. {
  95.   return (RX_BUFFER_SIZE + rx_buffer_head[uart] - rx_buffer_tail[uart]) & RX_BUFFER_MASK;
  96. }
  97.  
  98. int serialRead(uint8_t uart)
  99. {
  100.   // if the head isn't ahead of the tail, we don't have any characters
  101.   if (rx_buffer_head[uart] == rx_buffer_tail[uart]) {
  102.     return -1;
  103.   } else {
  104.     unsigned char c = rx_buffer[uart][rx_buffer_tail[uart]];
  105.     rx_buffer_tail[uart] = (rx_buffer_tail[uart] + 1) & RX_BUFFER_MASK;
  106.     return c;
  107.   }
  108. }
  109.  
  110. void serialFlush(uint8_t uart)
  111. {
  112.   // don't reverse this or there may be problems if the RX interrupt
  113.   // occurs after reading the value of rx_buffer_head but before writing
  114.   // the value to rx_buffer_tail; the previous value of rx_buffer_head
  115.   // may be written to rx_buffer_tail, making it appear as if the buffer
  116.   // were full, not empty.
  117.   rx_buffer_head[uart] = rx_buffer_tail[uart];
  118. }
  119.  
  120. #define UART_ISR(uart_) \
  121. ISR(USART##uart_##_RX_vect) \
  122. { \
  123.   unsigned char c = UDR##uart_; \
  124.   \
  125.   int i = (rx_buffer_head[uart_] + 1) & RX_BUFFER_MASK; \
  126.   \  
  127.   /* if we should be storing the received character into the location \
  128.      just before the tail (meaning that the head would advance to the \
  129.      current location of the tail), we're about to overflow the buffer \
  130.      and so we don't write the character or advance the head. */ \
  131.   if (i != rx_buffer_tail[uart_]) { \
  132.     rx_buffer[uart_][rx_buffer_head[uart_]] = c; \
  133.     rx_buffer_head[uart_] = i; \
  134.   } \
  135. }
  136.  
  137. UART_ISR(0)
  138. #if defined(__AVR_ATmega644P__)
  139. UART_ISR(1)
  140. #endif