Click here to Skip to main content
15,886,110 members
Articles / Internet of Things / Arduino

Arduino Unleashed

Rate me:
Please Sign up or sign in to vote.
4.90/5 (19 votes)
18 Sep 2010CPOL8 min read 171.2K   678   40  
This article is all about empowering you with more powerful tools that let you unleash your creativity without being limited by the default IDE.
  • arduino.zip
    • Arduino
      • .metadata
        • .lock
        • .log
        • .mylyn
          • contexts
          • repositories.xml.zip
        • .plugins
          • de.innot.avreclipse.core
          • org.eclipse.cdt.core
          • org.eclipse.cdt.make.core
          • org.eclipse.cdt.managedbuilder.core
          • org.eclipse.cdt.ui
          • org.eclipse.core.resources
            • .history
              • 0
                • 90178f64a8c1001f13c7f5de4bcffe65
              • 11
                • 90c8cb7aaec1001f1a968c8925716fac
              • 15
                • e003e79caec1001f1a968c8925716fac
              • 16
                • 309eaa2aaac1001f1a968c8925716fac
                • b00fc1e8a7c1001f13c7f5de4bcffe65
              • 17
                • 303f0b1aaac1001f1a968c8925716fac
              • 19
                • 50db879ba9c1001f13c7f5de4bcffe65
                • b0822d6baac1001f1a968c8925716fac
              • 1a
                • a0d01b72aec1001f1a968c8925716fac
              • 1d
                • 9006869ba9c1001f13c7f5de4bcffe65
              • 2
                • 40805c37b3c1001f1a968c8925716fac
              • 24
                • 908eddbfafc1001f1a968c8925716fac
              • 25
                • 60a2bb77aec1001f1a968c8925716fac
              • 26
                • 709c0a3eadc1001f1a968c8925716fac
              • 29
                • b0563bbbafc1001f1a968c8925716fac
              • 2a
                • 50b68fbfa7c1001f13c7f5de4bcffe65
              • 2f
                • 609fb36eaec1001f1a968c8925716fac
              • 3
                • 00b2433ea6c1001f13c7f5de4bcffe65
              • 30
                • e0862b6baac1001f1a968c8925716fac
              • 31
                • e0f5edd5a9c1001f13c7f5de4bcffe65
              • 32
                • 40a6c07eaac1001f1a968c8925716fac
              • 33
                • 7066e74ab3c1001f1a968c8925716fac
                • 80265848a7c1001f13c7f5de4bcffe65
              • 34
                • 00eb51d6adc1001f1a968c8925716fac
              • 35
                • d05a5b48a7c1001f13c7f5de4bcffe65
                • e046fe19aac1001f1a968c8925716fac
              • 36
                • 20ed7c6ba8c1001f13c7f5de4bcffe65
              • 37
                • f03b866ba8c1001f13c7f5de4bcffe65
              • 39
                • 507dde20a9c1001f13c7f5de4bcffe65
              • 3b
                • a0b1ce97aec1001f1a968c8925716fac
              • 3d
                • 0063ef74aec1001f1a968c8925716fac
              • 3e
                • e0191572aec1001f1a968c8925716fac
              • 42
                • 80903869aac1001f1a968c8925716fac
              • 45
                • 10c140a1aec1001f1a968c8925716fac
              • 47
                • 3073473caec1001f1a968c8925716fac
              • 48
                • c0b85d37b3c1001f1a968c8925716fac
              • 4f
                • 70572f6baac1001f1a968c8925716fac
              • 50
                • 30083988b3c1001f1a968c8925716fac
              • 51
                • d0723269a6c1001f13c7f5de4bcffe65
              • 52
                • 902b1441b0c1001f1a968c8925716fac
              • 54
                • d06a4b3caec1001f1a968c8925716fac
              • 56
                • 002c6d00aac1001f13c7f5de4bcffe65
                • b00a8fbdafc1001f1a968c8925716fac
                • b09bbdc9a7c1001f13c7f5de4bcffe65
              • 57
                • b0f54422aac1001f1a968c8925716fac
              • 58
                • d077b7b2afc1001f1a968c8925716fac
              • 5a
                • d05d819faec1001f1a968c8925716fac
              • 5b
                • b0f4b977aec1001f1a968c8925716fac
              • 5c
                • 007bc27eaac1001f1a968c8925716fac
              • 5d
                • 40e5c897aec1001f1a968c8925716fac
                • 608adfbfafc1001f1a968c8925716fac
                • d013899ba9c1001f13c7f5de4bcffe65
              • 5e
                • c0b1af2db3c1001f1a968c8925716fac
              • 5f
                • 3048ecd5a9c1001f13c7f5de4bcffe65
              • 61
                • 404b5cd6adc1001f1a968c8925716fac
              • 67
                • 20740441b0c1001f1a968c8925716fac
              • 68
                • 20aee081aec1001f1a968c8925716fac
              • 6a
                • f0ee1a37a9c1001f13c7f5de4bcffe65
              • 6b
                • d06be49caec1001f1a968c8925716fac
              • 6c
                • 10595034b0c1001f1a968c8925716fac
              • 7
                • d076c47eaac1001f1a968c8925716fac
              • 70
                • 10503ea1aec1001f1a968c8925716fac
                • 30b3ee79a7c1001f13c7f5de4bcffe65
                • e06d6369a6c1001f13c7f5de4bcffe65
              • 71
                • f06556d4a9c1001f13c7f5de4bcffe65
              • 72
                • 40a2011aaac1001f1a968c8925716fac
              • 74
                • 00145534b0c1001f1a968c8925716fac
                • 70add097aec1001f1a968c8925716fac
              • 77
                • 50a0ae2db3c1001f1a968c8925716fac
                • 70271c37a9c1001f13c7f5de4bcffe65
              • 79
                • 20b07f9faec1001f1a968c8925716fac
              • 7a
                • 400092bfa7c1001f13c7f5de4bcffe65
                • 8051a52db3c1001f1a968c8925716fac
              • 7b
                • 703298bdafc1001f1a968c8925716fac
              • 7d
                • 006f493caec1001f1a968c8925716fac
                • 0087c57aaec1001f1a968c8925716fac
                • f063093eadc1001f1a968c8925716fac
              • 7f
                • d033043eadc1001f1a968c8925716fac
              • 80
                • a038c7c9a7c1001f13c7f5de4bcffe65
                • f03e3e22aac1001f1a968c8925716fac
              • 81
                • 402ade9caec1001f1a968c8925716fac
                • 809b876ba8c1001f13c7f5de4bcffe65
              • 84
                • 30698b4ea8c1001f13c7f5de4bcffe65
              • 85
                • 90b7c839aec1001f1a968c8925716fac
              • 87
                • 2040ed74aec1001f1a968c8925716fac
              • 8a
                • 004b6169a6c1001f13c7f5de4bcffe65
                • 60a34622aac1001f1a968c8925716fac
              • 8c
                • 70f2e59caec1001f1a968c8925716fac
              • 8d
                • c02f1241b0c1001f1a968c8925716fac
              • 8f
                • a05b95bfa7c1001f13c7f5de4bcffe65
                • e082e281aec1001f1a968c8925716fac
              • 93
                • 602ce681aec1001f1a968c8925716fac
              • 98
                • 5096829faec1001f1a968c8925716fac
              • 99
                • 309f4822aac1001f1a968c8925716fac
                • b0b13c88b3c1001f1a968c8925716fac
              • a
                • 50b47d9faec1001f1a968c8925716fac
              • a4
                • f04b5937b3c1001f1a968c8925716fac
              • a7
                • b077a4e3a7c1001f13c7f5de4bcffe65
                • c015c5c9a7c1001f13c7f5de4bcffe65
              • aa
                • 70c9a82aaac1001f1a968c8925716fac
              • ab
                • 40411937a9c1001f13c7f5de4bcffe65
                • 60a56b00aac1001f13c7f5de4bcffe65
              • ad
                • 30ea3d88b3c1001f1a968c8925716fac
                • c0125bd6adc1001f1a968c8925716fac
              • ae
                • 9088be77aec1001f1a968c8925716fac
                • b0205b37b3c1001f1a968c8925716fac
              • af
                • d02d5234b0c1001f1a968c8925716fac
              • b
                • 007b4155aac1001f1a968c8925716fac
              • b0
                • 10ec9ae1a7c1001f13c7f5de4bcffe65
              • b2
                • 00a8ed4ab3c1001f1a968c8925716fac
              • b4
                • 6066b6b2afc1001f1a968c8925716fac
              • b5
                • 00caa2e3a7c1001f13c7f5de4bcffe65
              • b9
                • 70880d57aac1001f1a968c8925716fac
              • bb
                • 00e5fa3dadc1001f1a968c8925716fac
                • 10ec7f9ba9c1001f13c7f5de4bcffe65
              • bc
                • 0062bfe8a7c1001f13c7f5de4bcffe65
                • 2077bd77aec1001f1a968c8925716fac
              • c1
                • b04742a1aec1001f1a968c8925716fac
                • e0480641b0c1001f1a968c8925716fac
              • c2
                • 1052e020a9c1001f13c7f5de4bcffe65
                • d008d497aec1001f1a968c8925716fac
              • c5
                • 10fc93bfa7c1001f13c7f5de4bcffe65
              • c6
                • 207294e3a7c1001f13c7f5de4bcffe65
              • c7
                • 40840f57aac1001f1a968c8925716fac
              • c8
                • 900a53d4a9c1001f13c7f5de4bcffe65
                • c0bc1057aac1001f1a968c8925716fac
              • c9
                • 40a743a1aec1001f1a968c8925716fac
                • d0cce481aec1001f1a968c8925716fac
              • ca
                • 3066bde8a7c1001f13c7f5de4bcffe65
              • cb
                • 908ae120a9c1001f13c7f5de4bcffe65
              • cc
                • c02d0a1aaac1001f1a968c8925716fac
                • f08df0d5a9c1001f13c7f5de4bcffe65
              • d0
                • 2085eb4ab3c1001f1a968c8925716fac
              • d3
                • 000c5248a7c1001f13c7f5de4bcffe65
              • d5
                • a08c0b57aac1001f1a968c8925716fac
              • d6
                • 80e0ee4ab3c1001f1a968c8925716fac
              • d8
                • 20d0e3bfafc1001f1a968c8925716fac
              • d9
                • b0f4a62aaac1001f1a968c8925716fac
                • f06d2b69a6c1001f13c7f5de4bcffe65
              • dc
                • c0aece7aaec1001f1a968c8925716fac
              • e0
                • 80db5334b0c1001f1a968c8925716fac
              • e1
                • 00c31337a9c1001f13c7f5de4bcffe65
              • e3
                • 8081dc20a9c1001f13c7f5de4bcffe65
              • e5
                • 507e1d72aec1001f1a968c8925716fac
              • e6
                • 704b50d4a9c1001f13c7f5de4bcffe65
              • e7
                • 106559d6adc1001f1a968c8925716fac
              • e8
                • c0006f00aac1001f13c7f5de4bcffe65
                • c037f174aec1001f1a968c8925716fac
              • e9
                • a091b4b2afc1001f1a968c8925716fac
              • ea
                • 408e846ba8c1001f13c7f5de4bcffe65
              • eb
                • c08496bdafc1001f1a968c8925716fac
              • ec
                • 702d55d4a9c1001f13c7f5de4bcffe65
              • f1
                • b019ad2db3c1001f1a968c8925716fac
                • f0dc3a88b3c1001f1a968c8925716fac
              • f4
                • 50225a48a7c1001f13c7f5de4bcffe65
              • f7
                • 4076cd7aaec1001f1a968c8925716fac
              • f8
                • b0d6ab2aaac1001f1a968c8925716fac
              • fb
                • 10fda6b2afc1001f1a968c8925716fac
            • .projects
              • Arduino
                • .indexes
                  • 19
                    • history.index
                  • 33
                    • history.index
                  • af
                    • history.index
                  • history.index
                  • properties.index
                • .markers
              • BlinkenLights
                • .indexes
                  • 19
                    • history.index
                  • 33
                    • history.index
                  • af
                    • history.index
                  • history.index
                  • properties.index
            • .root
              • .indexes
                • history.version
                • properties.index
                • properties.version
              • 3.tree
            • .safetable
              • org.eclipse.core.resources
          • org.eclipse.core.runtime
          • org.eclipse.debug.core
          • org.eclipse.debug.ui
          • org.eclipse.epp.usagedata.recording
          • org.eclipse.equinox.p2.ui
          • org.eclipse.ltk.core.refactoring
          • org.eclipse.mylyn.bugzilla.core
          • org.eclipse.mylyn.tasks.ui
          • org.eclipse.team.cvs.core
          • org.eclipse.ui.intro
          • org.eclipse.ui.workbench
        • version.ini
      • Arduino
      • BlinkenLights
