├── README.md ├── bootloaders ├── RockSolid128A │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── cc0_atxmega_e5 │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── xmega │ ├── Makefile │ ├── README │ ├── avrdude.conf │ ├── fixhex.c │ ├── flash.xmega.sh │ ├── get_sig.sh │ └── xmegaBOOT.c ├── xmega128a1 │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── xmega128a1u │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── xmega128a4u │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── xmega32e5 │ ├── Makefile │ ├── README │ └── flash.xmega.sh └── xmega64d4 │ ├── Makefile │ ├── README │ └── flash.xmega.sh ├── cores └── xmega │ ├── Arduino.h │ ├── CDC.cpp │ ├── Client.h │ ├── HID.cpp │ ├── HardwareSerial.cpp │ ├── HardwareSerial.h │ ├── IPAddress.cpp │ ├── IPAddress.h │ ├── Platform.h │ ├── Print.cpp │ ├── Print.h │ ├── Printable.h │ ├── Server.h │ ├── Stream.cpp │ ├── Stream.h │ ├── Tone.cpp │ ├── USBAPI.h │ ├── USBCore.cpp │ ├── USBCore.h │ ├── USBDesc.h │ ├── Udp.h │ ├── WCharacter.h │ ├── WInterrupts.c │ ├── WMath.cpp │ ├── WString.cpp │ ├── WString.h │ ├── abi.cpp │ ├── avr-libc │ ├── malloc.c │ ├── realloc.c │ ├── sectionname.h │ └── stdlib_private.h │ ├── binary.h │ ├── hooks.c │ ├── main.cpp │ ├── new.cpp │ ├── new.h │ ├── wiring.c │ ├── wiring_analog.c │ ├── wiring_digital.c │ ├── wiring_private.h │ ├── wiring_pulse.c │ └── wiring_shift.c ├── patch-avrdude.5.11-avrdude.conf ├── patch-avrdude.6.1-avrdude.conf ├── patch.boards.txt └── variants ├── RockSolid128A └── pins_arduino.h ├── cc0_atxmega_e5 └── pins_arduino.h ├── xmega128a1 └── pins_arduino.h ├── xmega128a1u └── pins_arduino.h ├── xmega128a4u └── pins_arduino.h ├── xmega32e5 └── pins_arduino.h └── xmega64d4 └── pins_arduino.h /README.md: -------------------------------------------------------------------------------- 1 | arduino 2 | ======= 3 | 4 | (The XMegaForArduino project would not be possible without the Arduino IDE) 5 | 6 | This repository contains the files and other additions for the 7 | 'hardware/arduino' sub-directory in the arduino development environment 8 | that are necessary for supporting specific ATXmega CPU's. 9 | 10 | This software is provided 'as-is' with no warranty, implied or otherwise 11 | 12 | 13 |

14 | 15 | On versions of the IDE as of 1.6 you should be able to use the downloadable 16 | hardware manager configuration file at the following location: 17 | 18 |      19 | 20 | hardware manager URL for package_XMegaForArduino_index.json 21 | 22 | In some cases, you might want to do this manually, by downloading the latest 23 | 'tarball' of the distribution files, and extracting them directly into the 24 | 25 |     sketchbook/hardware 26 | 27 | directory tree. In some cases, this may be the preferable method. 28 | 29 | The distribution archives are located on the IDE repository, with names similar 30 | to the following: 31 | 32 |     XMegaForArduino-0.9.1.snapshot.txz 33 | 34 | where '0.9.1' would be the release snapshot version. Note that the 'tar.bz2' 35 | files are used by the JSON file, and don't have the correct directory tree. 36 | 37 | 38 | For earlier IDE versions (prior to 1.6), you will need to patch boards.txt 39 | and avrdude.conf manually, and probably patch avr-gcc, avr-binutils, and 40 | avr-libc . The patch files are available in the 'patches' repository as 41 | a part of this project. 42 | 43 | 44 | 45 | NOTE: in some cases there may be USB vendor IDs or product IDs that have 46 | been placed into the code FOR DEMONSTRATION PURPOSES ONLY. Under NO 47 | circumstances are you to ship ANY products with these IDs in place. They 48 | are only assigned for demonstration purposes, primarily so that the code 49 | will compile without errors or user-side edits. 50 | 51 | If your 'FOSS' product needs a vendor ID and product ID, you might try 52 | 53 |    54 | openmoko.org - USB Product IDs
55 |    56 | arduino on github - 'usbdrv' free USB IDs
57 | 58 | 59 |

* DERIVED OPEN SOURCE SOFTWARE *

60 | 61 | This software has been modified from Arduino 1.06 source that is covered by 62 | either [L]GPLv2 or [L]GPLv3. The licensing details are generally contained 63 | within the files themselves. However, if there are no licensing 64 | specifications, it should be treated as being the same as for the Arduino 1.06 65 | environment (and later, as appropriate). It is also available free of charge. 66 | 67 | For more information, see http://arduino.cc/ and relevant source files. 68 | 69 | 70 |

* ADDITIONAL LICENSING FOR XMEGA-SPECIFIC CODE - MIT-like license *

