diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..3483ea1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +package_arduino-lufa_index.json export-ignore +README.md export-ignore +readme_assets export-ignore +.git* export-ignore + diff --git a/.gitignore b/.gitignore index 30b0e3b..5a1b6ec 100644 --- a/.gitignore +++ b/.gitignore @@ -10,8 +10,4 @@ # Compiled Static libraries *.lai *.la -*.a - -# LUFA -LUFA/* -!LUFA/.keep \ No newline at end of file +*.a \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 91cf4ca..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "LUFA"] - path = LUFA - url = https://github.com/abcminiuser/lufa.git diff --git a/LUFA b/LUFA deleted file mode 160000 index 2cde257..0000000 --- a/LUFA +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2cde257fe11886bebbfe4aea816268c2739d89bb diff --git a/README.md b/README.md index 6ab2d77..dc51dfb 100644 --- a/README.md +++ b/README.md @@ -20,51 +20,50 @@ Thus, I managed to bring the powerful [LUFA] to Arduino! ## Installation -For the automatic installation, you need to have both git and Python 3.3 (or later) installed. These instructions work on both Linux and Windows. -Alternatively, there are instructions for [manual installation]. +As LUFA requires changes to the Arduino core files, it is installed as custom boards. -1. Close all open Arduino IDE windows! +In the Arduino IDE, go to `File > Preferences`, and in `Additional Boards Manager URLs` add the following entry : `https://github.com/Palatis/Arduino-Lufa/raw/master/package_arduino-lufa_index.json` -2. Navigate into your Arduino IDE's `libraries` folder. Replace `` with the install path of the Arduino IDE. + - ``` - $ cd /libraries - ``` +Restart Arduino IDE, and you should be able to find `Arduino LUFA AVR Boards` in the `Tools > Board > Boards Manager` -3. Clone both Arduino-Lufa and the LUFA submodule: + - ``` - $ git clone --recursive https://github.com/Palatis/Arduino-Lufa.git LUFA - ``` +Click on install, and `Arduino LUFA AVR Boards` should now appear as a submenu in `Tools > Board` -4. Install LUFA boards (more on this below) + - ``` - $ ./LUFA/install.py - ``` +## Installation (legacy) -5. Done! Proceed with the steps below to try out Arduino-Lufa +While not recommended, it is still possible to use the [legacy installation] method. -[manual installation]:docs/manual_installation.md +[legacy installation]:libraries/Arduino-LUFA/README.md ## First run and use -To test Arduino-Lufa, open the LUFA_DualVirtualSerial example, select your board type from -the `Tools > Board > Arduino LUFA AVR Boards` submenu and click __Verify__. +To test Arduino-Lufa, select your board type from the `Tools > Board > Arduino LUFA AVR Boards`, and open the `File > Examples > Exemple for this board > Arduino-LUFA > LUFA_DualVirtualSerial` example. + +Click __Verify__. + (**Note**: only atmega32u4 based boards are marked as compatible and will appear, if you are using another board which should be compatible please open an issue. Also see Note on __Upload__ below) - -To use Arduino-Lufa, include it as a library in your sketch: - - +To use Arduino-Lufa, select a LUFA board and add `#include ` to your sketch header. ### Note on uploading the LUFA_DualVirtualSerial sketch @@ -85,23 +84,7 @@ This is not permanent, however. To go back to the original state, upload a sketc If you need to compile sketches that use the Arduino Core USB Stack, you'll need to select the board type from the `Tools > Board > Arduino AVR Boards` submenu. -If you used the `activate.py` script to change the main core files (legacy method), then you'll have to deactivate -LUFA like so: - -``` -$ .//libraries/LUFA/deactivate.py -``` - -Uninstalling the LUFA AVR Boards can be done like so: - -``` -$ .//libraries/LUFA/uninstall.py -``` - -or simply by deleting the `.//hardware/arduino-LUFA` folder. - -If you work with a lot of different boards and find switching tedious, it is recommended to use the `install.py` script -so that you can always switch to and from LUFA by selecting the Arduino board from the relevant submenu. +Refer to [legacy installation] document if you previously installed using the legacy method and wish to deactivate Arduino-Lufa ## Credits * Victor Tseng: palatis _AT_ gmail _DOT_ com (Original Author) diff --git a/boards.txt b/boards.txt new file mode 100644 index 0000000..7c61a3e --- /dev/null +++ b/boards.txt @@ -0,0 +1,1054 @@ +# See: http://code.google.com/p/arduino/wiki/Platforms + +menu.cpu=Processor + +############################################################## + +yun.name=Arduino Yún (LUFA) +yun.upload.via_ssh=true + +yun.vid.0=0x2341 +yun.pid.0=0x0041 +yun.vid.1=0x2341 +yun.pid.1=0x8041 +yun.vid.2=0x2A03 +yun.pid.2=0x0041 +yun.vid.3=0x2A03 +yun.pid.3=0x8041 + +yun.upload.tool=avrdude +yun.upload.protocol=avr109 +yun.upload.maximum_size=28672 +yun.upload.maximum_data_size=2560 +yun.upload.speed=57600 +yun.upload.disable_flushing=true +yun.upload.use_1200bps_touch=true +yun.upload.wait_for_upload_port=true + +yun.bootloader.tool=avrdude +yun.bootloader.low_fuses=0xff +yun.bootloader.high_fuses=0xd8 +yun.bootloader.extended_fuses=0xfb +yun.bootloader.file=caterina/Caterina-Yun.hex +yun.bootloader.noblink=caterina/Caterina-Yun-noblink.hex +yun.bootloader.unlock_bits=0x3F +yun.bootloader.lock_bits=0x2F + +yun.build.mcu=atmega32u4 +yun.build.f_cpu=16000000L +yun.build.vid=0x2341 +yun.build.pid=0x8041 +yun.build.usb_product="Arduino Yun" +yun.build.board=AVR_YUN +yun.build.core=arduino +yun.build.variant=yun +yun.build.extra_flags={build.usb_flags} + +############################################################## + +#uno.name=Arduino Uno ((LUFA not supported)) + +#uno.vid.0=0x2341 +#uno.pid.0=0x0043 +#uno.vid.1=0x2341 +#uno.pid.1=0x0001 +#uno.vid.2=0x2A03 +#uno.pid.2=0x0043 +#uno.vid.3=0x2341 +#uno.pid.3=0x0243 + +#uno.upload.tool=avrdude +#uno.upload.protocol=arduino +#uno.upload.maximum_size=32256 +#uno.upload.maximum_data_size=2048 +#uno.upload.speed=115200 + +#uno.bootloader.tool=avrdude +#uno.bootloader.low_fuses=0xFF +#uno.bootloader.high_fuses=0xDE +#uno.bootloader.extended_fuses=0xFD +#uno.bootloader.unlock_bits=0x3F +#uno.bootloader.lock_bits=0x0F +#uno.bootloader.file=optiboot/optiboot_atmega328.hex + +#uno.build.mcu=atmega328p +#uno.build.f_cpu=16000000L +#uno.build.board=AVR_UNO +#uno.build.core=arduino +#uno.build.variant=standard + +############################################################## + +#diecimila.name=Arduino Duemilanove or Diecimila ((LUFA not supported)) + +#diecimila.upload.tool=avrdude +#diecimila.upload.protocol=arduino + +#diecimila.bootloader.tool=avrdude +#diecimila.bootloader.low_fuses=0xFF +#diecimila.bootloader.unlock_bits=0x3F +#diecimila.bootloader.lock_bits=0x0F + +#diecimila.build.f_cpu=16000000L +#diecimila.build.board=AVR_DUEMILANOVE +#diecimila.build.core=arduino +#diecimila.build.variant=standard + +## Arduino Duemilanove or Diecimila w/ ATmega328P +## ---------------------------------------------- +#diecimila.menu.cpu.atmega328=ATmega328P + +#diecimila.menu.cpu.atmega328.upload.maximum_size=30720 +#diecimila.menu.cpu.atmega328.upload.maximum_data_size=2048 +#diecimila.menu.cpu.atmega328.upload.speed=57600 + +#diecimila.menu.cpu.atmega328.bootloader.high_fuses=0xDA +#diecimila.menu.cpu.atmega328.bootloader.extended_fuses=0xFD +#diecimila.menu.cpu.atmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328.hex + +#diecimila.menu.cpu.atmega328.build.mcu=atmega328p + +## Arduino Duemilanove or Diecimila w/ ATmega168 +## --------------------------------------------- +#diecimila.menu.cpu.atmega168=ATmega168 + +#diecimila.menu.cpu.atmega168.upload.maximum_size=14336 +#diecimila.menu.cpu.atmega168.upload.maximum_data_size=1024 +#diecimila.menu.cpu.atmega168.upload.speed=19200 + +#diecimila.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#diecimila.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#diecimila.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_diecimila.hex + +#diecimila.menu.cpu.atmega168.build.mcu=atmega168 + +############################################################## + +#nano.name=Arduino Nano ((LUFA not supported)) + +#nano.upload.tool=avrdude +#nano.upload.protocol=arduino + +#nano.bootloader.tool=avrdude +#nano.bootloader.unlock_bits=0x3F +#nano.bootloader.lock_bits=0x0F + +#nano.build.f_cpu=16000000L +#nano.build.board=AVR_NANO +#nano.build.core=arduino +#nano.build.variant=eightanaloginputs + +## Arduino Nano w/ ATmega328P +## -------------------------- +#nano.menu.cpu.atmega328=ATmega328P + +#nano.menu.cpu.atmega328.upload.maximum_size=30720 +#nano.menu.cpu.atmega328.upload.maximum_data_size=2048 +#nano.menu.cpu.atmega328.upload.speed=115200 + +#nano.menu.cpu.atmega328.bootloader.low_fuses=0xFF +#nano.menu.cpu.atmega328.bootloader.high_fuses=0xDA +#nano.menu.cpu.atmega328.bootloader.extended_fuses=0xFD +#nano.menu.cpu.atmega328.bootloader.file=optiboot/optiboot_atmega328.hex + +#nano.menu.cpu.atmega328.build.mcu=atmega328p + +## Arduino Nano w/ ATmega328P (old bootloader) +## -------------------------- +#nano.menu.cpu.atmega328old=ATmega328P (Old Bootloader) + +#nano.menu.cpu.atmega328old.upload.maximum_size=30720 +#nano.menu.cpu.atmega328old.upload.maximum_data_size=2048 +#nano.menu.cpu.atmega328old.upload.speed=57600 + +#nano.menu.cpu.atmega328old.bootloader.low_fuses=0xFF +#nano.menu.cpu.atmega328old.bootloader.high_fuses=0xDA +#nano.menu.cpu.atmega328old.bootloader.extended_fuses=0xFD +#nano.menu.cpu.atmega328old.bootloader.file=atmega/ATmegaBOOT_168_atmega328.hex + +#nano.menu.cpu.atmega328old.build.mcu=atmega328p + +## Arduino Nano w/ ATmega168 +## ------------------------- +#nano.menu.cpu.atmega168=ATmega168 + +#nano.menu.cpu.atmega168.upload.maximum_size=14336 +#nano.menu.cpu.atmega168.upload.maximum_data_size=1024 +#nano.menu.cpu.atmega168.upload.speed=19200 + +#nano.menu.cpu.atmega168.bootloader.low_fuses=0xff +#nano.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#nano.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#nano.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_diecimila.hex + +#nano.menu.cpu.atmega168.build.mcu=atmega168 + +############################################################## + +#mega.name=Arduino Mega or Mega 2560 ((LUFA not supported)) + +#mega.vid.0=0x2341 +#mega.pid.0=0x0010 +#mega.vid.1=0x2341 +#mega.pid.1=0x0042 +#mega.vid.2=0x2A03 +#mega.pid.2=0x0010 +#mega.vid.3=0x2A03 +#mega.pid.3=0x0042 +#mega.vid.4=0x2341 +#mega.pid.4=0x0210 +#mega.vid.5=0x2341 +#mega.pid.5=0x0242 + +#mega.upload.tool=avrdude +#mega.upload.maximum_data_size=8192 + +#mega.bootloader.tool=avrdude +#mega.bootloader.low_fuses=0xFF +#mega.bootloader.unlock_bits=0x3F +#mega.bootloader.lock_bits=0x0F + +#mega.build.f_cpu=16000000L +#mega.build.core=arduino +#mega.build.variant=mega +# default board may be overridden by the cpu menu +#mega.build.board=AVR_MEGA2560 + +## Arduino Mega w/ ATmega2560 +## ------------------------- +#mega.menu.cpu.atmega2560=ATmega2560 (Mega 2560) + +#mega.menu.cpu.atmega2560.upload.protocol=wiring +#mega.menu.cpu.atmega2560.upload.maximum_size=253952 +#mega.menu.cpu.atmega2560.upload.speed=115200 + +#mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD8 +#mega.menu.cpu.atmega2560.bootloader.extended_fuses=0xFD +#mega.menu.cpu.atmega2560.bootloader.file=stk500v2/stk500boot_v2_mega2560.hex + +#mega.menu.cpu.atmega2560.build.mcu=atmega2560 +#mega.menu.cpu.atmega2560.build.board=AVR_MEGA2560 + +## Arduino Mega w/ ATmega1280 +## ------------------------- +#mega.menu.cpu.atmega1280=ATmega1280 + +#mega.menu.cpu.atmega1280.upload.protocol=arduino +#mega.menu.cpu.atmega1280.upload.maximum_size=126976 +#mega.menu.cpu.atmega1280.upload.speed=57600 + +#mega.menu.cpu.atmega1280.bootloader.high_fuses=0xDA +#mega.menu.cpu.atmega1280.bootloader.extended_fuses=0xF5 +#mega.menu.cpu.atmega1280.bootloader.file=atmega/ATmegaBOOT_168_atmega1280.hex + +#mega.menu.cpu.atmega1280.build.mcu=atmega1280 +#mega.menu.cpu.atmega1280.build.board=AVR_MEGA + +############################################################## + +#megaADK.name=Arduino Mega ADK ((LUFA not supported)) + +#megaADK.vid.0=0x2341 +#megaADK.pid.0=0x003f +#megaADK.vid.1=0x2341 +#megaADK.pid.1=0x0044 +#megaADK.vid.2=0x2A03 +#megaADK.pid.2=0x003f +#megaADK.vid.3=0x2A03 +#megaADK.pid.3=0x0044 + +#megaADK.upload.tool=avrdude +#megaADK.upload.protocol=wiring +#megaADK.upload.maximum_size=253952 +#megaADK.upload.maximum_data_size=8192 +#megaADK.upload.speed=115200 + +#megaADK.bootloader.tool=avrdude +#megaADK.bootloader.low_fuses=0xFF +#megaADK.bootloader.high_fuses=0xD8 +#megaADK.bootloader.extended_fuses=0xFD +#megaADK.bootloader.file=stk500v2/stk500boot_v2_mega2560.hex +#megaADK.bootloader.unlock_bits=0x3F +#megaADK.bootloader.lock_bits=0x0F + +#megaADK.build.mcu=atmega2560 +#megaADK.build.f_cpu=16000000L +#megaADK.build.board=AVR_ADK +#megaADK.build.core=arduino +#megaADK.build.variant=mega + +############################################################## + +leonardo.name=Arduino Leonardo (LUFA) +leonardo.vid.0=0x2341 +leonardo.pid.0=0x0036 +leonardo.vid.1=0x2341 +leonardo.pid.1=0x8036 +leonardo.vid.2=0x2A03 +leonardo.pid.2=0x0036 +leonardo.vid.3=0x2A03 +leonardo.pid.3=0x8036 + +leonardo.upload.tool=avrdude +leonardo.upload.protocol=avr109 +leonardo.upload.maximum_size=28672 +leonardo.upload.maximum_data_size=2560 +leonardo.upload.speed=57600 +leonardo.upload.disable_flushing=true +leonardo.upload.use_1200bps_touch=true +leonardo.upload.wait_for_upload_port=true + +leonardo.bootloader.tool=avrdude +leonardo.bootloader.low_fuses=0xff +leonardo.bootloader.high_fuses=0xd8 +leonardo.bootloader.extended_fuses=0xcb +leonardo.bootloader.file=caterina/Caterina-Leonardo.hex +leonardo.bootloader.unlock_bits=0x3F +leonardo.bootloader.lock_bits=0x2F + +leonardo.build.mcu=atmega32u4 +leonardo.build.f_cpu=16000000L +leonardo.build.vid=0x2341 +leonardo.build.pid=0x8036 +leonardo.build.usb_product="Arduino Leonardo" +leonardo.build.board=AVR_LEONARDO +leonardo.build.core=arduino +leonardo.build.variant=leonardo +leonardo.build.extra_flags={build.usb_flags} + +############################################################## + +leonardoeth.name=Arduino Leonardo ETH (LUFA) +leonardoeth.vid.0=0x2a03 +leonardoeth.pid.0=0x0040 +leonardoeth.vid.1=0x2a03 +leonardoeth.pid.1=0x8040 + +leonardoeth.upload.tool=avrdude +leonardoeth.upload.protocol=avr109 +leonardoeth.upload.maximum_size=28672 +leonardoeth.upload.maximum_data_size=2560 +leonardoeth.upload.speed=57600 +leonardoeth.upload.disable_flushing=true +leonardoeth.upload.use_1200bps_touch=true +leonardoeth.upload.wait_for_upload_port=true + +leonardoeth.bootloader.tool=avrdude +leonardoeth.bootloader.low_fuses=0xff +leonardoeth.bootloader.high_fuses=0xd8 +leonardoeth.bootloader.extended_fuses=0xcb +leonardoeth.bootloader.file=caterina/Caterina-LeonardoEthernet.hex +leonardoeth.bootloader.unlock_bits=0x3F +leonardoeth.bootloader.lock_bits=0x2F + +leonardoeth.build.mcu=atmega32u4 +leonardoeth.build.f_cpu=16000000L +leonardoeth.build.vid=0x2a03 +leonardoeth.build.pid=0x8040 +leonardoeth.build.usb_product="Arduino Leonardo ETH" +leonardoeth.build.board=AVR_LEONARDO_ETH +leonardoeth.build.core=arduino +leonardoeth.build.variant=leonardo +leonardoeth.build.extra_flags={build.usb_flags} + +############################################################## + +micro.name=Arduino Micro (LUFA) + +micro.vid.0=0x2341 +micro.pid.0=0x0037 +micro.vid.1=0x2341 +micro.pid.1=0x8037 +micro.vid.2=0x2A03 +micro.pid.2=0x0037 +micro.vid.3=0x2A03 +micro.pid.3=0x8037 +micro.vid.4=0x2341 +micro.pid.4=0x0237 +micro.vid.5=0x2341 +micro.pid.5=0x8237 + +micro.upload.tool=avrdude +micro.upload.protocol=avr109 +micro.upload.maximum_size=28672 +micro.upload.maximum_data_size=2560 +micro.upload.speed=57600 +micro.upload.disable_flushing=true +micro.upload.use_1200bps_touch=true +micro.upload.wait_for_upload_port=true + +micro.bootloader.tool=avrdude +micro.bootloader.low_fuses=0xff +micro.bootloader.high_fuses=0xd8 +micro.bootloader.extended_fuses=0xcb +micro.bootloader.file=caterina/Caterina-Micro.hex +micro.bootloader.unlock_bits=0x3F +micro.bootloader.lock_bits=0x2F + +micro.build.mcu=atmega32u4 +micro.build.f_cpu=16000000L +micro.build.vid=0x2341 +micro.build.pid=0x8037 +micro.build.usb_product="Arduino Micro" +micro.build.board=AVR_MICRO +micro.build.core=arduino +micro.build.variant=micro +micro.build.extra_flags={build.usb_flags} + +############################################################## + +esplora.name=Arduino Esplora (LUFA) +esplora.vid.0=0x2341 +esplora.pid.0=0x003C +esplora.vid.1=0x2341 +esplora.pid.1=0x803C +esplora.vid.2=0x2A03 +esplora.pid.2=0x003C +esplora.vid.3=0x2A03 +esplora.pid.3=0x803C + +esplora.upload.tool=avrdude +esplora.upload.protocol=avr109 +esplora.upload.maximum_size=28672 +esplora.upload.maximum_data_size=2560 +esplora.upload.speed=57600 +esplora.upload.disable_flushing=true +esplora.upload.use_1200bps_touch=true +esplora.upload.wait_for_upload_port=true + +esplora.bootloader.tool=avrdude +esplora.bootloader.low_fuses=0xff +esplora.bootloader.high_fuses=0xd8 +esplora.bootloader.extended_fuses=0xcb +esplora.bootloader.file=caterina/Caterina-Esplora.hex +esplora.bootloader.unlock_bits=0x3F +esplora.bootloader.lock_bits=0x2F + +esplora.build.mcu=atmega32u4 +esplora.build.f_cpu=16000000L +esplora.build.vid=0x2341 +esplora.build.pid=0x803c +esplora.build.usb_product="Arduino Esplora" +esplora.build.board=AVR_ESPLORA +esplora.build.core=arduino +esplora.build.variant=leonardo +esplora.build.extra_flags={build.usb_flags} + +############################################################## + +#mini.name=Arduino Mini ((LUFA not supported)) + +#mini.upload.tool=avrdude +#mini.upload.protocol=arduino + +#mini.bootloader.tool=avrdude +#mini.bootloader.low_fuses=0xff +#mini.bootloader.unlock_bits=0x3F +#mini.bootloader.lock_bits=0x0F + +#mini.build.f_cpu=16000000L +#mini.build.board=AVR_MINI +#mini.build.core=arduino +#mini.build.variant=eightanaloginputs + +## Arduino Mini w/ ATmega328P +## -------------------------- +#mini.menu.cpu.atmega328=ATmega328P + +#mini.menu.cpu.atmega328.upload.maximum_size=28672 +#mini.menu.cpu.atmega328.upload.maximum_data_size=2048 +#mini.menu.cpu.atmega328.upload.speed=115200 + +#mini.menu.cpu.atmega328.bootloader.high_fuses=0xd8 +#mini.menu.cpu.atmega328.bootloader.extended_fuses=0xFD +#mini.menu.cpu.atmega328.bootloader.file=optiboot/optiboot_atmega328-Mini.hex + +#mini.menu.cpu.atmega328.build.mcu=atmega328p + +## Arduino Mini w/ ATmega168 +## ------------------------- +#mini.menu.cpu.atmega168=ATmega168 + +#mini.menu.cpu.atmega168.upload.maximum_size=14336 +#mini.menu.cpu.atmega168.upload.maximum_data_size=1024 +#mini.menu.cpu.atmega168.upload.speed=19200 + +#mini.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#mini.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#mini.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_ng.hex + +#mini.menu.cpu.atmega168.build.mcu=atmega168 + +############################################################## + +#ethernet.name=Arduino Ethernet ((LUFA not supported)) + +#ethernet.upload.tool=avrdude +#ethernet.upload.protocol=arduino +#ethernet.upload.maximum_size=32256 +#ethernet.upload.maximum_data_size=2048 +#ethernet.upload.speed=115200 + +#ethernet.bootloader.tool=avrdude +#ethernet.bootloader.low_fuses=0xff +#ethernet.bootloader.high_fuses=0xde +#ethernet.bootloader.extended_fuses=0xFD +#ethernet.bootloader.file=optiboot/optiboot_atmega328.hex +#ethernet.bootloader.unlock_bits=0x3F +#ethernet.bootloader.lock_bits=0x0F + +#ethernet.build.variant=ethernet +#ethernet.build.mcu=atmega328p +#ethernet.build.f_cpu=16000000L +#ethernet.build.board=AVR_ETHERNET +#ethernet.build.core=arduino + +############################################################## + +#fio.name=Arduino Fio ((LUFA not supported)) + +#fio.upload.tool=avrdude +#fio.upload.protocol=arduino +#fio.upload.maximum_size=30720 +#fio.upload.maximum_data_size=2048 +#fio.upload.speed=57600 + +#fio.bootloader.tool=avrdude +#fio.bootloader.low_fuses=0xFF +#fio.bootloader.high_fuses=0xDA +#fio.bootloader.extended_fuses=0xFD +#fio.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex +#fio.bootloader.unlock_bits=0x3F +#fio.bootloader.lock_bits=0x0F + +#fio.build.mcu=atmega328p +#fio.build.f_cpu=8000000L +#fio.build.board=AVR_FIO +#fio.build.core=arduino +#fio.build.variant=eightanaloginputs + +############################################################## + +#bt.name=Arduino BT ((LUFA not supported)) + +#bt.upload.tool=avrdude +#bt.upload.protocol=arduino +#bt.upload.speed=19200 +#bt.upload.disable_flushing=true + +#bt.bootloader.tool=avrdude +#bt.bootloader.low_fuses=0xff +#bt.bootloader.unlock_bits=0x3F +#bt.bootloader.lock_bits=0x0F + +#bt.build.f_cpu=16000000L +#bt.build.board=AVR_BT +#bt.build.core=arduino +#bt.build.variant=eightanaloginputs + +## Arduino BT w/ ATmega328P +## ------------------------ +#bt.menu.cpu.atmega328=ATmega328P +#bt.menu.cpu.atmega328.upload.maximum_size=28672 +#bt.menu.cpu.atmega328.upload.maximum_data_size=2048 + +#bt.menu.cpu.atmega328.bootloader.high_fuses=0xd8 +#bt.menu.cpu.atmega328.bootloader.extended_fuses=0xFD +#bt.menu.cpu.atmega328.bootloader.file=bt/ATmegaBOOT_168_atmega328_bt.hex + +#bt.menu.cpu.atmega328.build.mcu=atmega328p + +## Arduino BT w/ ATmega168 +## ----------------------- +#bt.menu.cpu.atmega168=ATmega168 +#bt.menu.cpu.atmega168.upload.maximum_size=14336 +#bt.menu.cpu.atmega168.upload.maximum_data_size=1024 + +#bt.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#bt.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#bt.menu.cpu.atmega168.bootloader.file=bt/ATmegaBOOT_168.hex + +#bt.menu.cpu.atmega168.build.mcu=atmega168 + +############################################################## + +LilyPadUSB.name=LilyPad Arduino USB (LUFA) +LilyPadUSB.vid.0=0x1B4F +LilyPadUSB.pid.0=0x9207 +LilyPadUSB.vid.1=0x1B4F +LilyPadUSB.pid.1=0x9208 + +LilyPadUSB.upload.tool=avrdude +LilyPadUSB.upload.protocol=avr109 +LilyPadUSB.upload.maximum_size=28672 +LilyPadUSB.upload.maximum_data_size=2560 +LilyPadUSB.upload.speed=57600 +LilyPadUSB.upload.disable_flushing=true +LilyPadUSB.upload.use_1200bps_touch=true +LilyPadUSB.upload.wait_for_upload_port=true + +LilyPadUSB.bootloader.tool=avrdude +LilyPadUSB.bootloader.low_fuses=0xff +LilyPadUSB.bootloader.high_fuses=0xd8 +LilyPadUSB.bootloader.extended_fuses=0xce +LilyPadUSB.bootloader.file=caterina-LilyPadUSB/Caterina-LilyPadUSB.hex +LilyPadUSB.bootloader.unlock_bits=0x3F +LilyPadUSB.bootloader.lock_bits=0x2F + +LilyPadUSB.build.mcu=atmega32u4 +LilyPadUSB.build.f_cpu=8000000L +LilyPadUSB.build.vid=0x1B4F +LilyPadUSB.build.pid=0x9208 +LilyPadUSB.build.usb_product="LilyPad USB" +LilyPadUSB.build.board=AVR_LILYPAD_USB +LilyPadUSB.build.core=arduino +LilyPadUSB.build.variant=leonardo +LilyPadUSB.build.extra_flags={build.usb_flags} + +############################################################## + +#lilypad.name=LilyPad Arduino ((LUFA not supported)) + +#lilypad.upload.tool=avrdude +#lilypad.upload.protocol=arduino + +#lilypad.bootloader.tool=avrdude +#lilypad.bootloader.unlock_bits=0x3F +#lilypad.bootloader.lock_bits=0x0F + +#lilypad.build.f_cpu=8000000L +#lilypad.build.board=AVR_LILYPAD +#lilypad.build.core=arduino +#lilypad.build.variant=standard + +## LilyPad Arduino w/ ATmega328P +## ----------------------------- +#lilypad.menu.cpu.atmega328=ATmega328P + +#lilypad.menu.cpu.atmega328.upload.maximum_size=30720 +#lilypad.menu.cpu.atmega328.upload.maximum_data_size=2048 +#lilypad.menu.cpu.atmega328.upload.speed=57600 + +#lilypad.menu.cpu.atmega328.bootloader.low_fuses=0xFF +#lilypad.menu.cpu.atmega328.bootloader.high_fuses=0xDA +#lilypad.menu.cpu.atmega328.bootloader.extended_fuses=0xFD +#lilypad.menu.cpu.atmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex + +#lilypad.menu.cpu.atmega328.build.mcu=atmega328p + +## LilyPad Arduino w/ ATmega168 +## ---------------------------- +#lilypad.menu.cpu.atmega168=ATmega168 + +#lilypad.menu.cpu.atmega168.upload.maximum_size=14336 +#lilypad.menu.cpu.atmega168.upload.maximum_data_size=1024 +#lilypad.menu.cpu.atmega168.upload.speed=19200 + +#lilypad.menu.cpu.atmega168.bootloader.low_fuses=0xe2 +#lilypad.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#lilypad.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#lilypad.menu.cpu.atmega168.bootloader.file=lilypad/LilyPadBOOT_168.hex + +#lilypad.menu.cpu.atmega168.build.mcu=atmega168 + +############################################################## + +#pro.name=Arduino Pro or Pro Mini ((LUFA not supported)) + +#pro.upload.tool=avrdude +#pro.upload.protocol=arduino + +#pro.bootloader.tool=avrdude +#pro.bootloader.unlock_bits=0x3F +#pro.bootloader.lock_bits=0x0F + +#pro.build.board=AVR_PRO +#pro.build.core=arduino +#pro.build.variant=eightanaloginputs + +## Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328P +## -------------------------------------------------- +#pro.menu.cpu.16MHzatmega328=ATmega328P (5V, 16 MHz) + +#pro.menu.cpu.16MHzatmega328.upload.maximum_size=30720 +#pro.menu.cpu.16MHzatmega328.upload.maximum_data_size=2048 +#pro.menu.cpu.16MHzatmega328.upload.speed=57600 + +#pro.menu.cpu.16MHzatmega328.bootloader.low_fuses=0xFF +#pro.menu.cpu.16MHzatmega328.bootloader.high_fuses=0xDA +#pro.menu.cpu.16MHzatmega328.bootloader.extended_fuses=0xFD +#pro.menu.cpu.16MHzatmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328.hex + +#pro.menu.cpu.16MHzatmega328.build.mcu=atmega328p +#pro.menu.cpu.16MHzatmega328.build.f_cpu=16000000L + +## Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328P +## --------------------------------------------------- +#pro.menu.cpu.8MHzatmega328=ATmega328P (3.3V, 8 MHz) + +#pro.menu.cpu.8MHzatmega328.upload.maximum_size=30720 +#pro.menu.cpu.8MHzatmega328.upload.maximum_data_size=2048 +#pro.menu.cpu.8MHzatmega328.upload.speed=57600 + +#pro.menu.cpu.8MHzatmega328.bootloader.low_fuses=0xFF +#pro.menu.cpu.8MHzatmega328.bootloader.high_fuses=0xDA +#pro.menu.cpu.8MHzatmega328.bootloader.extended_fuses=0xFD +#pro.menu.cpu.8MHzatmega328.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex + +#pro.menu.cpu.8MHzatmega328.build.mcu=atmega328p +#pro.menu.cpu.8MHzatmega328.build.f_cpu=8000000L + +## Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168 +## ------------------------------------------------- +#pro.menu.cpu.16MHzatmega168=ATmega168 (5V, 16 MHz) + +#pro.menu.cpu.16MHzatmega168.upload.maximum_size=14336 +#pro.menu.cpu.16MHzatmega168.upload.maximum_data_size=1024 +#pro.menu.cpu.16MHzatmega168.upload.speed=19200 + +#pro.menu.cpu.16MHzatmega168.bootloader.low_fuses=0xff +#pro.menu.cpu.16MHzatmega168.bootloader.high_fuses=0xdd +#pro.menu.cpu.16MHzatmega168.bootloader.extended_fuses=0xF8 +#pro.menu.cpu.16MHzatmega168.bootloader.file=atmega/ATmegaBOOT_168_diecimila.hex + +#pro.menu.cpu.16MHzatmega168.build.mcu=atmega168 +#pro.menu.cpu.16MHzatmega168.build.f_cpu=16000000L + +## Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168 +## -------------------------------------------------- +#pro.menu.cpu.8MHzatmega168=ATmega168 (3.3V, 8 MHz) + +#pro.menu.cpu.8MHzatmega168.upload.maximum_size=14336 +#pro.menu.cpu.8MHzatmega168.upload.maximum_data_size=1024 +#pro.menu.cpu.8MHzatmega168.upload.speed=19200 + +#pro.menu.cpu.8MHzatmega168.bootloader.low_fuses=0xc6 +#pro.menu.cpu.8MHzatmega168.bootloader.high_fuses=0xdd +#pro.menu.cpu.8MHzatmega168.bootloader.extended_fuses=0xF8 +#pro.menu.cpu.8MHzatmega168.bootloader.file=atmega/ATmegaBOOT_168_pro_8MHz.hex + +#pro.menu.cpu.8MHzatmega168.build.mcu=atmega168 +#pro.menu.cpu.8MHzatmega168.build.f_cpu=8000000L + +############################################################## + +#atmegang.name=Arduino NG or older ((LUFA not supported)) + +#atmegang.upload.tool=avrdude +#atmegang.upload.protocol=arduino +#atmegang.upload.speed=19200 + +#atmegang.bootloader.tool=avrdude +#atmegang.bootloader.unlock_bits=0x3F +#atmegang.bootloader.lock_bits=0x0F + +#atmegang.build.mcu=atmegang +#atmegang.build.f_cpu=16000000L +#atmegang.build.board=AVR_NG +#atmegang.build.core=arduino +#atmegang.build.variant=standard + +## Arduino NG or older w/ ATmega168 +## -------------------------------- +#atmegang.menu.cpu.atmega168=ATmega168 + +#atmegang.menu.cpu.atmega168.upload.maximum_size=14336 +#atmegang.menu.cpu.atmega168.upload.maximum_data_size=1024 + +#atmegang.menu.cpu.atmega168.bootloader.low_fuses=0xff +#atmegang.menu.cpu.atmega168.bootloader.high_fuses=0xdd +#atmegang.menu.cpu.atmega168.bootloader.extended_fuses=0xF8 +#atmegang.menu.cpu.atmega168.bootloader.file=atmega/ATmegaBOOT_168_ng.hex + +#atmegang.menu.cpu.atmega168.build.mcu=atmega168 + +## Arduino NG or older w/ ATmega8 +## ------------------------------ +#atmegang.menu.cpu.atmega8=ATmega8 + +#atmegang.menu.cpu.atmega8.upload.maximum_size=7168 +#atmegang.menu.cpu.atmega8.upload.maximum_data_size=1024 + +#atmegang.menu.cpu.atmega8.bootloader.low_fuses=0xdf +#atmegang.menu.cpu.atmega8.bootloader.high_fuses=0xca +#atmegang.menu.cpu.atmega8.bootloader.extended_fuses= +#atmegang.menu.cpu.atmega8.bootloader.file=atmega8/ATmegaBOOT-prod-firmware-2009-11-07.hex + +#atmegang.menu.cpu.atmega8.build.mcu=atmega8 + +############################################################## + +robotControl.name=Arduino Robot Control (LUFA) +robotControl.vid.0=0x2341 +robotControl.pid.0=0x0038 +robotControl.vid.1=0x2341 +robotControl.pid.1=0x8038 +robotControl.vid.2=0x2A03 +robotControl.pid.2=0x0038 +robotControl.vid.3=0x2A03 +robotControl.pid.3=0x8038 + +robotControl.upload.tool=avrdude +robotControl.upload.protocol=avr109 +robotControl.upload.maximum_size=28672 +robotControl.upload.maximum_data_size=2560 +robotControl.upload.speed=57600 +robotControl.upload.disable_flushing=true +robotControl.upload.use_1200bps_touch=true +robotControl.upload.wait_for_upload_port=true + +robotControl.bootloader.tool=avrdude +robotControl.bootloader.low_fuses=0xff +robotControl.bootloader.high_fuses=0xd8 +robotControl.bootloader.extended_fuses=0xcb +robotControl.bootloader.file=caterina-Arduino_Robot/Caterina-Robot-Control.hex +robotControl.bootloader.unlock_bits=0x3F +robotControl.bootloader.lock_bits=0x2F + +robotControl.build.mcu=atmega32u4 +robotControl.build.f_cpu=16000000L +robotControl.build.vid=0x2341 +robotControl.build.pid=0x8038 +robotControl.build.usb_product="Robot Control" +robotControl.build.board=AVR_ROBOT_CONTROL +robotControl.build.core=arduino +robotControl.build.variant=robot_control +robotControl.build.extra_flags={build.usb_flags} + +############################################################## + +robotMotor.name=Arduino Robot Motor (LUFA) +robotMotor.vid.0=0x2341 +robotMotor.pid.0=0x0039 +robotMotor.vid.1=0x2341 +robotMotor.pid.1=0x8039 +robotMotor.vid.2=0x2A03 +robotMotor.pid.2=0x0039 +robotMotor.vid.3=0x2A03 +robotMotor.pid.3=0x8039 + +robotMotor.upload.tool=avrdude +robotMotor.upload.protocol=avr109 +robotMotor.upload.maximum_size=28672 +robotMotor.upload.maximum_data_size=2560 +robotMotor.upload.speed=57600 +robotMotor.upload.disable_flushing=true +robotMotor.upload.use_1200bps_touch=true +robotMotor.upload.wait_for_upload_port=true + +robotMotor.bootloader.tool=avrdude +robotMotor.bootloader.low_fuses=0xff +robotMotor.bootloader.high_fuses=0xd8 +robotMotor.bootloader.extended_fuses=0xcb +robotMotor.bootloader.file=caterina-Arduino_Robot/Caterina-Robot-Motor.hex +robotMotor.bootloader.unlock_bits=0x3F +robotMotor.bootloader.lock_bits=0x2F + +robotMotor.build.mcu=atmega32u4 +robotMotor.build.f_cpu=16000000L +robotMotor.build.vid=0x2341 +robotMotor.build.pid=0x8039 +robotMotor.build.usb_product="Robot Motor" +robotMotor.build.board=AVR_ROBOT_MOTOR +robotMotor.build.core=arduino +robotMotor.build.variant=robot_motor +robotMotor.build.extra_flags={build.usb_flags} + +############################################################## + +#gemma.vid.0=0x2341 +#gemma.pid.0=0x0c9f + +#gemma.name=Arduino Gemma ((LUFA not supported)) + +#gemma.bootloader.low_fuses=0xF1 +#gemma.bootloader.high_fuses=0xD5 +#gemma.bootloader.extended_fuses=0xFE +#gemma.bootloader.tool=avrdude +#gemma.bootloader.lock_bits= +#gemma.bootloader.unlock_bits= +#gemma.bootloader.file=gemma/gemma_v1.hex + +#gemma.build.mcu=attiny85 +#gemma.build.f_cpu=8000000L +#gemma.build.core=arduino +#gemma.build.variant=gemma +#gemma.build.board=AVR_GEMMA + +#gemma.upload.tool=avrdude +#gemma.upload.maximum_size=5310 + +############################################################## + +# Adafruit Circuit Playground 32u4 w/Caterina Configuration +circuitplay32u4cat.name=Adafruit Circuit Playground (LUFA) +circuitplay32u4cat.bootloader.low_fuses=0xff +circuitplay32u4cat.bootloader.high_fuses=0xd8 +circuitplay32u4cat.bootloader.extended_fuses=0xcb +circuitplay32u4cat.bootloader.file=caterina/Caterina-Circuitplay32u4.hex +circuitplay32u4cat.bootloader.unlock_bits=0x3F +circuitplay32u4cat.bootloader.lock_bits=0x2F +circuitplay32u4cat.bootloader.tool=avrdude +circuitplay32u4cat.build.mcu=atmega32u4 +circuitplay32u4cat.build.f_cpu=8000000L +circuitplay32u4cat.build.vid=0x239A +circuitplay32u4cat.build.pid=0x8011 +circuitplay32u4cat.build.core=arduino +circuitplay32u4cat.build.variant=circuitplay32u4 +circuitplay32u4cat.build.board=AVR_CIRCUITPLAY +circuitplay32u4cat.build.usb_product="Circuit Playground" +circuitplay32u4cat.build.usb_manufacturer="Adafruit" +circuitplay32u4cat.build.extra_flags={build.usb_flags} +circuitplay32u4cat.upload.protocol=avr109 +circuitplay32u4cat.upload.maximum_size=28672 +circuitplay32u4cat.upload.speed=57600 +circuitplay32u4cat.upload.disable_flushing=true +circuitplay32u4cat.upload.use_1200bps_touch=true +circuitplay32u4cat.upload.wait_for_upload_port=true +circuitplay32u4cat.upload.tool=avrdude +circuitplay32u4cat.vid.0=0x239A +circuitplay32u4cat.pid.0=0x8011 + +############################################################## + +yunmini.name=Arduino Yún Mini (LUFA) +yunmini.upload.via_ssh=true + +yunmini.vid.0=0x2a03 +yunmini.pid.0=0x0050 +yunmini.vid.1=0x2a03 +yunmini.pid.1=0x8050 + +yunmini.upload.tool=avrdude +yunmini.upload.protocol=avr109 +yunmini.upload.maximum_size=28672 +yunmini.upload.maximum_data_size=2560 +yunmini.upload.speed=57600 +yunmini.upload.disable_flushing=true +yunmini.upload.use_1200bps_touch=true +yunmini.upload.wait_for_upload_port=true + +yunmini.bootloader.tool=avrdude +yunmini.bootloader.low_fuses=0xff +yunmini.bootloader.high_fuses=0xd8 +yunmini.bootloader.extended_fuses=0xfb +yunmini.bootloader.file=caterina/Caterina-YunMini.hex +yunmini.bootloader.unlock_bits=0x3F +yunmini.bootloader.lock_bits=0x2F + +yunmini.build.mcu=atmega32u4 +yunmini.build.f_cpu=16000000L +yunmini.build.vid=0x2a03 +yunmini.build.pid=0x8050 +yunmini.build.usb_product="Arduino Yún Mini" +yunmini.build.board=AVR_YUNMINI +yunmini.build.core=arduino +yunmini.build.variant=yun +yunmini.build.extra_flags={build.usb_flags} + +############################################################## + +chiwawa.name=Arduino Industrial 101 (LUFA) +chiwawa.upload.via_ssh=true + +chiwawa.vid.0=0x2a03 +chiwawa.pid.0=0x0056 +chiwawa.vid.1=0x2a03 +chiwawa.pid.1=0x8056 + +chiwawa.upload.tool=avrdude +chiwawa.upload.protocol=avr109 +chiwawa.upload.maximum_size=28672 +chiwawa.upload.maximum_data_size=2560 +chiwawa.upload.speed=57600 +chiwawa.upload.disable_flushing=true +chiwawa.upload.use_1200bps_touch=true +chiwawa.upload.wait_for_upload_port=true + +chiwawa.bootloader.tool=avrdude +chiwawa.bootloader.low_fuses=0xff +chiwawa.bootloader.high_fuses=0xd8 +chiwawa.bootloader.extended_fuses=0xfb +chiwawa.bootloader.file=caterina/Caterina-Industrial101.hex +chiwawa.bootloader.unlock_bits=0x3F +chiwawa.bootloader.lock_bits=0x2F + +chiwawa.build.mcu=atmega32u4 +chiwawa.build.f_cpu=16000000L +chiwawa.build.vid=0x2a03 +chiwawa.build.pid=0x8056 +chiwawa.build.usb_product="Arduino Industrial 101" +chiwawa.build.board=AVR_INDUSTRIAL101 +chiwawa.build.core=arduino +chiwawa.build.variant=yun +chiwawa.build.extra_flags={build.usb_flags} + +############################################################## + +one.name=Linino One (LUFA) +one.upload.via_ssh=true + +one.vid.0=0x2a03 +one.pid.0=0x0001 +one.vid.1=0x2a03 +one.pid.1=0x8001 + +one.upload.tool=avrdude +one.upload.protocol=avr109 +one.upload.maximum_size=28672 +one.upload.maximum_data_size=2560 +one.upload.speed=57600 +one.upload.disable_flushing=true +one.upload.use_1200bps_touch=true +one.upload.wait_for_upload_port=true + +one.bootloader.tool=avrdude +one.bootloader.low_fuses=0xff +one.bootloader.high_fuses=0xd8 +one.bootloader.extended_fuses=0xfb +one.bootloader.file=caterina/Caterina-LininoOne.hex +one.bootloader.unlock_bits=0x3F +one.bootloader.lock_bits=0x2F + +one.build.mcu=atmega32u4 +one.build.f_cpu=16000000L +one.build.vid=0x2a03 +one.build.pid=0x8001 +one.build.usb_product="Linino One" +one.build.board=AVR_LININO_ONE +one.build.core=arduino +one.build.variant=yun +one.build.extra_flags={build.usb_flags} + +############################################################## + +#unowifi.name=Arduino Uno WiFi ((LUFA not supported)) +#unowifi.vid.0=0x2A03 +#unowifi.pid.0=0x0057 + +#unowifi.upload.tool=avrdude +#unowifi.upload.protocol=arduino +#unowifi.upload.maximum_size=32256 +#unowifi.upload.maximum_data_size=2048 +#unowifi.upload.speed=115200 +#unowifi.upload.network.endpoint_upload=/pgm/upload +#unowifi.upload.network.endpoint_sync=/pgm/sync +#unowifi.upload.network.sync_return=204:SYNC +#unowifi.upload.network.endpoint_reset=/log/reset +#unowifi.upload.network.port=80 + +#unowifi.bootloader.tool=avrdude +#unowifi.bootloader.low_fuses=0xFF +#unowifi.bootloader.high_fuses=0xDE +#unowifi.bootloader.extended_fuses=0x05 +#unowifi.bootloader.unlock_bits=0x3F +#unowifi.bootloader.lock_bits=0x0F +#unowifi.bootloader.file=optiboot/optiboot_atmega328.hex + +#unowifi.build.mcu=atmega328p +#unowifi.build.f_cpu=16000000L +#unowifi.build.board=AVR_UNO_WIFI_DEV_ED +#unowifi.build.core=arduino +#unowifi.build.variant=standard +#unowifi.build.esp_ch_uart_br=19200 +#unowifi.build.extra_flags=-DESP_CH_UART -DESP_CH_UART_BR={build.esp_ch_uart_br} diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h new file mode 100644 index 0000000..8b1ba7f --- /dev/null +++ b/cores/arduino/Arduino.h @@ -0,0 +1,268 @@ +/* + Arduino.h - Main include file for the Arduino SDK + Copyright (c) 2005-2013 Arduino Team. 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 +*/ + +#ifndef Arduino_h +#define Arduino_h + +#include +#include +#include +#include + +#include +#include +#include + +#include "binary.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +void yield(void); + +#define HIGH 0x1 +#define LOW 0x0 + +#define INPUT 0x0 +#define OUTPUT 0x1 +#define INPUT_PULLUP 0x2 + +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 +#define EULER 2.718281828459045235360287471352 + +#define SERIAL 0x0 +#define DISPLAY 0x1 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define CHANGE 1 +#define FALLING 2 +#define RISING 3 + +#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + #define DEFAULT 0 + #define EXTERNAL 1 + #define INTERNAL1V1 2 + #define INTERNAL INTERNAL1V1 +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + #define DEFAULT 0 + #define EXTERNAL 4 + #define INTERNAL1V1 8 + #define INTERNAL INTERNAL1V1 + #define INTERNAL2V56 9 + #define INTERNAL2V56_EXTCAP 13 +#else +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) +#define INTERNAL1V1 2 +#define INTERNAL2V56 3 +#else +#define INTERNAL 3 +#endif +#define DEFAULT 1 +#define EXTERNAL 0 +#endif + +// undefine stdlib's abs if encountered +#ifdef abs +#undef abs +#endif + +#define min(a,b) ((a)<(b)?(a):(b)) +#define max(a,b) ((a)>(b)?(a):(b)) +#define abs(x) ((x)>0?(x):-(x)) +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) +#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) +#define radians(deg) ((deg)*DEG_TO_RAD) +#define degrees(rad) ((rad)*RAD_TO_DEG) +#define sq(x) ((x)*(x)) + +#define interrupts() sei() +#define noInterrupts() cli() + +#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) +#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) +#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) + +#define lowByte(w) ((uint8_t) ((w) & 0xff)) +#define highByte(w) ((uint8_t) ((w) >> 8)) + +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitSet(value, bit) ((value) |= (1UL << (bit))) +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitToggle(value, bit) ((value) ^= (1UL << (bit))) +#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit)) + +// avr-libc defines _NOP() since 1.6.2 +#ifndef _NOP +#define _NOP() do { __asm__ volatile ("nop"); } while (0) +#endif + +typedef unsigned int word; + +#define bit(b) (1UL << (b)) + +typedef bool boolean; +typedef uint8_t byte; + +void init(void); +void initVariant(void); + +int atexit(void (*func)()) __attribute__((weak)); + +void pinMode(uint8_t pin, uint8_t mode); +void digitalWrite(uint8_t pin, uint8_t val); +int digitalRead(uint8_t pin); +int analogRead(uint8_t pin); +void analogReference(uint8_t mode); +void analogWrite(uint8_t pin, int val); + +unsigned long millis(void); +unsigned long micros(void); +void delay(unsigned long ms); +void delayMicroseconds(unsigned int us); +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); +unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); +uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); + +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode); +void detachInterrupt(uint8_t interruptNum); + +void setup(void); +void loop(void); + +// Get the bit location within the hardware port of the given virtual pin. +// This comes from the pins_*.c file for the active board configuration. + +#define analogInPinToBit(P) (P) + +// On the ATmega1280, the addresses of some of the port registers are +// greater than 255, so we can't store them in uint8_t's. +extern const uint16_t PROGMEM port_to_mode_PGM[]; +extern const uint16_t PROGMEM port_to_input_PGM[]; +extern const uint16_t PROGMEM port_to_output_PGM[]; + +extern const uint8_t PROGMEM digital_pin_to_port_PGM[]; +// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[]; +extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[]; +extern const uint8_t PROGMEM digital_pin_to_timer_PGM[]; + +// Get the bit location within the hardware port of the given virtual pin. +// This comes from the pins_*.c file for the active board configuration. +// +// These perform slightly better as macros compared to inline functions +// +#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) ) +#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) ) +#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) ) +#define analogInPinToBit(P) (P) +#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) ) +#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) ) +#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) ) + +#define NOT_A_PIN 0 +#define NOT_A_PORT 0 + +#define NOT_AN_INTERRUPT -1 + +#ifdef ARDUINO_MAIN +#define PA 1 +#define PB 2 +#define PC 3 +#define PD 4 +#define PE 5 +#define PF 6 +#define PG 7 +#define PH 8 +#define PJ 10 +#define PK 11 +#define PL 12 +#endif + +#define NOT_ON_TIMER 0 +#define TIMER0A 1 +#define TIMER0B 2 +#define TIMER1A 3 +#define TIMER1B 4 +#define TIMER1C 5 +#define TIMER2 6 +#define TIMER2A 7 +#define TIMER2B 8 + +#define TIMER3A 9 +#define TIMER3B 10 +#define TIMER3C 11 +#define TIMER4A 12 +#define TIMER4B 13 +#define TIMER4C 14 +#define TIMER4D 15 +#define TIMER5A 16 +#define TIMER5B 17 +#define TIMER5C 18 + +#ifdef __cplusplus +} // extern "C" +#endif + +#ifdef __cplusplus +#include "WCharacter.h" +#include "WString.h" +#include "HardwareSerial.h" +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 + +#include "USBAPI.h" +#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL) +#error "Targets with both UART0 and CDC serial not supported" +#endif + +END OF HIDDEN SECTION +#endif + +uint16_t makeWord(uint16_t w); +uint16_t makeWord(byte h, byte l); + +#define word(...) makeWord(__VA_ARGS__) + +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); +unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); + +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); +void noTone(uint8_t _pin); + +// WMath prototypes +long random(long); +long random(long, long); +void randomSeed(unsigned long); +long map(long, long, long, long, long); + +#endif + +#include "pins_arduino.h" + +#endif diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp new file mode 100644 index 0000000..ca0c10f --- /dev/null +++ b/cores/arduino/CDC.cpp @@ -0,0 +1,310 @@ + +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 + + +/* Copyright (c) 2011, Peter Barrett +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#include "USBAPI.h" +#include +#include + +#if defined(USBCON) + +typedef struct +{ + u32 dwDTERate; + u8 bCharFormat; + u8 bParityType; + u8 bDataBits; + u8 lineState; +} LineInfo; + +static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; +static volatile int32_t breakValue = -1; + +static u8 wdtcsr_save; + +#define WEAK __attribute__ ((weak)) + +extern const CDCDescriptor _cdcInterface PROGMEM; +const CDCDescriptor _cdcInterface = +{ + D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0), + + // CDC communication interface + D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0), + D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd) + D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not) + D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported + D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0 + D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40), + + // CDC data interface + D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0), + D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0), + D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0) +}; + +bool isLUFAbootloader() +{ + return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE; +} + +int CDC_GetInterface(u8* interfaceNum) +{ + interfaceNum[0] += 2; // uses 2 + return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface)); +} + +bool CDC_Setup(USBSetup& setup) +{ + u8 r = setup.bRequest; + u8 requestType = setup.bmRequestType; + + if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType) + { + if (CDC_GET_LINE_CODING == r) + { + USB_SendControl(0,(void*)&_usbLineInfo,7); + return true; + } + } + + if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType) + { + if (CDC_SEND_BREAK == r) + { + breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL; + } + + if (CDC_SET_LINE_CODING == r) + { + USB_RecvControl((void*)&_usbLineInfo,7); + } + + if (CDC_SET_CONTROL_LINE_STATE == r) + { + _usbLineInfo.lineState = setup.wValueL; + + // auto-reset into the bootloader is triggered when the port, already + // open at 1200 bps, is closed. this is the signal to start the watchdog + // with a relatively long period so it can finish housekeeping tasks + // like servicing endpoints before the sketch ends + + uint16_t magic_key_pos = MAGIC_KEY_POS; + +// If we don't use the new RAMEND directly, check manually if we have a newer bootloader. +// This is used to keep compatible with the old leonardo bootloaders. +// You are still able to set the magic key position manually to RAMEND-1 to save a few bytes for this check. +#if MAGIC_KEY_POS != (RAMEND-1) + // For future boards save the key in the inproblematic RAMEND + // Which is reserved for the main() return value (which will never return) + if (isLUFAbootloader()) { + // horray, we got a new bootloader! + magic_key_pos = (RAMEND-1); + } +#endif + + // We check DTR state to determine if host port is open (bit 0 of lineState). + if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) + { +#if MAGIC_KEY_POS != (RAMEND-1) + // Backup ram value if its not a newer bootloader and it hasn't already been saved. + // This should avoid memory corruption at least a bit, not fully + if (magic_key_pos != (RAMEND-1) && *(uint16_t *)magic_key_pos != MAGIC_KEY) { + *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos; + } +#endif + // Store boot key + *(uint16_t *)magic_key_pos = MAGIC_KEY; + // Save the watchdog state in case the reset is aborted. + wdtcsr_save = WDTCSR; + wdt_enable(WDTO_120MS); + } + else if (*(uint16_t *)magic_key_pos == MAGIC_KEY) + { + // Most OSs do some intermediate steps when configuring ports and DTR can + // twiggle more than once before stabilizing. + // To avoid spurious resets we set the watchdog to 120ms and eventually + // cancel if DTR goes back high. + // Cancellation is only done if an auto-reset was started, which is + // indicated by the magic key having been set. + + wdt_reset(); + // Restore the watchdog state in case the sketch was using it. + WDTCSR |= (1<= 0) { + return 1 + USB_Available(CDC_RX); + } + return USB_Available(CDC_RX); +} + +int Serial_::peek(void) +{ + if (peek_buffer < 0) + peek_buffer = USB_Recv(CDC_RX); + return peek_buffer; +} + +int Serial_::read(void) +{ + if (peek_buffer >= 0) { + int c = peek_buffer; + peek_buffer = -1; + return c; + } + return USB_Recv(CDC_RX); +} + +int Serial_::availableForWrite(void) +{ + return USB_SendSpace(CDC_TX); +} + +void Serial_::flush(void) +{ + USB_Flush(CDC_TX); +} + +size_t Serial_::write(uint8_t c) +{ + return write(&c, 1); +} + +size_t Serial_::write(const uint8_t *buffer, size_t size) +{ + /* only try to send bytes if the high-level CDC connection itself + is open (not just the pipe) - the OS should set lineState when the port + is opened and clear lineState when the port is closed. + bytes sent before the user opens the connection or after + the connection is closed are lost - just like with a UART. */ + + // TODO - ZE - check behavior on different OSes and test what happens if an + // open connection isn't broken cleanly (cable is yanked out, host dies + // or locks up, or host virtual serial port hangs) + if (_usbLineInfo.lineState > 0) { + int r = USB_Send(CDC_TX,buffer,size); + if (r > 0) { + return r; + } else { + setWriteError(); + return 0; + } + } + setWriteError(); + return 0; +} + +// This operator is a convenient way for a sketch to check whether the +// port has actually been configured and opened by the host (as opposed +// to just being connected to the host). It can be used, for example, in +// setup() before printing to ensure that an application on the host is +// actually ready to receive and display the data. +// We add a short delay before returning to fix a bug observed by Federico +// where the port is configured (lineState != 0) but not quite opened. +Serial_::operator bool() { + bool result = false; + if (_usbLineInfo.lineState > 0) + result = true; + delay(10); + return result; +} + +unsigned long Serial_::baud() { + // Disable interrupts while reading a multi-byte value + uint32_t baudrate; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + baudrate = _usbLineInfo.dwDTERate; + } + return baudrate; +} + +uint8_t Serial_::stopbits() { + return _usbLineInfo.bCharFormat; +} + +uint8_t Serial_::paritytype() { + return _usbLineInfo.bParityType; +} + +uint8_t Serial_::numbits() { + return _usbLineInfo.bDataBits; +} + +bool Serial_::dtr() { + return _usbLineInfo.lineState & 0x1; +} + +bool Serial_::rts() { + return _usbLineInfo.lineState & 0x2; +} + +int32_t Serial_::readBreak() { + int32_t ret; + // Disable IRQs while reading and clearing breakValue to make + // sure we don't overwrite a value just set by the ISR. + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + ret = breakValue; + breakValue = -1; + } + return ret; +} + +Serial_ Serial; + +#endif /* if defined(USBCON) */ + +END OF HIDDEN SECTION +#endif diff --git a/cores/arduino/Client.h b/cores/arduino/Client.h new file mode 100644 index 0000000..b8e5d93 --- /dev/null +++ b/cores/arduino/Client.h @@ -0,0 +1,45 @@ +/* + Client.h - Base class that provides Client + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef client_h +#define client_h +#include "Print.h" +#include "Stream.h" +#include "IPAddress.h" + +class Client : public Stream { + +public: + virtual int connect(IPAddress ip, uint16_t port) =0; + virtual int connect(const char *host, uint16_t port) =0; + virtual size_t write(uint8_t) =0; + virtual size_t write(const uint8_t *buf, size_t size) =0; + virtual int available() = 0; + virtual int read() = 0; + virtual int read(uint8_t *buf, size_t size) = 0; + virtual int peek() = 0; + virtual void flush() = 0; + virtual void stop() = 0; + virtual uint8_t connected() = 0; + virtual operator bool() = 0; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp new file mode 100644 index 0000000..e99d503 --- /dev/null +++ b/cores/arduino/HardwareSerial.cpp @@ -0,0 +1,281 @@ +/* + 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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include +#include +#include +#include +#include +#include "Arduino.h" + +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// this next line disables the entire HardwareSerial.cpp, +// this is so I can support Attiny series and any other chip without a uart +#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) + +// SerialEvent functions are weak, so when the user doesn't define them, +// the linker just sets their address to 0 (which is checked below). +// The Serialx_available is just a wrapper around Serialx.available(), +// but we can refer to it weakly so we don't pull in the entire +// HardwareSerial instance if the user doesn't also refer to it. +#if defined(HAVE_HWSERIAL0) + void serialEvent() __attribute__((weak)); + bool Serial0_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL1) + void serialEvent1() __attribute__((weak)); + bool Serial1_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL2) + void serialEvent2() __attribute__((weak)); + bool Serial2_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL3) + void serialEvent3() __attribute__((weak)); + bool Serial3_available() __attribute__((weak)); +#endif + +void serialEventRun(void) +{ +#if defined(HAVE_HWSERIAL0) + if (Serial0_available && serialEvent && Serial0_available()) serialEvent(); +#endif +#if defined(HAVE_HWSERIAL1) + if (Serial1_available && serialEvent1 && Serial1_available()) serialEvent1(); +#endif +#if defined(HAVE_HWSERIAL2) + if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2(); +#endif +#if defined(HAVE_HWSERIAL3) + if (Serial3_available && serialEvent3 && Serial3_available()) serialEvent3(); +#endif +} + +// macro to guard critical sections when needed for large TX buffer sizes +#if (SERIAL_TX_BUFFER_SIZE>256) +#define TX_BUFFER_ATOMIC ATOMIC_BLOCK(ATOMIC_RESTORESTATE) +#else +#define TX_BUFFER_ATOMIC +#endif + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +void HardwareSerial::_tx_udr_empty_irq(void) +{ + // If interrupts are enabled, there must be more data in the output + // buffer. Send the next byte + unsigned char c = _tx_buffer[_tx_buffer_tail]; + _tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE; + + *_udr = c; + + // clear the TXC bit -- "can be cleared by writing a one to its bit + // location". This makes sure flush() won't return until the bytes + // actually got written. Other r/w bits are preserved, and zeroes + // written to the rest. + +#ifdef MPCM0 + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0); +#else + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << TXC0))); +#endif + + if (_tx_buffer_head == _tx_buffer_tail) { + // Buffer empty, so disable interrupts + cbi(*_ucsrb, UDRIE0); + } +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void HardwareSerial::begin(unsigned long baud, byte config) +{ + // Try u2x mode first + uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2; + *_ucsra = 1 << U2X0; + + // hardcoded exception for 57600 for compatibility with the bootloader + // shipped with the Duemilanove and previous boards and the firmware + // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot + // be > 4095, so switch back to non-u2x mode if the baud rate is too + // low. + if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095)) + { + *_ucsra = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } + + // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register) + *_ubrrh = baud_setting >> 8; + *_ubrrl = baud_setting; + + _written = false; + + //set the data bits, parity, and stop bits +#if defined(__AVR_ATmega8__) + config |= 0x80; // select UCSRC register (shared with UBRRH) +#endif + *_ucsrc = config; + + sbi(*_ucsrb, RXEN0); + sbi(*_ucsrb, TXEN0); + sbi(*_ucsrb, RXCIE0); + cbi(*_ucsrb, UDRIE0); +} + +void HardwareSerial::end() +{ + // wait for transmission of outgoing data + flush(); + + cbi(*_ucsrb, RXEN0); + cbi(*_ucsrb, TXEN0); + cbi(*_ucsrb, RXCIE0); + cbi(*_ucsrb, UDRIE0); + + // clear any received data + _rx_buffer_head = _rx_buffer_tail; +} + +int HardwareSerial::available(void) +{ + return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE; +} + +int HardwareSerial::peek(void) +{ + if (_rx_buffer_head == _rx_buffer_tail) { + return -1; + } else { + return _rx_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[_rx_buffer_tail]; + _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; + return c; + } +} + +int HardwareSerial::availableForWrite(void) +{ + tx_buffer_index_t head; + tx_buffer_index_t tail; + + TX_BUFFER_ATOMIC { + head = _tx_buffer_head; + tail = _tx_buffer_tail; + } + if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail; + return tail - head - 1; +} + +void HardwareSerial::flush() +{ + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) + return; + + while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) { + if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0)) + // Interrupts are globally disabled, but the DR empty + // interrupt should be enabled, so poll the DR empty flag to + // prevent deadlock + if (bit_is_set(*_ucsra, UDRE0)) + _tx_udr_empty_irq(); + } + // If we get here, nothing is queued anymore (DRIE is disabled) and + // the hardware finished tranmission (TXC is set). +} + +size_t HardwareSerial::write(uint8_t c) +{ + _written = true; + // If the buffer and the data register is empty, just write the byte + // to the data register and be done. This shortcut helps + // significantly improve the effective datarate at high (> + // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown. + if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) { + // If TXC is cleared before writing UDR and the previous byte + // completes before writing to UDR, TXC will be set but a byte + // is still being transmitted causing flush() to return too soon. + // So writing UDR must happen first. + // Writing UDR and clearing TC must be done atomically, otherwise + // interrupts might delay the TXC clear so the byte written to UDR + // is transmitted (setting TXC) before clearing TXC. Then TXC will + // be cleared when no bytes are left, causing flush() to hang + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + *_udr = c; +#ifdef MPCM0 + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0); +#else + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << TXC0))); +#endif + } + return 1; + } + tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + while (i == _tx_buffer_tail) { + if (bit_is_clear(SREG, SREG_I)) { + // Interrupts are disabled, so we'll have to poll the data + // register empty flag ourselves. If it is set, pretend an + // interrupt has happened and call the handler to free up + // space for us. + if(bit_is_set(*_ucsra, UDRE0)) + _tx_udr_empty_irq(); + } else { + // nop, the interrupt handler will free up space for us + } + } + + _tx_buffer[_tx_buffer_head] = c; + + // make atomic to prevent execution of ISR between setting the + // head pointer and setting the interrupt flag resulting in buffer + // retransmission + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + _tx_buffer_head = i; + sbi(*_ucsrb, UDRIE0); + } + + return 1; +} + +#endif // whole file diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h new file mode 100644 index 0000000..17000c2 --- /dev/null +++ b/cores/arduino/HardwareSerial.h @@ -0,0 +1,161 @@ +/* + HardwareSerial.h - 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 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#ifndef HardwareSerial_h +#define HardwareSerial_h + +#include + +#include "Stream.h" + +// Define constants and variables for buffering incoming serial data. We're +// using a ring buffer (I think), in which head is the index of the location +// to which to write the next incoming character and tail is the index of the +// location from which to read. +// NOTE: a "power of 2" buffer size is reccomended to dramatically +// optimize all the modulo operations for ring buffers. +// WARNING: When buffer sizes are increased to > 256, the buffer index +// variables are automatically increased in size, but the extra +// atomicity guards needed for that are not implemented. This will +// often work, but occasionally a race condition can occur that makes +// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405 +#if !defined(SERIAL_TX_BUFFER_SIZE) +#if ((RAMEND - RAMSTART) < 1023) +#define SERIAL_TX_BUFFER_SIZE 16 +#else +#define SERIAL_TX_BUFFER_SIZE 64 +#endif +#endif +#if !defined(SERIAL_RX_BUFFER_SIZE) +#if ((RAMEND - RAMSTART) < 1023) +#define SERIAL_RX_BUFFER_SIZE 16 +#else +#define SERIAL_RX_BUFFER_SIZE 64 +#endif +#endif +#if (SERIAL_TX_BUFFER_SIZE>256) +typedef uint16_t tx_buffer_index_t; +#else +typedef uint8_t tx_buffer_index_t; +#endif +#if (SERIAL_RX_BUFFER_SIZE>256) +typedef uint16_t rx_buffer_index_t; +#else +typedef uint8_t rx_buffer_index_t; +#endif + +// Define config for Serial.begin(baud, config); +#define SERIAL_5N1 0x00 +#define SERIAL_6N1 0x02 +#define SERIAL_7N1 0x04 +#define SERIAL_8N1 0x06 +#define SERIAL_5N2 0x08 +#define SERIAL_6N2 0x0A +#define SERIAL_7N2 0x0C +#define SERIAL_8N2 0x0E +#define SERIAL_5E1 0x20 +#define SERIAL_6E1 0x22 +#define SERIAL_7E1 0x24 +#define SERIAL_8E1 0x26 +#define SERIAL_5E2 0x28 +#define SERIAL_6E2 0x2A +#define SERIAL_7E2 0x2C +#define SERIAL_8E2 0x2E +#define SERIAL_5O1 0x30 +#define SERIAL_6O1 0x32 +#define SERIAL_7O1 0x34 +#define SERIAL_8O1 0x36 +#define SERIAL_5O2 0x38 +#define SERIAL_6O2 0x3A +#define SERIAL_7O2 0x3C +#define SERIAL_8O2 0x3E + +class HardwareSerial : public Stream +{ + protected: + volatile uint8_t * const _ubrrh; + volatile uint8_t * const _ubrrl; + volatile uint8_t * const _ucsra; + volatile uint8_t * const _ucsrb; + volatile uint8_t * const _ucsrc; + volatile uint8_t * const _udr; + // Has any byte been written to the UART since begin() + bool _written; + + volatile rx_buffer_index_t _rx_buffer_head; + volatile rx_buffer_index_t _rx_buffer_tail; + volatile tx_buffer_index_t _tx_buffer_head; + volatile tx_buffer_index_t _tx_buffer_tail; + + // Don't put any members after these buffers, since only the first + // 32 bytes of this struct can be accessed quickly using the ldd + // instruction. + unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; + unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; + + public: + inline HardwareSerial( + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr); + void begin(unsigned long baud) { begin(baud, SERIAL_8N1); } + void begin(unsigned long, uint8_t); + void end(); + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool() { return true; } + + // Interrupt handlers - Not intended to be called externally + inline void _rx_complete_irq(void); + void _tx_udr_empty_irq(void); +}; + +#if defined(UBRRH) || defined(UBRR0H) + extern HardwareSerial Serial; + #define HAVE_HWSERIAL0 +#endif +#if defined(UBRR1H) + extern HardwareSerial Serial1; + #define HAVE_HWSERIAL1 +#endif +#if defined(UBRR2H) + extern HardwareSerial Serial2; + #define HAVE_HWSERIAL2 +#endif +#if defined(UBRR3H) + extern HardwareSerial Serial3; + #define HAVE_HWSERIAL3 +#endif + +extern void serialEventRun(void) __attribute__((weak)); + +#endif diff --git a/cores/arduino/HardwareSerial0.cpp b/cores/arduino/HardwareSerial0.cpp new file mode 100644 index 0000000..7d47ed2 --- /dev/null +++ b/cores/arduino/HardwareSerial0.cpp @@ -0,0 +1,79 @@ +/* + HardwareSerial0.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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL0) + +#if defined(USART_RX_vect) + ISR(USART_RX_vect) +#elif defined(USART0_RX_vect) + ISR(USART0_RX_vect) +#elif defined(USART_RXC_vect) + ISR(USART_RXC_vect) // ATmega8 +#else + #error "Don't know what the Data Received vector is called for Serial" +#endif + { + Serial._rx_complete_irq(); + } + +#if defined(UART0_UDRE_vect) +ISR(UART0_UDRE_vect) +#elif defined(UART_UDRE_vect) +ISR(UART_UDRE_vect) +#elif defined(USART0_UDRE_vect) +ISR(USART0_UDRE_vect) +#elif defined(USART_UDRE_vect) +ISR(USART_UDRE_vect) +#else + #error "Don't know what the Data Register Empty vector is called for Serial" +#endif +{ + Serial._tx_udr_empty_irq(); +} + +#if defined(UBRRH) && defined(UBRRL) + HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR); +#else + HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0); +#endif + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial0_available() { + return Serial.available(); +} + +#endif // HAVE_HWSERIAL0 diff --git a/cores/arduino/HardwareSerial1.cpp b/cores/arduino/HardwareSerial1.cpp new file mode 100644 index 0000000..a345cdb --- /dev/null +++ b/cores/arduino/HardwareSerial1.cpp @@ -0,0 +1,69 @@ +/* + HardwareSerial1.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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL1) + +#if defined(UART1_RX_vect) +ISR(UART1_RX_vect) +#elif defined(USART1_RX_vect) +ISR(USART1_RX_vect) +#else +#error "Don't know what the Data Register Empty vector is called for Serial1" +#endif +{ + Serial1._rx_complete_irq(); +} + +#if defined(UART1_UDRE_vect) +ISR(UART1_UDRE_vect) +#elif defined(USART1_UDRE_vect) +ISR(USART1_UDRE_vect) +#else +#error "Don't know what the Data Register Empty vector is called for Serial1" +#endif +{ + Serial1._tx_udr_empty_irq(); +} + +HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial1_available() { + return Serial1.available(); +} + +#endif // HAVE_HWSERIAL1 diff --git a/cores/arduino/HardwareSerial2.cpp b/cores/arduino/HardwareSerial2.cpp new file mode 100644 index 0000000..8e433b6 --- /dev/null +++ b/cores/arduino/HardwareSerial2.cpp @@ -0,0 +1,57 @@ +/* + HardwareSerial2.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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL2) + +ISR(USART2_RX_vect) +{ + Serial2._rx_complete_irq(); +} + +ISR(USART2_UDRE_vect) +{ + Serial2._tx_udr_empty_irq(); +} + +HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial2_available() { + return Serial2.available(); +} + +#endif // HAVE_HWSERIAL2 diff --git a/cores/arduino/HardwareSerial3.cpp b/cores/arduino/HardwareSerial3.cpp new file mode 100644 index 0000000..26aaee8 --- /dev/null +++ b/cores/arduino/HardwareSerial3.cpp @@ -0,0 +1,57 @@ +/* + HardwareSerial3.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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL3) + +ISR(USART3_RX_vect) +{ + Serial3._rx_complete_irq(); +} + +ISR(USART3_UDRE_vect) +{ + Serial3._tx_udr_empty_irq(); +} + +HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial3_available() { + return Serial3.available(); +} + +#endif // HAVE_HWSERIAL3 diff --git a/cores/arduino/HardwareSerial_private.h b/cores/arduino/HardwareSerial_private.h new file mode 100644 index 0000000..761a5e5 --- /dev/null +++ b/cores/arduino/HardwareSerial_private.h @@ -0,0 +1,123 @@ +/* + HardwareSerial_private.h - 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 + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus +*/ + +#include "wiring_private.h" + +// this next line disables the entire HardwareSerial.cpp, +// this is so I can support Attiny series and any other chip without a uart +#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) + +// Ensure that the various bit positions we use are available with a 0 +// postfix, so we can always use the values for UART0 for all UARTs. The +// alternative, passing the various values for each UART to the +// HardwareSerial constructor also works, but makes the code bigger and +// slower. +#if !defined(TXC0) +#if defined(TXC) +// Some chips like ATmega8 don't have UPE, only PE. The other bits are +// named as expected. +#if !defined(UPE) && defined(PE) +#define UPE PE +#endif +// On ATmega8, the uart and its bits are not numbered, so there is no TXC0 etc. +#define TXC0 TXC +#define RXEN0 RXEN +#define TXEN0 TXEN +#define RXCIE0 RXCIE +#define UDRIE0 UDRIE +#define U2X0 U2X +#define UPE0 UPE +#define UDRE0 UDRE +#elif defined(TXC1) +// Some devices have uart1 but no uart0 +#define TXC0 TXC1 +#define RXEN0 RXEN1 +#define TXEN0 TXEN1 +#define RXCIE0 RXCIE1 +#define UDRIE0 UDRIE1 +#define U2X0 U2X1 +#define UPE0 UPE1 +#define UDRE0 UDRE1 +#else +#error No UART found in HardwareSerial.cpp +#endif +#endif // !defined TXC0 + +// Check at compiletime that it is really ok to use the bit positions of +// UART0 for the other UARTs as well, in case these values ever get +// changed for future hardware. +#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \ + UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \ + UDRE1 != UDRE0) +#error "Not all bit positions for UART1 are the same as for UART0" +#endif +#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \ + UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \ + UDRE2 != UDRE0) +#error "Not all bit positions for UART2 are the same as for UART0" +#endif +#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \ + UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \ + UDRE3 != UDRE0) +#error "Not all bit positions for UART3 are the same as for UART0" +#endif + +// Constructors //////////////////////////////////////////////////////////////// + +HardwareSerial::HardwareSerial( + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr) : + _ubrrh(ubrrh), _ubrrl(ubrrl), + _ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc), + _udr(udr), + _rx_buffer_head(0), _rx_buffer_tail(0), + _tx_buffer_head(0), _tx_buffer_tail(0) +{ +} + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +void HardwareSerial::_rx_complete_irq(void) +{ + if (bit_is_clear(*_ucsra, UPE0)) { + // No Parity error, read byte and store it in the buffer if there is + // room + unsigned char c = *_udr; + rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_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[_rx_buffer_head] = c; + _rx_buffer_head = i; + } + } else { + // Parity error, read byte but discard it + *_udr; + }; +} + +#endif // whole file diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp new file mode 100644 index 0000000..d9fe5be --- /dev/null +++ b/cores/arduino/IPAddress.cpp @@ -0,0 +1,114 @@ +/* + IPAddress.cpp - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#include +#include + +IPAddress::IPAddress() +{ + _address.dword = 0; +} + +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) +{ + _address.bytes[0] = first_octet; + _address.bytes[1] = second_octet; + _address.bytes[2] = third_octet; + _address.bytes[3] = fourth_octet; +} + +IPAddress::IPAddress(uint32_t address) +{ + _address.dword = address; +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +bool IPAddress::fromString(const char *address) +{ + uint16_t acc = 0; // Accumulator + uint8_t dots = 0; + + while (*address) + { + char c = *address++; + if (c >= '0' && c <= '9') + { + acc = acc * 10 + (c - '0'); + if (acc > 255) { + // Value out of [0..255] range + return false; + } + } + else if (c == '.') + { + if (dots == 3) { + // Too much dots (there must be 3 dots) + return false; + } + _address.bytes[dots++] = acc; + acc = 0; + } + else + { + // Invalid char + return false; + } + } + + if (dots != 3) { + // Too few dots (there must be 3 dots) + return false; + } + _address.bytes[3] = acc; + return true; +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + _address.dword = address; + return *this; +} + +bool IPAddress::operator==(const uint8_t* addr) const +{ + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; +} + +size_t IPAddress::printTo(Print& p) const +{ + size_t n = 0; + for (int i =0; i < 3; i++) + { + n += p.print(_address.bytes[i], DEC); + n += p.print('.'); + } + n += p.print(_address.bytes[3], DEC); + return n; +} + diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h new file mode 100644 index 0000000..d762f2c --- /dev/null +++ b/cores/arduino/IPAddress.h @@ -0,0 +1,78 @@ +/* + IPAddress.h - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef IPAddress_h +#define IPAddress_h + +#include +#include "Printable.h" +#include "WString.h" + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() { return _address.bytes; }; + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) { return fromString(address.c_str()); } + + // Overloaded cast operator to allow IPAddress objects to be used where a pointer + // to a four-byte uint8_t array is expected + operator uint32_t() const { return _address.dword; }; + bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address.bytes[index]; }; + uint8_t& operator[](int index) { return _address.bytes[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + +#endif diff --git a/cores/arduino/PluggableUSB.cpp b/cores/arduino/PluggableUSB.cpp new file mode 100644 index 0000000..b08463a --- /dev/null +++ b/cores/arduino/PluggableUSB.cpp @@ -0,0 +1,123 @@ + +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 +/* + PluggableUSB.cpp + Copyright (c) 2015 Arduino LLC + + 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 +*/ + +#include "USBAPI.h" +#include "PluggableUSB.h" + +#if defined(USBCON) +#ifdef PLUGGABLE_USB_ENABLED + +extern uint8_t _initEndpoints[]; + +int PluggableUSB_::getInterface(uint8_t* interfaceCount) +{ + int sent = 0; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int res = node->getInterface(interfaceCount); + if (res < 0) + return -1; + sent += res; + } + return sent; +} + +int PluggableUSB_::getDescriptor(USBSetup& setup) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int ret = node->getDescriptor(setup); + // ret!=0 -> request has been processed + if (ret) + return ret; + } + return 0; +} + +void PluggableUSB_::getShortName(char *iSerialNum) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + iSerialNum += node->getShortName(iSerialNum); + } + *iSerialNum = 0; +} + +bool PluggableUSB_::setup(USBSetup& setup) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + if (node->setup(setup)) { + return true; + } + } + return false; +} + +bool PluggableUSB_::plug(PluggableUSBModule *node) +{ + if ((lastEp + node->numEndpoints) > USB_ENDPOINTS) { + return false; + } + + if (!rootNode) { + rootNode = node; + } else { + PluggableUSBModule *current = rootNode; + while (current->next) { + current = current->next; + } + current->next = node; + } + + node->pluggedInterface = lastIf; + node->pluggedEndpoint = lastEp; + lastIf += node->numInterfaces; + for (uint8_t i = 0; i < node->numEndpoints; i++) { + _initEndpoints[lastEp] = node->endpointType[i]; + lastEp++; + } + return true; + // restart USB layer??? +} + +PluggableUSB_& PluggableUSB() +{ + static PluggableUSB_ obj; + return obj; +} + +PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT), + lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT), + rootNode(NULL) +{ + // Empty +} + +#endif + +#endif /* if defined(USBCON) */ + +END OF HIDDEN SECTION +#endif diff --git a/cores/arduino/PluggableUSB.h b/cores/arduino/PluggableUSB.h new file mode 100644 index 0000000..04b5b15 --- /dev/null +++ b/cores/arduino/PluggableUSB.h @@ -0,0 +1,82 @@ + +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 +/* + PluggableUSB.h + Copyright (c) 2015 Arduino LLC + + 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 +*/ + +#ifndef PUSB_h +#define PUSB_h + +#include "USBAPI.h" +#include + +#if defined(USBCON) + +class PluggableUSBModule { +public: + PluggableUSBModule(uint8_t numEps, uint8_t numIfs, uint8_t *epType) : + numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) + { } + +protected: + virtual bool setup(USBSetup& setup) = 0; + virtual int getInterface(uint8_t* interfaceCount) = 0; + virtual int getDescriptor(USBSetup& setup) = 0; + virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; } + + uint8_t pluggedInterface; + uint8_t pluggedEndpoint; + + const uint8_t numEndpoints; + const uint8_t numInterfaces; + const uint8_t *endpointType; + + PluggableUSBModule *next = NULL; + + friend class PluggableUSB_; +}; + +class PluggableUSB_ { +public: + PluggableUSB_(); + bool plug(PluggableUSBModule *node); + int getInterface(uint8_t* interfaceCount); + int getDescriptor(USBSetup& setup); + bool setup(USBSetup& setup); + void getShortName(char *iSerialNum); + +private: + uint8_t lastIf; + uint8_t lastEp; + PluggableUSBModule* rootNode; +}; + +// Replacement for global singleton. +// This function prevents static-initialization-order-fiasco +// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use +PluggableUSB_& PluggableUSB(); + +#endif + +#endif + +END OF HIDDEN SECTION +#endif diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp new file mode 100644 index 0000000..1e4c99a --- /dev/null +++ b/cores/arduino/Print.cpp @@ -0,0 +1,266 @@ +/* + Print.cpp - Base class that provides print() and println() + Copyright (c) 2008 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 + + Modified 23 November 2006 by David A. Mellis + Modified 03 August 2015 by Chuck Todd + */ + +#include +#include +#include +#include +#include "Arduino.h" + +#include "Print.h" + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + while (size--) { + if (write(*buffer++)) n++; + else break; + } + return n; +} + +size_t Print::print(const __FlashStringHelper *ifsh) +{ + PGM_P p = reinterpret_cast(ifsh); + size_t n = 0; + while (1) { + unsigned char c = pgm_read_byte(p++); + if (c == 0) break; + if (write(c)) n++; + else break; + } + return n; +} + +size_t Print::print(const String &s) +{ + return write(s.c_str(), s.length()); +} + +size_t Print::print(const char str[]) +{ + return write(str); +} + +size_t Print::print(char c) +{ + return write(c); +} + +size_t Print::print(unsigned char b, int base) +{ + return print((unsigned long) b, base); +} + +size_t Print::print(int n, int base) +{ + return print((long) n, base); +} + +size_t Print::print(unsigned int n, int base) +{ + return print((unsigned long) n, base); +} + +size_t Print::print(long n, int base) +{ + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); + } +} + +size_t Print::print(unsigned long n, int base) +{ + if (base == 0) return write(n); + else return printNumber(n, base); +} + +size_t Print::print(double n, int digits) +{ + return printFloat(n, digits); +} + +size_t Print::println(const __FlashStringHelper *ifsh) +{ + size_t n = print(ifsh); + n += println(); + return n; +} + +size_t Print::print(const Printable& x) +{ + return x.printTo(*this); +} + +size_t Print::println(void) +{ + return write("\r\n"); +} + +size_t Print::println(const String &s) +{ + size_t n = print(s); + n += println(); + return n; +} + +size_t Print::println(const char c[]) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(char c) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(unsigned char b, int base) +{ + size_t n = print(b, base); + n += println(); + return n; +} + +size_t Print::println(int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(double num, int digits) +{ + size_t n = print(num, digits); + n += println(); + return n; +} + +size_t Print::println(const Printable& x) +{ + size_t n = print(x); + n += println(); + return n; +} + +// Private Methods ///////////////////////////////////////////////////////////// + +size_t Print::printNumber(unsigned long n, uint8_t base) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + char c = n % base; + n /= base; + + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); +} + +size_t Print::printFloat(double number, uint8_t digits) +{ + size_t n = 0; + + if (isnan(number)) return print("nan"); + if (isinf(number)) return print("inf"); + if (number > 4294967040.0) return print ("ovf"); // constant determined empirically + if (number <-4294967040.0) return print ("ovf"); // constant determined empirically + + // Handle negative numbers + if (number < 0.0) + { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + n += print('.'); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + unsigned int toPrint = (unsigned int)(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; +} diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h new file mode 100644 index 0000000..058a2ab --- /dev/null +++ b/cores/arduino/Print.h @@ -0,0 +1,93 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 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 +*/ + +#ifndef Print_h +#define Print_h + +#include +#include // for size_t + +#include "WString.h" +#include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar +#undef BIN +#endif +#define BIN 2 + +class Print +{ + private: + int write_error; + size_t printNumber(unsigned long, uint8_t); + size_t printFloat(double, uint8_t); + protected: + void setWriteError(int err = 1) { write_error = err; } + public: + Print() : write_error(0) {} + + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + + virtual size_t write(uint8_t) = 0; + size_t write(const char *str) { + if (str == NULL) return 0; + return write((const uint8_t *)str, strlen(str)); + } + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + // default to zero, meaning "a single write may block" + // should be overriden by subclasses with buffering + virtual int availableForWrite() { return 0; } + + size_t print(const __FlashStringHelper *); + size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + size_t print(const Printable&); + + size_t println(const __FlashStringHelper *); + size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + size_t println(const Printable&); + size_t println(void); + + virtual void flush() { /* Empty implementation for backward compatibility */ } +}; + +#endif diff --git a/cores/arduino/Printable.h b/cores/arduino/Printable.h new file mode 100644 index 0000000..2a1b2e9 --- /dev/null +++ b/cores/arduino/Printable.h @@ -0,0 +1,40 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef Printable_h +#define Printable_h + +#include + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ + +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/cores/arduino/Server.h b/cores/arduino/Server.h new file mode 100644 index 0000000..69e3e39 --- /dev/null +++ b/cores/arduino/Server.h @@ -0,0 +1,30 @@ +/* + Server.h - Base class that provides Server + Copyright (c) 2011 Adrian McEwen. 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 +*/ + +#ifndef server_h +#define server_h + +#include "Print.h" + +class Server : public Print { +public: + virtual void begin() =0; +}; + +#endif diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp new file mode 100644 index 0000000..9eff663 --- /dev/null +++ b/cores/arduino/Stream.cpp @@ -0,0 +1,318 @@ +/* + Stream.cpp - adds parsing methods to Stream class + Copyright (c) 2008 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 + + Created July 2011 + parsing functions based on TextFinder library by Michael Margolis + + findMulti/findUntil routines written by Jim Leonard/Xuth + */ + +#include "Arduino.h" +#include "Stream.h" + +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait + +// protected method to read stream with timeout +int Stream::timedRead() +{ + int c; + _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// protected method to peek stream with timeout +int Stream::timedPeek() +{ + int c; + _startMillis = millis(); + do { + c = peek(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// returns peek of the next digit in the stream or -1 if timeout +// discards non-numeric characters +int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal) +{ + int c; + while (1) { + c = timedPeek(); + + if( c < 0 || + c == '-' || + (c >= '0' && c <= '9') || + (detectDecimal && c == '.')) return c; + + switch( lookahead ){ + case SKIP_NONE: return -1; // Fail code. + case SKIP_WHITESPACE: + switch( c ){ + case ' ': + case '\t': + case '\r': + case '\n': break; + default: return -1; // Fail code. + } + case SKIP_ALL: + break; + } + read(); // discard non-numeric + } +} + +// Public Methods +////////////////////////////////////////////////////////////// + +void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait +{ + _timeout = timeout; +} + + // find returns true if the target string is found +bool Stream::find(char *target) +{ + return findUntil(target, strlen(target), NULL, 0); +} + +// reads data from the stream until the target string of given length is found +// returns true if target string is found, false if timed out +bool Stream::find(char *target, size_t length) +{ + return findUntil(target, length, NULL, 0); +} + +// as find but search ends if the terminator string is found +bool Stream::findUntil(char *target, char *terminator) +{ + return findUntil(target, strlen(target), terminator, strlen(terminator)); +} + +// reads data from the stream until the target string of the given length is found +// search terminated if the terminator string is found +// returns true if target string is found, false if terminated or timed out +bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) +{ + if (terminator == NULL) { + MultiTarget t[1] = {{target, targetLen, 0}}; + return findMulti(t, 1) == 0 ? true : false; + } else { + MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; + return findMulti(t, 2) == 0 ? true : false; + } +} + +// returns the first valid (long) integer value from the current position. +// lookahead determines how parseInt looks ahead in the stream. +// See LookaheadMode enumeration at the top of the file. +// Lookahead is terminated by the first character that is not a valid part of an integer. +// Once parsing commences, 'ignore' will be skipped in the stream. +long Stream::parseInt(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + long value = 0; + int c; + + c = peekNextDigit(lookahead, false); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore this character + else if(c == '-') + isNegative = true; + else if(c >= '0' && c <= '9') // is c a digit? + value = value * 10 + c - '0'; + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == ignore ); + + if(isNegative) + value = -value; + return value; +} + +// as parseInt but returns a floating point value +float Stream::parseFloat(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + bool isFraction = false; + long value = 0; + int c; + float fraction = 1.0; + + c = peekNextDigit(lookahead, true); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore + else if(c == '-') + isNegative = true; + else if (c == '.') + isFraction = true; + else if(c >= '0' && c <= '9') { // is c a digit? + value = value * 10 + c - '0'; + if(isFraction) + fraction *= 0.1; + } + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore ); + + if(isNegative) + value = -value; + if(isFraction) + return value * fraction; + else + return value; +} + +// read characters from stream into buffer +// terminates if length characters have been read, or timeout (see setTimeout) +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +// +size_t Stream::readBytes(char *buffer, size_t length) +{ + size_t count = 0; + while (count < length) { + int c = timedRead(); + if (c < 0) break; + *buffer++ = (char)c; + count++; + } + return count; +} + + +// as readBytes with terminator character +// terminates if length characters have been read, timeout, or if the terminator character detected +// returns the number of characters placed in the buffer (0 means no valid data found) + +size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) +{ + size_t index = 0; + while (index < length) { + int c = timedRead(); + if (c < 0 || c == terminator) break; + *buffer++ = (char)c; + index++; + } + return index; // return number of characters, not including null terminator +} + +String Stream::readString() +{ + String ret; + int c = timedRead(); + while (c >= 0) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +String Stream::readStringUntil(char terminator) +{ + String ret; + int c = timedRead(); + while (c >= 0 && c != terminator) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { + // any zero length target string automatically matches and would make + // a mess of the rest of the algorithm. + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + if (t->len <= 0) + return t - targets; + } + + while (1) { + int c = timedRead(); + if (c < 0) + return -1; + + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + // the simple case is if we match, deal with that first. + if (c == t->str[t->index]) { + if (++t->index == t->len) + return t - targets; + else + continue; + } + + // if not we need to walk back and see if we could have matched further + // down the stream (ie '1112' doesn't match the first position in '11112' + // but it will match the second position so we can't just reset the current + // index to 0 when we find a mismatch. + if (t->index == 0) + continue; + + int origIndex = t->index; + do { + --t->index; + // first check if current char works against the new current index + if (c != t->str[t->index]) + continue; + + // if it's the only char then we're good, nothing more to check + if (t->index == 0) { + t->index++; + break; + } + + // otherwise we need to check the rest of the found string + int diff = origIndex - t->index; + size_t i; + for (i = 0; i < t->index; ++i) { + if (t->str[i] != t->str[i + diff]) + break; + } + + // if we successfully got through the previous loop then our current + // index is good. + if (i == t->index) { + t->index++; + break; + } + + // otherwise we just try the next index + } while (t->index); + } + } + // unreachable + return -1; +} diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h new file mode 100644 index 0000000..8e950c7 --- /dev/null +++ b/cores/arduino/Stream.h @@ -0,0 +1,129 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 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 + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef Stream_h +#define Stream_h + +#include +#include "Print.h" + +// compatability macros for testing +/* +#define getInt() parseInt() +#define getInt(ignore) parseInt(ignore) +#define getFloat() parseFloat() +#define getFloat(ignore) parseFloat(ignore) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +// This enumeration provides the lookahead options for parseInt(), parseFloat() +// The rules set out here are used until either the first valid character is found +// or a time out occurs due to lack of input. +enum LookaheadMode{ + SKIP_ALL, // All invalid characters are ignored. + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. +}; + +#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field + +class Stream : public Print +{ + protected: + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read + unsigned long _startMillis; // used for timeout measurement + int timedRead(); // read stream with timeout + int timedPeek(); // peek stream with timeout + int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + unsigned long getTimeout(void) { return _timeout; } + + bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } + // returns true if target string is found, false if timed out + + bool find(char target) { return find (&target, 1); } + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } + + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // returns the first valid (long) integer value from the current position. + // lookahead determines how parseInt looks ahead in the stream. + // See LookaheadMode enumeration at the top of the file. + // Lookahead is terminated by the first character that is not a valid part of an integer. + // Once parsing commences, 'ignore' will be skipped in the stream. + + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // float version of parseInt + + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } + // terminates if length characters have been read or timeout (see setTimeout) + // returns the number of characters placed in the buffer (0 means no valid data found) + + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } + // terminates if length characters have been read, timeout, or if the terminator character detected + // returns the number of characters placed in the buffer (0 means no valid data found) + + // Arduino String functions to be added here + String readString(); + String readStringUntil(char terminator); + + protected: + long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } + float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } + // These overload exists for compatibility with any class that has derived + // Stream and used parseFloat/Int with a custom ignore character. To keep + // the public API simple, these overload remains protected. + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); +}; + +#undef NO_IGNORE_CHAR +#endif diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp new file mode 100644 index 0000000..1bfb3e3 --- /dev/null +++ b/cores/arduino/Tone.cpp @@ -0,0 +1,619 @@ +/* Tone.cpp + + A Tone Generator Library + + Written by Brett Hagman + + 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 + +Version Modified By Date Comments +------- ----------- -------- -------- +0001 B Hagman 09/08/02 Initial coding +0002 B Hagman 09/08/18 Multiple pins +0003 B Hagman 09/08/18 Moved initialization from constructor to begin() +0004 B Hagman 09/09/26 Fixed problems with ATmega8 +0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers + 09/11/25 Changed pin toggle method to XOR + 09/11/25 Fixed timer0 from being excluded +0006 D Mellis 09/12/29 Replaced objects with functions +0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register +0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY +0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62) +0010 jipp 15/04/13 added additional define check #2923 +*************************************************/ + +#include +#include +#include "Arduino.h" +#include "pins_arduino.h" + +#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) +#define TCCR2A TCCR2 +#define TCCR2B TCCR2 +#define COM2A1 COM21 +#define COM2A0 COM20 +#define OCR2A OCR2 +#define TIMSK2 TIMSK +#define OCIE2A OCIE2 +#define TIMER2_COMPA_vect TIMER2_COMP_vect +#define TIMSK1 TIMSK +#endif + +// timerx_toggle_count: +// > 0 - duration specified +// = 0 - stopped +// < 0 - infinitely (until stop() method called, or new play() called) + +#if !defined(__AVR_ATmega8__) +volatile long timer0_toggle_count; +volatile uint8_t *timer0_pin_port; +volatile uint8_t timer0_pin_mask; +#endif + +volatile long timer1_toggle_count; +volatile uint8_t *timer1_pin_port; +volatile uint8_t timer1_pin_mask; +volatile long timer2_toggle_count; +volatile uint8_t *timer2_pin_port; +volatile uint8_t timer2_pin_mask; + +#if defined(TIMSK3) +volatile long timer3_toggle_count; +volatile uint8_t *timer3_pin_port; +volatile uint8_t timer3_pin_mask; +#endif + +#if defined(TIMSK4) +volatile long timer4_toggle_count; +volatile uint8_t *timer4_pin_port; +volatile uint8_t timer4_pin_mask; +#endif + +#if defined(TIMSK5) +volatile long timer5_toggle_count; +volatile uint8_t *timer5_pin_port; +volatile uint8_t timer5_pin_mask; +#endif + + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ }; + +#elif defined(__AVR_ATmega8__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ }; + +#elif defined(__AVR_ATmega32U4__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER3 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ }; + +#else + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +// Leave timer 0 to last. +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ }; + +#endif + + + +static int8_t toneBegin(uint8_t _pin) +{ + int8_t _timer = -1; + + // if we're already using the pin, the timer should be configured. + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == _pin) { + return pgm_read_byte(tone_pin_to_timer_PGM + i); + } + } + + // search for an unused timer. + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == 255) { + tone_pins[i] = _pin; + _timer = pgm_read_byte(tone_pin_to_timer_PGM + i); + break; + } + } + + if (_timer != -1) + { + // Set timer specific stuff + // All timers in CTC mode + // 8 bit timers will require changing prescalar values, + // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar + switch (_timer) + { + #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01) + case 0: + // 8 bit timer + TCCR0A = 0; + TCCR0B = 0; + bitWrite(TCCR0A, WGM01, 1); + bitWrite(TCCR0B, CS00, 1); + timer0_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer0_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12) + case 1: + // 16 bit timer + TCCR1A = 0; + TCCR1B = 0; + bitWrite(TCCR1B, WGM12, 1); + bitWrite(TCCR1B, CS10, 1); + timer1_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer1_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR2A) && defined(TCCR2B) + case 2: + // 8 bit timer + TCCR2A = 0; + TCCR2B = 0; + bitWrite(TCCR2A, WGM21, 1); + bitWrite(TCCR2B, CS20, 1); + timer2_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer2_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3) + case 3: + // 16 bit timer + TCCR3A = 0; + TCCR3B = 0; + bitWrite(TCCR3B, WGM32, 1); + bitWrite(TCCR3B, CS30, 1); + timer3_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer3_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4) + case 4: + // 16 bit timer + TCCR4A = 0; + TCCR4B = 0; + #if defined(WGM42) + bitWrite(TCCR4B, WGM42, 1); + #elif defined(CS43) + // TODO this may not be correct + // atmega32u4 + bitWrite(TCCR4B, CS43, 1); + #endif + bitWrite(TCCR4B, CS40, 1); + timer4_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer4_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5) + case 5: + // 16 bit timer + TCCR5A = 0; + TCCR5B = 0; + bitWrite(TCCR5B, WGM52, 1); + bitWrite(TCCR5B, CS50, 1); + timer5_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer5_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + } + } + + return _timer; +} + + + +// frequency (in hertz) and duration (in milliseconds). + +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) +{ + uint8_t prescalarbits = 0b001; + long toggle_count = 0; + uint32_t ocr = 0; + int8_t _timer; + + _timer = toneBegin(_pin); + + if (_timer >= 0) + { + // Set the pinMode as OUTPUT + pinMode(_pin, OUTPUT); + + // if we are using an 8 bit timer, scan through prescalars to find the best fit + if (_timer == 0 || _timer == 2) + { + ocr = F_CPU / frequency / 2 - 1; + prescalarbits = 0b001; // ck/1: same for both timers + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 8 - 1; + prescalarbits = 0b010; // ck/8: same for both timers + + if (_timer == 2 && ocr > 255) + { + ocr = F_CPU / frequency / 2 / 32 - 1; + prescalarbits = 0b011; + } + + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 64 - 1; + prescalarbits = _timer == 0 ? 0b011 : 0b100; + + if (_timer == 2 && ocr > 255) + { + ocr = F_CPU / frequency / 2 / 128 - 1; + prescalarbits = 0b101; + } + + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 256 - 1; + prescalarbits = _timer == 0 ? 0b100 : 0b110; + if (ocr > 255) + { + // can't do any better than /1024 + ocr = F_CPU / frequency / 2 / 1024 - 1; + prescalarbits = _timer == 0 ? 0b101 : 0b111; + } + } + } + } + +#if defined(TCCR0B) + if (_timer == 0) + { + TCCR0B = (TCCR0B & 0b11111000) | prescalarbits; + } + else +#endif +#if defined(TCCR2B) + { + TCCR2B = (TCCR2B & 0b11111000) | prescalarbits; + } +#else + { + // dummy place holder to make the above ifdefs work + } +#endif + } + else + { + // two choices for the 16 bit timers: ck/1 or ck/64 + ocr = F_CPU / frequency / 2 - 1; + + prescalarbits = 0b001; + if (ocr > 0xffff) + { + ocr = F_CPU / frequency / 2 / 64 - 1; + prescalarbits = 0b011; + } + + if (_timer == 1) + { +#if defined(TCCR1B) + TCCR1B = (TCCR1B & 0b11111000) | prescalarbits; +#endif + } +#if defined(TCCR3B) + else if (_timer == 3) + TCCR3B = (TCCR3B & 0b11111000) | prescalarbits; +#endif +#if defined(TCCR4B) + else if (_timer == 4) + TCCR4B = (TCCR4B & 0b11111000) | prescalarbits; +#endif +#if defined(TCCR5B) + else if (_timer == 5) + TCCR5B = (TCCR5B & 0b11111000) | prescalarbits; +#endif + + } + + + // Calculate the toggle count + if (duration > 0) + { + toggle_count = 2 * frequency * duration / 1000; + } + else + { + toggle_count = -1; + } + + // Set the OCR for the given timer, + // set the toggle count, + // then turn on the interrupts + switch (_timer) + { + +#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A) + case 0: + OCR0A = ocr; + timer0_toggle_count = toggle_count; + bitWrite(TIMSK0, OCIE0A, 1); + break; +#endif + + case 1: +#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A) + OCR1A = ocr; + timer1_toggle_count = toggle_count; + bitWrite(TIMSK1, OCIE1A, 1); +#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A) + // this combination is for at least the ATmega32 + OCR1A = ocr; + timer1_toggle_count = toggle_count; + bitWrite(TIMSK, OCIE1A, 1); +#endif + break; + +#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A) + case 2: + OCR2A = ocr; + timer2_toggle_count = toggle_count; + bitWrite(TIMSK2, OCIE2A, 1); + break; +#endif + +#if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A) + case 3: + OCR3A = ocr; + timer3_toggle_count = toggle_count; + bitWrite(TIMSK3, OCIE3A, 1); + break; +#endif + +#if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A) + case 4: + OCR4A = ocr; + timer4_toggle_count = toggle_count; + bitWrite(TIMSK4, OCIE4A, 1); + break; +#endif + +#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A) + case 5: + OCR5A = ocr; + timer5_toggle_count = toggle_count; + bitWrite(TIMSK5, OCIE5A, 1); + break; +#endif + + } + } +} + + +// XXX: this function only works properly for timer 2 (the only one we use +// currently). for the others, it should end the tone, but won't restore +// proper PWM functionality for the timer. +void disableTimer(uint8_t _timer) +{ + switch (_timer) + { + case 0: + #if defined(TIMSK0) + TIMSK0 = 0; + #elif defined(TIMSK) + TIMSK = 0; // atmega32 + #endif + break; + +#if defined(TIMSK1) && defined(OCIE1A) + case 1: + bitWrite(TIMSK1, OCIE1A, 0); + break; +#endif + + case 2: + #if defined(TIMSK2) && defined(OCIE2A) + bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt + #endif + #if defined(TCCR2A) && defined(WGM20) + TCCR2A = (1 << WGM20); + #endif + #if defined(TCCR2B) && defined(CS22) + TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22); + #endif + #if defined(OCR2A) + OCR2A = 0; + #endif + break; + +#if defined(TIMSK3) && defined(OCIE3A) + case 3: + bitWrite(TIMSK3, OCIE3A, 0); + break; +#endif + +#if defined(TIMSK4) && defined(OCIE4A) + case 4: + bitWrite(TIMSK4, OCIE4A, 0); + break; +#endif + +#if defined(TIMSK5) && defined(OCIE5A) + case 5: + bitWrite(TIMSK5, OCIE5A, 0); + break; +#endif + } +} + + +void noTone(uint8_t _pin) +{ + int8_t _timer = -1; + + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == _pin) { + _timer = pgm_read_byte(tone_pin_to_timer_PGM + i); + tone_pins[i] = 255; + break; + } + } + + disableTimer(_timer); + + digitalWrite(_pin, 0); +} + +#ifdef USE_TIMER0 +ISR(TIMER0_COMPA_vect) +{ + if (timer0_toggle_count != 0) + { + // toggle the pin + *timer0_pin_port ^= timer0_pin_mask; + + if (timer0_toggle_count > 0) + timer0_toggle_count--; + } + else + { + disableTimer(0); + *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER1 +ISR(TIMER1_COMPA_vect) +{ + if (timer1_toggle_count != 0) + { + // toggle the pin + *timer1_pin_port ^= timer1_pin_mask; + + if (timer1_toggle_count > 0) + timer1_toggle_count--; + } + else + { + disableTimer(1); + *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER2 +ISR(TIMER2_COMPA_vect) +{ + + if (timer2_toggle_count != 0) + { + // toggle the pin + *timer2_pin_port ^= timer2_pin_mask; + + if (timer2_toggle_count > 0) + timer2_toggle_count--; + } + else + { + // need to call noTone() so that the tone_pins[] entry is reset, so the + // timer gets initialized next time we call tone(). + // XXX: this assumes timer 2 is always the first one used. + noTone(tone_pins[0]); +// disableTimer(2); +// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER3 +ISR(TIMER3_COMPA_vect) +{ + if (timer3_toggle_count != 0) + { + // toggle the pin + *timer3_pin_port ^= timer3_pin_mask; + + if (timer3_toggle_count > 0) + timer3_toggle_count--; + } + else + { + disableTimer(3); + *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER4 +ISR(TIMER4_COMPA_vect) +{ + if (timer4_toggle_count != 0) + { + // toggle the pin + *timer4_pin_port ^= timer4_pin_mask; + + if (timer4_toggle_count > 0) + timer4_toggle_count--; + } + else + { + disableTimer(4); + *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER5 +ISR(TIMER5_COMPA_vect) +{ + if (timer5_toggle_count != 0) + { + // toggle the pin + *timer5_pin_port ^= timer5_pin_mask; + + if (timer5_toggle_count > 0) + timer5_toggle_count--; + } + else + { + disableTimer(5); + *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop + } +} +#endif diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h new file mode 100644 index 0000000..9eaee3a --- /dev/null +++ b/cores/arduino/USBAPI.h @@ -0,0 +1,217 @@ + +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 +/* + USBAPI.h + Copyright (c) 2005-2014 Arduino. 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 +*/ + +#ifndef __USBAPI__ +#define __USBAPI__ + +#include +#include +#include +#include +#include + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; + +#include "Arduino.h" + +// This definitions is usefull if you want to reduce the EP_SIZE to 16 +// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint +#ifndef USB_EP_SIZE +#define USB_EP_SIZE 64 +#endif + +#if defined(USBCON) + +#include "USBDesc.h" +#include "USBCore.h" + +//================================================================================ +//================================================================================ +// USB + +#define EP_TYPE_CONTROL (0x00) +#define EP_TYPE_BULK_IN ((1<256) +#error Please lower the CDC Buffer size +#endif + +class Serial_ : public Stream +{ +private: + int peek_buffer; +public: + Serial_() { peek_buffer = -1; }; + void begin(unsigned long); + void begin(unsigned long, uint8_t); + void end(void); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t*, size_t); + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool(); + + volatile uint8_t _rx_buffer_head; + volatile uint8_t _rx_buffer_tail; + unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; + + // This method allows processing "SEND_BREAK" requests sent by + // the USB host. Those requests indicate that the host wants to + // send a BREAK signal and are accompanied by a single uint16_t + // value, specifying the duration of the break. The value 0 + // means to end any current break, while the value 0xffff means + // to start an indefinite break. + // readBreak() will return the value of the most recent break + // request, but will return it at most once, returning -1 when + // readBreak() is called again (until another break request is + // received, which is again returned once). + // This also mean that if two break requests are received + // without readBreak() being called in between, the value of the + // first request is lost. + // Note that the value returned is a long, so it can return + // 0-0xffff as well as -1. + int32_t readBreak(); + + // These return the settings specified by the USB host for the + // serial port. These aren't really used, but are offered here + // in case a sketch wants to act on these settings. + uint32_t baud(); + uint8_t stopbits(); + uint8_t paritytype(); + uint8_t numbits(); + bool dtr(); + bool rts(); + enum { + ONE_STOP_BIT = 0, + ONE_AND_HALF_STOP_BIT = 1, + TWO_STOP_BITS = 2, + }; + enum { + NO_PARITY = 0, + ODD_PARITY = 1, + EVEN_PARITY = 2, + MARK_PARITY = 3, + SPACE_PARITY = 4, + }; + +}; +extern Serial_ Serial; + +#define HAVE_CDCSERIAL + +//================================================================================ +//================================================================================ +// Low level API + +typedef struct +{ + uint8_t bmRequestType; + uint8_t bRequest; + uint8_t wValueL; + uint8_t wValueH; + uint16_t wIndex; + uint16_t wLength; +} USBSetup; + +//================================================================================ +//================================================================================ +// MSC 'Driver' + +int MSC_GetInterface(uint8_t* interfaceNum); +int MSC_GetDescriptor(int i); +bool MSC_Setup(USBSetup& setup); +bool MSC_Data(uint8_t rx,uint8_t tx); + +//================================================================================ +//================================================================================ +// CSC 'Driver' + +int CDC_GetInterface(uint8_t* interfaceNum); +int CDC_GetDescriptor(int i); +bool CDC_Setup(USBSetup& setup); + +//================================================================================ +//================================================================================ + +#define TRANSFER_PGM 0x80 +#define TRANSFER_RELEASE 0x40 +#define TRANSFER_ZERO 0x20 + +int USB_SendControl(uint8_t flags, const void* d, int len); +int USB_RecvControl(void* d, int len); +int USB_RecvControlLong(void* d, int len); + +uint8_t USB_Available(uint8_t ep); +uint8_t USB_SendSpace(uint8_t ep); +int USB_Send(uint8_t ep, const void* data, int len); // blocking +int USB_Recv(uint8_t ep, void* data, int len); // non-blocking +int USB_Recv(uint8_t ep); // non-blocking +void USB_Flush(uint8_t ep); + +#endif + +#endif /* if defined(USBCON) */ + +END OF HIDDEN SECTION +#endif diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp new file mode 100644 index 0000000..31a7245 --- /dev/null +++ b/cores/arduino/USBCore.cpp @@ -0,0 +1,871 @@ + +//===================================== +//== This section is hidden by LUFA! == +//===================================== +#if 0 + + +/* Copyright (c) 2010, Peter Barrett +** Sleep/Wakeup support added by Michael Dreher +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#include "USBAPI.h" +#include "PluggableUSB.h" +#include + +#if defined(USBCON) + +/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ +#define TX_RX_LED_PULSE_MS 100 +volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ +volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ + +//================================================================== +//================================================================== + +extern const u16 STRING_LANGUAGE[] PROGMEM; +extern const u8 STRING_PRODUCT[] PROGMEM; +extern const u8 STRING_MANUFACTURER[] PROGMEM; +extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM; + +const u16 STRING_LANGUAGE[2] = { + (3<<8) | (2+2), + 0x0409 // English +}; + +#ifndef USB_PRODUCT +// If no product is provided, use USB IO Board +#define USB_PRODUCT "USB IO Board" +#endif + +const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT; + +#if USB_VID == 0x2341 +# if defined(USB_MANUFACTURER) +# undef USB_MANUFACTURER +# endif +# define USB_MANUFACTURER "Arduino LLC" +#elif USB_VID == 0x1b4f +# if defined(USB_MANUFACTURER) +# undef USB_MANUFACTURER +# endif +# define USB_MANUFACTURER "SparkFun" +#elif !defined(USB_MANUFACTURER) +// Fall through to unknown if no manufacturer name was provided in a macro +# define USB_MANUFACTURER "Unknown" +#endif + +const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER; + + +#define DEVICE_CLASS 0x02 + +// DEVICE DESCRIPTOR +const DeviceDescriptor USB_DeviceDescriptorIAD = + D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); + +//================================================================== +//================================================================== + +volatile u8 _usbConfiguration = 0; +volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device +volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits + +static inline void WaitIN(void) +{ + while (!(UEINTX & (1< len) { + n = len; + } + + { + LockEP lock(ep); + // Frame may have been released by the SOF interrupt handler + if (!ReadWriteAllowed()) + continue; + + len -= n; + if (ep & TRANSFER_ZERO) + { + while (n--) + Send8(0); + } + else if (ep & TRANSFER_PGM) + { + while (n--) + Send8(pgm_read_byte(data++)); + } + else + { + while (n--) + Send8(*data++); + } + + if (sendZlp) { + ReleaseTX(); + sendZlp = false; + } else if (!ReadWriteAllowed()) { // ...release if buffer is full... + ReleaseTX(); + if (len == 0) sendZlp = true; + } else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE + // XXX: TRANSFER_RELEASE is never used can be removed? + ReleaseTX(); + } + } + } + TXLED1; // light the TX LED + TxLEDPulse = TX_RX_LED_PULSE_MS; + return r; +} + +u8 _initEndpoints[USB_ENDPOINTS] = +{ + 0, // Control Endpoint + + EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM + EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT + EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN + + // Following endpoints are automatically initialized to 0 +}; + +#define EP_SINGLE_64 0x32 // EP0 +#define EP_DOUBLE_64 0x36 // Other endpoints +#define EP_SINGLE_16 0x12 + +static +void InitEP(u8 index, u8 type, u8 size) +{ + UENUM = index; + UECONX = (1< 64){ + recvLength = 64; + } + + // Write data to fit to the end (not the beginning) of the array + WaitOUT(); + Recv((u8*)d + len - length, recvLength); + ClearOUT(); + length -= recvLength; + } + return len; +} + +static u8 SendInterfaces() +{ + u8 interfaces = 0; + + CDC_GetInterface(&interfaces); + +#ifdef PLUGGABLE_USB_ENABLED + PluggableUSB().getInterface(&interfaces); +#endif + + return interfaces; +} + +// Construct a dynamic configuration descriptor +// This really needs dynamic endpoint allocation etc +// TODO +static +bool SendConfiguration(int maxlen) +{ + // Count and measure interfaces + InitControl(0); + u8 interfaces = SendInterfaces(); + ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces); + + // Now send them + InitControl(maxlen); + USB_SendControl(0,&config,sizeof(ConfigDescriptor)); + SendInterfaces(); + return true; +} + +static +bool SendDescriptor(USBSetup& setup) +{ + u8 t = setup.wValueH; + if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) + return SendConfiguration(setup.wLength); + + InitControl(setup.wLength); +#ifdef PLUGGABLE_USB_ENABLED + int ret = PluggableUSB().getDescriptor(setup); + if (ret != 0) { + return (ret > 0 ? true : false); + } +#endif + + const u8* desc_addr = 0; + if (USB_DEVICE_DESCRIPTOR_TYPE == t) + { + desc_addr = (const u8*)&USB_DeviceDescriptorIAD; + } + else if (USB_STRING_DESCRIPTOR_TYPE == t) + { + if (setup.wValueL == 0) { + desc_addr = (const u8*)&STRING_LANGUAGE; + } + else if (setup.wValueL == IPRODUCT) { + return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM); + } + else if (setup.wValueL == IMANUFACTURER) { + return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM); + } + else if (setup.wValueL == ISERIAL) { +#ifdef PLUGGABLE_USB_ENABLED + char name[ISERIAL_MAX_LEN]; + PluggableUSB().getShortName(name); + return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0); +#endif + } + else + return false; + } + + if (desc_addr == 0) + return false; + u8 desc_length = pgm_read_byte(desc_addr); + + USB_SendControl(TRANSFER_PGM,desc_addr,desc_length); + return true; +} + +// Endpoint 0 interrupt +ISR(USB_COM_vect) +{ + SetEP(0); + if (!ReceivedSetupInt()) + return; + + USBSetup setup; + Recv((u8*)&setup,8); + ClearSetupInt(); + + u8 requestType = setup.bmRequestType; + if (requestType & REQUEST_DEVICETOHOST) + WaitIN(); + else + ClearIN(); + + bool ok = true; + if (REQUEST_STANDARD == (requestType & REQUEST_TYPE)) + { + // Standard Requests + u8 r = setup.bRequest; + u16 wValue = setup.wValueL | (setup.wValueH << 8); + if (GET_STATUS == r) + { + if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE)) + { + Send8(_usbCurrentStatus); + Send8(0); + } + else + { + // TODO: handle the HALT state of an endpoint here + // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information + Send8(0); + Send8(0); + } + } + else if (CLEAR_FEATURE == r) + { + if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) + && (wValue == DEVICE_REMOTE_WAKEUP)) + { + _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED; + } + } + else if (SET_FEATURE == r) + { + if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) + && (wValue == DEVICE_REMOTE_WAKEUP)) + { + _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED; + } + } + else if (SET_ADDRESS == r) + { + WaitIN(); + UDADDR = setup.wValueL | (1<