/*
  HardwareSerial.cpp - Hardware serial library for Wiring
  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  
  Modified 23 November 2006 by David A. Mellis
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "wiring.h"
#include "wiring_private.h"

#include "HardwareSerial.h"

// Define constants and variables for buffering incoming serial data.  We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read.
#define RX_BUFFER_SIZE 128

struct ring_buffer {
  unsigned char buffer[RX_BUFFER_SIZE];
  int head;
  int tail;
};

ring_buffer rx_buffer = { { 0 }, 0, 0 };

#if defined(__AVR_ATmega1280__)
ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
#endif

inline void store_char(unsigned char c, ring_buffer *rx_buffer)
{
  int i = (rx_buffer->head + 1) % RX_BUFFER_SIZE;

  // if we should be storing the received character into the location
  // just before the tail (meaning that the head would advance to the
  // current location of the tail), we're about to overflow the buffer
  // and so we don't write the character or advance the head.
  if (i != rx_buffer->tail) {
    rx_buffer->buffer[rx_buffer->head] = c;
    rx_buffer->head = i;
  }
}

#if defined(__AVR_ATmega1280__)

SIGNAL(SIG_USART0_RECV)
{
  unsigned char c = UDR0;
  store_char(c, &rx_buffer);
}

SIGNAL(SIG_USART1_RECV)
{
  unsigned char c = UDR1;
  store_char(c, &rx_buffer1);
}

SIGNAL(SIG_USART2_RECV)
{
  unsigned char c = UDR2;
  store_char(c, &rx_buffer2);
}

SIGNAL(SIG_USART3_RECV)
{
  unsigned char c = UDR3;
  store_char(c, &rx_buffer3);
}

#else

#if defined(__AVR_ATmega8__)
SIGNAL(SIG_UART_RECV)
#else
SIGNAL(USART_RX_vect)
#endif
{
#if defined(__AVR_ATmega8__)
  unsigned char c = UDR;
#else
  unsigned char c = UDR0;
#endif
  store_char(c, &rx_buffer);
}

#endif

// Constructors ////////////////////////////////////////////////////////////////

HardwareSerial::HardwareSerial(ring_buffer *rx_buffer,
  volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
  volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
  volatile uint8_t *udr,
  uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udre, uint8_t u2x)
{
  _rx_buffer = rx_buffer;
  _ubrrh = ubrrh;
  _ubrrl = ubrrl;
  _ucsra = ucsra;
  _ucsrb = ucsrb;
  _udr = udr;
  _rxen = rxen;
  _txen = txen;
  _rxcie = rxcie;
  _udre = udre;
  _u2x = u2x;
}

// Public Methods //////////////////////////////////////////////////////////////

void HardwareSerial::begin(long baud)
{
  uint16_t baud_setting;
  bool use_u2x;

  // U2X mode is needed for baud rates higher than (CPU Hz / 16)
  if (baud > F_CPU / 16) {
    use_u2x = true;
  } else {
    // figure out if U2X mode would allow for a better connection
    
    // calculate the percent difference between the baud-rate specified and
    // the real baud rate for both U2X and non-U2X mode (0-255 error percent)
    uint8_t nonu2x_baud_error = abs((int)(255-((F_CPU/(16*(((F_CPU/8/baud-1)/2)+1))*255)/baud)));
    uint8_t u2x_baud_error = abs((int)(255-((F_CPU/(8*(((F_CPU/4/baud-1)/2)+1))*255)/baud)));
    
    // prefer non-U2X mode because it handles clock skew better
    use_u2x = (nonu2x_baud_error > u2x_baud_error);
  }
  
  if (use_u2x) {
    *_ucsra = 1 << _u2x;
    baud_setting = (F_CPU / 4 / baud - 1) / 2;
  } else {
    *_ucsra = 0;
    baud_setting = (F_CPU / 8 / baud - 1) / 2;
  }

  // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
  *_ubrrh = baud_setting >> 8;
  *_ubrrl = baud_setting;

  sbi(*_ucsrb, _rxen);
  sbi(*_ucsrb, _txen);
  sbi(*_ucsrb, _rxcie);
}

void HardwareSerial::end()
{
  cbi(*_ucsrb, _rxen);
  cbi(*_ucsrb, _txen);
  cbi(*_ucsrb, _rxcie);  
}

int HardwareSerial::available(void)
{
  return (RX_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % RX_BUFFER_SIZE;
}

int HardwareSerial::peek(void)
{
  if (_rx_buffer->head == _rx_buffer->tail) {
    return -1;
  } else {
    return _rx_buffer->buffer[_rx_buffer->tail];
  }
}

int HardwareSerial::read(void)
{
  // if the head isn't ahead of the tail, we don't have any characters
  if (_rx_buffer->head == _rx_buffer->tail) {
    return -1;
  } else {
    unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
    _rx_buffer->tail = (_rx_buffer->tail + 1) % RX_BUFFER_SIZE;
    return c;
  }
}

void HardwareSerial::flush()
{
  // don't reverse this or there may be problems if the RX interrupt
  // occurs after reading the value of rx_buffer_head but before writing
  // the value to rx_buffer_tail; the previous value of rx_buffer_head
  // may be written to rx_buffer_tail, making it appear as if the buffer
  // don't reverse this or there may be problems if the RX interrupt
  // occurs after reading the value of rx_buffer_head but before writing
  // the value to rx_buffer_tail; the previous value of rx_buffer_head
  // may be written to rx_buffer_tail, making it appear as if the buffer
  // were full, not empty.
  _rx_buffer->head = _rx_buffer->tail;
}

void HardwareSerial::write(uint8_t c)
{
  while (!((*_ucsra) & (1 << _udre)))
    ;

  *_udr = c;
}

// Preinstantiate Objects //////////////////////////////////////////////////////

#if defined(__AVR_ATmega8__)
HardwareSerial Serial(&rx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UDR, RXEN, TXEN, RXCIE, UDRE, U2X);
#else
HardwareSerial Serial(&rx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UDR0, RXEN0, TXEN0, RXCIE0, UDRE0, U2X0);
#endif

#if defined(__AVR_ATmega1280__)
HardwareSerial Serial1(&rx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UDR1, RXEN1, TXEN1, RXCIE1, UDRE1, U2X1);
HardwareSerial Serial2(&rx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UDR2, RXEN2, TXEN2, RXCIE2, UDRE2, U2X2);
HardwareSerial Serial3(&rx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UDR3, RXEN3, TXEN3, RXCIE3, UDRE3, U2X3);
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) NVIDIA India
India India
I am a software engineer by profession and an electronics engineer by education. Used to be (and still am) a hobby programmer, an electronics enthusiast/DIYer etc.

Some areas which interest me are...compiler construction, operating systems, robotics, science in general etc

Comments and Discussions