diff options
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/EEPROM/EEPROM.cpp | 50 | ||||
-rw-r--r-- | libraries/EEPROM/EEPROM.h | 127 | ||||
-rw-r--r-- | libraries/EEPROM/README.md | 139 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino | 24 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino | 50 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_get/eeprom_get.ino | 66 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino | 57 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_put/eeprom_put.ino | 56 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_read/eeprom_read.ino | 24 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_update/eeprom_update.ino | 69 | ||||
-rw-r--r-- | libraries/EEPROM/examples/eeprom_write/eeprom_write.ino | 50 | ||||
-rw-r--r-- | libraries/EEPROM/keywords.txt | 6 | ||||
-rw-r--r-- | libraries/EEPROM/library.properties | 4 | ||||
-rw-r--r-- | libraries/SPI/SPI.cpp | 10 | ||||
-rw-r--r-- | libraries/SoftwareSerial/SoftwareSerial.cpp | 334 | ||||
-rw-r--r-- | libraries/SoftwareSerial/SoftwareSerial.h | 17 |
16 files changed, 810 insertions, 273 deletions
diff --git a/libraries/EEPROM/EEPROM.cpp b/libraries/EEPROM/EEPROM.cpp deleted file mode 100644 index dfa1deb..0000000 --- a/libraries/EEPROM/EEPROM.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - EEPROM.cpp - EEPROM library - Copyright (c) 2006 David A. Mellis. 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 -*/ - -/****************************************************************************** - * Includes - ******************************************************************************/ - -#include <avr/eeprom.h> -#include "Arduino.h" -#include "EEPROM.h" - -/****************************************************************************** - * Definitions - ******************************************************************************/ - -/****************************************************************************** - * Constructors - ******************************************************************************/ - -/****************************************************************************** - * User API - ******************************************************************************/ - -uint8_t EEPROMClass::read(int address) -{ - return eeprom_read_byte((unsigned char *) address); -} - -void EEPROMClass::write(int address, uint8_t value) -{ - eeprom_write_byte((unsigned char *) address, value); -} - -EEPROMClass EEPROM; diff --git a/libraries/EEPROM/EEPROM.h b/libraries/EEPROM/EEPROM.h index aa2b577..cde75db 100644 --- a/libraries/EEPROM/EEPROM.h +++ b/libraries/EEPROM/EEPROM.h @@ -1,6 +1,7 @@ /* EEPROM.h - EEPROM library - Copyright (c) 2006 David A. Mellis. All right reserved. + Original Copyright (c) 2006 David A. Mellis. All right reserved. + New version by Christopher Andrews 2015. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,15 +22,125 @@ #define EEPROM_h #include <inttypes.h> +#include <avr/eeprom.h> +#include <avr/io.h> -class EEPROMClass -{ - public: - uint8_t read(int); - void write(int, uint8_t); +/*** + EERef class. + + This object references an EEPROM cell. + Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. + This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. +***/ + +struct EERef{ + + EERef( const int index ) + : index( index ) {} + + //Access/read members. + uint8_t operator*() const { return eeprom_read_byte( (uint8_t*) index ); } + operator const uint8_t() const { return **this; } + + //Assignment/write members. + EERef &operator=( const EERef &ref ) { return *this = *ref; } + EERef &operator=( uint8_t in ) { return eeprom_write_byte( (uint8_t*) index, in ), *this; } + EERef &operator +=( uint8_t in ) { return *this = **this + in; } + EERef &operator -=( uint8_t in ) { return *this = **this - in; } + EERef &operator *=( uint8_t in ) { return *this = **this * in; } + EERef &operator /=( uint8_t in ) { return *this = **this / in; } + EERef &operator ^=( uint8_t in ) { return *this = **this ^ in; } + EERef &operator %=( uint8_t in ) { return *this = **this % in; } + EERef &operator &=( uint8_t in ) { return *this = **this & in; } + EERef &operator |=( uint8_t in ) { return *this = **this | in; } + EERef &operator <<=( uint8_t in ) { return *this = **this << in; } + EERef &operator >>=( uint8_t in ) { return *this = **this >> in; } + + EERef &update( uint8_t in ) { return in != *this ? *this = in : *this; } + + /** Prefix increment/decrement **/ + EERef& operator++() { return *this += 1; } + EERef& operator--() { return *this -= 1; } + + /** Postfix increment/decrement **/ + uint8_t operator++ (int){ + uint8_t ret = **this; + return ++(*this), ret; + } + + uint8_t operator-- (int){ + uint8_t ret = **this; + return --(*this), ret; + } + + int index; //Index of current EEPROM cell. +}; + +/*** + EEPtr class. + + This object is a bidirectional pointer to EEPROM cells represented by EERef objects. + Just like a normal pointer type, this can be dereferenced and repositioned using + increment/decrement operators. +***/ + +struct EEPtr{ + + EEPtr( const int index ) + : index( index ) {} + + operator const int() const { return index; } + EEPtr &operator=( int in ) { return index = in, *this; } + + //Iterator functionality. + bool operator!=( const EEPtr &ptr ) { return index != ptr.index; } + EERef operator*() { return index; } + + /** Prefix & Postfix increment/decrement **/ + EEPtr& operator++() { return ++index, *this; } + EEPtr& operator--() { return --index, *this; } + EEPtr operator++ (int) { return index++; } + EEPtr operator-- (int) { return index--; } + + int index; //Index of current EEPROM cell. }; -extern EEPROMClass EEPROM; +/*** + EEPROMClass class. + + This object represents the entire EEPROM space. + It wraps the functionality of EEPtr and EERef into a basic interface. + This class is also 100% backwards compatible with earlier Arduino core releases. +***/ -#endif +struct EEPROMClass{ + + //Basic user access methods. + EERef operator[]( const int idx ) { return idx; } + uint8_t read( int idx ) { return EERef( idx ); } + void write( int idx, uint8_t val ) { (EERef( idx )) = val; } + void update( int idx, uint8_t val ) { EERef( idx ).update( val ); } + + //STL and C++11 iteration capability. + EEPtr begin() { return 0x00; } + EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid. + uint16_t length() { return E2END + 1; } + + //Functionality to 'get' and 'put' objects to and from EEPROM. + template< typename T > T &get( int idx, T &t ){ + EEPtr e = idx; + uint8_t *ptr = (uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++e ) *ptr++ = *e; + return t; + } + + template< typename T > const T &put( int idx, const T &t ){ + EEPtr e = idx; + const uint8_t *ptr = (const uint8_t*) &t; + for( int count = sizeof(T) ; count ; --count, ++e ) (*e).update( *ptr++ ); + return t; + } +}; +static EEPROMClass EEPROM; +#endif
\ No newline at end of file diff --git a/libraries/EEPROM/README.md b/libraries/EEPROM/README.md new file mode 100644 index 0000000..a624136 --- /dev/null +++ b/libraries/EEPROM/README.md @@ -0,0 +1,139 @@ +## **EEPROM Library V2.0** for Arduino + +**Written by:** _Christopher Andrews_. + +### **What is the EEPROM library.** + +Th EEPROM library provides an easy to use interface to interact with the internal non-volatile storage found in AVR based Arduino boards. This library will work on many AVR devices like ATtiny and ATmega chips. + +### **How to use it** +The EEPROM library is included in your IDE download. To add its functionality to your sketch you'll need to reference the library header file. You do this by adding an include directive to the top of your sketch. + +```Arduino +#include <EEPROM.h> + +void setup(){ + +} + +void loop(){ + +} + +``` + +The library provides a global variable named `EEPROM`, you use this variable to access the library functions. The methods provided in the EEPROM class are listed below. + +You can view all the examples [here](examples/). + +### **Library functions** + +#### **`EEPROM.read( address )`** [[_example_]](examples/eeprom_read/eeprom_read.ino) + +This function allows you to read a single byte of data from the eeprom. +Its only parameter is an `int` which should be set to the address you wish to read. + +The function returns an `unsigned char` containing the value read. + +#### **`EEPROM.write( address, value )`** [[_example_]](examples/eeprom_write/eeprom_write.ino) + +The `write()` method allows you to write a single byte of data to the EEPROM. +Two parameters are needed. The first is an `int` containing the address that is to be written, and the second is a the data to be written (`unsigned char`). + +This function does not return any value. + +#### **`EEPROM.update( address, value )`** [[_example_]](examples/eeprom_update/eeprom_update.ino) + +This function is similar to `EEPROM.write()` however this method will only write data if the cell contents pointed to by `address` is different to `value`. This method can help prevent unnecessary wear on the EEPROM cells. + +This function does not return any value. + +#### **`EEPROM.get( address, object )`** [[_example_]](examples/eeprom_get/eeprom_get.ino) + +This function will retrieve any object from the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to read. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience. + +#### **`EEPROM.put( address, object )`** [[_example_]](examples/eeprom_put/eeprom_put.ino) + +This function will write any object to the EEPROM. +Two parameters are needed to call this function. The first is an `int` containing the address that is to be written, and the second is the object you would like to write. + +This function uses the _update_ method to write its data, and therefore only rewrites changed cells. + +This function returns a reference to the `object` passed in. It does not need to be used and is only returned for conveience. + +#### **Subscript operator: `EEPROM[address]`** [[_example_]](examples/eeprom_crc/eeprom_crc.ino) + +This operator allows using the identifier `EEPROM` like an array. +EEPROM cells can be read _and_ **_written_** directly using this method. + +This operator returns a reference to the EEPROM cell. + +```c++ +unsigned char val; + +//Read first EEPROM cell. +val = EEPROM[ 0 ]; + +//Write first EEPROM cell. +EEPROM[ 0 ] = val; + +//Compare contents +if( val == EEPROM[ 0 ] ){ + //Do something... +} +``` + +#### **`EEPROM.length()`** + +This function returns an `unsigned int` containing the number of cells in the EEPROM. + +--- + +### **Advanced features** + +This library uses a component based approach to provide its functionality. This means you can also use these components to design a customized approach. Two background classes are available for use: `EERef` & `EEPtr`. + +#### **`EERef` class** + +This object references an EEPROM cell. +Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM. +This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell. + +```C++ +EERef ref = EEPROM[ 10 ]; //Create a reference to 11th cell. + +ref = 4; //write to EEPROM cell. + +unsigned char val = ref; //Read referenced cell. +``` + +#### **`EEPtr` class** + +This object is a bidirectional pointer to EEPROM cells represented by `EERef` objects. +Just like a normal pointer type, this type can be dereferenced and repositioned using +increment/decrement operators. + +```C++ +EEPtr ptr = 10; //Create a pointer to 11th cell. + +*ptr = 4; //dereference and write to EEPROM cell. + +unsigned char val = *ptr; //dereference and read. + +ptr++; //Move to next EEPROM cell. +``` + +#### **`EEPROM.begin()`** + +This function returns an `EEPtr` pointing to the first cell in the EEPROM. +This is useful for STL objects, custom iteration and C++11 style ranged for loops. + +#### **`EEPROM.end()`** + +This function returns an `EEPtr` pointing at the location after the last EEPROM cell. +Used with `begin()` to provide custom iteration. + +**Note:** The `EEPtr` returned is invalid as it is out of range. Infact the hardware causes wrapping of the address (overflow) and `EEPROM.end()` actually references the first EEPROM cell. diff --git a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino index b18ff2c..49eb5fe 100644 --- a/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino +++ b/libraries/EEPROM/examples/eeprom_clear/eeprom_clear.ino @@ -2,22 +2,34 @@ * EEPROM Clear * * Sets all of the bytes of the EEPROM to 0. + * Please see eeprom_iteration for a more in depth + * look at how to traverse the EEPROM. + * * This example code is in the public domain. - */ #include <EEPROM.h> void setup() { - // write a 0 to all 512 bytes of the EEPROM - for (int i = 0; i < 512; i++) + + /*** + Iterate through each byte of the EEPROM storage. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ + + for ( int i = 0 ; i < EEPROM.length() ; i++ ) EEPROM.write(i, 0); // turn the LED on when we're done digitalWrite(13, HIGH); } -void loop() -{ -} +void loop(){ /** Empty loop. **/ } diff --git a/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino new file mode 100644 index 0000000..8461d56 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_crc/eeprom_crc.ino @@ -0,0 +1,50 @@ +/*** + Written by Christopher Andrews. + CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ). + + A CRC is a simple way of checking whether data has changed or become corrupted. + This example calculates a CRC value directly on the EEPROM values. + The purpose of this example is to highlight how the EEPROM object can be used just like an array. +***/ + +#include <Arduino.h> +#include <EEPROM.h> + +void setup(){ + + //Start serial + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + //Print length of data to run CRC on. + Serial.print( "EEPROM length: " ); + Serial.println( EEPROM.length() ); + + //Print the result of calling eeprom_crc() + Serial.print( "CRC32 of EEPROM data: 0x" ); + Serial.println( eeprom_crc(), HEX ); + Serial.print( "\n\nDone!" ); +} + +void loop(){ /* Empty loop */ } + +unsigned long eeprom_crc( void ){ + + const unsigned long crc_table[16] = { + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c + }; + + unsigned long crc = ~0L; + + for( int index = 0 ; index < EEPROM.length() ; ++index ){ + crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4); + crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4); + crc = ~crc; + } + return crc; +}
\ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino new file mode 100644 index 0000000..6620999 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_get/eeprom_get.ino @@ -0,0 +1,66 @@ +/*** + eeprom_get example. + + This shows how to use the EEPROM.get() method. + + To pre-set the EEPROM data, run the example sketch eeprom_put. + This sketch will run without it, however, the values shown + will be shown from what ever is already on the EEPROM. + + This may cause the serial object to print out a large string + of garbage if there is no null character inside one of the strings + loaded. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include <EEPROM.h> + +void setup(){ + + float f = 0.00f; //Variable to store data read from EEPROM. + int eeAddress = 0; //EEPROM address to start reading from + + Serial.begin( 9600 ); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + Serial.print( "Read float from EEPROM: " ); + + //Get the float data from the EEPROM at position 'eeAddress' + EEPROM.get( eeAddress, f ); + Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float. + + /*** + As get also returns a reference to 'f', you can use it inline. + E.g: Serial.print( EEPROM.get( eeAddress, f ) ); + ***/ + + /*** + Get can be used with custom structures too. + I have separated this into an extra function. + ***/ + + secondTest(); //Run the next test. +} + +struct MyObject{ + float field1; + byte field2; + char name[10]; +}; + +void secondTest(){ + int eeAddress = sizeof(float); //Move address to the next byte after float 'f'. + + MyObject customVar; //Variable to store custom object read from EEPROM. + EEPROM.get( eeAddress, customVar ); + + Serial.println( "Read custom object from EEPROM: " ); + Serial.println( customVar.field1 ); + Serial.println( customVar.field2 ); + Serial.println( customVar.name ); +} + +void loop(){ /* Empty loop */ }
\ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino new file mode 100644 index 0000000..650c90a --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino @@ -0,0 +1,57 @@ +/*** + eeprom_iteration example. + + A set of example snippets highlighting the + simplest methods for traversing the EEPROM. + + Running this sketch is not necessary, this is + simply highlighting certain programming methods. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include <EEPROM.h> + +void setup() { + + /*** + Iterate the EEPROM using a for loop. + ***/ + + for( int index = 0 ; index < EEPROM.length() ; index++ ){ + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + } + + /*** + Iterate the EEPROM using a while loop. + ***/ + + int index = 0; + + while( index < EEPROM.length() ){ + + //Add one to each cell in the EEPROM + EEPROM[ index ] += 1; + index++; + } + + /*** + Iterate the EEPROM using a do-while loop. + ***/ + + int idx = 0; //Used 'idx' to avoid name conflict with 'index' above. + + do{ + + //Add one to each cell in the EEPROM + EEPROM[ idx ] += 1; + idx++; + }while( idx < EEPROM.length() ); + + +} //End of setup function. + +void loop(){}
\ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino new file mode 100644 index 0000000..186cf95 --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_put/eeprom_put.ino @@ -0,0 +1,56 @@ +/*** + eeprom_put example. + + This shows how to use the EEPROM.put() method. + Also, this sketch will pre-set the EEPROM data for the + example sketch eeprom_get. + + Note, unlike the single byte version EEPROM.write(), + the put method will use update semantics. As in a byte + will only be written to the EEPROM if the data is actually + different. + + Written by Christopher Andrews 2015 + Released under MIT licence. +***/ + +#include <EEPROM.h> + +struct MyObject{ + float field1; + byte field2; + char name[10]; +}; + +void setup(){ + + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + float f = 123.456f; //Variable to store in EEPROM. + int eeAddress = 0; //Location we want the data to be put. + + + //One simple call, with the address first and the object second. + EEPROM.put( eeAddress, f ); + + Serial.println("Written float data type!"); + + /** Put is designed for use with custom structures also. **/ + + //Data to store. + MyObject customVar = { + 3.14f, + 65, + "Working!" + }; + + eeAddress += sizeof(float); //Move address to the next byte after float 'f'. + + EEPROM.put( eeAddress, customVar ); + Serial.print( "Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!" ); +} + +void loop(){ /* Empty loop */ }
\ No newline at end of file diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino index ebf79d6..68c4ffc 100644 --- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino +++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino @@ -31,13 +31,27 @@ void loop() Serial.print(value, DEC); Serial.println(); - // advance to the next address of the EEPROM + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ address = address + 1; - - // there are only 512 bytes of EEPROM, from 0 to 511, so if we're - // on address 512, wrap around to address 0 - if (address == 512) + if(address == EEPROM.length()) address = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++address &= EEPROM.length() - 1; + ***/ delay(500); } diff --git a/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino new file mode 100644 index 0000000..831056f --- /dev/null +++ b/libraries/EEPROM/examples/eeprom_update/eeprom_update.ino @@ -0,0 +1,69 @@ +/*** + EEPROM Update method + + Stores values read from analog input 0 into the EEPROM. + These values will stay in the EEPROM when the board is + turned off and may be retrieved later by another sketch. + + If a value has not changed in the EEPROM, it is not overwritten + which would reduce the life span of the EEPROM unnecessarily. + + Released using MIT licence. + ***/ + +#include <EEPROM.h> + +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int address = 0; + +void setup(){ /** EMpty setup **/ } + +void loop() +{ + /*** + need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + int val = analogRead(0) / 4; + + /*** + Update the particular EEPROM cell. + these values will remain there when the board is + turned off. + ***/ + EEPROM.update(address, val); + + /*** + The function EEPROM.update(address, val) is equivalent to the following: + + if( EEPROM.read(address) != val ){ + EEPROM.write(address, val); + } + ***/ + + + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ + address = address + 1; + if(address == EEPROM.length()) + address = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++address &= EEPROM.length() - 1; + ***/ + + delay(100); +} diff --git a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino index c047887..f07446c 100644 --- a/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino +++ b/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @@ -8,31 +8,51 @@ #include <EEPROM.h> -// the current address in the EEPROM (i.e. which byte -// we're going to write to next) -int addr = 0; +/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/ +int addr = 0; -void setup() -{ -} +void setup(){ /** Empty setup. **/} void loop() { - // need to divide by 4 because analog inputs range from - // 0 to 1023 and each byte of the EEPROM can only hold a - // value from 0 to 255. + /*** + Need to divide by 4 because analog inputs range from + 0 to 1023 and each byte of the EEPROM can only hold a + value from 0 to 255. + ***/ + int val = analogRead(0) / 4; - // write the value to the appropriate byte of the EEPROM. - // these values will remain there when the board is - // turned off. + /*** + Write the value to the appropriate byte of the EEPROM. + these values will remain there when the board is + turned off. + ***/ + EEPROM.write(addr, val); - // advance to the next address. there are 512 bytes in - // the EEPROM, so go back to 0 when we hit 512. + /*** + Advance to the next address, when at the end restart at the beginning. + + Larger AVR processors have larger EEPROM sizes, E.g: + - Arduno Duemilanove: 512b EEPROM storage. + - Arduino Uno: 1kb EEPROM storage. + - Arduino Mega: 4kb EEPROM storage. + + Rather than hard-coding the length, you should use the pre-provided length function. + This will make your code portable to all AVR processors. + ***/ addr = addr + 1; - if (addr == 512) + if(addr == EEPROM.length()) addr = 0; + + /*** + As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an + EEPROM address is also doable by a bitwise and of the length - 1. + + ++addr &= EEPROM.length() - 1; + ***/ + delay(100); } diff --git a/libraries/EEPROM/keywords.txt b/libraries/EEPROM/keywords.txt index d3218fe..2cabc0b 100644 --- a/libraries/EEPROM/keywords.txt +++ b/libraries/EEPROM/keywords.txt @@ -1,5 +1,5 @@ ####################################### -# Syntax Coloring Map For Ultrasound +# Syntax Coloring Map For EEPROM ####################################### ####################################### @@ -7,11 +7,15 @@ ####################################### EEPROM KEYWORD1 +EERef KEYWORD1 +EEPtr KEYWORD2 ####################################### # Methods and Functions (KEYWORD2) ####################################### +update KEYWORD2 + ####################################### # Constants (LITERAL1) ####################################### diff --git a/libraries/EEPROM/library.properties b/libraries/EEPROM/library.properties index 796f7cb..955a0ae 100644 --- a/libraries/EEPROM/library.properties +++ b/libraries/EEPROM/library.properties @@ -1,6 +1,6 @@ name=EEPROM -version=1.0 -author=Arduino +version=2.0 +author=Arduino, Christopher Andrews maintainer=Arduino <info@arduino.cc> sentence=Enables reading and writing to the permanent board storage. For all Arduino boards BUT Arduino DUE. paragraph= diff --git a/libraries/SPI/SPI.cpp b/libraries/SPI/SPI.cpp index 9e7a116..af14e07 100644 --- a/libraries/SPI/SPI.cpp +++ b/libraries/SPI/SPI.cpp @@ -29,7 +29,15 @@ void SPIClass::begin() noInterrupts(); // Protect from a scheduler and prevent transactionBegin if (!initialized) { // Set SS to high so a connected chip will be "deselected" by default - digitalWrite(SS, HIGH); + uint8_t port = digitalPinToPort(SS); + uint8_t bit = digitalPinToBitMask(SS); + volatile uint8_t *reg = portModeRegister(port); + + // if the SS pin is not already configured as an output + // then set it high (to enable the internal pull-up resistor) + if(!(*reg & bit)){ + digitalWrite(SS, HIGH); + } // When the SS pin is set as OUTPUT, it can be used as // a general purpose output port (it doesn't influence diff --git a/libraries/SoftwareSerial/SoftwareSerial.cpp b/libraries/SoftwareSerial/SoftwareSerial.cpp index d1f6c92..527f3f9 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.cpp +++ b/libraries/SoftwareSerial/SoftwareSerial.cpp @@ -42,92 +42,7 @@ http://arduiniana.org. #include <avr/pgmspace.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
-//
-// Lookup table
-//
-typedef struct _DELAY_TABLE
-{
- long baud;
- unsigned short rx_delay_centering;
- unsigned short rx_delay_intrabit;
- unsigned short rx_delay_stopbit;
- unsigned short tx_delay;
-} DELAY_TABLE;
-
-#if F_CPU == 16000000
-
-static const DELAY_TABLE PROGMEM table[] =
-{
- // baud rxcenter rxintra rxstop tx
- { 115200, 1, 17, 17, 12, },
- { 57600, 10, 37, 37, 33, },
- { 38400, 25, 57, 57, 54, },
- { 31250, 31, 70, 70, 68, },
- { 28800, 34, 77, 77, 74, },
- { 19200, 54, 117, 117, 114, },
- { 14400, 74, 156, 156, 153, },
- { 9600, 114, 236, 236, 233, },
- { 4800, 233, 474, 474, 471, },
- { 2400, 471, 950, 950, 947, },
- { 1200, 947, 1902, 1902, 1899, },
- { 600, 1902, 3804, 3804, 3800, },
- { 300, 3804, 7617, 7617, 7614, },
-};
-
-const int XMIT_START_ADJUSTMENT = 5;
-
-#elif F_CPU == 8000000
-
-static const DELAY_TABLE table[] PROGMEM =
-{
- // baud rxcenter rxintra rxstop tx
- { 115200, 1, 5, 5, 3, },
- { 57600, 1, 15, 15, 13, },
- { 38400, 2, 25, 26, 23, },
- { 31250, 7, 32, 33, 29, },
- { 28800, 11, 35, 35, 32, },
- { 19200, 20, 55, 55, 52, },
- { 14400, 30, 75, 75, 72, },
- { 9600, 50, 114, 114, 112, },
- { 4800, 110, 233, 233, 230, },
- { 2400, 229, 472, 472, 469, },
- { 1200, 467, 948, 948, 945, },
- { 600, 948, 1895, 1895, 1890, },
- { 300, 1895, 3805, 3805, 3802, },
-};
-
-const int XMIT_START_ADJUSTMENT = 4;
-
-#elif F_CPU == 20000000
-
-// 20MHz support courtesy of the good people at macegr.com.
-// Thanks, Garrett!
-
-static const DELAY_TABLE PROGMEM table[] =
-{
- // baud rxcenter rxintra rxstop tx
- { 115200, 3, 21, 21, 18, },
- { 57600, 20, 43, 43, 41, },
- { 38400, 37, 73, 73, 70, },
- { 31250, 45, 89, 89, 88, },
- { 28800, 46, 98, 98, 95, },
- { 19200, 71, 148, 148, 145, },
- { 14400, 96, 197, 197, 194, },
- { 9600, 146, 297, 297, 294, },
- { 4800, 296, 595, 595, 592, },
- { 2400, 592, 1189, 1189, 1186, },
- { 1200, 1187, 2379, 2379, 2376, },
- { 600, 2379, 4759, 4759, 4755, },
- { 300, 4759, 9523, 9523, 9520, },
-};
-
-const int XMIT_START_ADJUSTMENT = 6;
-
-#else
-
-#error This version of SoftwareSerial supports only 20, 16 and 8MHz processors
-
-#endif
+#include <util/delay_basic.h>
//
// Statics
@@ -162,36 +77,44 @@ inline void DebugPulse(uint8_t pin, uint8_t count) /* static */
inline void SoftwareSerial::tunedDelay(uint16_t delay) {
- uint8_t tmp=0;
-
- asm volatile("sbiw %0, 0x01 \n\t"
- "ldi %1, 0xFF \n\t"
- "cpi %A0, 0xFF \n\t"
- "cpc %B0, %1 \n\t"
- "brne .-10 \n\t"
- : "+r" (delay), "+a" (tmp)
- : "0" (delay)
- );
+ _delay_loop_2(delay);
}
// This function sets the current object as the "listening"
// one and returns true if it replaces another
bool SoftwareSerial::listen()
{
+ if (!_rx_delay_stopbit)
+ return false;
+
if (active_object != this)
{
+ if (active_object)
+ active_object->stopListening();
+
_buffer_overflow = false;
- uint8_t oldSREG = SREG;
- cli();
_receive_buffer_head = _receive_buffer_tail = 0;
active_object = this;
- SREG = oldSREG;
+
+ setRxIntMsk(true);
return true;
}
return false;
}
+// Stop listening. Returns true if we were actually listening.
+bool SoftwareSerial::stopListening()
+{
+ if (active_object == this)
+ {
+ setRxIntMsk(false);
+ active_object = NULL;
+ return true;
+ }
+ return false;
+}
+
//
// The receive routine called by the interrupt handler
//
@@ -220,43 +143,49 @@ void SoftwareSerial::recv() // so interrupt is probably not for us
if (_inverse_logic ? rx_pin_read() : !rx_pin_read())
{
+ // Disable further interrupts during reception, this prevents
+ // triggering another interrupt directly after we return, which can
+ // cause problems at higher baudrates.
+ setRxIntMsk(false);
+
// Wait approximately 1/2 of a bit width to "center" the sample
tunedDelay(_rx_delay_centering);
DebugPulse(_DEBUG_PIN2, 1);
// Read each of the 8 bits
- for (uint8_t i=0x1; i; i <<= 1)
+ for (uint8_t i=8; i > 0; --i)
{
tunedDelay(_rx_delay_intrabit);
+ d >>= 1;
DebugPulse(_DEBUG_PIN2, 1);
- uint8_t noti = ~i;
if (rx_pin_read())
- d |= i;
- else // else clause added to ensure function timing is ~balanced
- d &= noti;
+ d |= 0x80;
}
- // skip the stop bit
- tunedDelay(_rx_delay_stopbit);
- DebugPulse(_DEBUG_PIN2, 1);
-
if (_inverse_logic)
d = ~d;
// if buffer full, set the overflow flag and return
- if ((_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF != _receive_buffer_head)
+ uint8_t next = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
+ if (next != _receive_buffer_head)
{
// save new data in buffer: tail points to where byte goes
_receive_buffer[_receive_buffer_tail] = d; // save new byte
- _receive_buffer_tail = (_receive_buffer_tail + 1) % _SS_MAX_RX_BUFF;
+ _receive_buffer_tail = next;
}
else
{
-#if _DEBUG // for scope: pulse pin as overflow indictator
DebugPulse(_DEBUG_PIN1, 1);
-#endif
_buffer_overflow = true;
}
+
+ // skip the stop bit
+ tunedDelay(_rx_delay_stopbit);
+ DebugPulse(_DEBUG_PIN1, 1);
+
+ // Re-enable interrupts when we're sure to be inside the stop bit
+ setRxIntMsk(true);
+
}
#if GCC_VERSION < 40302
@@ -275,14 +204,6 @@ void SoftwareSerial::recv() #endif
}
-void SoftwareSerial::tx_pin_write(uint8_t pin_state)
-{
- if (pin_state == LOW)
- *_transmitPortRegister &= ~_transmitBitMask;
- else
- *_transmitPortRegister |= _transmitBitMask;
-}
-
uint8_t SoftwareSerial::rx_pin_read()
{
return *_receivePortRegister & _receiveBitMask;
@@ -309,24 +230,15 @@ ISR(PCINT0_vect) #endif
#if defined(PCINT1_vect)
-ISR(PCINT1_vect)
-{
- SoftwareSerial::handle_interrupt();
-}
+ISR(PCINT1_vect, ISR_ALIASOF(PCINT0_vect));
#endif
#if defined(PCINT2_vect)
-ISR(PCINT2_vect)
-{
- SoftwareSerial::handle_interrupt();
-}
+ISR(PCINT2_vect, ISR_ALIASOF(PCINT0_vect));
#endif
#if defined(PCINT3_vect)
-ISR(PCINT3_vect)
-{
- SoftwareSerial::handle_interrupt();
-}
+ISR(PCINT3_vect, ISR_ALIASOF(PCINT0_vect));
#endif
//
@@ -354,8 +266,12 @@ SoftwareSerial::~SoftwareSerial() void SoftwareSerial::setTX(uint8_t tx)
{
- pinMode(tx, OUTPUT);
+ // First write, then set output. If we do this the other way around,
+ // the pin would be output low for a short while before switching to
+ // output hihg. Now, it is input with pullup for a short while, which
+ // is fine. With inverse logic, either order is fine.
digitalWrite(tx, _inverse_logic ? LOW : HIGH);
+ pinMode(tx, OUTPUT);
_transmitBitMask = digitalPinToBitMask(tx);
uint8_t port = digitalPinToPort(tx);
_transmitPortRegister = portOutputRegister(port);
@@ -372,6 +288,13 @@ void SoftwareSerial::setRX(uint8_t rx) _receivePortRegister = portInputRegister(port);
}
+uint16_t SoftwareSerial::subtract_cap(uint16_t num, uint16_t sub) {
+ if (num > sub)
+ return num - sub;
+ else
+ return 1;
+}
+
//
// Public methods
//
@@ -380,27 +303,64 @@ void SoftwareSerial::begin(long speed) {
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
- for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
- {
- long baud = pgm_read_dword(&table[i].baud);
- if (baud == speed)
- {
- _rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
- _rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
- _rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
- _tx_delay = pgm_read_word(&table[i].tx_delay);
- break;
- }
- }
+ // Precalculate the various delays, in number of 4-cycle delays
+ uint16_t bit_delay = (F_CPU / speed) / 4;
+
+ // 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit,
+ // 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits,
+ // 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit
+ // These are all close enough to just use 15 cycles, since the inter-bit
+ // timings are the most critical (deviations stack 8 times)
+ _tx_delay = subtract_cap(bit_delay, 15 / 4);
+
+ // Only setup rx when we have a valid PCINT for this pin
+ if (digitalPinToPCICR(_receivePin)) {
+ #if GCC_VERSION > 40800
+ // Timings counted from gcc 4.8.2 output. This works up to 115200 on
+ // 16Mhz and 57600 on 8Mhz.
+ //
+ // When the start bit occurs, there are 3 or 4 cycles before the
+ // interrupt flag is set, 4 cycles before the PC is set to the right
+ // interrupt vector address and the old PC is pushed on the stack,
+ // and then 75 cycles of instructions (including the RJMP in the
+ // ISR vector table) until the first delay. After the delay, there
+ // are 17 more cycles until the pin value is read (excluding the
+ // delay in the loop).
+ // We want to have a total delay of 1.5 bit time. Inside the loop,
+ // we already wait for 1 bit time - 23 cycles, so here we wait for
+ // 0.5 bit time - (71 + 18 - 22) cycles.
+ _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 75 + 17 - 23) / 4);
+
+ // There are 23 cycles in each loop iteration (excluding the delay)
+ _rx_delay_intrabit = subtract_cap(bit_delay, 23 / 4);
+
+ // There are 37 cycles from the last bit read to the start of
+ // stopbit delay and 11 cycles from the delay until the interrupt
+ // mask is enabled again (which _must_ happen during the stopbit).
+ // This delay aims at 3/4 of a bit time, meaning the end of the
+ // delay will be at 1/4th of the stopbit. This allows some extra
+ // time for ISR cleanup, which makes 115200 baud at 16Mhz work more
+ // reliably
+ _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (37 + 11) / 4);
+ #else // Timings counted from gcc 4.3.2 output
+ // Note that this code is a _lot_ slower, mostly due to bad register
+ // allocation choices of gcc. This works up to 57600 on 16Mhz and
+ // 38400 on 8Mhz.
+ _rx_delay_centering = subtract_cap(bit_delay / 2, (4 + 4 + 97 + 29 - 11) / 4);
+ _rx_delay_intrabit = subtract_cap(bit_delay, 11 / 4);
+ _rx_delay_stopbit = subtract_cap(bit_delay * 3 / 4, (44 + 17) / 4);
+ #endif
+
+
+ // Enable the PCINT for the entire port here, but never disable it
+ // (others might also need it, so we disable the interrupt by using
+ // the per-pin PCMSK register).
+ *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
+ // Precalculate the pcint mask register and value, so setRxIntMask
+ // can be used inside the ISR without costing too much time.
+ _pcint_maskreg = digitalPinToPCMSK(_receivePin);
+ _pcint_maskvalue = _BV(digitalPinToPCMSKbit(_receivePin));
- // Set up RX interrupts, but only if we have a valid RX baud rate
- if (_rx_delay_stopbit)
- {
- if (digitalPinToPCICR(_receivePin))
- {
- *digitalPinToPCICR(_receivePin) |= _BV(digitalPinToPCICRbit(_receivePin));
- *digitalPinToPCMSK(_receivePin) |= _BV(digitalPinToPCMSKbit(_receivePin));
- }
tunedDelay(_tx_delay); // if we were low this establishes the end
}
@@ -412,10 +372,17 @@ void SoftwareSerial::begin(long speed) listen();
}
+void SoftwareSerial::setRxIntMsk(bool enable)
+{
+ if (enable)
+ *_pcint_maskreg |= _pcint_maskvalue;
+ else
+ *_pcint_maskreg &= ~_pcint_maskvalue;
+}
+
void SoftwareSerial::end()
{
- if (digitalPinToPCMSK(_receivePin))
- *digitalPinToPCMSK(_receivePin) &= ~_BV(digitalPinToPCMSKbit(_receivePin));
+ stopListening();
}
@@ -450,42 +417,47 @@ size_t SoftwareSerial::write(uint8_t b) return 0;
}
+ // By declaring these as local variables, the compiler will put them
+ // in registers _before_ disabling interrupts and entering the
+ // critical timing sections below, which makes it a lot easier to
+ // verify the cycle timings
+ volatile uint8_t *reg = _transmitPortRegister;
+ uint8_t reg_mask = _transmitBitMask;
+ uint8_t inv_mask = ~_transmitBitMask;
uint8_t oldSREG = SREG;
+ bool inv = _inverse_logic;
+ uint16_t delay = _tx_delay;
+
+ if (inv)
+ b = ~b;
+
cli(); // turn off interrupts for a clean txmit
// Write the start bit
- tx_pin_write(_inverse_logic ? HIGH : LOW);
- tunedDelay(_tx_delay + XMIT_START_ADJUSTMENT);
+ if (inv)
+ *reg |= reg_mask;
+ else
+ *reg &= inv_mask;
+
+ tunedDelay(delay);
// Write each of the 8 bits
- if (_inverse_logic)
+ for (uint8_t i = 8; i > 0; --i)
{
- for (byte mask = 0x01; mask; mask <<= 1)
- {
- if (b & mask) // choose bit
- tx_pin_write(LOW); // send 1
- else
- tx_pin_write(HIGH); // send 0
-
- tunedDelay(_tx_delay);
- }
+ if (b & 1) // choose bit
+ *reg |= reg_mask; // send 1
+ else
+ *reg &= inv_mask; // send 0
- tx_pin_write(LOW); // restore pin to natural state
+ tunedDelay(delay);
+ b >>= 1;
}
- else
- {
- for (byte mask = 0x01; mask; mask <<= 1)
- {
- if (b & mask) // choose bit
- tx_pin_write(HIGH); // send 1
- else
- tx_pin_write(LOW); // send 0
-
- tunedDelay(_tx_delay);
- }
- tx_pin_write(HIGH); // restore pin to natural state
- }
+ // restore pin to natural state
+ if (inv)
+ *reg &= inv_mask;
+ else
+ *reg |= reg_mask;
SREG = oldSREG; // turn interrupts back on
tunedDelay(_tx_delay);
diff --git a/libraries/SoftwareSerial/SoftwareSerial.h b/libraries/SoftwareSerial/SoftwareSerial.h index a6a60b5..274f3df 100644 --- a/libraries/SoftwareSerial/SoftwareSerial.h +++ b/libraries/SoftwareSerial/SoftwareSerial.h @@ -53,7 +53,10 @@ private: volatile uint8_t *_receivePortRegister;
uint8_t _transmitBitMask;
volatile uint8_t *_transmitPortRegister;
+ volatile uint8_t *_pcint_maskreg;
+ uint8_t _pcint_maskvalue;
+ // Expressed as 4-cycle delays (must never be 0!)
uint16_t _rx_delay_centering;
uint16_t _rx_delay_intrabit;
uint16_t _rx_delay_stopbit;
@@ -69,11 +72,15 @@ private: static SoftwareSerial *active_object;
// private methods
- void recv();
+ void recv() __attribute__((__always_inline__));
uint8_t rx_pin_read();
- void tx_pin_write(uint8_t pin_state);
+ void tx_pin_write(uint8_t pin_state) __attribute__((__always_inline__));
void setTX(uint8_t transmitPin);
void setRX(uint8_t receivePin);
+ void setRxIntMsk(bool enable) __attribute__((__always_inline__));
+
+ // Return num - sub, or 1 if the result would be < 1
+ static uint16_t subtract_cap(uint16_t num, uint16_t sub);
// private static method for timing
static inline void tunedDelay(uint16_t delay);
@@ -86,18 +93,20 @@ public: bool listen();
void end();
bool isListening() { return this == active_object; }
- bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
+ bool stopListening();
+ bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
int peek();
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
+ operator bool() { return true; }
using Print::write;
// public only for easy access by interrupt handlers
- static inline void handle_interrupt();
+ static inline void handle_interrupt() __attribute__((__always_inline__));
};
// Arduino 0012 workaround
|