71 | 72 | Portions of this code were written SPECIFICALLY for the XMEGA series processor 73 | and are not actually 'derived code', but are included in the source as part of 74 | a 'derived work'. As such, within the context of the Arduino build 75 | environment, and with respect to the implementation of the supporting 76 | functions, the entire work should be considered a 'derived work'. 77 | 78 | HOWEVER, if you were to make copies of ONLY the XMEGA-specific code for your 79 | own purpose, you may license use of ONLY the XMEGA-specific code, which does 80 | not appear in the original Arduino files from which this work was derived, to 81 | use in your own work, provided that you include a copyright statement similar 82 | to the following: 83 | 84 | 85 |   Portions of this work have been used by permission from the XMegaForArduino
86 |   project and are Copyright (c) 2015 by S.F.T. Inc. - all rights reserved 87 | 88 | 89 | So if you simply included portions of the 'clock setup' code for the XMEGA 90 | processor within your own work, you could license that under an MIT-like 91 | as stated above, in lieu of an [L]GPLv2 or [L]GPLv3 license (as appropriate). 92 | Keep in mind that inclusion of actual GPL-derived code would not be allowed, 93 | so at the most you could copy the XMEGA-specific code, but not the entire 94 | function, nor implement something that could be considered 'derived from' the 95 | GPL version of the code. This distinction has been provided specifically so 96 | that you can use this project as 'sample code' for your own work, for doing 97 | some of the more difficult tasks (like setting up the clock), without being 98 | locked into using GPL for your own code afterwards. 99 | 100 | If you believe that your resulting work MIGHT STILL be a derived work, you 101 | should consider using the appropriate [L]GPL license. 102 | 103 | 104 | -------------------------------------------------------------------------------- /bootloaders/RockSolid128A/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for RockSolid128A - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | USE_TARGET=RockSolid128A 6 | 7 | 8 | xmega: 9 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 10 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 11 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 12 | 13 | flashboot: 14 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 15 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p at$(USE_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 16 | 17 | 18 | 19 | clean: 20 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 21 | 22 | 23 | -------------------------------------------------------------------------------- /bootloaders/RockSolid128A/README: -------------------------------------------------------------------------------- 1 | Rock Solid XMega 128A bootloader 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | make xmega; make flashboot 8 | 9 | 10 | original source: 11 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 12 | http://ladyada.net/library/arduino/Adaboot328.zip 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bootloaders/RockSolid128A/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C../xmega/avrdude.conf -v -v -v -v -v \ 9 | -patxmega128a1u -cwiring -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/cc0_atxmega_e5/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega32e5 - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | USE_TARGET=cc0_atxmega_e5 6 | CPU_TARGET=atxmega32e5 7 | 8 | # this board re-maps the serial port pins 9 | CFLAGS += -DSERIAL_PORT_REMAP 10 | 11 | 12 | xmega: 13 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 14 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 15 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 16 | 17 | flashboot: 18 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 19 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 20 | 21 | 22 | 23 | clean: 24 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 25 | 26 | 27 | -------------------------------------------------------------------------------- /bootloaders/cc0_atxmega_e5/README: -------------------------------------------------------------------------------- 1 | This is a modification of the Arduino bootloader, which is derived from the 2 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 3 | and uses an AVRISPmkII to program the target. 4 | 5 | make xmega; make flashboot 6 | 7 | 8 | original source: 9 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 10 | http://ladyada.net/library/arduino/Adaboot328.zip 11 | 12 | 13 | ABOUT THE HARDWARE 14 | ------------------ 15 | 16 | The cc0 xmega32e5 breakout board used to be available from 17 | 18 | https://www.tindie.com/products/andete/atxmega32e5-breakout-board/ 19 | 20 | However, it appears that the board is no longer being supported. 21 | 22 | Since I happen to have access to one of these (which was part of an initial 23 | evaluation of xmega processors for a customer project), I have included a 24 | variant for the board layout. 25 | 26 | This particular board requires 'shifting' the serial pins, which can be on 27 | port C pins 2 and 3 (normal settings), or port C pins 6 and 7 (the 'shifted' 28 | settings). The xmega processor allows you to do this particular kind of 29 | re-configuration, and this is what the wiring of the 'cc0' board needs to 30 | happen. 31 | 32 | 33 | In many ways it is a good example of a hardware variant implementation. 34 | 35 | A few items worth noting: 36 | 37 | 1. default serial on PORTC pins 6, 7 (as mentioned) 38 | 2. default LED on Port R, pin 1 39 | 3. bootloader uses 'arduino' protocol 40 | 4. serial pinout works with Adafruit 3.3v USB serial adaptor or the 41 | 'FTDI friend' as long as you don't wire up the 5V pin on the connector. 42 | 5. tested most recently with 'Blink' script using newest bootloader and 43 | 'work in progress' IDE (1/30/2017). 44 | 45 | 46 | -------------------------------------------------------------------------------- /bootloaders/cc0_atxmega_e5/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C./avrdude.conf -v -v -v -v -v \ 9 | -patxmega64d4 -carduino -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for xmega bootloader for ATxmega64d4 processor 2 | # 3 | # (some comments from original retained for reference) 4 | # 5 | # Makefile for ATmegaBOOT 6 | # E.Lins, 18.7.2005 7 | # $Id: Makefile,v 1.4 2008/10/05 17:41:43 cvs Exp $ 8 | # 9 | 10 | # program name should not be changed... 11 | PROGRAM = xmegaBOOT 12 | 13 | # enter the parameters for the avrdude isp tool 14 | ISPTOOL ?= avrispmkii 15 | ISPPORT ?= usb 16 | ISPSPEED ?= 17 | 18 | # the conf file is 'special' for the xmega and has more definitions in it 19 | ISPCONF ?= ./avrdude.conf 20 | 21 | 22 | # TARGET definition - sets up a bunch of stuff 23 | 24 | TARGET ?= 25 | BOOTLOADER_SUFFIX ?= 26 | USE_STK500V2 ?= 27 | USE_CATERINA ?= 28 | GREP_LINE ?= 29 | 30 | # some defaults 31 | # AVR frequency - normally 32Mhz 32 | AVR_FREQ ?= 32000000L 33 | 34 | # max time count - how long to wait after blinking - for 'arduino' protocol 35 | # the 'wiring' protocol will use a different default timeout 36 | MAX_TIME_COUNT ?= F_CPU>>3 37 | 38 | # LED flashes - default is 3 39 | NUM_LED_FLASHES ?= 3 40 | 41 | 42 | # known targets 43 | # standard targets: 44 | # xmega16d4 xmega32d4 xmega64d4 xmega128d4 45 | # xmega16a1 xmega16a1u xmega32a1 xmega32a1u 46 | # xmega64a1 xmega64a1u xmega128a1 xmega128a1u 47 | # xmega64a4 xmega64a4u xmega128a4 xmega128a4u 48 | # xmega8e5 xmega16e5 xmega32e5 49 | # or one of the following custom targets 50 | # cc0_atxmega_e5 RockSolid128A 51 | 52 | ifneq ($(strip $(TARGET)),) 53 | 54 | # if the target name begins with 'xmega' it's a standard target 55 | 56 | ifeq ($(shell echo $(TARGET) | awk '{ print substr($$0,1,5); }'),xmega) 57 | 58 | TRGSFX:= $(shell echo $(TARGET) | awk '{ print substr($$0,6,99); }') 59 | TRGMEM:= $(shell echo $(TRGSFX) | awk '{ A=substr($$0,length($$0),1); if(A=="u") print substr($$0,1,length($$0)-3); else print substr($$0,1,length($$0)-2);}') 60 | 61 | # use TRGMEM to determine how to set up the start address 62 | ifeq ($(strip $(TRGMEM)),8) 63 | START_ADDR := 0x2000 64 | endif 65 | ifeq ($(strip $(TRGMEM)),16) 66 | START_ADDR := 0x4000 67 | endif 68 | ifeq ($(strip $(TRGMEM)),32) 69 | START_ADDR := 0x8000 70 | endif 71 | ifeq ($(strip $(TRGMEM)),64) 72 | START_ADDR := 0x10000 73 | GREP_LINE := ":020000021000EC" 74 | endif 75 | ifeq ($(strip $(TRGMEM)),128) 76 | START_ADDR := 0x20000 77 | ifeq ($(strip $(USE_CATERINA)),) 78 | USE_STK500V2 := 1 79 | endif 80 | GREP_LINE := ":020000022000DC" 81 | # decrease the maximum timeout 82 | MAX_TIME_COUNT := F_CPU>>4 83 | endif 84 | ifeq ($(strip $(TRGMEM)),192) 85 | START_ADDR := 0x30000 86 | ifeq ($(strip $(USE_CATERINA)),) 87 | USE_STK500V2 := 1 88 | endif 89 | # decrease the maximum timeout 90 | MAX_TIME_COUNT := F_CPU>>4 91 | endif 92 | ifeq ($(strip $(TRGMEM)),256) 93 | START_ADDR := 0x40000 94 | ifeq ($(strip $(USE_CATERINA)),) 95 | USE_STK500V2 := 1 96 | endif 97 | # decrease the maximum timeout 98 | MAX_TIME_COUNT := F_CPU>>4 99 | endif 100 | ifeq ($(strip $(TRGMEM)),384) 101 | START_ADDR := 0x60000 102 | ifeq ($(strip $(USE_CATERINA)),) 103 | USE_STK500V2 := 1 104 | endif 105 | # decrease the maximum timeout 106 | MAX_TIME_COUNT := F_CPU>>4 107 | endif 108 | 109 | # others are pretty much the same for everything 110 | MCU_TARGET := at$(TARGET) 111 | SERIAL_PORT := PORTD 112 | SERIAL_USART := USARTD0 113 | SERIAL_PORT_REMAP := no 114 | LED_BUILTIN_PORT := PORTR 115 | LED_BUILTIN_PIN := 1 116 | 117 | # END OF STANDARD TARGETS 118 | 119 | else 120 | 121 | # CUSTOM TARGETS supported by this project, not starting with 'xmega' 122 | 123 | ifeq ($(TARGET),cc0_atxmega_e5) 124 | 125 | MCU_TARGET := atxmega32e5 126 | START_ADDR := 0x8000 127 | SERIAL_PORT := PORTD 128 | SERIAL_USART := USARTD0 129 | SERIAL_PORT_REMAP := yes 130 | LED_BUILTIN_PORT := PORTR 131 | LED_BUILTIN_PIN := 1 132 | 133 | endif 134 | 135 | ifeq ($(TARGET),RockSolid128A) 136 | 137 | MCU_TARGET := atxmega128a1u 138 | START_ADDR := 0x20000 139 | SERIAL_PORT := PORTC 140 | SERIAL_USART := USARTC0 141 | SERIAL_PORT_REMAP := no 142 | LED_BUILTIN_PORT := PORTQ 143 | LED_BUILTIN_PIN := 3 144 | # this one has more than 64k, so it needs STK500V2 145 | USE_STK500V2 := 1 146 | GREP_LINE := ":020000022000DC" 147 | # increase the maximum timeout 148 | MAX_TIME_COUNT := F_CPU>>2 149 | 150 | endif 151 | 152 | ifeq ($(TARGET),Test128) 153 | 154 | MCU_TARGET := atxmega128a1u 155 | START_ADDR := 0x20000 156 | SERIAL_PORT := PORTD 157 | SERIAL_USART := USARTD0 158 | SERIAL_PORT_REMAP := no 159 | LED_BUILTIN_PORT := PORTR 160 | LED_BUILTIN_PIN := 1 161 | # note, NOT using STK500V2 in this one 162 | GREP_LINE := ":020000022000DC" 163 | 164 | endif 165 | 166 | ifeq ($(TARGET),Test64) 167 | 168 | MCU_TARGET := atxmega64d4 169 | START_ADDR := 0x10000 170 | SERIAL_PORT := PORTD 171 | SERIAL_USART := USARTD0 172 | SERIAL_PORT_REMAP := no 173 | LED_BUILTIN_PORT := PORTR 174 | LED_BUILTIN_PIN := 1 175 | # testing STK500V2 with this one 176 | USE_STK500V2 := 1 177 | GREP_LINE := ":020000021000EC" 178 | 179 | # increase the maximum timeout 180 | MAX_TIME_COUNT := F_CPU>>2 181 | 182 | endif 183 | 184 | # anything else besides those 2 targets above will generate an error later 185 | 186 | endif 187 | # END OF BUILTIN STANDARD TARGETS AND CUSTOM TARGETS CHECK 188 | 189 | else 190 | # at this point 'TARGET' is blank, but user can specify other things directly 191 | 192 | # default parameters. specify non-defaults on command line 193 | # certain ones are required. 194 | 195 | MCU_TARGET ?= help 196 | START_ADDR ?= 197 | SERIAL_PORT ?= PORTD 198 | SERIAL_USART ?= 199 | SERIAL_PORT_REMAP ?= no 200 | LED_BUILTIN_PORT ?= PORTR 201 | LED_BUILTIN_PIN ?= 1 202 | 203 | endif 204 | 205 | 206 | ifeq ($(strip $(SERIAL_USART)),) 207 | # default to the 'n' in 'PORTn' as 'USARTn0' 208 | ifneq ($(strip $(SERIAL_PORT)),) 209 | SERIAL_USART := $(shell echo $(SERIAL_PORT) | awk '{ print "USART" substr($$0,5,1) "0"; }') 210 | endif 211 | endif 212 | 213 | # suffix for boot image file name assigned here 214 | 215 | ifeq ($(strip $(BOOTLOADER_SUFFIX)),) 216 | # blank bootloader suffix at this time - create one using build options 217 | ifeq ($(strip $(TARGET)),) 218 | # target is blank 219 | ifneq ($(strip $(USE_CATERINA)),) 220 | # exclude serial port info, add '_U' at the end 221 | BOOTLDR_SUFFIX := $(MCU_TARGET)_$(LED_BUILTIN_PORT)_$(LED_BUILTIN_PIN)_U 222 | else 223 | # serial port info is part of the bootloader suffix 224 | BOOTLDR_SUFFIX := $(MCU_TARGET)_$(SERIAL_PORT)_$(SERIAL_PORT_REMAP)_$(LED_BUILTIN_PORT)_$(LED_BUILTIN_PIN) 225 | endif 226 | else 227 | # target is not blank 228 | ifneq ($(strip $(USE_CATERINA)),) 229 | # add '_U' to TARGET for suffix 230 | BOOTLDR_SUFFIX := $(TARGET)_U 231 | else 232 | # just TARGET for suffix 233 | BOOTLDR_SUFFIX := $(TARGET) 234 | endif 235 | endif 236 | # end of 'bootloader suffix was blank' 237 | else 238 | # bootloader suffix NOT blank 239 | 240 | ifneq ($(strip $(USE_CATERINA)),) 241 | # for USB, add a '_U' suffix 242 | BOOTLDR_SUFFIX := $(BOOTLOADER_SUFFIX)_U 243 | else 244 | BOOTLDR_SUFFIX := $(BOOTLOADER_SUFFIX) 245 | endif 246 | 247 | endif 248 | 249 | 250 | # xmega64 bootloader starts at 10000H. Others depend on CPU 251 | LDSECTION = --section-start=.text=$(START_ADDR) 252 | 253 | 254 | ####################### 255 | # FUSES FUSES FUSES 256 | ####################### 257 | 258 | # fuses are a little different for the xmega. these values are derived from 259 | # experimentation. They do NOT lock any of the NVRAM sections after flashing 260 | 261 | # xmega fuse assignments 262 | 263 | # lockbits - must have BOTH bits 0 and 1 SET or you'll have to chip erase to get them back 264 | # bit assignments: 11 = NOLOCK, 10 = WLOCK, 01 = RLOCK, 00 = RWLOCK (typically) 265 | # bit 7:6 bootloader bit 5:4 application bit 3:2 app table bit 1:0 programming interface 266 | # 267 | # to ONLY lock the bootloader, set LOCKBITS to 0xBF (bit 6 clear, all others set) 268 | #LOCKBITS_LOCK=0xFF 269 | LOCKBITS_LOCK=0xBF 270 | LOCKBITS_UNLOCK=0xFF 271 | 272 | # 4.13.1 273 | # FUSEBYTE1 - watchdog timer bits - set to FF for now - see 9.7.1 and 9.7.2 274 | FUSE1=0xFF 275 | 276 | # 4.13.2 277 | # FUSEBYTE2 - bit 6 = STARTUP (0 for bootloader, 1 for app address 0000) 278 | # bit 5 = TOSCSEL (set to 1, always - implies 'shared' when applicable) 279 | # bits 1:0 = sleep BOD mode/disable 11 to disable, 10 or 01 otherwise (see manual) 280 | #11111111 = FF (bit 6 set) 281 | #10111111 = BF (bit 6 clear) 282 | FUSE2=0xBF 283 | 284 | 285 | # 4.13.3 286 | # FUSEBYTE4 bit 4 = reset disable (0 to disable, 1 = normal) 287 | # bits 3:2 = startup time delay (00 = 64, 01 = 4, 11 = 0) 288 | # bit 1 = watchdog timer config lock (0 to lock, 1 = unlocked) 289 | # all other bits should be 1 (note bit 0 may be read as a 0!) 290 | FUSE4=0xFF 291 | 292 | 293 | # 4.13.4 294 | # FUSEBYTE5 bits 5:4 = (running) BOD mode/disable 11 to disable, 10 or 01 otherwise (see manual) 295 | # bit 3 = 'preserve EEPROM during chip erase' - 0 to NOT erase EEPROM, 1 to erase 296 | # bits 2:0 = BOD level - see table 8-2 - 000 = 1.6, 111 = 3.0 at 0.2v increments 297 | #FUSE5=0xF8 298 | #FUSE5=0xF0 299 | FUSE5=0xEA 300 | # EA enables undervolts with mode 10 (continuous) and setting at 2.6v 301 | # TODO: determine if this is the best setting to use 302 | 303 | 304 | #/usr/local/bin/avrdude -C/home/zelgadis/boardformula/avrdude.conf -patxmega64d4 -cavrispmkII -Pusb -e \ 305 | # -Ulockbits:w:${LOCKBITS_UNLOCK}:m -Ufuse1:w:${FUSE1}:m -Ufuse2:w:${FUSE2}:m \ 306 | # -Ufuse4:w:${FUSE4}:m -Ufuse5:w:${FUSE5}:m 307 | 308 | #/usr/local/bin/avrdude -C/home/zelgadis/boardformula/avrdude.conf -patxmega64d4 -cavrispmkII -Pusb \ 309 | # -Uflash:w:${hex_file}:i -Ulockbits:w:${LOCKBITS_LOCK}:m 310 | 311 | 312 | OBJ = $(PROGRAM)_$(BOOTLDR_SUFFIX).o 313 | OPTIMIZE = -O2 314 | 315 | DEFS = 316 | LIBS = 317 | 318 | CC = avr-gcc 319 | 320 | OBJCOPY = avr-objcopy 321 | OBJDUMP = avr-objdump 322 | 323 | CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) -DF_CPU=$(AVR_FREQ) $(DEFS) 324 | LDFLAGS = -Wl,$(LDSECTION) 325 | # Override is only needed by avr-lib build system. 326 | #override LDFLAGS = -Wl,-Map,$(PROGRAM).map,$(LDSECTION) 327 | 328 | 329 | 330 | # this next part should be modified as needed 331 | # I set the max time count to 1/8 the CPU frequency, 3 flashes on the LED, 332 | # and flash baud rate fixed at 115200 (just like the Uno, actually) 333 | 334 | CFLAGS += '-DMAX_TIME_COUNT=$(MAX_TIME_COUNT)' '-DNUM_LED_FLASHES=$(NUM_LED_FLASHES)' \ 335 | '-DWATCHDOG_MODS' '-DDOUBLE_SPEED' 336 | 337 | # stk500v2 protocol 338 | ifneq ($(strip $(USE_CATERINA)),) 339 | CFLAGS += '-DUSE_CATERINA' 340 | else 341 | ifneq ($(strip $(USE_STK500V2)),) 342 | CFLAGS += '-DUSE_STK500V2' 343 | endif 344 | endif 345 | 346 | # change this next part for a different baud rate (but why would you?) 347 | CFLAGS += '-DBAUD_RATE=115200' 348 | 349 | # this next part incorporates the definitions from earlier 350 | 351 | CFLAGS += '-DSERIAL_PORT=$(SERIAL_PORT)' '-DSERIAL_USART=$(SERIAL_USART)' 352 | 353 | # do the 'negative' responses here 354 | ifneq ($(strip $(SERIAL_PORT_REMAP)),) 355 | ifeq ($(strip $(SERIAL_PORT_REMAP)),0) 356 | else 357 | ifeq ($(strip $(SERIAL_PORT_REMAP)),n) 358 | else 359 | ifeq ($(strip $(SERIAL_PORT_REMAP)),no) 360 | else 361 | 362 | ifeq ($(strip $(SERIAL_PORT_REMAP)),yes) 363 | CFLAGS += '-DSERIAL_PORT_REMAP' 364 | else 365 | $(error you must specify SERIAL_PORT_REMAP correctly, was '"' $(SERIAL_PORT_REMAP) '"') 366 | endif 367 | endif 368 | endif 369 | endif 370 | endif 371 | 372 | ifneq ($(strip $(LED_BUILTIN_PORT)),) 373 | CFLAGS += '-DLED_BUILTIN_PORT=$(LED_BUILTIN_PORT)' '-DLED_BUILTIN_PIN=$(LED_BUILTIN_PIN)' 374 | endif 375 | 376 | # this defines SERIAL_PORT SERIAL_PORT_REMAP LED_BUILTIN_PORT LED_BUILTIN_PIN 377 | # with 'SERIAL_PORT_REMAP' being optional - test with #ifdef in code 378 | 379 | # now do the sig bytes - that's the last of it 380 | 381 | # OPTIONAL: getting sig bytes from CPU type 382 | 383 | SIG_BYTES := $(shell ./get_sig.sh $(MCU_TARGET)) 384 | 385 | ifneq ($(trim $(SIG_BYTES)),) 386 | 387 | CFLAGS += "-DSIG1=$(shell echo $(SIG_BYTES) | awk '{ print $1; }')" 388 | CFLAGS += "-DSIG2=$(shell echo $(SIG_BYTES) | awk '{ print $2; }')" 389 | CFLAGS += "-DSIG3=$(shell echo $(SIG_BYTES) | awk '{ print $3; }')" 390 | 391 | endif 392 | 393 | 394 | 395 | all: 396 | echo $(CFLAGS) 397 | 398 | xmega: $(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex 399 | @echo building xmega image for $(TARGET) 400 | 401 | 402 | $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex: $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).elf 403 | $(OBJCOPY) -j .text -j .data -O ihex $< $@ 404 | 405 | 406 | $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).elf: $(OBJ) 407 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) 408 | 409 | 410 | $(OBJ): $(PROGRAM).c 411 | $(CC) $(CFLAGS) -c -o $@ $^ 412 | 413 | 414 | clean_$(BOOTLDR_SUFFIX): clean_fixhex 415 | -rm $(PROGRAM)_$(BOOTLDR_SUFFIX).o 416 | -rm $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).elf 417 | -rm $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex 418 | -rm $(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex 419 | 420 | #flashall: $(PROGRAM)_xmega_plus_image.hex fusesxmega 421 | # avrdude -C $(ISPCONF) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \ 422 | # -U flash:w:$(PROGRAM)_xmega_plus_image.hex -U lockbits:w:$(LOCKBITS_LOCK):m 423 | 424 | 425 | ifneq ($(strip $(GREP_LINE)),) 426 | 427 | $(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex: $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex 428 | grep -v $(GREP_LINE) $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex \ 429 | >$(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex 430 | 431 | clean_fixhex: 432 | 433 | 434 | else 435 | 436 | fixhex: fixhex.c 437 | cc -o fixhex fixhex.c 438 | 439 | $(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex: $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex fixhex 440 | ./fixhex $(PROGRAM)_xmega_$(BOOTLDR_SUFFIX).hex $(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex 441 | 442 | clean_fixhex: 443 | -rm fixhex 444 | 445 | endif 446 | 447 | 448 | flashboot: xmega fusesxmega 449 | # in order to flash the boot loader I have to flash it as the "boot" section (no choice) 450 | # since the hex file begins with an extended address record, if I were to simply TRIM THAT OFF 451 | # then the atxmega64d4 will flash the bootloader correctly. 452 | # TODO: write a program to correctly manage this out of a combined file 453 | avrdude -C $(ISPCONF) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \ 454 | -Uboot:w:$(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex:i \ 455 | -Ulock:w:$(LOCKBITS_LOCK):m 456 | 457 | # -Ulockbits:w:$(LOCKBITS_LOCK):m 458 | 459 | flashboot_cmd: 460 | @echo avrdude -C $(ISPCONF) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) \ 461 | -Uboot:w:$(PROGRAM)_xmega0_$(BOOTLDR_SUFFIX).hex:i \ 462 | -Ulock:w:$(LOCKBITS_LOCK):m 463 | 464 | # -Ulockbits:w:$(LOCKBITS_LOCK):m 465 | 466 | 467 | # this next target programs the fuses and leaves lock bits 'unlocked' for flashing 468 | # it also performs a chip erase which normally includes the EEPROM as well 469 | fusesxmega: 470 | while ! avrdude -C $(ISPCONF) -c $(ISPTOOL) -p $(MCU_TARGET) -P $(ISPPORT) $(ISPSPEED) -e \ 471 | -Ulock:w:${LOCKBITS_UNLOCK}:m -Ufuse1:w:${FUSE1}:m -Ufuse2:w:${FUSE2}:m \ 472 | -Ufuse4:w:${FUSE4}:m -Ufuse5:w:${FUSE5}:m ; do sleep 1 ; done 473 | 474 | # -Ulockbits:w:${LOCKBITS_UNLOCK}:m -Ufuse1:w:${FUSE1}:m -Ufuse2:w:${FUSE2}:m \ 475 | 476 | %.elf: $(OBJ) 477 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) 478 | 479 | clean: 480 | -rm -rf *.o *.elf *.lst *.map *.sym *.lss *.eep *.srec *.bin *.hex 481 | -rm -f fixhex 482 | 483 | %.lst: %.elf 484 | $(OBJDUMP) -h -S $< > $@ 485 | 486 | %.hex: %.elf 487 | $(OBJCOPY) -j .text -j .data -O ihex $< $@ 488 | 489 | %.srec: %.elf 490 | $(OBJCOPY) -j .text -j .data -O srec $< $@ 491 | 492 | %.bin: %.elf 493 | $(OBJCOPY) -j .text -j .data -O binary $< $@ 494 | 495 | 496 | -------------------------------------------------------------------------------- /bootloaders/xmega/README: -------------------------------------------------------------------------------- 1 | This is a modification of the Arduino bootloader, which is derived from the 2 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 3 | and uses an AVRISPmkII to program the target. 4 | 5 | 6 | make xmega TARGET=target; make flashboot TARGET=target 7 | 8 | where 9 | 'target' is one of the following standard targets: 10 | xmega16d4 xmega32d4 xmega64d4 xmega128d4 11 | xmega16a1 xmega16a1u xmega32a1 xmega32a1u 12 | xmega64a1 xmega64a1u xmega128a1 xmega128a1u 13 | xmega8e5 xmega16e5 xmega32e5 14 | or one of the following custom targets 15 | cc0_atxmega_e5 MikeXMega 16 | 17 | Alternately you can build and flash with the following variables pre-assigned: 18 | MCU_TARGET=cputype (atxmega64d4, atxmega32e5, etc.) 19 | START_ADDR={bootloader start address} 20 | SERIAL_PORT=portname 21 | SERIAL_USART=usartname 22 | SERIAL_PORT_REMAP=[yes|no|1|0] (default is 'no') 23 | LED_BUILTIN_PORT=portname 24 | LED_BUILTIN_PIN=pin_number 25 | 26 | where 27 | 'portname' is a valid port name (such as 'PORTC') 28 | 'usartname' is an optional USART name (such as 'USARTC0') 29 | 'pin_number' is a valid pin number (such as '2') 30 | 'bootloader start address' is the address of the boot section, in hex 31 | i.e. a value such as '0x10000' 32 | -and- 33 | 'cputype' is a valid cputype recognized by gcc and binutils 34 | 35 | So, to make clean and flash a custom bootloader you could do the following: 36 | make clean xmega flashboot MCU_TARGET=atxmega64a1u START_ADDR=0x10000 SERIAL_PORT=PORTC 37 | 38 | This would build a bootloader for an atxmega64a1u 39 | 40 | 41 | original bootloader source: 42 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 43 | http://ladyada.net/library/arduino/Adaboot328.zip 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /bootloaders/xmega/fixhex.c: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // // 3 | // __ _ _ // 4 | // / _|(_)__ __| |__ ___ __ __ ___ // 5 | // | |_ | |\ \/ /| '_ \ / _ \\ \/ / / __| // 6 | // | _|| | > < | | | || __/ > < _| (__ // 7 | // |_| |_|/_/\_\|_| |_| \___|/_/\_\(_)\___| // 8 | // // 9 | // // 10 | ////////////////////////////////////////////////////////////////////////////// 11 | // // 12 | // Copyright (c) 2014 by S.F.T. Inc. - All rights reserved // 13 | // Use, copying, and distribution of this software are licensed according // 14 | // to the GPLv2, LGPLv2, or BSD license, as appropriate, your choice. // 15 | // NO WARRANTY EITHER EXPLICIT OR IMPLIED. USE AT YOUR OWN RISK // 16 | // // 17 | ////////////////////////////////////////////////////////////////////////////// 18 | 19 | // this program will modify the hex file to change the start address from 8000H to 0000H 20 | // so that I can flash it into the bootloader at offset 0. 21 | 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | 29 | unsigned char hexdigit(const char *p1) 30 | { 31 | if(*p1 >= '0' && *p1 <= '9') 32 | { 33 | return *p1 - '0'; 34 | } 35 | else if(*p1 >= 'a' && *p1 <= 'f') 36 | { 37 | return *p1 - 'a' + 10; 38 | } 39 | else if(*p1 >= 'A' && *p1 <= 'F') 40 | { 41 | return *p1 - 'A' + 10; 42 | } 43 | 44 | return 0; 45 | } 46 | 47 | unsigned short hexval(const char *p1) 48 | { 49 | return hexdigit(p1) * 16 + hexdigit(p1 + 1); 50 | } 51 | 52 | int main(int argc, char *argv[]) 53 | { 54 | FILE *pIn, *pOut; 55 | char tbuf[256]; 56 | int i1, i2, cb1; 57 | char *p1; 58 | static const char szHex[]="0123456789ABCDEF"; 59 | 60 | 61 | if(argc <= 2) 62 | { 63 | printf("usage: fixhex infile outfile\n"); 64 | return 1; 65 | } 66 | 67 | pIn = fopen(argv[1], "r"); 68 | if(!pIn) 69 | { 70 | fprintf(stderr, "unable to read from %s, error %d\n", argv[1], errno); 71 | 72 | return -1; 73 | } 74 | 75 | pOut = fopen(argv[2], "w"); 76 | if(!pOut) 77 | { 78 | fclose(pIn); 79 | 80 | fprintf(stderr, "unable to write to %s, error %d\n", argv[2], errno); 81 | return -2; 82 | } 83 | 84 | while(!feof(pIn)) 85 | { 86 | if(!fgets(tbuf, sizeof(tbuf) - 1, pIn)) 87 | { 88 | break; 89 | } 90 | 91 | // line starts with :00 or :nn for byte count 92 | 93 | if(tbuf[0] != ':' || strlen(tbuf) < 9 || // not a valid intel hex record 94 | (tbuf[1] == '0' && tbuf[2] == '0') || // length is zero 95 | tbuf[7] != '0' || tbuf[8] != '0') // not a 'data' record 96 | { 97 | // printf("here I am: %c %d %c %c %c %c\n", tbuf[0], strlen(tbuf), tbuf[1], tbuf[2], tbuf[7], tbuf[8]); 98 | fputs(tbuf, pOut); 99 | continue; 100 | } 101 | 102 | // re-write the string. 1st 2 chars is byte count 103 | 104 | if(tbuf[3] == '8') 105 | { 106 | tbuf[3] = '0'; 107 | 108 | // re-do checksum since I modified the data 109 | 110 | p1 = &(tbuf[1]); 111 | i2 = hexval(p1); // note this is also the count *and* the new running checksum 112 | p1 += 2; 113 | 114 | // next 3 '2-bytes' are the address + type [00], then 'n' bytes of data, then checksum byte + CRLF 115 | for(i1=0, cb1=i2; i1 < i2 + 3; i1++, p1 += 2) 116 | { 117 | cb1 += hexval(p1); // determine the checksum 118 | } 119 | 120 | // now write the 2-digit hex value into the correct characters pointed by p1 121 | // which is a 2's compliment of the sum of the length, address, type, and data 122 | // see https://en.wikipedia.org/wiki/.hex 123 | 124 | cb1 = -cb1; // 2's compliment 125 | *(p1++) = szHex[(cb1 & 0xf0) >> 4]; // write upper nybble to text data 126 | *p1 = szHex[cb1 & 0xf]; // write lower nybble to text data 127 | 128 | // printf("TEMP: %02x %02x %s\n", (cb1 & 0xff), ((-cb1) & 0xff), p1 - 1); 129 | } 130 | 131 | fputs(tbuf, pOut); 132 | } 133 | 134 | fclose(pIn); 135 | fclose(pOut); 136 | 137 | return 0; 138 | } 139 | 140 | 141 | -------------------------------------------------------------------------------- /bootloaders/xmega/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C./avrdude.conf -v -v -v -v -v \ 9 | -patxmega64d4 -carduino -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega/get_sig.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | ID=`echo $1 | awk '{ if(substr($0,1,7)=="atxmega") print "x" substr($0,8,99); else print $0; }'` 5 | 6 | 7 | cat </tmp/get_sig.txt 8 | BEGIN { 9 | F=0; 10 | } 11 | { 12 | A=\$0; 13 | while(length(A) > 0 && substr(A,1,1)<=" ") 14 | { 15 | A=substr(A,2,length(A)-1); 16 | } 17 | 18 | if(F==0) 19 | { 20 | if(substr(A,1,4)=="part") 21 | { 22 | F=1; 23 | } 24 | } 25 | else if(F==1) 26 | { 27 | if(substr(A,1,2)=="id" && (substr(A,3,1)=="=" || substr(A,3,1)<=" ")) 28 | { 29 | A=substr(A,3,length(A)-2); 30 | 31 | while(substr(A,1,1) <= " " && length(A) > 0) 32 | { 33 | A=substr(A,2,length(A)-1); 34 | } 35 | 36 | if(substr(A,1,1)=="=") 37 | { 38 | A=substr(A,2,length(A)-1); 39 | 40 | while(substr(A,1,1) <= " " && length(A) > 0) 41 | { 42 | A=substr(A,2,length(A)-1); 43 | } 44 | 45 | if(substr(A,length(A),1) == ";") 46 | { 47 | A=substr(A,1,length(A)-1); 48 | while(length(A) > 0 && substr(A,length(A),1) <= " ") 49 | { 50 | A=substr(A,1,length(A)-1); 51 | } 52 | } 53 | 54 | if(substr(A,1,1)=="\"" && substr(A,length(A),1)=="\"") 55 | { 56 | A=substr(A,2,length(A)-2); 57 | } 58 | 59 | if(A=="$ID") 60 | { 61 | F=2; 62 | } 63 | } 64 | } 65 | } 66 | else if(F==2) 67 | { 68 | if(substr(A,1,4)=="part") 69 | { 70 | F=1; 71 | } 72 | else 73 | { 74 | if(substr(A,1,9) == "signature") 75 | { 76 | A = substr(A,10,length(A)-9); 77 | 78 | while(substr(A,1,1) <= " " && length(A) > 0) 79 | { 80 | A=substr(A,2,length(A)-1); 81 | } 82 | 83 | if(substr(A,1,1)=="=") 84 | { 85 | A=substr(A,2,length(A)-1); 86 | 87 | while(substr(A,1,1) <= " " && length(A) > 0) 88 | { 89 | A=substr(A,2,length(A)-1); 90 | } 91 | 92 | if(substr(A,length(A),1) == ";") 93 | { 94 | A=substr(A,1,length(A)-1); 95 | while(length(A) > 0 && substr(A,length(A),1) <= " ") 96 | { 97 | A=substr(A,1,length(A)-1); 98 | } 99 | } 100 | 101 | print A; 102 | } 103 | 104 | F=0; 105 | } 106 | } 107 | } 108 | 109 | } 110 | THING 111 | 112 | # simply output the text 113 | 114 | awk -f /tmp/get_sig.txt < avrdude.conf 115 | 116 | 117 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega128a1 - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | USE_TARGET=xmega128a1 6 | 7 | 8 | xmega: 9 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 10 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 11 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 12 | 13 | flashboot: 14 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 15 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p at$(USE_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 16 | 17 | 18 | 19 | clean: 20 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 21 | 22 | 23 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1/README: -------------------------------------------------------------------------------- 1 | ATXMega128A1 bootloader (tested on ATXMega128A1U, should work ok) 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | make xmega; make flashboot 8 | 9 | 10 | original source: 11 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 12 | http://ladyada.net/library/arduino/Adaboot328.zip 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C../xmega/avrdude.conf -v -v -v -v -v \ 9 | -patxmega128a1 -cwiring -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1u/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega128a1 - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | #USE_TARGET=Test128 6 | #CPU_TARGET=atxmega128a1u 7 | USE_TARGET=xmega128a1u 8 | CPU_TARGET=at$(USE_TARGET) 9 | 10 | xmega: 11 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 12 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 13 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 14 | 15 | flashboot: 16 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 17 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 18 | 19 | 20 | 21 | clean: 22 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 23 | 24 | 25 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1u/README: -------------------------------------------------------------------------------- 1 | ATXMega128A1U bootloader 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | make xmega; make flashboot 8 | 9 | 10 | original source: 11 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 12 | http://ladyada.net/library/arduino/Adaboot328.zip 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bootloaders/xmega128a1u/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C../xmega/avrdude.conf -v -v -v -v -v \ 9 | -patxmega128a1u -cwiring -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega128a4u/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega128a4u - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | USE_TARGET=xmega128a4u 6 | CPU_TARGET=at$(USE_TARGET) 7 | LED_BUILTIN_PORT=PORTC 8 | LED_BUILTIN_PIN=7 9 | 10 | xmega: 11 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) LED_BUILTIN_PORT=$(LED_BUILTIN_PORT) LED_BUILTIN_PIN=$(LED_BUILTIN_PIN) 12 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 13 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 14 | 15 | usb: 16 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) USE_CATERINA=1 LED_BUILTIN_PORT=$(LED_BUILTIN_PORT) LED_BUILTIN_PIN=$(LED_BUILTIN_PIN) 17 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET)_U.hex . 18 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET)_U.hex . 19 | 20 | flashboot: 21 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 22 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulock:w:0xBF:m 23 | 24 | # avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 25 | 26 | flashbootusb: 27 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 28 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET)_U.hex:i -Ulock:w:0xBF:m 29 | 30 | 31 | clean: 32 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 33 | 34 | 35 | -------------------------------------------------------------------------------- /bootloaders/xmega128a4u/README: -------------------------------------------------------------------------------- 1 | ATXMega128A4U bootloader 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | For a serial port bootloader: 8 | 9 | make xmega; make flashboot 10 | 11 | For a USB bootloader: 12 | 13 | make usb; make flashbootusb 14 | 15 | 16 | original source: 17 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 18 | http://ladyada.net/library/arduino/Adaboot328.zip 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /bootloaders/xmega128a4u/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C../xmega/avrdude.conf -v -v -v -v -v \ 9 | -patxmega128a4u -cwiring -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega32e5/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega32e5 - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | USE_TARGET=xmega32e5 6 | CPU_TARGET=at$(USE_TARGET) 7 | 8 | 9 | xmega: 10 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 11 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 12 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 13 | 14 | flashboot: 15 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 16 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 17 | 18 | clean: 19 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 20 | 21 | 22 | -------------------------------------------------------------------------------- /bootloaders/xmega32e5/README: -------------------------------------------------------------------------------- 1 | ATXMega32e5 bootloader 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | make xmega; make flashboot 8 | 9 | 10 | original source: 11 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 12 | http://ladyada.net/library/arduino/Adaboot328.zip 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bootloaders/xmega32e5/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C./avrdude.conf -v -v -v -v -v \ 9 | -patxmega32e5 -carduino -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /bootloaders/xmega64d4/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for atxmega64d4 - invokes 'universal' Makefile in ../xmega 2 | # 3 | # 4 | 5 | #USE_TARGET=Test64 6 | #CPU_TARGET=atxmega64d4 7 | USE_TARGET=xmega64d4 8 | CPU_TARGET=at$(USE_TARGET) 9 | 10 | 11 | xmega: 12 | $(MAKE) -C ../xmega xmega TARGET=$(USE_TARGET) 13 | cp -p ../xmega/xmegaBOOT_xmega_$(USE_TARGET).hex . 14 | cp -p ../xmega/xmegaBOOT_xmega0_$(USE_TARGET).hex . 15 | 16 | flashboot: 17 | $(MAKE) -C ../xmega fusesxmega TARGET=$(USE_TARGET) 18 | avrdude -C ../xmega/avrdude.conf -c avrispmkii -p $(CPU_TARGET) -P usb -Uboot:w:xmegaBOOT_xmega0_$(USE_TARGET).hex:i -Ulockbits:w:0xBF:m 19 | 20 | clean: 21 | $(MAKE) -C ../xmega clean_$(USE_TARGET) TARGET=$(USE_TARGET) 22 | 23 | 24 | -------------------------------------------------------------------------------- /bootloaders/xmega64d4/README: -------------------------------------------------------------------------------- 1 | ATXMega64D4 bootloader 2 | 3 | This is a modification of the Arduino bootloader, which is derived from the 4 | Adafruit bootloader 'as downloaded' that builds on POSIX-compatible systems 5 | and uses an AVRISPmkII to program the target. 6 | 7 | make xmega; make flashboot 8 | 9 | 10 | original source: 11 | http://learn.adafruit.com/arduino-tips-tricks-and-techniques/bootloader 12 | http://ladyada.net/library/arduino/Adaboot328.zip 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /bootloaders/xmega64d4/flash.xmega.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if test -z "$1" ; then 4 | echo usage: flash.xmega.sh [hexfile] 5 | exit 6 | fi 7 | 8 | /usr/local/bin/avrdude -C../xmega/avrdude.conf -v -v -v -v -v \ 9 | -patxmega64d4 -carduino -P/dev/ttyU0 -b115200 -D -Uflash:w:$1:i 10 | 11 | 12 | -------------------------------------------------------------------------------- /cores/xmega/Client.h: -------------------------------------------------------------------------------- 1 | /* 2 | Client.h - Base class that provides Client 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef client_h 21 | #define client_h 22 | #include "Print.h" 23 | #include "Stream.h" 24 | #include "IPAddress.h" 25 | 26 | class Client : public Stream { 27 | 28 | public: 29 | virtual int connect(IPAddress ip, uint16_t port) =0; 30 | virtual int connect(const char *host, uint16_t port) =0; 31 | virtual size_t write(uint8_t) =0; 32 | virtual size_t write(const uint8_t *buf, size_t size) =0; 33 | virtual int available() = 0; 34 | virtual int read() = 0; 35 | virtual int read(uint8_t *buf, size_t size) = 0; 36 | virtual int peek() = 0; 37 | virtual void flush() = 0; 38 | virtual void stop() = 0; 39 | virtual uint8_t connected() = 0; 40 | virtual operator bool() = 0; 41 | protected: 42 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /cores/xmega/HardwareSerial.h: -------------------------------------------------------------------------------- 1 | /* 2 | HardwareSerial.h - Hardware serial library for Wiring 3 | Copyright (c) 2006 Nicholas Zambetti. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | Modified 28 September 2010 by Mark Sproul 20 | Modified 14 August 2012 by Alarus 21 | 22 | Updated for 'xmega' core by bob frazier, S.F.T. Inc. - http://mrp3.com/ 23 | 24 | In some cases, the xmega updates make assumptions about the pin assignments. 25 | See 'pins_arduino.h' for more detail. 26 | 27 | */ 28 | 29 | #ifndef HardwareSerial_h 30 | #define HardwareSerial_h 31 | 32 | #include 33 | 34 | #include "Stream.h" 35 | 36 | struct ring_buffer; 37 | 38 | class HardwareSerial : public Stream 39 | { 40 | protected: // NEVER 'private' 41 | // NOTE: xmega uses a structure pointer rather than individual pointers 42 | // and the bit flags are consistent for all USART devices 43 | ring_buffer *_rx_buffer; 44 | ring_buffer *_tx_buffer; 45 | volatile USART_t *_usart; 46 | bool transmitting; 47 | public: 48 | HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer, uint16_t usart) __attribute__ ((noinline)); 49 | void init(ring_buffer *rx_buffer, ring_buffer *tx_buffer, uint16_t usart) __attribute__ ((noinline)); 50 | void begin(unsigned long); 51 | void begin(unsigned long, uint8_t); 52 | void end(); 53 | virtual int available(void); 54 | virtual int peek(void); 55 | virtual int read(void); 56 | virtual void flush(void); 57 | virtual size_t write(uint8_t); 58 | inline size_t write(unsigned long n) { return write((uint8_t)n); } 59 | inline size_t write(long n) { return write((uint8_t)n); } 60 | inline size_t write(unsigned int n) { return write((uint8_t)n); } 61 | inline size_t write(int n) { return write((uint8_t)n); } 62 | using Print::write; // pull in write(str) and write(buf, size) from Print 63 | operator bool(); 64 | }; 65 | 66 | // Define config for Serial.begin(baud, config); 67 | // PMODE 5:4 00=none 10=even 11=odd 68 | // SBMODE 3 0=1 stop 1=2 stop 69 | // CHSIZE 2:0 000=5 bit 001=6 bit 010=7 bit 011=8 bit 111=9 bit 70 | 71 | #define SERIAL_TWO_STOP _BV(USART_SBMODE_bp) 72 | #define SERIAL_EVEN_PARITY _BV(USART_PMODE1_bp) 73 | #define SERIAL_ODD_PARITY (_BV(USART_PMODE1_bp) | _BV(USART_PMODE0_bp)) 74 | 75 | #define SERIAL_5N1 0x00 76 | #define SERIAL_6N1 0x01 77 | #define SERIAL_7N1 0x02 78 | #define SERIAL_8N1 0x03 79 | #define SERIAL_5N2 (SERIAL_5N1 | SERIAL_TWO_STOP) 80 | #define SERIAL_6N2 (SERIAL_6N1 | SERIAL_TWO_STOP) 81 | #define SERIAL_7N2 (SERIAL_7N1 | SERIAL_TWO_STOP) 82 | #define SERIAL_8N2 (SERIAL_8N1 | SERIAL_TWO_STOP) 83 | #define SERIAL_5E1 (SERIAL_5N1 | SERIAL_EVEN_PARITY) 84 | #define SERIAL_6E1 (SERIAL_6N1 | SERIAL_EVEN_PARITY) 85 | #define SERIAL_7E1 (SERIAL_7N1 | SERIAL_EVEN_PARITY) 86 | #define SERIAL_8E1 (SERIAL_8N1 | SERIAL_EVEN_PARITY) 87 | #define SERIAL_5E2 (SERIAL_5N2 | SERIAL_EVEN_PARITY) 88 | #define SERIAL_6E2 (SERIAL_6N2 | SERIAL_EVEN_PARITY) 89 | #define SERIAL_7E2 (SERIAL_7N2 | SERIAL_EVEN_PARITY) 90 | #define SERIAL_8E2 (SERIAL_8N2 | SERIAL_EVEN_PARITY) 91 | #define SERIAL_5O1 (SERIAL_5N1 | SERIAL_ODD_PARITY) 92 | #define SERIAL_6O1 (SERIAL_6N1 | SERIAL_ODD_PARITY) 93 | #define SERIAL_7O1 (SERIAL_7N1 | SERIAL_ODD_PARITY) 94 | #define SERIAL_8O1 (SERIAL_8N1 | SERIAL_ODD_PARITY) 95 | #define SERIAL_5O2 (SERIAL_5N2 | SERIAL_ODD_PARITY) 96 | #define SERIAL_6O2 (SERIAL_6N2 | SERIAL_ODD_PARITY) 97 | #define SERIAL_7O2 (SERIAL_7N2 | SERIAL_ODD_PARITY) 98 | #define SERIAL_8O2 (SERIAL_8N2 | SERIAL_ODD_PARITY) 99 | 100 | 101 | // this is where I must include 'pins_arduino.h' to get the 'USBCON' definition 102 | #include "pins_arduino.h" 103 | 104 | // DEFAULT SERIAL or 'SERIAL 1' 105 | #if defined(USBCON) 106 | // NOTE: 'Serial1' will be the hardware serial and 'Serial' the USB serial 107 | // whenever 'USBCON' is defined in pins_arduino.h 108 | 109 | #include 110 | 111 | extern HardwareSerial Serial1; 112 | #else // normal hardware serial 113 | extern HardwareSerial Serial; 114 | #define Serial1 Serial /* define as 'Serial' so compatible code won't break */ 115 | #endif 116 | 117 | extern HardwareSerial Serial2; // this is the same regardless of USBCON (there will always be at least 2) 118 | 119 | #ifdef SERIAL_2_PORT_NAME /* note these names are off by 1 with the 'Serial_N_' definitions */ 120 | extern HardwareSerial Serial3; 121 | #endif // SERIAL_2_PORT_NAME 122 | #ifdef SERIAL_3_PORT_NAME 123 | extern HardwareSerial Serial4; 124 | #endif // SERIAL_3_PORT_NAME 125 | #ifdef SERIAL_4_PORT_NAME 126 | extern HardwareSerial Serial5; 127 | #endif // SERIAL_4_PORT_NAME 128 | #ifdef SERIAL_5_PORT_NAME 129 | extern HardwareSerial Serial6; 130 | #endif // SERIAL_5_PORT_NAME 131 | #ifdef SERIAL_6_PORT_NAME 132 | extern HardwareSerial Serial7; 133 | #endif // SERIAL_6_PORT_NAME 134 | #ifdef SERIAL_7_PORT_NAME 135 | extern HardwareSerial Serial8; 136 | #endif // SERIAL_7_PORT_NAME 137 | 138 | // this function calls the serial event handlers. you can override them (hence 'weak') 139 | // the default implementation checks for data available and executes the callback if so 140 | extern void serialEventRun(void) __attribute__((weak)); 141 | 142 | #endif // HardwareSerial_h 143 | 144 | -------------------------------------------------------------------------------- /cores/xmega/IPAddress.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | IPAddress.cpp - Base class that provides IPAddress 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | IPAddress::IPAddress() 24 | { 25 | memset(_address, 0, sizeof(_address)); 26 | } 27 | 28 | IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) 29 | { 30 | _address[0] = first_octet; 31 | _address[1] = second_octet; 32 | _address[2] = third_octet; 33 | _address[3] = fourth_octet; 34 | } 35 | 36 | IPAddress::IPAddress(uint32_t address) 37 | { 38 | memcpy(_address, &address, sizeof(_address)); 39 | } 40 | 41 | IPAddress::IPAddress(const uint8_t *address) 42 | { 43 | memcpy(_address, address, sizeof(_address)); 44 | } 45 | 46 | IPAddress& IPAddress::operator=(const uint8_t *address) 47 | { 48 | memcpy(_address, address, sizeof(_address)); 49 | return *this; 50 | } 51 | 52 | IPAddress& IPAddress::operator=(uint32_t address) 53 | { 54 | memcpy(_address, (const uint8_t *)&address, sizeof(_address)); 55 | return *this; 56 | } 57 | 58 | bool IPAddress::operator==(const uint8_t* addr) 59 | { 60 | return memcmp(addr, _address, sizeof(_address)) == 0; 61 | } 62 | 63 | size_t IPAddress::printTo(Print& p) const 64 | { 65 | size_t n = 0; 66 | for (int i =0; i < 3; i++) 67 | { 68 | n += p.print(_address[i], DEC); 69 | n += p.print('.'); 70 | } 71 | n += p.print(_address[3], DEC); 72 | return n; 73 | } 74 | 75 | -------------------------------------------------------------------------------- /cores/xmega/IPAddress.h: -------------------------------------------------------------------------------- 1 | /* 2 | IPAddress.h - Base class that provides IPAddress 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef IPAddress_h 21 | #define IPAddress_h 22 | 23 | #include 24 | 25 | // A class to make it easier to handle and pass around IP addresses 26 | 27 | class IPAddress : public Printable 28 | { 29 | private: 30 | uint8_t _address[4]; // IPv4 address 31 | // Access the raw byte array containing the address. Because this returns a pointer 32 | // to the internal structure rather than a copy of the address this function should only 33 | // be used when you know that the usage of the returned uint8_t* will be transient and not 34 | // stored. 35 | uint8_t* raw_address() { return _address; }; 36 | 37 | public: 38 | // Constructors 39 | IPAddress(); 40 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); 41 | IPAddress(uint32_t address); 42 | IPAddress(const uint8_t *address); 43 | 44 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer 45 | // to a four-byte uint8_t array is expected 46 | operator uint32_t() 47 | { 48 | register const uint32_t *p1 = (const uint32_t *)(const void *)_address; 49 | return *p1; 50 | }; 51 | bool operator==(const IPAddress& addr) 52 | { 53 | register const uint32_t *p1 = (const uint32_t*)_address; 54 | register const uint32_t *p2 = (const uint32_t*)(addr._address); 55 | 56 | return *p1 == *p2; 57 | }; 58 | 59 | bool operator==(const uint8_t* addr); 60 | 61 | // Overloaded index operator to allow getting and setting individual octets of the address 62 | uint8_t operator[](int index) const { return _address[index]; }; 63 | uint8_t& operator[](int index) { return _address[index]; }; 64 | 65 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types 66 | IPAddress& operator=(const uint8_t *address); 67 | IPAddress& operator=(uint32_t address); 68 | 69 | virtual size_t printTo(Print& p) const; 70 | 71 | friend class EthernetClass; 72 | friend class UDP; 73 | friend class Client; 74 | friend class Server; 75 | friend class DhcpClass; 76 | friend class DNSClient; 77 | }; 78 | 79 | const IPAddress INADDR_NONE(0,0,0,0); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /cores/xmega/Platform.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __PLATFORM_H__ 3 | #define __PLATFORM_H__ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #ifndef _PLATFORM_H_TYPES_DEFINED_ 12 | #define _PLATFORM_H_TYPES_DEFINED_ 13 | // this is a temporary fix for USCore.h data types and header file include order issues 14 | typedef unsigned char u8; 15 | typedef unsigned short u16; 16 | typedef unsigned long u32; 17 | #endif // _PLATFORM_H_TYPES_DEFINED_ 18 | 19 | #include "Arduino.h" 20 | 21 | #if defined(USBCON) 22 | #include "USBDesc.h" 23 | #include "USBCore.h" 24 | #include "USBAPI.h" 25 | #endif /* if defined(USBCON) */ 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /cores/xmega/Print.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Print.cpp - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | Modified 23 November 2006 by David A. Mellis 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "Arduino.h" 27 | 28 | #include "Print.h" 29 | 30 | // Public Methods ////////////////////////////////////////////////////////////// 31 | 32 | /* default implementation: may be overridden */ 33 | size_t Print::write(const uint8_t *buffer, size_t size) 34 | { 35 | size_t n = 0; 36 | while (size--) 37 | { 38 | register size_t cb = write(*buffer++); 39 | 40 | if(!cb) // error return (prevents infinite loops) 41 | { 42 | break; 43 | } 44 | 45 | n += cb; 46 | } 47 | return n; 48 | } 49 | 50 | size_t Print::print(const __FlashStringHelper *ifsh) 51 | { 52 | PGM_P p = reinterpret_cast(ifsh); 53 | size_t n = 0; 54 | char tbuf[32]; // will write 32 chars at a time (helps for USB) 55 | register char *p1; 56 | 57 | 58 | p1 = tbuf; 59 | 60 | while (1) 61 | { 62 | unsigned char c = pgm_read_byte(p++); 63 | 64 | if(c == 0) 65 | { 66 | if(p1 > &(tbuf[0])) 67 | { 68 | n += write(tbuf, p1 - &(tbuf[0])); // write whatever I've got 69 | } 70 | 71 | break; 72 | } 73 | 74 | if(p1 >= &(tbuf[sizeof(tbuf)])) 75 | { 76 | register size_t cb = write(tbuf, p1 - &(tbuf[0])); 77 | 78 | if(!cb) // error (prevents infinite loops) 79 | { 80 | break; 81 | } 82 | 83 | n += cb; 84 | p1 = tbuf; 85 | } 86 | 87 | *(p1++) = c; 88 | } 89 | 90 | return n; 91 | } 92 | 93 | size_t Print::print(const String &s) 94 | { 95 | return write(s.c_str(), s.length()); 96 | } 97 | 98 | size_t Print::print(const char str[]) 99 | { 100 | return write(str); 101 | } 102 | 103 | size_t Print::print(char c) 104 | { 105 | return write(c); 106 | } 107 | 108 | size_t Print::print(unsigned char b, int base) 109 | { 110 | return print((unsigned long) b, base); 111 | } 112 | 113 | size_t Print::print(int n, int base) 114 | { 115 | return print((long) n, base); 116 | } 117 | 118 | size_t Print::print(unsigned int n, int base) 119 | { 120 | return print((unsigned long) n, base); 121 | } 122 | 123 | size_t Print::print(long n, int base) 124 | { 125 | if (base == 0) 126 | { 127 | return write(n); 128 | } 129 | else if (base == 10) 130 | { 131 | if (n < 0) 132 | { 133 | int t = print('-'); 134 | n = -n; 135 | return printNumber(n, 10) + t; 136 | } 137 | return printNumber(n, 10); 138 | } 139 | else 140 | { 141 | return printNumber(n, base); 142 | } 143 | } 144 | 145 | size_t Print::print(unsigned long n, int base) 146 | { 147 | if (base == 0) 148 | return write(n); 149 | else 150 | return printNumber(n, base); 151 | } 152 | 153 | size_t Print::print(double n, int digits) 154 | { 155 | return printFloat(n, digits); 156 | } 157 | 158 | size_t Print::println(const __FlashStringHelper *ifsh) 159 | { 160 | size_t n = print(ifsh); 161 | n += println(); 162 | return n; 163 | } 164 | 165 | size_t Print::print(const Printable& x) 166 | { 167 | return x.printTo(*this); 168 | } 169 | 170 | size_t Print::println(void) 171 | { 172 | char tbuf[2]; 173 | 174 | tbuf[0] = '\r'; // this should be more efficient, and smaller 175 | tbuf[1] = '\n'; 176 | return write(tbuf, 2); 177 | // size_t n = print('\r'); 178 | // n += print('\n'); 179 | // return n; 180 | } 181 | 182 | size_t Print::println(const String &s) 183 | { 184 | size_t n = print(s); 185 | n += println(); 186 | return n; 187 | } 188 | 189 | size_t Print::println(const char c[]) 190 | { 191 | size_t n = print(c); 192 | n += println(); 193 | return n; 194 | } 195 | 196 | size_t Print::println(char c) 197 | { 198 | size_t n = print(c); 199 | n += println(); 200 | return n; 201 | } 202 | 203 | size_t Print::println(unsigned char b, int base) 204 | { 205 | size_t n = print(b, base); 206 | n += println(); 207 | return n; 208 | } 209 | 210 | size_t Print::println(int num, int base) 211 | { 212 | size_t n = print(num, base); 213 | n += println(); 214 | return n; 215 | } 216 | 217 | size_t Print::println(unsigned int num, int base) 218 | { 219 | size_t n = print(num, base); 220 | n += println(); 221 | return n; 222 | } 223 | 224 | size_t Print::println(long num, int base) 225 | { 226 | size_t n = print(num, base); 227 | n += println(); 228 | return n; 229 | } 230 | 231 | size_t Print::println(unsigned long num, int base) 232 | { 233 | size_t n = print(num, base); 234 | n += println(); 235 | return n; 236 | } 237 | 238 | size_t Print::println(double num, int digits) 239 | { 240 | size_t n = print(num, digits); 241 | n += println(); 242 | return n; 243 | } 244 | 245 | size_t Print::println(const Printable& x) 246 | { 247 | size_t n = print(x); 248 | n += println(); 249 | return n; 250 | } 251 | 252 | // Private Methods ///////////////////////////////////////////////////////////// 253 | 254 | size_t Print::printNumber(unsigned long n, uint8_t base) 255 | { 256 | char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. 257 | char *str = &buf[sizeof(buf) - 1]; 258 | 259 | *str = '\0'; 260 | 261 | // prevent crash if called with base == 1 262 | if (base < 2) 263 | base = 10; 264 | 265 | do 266 | { 267 | unsigned long m = n; 268 | n /= base; 269 | char c = m - base * n; 270 | *--str = c < 10 ? c + '0' : c + 'A' - 10; 271 | } while(n); 272 | 273 | return write(str); 274 | } 275 | 276 | size_t Print::printFloat(double number, uint8_t digits) 277 | { 278 | size_t n = 0; 279 | 280 | if (isnan(number)) 281 | return print("nan"); 282 | if (isinf(number)) 283 | return print("inf"); 284 | if (number > 4294967040.0) 285 | return print ("ovf"); // constant determined empirically 286 | if (number <-4294967040.0) 287 | return print ("ovf"); // constant determined empirically 288 | 289 | // Handle negative numbers 290 | if (number < 0.0) 291 | { 292 | n += print('-'); 293 | number = -number; 294 | } 295 | 296 | // Round correctly so that print(1.999, 2) prints as "2.00" 297 | double rounding = 0.5; 298 | for (uint8_t i=0; i 0) 312 | { 313 | n += print("."); 314 | } 315 | 316 | // Extract digits from the remainder one at a time 317 | while (digits-- > 0) 318 | { 319 | remainder *= 10.0; 320 | int toPrint = int(remainder); 321 | n += print(toPrint); 322 | remainder -= toPrint; 323 | } 324 | 325 | return n; 326 | } 327 | 328 | // things that I added 329 | 330 | int Print::printf(const char *pszFormat, ...) /*__attribute__ ((format(printf, 2, 3)))*/ // added API for 'printf' since it has been suggested... 331 | { 332 | va_list va; 333 | int cbOut; 334 | 335 | va_start(va, pszFormat); 336 | 337 | cbOut = vsnprintf(NULL, 0, pszFormat, va); 338 | 339 | if(cbOut > 0) 340 | { 341 | char *p1 = (char *)malloc(cbOut + 2); 342 | 343 | if(p1) 344 | { 345 | cbOut = vsnprintf(p1, cbOut + 1, pszFormat, va); 346 | print(p1); 347 | free(p1); 348 | } 349 | } 350 | 351 | va_end(va); 352 | 353 | return cbOut; 354 | } 355 | 356 | int Print::printf_P(const char *pszFormat, ...) /*__attribute__ ((format(printf_P, 2, 3)))*/ // added API for 'printf' since it has been suggested... 357 | { 358 | va_list va; 359 | int cbOut; 360 | 361 | va_start(va, pszFormat); 362 | 363 | cbOut = vsnprintf_P(NULL, 0, (const char *)pszFormat, va); 364 | 365 | if(cbOut > 0) 366 | { 367 | char *p1 = (char *)malloc(cbOut + 2); 368 | 369 | if(p1) 370 | { 371 | cbOut = vsnprintf_P(p1, cbOut + 1, (const char *)pszFormat, va); 372 | print(p1); 373 | free(p1); 374 | } 375 | } 376 | 377 | va_end(va); 378 | 379 | return cbOut; 380 | } 381 | 382 | -------------------------------------------------------------------------------- /cores/xmega/Print.h: -------------------------------------------------------------------------------- 1 | /* 2 | Print.h - Base class that provides print() and println() 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Print_h 21 | #define Print_h 22 | 23 | #include 24 | #include // for size_t 25 | 26 | #include "WString.h" 27 | #include "Printable.h" 28 | 29 | #define DEC 10 30 | #define HEX 16 31 | #define OCT 8 32 | #define BIN 2 33 | 34 | class Print 35 | { 36 | private: 37 | int write_error; 38 | size_t printNumber(unsigned long, uint8_t); 39 | size_t printFloat(double, uint8_t); 40 | 41 | protected: 42 | void setWriteError(int err = 1) { write_error = err; } 43 | 44 | public: 45 | Print() : write_error(0) {} // default constructor only assigns 0 to 'write_error' 46 | 47 | int getWriteError() { return write_error; } 48 | void clearWriteError() { setWriteError(0); } 49 | 50 | virtual size_t write(uint8_t) = 0; // derived class must implement 51 | size_t write(const char *str) 52 | { 53 | if (str == NULL) return 0; 54 | return write((const uint8_t *)str, strlen(str)); 55 | } 56 | 57 | // write multiple characters from a buffer - default calls 'write(uint8_t)' on each byte 58 | // NOTE: for better efficiency, a derived class SHOULD implement its own 'write' for a buffer 59 | 60 | virtual size_t write(const uint8_t *buffer, size_t size); // overridable 61 | size_t write(const char *buffer, size_t size) 62 | { 63 | return write((const uint8_t *)buffer, size); 64 | } 65 | 66 | // custom mod - implement 'printf' member function 67 | int printf(const char *pszFormat, ...) __attribute__ ((format(printf, 2, 3))); // added API for 'printf' since it has been suggested... 68 | int printf_P(const char *pszFormat, ...) __attribute__ ((format(printf, 2, 3))); // added API for 'printf_P' since it has been suggested... 69 | 70 | size_t print(const __FlashStringHelper *); 71 | size_t print(const String &); 72 | size_t print(const char[]); 73 | size_t print(char); 74 | size_t print(unsigned char, int = DEC); 75 | size_t print(int, int = DEC); 76 | size_t print(unsigned int, int = DEC); 77 | size_t print(long, int = DEC); 78 | size_t print(unsigned long, int = DEC); 79 | size_t print(double, int = 2); 80 | size_t print(const Printable&); 81 | 82 | size_t println(const __FlashStringHelper *); 83 | size_t println(const String &s); 84 | size_t println(const char[]); 85 | size_t println(char); 86 | size_t println(unsigned char, int = DEC); 87 | size_t println(int, int = DEC); 88 | size_t println(unsigned int, int = DEC); 89 | size_t println(long, int = DEC); 90 | size_t println(unsigned long, int = DEC); 91 | size_t println(double, int = 2); 92 | size_t println(const Printable&); 93 | size_t println(void); 94 | }; 95 | 96 | #endif 97 | 98 | -------------------------------------------------------------------------------- /cores/xmega/Printable.h: -------------------------------------------------------------------------------- 1 | /* 2 | Printable.h - Interface class that allows printing of complex types 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Printable_h 21 | #define Printable_h 22 | 23 | #include 24 | 25 | class Print; 26 | 27 | /** The Printable class provides a way for new classes to allow themselves to be printed. 28 | By deriving from Printable and implementing the printTo method, it will then be possible 29 | for users to print out instances of this class by passing them into the usual 30 | Print::print and Print::println methods. 31 | */ 32 | 33 | class Printable 34 | { 35 | public: 36 | virtual size_t printTo(Print& p) const = 0; 37 | }; 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /cores/xmega/Server.h: -------------------------------------------------------------------------------- 1 | /* 2 | Server.h - Base class that provides Server 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef server_h 21 | #define server_h 22 | 23 | class Server : public Print { 24 | public: 25 | virtual void begin() =0; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /cores/xmega/Stream.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Stream.cpp - adds parsing methods to Stream class 3 | Copyright (c) 2008 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | Created July 2011 20 | parsing functions based on TextFinder library by Michael Margolis 21 | */ 22 | 23 | #include "Arduino.h" 24 | #include "Stream.h" 25 | 26 | // if/branch optimization 27 | #define UNLIKELY(x) (__builtin_expect (!!(x), 0)) 28 | #define LIKELY(x) (__builtin_expect (!!(x), 1)) 29 | 30 | 31 | #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait 32 | #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field 33 | 34 | // private method to read stream with timeout 35 | int Stream::timedRead() 36 | { 37 | int c; 38 | 39 | _startMillis = millis(); 40 | 41 | do 42 | { 43 | c = read(); 44 | 45 | if (c >= 0) 46 | return c; 47 | 48 | wait_for_interrupt(); // XMega enhancement 49 | 50 | } while(millis() - _startMillis < _timeout); 51 | 52 | return -1; // -1 indicates timeout 53 | } 54 | 55 | // private method to peek stream with timeout 56 | int Stream::timedPeek() 57 | { 58 | int c; 59 | 60 | _startMillis = millis(); 61 | 62 | do 63 | { 64 | c = peek(); 65 | 66 | if (c >= 0) 67 | return c; 68 | 69 | wait_for_interrupt(); // XMega enhancement 70 | 71 | } while(millis() - _startMillis < _timeout); 72 | 73 | return -1; // -1 indicates timeout 74 | } 75 | 76 | // returns peek of the next digit in the stream or -1 if timeout 77 | // discards non-numeric characters 78 | int Stream::peekNextDigit() 79 | { 80 | int c; 81 | while (1) 82 | { 83 | c = timedPeek(); 84 | 85 | // refactored this for (possibly) better efficiency AND readability - seems to be the same size/footprint 86 | // if (c < 0) 87 | // return c; // timeout 88 | // 89 | // if (c == '-') 90 | // return c; 91 | // 92 | // if (c >= '0' && c <= '9') 93 | // return c; 94 | 95 | if(UNLIKELY(c < 0) || // refactored. < 0 is a timeout 96 | UNLIKELY(c == '-') || // negative (TODO: test for leading char only, or lead on exponent) 97 | LIKELY(c >= '0' && c <= '9')) // numeric digit; TODO test for exponent on float? 98 | { 99 | return c; 100 | } 101 | 102 | read(); // discard non-numeric 103 | } 104 | } 105 | 106 | // Public Methods 107 | ////////////////////////////////////////////////////////////// 108 | 109 | void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait 110 | { 111 | _timeout = timeout; 112 | } 113 | 114 | // find returns true if the target string is found 115 | bool Stream::find(char *target) 116 | { 117 | return findUntil(target, NULL/*""*/); 118 | } 119 | 120 | // reads data from the stream until the target string of given length is found 121 | // returns true if target string is found, false if timed out 122 | bool Stream::find(char *target, size_t length) 123 | { 124 | return findUntil(target, length, NULL, 0); 125 | } 126 | 127 | // as find but search ends if the terminator string is found 128 | bool Stream::findUntil(char *target, char *terminator) 129 | { 130 | return findUntil(target, strlen(target), terminator, strlen(terminator)); 131 | } 132 | 133 | // reads data from the stream until the target string of the given length is found 134 | // search terminated if the terminator string is found 135 | // returns true if target string is found, false if terminated or timed out 136 | bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) 137 | { 138 | size_t index = 0; // maximum target string length is 64k bytes! 139 | size_t termIndex = 0; 140 | int c; 141 | 142 | if(!target || *target == 0) // update, allow NULL pointer 143 | { 144 | return true; // return true if target is a null string 145 | } 146 | 147 | while( (c = timedRead()) > 0) 148 | { 149 | 150 | if(c != target[index]) 151 | { 152 | index = 0; // reset index if any char does not match 153 | } 154 | 155 | if( c == target[index]) 156 | { 157 | //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1); 158 | if(++index >= targetLen) // return true if all chars in the target match 159 | { 160 | return true; 161 | } 162 | } 163 | 164 | // allow for NULL 'terminator' 165 | if(terminator && termLen > 0 && 166 | UNLIKELY(c == terminator[termIndex])) 167 | { 168 | if(++termIndex >= termLen) 169 | { 170 | return false; // return false if terminate string found before target string 171 | } 172 | } 173 | else 174 | { 175 | termIndex = 0; 176 | } 177 | } 178 | 179 | return false; 180 | } 181 | 182 | 183 | // returns the first valid (long) integer value from the current position. 184 | // initial characters that are not digits (or the minus sign) are skipped 185 | // function is terminated by the first character that is not a digit. 186 | long Stream::parseInt() 187 | { 188 | return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout) 189 | } 190 | 191 | // as above but a given skipChar is ignored 192 | // this allows format characters (typically commas) in values to be ignored 193 | long Stream::parseInt(char skipChar) 194 | { 195 | boolean isNegative = false; 196 | long value = 0; 197 | int c; 198 | 199 | c = peekNextDigit(); 200 | // ignore non numeric leading characters 201 | if(c < 0) 202 | return 0; // zero returned if timeout 203 | 204 | do 205 | { 206 | if(c == skipChar) 207 | ; // ignore this charactor 208 | else if(c == '-') 209 | isNegative = true; 210 | else if(c >= '0' && c <= '9') // is c a digit? 211 | value = value * 10 + c - '0'; 212 | read(); // consume the character we got with peek 213 | c = timedPeek(); 214 | } 215 | while( (c >= '0' && c <= '9') || c == skipChar ) 216 | ; 217 | 218 | if(isNegative) 219 | value = -value; 220 | 221 | return value; 222 | } 223 | 224 | 225 | // as parseInt but returns a floating point value 226 | float Stream::parseFloat() 227 | { 228 | return parseFloat(NO_SKIP_CHAR); 229 | } 230 | 231 | // as above but the given skipChar is ignored 232 | // this allows format characters (typically commas) in values to be ignored 233 | float Stream::parseFloat(char skipChar) 234 | { 235 | boolean isNegative = false; 236 | boolean isFraction = false; 237 | long value = 0; 238 | char c; 239 | float fraction = 1.0; 240 | 241 | c = peekNextDigit(); 242 | // ignore non numeric leading characters 243 | if(c < 0) 244 | { 245 | return 0; // zero returned if timeout 246 | } 247 | 248 | do 249 | { 250 | if(c == skipChar) 251 | { 252 | // ignore 253 | } 254 | else if(c == '-') 255 | { 256 | isNegative = true; 257 | } 258 | else if (c == '.') 259 | { 260 | isFraction = true; 261 | } 262 | else if(c >= '0' && c <= '9') // is c a digit? 263 | { 264 | value = value * 10 + c - '0'; 265 | 266 | if(isFraction) 267 | { 268 | fraction *= 0.1; 269 | } 270 | } 271 | 272 | read(); // consume the character we got with peek 273 | c = timedPeek(); 274 | } 275 | 276 | while( (c >= '0' && c <= '9') || c == '.' || c == skipChar ) 277 | ; 278 | 279 | if(isNegative) 280 | value = -value; 281 | if(isFraction) 282 | return value * fraction; 283 | else 284 | return value; 285 | } 286 | 287 | // read characters from stream into buffer 288 | // terminates if length characters have been read, or timeout (see setTimeout) 289 | // returns the number of characters placed in the buffer 290 | // the buffer is NOT null terminated. 291 | // 292 | size_t Stream::readBytes(char *buffer, size_t length) 293 | { 294 | size_t count = 0; 295 | 296 | while (count < length) 297 | { 298 | int c = timedRead(); 299 | 300 | if (c < 0) 301 | break; 302 | 303 | *buffer++ = (char)c; 304 | count++; 305 | } 306 | 307 | return count; 308 | } 309 | 310 | 311 | // as readBytes with terminator character 312 | // terminates if length characters have been read, timeout, or if the terminator character detected 313 | // returns the number of characters placed in the buffer (0 means no valid data found) 314 | 315 | size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) 316 | { 317 | if (length < 1) 318 | return 0; 319 | 320 | size_t index = 0; 321 | 322 | while (index < length) 323 | { 324 | int c = timedRead(); 325 | if (c < 0 || c == terminator) break; 326 | *buffer++ = (char)c; 327 | index++; 328 | } 329 | 330 | return index; // return number of characters, not including null terminator 331 | } 332 | 333 | String Stream::readString() 334 | { 335 | String ret; 336 | 337 | int c = timedRead(); 338 | 339 | while (c >= 0) 340 | { 341 | ret += (char)c; 342 | c = timedRead(); 343 | } 344 | 345 | return ret; 346 | } 347 | 348 | String Stream::readStringUntil(char terminator) 349 | { 350 | String ret; 351 | 352 | int c = timedRead(); 353 | 354 | while (c >= 0 && c != terminator) 355 | { 356 | ret += (char)c; 357 | c = timedRead(); 358 | } 359 | 360 | return ret; 361 | } 362 | 363 | -------------------------------------------------------------------------------- /cores/xmega/Stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | Stream.h - base class for character-based streams. 3 | Copyright (c) 2010 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | 19 | parsing functions based on TextFinder library by Michael Margolis 20 | */ 21 | 22 | #ifndef Stream_h 23 | #define Stream_h 24 | 25 | #include 26 | #include "Print.h" 27 | 28 | // compatability macros for testing 29 | /* 30 | #define getInt() parseInt() 31 | #define getInt(skipChar) parseInt(skipchar) 32 | #define getFloat() parseFloat() 33 | #define getFloat(skipChar) parseFloat(skipChar) 34 | #define getString( pre_string, post_string, buffer, length) 35 | readBytesBetween( pre_string, terminator, buffer, length) 36 | */ 37 | 38 | class Stream : public Print 39 | { 40 | protected: 41 | unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read 42 | unsigned long _startMillis; // used for timeout measurement 43 | int timedRead(); // private method to read stream with timeout 44 | int timedPeek(); // private method to peek stream with timeout 45 | int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout 46 | 47 | public: 48 | virtual int available() = 0; 49 | virtual int read() = 0; 50 | virtual int peek() = 0; 51 | virtual void flush() = 0; 52 | 53 | Stream() {_timeout=1000;} 54 | 55 | // parsing methods 56 | 57 | void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second 58 | 59 | bool find(char *target); // reads data from the stream until the target string is found 60 | // returns true if target string is found, false if timed out (see setTimeout) 61 | 62 | bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found 63 | // returns true if target string is found, false if timed out 64 | 65 | bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found 66 | 67 | bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found 68 | 69 | 70 | long parseInt(); // returns the first valid (long) integer value from the current position. 71 | // initial characters that are not digits (or the minus sign) are skipped 72 | // integer is terminated by the first character that is not a digit. 73 | 74 | float parseFloat(); // float version of parseInt 75 | 76 | size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer 77 | // terminates if length characters have been read or timeout (see setTimeout) 78 | // returns the number of characters placed in the buffer (0 means no valid data found) 79 | 80 | size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character 81 | // terminates if length characters have been read, timeout, or if the terminator character detected 82 | // returns the number of characters placed in the buffer (0 means no valid data found) 83 | 84 | // Arduino String functions to be added here 85 | String readString(); 86 | String readStringUntil(char terminator); 87 | 88 | protected: 89 | long parseInt(char skipChar); // as above but the given skipChar is ignored 90 | // as above but the given skipChar is ignored 91 | // this allows format characters (typically commas) in values to be ignored 92 | 93 | float parseFloat(char skipChar); // as above but the given skipChar is ignored 94 | }; 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /cores/xmega/USBAPI.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // // 3 | // _ _ ____ ____ _ _ _ // 4 | // | | | |/ ___| | __ ) / \ _ __ (_) | |__ // 5 | // | | | |\___ \ | _ \ / _ \ | '_ \ | | | '_ \ // 6 | // | |_| | ___) || |_) |/ ___ \ | |_) || | _ | | | | // 7 | // \___/ |____/ |____//_/ \_\| .__/ |_|(_)|_| |_| // 8 | // |_| // 9 | // // 10 | ////////////////////////////////////////////////////////////////////////////// 11 | 12 | #ifndef __USBAPI__ 13 | #define __USBAPI__ 14 | 15 | #if defined(USBCON) 16 | 17 | #include "USBCore.h" /* make sure since I use its definitions here */ 18 | 19 | #ifdef DEBUG_CODE 20 | #ifdef __cplusplus 21 | extern "C" 22 | { 23 | #endif // __cplusplus 24 | extern void error_print(const char *p1); // TEMPORARY 25 | extern void error_print_(const char *p1); // TEMPORARY 26 | extern void error_printH(unsigned long); // TEMPORARY 27 | extern void error_printH_(unsigned long); // TEMPORARY 28 | extern void error_printL(unsigned long); // TEMPORARY 29 | extern void error_printL_(unsigned long); // TEMPORARY 30 | extern void error_printP(const void * /*PROGMEM*/ p1); // TEMPORARY 31 | extern void error_printP_(const void * /*PROGMEM*/ p1); // TEMPORARY 32 | extern void DumpHex(void *pBuf, uint8_t nBytes); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif // __cplusplus 37 | 38 | #else // DEBUG_CODE 39 | 40 | #define error_print(p1) 41 | #define error_print_(p1) 42 | #define error_printH(X) 43 | #define error_printH_(X) 44 | #define error_printL(X) 45 | #define error_printL_(X) 46 | #define error_printP(p1) 47 | #define error_printP_(p1) 48 | #define DumpHex(X,Y) 49 | 50 | #endif // DEBUG_CODE 51 | 52 | 53 | //================================================================================ 54 | //================================================================================ 55 | // USB 56 | 57 | class USBDevice_ 58 | { 59 | public: 60 | USBDevice_(); 61 | bool configured(); 62 | 63 | void attach(); 64 | void detach(); // Serial port goes down too... 65 | void poll(); 66 | 67 | // assign speed - FAST (12Mbit) or LOW - 'true' for FAST, 'false' for LOW (default) 68 | static void SetSpeed(bool bFast) { bSpeed = bFast; } 69 | static inline bool GetSpeed(void) { return bSpeed; } 70 | 71 | protected: 72 | static XMegaEPDataStruct *GetEPData(); // necessary 73 | 74 | static bool bSpeed; 75 | }; 76 | 77 | extern USBDevice_ USBDevice; 78 | 79 | //================================================================================ 80 | //================================================================================ 81 | // Serial over CDC (Serial1 is the physical port) 82 | 83 | class Serial_ : public Stream 84 | { 85 | protected: 86 | int peek_buffer; 87 | public: 88 | Serial_() { peek_buffer = -1; }; 89 | void begin(unsigned long); 90 | void begin(unsigned long, uint8_t); 91 | void end(void); 92 | 93 | virtual int available(void); 94 | virtual int peek(void); 95 | virtual int read(void); 96 | virtual void flush(void); 97 | virtual size_t write(uint8_t); 98 | virtual size_t write(const uint8_t*, size_t); 99 | 100 | using Print::write; // pull in write(str) etc. from Print 101 | 102 | operator bool(); 103 | }; 104 | extern Serial_ Serial; // NOTE: HardwareSerial.h defines the 1st port as 'Serial1' whenever USBCON defined 105 | 106 | //================================================================================ 107 | //================================================================================ 108 | // Mouse 109 | 110 | #define MOUSE_LEFT 1 111 | #define MOUSE_RIGHT 2 112 | #define MOUSE_MIDDLE 4 113 | #define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) 114 | 115 | class Mouse_ 116 | { 117 | protected: 118 | uint8_t _buttons; 119 | void buttons(uint8_t b); 120 | public: 121 | Mouse_(void); 122 | void begin(void); 123 | void end(void); 124 | void click(uint8_t b = MOUSE_LEFT); 125 | void move(signed char x, signed char y, signed char wheel = 0); 126 | void press(uint8_t b = MOUSE_LEFT); // press LEFT by default 127 | void release(uint8_t b = MOUSE_LEFT); // release LEFT by default 128 | bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default 129 | }; 130 | extern Mouse_ Mouse; 131 | 132 | //================================================================================ 133 | //================================================================================ 134 | // Keyboard 135 | 136 | #define KEY_LEFT_CTRL 0x80 137 | #define KEY_LEFT_SHIFT 0x81 138 | #define KEY_LEFT_ALT 0x82 139 | #define KEY_LEFT_GUI 0x83 140 | #define KEY_RIGHT_CTRL 0x84 141 | #define KEY_RIGHT_SHIFT 0x85 142 | #define KEY_RIGHT_ALT 0x86 143 | #define KEY_RIGHT_GUI 0x87 144 | 145 | #define KEY_UP_ARROW 0xDA 146 | #define KEY_DOWN_ARROW 0xD9 147 | #define KEY_LEFT_ARROW 0xD8 148 | #define KEY_RIGHT_ARROW 0xD7 149 | #define KEY_BACKSPACE 0xB2 150 | #define KEY_TAB 0xB3 151 | #define KEY_RETURN 0xB0 152 | #define KEY_ESC 0xB1 153 | #define KEY_INSERT 0xD1 154 | #define KEY_DELETE 0xD4 155 | #define KEY_PAGE_UP 0xD3 156 | #define KEY_PAGE_DOWN 0xD6 157 | #define KEY_HOME 0xD2 158 | #define KEY_END 0xD5 159 | #define KEY_CAPS_LOCK 0xC1 160 | #define KEY_F1 0xC2 161 | #define KEY_F2 0xC3 162 | #define KEY_F3 0xC4 163 | #define KEY_F4 0xC5 164 | #define KEY_F5 0xC6 165 | #define KEY_F6 0xC7 166 | #define KEY_F7 0xC8 167 | #define KEY_F8 0xC9 168 | #define KEY_F9 0xCA 169 | #define KEY_F10 0xCB 170 | #define KEY_F11 0xCC 171 | #define KEY_F12 0xCD 172 | 173 | // Low level key report: up to 6 keys and shift, ctrl etc at once 174 | typedef struct 175 | { 176 | uint8_t modifiers; 177 | uint8_t reserved; 178 | uint8_t keys[6]; 179 | } KeyReport; 180 | 181 | class Keyboard_ : public Print 182 | { 183 | protected: 184 | KeyReport _keyReport; 185 | void sendReport(KeyReport* keys); 186 | public: 187 | Keyboard_(void); 188 | void begin(void); 189 | void end(void); 190 | virtual size_t write(uint8_t k); 191 | virtual size_t press(uint8_t k); 192 | virtual size_t release(uint8_t k); 193 | virtual void releaseAll(void); 194 | }; 195 | extern Keyboard_ Keyboard; 196 | 197 | //================================================================================ 198 | //================================================================================ 199 | // Low level API 200 | 201 | typedef struct 202 | { 203 | uint8_t bmRequestType; 204 | uint8_t bRequest; 205 | uint8_t wValueL; 206 | uint8_t wValueH; 207 | uint16_t wIndex; 208 | uint16_t wLength; 209 | } Setup; 210 | 211 | //================================================================================ 212 | //================================================================================ 213 | // HID 'Driver' 214 | 215 | int HID_GetNumInterfaces(void); 216 | int HID_GetInterfaceDataLength(void); 217 | int HID_SendInterfaceData(void); 218 | bool HID_SendDeviceDescriptor(uint8_t nLen); 219 | 220 | int HID_GetDescriptor(int i); // handles the 'GET DESCRIPTOR' control packet 221 | bool HID_Setup(Setup& setup); // handles a 'SETUP' control packet 222 | 223 | void HID_SendReport(uint8_t id, const void* data, int len); 224 | 225 | void HID_Reset(void); // called whenever I get a bus reset 226 | 227 | //================================================================================ 228 | //================================================================================ 229 | // CDC 'Driver' 230 | 231 | bool CDC_SendIAD(void); 232 | int CDC_GetNumInterfaces(void); 233 | int CDC_GetInterfaceDataLength(void); 234 | int CDC_SendInterfaceData(void); 235 | bool CDC_SendDeviceDescriptor(uint8_t nLen); 236 | 237 | int CDC_GetDescriptor(int i); // handles the 'GET DESCRIPTOR' control packet 238 | bool CDC_Setup(Setup& setup); // handles a 'SETUP' control packet 239 | void CDC_FrameReceived(void); // call when frame is received and EP is configured 240 | void CDC_SendACM(void); // call when you need to send a packet on the interrupt EP 241 | 242 | void CDC_Reset(void); // called whenever I get a bus reset 243 | 244 | 245 | //================================================================================ 246 | //================================================================================ 247 | 248 | #define TRANSFER_PGM 0x80 249 | #define TRANSFER_RELEASE 0x40 250 | #define TRANSFER_TOGGLE_ON 0x20 /* assign this to pre-set the 'toggle' bit on - only works when send queue is empty */ 251 | #define TRANSFER_TOGGLE_OFF 0x10 /* assign this to pre-set the 'toggle' bit off - only works when send queue is empty */ 252 | 253 | // NOTE: USB_SendControl returns # of bytes sent, or 0x8000 if a ZLP is sent 254 | // it will return 0 on error, such as the inability to allocate a buffer 255 | // control packets send 64 bytes at a time, so the total size is limited 256 | // by the number of available buffers. 257 | 258 | int USB_SendControl(uint8_t flags, const void* d, int len); 259 | #ifdef PROGMEM 260 | int USB_SendControlP(uint8_t flags, const void * PROGMEM d, int len); 261 | // called internally if you use TRANSFER_PGM flag; you can also call this directly 262 | #endif // PROGMEM 263 | 264 | uint16_t USB_Available(uint8_t ep); // returns # of bytes in the buffer on an OUT or CONTROL endpoint 265 | uint16_t USB_SendQLength(uint8_t ep); // returns # of buffers in the send queue for an IN or CONTROL endpoint 266 | bool USB_IsSendQFull(uint8_t ep); // this returns TRUE if there are too many outgoing buffers already (IN, CONTROL) 267 | bool USB_IsStalled(uint8_t ep); // this tells me I'm 'stalled' (BULK IN, INTERRUPT, CONTROL) 268 | int USB_Send(uint8_t ep, const void* data, // send endpoint data. bSendNow marks it "to send" 269 | int len, uint8_t bSendNow); 270 | int USB_Recv(uint8_t ep, void* data, // 'receive' data from endpoint receive queue. returns < 0 on error, or # of bytes 271 | int len); 272 | int USB_Recv(uint8_t ep); // 'receive' one byte of data from endpoint receive queue 273 | void USB_Flush(uint8_t ep); // 'sends' all pending data by marking the buffers "to send" 274 | 275 | uint16_t GetFrameNumber(void); // a debug API to obtain the latest USB frame number 276 | uint8_t USB_GetEPType(uint8_t nEP); // another debug API to return endpoint type by index 277 | 278 | #endif 279 | 280 | #endif /* if defined(USBCON) */ 281 | 282 | -------------------------------------------------------------------------------- /cores/xmega/USBCore.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // // 3 | // _ _ ____ ____ ____ _ // 4 | // | | | |/ ___| | __ ) / ___| ___ _ __ ___ | |__ // 5 | // | | | |\___ \ | _ \ | | / _ \ | '__|/ _ \ | '_ \ // 6 | // | |_| | ___) || |_) || |___| (_) || | | __/ _ | | | | // 7 | // \___/ |____/ |____/ \____|\___/ |_| \___|(_)|_| |_| // 8 | // // 9 | // // 10 | ////////////////////////////////////////////////////////////////////////////// 11 | 12 | // Copyright (c) 2010, Peter Barrett 13 | /* 14 | ** Permission to use, copy, modify, and/or distribute this software for 15 | ** any purpose with or without fee is hereby granted, provided that the 16 | ** above copyright notice and this permission notice appear in all copies. 17 | ** 18 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 19 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 20 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 21 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 22 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 23 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 25 | ** SOFTWARE. 26 | */ 27 | 28 | #ifndef __USBCORE_H__ 29 | #define __USBCORE_H__ 30 | 31 | // USB implementation MUST have USB_VID and USB_PID defined - can be done in 'boards.txt' 32 | #if defined(USBCON) /* TODO add others here, things that require VID and PID for USB */ 33 | #ifdef USB_VID 34 | #if USB_VID==null 35 | #error cannot work with NULL value for VID 36 | #endif // USB_VID==null 37 | #else 38 | #error must define USB_VID 39 | #endif // USB_VID 40 | 41 | #ifdef USB_PID 42 | #if USB_PID==null 43 | #error cannot work with NULL value for PID 44 | #endif // USB_PID==null 45 | #else 46 | #error must define USB_PID 47 | #endif // USB_PID 48 | #endif // defined(USBCON) 49 | 50 | 51 | #ifndef _PLATFORM_H_TYPES_DEFINED_ /* TEMPORARY FIX, SEE Platform.h */ 52 | #define _PLATFORM_H_TYPES_DEFINED_ 53 | typedef unsigned char u8; 54 | typedef unsigned short u16; 55 | typedef unsigned long u32; 56 | #endif // _PLATFORM_H_TYPES_DEFINED_ 57 | 58 | // Standard requests 59 | #define GET_STATUS 0 60 | #define CLEAR_FEATURE 1 61 | #define SET_FEATURE 3 62 | #define SET_ADDRESS 5 63 | #define GET_DESCRIPTOR 6 64 | #define SET_DESCRIPTOR 7 65 | #define GET_CONFIGURATION 8 66 | #define SET_CONFIGURATION 9 67 | #define GET_INTERFACE 10 68 | #define SET_INTERFACE 11 69 | 70 | 71 | // bmRequestType 72 | #define REQUEST_HOSTTODEVICE 0x00 73 | #define REQUEST_DEVICETOHOST 0x80 /* device to host, i.e. 'in' - when not set, it's 'out' (host to device) */ 74 | #define REQUEST_DIRECTION 0x80 75 | 76 | #define REQUEST_STANDARD 0x00 77 | #define REQUEST_CLASS 0x20 78 | #define REQUEST_VENDOR 0x40 79 | #define REQUEST_TYPE 0x60 80 | 81 | #define REQUEST_DEVICE 0x00 82 | #define REQUEST_INTERFACE 0x01 83 | #define REQUEST_ENDPOINT 0x02 84 | #define REQUEST_OTHER 0x03 85 | #define REQUEST_RECIPIENT 0x03 86 | 87 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE) 88 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE) 89 | 90 | // Class requests 91 | 92 | #define CDC_SET_LINE_CODING 0x20 93 | #define CDC_GET_LINE_CODING 0x21 94 | #define CDC_SET_CONTROL_LINE_STATE 0x22 95 | 96 | #define MSC_RESET 0xFF 97 | #define MSC_GET_MAX_LUN 0xFE 98 | 99 | #define HID_GET_REPORT 0x01 100 | #define HID_GET_IDLE 0x02 101 | #define HID_GET_PROTOCOL 0x03 102 | #define HID_SET_REPORT 0x09 103 | #define HID_SET_IDLE 0x0A 104 | #define HID_SET_PROTOCOL 0x0B 105 | 106 | // Descriptors 107 | 108 | #define USB_DEVICE_DESC_SIZE 18 109 | #define USB_CONFIGUARTION_DESC_SIZE 9 110 | #define USB_INTERFACE_DESC_SIZE 9 111 | #define USB_ENDPOINT_DESC_SIZE 7 112 | 113 | 114 | #define USB_DEVICE_DESCRIPTOR_TYPE 1 115 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 116 | #define USB_STRING_DESCRIPTOR_TYPE 3 117 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4 118 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5 119 | #define USB_DEVICE_QUALIFIER_TYPE 6 120 | #define USB_OTHER_SPEED_CONFIGURATION_TYPE 7 121 | #define USB_INTERFACE_POWER_TYPE 8 122 | #define USB_OTG_TYPE 9 123 | #define USB_DEVICE_DEBUG_TYPE 10 124 | #define USB_INTERFACE_ASSOCIATION_TYPE 11 /* see InterfaceAssociationDescriptor_ecn.pdf */ 125 | // NOTE: for USB_INTERFACE_ASSOCIATION_TYPE respond with IADDescriptor 126 | 127 | #define USB_DEVICE_CLASS_ZERO 0x00 128 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 129 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 130 | #define USB_DEVICE_CLASS_STORAGE 0x08 131 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF 132 | 133 | #define USB_CONFIG_POWERED_MASK 0x40 134 | #define USB_CONFIG_BUS_POWERED 0x80 135 | #define USB_CONFIG_SELF_POWERED 0xC0 136 | #define USB_CONFIG_REMOTE_WAKEUP 0x20 137 | 138 | // bMaxPower in Configuration Descriptor 139 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2) 140 | 141 | // bEndpointAddress in Endpoint Descriptor 142 | #define USB_ENDPOINT_DIRECTION_MASK 0x80 143 | #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00) 144 | #define USB_ENDPOINT_IN(addr) ((addr) | 0x80) 145 | 146 | #define USB_ENDPOINT_TYPE_MASK 0x03 147 | #define USB_ENDPOINT_TYPE_CONTROL 0x00 148 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 149 | #define USB_ENDPOINT_TYPE_BULK 0x02 150 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03 151 | 152 | #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF) 153 | 154 | #define CDC_V1_10 0x0110 155 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 156 | 157 | #define CDC_CALL_MANAGEMENT 0x01 158 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02 159 | #define CDC_HEADER 0x00 160 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 161 | #define CDC_UNION 0x06 162 | #define CDC_CS_INTERFACE 0x24 163 | #define CDC_CS_ENDPOINT 0x25 164 | #define CDC_DATA_INTERFACE_CLASS 0x0A 165 | 166 | #define MSC_SUBCLASS_SCSI 0x06 167 | #define MSC_PROTOCOL_BULK_ONLY 0x50 168 | 169 | #define HID_HID_DESCRIPTOR_TYPE 0x21 170 | #define HID_REPORT_DESCRIPTOR_TYPE 0x22 171 | #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23 172 | 173 | // CONTROL LINE STATE and SERIAL STATE - BIT VALUES (16-bits, LE) 174 | // USED BY 'SET CONTROL LINE STATE' and 'SERIAL STATE' FOR CAM INTERRUPT 'IN' 175 | #define CONTROL_LINE_STATE_DTR 0x01 /* data terminal ready [ok to send on IN] */ 176 | #define CONTROL_LINE_STATE_RTS 0x02 /* ready to send [has data ready for OUT] */ 177 | #define SERIAL_STATE_RX_CARRIER_DCD 0x01 /* receive carrier detect - on to allow receive */ 178 | #define SERIAL_STATE_TX_CARRIER_DSR 0x02 /* data set ready - inform host I have data */ 179 | #define SERIAL_STATE_BREAK_DETECT 0x04 /* 'break' detect */ 180 | #define SERIAL_STATE_RING_DETECT 0x08 /* 'ring' detect */ 181 | #define SERIAL_STATE_FRAMING_ERROR 0x10 /* framing error */ 182 | #define SERIAL_STATE_PARITY_ERROR 0x20 /* parity error */ 183 | #define SERIAL_STATE_OVERRUN 0x40 /* overrun input (data lost) */ 184 | 185 | 186 | // A1U series needs 16-byte alignment for endpoint structure 187 | #if defined(__AVR_ATxmega64A1U__) || defined(__AVR_ATxmega128A1U__) 188 | #define A1U_SERIES 189 | #endif // A1U 190 | 191 | // Device 192 | typedef struct _device_descriptor_ 193 | { 194 | u8 len; // 18 195 | u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE 196 | u16 usbVersion; // 0x200 197 | u8 deviceClass; 198 | u8 deviceSubClass; 199 | u8 deviceProtocol; 200 | u8 packetSize0; // Packet 0 201 | u16 idVendor; 202 | u16 idProduct; 203 | u16 deviceVersion; // 0x100 204 | u8 iManufacturer; 205 | u8 iProduct; 206 | u8 iSerialNumber; 207 | u8 bNumConfigurations; 208 | } DeviceDescriptor; 209 | 210 | // Config 211 | typedef struct _config_descriptor_ 212 | { 213 | u8 len; // 9 214 | u8 dtype; // 2 215 | u16 clen; // total length 216 | u8 numInterfaces; 217 | u8 config; 218 | u8 iconfig; 219 | u8 attributes; 220 | u8 maxPower; 221 | } ConfigDescriptor; 222 | 223 | // String 224 | 225 | // Interface 226 | typedef struct _interface_descriptor_ 227 | { 228 | u8 len; // 9 229 | u8 dtype; // 4 230 | u8 number; 231 | u8 alternate; 232 | u8 numEndpoints; 233 | u8 interfaceClass; 234 | u8 interfaceSubClass; 235 | u8 protocol; 236 | u8 iInterface; 237 | } InterfaceDescriptor; 238 | 239 | // Endpoint 240 | typedef struct _endpoint_descriptor_ 241 | { 242 | u8 len; // 7 243 | u8 dtype; // 5 244 | u8 addr; 245 | u8 attr; 246 | u16 packetSize; 247 | u8 interval; 248 | } EndpointDescriptor; 249 | 250 | // Interface Association Descriptor 251 | // Used to bind 2 interfaces together in CDC compostite device 252 | typedef struct _iad_descriptor_ 253 | { 254 | u8 len; // 8 255 | u8 dtype; // 11 256 | u8 firstInterface; 257 | u8 interfaceCount; 258 | u8 functionClass; 259 | u8 funtionSubClass; 260 | u8 functionProtocol; 261 | u8 iInterface; 262 | } IADDescriptor; 263 | 264 | // CDC CS interface descriptor 265 | typedef struct _cdccs_interface_descriptor_ 266 | { 267 | u8 len; // 5 268 | u8 dtype; // 0x24 269 | u8 subtype; 270 | u8 d0; 271 | u8 d1; 272 | } CDCCSInterfaceDescriptor; 273 | 274 | typedef struct _cdccs_interface_descriptor4_ 275 | { 276 | u8 len; // 4 277 | u8 dtype; // 0x24 278 | u8 subtype; 279 | u8 d0; 280 | } CDCCSInterfaceDescriptor4; 281 | 282 | typedef struct _cm_functional_descriptor_ 283 | { 284 | u8 len; // 5 285 | u8 dtype; // 0x24 286 | u8 subtype; // 1 287 | u8 bmCapabilities; 288 | u8 bDataInterface; 289 | } CMFunctionalDescriptor; 290 | 291 | typedef struct _acm_functional_descriptor_ 292 | { 293 | u8 len; 294 | u8 dtype; // 0x24 295 | u8 subtype; // 1 296 | u8 bmCapabilities; 297 | } ACMFunctionalDescriptor; 298 | 299 | typedef struct _cdc_descriptor_ 300 | { 301 | // // IAD 302 | // IADDescriptor iad; // for complex endpoints (apparently not critical) 303 | 304 | // Control 305 | InterfaceDescriptor cif; // 306 | CDCCSInterfaceDescriptor header; 307 | // CMFunctionalDescriptor callManagement; // Call Management 308 | ACMFunctionalDescriptor controlManagement; // ACM 309 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION 310 | EndpointDescriptor cifin; 311 | 312 | // Data 313 | InterfaceDescriptor dif; 314 | EndpointDescriptor in; 315 | EndpointDescriptor out; 316 | } CDCDescriptor; 317 | 318 | typedef struct _msc_descriptor_ 319 | { 320 | InterfaceDescriptor msc; 321 | EndpointDescriptor in; 322 | EndpointDescriptor out; 323 | } MSCDescriptor; 324 | 325 | typedef struct _hid_desc_descriptor_ 326 | { 327 | u8 len; // 9 328 | u8 dtype; // 0x21 329 | u8 addr; 330 | u8 versionL; // 0x101 331 | u8 versionH; // 0x101 332 | u8 country; 333 | u8 desctype; // 0x22 report 334 | u8 descLenL; 335 | u8 descLenH; 336 | } HIDDescDescriptor; 337 | 338 | typedef struct _hid_descriptor_ 339 | { 340 | InterfaceDescriptor hid; 341 | HIDDescDescriptor desc; 342 | EndpointDescriptor in; 343 | } HIDDescriptor; 344 | 345 | 346 | // XMEGA STRUCTURES 347 | 348 | #define MAXEP 5 /*15*/ /* 4 bit value, 0-15 - see 20.14.1 'CTRLA register' in AU manual */ 349 | // I assigned this to '5' because there are only 5 endpoints being used by THIS code. 350 | // The 'mega' code could only have a total of 6 and 0 was always the 'control' endpoint 351 | // and setting a 'smaller-than-maximum' value for XMEGA burns up less RAM. 352 | // If this poses a problem I _could_ malloc the structures, but to be effective, it would 353 | // have to require a reboot to change it, or you'd get major RAM fragmentation 354 | // 355 | // TODO: put MAXEP into pins_arduino.h and override if too small (or not defined already) 356 | 357 | typedef union _xmega_fifo_entry_ 358 | { 359 | struct 360 | { 361 | uint8_t h, l; 362 | }; 363 | uint16_t heW; // high endian word 364 | } __attribute__(( packed )) XMegaFIFOEntry; // exactly 4 bytes 365 | 366 | typedef struct _xmega_fifo_ 367 | { 368 | XMegaFIFOEntry in; 369 | XMegaFIFOEntry out; 370 | } __attribute__(( packed )) XMegaFIFO; // must be exactly 8 bytes long 371 | 372 | // see AU manual, pg 231 section 20.4 373 | typedef struct _xmega_endpoint_descriptor_ 374 | { 375 | volatile uint8_t status; 376 | volatile uint8_t ctrl; 377 | volatile uint16_t /*HLByteWord*/ cnt; // so, cnt.w is the 16-bit value, cnt.l and cnt.h the 8-bit low/high 378 | // it is the 'data count' value. it may be zero. 379 | // only bits 1:0 of cnt.h are valid. cnt.w should be 'and'd with 0x3ff 380 | // the high bit of cnt.w and cnt.h is the 'AZLP' (auto zero length packet) bit 381 | // see AU manual section 20.15.4 382 | volatile uint16_t /*HLByteWord*/ dataptr; // pointer to data buffer. max packet length assigned to CTRL [1:0] or [2:0] 383 | // see table 20-5 in AU manual for max packet length. may need 2 buffers "that long" 384 | volatile uint16_t /*HLByteWord*/ auxdata; // used for multi-packet transfers 385 | } __attribute__((packed)) XMegaEndpointDescriptor; // NOTE: 2 per channel (one 'out', one 'in') pointed by EPPTR 386 | 387 | typedef struct _xmega_endpoint_channel_ 388 | { 389 | XMegaEndpointDescriptor out; // must be exactly 8 bytes 390 | XMegaEndpointDescriptor in; // must be exactly 8 bytes 391 | } 392 | #ifdef A1U_SERIES 393 | __attribute (( aligned (16) )) 394 | #else // not an A1U series 395 | __attribute__ (( aligned (2) )) 396 | #endif // A1U or not 397 | XMegaEndpointChannel; 398 | 399 | // also section 20.4 in AU manual 400 | typedef struct _XMegaEPDataStruct_ 401 | { 402 | #ifdef A1U_SERIES /* only for A1U - this was 'kind of' a bug */ 403 | #if ((MAXEP+1)%2) != 0 // A1U needs 16-byte boundary, not merely word-aligned (and XMegaFIFO is 8 bytes long, currently) 404 | uint8_t padding[sizeof(XMegaFIFO) * (2 - ((MAXEP+1)%2))] __attribute__ ((packed)); // this should 16-byte align 'endpoint' as required 405 | #endif // needs padding 406 | #endif // XMEGA A1U series 407 | XMegaFIFO fifo[MAXEP + 1]; 408 | XMegaEndpointChannel endpoint[MAXEP + 1] __attribute__ ((packed)); // point EPPTR to THIS (must be word boundary) 409 | volatile uint16_t framenum; // 1 word frame number (so struct length NOT aligned, maybe) 410 | } 411 | #ifdef A1U_SERIES 412 | __attribute (( aligned (16) )) 413 | #else // not an A1U series 414 | __attribute__ (( aligned (2) )) 415 | #endif // A1U or not 416 | XMegaEPDataStruct; ///*__attribute__ ((packed))*/; // note: point EPPTR to &endpoint[0], word alignment needed 417 | 418 | extern "C" uint16_t endpoint_data_pointer(void); 419 | #ifdef DEBUG_CODE 420 | #define EP_DATA_STRUCT() ((XMegaEPDataStruct *)(endpoint_data_pointer() - (uint16_t)&(((XMegaEPDataStruct *)0)->endpoint[0]))) 421 | #endif // DEBUG_CODE 422 | 423 | // this is the USB Device Descriptor 424 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ 425 | { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } 426 | 427 | #define D_CONFIG(_totalLength,_interfaces) \ 428 | { 9, 2, _totalLength, (uint8_t)_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) } 429 | 430 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ 431 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } 432 | 433 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ 434 | { 7, 5, _addr,_attr,_packetSize, _interval } 435 | 436 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ 437 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } 438 | 439 | #define D_HIDREPORT(_descriptorLength) \ 440 | { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 } 441 | 442 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } 443 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } 444 | 445 | 446 | #endif 447 | 448 | -------------------------------------------------------------------------------- /cores/xmega/USBDesc.h: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////////////// 2 | // // 3 | // _ _ ____ ____ ____ _ // 4 | // | | | |/ ___| | __ ) | _ \ ___ ___ ___ | |__ // 5 | // | | | |\___ \ | _ \ | | | | / _ \/ __| / __| | '_ \ // 6 | // | |_| | ___) || |_) || |_| || __/\__ \| (__ _ | | | | // 7 | // \___/ |____/ |____/ |____/ \___||___/ \___|(_)|_| |_| // 8 | // // 9 | // // 10 | ////////////////////////////////////////////////////////////////////////////// 11 | 12 | /* Copyright (c) 2011, Peter Barrett 13 | ** 14 | ** Permission to use, copy, modify, and/or distribute this software for 15 | ** any purpose with or without fee is hereby granted, provided that the 16 | ** above copyright notice and this permission notice appear in all copies. 17 | ** 18 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 19 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 20 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 21 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES 22 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 23 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 25 | ** SOFTWARE. 26 | */ 27 | 28 | // NOTE: don't define these here; define them in pins_arduino.h 29 | //#define CDC_ENABLED 30 | //#define HID_ENABLED 31 | 32 | 33 | #ifdef CDC_ENABLED 34 | #define CDC_INTERFACE_COUNT 2 35 | #define CDC_ENDPOINT_COUNT 3 36 | #else 37 | #define CDC_INTERFACE_COUNT 0 38 | #define CDC_ENPOINT_COUNT 0 39 | #endif 40 | 41 | #ifdef HID_ENABLED 42 | #define HID_INTERFACE_COUNT 1 43 | #define HID_ENPOINT_COUNT 1 44 | #else 45 | #define HID_INTERFACE_COUNT 0 46 | #define HID_ENPOINT_COUNT 0 47 | #endif 48 | 49 | #define CDC_ACM_INTERFACE 0 // CDC ACM interface 50 | #define CDC_DATA_INTERFACE 1 // CDC Data interface 51 | #define CDC_FIRST_ENDPOINT 1 52 | #define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First 53 | #define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1) 54 | #define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2) 55 | 56 | #define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface 57 | #define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT) 58 | #define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT) 59 | 60 | #define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT) 61 | 62 | #ifdef CDC_ENABLED 63 | #define CDC_RX CDC_ENDPOINT_OUT 64 | #define CDC_TX CDC_ENDPOINT_IN 65 | #define CDC_ACM CDC_ENDPOINT_ACM 66 | #endif 67 | 68 | #ifdef HID_ENABLED 69 | #define HID_TX HID_ENDPOINT_INT 70 | #endif 71 | 72 | // string indexes/indices for USB_STRING_DESCRIPTOR_TYPE 73 | #define USB_STRING_INDEX_LANGUAGE 0 /* not actually a STRING though */ 74 | #define USB_STRING_INDEX_MANUFACTURER 1 75 | #define USB_STRING_INDEX_PRODUCT 2 76 | #define USB_STRING_INDEX_DESCRIPTION 3 77 | #define USB_STRING_INDEX_VERSION 4 78 | #define USB_STRING_INDEX_URL 5 79 | #define USB_STRING_INDEX_SERIAL 6 80 | #define USB_STRING_INDEX_CONFIG 7 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /cores/xmega/Udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets. 3 | * 4 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 5 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 6 | * might not happen often in practice, but in larger network topologies, a UDP 7 | * packet can be received out of sequence. 8 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 9 | * aware of it. Again, this may not be a concern in practice on small local networks. 10 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 11 | * 12 | * MIT License: 13 | * Copyright (c) 2008 Bjoern Hartmann 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to deal 16 | * in the Software without restriction, including without limitation the rights 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | * copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | * 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | * THE SOFTWARE. 31 | * 32 | * bjoern@cs.stanford.edu 12/30/2008 33 | */ 34 | 35 | #ifndef udp_h 36 | #define udp_h 37 | 38 | #include 39 | #include 40 | 41 | class UDP : public Stream { 42 | 43 | public: 44 | virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use 45 | virtual void stop() =0; // Finish with the UDP socket 46 | 47 | // Sending UDP packets 48 | 49 | // Start building up a packet to send to the remote host specific in ip and port 50 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port 51 | virtual int beginPacket(IPAddress ip, uint16_t port) =0; 52 | // Start building up a packet to send to the remote host specific in host and port 53 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 54 | virtual int beginPacket(const char *host, uint16_t port) =0; 55 | // Finish off this packet and send it 56 | // Returns 1 if the packet was sent successfully, 0 if there was an error 57 | virtual int endPacket() =0; 58 | // Write a single byte into the packet 59 | virtual size_t write(uint8_t) =0; 60 | // Write size bytes from buffer into the packet 61 | virtual size_t write(const uint8_t *buffer, size_t size) =0; 62 | 63 | // Start processing the next available incoming packet 64 | // Returns the size of the packet in bytes, or 0 if no packets are available 65 | virtual int parsePacket() =0; 66 | // Number of bytes remaining in the current packet 67 | virtual int available() =0; 68 | // Read a single byte from the current packet 69 | virtual int read() =0; 70 | // Read up to len bytes from the current packet and place them into buffer 71 | // Returns the number of bytes read, or 0 if none are available 72 | virtual int read(unsigned char* buffer, size_t len) =0; 73 | // Read up to len characters from the current packet and place them into buffer 74 | // Returns the number of characters read, or 0 if none are available 75 | virtual int read(char* buffer, size_t len) =0; 76 | // Return the next byte from the current packet without moving on to the next byte 77 | virtual int peek() =0; 78 | virtual void flush() =0; // Finish reading the current packet 79 | 80 | // Return the IP address of the host who sent the current incoming packet 81 | virtual IPAddress remoteIP() =0; 82 | // Return the port of the host who sent the current incoming packet 83 | virtual uint16_t remotePort() =0; 84 | protected: 85 | uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; 86 | }; 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /cores/xmega/WCharacter.h: -------------------------------------------------------------------------------- 1 | /* 2 | WCharacter.h - Character utility functions for Wiring & Arduino 3 | Copyright (c) 2010 Hernando Barragan. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Character_h 21 | #define Character_h 22 | 23 | #include 24 | 25 | // WCharacter.h prototypes 26 | inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); 27 | inline boolean isAlpha(int c) __attribute__((always_inline)); 28 | inline boolean isAscii(int c) __attribute__((always_inline)); 29 | inline boolean isWhitespace(int c) __attribute__((always_inline)); 30 | inline boolean isControl(int c) __attribute__((always_inline)); 31 | inline boolean isDigit(int c) __attribute__((always_inline)); 32 | inline boolean isGraph(int c) __attribute__((always_inline)); 33 | inline boolean isLowerCase(int c) __attribute__((always_inline)); 34 | inline boolean isPrintable(int c) __attribute__((always_inline)); 35 | inline boolean isPunct(int c) __attribute__((always_inline)); 36 | inline boolean isSpace(int c) __attribute__((always_inline)); 37 | inline boolean isUpperCase(int c) __attribute__((always_inline)); 38 | inline boolean isHexadecimalDigit(int c) __attribute__((always_inline)); 39 | inline int toAscii(int c) __attribute__((always_inline)); 40 | inline int toLowerCase(int c) __attribute__((always_inline)); 41 | inline int toUpperCase(int c)__attribute__((always_inline)); 42 | 43 | 44 | // Checks for an alphanumeric character. 45 | // It is equivalent to (isalpha(c) || isdigit(c)). 46 | inline boolean isAlphaNumeric(int c) 47 | { 48 | return ( isalnum(c) == 0 ? false : true); 49 | } 50 | 51 | 52 | // Checks for an alphabetic character. 53 | // It is equivalent to (isupper(c) || islower(c)). 54 | inline boolean isAlpha(int c) 55 | { 56 | return ( isalpha(c) == 0 ? false : true); 57 | } 58 | 59 | 60 | // Checks whether c is a 7-bit unsigned char value 61 | // that fits into the ASCII character set. 62 | inline boolean isAscii(int c) 63 | { 64 | return ( isascii (c) == 0 ? false : true); 65 | } 66 | 67 | 68 | // Checks for a blank character, that is, a space or a tab. 69 | inline boolean isWhitespace(int c) 70 | { 71 | return ( isblank (c) == 0 ? false : true); 72 | } 73 | 74 | 75 | // Checks for a control character. 76 | inline boolean isControl(int c) 77 | { 78 | return ( iscntrl (c) == 0 ? false : true); 79 | } 80 | 81 | 82 | // Checks for a digit (0 through 9). 83 | inline boolean isDigit(int c) 84 | { 85 | return ( isdigit (c) == 0 ? false : true); 86 | } 87 | 88 | 89 | // Checks for any printable character except space. 90 | inline boolean isGraph(int c) 91 | { 92 | return ( isgraph (c) == 0 ? false : true); 93 | } 94 | 95 | 96 | // Checks for a lower-case character. 97 | inline boolean isLowerCase(int c) 98 | { 99 | return (islower (c) == 0 ? false : true); 100 | } 101 | 102 | 103 | // Checks for any printable character including space. 104 | inline boolean isPrintable(int c) 105 | { 106 | return ( isprint (c) == 0 ? false : true); 107 | } 108 | 109 | 110 | // Checks for any printable character which is not a space 111 | // or an alphanumeric character. 112 | inline boolean isPunct(int c) 113 | { 114 | return ( ispunct (c) == 0 ? false : true); 115 | } 116 | 117 | 118 | // Checks for white-space characters. For the avr-libc library, 119 | // these are: space, formfeed ('\f'), newline ('\n'), carriage 120 | // return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). 121 | inline boolean isSpace(int c) 122 | { 123 | return ( isspace (c) == 0 ? false : true); 124 | } 125 | 126 | 127 | // Checks for an uppercase letter. 128 | inline boolean isUpperCase(int c) 129 | { 130 | return ( isupper (c) == 0 ? false : true); 131 | } 132 | 133 | 134 | // Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 135 | // 8 9 a b c d e f A B C D E F. 136 | inline boolean isHexadecimalDigit(int c) 137 | { 138 | return ( isxdigit (c) == 0 ? false : true); 139 | } 140 | 141 | 142 | // Converts c to a 7-bit unsigned char value that fits into the 143 | // ASCII character set, by clearing the high-order bits. 144 | inline int toAscii(int c) 145 | { 146 | return toascii (c); 147 | } 148 | 149 | 150 | // Warning: 151 | // Many people will be unhappy if you use this function. 152 | // This function will convert accented letters into random 153 | // characters. 154 | 155 | // Converts the letter c to lower case, if possible. 156 | inline int toLowerCase(int c) 157 | { 158 | return tolower (c); 159 | } 160 | 161 | 162 | // Converts the letter c to upper case, if possible. 163 | inline int toUpperCase(int c) 164 | { 165 | return toupper (c); 166 | } 167 | 168 | #endif -------------------------------------------------------------------------------- /cores/xmega/WMath.cpp: -------------------------------------------------------------------------------- 1 | /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2 | 3 | /* 4 | Part of the Wiring project - http://wiring.org.co 5 | Copyright (c) 2004-06 Hernando Barragan 6 | Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ 7 | 8 | This library is free software; you can redistribute it and/or 9 | modify it under the terms of the GNU Lesser General Public 10 | License as published by the Free Software Foundation; either 11 | version 2.1 of the License, or (at your option) any later version. 12 | 13 | This library is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General 19 | Public License along with this library; if not, write to the 20 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 21 | Boston, MA 02111-1307 USA 22 | 23 | $Id$ 24 | */ 25 | 26 | extern "C" { 27 | #include "stdlib.h" 28 | } 29 | 30 | void randomSeed(unsigned int seed) 31 | { 32 | if (seed != 0) { 33 | srandom(seed); 34 | } 35 | } 36 | 37 | long random(long howbig) 38 | { 39 | if (howbig == 0) { 40 | return 0; 41 | } 42 | return random() % howbig; 43 | } 44 | 45 | long random(long howsmall, long howbig) 46 | { 47 | if (howsmall >= howbig) { 48 | return howsmall; 49 | } 50 | long diff = howbig - howsmall; 51 | return random(diff) + howsmall; 52 | } 53 | 54 | long map(long x, long in_min, long in_max, long out_min, long out_max) 55 | { 56 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 57 | } 58 | 59 | unsigned int makeWord(unsigned int w) { return w; } 60 | unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } -------------------------------------------------------------------------------- /cores/xmega/WString.h: -------------------------------------------------------------------------------- 1 | /* 2 | WString.h - String library for Wiring & Arduino 3 | ...mostly rewritten by Paul Stoffregen... 4 | Copyright (c) 2009-10 Hernando Barragan. All right reserved. 5 | Copyright 2011, Paul Stoffregen, paul@pjrc.com 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | */ 21 | 22 | #ifndef String_class_h 23 | #define String_class_h 24 | #ifdef __cplusplus 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | // When compiling programs with this class, the following gcc parameters 32 | // dramatically increase performance and memory (RAM) efficiency, typically 33 | // with little or no increase in code size. 34 | // -felide-constructors 35 | // -std=c++0x 36 | 37 | class __FlashStringHelper; 38 | #define F(string_literal) (reinterpret_cast(PSTR(string_literal))) 39 | 40 | // An inherited class for holding the result of a concatenation. These 41 | // result objects are assumed to be writable by subsequent concatenations. 42 | class StringSumHelper; 43 | 44 | // The string class 45 | class String 46 | { 47 | // use a function pointer to allow for "if (s)" without the 48 | // complications of an operator bool(). for more information, see: 49 | // http://www.artima.com/cppsource/safebool.html 50 | typedef void (String::*StringIfHelperType)() const; 51 | void StringIfHelper() const {} 52 | 53 | public: 54 | // constructors 55 | // creates a copy of the initial value. 56 | // if the initial value is null or invalid, or if memory allocation 57 | // fails, the string will be marked as invalid (i.e. "if (s)" will 58 | // be false). 59 | String(const char *cstr = ""); 60 | String(const String &str); 61 | String(const __FlashStringHelper *str); 62 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ 63 | String(String &&rval); 64 | String(StringSumHelper &&rval); 65 | #endif 66 | explicit String(char c); 67 | explicit String(unsigned char, unsigned char base=10); 68 | explicit String(int, unsigned char base=10); 69 | explicit String(unsigned int, unsigned char base=10); 70 | explicit String(long, unsigned char base=10); 71 | explicit String(unsigned long, unsigned char base=10); 72 | explicit String(float, unsigned char decimalPlaces=2); 73 | explicit String(double, unsigned char decimalPlaces=2); 74 | ~String(void); 75 | 76 | // memory management 77 | // return true on success, false on failure (in which case, the string 78 | // is left unchanged). reserve(0), if successful, will validate an 79 | // invalid string (i.e., "if (s)" will be true afterwards) 80 | unsigned char reserve(unsigned int size); 81 | inline unsigned int length(void) const {return len;} 82 | 83 | // creates a copy of the assigned value. if the value is null or 84 | // invalid, or if the memory allocation fails, the string will be 85 | // marked as invalid ("if (s)" will be false). 86 | String & operator = (const String &rhs); 87 | String & operator = (const char *cstr); 88 | String & operator = (const __FlashStringHelper *str); 89 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ 90 | String & operator = (String &&rval); 91 | String & operator = (StringSumHelper &&rval); 92 | #endif 93 | 94 | // concatenate (works w/ built-in types) 95 | 96 | // returns true on success, false on failure (in which case, the string 97 | // is left unchanged). if the argument is null or invalid, the 98 | // concatenation is considered unsucessful. 99 | unsigned char concat(const String &str); 100 | unsigned char concat(const char *cstr); 101 | unsigned char concat(char c); 102 | unsigned char concat(unsigned char c); 103 | unsigned char concat(int num); 104 | unsigned char concat(unsigned int num); 105 | unsigned char concat(long num); 106 | unsigned char concat(unsigned long num); 107 | unsigned char concat(float num); 108 | unsigned char concat(double num); 109 | unsigned char concat(const __FlashStringHelper * str); 110 | 111 | // if there's not enough memory for the concatenated value, the string 112 | // will be left unchanged (but this isn't signalled in any way) 113 | String & operator += (const String &rhs) {concat(rhs); return (*this);} 114 | String & operator += (const char *cstr) {concat(cstr); return (*this);} 115 | String & operator += (char c) {concat(c); return (*this);} 116 | String & operator += (unsigned char num) {concat(num); return (*this);} 117 | String & operator += (int num) {concat(num); return (*this);} 118 | String & operator += (unsigned int num) {concat(num); return (*this);} 119 | String & operator += (long num) {concat(num); return (*this);} 120 | String & operator += (unsigned long num) {concat(num); return (*this);} 121 | String & operator += (float num) {concat(num); return (*this);} 122 | String & operator += (double num) {concat(num); return (*this);} 123 | String & operator += (const __FlashStringHelper *str){concat(str); return (*this);} 124 | 125 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); 126 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); 127 | friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); 128 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); 129 | friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); 130 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); 131 | friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); 132 | friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); 133 | friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); 134 | friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); 135 | friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs); 136 | 137 | // comparison (only works w/ Strings and "strings") 138 | operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } 139 | int compareTo(const String &s) const; 140 | unsigned char equals(const String &s) const; 141 | unsigned char equals(const char *cstr) const; 142 | unsigned char operator == (const String &rhs) const {return equals(rhs);} 143 | unsigned char operator == (const char *cstr) const {return equals(cstr);} 144 | unsigned char operator != (const String &rhs) const {return !equals(rhs);} 145 | unsigned char operator != (const char *cstr) const {return !equals(cstr);} 146 | unsigned char operator < (const String &rhs) const; 147 | unsigned char operator > (const String &rhs) const; 148 | unsigned char operator <= (const String &rhs) const; 149 | unsigned char operator >= (const String &rhs) const; 150 | unsigned char equalsIgnoreCase(const String &s) const; 151 | unsigned char startsWith( const String &prefix) const; 152 | unsigned char startsWith(const String &prefix, unsigned int offset) const; 153 | unsigned char endsWith(const String &suffix) const; 154 | 155 | // character acccess 156 | char charAt(unsigned int index) const; 157 | void setCharAt(unsigned int index, char c); 158 | char operator [] (unsigned int index) const; 159 | char& operator [] (unsigned int index); 160 | void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; 161 | void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const 162 | {getBytes((unsigned char *)buf, bufsize, index);} 163 | const char * c_str() const { return buffer; } 164 | 165 | // search 166 | int indexOf( char ch ) const; 167 | int indexOf( char ch, unsigned int fromIndex ) const; 168 | int indexOf( const String &str ) const; 169 | int indexOf( const String &str, unsigned int fromIndex ) const; 170 | int lastIndexOf( char ch ) const; 171 | int lastIndexOf( char ch, unsigned int fromIndex ) const; 172 | int lastIndexOf( const String &str ) const; 173 | int lastIndexOf( const String &str, unsigned int fromIndex ) const; 174 | String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); }; 175 | String substring( unsigned int beginIndex, unsigned int endIndex ) const; 176 | 177 | // modification 178 | void replace(char find, char replace); 179 | void replace(const String& find, const String& replace); 180 | void remove(unsigned int index); 181 | void remove(unsigned int index, unsigned int count); 182 | void toLowerCase(void); 183 | void toUpperCase(void); 184 | void trim(void); 185 | 186 | // parsing/conversion 187 | long toInt(void) const; 188 | float toFloat(void) const; 189 | 190 | protected: 191 | char *buffer; // the actual char array 192 | unsigned int capacity; // the array length minus one (for the '\0') 193 | unsigned int len; // the String length (not counting the '\0') 194 | protected: 195 | void init(void); 196 | void invalidate(void); 197 | unsigned char changeBuffer(unsigned int maxStrLen); 198 | unsigned char concat(const char *cstr, unsigned int length); 199 | 200 | // copy and move 201 | String & copy(const char *cstr, unsigned int length); 202 | String & copy(const __FlashStringHelper *pstr, unsigned int length); 203 | #ifdef __GXX_EXPERIMENTAL_CXX0X__ 204 | void move(String &rhs); 205 | #endif 206 | }; 207 | 208 | class StringSumHelper : public String 209 | { 210 | public: 211 | StringSumHelper(const String &s) : String(s) {} 212 | StringSumHelper(const char *p) : String(p) {} 213 | StringSumHelper(char c) : String(c) {} 214 | StringSumHelper(unsigned char num) : String(num) {} 215 | StringSumHelper(int num) : String(num) {} 216 | StringSumHelper(unsigned int num) : String(num) {} 217 | StringSumHelper(long num) : String(num) {} 218 | StringSumHelper(unsigned long num) : String(num) {} 219 | StringSumHelper(float num) : String(num) {} 220 | StringSumHelper(double num) : String(num) {} 221 | }; 222 | 223 | #endif // __cplusplus 224 | #endif // String_class_h 225 | -------------------------------------------------------------------------------- /cores/xmega/abi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2014 Arduino. All right reserved. 3 | 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | 9 | This library is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 | See the GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | #include 20 | 21 | extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); 22 | extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); 23 | 24 | void __cxa_pure_virtual(void) 25 | { 26 | // We might want to write some diagnostics to uart in this case 27 | //std::terminate(); 28 | abort(); 29 | } 30 | 31 | void __cxa_deleted_virtual(void) 32 | { 33 | // We might want to write some diagnostics to uart in this case 34 | //std::terminate(); 35 | abort(); 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /cores/xmega/avr-libc/malloc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2002, 2004, 2010 Joerg Wunsch 2 | Copyright (c) 2010 Gerben van den Broeke 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | * Neither the name of the copyright holders nor the names of 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 | POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | 34 | /* $Id: malloc.c 2149 2010-06-09 20:45:37Z joerg_wunsch $ */ 35 | 36 | #include 37 | #include "sectionname.h" 38 | #include "stdlib_private.h" 39 | 40 | #include 41 | 42 | /* 43 | * Exported interface: 44 | * 45 | * When extending the data segment, the allocator will not try to go 46 | * beyond the current stack limit, decreased by __malloc_margin bytes. 47 | * Thus, all possible stack frames of interrupt routines that could 48 | * interrupt the current function, plus all further nested function 49 | * calls must not require more stack space, or they'll risk to collide 50 | * with the data segment. 51 | */ 52 | 53 | /* May be changed by the user only before the first malloc() call. */ 54 | 55 | size_t __malloc_margin = 128; 56 | char *__malloc_heap_start = &__heap_start; 57 | char *__malloc_heap_end = &__heap_end; 58 | 59 | char *__brkval; 60 | struct __freelist *__flp; 61 | 62 | ATTRIBUTE_CLIB_SECTION 63 | void * 64 | malloc(size_t len) 65 | { 66 | struct __freelist *fp1, *fp2, *sfp1=NULL, *sfp2=NULL; // BBB - added '=NULL' for sfp1, sfp2 as they were warned as being uninitialized 67 | char *cp; 68 | size_t s, avail; 69 | 70 | /* 71 | * Our minimum chunk size is the size of a pointer (plus the 72 | * size of the "sz" field, but we don't need to account for 73 | * this), otherwise we could not possibly fit a freelist entry 74 | * into the chunk later. 75 | */ 76 | if (len < sizeof(struct __freelist) - sizeof(size_t)) 77 | len = sizeof(struct __freelist) - sizeof(size_t); 78 | 79 | /* 80 | * First, walk the free list and try finding a chunk that 81 | * would match exactly. If we found one, we are done. While 82 | * walking, note down the smallest chunk we found that would 83 | * still fit the request -- we need it for step 2. 84 | * 85 | */ 86 | for (s = 0, fp1 = __flp, fp2 = 0; 87 | fp1; 88 | fp2 = fp1, fp1 = fp1->nx) { 89 | if (fp1->sz < len) 90 | continue; 91 | if (fp1->sz == len) { 92 | /* 93 | * Found it. Disconnect the chunk from the 94 | * freelist, and return it. 95 | */ 96 | if (fp2) 97 | fp2->nx = fp1->nx; 98 | else 99 | __flp = fp1->nx; 100 | return &(fp1->nx); 101 | } 102 | else { 103 | if (s == 0 || fp1->sz < s) { 104 | /* this is the smallest chunk found so far */ 105 | s = fp1->sz; 106 | sfp1 = fp1; 107 | sfp2 = fp2; 108 | } 109 | } 110 | } 111 | /* 112 | * Step 2: If we found a chunk on the freelist that would fit 113 | * (but was too large), look it up again and use it, since it 114 | * is our closest match now. Since the freelist entry needs 115 | * to be split into two entries then, watch out that the 116 | * difference between the requested size and the size of the 117 | * chunk found is large enough for another freelist entry; if 118 | * not, just enlarge the request size to what we have found, 119 | * and use the entire chunk. 120 | */ 121 | if (s) { 122 | if (s - len < sizeof(struct __freelist)) { 123 | /* Disconnect it from freelist and return it. */ 124 | if (sfp2) 125 | sfp2->nx = sfp1->nx; 126 | else 127 | __flp = sfp1->nx; 128 | return &(sfp1->nx); 129 | } 130 | /* 131 | * Split them up. Note that we leave the first part 132 | * as the new (smaller) freelist entry, and return the 133 | * upper portion to the caller. This saves us the 134 | * work to fix up the freelist chain; we just need to 135 | * fixup the size of the current entry, and note down 136 | * the size of the new chunk before returning it to 137 | * the caller. 138 | */ 139 | cp = (char *)sfp1; 140 | s -= len; 141 | cp += s; 142 | sfp2 = (struct __freelist *)cp; 143 | sfp2->sz = len; 144 | sfp1->sz = s - sizeof(size_t); 145 | return &(sfp2->nx); 146 | } 147 | /* 148 | * Step 3: If the request could not be satisfied from a 149 | * freelist entry, just prepare a new chunk. This means we 150 | * need to obtain more memory first. The largest address just 151 | * not allocated so far is remembered in the brkval variable. 152 | * Under Unix, the "break value" was the end of the data 153 | * segment as dynamically requested from the operating system. 154 | * Since we don't have an operating system, just make sure 155 | * that we don't collide with the stack. 156 | */ 157 | if (__brkval == 0) 158 | __brkval = __malloc_heap_start; 159 | cp = __malloc_heap_end; 160 | if (cp == 0) 161 | cp = STACK_POINTER() - __malloc_margin; 162 | if (cp <= __brkval) 163 | /* 164 | * Memory exhausted. 165 | */ 166 | return 0; 167 | avail = cp - __brkval; 168 | /* 169 | * Both tests below are needed to catch the case len >= 0xfffe. 170 | */ 171 | if (avail >= len && avail >= len + sizeof(size_t)) { 172 | fp1 = (struct __freelist *)__brkval; 173 | __brkval += len + sizeof(size_t); 174 | fp1->sz = len; 175 | return &(fp1->nx); 176 | } 177 | /* 178 | * Step 4: There's no help, just fail. :-/ 179 | */ 180 | return 0; 181 | } 182 | 183 | 184 | ATTRIBUTE_CLIB_SECTION 185 | void 186 | free(void *p) 187 | { 188 | struct __freelist *fp1, *fp2, *fpnew; 189 | char *cp1, *cp2, *cpnew; 190 | 191 | /* ISO C says free(NULL) must be a no-op */ 192 | if (p == 0) 193 | return; 194 | 195 | cpnew = p; 196 | cpnew -= sizeof(size_t); 197 | fpnew = (struct __freelist *)cpnew; 198 | fpnew->nx = 0; 199 | 200 | /* 201 | * Trivial case first: if there's no freelist yet, our entry 202 | * will be the only one on it. If this is the last entry, we 203 | * can reduce __brkval instead. 204 | */ 205 | if (__flp == 0) { 206 | if ((char *)p + fpnew->sz == __brkval) 207 | __brkval = cpnew; 208 | else 209 | __flp = fpnew; 210 | return; 211 | } 212 | 213 | /* 214 | * Now, find the position where our new entry belongs onto the 215 | * freelist. Try to aggregate the chunk with adjacent chunks 216 | * if possible. 217 | */ 218 | for (fp1 = __flp, fp2 = 0; 219 | fp1; 220 | fp2 = fp1, fp1 = fp1->nx) { 221 | if (fp1 < fpnew) 222 | continue; 223 | cp1 = (char *)fp1; 224 | fpnew->nx = fp1; 225 | if ((char *)&(fpnew->nx) + fpnew->sz == cp1) { 226 | /* upper chunk adjacent, assimilate it */ 227 | fpnew->sz += fp1->sz + sizeof(size_t); 228 | fpnew->nx = fp1->nx; 229 | } 230 | if (fp2 == 0) { 231 | /* new head of freelist */ 232 | __flp = fpnew; 233 | return; 234 | } 235 | break; 236 | } 237 | /* 238 | * Note that we get here either if we hit the "break" above, 239 | * or if we fell off the end of the loop. The latter means 240 | * we've got a new topmost chunk. Either way, try aggregating 241 | * with the lower chunk if possible. 242 | */ 243 | fp2->nx = fpnew; 244 | cp2 = (char *)&(fp2->nx); 245 | if (cp2 + fp2->sz == cpnew) { 246 | /* lower junk adjacent, merge */ 247 | fp2->sz += fpnew->sz + sizeof(size_t); 248 | fp2->nx = fpnew->nx; 249 | } 250 | /* 251 | * If there's a new topmost chunk, lower __brkval instead. 252 | */ 253 | for (fp1 = __flp, fp2 = 0; 254 | fp1->nx != 0; 255 | fp2 = fp1, fp1 = fp1->nx) 256 | /* advance to entry just before end of list */; 257 | cp2 = (char *)&(fp1->nx); 258 | if (cp2 + fp1->sz == __brkval) { 259 | if (fp2 == NULL) 260 | /* Freelist is empty now. */ 261 | __flp = NULL; 262 | else 263 | fp2->nx = NULL; 264 | __brkval = cp2 - sizeof(size_t); 265 | } 266 | } 267 | 268 | -------------------------------------------------------------------------------- /cores/xmega/avr-libc/realloc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2004, 2010 Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the 13 | distribution. 14 | 15 | * Neither the name of the copyright holders nor the names of 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | /* $Id: realloc.c 2127 2010-06-07 14:49:37Z joerg_wunsch $ */ 32 | 33 | #include 34 | #include 35 | #include "sectionname.h" 36 | #include "stdlib_private.h" 37 | 38 | #include 39 | 40 | ATTRIBUTE_CLIB_SECTION 41 | void * 42 | realloc(void *ptr, size_t len) 43 | { 44 | struct __freelist *fp1, *fp2, *fp3, *ofp3; 45 | char *cp, *cp1; 46 | void *memp; 47 | size_t s, incr; 48 | 49 | /* Trivial case, required by C standard. */ 50 | if (ptr == 0) 51 | return malloc(len); 52 | 53 | cp1 = (char *)ptr; 54 | cp1 -= sizeof(size_t); 55 | fp1 = (struct __freelist *)cp1; 56 | 57 | cp = (char *)ptr + len; /* new next pointer */ 58 | if (cp < cp1) 59 | /* Pointer wrapped across top of RAM, fail. */ 60 | return 0; 61 | 62 | /* 63 | * See whether we are growing or shrinking. When shrinking, 64 | * we split off a chunk for the released portion, and call 65 | * free() on it. Therefore, we can only shrink if the new 66 | * size is at least sizeof(struct __freelist) smaller than the 67 | * previous size. 68 | */ 69 | if (len <= fp1->sz) { 70 | /* The first test catches a possible unsigned int 71 | * rollover condition. */ 72 | if (fp1->sz <= sizeof(struct __freelist) || 73 | len > fp1->sz - sizeof(struct __freelist)) 74 | return ptr; 75 | fp2 = (struct __freelist *)cp; 76 | fp2->sz = fp1->sz - len - sizeof(size_t); 77 | fp1->sz = len; 78 | free(&(fp2->nx)); 79 | return ptr; 80 | } 81 | 82 | /* 83 | * If we get here, we are growing. First, see whether there 84 | * is space in the free list on top of our current chunk. 85 | */ 86 | incr = len - fp1->sz; 87 | cp = (char *)ptr + fp1->sz; 88 | fp2 = (struct __freelist *)cp; 89 | for (s = 0, ofp3 = 0, fp3 = __flp; 90 | fp3; 91 | ofp3 = fp3, fp3 = fp3->nx) { 92 | if (fp3 == fp2 && fp3->sz + sizeof(size_t) >= incr) { 93 | /* found something that fits */ 94 | if (fp3->sz + sizeof(size_t) - incr > sizeof(struct __freelist)) { 95 | /* split off a new freelist entry */ 96 | cp = (char *)ptr + len; 97 | fp2 = (struct __freelist *)cp; 98 | fp2->nx = fp3->nx; 99 | fp2->sz = fp3->sz - incr; 100 | fp1->sz = len; 101 | } else { 102 | /* it just fits, so use it entirely */ 103 | fp1->sz += fp3->sz + sizeof(size_t); 104 | fp2 = fp3->nx; 105 | } 106 | if (ofp3) 107 | ofp3->nx = fp2; 108 | else 109 | __flp = fp2; 110 | return ptr; 111 | } 112 | /* 113 | * Find the largest chunk on the freelist while 114 | * walking it. 115 | */ 116 | if (fp3->sz > s) 117 | s = fp3->sz; 118 | } 119 | /* 120 | * If we are the topmost chunk in memory, and there was no 121 | * large enough chunk on the freelist that could be re-used 122 | * (by a call to malloc() below), quickly extend the 123 | * allocation area if possible, without need to copy the old 124 | * data. 125 | */ 126 | if (__brkval == (char *)ptr + fp1->sz && len > s) { 127 | cp1 = __malloc_heap_end; 128 | cp = (char *)ptr + len; 129 | if (cp1 == 0) 130 | cp1 = STACK_POINTER() - __malloc_margin; 131 | if (cp < cp1) { 132 | __brkval = cp; 133 | fp1->sz = len; 134 | return ptr; 135 | } 136 | /* If that failed, we are out of luck. */ 137 | return 0; 138 | } 139 | 140 | /* 141 | * Call malloc() for a new chunk, then copy over the data, and 142 | * release the old region. 143 | */ 144 | if ((memp = malloc(len)) == 0) 145 | return 0; 146 | memcpy(memp, ptr, fp1->sz); 147 | free(ptr); 148 | return memp; 149 | } 150 | 151 | -------------------------------------------------------------------------------- /cores/xmega/avr-libc/sectionname.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Atmel Corporation 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the 13 | distribution. 14 | 15 | * Neither the name of the copyright holders nor the names of 16 | contributors may be used to endorse or promote products derived 17 | from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef __SECTIONNAME_H__ 33 | #define __SECTIONNAME_H__ 34 | 35 | /* Put all avr-libc functions in a common, unique sub-section name under .text. */ 36 | 37 | #define CLIB_SECTION .text.avr-libc 38 | #define MLIB_SECTION .text.avr-libc.fplib 39 | 40 | #define STR(x) _STR(x) 41 | #define _STR(x) #x 42 | 43 | #define ATTRIBUTE_CLIB_SECTION __attribute__ ((section (STR(CLIB_SECTION)))) 44 | #define ATTRIBUTE_MLIB_SECTION __attribute__ ((section (STR(MLIB_SECTION)))) 45 | 46 | #define ASSEMBLY_CLIB_SECTION .section CLIB_SECTION, "ax", @progbits 47 | #define ASSEMBLY_MLIB_SECTION .section MLIB_SECTION, "ax", @progbits 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /cores/xmega/avr-libc/stdlib_private.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2004, Joerg Wunsch 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in 11 | the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of the copyright holders nor the names of 14 | contributors may be used to endorse or promote products derived 15 | from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | /* $Id: stdlib_private.h 1657 2008-03-24 17:11:08Z arcanum $ */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #if !defined(__DOXYGEN__) 37 | 38 | struct __freelist { 39 | size_t sz; 40 | struct __freelist *nx; 41 | }; 42 | 43 | #endif 44 | 45 | extern char *__brkval; /* first location not yet allocated */ 46 | extern struct __freelist *__flp; /* freelist pointer (head of freelist) */ 47 | extern size_t __malloc_margin; /* user-changeable before the first malloc() */ 48 | extern char *__malloc_heap_start; 49 | extern char *__malloc_heap_end; 50 | 51 | extern char __heap_start; 52 | extern char __heap_end; 53 | 54 | /* Needed for definition of AVR_STACK_POINTER_REG. */ 55 | #include 56 | 57 | #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG) 58 | 59 | -------------------------------------------------------------------------------- /cores/xmega/binary.h: -------------------------------------------------------------------------------- 1 | /* 2 | binary.h - Definitions for binary constants 3 | Copyright (c) 2006 David A. Mellis. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #ifndef Binary_h 21 | #define Binary_h 22 | 23 | #define B0 0 24 | #define B00 0 25 | #define B000 0 26 | #define B0000 0 27 | #define B00000 0 28 | #define B000000 0 29 | #define B0000000 0 30 | #define B00000000 0 31 | #define B1 1 32 | #define B01 1 33 | #define B001 1 34 | #define B0001 1 35 | #define B00001 1 36 | #define B000001 1 37 | #define B0000001 1 38 | #define B00000001 1 39 | #define B10 2 40 | #define B010 2 41 | #define B0010 2 42 | #define B00010 2 43 | #define B000010 2 44 | #define B0000010 2 45 | #define B00000010 2 46 | #define B11 3 47 | #define B011 3 48 | #define B0011 3 49 | #define B00011 3 50 | #define B000011 3 51 | #define B0000011 3 52 | #define B00000011 3 53 | #define B100 4 54 | #define B0100 4 55 | #define B00100 4 56 | #define B000100 4 57 | #define B0000100 4 58 | #define B00000100 4 59 | #define B101 5 60 | #define B0101 5 61 | #define B00101 5 62 | #define B000101 5 63 | #define B0000101 5 64 | #define B00000101 5 65 | #define B110 6 66 | #define B0110 6 67 | #define B00110 6 68 | #define B000110 6 69 | #define B0000110 6 70 | #define B00000110 6 71 | #define B111 7 72 | #define B0111 7 73 | #define B00111 7 74 | #define B000111 7 75 | #define B0000111 7 76 | #define B00000111 7 77 | #define B1000 8 78 | #define B01000 8 79 | #define B001000 8 80 | #define B0001000 8 81 | #define B00001000 8 82 | #define B1001 9 83 | #define B01001 9 84 | #define B001001 9 85 | #define B0001001 9 86 | #define B00001001 9 87 | #define B1010 10 88 | #define B01010 10 89 | #define B001010 10 90 | #define B0001010 10 91 | #define B00001010 10 92 | #define B1011 11 93 | #define B01011 11 94 | #define B001011 11 95 | #define B0001011 11 96 | #define B00001011 11 97 | #define B1100 12 98 | #define B01100 12 99 | #define B001100 12 100 | #define B0001100 12 101 | #define B00001100 12 102 | #define B1101 13 103 | #define B01101 13 104 | #define B001101 13 105 | #define B0001101 13 106 | #define B00001101 13 107 | #define B1110 14 108 | #define B01110 14 109 | #define B001110 14 110 | #define B0001110 14 111 | #define B00001110 14 112 | #define B1111 15 113 | #define B01111 15 114 | #define B001111 15 115 | #define B0001111 15 116 | #define B00001111 15 117 | #define B10000 16 118 | #define B010000 16 119 | #define B0010000 16 120 | #define B00010000 16 121 | #define B10001 17 122 | #define B010001 17 123 | #define B0010001 17 124 | #define B00010001 17 125 | #define B10010 18 126 | #define B010010 18 127 | #define B0010010 18 128 | #define B00010010 18 129 | #define B10011 19 130 | #define B010011 19 131 | #define B0010011 19 132 | #define B00010011 19 133 | #define B10100 20 134 | #define B010100 20 135 | #define B0010100 20 136 | #define B00010100 20 137 | #define B10101 21 138 | #define B010101 21 139 | #define B0010101 21 140 | #define B00010101 21 141 | #define B10110 22 142 | #define B010110 22 143 | #define B0010110 22 144 | #define B00010110 22 145 | #define B10111 23 146 | #define B010111 23 147 | #define B0010111 23 148 | #define B00010111 23 149 | #define B11000 24 150 | #define B011000 24 151 | #define B0011000 24 152 | #define B00011000 24 153 | #define B11001 25 154 | #define B011001 25 155 | #define B0011001 25 156 | #define B00011001 25 157 | #define B11010 26 158 | #define B011010 26 159 | #define B0011010 26 160 | #define B00011010 26 161 | #define B11011 27 162 | #define B011011 27 163 | #define B0011011 27 164 | #define B00011011 27 165 | #define B11100 28 166 | #define B011100 28 167 | #define B0011100 28 168 | #define B00011100 28 169 | #define B11101 29 170 | #define B011101 29 171 | #define B0011101 29 172 | #define B00011101 29 173 | #define B11110 30 174 | #define B011110 30 175 | #define B0011110 30 176 | #define B00011110 30 177 | #define B11111 31 178 | #define B011111 31 179 | #define B0011111 31 180 | #define B00011111 31 181 | #define B100000 32 182 | #define B0100000 32 183 | #define B00100000 32 184 | #define B100001 33 185 | #define B0100001 33 186 | #define B00100001 33 187 | #define B100010 34 188 | #define B0100010 34 189 | #define B00100010 34 190 | #define B100011 35 191 | #define B0100011 35 192 | #define B00100011 35 193 | #define B100100 36 194 | #define B0100100 36 195 | #define B00100100 36 196 | #define B100101 37 197 | #define B0100101 37 198 | #define B00100101 37 199 | #define B100110 38 200 | #define B0100110 38 201 | #define B00100110 38 202 | #define B100111 39 203 | #define B0100111 39 204 | #define B00100111 39 205 | #define B101000 40 206 | #define B0101000 40 207 | #define B00101000 40 208 | #define B101001 41 209 | #define B0101001 41 210 | #define B00101001 41 211 | #define B101010 42 212 | #define B0101010 42 213 | #define B00101010 42 214 | #define B101011 43 215 | #define B0101011 43 216 | #define B00101011 43 217 | #define B101100 44 218 | #define B0101100 44 219 | #define B00101100 44 220 | #define B101101 45 221 | #define B0101101 45 222 | #define B00101101 45 223 | #define B101110 46 224 | #define B0101110 46 225 | #define B00101110 46 226 | #define B101111 47 227 | #define B0101111 47 228 | #define B00101111 47 229 | #define B110000 48 230 | #define B0110000 48 231 | #define B00110000 48 232 | #define B110001 49 233 | #define B0110001 49 234 | #define B00110001 49 235 | #define B110010 50 236 | #define B0110010 50 237 | #define B00110010 50 238 | #define B110011 51 239 | #define B0110011 51 240 | #define B00110011 51 241 | #define B110100 52 242 | #define B0110100 52 243 | #define B00110100 52 244 | #define B110101 53 245 | #define B0110101 53 246 | #define B00110101 53 247 | #define B110110 54 248 | #define B0110110 54 249 | #define B00110110 54 250 | #define B110111 55 251 | #define B0110111 55 252 | #define B00110111 55 253 | #define B111000 56 254 | #define B0111000 56 255 | #define B00111000 56 256 | #define B111001 57 257 | #define B0111001 57 258 | #define B00111001 57 259 | #define B111010 58 260 | #define B0111010 58 261 | #define B00111010 58 262 | #define B111011 59 263 | #define B0111011 59 264 | #define B00111011 59 265 | #define B111100 60 266 | #define B0111100 60 267 | #define B00111100 60 268 | #define B111101 61 269 | #define B0111101 61 270 | #define B00111101 61 271 | #define B111110 62 272 | #define B0111110 62 273 | #define B00111110 62 274 | #define B111111 63 275 | #define B0111111 63 276 | #define B00111111 63 277 | #define B1000000 64 278 | #define B01000000 64 279 | #define B1000001 65 280 | #define B01000001 65 281 | #define B1000010 66 282 | #define B01000010 66 283 | #define B1000011 67 284 | #define B01000011 67 285 | #define B1000100 68 286 | #define B01000100 68 287 | #define B1000101 69 288 | #define B01000101 69 289 | #define B1000110 70 290 | #define B01000110 70 291 | #define B1000111 71 292 | #define B01000111 71 293 | #define B1001000 72 294 | #define B01001000 72 295 | #define B1001001 73 296 | #define B01001001 73 297 | #define B1001010 74 298 | #define B01001010 74 299 | #define B1001011 75 300 | #define B01001011 75 301 | #define B1001100 76 302 | #define B01001100 76 303 | #define B1001101 77 304 | #define B01001101 77 305 | #define B1001110 78 306 | #define B01001110 78 307 | #define B1001111 79 308 | #define B01001111 79 309 | #define B1010000 80 310 | #define B01010000 80 311 | #define B1010001 81 312 | #define B01010001 81 313 | #define B1010010 82 314 | #define B01010010 82 315 | #define B1010011 83 316 | #define B01010011 83 317 | #define B1010100 84 318 | #define B01010100 84 319 | #define B1010101 85 320 | #define B01010101 85 321 | #define B1010110 86 322 | #define B01010110 86 323 | #define B1010111 87 324 | #define B01010111 87 325 | #define B1011000 88 326 | #define B01011000 88 327 | #define B1011001 89 328 | #define B01011001 89 329 | #define B1011010 90 330 | #define B01011010 90 331 | #define B1011011 91 332 | #define B01011011 91 333 | #define B1011100 92 334 | #define B01011100 92 335 | #define B1011101 93 336 | #define B01011101 93 337 | #define B1011110 94 338 | #define B01011110 94 339 | #define B1011111 95 340 | #define B01011111 95 341 | #define B1100000 96 342 | #define B01100000 96 343 | #define B1100001 97 344 | #define B01100001 97 345 | #define B1100010 98 346 | #define B01100010 98 347 | #define B1100011 99 348 | #define B01100011 99 349 | #define B1100100 100 350 | #define B01100100 100 351 | #define B1100101 101 352 | #define B01100101 101 353 | #define B1100110 102 354 | #define B01100110 102 355 | #define B1100111 103 356 | #define B01100111 103 357 | #define B1101000 104 358 | #define B01101000 104 359 | #define B1101001 105 360 | #define B01101001 105 361 | #define B1101010 106 362 | #define B01101010 106 363 | #define B1101011 107 364 | #define B01101011 107 365 | #define B1101100 108 366 | #define B01101100 108 367 | #define B1101101 109 368 | #define B01101101 109 369 | #define B1101110 110 370 | #define B01101110 110 371 | #define B1101111 111 372 | #define B01101111 111 373 | #define B1110000 112 374 | #define B01110000 112 375 | #define B1110001 113 376 | #define B01110001 113 377 | #define B1110010 114 378 | #define B01110010 114 379 | #define B1110011 115 380 | #define B01110011 115 381 | #define B1110100 116 382 | #define B01110100 116 383 | #define B1110101 117 384 | #define B01110101 117 385 | #define B1110110 118 386 | #define B01110110 118 387 | #define B1110111 119 388 | #define B01110111 119 389 | #define B1111000 120 390 | #define B01111000 120 391 | #define B1111001 121 392 | #define B01111001 121 393 | #define B1111010 122 394 | #define B01111010 122 395 | #define B1111011 123 396 | #define B01111011 123 397 | #define B1111100 124 398 | #define B01111100 124 399 | #define B1111101 125 400 | #define B01111101 125 401 | #define B1111110 126 402 | #define B01111110 126 403 | #define B1111111 127 404 | #define B01111111 127 405 | #define B10000000 128 406 | #define B10000001 129 407 | #define B10000010 130 408 | #define B10000011 131 409 | #define B10000100 132 410 | #define B10000101 133 411 | #define B10000110 134 412 | #define B10000111 135 413 | #define B10001000 136 414 | #define B10001001 137 415 | #define B10001010 138 416 | #define B10001011 139 417 | #define B10001100 140 418 | #define B10001101 141 419 | #define B10001110 142 420 | #define B10001111 143 421 | #define B10010000 144 422 | #define B10010001 145 423 | #define B10010010 146 424 | #define B10010011 147 425 | #define B10010100 148 426 | #define B10010101 149 427 | #define B10010110 150 428 | #define B10010111 151 429 | #define B10011000 152 430 | #define B10011001 153 431 | #define B10011010 154 432 | #define B10011011 155 433 | #define B10011100 156 434 | #define B10011101 157 435 | #define B10011110 158 436 | #define B10011111 159 437 | #define B10100000 160 438 | #define B10100001 161 439 | #define B10100010 162 440 | #define B10100011 163 441 | #define B10100100 164 442 | #define B10100101 165 443 | #define B10100110 166 444 | #define B10100111 167 445 | #define B10101000 168 446 | #define B10101001 169 447 | #define B10101010 170 448 | #define B10101011 171 449 | #define B10101100 172 450 | #define B10101101 173 451 | #define B10101110 174 452 | #define B10101111 175 453 | #define B10110000 176 454 | #define B10110001 177 455 | #define B10110010 178 456 | #define B10110011 179 457 | #define B10110100 180 458 | #define B10110101 181 459 | #define B10110110 182 460 | #define B10110111 183 461 | #define B10111000 184 462 | #define B10111001 185 463 | #define B10111010 186 464 | #define B10111011 187 465 | #define B10111100 188 466 | #define B10111101 189 467 | #define B10111110 190 468 | #define B10111111 191 469 | #define B11000000 192 470 | #define B11000001 193 471 | #define B11000010 194 472 | #define B11000011 195 473 | #define B11000100 196 474 | #define B11000101 197 475 | #define B11000110 198 476 | #define B11000111 199 477 | #define B11001000 200 478 | #define B11001001 201 479 | #define B11001010 202 480 | #define B11001011 203 481 | #define B11001100 204 482 | #define B11001101 205 483 | #define B11001110 206 484 | #define B11001111 207 485 | #define B11010000 208 486 | #define B11010001 209 487 | #define B11010010 210 488 | #define B11010011 211 489 | #define B11010100 212 490 | #define B11010101 213 491 | #define B11010110 214 492 | #define B11010111 215 493 | #define B11011000 216 494 | #define B11011001 217 495 | #define B11011010 218 496 | #define B11011011 219 497 | #define B11011100 220 498 | #define B11011101 221 499 | #define B11011110 222 500 | #define B11011111 223 501 | #define B11100000 224 502 | #define B11100001 225 503 | #define B11100010 226 504 | #define B11100011 227 505 | #define B11100100 228 506 | #define B11100101 229 507 | #define B11100110 230 508 | #define B11100111 231 509 | #define B11101000 232 510 | #define B11101001 233 511 | #define B11101010 234 512 | #define B11101011 235 513 | #define B11101100 236 514 | #define B11101101 237 515 | #define B11101110 238 516 | #define B11101111 239 517 | #define B11110000 240 518 | #define B11110001 241 519 | #define B11110010 242 520 | #define B11110011 243 521 | #define B11110100 244 522 | #define B11110101 245 523 | #define B11110110 246 524 | #define B11110111 247 525 | #define B11111000 248 526 | #define B11111001 249 527 | #define B11111010 250 528 | #define B11111011 251 529 | #define B11111100 252 530 | #define B11111101 253 531 | #define B11111110 254 532 | #define B11111111 255 533 | 534 | #endif 535 | -------------------------------------------------------------------------------- /cores/xmega/hooks.c: -------------------------------------------------------------------------------- 1 | /* 2 | hooks.c - Partial implementation of the Wiring API for the ATmega8. 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2012 Arduino. All right reserved. 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 | See the GNU Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 | 21 | Updated for 'xmega' core by bob frazier, S.F.T. Inc. - http://mrp3.com/ 22 | 23 | In some cases, the xmega updates make assumptions about the pin assignments. 24 | See 'pins_arduino.h' for more detail. 25 | 26 | */ 27 | 28 | /** 29 | * Empty yield() hook. 30 | * 31 | * This function is intended to be used by library writers to build 32 | * libraries or sketches that supports cooperative threads. 33 | * 34 | * Its defined as a weak symbol and it can be redefined to implement a 35 | * real cooperative scheduler. 36 | */ 37 | static void __empty() 38 | { 39 | // Empty 40 | } 41 | 42 | void yield(void) __attribute__ ((weak, alias("__empty"))); 43 | -------------------------------------------------------------------------------- /cores/xmega/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | main.cpp - Main loop for Arduino sketches 3 | Copyright (c) 2005-2013 Arduino Team. All right reserved. 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | //#include Platform.h includes Arduino.h 21 | #include /* to make sure that Arduino.h is included, as well as pins_arduino.h and other things */ 22 | 23 | 24 | #ifdef EIND 25 | // if I have an EIND register, I want it pre-loaded with the correct value 26 | // for info on THIS thing, see http://gcc.gnu.org/onlinedocs/gcc/AVR-Options.html 27 | // in essence, 'init3' section functions run just before 'main()' 28 | 29 | // must prototype it to get all of the attributes 30 | static void __attribute__((section(".init3"),naked,used,no_instrument_function)) init3_set_eind (void); 31 | 32 | void init3_set_eind (void) 33 | { 34 | __asm volatile ("ldi r24,pm_hh8(__trampolines_start)\n\t" 35 | "out %i0,r24" :: "n" (&EIND) : "r24","memory"); 36 | } 37 | #endif // EIND 38 | 39 | //Declared weak in Arduino.h to allow user redefinitions. 40 | int atexit(void (*func)()) { return 0; } 41 | 42 | // Weak empty variant initialization function. 43 | // May be redefined by variant files. 44 | void initVariant() __attribute__((weak)); 45 | void initVariant() { } 46 | 47 | int main(void) 48 | { 49 | 50 | init(); 51 | 52 | initVariant(); 53 | 54 | // TEMPORARY - moved [so I can debug it] 55 | //#if defined(USBCON) 56 | // USBDevice.attach(); 57 | //#endif 58 | 59 | setup(); 60 | 61 | for (;;) { 62 | loop(); 63 | if (serialEventRun) serialEventRun(); 64 | } 65 | 66 | return 0; 67 | } 68 | 69 | -------------------------------------------------------------------------------- /cores/xmega/new.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void * operator new(size_t size) 4 | { 5 | return malloc(size); 6 | } 7 | 8 | void * operator new[](size_t size) 9 | { 10 | return malloc(size); 11 | } 12 | 13 | void operator delete(void * ptr) 14 | { 15 | free(ptr); 16 | } 17 | 18 | void operator delete[](void * ptr) 19 | { 20 | free(ptr); 21 | } 22 | 23 | int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);}; 24 | void __cxa_guard_release (__guard *g) {*(char *)g = 1;}; 25 | void __cxa_guard_abort (__guard *) {}; 26 | 27 | void __cxa_pure_virtual(void) {}; 28 | 29 | -------------------------------------------------------------------------------- /cores/xmega/new.h: -------------------------------------------------------------------------------- 1 | /* Header to define new/delete operators as they aren't provided by avr-gcc by default 2 | Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453 3 | */ 4 | 5 | #ifndef NEW_H 6 | #define NEW_H 7 | 8 | #include 9 | 10 | void * operator new(size_t size); 11 | void * operator new[](size_t size); 12 | void operator delete(void * ptr); 13 | void operator delete[](void * ptr); 14 | 15 | __extension__ typedef int __guard __attribute__((mode (__DI__))); 16 | 17 | extern "C" int __cxa_guard_acquire(__guard *); 18 | extern "C" void __cxa_guard_release (__guard *); 19 | extern "C" void __cxa_guard_abort (__guard *); 20 | 21 | extern "C" void __cxa_pure_virtual(void); 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /cores/xmega/wiring_digital.c: -------------------------------------------------------------------------------- 1 | /* 2 | wiring_digital.c - digital input and output functions 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2005-2006 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | 22 | Modified 28 September 2010 by Mark Sproul 23 | 24 | Updated for 'xmega' core by bob frazier, S.F.T. Inc. - http://mrp3.com/ 25 | 26 | In some cases, the xmega updates make assumptions about the pin assignments. 27 | See 'pins_arduino.h' for more detail. 28 | 29 | */ 30 | 31 | #define ARDUINO_MAIN 32 | #include "wiring_private.h" 33 | #include "pins_arduino.h" 34 | 35 | void pinMode(uint8_t pin, uint8_t mode) 36 | { 37 | uint8_t bit = digitalPinToBitMask(pin); 38 | uint8_t port = digitalPinToPort(pin); 39 | uint8_t sense = mode & INPUT_SENSE_MASK; 40 | uint8_t invert = mode & INPUT_OUTPUT_INVERT; 41 | volatile uint8_t *reg, /* *out,*/ *ctrl; 42 | 43 | mode &= INPUT_OUTPUT_MASK; // remove 'sense' bits 44 | 45 | if(port == NOT_A_PIN) 46 | { 47 | return; 48 | } 49 | 50 | reg = portModeRegister(port); // D manual section 11.12.1 51 | 52 | if(sense == INPUT_SENSE_DISABLED && reg != &PORTR_DIR) // 'DISABLED' 53 | { 54 | sense = PORT_ISC_INPUT_DISABLE_gc; // bit values for 'INTPUT_DISABLED' (sic) 55 | } 56 | else if(sense == INPUT_SENSE_RISING) 57 | { 58 | sense = PORT_ISC_RISING_gc; // rising 59 | } 60 | else if(sense == INPUT_SENSE_FALLING) 61 | { 62 | sense = PORT_ISC_FALLING_gc; // falling 63 | } 64 | else if(sense == INPUT_SENSE_LEVEL) 65 | { 66 | sense = PORT_ISC_LEVEL_gc; // LOW level (except events, which use HIGH level) 67 | } 68 | else // if(sense == INPUT_SENSE_BOTH) all others including 'DEFAULT' 69 | { 70 | sense = PORT_ISC_BOTHEDGES_gc; // 'both rising and falling' 71 | } 72 | 73 | if(invert) // inverted bit value 74 | { 75 | sense |= _BV(PORT_INVEN_bp); // see 11.12.15 in D manual - 'invert' bit 76 | } 77 | 78 | 79 | // JWS: can I let the optimizer do this? 80 | // reg = portModeRegister(port); // D manual section 11.12.1 (MOVED upwards, BBB) 81 | // out = portOutputRegister(port); // D manual section 11.12.5 (not used - BBB) 82 | ctrl = pinControlRegister(pin); // D manual section 11.12.15 83 | 84 | uint8_t oldSREG = SREG; 85 | cli(); // clear interrupt flag until I'm done assigning pin stuff 86 | 87 | if(mode == INPUT) 88 | { 89 | *ctrl = sense | PORT_OPC_TOTEM_gc; 90 | 91 | *reg &= ~bit; 92 | } 93 | else if(mode == INPUT_PULLUP) 94 | { 95 | *ctrl = sense | PORT_OPC_PULLUP_gc; // input pullup 96 | 97 | *reg &= ~bit; 98 | } 99 | else if(mode == INPUT_AND_PULLUP) 100 | { 101 | *ctrl = sense | PORT_OPC_WIREDANDPULL_gc; // wired 'and' (open drain) with pullup 102 | 103 | *reg &= ~bit; 104 | } 105 | else if(mode == INPUT_PULLDOWN) 106 | { 107 | *ctrl = sense | PORT_OPC_PULLDOWN_gc; // input pullDOWN 108 | 109 | *reg &= ~bit; 110 | } 111 | else if(mode == INPUT_OR_PULLDOWN) 112 | { 113 | *ctrl = sense | PORT_OPC_WIREDORPULL_gc; // wired 'or' (open drain) with pulldown 114 | 115 | *reg &= ~bit; 116 | } 117 | else if(mode == INPUT_BUS_KEEPER) 118 | { 119 | *ctrl = sense | PORT_OPC_BUSKEEPER_gc; // bus keeper 120 | 121 | *reg &= ~bit; 122 | } 123 | else if(mode == OUTPUT_OR) 124 | { 125 | *ctrl = sense | PORT_OPC_WIREDOR_gc; // wired 'or' (open drain) 126 | 127 | *reg |= bit; 128 | } 129 | else if(mode == OUTPUT_AND) 130 | { 131 | *ctrl = sense | PORT_OPC_WIREDAND_gc; // wired 'and' (open drain) 132 | 133 | *reg |= bit; 134 | } 135 | else if(mode == OUTPUT_OR_PULLDOWN) 136 | { 137 | *ctrl = sense | PORT_OPC_WIREDORPULL_gc; // wired 'or' (open drain) with pulldown 138 | 139 | *reg |= bit; 140 | } 141 | else if(mode == OUTPUT_AND_PULLUP) 142 | { 143 | *ctrl = sense | PORT_OPC_WIREDANDPULL_gc; // wired 'and' (open drain) with pullup 144 | 145 | *reg |= bit; 146 | } 147 | else // if(mode == OUTPUT) assume OUTPUT without open drain and/or nor pullup/down 148 | { 149 | *ctrl = sense | PORT_OPC_TOTEM_gc; // 'totem pole' (the default) 150 | 151 | *reg |= bit; 152 | } 153 | 154 | SREG = oldSREG; 155 | 156 | } 157 | 158 | // Forcing this inline keeps the callers from having to push their own stuff 159 | // on the stack. It is a good performance win and only takes 1 more byte per 160 | // user than calling. (It will take more bytes on the 168.) 161 | // 162 | // But shouldn't this be moved into pinMode? Seems silly to check and do on 163 | // each digitalread or write. 164 | // 165 | // Mark Sproul: 166 | // - Removed inline. Save 170 bytes on atmega1280 167 | // - changed to a switch statment; added 32 bytes but much easier to read and maintain. 168 | // - Added more #ifdefs, now compiles for atmega645 169 | // 170 | //static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); 171 | //static inline void turnOffPWM(uint8_t timer) 172 | 173 | // BBB - added 'bit' parameter for xmega - it's a bit MASK, not a bit number 174 | // use the result from digitalPinToBitMask(pin) for 'bit' 175 | 176 | static void turnOffPWM(uint8_t timer, uint8_t bit) 177 | { 178 | #ifdef TCC4 /* 'E' series and later that have TCC4 */ 179 | register uint8_t mode; 180 | #endif // TCC4 181 | 182 | #ifdef TCC4 /* 'E' series and later that have TCC4 */ 183 | if(timer == TIMERC4) 184 | { 185 | if(bit == 1) 186 | { 187 | mode = (TCC4_CTRLE & ~TC4_LCCAMODE_gm); 188 | } 189 | else if(bit == 2) 190 | { 191 | mode = (TCC4_CTRLE & ~TC4_LCCBMODE_gm); 192 | } 193 | else if(bit == 4) 194 | { 195 | mode = (TCC4_CTRLE & ~TC4_LCCCMODE_gm); 196 | } 197 | else if(bit == 8) 198 | { 199 | mode = (TCC4_CTRLE & ~TC4_LCCDMODE_gm); 200 | } 201 | else if(bit == 16) 202 | { 203 | mode = (TCC4_CTRLF & ~TC4_HCCAMODE_gm); 204 | } 205 | else if(bit == 32) 206 | { 207 | mode = (TCC4_CTRLF & ~TC4_HCCBMODE_gm); 208 | } 209 | else if(bit == 64) 210 | { 211 | mode = (TCC4_CTRLF & ~TC4_HCCCMODE_gm); 212 | } 213 | else if(bit == 128) 214 | { 215 | mode = (TCC4_CTRLF & ~TC4_HCCDMODE_gm); 216 | } 217 | else 218 | { 219 | return; 220 | } 221 | 222 | if(bit <= 8) 223 | { 224 | TCC4_CTRLE = mode; 225 | } 226 | else 227 | { 228 | TCC4_CTRLF = mode; 229 | } 230 | } 231 | else if(timer == TIMERD5) 232 | { 233 | if(bit == 1 || bit == 16) 234 | { 235 | mode = (TCD5_CTRLE & ~TC5_LCCAMODE_gm); 236 | } 237 | else if(bit == 2 || bit == 32) 238 | { 239 | mode = (TCD5_CTRLE & ~TC5_LCCBMODE_gm); 240 | } 241 | else if(bit == 4 || bit == 64) 242 | { 243 | mode = (TCD5_CTRLF & ~TC5_HCCAMODE_gm); 244 | } 245 | else if(bit == 8 || bit == 128) 246 | { 247 | mode = (TCD5_CTRLF & ~TC5_HCCBMODE_gm); 248 | } 249 | else 250 | { 251 | return; 252 | } 253 | 254 | // TODO: check to see if it was enabled AND the bit was configured properly for PWM 255 | // AND was properly mapped (L vs H, REMAP register?) 256 | if(bit == 1 || bit == 2 || bit == 16 || bit == 32) 257 | { 258 | TCD5_CTRLE = mode; 259 | } 260 | else 261 | { 262 | TCD5_CTRLF = mode; 263 | } 264 | } 265 | #else // everything else NOT an 'E' series 266 | 267 | #ifndef TCC2 /* A1 series does not define this and I need TC0 */ 268 | if(timer == TIMERD2) 269 | { 270 | TCD0_CTRLB &= ~bit; // DISables PWM output 271 | } 272 | else if(timer == TIMERC2) 273 | { 274 | TCC0_CTRLB &= ~bit; // DISables PWM output 275 | } 276 | #if NUM_DIGITAL_PINS > 22 /* which means we have PORT E but with 8 pins, not 4 */ 277 | else if(timer == TIMERE2) 278 | { 279 | TCE0_CTRLB &= ~bit; // DISables PWM output 280 | } 281 | #if NUM_DIGITAL_PINS > 30 /* which means we have PORT F */ 282 | else if(timer == TIMERF2) 283 | { 284 | TCF0_CTRLB &= ~bit; // DISables PWM output 285 | } 286 | #endif // NUM_DIGITAL_PINS > 30 287 | 288 | #elif NUM_DIGITAL_PINS > 18 /* which means we have PORT E but only with 4 pins */ 289 | else if(timer == TIMERE0) 290 | { 291 | TCE0_CTRLB &= ~(bit << 4); // DISables PWM output 292 | // note that the 'enable' bits are in CTRLB and in upper nybble 293 | } 294 | #endif // NUM_DIGITAL_PINS > 18, 22 295 | 296 | #else // TCC2 297 | if(timer == TIMERD2) 298 | { 299 | TCD2_CTRLB &= ~bit; // DISables PWM output 300 | } 301 | else if(timer == TIMERC2) 302 | { 303 | TCC2_CTRLB &= ~bit; // DISables PWM output 304 | } 305 | #if NUM_DIGITAL_PINS > 22 /* which means we have PORT E but with 8 pins, not 4 */ 306 | else if(timer == TIMERE2) 307 | { 308 | // 128A1 has 8 pins on PORT E. 309 | TCE2_CTRLB &= ~bit; // DISables PWM output 310 | } 311 | #if NUM_DIGITAL_PINS > 30 /* which means we have PORT F */ 312 | else if(timer == TIMERF2) 313 | { 314 | TCF2_CTRLB &= ~bit; // DISables PWM output 315 | } 316 | #endif // NUM_DIGITAL_PINS > 30 317 | 318 | #elif NUM_DIGITAL_PINS > 18 /* which means we have PORT E but only with 4 pins */ 319 | else if(timer == TIMERE0) 320 | { 321 | // 64d4 and 128a4 have 4 pins on PORT E, as implemented here 322 | TCE0_CTRLB &= ~(bit << 4); // DISables PWM output 323 | // note that the 'enable' bits are in CTRLB and in upper nybble 324 | } 325 | #endif // NUM_DIGITAL_PINS > 18, 22 326 | 327 | #endif // TCC2 328 | #endif // TCC4 329 | 330 | } 331 | 332 | void digitalWrite(uint8_t pin, uint8_t val) 333 | { 334 | uint8_t timer = digitalPinToTimer(pin); 335 | uint8_t bit = digitalPinToBitMask(pin); 336 | uint8_t port = digitalPinToPort(pin); 337 | volatile uint8_t *out, *ctrl; 338 | 339 | if(port == NOT_A_PIN) 340 | { 341 | return; 342 | } 343 | 344 | ctrl = pinControlRegister(pin); // D manual section 11.12.15 345 | 346 | if(*ctrl & _BV(PORT_INVEN_bp)) // inverted 347 | { 348 | val = !val; // invert the value (so it's consistent with the pin) 349 | } 350 | 351 | // If it's a pin that supports PWM output, we need to turn it off 352 | // before doing a digital write. 353 | 354 | // TODO: move this feature to pinMode() like it should be 355 | // or set a flag to be used with analogWrite() 356 | // (for now it's probably faster just to call it) 357 | 358 | if(timer != NOT_ON_TIMER) 359 | { 360 | turnOffPWM(timer, bit); 361 | } 362 | 363 | out = portOutputRegister(port); 364 | 365 | uint8_t oldSREG = SREG; 366 | cli(); 367 | 368 | if(val == LOW) 369 | { 370 | *out &= ~bit; 371 | } 372 | else 373 | { 374 | *out |= bit; 375 | } 376 | 377 | SREG = oldSREG; 378 | } 379 | 380 | int digitalRead(uint8_t pin) 381 | { 382 | uint8_t timer = digitalPinToTimer(pin); 383 | uint8_t bit = digitalPinToBitMask(pin); 384 | uint8_t port = digitalPinToPort(pin); 385 | volatile uint8_t *ctrl; 386 | uint8_t bSet; 387 | 388 | if(port == NOT_A_PIN) 389 | { 390 | return LOW; 391 | } 392 | 393 | // If the pin that support PWM output, we need to turn it off 394 | // before getting a digital reading. 395 | 396 | // TODO: move this feature to pinMode() like it should be 397 | // or set a flag to be used with analogWrite() 398 | // (for now it's probably faster just to call it) 399 | 400 | if(timer != NOT_ON_TIMER) 401 | { 402 | turnOffPWM(timer, bit); 403 | } 404 | 405 | bSet = (*portInputRegister(port) & bit) ? true : false; 406 | 407 | // if the 'invert' flag is on, I invert the digital value 408 | // this is so that the result of 'digitalRead' and 'digitalWrite' 409 | // are ALWAYS consistent with the actual pin level. Inversion is 410 | // needed for proper interrupt control. So for the best consistency, 411 | // the invert flag will only (really) be needed for LEVEL interrupts. 412 | 413 | ctrl = pinControlRegister(pin); // D manual section 11.12.15 414 | 415 | if(*ctrl & _BV(PORT_INVEN_bp)) // inverted 416 | { 417 | bSet = !bSet; 418 | } 419 | 420 | return bSet ? HIGH : LOW; 421 | } 422 | 423 | -------------------------------------------------------------------------------- /cores/xmega/wiring_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | wiring_private.h - Internal header file. 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2005-2006 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | 22 | $Id: wiring.h 239 2007-01-12 17:58:39Z mellis $ 23 | */ 24 | 25 | #ifndef WiringPrivate_h 26 | #define WiringPrivate_h 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include "Arduino.h" 34 | 35 | // things that need to be defined in order for the code to compile 36 | 37 | // definitions for ADC-related product signature row entries 38 | #ifndef PRODSIGNATURES_ADCACAL0 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 39 | #define PRODSIGNATURES_ADCACAL0 _SFR_MEM8(0x0020) 40 | #endif // PRODSIGNATURES_ADCACAL0 41 | 42 | #ifndef PRODSIGNATURES_ADCACAL1 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 43 | #define PRODSIGNATURES_ADCACAL1 _SFR_MEM8(0x0021) 44 | #endif // PRODSIGNATURES_ADCACAL1 45 | 46 | #ifndef PRODSIGNATURES_ADCBCAL0 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 47 | #define PRODSIGNATURES_ADCBCAL0 _SFR_MEM8(0x0024) 48 | #endif // PRODSIGNATURES_ADCBCAL0 49 | 50 | #ifndef PRODSIGNATURES_ADCBCAL1 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 51 | #define PRODSIGNATURES_ADCBCAL1 _SFR_MEM8(0x0025) 52 | #endif // PRODSIGNATURES_ADCBCAL1 53 | 54 | // definitions for USB-related product signature row entries 55 | #ifndef PRODSIGNATURES_USBCAL0 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 56 | #define PRODSIGNATURES_USBCAL0 _SFR_MEM8(0x001a) 57 | #endif // PRODSIGNATURES_USBCAL0 58 | 59 | #ifndef PRODSIGNATURES_USBCAL1 /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 60 | #define PRODSIGNATURES_USBCAL1 _SFR_MEM8(0x001b) 61 | #endif // PRODSIGNATURES_USBCAL1 62 | 63 | #ifndef PRODSIGNATURES_USBRCOSC /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 64 | #define PRODSIGNATURES_USBRCOSC _SFR_MEM8(0x001c) 65 | #endif // PRODSIGNATURES_USBRCOSC 66 | 67 | #ifndef PRODSIGNATURES_USBRCOSCA /* _MOST_ headers don't define this properly - see ATMel Studio headers */ 68 | #define PRODSIGNATURES_USBRCOSCA _SFR_MEM8(0x001d) 69 | #endif // PRODSIGNATURES_USBRCOSCA 70 | 71 | 72 | #ifndef PRODSIGNATURES_DACA0OFFCAL 73 | #define PRODSIGNATURES_DACA0OFFCAL _SFR_MEM8(0x0030) 74 | #endif // PRODSIGNATURES_DACA0OFFCAL 75 | 76 | #ifndef PRODSIGNATURES_DACA0GAINCAL 77 | #define PRODSIGNATURES_DACA0GAINCAL _SFR_MEM8(0x0031) 78 | #endif // PRODSIGNATURES_DACA0GAINCAL 79 | 80 | #ifndef PRODSIGNATURES_DACB0OFFCAL 81 | #define PRODSIGNATURES_DACB0OFFCAL _SFR_MEM8(0x0032) 82 | #endif // PRODSIGNATURES_DACA0OFFCAL 83 | 84 | #ifndef PRODSIGNATURES_DACB0GAINCAL 85 | #define PRODSIGNATURES_DACB0GAINCAL _SFR_MEM8(0x0033) 86 | #endif // PRODSIGNATURES_DACB0GAINCAL 87 | 88 | #ifndef PRODSIGNATURES_DACA1OFFCAL 89 | #define PRODSIGNATURES_DACA1OFFCAL _SFR_MEM8(0x0034) 90 | #endif // PRODSIGNATURES_DACA0OFFCAL 91 | 92 | #ifndef PRODSIGNATURES_DACA1GAINCAL 93 | #define PRODSIGNATURES_DACA1GAINCAL _SFR_MEM8(0x0035) 94 | #endif // PRODSIGNATURES_DACA1GAINCAL 95 | 96 | #ifndef PRODSIGNATURES_DACB1OFFCAL 97 | #define PRODSIGNATURES_DACB1OFFCAL _SFR_MEM8(0x0036) 98 | #endif // PRODSIGNATURES_DACB1OFFCAL 99 | 100 | #ifndef PRODSIGNATURES_DACB1GAINCAL 101 | #define PRODSIGNATURES_DACB1GAINCAL _SFR_MEM8(0x0037) 102 | #endif // PRODSIGNATURES_DACB1GAINCAL 103 | 104 | 105 | 106 | #ifndef ADCA_CH0_SCAN /* A1 headers don't define this properly */ 107 | #define ADCA_CH0_SCAN _SFR_MEM8(0x0226) 108 | #define ADC_REFSEL_INTVCC2_gc (0x04<<4) 109 | #define ADC_CH_GAIN_DIV2_gc (0x07<<2) 110 | #endif // ADCA_CH0_SCAN 111 | 112 | #ifndef ADC_REFSEL2_bm /* A1 headers don't define this properly, but A1U headers do [and others] */ 113 | #define ADC_REFSEL2_bm (1<<6) /* Reference Selection bit 2 mask. */ 114 | #define ADC_REFSEL2_bp 6 /* Reference Selection bit 2 position. */ 115 | #endif // ADC_REFSEL2_bm 116 | 117 | #ifndef PORT_USART0_bm 118 | #define PORT_USART0_bm 0x10 /* Usart0 bit mask for port remap register. */ 119 | #endif // PORT_USART0_bm 120 | 121 | 122 | #ifdef __cplusplus 123 | extern "C"{ 124 | #endif 125 | 126 | #ifndef cbi 127 | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) 128 | #endif 129 | #ifndef sbi 130 | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) 131 | #endif 132 | 133 | // NOTE: xmega is SO different that these need to be in pins_arduino.h 134 | // some xmegas have 2 per port, others 1 per port, and the # of ports vary greatly 135 | //#define EXTERNAL_INT_0 0 136 | //#define EXTERNAL_INT_1 1 137 | //#define EXTERNAL_INT_2 2 138 | //#define EXTERNAL_INT_3 3 139 | //#define EXTERNAL_INT_4 4 140 | //#define EXTERNAL_INT_5 5 141 | //#define EXTERNAL_INT_6 6 142 | //#define EXTERNAL_INT_7 7 143 | 144 | // CPU-specific definitions go into the 'pins_arduino.h' files anyway 145 | // 146 | //#if defined(SOME_CPU) 147 | //#define EXTERNAL_NUM_INTERRUPTS 8 148 | //#endif 149 | 150 | typedef void (*voidFuncPtr)(void); 151 | 152 | #ifdef __cplusplus 153 | } // extern "C" 154 | #endif 155 | 156 | #endif 157 | 158 | 159 | -------------------------------------------------------------------------------- /cores/xmega/wiring_pulse.c: -------------------------------------------------------------------------------- 1 | /* 2 | wiring_pulse.c - pulseIn() function 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2005-2006 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | 22 | $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ 23 | */ 24 | 25 | #include "wiring_private.h" 26 | #include "pins_arduino.h" 27 | 28 | /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH 29 | * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds 30 | * to 3 minutes in length, but must be called at least a few dozen microseconds 31 | * before the start of the pulse. */ 32 | 33 | // NOTE: Arduino code now has a wiring_pulse.S that implements this function 34 | // as assembly code. For now, the C code will stay in the xmega version 35 | // but at some point the assembly version should be implemented. 36 | 37 | unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) 38 | { 39 | // cache the port and bit of the pin in order to speed up the 40 | // pulse width measuring loop and achieve finer resolution. calling 41 | // digitalRead() instead yields much coarser resolution. 42 | uint8_t bit = digitalPinToBitMask(pin); 43 | uint8_t port = digitalPinToPort(pin); 44 | uint8_t stateMask = (state ? bit : 0); 45 | unsigned long width = 0; // keep initialization out of time critical area 46 | 47 | // convert the timeout from microseconds to a number of times through 48 | // the initial loop; it takes 16 clock cycles per iteration. 49 | unsigned long numloops = 0; 50 | unsigned long maxloops = microsecondsToClockCycles(timeout) / 16; 51 | 52 | // wait for any previous pulse to end 53 | while((*portInputRegister(port) & bit) == stateMask) 54 | { 55 | if(numloops++ == maxloops) 56 | { 57 | return 0; 58 | } 59 | } 60 | 61 | // wait for the pulse to start 62 | while((*portInputRegister(port) & bit) != stateMask) 63 | { 64 | if(numloops++ == maxloops) 65 | { 66 | return 0; 67 | } 68 | } 69 | 70 | // wait for the pulse to stop 71 | while((*portInputRegister(port) & bit) == stateMask) 72 | { 73 | if(numloops++ == maxloops) 74 | { 75 | return 0; 76 | } 77 | 78 | width++; 79 | } 80 | 81 | // convert the reading to microseconds. The loop has been determined 82 | // to be 20 clock cycles long and have about 16 clocks between the edge 83 | // and the start of the loop. There will be some error introduced by 84 | // the interrupt handlers. 85 | return clockCyclesToMicroseconds(width * 21 + 16); 86 | } 87 | 88 | // Added 11/10/2018 to bring it up to latest Arduino code 89 | 90 | /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH 91 | * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds 92 | * to 3 minutes in length, but must be called at least a few dozen microseconds 93 | * before the start of the pulse. 94 | * 95 | * ATTENTION: 96 | * this function relies on micros() so cannot be used in noInterrupt() context 97 | */ 98 | unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) 99 | { 100 | // cache the port and bit of the pin in order to speed up the 101 | // pulse width measuring loop and achieve finer resolution. calling 102 | // digitalRead() instead yields much coarser resolution. 103 | uint8_t bit = digitalPinToBitMask(pin); 104 | uint8_t port = digitalPinToPort(pin); 105 | uint8_t stateMask = (state ? bit : 0); 106 | 107 | unsigned long startMicros = micros(); 108 | 109 | // wait for any previous pulse to end 110 | while((*portInputRegister(port) & bit) == stateMask) 111 | { 112 | if(micros() - startMicros > timeout) 113 | { 114 | return 0; 115 | } 116 | } 117 | 118 | // wait for the pulse to start 119 | while((*portInputRegister(port) & bit) != stateMask) 120 | { 121 | if(micros() - startMicros > timeout) 122 | { 123 | return 0; 124 | } 125 | } 126 | 127 | unsigned long start = micros(); 128 | // wait for the pulse to stop 129 | while((*portInputRegister(port) & bit) == stateMask) 130 | { 131 | if(micros() - startMicros > timeout) 132 | { 133 | return 0; 134 | } 135 | } 136 | 137 | return micros() - start; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /cores/xmega/wiring_shift.c: -------------------------------------------------------------------------------- 1 | /* 2 | wiring_shift.c - shiftOut() function 3 | Part of Arduino - http://www.arduino.cc/ 4 | 5 | Copyright (c) 2005-2006 David A. Mellis 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General 18 | Public License along with this library; if not, write to the 19 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, 20 | Boston, MA 02111-1307 USA 21 | 22 | $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $ 23 | */ 24 | 25 | #include "wiring_private.h" 26 | 27 | uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { 28 | uint8_t value = 0; 29 | uint8_t i; 30 | 31 | for (i = 0; i < 8; ++i) { 32 | digitalWrite(clockPin, HIGH); 33 | if (bitOrder == LSBFIRST) 34 | value |= digitalRead(dataPin) << i; 35 | else 36 | value |= digitalRead(dataPin) << (7 - i); 37 | digitalWrite(clockPin, LOW); 38 | } 39 | return value; 40 | } 41 | 42 | void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) 43 | { 44 | uint8_t i; 45 | 46 | for (i = 0; i < 8; i++) { 47 | if (bitOrder == LSBFIRST) 48 | digitalWrite(dataPin, !!(val & (1 << i))); 49 | else 50 | digitalWrite(dataPin, !!(val & (1 << (7 - i)))); 51 | 52 | digitalWrite(clockPin, HIGH); 53 | digitalWrite(clockPin, LOW); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /patch-avrdude.6.1-avrdude.conf: -------------------------------------------------------------------------------- 1 | --- avrdude.conf.orig 2014-10-29 13:39:56.000000000 -0700 2 | +++ avrdude.conf 2014-10-29 14:03:19.000000000 -0700 3 | @@ -13216,13 +13216,22 @@ 4 | desc = "AVR XMEGA family common values"; 5 | has_pdi = yes; 6 | nvm_base = 0x01c0; 7 | mcu_base = 0x0090; 8 | 9 | +# needed for wiring aka stk500v2 protocol 10 | + reset = dedicated; 11 | + pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", 12 | + "x x x x x x x x x x x x x x x x"; 13 | + 14 | + 15 | memory "signature" 16 | size = 3; 17 | offset = 0x1000090; 18 | +# needed for wiring aka stk500v2 protocol 19 | + read = "0 0 1 1 0 0 0 0 x x x x x x x x", 20 | + "x x x x x x a1 a0 o o o o o o o o"; 21 | ; 22 | 23 | memory "prodsig" 24 | size = 0x32; 25 | offset = 0x8e0200; 26 | @@ -13644,10 +13653,46 @@ 27 | memory "flash" 28 | size = 0x22000; 29 | offset = 0x800000; 30 | page_size = 0x200; 31 | readsize = 0x100; 32 | +# needed for wiring aka stk500v2 protocol 33 | + paged = yes; # this appears to affect how it is read/written with stk500v2 34 | + num_pages = 272; 35 | + blocksize = 0x200; 36 | + mode = 0x41; 37 | + delay = 10; 38 | + 39 | + read_lo = " 0 0 1 0 0 0 0 0", 40 | + "a15 a14 a13 a12 a11 a10 a9 a8", 41 | + " a7 a6 a5 a4 a3 a2 a1 a0", 42 | + " o o o o o o o o"; 43 | + 44 | + read_hi = " 0 0 1 0 1 0 0 0", 45 | + "a15 a14 a13 a12 a11 a10 a9 a8", 46 | + " a7 a6 a5 a4 a3 a2 a1 a0", 47 | + " o o o o o o o o"; 48 | + 49 | + loadpage_lo = " 0 1 0 0 0 0 0 0", 50 | + " x x x x x x x x", 51 | + " x a6 a5 a4 a3 a2 a1 a0", 52 | + " i i i i i i i i"; 53 | + 54 | + loadpage_hi = " 0 1 0 0 1 0 0 0", 55 | + " x x x x x x x x", 56 | + " x a6 a5 a4 a3 a2 a1 a0", 57 | + " i i i i i i i i"; 58 | + 59 | + writepage = " 0 1 0 0 1 1 0 0", 60 | + "a15 a14 a13 a12 a11 a10 a9 a8", 61 | + " a7 x x x x x x x", 62 | + " x x x x x x x x"; 63 | + 64 | + load_ext_addr = " 0 1 0 0 1 1 0 1", 65 | + " 0 0 0 0 0 0 0 0", 66 | + " 0 0 0 0 0 0 0 a16", 67 | + " 0 0 0 0 0 0 0 0"; 68 | ; 69 | 70 | memory "usersig" 71 | size = 0x200; 72 | offset = 0x8e0400; 73 | @@ -13775,10 +13820,46 @@ 74 | memory "flash" 75 | size = 0x22000; 76 | offset = 0x800000; 77 | page_size = 0x200; 78 | readsize = 0x100; 79 | +# needed for wiring aka stk500v2 protocol 80 | + paged = yes; # this appears to affect how it is read/written with stk500v2 81 | + num_pages = 272; 82 | + blocksize = 0x200; 83 | + mode = 0x41; 84 | + delay = 10; 85 | + 86 | + read_lo = " 0 0 1 0 0 0 0 0", 87 | + "a15 a14 a13 a12 a11 a10 a9 a8", 88 | + " a7 a6 a5 a4 a3 a2 a1 a0", 89 | + " o o o o o o o o"; 90 | + 91 | + read_hi = " 0 0 1 0 1 0 0 0", 92 | + "a15 a14 a13 a12 a11 a10 a9 a8", 93 | + " a7 a6 a5 a4 a3 a2 a1 a0", 94 | + " o o o o o o o o"; 95 | + 96 | + loadpage_lo = " 0 1 0 0 0 0 0 0", 97 | + " x x x x x x x x", 98 | + " x a6 a5 a4 a3 a2 a1 a0", 99 | + " i i i i i i i i"; 100 | + 101 | + loadpage_hi = " 0 1 0 0 1 0 0 0", 102 | + " x x x x x x x x", 103 | + " x a6 a5 a4 a3 a2 a1 a0", 104 | + " i i i i i i i i"; 105 | + 106 | + writepage = " 0 1 0 0 1 1 0 0", 107 | + "a15 a14 a13 a12 a11 a10 a9 a8", 108 | + " a7 x x x x x x x", 109 | + " x x x x x x x x"; 110 | + 111 | + load_ext_addr = " 0 1 0 0 1 1 0 1", 112 | + " 0 0 0 0 0 0 0 0", 113 | + " 0 0 0 0 0 0 0 a16", 114 | + " 0 0 0 0 0 0 0 0"; 115 | ; 116 | 117 | memory "usersig" 118 | size = 0x200; 119 | offset = 0x8e0400; 120 | @@ -13956,10 +14037,46 @@ 121 | memory "flash" 122 | size = 0x32000; 123 | offset = 0x800000; 124 | page_size = 0x200; 125 | readsize = 0x100; 126 | +# needed for wiring aka stk500v2 protocol 127 | + paged = yes; # this appears to affect how it is read/written with stk500v2 128 | + num_pages = 400; 129 | + blocksize = 0x200; 130 | + mode = 0x41; 131 | + delay = 10; 132 | + 133 | + read_lo = " 0 0 1 0 0 0 0 0", 134 | + "a15 a14 a13 a12 a11 a10 a9 a8", 135 | + " a7 a6 a5 a4 a3 a2 a1 a0", 136 | + " o o o o o o o o"; 137 | + 138 | + read_hi = " 0 0 1 0 1 0 0 0", 139 | + "a15 a14 a13 a12 a11 a10 a9 a8", 140 | + " a7 a6 a5 a4 a3 a2 a1 a0", 141 | + " o o o o o o o o"; 142 | + 143 | + loadpage_lo = " 0 1 0 0 0 0 0 0", 144 | + " x x x x x x x x", 145 | + " x a6 a5 a4 a3 a2 a1 a0", 146 | + " i i i i i i i i"; 147 | + 148 | + loadpage_hi = " 0 1 0 0 1 0 0 0", 149 | + " x x x x x x x x", 150 | + " x a6 a5 a4 a3 a2 a1 a0", 151 | + " i i i i i i i i"; 152 | + 153 | + writepage = " 0 1 0 0 1 1 0 0", 154 | + "a15 a14 a13 a12 a11 a10 a9 a8", 155 | + " a7 x x x x x x x", 156 | + " x x x x x x x x"; 157 | + 158 | + load_ext_addr = " 0 1 0 0 1 1 0 1", 159 | + " 0 0 0 0 0 0 0 0", 160 | + " 0 0 0 0 0 0 0 a16", 161 | + " 0 0 0 0 0 0 0 0"; 162 | ; 163 | 164 | memory "usersig" 165 | size = 0x200; 166 | offset = 0x8e0400; 167 | @@ -14056,10 +14173,46 @@ 168 | memory "flash" 169 | size = 0x42000; 170 | offset = 0x800000; 171 | page_size = 0x200; 172 | readsize = 0x100; 173 | +# needed for wiring aka stk500v2 protocol 174 | + paged = yes; # this appears to affect how it is read/written with stk500v2 175 | + num_pages = 528; 176 | + blocksize = 0x200; 177 | + mode = 0x41; 178 | + delay = 10; 179 | + 180 | + read_lo = " 0 0 1 0 0 0 0 0", 181 | + "a15 a14 a13 a12 a11 a10 a9 a8", 182 | + " a7 a6 a5 a4 a3 a2 a1 a0", 183 | + " o o o o o o o o"; 184 | + 185 | + read_hi = " 0 0 1 0 1 0 0 0", 186 | + "a15 a14 a13 a12 a11 a10 a9 a8", 187 | + " a7 a6 a5 a4 a3 a2 a1 a0", 188 | + " o o o o o o o o"; 189 | + 190 | + loadpage_lo = " 0 1 0 0 0 0 0 0", 191 | + " x x x x x x x x", 192 | + " x a6 a5 a4 a3 a2 a1 a0", 193 | + " i i i i i i i i"; 194 | + 195 | + loadpage_hi = " 0 1 0 0 1 0 0 0", 196 | + " x x x x x x x x", 197 | + " x a6 a5 a4 a3 a2 a1 a0", 198 | + " i i i i i i i i"; 199 | + 200 | + writepage = " 0 1 0 0 1 1 0 0", 201 | + "a15 a14 a13 a12 a11 a10 a9 a8", 202 | + " a7 x x x x x x x", 203 | + " x x x x x x x x"; 204 | + 205 | + load_ext_addr = " 0 1 0 0 1 1 0 1", 206 | + " 0 0 0 0 0 0 0 0", 207 | + " 0 0 0 0 0 0 0 a16", 208 | + " 0 0 0 0 0 0 0 0"; 209 | ; 210 | 211 | memory "usersig" 212 | size = 0x200; 213 | offset = 0x8e0400; 214 | @@ -14177,10 +14330,46 @@ 215 | memory "flash" 216 | size = 0x62000; 217 | offset = 0x800000; 218 | page_size = 0x200; 219 | readsize = 0x100; 220 | +# needed for wiring aka stk500v2 protocol 221 | + paged = yes; # this appears to affect how it is read/written with stk500v2 222 | + num_pages = 784; 223 | + blocksize = 0x200; 224 | + mode = 0x41; 225 | + delay = 10; 226 | + 227 | + read_lo = " 0 0 1 0 0 0 0 0", 228 | + "a15 a14 a13 a12 a11 a10 a9 a8", 229 | + " a7 a6 a5 a4 a3 a2 a1 a0", 230 | + " o o o o o o o o"; 231 | + 232 | + read_hi = " 0 0 1 0 1 0 0 0", 233 | + "a15 a14 a13 a12 a11 a10 a9 a8", 234 | + " a7 a6 a5 a4 a3 a2 a1 a0", 235 | + " o o o o o o o o"; 236 | + 237 | + loadpage_lo = " 0 1 0 0 0 0 0 0", 238 | + " x x x x x x x x", 239 | + " x a6 a5 a4 a3 a2 a1 a0", 240 | + " i i i i i i i i"; 241 | + 242 | + loadpage_hi = " 0 1 0 0 1 0 0 0", 243 | + " x x x x x x x x", 244 | + " x a6 a5 a4 a3 a2 a1 a0", 245 | + " i i i i i i i i"; 246 | + 247 | + writepage = " 0 1 0 0 1 1 0 0", 248 | + "a15 a14 a13 a12 a11 a10 a9 a8", 249 | + " a7 x x x x x x x", 250 | + " x x x x x x x x"; 251 | + 252 | + load_ext_addr = " 0 1 0 0 1 1 0 1", 253 | + " 0 0 0 0 0 0 0 0", 254 | + " 0 0 0 0 0 0 0 a16", 255 | + " 0 0 0 0 0 0 0 0"; 256 | ; 257 | 258 | memory "usersig" 259 | size = 0x200; 260 | offset = 0x8e0400; 261 | @@ -14247,10 +14436,16 @@ 262 | size = 0x80; 263 | offset = 0x8e0400; 264 | page_size = 0x80; 265 | readsize = 0x100; 266 | ; 267 | + 268 | + memory "fuse6" 269 | + size = 1; 270 | + offset = 0x8f0026; 271 | + ; 272 | + 273 | ; 274 | 275 | #------------------------------------------------------------ 276 | # ATxmega16E5 277 | #------------------------------------------------------------ 278 | @@ -14299,10 +14494,16 @@ 279 | size = 0x80; 280 | offset = 0x8e0400; 281 | page_size = 0x80; 282 | readsize = 0x100; 283 | ; 284 | + 285 | + memory "fuse6" 286 | + size = 1; 287 | + offset = 0x8f0026; 288 | + ; 289 | + 290 | ; 291 | 292 | #------------------------------------------------------------ 293 | # ATxmega32E5 294 | #------------------------------------------------------------ 295 | @@ -14351,10 +14552,16 @@ 296 | size = 0x80; 297 | offset = 0x8e0400; 298 | page_size = 0x80; 299 | readsize = 0x100; 300 | ; 301 | + 302 | + memory "fuse6" 303 | + size = 1; 304 | + offset = 0x8f0026; 305 | + ; 306 | + 307 | ; 308 | 309 | #------------------------------------------------------------ 310 | # AVR32UC3A0512 311 | #------------------------------------------------------------ 312 | -------------------------------------------------------------------------------- /patch.boards.txt: -------------------------------------------------------------------------------- 1 | --- hardware/arduino/boards.txt 2013-05-17 12:48:38.000000000 -0700 2 | +++ hardware/arduino/boards.txt 2014-10-25 01:23:03.000000000 -0700 3 | @@ -563,5 +563,96 @@ 4 | robotMotor.build.vid=0x2341 5 | robotMotor.build.pid=0x8039 6 | robotMotor.build.core=robot 7 | robotMotor.build.variant=robot_motor 8 | 9 | +############################################################## 10 | + 11 | +xmega64d4.name=ATXMega64D4 12 | + 13 | +xmega64d4.upload.protocol=arduino 14 | +xmega64d4.upload.maximum_size=65536 15 | +xmega64d4.upload.speed=115200 16 | +#xmega64d4.bootloader.low_fuses=0xff 17 | +#xmega64d4.bootloader.high_fuses=0xde 18 | +#xmega64d4.bootloader.extended_fuses=0x05 19 | +#xmega64d4.bootloader.path=xmega64d4 20 | +#xmega64d4.bootloader.file=xmegaBOOT_xmega0.hex 21 | +#xmega64d4.bootloader.unlock_bits=0x3F 22 | +#xmega64d4.bootloader.lock_bits=0x0F 23 | +xmega64d4.build.mcu=atxmega64d4 24 | +xmega64d4.build.f_cpu=32000000L 25 | +xmega64d4.build.core=xmega 26 | +xmega64d4.build.variant=xmega64d4 27 | + 28 | +############################################################## 29 | + 30 | +xmega32e5.name=ATXMega32E5 31 | + 32 | +xmega32e5.upload.protocol=arduino 33 | +xmega32e5.upload.maximum_size=32768 34 | +xmega32e5.upload.speed=115200 35 | +#xmega32e5.bootloader.low_fuses=0xff 36 | +#xmega32e5.bootloader.high_fuses=0xde 37 | +#xmega32e5.bootloader.extended_fuses=0x05 38 | +#xmega32e5.bootloader.path=xmega32e5 39 | +#xmega32e5.bootloader.file=xmegaBOOT_xmega0.hex 40 | +#xmega32e5.bootloader.unlock_bits=0x3F 41 | +#xmega32e5.bootloader.lock_bits=0x0F 42 | +xmega32e5.build.mcu=atxmega32e5 43 | +xmega32e5.build.f_cpu=32000000L 44 | +xmega32e5.build.core=xmega 45 | +xmega32e5.build.variant=xmega32e5 46 | + 47 | + 48 | +############################################################## 49 | + 50 | +cc0x32e5.name=CC0 ATXMega32E5 breakout 51 | + 52 | +cc0x32e5.upload.protocol=arduino 53 | +cc0x32e5.upload.maximum_size=32768 54 | +cc0x32e5.upload.speed=115200 55 | +cc0x32e5.build.mcu=atxmega32e5 56 | +cc0x32e5.build.f_cpu=32000000L 57 | +cc0x32e5.build.core=xmega 58 | +cc0x32e5.build.variant=cc0_atxmega_e5 59 | + 60 | + 61 | +############################################################## 62 | + 63 | +xmega128a1.name=ATXMega128A1 64 | + 65 | +xmega128a1.upload.protocol=wiring 66 | +xmega128a1.upload.maximum_size=131072 67 | +xmega128a1.upload.speed=115200 68 | +xmega128a1.build.mcu=atxmega128a1 69 | +xmega128a1.build.f_cpu=32000000L 70 | +xmega128a1.build.core=xmega 71 | +xmega128a1.build.variant=xmega128a1 72 | + 73 | + 74 | +############################################################## 75 | + 76 | +xmega128a1u.name=ATXmega128A1U 77 | + 78 | +xmega128a1u.upload.protocol=wiring 79 | +xmega128a1u.upload.maximum_size=131072 80 | +xmega128a1u.upload.speed=115200 81 | +xmega128a1u.build.mcu=atxmega128a1u 82 | +xmega128a1u.build.f_cpu=32000000L 83 | +xmega128a1u.build.core=xmega 84 | +xmega128a1u.build.variant=xmega128a1u 85 | +# see https://raw.githubusercontent.com/arduino/ArduinoISP/master/usbdrv/USB-IDs-for-free.txt 86 | +xmega128a1u.build.vid=0x16c0 87 | +xmega128a1u.build.pid=0x05e1 88 | + 89 | + 90 | +############################################################## 91 | + 92 | +xmega128a4u.name=ATXmega128A4U 93 | + 94 | +xmega128a4u.upload.protocol=wiring 95 | +xmega128a4u.upload.maximum_size=131072 96 | +xmega128a4u.upload.speed=115200 97 | +xmega128a4u.build.mcu=atxmega128a4u 98 | +xmega128a4u.build.f_cpu=32000000L 99 | +xmega128a4u.build.core=xmega 100 | +xmega128a4u.build.variant=xmega128a4u 101 | +# see https://raw.githubusercontent.com/arduino/ArduinoISP/master/usbdrv/USB-IDs-for-free.txt 102 | +xmega128a4u.build.vid=0x16c0 103 | +xmega128a4u.build.pid=0x05e1 104 | + 105 | + 106 | +############################################################## 107 | + 108 | +RockSolid128A.name=Rock Solid XMega 128A 109 | + 110 | +RockSolid128A.upload.protocol=wiring 111 | +RockSolid128A.upload.maximum_size=131072 112 | +RockSolid128A.upload.speed=115200 113 | +RockSolid128A.build.mcu=atxmega128a1u 114 | +RockSolid128A.build.f_cpu=32000000L 115 | +RockSolid128A.build.core=xmega 116 | +RockSolid128A.build.variant=RockSolid128A 117 | + 118 | + 119 | --------------------------------------------------------------------------------