commit 4a77bc0c436a34f28685c6be79bc25e1a37e193c Author: Katya Date: Thu Feb 29 16:55:28 2024 +0300 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..42a1f04 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Arduino AVR Boards + +[![Check Arduino status](https://github.com/arduino/ArduinoCore-avr/actions/workflows/check-arduino.yml/badge.svg)](https://github.com/arduino/ArduinoCore-avr/actions/workflows/check-arduino.yml) +[![Compile Examples status](https://github.com/arduino/ArduinoCore-avr/actions/workflows/compile-platform-examples.yml/badge.svg)](https://github.com/arduino/ArduinoCore-avr/actions/workflows/compile-platform-examples.yml) +[![Spell Check status](https://github.com/arduino/ArduinoCore-avr/actions/workflows/spell-check.yml/badge.svg)](https://github.com/arduino/ArduinoCore-avr/actions/workflows/spell-check.yml) + +This repository contains the source code and configuration files of the Arduino AVR Boards +[platform](https://arduino.github.io/arduino-cli/latest/platform-specification/). diff --git a/boards.txt b/boards.txt new file mode 100644 index 0000000..f94e978 --- /dev/null +++ b/boards.txt @@ -0,0 +1,81 @@ +# See: https://arduino.github.io/arduino-cli/latest/platform-specification/ + +menu.cpu=Processor + +############################################################## + +mega.name=idiBus Master + +mega.vid.0=0x2341 +mega.pid.0=0x0010 +mega.vid.1=0x2341 +mega.pid.1=0x0042 +mega.vid.2=0x2A03 +mega.pid.2=0x0010 +mega.vid.3=0x2A03 +mega.pid.3=0x0042 +mega.vid.4=0x2341 +mega.pid.4=0x0210 +mega.vid.5=0x2341 +mega.pid.5=0x0242 +mega.upload_port.0.vid=0x2341 +mega.upload_port.0.pid=0x0010 +mega.upload_port.1.vid=0x2341 +mega.upload_port.1.pid=0x0042 +mega.upload_port.2.vid=0x2A03 +mega.upload_port.2.pid=0x0010 +mega.upload_port.3.vid=0x2A03 +mega.upload_port.3.pid=0x0042 +mega.upload_port.4.vid=0x2341 +mega.upload_port.4.pid=0x0210 +mega.upload_port.5.vid=0x2341 +mega.upload_port.5.pid=0x0242 +mega.upload_port.6.board=mega + +mega.upload.tool=arduino:avrdude +mega.upload.tool.default=avrdude +mega.upload.maximum_data_size=8192 + +mega.bootloader.tool=arduino:avrdude +mega.bootloader.tool.default=avrdude +mega.bootloader.low_fuses=0xFF +mega.bootloader.unlock_bits=0x3F +mega.bootloader.lock_bits=0xFF + +mega.build.f_cpu=16000000L +mega.build.variant=mega +mega.build.core=arduino:arduino +# default board may be overridden by the cpu menu +mega.build.board=AVR_MEGA2560 + +## Arduino Mega w/ ATmega2560 +## ------------------------- +mega.menu.cpu.atmega2560=ATmega2560 (Mega 2560) + +mega.menu.cpu.atmega2560.upload.protocol=wiring +mega.menu.cpu.atmega2560.upload.maximum_size=253952 +mega.menu.cpu.atmega2560.upload.speed=115200 + +mega.menu.cpu.atmega2560.bootloader.high_fuses=0xD0 +mega.menu.cpu.atmega2560.bootloader.extended_fuses=0xFD +mega.menu.cpu.atmega2560.bootloader.file=stk500v2/stk500boot_v2_mega2560.hex + +mega.menu.cpu.atmega2560.build.mcu=atmega2560 +mega.menu.cpu.atmega2560.build.board=AVR_MEGA2560 + +## Arduino Mega w/ ATmega1280 +## ------------------------- +mega.menu.cpu.atmega1280=ATmega1280 + +mega.menu.cpu.atmega1280.upload.protocol=arduino +mega.menu.cpu.atmega1280.upload.maximum_size=126976 +mega.menu.cpu.atmega1280.upload.speed=57600 + +mega.menu.cpu.atmega1280.bootloader.high_fuses=0xDA +mega.menu.cpu.atmega1280.bootloader.extended_fuses=0xF5 +mega.menu.cpu.atmega1280.bootloader.file=atmega/ATmegaBOOT_168_atmega1280.hex + +mega.menu.cpu.atmega1280.build.mcu=atmega1280 +mega.menu.cpu.atmega1280.build.board=AVR_MEGA + +############################################################## diff --git a/bootloaders/stk500v2/License.txt b/bootloaders/stk500v2/License.txt new file mode 100644 index 0000000..a66eb90 --- /dev/null +++ b/bootloaders/stk500v2/License.txt @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/bootloaders/stk500v2/Makefile b/bootloaders/stk500v2/Makefile new file mode 100644 index 0000000..c4cf20d --- /dev/null +++ b/bootloaders/stk500v2/Makefile @@ -0,0 +1,127 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +SHELL := cmd.exe +RM := rm -rf + +USER_OBJS := + +LIBS := +PROJ := + +O_SRCS := +C_SRCS := +S_SRCS := +S_UPPER_SRCS := +OBJ_SRCS := +ASM_SRCS := +PREPROCESSING_SRCS := +OBJS := +OBJS_AS_ARGS := +C_DEPS := +C_DEPS_AS_ARGS := +EXECUTABLES := +OUTPUT_FILE_PATH := +OUTPUT_FILE_PATH_AS_ARGS := +AVR_APP_PATH :=$$$AVR_APP_PATH$$$ +QUOTE := " +ADDITIONAL_DEPENDENCIES:= +OUTPUT_FILE_DEP:= +LIB_DEP:= +LINKER_SCRIPT_DEP:= + +# Every subdirectory with source files must be described here +SUBDIRS := + + +# Add inputs and outputs from these tool invocations to the build variables +C_SRCS += \ +../stk500boot.c + + +PREPROCESSING_SRCS += + + +ASM_SRCS += + + +OBJS += \ +stk500boot.o + +OBJS_AS_ARGS += \ +stk500boot.o + +C_DEPS += \ +stk500boot.d + +C_DEPS_AS_ARGS += \ +stk500boot.d + +OUTPUT_FILE_PATH +=stk500boot_v2_mega2560.elf + +OUTPUT_FILE_PATH_AS_ARGS +=stk500boot_v2_mega2560.elf + +ADDITIONAL_DEPENDENCIES:= + +OUTPUT_FILE_DEP:= ./makedep.mk + +LIB_DEP+= + +LINKER_SCRIPT_DEP+= + + +# AVR32/GNU C Compiler +./stk500boot.o: .././stk500boot.c + @echo Building file: $< + @echo Invoking: AVR/GNU C Compiler : 5.4.0 + $(QUOTE)C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE) -x c -funsigned-char -funsigned-bitfields -DDEBUG -DF_CPU=16000000UL -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\include" -Os -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -mrelax -g2 -Wall -mmcu=atmega2560 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560" -c -std=gnu99 -MD -MP -MF "$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -o "$@" "$<" + @echo Finished building: $< + + + + + +# AVR32/GNU Preprocessing Assembler + + + +# AVR32/GNU Assembler + + + + +ifneq ($(MAKECMDGOALS),clean) +ifneq ($(strip $(C_DEPS)),) +-include $(C_DEPS) +endif +endif + +# Add inputs and outputs from these tool invocations to the build variables + +# All Target +all: $(OUTPUT_FILE_PATH) $(ADDITIONAL_DEPENDENCIES) + +$(OUTPUT_FILE_PATH): $(OBJS) $(USER_OBJS) $(OUTPUT_FILE_DEP) $(LIB_DEP) $(LINKER_SCRIPT_DEP) + @echo Building target: $@ + @echo Invoking: AVR/GNU Linker : 5.4.0 + $(QUOTE)C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-gcc.exe$(QUOTE) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(OBJS_AS_ARGS) $(USER_OBJS) $(LIBS) -Wl,-Map="stk500boot_v2_mega2560.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -mrelax -Wl,-section-start=.text=3e000 -mmcu=atmega2560 -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560" + @echo Finished building target: $@ + "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O ihex -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "stk500boot_v2_mega2560.elf" "stk500boot_v2_mega2560.hex" + "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex "stk500boot_v2_mega2560.elf" "stk500boot_v2_mega2560.eep" || exit 0 + "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "stk500boot_v2_mega2560.elf" > "stk500boot_v2_mega2560.lss" + "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objcopy.exe" -O srec -R .eeprom -R .fuse -R .lock -R .signature -R .user_signatures "stk500boot_v2_mega2560.elf" "stk500boot_v2_mega2560.srec" + "C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-size.exe" "stk500boot_v2_mega2560.elf" + + + + + + + +# Other Targets +clean: + -$(RM) $(OBJS_AS_ARGS) $(EXECUTABLES) + -$(RM) $(C_DEPS_AS_ARGS) + rm -rf "stk500boot_v2_mega2560.elf" "stk500boot_v2_mega2560.a" "stk500boot_v2_mega2560.hex" "stk500boot_v2_mega2560.lss" "stk500boot_v2_mega2560.eep" "stk500boot_v2_mega2560.map" "stk500boot_v2_mega2560.srec" "stk500boot_v2_mega2560.usersignatures" + \ No newline at end of file diff --git a/bootloaders/stk500v2/avr_cpunames.h b/bootloaders/stk500v2/avr_cpunames.h new file mode 100644 index 0000000..ad0ed9c --- /dev/null +++ b/bootloaders/stk500v2/avr_cpunames.h @@ -0,0 +1,189 @@ +//************************************************************************************************** +//* +//* Atmel AVR CPU name strings +//* +//************************************************************************************************** +//* Sep 19, 2010 Started on avr_cpunames.h +//************************************************************************************************** + +//#include "avr_cpunames.h" + +//************************************************************************************************** + + +#if defined (__AVR_AT94K__) + #define _AVR_CPU_NAME_ "AT94k" +#elif defined (__AVR_AT43USB320__) +#elif defined (__AVR_AT43USB355__) +#elif defined (__AVR_AT76C711__) +#elif defined (__AVR_AT86RF401__) +#elif defined (__AVR_AT90PWM1__) +#elif defined (__AVR_AT90PWM2__) +#elif defined (__AVR_AT90PWM2B__) +#elif defined (__AVR_AT90PWM3__) +#elif defined (__AVR_AT90PWM3B__) +#elif defined (__AVR_AT90PWM216__) +#elif defined (__AVR_AT90PWM316__) +#elif defined (__AVR_ATmega32C1__) +#elif defined (__AVR_ATmega32M1__) +#elif defined (__AVR_ATmega32U4__) + #define _AVR_CPU_NAME_ "ATmega32U4" +#elif defined (__AVR_ATmega32U6__) + #define _AVR_CPU_NAME_ "ATmega32U6" +#elif defined (__AVR_ATmega128__) + #define _AVR_CPU_NAME_ "Atmega128" +#elif defined (__AVR_ATmega1280__) + #define _AVR_CPU_NAME_ "ATmega1280" +#elif defined (__AVR_ATmega1281__) + #define _AVR_CPU_NAME_ "ATmega1281" +#elif defined (__AVR_ATmega1284P__) + #define _AVR_CPU_NAME_ "ATmega1284" +#elif defined (__AVR_ATmega128RFA1__) + #define _AVR_CPU_NAME_ "ATmega128RFA1" +#elif defined (__AVR_ATmega2560__) + #define _AVR_CPU_NAME_ "ATmega2560" +#elif defined (__AVR_ATmega2561__) + #define _AVR_CPU_NAME_ "ATmega2561" +#elif defined (__AVR_AT90CAN32__) + #define _AVR_CPU_NAME_ "AT90CAN32" +#elif defined (__AVR_AT90CAN64__) + #define _AVR_CPU_NAME_ "AT90CAN64" +#elif defined (__AVR_AT90CAN128__) + #define _AVR_CPU_NAME_ "AT90CAN128" +#elif defined (__AVR_AT90USB82__) + #define _AVR_CPU_NAME_ "AT90USB82" +#elif defined (__AVR_AT90USB162__) + #define _AVR_CPU_NAME_ "AT90USB162" +#elif defined (__AVR_AT90USB646__) + #define _AVR_CPU_NAME_ "AT90USB646" +#elif defined (__AVR_AT90USB647__) + #define _AVR_CPU_NAME_ "AT90USB647" +#elif defined (__AVR_AT90USB1286__) + #define _AVR_CPU_NAME_ "AT90USB1286" +#elif defined (__AVR_AT90USB1287__) + #define _AVR_CPU_NAME_ "AT90USB1287" +#elif defined (__AVR_ATmega64__) + #define _AVR_CPU_NAME_ "ATmega64" +#elif defined (__AVR_ATmega640__) + #define _AVR_CPU_NAME_ "ATmega640" +#elif defined (__AVR_ATmega644__) + #define _AVR_CPU_NAME_ "ATmega644" +#elif defined (__AVR_ATmega644P__) + #define _AVR_CPU_NAME_ "ATmega644P" +#elif defined (__AVR_ATmega645__) + #define _AVR_CPU_NAME_ "ATmega645" +#elif defined (__AVR_ATmega6450__) + #define _AVR_CPU_NAME_ "ATmega6450" +#elif defined (__AVR_ATmega649__) + #define _AVR_CPU_NAME_ "ATmega649" +#elif defined (__AVR_ATmega6490__) + #define _AVR_CPU_NAME_ "ATmega6490" +#elif defined (__AVR_ATmega103__) + #define _AVR_CPU_NAME_ "ATmega103" +#elif defined (__AVR_ATmega32__) + #define _AVR_CPU_NAME_ "Atmega32" +#elif defined (__AVR_ATmega323__) + #define _AVR_CPU_NAME_ "ATmega323" +#elif defined (__AVR_ATmega324P__) + #define _AVR_CPU_NAME_ "ATmega324P" +#elif defined (__AVR_ATmega325__) + #define _AVR_CPU_NAME_ "ATmega325" +#elif defined (__AVR_ATmega325P__) + #define _AVR_CPU_NAME_ "ATmega325P" +#elif defined (__AVR_ATmega3250__) + #define _AVR_CPU_NAME_ "ATmega3250" +#elif defined (__AVR_ATmega3250P__) + #define _AVR_CPU_NAME_ "ATmega3250P" +#elif defined (__AVR_ATmega328P__) + #define _AVR_CPU_NAME_ "ATmega328P" +#elif defined (__AVR_ATmega329__) + #define _AVR_CPU_NAME_ "ATmega329" +#elif defined (__AVR_ATmega329P__) + #define _AVR_CPU_NAME_ "ATmega329P" +#elif defined (__AVR_ATmega3290__) + #define _AVR_CPU_NAME_ "ATmega3290" +#elif defined (__AVR_ATmega3290P__) + #define _AVR_CPU_NAME_ "ATmega3290P" +#elif defined (__AVR_ATmega32HVB__) + #define _AVR_CPU_NAME_ "ATmega32HVB" +#elif defined (__AVR_ATmega406__) + #define _AVR_CPU_NAME_ "ATmega406" +#elif defined (__AVR_ATmega16__) + #define _AVR_CPU_NAME_ "Atmega16" +#elif defined (__AVR_ATmega161__) + #define _AVR_CPU_NAME_ "ATmega161" +#elif defined (__AVR_ATmega162__) + #define _AVR_CPU_NAME_ "ATmega162" +#elif defined (__AVR_ATmega163__) + #define _AVR_CPU_NAME_ "ATmega163" +#elif defined (__AVR_ATmega164P__) + #define _AVR_CPU_NAME_ "ATmega164P" +#elif defined (__AVR_ATmega165__) + #define _AVR_CPU_NAME_ "ATmega165" +#elif defined (__AVR_ATmega165P__) + #define _AVR_CPU_NAME_ "ATmega165P" +#elif defined (__AVR_ATmega168__) + #define _AVR_CPU_NAME_ "ATmega168" +#elif defined (__AVR_ATmega168P__) + #define _AVR_CPU_NAME_ "ATmega168P" +#elif defined (__AVR_ATmega169__) + #define _AVR_CPU_NAME_ "Atmega169" +#elif defined (__AVR_ATmega169P__) + #define _AVR_CPU_NAME_ "ATmega169P" +#elif defined (__AVR_ATmega8HVA__) + #define _AVR_CPU_NAME_ "ATmega8HVA" +#elif defined (__AVR_ATmega16HVA__) + #define _AVR_CPU_NAME_ "ATmega16HVA" +#elif defined (__AVR_ATmega8__) + #define _AVR_CPU_NAME_ "ATmega8" +#elif defined (__AVR_ATmega48__) + #define _AVR_CPU_NAME_ "ATmega48" +#elif defined (__AVR_ATmega48P__) + #define _AVR_CPU_NAME_ "ATmega48P" +#elif defined (__AVR_ATmega88__) + #define _AVR_CPU_NAME_ "ATmega88" +#elif defined (__AVR_ATmega88P__) + #define _AVR_CPU_NAME_ "ATmega88P" +#elif defined (__AVR_ATmega8515__) + #define _AVR_CPU_NAME_ "ATmega8515" +#elif defined (__AVR_ATmega8535__) + #define _AVR_CPU_NAME_ "ATmega8535" +#elif defined (__AVR_AT90S8535__) +#elif defined (__AVR_AT90C8534__) +#elif defined (__AVR_AT90S8515__) +#elif defined (__AVR_AT90S4434__) +#elif defined (__AVR_AT90S4433__) +#elif defined (__AVR_AT90S4414__) +#elif defined (__AVR_ATtiny22__) +#elif defined (__AVR_ATtiny26__) +#elif defined (__AVR_AT90S2343__) +#elif defined (__AVR_AT90S2333__) +#elif defined (__AVR_AT90S2323__) +#elif defined (__AVR_AT90S2313__) +#elif defined (__AVR_ATtiny2313__) + #define _AVR_CPU_NAME_ "ATtiny2313" +#elif defined (__AVR_ATtiny13__) +#elif defined (__AVR_ATtiny13A__) +#elif defined (__AVR_ATtiny25__) +#elif defined (__AVR_ATtiny45__) +#elif defined (__AVR_ATtiny85__) +#elif defined (__AVR_ATtiny24__) +#elif defined (__AVR_ATtiny44__) +#elif defined (__AVR_ATtiny84__) +#elif defined (__AVR_ATtiny261__) +#elif defined (__AVR_ATtiny461__) +#elif defined (__AVR_ATtiny861__) +#elif defined (__AVR_ATtiny43U__) +#elif defined (__AVR_ATtiny48__) +#elif defined (__AVR_ATtiny88__) +#elif defined (__AVR_ATtiny167__) +#elif defined (__AVR_ATmega8U2__) + #define _AVR_CPU_NAME_ "ATmega8U2" +#else + #error cpu not defined +#endif + + +#if !defined (_AVR_CPU_NAME_) +// #define _AVR_CPU_NAME_ "UNKNOWN" +#endif diff --git a/bootloaders/stk500v2/avrinterruptnames.h b/bootloaders/stk500v2/avrinterruptnames.h new file mode 100644 index 0000000..e7e3ed9 --- /dev/null +++ b/bootloaders/stk500v2/avrinterruptnames.h @@ -0,0 +1,1040 @@ +//************************************************************************************************** +//* +//* interrupt vector names +//* +//* It is important to note that the vector numbers listed here +//* are the ATMEL documentation numbers. The Arduino numbers are 1 less +//* This is because the Atmel docs start numbering the interrupts at 1 +//* when it is actually vector #0 in the table. +//************************************************************************************************** +//* Jun 1, 2010 Added support for ATmega1281 +//* Jun 30, 2010 Putting in more ifdefs to conserve space +//* Jul 3, 2010 More #ifdefs to conserve space and testing on most of my boards +//* Jul 4, 2010 Started using vector defs for #ifdefs as defined in +//* Jul 13, 2010 Added support for __AVR_ATmega128__ +//* Aug 26, 2010 Added support for __AVR_ATmega2561__ +//* Sep 13, 2010 Added support for __AVR_AT90CAN32__ __AVR_AT90CAN64__ __AVR_AT90CAN128__ +//************************************************************************************************** + +//#include "avrinterruptnames.h" + +//************************************************************************************************** +//* this defines the interrupt vectors and allows us to compile ONLY those strings that are actually +//* in the target CPU. This way we do not have to keep making changes based on cpu, it will be +//* automatic even if we add a new CPU +#ifndef _AVR_IO_H_ + #include +#endif +//************************************************************************************************** + +#ifdef __MWERKS__ + #define prog_char char + #define PGM_P char * +#endif + + prog_char gAvrInt_RESET[] PROGMEM = "RESET"; +#ifdef INT0_vect + prog_char gAvrInt_INT0[] PROGMEM = "INT0"; +#endif +#ifdef INT1_vect + prog_char gAvrInt_INT1[] PROGMEM = "INT1"; +#endif +#ifdef INT2_vect + prog_char gAvrInt_INT2[] PROGMEM = "INT2"; +#endif +#ifdef INT3_vect + prog_char gAvrInt_INT3[] PROGMEM = "INT3"; +#endif +#ifdef INT4_vect + prog_char gAvrInt_INT4[] PROGMEM = "INT4"; +#endif +#ifdef INT5_vect + prog_char gAvrInt_INT5[] PROGMEM = "INT5"; +#endif +#ifdef INT6_vect + prog_char gAvrInt_INT6[] PROGMEM = "INT6"; +#endif +#ifdef INT7_vect + prog_char gAvrInt_INT7[] PROGMEM = "INT7"; +#endif +#ifdef PCINT0_vect + prog_char gAvrInt_PCINT0[] PROGMEM = "PCINT0"; +#endif +#ifdef PCINT1_vect + prog_char gAvrInt_PCINT1[] PROGMEM = "PCINT1"; +#endif +#ifdef PCINT2_vect + prog_char gAvrInt_PCINT2[] PROGMEM = "PCINT2"; +#endif +#ifdef PCINT3_vect + prog_char gAvrInt_PCINT3[] PROGMEM = "PCINT3"; +#endif +#ifdef WDT_vect + prog_char gAvrInt_WDT[] PROGMEM = "WDT"; +#endif +#ifdef TIMER0_COMP_vect + prog_char gAvrInt_TIMER0_COMP[] PROGMEM = "TIMER0 COMP"; +#endif +#ifdef TIMER0_COMPA_vect + prog_char gAvrInt_TIMER0_COMPA[] PROGMEM = "TIMER0 COMPA"; +#endif +#ifdef TIMER0_COMPB_vect + prog_char gAvrInt_TIMER0_COMPB[] PROGMEM = "TIMER0 COMPB"; +#endif +#ifdef TIMER0_OVF_vect + prog_char gAvrInt_TIMER0_OVF[] PROGMEM = "TIMER0 OVF"; +#endif +#ifdef TIMER1_CAPT_vect + prog_char gAvrInt_TIMER1_CAPT[] PROGMEM = "TIMER1 CAPT"; +#endif +#ifdef TIMER1_COMPA_vect + prog_char gAvrInt_TIMER1_COMPA[] PROGMEM = "TIMER1 COMPA"; +#endif +#ifdef TIMER1_COMPB_vect + prog_char gAvrInt_TIMER1_COMPB[] PROGMEM = "TIMER1 COMPB"; +#endif +#ifdef TIMER1_COMPC_vect + prog_char gAvrInt_TIMER1_COMPC[] PROGMEM = "TIMER1 COMPC"; +#endif +#ifdef TIMER1_OVF_vect + prog_char gAvrInt_TIMER1_OVF[] PROGMEM = "TIMER1 OVF"; +#endif +#ifdef TIMER2_COMP_vect + prog_char gAvrInt_TIMER2_COMP[] PROGMEM = "TIMER2 COMP"; +#endif +#ifdef TIMER2_COMPA_vect + prog_char gAvrInt_TIMER2_COMPA[] PROGMEM = "TIMER2 COMPA"; +#endif +#ifdef TIMER2_COMPB_vect + prog_char gAvrInt_TIMER2_COMPB[] PROGMEM = "TIMER2 COMPB"; +#endif +#ifdef TIMER2_OVF_vect + prog_char gAvrInt_TIMER2_OVF[] PROGMEM = "TIMER2 OVF"; +#endif +#ifdef TIMER3_CAPT_vect + prog_char gAvrInt_TIMER3_CAPT[] PROGMEM = "TIMER3 CAPT"; +#endif +#ifdef TIMER3_COMPA_vect + prog_char gAvrInt_TIMER3_COMPA[] PROGMEM = "TIMER3 COMPA"; +#endif +#ifdef TIMER3_COMPB_vect + prog_char gAvrInt_TIMER3_COMPB[] PROGMEM = "TIMER3 COMPB"; +#endif +#ifdef TIMER3_COMPC_vect + prog_char gAvrInt_TIMER3_COMPC[] PROGMEM = "TIMER3 COMPC"; +#endif +#ifdef TIMER3_OVF_vect + prog_char gAvrInt_TIMER3_OVF[] PROGMEM = "TIMER3 OVF"; +#endif +#ifdef TIMER4_CAPT_vect + prog_char gAvrInt_TIMER4_CAPT[] PROGMEM = "TIMER4 CAPT"; +#endif +#ifdef TIMER4_COMPA_vect + prog_char gAvrInt_TIMER4_COMPA[] PROGMEM = "TIMER4 COMPA"; +#endif +#ifdef TIMER4_COMPB_vect + prog_char gAvrInt_TIMER4_COMPB[] PROGMEM = "TIMER4 COMPB"; +#endif +#ifdef TIMER4_COMPC_vect + prog_char gAvrInt_TIMER4_COMPC[] PROGMEM = "TIMER4 COMPC"; +#endif +#ifdef TIMER4_COMPD_vect + prog_char gAvrInt_TIMER4_COMPD[] PROGMEM = "TIMER4 COMPD"; +#endif +#ifdef TIMER4_OVF_vect + prog_char gAvrInt_TIMER4_OVF[] PROGMEM = "TIMER4 OVF"; +#endif +#ifdef TIMER4_FPF_vect + prog_char gAvrInt_TIMER4_FPF[] PROGMEM = "TIMER4 Fault Protection"; +#endif +#ifdef TIMER5_CAPT_vect + prog_char gAvrInt_TIMER5_CAPT[] PROGMEM = "TIMER5 CAPT"; +#endif +#ifdef TIMER5_COMPA_vect + prog_char gAvrInt_TIMER5_COMPA[] PROGMEM = "TIMER5 COMPA"; +#endif +#ifdef TIMER5_COMPB_vect + prog_char gAvrInt_TIMER5_COMPB[] PROGMEM = "TIMER5 COMPB"; +#endif +#ifdef TIMER5_COMPC_vect + prog_char gAvrInt_TIMER5_COMPC[] PROGMEM = "TIMER5 COMPC"; +#endif +#ifdef TIMER5_OVF_vect + prog_char gAvrInt_TIMER5_OVF[] PROGMEM = "TIMER5 OVF"; +#endif + +//* when there is only 1 usart +#if defined(USART_RX_vect) || defined(USART_RXC_vect) + prog_char gAvrInt_USART_RX[] PROGMEM = "USART RX"; +#endif +#if defined(USART_UDRE_vect) + prog_char gAvrInt_USART_UDRE[] PROGMEM = "USART UDRE"; +#endif +#if defined(USART_TX_vect) || defined(USART_TXC_vect) + prog_char gAvrInt_USART_TX[] PROGMEM = "USART TX"; +#endif + + +//* usart 0 +#if defined(USART0_RX_vect) + prog_char gAvrInt_USART0_RX[] PROGMEM = "USART0 RX"; +#endif +#if defined(USART0_UDRE_vect) + prog_char gAvrInt_USART0_UDRE[] PROGMEM = "USART0 UDRE"; +#endif +#if defined(USART0_TX_vect) + prog_char gAvrInt_USART0_TX[] PROGMEM = "USART0 TX"; +#endif + + +//* usart 1 +#ifdef USART1_RX_vect + prog_char gAvrInt_USART1_RX[] PROGMEM = "USART1 RX"; +#endif +#ifdef USART1_UDRE_vect + prog_char gAvrInt_USART1_UDRE[] PROGMEM = "USART1 UDRE"; +#endif +#ifdef USART1_TX_vect + prog_char gAvrInt_USART1_TX[] PROGMEM = "USART1 TX"; +#endif + +//* usart 2 +#ifdef USART2_RX_vect + prog_char gAvrInt_USART2_RX[] PROGMEM = "USART2 RX"; +#endif +#ifdef USART2_UDRE_vect + prog_char gAvrInt_USART2_UDRE[] PROGMEM = "USART2 UDRE"; +#endif +#ifdef USART2_TX_vect + prog_char gAvrInt_USART2_TX[] PROGMEM = "USART2 TX"; +#endif + +//* usart 3 +#ifdef USART3_RX_vect + prog_char gAvrInt_USART3_RX[] PROGMEM = "USART3 RX"; +#endif +#ifdef USART3_UDRE_vect + prog_char gAvrInt_USART3_UDRE[] PROGMEM = "USART3 UDRE"; +#endif +#ifdef USART3_TX_vect + prog_char gAvrInt_USART3_TX[] PROGMEM = "USART3 TX"; +#endif +#ifdef SPI_STC_vect + prog_char gAvrInt_SPI_STC[] PROGMEM = "SPI STC"; +#endif +#ifdef ADC_vect + prog_char gAvrInt_ADC[] PROGMEM = "ADC"; +#endif +#if defined(ANALOG_COMP_vect) || defined(ANA_COMP_vect) + prog_char gAvrInt_ANALOG_COMP[] PROGMEM = "ANALOG COMP"; +#endif +#if defined(EE_READY_vect) || defined(EE_RDY_vect) + prog_char gAvrInt_EE_READY[] PROGMEM = "EE READY"; +#endif +#ifdef TWI_vect + prog_char gAvrInt_TWI[] PROGMEM = "TWI"; +#endif +#if defined(SPM_READY_vect) || defined(SPM_RDY_vect) + prog_char gAvrInt_SPM_READY[] PROGMEM = "SPM READY"; +#endif +#ifdef USI_START_vect + prog_char gAvrInt_USI_START[] PROGMEM = "USI START"; +#endif +#ifdef USI_OVERFLOW_vect + prog_char gAvrInt_USI_OVERFLOW[] PROGMEM = "USI OVERFLOW"; +#endif +#ifdef USB_GEN_vect + prog_char gAvrInt_USB_General[] PROGMEM = "USB General"; +#endif +#ifdef USB_COM_vect + prog_char gAvrInt_USB_Endpoint[] PROGMEM = "USB Endpoint"; +#endif + +#ifdef LCD_vect + prog_char gAvrInt_LCD_StartFrame[] PROGMEM = "LCD Start of Frame"; +#endif + +//* these are for the chips with CAN bus support +#ifdef CANIT_vect + prog_char gAvrInt_CAN_TrafnsferCE[] PROGMEM = "CAN Transfer Complete or Error"; +#endif +#ifdef OVRIT_vect + prog_char gAvrInt_CAN_TimerOverRun[] PROGMEM = "CAN Timer Overrun"; +#endif + +//* these are for __AVR_ATmega128RFA1__ +#ifdef TRX24_PLL_LOCK_vect + prog_char gAvrInt_TRN_PLL_LOCK[] PROGMEM = "TRX24_PLL_LOCK"; +#endif +#ifdef TRX24_PLL_UNLOCK_vect + prog_char gAvrInt_TRN_PLL_UNLOCK[] PROGMEM = "TRX24_PLL_UNLOCK"; +#endif +#ifdef TRX24_RX_START_vect + prog_char gAvrInt_TRN_RX_START[] PROGMEM = "TRX24_RX_START"; +#endif +#ifdef TRX24_RX_END_vect + prog_char gAvrInt_TRN_RX_END[] PROGMEM = "TRX24_RX_END"; +#endif +#ifdef TRX24_CCA_ED_DONE_vect + prog_char gAvrInt_TRN_CAAED_DONE[] PROGMEM = "TRX24_CCA_ED_DONE"; +#endif +#ifdef TRX24_XAH_AMI_vect + prog_char gAvrInt_TRN_FRAME_MATCH[] PROGMEM = "TRX24_FRAME_ADDRESS_MATCH"; +#endif +#ifdef TRX24_TX_END_vect + prog_char gAvrInt_TRN_TX_END[] PROGMEM = "TRX24_TX_END"; +#endif +#ifdef TRX24_AWAKE_vect + prog_char gAvrInt_TRN_AWAKE[] PROGMEM = "TRX24_AWAKE"; +#endif +#ifdef SCNT_CMP1_vect + prog_char gAvrInt_SCNT_CMP1[] PROGMEM = "SCNT_CMP1"; +#endif +#ifdef SCNT_CMP2_vect + prog_char gAvrInt_SCNT_CMP2[] PROGMEM = "SCNT_CMP2"; +#endif +#ifdef SCNT_CMP3_vect + prog_char gAvrInt_SCNT_CMP3[] PROGMEM = "SCNT_CMP3"; +#endif +#ifdef SCNT_OVFL_vect + prog_char gAvrInt_SCNT_OVFL[] PROGMEM = "SCNT_OVFL"; +#endif +#ifdef SCNT_BACKOFF_vect + prog_char gAvrInt_SCNT_BACKOFF[] PROGMEM = "SCNT_BACKOFF"; +#endif +#ifdef AES_READY_vect + prog_char gAvrInt_AES_READY[] PROGMEM = "AES_READY"; +#endif +#ifdef BAT_LOW_vect + prog_char gAvrInt_BAT_LOW[] PROGMEM = "BAT_LOW"; +#endif + + + +//************************************************************************************************** +//* these do not have vector defs and have to be done by CPU type +#if defined(__AVR_ATmega645__ ) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) + prog_char gAvrInt_NOT_USED[] PROGMEM = "NOT_USED"; +#endif +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega128RFA1__) + prog_char gAvrInt_RESERVED[] PROGMEM = "Reserved"; +#endif + + prog_char gAvrInt_END[] PROGMEM = "*"; + + + + + +//************************************************************************************************** +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__) +#pragma mark __AVR_ATmega168__ / __AVR_ATmega328P__ / __AVR_ATmega328__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_PCINT0, // 4 + gAvrInt_PCINT1, // 5 + gAvrInt_PCINT2, // 6 + gAvrInt_WDT, // 7 + gAvrInt_TIMER2_COMPA, // 8 + gAvrInt_TIMER2_COMPB, // 9 + gAvrInt_TIMER2_OVF, // 10 + gAvrInt_TIMER1_CAPT, // 11 + gAvrInt_TIMER1_COMPA, // 12 + gAvrInt_TIMER1_COMPB, // 13 + gAvrInt_TIMER1_OVF, // 14 + gAvrInt_TIMER0_COMPA, // 15 + gAvrInt_TIMER0_COMPB, // 16 + gAvrInt_TIMER0_OVF, // 17 + gAvrInt_SPI_STC, // 18 + gAvrInt_USART_RX, // 19 + gAvrInt_USART_UDRE, // 20 + gAvrInt_USART_TX, // 21 + gAvrInt_ADC, // 22 + gAvrInt_EE_READY, // 23 + gAvrInt_ANALOG_COMP, // 24 + gAvrInt_TWI, // 25 + gAvrInt_SPM_READY, // 26 +}; + +#endif + +//************************************************************************************************** +#if defined(__AVR_ATmega169__) +#pragma mark __AVR_ATmega169__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_PCINT0, // 3 + gAvrInt_PCINT1, // 4 + gAvrInt_TIMER2_COMP, // 5 + gAvrInt_TIMER2_OVF, // 6 + gAvrInt_TIMER1_CAPT, // 7 + gAvrInt_TIMER1_COMPA, // 8 + gAvrInt_TIMER1_COMPB, // 9 + gAvrInt_TIMER1_OVF, // 10 + gAvrInt_TIMER0_COMP, // 11 + gAvrInt_TIMER0_OVF, // 12 + gAvrInt_SPI_STC, // 13 + gAvrInt_USART0_RX, // 14 + gAvrInt_USART0_UDRE, // 15 + gAvrInt_USART0_TX, // 16 + gAvrInt_USI_START, // 17 + gAvrInt_USI_OVERFLOW, // 18 + gAvrInt_ANALOG_COMP, // 19 + gAvrInt_ADC, // 20 + gAvrInt_EE_READY, // 21 + gAvrInt_SPM_READY, // 22 + gAvrInt_LCD_StartFrame, // 23 + +}; + +#endif + + +//************************************************************************************************** +#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) +#pragma mark __AVR_ATmega640__ __AVR_ATmega1280__ __AVR_ATmega1281__ __AVR_ATmega2560__ __AVR_ATmega2561__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_INT3, // 5 + gAvrInt_INT4, // 6 + gAvrInt_INT5, // 7 + gAvrInt_INT6, // 8 + gAvrInt_INT7, // 9 + gAvrInt_PCINT0, // 10 + gAvrInt_PCINT1, // 11 +#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + gAvrInt_PCINT2, // 12 +#else + gAvrInt_NOT_USED, // 12 +#endif + gAvrInt_WDT, // 13 + gAvrInt_TIMER2_COMPA, // 14 + gAvrInt_TIMER2_COMPB, // 15 + gAvrInt_TIMER2_OVF, // 16 + gAvrInt_TIMER1_CAPT, // 17 + gAvrInt_TIMER1_COMPA, // 18 + gAvrInt_TIMER1_COMPB, // 19 + gAvrInt_TIMER1_COMPC, // 20 + gAvrInt_TIMER1_OVF, // 21 + gAvrInt_TIMER0_COMPA, // 22 + gAvrInt_TIMER0_COMPB, // 23 + gAvrInt_TIMER0_OVF, // 24 + gAvrInt_SPI_STC, // 25 + + gAvrInt_USART0_RX, // 26 + gAvrInt_USART0_UDRE, // 27 + gAvrInt_USART0_TX, // 28 + gAvrInt_ANALOG_COMP, // 29 + gAvrInt_ADC, // 30 + gAvrInt_EE_READY, // 31 + + gAvrInt_TIMER3_CAPT, // 32 + gAvrInt_TIMER3_COMPA, // 33 + gAvrInt_TIMER3_COMPB, // 34 + gAvrInt_TIMER3_COMPC, // 35 + gAvrInt_TIMER3_OVF, // 36 + + gAvrInt_USART1_RX, // 37 + gAvrInt_USART1_UDRE, // 38 + gAvrInt_USART1_TX, // 39 + gAvrInt_TWI, // 40 + gAvrInt_SPM_READY, // 41 +#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + gAvrInt_TIMER4_CAPT, // 42 +#else + gAvrInt_NOT_USED, // 42 +#endif + gAvrInt_TIMER4_COMPA, // 43 + gAvrInt_TIMER4_COMPB, // 44 + gAvrInt_TIMER4_COMPC, // 45 + gAvrInt_TIMER4_OVF, // 46 +#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + gAvrInt_TIMER5_CAPT, // 47 +#else + gAvrInt_NOT_USED, // 47 +#endif + gAvrInt_TIMER5_COMPA, // 48 + gAvrInt_TIMER5_COMPB, // 49 + gAvrInt_TIMER5_COMPC, // 50 + gAvrInt_TIMER5_OVF, // 51 + +#if defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + gAvrInt_USART2_RX, // 52 + gAvrInt_USART2_UDRE, // 53 + gAvrInt_USART2_TX, // 54 + + gAvrInt_USART3_RX, // 55 + gAvrInt_USART3_UDRE, // 56 + gAvrInt_USART3_TX, // 57 +#endif + +}; + +#endif + + + +//************************************************************************************************** +#if defined(__AVR_ATmega324P__ ) || defined(__AVR_ATmega644__ ) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) +#pragma mark __AVR_ATmega324P__ __AVR_ATmega644__ __AVR_ATmega644P__ __AVR_ATmega1284P__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_PCINT0, // 5 + gAvrInt_PCINT1, // 6 + gAvrInt_PCINT2, // 7 + gAvrInt_PCINT3, // 8 + gAvrInt_WDT, // 9 + gAvrInt_TIMER2_COMPA, // 10 + gAvrInt_TIMER2_COMPB, // 11 + gAvrInt_TIMER2_OVF, // 12 + gAvrInt_TIMER1_CAPT, // 13 + gAvrInt_TIMER1_COMPA, // 14 + gAvrInt_TIMER1_COMPB, // 15 + gAvrInt_TIMER1_OVF, // 16 + gAvrInt_TIMER0_COMPA, // 17 + gAvrInt_TIMER0_COMPB, // 18 + gAvrInt_TIMER0_OVF, // 19 + gAvrInt_SPI_STC, // 20 + gAvrInt_USART0_RX, // 21 + gAvrInt_USART0_UDRE, // 22 + gAvrInt_USART0_TX, // 23 + gAvrInt_ANALOG_COMP, // 24 + gAvrInt_ADC, // 25 + gAvrInt_EE_READY, // 26 + gAvrInt_TWI, // 27 + gAvrInt_SPM_READY, // 28 + +#if defined(__AVR_ATmega324P__ ) || defined(__AVR_ATmega644P__) + gAvrInt_USART1_RX, // 29 + gAvrInt_USART1_UDRE, // 30 + gAvrInt_USART1_TX, // 31 +#endif + +}; + + +#endif + +//************************************************************************************************** +#if defined(__AVR_ATmega1284P__ ) +#pragma mark __AVR_ATmega1284P__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_PCINT0, // 5 + gAvrInt_PCINT1, // 6 + gAvrInt_PCINT2, // 7 + gAvrInt_PCINT3, // 8 + gAvrInt_WDT, // 9 + gAvrInt_TIMER2_COMPA, // 10 + gAvrInt_TIMER2_COMPB, // 11 + gAvrInt_TIMER2_OVF, // 12 + gAvrInt_TIMER1_CAPT, // 13 + gAvrInt_TIMER1_COMPA, // 14 + gAvrInt_TIMER1_COMPB, // 15 + gAvrInt_TIMER1_OVF, // 16 + gAvrInt_TIMER0_COMPA, // 17 + gAvrInt_TIMER0_COMPB, // 18 + gAvrInt_TIMER0_OVF, // 19 + gAvrInt_SPI_STC, // 20 + gAvrInt_USART0_RX, // 21 + gAvrInt_USART0_UDRE, // 22 + gAvrInt_USART0_TX, // 23 + gAvrInt_ANALOG_COMP, // 24 + gAvrInt_ADC, // 25 + gAvrInt_EE_READY, // 26 + gAvrInt_TWI, // 27 + gAvrInt_SPM_READY, // 28 + + gAvrInt_USART1_RX, // 29 + gAvrInt_USART1_UDRE, // 30 + gAvrInt_USART1_TX, // 31 + //* these are NOT documented in doc8272.pdf + //* they are in iom1284p.h + gAvrInt_TIMER3_CAPT, // 32 + gAvrInt_TIMER3_COMPA, // 33 + gAvrInt_TIMER3_COMPB, // 34 + gAvrInt_TIMER3_OVF, // 35 + + +}; + + +#endif + + +//************************************************************************************************** +#if defined(__AVR_ATmega645__ ) +#pragma mark __AVR_ATmega645__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_PCINT0, // 3 + gAvrInt_PCINT1, // 4 + gAvrInt_TIMER2_COMP, // 5 + gAvrInt_TIMER2_OVF, // 6 + gAvrInt_TIMER1_CAPT, // 7 + gAvrInt_TIMER1_COMPA, // 8 + gAvrInt_TIMER1_COMPB, // 9 + gAvrInt_TIMER1_OVF, // 10 + gAvrInt_TIMER0_COMP, // 11 + gAvrInt_TIMER0_OVF, // 12 + gAvrInt_SPI_STC, // 13 + gAvrInt_USART0_RX, // 14 + gAvrInt_USART0_UDRE, // 15 + gAvrInt_USART0_TX, // 16 + gAvrInt_USI_START, // 17 + gAvrInt_USI_OVERFLOW, // 18 + gAvrInt_ANALOG_COMP, // 19 + gAvrInt_ADC, // 20 + gAvrInt_EE_READY, // 21 + gAvrInt_SPM_READY, // 22 + gAvrInt_NOT_USED, // 23 + +#if defined(__AVR_ATmega3250__) || defined(__AVR_ATmega6450__) + gAvrInt_PCINT2, // 24 + gAvrInt_PCINT3, // 25 +#endif +}; + + +#endif + +//************************************************************************************************** +#if defined(__AVR_ATmega16__ ) +#pragma mark __AVR_ATmega16__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_TIMER2_COMP, // 4 + gAvrInt_TIMER2_OVF, // 5 + gAvrInt_TIMER1_CAPT, // 6 + gAvrInt_TIMER1_COMPA, // 7 + gAvrInt_TIMER1_COMPB, // 8 + gAvrInt_TIMER1_OVF, // 9 + gAvrInt_TIMER0_OVF, // 10 + gAvrInt_SPI_STC, // 11 + gAvrInt_USART_RX, // 12 + gAvrInt_USART_UDRE, // 13 + gAvrInt_USART_TX, // 14 + gAvrInt_ADC, // 15 + gAvrInt_EE_READY, // 16 + gAvrInt_ANALOG_COMP, // 17 + gAvrInt_TWI, // 18 + gAvrInt_INT2, // 19 + gAvrInt_TIMER0_COMP, // 20 + gAvrInt_SPM_READY, // 21 + +}; + + +#endif + +//************************************************************************************************** +#if defined(__AVR_ATmega32__ ) +#pragma mark __AVR_ATmega32__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_TIMER2_COMP, // 5 + gAvrInt_TIMER2_OVF, // 6 + gAvrInt_TIMER1_CAPT, // 7 + gAvrInt_TIMER1_COMPA, // 8 + gAvrInt_TIMER1_COMPB, // 9 + gAvrInt_TIMER1_OVF, // 10 + gAvrInt_TIMER0_COMP, // 11 + gAvrInt_TIMER0_OVF, // 12 + gAvrInt_SPI_STC, // 13 + gAvrInt_USART_RX, // 14 + gAvrInt_USART_UDRE, // 15 + gAvrInt_USART_TX, // 16 + gAvrInt_ADC, // 17 + gAvrInt_EE_READY, // 18 + gAvrInt_ANALOG_COMP, // 19 + gAvrInt_TWI, // 20 + gAvrInt_SPM_READY, // 21 + +}; + + +#endif + +//************************************************************************************************** +#if defined(__AVR_ATmega32U4__) +#pragma mark __AVR_ATmega32U4__ +//* teensy 2.0 +//* http://www.pjrc.com/teensy/pinout.html +#define _INTERRUPT_NAMES_DEFINED_ + + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_INT3, // 5 + gAvrInt_RESERVED, // 6 + gAvrInt_RESERVED, // 7 + gAvrInt_INT6, // 8 + gAvrInt_RESERVED, // 9 + gAvrInt_PCINT0, // 10 + gAvrInt_USB_General, // 11 + gAvrInt_USB_Endpoint, // 12 + gAvrInt_WDT, // 13 + gAvrInt_RESERVED, // 14 + gAvrInt_RESERVED, // 15 + gAvrInt_RESERVED, // 16 + gAvrInt_TIMER1_CAPT, // 17 + gAvrInt_TIMER1_COMPA, // 18 + gAvrInt_TIMER1_COMPB, // 19 + gAvrInt_TIMER1_COMPC, // 20 + gAvrInt_TIMER1_OVF, // 21 + gAvrInt_TIMER0_COMPA, // 22 + gAvrInt_TIMER0_COMPB, // 23 + gAvrInt_TIMER0_OVF, // 24 + gAvrInt_SPI_STC, // 25 + + gAvrInt_USART1_RX, // 26 + gAvrInt_USART1_UDRE, // 27 + gAvrInt_USART1_TX, // 28 + gAvrInt_ANALOG_COMP, // 29 + + gAvrInt_ADC, // 30 + gAvrInt_EE_READY, // 31 + + gAvrInt_TIMER3_CAPT, // 32 + gAvrInt_TIMER3_COMPA, // 33 + gAvrInt_TIMER3_COMPB, // 34 + gAvrInt_TIMER3_COMPC, // 35 + gAvrInt_TIMER3_OVF, // 36 + gAvrInt_TWI, // 37 + gAvrInt_SPM_READY, // 38 + + gAvrInt_TIMER4_COMPA, // 39 + gAvrInt_TIMER4_COMPB, // 40 + gAvrInt_TIMER4_COMPD, // 41 + gAvrInt_TIMER4_OVF, // 42 + gAvrInt_TIMER4_FPF, // 43 +}; + +#endif + +//************************************************************************************************** +#if defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) +#pragma mark __AVR_AT90USB1286__ +//* teensy++ 2.0 +//* http://www.pjrc.com/teensy/pinout.html +#define _INTERRUPT_NAMES_DEFINED_ + + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_INT3, // 5 + gAvrInt_INT4, // 6 + gAvrInt_INT5, // 7 + gAvrInt_INT6, // 8 + gAvrInt_INT7, // 9 + gAvrInt_PCINT0, // 10 + gAvrInt_USB_General, // 11 + gAvrInt_USB_Endpoint, // 12 + gAvrInt_WDT, // 13 + gAvrInt_TIMER2_COMPA, // 14 + gAvrInt_TIMER2_COMPB, // 15 + gAvrInt_TIMER2_OVF, // 16 + gAvrInt_TIMER1_CAPT, // 17 + gAvrInt_TIMER1_COMPA, // 18 + gAvrInt_TIMER1_COMPB, // 19 + gAvrInt_TIMER1_COMPC, // 20 + gAvrInt_TIMER1_OVF, // 21 + gAvrInt_TIMER0_COMPA, // 22 + gAvrInt_TIMER0_COMPB, // 23 + gAvrInt_TIMER0_OVF, // 24 + gAvrInt_SPI_STC, // 25 + + gAvrInt_USART1_RX, // 26 + gAvrInt_USART1_UDRE, // 27 + gAvrInt_USART1_TX, // 28 + gAvrInt_ANALOG_COMP, // 29 + + gAvrInt_ADC, // 30 + gAvrInt_EE_READY, // 31 + + gAvrInt_TIMER3_CAPT, // 32 + gAvrInt_TIMER3_COMPA, // 33 + gAvrInt_TIMER3_COMPB, // 34 + gAvrInt_TIMER3_COMPC, // 35 + gAvrInt_TIMER3_OVF, // 36 + gAvrInt_TWI, // 37 + gAvrInt_SPM_READY, // 38 + +}; + +#endif + + + + +//************************************************************************************************** +#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega64__) +#pragma mark __AVR_ATmega64__ __AVR_ATmega128__ +#define _INTERRUPT_NAMES_DEFINED_ + + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_INT3, // 5 + gAvrInt_INT4, // 6 + gAvrInt_INT5, // 7 + gAvrInt_INT6, // 8 + gAvrInt_INT7, // 9 + gAvrInt_TIMER2_COMP, // 10 + gAvrInt_TIMER2_OVF, // 11 + gAvrInt_TIMER1_CAPT, // 12 + gAvrInt_TIMER1_COMPA, // 13 + gAvrInt_TIMER1_COMPB, // 14 + gAvrInt_TIMER1_OVF, // 15 + gAvrInt_TIMER0_COMP, // 16 + gAvrInt_TIMER0_OVF, // 17 + gAvrInt_SPI_STC, // 18 + gAvrInt_USART0_RX, // 19 + gAvrInt_USART0_UDRE, // 20 + gAvrInt_USART0_TX, // 21 + gAvrInt_ADC, // 22 + gAvrInt_EE_READY, // 23 + gAvrInt_ANALOG_COMP, // 24 + gAvrInt_TIMER1_COMPC, // 25 + gAvrInt_TIMER3_CAPT, // 26 + gAvrInt_TIMER3_COMPA, // 27 + gAvrInt_TIMER3_COMPB, // 28 + gAvrInt_TIMER3_COMPC, // 29 + gAvrInt_TIMER3_OVF, // 30 + gAvrInt_USART1_RX, // 31 + gAvrInt_USART1_UDRE, // 32 + gAvrInt_USART1_TX, // 33 + gAvrInt_TWI, // 34 + gAvrInt_SPM_READY, // 35 + +}; + +#endif + +//************************************************************************************************** +#if defined(__AVR_AT90CAN32__) || defined(__AVR_AT90CAN64__) || defined(__AVR_AT90CAN128__) +#pragma mark __AVR_AT90CAN32__ __AVR_AT90CAN64__ __AVR_AT90CAN128__ + +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + + gAvrInt_RESET, // 1 + gAvrInt_INT0, // 2 + gAvrInt_INT1, // 3 + gAvrInt_INT2, // 4 + gAvrInt_INT3, // 5 + gAvrInt_INT4, // 6 + gAvrInt_INT5, // 7 + gAvrInt_INT6, // 8 + gAvrInt_INT7, // 9 + gAvrInt_TIMER2_COMP, // 10 + gAvrInt_TIMER2_OVF, // 11 + gAvrInt_TIMER1_CAPT, // 12 + gAvrInt_TIMER1_COMPA, // 13 + gAvrInt_TIMER1_COMPB, // 14 + gAvrInt_TIMER1_COMPC, // 15 + gAvrInt_TIMER1_OVF, // 16 + gAvrInt_TIMER0_COMP, // 17 + gAvrInt_TIMER0_OVF, // 18 + gAvrInt_CAN_TrafnsferCE, // 19 + gAvrInt_CAN_TimerOverRun, // 20 + gAvrInt_SPI_STC, // 21 + gAvrInt_USART0_RX, // 22 + gAvrInt_USART0_UDRE, // 23 + gAvrInt_USART0_TX, // 24 + gAvrInt_ANALOG_COMP, // 25 + gAvrInt_ADC, // 26 + gAvrInt_EE_READY, // 27 + gAvrInt_TIMER3_CAPT, // 28 + gAvrInt_TIMER3_COMPA, // 29 + gAvrInt_TIMER3_COMPB, // 30 + gAvrInt_TIMER3_COMPC, // 31 + gAvrInt_TIMER3_OVF, // 32 + gAvrInt_USART1_RX, // 33 + gAvrInt_USART1_UDRE, // 34 + gAvrInt_USART1_TX, // 35 + gAvrInt_TWI, // 36 + gAvrInt_SPM_READY, // 37 +}; + +#endif + +//************************************************************************************************** +#if defined (__AVR_ATmega128RFA1__) +#pragma mark __AVR_ATmega128RFA1__ +#define _INTERRUPT_NAMES_DEFINED_ + +PGM_P gInterruptNameTable[] PROGMEM = +{ + //* Atmel changed the number scheme for interrupt vectors + gAvrInt_RESET, // 0 + gAvrInt_INT0, // 1 + gAvrInt_INT1, // 2 + gAvrInt_INT2, // 3 + gAvrInt_INT3, // 4 + gAvrInt_INT4, // 5 + gAvrInt_INT5, // 6 + gAvrInt_INT6, // 7 + gAvrInt_INT7, // 8 + gAvrInt_PCINT0, // 9 + gAvrInt_PCINT1, // 10 + gAvrInt_PCINT2, // 11 + gAvrInt_WDT, // 12 + gAvrInt_TIMER2_COMPA, // 13 + gAvrInt_TIMER2_COMPB, // 14 + gAvrInt_TIMER2_OVF, // 15 + gAvrInt_TIMER1_CAPT, // 16 + gAvrInt_TIMER1_COMPA, // 17 + gAvrInt_TIMER1_COMPB, // 18 + gAvrInt_TIMER1_COMPC, // 19 + gAvrInt_TIMER1_OVF, // 20 + gAvrInt_TIMER0_COMPA, // 21 + gAvrInt_TIMER0_COMPB, // 22 + gAvrInt_TIMER0_OVF, // 23 + gAvrInt_SPI_STC, // 24 + gAvrInt_USART0_RX, // 25 + gAvrInt_USART0_UDRE, // 26 + gAvrInt_USART0_TX, // 27 + gAvrInt_ANALOG_COMP, // 28 + gAvrInt_ADC, // 29 + gAvrInt_EE_READY, // 30 + gAvrInt_TIMER3_CAPT, // 31 + gAvrInt_TIMER3_COMPA, // 32 + gAvrInt_TIMER3_COMPB, // 33 + gAvrInt_TIMER3_COMPC, // 34 + gAvrInt_TIMER3_OVF, // 35 + gAvrInt_USART1_RX, // 36 + gAvrInt_USART1_UDRE, // 37 + gAvrInt_USART1_TX, // 38 + gAvrInt_TWI, // 39 + gAvrInt_SPM_READY, // 40 + gAvrInt_TIMER4_CAPT, // 41 + gAvrInt_TIMER4_COMPA, // 42 + gAvrInt_TIMER4_COMPB, // 43 + gAvrInt_TIMER4_COMPC, // 44 + gAvrInt_TIMER4_OVF, // 45 + gAvrInt_TIMER5_CAPT, // 46 + gAvrInt_TIMER5_COMPA, // 47 + gAvrInt_TIMER5_COMPB, // 48 + gAvrInt_TIMER5_COMPC, // 49 + gAvrInt_TIMER5_OVF, // 50 +#if 1 + gAvrInt_RESERVED, // 51 + gAvrInt_RESERVED, // 52 + gAvrInt_RESERVED, // 53 + + gAvrInt_RESERVED, // 54 + gAvrInt_RESERVED, // 55 + gAvrInt_RESERVED, // 56 + +#else + gAvrInt_USART2_RX, // 51 + gAvrInt_USART2_UDRE, // 52 + gAvrInt_USART2_TX, // 53 + + gAvrInt_USART3_RX, // 54 + gAvrInt_USART3_UDRE, // 55 + gAvrInt_USART3_TX, // 56 +#endif + gAvrInt_TRN_PLL_LOCK, // 57 + gAvrInt_TRN_PLL_UNLOCK, // 58 + gAvrInt_TRN_RX_START, // 59 + gAvrInt_TRN_RX_END, // 60 + gAvrInt_TRN_CAAED_DONE, // 61 + gAvrInt_TRN_FRAME_MATCH,// 62 + gAvrInt_TRN_TX_END, // 63 + gAvrInt_TRN_AWAKE, // 64 + + gAvrInt_SCNT_CMP1, // 65 + gAvrInt_SCNT_CMP2, // 66 + gAvrInt_SCNT_CMP3, // 67 + gAvrInt_SCNT_OVFL, // 68 + gAvrInt_SCNT_BACKOFF, // 69 + gAvrInt_AES_READY, // 70 + gAvrInt_BAT_LOW, // 71 + + +}; + +#endif + + +#if !defined(_INTERRUPT_NAMES_DEFINED_) + #warning No interrupt string defs for this cpu +#endif + diff --git a/bootloaders/stk500v2/command.h b/bootloaders/stk500v2/command.h new file mode 100644 index 0000000..03b1b38 --- /dev/null +++ b/bootloaders/stk500v2/command.h @@ -0,0 +1,114 @@ +//**** ATMEL AVR - A P P L I C A T I O N N O T E ************************ +//* +//* Title: AVR068 - STK500 Communication Protocol +//* Filename: command.h +//* Version: 1.0 +//* Last updated: 31.01.2005 +//* +//* Support E-mail: avr@atmel.com +//* +//************************************************************************** + +// *****************[ STK message constants ]*************************** + +#define MESSAGE_START 0x1B //= ESC = 27 decimal +#define TOKEN 0x0E + +// *****************[ STK general command constants ]************************** + +#define CMD_SIGN_ON 0x01 +#define CMD_SET_PARAMETER 0x02 +#define CMD_GET_PARAMETER 0x03 +#define CMD_SET_DEVICE_PARAMETERS 0x04 +#define CMD_OSCCAL 0x05 +#define CMD_LOAD_ADDRESS 0x06 +#define CMD_FIRMWARE_UPGRADE 0x07 + + +// *****************[ STK ISP command constants ]****************************** + +#define CMD_ENTER_PROGMODE_ISP 0x10 +#define CMD_LEAVE_PROGMODE_ISP 0x11 +#define CMD_CHIP_ERASE_ISP 0x12 +#define CMD_PROGRAM_FLASH_ISP 0x13 +#define CMD_READ_FLASH_ISP 0x14 +#define CMD_PROGRAM_EEPROM_ISP 0x15 +#define CMD_READ_EEPROM_ISP 0x16 +#define CMD_PROGRAM_FUSE_ISP 0x17 +#define CMD_READ_FUSE_ISP 0x18 +#define CMD_PROGRAM_LOCK_ISP 0x19 +#define CMD_READ_LOCK_ISP 0x1A +#define CMD_READ_SIGNATURE_ISP 0x1B +#define CMD_READ_OSCCAL_ISP 0x1C +#define CMD_SPI_MULTI 0x1D + +// *****************[ STK PP command constants ]******************************* + +#define CMD_ENTER_PROGMODE_PP 0x20 +#define CMD_LEAVE_PROGMODE_PP 0x21 +#define CMD_CHIP_ERASE_PP 0x22 +#define CMD_PROGRAM_FLASH_PP 0x23 +#define CMD_READ_FLASH_PP 0x24 +#define CMD_PROGRAM_EEPROM_PP 0x25 +#define CMD_READ_EEPROM_PP 0x26 +#define CMD_PROGRAM_FUSE_PP 0x27 +#define CMD_READ_FUSE_PP 0x28 +#define CMD_PROGRAM_LOCK_PP 0x29 +#define CMD_READ_LOCK_PP 0x2A +#define CMD_READ_SIGNATURE_PP 0x2B +#define CMD_READ_OSCCAL_PP 0x2C + +#define CMD_SET_CONTROL_STACK 0x2D + +// *****************[ STK HVSP command constants ]***************************** + +#define CMD_ENTER_PROGMODE_HVSP 0x30 +#define CMD_LEAVE_PROGMODE_HVSP 0x31 +#define CMD_CHIP_ERASE_HVSP 0x32 +#define CMD_PROGRAM_FLASH_HVSP ` 0x33 +#define CMD_READ_FLASH_HVSP 0x34 +#define CMD_PROGRAM_EEPROM_HVSP 0x35 +#define CMD_READ_EEPROM_HVSP 0x36 +#define CMD_PROGRAM_FUSE_HVSP 0x37 +#define CMD_READ_FUSE_HVSP 0x38 +#define CMD_PROGRAM_LOCK_HVSP 0x39 +#define CMD_READ_LOCK_HVSP 0x3A +#define CMD_READ_SIGNATURE_HVSP 0x3B +#define CMD_READ_OSCCAL_HVSP 0x3C + +// *****************[ STK status constants ]*************************** + +// Success +#define STATUS_CMD_OK 0x00 + +// Warnings +#define STATUS_CMD_TOUT 0x80 +#define STATUS_RDY_BSY_TOUT 0x81 +#define STATUS_SET_PARAM_MISSING 0x82 + +// Errors +#define STATUS_CMD_FAILED 0xC0 +#define STATUS_CKSUM_ERROR 0xC1 +#define STATUS_CMD_UNKNOWN 0xC9 + +// *****************[ STK parameter constants ]*************************** +#define PARAM_BUILD_NUMBER_LOW 0x80 +#define PARAM_BUILD_NUMBER_HIGH 0x81 +#define PARAM_HW_VER 0x90 +#define PARAM_SW_MAJOR 0x91 +#define PARAM_SW_MINOR 0x92 +#define PARAM_VTARGET 0x94 +#define PARAM_VADJUST 0x95 +#define PARAM_OSC_PSCALE 0x96 +#define PARAM_OSC_CMATCH 0x97 +#define PARAM_SCK_DURATION 0x98 +#define PARAM_TOPCARD_DETECT 0x9A +#define PARAM_STATUS 0x9C +#define PARAM_DATA 0x9D +#define PARAM_RESET_POLARITY 0x9E +#define PARAM_CONTROLLER_INIT 0x9F + +// *****************[ STK answer constants ]*************************** + +#define ANSWER_CKSUM_ERROR 0xB0 + diff --git a/bootloaders/stk500v2/makedep.mk b/bootloaders/stk500v2/makedep.mk new file mode 100644 index 0000000..bb85aa7 --- /dev/null +++ b/bootloaders/stk500v2/makedep.mk @@ -0,0 +1,6 @@ +################################################################################ +# Automatically-generated file. Do not edit or delete the file +################################################################################ + +stk500boot.c + diff --git a/bootloaders/stk500v2/stk500boot.c b/bootloaders/stk500v2/stk500boot.c new file mode 100644 index 0000000..de4ce8c --- /dev/null +++ b/bootloaders/stk500v2/stk500boot.c @@ -0,0 +1,1194 @@ +/***************************************************************************** +Title: STK500v2 compatible bootloader + Modified for Wiring board ATMega128-16MHz +Author: Peter Fleury http://jump.to/fleury +Compiler: avr-gcc 3.4.5 or 4.1 / avr-libc 1.4.3 +Hardware: All AVRs with bootloader support, tested with ATmega8 +License: GNU General Public License + +Modified: Worapoht Kornkaewwattanakul http://www.avride.com +Date: 17 October 2007 +Update: 1st, 29 Dec 2007 : Enable CMD_SPI_MULTI but ignore unused command by return 0x00 byte response.. +Compiler: WINAVR20060421 +Description: add timeout feature like previous Wiring bootloader + +DESCRIPTION: + This program allows an AVR with bootloader capabilities to + read/write its own Flash/EEprom. To enter Programming mode + an input pin is checked. If this pin is pulled low, programming mode + is entered. If not, normal execution is done from $0000 + "reset" vector in Application area. + Size fits into a 1024 word bootloader section + when compiled with avr-gcc 4.1 + (direct replace on Wiring Board without fuse setting changed) + +USAGE: + - Set AVR MCU type and clock-frequency (F_CPU) in the Makefile. + - Set baud rate below (AVRISP only works with 115200 bps) + - compile/link the bootloader with the supplied Makefile + - program the "Boot Flash section size" (BOOTSZ fuses), + for boot-size 1024 words: program BOOTSZ01 + - enable the BOOT Reset Vector (program BOOTRST) + - Upload the hex file to the AVR using any ISP programmer + - Program Boot Lock Mode 3 (program BootLock 11 and BootLock 12 lock bits) // (leave them) + - Reset your AVR while keeping PROG_PIN pulled low // (for enter bootloader by switch) + - Start AVRISP Programmer (AVRStudio/Tools/Program AVR) + - AVRISP will detect the bootloader + - Program your application FLASH file and optional EEPROM file using AVRISP + +Note: + Erasing the device without flashing, through AVRISP GUI button "Erase Device" + is not implemented, due to AVRStudio limitations. + Flash is always erased before programming. + + AVRdude: + Please uncomment #define REMOVE_CMD_SPI_MULTI when using AVRdude. + Comment #define REMOVE_PROGRAM_LOCK_BIT_SUPPORT to reduce code size + Read Fuse Bits and Read/Write Lock Bits is not supported + +NOTES: + Based on Atmel Application Note AVR109 - Self-programming + Based on Atmel Application Note AVR068 - STK500v2 Protocol + +LICENSE: + Copyright (C) 2006 Peter Fleury + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*****************************************************************************/ + +//************************************************************************ +//* Edit History +//************************************************************************ +//* Jul 7, 2010 = Mark Sproul msproul@skycharoit.com +//* Jul 7, 2010 Working on mega2560. No Auto-restart +//* Jul 7, 2010 Switched to 8K bytes (4K words) so that we have room for the monitor +//* Jul 8, 2010 Found older version of source that had auto restart, put that code back in +//* Jul 8, 2010 Adding monitor code +//* Jul 11, 2010 Added blinking LED while waiting for download to start +//* Jul 11, 2010 Added EEPROM test +//* Jul 29, 2010 Added recchar_timeout for timing out on bootloading +//* Aug 23, 2010 Added support for atmega2561 +//* Aug 26, 2010 Removed support for BOOT_BY_SWITCH +//* Sep 8, 2010 Added support for atmega16 +//* Nov 9, 2010 Issue 392:Fixed bug that 3 !!! in code would cause it to jump to monitor +//* Jun 24, 2011 Removed analogRead (was not used) +//* Dec 29, 2011 Issue 181: added watch dog timmer support +//* Dec 29, 2011 Issue 505: bootloader is comparing the seqNum to 1 or the current sequence +//* Jan 1, 2012 Issue 543: CMD_CHIP_ERASE_ISP now returns STATUS_CMD_FAILED instead of STATUS_CMD_OK +//* Jan 1, 2012 Issue 543: Write EEPROM now does something (NOT TESTED) +//* Jan 1, 2012 Issue 544: stk500v2 bootloader doesn't support reading fuses +//************************************************************************ + +//************************************************************************ +//* these are used to test issues +//* http://code.google.com/p/arduino/issues/detail?id=505 +//* Reported by mark.stubbs, Mar 14, 2011 +//* The STK500V2 bootloader is comparing the seqNum to 1 or the current sequence +//* (IE: Requiring the sequence to be 1 or match seqNum before continuing). +//* The correct behavior is for the STK500V2 to accept the PC's sequence number, and echo it back for the reply message. +#define _FIX_ISSUE_505_ +//************************************************************************ +//* Issue 181: added watch dog timmer support +#define _FIX_ISSUE_181_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "command.h" + + +#ifndef EEWE + #define EEWE 1 +#endif +#ifndef EEMWE + #define EEMWE 2 +#endif + +//#define _DEBUG_SERIAL_ +//#define _DEBUG_WITH_LEDS_ + + +/* + * Uncomment the following lines to save code space + */ +#define REMOVE_PROGRAM_LOCK_BIT_SUPPORT // disable program lock bits +#define REMOVE_BOOTLOADER_LED // no LED to show active bootloader +//#define REMOVE_CMD_SPI_MULTI // disable processing of SPI_MULTI commands, Remark this line for AVRDUDE +// + +//************************************************************************ +//FUSES +#include + +#if defined (__AVR_ATmega2560__) + FUSES={ .extended = 0xFC, .high = 0xD8, .low = 0xE0}; // Fuses + Lock programming + LOCKBITS= 0xCC; +#else + #error "FUSES for selected CPU not set" +#endif +//************************************************************************ + +//************************************************************************ +//* LED on pin "PROGLED_PIN" on port "PROGLED_PORT" +//* indicates that bootloader is active +//* PG2 -> LED on Wiring board +//************************************************************************ +#define BLINK_LED_WHILE_WAITING + +#ifdef _MEGA_BOARD_ + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB7 +#elif defined( _BOARD_AMBER128_ ) + //* this is for the amber 128 http://www.soc-robotics.com/ + //* onbarod led is PORTE4 + #define PROGLED_PORT PORTD + #define PROGLED_DDR DDRD + #define PROGLED_PIN PINE7 +#elif defined( _CEREBOTPLUS_BOARD_ ) || defined(_CEREBOT_II_BOARD_) + //* this is for the Cerebot 2560 board and the Cerebot-ii + //* onbarod leds are on PORTE4-7 + #define PROGLED_PORT PORTE + #define PROGLED_DDR DDRE + #define PROGLED_PIN PINE7 +#elif defined( _PENGUINO_ ) + //* this is for the Penguino + //* onbarod led is PORTE4 + #define PROGLED_PORT PORTC + #define PROGLED_DDR DDRC + #define PROGLED_PIN PINC6 +#elif defined( _ANDROID_2561_ ) || defined( __AVR_ATmega2561__ ) + //* this is for the Boston Android 2561 + //* onbarod led is PORTE4 + #define PROGLED_PORT PORTA + #define PROGLED_DDR DDRA + #define PROGLED_PIN PINA3 +#elif defined( _BOARD_MEGA16 ) + //* onbarod led is PORTA7 + #define PROGLED_PORT PORTA + #define PROGLED_DDR DDRA + #define PROGLED_PIN PINA7 + #define UART_BAUDRATE_DOUBLE_SPEED 0 + +#elif defined( _BOARD_BAHBOT_ ) + //* dosent have an onboard LED but this is what will probably be added to this port + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB0 + +#elif defined( _BOARD_ROBOTX_ ) + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB6 +#elif defined( _BOARD_CUSTOM1284_BLINK_B0_ ) + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB0 +#elif defined( _BOARD_CUSTOM1284_ ) + #define PROGLED_PORT PORTD + #define PROGLED_DDR DDRD + #define PROGLED_PIN PIND5 +#elif defined( _AVRLIP_ ) + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB5 +#elif defined( _BOARD_STK500_ ) + #define PROGLED_PORT PORTA + #define PROGLED_DDR DDRA + #define PROGLED_PIN PINA7 +#elif defined( _BOARD_STK502_ ) + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB5 +#elif defined( _BOARD_STK525_ ) + #define PROGLED_PORT PORTB + #define PROGLED_DDR DDRB + #define PROGLED_PIN PINB7 +#else + #define PROGLED_PORT PORTG + #define PROGLED_DDR DDRG + #define PROGLED_PIN PING2 +#endif + + + +/* + * define CPU frequency in Mhz here if not defined in Makefile + */ +#ifndef F_CPU + #define F_CPU 16000000UL +#endif + +#define _BLINK_LOOP_COUNT_ (F_CPU / 2250) +/* + * UART Baudrate, AVRStudio AVRISP only accepts 115200 bps + */ + +#ifndef BAUDRATE + #define BAUDRATE 115200 +#endif + +/* + * Enable (1) or disable (0) USART double speed operation + */ +#ifndef UART_BAUDRATE_DOUBLE_SPEED + #if defined (__AVR_ATmega32__) + #define UART_BAUDRATE_DOUBLE_SPEED 0 + #else + #define UART_BAUDRATE_DOUBLE_SPEED 1 + #endif +#endif + +/* + * HW and SW version, reported to AVRISP, must match version of AVRStudio + */ +#define CONFIG_PARAM_BUILD_NUMBER_LOW 0 +#define CONFIG_PARAM_BUILD_NUMBER_HIGH 0 +#define CONFIG_PARAM_HW_VER 0x0F +#define CONFIG_PARAM_SW_MAJOR 2 +#define CONFIG_PARAM_SW_MINOR 0x0A + +/* + * Calculate the address where the bootloader starts from FLASHEND and BOOTSIZE + * (adjust BOOTSIZE below and BOOTLOADER_ADDRESS in Makefile if you want to change the size of the bootloader) + */ +//#define BOOTSIZE 1024 +#if FLASHEND > 0x0F000 + #define BOOTSIZE 8192 +#else + #define BOOTSIZE 2048 +#endif + +#define APP_END (FLASHEND -(2*BOOTSIZE) + 1) + +/* + * Signature bytes are not available in avr-gcc io_xxx.h + */ +#if defined (__AVR_ATmega8__) + #define SIGNATURE_BYTES 0x1E9307 +#elif defined (__AVR_ATmega16__) + #define SIGNATURE_BYTES 0x1E9403 +#elif defined (__AVR_ATmega32__) + #define SIGNATURE_BYTES 0x1E9502 +#elif defined (__AVR_ATmega8515__) + #define SIGNATURE_BYTES 0x1E9306 +#elif defined (__AVR_ATmega8535__) + #define SIGNATURE_BYTES 0x1E9308 +#elif defined (__AVR_ATmega162__) + #define SIGNATURE_BYTES 0x1E9404 +#elif defined (__AVR_ATmega128__) + #define SIGNATURE_BYTES 0x1E9702 +#elif defined (__AVR_ATmega1280__) + #define SIGNATURE_BYTES 0x1E9703 +#elif defined (__AVR_ATmega2560__) + #define SIGNATURE_BYTES 0x1E9801 +#elif defined (__AVR_ATmega2561__) + #define SIGNATURE_BYTES 0x1e9802 +#elif defined (__AVR_ATmega1284P__) + #define SIGNATURE_BYTES 0x1e9705 +#elif defined (__AVR_ATmega640__) + #define SIGNATURE_BYTES 0x1e9608 +#elif defined (__AVR_ATmega64__) + #define SIGNATURE_BYTES 0x1E9602 +#elif defined (__AVR_ATmega169__) + #define SIGNATURE_BYTES 0x1e9405 +#elif defined (__AVR_AT90USB1287__) + #define SIGNATURE_BYTES 0x1e9782 +#else + #error "no signature definition for MCU available" +#endif + + +#if defined(_BOARD_ROBOTX_) || defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__) + #define UART_BAUD_RATE_LOW UBRR1L + #define UART_STATUS_REG UCSR1A + #define UART_CONTROL_REG UCSR1B + #define UART_ENABLE_TRANSMITTER TXEN1 + #define UART_ENABLE_RECEIVER RXEN1 + #define UART_TRANSMIT_COMPLETE TXC1 + #define UART_RECEIVE_COMPLETE RXC1 + #define UART_DATA_REG UDR1 + #define UART_DOUBLE_SPEED U2X1 + +#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ + || defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) + /* ATMega8 with one USART */ + #define UART_BAUD_RATE_LOW UBRRL + #define UART_STATUS_REG UCSRA + #define UART_CONTROL_REG UCSRB + #define UART_ENABLE_TRANSMITTER TXEN + #define UART_ENABLE_RECEIVER RXEN + #define UART_TRANSMIT_COMPLETE TXC + #define UART_RECEIVE_COMPLETE RXC + #define UART_DATA_REG UDR + #define UART_DOUBLE_SPEED U2X + +#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega162__) \ + || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) + /* ATMega with two USART, use UART0 */ + #define UART_BAUD_RATE_LOW UBRR0L + #define UART_STATUS_REG UCSR0A + #define UART_CONTROL_REG UCSR0B + #define UART_ENABLE_TRANSMITTER TXEN0 + #define UART_ENABLE_RECEIVER RXEN0 + #define UART_TRANSMIT_COMPLETE TXC0 + #define UART_RECEIVE_COMPLETE RXC0 + #define UART_DATA_REG UDR0 + #define UART_DOUBLE_SPEED U2X0 +#elif defined(UBRR0L) && defined(UCSR0A) && defined(TXEN0) + /* ATMega with two USART, use UART0 */ + #define UART_BAUD_RATE_LOW UBRR0L + #define UART_STATUS_REG UCSR0A + #define UART_CONTROL_REG UCSR0B + #define UART_ENABLE_TRANSMITTER TXEN0 + #define UART_ENABLE_RECEIVER RXEN0 + #define UART_TRANSMIT_COMPLETE TXC0 + #define UART_RECEIVE_COMPLETE RXC0 + #define UART_DATA_REG UDR0 + #define UART_DOUBLE_SPEED U2X0 +#elif defined(UBRRL) && defined(UCSRA) && defined(UCSRB) && defined(TXEN) && defined(RXEN) + //* catch all + #define UART_BAUD_RATE_LOW UBRRL + #define UART_STATUS_REG UCSRA + #define UART_CONTROL_REG UCSRB + #define UART_ENABLE_TRANSMITTER TXEN + #define UART_ENABLE_RECEIVER RXEN + #define UART_TRANSMIT_COMPLETE TXC + #define UART_RECEIVE_COMPLETE RXC + #define UART_DATA_REG UDR + #define UART_DOUBLE_SPEED U2X +#else + #error "no UART definition for MCU available" +#endif + + + +/* + * Macro to calculate UBBR from XTAL and baudrate + */ +#if defined(__AVR_ATmega32__) && UART_BAUDRATE_DOUBLE_SPEED + #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu / 4 / baudRate - 1) / 2) +#elif defined(__AVR_ATmega32__) + #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu / 8 / baudRate - 1) / 2) +#elif UART_BAUDRATE_DOUBLE_SPEED + #define UART_BAUD_SELECT(baudRate,xtalCpu) (((float)(xtalCpu))/(((float)(baudRate))*8.0)-1.0+0.5) +#else + #define UART_BAUD_SELECT(baudRate,xtalCpu) (((float)(xtalCpu))/(((float)(baudRate))*16.0)-1.0+0.5) +#endif + + +/* + * States used in the receive state machine + */ +#define ST_START 0 +#define ST_GET_SEQ_NUM 1 +#define ST_MSG_SIZE_1 2 +#define ST_MSG_SIZE_2 3 +#define ST_GET_TOKEN 4 +#define ST_GET_DATA 5 +#define ST_GET_CHECK 6 +#define ST_PROCESS 7 + +/* + * use 16bit address variable for ATmegas with <= 64K flash + */ +#if defined(RAMPZ) + typedef uint32_t address_t; +#else + typedef uint16_t address_t; +#endif + +/* + * function prototypes + */ +static void sendchar(char c); +static unsigned char recchar(void); + +/* + * since this bootloader is not linked against the avr-gcc crt1 functions, + * to reduce the code size, we need to provide our own initialization + */ +void __jumpMain (void) __attribute__ ((naked)) __attribute__ ((section (".init9"))); +#include + +//#define SPH_REG 0x3E +//#define SPL_REG 0x3D + +//***************************************************************************** +void __jumpMain(void) +{ +//* July 17, 2010 Added stack pointer initialzation +//* the first line did not do the job on the ATmega128 + + asm volatile ( ".set __stack, %0" :: "i" (RAMEND) ); + +//* set stack pointer to top of RAM + + asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) ); + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) ); + + asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) ); + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) ); + + asm volatile ( "clr __zero_reg__" ); // GCC depends on register r1 set to 0 + asm volatile ( "out %0, __zero_reg__" :: "I" (_SFR_IO_ADDR(SREG)) ); // set SREG to 0 + asm volatile ( "jmp main"); // jump to main() +} + + +//***************************************************************************** +void delay_ms(unsigned int timedelay) +{ + unsigned int i; + for (i=0;i> 1) +//***************************************************************************** +static unsigned char recchar_timeout(void) +{ +uint32_t count = 0; + + while (!(UART_STATUS_REG & (1 << UART_RECEIVE_COMPLETE))) + { + // wait for data + count++; + if (count > MAX_TIME_COUNT) + { + unsigned int data; + #if (FLASHEND > 0x10000) + data = pgm_read_word_far(0); //* get the first word of the user program + #else + data = pgm_read_word_near(0); //* get the first word of the user program + #endif + if (data != 0xffff) //* make sure its valid before jumping to it. + { + asm volatile( + "clr r30 \n\t" + "clr r31 \n\t" + "ijmp \n\t" + ); + } + count = 0; + } + } + return UART_DATA_REG; +} + +//* for watch dog timer startup +void (*app_start)(void) = 0x0000; + + +//***************************************************************************** +int main(void) +{ + address_t address = 0; + address_t eraseAddress = 0; + unsigned char msgParseState; + unsigned int ii = 0; + unsigned char checksum = 0; + unsigned char seqNum = 0; + unsigned int msgLength = 0; + unsigned char msgBuffer[285]; + unsigned char c, *p; + unsigned char isLeave = 0; + + unsigned long boot_timeout; + unsigned long boot_timer; + unsigned int boot_state; +#ifdef ENABLE_MONITOR + unsigned int exPointCntr = 0; + unsigned int rcvdCharCntr = 0; +#endif + + //* some chips dont set the stack properly + asm volatile ( ".set __stack, %0" :: "i" (RAMEND) ); + asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) ); + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) ); + asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) ); + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) ); + +#ifdef _FIX_ISSUE_181_ + //************************************************************************ + //* Dec 29, 2011 Issue #181, added watch dog timmer support + //* handle the watch dog timer + uint8_t mcuStatusReg; + mcuStatusReg = MCUSR; + + __asm__ __volatile__ ("cli"); + __asm__ __volatile__ ("wdr"); + MCUSR = 0; + WDTCSR |= _BV(WDCE) | _BV(WDE); + WDTCSR = 0; + __asm__ __volatile__ ("sei"); + // check if WDT generated the reset, if so, go straight to app + if (mcuStatusReg & _BV(WDRF)) + { + app_start(); + } + //************************************************************************ +#endif + + + boot_timer = 0; + boot_state = 0; + +#ifdef BLINK_LED_WHILE_WAITING +// boot_timeout = 90000; //* should be about 4 seconds +// boot_timeout = 170000; + boot_timeout = 20000; //* should be about 1 second +#else + boot_timeout = 3500000; // 7 seconds , approx 2us per step when optimize "s" +#endif + /* + * Branch to bootloader or application code ? + */ + +#ifndef REMOVE_BOOTLOADER_LED + /* PROG_PIN pulled low, indicate with LED that bootloader is active */ + PROGLED_DDR |= (1< boot_timeout) + { + boot_state = 1; // (after ++ -> boot_state=2 bootloader timeout, jump to main 0x00000 ) + } + #ifdef BLINK_LED_WHILE_WAITING + if ((boot_timer % _BLINK_LOOP_COUNT_) == 0) + { + //* toggle the LED + PROGLED_PORT ^= (1<> 16) & 0x000000FF; + } + else if ( signatureIndex == 1 ) + { + answerByte = (SIGNATURE_BYTES >> 8) & 0x000000FF; + } + else + { + answerByte = SIGNATURE_BYTES & 0x000000FF; + } + } + else if ( msgBuffer[4] & 0x50 ) + { + //* Issue 544: stk500v2 bootloader doesn't support reading fuses + //* I cant find the docs that say what these are supposed to be but this was figured out by trial and error + // answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS); + // answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); + // answerByte = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS); + if (msgBuffer[4] == 0x50) + { + answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS); + } + else if (msgBuffer[4] == 0x58) + { + answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); + } + else + { + answerByte = 0; + } + } + else + { + answerByte = 0; // for all others command are not implemented, return dummy value for AVRDUDE happy + } + if ( !flag ) + { + msgLength = 7; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = 0; + msgBuffer[3] = msgBuffer[4]; + msgBuffer[4] = 0; + msgBuffer[5] = answerByte; + msgBuffer[6] = STATUS_CMD_OK; + } + } + break; + #endif + case CMD_SIGN_ON: + msgLength = 11; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = 8; + msgBuffer[3] = 'A'; + msgBuffer[4] = 'V'; + msgBuffer[5] = 'R'; + msgBuffer[6] = 'I'; + msgBuffer[7] = 'S'; + msgBuffer[8] = 'P'; + msgBuffer[9] = '_'; + msgBuffer[10] = '2'; + break; + + case CMD_GET_PARAMETER: + { + unsigned char value; + + switch(msgBuffer[1]) + { + case PARAM_BUILD_NUMBER_LOW: + value = CONFIG_PARAM_BUILD_NUMBER_LOW; + break; + case PARAM_BUILD_NUMBER_HIGH: + value = CONFIG_PARAM_BUILD_NUMBER_HIGH; + break; + case PARAM_HW_VER: + value = CONFIG_PARAM_HW_VER; + break; + case PARAM_SW_MAJOR: + value = CONFIG_PARAM_SW_MAJOR; + break; + case PARAM_SW_MINOR: + value = CONFIG_PARAM_SW_MINOR; + break; + default: + value = 0; + break; + } + msgLength = 3; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = value; + } + break; + + case CMD_LEAVE_PROGMODE_ISP: + isLeave = 1; + //* fall thru + + case CMD_SET_PARAMETER: + case CMD_ENTER_PROGMODE_ISP: + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + break; + + case CMD_READ_SIGNATURE_ISP: + { + unsigned char signatureIndex = msgBuffer[4]; + unsigned char signature; + + if ( signatureIndex == 0 ) + signature = (SIGNATURE_BYTES >>16) & 0x000000FF; + else if ( signatureIndex == 1 ) + signature = (SIGNATURE_BYTES >> 8) & 0x000000FF; + else + signature = SIGNATURE_BYTES & 0x000000FF; + + msgLength = 4; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = signature; + msgBuffer[3] = STATUS_CMD_OK; + } + break; + + case CMD_READ_LOCK_ISP: + msgLength = 4; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = boot_lock_fuse_bits_get( GET_LOCK_BITS ); + msgBuffer[3] = STATUS_CMD_OK; + break; + + case CMD_READ_FUSE_ISP: + { + unsigned char fuseBits; + + if ( msgBuffer[2] == 0x50 ) + { + if ( msgBuffer[3] == 0x08 ) + fuseBits = boot_lock_fuse_bits_get( GET_EXTENDED_FUSE_BITS ); + else + fuseBits = boot_lock_fuse_bits_get( GET_LOW_FUSE_BITS ); + } + else + { + fuseBits = boot_lock_fuse_bits_get( GET_HIGH_FUSE_BITS ); + } + msgLength = 4; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = fuseBits; + msgBuffer[3] = STATUS_CMD_OK; + } + break; + + #ifndef REMOVE_PROGRAM_LOCK_BIT_SUPPORT + case CMD_PROGRAM_LOCK_ISP: + { + unsigned char lockBits = msgBuffer[4]; + + lockBits = (~lockBits) & 0x3C; // mask BLBxx bits + boot_lock_bits_set(lockBits); // and program it + boot_spm_busy_wait(); + + msgLength = 3; + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = STATUS_CMD_OK; + } + break; + #endif + case CMD_CHIP_ERASE_ISP: + eraseAddress = 0; + msgLength = 2; + // msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[1] = STATUS_CMD_FAILED; //* isue 543, return FAILED instead of OK + break; + + case CMD_LOAD_ADDRESS: + #if defined(RAMPZ) + address = ( ((address_t)(msgBuffer[1])<<24)|((address_t)(msgBuffer[2])<<16)|((address_t)(msgBuffer[3])<<8)|(msgBuffer[4]) )<<1; + #else + address = ( ((msgBuffer[3])<<8)|(msgBuffer[4]) )<<1; //convert word to byte address + #endif + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + break; + + case CMD_PROGRAM_FLASH_ISP: + case CMD_PROGRAM_EEPROM_ISP: + { + unsigned int size = ((msgBuffer[1])<<8) | msgBuffer[2]; + unsigned char *p = msgBuffer+10; + unsigned int data; + unsigned char highByte, lowByte; + address_t tempaddress = address; + + + if ( msgBuffer[0] == CMD_PROGRAM_FLASH_ISP ) + { + // erase only main section (bootloader protection) + while (eraseAddress < APP_END ) + { + boot_page_erase(eraseAddress); // Perform page erase + boot_spm_busy_wait(); // Wait until the memory is erased. + eraseAddress += SPM_PAGESIZE; // point to next page to be erase + } + + /* Write FLASH */ + do { + lowByte = *p++; + highByte = *p++; + + data = (highByte << 8) | lowByte; + boot_page_fill(address,data); + + address = address + 2; // Select next word in memory + size -= 2; // Reduce number of bytes to write by two + } while (size); // Loop until all bytes written + + boot_page_write(tempaddress); + boot_spm_busy_wait(); + boot_rww_enable(); // Re-enable the RWW section + } + else + { + //* issue 543, this should work, It has not been tested. + uint16_t ii = address >> 1; + /* write EEPROM */ + while (size) { + eeprom_write_byte((uint8_t*)ii, *p++); + address+=2; // Select next EEPROM byte + ii++; + size--; + } + } + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + } + break; + + case CMD_READ_FLASH_ISP: + case CMD_READ_EEPROM_ISP: + { + unsigned int size = ((msgBuffer[1])<<8) | msgBuffer[2]; + unsigned char *p = msgBuffer+1; + msgLength = size+3; + + *p++ = STATUS_CMD_OK; + if (msgBuffer[0] == CMD_READ_FLASH_ISP ) + { + //Read protection + if (eraseAddress < APP_END) { + msgLength = 2; + msgBuffer[1] = STATUS_CMD_FAILED; + break; + } + + unsigned int data; + + // Read FLASH + do { + //#if defined(RAMPZ) + #if (FLASHEND > 0x10000) + data = pgm_read_word_far(address); + #else + data = pgm_read_word_near(address); + #endif + *p++ = (unsigned char)data; //LSB + *p++ = (unsigned char)(data >> 8); //MSB + address += 2; // Select next word in memory + size -= 2; + }while (size); + } + else + { + /* Read EEPROM */ + do { + EEARL = address; // Setup EEPROM address + EEARH = ((address >> 8)); + address++; // Select next EEPROM byte + EECR |= (1<>8)&0xFF); + sendchar(c); + checksum ^= c; + + c = msgLength&0x00FF; + sendchar(c); + checksum ^= c; + + sendchar(TOKEN); + checksum ^= TOKEN; + + p = msgBuffer; + while ( msgLength ) + { + c = *p++; + sendchar(c); + checksum ^=c; + msgLength--; + } + sendchar(checksum); + seqNum++; + + #ifndef REMOVE_BOOTLOADER_LED + //* toggle the LED + PROGLED_PORT ^= (1< + + +base address = f000 +avrdude: Device signature = 0x1e9703 +avrdude: safemode: lfuse reads as FF +avrdude: safemode: hfuse reads as D8 +avrdude: safemode: efuse reads as F5 +avrdude> +*/ + diff --git a/bootloaders/stk500v2/stk500boot.componentinfo.xml b/bootloaders/stk500v2/stk500boot.componentinfo.xml new file mode 100644 index 0000000..a07492b --- /dev/null +++ b/bootloaders/stk500v2/stk500boot.componentinfo.xml @@ -0,0 +1,86 @@ + + + + + + + Device + Startup + + + Atmel + 1.7.0 + C:/Program Files (x86)\Atmel\Studio\7.0\Packs + + + + + C:/Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\include\ + + include + C + + + include/ + + + + + C:/Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\include\avr\iom2560.h + + header + C + MkwPezikXVtYA90mpLpFfA== + + include/avr/iom2560.h + + + + + C:/Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\templates\main.c + template + source + C Exe + iLje1HNt5V/l3ISGz67DFw== + + templates/main.c + Main file (.c) + + + + C:/Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\templates\main.cpp + template + source + C Exe + mkKaE95TOoATsuBGv6jmxg== + + templates/main.cpp + Main file (.cpp) + + + + C:/Program Files (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560 + + libraryPrefix + GCC + + + gcc/dev/atmega2560 + + + + + ATmega_DFP + C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/Atmel.ATmega_DFP.pdsc + 1.7.374 + true + ATmega2560 + + + + Resolved + Fixed + true + + + \ No newline at end of file diff --git a/bootloaders/stk500v2/stk500boot.cproj b/bootloaders/stk500v2/stk500boot.cproj new file mode 100644 index 0000000..11b1ca6 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot.cproj @@ -0,0 +1,174 @@ + + + + 2.0 + 7.0 + com.Atmel.AVRGCC8.C + dce6c7e3-ee26-4d79-826b-08594b9ad897 + ATmega2560 + none + Executable + C + $(MSBuildProjectName) + .elf + $(MSBuildProjectDirectory)\$(Configuration) + STK500 + stk500boot + STK500 + Native + true + false + true + true + 0x20000000 + + true + exception_table + 2 + 0 + 0 + + com.atmel.avrdbg.tool.atmelice + J41800094359 + 0x1E9801 + + + + 200000 + 125000 + + ISP + + com.atmel.avrdbg.tool.atmelice + J41800094359 + Atmel-ICE + + ISP + 125000 + + + + + + + + com.atmel.avrdbg.tool.simulator + + + Simulator + + + + + + -mmcu=atmega2560 -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560" + True + True + True + True + True + False + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\include\ + + + Optimize for size (-Os) + True + True + True + + + libm + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\include\ + + + + + + + + + -mmcu=atmega2560 -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\gcc\dev\atmega2560" + True + True + True + True + True + False + True + True + + + DEBUG + F_CPU=16000000UL + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\include\ + + + Optimize for size (-Os) + True + True + Default (-g2) + True + + + libm + + + + + .text=3e000 + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.7.374\include\ + + + Default (-Wa,-g) + + + False + $(MSBuildProjectDirectory)\$(Configuration) + mega2560 + clean + C:\Users\Kirill\Documents\GIT\STK500\STK500\Debug\Makefile + stk500boot_v2_mega2560 + .elf + + + + compile + + + compile + + + compile + + + compile + + + + + compile + + + + \ No newline at end of file diff --git a/bootloaders/stk500v2/stk500boot.d b/bootloaders/stk500v2/stk500boot.d new file mode 100644 index 0000000..74aa067 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot.d @@ -0,0 +1,67 @@ +stk500boot.d stk500boot.o: .././stk500boot.c \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\inttypes.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include\stdint.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdint.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\io.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\sfr_defs.h \ + C:\Program\ Files\ (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\include/avr/iom2560.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\portpins.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\common.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\version.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\fuse.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\lock.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\interrupt.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\boot.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\eeprom.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include\stddef.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include-fixed\limits.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\pgmspace.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util\delay.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util\delay_basic.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\math.h \ + c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdlib.h \ + .././command.h + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\inttypes.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include\stdint.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdint.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\io.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\sfr_defs.h: + +C:\Program\ Files\ (x86)\Atmel\Studio\7.0\Packs\Atmel\ATmega_DFP\1.7.374\include/avr/iom2560.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\portpins.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\common.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\version.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\fuse.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\lock.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\interrupt.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\boot.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\eeprom.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include\stddef.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\lib\gcc\avr\5.4.0\include-fixed\limits.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\avr\pgmspace.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util\delay.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util\delay_basic.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\math.h: + +c:\program\ files\ (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\stdlib.h: + +.././command.h: diff --git a/bootloaders/stk500v2/stk500boot.o b/bootloaders/stk500v2/stk500boot.o new file mode 100644 index 0000000..efe848a Binary files /dev/null and b/bootloaders/stk500v2/stk500boot.o differ diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.eep b/bootloaders/stk500v2/stk500boot_v2_mega2560.eep new file mode 100644 index 0000000..8c230f4 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot_v2_mega2560.eep @@ -0,0 +1,2 @@ +:040000033000E000E9 +:00000001FF diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.elf b/bootloaders/stk500v2/stk500boot_v2_mega2560.elf new file mode 100644 index 0000000..fd66580 Binary files /dev/null and b/bootloaders/stk500v2/stk500boot_v2_mega2560.elf differ diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.hex b/bootloaders/stk500v2/stk500boot_v2_mega2560.hex new file mode 100644 index 0000000..cc5cd63 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot_v2_mega2560.hex @@ -0,0 +1,124 @@ +:020000023000CC +:10E0000095C00000B9C00000B7C00000B5C0000056 +:10E01000B3C00000B1C00000AFC00000ADC0000040 +:10E02000ABC00000A9C00000A7C00000A5C0000050 +:10E03000A3C00000A1C000009FC000009DC0000060 +:10E040009BC0000099C0000097C0000095C0000070 +:10E0500093C0000091C000008FC000008DC0000080 +:10E060008BC0000089C0000087C0000085C0000090 +:10E0700083C0000081C000007FC000007DC00000A0 +:10E080007BC0000079C0000077C0000075C00000B0 +:10E0900073C0000071C000006FC000006DC00000C0 +:10E0A0006BC0000069C0000067C0000065C00000D0 +:10E0B00063C0000061C000005FC000005DC00000E0 +:10E0C0005BC0000059C0000057C0000055C00000F0 +:10E0D00053C0000051C000004FC000004DC0000000 +:10E0E0004BC000006BF171F179F180F184F18CF19A +:10E0F0009DF1DDF1B0F1F4F11EF31EF33EF21EF3DB +:10E100001EF31EF31EF31EF31EF31EF31EF31EF387 +:10E11000B0F104F235F24BF2C5F24BF2C5F21EF348 +:10E1200020F21EF313F208F21EF3B2F111241FBE07 +:10E13000CFEFD1E2DEBFCDBF01E00CBF12E0A0E027 +:10E14000B2E0EAE7F7EE03E00BBF02C007900D92E2 +:10E15000A430B107D9F722E0A4E0B2E001C01D92DB +:10E16000A630B207E1F715D006C301E20EBF0FEFEC +:10E170000DBF11241FBE0DC043CF8093C6008091F8 +:10E18000C00086FFFCCF8091C00080648093C000F7 +:10E190000895CF93DF93CDB7DEB7C052D1400FB60D +:10E1A000F894DEBF0FBECDBF01E20EBF0FEF0DBF73 +:10E1B00094B7F894A89514BE80916000886180930C +:10E1C000600010926000789493FF05C0E091040213 +:10E1D000F091050219958091C00082608093C00083 +:10E1E00080E18093C40088E18093C100000000E0DA +:10E1F00010E0C12CD12C760197EC892E9BE1992E51 +:10E20000A12CB12C24E0722E8091C00087FD25C086 +:10E210000115110511F525E02A95F1F700003FEFF2 +:10E22000C31AD30AE30AF30A91E080E041E2C4167C +:10E230004EE4D406E104F10408F490E0092F182F0D +:10E24000C701B601A501940157D2672B682B692B32 +:10E25000D9F684B3872584BBD7CF0F5F1F4F01301A +:10E26000110509F032C2C25EDE4F1882CE51D14094 +:10E27000D12C212C312C812C912C5401412C512C4E +:10E28000320140E050E020E001301105B1F081E0C2 +:10E2900092E1AAE7B0E03091C00037FD0EC00197CF +:10E2A000A109B109C1F7ABBFFC0187919691019615 +:10E2B00071F3EE27FF270994EACF9091C600E22E72 +:10E2C000F12CF7E0EF16F104D8F5F7018827EE58A6 +:10E2D000FF408E4F33C29B3109F405C200E010E0CD +:10E2E000D3CFD926C25EDE4F9883CE51D14022E0F3 +:10E2F000F5CF492F50E0542F4427D92623E0EECF05 +:10E30000492BD92624E0EACF9E3009F0F0C1D92666 +:10E31000212C312C25E0E2CFB1016F5F7F4FE1E08E +:10E32000F0E0EC0FFD1FE20DF31D9083D926461798 +:10E33000570709F4DEC11B01D1CF9D11D8C102C01E +:10E34000273061F62981822F90E0FC013197ED3171 +:10E35000F10508F073C18827E758FF408E4FEEC1E2 +:10E36000C12C54C09D81903339F48F818823B1F042 +:10E370008130B1F488E917C0892F807599F0903504 +:10E3800021F489E0E0E0F0E005C0983559F4E3E0DD +:10E39000F0E089E080935700849105C08EE103C0CE +:10E3A00081E001C080E01A821B829C831D828E83E3 +:10E3B0001F82C12C07E010E046C11A8288E08B83DF +:10E3C00081E48C8386E58D8382E58E8389E48F8367 +:10E3D00083E5888780E589878FE58A8782E38B8755 +:10E3E000C12C0BE010E02FC1EA81E059E33028F4A2 +:10E3F000F0E0E050FE4F808101C080E01A828B8304 +:10E40000C12C03E010E01FC1CC24C3941A8219C1AF +:10E410008D81882321F0813021F488E924C08EE1A8 +:10E4200022C081E020C01A82E1E0F0E089E0809320 +:10E43000570084918B831C82C12C04E010E003C13F +:10E440008B81803551F48C81883019F4E2E0F0E062 +:10E4500006C089E0E0E0F0E003C0E3E0F0E089E03E +:10E460008093570084911A82E5CF80EC8A83C12C77 +:10E4700002E010E0812C912C5401E5C06A817B817F +:10E480008C819D8163D12B013C01440C551C661C81 +:10E49000771C1A82D5C0EA80FB80FE24EF24FE247C +:10E4A000233109F03CC0811440EC940643E0A406FB +:10E4B000B10478F423E0F401A0925B0020935700AC +:10E4C000E89507B600FCFDCF3FEF931AA30AB30A05 +:10E4D000EACF93018201DE011B968D919D9141E06F +:10E4E0000C01F80120935B0040935700E89511243C +:10E4F0000E5F1F4F2F4F3F4F82E0E81AF10869F778 +:10E5000095E0F20160925B0090935700E89507B6A2 +:10E5100000FCFDCF81E180935700E89532C0D30124 +:10E52000C201B695A795979587956C01FE013B961C +:10E53000C15EDE4FF983E883CF51D14000E010E0A7 +:10E540000E151F05A1F0C15EDE4FE881F981CF51A4 +:10E55000D1406191C15EDE4FF983E883CF51D14054 +:10E56000C8018C0D9D1DF9D00F5F1F4FE9CF20E032 +:10E5700030E0000F111F221F331F040D151D261D33 +:10E58000371D1A82280139015BC08A819B81982737 +:10E59000892798278C010D5F1F4F1A82243101F5BE +:10E5A0008114F0EC9F06F3E0AF06B10408F446C016 +:10E5B000DE0111966BBEF2012791369112962C93D3 +:10E5C0001297FD01349613963C93139722E0420E66 +:10E5D000511C611C711C02971296009759F72BC0B1 +:10E5E000FE0131969F01280F391FB301A20141BDE1 +:10E5F000C52ED62EE72EFF24C2BC4F5F5F4F6F4F54 +:10E600007F4FF89AA0B5A28331962E173F0779F76E +:10E61000AC014150510960E070E04F5F5F4F6F4FB8 +:10E620007F4F440E551E661E771EE3E0F0E0EC0FB0 +:10E63000FD1FE80FF91F1082C12C05C080EC8A83F2 +:10E64000C12C02E010E08BE198DDC25EDE4F8881D4 +:10E65000CE51D14092DD812F90DD802F8EDD85E17E +:10E66000C25EDE4FD880CE51D140D826D0268EE073 +:10E6700084DDD126CE0101967C01080F191F0E15ED +:10E680001F0589F0F70121917F01C15EDE4F2883CC +:10E69000CF51D140822F71DDC15EDE4F3881CF5125 +:10E6A000D140D326ECCF8D2D68DDC25EDE4F488190 +:10E6B000CE51D1404F5FC25EDE4F4883CE51D14034 +:10E6C00000E010E0CC2009F4DCCD00008091C00017 +:10E6D0008D7F8093C00081E180935700E895EE27FD +:10E6E000FF270994FFCF8BE1D82E21E0F7CD20E062 +:10E6F000F5CD1A0126E0F2CDA1E21A2EAA1BBB1B12 +:10E70000FD010DC0AA1FBB1FEE1FFF1FA217B307FD +:10E71000E407F50720F0A21BB30BE40BF50B661F13 +:10E72000771F881F991F1A9469F7609570958095D7 +:10E7300090959B01AC01BD01CF010895EE0FFF1F25 +:10E74000881F8BBF0790F691E02D199469279627B3 +:10E7500069277827872778270895262FF999FECFEC +:10E760001FBA92BD81BD20BD0FB6F894FA9AF99AEE +:0AE770000FBE01960895F894FFCF44 +:04E77A000F020A0080 +:040000033000E000E9 +:00000001FF diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.lss b/bootloaders/stk500v2/stk500boot_v2_mega2560.lss new file mode 100644 index 0000000..1a72fb3 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot_v2_mega2560.lss @@ -0,0 +1,1641 @@ + +stk500boot_v2_mega2560.elf: file format elf32-avr + +Sections: +Idx Name Size VMA LMA File off Algn + 0 .data 00000004 00800200 0003e77a 0000084e 2**0 + CONTENTS, ALLOC, LOAD, DATA + 1 .text 0000077a 0003e000 0003e000 000000d4 2**1 + CONTENTS, ALLOC, LOAD, READONLY, CODE + 2 .bss 00000002 00800204 00800204 00000852 2**0 + ALLOC + 3 .fuse 00000003 00820000 00820000 00000852 2**0 + CONTENTS, ALLOC, LOAD, DATA + 4 .lock 00000001 00830000 00830000 00000855 2**0 + CONTENTS, ALLOC, LOAD, DATA + 5 .comment 00000030 00000000 00000000 00000856 2**0 + CONTENTS, READONLY + 6 .note.gnu.avr.deviceinfo 00000040 00000000 00000000 00000888 2**2 + CONTENTS, READONLY + 7 .debug_aranges 00000058 00000000 00000000 000008c8 2**3 + CONTENTS, READONLY, DEBUGGING + 8 .debug_info 00001367 00000000 00000000 00000920 2**0 + CONTENTS, READONLY, DEBUGGING + 9 .debug_abbrev 00000dac 00000000 00000000 00001c87 2**0 + CONTENTS, READONLY, DEBUGGING + 10 .debug_line 00000988 00000000 00000000 00002a33 2**0 + CONTENTS, READONLY, DEBUGGING + 11 .debug_frame 00000068 00000000 00000000 000033bc 2**2 + CONTENTS, READONLY, DEBUGGING + 12 .debug_str 000006f2 00000000 00000000 00003424 2**0 + CONTENTS, READONLY, DEBUGGING + 13 .debug_loc 00000aa4 00000000 00000000 00003b16 2**0 + CONTENTS, READONLY, DEBUGGING + 14 .debug_ranges 00000058 00000000 00000000 000045ba 2**0 + CONTENTS, READONLY, DEBUGGING + +Disassembly of section .text: + +0003e000 <__vectors>: + 3e000: 95 c0 rjmp .+298 ; 0x3e12c <__ctors_end> + 3e002: 00 00 nop + 3e004: b9 c0 rjmp .+370 ; 0x3e178 <__bad_interrupt> + 3e006: 00 00 nop + 3e008: b7 c0 rjmp .+366 ; 0x3e178 <__bad_interrupt> + 3e00a: 00 00 nop + 3e00c: b5 c0 rjmp .+362 ; 0x3e178 <__bad_interrupt> + 3e00e: 00 00 nop + 3e010: b3 c0 rjmp .+358 ; 0x3e178 <__bad_interrupt> + 3e012: 00 00 nop + 3e014: b1 c0 rjmp .+354 ; 0x3e178 <__bad_interrupt> + 3e016: 00 00 nop + 3e018: af c0 rjmp .+350 ; 0x3e178 <__bad_interrupt> + 3e01a: 00 00 nop + 3e01c: ad c0 rjmp .+346 ; 0x3e178 <__bad_interrupt> + 3e01e: 00 00 nop + 3e020: ab c0 rjmp .+342 ; 0x3e178 <__bad_interrupt> + 3e022: 00 00 nop + 3e024: a9 c0 rjmp .+338 ; 0x3e178 <__bad_interrupt> + 3e026: 00 00 nop + 3e028: a7 c0 rjmp .+334 ; 0x3e178 <__bad_interrupt> + 3e02a: 00 00 nop + 3e02c: a5 c0 rjmp .+330 ; 0x3e178 <__bad_interrupt> + 3e02e: 00 00 nop + 3e030: a3 c0 rjmp .+326 ; 0x3e178 <__bad_interrupt> + 3e032: 00 00 nop + 3e034: a1 c0 rjmp .+322 ; 0x3e178 <__bad_interrupt> + 3e036: 00 00 nop + 3e038: 9f c0 rjmp .+318 ; 0x3e178 <__bad_interrupt> + 3e03a: 00 00 nop + 3e03c: 9d c0 rjmp .+314 ; 0x3e178 <__bad_interrupt> + 3e03e: 00 00 nop + 3e040: 9b c0 rjmp .+310 ; 0x3e178 <__bad_interrupt> + 3e042: 00 00 nop + 3e044: 99 c0 rjmp .+306 ; 0x3e178 <__bad_interrupt> + 3e046: 00 00 nop + 3e048: 97 c0 rjmp .+302 ; 0x3e178 <__bad_interrupt> + 3e04a: 00 00 nop + 3e04c: 95 c0 rjmp .+298 ; 0x3e178 <__bad_interrupt> + 3e04e: 00 00 nop + 3e050: 93 c0 rjmp .+294 ; 0x3e178 <__bad_interrupt> + 3e052: 00 00 nop + 3e054: 91 c0 rjmp .+290 ; 0x3e178 <__bad_interrupt> + 3e056: 00 00 nop + 3e058: 8f c0 rjmp .+286 ; 0x3e178 <__bad_interrupt> + 3e05a: 00 00 nop + 3e05c: 8d c0 rjmp .+282 ; 0x3e178 <__bad_interrupt> + 3e05e: 00 00 nop + 3e060: 8b c0 rjmp .+278 ; 0x3e178 <__bad_interrupt> + 3e062: 00 00 nop + 3e064: 89 c0 rjmp .+274 ; 0x3e178 <__bad_interrupt> + 3e066: 00 00 nop + 3e068: 87 c0 rjmp .+270 ; 0x3e178 <__bad_interrupt> + 3e06a: 00 00 nop + 3e06c: 85 c0 rjmp .+266 ; 0x3e178 <__bad_interrupt> + 3e06e: 00 00 nop + 3e070: 83 c0 rjmp .+262 ; 0x3e178 <__bad_interrupt> + 3e072: 00 00 nop + 3e074: 81 c0 rjmp .+258 ; 0x3e178 <__bad_interrupt> + 3e076: 00 00 nop + 3e078: 7f c0 rjmp .+254 ; 0x3e178 <__bad_interrupt> + 3e07a: 00 00 nop + 3e07c: 7d c0 rjmp .+250 ; 0x3e178 <__bad_interrupt> + 3e07e: 00 00 nop + 3e080: 7b c0 rjmp .+246 ; 0x3e178 <__bad_interrupt> + 3e082: 00 00 nop + 3e084: 79 c0 rjmp .+242 ; 0x3e178 <__bad_interrupt> + 3e086: 00 00 nop + 3e088: 77 c0 rjmp .+238 ; 0x3e178 <__bad_interrupt> + 3e08a: 00 00 nop + 3e08c: 75 c0 rjmp .+234 ; 0x3e178 <__bad_interrupt> + 3e08e: 00 00 nop + 3e090: 73 c0 rjmp .+230 ; 0x3e178 <__bad_interrupt> + 3e092: 00 00 nop + 3e094: 71 c0 rjmp .+226 ; 0x3e178 <__bad_interrupt> + 3e096: 00 00 nop + 3e098: 6f c0 rjmp .+222 ; 0x3e178 <__bad_interrupt> + 3e09a: 00 00 nop + 3e09c: 6d c0 rjmp .+218 ; 0x3e178 <__bad_interrupt> + 3e09e: 00 00 nop + 3e0a0: 6b c0 rjmp .+214 ; 0x3e178 <__bad_interrupt> + 3e0a2: 00 00 nop + 3e0a4: 69 c0 rjmp .+210 ; 0x3e178 <__bad_interrupt> + 3e0a6: 00 00 nop + 3e0a8: 67 c0 rjmp .+206 ; 0x3e178 <__bad_interrupt> + 3e0aa: 00 00 nop + 3e0ac: 65 c0 rjmp .+202 ; 0x3e178 <__bad_interrupt> + 3e0ae: 00 00 nop + 3e0b0: 63 c0 rjmp .+198 ; 0x3e178 <__bad_interrupt> + 3e0b2: 00 00 nop + 3e0b4: 61 c0 rjmp .+194 ; 0x3e178 <__bad_interrupt> + 3e0b6: 00 00 nop + 3e0b8: 5f c0 rjmp .+190 ; 0x3e178 <__bad_interrupt> + 3e0ba: 00 00 nop + 3e0bc: 5d c0 rjmp .+186 ; 0x3e178 <__bad_interrupt> + 3e0be: 00 00 nop + 3e0c0: 5b c0 rjmp .+182 ; 0x3e178 <__bad_interrupt> + 3e0c2: 00 00 nop + 3e0c4: 59 c0 rjmp .+178 ; 0x3e178 <__bad_interrupt> + 3e0c6: 00 00 nop + 3e0c8: 57 c0 rjmp .+174 ; 0x3e178 <__bad_interrupt> + 3e0ca: 00 00 nop + 3e0cc: 55 c0 rjmp .+170 ; 0x3e178 <__bad_interrupt> + 3e0ce: 00 00 nop + 3e0d0: 53 c0 rjmp .+166 ; 0x3e178 <__bad_interrupt> + 3e0d2: 00 00 nop + 3e0d4: 51 c0 rjmp .+162 ; 0x3e178 <__bad_interrupt> + 3e0d6: 00 00 nop + 3e0d8: 4f c0 rjmp .+158 ; 0x3e178 <__bad_interrupt> + 3e0da: 00 00 nop + 3e0dc: 4d c0 rjmp .+154 ; 0x3e178 <__bad_interrupt> + 3e0de: 00 00 nop + 3e0e0: 4b c0 rjmp .+150 ; 0x3e178 <__bad_interrupt> + 3e0e2: 00 00 nop + 3e0e4: 6b f1 brvs .+90 ; 0x3e140 <__do_copy_data+0x4> + 3e0e6: 71 f1 breq .+92 ; 0x3e144 <__do_copy_data+0x8> + 3e0e8: 79 f1 breq .+94 ; 0x3e148 <__do_copy_data+0xc> + 3e0ea: 80 f1 brcs .+96 ; 0x3e14c <__do_copy_data+0x10> + 3e0ec: 84 f1 brlt .+96 ; 0x3e14e <__do_copy_data+0x12> + 3e0ee: 8c f1 brlt .+98 ; 0x3e152 <__do_copy_data+0x16> + 3e0f0: 9d f1 brhs .+102 ; 0x3e158 <__do_clear_bss+0x2> + 3e0f2: dd f1 brhs .+118 ; 0x3e16a <__jumpMain> + 3e0f4: b0 f1 brcs .+108 ; 0x3e162 <.do_clear_bss_start+0x2> + 3e0f6: f4 f1 brlt .+124 ; 0x3e174 <__jumpMain+0xa> + 3e0f8: 1e f3 brts .-58 ; 0x3e0c0 <__vectors+0xc0> + 3e0fa: 1e f3 brts .-58 ; 0x3e0c2 <__vectors+0xc2> + 3e0fc: 3e f2 brts .-114 ; 0x3e08c <__vectors+0x8c> + 3e0fe: 1e f3 brts .-58 ; 0x3e0c6 <__vectors+0xc6> + 3e100: 1e f3 brts .-58 ; 0x3e0c8 <__vectors+0xc8> + 3e102: 1e f3 brts .-58 ; 0x3e0ca <__vectors+0xca> + 3e104: 1e f3 brts .-58 ; 0x3e0cc <__vectors+0xcc> + 3e106: 1e f3 brts .-58 ; 0x3e0ce <__vectors+0xce> + 3e108: 1e f3 brts .-58 ; 0x3e0d0 <__vectors+0xd0> + 3e10a: 1e f3 brts .-58 ; 0x3e0d2 <__vectors+0xd2> + 3e10c: 1e f3 brts .-58 ; 0x3e0d4 <__vectors+0xd4> + 3e10e: 1e f3 brts .-58 ; 0x3e0d6 <__vectors+0xd6> + 3e110: b0 f1 brcs .+108 ; 0x3e17e + 3e112: 04 f2 brlt .-128 ; 0x3e094 <__vectors+0x94> + 3e114: 35 f2 brhs .-116 ; 0x3e0a2 <__vectors+0xa2> + 3e116: 4b f2 brvs .-110 ; 0x3e0aa <__vectors+0xaa> + 3e118: c5 f2 brhs .-80 ; 0x3e0ca <__vectors+0xca> + 3e11a: 4b f2 brvs .-110 ; 0x3e0ae <__vectors+0xae> + 3e11c: c5 f2 brhs .-80 ; 0x3e0ce <__vectors+0xce> + 3e11e: 1e f3 brts .-58 ; 0x3e0e6 <__vectors+0xe6> + 3e120: 20 f2 brcs .-120 ; 0x3e0aa <__vectors+0xaa> + 3e122: 1e f3 brts .-58 ; 0x3e0ea <__vectors+0xea> + 3e124: 13 f2 brvs .-124 ; 0x3e0aa <__vectors+0xaa> + 3e126: 08 f2 brcs .-126 ; 0x3e0aa <__vectors+0xaa> + 3e128: 1e f3 brts .-58 ; 0x3e0f0 <__vectors+0xf0> + 3e12a: b2 f1 brmi .+108 ; 0x3e198 + +0003e12c <__ctors_end>: + 3e12c: 11 24 eor r1, r1 + 3e12e: 1f be out 0x3f, r1 ; 63 + 3e130: cf ef ldi r28, 0xFF ; 255 + 3e132: d1 e2 ldi r29, 0x21 ; 33 + 3e134: de bf out 0x3e, r29 ; 62 + 3e136: cd bf out 0x3d, r28 ; 61 + 3e138: 01 e0 ldi r16, 0x01 ; 1 + 3e13a: 0c bf out 0x3c, r16 ; 60 + +0003e13c <__do_copy_data>: + 3e13c: 12 e0 ldi r17, 0x02 ; 2 + 3e13e: a0 e0 ldi r26, 0x00 ; 0 + 3e140: b2 e0 ldi r27, 0x02 ; 2 + 3e142: ea e7 ldi r30, 0x7A ; 122 + 3e144: f7 ee ldi r31, 0xE7 ; 231 + 3e146: 03 e0 ldi r16, 0x03 ; 3 + 3e148: 0b bf out 0x3b, r16 ; 59 + 3e14a: 02 c0 rjmp .+4 ; 0x3e150 <__do_copy_data+0x14> + 3e14c: 07 90 elpm r0, Z+ + 3e14e: 0d 92 st X+, r0 + 3e150: a4 30 cpi r26, 0x04 ; 4 + 3e152: b1 07 cpc r27, r17 + 3e154: d9 f7 brne .-10 ; 0x3e14c <__do_copy_data+0x10> + +0003e156 <__do_clear_bss>: + 3e156: 22 e0 ldi r18, 0x02 ; 2 + 3e158: a4 e0 ldi r26, 0x04 ; 4 + 3e15a: b2 e0 ldi r27, 0x02 ; 2 + 3e15c: 01 c0 rjmp .+2 ; 0x3e160 <.do_clear_bss_start> + +0003e15e <.do_clear_bss_loop>: + 3e15e: 1d 92 st X+, r1 + +0003e160 <.do_clear_bss_start>: + 3e160: a6 30 cpi r26, 0x06 ; 6 + 3e162: b2 07 cpc r27, r18 + 3e164: e1 f7 brne .-8 ; 0x3e15e <.do_clear_bss_loop> + 3e166: 15 d0 rcall .+42 ; 0x3e192
+ 3e168: 06 c3 rjmp .+1548 ; 0x3e776 <_exit> + +0003e16a <__jumpMain>: + 3e16a: 01 e2 ldi r16, 0x21 ; 33 + unsigned int i; + for (i=0;i + +0003e178 <__bad_interrupt>: + 3e178: 43 cf rjmp .-378 ; 0x3e000 <__vectors> + +0003e17a : + 3e17a: 80 93 c6 00 sts 0x00C6, r24 ; 0x8000c6 <__TEXT_REGION_LENGTH__+0x7c00c6> + 3e17e: 80 91 c0 00 lds r24, 0x00C0 ; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7c00c0> + 3e182: 86 ff sbrs r24, 6 + 3e184: fc cf rjmp .-8 ; 0x3e17e + 3e186: 80 91 c0 00 lds r24, 0x00C0 ; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7c00c0> + 3e18a: 80 64 ori r24, 0x40 ; 64 + 3e18c: 80 93 c0 00 sts 0x00C0, r24 ; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7c00c0> + 3e190: 08 95 ret + +0003e192
: +void (*app_start)(void) = 0x0000; + + +//***************************************************************************** +int main(void) +{ + 3e192: cf 93 push r28 + 3e194: df 93 push r29 + 3e196: cd b7 in r28, 0x3d ; 61 + 3e198: de b7 in r29, 0x3e ; 62 + 3e19a: c0 52 subi r28, 0x20 ; 32 + 3e19c: d1 40 sbci r29, 0x01 ; 1 + 3e19e: 0f b6 in r0, 0x3f ; 63 + 3e1a0: f8 94 cli + 3e1a2: de bf out 0x3e, r29 ; 62 + 3e1a4: 0f be out 0x3f, r0 ; 63 + 3e1a6: cd bf out 0x3d, r28 ; 61 + unsigned int rcvdCharCntr = 0; +#endif + + //* some chips dont set the stack properly + asm volatile ( ".set __stack, %0" :: "i" (RAMEND) ); + asm volatile ( "ldi 16, %0" :: "i" (RAMEND >> 8) ); + 3e1a8: 01 e2 ldi r16, 0x21 ; 33 + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_HI_ADDR) ); + 3e1aa: 0e bf out 0x3e, r16 ; 62 + asm volatile ( "ldi 16, %0" :: "i" (RAMEND & 0x0ff) ); + 3e1ac: 0f ef ldi r16, 0xFF ; 255 + asm volatile ( "out %0,16" :: "i" (AVR_STACK_POINTER_LO_ADDR) ); + 3e1ae: 0d bf out 0x3d, r16 ; 61 +#ifdef _FIX_ISSUE_181_ + //************************************************************************ + //* Dec 29, 2011 Issue #181, added watch dog timmer support + //* handle the watch dog timer + uint8_t mcuStatusReg; + mcuStatusReg = MCUSR; + 3e1b0: 94 b7 in r25, 0x34 ; 52 + + __asm__ __volatile__ ("cli"); + 3e1b2: f8 94 cli + __asm__ __volatile__ ("wdr"); + 3e1b4: a8 95 wdr + MCUSR = 0; + 3e1b6: 14 be out 0x34, r1 ; 52 + WDTCSR |= _BV(WDCE) | _BV(WDE); + 3e1b8: 80 91 60 00 lds r24, 0x0060 ; 0x800060 <__TEXT_REGION_LENGTH__+0x7c0060> + 3e1bc: 88 61 ori r24, 0x18 ; 24 + 3e1be: 80 93 60 00 sts 0x0060, r24 ; 0x800060 <__TEXT_REGION_LENGTH__+0x7c0060> + WDTCSR = 0; + 3e1c2: 10 92 60 00 sts 0x0060, r1 ; 0x800060 <__TEXT_REGION_LENGTH__+0x7c0060> + __asm__ __volatile__ ("sei"); + 3e1c6: 78 94 sei + // check if WDT generated the reset, if so, go straight to app + if (mcuStatusReg & _BV(WDRF)) + 3e1c8: 93 ff sbrs r25, 3 + 3e1ca: 05 c0 rjmp .+10 ; 0x3e1d6 + { + app_start(); + 3e1cc: e0 91 04 02 lds r30, 0x0204 ; 0x800204 <__data_end> + 3e1d0: f0 91 05 02 lds r31, 0x0205 ; 0x800205 <__data_end+0x1> + 3e1d4: 19 95 eicall + /* + * Init UART + * set baudrate and enable USART receiver and transmiter without interrupts + */ +#if UART_BAUDRATE_DOUBLE_SPEED + UART_STATUS_REG |= (1 < + 3e1da: 82 60 ori r24, 0x02 ; 2 + 3e1dc: 80 93 c0 00 sts 0x00C0, r24 ; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7c00c0> +#endif + UART_BAUD_RATE_LOW = UART_BAUD_SELECT(BAUDRATE,F_CPU); + 3e1e0: 80 e1 ldi r24, 0x10 ; 16 + 3e1e2: 80 93 c4 00 sts 0x00C4, r24 ; 0x8000c4 <__TEXT_REGION_LENGTH__+0x7c00c4> + UART_CONTROL_REG = (1 << UART_ENABLE_RECEIVER) | (1 << UART_ENABLE_TRANSMITTER); + 3e1e6: 88 e1 ldi r24, 0x18 ; 24 + 3e1e8: 80 93 c1 00 sts 0x00C1, r24 ; 0x8000c1 <__TEXT_REGION_LENGTH__+0x7c00c1> + + asm volatile ("nop"); // wait until port has changed + 3e1ec: 00 00 nop + 3e1ee: 00 e0 ldi r16, 0x00 ; 0 + 3e1f0: 10 e0 ldi r17, 0x00 ; 0 + 3e1f2: c1 2c mov r12, r1 + 3e1f4: d1 2c mov r13, r1 + 3e1f6: 76 01 movw r14, r12 + if (boot_timer > boot_timeout) + { + boot_state = 1; // (after ++ -> boot_state=2 bootloader timeout, jump to main 0x00000 ) + } + #ifdef BLINK_LED_WHILE_WAITING + if ((boot_timer % _BLINK_LOOP_COUNT_) == 0) + 3e1f8: 97 ec ldi r25, 0xC7 ; 199 + 3e1fa: 89 2e mov r8, r25 + 3e1fc: 9b e1 ldi r25, 0x1B ; 27 + 3e1fe: 99 2e mov r9, r25 + 3e200: a1 2c mov r10, r1 + 3e202: b1 2c mov r11, r1 + { + //* toggle the LED + PROGLED_PORT ^= (1< + delay_ms(100); +#endif + + while (boot_state==0) + { + while ((!(Serial_Available())) && (boot_state == 0)) // wait for data + 3e20c: 87 fd sbrc r24, 7 + 3e20e: 25 c0 rjmp .+74 ; 0x3e25a + 3e210: 01 15 cp r16, r1 + 3e212: 11 05 cpc r17, r1 + 3e214: 11 f5 brne .+68 ; 0x3e25a + #else + //round up by default + __ticks_dc = (uint32_t)(ceil(fabs(__tmp))); + #endif + + __builtin_avr_delay_cycles(__ticks_dc); + 3e216: 25 e0 ldi r18, 0x05 ; 5 + 3e218: 2a 95 dec r18 + 3e21a: f1 f7 brne .-4 ; 0x3e218 + 3e21c: 00 00 nop + { + _delay_ms(0.001); + boot_timer++; + 3e21e: 3f ef ldi r19, 0xFF ; 255 + 3e220: c3 1a sub r12, r19 + 3e222: d3 0a sbc r13, r19 + 3e224: e3 0a sbc r14, r19 + 3e226: f3 0a sbc r15, r19 + if (boot_timer > boot_timeout) + { + boot_state = 1; // (after ++ -> boot_state=2 bootloader timeout, jump to main 0x00000 ) + 3e228: 91 e0 ldi r25, 0x01 ; 1 + 3e22a: 80 e0 ldi r24, 0x00 ; 0 + 3e22c: 41 e2 ldi r20, 0x21 ; 33 + 3e22e: c4 16 cp r12, r20 + 3e230: 4e e4 ldi r20, 0x4E ; 78 + 3e232: d4 06 cpc r13, r20 + 3e234: e1 04 cpc r14, r1 + 3e236: f1 04 cpc r15, r1 + 3e238: 08 f4 brcc .+2 ; 0x3e23c + 3e23a: 90 e0 ldi r25, 0x00 ; 0 + 3e23c: 09 2f mov r16, r25 + 3e23e: 18 2f mov r17, r24 + } + #ifdef BLINK_LED_WHILE_WAITING + if ((boot_timer % _BLINK_LOOP_COUNT_) == 0) + 3e240: c7 01 movw r24, r14 + 3e242: b6 01 movw r22, r12 + 3e244: a5 01 movw r20, r10 + 3e246: 94 01 movw r18, r8 + 3e248: 57 d2 rcall .+1198 ; 0x3e6f8 <__udivmodsi4> + 3e24a: 67 2b or r22, r23 + 3e24c: 68 2b or r22, r24 + 3e24e: 69 2b or r22, r25 + 3e250: d9 f6 brne .-74 ; 0x3e208 + { + //* toggle the LED + PROGLED_PORT ^= (1< + } + #endif + } + boot_state++; // ( if boot_state=1 bootloader received byte from UART, enter bootloader mode) + 3e25a: 0f 5f subi r16, 0xFF ; 255 + 3e25c: 1f 4f sbci r17, 0xFF ; 255 + } + + + if (boot_state==1) + 3e25e: 01 30 cpi r16, 0x01 ; 1 + 3e260: 11 05 cpc r17, r1 + 3e262: 09 f0 breq .+2 ; 0x3e266 + 3e264: 32 c2 rjmp .+1124 ; 0x3e6ca + 3e266: c2 5e subi r28, 0xE2 ; 226 + 3e268: de 4f sbci r29, 0xFE ; 254 + 3e26a: 18 82 st Y, r1 + 3e26c: ce 51 subi r28, 0x1E ; 30 + 3e26e: d1 40 sbci r29, 0x01 ; 1 + 3e270: d1 2c mov r13, r1 + 3e272: 21 2c mov r2, r1 + 3e274: 31 2c mov r3, r1 + 3e276: 81 2c mov r8, r1 + 3e278: 91 2c mov r9, r1 + 3e27a: 54 01 movw r10, r8 + 3e27c: 41 2c mov r4, r1 + 3e27e: 51 2c mov r5, r1 + 3e280: 32 01 movw r6, r4 + UART_STATUS_REG |= (1 < + UART_STATUS_REG |= (1 < + 3e29a: 37 fd sbrc r19, 7 + 3e29c: 0e c0 rjmp .+28 ; 0x3e2ba + 3e29e: 01 97 sbiw r24, 0x01 ; 1 + 3e2a0: a1 09 sbc r26, r1 + 3e2a2: b1 09 sbc r27, r1 + { + // wait for data + count++; + if (count > MAX_TIME_COUNT) + 3e2a4: c1 f7 brne .-16 ; 0x3e296 + { + unsigned int data; + #if (FLASHEND > 0x10000) + data = pgm_read_word_far(0); //* get the first word of the user program + 3e2a6: ab bf out 0x3b, r26 ; 59 + 3e2a8: fc 01 movw r30, r24 + 3e2aa: 87 91 elpm r24, Z+ + 3e2ac: 96 91 elpm r25, Z + #else + data = pgm_read_word_near(0); //* get the first word of the user program + #endif + if (data != 0xffff) //* make sure its valid before jumping to it. + 3e2ae: 01 96 adiw r24, 0x01 ; 1 + 3e2b0: 71 f3 breq .-36 ; 0x3e28e + { + asm volatile( + 3e2b2: ee 27 eor r30, r30 + 3e2b4: ff 27 eor r31, r31 + 3e2b6: 09 94 ijmp + 3e2b8: ea cf rjmp .-44 ; 0x3e28e + ); + } + count = 0; + } + } + return UART_DATA_REG; + 3e2ba: 90 91 c6 00 lds r25, 0x00C6 ; 0x8000c6 <__TEXT_REGION_LENGTH__+0x7c00c6> + // c = recchar(); + c = recchar_timeout(); + + } + + switch (msgParseState) + 3e2be: e2 2e mov r14, r18 + 3e2c0: f1 2c mov r15, r1 + 3e2c2: f7 e0 ldi r31, 0x07 ; 7 + 3e2c4: ef 16 cp r14, r31 + 3e2c6: f1 04 cpc r15, r1 + 3e2c8: d8 f5 brcc .+118 ; 0x3e340 + 3e2ca: f7 01 movw r30, r14 + 3e2cc: 88 27 eor r24, r24 + 3e2ce: ee 58 subi r30, 0x8E ; 142 + 3e2d0: ff 40 sbci r31, 0x0F ; 15 + 3e2d2: 8e 4f sbci r24, 0xFE ; 254 + 3e2d4: 33 c2 rjmp .+1126 ; 0x3e73c <__tablejump2__> + { + case ST_START: + if ( c == MESSAGE_START ) + 3e2d6: 9b 31 cpi r25, 0x1B ; 27 + 3e2d8: 09 f4 brne .+2 ; 0x3e2dc + 3e2da: 05 c2 rjmp .+1034 ; 0x3e6e6 + 3e2dc: 00 e0 ldi r16, 0x00 ; 0 + 3e2de: 10 e0 ldi r17, 0x00 ; 0 + 3e2e0: d3 cf rjmp .-90 ; 0x3e288 + + case ST_GET_SEQ_NUM: + #ifdef _FIX_ISSUE_505_ + seqNum = c; + msgParseState = ST_MSG_SIZE_1; + checksum ^= c; + 3e2e2: d9 26 eor r13, r25 + else + { + msgParseState = ST_START; + } + #endif + break; + 3e2e4: c2 5e subi r28, 0xE2 ; 226 + 3e2e6: de 4f sbci r29, 0xFE ; 254 + 3e2e8: 98 83 st Y, r25 + 3e2ea: ce 51 subi r28, 0x1E ; 30 + 3e2ec: d1 40 sbci r29, 0x01 ; 1 + break; + + case ST_GET_SEQ_NUM: + #ifdef _FIX_ISSUE_505_ + seqNum = c; + msgParseState = ST_MSG_SIZE_1; + 3e2ee: 22 e0 ldi r18, 0x02 ; 2 + else + { + msgParseState = ST_START; + } + #endif + break; + 3e2f0: f5 cf rjmp .-22 ; 0x3e2dc + + case ST_MSG_SIZE_1: + msgLength = c<<8; + 3e2f2: 49 2f mov r20, r25 + 3e2f4: 50 e0 ldi r21, 0x00 ; 0 + 3e2f6: 54 2f mov r21, r20 + 3e2f8: 44 27 eor r20, r20 + msgParseState = ST_MSG_SIZE_2; + checksum ^= c; + 3e2fa: d9 26 eor r13, r25 + #endif + break; + + case ST_MSG_SIZE_1: + msgLength = c<<8; + msgParseState = ST_MSG_SIZE_2; + 3e2fc: 23 e0 ldi r18, 0x03 ; 3 + checksum ^= c; + break; + 3e2fe: ee cf rjmp .-36 ; 0x3e2dc + + case ST_MSG_SIZE_2: + msgLength |= c; + 3e300: 49 2b or r20, r25 + msgParseState = ST_GET_TOKEN; + checksum ^= c; + 3e302: d9 26 eor r13, r25 + checksum ^= c; + break; + + case ST_MSG_SIZE_2: + msgLength |= c; + msgParseState = ST_GET_TOKEN; + 3e304: 24 e0 ldi r18, 0x04 ; 4 + checksum ^= c; + break; + 3e306: ea cf rjmp .-44 ; 0x3e2dc + + case ST_GET_TOKEN: + if ( c == TOKEN ) + 3e308: 9e 30 cpi r25, 0x0E ; 14 + 3e30a: 09 f0 breq .+2 ; 0x3e30e + 3e30c: f0 c1 rjmp .+992 ; 0x3e6ee + { + msgParseState = ST_GET_DATA; + checksum ^= c; + 3e30e: d9 26 eor r13, r25 + ii = 0; + 3e310: 21 2c mov r2, r1 + 3e312: 31 2c mov r3, r1 + break; + + case ST_GET_TOKEN: + if ( c == TOKEN ) + { + msgParseState = ST_GET_DATA; + 3e314: 25 e0 ldi r18, 0x05 ; 5 + 3e316: e2 cf rjmp .-60 ; 0x3e2dc + msgParseState = ST_START; + } + break; + + case ST_GET_DATA: + msgBuffer[ii++] = c; + 3e318: b1 01 movw r22, r2 + 3e31a: 6f 5f subi r22, 0xFF ; 255 + 3e31c: 7f 4f sbci r23, 0xFF ; 255 + 3e31e: e1 e0 ldi r30, 0x01 ; 1 + 3e320: f0 e0 ldi r31, 0x00 ; 0 + 3e322: ec 0f add r30, r28 + 3e324: fd 1f adc r31, r29 + 3e326: e2 0d add r30, r2 + 3e328: f3 1d adc r31, r3 + 3e32a: 90 83 st Z, r25 + checksum ^= c; + 3e32c: d9 26 eor r13, r25 + if (ii == msgLength ) + 3e32e: 46 17 cp r20, r22 + 3e330: 57 07 cpc r21, r23 + 3e332: 09 f4 brne .+2 ; 0x3e336 + 3e334: de c1 rjmp .+956 ; 0x3e6f2 + 3e336: 1b 01 movw r2, r22 + 3e338: d1 cf rjmp .-94 ; 0x3e2dc + msgParseState = ST_GET_CHECK; + } + break; + + case ST_GET_CHECK: + if ( c == checksum ) + 3e33a: 9d 11 cpse r25, r13 + 3e33c: d8 c1 rjmp .+944 ; 0x3e6ee + 3e33e: 02 c0 rjmp .+4 ; 0x3e344 + { + /* + * Collect received bytes to a complete message + */ + msgParseState = ST_START; + while ( msgParseState != ST_PROCESS ) + 3e340: 27 30 cpi r18, 0x07 ; 7 + 3e342: 61 f6 brne .-104 ; 0x3e2dc + + /* + * Now process the STK500 commands, see Atmel Appnote AVR068 + */ + + switch (msgBuffer[0]) + 3e344: 29 81 ldd r18, Y+1 ; 0x01 + 3e346: 82 2f mov r24, r18 + 3e348: 90 e0 ldi r25, 0x00 ; 0 + 3e34a: fc 01 movw r30, r24 + 3e34c: 31 97 sbiw r30, 0x01 ; 1 + 3e34e: ed 31 cpi r30, 0x1D ; 29 + 3e350: f1 05 cpc r31, r1 + 3e352: 08 f0 brcs .+2 ; 0x3e356 + 3e354: 73 c1 rjmp .+742 ; 0x3e63c + 3e356: 88 27 eor r24, r24 + 3e358: e7 58 subi r30, 0x87 ; 135 + 3e35a: ff 40 sbci r31, 0x0F ; 15 + 3e35c: 8e 4f sbci r24, 0xFE ; 254 + 3e35e: ee c1 rjmp .+988 ; 0x3e73c <__tablejump2__> + 3e360: c1 2c mov r12, r1 + 3e362: 54 c0 rjmp .+168 ; 0x3e40c + case CMD_SPI_MULTI: + { + unsigned char answerByte; + unsigned char flag=0; + + if ( msgBuffer[4]== 0x30 ) + 3e364: 9d 81 ldd r25, Y+5 ; 0x05 + 3e366: 90 33 cpi r25, 0x30 ; 48 + 3e368: 39 f4 brne .+14 ; 0x3e378 + { + unsigned char signatureIndex = msgBuffer[6]; + 3e36a: 8f 81 ldd r24, Y+7 ; 0x07 + + if ( signatureIndex == 0 ) + 3e36c: 88 23 and r24, r24 + 3e36e: b1 f0 breq .+44 ; 0x3e39c + { + answerByte = (SIGNATURE_BYTES >> 16) & 0x000000FF; + } + else if ( signatureIndex == 1 ) + 3e370: 81 30 cpi r24, 0x01 ; 1 + 3e372: b1 f4 brne .+44 ; 0x3e3a0 + { + answerByte = (SIGNATURE_BYTES >> 8) & 0x000000FF; + 3e374: 88 e9 ldi r24, 0x98 ; 152 + 3e376: 17 c0 rjmp .+46 ; 0x3e3a6 + else + { + answerByte = SIGNATURE_BYTES & 0x000000FF; + } + } + else if ( msgBuffer[4] & 0x50 ) + 3e378: 89 2f mov r24, r25 + 3e37a: 80 75 andi r24, 0x50 ; 80 + 3e37c: 99 f0 breq .+38 ; 0x3e3a4 + //* Issue 544: stk500v2 bootloader doesn't support reading fuses + //* I cant find the docs that say what these are supposed to be but this was figured out by trial and error + // answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS); + // answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); + // answerByte = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS); + if (msgBuffer[4] == 0x50) + 3e37e: 90 35 cpi r25, 0x50 ; 80 + 3e380: 21 f4 brne .+8 ; 0x3e38a + { + answerByte = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS); + 3e382: 89 e0 ldi r24, 0x09 ; 9 + 3e384: e0 e0 ldi r30, 0x00 ; 0 + 3e386: f0 e0 ldi r31, 0x00 ; 0 + 3e388: 05 c0 rjmp .+10 ; 0x3e394 + } + else if (msgBuffer[4] == 0x58) + 3e38a: 98 35 cpi r25, 0x58 ; 88 + 3e38c: 59 f4 brne .+22 ; 0x3e3a4 + { + answerByte = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS); + 3e38e: e3 e0 ldi r30, 0x03 ; 3 + 3e390: f0 e0 ldi r31, 0x00 ; 0 + 3e392: 89 e0 ldi r24, 0x09 ; 9 + 3e394: 80 93 57 00 sts 0x0057, r24 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e398: 84 91 lpm r24, Z + 3e39a: 05 c0 rjmp .+10 ; 0x3e3a6 + { + unsigned char signatureIndex = msgBuffer[6]; + + if ( signatureIndex == 0 ) + { + answerByte = (SIGNATURE_BYTES >> 16) & 0x000000FF; + 3e39c: 8e e1 ldi r24, 0x1E ; 30 + 3e39e: 03 c0 rjmp .+6 ; 0x3e3a6 + { + answerByte = (SIGNATURE_BYTES >> 8) & 0x000000FF; + } + else + { + answerByte = SIGNATURE_BYTES & 0x000000FF; + 3e3a0: 81 e0 ldi r24, 0x01 ; 1 + 3e3a2: 01 c0 rjmp .+2 ; 0x3e3a6 + answerByte = 0; + } + } + else + { + answerByte = 0; // for all others command are not implemented, return dummy value for AVRDUDE happy + 3e3a4: 80 e0 ldi r24, 0x00 ; 0 + } + if ( !flag ) + { + msgLength = 7; + msgBuffer[1] = STATUS_CMD_OK; + 3e3a6: 1a 82 std Y+2, r1 ; 0x02 + msgBuffer[2] = 0; + 3e3a8: 1b 82 std Y+3, r1 ; 0x03 + msgBuffer[3] = msgBuffer[4]; + 3e3aa: 9c 83 std Y+4, r25 ; 0x04 + msgBuffer[4] = 0; + 3e3ac: 1d 82 std Y+5, r1 ; 0x05 + msgBuffer[5] = answerByte; + 3e3ae: 8e 83 std Y+6, r24 ; 0x06 + msgBuffer[6] = STATUS_CMD_OK; + 3e3b0: 1f 82 std Y+7, r1 ; 0x07 + 3e3b2: c1 2c mov r12, r1 + { + answerByte = 0; // for all others command are not implemented, return dummy value for AVRDUDE happy + } + if ( !flag ) + { + msgLength = 7; + 3e3b4: 07 e0 ldi r16, 0x07 ; 7 + 3e3b6: 10 e0 ldi r17, 0x00 ; 0 + 3e3b8: 46 c1 rjmp .+652 ; 0x3e646 + } + break; + #endif + case CMD_SIGN_ON: + msgLength = 11; + msgBuffer[1] = STATUS_CMD_OK; + 3e3ba: 1a 82 std Y+2, r1 ; 0x02 + msgBuffer[2] = 8; + 3e3bc: 88 e0 ldi r24, 0x08 ; 8 + 3e3be: 8b 83 std Y+3, r24 ; 0x03 + msgBuffer[3] = 'A'; + 3e3c0: 81 e4 ldi r24, 0x41 ; 65 + 3e3c2: 8c 83 std Y+4, r24 ; 0x04 + msgBuffer[4] = 'V'; + 3e3c4: 86 e5 ldi r24, 0x56 ; 86 + 3e3c6: 8d 83 std Y+5, r24 ; 0x05 + msgBuffer[5] = 'R'; + 3e3c8: 82 e5 ldi r24, 0x52 ; 82 + 3e3ca: 8e 83 std Y+6, r24 ; 0x06 + msgBuffer[6] = 'I'; + 3e3cc: 89 e4 ldi r24, 0x49 ; 73 + 3e3ce: 8f 83 std Y+7, r24 ; 0x07 + msgBuffer[7] = 'S'; + 3e3d0: 83 e5 ldi r24, 0x53 ; 83 + 3e3d2: 88 87 std Y+8, r24 ; 0x08 + msgBuffer[8] = 'P'; + 3e3d4: 80 e5 ldi r24, 0x50 ; 80 + 3e3d6: 89 87 std Y+9, r24 ; 0x09 + msgBuffer[9] = '_'; + 3e3d8: 8f e5 ldi r24, 0x5F ; 95 + 3e3da: 8a 87 std Y+10, r24 ; 0x0a + msgBuffer[10] = '2'; + 3e3dc: 82 e3 ldi r24, 0x32 ; 50 + 3e3de: 8b 87 std Y+11, r24 ; 0x0b + break; + 3e3e0: c1 2c mov r12, r1 + } + } + break; + #endif + case CMD_SIGN_ON: + msgLength = 11; + 3e3e2: 0b e0 ldi r16, 0x0B ; 11 + 3e3e4: 10 e0 ldi r17, 0x00 ; 0 + msgBuffer[6] = 'I'; + msgBuffer[7] = 'S'; + msgBuffer[8] = 'P'; + msgBuffer[9] = '_'; + msgBuffer[10] = '2'; + break; + 3e3e6: 2f c1 rjmp .+606 ; 0x3e646 + 3e3e8: ea 81 ldd r30, Y+2 ; 0x02 + 3e3ea: e0 59 subi r30, 0x90 ; 144 + 3e3ec: e3 30 cpi r30, 0x03 ; 3 + 3e3ee: 28 f4 brcc .+10 ; 0x3e3fa + 3e3f0: f0 e0 ldi r31, 0x00 ; 0 + 3e3f2: e0 50 subi r30, 0x00 ; 0 + 3e3f4: fe 4f sbci r31, 0xFE ; 254 + 3e3f6: 80 81 ld r24, Z + 3e3f8: 01 c0 rjmp .+2 ; 0x3e3fc + + case CMD_GET_PARAMETER: + { + unsigned char value; + + switch(msgBuffer[1]) + 3e3fa: 80 e0 ldi r24, 0x00 ; 0 + default: + value = 0; + break; + } + msgLength = 3; + msgBuffer[1] = STATUS_CMD_OK; + 3e3fc: 1a 82 std Y+2, r1 ; 0x02 + msgBuffer[2] = value; + 3e3fe: 8b 83 std Y+3, r24 ; 0x03 + } + break; + 3e400: c1 2c mov r12, r1 + break; + default: + value = 0; + break; + } + msgLength = 3; + 3e402: 03 e0 ldi r16, 0x03 ; 3 + 3e404: 10 e0 ldi r17, 0x00 ; 0 + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = value; + } + break; + 3e406: 1f c1 rjmp .+574 ; 0x3e646 + + case CMD_LEAVE_PROGMODE_ISP: + isLeave = 1; + 3e408: cc 24 eor r12, r12 + 3e40a: c3 94 inc r12 + //* fall thru + + case CMD_SET_PARAMETER: + case CMD_ENTER_PROGMODE_ISP: + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + 3e40c: 1a 82 std Y+2, r1 ; 0x02 + 3e40e: 19 c1 rjmp .+562 ; 0x3e642 + break; + + case CMD_READ_SIGNATURE_ISP: + { + unsigned char signatureIndex = msgBuffer[4]; + 3e410: 8d 81 ldd r24, Y+5 ; 0x05 + unsigned char signature; + + if ( signatureIndex == 0 ) + 3e412: 88 23 and r24, r24 + 3e414: 21 f0 breq .+8 ; 0x3e41e + signature = (SIGNATURE_BYTES >>16) & 0x000000FF; + else if ( signatureIndex == 1 ) + 3e416: 81 30 cpi r24, 0x01 ; 1 + 3e418: 21 f4 brne .+8 ; 0x3e422 + signature = (SIGNATURE_BYTES >> 8) & 0x000000FF; + 3e41a: 88 e9 ldi r24, 0x98 ; 152 + 3e41c: 24 c0 rjmp .+72 ; 0x3e466 + { + unsigned char signatureIndex = msgBuffer[4]; + unsigned char signature; + + if ( signatureIndex == 0 ) + signature = (SIGNATURE_BYTES >>16) & 0x000000FF; + 3e41e: 8e e1 ldi r24, 0x1E ; 30 + 3e420: 22 c0 rjmp .+68 ; 0x3e466 + else if ( signatureIndex == 1 ) + signature = (SIGNATURE_BYTES >> 8) & 0x000000FF; + else + signature = SIGNATURE_BYTES & 0x000000FF; + 3e422: 81 e0 ldi r24, 0x01 ; 1 + 3e424: 20 c0 rjmp .+64 ; 0x3e466 + } + break; + + case CMD_READ_LOCK_ISP: + msgLength = 4; + msgBuffer[1] = STATUS_CMD_OK; + 3e426: 1a 82 std Y+2, r1 ; 0x02 + msgBuffer[2] = boot_lock_fuse_bits_get( GET_LOCK_BITS ); + 3e428: e1 e0 ldi r30, 0x01 ; 1 + 3e42a: f0 e0 ldi r31, 0x00 ; 0 + 3e42c: 89 e0 ldi r24, 0x09 ; 9 + 3e42e: 80 93 57 00 sts 0x0057, r24 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e432: 84 91 lpm r24, Z + 3e434: 8b 83 std Y+3, r24 ; 0x03 + msgBuffer[3] = STATUS_CMD_OK; + 3e436: 1c 82 std Y+4, r1 ; 0x04 + break; + 3e438: c1 2c mov r12, r1 + msgBuffer[3] = STATUS_CMD_OK; + } + break; + + case CMD_READ_LOCK_ISP: + msgLength = 4; + 3e43a: 04 e0 ldi r16, 0x04 ; 4 + 3e43c: 10 e0 ldi r17, 0x00 ; 0 + msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[2] = boot_lock_fuse_bits_get( GET_LOCK_BITS ); + msgBuffer[3] = STATUS_CMD_OK; + break; + 3e43e: 03 c1 rjmp .+518 ; 0x3e646 + + case CMD_READ_FUSE_ISP: + { + unsigned char fuseBits; + + if ( msgBuffer[2] == 0x50 ) + 3e440: 8b 81 ldd r24, Y+3 ; 0x03 + 3e442: 80 35 cpi r24, 0x50 ; 80 + 3e444: 51 f4 brne .+20 ; 0x3e45a + { + if ( msgBuffer[3] == 0x08 ) + 3e446: 8c 81 ldd r24, Y+4 ; 0x04 + 3e448: 88 30 cpi r24, 0x08 ; 8 + 3e44a: 19 f4 brne .+6 ; 0x3e452 + fuseBits = boot_lock_fuse_bits_get( GET_EXTENDED_FUSE_BITS ); + 3e44c: e2 e0 ldi r30, 0x02 ; 2 + 3e44e: f0 e0 ldi r31, 0x00 ; 0 + 3e450: 06 c0 rjmp .+12 ; 0x3e45e + else + fuseBits = boot_lock_fuse_bits_get( GET_LOW_FUSE_BITS ); + 3e452: 89 e0 ldi r24, 0x09 ; 9 + 3e454: e0 e0 ldi r30, 0x00 ; 0 + 3e456: f0 e0 ldi r31, 0x00 ; 0 + 3e458: 03 c0 rjmp .+6 ; 0x3e460 + } + else + { + fuseBits = boot_lock_fuse_bits_get( GET_HIGH_FUSE_BITS ); + 3e45a: e3 e0 ldi r30, 0x03 ; 3 + 3e45c: f0 e0 ldi r31, 0x00 ; 0 + 3e45e: 89 e0 ldi r24, 0x09 ; 9 + 3e460: 80 93 57 00 sts 0x0057, r24 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e464: 84 91 lpm r24, Z + } + msgLength = 4; + msgBuffer[1] = STATUS_CMD_OK; + 3e466: 1a 82 std Y+2, r1 ; 0x02 + 3e468: e5 cf rjmp .-54 ; 0x3e434 + #endif + case CMD_CHIP_ERASE_ISP: + eraseAddress = 0; + msgLength = 2; + // msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[1] = STATUS_CMD_FAILED; //* isue 543, return FAILED instead of OK + 3e46a: 80 ec ldi r24, 0xC0 ; 192 + 3e46c: 8a 83 std Y+2, r24 ; 0x02 + break; + 3e46e: c1 2c mov r12, r1 + } + break; + #endif + case CMD_CHIP_ERASE_ISP: + eraseAddress = 0; + msgLength = 2; + 3e470: 02 e0 ldi r16, 0x02 ; 2 + 3e472: 10 e0 ldi r17, 0x00 ; 0 + msgBuffer[2] = STATUS_CMD_OK; + } + break; + #endif + case CMD_CHIP_ERASE_ISP: + eraseAddress = 0; + 3e474: 81 2c mov r8, r1 + 3e476: 91 2c mov r9, r1 + 3e478: 54 01 movw r10, r8 + msgLength = 2; + // msgBuffer[1] = STATUS_CMD_OK; + msgBuffer[1] = STATUS_CMD_FAILED; //* isue 543, return FAILED instead of OK + break; + 3e47a: e5 c0 rjmp .+458 ; 0x3e646 + 3e47c: 6a 81 ldd r22, Y+2 ; 0x02 + 3e47e: 7b 81 ldd r23, Y+3 ; 0x03 + 3e480: 8c 81 ldd r24, Y+4 ; 0x04 + 3e482: 9d 81 ldd r25, Y+5 ; 0x05 + 3e484: 63 d1 rcall .+710 ; 0x3e74c <__bswapsi2> + + case CMD_LOAD_ADDRESS: + #if defined(RAMPZ) + address = ( ((address_t)(msgBuffer[1])<<24)|((address_t)(msgBuffer[2])<<16)|((address_t)(msgBuffer[3])<<8)|(msgBuffer[4]) )<<1; + 3e486: 2b 01 movw r4, r22 + 3e488: 3c 01 movw r6, r24 + 3e48a: 44 0c add r4, r4 + 3e48c: 55 1c adc r5, r5 + 3e48e: 66 1c adc r6, r6 + 3e490: 77 1c adc r7, r7 + #else + address = ( ((msgBuffer[3])<<8)|(msgBuffer[4]) )<<1; //convert word to byte address + #endif + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + 3e492: 1a 82 std Y+2, r1 ; 0x02 + 3e494: d5 c0 rjmp .+426 ; 0x3e640 + 3e496: ea 80 ldd r14, Y+2 ; 0x02 + 3e498: fb 80 ldd r15, Y+3 ; 0x03 + 3e49a: fe 24 eor r15, r14 + 3e49c: ef 24 eor r14, r15 + 3e49e: fe 24 eor r15, r14 + unsigned int data; + unsigned char highByte, lowByte; + address_t tempaddress = address; + + + if ( msgBuffer[0] == CMD_PROGRAM_FLASH_ISP ) + 3e4a0: 23 31 cpi r18, 0x13 ; 19 + 3e4a2: 09 f0 breq .+2 ; 0x3e4a6 + 3e4a4: 3c c0 rjmp .+120 ; 0x3e51e + { + // erase only main section (bootloader protection) + while (eraseAddress < APP_END ) + 3e4a6: 81 14 cp r8, r1 + 3e4a8: 40 ec ldi r20, 0xC0 ; 192 + 3e4aa: 94 06 cpc r9, r20 + 3e4ac: 43 e0 ldi r20, 0x03 ; 3 + 3e4ae: a4 06 cpc r10, r20 + 3e4b0: b1 04 cpc r11, r1 + 3e4b2: 78 f4 brcc .+30 ; 0x3e4d2 + { + boot_page_erase(eraseAddress); // Perform page erase + 3e4b4: 23 e0 ldi r18, 0x03 ; 3 + 3e4b6: f4 01 movw r30, r8 + 3e4b8: a0 92 5b 00 sts 0x005B, r10 ; 0x80005b <__TEXT_REGION_LENGTH__+0x7c005b> + 3e4bc: 20 93 57 00 sts 0x0057, r18 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e4c0: e8 95 spm + boot_spm_busy_wait(); // Wait until the memory is erased. + 3e4c2: 07 b6 in r0, 0x37 ; 55 + 3e4c4: 00 fc sbrc r0, 0 + 3e4c6: fd cf rjmp .-6 ; 0x3e4c2 + eraseAddress += SPM_PAGESIZE; // point to next page to be erase + 3e4c8: 3f ef ldi r19, 0xFF ; 255 + 3e4ca: 93 1a sub r9, r19 + 3e4cc: a3 0a sbc r10, r19 + 3e4ce: b3 0a sbc r11, r19 + 3e4d0: ea cf rjmp .-44 ; 0x3e4a6 + 3e4d2: 93 01 movw r18, r6 + 3e4d4: 82 01 movw r16, r4 + 3e4d6: de 01 movw r26, r28 + 3e4d8: 1b 96 adiw r26, 0x0b ; 11 + 3e4da: 8d 91 ld r24, X+ + 3e4dc: 9d 91 ld r25, X+ + do { + lowByte = *p++; + highByte = *p++; + + data = (highByte << 8) | lowByte; + boot_page_fill(address,data); + 3e4de: 41 e0 ldi r20, 0x01 ; 1 + 3e4e0: 0c 01 movw r0, r24 + 3e4e2: f8 01 movw r30, r16 + 3e4e4: 20 93 5b 00 sts 0x005B, r18 ; 0x80005b <__TEXT_REGION_LENGTH__+0x7c005b> + 3e4e8: 40 93 57 00 sts 0x0057, r20 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e4ec: e8 95 spm + 3e4ee: 11 24 eor r1, r1 + + address = address + 2; // Select next word in memory + 3e4f0: 0e 5f subi r16, 0xFE ; 254 + 3e4f2: 1f 4f sbci r17, 0xFF ; 255 + 3e4f4: 2f 4f sbci r18, 0xFF ; 255 + 3e4f6: 3f 4f sbci r19, 0xFF ; 255 + size -= 2; // Reduce number of bytes to write by two + 3e4f8: 82 e0 ldi r24, 0x02 ; 2 + 3e4fa: e8 1a sub r14, r24 + 3e4fc: f1 08 sbc r15, r1 + } while (size); // Loop until all bytes written + 3e4fe: 69 f7 brne .-38 ; 0x3e4da + + boot_page_write(tempaddress); + 3e500: 95 e0 ldi r25, 0x05 ; 5 + 3e502: f2 01 movw r30, r4 + 3e504: 60 92 5b 00 sts 0x005B, r6 ; 0x80005b <__TEXT_REGION_LENGTH__+0x7c005b> + 3e508: 90 93 57 00 sts 0x0057, r25 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e50c: e8 95 spm + boot_spm_busy_wait(); + 3e50e: 07 b6 in r0, 0x37 ; 55 + 3e510: 00 fc sbrc r0, 0 + 3e512: fd cf rjmp .-6 ; 0x3e50e + boot_rww_enable(); // Re-enable the RWW section + 3e514: 81 e1 ldi r24, 0x11 ; 17 + 3e516: 80 93 57 00 sts 0x0057, r24 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e51a: e8 95 spm + 3e51c: 32 c0 rjmp .+100 ; 0x3e582 + } + else + { + //* issue 543, this should work, It has not been tested. + uint16_t ii = address >> 1; + 3e51e: d3 01 movw r26, r6 + 3e520: c2 01 movw r24, r4 + 3e522: b6 95 lsr r27 + 3e524: a7 95 ror r26 + 3e526: 97 95 ror r25 + 3e528: 87 95 ror r24 + 3e52a: 6c 01 movw r12, r24 + 3e52c: fe 01 movw r30, r28 + 3e52e: 3b 96 adiw r30, 0x0b ; 11 + 3e530: c1 5e subi r28, 0xE1 ; 225 + 3e532: de 4f sbci r29, 0xFE ; 254 + 3e534: f9 83 std Y+1, r31 ; 0x01 + 3e536: e8 83 st Y, r30 + 3e538: cf 51 subi r28, 0x1F ; 31 + 3e53a: d1 40 sbci r29, 0x01 ; 1 + /* write EEPROM */ + while (size) { + 3e53c: 00 e0 ldi r16, 0x00 ; 0 + 3e53e: 10 e0 ldi r17, 0x00 ; 0 + 3e540: 0e 15 cp r16, r14 + 3e542: 1f 05 cpc r17, r15 + 3e544: a1 f0 breq .+40 ; 0x3e56e + eeprom_write_byte((uint8_t*)ii, *p++); + 3e546: c1 5e subi r28, 0xE1 ; 225 + 3e548: de 4f sbci r29, 0xFE ; 254 + 3e54a: e8 81 ld r30, Y + 3e54c: f9 81 ldd r31, Y+1 ; 0x01 + 3e54e: cf 51 subi r28, 0x1F ; 31 + 3e550: d1 40 sbci r29, 0x01 ; 1 + 3e552: 61 91 ld r22, Z+ + 3e554: c1 5e subi r28, 0xE1 ; 225 + 3e556: de 4f sbci r29, 0xFE ; 254 + 3e558: f9 83 std Y+1, r31 ; 0x01 + 3e55a: e8 83 st Y, r30 + 3e55c: cf 51 subi r28, 0x1F ; 31 + 3e55e: d1 40 sbci r29, 0x01 ; 1 + 3e560: c8 01 movw r24, r16 + 3e562: 8c 0d add r24, r12 + 3e564: 9d 1d adc r25, r13 + 3e566: f9 d0 rcall .+498 ; 0x3e75a + 3e568: 0f 5f subi r16, 0xFF ; 255 + 3e56a: 1f 4f sbci r17, 0xFF ; 255 + 3e56c: e9 cf rjmp .-46 ; 0x3e540 + 3e56e: 20 e0 ldi r18, 0x00 ; 0 + 3e570: 30 e0 ldi r19, 0x00 ; 0 + 3e572: 00 0f add r16, r16 + 3e574: 11 1f adc r17, r17 + 3e576: 22 1f adc r18, r18 + 3e578: 33 1f adc r19, r19 + 3e57a: 04 0d add r16, r4 + 3e57c: 15 1d adc r17, r5 + 3e57e: 26 1d adc r18, r6 + 3e580: 37 1d adc r19, r7 + ii++; + size--; + } + } + msgLength = 2; + msgBuffer[1] = STATUS_CMD_OK; + 3e582: 1a 82 std Y+2, r1 ; 0x02 + } + break; + 3e584: 28 01 movw r4, r16 + 3e586: 39 01 movw r6, r18 + 3e588: 5b c0 rjmp .+182 ; 0x3e640 + 3e58a: 8a 81 ldd r24, Y+2 ; 0x02 + 3e58c: 9b 81 ldd r25, Y+3 ; 0x03 + 3e58e: 98 27 eor r25, r24 + 3e590: 89 27 eor r24, r25 + 3e592: 98 27 eor r25, r24 + case CMD_READ_FLASH_ISP: + case CMD_READ_EEPROM_ISP: + { + unsigned int size = ((msgBuffer[1])<<8) | msgBuffer[2]; + unsigned char *p = msgBuffer+1; + msgLength = size+3; + 3e594: 8c 01 movw r16, r24 + 3e596: 0d 5f subi r16, 0xFD ; 253 + 3e598: 1f 4f sbci r17, 0xFF ; 255 + + *p++ = STATUS_CMD_OK; + 3e59a: 1a 82 std Y+2, r1 ; 0x02 + if (msgBuffer[0] == CMD_READ_FLASH_ISP ) + 3e59c: 24 31 cpi r18, 0x14 ; 20 + 3e59e: 01 f5 brne .+64 ; 0x3e5e0 + { + //Read protection + if (eraseAddress < APP_END) { + 3e5a0: 81 14 cp r8, r1 + 3e5a2: f0 ec ldi r31, 0xC0 ; 192 + 3e5a4: 9f 06 cpc r9, r31 + 3e5a6: f3 e0 ldi r31, 0x03 ; 3 + 3e5a8: af 06 cpc r10, r31 + 3e5aa: b1 04 cpc r11, r1 + 3e5ac: 08 f4 brcc .+2 ; 0x3e5b0 + 3e5ae: 46 c0 rjmp .+140 ; 0x3e63c + 3e5b0: de 01 movw r26, r28 + 3e5b2: 11 96 adiw r26, 0x01 ; 1 + + // Read FLASH + do { + //#if defined(RAMPZ) + #if (FLASHEND > 0x10000) + data = pgm_read_word_far(address); + 3e5b4: 6b be out 0x3b, r6 ; 59 + 3e5b6: f2 01 movw r30, r4 + 3e5b8: 27 91 elpm r18, Z+ + 3e5ba: 36 91 elpm r19, Z + #else + data = pgm_read_word_near(address); + #endif + *p++ = (unsigned char)data; //LSB + 3e5bc: 12 96 adiw r26, 0x02 ; 2 + 3e5be: 2c 93 st X, r18 + 3e5c0: 12 97 sbiw r26, 0x02 ; 2 + 3e5c2: fd 01 movw r30, r26 + 3e5c4: 34 96 adiw r30, 0x04 ; 4 + *p++ = (unsigned char)(data >> 8); //MSB + 3e5c6: 13 96 adiw r26, 0x03 ; 3 + 3e5c8: 3c 93 st X, r19 + 3e5ca: 13 97 sbiw r26, 0x03 ; 3 + address += 2; // Select next word in memory + 3e5cc: 22 e0 ldi r18, 0x02 ; 2 + 3e5ce: 42 0e add r4, r18 + 3e5d0: 51 1c adc r5, r1 + 3e5d2: 61 1c adc r6, r1 + 3e5d4: 71 1c adc r7, r1 + size -= 2; + 3e5d6: 02 97 sbiw r24, 0x02 ; 2 + 3e5d8: 12 96 adiw r26, 0x02 ; 2 + }while (size); + 3e5da: 00 97 sbiw r24, 0x00 ; 0 + 3e5dc: 59 f7 brne .-42 ; 0x3e5b4 + 3e5de: 2b c0 rjmp .+86 ; 0x3e636 + 3e5e0: fe 01 movw r30, r28 + 3e5e2: 31 96 adiw r30, 0x01 ; 1 + 3e5e4: 9f 01 movw r18, r30 + 3e5e6: 28 0f add r18, r24 + 3e5e8: 39 1f adc r19, r25 + unsigned int size = ((msgBuffer[1])<<8) | msgBuffer[2]; + unsigned char *p = msgBuffer+1; + msgLength = size+3; + + *p++ = STATUS_CMD_OK; + if (msgBuffer[0] == CMD_READ_FLASH_ISP ) + 3e5ea: b3 01 movw r22, r6 + 3e5ec: a2 01 movw r20, r4 + } + else + { + /* Read EEPROM */ + do { + EEARL = address; // Setup EEPROM address + 3e5ee: 41 bd out 0x21, r20 ; 33 + EEARH = ((address >> 8)); + 3e5f0: c5 2e mov r12, r21 + 3e5f2: d6 2e mov r13, r22 + 3e5f4: e7 2e mov r14, r23 + 3e5f6: ff 24 eor r15, r15 + 3e5f8: c2 bc out 0x22, r12 ; 34 + address++; // Select next EEPROM byte + 3e5fa: 4f 5f subi r20, 0xFF ; 255 + 3e5fc: 5f 4f sbci r21, 0xFF ; 255 + 3e5fe: 6f 4f sbci r22, 0xFF ; 255 + 3e600: 7f 4f sbci r23, 0xFF ; 255 + EECR |= (1< + 3e610: ac 01 movw r20, r24 + 3e612: 41 50 subi r20, 0x01 ; 1 + 3e614: 51 09 sbc r21, r1 + 3e616: 60 e0 ldi r22, 0x00 ; 0 + 3e618: 70 e0 ldi r23, 0x00 ; 0 + 3e61a: 4f 5f subi r20, 0xFF ; 255 + 3e61c: 5f 4f sbci r21, 0xFF ; 255 + 3e61e: 6f 4f sbci r22, 0xFF ; 255 + 3e620: 7f 4f sbci r23, 0xFF ; 255 + 3e622: 44 0e add r4, r20 + 3e624: 55 1e adc r5, r21 + 3e626: 66 1e adc r6, r22 + 3e628: 77 1e adc r7, r23 + 3e62a: e3 e0 ldi r30, 0x03 ; 3 + 3e62c: f0 e0 ldi r31, 0x00 ; 0 + 3e62e: ec 0f add r30, r28 + 3e630: fd 1f adc r31, r29 + 3e632: e8 0f add r30, r24 + 3e634: f9 1f adc r31, r25 + } + *p++ = STATUS_CMD_OK; + 3e636: 10 82 st Z, r1 + } + break; + 3e638: c1 2c mov r12, r1 + 3e63a: 05 c0 rjmp .+10 ; 0x3e646 + + default: + msgLength = 2; + msgBuffer[1] = STATUS_CMD_FAILED; + 3e63c: 80 ec ldi r24, 0xC0 ; 192 + break; + 3e63e: 8a 83 std Y+2, r24 ; 0x02 + *p++ = STATUS_CMD_OK; + } + break; + + default: + msgLength = 2; + 3e640: c1 2c mov r12, r1 + 3e642: 02 e0 ldi r16, 0x02 ; 2 + } + + /* + * Now send answer message back + */ + sendchar(MESSAGE_START); + 3e644: 10 e0 ldi r17, 0x00 ; 0 + 3e646: 8b e1 ldi r24, 0x1B ; 27 + checksum = MESSAGE_START^0; + + sendchar(seqNum); + 3e648: 98 dd rcall .-1232 ; 0x3e17a + 3e64a: c2 5e subi r28, 0xE2 ; 226 + 3e64c: de 4f sbci r29, 0xFE ; 254 + 3e64e: 88 81 ld r24, Y + 3e650: ce 51 subi r28, 0x1E ; 30 + checksum ^= seqNum; + + c = ((msgLength>>8)&0xFF); + sendchar(c); + 3e652: d1 40 sbci r29, 0x01 ; 1 + 3e654: 92 dd rcall .-1244 ; 0x3e17a + 3e656: 81 2f mov r24, r17 + checksum ^= c; + + c = msgLength&0x00FF; + sendchar(c); + 3e658: 90 dd rcall .-1248 ; 0x3e17a + 3e65a: 80 2f mov r24, r16 + 3e65c: 8e dd rcall .-1252 ; 0x3e17a + 3e65e: 85 e1 ldi r24, 0x15 ; 21 + 3e660: c2 5e subi r28, 0xE2 ; 226 + 3e662: de 4f sbci r29, 0xFE ; 254 + 3e664: d8 80 ld r13, Y + 3e666: ce 51 subi r28, 0x1E ; 30 + 3e668: d1 40 sbci r29, 0x01 ; 1 + 3e66a: d8 26 eor r13, r24 + checksum ^= c; + + sendchar(TOKEN); + 3e66c: d0 26 eor r13, r16 + 3e66e: 8e e0 ldi r24, 0x0E ; 14 + 3e670: 84 dd rcall .-1272 ; 0x3e17a + checksum ^= TOKEN; + 3e672: d1 26 eor r13, r17 + 3e674: ce 01 movw r24, r28 + 3e676: 01 96 adiw r24, 0x01 ; 1 + 3e678: 7c 01 movw r14, r24 + 3e67a: 08 0f add r16, r24 + 3e67c: 19 1f adc r17, r25 + + p = msgBuffer; + while ( msgLength ) + 3e67e: 0e 15 cp r16, r14 + 3e680: 1f 05 cpc r17, r15 + { + c = *p++; + 3e682: 89 f0 breq .+34 ; 0x3e6a6 + 3e684: f7 01 movw r30, r14 + 3e686: 21 91 ld r18, Z+ + 3e688: 7f 01 movw r14, r30 + 3e68a: c1 5e subi r28, 0xE1 ; 225 + 3e68c: de 4f sbci r29, 0xFE ; 254 + 3e68e: 28 83 st Y, r18 + 3e690: cf 51 subi r28, 0x1F ; 31 + sendchar(c); + 3e692: d1 40 sbci r29, 0x01 ; 1 + 3e694: 82 2f mov r24, r18 + checksum ^=c; + 3e696: 71 dd rcall .-1310 ; 0x3e17a + 3e698: c1 5e subi r28, 0xE1 ; 225 + 3e69a: de 4f sbci r29, 0xFE ; 254 + 3e69c: 38 81 ld r19, Y + 3e69e: cf 51 subi r28, 0x1F ; 31 + 3e6a0: d1 40 sbci r29, 0x01 ; 1 + 3e6a2: d3 26 eor r13, r19 + msgLength--; + } + sendchar(checksum); + 3e6a4: ec cf rjmp .-40 ; 0x3e67e + 3e6a6: 8d 2d mov r24, r13 + 3e6a8: 68 dd rcall .-1328 ; 0x3e17a + seqNum++; + 3e6aa: c2 5e subi r28, 0xE2 ; 226 + 3e6ac: de 4f sbci r29, 0xFE ; 254 + 3e6ae: 48 81 ld r20, Y + 3e6b0: ce 51 subi r28, 0x1E ; 30 + 3e6b2: d1 40 sbci r29, 0x01 ; 1 + 3e6b4: 4f 5f subi r20, 0xFF ; 255 + 3e6b6: c2 5e subi r28, 0xE2 ; 226 + 3e6b8: de 4f sbci r29, 0xFE ; 254 + 3e6ba: 48 83 st Y, r20 + 3e6bc: ce 51 subi r28, 0x1E ; 30 + 3e6be: d1 40 sbci r29, 0x01 ; 1 + 3e6c0: 00 e0 ldi r16, 0x00 ; 0 + 3e6c2: 10 e0 ldi r17, 0x00 ; 0 + + + if (boot_state==1) + { + //* main loop + while (!isLeave) + 3e6c4: cc 20 and r12, r12 + 3e6c6: 09 f4 brne .+2 ; 0x3e6ca + 3e6c8: dc cd rjmp .-1096 ; 0x3e282 +// PROGLED_PORT |= (1< + 3e6d0: 8d 7f andi r24, 0xFD ; 253 + 3e6d2: 80 93 c0 00 sts 0x00C0, r24 ; 0x8000c0 <__TEXT_REGION_LENGTH__+0x7c00c0> + boot_rww_enable(); // enable application section + 3e6d6: 81 e1 ldi r24, 0x11 ; 17 + 3e6d8: 80 93 57 00 sts 0x0057, r24 ; 0x800057 <__TEXT_REGION_LENGTH__+0x7c0057> + 3e6dc: e8 95 spm + + + asm volatile( + 3e6de: ee 27 eor r30, r30 + 3e6e0: ff 27 eor r31, r31 + 3e6e2: 09 94 ijmp + 3e6e4: ff cf rjmp .-2 ; 0x3e6e4 + { + case ST_START: + if ( c == MESSAGE_START ) + { + msgParseState = ST_GET_SEQ_NUM; + checksum = MESSAGE_START^0; + 3e6e6: 8b e1 ldi r24, 0x1B ; 27 + 3e6e8: d8 2e mov r13, r24 + switch (msgParseState) + { + case ST_START: + if ( c == MESSAGE_START ) + { + msgParseState = ST_GET_SEQ_NUM; + 3e6ea: 21 e0 ldi r18, 0x01 ; 1 + 3e6ec: f7 cd rjmp .-1042 ; 0x3e2dc + checksum ^= c; + ii = 0; + } + else + { + msgParseState = ST_START; + 3e6ee: 20 e0 ldi r18, 0x00 ; 0 + 3e6f0: f5 cd rjmp .-1046 ; 0x3e2dc + 3e6f2: 1a 01 movw r2, r20 + case ST_GET_DATA: + msgBuffer[ii++] = c; + checksum ^= c; + if (ii == msgLength ) + { + msgParseState = ST_GET_CHECK; + 3e6f4: 26 e0 ldi r18, 0x06 ; 6 + 3e6f6: f2 cd rjmp .-1052 ; 0x3e2dc + +0003e6f8 <__udivmodsi4>: + 3e6f8: a1 e2 ldi r26, 0x21 ; 33 + 3e6fa: 1a 2e mov r1, r26 + 3e6fc: aa 1b sub r26, r26 + 3e6fe: bb 1b sub r27, r27 + 3e700: fd 01 movw r30, r26 + 3e702: 0d c0 rjmp .+26 ; 0x3e71e <__udivmodsi4_ep> + +0003e704 <__udivmodsi4_loop>: + 3e704: aa 1f adc r26, r26 + 3e706: bb 1f adc r27, r27 + 3e708: ee 1f adc r30, r30 + 3e70a: ff 1f adc r31, r31 + 3e70c: a2 17 cp r26, r18 + 3e70e: b3 07 cpc r27, r19 + 3e710: e4 07 cpc r30, r20 + 3e712: f5 07 cpc r31, r21 + 3e714: 20 f0 brcs .+8 ; 0x3e71e <__udivmodsi4_ep> + 3e716: a2 1b sub r26, r18 + 3e718: b3 0b sbc r27, r19 + 3e71a: e4 0b sbc r30, r20 + 3e71c: f5 0b sbc r31, r21 + +0003e71e <__udivmodsi4_ep>: + 3e71e: 66 1f adc r22, r22 + 3e720: 77 1f adc r23, r23 + 3e722: 88 1f adc r24, r24 + 3e724: 99 1f adc r25, r25 + 3e726: 1a 94 dec r1 + 3e728: 69 f7 brne .-38 ; 0x3e704 <__udivmodsi4_loop> + 3e72a: 60 95 com r22 + 3e72c: 70 95 com r23 + 3e72e: 80 95 com r24 + 3e730: 90 95 com r25 + 3e732: 9b 01 movw r18, r22 + 3e734: ac 01 movw r20, r24 + 3e736: bd 01 movw r22, r26 + 3e738: cf 01 movw r24, r30 + 3e73a: 08 95 ret + +0003e73c <__tablejump2__>: + 3e73c: ee 0f add r30, r30 + 3e73e: ff 1f adc r31, r31 + 3e740: 88 1f adc r24, r24 + 3e742: 8b bf out 0x3b, r24 ; 59 + 3e744: 07 90 elpm r0, Z+ + 3e746: f6 91 elpm r31, Z + 3e748: e0 2d mov r30, r0 + 3e74a: 19 94 eijmp + +0003e74c <__bswapsi2>: + 3e74c: 69 27 eor r22, r25 + 3e74e: 96 27 eor r25, r22 + 3e750: 69 27 eor r22, r25 + 3e752: 78 27 eor r23, r24 + 3e754: 87 27 eor r24, r23 + 3e756: 78 27 eor r23, r24 + 3e758: 08 95 ret + +0003e75a : + 3e75a: 26 2f mov r18, r22 + +0003e75c : + 3e75c: f9 99 sbic 0x1f, 1 ; 31 + 3e75e: fe cf rjmp .-4 ; 0x3e75c + 3e760: 1f ba out 0x1f, r1 ; 31 + 3e762: 92 bd out 0x22, r25 ; 34 + 3e764: 81 bd out 0x21, r24 ; 33 + 3e766: 20 bd out 0x20, r18 ; 32 + 3e768: 0f b6 in r0, 0x3f ; 63 + 3e76a: f8 94 cli + 3e76c: fa 9a sbi 0x1f, 2 ; 31 + 3e76e: f9 9a sbi 0x1f, 1 ; 31 + 3e770: 0f be out 0x3f, r0 ; 63 + 3e772: 01 96 adiw r24, 0x01 ; 1 + 3e774: 08 95 ret + +0003e776 <_exit>: + 3e776: f8 94 cli + +0003e778 <__stop_program>: + 3e778: ff cf rjmp .-2 ; 0x3e778 <__stop_program> diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.map b/bootloaders/stk500v2/stk500boot_v2_mega2560.map new file mode 100644 index 0000000..da0265a --- /dev/null +++ b/bootloaders/stk500v2/stk500boot_v2_mega2560.map @@ -0,0 +1,582 @@ +Archive member included to satisfy reference by file (symbol) + +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + stk500boot.o (__udivmodsi4) +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o (exit) +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + stk500boot.o (__tablejump2__) +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + stk500boot.o (__do_copy_data) +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + stk500boot.o (__do_clear_bss) +c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + stk500boot.o (__bswapsi2) +C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + stk500boot.o (eeprom_write_byte) + +Discarded input sections + + .data 0x00000000 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .bss 0x00000000 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .text 0x00000000 0x0 stk500boot.o + .data 0x00000000 0x0 stk500boot.o + .bss 0x00000000 0x0 stk500boot.o + .text.delay_ms + 0x00000000 0x1e stk500boot.o + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc.builtins + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.div + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.builtins + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.div + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.builtins + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.div + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.builtins + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.div + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.builtins + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + .text 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .data 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .bss 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc.mul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc.div + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc.prologue + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc.fmul + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text.libgcc.fixed + 0x00000000 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + .text 0x00000000 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + .data 0x00000000 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + .bss 0x00000000 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + +Memory Configuration + +Name Origin Length Attributes +text 0x00000000 0x00040000 xr +data 0x00800200 0x00002000 rw !x +eeprom 0x00810000 0x00001000 rw !x +fuse 0x00820000 0x00000003 rw !x +lock 0x00830000 0x00000400 rw !x +signature 0x00840000 0x00000400 rw !x +user_signatures 0x00850000 0x00000400 rw !x +*default* 0x00000000 0xffffffff + +Linker script and memory map + +Address of section .data set to 0x800200 +LOAD C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o +LOAD stk500boot.o +START GROUP +LOAD c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/../../../../avr/lib/avr6\libm.a +END GROUP +Address of section .text set to 0x3e000 +START GROUP +LOAD c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a +LOAD c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/../../../../avr/lib/avr6\libm.a +LOAD c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/../../../../avr/lib/avr6\libc.a +LOAD C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a +END GROUP + 0x00000000 __TEXT_REGION_ORIGIN__ = DEFINED (__TEXT_REGION_ORIGIN__)?__TEXT_REGION_ORIGIN__:0x0 + [0x00800200] __DATA_REGION_ORIGIN__ = DEFINED (__DATA_REGION_ORIGIN__)?__DATA_REGION_ORIGIN__:0x800200 + [0x00040000] __TEXT_REGION_LENGTH__ = DEFINED (__TEXT_REGION_LENGTH__)?__TEXT_REGION_LENGTH__:0x100000 + [0x00002000] __DATA_REGION_LENGTH__ = DEFINED (__DATA_REGION_LENGTH__)?__DATA_REGION_LENGTH__:0xfe00 + [0x00001000] __EEPROM_REGION_LENGTH__ = DEFINED (__EEPROM_REGION_LENGTH__)?__EEPROM_REGION_LENGTH__:0x10000 + [0x00000003] __FUSE_REGION_LENGTH__ = DEFINED (__FUSE_REGION_LENGTH__)?__FUSE_REGION_LENGTH__:0x400 + 0x00000400 __LOCK_REGION_LENGTH__ = DEFINED (__LOCK_REGION_LENGTH__)?__LOCK_REGION_LENGTH__:0x400 + 0x00000400 __SIGNATURE_REGION_LENGTH__ = DEFINED (__SIGNATURE_REGION_LENGTH__)?__SIGNATURE_REGION_LENGTH__:0x400 + 0x00000400 __USER_SIGNATURE_REGION_LENGTH__ = DEFINED (__USER_SIGNATURE_REGION_LENGTH__)?__USER_SIGNATURE_REGION_LENGTH__:0x400 + +.hash + *(.hash) + +.dynsym + *(.dynsym) + +.dynstr + *(.dynstr) + +.gnu.version + *(.gnu.version) + +.gnu.version_d + *(.gnu.version_d) + +.gnu.version_r + *(.gnu.version_r) + +.rel.init + *(.rel.init) + +.rela.init + *(.rela.init) + +.rel.text + *(.rel.text) + *(.rel.text.*) + *(.rel.gnu.linkonce.t*) + +.rela.text + *(.rela.text) + *(.rela.text.*) + *(.rela.gnu.linkonce.t*) + +.rel.fini + *(.rel.fini) + +.rela.fini + *(.rela.fini) + +.rel.rodata + *(.rel.rodata) + *(.rel.rodata.*) + *(.rel.gnu.linkonce.r*) + +.rela.rodata + *(.rela.rodata) + *(.rela.rodata.*) + *(.rela.gnu.linkonce.r*) + +.rel.data + *(.rel.data) + *(.rel.data.*) + *(.rel.gnu.linkonce.d*) + +.rela.data + *(.rela.data) + *(.rela.data.*) + *(.rela.gnu.linkonce.d*) + +.rel.ctors + *(.rel.ctors) + +.rela.ctors + *(.rela.ctors) + +.rel.dtors + *(.rel.dtors) + +.rela.dtors + *(.rela.dtors) + +.rel.got + *(.rel.got) + +.rela.got + *(.rela.got) + +.rel.bss + *(.rel.bss) + +.rela.bss + *(.rela.bss) + +.rel.plt + *(.rel.plt) + +.rela.plt + *(.rela.plt) + +.text 0x0003e000 0x77a + *(.vectors) + .vectors 0x0003e000 0xe4 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + 0x0003e000 __vector_default + 0x0003e000 __vectors + *(.vectors) + *(.progmem.gcc*) + .progmem.gcc_sw_table.main + 0x0003e0e4 0x48 stk500boot.o + 0x0003e12c . = ALIGN (0x2) + 0x0003e12c __trampolines_start = . + *(.trampolines) + .trampolines 0x0003e12c 0x0 linker stubs + *(.trampolines*) + 0x0003e12c __trampolines_end = . + *libprintf_flt.a:*(.progmem.data) + *libc.a:*(.progmem.data) + *(.progmem*) + 0x0003e12c . = ALIGN (0x2) + *(.jumptables) + *(.jumptables*) + *(.lowtext) + *(.lowtext*) + 0x0003e12c __ctors_start = . + *(.ctors) + 0x0003e12c __ctors_end = . + 0x0003e12c __dtors_start = . + *(.dtors) + 0x0003e12c __dtors_end = . + SORT(*)(.ctors) + SORT(*)(.dtors) + *(.init0) + .init0 0x0003e12c 0x0 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + 0x0003e12c __init + *(.init0) + *(.init1) + *(.init1) + *(.init2) + .init2 0x0003e12c 0x10 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + *(.init2) + *(.init3) + *(.init3) + *(.init4) + .init4 0x0003e13c 0x1a c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_copy_data.o) + 0x0003e13c __do_copy_data + .init4 0x0003e156 0x10 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_clear_bss.o) + 0x0003e156 __do_clear_bss + *(.init4) + *(.init5) + *(.init5) + *(.init6) + *(.init6) + *(.init7) + *(.init7) + *(.init8) + *(.init8) + *(.init9) + .init9 0x0003e166 0x4 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .init9 0x0003e16a 0xe stk500boot.o + 0x0003e16a __jumpMain + *(.init9) + *(.text) + .text 0x0003e178 0x2 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + 0x0003e178 __vector_38 + 0x0003e178 __vector_22 + 0x0003e178 __vector_28 + 0x0003e178 __vector_1 + 0x0003e178 __vector_32 + 0x0003e178 __vector_34 + 0x0003e178 __vector_24 + 0x0003e178 __vector_12 + 0x0003e178 __vector_55 + 0x0003e178 __vector_46 + 0x0003e178 __bad_interrupt + 0x0003e178 __vector_6 + 0x0003e178 __vector_31 + 0x0003e178 __vector_35 + 0x0003e178 __vector_39 + 0x0003e178 __vector_3 + 0x0003e178 __vector_23 + 0x0003e178 __vector_30 + 0x0003e178 __vector_45 + 0x0003e178 __vector_25 + 0x0003e178 __vector_11 + 0x0003e178 __vector_54 + 0x0003e178 __vector_13 + 0x0003e178 __vector_17 + 0x0003e178 __vector_19 + 0x0003e178 __vector_56 + 0x0003e178 __vector_7 + 0x0003e178 __vector_49 + 0x0003e178 __vector_41 + 0x0003e178 __vector_43 + 0x0003e178 __vector_27 + 0x0003e178 __vector_5 + 0x0003e178 __vector_33 + 0x0003e178 __vector_47 + 0x0003e178 __vector_52 + 0x0003e178 __vector_37 + 0x0003e178 __vector_4 + 0x0003e178 __vector_44 + 0x0003e178 __vector_51 + 0x0003e178 __vector_9 + 0x0003e178 __vector_2 + 0x0003e178 __vector_21 + 0x0003e178 __vector_15 + 0x0003e178 __vector_36 + 0x0003e178 __vector_29 + 0x0003e178 __vector_40 + 0x0003e178 __vector_8 + 0x0003e178 __vector_26 + 0x0003e178 __vector_48 + 0x0003e178 __vector_14 + 0x0003e178 __vector_53 + 0x0003e178 __vector_10 + 0x0003e178 __vector_50 + 0x0003e178 __vector_16 + 0x0003e178 __vector_18 + 0x0003e178 __vector_20 + 0x0003e178 __vector_42 + 0x0003e17a . = ALIGN (0x2) + *(.text.*) + .text.sendchar + 0x0003e17a 0x18 stk500boot.o + .text.startup.main + 0x0003e192 0x566 stk500boot.o + 0x0003e192 main + .text.libgcc.div + 0x0003e6f8 0x44 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_udivmodsi4.o) + 0x0003e6f8 __udivmodsi4 + .text.libgcc 0x0003e73c 0x10 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_tablejump2.o) + 0x0003e73c __tablejump2__ + .text.libgcc.builtins + 0x0003e74c 0xe c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_bswapsi2.o) + 0x0003e74c __bswapsi2 + .text.avr-libc + 0x0003e75a 0x1c C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + 0x0003e75a eeprom_write_byte + 0x0003e75c eeprom_write_r18 + 0x0003e776 . = ALIGN (0x2) + *(.fini9) + .fini9 0x0003e776 0x0 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + 0x0003e776 _exit + 0x0003e776 exit + *(.fini9) + *(.fini8) + *(.fini8) + *(.fini7) + *(.fini7) + *(.fini6) + *(.fini6) + *(.fini5) + *(.fini5) + *(.fini4) + *(.fini4) + *(.fini3) + *(.fini3) + *(.fini2) + *(.fini2) + *(.fini1) + *(.fini1) + *(.fini0) + .fini0 0x0003e776 0x4 c:/program files (x86)/atmel/studio/7.0/toolchain/avr8/avr8-gnu-toolchain/bin/../lib/gcc/avr/5.4.0/avr6\libgcc.a(_exit.o) + *(.fini0) + 0x0003e77a _etext = . + +.data 0x00800200 0x4 load address 0x0003e77a + 0x00800200 PROVIDE (__data_start, .) + *(.data) + *(.data*) + *(.gnu.linkonce.d*) + *(.rodata) + *(.rodata*) + .rodata.CSWTCH.27 + 0x00800200 0x3 stk500boot.o + *(.gnu.linkonce.r*) + 0x00800204 . = ALIGN (0x2) + *fill* 0x00800203 0x1 + 0x00800204 _edata = . + 0x00800204 PROVIDE (__data_end, .) + +.bss 0x00800204 0x2 + 0x00800204 PROVIDE (__bss_start, .) + *(.bss) + *(.bss*) + .bss.app_start + 0x00800204 0x2 stk500boot.o + 0x00800204 app_start + *(COMMON) + 0x00800206 PROVIDE (__bss_end, .) + 0x0003e77a __data_load_start = LOADADDR (.data) + 0x0003e77e __data_load_end = (__data_load_start + SIZEOF (.data)) + +.noinit 0x00800206 0x0 + [!provide] PROVIDE (__noinit_start, .) + *(.noinit*) + [!provide] PROVIDE (__noinit_end, .) + 0x00800206 _end = . + [!provide] PROVIDE (__heap_start, .) + +.eeprom 0x00810000 0x0 + *(.eeprom*) + 0x00810000 __eeprom_end = . + +.fuse 0x00820000 0x3 + *(.fuse) + .fuse 0x00820000 0x3 stk500boot.o + 0x00820000 __fuse + *(.lfuse) + *(.hfuse) + *(.efuse) + +.lock 0x00830000 0x1 + *(.lock*) + .lock 0x00830000 0x1 stk500boot.o + 0x00830000 __lock + +.signature + *(.signature*) + +.user_signatures + *(.user_signatures*) + +.stab + *(.stab) + +.stabstr + *(.stabstr) + +.stab.excl + *(.stab.excl) + +.stab.exclstr + *(.stab.exclstr) + +.stab.index + *(.stab.index) + +.stab.indexstr + *(.stab.indexstr) + +.comment 0x00000000 0x30 + *(.comment) + .comment 0x00000000 0x30 stk500boot.o + 0x31 (size before relaxing) + +.note.gnu.avr.deviceinfo + 0x00000000 0x40 + .note.gnu.avr.deviceinfo + 0x00000000 0x40 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + +.note.gnu.build-id + *(.note.gnu.build-id) + +.debug + *(.debug) + +.line + *(.line) + +.debug_srcinfo + *(.debug_srcinfo) + +.debug_sfnames + *(.debug_sfnames) + +.debug_aranges 0x00000000 0x58 + *(.debug_aranges) + .debug_aranges + 0x00000000 0x38 stk500boot.o + .debug_aranges + 0x00000038 0x20 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + +.debug_pubnames + *(.debug_pubnames) + +.debug_info 0x00000000 0x1367 + *(.debug_info .gnu.linkonce.wi.*) + .debug_info 0x00000000 0xbbc C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .debug_info 0x00000bbc 0x711 stk500boot.o + .debug_info 0x000012cd 0x9a C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + +.debug_abbrev 0x00000000 0xdac + *(.debug_abbrev) + .debug_abbrev 0x00000000 0xb1a C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .debug_abbrev 0x00000b1a 0x27e stk500boot.o + .debug_abbrev 0x00000d98 0x14 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + +.debug_line 0x00000000 0x988 + *(.debug_line .debug_line.* .debug_line_end) + .debug_line 0x00000000 0x1fa C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .debug_line 0x000001fa 0x6f0 stk500boot.o + .debug_line 0x000008ea 0x9e C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6\libatmega2560.a(eewr_byte.o) + +.debug_frame 0x00000000 0x68 + *(.debug_frame) + .debug_frame 0x00000000 0x68 stk500boot.o + +.debug_str 0x00000000 0x6f2 + *(.debug_str) + .debug_str 0x00000000 0x3e6 C:/Program Files (x86)/Atmel/Studio/7.0/Packs/Atmel/ATmega_DFP/1.7.374/gcc/dev/atmega2560/avr6/crtatmega2560.o + .debug_str 0x000003e6 0x30c stk500boot.o + 0x356 (size before relaxing) + +.debug_loc 0x00000000 0xaa4 + *(.debug_loc) + .debug_loc 0x00000000 0xaa4 stk500boot.o + +.debug_macinfo + *(.debug_macinfo) + +.debug_weaknames + *(.debug_weaknames) + +.debug_funcnames + *(.debug_funcnames) + +.debug_typenames + *(.debug_typenames) + +.debug_varnames + *(.debug_varnames) + +.debug_pubtypes + *(.debug_pubtypes) + +.debug_ranges 0x00000000 0x58 + *(.debug_ranges) + .debug_ranges 0x00000000 0x58 stk500boot.o + +.debug_macro + *(.debug_macro) +OUTPUT(stk500boot_v2_mega2560.elf elf32-avr) +LOAD linker stubs diff --git a/bootloaders/stk500v2/stk500boot_v2_mega2560.srec b/bootloaders/stk500v2/stk500boot_v2_mega2560.srec new file mode 100644 index 0000000..043d371 --- /dev/null +++ b/bootloaders/stk500v2/stk500boot_v2_mega2560.srec @@ -0,0 +1,123 @@ +S01E000073746B353030626F6F745F76325F6D656761323536302E737265639E +S21403E00095C00000B9C00000B7C00000B5C000004E +S21403E010B3C00000B1C00000AFC00000ADC0000038 +S21403E020ABC00000A9C00000A7C00000A5C0000048 +S21403E030A3C00000A1C000009FC000009DC0000058 +S21403E0409BC0000099C0000097C0000095C0000068 +S21403E05093C0000091C000008FC000008DC0000078 +S21403E0608BC0000089C0000087C0000085C0000088 +S21403E07083C0000081C000007FC000007DC0000098 +S21403E0807BC0000079C0000077C0000075C00000A8 +S21403E09073C0000071C000006FC000006DC00000B8 +S21403E0A06BC0000069C0000067C0000065C00000C8 +S21403E0B063C0000061C000005FC000005DC00000D8 +S21403E0C05BC0000059C0000057C0000055C00000E8 +S21403E0D053C0000051C000004FC000004DC00000F8 +S21403E0E04BC000006BF171F179F180F184F18CF192 +S21403E0F09DF1DDF1B0F1F4F11EF31EF33EF21EF3D3 +S21403E1001EF31EF31EF31EF31EF31EF31EF31EF37F +S21403E110B0F104F235F24BF2C5F24BF2C5F21EF340 +S21403E12020F21EF313F208F21EF3B2F111241FBEFF +S21403E130CFEFD1E2DEBFCDBF01E00CBF12E0A0E01F +S21403E140B2E0EAE7F7EE03E00BBF02C007900D92DA +S21403E150A430B107D9F722E0A4E0B2E001C01D92D3 +S21403E160A630B207E1F715D006C301E20EBF0FEFE4 +S21403E1700DBF11241FBE0DC043CF8093C6008091F0 +S21403E180C00086FFFCCF8091C00080648093C000EF +S21403E1900895CF93DF93CDB7DEB7C052D1400FB605 +S21403E1A0F894DEBF0FBECDBF01E20EBF0FEF0DBF6B +S21403E1B094B7F894A89514BE809160008861809304 +S21403E1C0600010926000789493FF05C0E09104020B +S21403E1D0F091050219958091C00082608093C0007B +S21403E1E080E18093C40088E18093C100000000E0D2 +S21403E1F010E0C12CD12C760197EC892E9BE1992E49 +S21403E200A12CB12C24E0722E8091C00087FD25C07E +S21403E2100115110511F525E02A95F1F700003FEFEA +S21403E220C31AD30AE30AF30A91E080E041E2C41674 +S21403E2304EE4D406E104F10408F490E0092F182F05 +S21403E240C701B601A501940157D2672B682B692B2A +S21403E250D9F684B3872584BBD7CF0F5F1F4F013012 +S21403E260110509F032C2C25EDE4F1882CE51D1408C +S21403E270D12C212C312C812C912C5401412C512C46 +S21403E280320140E050E020E001301105B1F081E0BA +S21403E29092E1AAE7B0E03091C00037FD0EC00197C7 +S21403E2A0A109B109C1F7ABBFFC018791969101960D +S21403E2B071F3EE27FF270994EACF9091C600E22E6A +S21403E2C0F12CF7E0EF16F104D8F5F7018827EE589E +S21403E2D0FF408E4F33C29B3109F405C200E010E0C5 +S21403E2E0D3CFD926C25EDE4F9883CE51D14022E0EB +S21403E2F0F5CF492F50E0542F4427D92623E0EECFFD +S21403E300492BD92624E0EACF9E3009F0F0C1D9265E +S21403E310212C312C25E0E2CFB1016F5F7F4FE1E086 +S21403E320F0E0EC0FFD1FE20DF31D9083D926461790 +S21403E330570709F4DEC11B01D1CF9D11D8C102C016 +S21403E340273061F62981822F90E0FC013197ED3169 +S21403E350F10508F073C18827E758FF408E4FEEC1DA +S21403E360C12C54C09D81903339F48F818823B1F03A +S21403E3708130B1F488E917C0892F807599F09035FC +S21403E38021F489E0E0E0F0E005C0983559F4E3E0D5 +S21403E390F0E089E080935700849105C08EE103C0C6 +S21403E3A081E001C080E01A821B829C831D828E83DB +S21403E3B01F82C12C07E010E046C11A8288E08B83D7 +S21403E3C081E48C8386E58D8382E58E8389E48F835F +S21403E3D083E5888780E589878FE58A8782E38B874D +S21403E3E0C12C0BE010E02FC1EA81E059E33028F49A +S21403E3F0F0E0E050FE4F808101C080E01A828B83FC +S21403E400C12C03E010E01FC1CC24C3941A8219C1A7 +S21403E4108D81882321F0813021F488E924C08EE1A0 +S21403E42022C081E020C01A82E1E0F0E089E0809318 +S21403E430570084918B831C82C12C04E010E003C137 +S21403E4408B81803551F48C81883019F4E2E0F0E05A +S21403E45006C089E0E0E0F0E003C0E3E0F0E089E036 +S21403E4608093570084911A82E5CF80EC8A83C12C6F +S21403E47002E010E0812C912C5401E5C06A817B8177 +S21403E4808C819D8163D12B013C01440C551C661C79 +S21403E490771C1A82D5C0EA80FB80FE24EF24FE2474 +S21403E4A0233109F03CC0811440EC940643E0A406F3 +S21403E4B0B10478F423E0F401A0925B0020935700A4 +S21403E4C0E89507B600FCFDCF3FEF931AA30AB30AFD +S21403E4D0EACF93018201DE011B968D919D9141E067 +S21403E4E00C01F80120935B0040935700E895112434 +S21403E4F00E5F1F4F2F4F3F4F82E0E81AF10869F770 +S21403E50095E0F20160925B0090935700E89507B69A +S21403E51000FCFDCF81E180935700E89532C0D3011C +S21403E520C201B695A795979587956C01FE013B9614 +S21403E530C15EDE4FF983E883CF51D14000E010E09F +S21403E5400E151F05A1F0C15EDE4FE881F981CF519C +S21403E550D1406191C15EDE4FF983E883CF51D1404C +S21403E560C8018C0D9D1DF9D00F5F1F4FE9CF20E02A +S21403E57030E0000F111F221F331F040D151D261D2B +S21403E580371D1A82280139015BC08A819B8198272F +S21403E590892798278C010D5F1F4F1A82243101F5B6 +S21403E5A08114F0EC9F06F3E0AF06B10408F446C00E +S21403E5B0DE0111966BBEF2012791369112962C93CB +S21403E5C01297FD01349613963C93139722E0420E5E +S21403E5D0511C611C711C02971296009759F72BC0A9 +S21403E5E0FE0131969F01280F391FB301A20141BDD9 +S21403E5F0C52ED62EE72EFF24C2BC4F5F5F4F6F4F4C +S21403E6007F4FF89AA0B5A28331962E173F0779F766 +S21403E610AC014150510960E070E04F5F5F4F6F4FB0 +S21403E6207F4F440E551E661E771EE3E0F0E0EC0FA8 +S21403E630FD1FE80FF91F1082C12C05C080EC8A83EA +S21403E640C12C02E010E08BE198DDC25EDE4F8881CC +S21403E650CE51D14092DD812F90DD802F8EDD85E176 +S21403E660C25EDE4FD880CE51D140D826D0268EE06B +S21403E67084DDD126CE0101967C01080F191F0E15E5 +S21403E6801F0589F0F70121917F01C15EDE4F2883C4 +S21403E690CF51D140822F71DDC15EDE4F3881CF511D +S21403E6A0D140D326ECCF8D2D68DDC25EDE4F488188 +S21403E6B0CE51D1404F5FC25EDE4F4883CE51D1402C +S21403E6C000E010E0CC2009F4DCCD00008091C0000F +S21403E6D08D7F8093C00081E180935700E895EE27F5 +S21403E6E0FF270994FFCF8BE1D82E21E0F7CD20E05A +S21403E6F0F5CD1A0126E0F2CDA1E21A2EAA1BBB1B0A +S21403E700FD010DC0AA1FBB1FEE1FFF1FA217B307F5 +S21403E710E407F50720F0A21BB30BE40BF50B661F0B +S21403E720771F881F991F1A9469F7609570958095CF +S21403E73090959B01AC01BD01CF010895EE0FFF1F1D +S21403E740881F8BBF0790F691E02D199469279627AB +S21403E75069277827872778270895262FF999FECFE4 +S21403E7601FBA92BD81BD20BD0FB6F894FA9AF99AE6 +S20E03E7700FBE01960895F894FFCF3C +S20803E77A0F020A0078 +S80403E00018 diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h new file mode 100644 index 0000000..91eeb16 --- /dev/null +++ b/cores/arduino/Arduino.h @@ -0,0 +1,260 @@ +/* + Arduino.h - Main include file for the Arduino SDK + Copyright (c) 2005-2013 Arduino Team. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Arduino_h +#define Arduino_h + +#include +#include +#include +#include + +#include +#include +#include + +#include "binary.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +void yield(void); + +#define HIGH 0x1 +#define LOW 0x0 + +#define INPUT 0x0 +#define OUTPUT 0x1 +#define INPUT_PULLUP 0x2 + +#define PI 3.1415926535897932384626433832795 +#define HALF_PI 1.5707963267948966192313216916398 +#define TWO_PI 6.283185307179586476925286766559 +#define DEG_TO_RAD 0.017453292519943295769236907684886 +#define RAD_TO_DEG 57.295779513082320876798154814105 +#define EULER 2.718281828459045235360287471352 + +#define SERIAL 0x0 +#define DISPLAY 0x1 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define CHANGE 1 +#define FALLING 2 +#define RISING 3 + +#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + #define DEFAULT 0 + #define EXTERNAL 1 + #define INTERNAL1V1 2 + #define INTERNAL INTERNAL1V1 +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + #define DEFAULT 0 + #define EXTERNAL 4 + #define INTERNAL1V1 8 + #define INTERNAL INTERNAL1V1 + #define INTERNAL2V56 9 + #define INTERNAL2V56_EXTCAP 13 +#else +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) +#define INTERNAL1V1 2 +#define INTERNAL2V56 3 +#else +#define INTERNAL 3 +#endif +#define DEFAULT 1 +#define EXTERNAL 0 +#endif + +// undefine stdlib's abs if encountered +#ifdef abs +#undef abs +#endif + +#define min(a,b) ((a)<(b)?(a):(b)) +#define max(a,b) ((a)>(b)?(a):(b)) +#define abs(x) ((x)>0?(x):-(x)) +#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) +#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5)) +#define radians(deg) ((deg)*DEG_TO_RAD) +#define degrees(rad) ((rad)*RAD_TO_DEG) +#define sq(x) ((x)*(x)) + +#define interrupts() sei() +#define noInterrupts() cli() + +#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L ) +#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() ) +#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() ) + +#define lowByte(w) ((uint8_t) ((w) & 0xff)) +#define highByte(w) ((uint8_t) ((w) >> 8)) + +#define bitRead(value, bit) (((value) >> (bit)) & 0x01) +#define bitSet(value, bit) ((value) |= (1UL << (bit))) +#define bitClear(value, bit) ((value) &= ~(1UL << (bit))) +#define bitToggle(value, bit) ((value) ^= (1UL << (bit))) +#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet(value, bit) : bitClear(value, bit)) + +// avr-libc defines _NOP() since 1.6.2 +#ifndef _NOP +#define _NOP() do { __asm__ volatile ("nop"); } while (0) +#endif + +typedef unsigned int word; + +#define bit(b) (1UL << (b)) + +typedef bool boolean; +typedef uint8_t byte; + +void init(void); +void initVariant(void); + +int atexit(void (*func)()) __attribute__((weak)); + +void pinMode(uint8_t pin, uint8_t mode); +void digitalWrite(uint8_t pin, uint8_t val); +int digitalRead(uint8_t pin); +int analogRead(uint8_t pin); +void analogReference(uint8_t mode); +void analogWrite(uint8_t pin, int val); + +unsigned long millis(void); +unsigned long micros(void); +void delay(unsigned long ms); +void delayMicroseconds(unsigned int us); +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout); +unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout); + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); +uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder); + +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode); +void detachInterrupt(uint8_t interruptNum); + +void setup(void); +void loop(void); + +// Get the bit location within the hardware port of the given virtual pin. +// This comes from the pins_*.c file for the active board configuration. + +#define analogInPinToBit(P) (P) + +// On the ATmega1280, the addresses of some of the port registers are +// greater than 255, so we can't store them in uint8_t's. +extern const uint16_t PROGMEM port_to_mode_PGM[]; +extern const uint16_t PROGMEM port_to_input_PGM[]; +extern const uint16_t PROGMEM port_to_output_PGM[]; + +extern const uint8_t PROGMEM digital_pin_to_port_PGM[]; +// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[]; +extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[]; +extern const uint8_t PROGMEM digital_pin_to_timer_PGM[]; + +// Get the bit location within the hardware port of the given virtual pin. +// This comes from the pins_*.c file for the active board configuration. +// +// These perform slightly better as macros compared to inline functions +// +#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) ) +#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) ) +#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) ) +#define analogInPinToBit(P) (P) +#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) ) +#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) ) +#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) ) + +#define NOT_A_PIN 0 +#define NOT_A_PORT 0 + +#define NOT_AN_INTERRUPT -1 + +#ifdef ARDUINO_MAIN +#define PA 1 +#define PB 2 +#define PC 3 +#define PD 4 +#define PE 5 +#define PF 6 +#define PG 7 +#define PH 8 +#define PJ 10 +#define PK 11 +#define PL 12 +#endif + +#define NOT_ON_TIMER 0 +#define TIMER0A 1 +#define TIMER0B 2 +#define TIMER1A 3 +#define TIMER1B 4 +#define TIMER1C 5 +#define TIMER2 6 +#define TIMER2A 7 +#define TIMER2B 8 + +#define TIMER3A 9 +#define TIMER3B 10 +#define TIMER3C 11 +#define TIMER4A 12 +#define TIMER4B 13 +#define TIMER4C 14 +#define TIMER4D 15 +#define TIMER5A 16 +#define TIMER5B 17 +#define TIMER5C 18 + +#ifdef __cplusplus +} // extern "C" +#endif + +#ifdef __cplusplus +#include "WCharacter.h" +#include "WString.h" +#include "HardwareSerial.h" +#include "USBAPI.h" +#if defined(HAVE_HWSERIAL0) && defined(HAVE_CDCSERIAL) +#error "Targets with both UART0 and CDC serial not supported" +#endif + +uint16_t makeWord(uint16_t w); +uint16_t makeWord(byte h, byte l); + +#define word(...) makeWord(__VA_ARGS__) + +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); +unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L); + +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0); +void noTone(uint8_t _pin); + +// WMath prototypes +long random(long); +long random(long, long); +void randomSeed(unsigned long); +long map(long, long, long, long, long); + +#endif + +#include "pins_arduino.h" + +#endif diff --git a/cores/arduino/CDC.cpp b/cores/arduino/CDC.cpp new file mode 100644 index 0000000..4fe3bf5 --- /dev/null +++ b/cores/arduino/CDC.cpp @@ -0,0 +1,310 @@ + + +/* Copyright (c) 2011, Peter Barrett +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#include "USBAPI.h" +#include +#include + +#if defined(USBCON) + +#ifndef CDC_ENABLED + +#warning "! Disabled serial console via USB (CDC)!" +#warning "! With this change you'll have to use the Arduino's reset button/pin to flash (upload)!" + +#else // CDC not disabled + +typedef struct +{ + u32 dwDTERate; + u8 bCharFormat; + u8 bParityType; + u8 bDataBits; + u8 lineState; +} LineInfo; + +static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 }; +static volatile int32_t breakValue = -1; + +static u8 wdtcsr_save; + +#define WEAK __attribute__ ((weak)) + +extern const CDCDescriptor _cdcInterface PROGMEM; +const CDCDescriptor _cdcInterface = +{ + D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0), + + // CDC communication interface + D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0), + D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd) + D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not) + D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported + D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0 + D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40), + + // CDC data interface + D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0), + D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0), + D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,USB_EP_SIZE,0) +}; + +bool isLUFAbootloader() +{ + return pgm_read_word(FLASHEND - 1) == NEW_LUFA_SIGNATURE; +} + +int CDC_GetInterface(u8* interfaceNum) +{ + interfaceNum[0] += 2; // uses 2 + return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface)); +} + +bool CDC_Setup(USBSetup& setup) +{ + u8 r = setup.bRequest; + u8 requestType = setup.bmRequestType; + + if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType) + { + if (CDC_GET_LINE_CODING == r) + { + USB_SendControl(0,(void*)&_usbLineInfo,7); + return true; + } + } + + if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType) + { + if (CDC_SEND_BREAK == r) + { + breakValue = ((uint16_t)setup.wValueH << 8) | setup.wValueL; + } + + if (CDC_SET_LINE_CODING == r) + { + USB_RecvControl((void*)&_usbLineInfo,7); + } + + if (CDC_SET_CONTROL_LINE_STATE == r) + { + _usbLineInfo.lineState = setup.wValueL; + + // auto-reset into the bootloader is triggered when the port, already + // open at 1200 bps, is closed. this is the signal to start the watchdog + // with a relatively long period so it can finish housekeeping tasks + // like servicing endpoints before the sketch ends + + uint16_t magic_key_pos = MAGIC_KEY_POS; + +// If we don't use the new RAMEND directly, check manually if we have a newer bootloader. +// This is used to keep compatible with the old leonardo bootloaders. +// You are still able to set the magic key position manually to RAMEND-1 to save a few bytes for this check. +#if MAGIC_KEY_POS != (RAMEND-1) + // For future boards save the key in the inproblematic RAMEND + // Which is reserved for the main() return value (which will never return) + if (isLUFAbootloader()) { + // hooray, we got a new bootloader! + magic_key_pos = (RAMEND-1); + } +#endif + + // We check DTR state to determine if host port is open (bit 0 of lineState). + if (1200 == _usbLineInfo.dwDTERate && (_usbLineInfo.lineState & 0x01) == 0) + { +#if MAGIC_KEY_POS != (RAMEND-1) + // Backup ram value if its not a newer bootloader and it hasn't already been saved. + // This should avoid memory corruption at least a bit, not fully + if (magic_key_pos != (RAMEND-1) && *(uint16_t *)magic_key_pos != MAGIC_KEY) { + *(uint16_t *)(RAMEND-1) = *(uint16_t *)magic_key_pos; + } +#endif + // Store boot key + *(uint16_t *)magic_key_pos = MAGIC_KEY; + // Save the watchdog state in case the reset is aborted. + wdtcsr_save = WDTCSR; + wdt_enable(WDTO_120MS); + } + else if (*(uint16_t *)magic_key_pos == MAGIC_KEY) + { + // Most OSs do some intermediate steps when configuring ports and DTR can + // twiggle more than once before stabilizing. + // To avoid spurious resets we set the watchdog to 120ms and eventually + // cancel if DTR goes back high. + // Cancellation is only done if an auto-reset was started, which is + // indicated by the magic key having been set. + + wdt_reset(); + // Restore the watchdog state in case the sketch was using it. + WDTCSR |= (1<= 0) { + return 1 + USB_Available(CDC_RX); + } + return USB_Available(CDC_RX); +} + +int Serial_::peek(void) +{ + if (peek_buffer < 0) + peek_buffer = USB_Recv(CDC_RX); + return peek_buffer; +} + +int Serial_::read(void) +{ + if (peek_buffer >= 0) { + int c = peek_buffer; + peek_buffer = -1; + return c; + } + return USB_Recv(CDC_RX); +} + +int Serial_::availableForWrite(void) +{ + return USB_SendSpace(CDC_TX); +} + +void Serial_::flush(void) +{ + USB_Flush(CDC_TX); +} + +size_t Serial_::write(uint8_t c) +{ + return write(&c, 1); +} + +size_t Serial_::write(const uint8_t *buffer, size_t size) +{ + /* only try to send bytes if the high-level CDC connection itself + is open (not just the pipe) - the OS should set lineState when the port + is opened and clear lineState when the port is closed. + bytes sent before the user opens the connection or after + the connection is closed are lost - just like with a UART. */ + + // TODO - ZE - check behavior on different OSes and test what happens if an + // open connection isn't broken cleanly (cable is yanked out, host dies + // or locks up, or host virtual serial port hangs) + if (_usbLineInfo.lineState > 0) { + int r = USB_Send(CDC_TX,buffer,size); + if (r > 0) { + return r; + } else { + setWriteError(); + return 0; + } + } + setWriteError(); + return 0; +} + +// This operator is a convenient way for a sketch to check whether the +// port has actually been configured and opened by the host (as opposed +// to just being connected to the host). It can be used, for example, in +// setup() before printing to ensure that an application on the host is +// actually ready to receive and display the data. +// We add a short delay before returning to fix a bug observed by Federico +// where the port is configured (lineState != 0) but not quite opened. +Serial_::operator bool() { + bool result = false; + if (_usbLineInfo.lineState > 0) + result = true; + delay(10); + return result; +} + +unsigned long Serial_::baud() { + // Disable interrupts while reading a multi-byte value + uint32_t baudrate; + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + baudrate = _usbLineInfo.dwDTERate; + } + return baudrate; +} + +uint8_t Serial_::stopbits() { + return _usbLineInfo.bCharFormat; +} + +uint8_t Serial_::paritytype() { + return _usbLineInfo.bParityType; +} + +uint8_t Serial_::numbits() { + return _usbLineInfo.bDataBits; +} + +bool Serial_::dtr() { + return _usbLineInfo.lineState & 0x1; +} + +bool Serial_::rts() { + return _usbLineInfo.lineState & 0x2; +} + +int32_t Serial_::readBreak() { + int32_t ret; + // Disable IRQs while reading and clearing breakValue to make + // sure we don't overwrite a value just set by the ISR. + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + ret = breakValue; + breakValue = -1; + } + return ret; +} + +Serial_ Serial; + +#endif /* if defined(CDC_ENABLED) */ +#endif /* if defined(USBCON) */ diff --git a/cores/arduino/Client.h b/cores/arduino/Client.h new file mode 100644 index 0000000..b8e5d93 --- /dev/null +++ b/cores/arduino/Client.h @@ -0,0 +1,45 @@ +/* + Client.h - Base class that provides Client + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef client_h +#define client_h +#include "Print.h" +#include "Stream.h" +#include "IPAddress.h" + +class Client : public Stream { + +public: + virtual int connect(IPAddress ip, uint16_t port) =0; + virtual int connect(const char *host, uint16_t port) =0; + virtual size_t write(uint8_t) =0; + virtual size_t write(const uint8_t *buf, size_t size) =0; + virtual int available() = 0; + virtual int read() = 0; + virtual int read(uint8_t *buf, size_t size) = 0; + virtual int peek() = 0; + virtual void flush() = 0; + virtual void stop() = 0; + virtual uint8_t connected() = 0; + virtual operator bool() = 0; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp new file mode 100644 index 0000000..561127f --- /dev/null +++ b/cores/arduino/HardwareSerial.cpp @@ -0,0 +1,281 @@ +/* + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include +#include +#include +#include +#include +#include "Arduino.h" + +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// this next line disables the entire HardwareSerial.cpp, +// this is so I can support Attiny series and any other chip without a uart +#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) + +// SerialEvent functions are weak, so when the user doesn't define them, +// the linker just sets their address to 0 (which is checked below). +// The Serialx_available is just a wrapper around Serialx.available(), +// but we can refer to it weakly so we don't pull in the entire +// HardwareSerial instance if the user doesn't also refer to it. +#if defined(HAVE_HWSERIAL0) + void serialEvent() __attribute__((weak)); + bool Serial0_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL1) + void serialEvent1() __attribute__((weak)); + bool Serial1_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL2) + void serialEvent2() __attribute__((weak)); + bool Serial2_available() __attribute__((weak)); +#endif + +#if defined(HAVE_HWSERIAL3) + void serialEvent3() __attribute__((weak)); + bool Serial3_available() __attribute__((weak)); +#endif + +void serialEventRun(void) +{ +#if defined(HAVE_HWSERIAL0) + if (Serial0_available && serialEvent && Serial0_available()) serialEvent(); +#endif +#if defined(HAVE_HWSERIAL1) + if (Serial1_available && serialEvent1 && Serial1_available()) serialEvent1(); +#endif +#if defined(HAVE_HWSERIAL2) + if (Serial2_available && serialEvent2 && Serial2_available()) serialEvent2(); +#endif +#if defined(HAVE_HWSERIAL3) + if (Serial3_available && serialEvent3 && Serial3_available()) serialEvent3(); +#endif +} + +// macro to guard critical sections when needed for large TX buffer sizes +#if (SERIAL_TX_BUFFER_SIZE>256) +#define TX_BUFFER_ATOMIC ATOMIC_BLOCK(ATOMIC_RESTORESTATE) +#else +#define TX_BUFFER_ATOMIC +#endif + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +void HardwareSerial::_tx_udr_empty_irq(void) +{ + // If interrupts are enabled, there must be more data in the output + // buffer. Send the next byte + unsigned char c = _tx_buffer[_tx_buffer_tail]; + _tx_buffer_tail = (_tx_buffer_tail + 1) % SERIAL_TX_BUFFER_SIZE; + + *_udr = c; + + // clear the TXC bit -- "can be cleared by writing a one to its bit + // location". This makes sure flush() won't return until the bytes + // actually got written. Other r/w bits are preserved, and zeroes + // written to the rest. + +#ifdef MPCM0 + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0); +#else + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << TXC0))); +#endif + + if (_tx_buffer_head == _tx_buffer_tail) { + // Buffer empty, so disable interrupts + cbi(*_ucsrb, UDRIE0); + } +} + +// Public Methods ////////////////////////////////////////////////////////////// + +void HardwareSerial::begin(unsigned long baud, byte config) +{ + // Try u2x mode first + uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2; + *_ucsra = 1 << U2X0; + + // hardcoded exception for 57600 for compatibility with the bootloader + // shipped with the Duemilanove and previous boards and the firmware + // on the 8U2 on the Uno and Mega 2560. Also, The baud_setting cannot + // be > 4095, so switch back to non-u2x mode if the baud rate is too + // low. + if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095)) + { + *_ucsra = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } + + // assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register) + *_ubrrh = baud_setting >> 8; + *_ubrrl = baud_setting; + + _written = false; + + //set the data bits, parity, and stop bits +#if defined(__AVR_ATmega8__) + config |= 0x80; // select UCSRC register (shared with UBRRH) +#endif + *_ucsrc = config; + + sbi(*_ucsrb, RXEN0); + sbi(*_ucsrb, TXEN0); + sbi(*_ucsrb, RXCIE0); + cbi(*_ucsrb, UDRIE0); +} + +void HardwareSerial::end() +{ + // wait for transmission of outgoing data + flush(); + + cbi(*_ucsrb, RXEN0); + cbi(*_ucsrb, TXEN0); + cbi(*_ucsrb, RXCIE0); + cbi(*_ucsrb, UDRIE0); + + // clear any received data + _rx_buffer_head = _rx_buffer_tail; +} + +int HardwareSerial::available(void) +{ + return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE; +} + +int HardwareSerial::peek(void) +{ + if (_rx_buffer_head == _rx_buffer_tail) { + return -1; + } else { + return _rx_buffer[_rx_buffer_tail]; + } +} + +int HardwareSerial::read(void) +{ + // if the head isn't ahead of the tail, we don't have any characters + if (_rx_buffer_head == _rx_buffer_tail) { + return -1; + } else { + unsigned char c = _rx_buffer[_rx_buffer_tail]; + _rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE; + return c; + } +} + +int HardwareSerial::availableForWrite(void) +{ + tx_buffer_index_t head; + tx_buffer_index_t tail; + + TX_BUFFER_ATOMIC { + head = _tx_buffer_head; + tail = _tx_buffer_tail; + } + if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail; + return tail - head - 1; +} + +void HardwareSerial::flush() +{ + // If we have never written a byte, no need to flush. This special + // case is needed since there is no way to force the TXC (transmit + // complete) bit to 1 during initialization + if (!_written) + return; + + while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) { + if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0)) + // Interrupts are globally disabled, but the DR empty + // interrupt should be enabled, so poll the DR empty flag to + // prevent deadlock + if (bit_is_set(*_ucsra, UDRE0)) + _tx_udr_empty_irq(); + } + // If we get here, nothing is queued anymore (DRIE is disabled) and + // the hardware finished transmission (TXC is set). +} + +size_t HardwareSerial::write(uint8_t c) +{ + _written = true; + // If the buffer and the data register is empty, just write the byte + // to the data register and be done. This shortcut helps + // significantly improve the effective datarate at high (> + // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown. + if (_tx_buffer_head == _tx_buffer_tail && bit_is_set(*_ucsra, UDRE0)) { + // If TXC is cleared before writing UDR and the previous byte + // completes before writing to UDR, TXC will be set but a byte + // is still being transmitted causing flush() to return too soon. + // So writing UDR must happen first. + // Writing UDR and clearing TC must be done atomically, otherwise + // interrupts might delay the TXC clear so the byte written to UDR + // is transmitted (setting TXC) before clearing TXC. Then TXC will + // be cleared when no bytes are left, causing flush() to hang + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + *_udr = c; +#ifdef MPCM0 + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << MPCM0))) | (1 << TXC0); +#else + *_ucsra = ((*_ucsra) & ((1 << U2X0) | (1 << TXC0))); +#endif + } + return 1; + } + tx_buffer_index_t i = (_tx_buffer_head + 1) % SERIAL_TX_BUFFER_SIZE; + + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + while (i == _tx_buffer_tail) { + if (bit_is_clear(SREG, SREG_I)) { + // Interrupts are disabled, so we'll have to poll the data + // register empty flag ourselves. If it is set, pretend an + // interrupt has happened and call the handler to free up + // space for us. + if(bit_is_set(*_ucsra, UDRE0)) + _tx_udr_empty_irq(); + } else { + // nop, the interrupt handler will free up space for us + } + } + + _tx_buffer[_tx_buffer_head] = c; + + // make atomic to prevent execution of ISR between setting the + // head pointer and setting the interrupt flag resulting in buffer + // retransmission + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + _tx_buffer_head = i; + sbi(*_ucsrb, UDRIE0); + } + + return 1; +} + +#endif // whole file diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h new file mode 100644 index 0000000..6ff29d0 --- /dev/null +++ b/cores/arduino/HardwareSerial.h @@ -0,0 +1,161 @@ +/* + HardwareSerial.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#ifndef HardwareSerial_h +#define HardwareSerial_h + +#include + +#include "Stream.h" + +// Define constants and variables for buffering incoming serial data. We're +// using a ring buffer (I think), in which head is the index of the location +// to which to write the next incoming character and tail is the index of the +// location from which to read. +// NOTE: a "power of 2" buffer size is recommended to dramatically +// optimize all the modulo operations for ring buffers. +// WARNING: When buffer sizes are increased to > 256, the buffer index +// variables are automatically increased in size, but the extra +// atomicity guards needed for that are not implemented. This will +// often work, but occasionally a race condition can occur that makes +// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405 +#if !defined(SERIAL_TX_BUFFER_SIZE) +#if ((RAMEND - RAMSTART) < 1023) +#define SERIAL_TX_BUFFER_SIZE 16 +#else +#define SERIAL_TX_BUFFER_SIZE 64 +#endif +#endif +#if !defined(SERIAL_RX_BUFFER_SIZE) +#if ((RAMEND - RAMSTART) < 1023) +#define SERIAL_RX_BUFFER_SIZE 16 +#else +#define SERIAL_RX_BUFFER_SIZE 64 +#endif +#endif +#if (SERIAL_TX_BUFFER_SIZE>256) +typedef uint16_t tx_buffer_index_t; +#else +typedef uint8_t tx_buffer_index_t; +#endif +#if (SERIAL_RX_BUFFER_SIZE>256) +typedef uint16_t rx_buffer_index_t; +#else +typedef uint8_t rx_buffer_index_t; +#endif + +// Define config for Serial.begin(baud, config); +#define SERIAL_5N1 0x00 +#define SERIAL_6N1 0x02 +#define SERIAL_7N1 0x04 +#define SERIAL_8N1 0x06 +#define SERIAL_5N2 0x08 +#define SERIAL_6N2 0x0A +#define SERIAL_7N2 0x0C +#define SERIAL_8N2 0x0E +#define SERIAL_5E1 0x20 +#define SERIAL_6E1 0x22 +#define SERIAL_7E1 0x24 +#define SERIAL_8E1 0x26 +#define SERIAL_5E2 0x28 +#define SERIAL_6E2 0x2A +#define SERIAL_7E2 0x2C +#define SERIAL_8E2 0x2E +#define SERIAL_5O1 0x30 +#define SERIAL_6O1 0x32 +#define SERIAL_7O1 0x34 +#define SERIAL_8O1 0x36 +#define SERIAL_5O2 0x38 +#define SERIAL_6O2 0x3A +#define SERIAL_7O2 0x3C +#define SERIAL_8O2 0x3E + +class HardwareSerial : public Stream +{ + protected: + volatile uint8_t * const _ubrrh; + volatile uint8_t * const _ubrrl; + volatile uint8_t * const _ucsra; + volatile uint8_t * const _ucsrb; + volatile uint8_t * const _ucsrc; + volatile uint8_t * const _udr; + // Has any byte been written to the UART since begin() + bool _written; + + volatile rx_buffer_index_t _rx_buffer_head; + volatile rx_buffer_index_t _rx_buffer_tail; + volatile tx_buffer_index_t _tx_buffer_head; + volatile tx_buffer_index_t _tx_buffer_tail; + + // Don't put any members after these buffers, since only the first + // 32 bytes of this struct can be accessed quickly using the ldd + // instruction. + unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE]; + unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE]; + + public: + inline HardwareSerial( + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr); + void begin(unsigned long baud) { begin(baud, SERIAL_8N1); } + void begin(unsigned long, uint8_t); + void end(); + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + inline size_t write(unsigned long n) { return write((uint8_t)n); } + inline size_t write(long n) { return write((uint8_t)n); } + inline size_t write(unsigned int n) { return write((uint8_t)n); } + inline size_t write(int n) { return write((uint8_t)n); } + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool() { return true; } + + // Interrupt handlers - Not intended to be called externally + inline void _rx_complete_irq(void); + void _tx_udr_empty_irq(void); +}; + +#if defined(UBRRH) || defined(UBRR0H) + extern HardwareSerial Serial; + #define HAVE_HWSERIAL0 +#endif +#if defined(UBRR1H) + extern HardwareSerial Serial1; + #define HAVE_HWSERIAL1 +#endif +#if defined(UBRR2H) + extern HardwareSerial Serial2; + #define HAVE_HWSERIAL2 +#endif +#if defined(UBRR3H) + extern HardwareSerial Serial3; + #define HAVE_HWSERIAL3 +#endif + +extern void serialEventRun(void) __attribute__((weak)); + +#endif diff --git a/cores/arduino/HardwareSerial0.cpp b/cores/arduino/HardwareSerial0.cpp new file mode 100644 index 0000000..7d47ed2 --- /dev/null +++ b/cores/arduino/HardwareSerial0.cpp @@ -0,0 +1,79 @@ +/* + HardwareSerial0.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL0) + +#if defined(USART_RX_vect) + ISR(USART_RX_vect) +#elif defined(USART0_RX_vect) + ISR(USART0_RX_vect) +#elif defined(USART_RXC_vect) + ISR(USART_RXC_vect) // ATmega8 +#else + #error "Don't know what the Data Received vector is called for Serial" +#endif + { + Serial._rx_complete_irq(); + } + +#if defined(UART0_UDRE_vect) +ISR(UART0_UDRE_vect) +#elif defined(UART_UDRE_vect) +ISR(UART_UDRE_vect) +#elif defined(USART0_UDRE_vect) +ISR(USART0_UDRE_vect) +#elif defined(USART_UDRE_vect) +ISR(USART_UDRE_vect) +#else + #error "Don't know what the Data Register Empty vector is called for Serial" +#endif +{ + Serial._tx_udr_empty_irq(); +} + +#if defined(UBRRH) && defined(UBRRL) + HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR); +#else + HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0); +#endif + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial0_available() { + return Serial.available(); +} + +#endif // HAVE_HWSERIAL0 diff --git a/cores/arduino/HardwareSerial1.cpp b/cores/arduino/HardwareSerial1.cpp new file mode 100644 index 0000000..a345cdb --- /dev/null +++ b/cores/arduino/HardwareSerial1.cpp @@ -0,0 +1,69 @@ +/* + HardwareSerial1.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL1) + +#if defined(UART1_RX_vect) +ISR(UART1_RX_vect) +#elif defined(USART1_RX_vect) +ISR(USART1_RX_vect) +#else +#error "Don't know what the Data Register Empty vector is called for Serial1" +#endif +{ + Serial1._rx_complete_irq(); +} + +#if defined(UART1_UDRE_vect) +ISR(UART1_UDRE_vect) +#elif defined(USART1_UDRE_vect) +ISR(USART1_UDRE_vect) +#else +#error "Don't know what the Data Register Empty vector is called for Serial1" +#endif +{ + Serial1._tx_udr_empty_irq(); +} + +HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial1_available() { + return Serial1.available(); +} + +#endif // HAVE_HWSERIAL1 diff --git a/cores/arduino/HardwareSerial2.cpp b/cores/arduino/HardwareSerial2.cpp new file mode 100644 index 0000000..8e433b6 --- /dev/null +++ b/cores/arduino/HardwareSerial2.cpp @@ -0,0 +1,57 @@ +/* + HardwareSerial2.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL2) + +ISR(USART2_RX_vect) +{ + Serial2._rx_complete_irq(); +} + +ISR(USART2_UDRE_vect) +{ + Serial2._tx_udr_empty_irq(); +} + +HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial2_available() { + return Serial2.available(); +} + +#endif // HAVE_HWSERIAL2 diff --git a/cores/arduino/HardwareSerial3.cpp b/cores/arduino/HardwareSerial3.cpp new file mode 100644 index 0000000..26aaee8 --- /dev/null +++ b/cores/arduino/HardwareSerial3.cpp @@ -0,0 +1,57 @@ +/* + HardwareSerial3.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus + Modified 3 December 2013 by Matthijs Kooijman +*/ + +#include "Arduino.h" +#include "HardwareSerial.h" +#include "HardwareSerial_private.h" + +// Each HardwareSerial is defined in its own file, since the linker pulls +// in the entire file when any element inside is used. --gc-sections can +// additionally cause unused symbols to be dropped, but ISRs have the +// "used" attribute so are never dropped and they keep the +// HardwareSerial instance in as well. Putting each instance in its own +// file prevents the linker from pulling in any unused instances in the +// first place. + +#if defined(HAVE_HWSERIAL3) + +ISR(USART3_RX_vect) +{ + Serial3._rx_complete_irq(); +} + +ISR(USART3_UDRE_vect) +{ + Serial3._tx_udr_empty_irq(); +} + +HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3); + +// Function that can be weakly referenced by serialEventRun to prevent +// pulling in this file if it's not otherwise used. +bool Serial3_available() { + return Serial3.available(); +} + +#endif // HAVE_HWSERIAL3 diff --git a/cores/arduino/HardwareSerial_private.h b/cores/arduino/HardwareSerial_private.h new file mode 100644 index 0000000..2e23cec --- /dev/null +++ b/cores/arduino/HardwareSerial_private.h @@ -0,0 +1,123 @@ +/* + HardwareSerial_private.h - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 28 September 2010 by Mark Sproul + Modified 14 August 2012 by Alarus +*/ + +#include "wiring_private.h" + +// this next line disables the entire HardwareSerial.cpp, +// this is so I can support Attiny series and any other chip without a uart +#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3) + +// Ensure that the various bit positions we use are available with a 0 +// postfix, so we can always use the values for UART0 for all UARTs. The +// alternative, passing the various values for each UART to the +// HardwareSerial constructor also works, but makes the code bigger and +// slower. +#if !defined(TXC0) +#if defined(TXC) +// Some chips like ATmega8 don't have UPE, only PE. The other bits are +// named as expected. +#if !defined(UPE) && defined(PE) +#define UPE PE +#endif +// On ATmega8, the uart and its bits are not numbered, so there is no TXC0 etc. +#define TXC0 TXC +#define RXEN0 RXEN +#define TXEN0 TXEN +#define RXCIE0 RXCIE +#define UDRIE0 UDRIE +#define U2X0 U2X +#define UPE0 UPE +#define UDRE0 UDRE +#elif defined(TXC1) +// Some devices have uart1 but no uart0 +#define TXC0 TXC1 +#define RXEN0 RXEN1 +#define TXEN0 TXEN1 +#define RXCIE0 RXCIE1 +#define UDRIE0 UDRIE1 +#define U2X0 U2X1 +#define UPE0 UPE1 +#define UDRE0 UDRE1 +#else +#error No UART found in HardwareSerial.cpp +#endif +#endif // !defined TXC0 + +// Check at compile time that it is really ok to use the bit positions of +// UART0 for the other UARTs as well, in case these values ever get +// changed for future hardware. +#if defined(TXC1) && (TXC1 != TXC0 || RXEN1 != RXEN0 || RXCIE1 != RXCIE0 || \ + UDRIE1 != UDRIE0 || U2X1 != U2X0 || UPE1 != UPE0 || \ + UDRE1 != UDRE0) +#error "Not all bit positions for UART1 are the same as for UART0" +#endif +#if defined(TXC2) && (TXC2 != TXC0 || RXEN2 != RXEN0 || RXCIE2 != RXCIE0 || \ + UDRIE2 != UDRIE0 || U2X2 != U2X0 || UPE2 != UPE0 || \ + UDRE2 != UDRE0) +#error "Not all bit positions for UART2 are the same as for UART0" +#endif +#if defined(TXC3) && (TXC3 != TXC0 || RXEN3 != RXEN0 || RXCIE3 != RXCIE0 || \ + UDRIE3 != UDRIE0 || U3X3 != U3X0 || UPE3 != UPE0 || \ + UDRE3 != UDRE0) +#error "Not all bit positions for UART3 are the same as for UART0" +#endif + +// Constructors //////////////////////////////////////////////////////////////// + +HardwareSerial::HardwareSerial( + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr) : + _ubrrh(ubrrh), _ubrrl(ubrrl), + _ucsra(ucsra), _ucsrb(ucsrb), _ucsrc(ucsrc), + _udr(udr), + _rx_buffer_head(0), _rx_buffer_tail(0), + _tx_buffer_head(0), _tx_buffer_tail(0) +{ +} + +// Actual interrupt handlers ////////////////////////////////////////////////////////////// + +void HardwareSerial::_rx_complete_irq(void) +{ + if (bit_is_clear(*_ucsra, UPE0)) { + // No Parity error, read byte and store it in the buffer if there is + // room + unsigned char c = *_udr; + rx_buffer_index_t i = (unsigned int)(_rx_buffer_head + 1) % SERIAL_RX_BUFFER_SIZE; + + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != _rx_buffer_tail) { + _rx_buffer[_rx_buffer_head] = c; + _rx_buffer_head = i; + } + } else { + // Parity error, read byte but discard it + *_udr; + }; +} + +#endif // whole file diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp new file mode 100644 index 0000000..d9fe5be --- /dev/null +++ b/cores/arduino/IPAddress.cpp @@ -0,0 +1,114 @@ +/* + IPAddress.cpp - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include +#include + +IPAddress::IPAddress() +{ + _address.dword = 0; +} + +IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet) +{ + _address.bytes[0] = first_octet; + _address.bytes[1] = second_octet; + _address.bytes[2] = third_octet; + _address.bytes[3] = fourth_octet; +} + +IPAddress::IPAddress(uint32_t address) +{ + _address.dword = address; +} + +IPAddress::IPAddress(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); +} + +bool IPAddress::fromString(const char *address) +{ + uint16_t acc = 0; // Accumulator + uint8_t dots = 0; + + while (*address) + { + char c = *address++; + if (c >= '0' && c <= '9') + { + acc = acc * 10 + (c - '0'); + if (acc > 255) { + // Value out of [0..255] range + return false; + } + } + else if (c == '.') + { + if (dots == 3) { + // Too much dots (there must be 3 dots) + return false; + } + _address.bytes[dots++] = acc; + acc = 0; + } + else + { + // Invalid char + return false; + } + } + + if (dots != 3) { + // Too few dots (there must be 3 dots) + return false; + } + _address.bytes[3] = acc; + return true; +} + +IPAddress& IPAddress::operator=(const uint8_t *address) +{ + memcpy(_address.bytes, address, sizeof(_address.bytes)); + return *this; +} + +IPAddress& IPAddress::operator=(uint32_t address) +{ + _address.dword = address; + return *this; +} + +bool IPAddress::operator==(const uint8_t* addr) const +{ + return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0; +} + +size_t IPAddress::printTo(Print& p) const +{ + size_t n = 0; + for (int i =0; i < 3; i++) + { + n += p.print(_address.bytes[i], DEC); + n += p.print('.'); + } + n += p.print(_address.bytes[3], DEC); + return n; +} + diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h new file mode 100644 index 0000000..d762f2c --- /dev/null +++ b/cores/arduino/IPAddress.h @@ -0,0 +1,78 @@ +/* + IPAddress.h - Base class that provides IPAddress + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef IPAddress_h +#define IPAddress_h + +#include +#include "Printable.h" +#include "WString.h" + +// A class to make it easier to handle and pass around IP addresses + +class IPAddress : public Printable { +private: + union { + uint8_t bytes[4]; // IPv4 address + uint32_t dword; + } _address; + + // Access the raw byte array containing the address. Because this returns a pointer + // to the internal structure rather than a copy of the address this function should only + // be used when you know that the usage of the returned uint8_t* will be transient and not + // stored. + uint8_t* raw_address() { return _address.bytes; }; + +public: + // Constructors + IPAddress(); + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); + IPAddress(uint32_t address); + IPAddress(const uint8_t *address); + + bool fromString(const char *address); + bool fromString(const String &address) { return fromString(address.c_str()); } + + // Overloaded cast operator to allow IPAddress objects to be used where a pointer + // to a four-byte uint8_t array is expected + operator uint32_t() const { return _address.dword; }; + bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; + bool operator==(const uint8_t* addr) const; + + // Overloaded index operator to allow getting and setting individual octets of the address + uint8_t operator[](int index) const { return _address.bytes[index]; }; + uint8_t& operator[](int index) { return _address.bytes[index]; }; + + // Overloaded copy operators to allow initialisation of IPAddress objects from other types + IPAddress& operator=(const uint8_t *address); + IPAddress& operator=(uint32_t address); + + virtual size_t printTo(Print& p) const; + + friend class EthernetClass; + friend class UDP; + friend class Client; + friend class Server; + friend class DhcpClass; + friend class DNSClient; +}; + +const IPAddress INADDR_NONE(0,0,0,0); + +#endif diff --git a/cores/arduino/PluggableUSB.cpp b/cores/arduino/PluggableUSB.cpp new file mode 100644 index 0000000..c489d9f --- /dev/null +++ b/cores/arduino/PluggableUSB.cpp @@ -0,0 +1,115 @@ +/* + PluggableUSB.cpp + Copyright (c) 2015 Arduino LLC + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "USBAPI.h" +#include "PluggableUSB.h" + +#if defined(USBCON) +#ifdef PLUGGABLE_USB_ENABLED + +extern uint8_t _initEndpoints[]; + +int PluggableUSB_::getInterface(uint8_t* interfaceCount) +{ + int sent = 0; + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int res = node->getInterface(interfaceCount); + if (res < 0) + return -1; + sent += res; + } + return sent; +} + +int PluggableUSB_::getDescriptor(USBSetup& setup) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + int ret = node->getDescriptor(setup); + // ret!=0 -> request has been processed + if (ret) + return ret; + } + return 0; +} + +void PluggableUSB_::getShortName(char *iSerialNum) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + iSerialNum += node->getShortName(iSerialNum); + } + *iSerialNum = 0; +} + +bool PluggableUSB_::setup(USBSetup& setup) +{ + PluggableUSBModule* node; + for (node = rootNode; node; node = node->next) { + if (node->setup(setup)) { + return true; + } + } + return false; +} + +bool PluggableUSB_::plug(PluggableUSBModule *node) +{ + if ((lastEp + node->numEndpoints) > USB_ENDPOINTS) { + return false; + } + + if (!rootNode) { + rootNode = node; + } else { + PluggableUSBModule *current = rootNode; + while (current->next) { + current = current->next; + } + current->next = node; + } + + node->pluggedInterface = lastIf; + node->pluggedEndpoint = lastEp; + lastIf += node->numInterfaces; + for (uint8_t i = 0; i < node->numEndpoints; i++) { + _initEndpoints[lastEp] = node->endpointType[i]; + lastEp++; + } + return true; + // restart USB layer??? +} + +PluggableUSB_& PluggableUSB() +{ + static PluggableUSB_ obj; + return obj; +} + +PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT), + lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT), + rootNode(NULL) +{ + // Empty +} + +#endif + +#endif /* if defined(USBCON) */ diff --git a/cores/arduino/PluggableUSB.h b/cores/arduino/PluggableUSB.h new file mode 100644 index 0000000..507f0df --- /dev/null +++ b/cores/arduino/PluggableUSB.h @@ -0,0 +1,74 @@ +/* + PluggableUSB.h + Copyright (c) 2015 Arduino LLC + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef PUSB_h +#define PUSB_h + +#include "USBAPI.h" +#include + +#if defined(USBCON) + +class PluggableUSBModule { +public: + PluggableUSBModule(uint8_t numEps, uint8_t numIfs, uint8_t *epType) : + numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) + { } + +protected: + virtual bool setup(USBSetup& setup) = 0; + virtual int getInterface(uint8_t* interfaceCount) = 0; + virtual int getDescriptor(USBSetup& setup) = 0; + virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; } + + uint8_t pluggedInterface; + uint8_t pluggedEndpoint; + + const uint8_t numEndpoints; + const uint8_t numInterfaces; + const uint8_t *endpointType; + + PluggableUSBModule *next = NULL; + + friend class PluggableUSB_; +}; + +class PluggableUSB_ { +public: + PluggableUSB_(); + bool plug(PluggableUSBModule *node); + int getInterface(uint8_t* interfaceCount); + int getDescriptor(USBSetup& setup); + bool setup(USBSetup& setup); + void getShortName(char *iSerialNum); + +private: + uint8_t lastIf; + uint8_t lastEp; + PluggableUSBModule* rootNode; +}; + +// Replacement for global singleton. +// This function prevents static-initialization-order-fiasco +// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use +PluggableUSB_& PluggableUSB(); + +#endif + +#endif diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp new file mode 100644 index 0000000..1e4c99a --- /dev/null +++ b/cores/arduino/Print.cpp @@ -0,0 +1,266 @@ +/* + Print.cpp - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis + Modified 03 August 2015 by Chuck Todd + */ + +#include +#include +#include +#include +#include "Arduino.h" + +#include "Print.h" + +// Public Methods ////////////////////////////////////////////////////////////// + +/* default implementation: may be overridden */ +size_t Print::write(const uint8_t *buffer, size_t size) +{ + size_t n = 0; + while (size--) { + if (write(*buffer++)) n++; + else break; + } + return n; +} + +size_t Print::print(const __FlashStringHelper *ifsh) +{ + PGM_P p = reinterpret_cast(ifsh); + size_t n = 0; + while (1) { + unsigned char c = pgm_read_byte(p++); + if (c == 0) break; + if (write(c)) n++; + else break; + } + return n; +} + +size_t Print::print(const String &s) +{ + return write(s.c_str(), s.length()); +} + +size_t Print::print(const char str[]) +{ + return write(str); +} + +size_t Print::print(char c) +{ + return write(c); +} + +size_t Print::print(unsigned char b, int base) +{ + return print((unsigned long) b, base); +} + +size_t Print::print(int n, int base) +{ + return print((long) n, base); +} + +size_t Print::print(unsigned int n, int base) +{ + return print((unsigned long) n, base); +} + +size_t Print::print(long n, int base) +{ + if (base == 0) { + return write(n); + } else if (base == 10) { + if (n < 0) { + int t = print('-'); + n = -n; + return printNumber(n, 10) + t; + } + return printNumber(n, 10); + } else { + return printNumber(n, base); + } +} + +size_t Print::print(unsigned long n, int base) +{ + if (base == 0) return write(n); + else return printNumber(n, base); +} + +size_t Print::print(double n, int digits) +{ + return printFloat(n, digits); +} + +size_t Print::println(const __FlashStringHelper *ifsh) +{ + size_t n = print(ifsh); + n += println(); + return n; +} + +size_t Print::print(const Printable& x) +{ + return x.printTo(*this); +} + +size_t Print::println(void) +{ + return write("\r\n"); +} + +size_t Print::println(const String &s) +{ + size_t n = print(s); + n += println(); + return n; +} + +size_t Print::println(const char c[]) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(char c) +{ + size_t n = print(c); + n += println(); + return n; +} + +size_t Print::println(unsigned char b, int base) +{ + size_t n = print(b, base); + n += println(); + return n; +} + +size_t Print::println(int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned int num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(unsigned long num, int base) +{ + size_t n = print(num, base); + n += println(); + return n; +} + +size_t Print::println(double num, int digits) +{ + size_t n = print(num, digits); + n += println(); + return n; +} + +size_t Print::println(const Printable& x) +{ + size_t n = print(x); + n += println(); + return n; +} + +// Private Methods ///////////////////////////////////////////////////////////// + +size_t Print::printNumber(unsigned long n, uint8_t base) +{ + char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. + char *str = &buf[sizeof(buf) - 1]; + + *str = '\0'; + + // prevent crash if called with base == 1 + if (base < 2) base = 10; + + do { + char c = n % base; + n /= base; + + *--str = c < 10 ? c + '0' : c + 'A' - 10; + } while(n); + + return write(str); +} + +size_t Print::printFloat(double number, uint8_t digits) +{ + size_t n = 0; + + if (isnan(number)) return print("nan"); + if (isinf(number)) return print("inf"); + if (number > 4294967040.0) return print ("ovf"); // constant determined empirically + if (number <-4294967040.0) return print ("ovf"); // constant determined empirically + + // Handle negative numbers + if (number < 0.0) + { + n += print('-'); + number = -number; + } + + // Round correctly so that print(1.999, 2) prints as "2.00" + double rounding = 0.5; + for (uint8_t i=0; i 0) { + n += print('.'); + } + + // Extract digits from the remainder one at a time + while (digits-- > 0) + { + remainder *= 10.0; + unsigned int toPrint = (unsigned int)(remainder); + n += print(toPrint); + remainder -= toPrint; + } + + return n; +} diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h new file mode 100644 index 0000000..0097cc1 --- /dev/null +++ b/cores/arduino/Print.h @@ -0,0 +1,93 @@ +/* + Print.h - Base class that provides print() and println() + Copyright (c) 2008 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Print_h +#define Print_h + +#include +#include // for size_t + +#include "WString.h" +#include "Printable.h" + +#define DEC 10 +#define HEX 16 +#define OCT 8 +#ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar +#undef BIN +#endif +#define BIN 2 + +class Print +{ + private: + int write_error; + size_t printNumber(unsigned long, uint8_t); + size_t printFloat(double, uint8_t); + protected: + void setWriteError(int err = 1) { write_error = err; } + public: + Print() : write_error(0) {} + + int getWriteError() { return write_error; } + void clearWriteError() { setWriteError(0); } + + virtual size_t write(uint8_t) = 0; + size_t write(const char *str) { + if (str == NULL) return 0; + return write((const uint8_t *)str, strlen(str)); + } + virtual size_t write(const uint8_t *buffer, size_t size); + size_t write(const char *buffer, size_t size) { + return write((const uint8_t *)buffer, size); + } + + // default to zero, meaning "a single write may block" + // should be overridden by subclasses with buffering + virtual int availableForWrite() { return 0; } + + size_t print(const __FlashStringHelper *); + size_t print(const String &); + size_t print(const char[]); + size_t print(char); + size_t print(unsigned char, int = DEC); + size_t print(int, int = DEC); + size_t print(unsigned int, int = DEC); + size_t print(long, int = DEC); + size_t print(unsigned long, int = DEC); + size_t print(double, int = 2); + size_t print(const Printable&); + + size_t println(const __FlashStringHelper *); + size_t println(const String &s); + size_t println(const char[]); + size_t println(char); + size_t println(unsigned char, int = DEC); + size_t println(int, int = DEC); + size_t println(unsigned int, int = DEC); + size_t println(long, int = DEC); + size_t println(unsigned long, int = DEC); + size_t println(double, int = 2); + size_t println(const Printable&); + size_t println(void); + + virtual void flush() { /* Empty implementation for backward compatibility */ } +}; + +#endif diff --git a/cores/arduino/Printable.h b/cores/arduino/Printable.h new file mode 100644 index 0000000..2a1b2e9 --- /dev/null +++ b/cores/arduino/Printable.h @@ -0,0 +1,40 @@ +/* + Printable.h - Interface class that allows printing of complex types + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Printable_h +#define Printable_h + +#include + +class Print; + +/** The Printable class provides a way for new classes to allow themselves to be printed. + By deriving from Printable and implementing the printTo method, it will then be possible + for users to print out instances of this class by passing them into the usual + Print::print and Print::println methods. +*/ + +class Printable +{ + public: + virtual size_t printTo(Print& p) const = 0; +}; + +#endif + diff --git a/cores/arduino/Server.h b/cores/arduino/Server.h new file mode 100644 index 0000000..69e3e39 --- /dev/null +++ b/cores/arduino/Server.h @@ -0,0 +1,30 @@ +/* + Server.h - Base class that provides Server + Copyright (c) 2011 Adrian McEwen. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef server_h +#define server_h + +#include "Print.h" + +class Server : public Print { +public: + virtual void begin() =0; +}; + +#endif diff --git a/cores/arduino/Stream.cpp b/cores/arduino/Stream.cpp new file mode 100644 index 0000000..9eff663 --- /dev/null +++ b/cores/arduino/Stream.cpp @@ -0,0 +1,318 @@ +/* + Stream.cpp - adds parsing methods to Stream class + Copyright (c) 2008 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Created July 2011 + parsing functions based on TextFinder library by Michael Margolis + + findMulti/findUntil routines written by Jim Leonard/Xuth + */ + +#include "Arduino.h" +#include "Stream.h" + +#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait + +// protected method to read stream with timeout +int Stream::timedRead() +{ + int c; + _startMillis = millis(); + do { + c = read(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// protected method to peek stream with timeout +int Stream::timedPeek() +{ + int c; + _startMillis = millis(); + do { + c = peek(); + if (c >= 0) return c; + } while(millis() - _startMillis < _timeout); + return -1; // -1 indicates timeout +} + +// returns peek of the next digit in the stream or -1 if timeout +// discards non-numeric characters +int Stream::peekNextDigit(LookaheadMode lookahead, bool detectDecimal) +{ + int c; + while (1) { + c = timedPeek(); + + if( c < 0 || + c == '-' || + (c >= '0' && c <= '9') || + (detectDecimal && c == '.')) return c; + + switch( lookahead ){ + case SKIP_NONE: return -1; // Fail code. + case SKIP_WHITESPACE: + switch( c ){ + case ' ': + case '\t': + case '\r': + case '\n': break; + default: return -1; // Fail code. + } + case SKIP_ALL: + break; + } + read(); // discard non-numeric + } +} + +// Public Methods +////////////////////////////////////////////////////////////// + +void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait +{ + _timeout = timeout; +} + + // find returns true if the target string is found +bool Stream::find(char *target) +{ + return findUntil(target, strlen(target), NULL, 0); +} + +// reads data from the stream until the target string of given length is found +// returns true if target string is found, false if timed out +bool Stream::find(char *target, size_t length) +{ + return findUntil(target, length, NULL, 0); +} + +// as find but search ends if the terminator string is found +bool Stream::findUntil(char *target, char *terminator) +{ + return findUntil(target, strlen(target), terminator, strlen(terminator)); +} + +// reads data from the stream until the target string of the given length is found +// search terminated if the terminator string is found +// returns true if target string is found, false if terminated or timed out +bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen) +{ + if (terminator == NULL) { + MultiTarget t[1] = {{target, targetLen, 0}}; + return findMulti(t, 1) == 0 ? true : false; + } else { + MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}}; + return findMulti(t, 2) == 0 ? true : false; + } +} + +// returns the first valid (long) integer value from the current position. +// lookahead determines how parseInt looks ahead in the stream. +// See LookaheadMode enumeration at the top of the file. +// Lookahead is terminated by the first character that is not a valid part of an integer. +// Once parsing commences, 'ignore' will be skipped in the stream. +long Stream::parseInt(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + long value = 0; + int c; + + c = peekNextDigit(lookahead, false); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore this character + else if(c == '-') + isNegative = true; + else if(c >= '0' && c <= '9') // is c a digit? + value = value * 10 + c - '0'; + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || c == ignore ); + + if(isNegative) + value = -value; + return value; +} + +// as parseInt but returns a floating point value +float Stream::parseFloat(LookaheadMode lookahead, char ignore) +{ + bool isNegative = false; + bool isFraction = false; + long value = 0; + int c; + float fraction = 1.0; + + c = peekNextDigit(lookahead, true); + // ignore non numeric leading characters + if(c < 0) + return 0; // zero returned if timeout + + do{ + if(c == ignore) + ; // ignore + else if(c == '-') + isNegative = true; + else if (c == '.') + isFraction = true; + else if(c >= '0' && c <= '9') { // is c a digit? + value = value * 10 + c - '0'; + if(isFraction) + fraction *= 0.1; + } + read(); // consume the character we got with peek + c = timedPeek(); + } + while( (c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore ); + + if(isNegative) + value = -value; + if(isFraction) + return value * fraction; + else + return value; +} + +// read characters from stream into buffer +// terminates if length characters have been read, or timeout (see setTimeout) +// returns the number of characters placed in the buffer +// the buffer is NOT null terminated. +// +size_t Stream::readBytes(char *buffer, size_t length) +{ + size_t count = 0; + while (count < length) { + int c = timedRead(); + if (c < 0) break; + *buffer++ = (char)c; + count++; + } + return count; +} + + +// as readBytes with terminator character +// terminates if length characters have been read, timeout, or if the terminator character detected +// returns the number of characters placed in the buffer (0 means no valid data found) + +size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length) +{ + size_t index = 0; + while (index < length) { + int c = timedRead(); + if (c < 0 || c == terminator) break; + *buffer++ = (char)c; + index++; + } + return index; // return number of characters, not including null terminator +} + +String Stream::readString() +{ + String ret; + int c = timedRead(); + while (c >= 0) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +String Stream::readStringUntil(char terminator) +{ + String ret; + int c = timedRead(); + while (c >= 0 && c != terminator) + { + ret += (char)c; + c = timedRead(); + } + return ret; +} + +int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) { + // any zero length target string automatically matches and would make + // a mess of the rest of the algorithm. + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + if (t->len <= 0) + return t - targets; + } + + while (1) { + int c = timedRead(); + if (c < 0) + return -1; + + for (struct MultiTarget *t = targets; t < targets+tCount; ++t) { + // the simple case is if we match, deal with that first. + if (c == t->str[t->index]) { + if (++t->index == t->len) + return t - targets; + else + continue; + } + + // if not we need to walk back and see if we could have matched further + // down the stream (ie '1112' doesn't match the first position in '11112' + // but it will match the second position so we can't just reset the current + // index to 0 when we find a mismatch. + if (t->index == 0) + continue; + + int origIndex = t->index; + do { + --t->index; + // first check if current char works against the new current index + if (c != t->str[t->index]) + continue; + + // if it's the only char then we're good, nothing more to check + if (t->index == 0) { + t->index++; + break; + } + + // otherwise we need to check the rest of the found string + int diff = origIndex - t->index; + size_t i; + for (i = 0; i < t->index; ++i) { + if (t->str[i] != t->str[i + diff]) + break; + } + + // if we successfully got through the previous loop then our current + // index is good. + if (i == t->index) { + t->index++; + break; + } + + // otherwise we just try the next index + } while (t->index); + } + } + // unreachable + return -1; +} diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h new file mode 100644 index 0000000..21a247a --- /dev/null +++ b/cores/arduino/Stream.h @@ -0,0 +1,129 @@ +/* + Stream.h - base class for character-based streams. + Copyright (c) 2010 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + parsing functions based on TextFinder library by Michael Margolis +*/ + +#ifndef Stream_h +#define Stream_h + +#include +#include "Print.h" + +// compatibility macros for testing +/* +#define getInt() parseInt() +#define getInt(ignore) parseInt(ignore) +#define getFloat() parseFloat() +#define getFloat(ignore) parseFloat(ignore) +#define getString( pre_string, post_string, buffer, length) +readBytesBetween( pre_string, terminator, buffer, length) +*/ + +// This enumeration provides the lookahead options for parseInt(), parseFloat() +// The rules set out here are used until either the first valid character is found +// or a time out occurs due to lack of input. +enum LookaheadMode{ + SKIP_ALL, // All invalid characters are ignored. + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. +}; + +#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field + +class Stream : public Print +{ + protected: + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read + unsigned long _startMillis; // used for timeout measurement + int timedRead(); // read stream with timeout + int timedPeek(); // peek stream with timeout + int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout + + public: + virtual int available() = 0; + virtual int read() = 0; + virtual int peek() = 0; + + Stream() {_timeout=1000;} + +// parsing methods + + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + unsigned long getTimeout(void) { return _timeout; } + + bool find(char *target); // reads data from the stream until the target string is found + bool find(uint8_t *target) { return find ((char *)target); } + // returns true if target string is found, false if timed out (see setTimeout) + + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found + bool find(uint8_t *target, size_t length) { return find ((char *)target, length); } + // returns true if target string is found, false if timed out + + bool find(char target) { return find (&target, 1); } + + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found + bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); } + + bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); } + + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // returns the first valid (long) integer value from the current position. + // lookahead determines how parseInt looks ahead in the stream. + // See LookaheadMode enumeration at the top of the file. + // Lookahead is terminated by the first character that is not a valid part of an integer. + // Once parsing commences, 'ignore' will be skipped in the stream. + + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); + // float version of parseInt + + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer + size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); } + // terminates if length characters have been read or timeout (see setTimeout) + // returns the number of characters placed in the buffer (0 means no valid data found) + + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); } + // terminates if length characters have been read, timeout, or if the terminator character detected + // returns the number of characters placed in the buffer (0 means no valid data found) + + // Arduino String functions to be added here + String readString(); + String readStringUntil(char terminator); + + protected: + long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); } + float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); } + // These overload exists for compatibility with any class that has derived + // Stream and used parseFloat/Int with a custom ignore character. To keep + // the public API simple, these overload remains protected. + + struct MultiTarget { + const char *str; // string you're searching for + size_t len; // length of string you're searching for + size_t index; // index used by the search routine. + }; + + // This allows you to search for an arbitrary number of strings. + // Returns index of the target that is found first or -1 if timeout occurs. + int findMulti(struct MultiTarget *targets, int tCount); +}; + +#undef NO_IGNORE_CHAR +#endif diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp new file mode 100644 index 0000000..1bfb3e3 --- /dev/null +++ b/cores/arduino/Tone.cpp @@ -0,0 +1,619 @@ +/* Tone.cpp + + A Tone Generator Library + + Written by Brett Hagman + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Version Modified By Date Comments +------- ----------- -------- -------- +0001 B Hagman 09/08/02 Initial coding +0002 B Hagman 09/08/18 Multiple pins +0003 B Hagman 09/08/18 Moved initialization from constructor to begin() +0004 B Hagman 09/09/26 Fixed problems with ATmega8 +0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers + 09/11/25 Changed pin toggle method to XOR + 09/11/25 Fixed timer0 from being excluded +0006 D Mellis 09/12/29 Replaced objects with functions +0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register +0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY +0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62) +0010 jipp 15/04/13 added additional define check #2923 +*************************************************/ + +#include +#include +#include "Arduino.h" +#include "pins_arduino.h" + +#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) +#define TCCR2A TCCR2 +#define TCCR2B TCCR2 +#define COM2A1 COM21 +#define COM2A0 COM20 +#define OCR2A OCR2 +#define TIMSK2 TIMSK +#define OCIE2A OCIE2 +#define TIMER2_COMPA_vect TIMER2_COMP_vect +#define TIMSK1 TIMSK +#endif + +// timerx_toggle_count: +// > 0 - duration specified +// = 0 - stopped +// < 0 - infinitely (until stop() method called, or new play() called) + +#if !defined(__AVR_ATmega8__) +volatile long timer0_toggle_count; +volatile uint8_t *timer0_pin_port; +volatile uint8_t timer0_pin_mask; +#endif + +volatile long timer1_toggle_count; +volatile uint8_t *timer1_pin_port; +volatile uint8_t timer1_pin_mask; +volatile long timer2_toggle_count; +volatile uint8_t *timer2_pin_port; +volatile uint8_t timer2_pin_mask; + +#if defined(TIMSK3) +volatile long timer3_toggle_count; +volatile uint8_t *timer3_pin_port; +volatile uint8_t timer3_pin_mask; +#endif + +#if defined(TIMSK4) +volatile long timer4_toggle_count; +volatile uint8_t *timer4_pin_port; +volatile uint8_t timer4_pin_mask; +#endif + +#if defined(TIMSK5) +volatile long timer5_toggle_count; +volatile uint8_t *timer5_pin_port; +volatile uint8_t timer5_pin_mask; +#endif + + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ }; + +#elif defined(__AVR_ATmega8__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ }; + +#elif defined(__AVR_ATmega32U4__) + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER3 + +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ }; + +#else + +#define AVAILABLE_TONE_PINS 1 +#define USE_TIMER2 + +// Leave timer 0 to last. +const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ }; +static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ }; + +#endif + + + +static int8_t toneBegin(uint8_t _pin) +{ + int8_t _timer = -1; + + // if we're already using the pin, the timer should be configured. + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == _pin) { + return pgm_read_byte(tone_pin_to_timer_PGM + i); + } + } + + // search for an unused timer. + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == 255) { + tone_pins[i] = _pin; + _timer = pgm_read_byte(tone_pin_to_timer_PGM + i); + break; + } + } + + if (_timer != -1) + { + // Set timer specific stuff + // All timers in CTC mode + // 8 bit timers will require changing prescalar values, + // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar + switch (_timer) + { + #if defined(TCCR0A) && defined(TCCR0B) && defined(WGM01) + case 0: + // 8 bit timer + TCCR0A = 0; + TCCR0B = 0; + bitWrite(TCCR0A, WGM01, 1); + bitWrite(TCCR0B, CS00, 1); + timer0_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer0_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12) + case 1: + // 16 bit timer + TCCR1A = 0; + TCCR1B = 0; + bitWrite(TCCR1B, WGM12, 1); + bitWrite(TCCR1B, CS10, 1); + timer1_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer1_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR2A) && defined(TCCR2B) + case 2: + // 8 bit timer + TCCR2A = 0; + TCCR2B = 0; + bitWrite(TCCR2A, WGM21, 1); + bitWrite(TCCR2B, CS20, 1); + timer2_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer2_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3) + case 3: + // 16 bit timer + TCCR3A = 0; + TCCR3B = 0; + bitWrite(TCCR3B, WGM32, 1); + bitWrite(TCCR3B, CS30, 1); + timer3_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer3_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4) + case 4: + // 16 bit timer + TCCR4A = 0; + TCCR4B = 0; + #if defined(WGM42) + bitWrite(TCCR4B, WGM42, 1); + #elif defined(CS43) + // TODO this may not be correct + // atmega32u4 + bitWrite(TCCR4B, CS43, 1); + #endif + bitWrite(TCCR4B, CS40, 1); + timer4_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer4_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + + #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5) + case 5: + // 16 bit timer + TCCR5A = 0; + TCCR5B = 0; + bitWrite(TCCR5B, WGM52, 1); + bitWrite(TCCR5B, CS50, 1); + timer5_pin_port = portOutputRegister(digitalPinToPort(_pin)); + timer5_pin_mask = digitalPinToBitMask(_pin); + break; + #endif + } + } + + return _timer; +} + + + +// frequency (in hertz) and duration (in milliseconds). + +void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) +{ + uint8_t prescalarbits = 0b001; + long toggle_count = 0; + uint32_t ocr = 0; + int8_t _timer; + + _timer = toneBegin(_pin); + + if (_timer >= 0) + { + // Set the pinMode as OUTPUT + pinMode(_pin, OUTPUT); + + // if we are using an 8 bit timer, scan through prescalars to find the best fit + if (_timer == 0 || _timer == 2) + { + ocr = F_CPU / frequency / 2 - 1; + prescalarbits = 0b001; // ck/1: same for both timers + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 8 - 1; + prescalarbits = 0b010; // ck/8: same for both timers + + if (_timer == 2 && ocr > 255) + { + ocr = F_CPU / frequency / 2 / 32 - 1; + prescalarbits = 0b011; + } + + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 64 - 1; + prescalarbits = _timer == 0 ? 0b011 : 0b100; + + if (_timer == 2 && ocr > 255) + { + ocr = F_CPU / frequency / 2 / 128 - 1; + prescalarbits = 0b101; + } + + if (ocr > 255) + { + ocr = F_CPU / frequency / 2 / 256 - 1; + prescalarbits = _timer == 0 ? 0b100 : 0b110; + if (ocr > 255) + { + // can't do any better than /1024 + ocr = F_CPU / frequency / 2 / 1024 - 1; + prescalarbits = _timer == 0 ? 0b101 : 0b111; + } + } + } + } + +#if defined(TCCR0B) + if (_timer == 0) + { + TCCR0B = (TCCR0B & 0b11111000) | prescalarbits; + } + else +#endif +#if defined(TCCR2B) + { + TCCR2B = (TCCR2B & 0b11111000) | prescalarbits; + } +#else + { + // dummy place holder to make the above ifdefs work + } +#endif + } + else + { + // two choices for the 16 bit timers: ck/1 or ck/64 + ocr = F_CPU / frequency / 2 - 1; + + prescalarbits = 0b001; + if (ocr > 0xffff) + { + ocr = F_CPU / frequency / 2 / 64 - 1; + prescalarbits = 0b011; + } + + if (_timer == 1) + { +#if defined(TCCR1B) + TCCR1B = (TCCR1B & 0b11111000) | prescalarbits; +#endif + } +#if defined(TCCR3B) + else if (_timer == 3) + TCCR3B = (TCCR3B & 0b11111000) | prescalarbits; +#endif +#if defined(TCCR4B) + else if (_timer == 4) + TCCR4B = (TCCR4B & 0b11111000) | prescalarbits; +#endif +#if defined(TCCR5B) + else if (_timer == 5) + TCCR5B = (TCCR5B & 0b11111000) | prescalarbits; +#endif + + } + + + // Calculate the toggle count + if (duration > 0) + { + toggle_count = 2 * frequency * duration / 1000; + } + else + { + toggle_count = -1; + } + + // Set the OCR for the given timer, + // set the toggle count, + // then turn on the interrupts + switch (_timer) + { + +#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A) + case 0: + OCR0A = ocr; + timer0_toggle_count = toggle_count; + bitWrite(TIMSK0, OCIE0A, 1); + break; +#endif + + case 1: +#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A) + OCR1A = ocr; + timer1_toggle_count = toggle_count; + bitWrite(TIMSK1, OCIE1A, 1); +#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A) + // this combination is for at least the ATmega32 + OCR1A = ocr; + timer1_toggle_count = toggle_count; + bitWrite(TIMSK, OCIE1A, 1); +#endif + break; + +#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A) + case 2: + OCR2A = ocr; + timer2_toggle_count = toggle_count; + bitWrite(TIMSK2, OCIE2A, 1); + break; +#endif + +#if defined(OCR3A) && defined(TIMSK3) && defined(OCIE3A) + case 3: + OCR3A = ocr; + timer3_toggle_count = toggle_count; + bitWrite(TIMSK3, OCIE3A, 1); + break; +#endif + +#if defined(OCR4A) && defined(TIMSK4) && defined(OCIE4A) + case 4: + OCR4A = ocr; + timer4_toggle_count = toggle_count; + bitWrite(TIMSK4, OCIE4A, 1); + break; +#endif + +#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A) + case 5: + OCR5A = ocr; + timer5_toggle_count = toggle_count; + bitWrite(TIMSK5, OCIE5A, 1); + break; +#endif + + } + } +} + + +// XXX: this function only works properly for timer 2 (the only one we use +// currently). for the others, it should end the tone, but won't restore +// proper PWM functionality for the timer. +void disableTimer(uint8_t _timer) +{ + switch (_timer) + { + case 0: + #if defined(TIMSK0) + TIMSK0 = 0; + #elif defined(TIMSK) + TIMSK = 0; // atmega32 + #endif + break; + +#if defined(TIMSK1) && defined(OCIE1A) + case 1: + bitWrite(TIMSK1, OCIE1A, 0); + break; +#endif + + case 2: + #if defined(TIMSK2) && defined(OCIE2A) + bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt + #endif + #if defined(TCCR2A) && defined(WGM20) + TCCR2A = (1 << WGM20); + #endif + #if defined(TCCR2B) && defined(CS22) + TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22); + #endif + #if defined(OCR2A) + OCR2A = 0; + #endif + break; + +#if defined(TIMSK3) && defined(OCIE3A) + case 3: + bitWrite(TIMSK3, OCIE3A, 0); + break; +#endif + +#if defined(TIMSK4) && defined(OCIE4A) + case 4: + bitWrite(TIMSK4, OCIE4A, 0); + break; +#endif + +#if defined(TIMSK5) && defined(OCIE5A) + case 5: + bitWrite(TIMSK5, OCIE5A, 0); + break; +#endif + } +} + + +void noTone(uint8_t _pin) +{ + int8_t _timer = -1; + + for (int i = 0; i < AVAILABLE_TONE_PINS; i++) { + if (tone_pins[i] == _pin) { + _timer = pgm_read_byte(tone_pin_to_timer_PGM + i); + tone_pins[i] = 255; + break; + } + } + + disableTimer(_timer); + + digitalWrite(_pin, 0); +} + +#ifdef USE_TIMER0 +ISR(TIMER0_COMPA_vect) +{ + if (timer0_toggle_count != 0) + { + // toggle the pin + *timer0_pin_port ^= timer0_pin_mask; + + if (timer0_toggle_count > 0) + timer0_toggle_count--; + } + else + { + disableTimer(0); + *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER1 +ISR(TIMER1_COMPA_vect) +{ + if (timer1_toggle_count != 0) + { + // toggle the pin + *timer1_pin_port ^= timer1_pin_mask; + + if (timer1_toggle_count > 0) + timer1_toggle_count--; + } + else + { + disableTimer(1); + *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER2 +ISR(TIMER2_COMPA_vect) +{ + + if (timer2_toggle_count != 0) + { + // toggle the pin + *timer2_pin_port ^= timer2_pin_mask; + + if (timer2_toggle_count > 0) + timer2_toggle_count--; + } + else + { + // need to call noTone() so that the tone_pins[] entry is reset, so the + // timer gets initialized next time we call tone(). + // XXX: this assumes timer 2 is always the first one used. + noTone(tone_pins[0]); +// disableTimer(2); +// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER3 +ISR(TIMER3_COMPA_vect) +{ + if (timer3_toggle_count != 0) + { + // toggle the pin + *timer3_pin_port ^= timer3_pin_mask; + + if (timer3_toggle_count > 0) + timer3_toggle_count--; + } + else + { + disableTimer(3); + *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER4 +ISR(TIMER4_COMPA_vect) +{ + if (timer4_toggle_count != 0) + { + // toggle the pin + *timer4_pin_port ^= timer4_pin_mask; + + if (timer4_toggle_count > 0) + timer4_toggle_count--; + } + else + { + disableTimer(4); + *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop + } +} +#endif + + +#ifdef USE_TIMER5 +ISR(TIMER5_COMPA_vect) +{ + if (timer5_toggle_count != 0) + { + // toggle the pin + *timer5_pin_port ^= timer5_pin_mask; + + if (timer5_toggle_count > 0) + timer5_toggle_count--; + } + else + { + disableTimer(5); + *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop + } +} +#endif diff --git a/cores/arduino/USBAPI.h b/cores/arduino/USBAPI.h new file mode 100644 index 0000000..3ff1459 --- /dev/null +++ b/cores/arduino/USBAPI.h @@ -0,0 +1,209 @@ +/* + USBAPI.h + Copyright (c) 2005-2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef __USBAPI__ +#define __USBAPI__ + +#include +#include +#include +#include +#include + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; + +#include "Arduino.h" + +// This definitions is useful if you want to reduce the EP_SIZE to 16 +// at the moment only 64 and 16 as EP_SIZE for all EPs are supported except the control endpoint +#ifndef USB_EP_SIZE +#define USB_EP_SIZE 64 +#endif + +#if defined(USBCON) + +#include "USBDesc.h" +#include "USBCore.h" + +//================================================================================ +//================================================================================ +// USB + +#define EP_TYPE_CONTROL (0x00) +#define EP_TYPE_BULK_IN ((1<256) +#error Please lower the CDC Buffer size +#endif + +class Serial_ : public Stream +{ +private: + int peek_buffer; +public: + Serial_() { peek_buffer = -1; }; + void begin(unsigned long); + void begin(unsigned long, uint8_t); + void end(void); + + virtual int available(void); + virtual int peek(void); + virtual int read(void); + virtual int availableForWrite(void); + virtual void flush(void); + virtual size_t write(uint8_t); + virtual size_t write(const uint8_t*, size_t); + using Print::write; // pull in write(str) and write(buf, size) from Print + operator bool(); + + volatile uint8_t _rx_buffer_head; + volatile uint8_t _rx_buffer_tail; + unsigned char _rx_buffer[SERIAL_BUFFER_SIZE]; + + // This method allows processing "SEND_BREAK" requests sent by + // the USB host. Those requests indicate that the host wants to + // send a BREAK signal and are accompanied by a single uint16_t + // value, specifying the duration of the break. The value 0 + // means to end any current break, while the value 0xffff means + // to start an indefinite break. + // readBreak() will return the value of the most recent break + // request, but will return it at most once, returning -1 when + // readBreak() is called again (until another break request is + // received, which is again returned once). + // This also mean that if two break requests are received + // without readBreak() being called in between, the value of the + // first request is lost. + // Note that the value returned is a long, so it can return + // 0-0xffff as well as -1. + int32_t readBreak(); + + // These return the settings specified by the USB host for the + // serial port. These aren't really used, but are offered here + // in case a sketch wants to act on these settings. + uint32_t baud(); + uint8_t stopbits(); + uint8_t paritytype(); + uint8_t numbits(); + bool dtr(); + bool rts(); + enum { + ONE_STOP_BIT = 0, + ONE_AND_HALF_STOP_BIT = 1, + TWO_STOP_BITS = 2, + }; + enum { + NO_PARITY = 0, + ODD_PARITY = 1, + EVEN_PARITY = 2, + MARK_PARITY = 3, + SPACE_PARITY = 4, + }; + +}; +extern Serial_ Serial; + +#define HAVE_CDCSERIAL + +//================================================================================ +//================================================================================ +// Low level API + +typedef struct +{ + uint8_t bmRequestType; + uint8_t bRequest; + uint8_t wValueL; + uint8_t wValueH; + uint16_t wIndex; + uint16_t wLength; +} USBSetup; + +//================================================================================ +//================================================================================ +// MSC 'Driver' + +int MSC_GetInterface(uint8_t* interfaceNum); +int MSC_GetDescriptor(int i); +bool MSC_Setup(USBSetup& setup); +bool MSC_Data(uint8_t rx,uint8_t tx); + +//================================================================================ +//================================================================================ +// CSC 'Driver' + +int CDC_GetInterface(uint8_t* interfaceNum); +int CDC_GetDescriptor(int i); +bool CDC_Setup(USBSetup& setup); + +//================================================================================ +//================================================================================ + +#define TRANSFER_PGM 0x80 +#define TRANSFER_RELEASE 0x40 +#define TRANSFER_ZERO 0x20 + +int USB_SendControl(uint8_t flags, const void* d, int len); +int USB_RecvControl(void* d, int len); +int USB_RecvControlLong(void* d, int len); + +uint8_t USB_Available(uint8_t ep); +uint8_t USB_SendSpace(uint8_t ep); +int USB_Send(uint8_t ep, const void* data, int len); // blocking +int USB_Recv(uint8_t ep, void* data, int len); // non-blocking +int USB_Recv(uint8_t ep); // non-blocking +void USB_Flush(uint8_t ep); + +#endif + +#endif /* if defined(USBCON) */ diff --git a/cores/arduino/USBCore.cpp b/cores/arduino/USBCore.cpp new file mode 100644 index 0000000..9335238 --- /dev/null +++ b/cores/arduino/USBCore.cpp @@ -0,0 +1,879 @@ + + +/* Copyright (c) 2010, Peter Barrett +** Sleep/Wakeup support added by Michael Dreher +** +** Permission to use, copy, modify, and/or distribute this software for +** any purpose with or without fee is hereby granted, provided that the +** above copyright notice and this permission notice appear in all copies. +** +** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL +** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR +** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES +** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +** SOFTWARE. +*/ + +#include "USBAPI.h" +#include "PluggableUSB.h" +#include + +#if defined(USBCON) + +/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */ +#define TX_RX_LED_PULSE_MS 100 +volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ +volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ + +//================================================================== +//================================================================== + +extern const u16 STRING_LANGUAGE[] PROGMEM; +extern const u8 STRING_PRODUCT[] PROGMEM; +extern const u8 STRING_MANUFACTURER[] PROGMEM; +extern const DeviceDescriptor USB_DeviceDescriptorIAD PROGMEM; + +const u16 STRING_LANGUAGE[2] = { + (3<<8) | (2+2), + 0x0409 // English +}; + +#ifndef USB_PRODUCT +// If no product is provided, use USB IO Board +#define USB_PRODUCT "USB IO Board" +#endif + +const u8 STRING_PRODUCT[] PROGMEM = USB_PRODUCT; + +#if USB_VID == 0x2341 +# if defined(USB_MANUFACTURER) +# undef USB_MANUFACTURER +# endif +# define USB_MANUFACTURER "Arduino LLC" +#elif USB_VID == 0x1b4f +# if defined(USB_MANUFACTURER) +# undef USB_MANUFACTURER +# endif +# define USB_MANUFACTURER "SparkFun" +#elif !defined(USB_MANUFACTURER) +// Fall through to unknown if no manufacturer name was provided in a macro +# define USB_MANUFACTURER "Unknown" +#endif + +const u8 STRING_MANUFACTURER[] PROGMEM = USB_MANUFACTURER; + + +#define DEVICE_CLASS 0x02 + +// DEVICE DESCRIPTOR + +#ifdef CDC_ENABLED +const DeviceDescriptor USB_DeviceDescriptorIAD = + D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); +#else // CDC_DISABLED +// The default descriptor uses USB class OxEF, subclass 0x02 with protocol 1 +// which means "Interface Association Descriptor" - that's needed for the CDC, +// but doesn't make much sense as a default for custom devices when CDC is disabled. +// (0x00 means "Use class information in the Interface Descriptors" which should be generally ok) +const DeviceDescriptor USB_DeviceDescriptorIAD = + D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,ISERIAL,1); +#endif + +//================================================================== +//================================================================== + +volatile u8 _usbConfiguration = 0; +volatile u8 _usbCurrentStatus = 0; // meaning of bits see usb_20.pdf, Figure 9-4. Information Returned by a GetStatus() Request to a Device +volatile u8 _usbSuspendState = 0; // copy of UDINT to check SUSPI and WAKEUPI bits + +static inline void WaitIN(void) +{ + while (!(UEINTX & (1< len) { + n = len; + } + + { + LockEP lock(ep); + // Frame may have been released by the SOF interrupt handler + if (!ReadWriteAllowed()) + continue; + + len -= n; + if (ep & TRANSFER_ZERO) + { + while (n--) + Send8(0); + } + else if (ep & TRANSFER_PGM) + { + while (n--) + Send8(pgm_read_byte(data++)); + } + else + { + while (n--) + Send8(*data++); + } + + if (sendZlp) { + ReleaseTX(); + sendZlp = false; + } else if (!ReadWriteAllowed()) { // ...release if buffer is full... + ReleaseTX(); + if (len == 0) sendZlp = true; + } else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE + // XXX: TRANSFER_RELEASE is never used can be removed? + ReleaseTX(); + } + } + } + TXLED1; // light the TX LED + TxLEDPulse = TX_RX_LED_PULSE_MS; + return r; +} + +u8 _initEndpoints[USB_ENDPOINTS] = +{ + 0, // Control Endpoint + +#ifdef CDC_ENABLED + EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM + EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT + EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN +#endif + + // Following endpoints are automatically initialized to 0 +}; + +#define EP_SINGLE_64 0x32 // EP0 +#define EP_DOUBLE_64 0x36 // Other endpoints +#define EP_SINGLE_16 0x12 + +static +void InitEP(u8 index, u8 type, u8 size) +{ + UENUM = index; + UECONX = (1< 64){ + recvLength = 64; + } + + // Write data to fit to the end (not the beginning) of the array + WaitOUT(); + Recv((u8*)d + len - length, recvLength); + ClearOUT(); + length -= recvLength; + } + return len; +} + +static u8 SendInterfaces() +{ + u8 interfaces = 0; + +#ifdef CDC_ENABLED + CDC_GetInterface(&interfaces); +#endif + +#ifdef PLUGGABLE_USB_ENABLED + PluggableUSB().getInterface(&interfaces); +#endif + + return interfaces; +} + +// Construct a dynamic configuration descriptor +// This really needs dynamic endpoint allocation etc +// TODO +static +bool SendConfiguration(int maxlen) +{ + // Count and measure interfaces + InitControl(0); + u8 interfaces = SendInterfaces(); + ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces); + + // Now send them + InitControl(maxlen); + USB_SendControl(0,&config,sizeof(ConfigDescriptor)); + SendInterfaces(); + return true; +} + +static +bool SendDescriptor(USBSetup& setup) +{ + u8 t = setup.wValueH; + if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t) + return SendConfiguration(setup.wLength); + + InitControl(setup.wLength); +#ifdef PLUGGABLE_USB_ENABLED + int ret = PluggableUSB().getDescriptor(setup); + if (ret != 0) { + return (ret > 0 ? true : false); + } +#endif + + const u8* desc_addr = 0; + if (USB_DEVICE_DESCRIPTOR_TYPE == t) + { + desc_addr = (const u8*)&USB_DeviceDescriptorIAD; + } + else if (USB_STRING_DESCRIPTOR_TYPE == t) + { + if (setup.wValueL == 0) { + desc_addr = (const u8*)&STRING_LANGUAGE; + } + else if (setup.wValueL == IPRODUCT) { + return USB_SendStringDescriptor(STRING_PRODUCT, strlen(USB_PRODUCT), TRANSFER_PGM); + } + else if (setup.wValueL == IMANUFACTURER) { + return USB_SendStringDescriptor(STRING_MANUFACTURER, strlen(USB_MANUFACTURER), TRANSFER_PGM); + } + else if (setup.wValueL == ISERIAL) { +#ifdef PLUGGABLE_USB_ENABLED + char name[ISERIAL_MAX_LEN]; + PluggableUSB().getShortName(name); + return USB_SendStringDescriptor((uint8_t*)name, strlen(name), 0); +#endif + } + else + return false; + } + + if (desc_addr == 0) + return false; + u8 desc_length = pgm_read_byte(desc_addr); + + USB_SendControl(TRANSFER_PGM,desc_addr,desc_length); + return true; +} + +// Endpoint 0 interrupt +ISR(USB_COM_vect) +{ + SetEP(0); + if (!ReceivedSetupInt()) + return; + + USBSetup setup; + Recv((u8*)&setup,8); + ClearSetupInt(); + + u8 requestType = setup.bmRequestType; + if (requestType & REQUEST_DEVICETOHOST) + WaitIN(); + else + ClearIN(); + + bool ok = true; + if (REQUEST_STANDARD == (requestType & REQUEST_TYPE)) + { + // Standard Requests + u8 r = setup.bRequest; + u16 wValue = setup.wValueL | (setup.wValueH << 8); + if (GET_STATUS == r) + { + if (requestType == (REQUEST_DEVICETOHOST | REQUEST_STANDARD | REQUEST_DEVICE)) + { + Send8(_usbCurrentStatus); + Send8(0); + } + else + { + // TODO: handle the HALT state of an endpoint here + // see "Figure 9-6. Information Returned by a GetStatus() Request to an Endpoint" in usb_20.pdf for more information + Send8(0); + Send8(0); + } + } + else if (CLEAR_FEATURE == r) + { + if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) + && (wValue == DEVICE_REMOTE_WAKEUP)) + { + _usbCurrentStatus &= ~FEATURE_REMOTE_WAKEUP_ENABLED; + } + } + else if (SET_FEATURE == r) + { + if((requestType == (REQUEST_HOSTTODEVICE | REQUEST_STANDARD | REQUEST_DEVICE)) + && (wValue == DEVICE_REMOTE_WAKEUP)) + { + _usbCurrentStatus |= FEATURE_REMOTE_WAKEUP_ENABLED; + } + } + else if (SET_ADDRESS == r) + { + WaitIN(); + UDADDR = setup.wValueL | (1<> 8) & 0xFF) + +#define CDC_V1_10 0x0110 +#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 + +#define CDC_CALL_MANAGEMENT 0x01 +#define CDC_ABSTRACT_CONTROL_MODEL 0x02 +#define CDC_HEADER 0x00 +#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 +#define CDC_UNION 0x06 +#define CDC_CS_INTERFACE 0x24 +#define CDC_CS_ENDPOINT 0x25 +#define CDC_DATA_INTERFACE_CLASS 0x0A + +#define MSC_SUBCLASS_SCSI 0x06 +#define MSC_PROTOCOL_BULK_ONLY 0x50 + +#ifndef USB_VERSION +#define USB_VERSION 0x200 +#endif + +// Device +typedef struct { + u8 len; // 18 + u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE + u16 usbVersion; // 0x200 or 0x210 + u8 deviceClass; + u8 deviceSubClass; + u8 deviceProtocol; + u8 packetSize0; // Packet 0 + u16 idVendor; + u16 idProduct; + u16 deviceVersion; // 0x100 + u8 iManufacturer; + u8 iProduct; + u8 iSerialNumber; + u8 bNumConfigurations; +} DeviceDescriptor; + +// Config +typedef struct { + u8 len; // 9 + u8 dtype; // 2 + u16 clen; // total length + u8 numInterfaces; + u8 config; + u8 iconfig; + u8 attributes; + u8 maxPower; +} ConfigDescriptor; + +// String + +// Interface +typedef struct +{ + u8 len; // 9 + u8 dtype; // 4 + u8 number; + u8 alternate; + u8 numEndpoints; + u8 interfaceClass; + u8 interfaceSubClass; + u8 protocol; + u8 iInterface; +} InterfaceDescriptor; + +// Endpoint +typedef struct +{ + u8 len; // 7 + u8 dtype; // 5 + u8 addr; + u8 attr; + u16 packetSize; + u8 interval; +} EndpointDescriptor; + +// Interface Association Descriptor +// Used to bind 2 interfaces together in CDC composite device +typedef struct +{ + u8 len; // 8 + u8 dtype; // 11 + u8 firstInterface; + u8 interfaceCount; + u8 functionClass; + u8 funtionSubClass; + u8 functionProtocol; + u8 iInterface; +} IADDescriptor; + +// CDC CS interface descriptor +typedef struct +{ + u8 len; // 5 + u8 dtype; // 0x24 + u8 subtype; + u8 d0; + u8 d1; +} CDCCSInterfaceDescriptor; + +typedef struct +{ + u8 len; // 4 + u8 dtype; // 0x24 + u8 subtype; + u8 d0; +} CDCCSInterfaceDescriptor4; + +typedef struct +{ + u8 len; + u8 dtype; // 0x24 + u8 subtype; // 1 + u8 bmCapabilities; + u8 bDataInterface; +} CMFunctionalDescriptor; + +typedef struct +{ + u8 len; + u8 dtype; // 0x24 + u8 subtype; // 1 + u8 bmCapabilities; +} ACMFunctionalDescriptor; + +typedef struct +{ + // IAD + IADDescriptor iad; // Only needed on compound device + + // Control + InterfaceDescriptor cif; // + CDCCSInterfaceDescriptor header; + CMFunctionalDescriptor callManagement; // Call Management + ACMFunctionalDescriptor controlManagement; // ACM + CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION + EndpointDescriptor cifin; + + // Data + InterfaceDescriptor dif; + EndpointDescriptor in; + EndpointDescriptor out; +} CDCDescriptor; + +typedef struct +{ + InterfaceDescriptor msc; + EndpointDescriptor in; + EndpointDescriptor out; +} MSCDescriptor; + + +#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ + { 18, 1, USB_VERSION, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } + +#define D_CONFIG(_totalLength,_interfaces) \ + { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED | USB_CONFIG_REMOTE_WAKEUP, USB_CONFIG_POWER_MA(USB_CONFIG_POWER) } + +#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ + { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } + +#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ + { 7, 5, _addr,_attr,_packetSize, _interval } + +#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ + { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } + +#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } +#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } + +// Bootloader related fields +// Old Caterina bootloader places the MAGIC key into unsafe RAM locations (it can be rewritten +// by the running sketch before to actual reboot). +// Newer bootloaders, recognizable by the LUFA "signature" at the end of the flash, can handle both +// the usafe and the safe location. +#ifndef MAGIC_KEY +#define MAGIC_KEY 0x7777 +#endif + +#ifndef MAGIC_KEY_POS +#define MAGIC_KEY_POS 0x0800 +#endif + +#ifndef NEW_LUFA_SIGNATURE +#define NEW_LUFA_SIGNATURE 0xDCFB +#endif + +#endif diff --git a/cores/arduino/USBDesc.h b/cores/arduino/USBDesc.h new file mode 100644 index 0000000..b55ac20 --- /dev/null +++ b/cores/arduino/USBDesc.h @@ -0,0 +1,63 @@ +/* + Copyright (c) 2011, Peter Barrett + Copyright (c) 2015, Arduino LLC + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted, provided that the + above copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR + BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES + OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, + WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. + */ + +#define PLUGGABLE_USB_ENABLED + +#if defined(EPRST6) +#define USB_ENDPOINTS 7 // AtMegaxxU4 +#else +#define USB_ENDPOINTS 5 // AtMegaxxU2 +#endif + +#define ISERIAL_MAX_LEN 20 + +// Uncomment the following line or pass -DCDC_DISABLED to the compiler +// to disable CDC (serial console via USB). +// That's useful if you want to create an USB device (like an USB Boot Keyboard) +// that works even with problematic devices (like KVM switches). +// Keep in mind that with this change you'll have to use the Arduino's +// reset button to be able to flash it. +//#define CDC_DISABLED + +#ifndef CDC_DISABLED +#define CDC_ENABLED +#endif + +#ifdef CDC_ENABLED +#define CDC_INTERFACE_COUNT 2 +#define CDC_ENPOINT_COUNT 3 +#else // CDC_DISABLED +#define CDC_INTERFACE_COUNT 0 +#define CDC_ENPOINT_COUNT 0 +#endif + +#define CDC_ACM_INTERFACE 0 // CDC ACM +#define CDC_DATA_INTERFACE 1 // CDC Data +#define CDC_FIRST_ENDPOINT 1 +#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First +#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1) +#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2) + +#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT) + +#define CDC_RX CDC_ENDPOINT_OUT +#define CDC_TX CDC_ENDPOINT_IN + +#define IMANUFACTURER 1 +#define IPRODUCT 2 +#define ISERIAL 3 \ No newline at end of file diff --git a/cores/arduino/Udp.h b/cores/arduino/Udp.h new file mode 100644 index 0000000..89f31c6 --- /dev/null +++ b/cores/arduino/Udp.h @@ -0,0 +1,89 @@ +/* + * Udp.cpp: Library to send/receive UDP packets. + * + * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) + * 1) UDP does not guarantee the order in which assembled UDP packets are received. This + * might not happen often in practice, but in larger network topologies, a UDP + * packet can be received out of sequence. + * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being + * aware of it. Again, this may not be a concern in practice on small local networks. + * For more information, see http://www.cafeaulait.org/course/week12/35.html + * + * MIT License: + * Copyright (c) 2008 Bjoern Hartmann + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * bjoern@cs.stanford.edu 12/30/2008 + */ + +#ifndef udp_h +#define udp_h + +#include +#include + +class UDP : public Stream { + +public: + 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 + virtual uint8_t beginMulticast(IPAddress, uint16_t) { return 0; } // initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 on failure + virtual void stop() =0; // Finish with the UDP socket + + // Sending UDP packets + + // Start building up a packet to send to the remote host specific in ip and port + // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port + virtual int beginPacket(IPAddress ip, uint16_t port) =0; + // Start building up a packet to send to the remote host specific in host and port + // Returns 1 if successful, 0 if there was a problem resolving the hostname or port + virtual int beginPacket(const char *host, uint16_t port) =0; + // Finish off this packet and send it + // Returns 1 if the packet was sent successfully, 0 if there was an error + virtual int endPacket() =0; + // Write a single byte into the packet + virtual size_t write(uint8_t) =0; + // Write size bytes from buffer into the packet + virtual size_t write(const uint8_t *buffer, size_t size) =0; + + // Start processing the next available incoming packet + // Returns the size of the packet in bytes, or 0 if no packets are available + virtual int parsePacket() =0; + // Number of bytes remaining in the current packet + virtual int available() =0; + // Read a single byte from the current packet + virtual int read() =0; + // Read up to len bytes from the current packet and place them into buffer + // Returns the number of bytes read, or 0 if none are available + virtual int read(unsigned char* buffer, size_t len) =0; + // Read up to len characters from the current packet and place them into buffer + // Returns the number of characters read, or 0 if none are available + virtual int read(char* buffer, size_t len) =0; + // Return the next byte from the current packet without moving on to the next byte + virtual int peek() =0; + virtual void flush() =0; // Finish reading the current packet + + // Return the IP address of the host who sent the current incoming packet + virtual IPAddress remoteIP() =0; + // Return the port of the host who sent the current incoming packet + virtual uint16_t remotePort() =0; +protected: + uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); }; +}; + +#endif diff --git a/cores/arduino/WCharacter.h b/cores/arduino/WCharacter.h new file mode 100644 index 0000000..79733b5 --- /dev/null +++ b/cores/arduino/WCharacter.h @@ -0,0 +1,168 @@ +/* + WCharacter.h - Character utility functions for Wiring & Arduino + Copyright (c) 2010 Hernando Barragan. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef Character_h +#define Character_h + +#include + +// WCharacter.h prototypes +inline boolean isAlphaNumeric(int c) __attribute__((always_inline)); +inline boolean isAlpha(int c) __attribute__((always_inline)); +inline boolean isAscii(int c) __attribute__((always_inline)); +inline boolean isWhitespace(int c) __attribute__((always_inline)); +inline boolean isControl(int c) __attribute__((always_inline)); +inline boolean isDigit(int c) __attribute__((always_inline)); +inline boolean isGraph(int c) __attribute__((always_inline)); +inline boolean isLowerCase(int c) __attribute__((always_inline)); +inline boolean isPrintable(int c) __attribute__((always_inline)); +inline boolean isPunct(int c) __attribute__((always_inline)); +inline boolean isSpace(int c) __attribute__((always_inline)); +inline boolean isUpperCase(int c) __attribute__((always_inline)); +inline boolean isHexadecimalDigit(int c) __attribute__((always_inline)); +inline int toAscii(int c) __attribute__((always_inline)); +inline int toLowerCase(int c) __attribute__((always_inline)); +inline int toUpperCase(int c)__attribute__((always_inline)); + + +// Checks for an alphanumeric character. +// It is equivalent to (isalpha(c) || isdigit(c)). +inline boolean isAlphaNumeric(int c) +{ + return ( isalnum(c) == 0 ? false : true); +} + + +// Checks for an alphabetic character. +// It is equivalent to (isupper(c) || islower(c)). +inline boolean isAlpha(int c) +{ + return ( isalpha(c) == 0 ? false : true); +} + + +// Checks whether c is a 7-bit unsigned char value +// that fits into the ASCII character set. +inline boolean isAscii(int c) +{ + return ( isascii (c) == 0 ? false : true); +} + + +// Checks for a blank character, that is, a space or a tab. +inline boolean isWhitespace(int c) +{ + return ( isblank (c) == 0 ? false : true); +} + + +// Checks for a control character. +inline boolean isControl(int c) +{ + return ( iscntrl (c) == 0 ? false : true); +} + + +// Checks for a digit (0 through 9). +inline boolean isDigit(int c) +{ + return ( isdigit (c) == 0 ? false : true); +} + + +// Checks for any printable character except space. +inline boolean isGraph(int c) +{ + return ( isgraph (c) == 0 ? false : true); +} + + +// Checks for a lower-case character. +inline boolean isLowerCase(int c) +{ + return (islower (c) == 0 ? false : true); +} + + +// Checks for any printable character including space. +inline boolean isPrintable(int c) +{ + return ( isprint (c) == 0 ? false : true); +} + + +// Checks for any printable character which is not a space +// or an alphanumeric character. +inline boolean isPunct(int c) +{ + return ( ispunct (c) == 0 ? false : true); +} + + +// Checks for white-space characters. For the avr-libc library, +// these are: space, formfeed ('\f'), newline ('\n'), carriage +// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v'). +inline boolean isSpace(int c) +{ + return ( isspace (c) == 0 ? false : true); +} + + +// Checks for an uppercase letter. +inline boolean isUpperCase(int c) +{ + return ( isupper (c) == 0 ? false : true); +} + + +// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7 +// 8 9 a b c d e f A B C D E F. +inline boolean isHexadecimalDigit(int c) +{ + return ( isxdigit (c) == 0 ? false : true); +} + + +// Converts c to a 7-bit unsigned char value that fits into the +// ASCII character set, by clearing the high-order bits. +inline int toAscii(int c) +{ + return toascii (c); +} + + +// Warning: +// Many people will be unhappy if you use this function. +// This function will convert accented letters into random +// characters. + +// Converts the letter c to lower case, if possible. +inline int toLowerCase(int c) +{ + return tolower (c); +} + + +// Converts the letter c to upper case, if possible. +inline int toUpperCase(int c) +{ + return toupper (c); +} + +#endif \ No newline at end of file diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c new file mode 100644 index 0000000..38ea158 --- /dev/null +++ b/cores/arduino/WInterrupts.c @@ -0,0 +1,379 @@ +/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ + +/* + Part of the Wiring project - http://wiring.uniandes.edu.co + + Copyright (c) 2004-05 Hernando Barragan + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + + Modified 24 November 2006 by David A. Mellis + Modified 1 August 2010 by Mark Sproul +*/ + +#include +#include +#include +#include +#include + +#include "wiring_private.h" + +static void nothing(void) { +} + +static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = { +#if EXTERNAL_NUM_INTERRUPTS > 8 + #warning There are more than 8 external interrupts. Some callbacks may not be initialized. + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 7 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 6 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 5 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 4 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 3 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 2 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 1 + nothing, +#endif +#if EXTERNAL_NUM_INTERRUPTS > 0 + nothing, +#endif +}; + +void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) { + if(interruptNum < EXTERNAL_NUM_INTERRUPTS) { + intFunc[interruptNum] = userFunc; + + // Configure the interrupt mode (trigger on low input, any change, rising + // edge, or falling edge). The mode constants were chosen to correspond + // to the configuration bits in the hardware register, so we simply shift + // the mode into place. + + // Enable the interrupt. + + switch (interruptNum) { +#if defined(__AVR_ATmega32U4__) + // I hate doing this, but the register assignment differs between the 1280/2560 + // and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't + // even present on the 32U4 this is the only way to distinguish between them. + case 0: + EICRA = (EICRA & ~((1<= howbig) { + return howsmall; + } + long diff = howbig - howsmall; + return random(diff) + howsmall; +} + +long map(long x, long in_min, long in_max, long out_min, long out_max) +{ + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +unsigned int makeWord(unsigned int w) { return w; } +unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp new file mode 100644 index 0000000..043fda7 --- /dev/null +++ b/cores/arduino/WString.cpp @@ -0,0 +1,750 @@ +/* + WString.cpp - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All rights reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "WString.h" + +/*********************************************/ +/* Constructors */ +/*********************************************/ + +String::String(const char *cstr) +{ + init(); + if (cstr) copy(cstr, strlen(cstr)); +} + +String::String(const String &value) +{ + init(); + *this = value; +} + +String::String(const __FlashStringHelper *pstr) +{ + init(); + *this = pstr; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +String::String(String &&rval) +{ + init(); + move(rval); +} +String::String(StringSumHelper &&rval) +{ + init(); + move(rval); +} +#endif + +String::String(char c) +{ + init(); + char buf[2]; + buf[0] = c; + buf[1] = 0; + *this = buf; +} + +String::String(unsigned char value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned char)]; + utoa(value, buf, base); + *this = buf; +} + +String::String(int value, unsigned char base) +{ + init(); + char buf[2 + 8 * sizeof(int)]; + itoa(value, buf, base); + *this = buf; +} + +String::String(unsigned int value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned int)]; + utoa(value, buf, base); + *this = buf; +} + +String::String(long value, unsigned char base) +{ + init(); + char buf[2 + 8 * sizeof(long)]; + ltoa(value, buf, base); + *this = buf; +} + +String::String(unsigned long value, unsigned char base) +{ + init(); + char buf[1 + 8 * sizeof(unsigned long)]; + ultoa(value, buf, base); + *this = buf; +} + +String::String(float value, unsigned char decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::String(double value, unsigned char decimalPlaces) +{ + init(); + char buf[33]; + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); +} + +String::~String() +{ + if (buffer) free(buffer); +} + +/*********************************************/ +/* Memory Management */ +/*********************************************/ + +inline void String::init(void) +{ + buffer = NULL; + capacity = 0; + len = 0; +} + +void String::invalidate(void) +{ + if (buffer) free(buffer); + buffer = NULL; + capacity = len = 0; +} + +unsigned char String::reserve(unsigned int size) +{ + if (buffer && capacity >= size) return 1; + if (changeBuffer(size)) { + if (len == 0) buffer[0] = 0; + return 1; + } + return 0; +} + +unsigned char String::changeBuffer(unsigned int maxStrLen) +{ + char *newbuffer = (char *)realloc(buffer, maxStrLen + 1); + if (newbuffer) { + buffer = newbuffer; + capacity = maxStrLen; + return 1; + } + return 0; +} + +/*********************************************/ +/* Copy and Move */ +/*********************************************/ + +String & String::copy(const char *cstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy(buffer, cstr); + return *this; +} + +String & String::copy(const __FlashStringHelper *pstr, unsigned int length) +{ + if (!reserve(length)) { + invalidate(); + return *this; + } + len = length; + strcpy_P(buffer, (PGM_P)pstr); + return *this; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +void String::move(String &rhs) +{ + if (buffer) { + if (rhs && capacity >= rhs.len) { + strcpy(buffer, rhs.buffer); + len = rhs.len; + rhs.len = 0; + return; + } else { + free(buffer); + } + } + buffer = rhs.buffer; + capacity = rhs.capacity; + len = rhs.len; + rhs.buffer = NULL; + rhs.capacity = 0; + rhs.len = 0; +} +#endif + +String & String::operator = (const String &rhs) +{ + if (this == &rhs) return *this; + + if (rhs.buffer) copy(rhs.buffer, rhs.len); + else invalidate(); + + return *this; +} + +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) +String & String::operator = (String &&rval) +{ + if (this != &rval) move(rval); + return *this; +} + +String & String::operator = (StringSumHelper &&rval) +{ + if (this != &rval) move(rval); + return *this; +} +#endif + +String & String::operator = (const char *cstr) +{ + if (cstr) copy(cstr, strlen(cstr)); + else invalidate(); + + return *this; +} + +String & String::operator = (const __FlashStringHelper *pstr) +{ + if (pstr) copy(pstr, strlen_P((PGM_P)pstr)); + else invalidate(); + + return *this; +} + +/*********************************************/ +/* concat */ +/*********************************************/ + +unsigned char String::concat(const String &s) +{ + return concat(s.buffer, s.len); +} + +unsigned char String::concat(const char *cstr, unsigned int length) +{ + unsigned int newlen = len + length; + if (!cstr) return 0; + if (length == 0) return 1; + if (!reserve(newlen)) return 0; + strcpy(buffer + len, cstr); + len = newlen; + return 1; +} + +unsigned char String::concat(const char *cstr) +{ + if (!cstr) return 0; + return concat(cstr, strlen(cstr)); +} + +unsigned char String::concat(char c) +{ + char buf[2]; + buf[0] = c; + buf[1] = 0; + return concat(buf, 1); +} + +unsigned char String::concat(unsigned char num) +{ + char buf[1 + 3 * sizeof(unsigned char)]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(int num) +{ + char buf[2 + 3 * sizeof(int)]; + itoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned int num) +{ + char buf[1 + 3 * sizeof(unsigned int)]; + utoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(long num) +{ + char buf[2 + 3 * sizeof(long)]; + ltoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(unsigned long num) +{ + char buf[1 + 3 * sizeof(unsigned long)]; + ultoa(num, buf, 10); + return concat(buf, strlen(buf)); +} + +unsigned char String::concat(float num) +{ + char buf[20]; + char* string = dtostrf(num, 4, 2, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(double num) +{ + char buf[20]; + char* string = dtostrf(num, 4, 2, buf); + return concat(string, strlen(string)); +} + +unsigned char String::concat(const __FlashStringHelper * str) +{ + if (!str) return 0; + int length = strlen_P((const char *) str); + if (length == 0) return 1; + unsigned int newlen = len + length; + if (!reserve(newlen)) return 0; + strcpy_P(buffer + len, (const char *) str); + len = newlen; + return 1; +} + +/*********************************************/ +/* Concatenate */ +/*********************************************/ + +StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs.buffer, rhs.len)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr) +{ + StringSumHelper &a = const_cast(lhs); + if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, char c) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(c)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, int num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, long num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, float num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, double num) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(num)) a.invalidate(); + return a; +} + +StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs) +{ + StringSumHelper &a = const_cast(lhs); + if (!a.concat(rhs)) a.invalidate(); + return a; +} + +/*********************************************/ +/* Comparison */ +/*********************************************/ + +int String::compareTo(const String &s) const +{ + if (!buffer || !s.buffer) { + if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer; + if (buffer && len > 0) return *(unsigned char *)buffer; + return 0; + } + return strcmp(buffer, s.buffer); +} + +unsigned char String::equals(const String &s2) const +{ + return (len == s2.len && compareTo(s2) == 0); +} + +unsigned char String::equals(const char *cstr) const +{ + if (len == 0) return (cstr == NULL || *cstr == 0); + if (cstr == NULL) return buffer[0] == 0; + return strcmp(buffer, cstr) == 0; +} + +unsigned char String::operator<(const String &rhs) const +{ + return compareTo(rhs) < 0; +} + +unsigned char String::operator>(const String &rhs) const +{ + return compareTo(rhs) > 0; +} + +unsigned char String::operator<=(const String &rhs) const +{ + return compareTo(rhs) <= 0; +} + +unsigned char String::operator>=(const String &rhs) const +{ + return compareTo(rhs) >= 0; +} + +unsigned char String::equalsIgnoreCase( const String &s2 ) const +{ + if (this == &s2) return 1; + if (len != s2.len) return 0; + if (len == 0) return 1; + const char *p1 = buffer; + const char *p2 = s2.buffer; + while (*p1) { + if (tolower(*p1++) != tolower(*p2++)) return 0; + } + return 1; +} + +unsigned char String::startsWith( const String &s2 ) const +{ + if (len < s2.len) return 0; + return startsWith(s2, 0); +} + +unsigned char String::startsWith( const String &s2, unsigned int offset ) const +{ + if (offset > len - s2.len || !buffer || !s2.buffer) return 0; + return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0; +} + +unsigned char String::endsWith( const String &s2 ) const +{ + if ( len < s2.len || !buffer || !s2.buffer) return 0; + return strcmp(&buffer[len - s2.len], s2.buffer) == 0; +} + +/*********************************************/ +/* Character Access */ +/*********************************************/ + +char String::charAt(unsigned int loc) const +{ + return operator[](loc); +} + +void String::setCharAt(unsigned int loc, char c) +{ + if (loc < len) buffer[loc] = c; +} + +char & String::operator[](unsigned int index) +{ + static char dummy_writable_char; + if (index >= len || !buffer) { + dummy_writable_char = 0; + return dummy_writable_char; + } + return buffer[index]; +} + +char String::operator[]( unsigned int index ) const +{ + if (index >= len || !buffer) return 0; + return buffer[index]; +} + +void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const +{ + if (!bufsize || !buf) return; + if (index >= len) { + buf[0] = 0; + return; + } + unsigned int n = bufsize - 1; + if (n > len - index) n = len - index; + strncpy((char *)buf, buffer + index, n); + buf[n] = 0; +} + +/*********************************************/ +/* Search */ +/*********************************************/ + +int String::indexOf(char c) const +{ + return indexOf(c, 0); +} + +int String::indexOf( char ch, unsigned int fromIndex ) const +{ + if (fromIndex >= len) return -1; + const char* temp = strchr(buffer + fromIndex, ch); + if (temp == NULL) return -1; + return temp - buffer; +} + +int String::indexOf(const String &s2) const +{ + return indexOf(s2, 0); +} + +int String::indexOf(const String &s2, unsigned int fromIndex) const +{ + if (fromIndex >= len) return -1; + const char *found = strstr(buffer + fromIndex, s2.buffer); + if (found == NULL) return -1; + return found - buffer; +} + +int String::lastIndexOf( char theChar ) const +{ + return lastIndexOf(theChar, len - 1); +} + +int String::lastIndexOf(char ch, unsigned int fromIndex) const +{ + if (fromIndex >= len) return -1; + char tempchar = buffer[fromIndex + 1]; + buffer[fromIndex + 1] = '\0'; + char* temp = strrchr( buffer, ch ); + buffer[fromIndex + 1] = tempchar; + if (temp == NULL) return -1; + return temp - buffer; +} + +int String::lastIndexOf(const String &s2) const +{ + return lastIndexOf(s2, len - s2.len); +} + +int String::lastIndexOf(const String &s2, unsigned int fromIndex) const +{ + if (s2.len == 0 || len == 0 || s2.len > len) return -1; + if (fromIndex >= len) fromIndex = len - 1; + int found = -1; + for (char *p = buffer; p <= buffer + fromIndex; p++) { + p = strstr(p, s2.buffer); + if (!p) break; + if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer; + } + return found; +} + +String String::substring(unsigned int left, unsigned int right) const +{ + if (left > right) { + unsigned int temp = right; + right = left; + left = temp; + } + String out; + if (left >= len) return out; + if (right > len) right = len; + char temp = buffer[right]; // save the replaced character + buffer[right] = '\0'; + out = buffer + left; // pointer arithmetic + buffer[right] = temp; //restore character + return out; +} + +/*********************************************/ +/* Modification */ +/*********************************************/ + +void String::replace(char find, char replace) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + if (*p == find) *p = replace; + } +} + +void String::replace(const String& find, const String& replace) +{ + if (len == 0 || find.len == 0) return; + int diff = replace.len - find.len; + char *readFrom = buffer; + char *foundAt; + if (diff == 0) { + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + memcpy(foundAt, replace.buffer, replace.len); + readFrom = foundAt + replace.len; + } + } else if (diff < 0) { + char *writeTo = buffer; + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + unsigned int n = foundAt - readFrom; + memcpy(writeTo, readFrom, n); + writeTo += n; + memcpy(writeTo, replace.buffer, replace.len); + writeTo += replace.len; + readFrom = foundAt + find.len; + len += diff; + } + strcpy(writeTo, readFrom); + } else { + unsigned int size = len; // compute size needed for result + while ((foundAt = strstr(readFrom, find.buffer)) != NULL) { + readFrom = foundAt + find.len; + size += diff; + } + if (size == len) return; + if (size > capacity && !changeBuffer(size)) return; // XXX: tell user! + int index = len - 1; + while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) { + readFrom = buffer + index + find.len; + memmove(readFrom + diff, readFrom, len - (readFrom - buffer)); + len += diff; + buffer[len] = 0; + memcpy(buffer + index, replace.buffer, replace.len); + index--; + } + } +} + +void String::remove(unsigned int index){ + // Pass the biggest integer as the count. The remove method + // below will take care of truncating it at the end of the + // string. + remove(index, (unsigned int)-1); +} + +void String::remove(unsigned int index, unsigned int count){ + if (index >= len) { return; } + if (count <= 0) { return; } + if (count > len - index) { count = len - index; } + char *writeTo = buffer + index; + len = len - count; + strncpy(writeTo, buffer + index + count,len - index); + buffer[len] = 0; +} + +void String::toLowerCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = tolower(*p); + } +} + +void String::toUpperCase(void) +{ + if (!buffer) return; + for (char *p = buffer; *p; p++) { + *p = toupper(*p); + } +} + +void String::trim(void) +{ + if (!buffer || len == 0) return; + char *begin = buffer; + while (isspace(*begin)) begin++; + char *end = buffer + len - 1; + while (isspace(*end) && end >= begin) end--; + len = end + 1 - begin; + if (begin > buffer) memcpy(buffer, begin, len); + buffer[len] = 0; +} + +/*********************************************/ +/* Parsing / Conversion */ +/*********************************************/ + +long String::toInt(void) const +{ + if (buffer) return atol(buffer); + return 0; +} + +float String::toFloat(void) const +{ + return float(toDouble()); +} + +double String::toDouble(void) const +{ + if (buffer) return atof(buffer); + return 0; +} diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h new file mode 100644 index 0000000..2cf4cd7 --- /dev/null +++ b/cores/arduino/WString.h @@ -0,0 +1,229 @@ +/* + WString.h - String library for Wiring & Arduino + ...mostly rewritten by Paul Stoffregen... + Copyright (c) 2009-10 Hernando Barragan. All right reserved. + Copyright 2011, Paul Stoffregen, paul@pjrc.com + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef String_class_h +#define String_class_h +#ifdef __cplusplus + +#include +#include +#include +#include + +// When compiling programs with this class, the following gcc parameters +// dramatically increase performance and memory (RAM) efficiency, typically +// with little or no increase in code size. +// -felide-constructors +// -std=c++0x + +class __FlashStringHelper; +#define F(string_literal) (reinterpret_cast(PSTR(string_literal))) + +// An inherited class for holding the result of a concatenation. These +// result objects are assumed to be writable by subsequent concatenations. +class StringSumHelper; + +// The string class +class String +{ + // use a function pointer to allow for "if (s)" without the + // complications of an operator bool(). for more information, see: + // http://www.artima.com/cppsource/safebool.html + typedef void (String::*StringIfHelperType)() const; + void StringIfHelper() const {} + +public: + // constructors + // creates a copy of the initial value. + // if the initial value is null or invalid, or if memory allocation + // fails, the string will be marked as invalid (i.e. "if (s)" will + // be false). + String(const char *cstr = ""); + String(const String &str); + String(const __FlashStringHelper *str); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String(String &&rval); + String(StringSumHelper &&rval); + #endif + explicit String(char c); + explicit String(unsigned char, unsigned char base=10); + explicit String(int, unsigned char base=10); + explicit String(unsigned int, unsigned char base=10); + explicit String(long, unsigned char base=10); + explicit String(unsigned long, unsigned char base=10); + explicit String(float, unsigned char decimalPlaces=2); + explicit String(double, unsigned char decimalPlaces=2); + ~String(void); + + // memory management + // return true on success, false on failure (in which case, the string + // is left unchanged). reserve(0), if successful, will validate an + // invalid string (i.e., "if (s)" will be true afterwards) + unsigned char reserve(unsigned int size); + inline unsigned int length(void) const {return len;} + + // creates a copy of the assigned value. if the value is null or + // invalid, or if the memory allocation fails, the string will be + // marked as invalid ("if (s)" will be false). + String & operator = (const String &rhs); + String & operator = (const char *cstr); + String & operator = (const __FlashStringHelper *str); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + String & operator = (String &&rval); + String & operator = (StringSumHelper &&rval); + #endif + + // concatenate (works w/ built-in types) + + // returns true on success, false on failure (in which case, the string + // is left unchanged). if the argument is null or invalid, the + // concatenation is considered unsuccessful. + unsigned char concat(const String &str); + unsigned char concat(const char *cstr); + unsigned char concat(char c); + unsigned char concat(unsigned char c); + unsigned char concat(int num); + unsigned char concat(unsigned int num); + unsigned char concat(long num); + unsigned char concat(unsigned long num); + unsigned char concat(float num); + unsigned char concat(double num); + unsigned char concat(const __FlashStringHelper * str); + + // if there's not enough memory for the concatenated value, the string + // will be left unchanged (but this isn't signalled in any way) + String & operator += (const String &rhs) {concat(rhs); return (*this);} + String & operator += (const char *cstr) {concat(cstr); return (*this);} + String & operator += (char c) {concat(c); return (*this);} + String & operator += (unsigned char num) {concat(num); return (*this);} + String & operator += (int num) {concat(num); return (*this);} + String & operator += (unsigned int num) {concat(num); return (*this);} + String & operator += (long num) {concat(num); return (*this);} + String & operator += (unsigned long num) {concat(num); return (*this);} + String & operator += (float num) {concat(num); return (*this);} + String & operator += (double num) {concat(num); return (*this);} + String & operator += (const __FlashStringHelper *str){concat(str); return (*this);} + + friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr); + friend StringSumHelper & operator + (const StringSumHelper &lhs, char c); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, float num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, double num); + friend StringSumHelper & operator + (const StringSumHelper &lhs, const __FlashStringHelper *rhs); + + // comparison (only works w/ Strings and "strings") + operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; } + int compareTo(const String &s) const; + unsigned char equals(const String &s) const; + unsigned char equals(const char *cstr) const; + unsigned char operator == (const String &rhs) const {return equals(rhs);} + unsigned char operator == (const char *cstr) const {return equals(cstr);} + unsigned char operator != (const String &rhs) const {return !equals(rhs);} + unsigned char operator != (const char *cstr) const {return !equals(cstr);} + unsigned char operator < (const String &rhs) const; + unsigned char operator > (const String &rhs) const; + unsigned char operator <= (const String &rhs) const; + unsigned char operator >= (const String &rhs) const; + unsigned char equalsIgnoreCase(const String &s) const; + unsigned char startsWith( const String &prefix) const; + unsigned char startsWith(const String &prefix, unsigned int offset) const; + unsigned char endsWith(const String &suffix) const; + + // character access + char charAt(unsigned int index) const; + void setCharAt(unsigned int index, char c); + char operator [] (unsigned int index) const; + char& operator [] (unsigned int index); + void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const; + void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const + { getBytes((unsigned char *)buf, bufsize, index); } + const char* c_str() const { return buffer; } + char* begin() { return buffer; } + char* end() { return buffer + length(); } + const char* begin() const { return c_str(); } + const char* end() const { return c_str() + length(); } + + // search + int indexOf( char ch ) const; + int indexOf( char ch, unsigned int fromIndex ) const; + int indexOf( const String &str ) const; + int indexOf( const String &str, unsigned int fromIndex ) const; + int lastIndexOf( char ch ) const; + int lastIndexOf( char ch, unsigned int fromIndex ) const; + int lastIndexOf( const String &str ) const; + int lastIndexOf( const String &str, unsigned int fromIndex ) const; + String substring( unsigned int beginIndex ) const { return substring(beginIndex, len); }; + String substring( unsigned int beginIndex, unsigned int endIndex ) const; + + // modification + void replace(char find, char replace); + void replace(const String& find, const String& replace); + void remove(unsigned int index); + void remove(unsigned int index, unsigned int count); + void toLowerCase(void); + void toUpperCase(void); + void trim(void); + + // parsing/conversion + long toInt(void) const; + float toFloat(void) const; + double toDouble(void) const; + +protected: + char *buffer; // the actual char array + unsigned int capacity; // the array length minus one (for the '\0') + unsigned int len; // the String length (not counting the '\0') +protected: + void init(void); + void invalidate(void); + unsigned char changeBuffer(unsigned int maxStrLen); + unsigned char concat(const char *cstr, unsigned int length); + + // copy and move + String & copy(const char *cstr, unsigned int length); + String & copy(const __FlashStringHelper *pstr, unsigned int length); + #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) + void move(String &rhs); + #endif +}; + +class StringSumHelper : public String +{ +public: + StringSumHelper(const String &s) : String(s) {} + StringSumHelper(const char *p) : String(p) {} + StringSumHelper(char c) : String(c) {} + StringSumHelper(unsigned char num) : String(num) {} + StringSumHelper(int num) : String(num) {} + StringSumHelper(unsigned int num) : String(num) {} + StringSumHelper(long num) : String(num) {} + StringSumHelper(unsigned long num) : String(num) {} + StringSumHelper(float num) : String(num) {} + StringSumHelper(double num) : String(num) {} +}; + +#endif // __cplusplus +#endif // String_class_h diff --git a/cores/arduino/abi.cpp b/cores/arduino/abi.cpp new file mode 100644 index 0000000..6e1b0f8 --- /dev/null +++ b/cores/arduino/abi.cpp @@ -0,0 +1,36 @@ +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__)); +extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__)); + +namespace std { + [[gnu::weak, noreturn]] void terminate() { + abort(); + } +} + +void __cxa_pure_virtual(void) { + std::terminate(); +} + +void __cxa_deleted_virtual(void) { + std::terminate(); +} diff --git a/cores/arduino/binary.h b/cores/arduino/binary.h new file mode 100644 index 0000000..aec4c73 --- /dev/null +++ b/cores/arduino/binary.h @@ -0,0 +1,534 @@ +/* + binary.h - Definitions for binary constants + Copyright (c) 2006 David A. Mellis. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef Binary_h +#define Binary_h + +#define B0 0 +#define B00 0 +#define B000 0 +#define B0000 0 +#define B00000 0 +#define B000000 0 +#define B0000000 0 +#define B00000000 0 +#define B1 1 +#define B01 1 +#define B001 1 +#define B0001 1 +#define B00001 1 +#define B000001 1 +#define B0000001 1 +#define B00000001 1 +#define B10 2 +#define B010 2 +#define B0010 2 +#define B00010 2 +#define B000010 2 +#define B0000010 2 +#define B00000010 2 +#define B11 3 +#define B011 3 +#define B0011 3 +#define B00011 3 +#define B000011 3 +#define B0000011 3 +#define B00000011 3 +#define B100 4 +#define B0100 4 +#define B00100 4 +#define B000100 4 +#define B0000100 4 +#define B00000100 4 +#define B101 5 +#define B0101 5 +#define B00101 5 +#define B000101 5 +#define B0000101 5 +#define B00000101 5 +#define B110 6 +#define B0110 6 +#define B00110 6 +#define B000110 6 +#define B0000110 6 +#define B00000110 6 +#define B111 7 +#define B0111 7 +#define B00111 7 +#define B000111 7 +#define B0000111 7 +#define B00000111 7 +#define B1000 8 +#define B01000 8 +#define B001000 8 +#define B0001000 8 +#define B00001000 8 +#define B1001 9 +#define B01001 9 +#define B001001 9 +#define B0001001 9 +#define B00001001 9 +#define B1010 10 +#define B01010 10 +#define B001010 10 +#define B0001010 10 +#define B00001010 10 +#define B1011 11 +#define B01011 11 +#define B001011 11 +#define B0001011 11 +#define B00001011 11 +#define B1100 12 +#define B01100 12 +#define B001100 12 +#define B0001100 12 +#define B00001100 12 +#define B1101 13 +#define B01101 13 +#define B001101 13 +#define B0001101 13 +#define B00001101 13 +#define B1110 14 +#define B01110 14 +#define B001110 14 +#define B0001110 14 +#define B00001110 14 +#define B1111 15 +#define B01111 15 +#define B001111 15 +#define B0001111 15 +#define B00001111 15 +#define B10000 16 +#define B010000 16 +#define B0010000 16 +#define B00010000 16 +#define B10001 17 +#define B010001 17 +#define B0010001 17 +#define B00010001 17 +#define B10010 18 +#define B010010 18 +#define B0010010 18 +#define B00010010 18 +#define B10011 19 +#define B010011 19 +#define B0010011 19 +#define B00010011 19 +#define B10100 20 +#define B010100 20 +#define B0010100 20 +#define B00010100 20 +#define B10101 21 +#define B010101 21 +#define B0010101 21 +#define B00010101 21 +#define B10110 22 +#define B010110 22 +#define B0010110 22 +#define B00010110 22 +#define B10111 23 +#define B010111 23 +#define B0010111 23 +#define B00010111 23 +#define B11000 24 +#define B011000 24 +#define B0011000 24 +#define B00011000 24 +#define B11001 25 +#define B011001 25 +#define B0011001 25 +#define B00011001 25 +#define B11010 26 +#define B011010 26 +#define B0011010 26 +#define B00011010 26 +#define B11011 27 +#define B011011 27 +#define B0011011 27 +#define B00011011 27 +#define B11100 28 +#define B011100 28 +#define B0011100 28 +#define B00011100 28 +#define B11101 29 +#define B011101 29 +#define B0011101 29 +#define B00011101 29 +#define B11110 30 +#define B011110 30 +#define B0011110 30 +#define B00011110 30 +#define B11111 31 +#define B011111 31 +#define B0011111 31 +#define B00011111 31 +#define B100000 32 +#define B0100000 32 +#define B00100000 32 +#define B100001 33 +#define B0100001 33 +#define B00100001 33 +#define B100010 34 +#define B0100010 34 +#define B00100010 34 +#define B100011 35 +#define B0100011 35 +#define B00100011 35 +#define B100100 36 +#define B0100100 36 +#define B00100100 36 +#define B100101 37 +#define B0100101 37 +#define B00100101 37 +#define B100110 38 +#define B0100110 38 +#define B00100110 38 +#define B100111 39 +#define B0100111 39 +#define B00100111 39 +#define B101000 40 +#define B0101000 40 +#define B00101000 40 +#define B101001 41 +#define B0101001 41 +#define B00101001 41 +#define B101010 42 +#define B0101010 42 +#define B00101010 42 +#define B101011 43 +#define B0101011 43 +#define B00101011 43 +#define B101100 44 +#define B0101100 44 +#define B00101100 44 +#define B101101 45 +#define B0101101 45 +#define B00101101 45 +#define B101110 46 +#define B0101110 46 +#define B00101110 46 +#define B101111 47 +#define B0101111 47 +#define B00101111 47 +#define B110000 48 +#define B0110000 48 +#define B00110000 48 +#define B110001 49 +#define B0110001 49 +#define B00110001 49 +#define B110010 50 +#define B0110010 50 +#define B00110010 50 +#define B110011 51 +#define B0110011 51 +#define B00110011 51 +#define B110100 52 +#define B0110100 52 +#define B00110100 52 +#define B110101 53 +#define B0110101 53 +#define B00110101 53 +#define B110110 54 +#define B0110110 54 +#define B00110110 54 +#define B110111 55 +#define B0110111 55 +#define B00110111 55 +#define B111000 56 +#define B0111000 56 +#define B00111000 56 +#define B111001 57 +#define B0111001 57 +#define B00111001 57 +#define B111010 58 +#define B0111010 58 +#define B00111010 58 +#define B111011 59 +#define B0111011 59 +#define B00111011 59 +#define B111100 60 +#define B0111100 60 +#define B00111100 60 +#define B111101 61 +#define B0111101 61 +#define B00111101 61 +#define B111110 62 +#define B0111110 62 +#define B00111110 62 +#define B111111 63 +#define B0111111 63 +#define B00111111 63 +#define B1000000 64 +#define B01000000 64 +#define B1000001 65 +#define B01000001 65 +#define B1000010 66 +#define B01000010 66 +#define B1000011 67 +#define B01000011 67 +#define B1000100 68 +#define B01000100 68 +#define B1000101 69 +#define B01000101 69 +#define B1000110 70 +#define B01000110 70 +#define B1000111 71 +#define B01000111 71 +#define B1001000 72 +#define B01001000 72 +#define B1001001 73 +#define B01001001 73 +#define B1001010 74 +#define B01001010 74 +#define B1001011 75 +#define B01001011 75 +#define B1001100 76 +#define B01001100 76 +#define B1001101 77 +#define B01001101 77 +#define B1001110 78 +#define B01001110 78 +#define B1001111 79 +#define B01001111 79 +#define B1010000 80 +#define B01010000 80 +#define B1010001 81 +#define B01010001 81 +#define B1010010 82 +#define B01010010 82 +#define B1010011 83 +#define B01010011 83 +#define B1010100 84 +#define B01010100 84 +#define B1010101 85 +#define B01010101 85 +#define B1010110 86 +#define B01010110 86 +#define B1010111 87 +#define B01010111 87 +#define B1011000 88 +#define B01011000 88 +#define B1011001 89 +#define B01011001 89 +#define B1011010 90 +#define B01011010 90 +#define B1011011 91 +#define B01011011 91 +#define B1011100 92 +#define B01011100 92 +#define B1011101 93 +#define B01011101 93 +#define B1011110 94 +#define B01011110 94 +#define B1011111 95 +#define B01011111 95 +#define B1100000 96 +#define B01100000 96 +#define B1100001 97 +#define B01100001 97 +#define B1100010 98 +#define B01100010 98 +#define B1100011 99 +#define B01100011 99 +#define B1100100 100 +#define B01100100 100 +#define B1100101 101 +#define B01100101 101 +#define B1100110 102 +#define B01100110 102 +#define B1100111 103 +#define B01100111 103 +#define B1101000 104 +#define B01101000 104 +#define B1101001 105 +#define B01101001 105 +#define B1101010 106 +#define B01101010 106 +#define B1101011 107 +#define B01101011 107 +#define B1101100 108 +#define B01101100 108 +#define B1101101 109 +#define B01101101 109 +#define B1101110 110 +#define B01101110 110 +#define B1101111 111 +#define B01101111 111 +#define B1110000 112 +#define B01110000 112 +#define B1110001 113 +#define B01110001 113 +#define B1110010 114 +#define B01110010 114 +#define B1110011 115 +#define B01110011 115 +#define B1110100 116 +#define B01110100 116 +#define B1110101 117 +#define B01110101 117 +#define B1110110 118 +#define B01110110 118 +#define B1110111 119 +#define B01110111 119 +#define B1111000 120 +#define B01111000 120 +#define B1111001 121 +#define B01111001 121 +#define B1111010 122 +#define B01111010 122 +#define B1111011 123 +#define B01111011 123 +#define B1111100 124 +#define B01111100 124 +#define B1111101 125 +#define B01111101 125 +#define B1111110 126 +#define B01111110 126 +#define B1111111 127 +#define B01111111 127 +#define B10000000 128 +#define B10000001 129 +#define B10000010 130 +#define B10000011 131 +#define B10000100 132 +#define B10000101 133 +#define B10000110 134 +#define B10000111 135 +#define B10001000 136 +#define B10001001 137 +#define B10001010 138 +#define B10001011 139 +#define B10001100 140 +#define B10001101 141 +#define B10001110 142 +#define B10001111 143 +#define B10010000 144 +#define B10010001 145 +#define B10010010 146 +#define B10010011 147 +#define B10010100 148 +#define B10010101 149 +#define B10010110 150 +#define B10010111 151 +#define B10011000 152 +#define B10011001 153 +#define B10011010 154 +#define B10011011 155 +#define B10011100 156 +#define B10011101 157 +#define B10011110 158 +#define B10011111 159 +#define B10100000 160 +#define B10100001 161 +#define B10100010 162 +#define B10100011 163 +#define B10100100 164 +#define B10100101 165 +#define B10100110 166 +#define B10100111 167 +#define B10101000 168 +#define B10101001 169 +#define B10101010 170 +#define B10101011 171 +#define B10101100 172 +#define B10101101 173 +#define B10101110 174 +#define B10101111 175 +#define B10110000 176 +#define B10110001 177 +#define B10110010 178 +#define B10110011 179 +#define B10110100 180 +#define B10110101 181 +#define B10110110 182 +#define B10110111 183 +#define B10111000 184 +#define B10111001 185 +#define B10111010 186 +#define B10111011 187 +#define B10111100 188 +#define B10111101 189 +#define B10111110 190 +#define B10111111 191 +#define B11000000 192 +#define B11000001 193 +#define B11000010 194 +#define B11000011 195 +#define B11000100 196 +#define B11000101 197 +#define B11000110 198 +#define B11000111 199 +#define B11001000 200 +#define B11001001 201 +#define B11001010 202 +#define B11001011 203 +#define B11001100 204 +#define B11001101 205 +#define B11001110 206 +#define B11001111 207 +#define B11010000 208 +#define B11010001 209 +#define B11010010 210 +#define B11010011 211 +#define B11010100 212 +#define B11010101 213 +#define B11010110 214 +#define B11010111 215 +#define B11011000 216 +#define B11011001 217 +#define B11011010 218 +#define B11011011 219 +#define B11011100 220 +#define B11011101 221 +#define B11011110 222 +#define B11011111 223 +#define B11100000 224 +#define B11100001 225 +#define B11100010 226 +#define B11100011 227 +#define B11100100 228 +#define B11100101 229 +#define B11100110 230 +#define B11100111 231 +#define B11101000 232 +#define B11101001 233 +#define B11101010 234 +#define B11101011 235 +#define B11101100 236 +#define B11101101 237 +#define B11101110 238 +#define B11101111 239 +#define B11110000 240 +#define B11110001 241 +#define B11110010 242 +#define B11110011 243 +#define B11110100 244 +#define B11110101 245 +#define B11110110 246 +#define B11110111 247 +#define B11111000 248 +#define B11111001 249 +#define B11111010 250 +#define B11111011 251 +#define B11111100 252 +#define B11111101 253 +#define B11111110 254 +#define B11111111 255 + +#endif diff --git a/cores/arduino/hooks.c b/cores/arduino/hooks.c new file mode 100644 index 0000000..641eabc --- /dev/null +++ b/cores/arduino/hooks.c @@ -0,0 +1,31 @@ +/* + Copyright (c) 2012 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * Empty yield() hook. + * + * This function is intended to be used by library writers to build + * libraries or sketches that supports cooperative threads. + * + * Its defined as a weak symbol and it can be redefined to implement a + * real cooperative scheduler. + */ +static void __empty() { + // Empty +} +void yield(void) __attribute__ ((weak, alias("__empty"))); diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp new file mode 100644 index 0000000..434cd40 --- /dev/null +++ b/cores/arduino/main.cpp @@ -0,0 +1,52 @@ +/* + main.cpp - Main loop for Arduino sketches + Copyright (c) 2005-2013 Arduino Team. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +// Declared weak in Arduino.h to allow user redefinitions. +int atexit(void (* /*func*/ )()) { return 0; } + +// Weak empty variant initialization function. +// May be redefined by variant files. +void initVariant() __attribute__((weak)); +void initVariant() { } + +void setupUSB() __attribute__((weak)); +void setupUSB() { } + +int main(void) +{ + init(); + + initVariant(); + +#if defined(USBCON) + USBDevice.attach(); +#endif + + setup(); + + for (;;) { + loop(); + if (serialEventRun) serialEventRun(); + } + + return 0; +} + diff --git a/cores/arduino/new b/cores/arduino/new new file mode 100644 index 0000000..8cf2103 --- /dev/null +++ b/cores/arduino/new @@ -0,0 +1,66 @@ +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef NEW_H +#define NEW_H + +#include + +namespace std { + struct nothrow_t {}; + extern const nothrow_t nothrow; + + // These are not actually implemented, to prevent overhead and + // complexity. They are still declared to allow implementing + // them in user code if needed. + typedef void (*new_handler)(); + new_handler set_new_handler(new_handler new_p) noexcept; + new_handler get_new_handler() noexcept; + + // This is normally declared in various headers that we do not have + // available, so just define it here. We could also use ::size_t + // below, but then anyone including can no longer assume + // std::size_t is available. + using size_t = ::size_t; +} // namespace std + +[[gnu::weak]] void * operator new(std::size_t size); +[[gnu::weak]] void * operator new[](std::size_t size); + +[[gnu::weak]] void * operator new(std::size_t size, const std::nothrow_t tag) noexcept; +[[gnu::weak]] void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept; + +void * operator new(std::size_t size, void *place) noexcept; +void * operator new[](std::size_t size, void *place) noexcept; + +[[gnu::weak]] void operator delete(void * ptr) noexcept; +[[gnu::weak]] void operator delete[](void * ptr) noexcept; + +#if __cplusplus >= 201402L +[[gnu::weak]] void operator delete(void* ptr, std::size_t size) noexcept; +[[gnu::weak]] void operator delete[](void * ptr, std::size_t size) noexcept; +#endif // __cplusplus >= 201402L + +[[gnu::weak]] void operator delete(void* ptr, const std::nothrow_t& tag) noexcept; +[[gnu::weak]] void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept; + +void operator delete(void* ptr, void* place) noexcept; +void operator delete[](void* ptr, void* place) noexcept; + +#endif + diff --git a/cores/arduino/new.cpp b/cores/arduino/new.cpp new file mode 100644 index 0000000..7ca4931 --- /dev/null +++ b/cores/arduino/new.cpp @@ -0,0 +1,117 @@ +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "new.h" + +// The C++ spec dictates that allocation failure should cause the +// (non-nothrow version of the) operator new to throw an exception. +// Since we expect to have exceptions disabled, it would be more +// appropriate (and probably standards-compliant) to terminate instead. +// Historically failure causes null to be returned, but this define +// allows switching to more robust terminating behaviour (that might +// become the default at some point in the future). Note that any code +// that wants null to be returned can (and should) use the nothrow +// versions of the new statement anyway and is unaffected by this. +// #define NEW_TERMINATES_ON_FAILURE + +namespace std { + // Defined in abi.cpp + void terminate(); + + const nothrow_t nothrow; +} + +static void * new_helper(std::size_t size) { + // Even zero-sized allocations should return a unique pointer, but + // malloc does not guarantee this + if (size == 0) + size = 1; + return malloc(size); +} + +void * operator new(std::size_t size) { + void *res = new_helper(size); +#if defined(NEW_TERMINATES_ON_FAILURE) + if (!res) + std::terminate(); +#endif + return res; +} +void * operator new[](std::size_t size) { + return operator new(size); +} + +void * operator new(std::size_t size, const std::nothrow_t tag) noexcept { +#if defined(NEW_TERMINATES_ON_FAILURE) + // Cannot call throwing operator new as standard suggests, so call + // new_helper directly then + return new_helper(size); +#else + return operator new(size); +#endif +} +void * operator new[](std::size_t size, const std::nothrow_t& tag) noexcept { +#if defined(NEW_TERMINATES_ON_FAILURE) + // Cannot call throwing operator new[] as standard suggests, so call + // malloc directly then + return new_helper(size); +#else + return operator new[](size); +#endif +} + +void * operator new(std::size_t size, void *place) noexcept { + // Nothing to do + (void)size; // unused + return place; +} +void * operator new[](std::size_t size, void *place) noexcept { + return operator new(size, place); +} + +void operator delete(void * ptr) noexcept { + free(ptr); +} +void operator delete[](void * ptr) noexcept { + operator delete(ptr); +} + +#if __cplusplus >= 201402L +void operator delete(void* ptr, std::size_t size) noexcept { + operator delete(ptr); +} +void operator delete[](void * ptr, std::size_t size) noexcept { + operator delete[](ptr); +} +#endif // __cplusplus >= 201402L + +void operator delete(void* ptr, const std::nothrow_t& tag) noexcept { + operator delete(ptr); +} +void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { + operator delete[](ptr); +} + +void operator delete(void* ptr, void* place) noexcept { + (void)ptr; (void)place; // unused + // Nothing to do +} +void operator delete[](void* ptr, void* place) noexcept { + (void)ptr; (void)place; // unused + // Nothing to do +} diff --git a/cores/arduino/new.h b/cores/arduino/new.h new file mode 100644 index 0000000..d529853 --- /dev/null +++ b/cores/arduino/new.h @@ -0,0 +1,3 @@ +// This file originally used a non-standard name for this Arduino core +// only, so still expose the old new.h name for compatibility. +#include "new" diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c new file mode 100644 index 0000000..8caf455 --- /dev/null +++ b/cores/arduino/wiring.c @@ -0,0 +1,392 @@ +/* + wiring.c - Partial implementation of the Wiring API for the ATmega8. + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +#include "wiring_private.h" + +// the prescaler is set so that timer0 ticks every 64 clock cycles, and the +// the overflow handler is called every 256 ticks. +#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) + +// the whole number of milliseconds per timer0 overflow +#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000) + +// the fractional number of milliseconds per timer0 overflow. we shift right +// by three to fit these numbers into a byte. (for the clock speeds we care +// about - 8 and 16 MHz - this doesn't lose precision.) +#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3) +#define FRACT_MAX (1000 >> 3) + +volatile unsigned long timer0_overflow_count = 0; +volatile unsigned long timer0_millis = 0; +static unsigned char timer0_fract = 0; + +#if defined(TIM0_OVF_vect) +ISR(TIM0_OVF_vect) +#else +ISR(TIMER0_OVF_vect) +#endif +{ + // copy these to local variables so they can be stored in registers + // (volatile variables must be read from memory on every access) + unsigned long m = timer0_millis; + unsigned char f = timer0_fract; + + m += MILLIS_INC; + f += FRACT_INC; + if (f >= FRACT_MAX) { + f -= FRACT_MAX; + m += 1; + } + + timer0_fract = f; + timer0_millis = m; + timer0_overflow_count++; +} + +unsigned long millis() +{ + unsigned long m; + uint8_t oldSREG = SREG; + + // disable interrupts while we read timer0_millis or we might get an + // inconsistent value (e.g. in the middle of a write to timer0_millis) + cli(); + m = timer0_millis; + SREG = oldSREG; + + return m; +} + +unsigned long micros() { + unsigned long m; + uint8_t oldSREG = SREG, t; + + cli(); + m = timer0_overflow_count; +#if defined(TCNT0) + t = TCNT0; +#elif defined(TCNT0L) + t = TCNT0L; +#else + #error TIMER 0 not defined +#endif + +#ifdef TIFR0 + if ((TIFR0 & _BV(TOV0)) && (t < 255)) + m++; +#else + if ((TIFR & _BV(TOV0)) && (t < 255)) + m++; +#endif + + SREG = oldSREG; + + return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond()); +} + +void delay(unsigned long ms) +{ + uint32_t start = micros(); + + while (ms > 0) { + yield(); + while ( ms > 0 && (micros() - start) >= 1000) { + ms--; + start += 1000; + } + } +} + +/* Delay for the given number of microseconds. Assumes a 1, 8, 12, 16, 20 or 24 MHz clock. */ +void delayMicroseconds(unsigned int us) +{ + // call = 4 cycles + 2 to 4 cycles to init us(2 for constant delay, 4 for variable) + + // calling avrlib's delay_us() function with low values (e.g. 1 or + // 2 microseconds) gives delays longer than desired. + //delay_us(us); +#if F_CPU >= 24000000L + // for the 24 MHz clock for the adventurous ones trying to overclock + + // zero delay fix + if (!us) return; // = 3 cycles, (4 when true) + + // the following loop takes a 1/6 of a microsecond (4 cycles) + // per iteration, so execute it six times for each microsecond of + // delay requested. + us *= 6; // x6 us, = 7 cycles + + // account for the time taken in the preceding commands. + // we just burned 22 (24) cycles above, remove 5, (5*4=20) + // us is at least 6 so we can subtract 5 + us -= 5; //=2 cycles + +#elif F_CPU >= 20000000L + // for the 20 MHz clock on rare Arduino boards + + // for a one-microsecond delay, simply return. the overhead + // of the function call takes 18 (20) cycles, which is 1us + __asm__ __volatile__ ( + "nop" "\n\t" + "nop" "\n\t" + "nop" "\n\t" + "nop"); //just waiting 4 cycles + if (us <= 1) return; // = 3 cycles, (4 when true) + + // the following loop takes a 1/5 of a microsecond (4 cycles) + // per iteration, so execute it five times for each microsecond of + // delay requested. + us = (us << 2) + us; // x5 us, = 7 cycles + + // account for the time taken in the preceding commands. + // we just burned 26 (28) cycles above, remove 7, (7*4=28) + // us is at least 10 so we can subtract 7 + us -= 7; // 2 cycles + +#elif F_CPU >= 16000000L + // for the 16 MHz clock on most Arduino boards + + // for a one-microsecond delay, simply return. the overhead + // of the function call takes 14 (16) cycles, which is 1us + if (us <= 1) return; // = 3 cycles, (4 when true) + + // the following loop takes 1/4 of a microsecond (4 cycles) + // per iteration, so execute it four times for each microsecond of + // delay requested. + us <<= 2; // x4 us, = 4 cycles + + // account for the time taken in the preceding commands. + // we just burned 19 (21) cycles above, remove 5, (5*4=20) + // us is at least 8 so we can subtract 5 + us -= 5; // = 2 cycles, + +#elif F_CPU >= 12000000L + // for the 12 MHz clock if somebody is working with USB + + // for a 1 microsecond delay, simply return. the overhead + // of the function call takes 14 (16) cycles, which is 1.5us + if (us <= 1) return; // = 3 cycles, (4 when true) + + // the following loop takes 1/3 of a microsecond (4 cycles) + // per iteration, so execute it three times for each microsecond of + // delay requested. + us = (us << 1) + us; // x3 us, = 5 cycles + + // account for the time taken in the preceding commands. + // we just burned 20 (22) cycles above, remove 5, (5*4=20) + // us is at least 6 so we can subtract 5 + us -= 5; //2 cycles + +#elif F_CPU >= 8000000L + // for the 8 MHz internal clock + + // for a 1 and 2 microsecond delay, simply return. the overhead + // of the function call takes 14 (16) cycles, which is 2us + if (us <= 2) return; // = 3 cycles, (4 when true) + + // the following loop takes 1/2 of a microsecond (4 cycles) + // per iteration, so execute it twice for each microsecond of + // delay requested. + us <<= 1; //x2 us, = 2 cycles + + // account for the time taken in the preceding commands. + // we just burned 17 (19) cycles above, remove 4, (4*4=16) + // us is at least 6 so we can subtract 4 + us -= 4; // = 2 cycles + +#else + // for the 1 MHz internal clock (default settings for common Atmega microcontrollers) + + // the overhead of the function calls is 14 (16) cycles + if (us <= 16) return; //= 3 cycles, (4 when true) + if (us <= 25) return; //= 3 cycles, (4 when true), (must be at least 25 if we want to subtract 22) + + // compensate for the time taken by the preceding and next commands (about 22 cycles) + us -= 22; // = 2 cycles + // the following loop takes 4 microseconds (4 cycles) + // per iteration, so execute it us/4 times + // us is at least 4, divided by 4 gives us 1 (no zero delay bug) + us >>= 2; // us div 4, = 4 cycles + + +#endif + + // busy wait + __asm__ __volatile__ ( + "1: sbiw %0,1" "\n\t" // 2 cycles + "brne 1b" : "=w" (us) : "0" (us) // 2 cycles + ); + // return = 4 cycles +} + +void init() +{ + // this needs to be called before setup() or some functions won't + // work there + sei(); + + // on the ATmega168, timer 0 is also used for fast hardware pwm + // (using phase-correct PWM would mean that timer 0 overflowed half as often + // resulting in different millis() behavior on the ATmega8 and ATmega168) +#if defined(TCCR0A) && defined(WGM01) + sbi(TCCR0A, WGM01); + sbi(TCCR0A, WGM00); +#endif + + // set timer 0 prescale factor to 64 +#if defined(__AVR_ATmega128__) + // CPU specific: different values for the ATmega128 + sbi(TCCR0, CS02); +#elif defined(TCCR0) && defined(CS01) && defined(CS00) + // this combination is for the standard atmega8 + sbi(TCCR0, CS01); + sbi(TCCR0, CS00); +#elif defined(TCCR0B) && defined(CS01) && defined(CS00) + // this combination is for the standard 168/328/1280/2560 + sbi(TCCR0B, CS01); + sbi(TCCR0B, CS00); +#elif defined(TCCR0A) && defined(CS01) && defined(CS00) + // this combination is for the __AVR_ATmega645__ series + sbi(TCCR0A, CS01); + sbi(TCCR0A, CS00); +#else + #error Timer 0 prescale factor 64 not set correctly +#endif + + // enable timer 0 overflow interrupt +#if defined(TIMSK) && defined(TOIE0) + sbi(TIMSK, TOIE0); +#elif defined(TIMSK0) && defined(TOIE0) + sbi(TIMSK0, TOIE0); +#else + #error Timer 0 overflow interrupt not set correctly +#endif + + // timers 1 and 2 are used for phase-correct hardware pwm + // this is better for motors as it ensures an even waveform + // note, however, that fast pwm mode can achieve a frequency of up + // 8 MHz (with a 16 MHz clock) at 50% duty cycle + +#if defined(TCCR1B) && defined(CS11) && defined(CS10) + TCCR1B = 0; + + // set timer 1 prescale factor to 64 + sbi(TCCR1B, CS11); +#if F_CPU >= 8000000L + sbi(TCCR1B, CS10); +#endif +#elif defined(TCCR1) && defined(CS11) && defined(CS10) + sbi(TCCR1, CS11); +#if F_CPU >= 8000000L + sbi(TCCR1, CS10); +#endif +#endif + // put timer 1 in 8-bit phase correct pwm mode +#if defined(TCCR1A) && defined(WGM10) + sbi(TCCR1A, WGM10); +#endif + + // set timer 2 prescale factor to 64 +#if defined(TCCR2) && defined(CS22) + sbi(TCCR2, CS22); +#elif defined(TCCR2B) && defined(CS22) + sbi(TCCR2B, CS22); +//#else + // Timer 2 not finished (may not be present on this CPU) +#endif + + // configure timer 2 for phase correct pwm (8-bit) +#if defined(TCCR2) && defined(WGM20) + sbi(TCCR2, WGM20); +#elif defined(TCCR2A) && defined(WGM20) + sbi(TCCR2A, WGM20); +//#else + // Timer 2 not finished (may not be present on this CPU) +#endif + +#if defined(TCCR3B) && defined(CS31) && defined(WGM30) + sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64 + sbi(TCCR3B, CS30); + sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode +#endif + +#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */ + sbi(TCCR4B, CS42); // set timer4 prescale factor to 64 + sbi(TCCR4B, CS41); + sbi(TCCR4B, CS40); + sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode + sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A + sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D +#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */ +#if defined(TCCR4B) && defined(CS41) && defined(WGM40) + sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64 + sbi(TCCR4B, CS40); + sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode +#endif +#endif /* end timer4 block for ATMEGA1280/2560 and similar */ + +#if defined(TCCR5B) && defined(CS51) && defined(WGM50) + sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64 + sbi(TCCR5B, CS50); + sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode +#endif + +#if defined(ADCSRA) + // set a2d prescaler so we are inside the desired 50-200 KHz range. + #if F_CPU >= 16000000 // 16 MHz / 128 = 125 KHz + sbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #elif F_CPU >= 8000000 // 8 MHz / 64 = 125 KHz + sbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + cbi(ADCSRA, ADPS0); + #elif F_CPU >= 4000000 // 4 MHz / 32 = 125 KHz + sbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #elif F_CPU >= 2000000 // 2 MHz / 16 = 125 KHz + sbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + cbi(ADCSRA, ADPS0); + #elif F_CPU >= 1000000 // 1 MHz / 8 = 125 KHz + cbi(ADCSRA, ADPS2); + sbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #else // 128 kHz / 2 = 64 KHz -> This is the closest you can get, the prescaler is 2 + cbi(ADCSRA, ADPS2); + cbi(ADCSRA, ADPS1); + sbi(ADCSRA, ADPS0); + #endif + // enable a2d conversions + sbi(ADCSRA, ADEN); +#endif + + // the bootloader connects pins 0 and 1 to the USART; disconnect them + // here so they can be used as normal digital i/o; they will be + // reconnected in Serial.begin() +#if defined(UCSRB) + UCSRB = 0; +#elif defined(UCSR0B) + UCSR0B = 0; +#endif +} diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c new file mode 100644 index 0000000..0de64f7 --- /dev/null +++ b/cores/arduino/wiring_analog.c @@ -0,0 +1,286 @@ +/* + wiring_analog.c - analog input and output + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + + Modified 28 September 2010 by Mark Sproul +*/ + +#include "wiring_private.h" +#include "pins_arduino.h" + +uint8_t analog_reference = DEFAULT; + +void analogReference(uint8_t mode) +{ + // can't actually set the register here because the default setting + // will connect AVCC and the AREF pin, which would cause a short if + // there's something connected to AREF. + analog_reference = mode; +} + +int analogRead(uint8_t pin) +{ + +#if defined(analogPinToChannel) +#if defined(__AVR_ATmega32U4__) + if (pin >= 18) pin -= 18; // allow for channel or pin numbers +#endif + pin = analogPinToChannel(pin); +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + if (pin >= 54) pin -= 54; // allow for channel or pin numbers +#elif defined(__AVR_ATmega32U4__) + if (pin >= 18) pin -= 18; // allow for channel or pin numbers +#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) + if (pin >= 24) pin -= 24; // allow for channel or pin numbers +#else + if (pin >= 14) pin -= 14; // allow for channel or pin numbers +#endif + +#if defined(ADCSRB) && defined(MUX5) + // the MUX5 bit of ADCSRB selects whether we're reading from channels + // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high). + ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5); +#endif + + // set the analog reference (high two bits of ADMUX) and select the + // channel (low 4 bits). this also sets ADLAR (left-adjust result) + // to 0 (the default). +#if defined(ADMUX) +#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = (analog_reference << 4) | (pin & 0x07); +#else + ADMUX = (analog_reference << 6) | (pin & 0x07); +#endif +#endif + + // without a delay, we seem to read from the wrong channel + //delay(1); + +#if defined(ADCSRA) && defined(ADC) + // start the conversion + sbi(ADCSRA, ADSC); + + // ADSC is cleared when the conversion finishes + while (bit_is_set(ADCSRA, ADSC)); + + // ADC macro takes care of reading ADC register. + // avr-gcc implements the proper reading order: ADCL is read first. + return ADC; +#else + // we dont have an ADC, return 0 + return 0; +#endif +} + +// Right now, PWM output only works on the pins with +// hardware support. These are defined in the appropriate +// pins_*.c file. For the rest of the pins, we default +// to digital output. +void analogWrite(uint8_t pin, int val) +{ + // We need to make sure the PWM output is enabled for those pins + // that support it, as we turn it off when digitally reading or + // writing with them. Also, make sure the pin is in output mode + // for consistenty with Wiring, which doesn't require a pinMode + // call for the analog output pins. + pinMode(pin, OUTPUT); + if (val == 0) + { + digitalWrite(pin, LOW); + } + else if (val == 255) + { + digitalWrite(pin, HIGH); + } + else + { + switch(digitalPinToTimer(pin)) + { + // XXX fix needed for atmega8 + #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__) + case TIMER0A: + // connect pwm to pin on timer 0 + sbi(TCCR0, COM00); + OCR0 = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0A1) + case TIMER0A: + // connect pwm to pin on timer 0, channel A + sbi(TCCR0A, COM0A1); + OCR0A = val; // set pwm duty + break; + #endif + + #if defined(TCCR0A) && defined(COM0B1) + case TIMER0B: + // connect pwm to pin on timer 0, channel B + sbi(TCCR0A, COM0B1); + OCR0B = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1A1) + case TIMER1A: + // connect pwm to pin on timer 1, channel A + sbi(TCCR1A, COM1A1); + OCR1A = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1B1) + case TIMER1B: + // connect pwm to pin on timer 1, channel B + sbi(TCCR1A, COM1B1); + OCR1B = val; // set pwm duty + break; + #endif + + #if defined(TCCR1A) && defined(COM1C1) + case TIMER1C: + // connect pwm to pin on timer 1, channel C + sbi(TCCR1A, COM1C1); + OCR1C = val; // set pwm duty + break; + #endif + + #if defined(TCCR2) && defined(COM21) + case TIMER2: + // connect pwm to pin on timer 2 + sbi(TCCR2, COM21); + OCR2 = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2A1) + case TIMER2A: + // connect pwm to pin on timer 2, channel A + sbi(TCCR2A, COM2A1); + OCR2A = val; // set pwm duty + break; + #endif + + #if defined(TCCR2A) && defined(COM2B1) + case TIMER2B: + // connect pwm to pin on timer 2, channel B + sbi(TCCR2A, COM2B1); + OCR2B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3A1) + case TIMER3A: + // connect pwm to pin on timer 3, channel A + sbi(TCCR3A, COM3A1); + OCR3A = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3B1) + case TIMER3B: + // connect pwm to pin on timer 3, channel B + sbi(TCCR3A, COM3B1); + OCR3B = val; // set pwm duty + break; + #endif + + #if defined(TCCR3A) && defined(COM3C1) + case TIMER3C: + // connect pwm to pin on timer 3, channel C + sbi(TCCR3A, COM3C1); + OCR3C = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) + case TIMER4A: + //connect pwm to pin on timer 4, channel A + sbi(TCCR4A, COM4A1); + #if defined(COM4A0) // only used on 32U4 + cbi(TCCR4A, COM4A0); + #endif + OCR4A = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4B1) + case TIMER4B: + // connect pwm to pin on timer 4, channel B + sbi(TCCR4A, COM4B1); + OCR4B = val; // set pwm duty + break; + #endif + + #if defined(TCCR4A) && defined(COM4C1) + case TIMER4C: + // connect pwm to pin on timer 4, channel C + sbi(TCCR4A, COM4C1); + OCR4C = val; // set pwm duty + break; + #endif + + #if defined(TCCR4C) && defined(COM4D1) + case TIMER4D: + // connect pwm to pin on timer 4, channel D + sbi(TCCR4C, COM4D1); + #if defined(COM4D0) // only used on 32U4 + cbi(TCCR4C, COM4D0); + #endif + OCR4D = val; // set pwm duty + break; + #endif + + + #if defined(TCCR5A) && defined(COM5A1) + case TIMER5A: + // connect pwm to pin on timer 5, channel A + sbi(TCCR5A, COM5A1); + OCR5A = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5B1) + case TIMER5B: + // connect pwm to pin on timer 5, channel B + sbi(TCCR5A, COM5B1); + OCR5B = val; // set pwm duty + break; + #endif + + #if defined(TCCR5A) && defined(COM5C1) + case TIMER5C: + // connect pwm to pin on timer 5, channel C + sbi(TCCR5A, COM5C1); + OCR5C = val; // set pwm duty + break; + #endif + + case NOT_ON_TIMER: + default: + if (val < 128) { + digitalWrite(pin, LOW); + } else { + digitalWrite(pin, HIGH); + } + } + } +} + diff --git a/cores/arduino/wiring_digital.c b/cores/arduino/wiring_digital.c new file mode 100644 index 0000000..432a150 --- /dev/null +++ b/cores/arduino/wiring_digital.c @@ -0,0 +1,179 @@ +/* + wiring_digital.c - digital input and output functions + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + + Modified 28 September 2010 by Mark Sproul +*/ + +#define ARDUINO_MAIN +#include "wiring_private.h" +#include "pins_arduino.h" + +void pinMode(uint8_t pin, uint8_t mode) +{ + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + volatile uint8_t *reg, *out; + + if (port == NOT_A_PIN) return; + + // JWS: can I let the optimizer do this? + reg = portModeRegister(port); + out = portOutputRegister(port); + + if (mode == INPUT) { + uint8_t oldSREG = SREG; + cli(); + *reg &= ~bit; + *out &= ~bit; + SREG = oldSREG; + } else if (mode == INPUT_PULLUP) { + uint8_t oldSREG = SREG; + cli(); + *reg &= ~bit; + *out |= bit; + SREG = oldSREG; + } else { + uint8_t oldSREG = SREG; + cli(); + *reg |= bit; + SREG = oldSREG; + } +} + +// Forcing this inline keeps the callers from having to push their own stuff +// on the stack. It is a good performance win and only takes 1 more byte per +// user than calling. (It will take more bytes on the 168.) +// +// But shouldn't this be moved into pinMode? Seems silly to check and do on +// each digitalread or write. +// +// Mark Sproul: +// - Removed inline. Save 170 bytes on atmega1280 +// - changed to a switch statement; added 32 bytes but much easier to read and maintain. +// - Added more #ifdefs, now compiles for atmega645 +// +//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline)); +//static inline void turnOffPWM(uint8_t timer) +static void turnOffPWM(uint8_t timer) +{ + switch (timer) + { + #if defined(TCCR1A) && defined(COM1A1) + case TIMER1A: cbi(TCCR1A, COM1A1); break; + #endif + #if defined(TCCR1A) && defined(COM1B1) + case TIMER1B: cbi(TCCR1A, COM1B1); break; + #endif + #if defined(TCCR1A) && defined(COM1C1) + case TIMER1C: cbi(TCCR1A, COM1C1); break; + #endif + + #if defined(TCCR2) && defined(COM21) + case TIMER2: cbi(TCCR2, COM21); break; + #endif + + #if defined(TCCR0A) && defined(COM0A1) + case TIMER0A: cbi(TCCR0A, COM0A1); break; + #endif + + #if defined(TCCR0A) && defined(COM0B1) + case TIMER0B: cbi(TCCR0A, COM0B1); break; + #endif + #if defined(TCCR2A) && defined(COM2A1) + case TIMER2A: cbi(TCCR2A, COM2A1); break; + #endif + #if defined(TCCR2A) && defined(COM2B1) + case TIMER2B: cbi(TCCR2A, COM2B1); break; + #endif + + #if defined(TCCR3A) && defined(COM3A1) + case TIMER3A: cbi(TCCR3A, COM3A1); break; + #endif + #if defined(TCCR3A) && defined(COM3B1) + case TIMER3B: cbi(TCCR3A, COM3B1); break; + #endif + #if defined(TCCR3A) && defined(COM3C1) + case TIMER3C: cbi(TCCR3A, COM3C1); break; + #endif + + #if defined(TCCR4A) && defined(COM4A1) + case TIMER4A: cbi(TCCR4A, COM4A1); break; + #endif + #if defined(TCCR4A) && defined(COM4B1) + case TIMER4B: cbi(TCCR4A, COM4B1); break; + #endif + #if defined(TCCR4A) && defined(COM4C1) + case TIMER4C: cbi(TCCR4A, COM4C1); break; + #endif + #if defined(TCCR4C) && defined(COM4D1) + case TIMER4D: cbi(TCCR4C, COM4D1); break; + #endif + + #if defined(TCCR5A) + case TIMER5A: cbi(TCCR5A, COM5A1); break; + case TIMER5B: cbi(TCCR5A, COM5B1); break; + case TIMER5C: cbi(TCCR5A, COM5C1); break; + #endif + } +} + +void digitalWrite(uint8_t pin, uint8_t val) +{ + uint8_t timer = digitalPinToTimer(pin); + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + volatile uint8_t *out; + + if (port == NOT_A_PIN) return; + + // If the pin that support PWM output, we need to turn it off + // before doing a digital write. + if (timer != NOT_ON_TIMER) turnOffPWM(timer); + + out = portOutputRegister(port); + + uint8_t oldSREG = SREG; + cli(); + + if (val == LOW) { + *out &= ~bit; + } else { + *out |= bit; + } + + SREG = oldSREG; +} + +int digitalRead(uint8_t pin) +{ + uint8_t timer = digitalPinToTimer(pin); + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + + if (port == NOT_A_PIN) return LOW; + + // If the pin that support PWM output, we need to turn it off + // before getting a digital reading. + if (timer != NOT_ON_TIMER) turnOffPWM(timer); + + if (*portInputRegister(port) & bit) return HIGH; + return LOW; +} diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h new file mode 100644 index 0000000..a277b14 --- /dev/null +++ b/cores/arduino/wiring_private.h @@ -0,0 +1,72 @@ +/* + wiring_private.h - Internal header file. + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +#ifndef WiringPrivate_h +#define WiringPrivate_h + +#include +#include +#include +#include + +#include "Arduino.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +#ifndef cbi +#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) +#endif +#ifndef sbi +#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) +#endif + +uint32_t countPulseASM(volatile uint8_t *port, uint8_t bit, uint8_t stateMask, unsigned long maxloops); + +#define EXTERNAL_INT_0 0 +#define EXTERNAL_INT_1 1 +#define EXTERNAL_INT_2 2 +#define EXTERNAL_INT_3 3 +#define EXTERNAL_INT_4 4 +#define EXTERNAL_INT_5 5 +#define EXTERNAL_INT_6 6 +#define EXTERNAL_INT_7 7 + +#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__) || \ + defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__) +#define EXTERNAL_NUM_INTERRUPTS 8 +#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) +#define EXTERNAL_NUM_INTERRUPTS 3 +#elif defined(__AVR_ATmega32U4__) +#define EXTERNAL_NUM_INTERRUPTS 5 +#else +#define EXTERNAL_NUM_INTERRUPTS 2 +#endif + +typedef void (*voidFuncPtr)(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif diff --git a/cores/arduino/wiring_pulse.S b/cores/arduino/wiring_pulse.S new file mode 100644 index 0000000..1dd22e6 --- /dev/null +++ b/cores/arduino/wiring_pulse.S @@ -0,0 +1,178 @@ +/* + wiring_pulse.s - pulseInASM() function in different flavours + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2014 Martino Facchin + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +/* + * The following routine was generated by avr-gcc 4.8.3 with the following parameters + * -gstabs -Wa,-ahlmsd=output.lst -dp -fverbose-asm -O2 + * on the original C function + * + * unsigned long pulseInSimpl(volatile uint8_t *port, uint8_t bit, uint8_t stateMask, unsigned long maxloops) + * { + * unsigned long width = 0; + * // wait for any previous pulse to end + * while ((*port & bit) == stateMask) + * if (--maxloops == 0) + * return 0; + * + * // wait for the pulse to start + * while ((*port & bit) != stateMask) + * if (--maxloops == 0) + * return 0; + * + * // wait for the pulse to stop + * while ((*port & bit) == stateMask) { + * if (++width == maxloops) + * return 0; + * } + * return width; + * } + * + * some compiler outputs were removed but the rest of the code is untouched + */ + +#include + +.section .text + +.global countPulseASM + +countPulseASM: + +.LM0: +.LFBB1: + push r12 ; ; 130 pushqi1/1 [length = 1] + push r13 ; ; 131 pushqi1/1 [length = 1] + push r14 ; ; 132 pushqi1/1 [length = 1] + push r15 ; ; 133 pushqi1/1 [length = 1] + push r16 ; ; 134 pushqi1/1 [length = 1] + push r17 ; ; 135 pushqi1/1 [length = 1] +/* prologue: function */ +/* frame size = 0 */ +/* stack size = 6 */ +.L__stack_usage = 6 + mov r30,r24 ; port, port ; 2 *movhi/1 [length = 2] + mov r31,r25 ; port, port +/* unsigned long width = 0; +*** // wait for any previous pulse to end +*** while ((*port & bit) == stateMask) +*/ +.LM1: + rjmp .L2 ; ; 181 jump [length = 1] +.L4: +/* if (--maxloops == 0) */ +.LM2: + subi r16,1 ; maxloops, ; 17 addsi3/2 [length = 4] + sbc r17, r1 ; maxloops + sbc r18, r1 ; maxloops + sbc r19, r1 ; maxloops + breq .L13 ; , ; 19 branch [length = 1] +.L2: +/* if (--maxloops == 0) */ +.LM3: + ld r25,Z ; D.1554, *port_7(D) ; 22 movqi_insn/4 [length = 1] + and r25,r22 ; D.1554, bit ; 24 andqi3/1 [length = 1] + cp r25,r20 ; D.1554, stateMask ; 25 *cmpqi/2 [length = 1] + breq .L4 ; , ; 26 branch [length = 1] + rjmp .L6 ; ; 184 jump [length = 1] +.L7: +/* return 0; +*** +*** // wait for the pulse to start +*** while ((*port & bit) != stateMask) +*** if (--maxloops == 0) +*/ +.LM4: + subi r16,1 ; maxloops, ; 31 addsi3/2 [length = 4] + sbc r17, r1 ; maxloops + sbc r18, r1 ; maxloops + sbc r19, r1 ; maxloops + breq .L13 ; , ; 33 branch [length = 1] +.L6: +/* if (--maxloops == 0) */ +.LM5: + ld r25,Z ; D.1554, *port_7(D) ; 41 movqi_insn/4 [length = 1] + and r25,r22 ; D.1554, bit ; 43 andqi3/1 [length = 1] + cpse r25,r20 ; D.1554, stateMask ; 44 enable_interrupt-3 [length = 1] + rjmp .L7 ; + mov r12, r1 ; width ; 7 *movsi/2 [length = 4] + mov r13, r1 ; width + mov r14, r1 ; width + mov r15, r1 ; width + rjmp .L9 ; ; 186 jump [length = 1] +.L10: +/* return 0; +*** +*** // wait for the pulse to stop +*** while ((*port & bit) == stateMask) { +*** if (++width == maxloops) +*/ +.LM6: + ldi r24,-1 ; , ; 50 addsi3/3 [length = 5] + sub r12,r24 ; width, + sbc r13,r24 ; width, + sbc r14,r24 ; width, + sbc r15,r24 ; width, + cp r16,r12 ; maxloops, width ; 51 *cmpsi/2 [length = 4] + cpc r17,r13 ; maxloops, width + cpc r18,r14 ; maxloops, width + cpc r19,r15 ; maxloops, width + breq .L13 ; , ; 52 branch [length = 1] +.L9: +/* if (++width == maxloops) */ +.LM7: + ld r24,Z ; D.1554, *port_7(D) ; 60 movqi_insn/4 [length = 1] + and r24,r22 ; D.1554, bit ; 62 andqi3/1 [length = 1] + cp r24,r20 ; D.1554, stateMask ; 63 *cmpqi/2 [length = 1] + breq .L10 ; , ; 64 branch [length = 1] +/* return 0; +*** } +*** return width; +*/ +.LM8: + mov r22,r12 ; D.1553, width ; 108 movqi_insn/1 [length = 1] + mov r23,r13 ; D.1553, width ; 109 movqi_insn/1 [length = 1] + mov r24,r14 ; D.1553, width ; 110 movqi_insn/1 [length = 1] + mov r25,r15 ; D.1553, width ; 111 movqi_insn/1 [length = 1] +/* epilogue start */ +.LM9: + pop r17 ; ; 171 popqi [length = 1] + pop r16 ; ; 172 popqi [length = 1] + pop r15 ; ; 173 popqi [length = 1] + pop r14 ; ; 174 popqi [length = 1] + pop r13 ; ; 175 popqi [length = 1] + pop r12 ; ; 176 popqi [length = 1] + ret ; 177 return_from_epilogue [length = 1] +.L13: +.LM10: + ldi r22,0 ; D.1553 ; 120 movqi_insn/1 [length = 1] + ldi r23,0 ; D.1553 ; 121 movqi_insn/1 [length = 1] + ldi r24,0 ; D.1553 ; 122 movqi_insn/1 [length = 1] + ldi r25,0 ; D.1553 ; 123 movqi_insn/1 [length = 1] +/* epilogue start */ +.LM11: + pop r17 ; ; 138 popqi [length = 1] + pop r16 ; ; 139 popqi [length = 1] + pop r15 ; ; 140 popqi [length = 1] + pop r14 ; ; 141 popqi [length = 1] + pop r13 ; ; 142 popqi [length = 1] + pop r12 ; ; 143 popqi [length = 1] + ret ; 144 return_from_epilogue [length = 1] diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c new file mode 100644 index 0000000..d6e0434 --- /dev/null +++ b/cores/arduino/wiring_pulse.c @@ -0,0 +1,93 @@ +/* + wiring_pulse.c - pulseIn() function + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +#include "wiring_private.h" +#include "pins_arduino.h" + +/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH + * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds + * to 3 minutes in length, but must be called at least a few dozen microseconds + * before the start of the pulse. + * + * This function performs better with short pulses in noInterrupt() context + */ +unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) +{ + // cache the port and bit of the pin in order to speed up the + // pulse width measuring loop and achieve finer resolution. calling + // digitalRead() instead yields much coarser resolution. + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + uint8_t stateMask = (state ? bit : 0); + + // convert the timeout from microseconds to a number of times through + // the initial loop; it takes approximately 16 clock cycles per iteration + unsigned long maxloops = microsecondsToClockCycles(timeout)/16; + + unsigned long width = countPulseASM(portInputRegister(port), bit, stateMask, maxloops); + + // prevent clockCyclesToMicroseconds to return bogus values if countPulseASM timed out + if (width) + return clockCyclesToMicroseconds(width * 16 + 16); + else + return 0; +} + +/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH + * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds + * to 3 minutes in length, but must be called at least a few dozen microseconds + * before the start of the pulse. + * + * ATTENTION: + * this function relies on micros() so cannot be used in noInterrupt() context + */ +unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) +{ + // cache the port and bit of the pin in order to speed up the + // pulse width measuring loop and achieve finer resolution. calling + // digitalRead() instead yields much coarser resolution. + uint8_t bit = digitalPinToBitMask(pin); + uint8_t port = digitalPinToPort(pin); + uint8_t stateMask = (state ? bit : 0); + + unsigned long startMicros = micros(); + + // wait for any previous pulse to end + while ((*portInputRegister(port) & bit) == stateMask) { + if (micros() - startMicros > timeout) + return 0; + } + + // wait for the pulse to start + while ((*portInputRegister(port) & bit) != stateMask) { + if (micros() - startMicros > timeout) + return 0; + } + + unsigned long start = micros(); + // wait for the pulse to stop + while ((*portInputRegister(port) & bit) == stateMask) { + if (micros() - startMicros > timeout) + return 0; + } + return micros() - start; +} diff --git a/cores/arduino/wiring_shift.c b/cores/arduino/wiring_shift.c new file mode 100644 index 0000000..a9b3be5 --- /dev/null +++ b/cores/arduino/wiring_shift.c @@ -0,0 +1,56 @@ +/* + wiring_shift.c - shiftOut() function + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +#include "wiring_private.h" + +uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) { + uint8_t value = 0; + uint8_t i; + + for (i = 0; i < 8; ++i) { + digitalWrite(clockPin, HIGH); + if (bitOrder == LSBFIRST) + value |= digitalRead(dataPin) << i; + else + value |= digitalRead(dataPin) << (7 - i); + digitalWrite(clockPin, LOW); + } + return value; +} + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) +{ + uint8_t i; + + for (i = 0; i < 8; i++) { + if (bitOrder == LSBFIRST) { + digitalWrite(dataPin, val & 1); + val >>= 1; + } else { + digitalWrite(dataPin, (val & 128) != 0); + val <<= 1; + } + + digitalWrite(clockPin, HIGH); + digitalWrite(clockPin, LOW); + } +} diff --git a/libraries/idiBusMaster/library.properties b/libraries/idiBusMaster/library.properties new file mode 100644 index 0000000..7c58b58 --- /dev/null +++ b/libraries/idiBusMaster/library.properties @@ -0,0 +1,8 @@ +name=IdiBusMaster +version=0.2.0 +author=OLIMP LLC +maintainer=OLIMP LLC +sentence=Microchip/Arduino library for interfacing with IdiBus devices. +url=https://olimp-z.ru +architectures=avr +includes=IdiBusMaster.h diff --git a/libraries/idiBusMaster/src/IDIBUS_1Wire.cpp b/libraries/idiBusMaster/src/IDIBUS_1Wire.cpp new file mode 100644 index 0000000..ac4f5c9 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_1Wire.cpp @@ -0,0 +1,157 @@ +#include "IDIBUS_1Wire.h" + +IdiBus_1Wire::IdiBus_1Wire (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Temperature = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_1Wire::IdiBus_1Wire () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Temperature = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_1Wire_DATA_TYPE IdiBus_1Wire::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_1Wire_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_1Wire_CH3_NUM]; } +} + +void IdiBus_1Wire::changeAllAutomatiocConversionStates() +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1Wire_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; + uint8_t ChDataSize = 1; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_1Wire_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_1Wire_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_1Wire_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_1Wire_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + } +} + +void IdiBus_1Wire::startAllOneShotConversions() +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1Wire_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; + uint8_t ChDataSize = 0; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_1Wire_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_1Wire_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_1Wire_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_1Wire_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + } +} + +uint8_t IdiBus_1Wire::getTemperature(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_1Wire_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1Wire_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x03; //Get Value + uint8_t ChDataSize = 2; //int16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Temperature = (int16_t) ( ((int16_t)MMES.RxData[1]<<8) | + ((int16_t)MMES.RxData[2])); + } + } + return this->CH[ChNum].MMES_Error; +} + +void IdiBus_1Wire::getAllChTemperature(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1Wire_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x03; + uint8_t ChDataSize = 2; //int16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_1Wire_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_1Wire_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_1Wire_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_1Wire_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Temperature = (uint16_t) ( + (uint16_t)((uint16_t)MMES.RxData[DataPos] << 8) | + (uint16_t)((uint16_t)MMES.RxData[DataPos+1]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +void IdiBus_1Wire::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +/* +void IdiBus_1Wire::initAllChs(void) +{ + //uint8_t TxData[2]; + //TxData[0] = (uint8_t)(CurrentLimit >> 8); + //TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1Wire_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_1Wire_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_1Wire_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + } + } +} +*/ diff --git a/libraries/idiBusMaster/src/IDIBUS_1Wire.h b/libraries/idiBusMaster/src/IDIBUS_1Wire.h new file mode 100644 index 0000000..18b3403 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_1Wire.h @@ -0,0 +1,51 @@ +#ifndef IDIBUS_1Wire_H_ +#define IDIBUS_1Wire_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_SETTINGS_ERROR 100U +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_CRC_ERROR 101U +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_NO_DATA 102U +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_NO_SENSOR 103U +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_AUTOCONVERSION_ENABLED 105U +#define IDIBUS_CUSTDEF_ERCODE_OneWIRE_CHANNEL_ONESHOT_IN_PROGRESS 106U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_1Wire_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_1Wire_CNTRL_DEV = 0, // ADC device ID + IDIBUS_1Wire_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (1Wire) + IDIBUS_1Wire_CH0_NUM = 0, // Name for 1st channel + IDIBUS_1Wire_CH1_NUM, // 2nd ... + IDIBUS_1Wire_CH2_NUM, // + IDIBUS_1Wire_CH3_NUM, // + IDIBUS_1Wire_CH_COUNT, // Module channel count + }; + +typedef struct { + int16_t Temperature; // Last voltage + uint8_t MMES_Error; // Last error +} IDIBUS_1Wire_DATA_TYPE; // Data structure for ADC channels + +class IdiBus_1Wire : public IdiBusSlave { // ADC module class + public: // Public methods + IdiBus_1Wire ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_1Wire (); // init without specified line and addr (default value line=NULL,addr=1) + + void startAllOneShotConversions(); + void changeAllAutomatiocConversionStates(); + uint8_t getTemperature(uint8_t ChNum); // Sends MMES and gets Ch voltage (writes to CH structure and return error code) + void getAllChTemperature(void); // Sends MMES and gets all Ch voltages (writes to CH structure) + + //void initAllChs(void); // Inits all adc channels on module + + IDIBUS_1Wire_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure for ADC channel + + protected : // 'Private' methods and structures + IDIBUS_1Wire_DATA_TYPE CH[IDIBUS_1Wire_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; + +#endif /* IDIBUS_41Wire_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_1xGen.cpp b/libraries/idiBusMaster/src/IDIBUS_1xGen.cpp new file mode 100644 index 0000000..682c18c --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_1xGen.cpp @@ -0,0 +1,145 @@ +#include "IDIBUS_1xGen.h" + +IdiBus_1xGen::IdiBus_1xGen (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + TMP.MMES_Error = 0; + TMP.RXlen = 0; +} + +IdiBus_1xGen::IdiBus_1xGen () : IdiBusSlave() +{ + TMP.MMES_Error = 0; + TMP.RXlen = 0; +} + +IDIBUS_1xGEN_DATA_TYPE IdiBus_1xGen::getData(void) +{ + return this->TMP; +} + +// setters +void IdiBus_1xGen::setFirstModeTime (uint8_t FirstTime) { + first_mode_time = FirstTime; +} + +void IdiBus_1xGen::setFirstModePWM (uint16_t FirstPWM) { + first_mode_pwm_value = FirstPWM; +} + +void IdiBus_1xGen::setFirstModeF (uint32_t FirstF) { + first_mode_f = FirstF; +} + +void IdiBus_1xGen::setFirstModeInvert (uint8_t FirstInvert) { + first_mode_invert = FirstInvert; +} + +void IdiBus_1xGen::setSecondModeTime (uint8_t SecondTime) { + second_mode_time = SecondTime; +} + +void IdiBus_1xGen::setSecondModePWM (uint16_t SecondPWM) { + second_mode_pwm_value = SecondPWM; +} + +void IdiBus_1xGen::setSecondModeF (uint32_t SecondF) { + second_mode_f = SecondF; +} + +void IdiBus_1xGen::setSecondModeInvert (uint8_t SecondInvert) { + second_mode_invert = SecondInvert; +} + +// getters +uint8_t IdiBus_1xGen::getFirstModeTime () { + return first_mode_time; +} + +uint16_t IdiBus_1xGen::getFirstModePWM () { + return first_mode_pwm_value; +} + +uint32_t IdiBus_1xGen::getFirstModeF () { + return first_mode_f; +} + +uint8_t IdiBus_1xGen::getFirstModeInvert () { + return first_mode_invert; +} + +uint8_t IdiBus_1xGen::getSecondModeTime () { + return second_mode_time; +} + +uint16_t IdiBus_1xGen::getSecondModePWM () { + return second_mode_pwm_value; +} + +uint32_t IdiBus_1xGen::getSecondModeF () { + return second_mode_f; +} + +uint8_t IdiBus_1xGen::getSecondModeInvert () { + return second_mode_invert; +} + +uint8_t IdiBus_1xGen::startGen(void) { + //uint8_t buf[10] = {this->first_mode_time, + //(uint8_t)((this->first_mode_pwm_value >> 8) & 0xFF), (uint8_t)(this->first_mode_pwm_value & 0xFF), + //this->first_mode_f, this->first_mode_invert, + //this->second_mode_time, + //(uint8_t)((this->second_mode_pwm_value >> 8) & 0xFF), (uint8_t)(this->second_mode_pwm_value & 0xFF), + //this->second_mode_f, this->second_mode_invert}; + + uint8_t buf[16] = {this->first_mode_time, + (uint8_t)((this->first_mode_pwm_value >> 8) & 0xFF), (uint8_t)(this->first_mode_pwm_value & 0xFF), + + (uint8_t)((this->first_mode_f >> 24) & 0xFF), + (uint8_t)((this->first_mode_f >> 16) & 0xFF), + (uint8_t)((this->first_mode_f >> 8) & 0xFF), + (uint8_t)(this->first_mode_f & 0xFF), + + this->first_mode_invert, + // + this->second_mode_time, + (uint8_t)((this->second_mode_pwm_value >> 8) & 0xFF), (uint8_t)(this->second_mode_pwm_value & 0xFF), + + (uint8_t)((this->second_mode_f >> 24) & 0xFF), + (uint8_t)((this->second_mode_f >> 16) & 0xFF), + (uint8_t)((this->second_mode_f >> 8) & 0xFF), + (uint8_t)(this->second_mode_f & 0xFF), + + this->second_mode_invert}; + + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1xGen_DEV; + MMES.CHNL.NUM = 0; + MMES.ComFunc = 0x01; + MMES.TxDataLength = 16; + MMES.TxData = buf; + uint8_t RequestError = this->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->TMP.MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->TMP.MMES_Error = MMES.RxData[0]; } + else { + this->TMP.MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; + } + return this->TMP.MMES_Error; +} + +uint8_t IdiBus_1xGen::resetGen() { + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_1xGen_DEV; + MMES.CHNL.NUM = 0; + MMES.ComFunc = 0x02; + + uint8_t ChDataSize = 0; + uint8_t RequestError = this->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->TMP.MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->TMP.MMES_Error = MMES.RxData[0]; } + else { + this->TMP.MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; + } + return this->TMP.MMES_Error; +} \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_1xGen.h b/libraries/idiBusMaster/src/IDIBUS_1xGen.h new file mode 100644 index 0000000..b0a47b1 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_1xGen.h @@ -0,0 +1,62 @@ +#ifndef IDIBUS_1XGEN_H_ +#define IDIBUS_1XGEN_H_ + +#include "IdiBusSlave.h" + +enum IDIBUS_1xGen_MODULE_DEVICES_LIST { + IDIBUS_1xGen_DEV = 0x00, + IDIBUS_1xGen_DEVICES_NUMBER +}; + +typedef struct { + uint8_t RXlen; + uint8_t RXbuf[255]; + uint8_t MMES_Error; // Last error +} IDIBUS_1xGEN_DATA_TYPE; + +class IdiBus_1xGen : public IdiBusSlave { + public: + IdiBus_1xGen(IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); + IdiBus_1xGen(); + + uint8_t startGen(); + uint8_t resetGen(); + + IDIBUS_1xGEN_DATA_TYPE getData(); + + void setFirstModeTime (uint8_t FirstTime); + void setFirstModePWM (uint16_t FirstPWM); + void setFirstModeF (uint32_t FirstF); + void setFirstModeInvert (uint8_t FirstInvert); + + void setSecondModeTime (uint8_t SecondTime); + void setSecondModePWM (uint16_t SecondPWM); + void setSecondModeF (uint32_t SecondF); + void setSecondModeInvert (uint8_t SecondInvert); + + uint8_t getFirstModeTime (); + uint16_t getFirstModePWM (); + uint32_t getFirstModeF (); + uint8_t getFirstModeInvert (); + + uint8_t getSecondModeTime (); + uint16_t getSecondModePWM (); + uint32_t getSecondModeF (); + uint8_t getSecondModeInvert (); + + protected: + IDIBUS_1xGEN_DATA_TYPE TMP; + + private: + uint8_t first_mode_time; + uint16_t first_mode_pwm_value; + uint32_t first_mode_f; + uint8_t first_mode_invert; + + uint8_t second_mode_time; + uint16_t second_mode_pwm_value; + uint32_t second_mode_f; + uint8_t second_mode_invert; +}; + +#endif /* IDIBUS_1XGEN_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.cpp b/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.cpp new file mode 100644 index 0000000..ebb8532 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.cpp @@ -0,0 +1,160 @@ +#include "IDIBUS_2SALTMETER.h" + +IdiBus_2SALT::IdiBus_2SALT (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Voltage = 0; + } +} + +IdiBus_2SALT::IdiBus_2SALT () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Voltage = 0; + } +} + +IDIBUS_2SALT_DATA_TYPE IdiBus_2SALT::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_2SALT_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_2SALT_CH1_NUM]; } +} + +uint8_t IdiBus_2SALT::getChSaltData(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_2SALT_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x03; //Get Value + uint8_t ChDataSize = 2; //uint16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Voltage = (uint16_t) ( + ((uint16_t)MMES.RxData[1] << 8 ) | + ((uint16_t)MMES.RxData[2]) ); + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_2SALT::getAllChsSaltData(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x03; + uint8_t ChDataSize = 2; //uint16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_2SALT_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_2SALT_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_2SALT_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_2SALT_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { + this->CH[I].Voltage = (uint16_t) ( + ((uint16_t)MMES.RxData[DataPos] << 8) | + ((uint16_t)MMES.RxData[DataPos+1]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +void IdiBus_2SALT::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +uint8_t IdiBus_2SALT::setCh(uint8_t ChNum, uint8_t state) +{ + if (ChNum >= IDIBUS_2SALT_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //init + MMES.TxDataLength = 1; + MMES.TxData = &state; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_2SALT::setAllCh(uint8_t state) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; //init + MMES.TxDataLength = 1; + MMES.TxData = &state; + MMES.CHNL.ALLSAME = 1; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_2SALT_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_2SALT_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + } + } +} + +uint8_t IdiBus_2SALT::startOneShot(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_2SALT_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x02; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_2SALT::startAllOneShot(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_2SALT_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_2SALT_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_2SALT_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + } + } +} + diff --git a/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.h b/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.h new file mode 100644 index 0000000..eda373c --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_2SALTMETER.h @@ -0,0 +1,51 @@ +#ifndef IDIBUS_2SALT_H_ +#define IDIBUS_2SALT_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_POWEROFF 100U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_CHANNEL_CAPTURE_IN_PROGRESS 101U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_CHANNEL_NOT_SELECTED 102U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_CHANNEL_NO_VALID_DATA 103U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_CHANNEL_NO_STABLE_DATA 104U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_TWI_ERROR 106U +#define IDIBUS_CUSTDEF_ERCODE_SALTMETER_TWI_BUSY 107U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_2SALT_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_2SALT_CNTRL_DEV = 0, // SALT device ID + IDIBUS_2SALT_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (SALT) + IDIBUS_2SALT_CH0_NUM = 0, // Name for 1st channel + IDIBUS_2SALT_CH1_NUM, // 2nd ... + IDIBUS_2SALT_CH_COUNT, // Channel count + }; + +typedef struct { + uint16_t Voltage; // Last returned value + uint8_t MMES_Error; // Last error +} IDIBUS_2SALT_DATA_TYPE; // Data structure for SALT channels + +class IdiBus_2SALT : public IdiBusSlave { // SALT module class + public: // Public methods + IdiBus_2SALT ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_2SALT(); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t getChSaltData(uint8_t ChNum); // Sends MMES and get Counts on ch (writes to CH structure and returns MMES error code) + void getAllChsSaltData(void); // Sends MMES and get Counts on ch (writes to CH structure) + + uint8_t startOneShot(uint8_t ChNum); + void startAllOneShot(void); + + uint8_t setCh(uint8_t ChNum, uint8_t state); // Inits specified counter channel on module + void setAllCh(uint8_t state); // Inits all counters on module + + IDIBUS_2SALT_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure + + protected: // 'Private' methods and structures + IDIBUS_2SALT_DATA_TYPE CH[IDIBUS_2SALT_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; +#endif diff --git a/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.cpp b/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.cpp new file mode 100644 index 0000000..671324f --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.cpp @@ -0,0 +1,250 @@ +#include "IDIBUS_3Thermo1HT.h" + +IdiBusDevice *IdiBus_3Thermo1HT::getDevice(uint8_t Num) +{ + switch (Num) + { + case ( IDIBUS_3Thermo1HT_THERMO_DEV ) : { return &this->THERMO_DEV; } + //case ( IDIBUS_INPV_CNTRL_DEV ) : { return &this->INPV_DEV; } + default : { return NULL; } + } +} + +IdiBus_3Thermo1HT_ThermoChannel::IdiBus_3Thermo1HT_ThermoChannel(void) : IdiBusChannel() +{ + this->Temperature = 0; + this->CJTemperature = 0; + this->MMES_Error = (1<<2); + /*Bit error list + 0 - Sensor fault + 1 - over/under voltage + 2 - no data error + 6 - TC out of range + 7 - CJ out of range + */ +} + +IdiBusChannel *IdiBus_3Thermo1HT_ThermoDevice::getChannel(uint8_t Num) +{ + if ( Num < IDIBUS_3Thermo1HT_Thermo_CH_COUNT ) { return &this->CH[Num]; } + else { return NULL; } +} + +IdiBus_3Thermo1HT_ThermoChannel IdiBus_3Thermo1HT_ThermoDevice::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_3Thermo1HT_Thermo_CH_COUNT ) { return this->CH[ChNum]; } + else { IdiBus_3Thermo1HT_ThermoChannel NewData; return NewData; } +} + +uint8_t IdiBus_3Thermo1HT_ThermoDevice::startOneShotConversion(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x02; //Start one shot conversion + + uint8_t ChDataSize = 0; //None + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + } + return this->CH[ChNum].MMES_Error; +} // + +void IdiBus_3Thermo1HT_ThermoDevice::startAllOneShotConversions() +{ + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02;//Set automatic conversion + uint8_t ChDataSize = 0; //None + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_3Thermo1HT_Thermo_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_3Thermo1HT_Thermo_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_3Thermo1HT_Thermo_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + } +} + +uint8_t IdiBus_3Thermo1HT_ThermoDevice::changeAutomatiocConversionState(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //Set automatic conversion + + uint8_t ChDataSize = 1; //uint8_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + //if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + //{ + //Ignoring return value + //} + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_3Thermo1HT_ThermoDevice::changeAllAutomatiocConversionStates(void) +{ + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01;//Set automatic conversion + uint8_t ChDataSize = 1; //uint8_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_3Thermo1HT_Thermo_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_3Thermo1HT_Thermo_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_3Thermo1HT_Thermo_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + //{ + //} + } + +} + +uint8_t IdiBus_3Thermo1HT_ThermoDevice::getTemperature(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x03; //Get Value + uint8_t ChDataSize = 3; //int24_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Temperature = (int32_t) ( ((int32_t)MMES.RxData[1]<<16) | + ((int32_t)MMES.RxData[2]<<8) | + ((int32_t)MMES.RxData[3])); + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_3Thermo1HT_ThermoDevice::getAllChTemperature(void) +{ + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x03; + uint8_t ChDataSize = 3; //int24_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_3Thermo1HT_Thermo_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_3Thermo1HT_Thermo_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_3Thermo1HT_Thermo_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Temperature = (int32_t) ( ((int32_t)MMES.RxData[DataPos]<<16) | + ((int32_t)MMES.RxData[DataPos+1]<<8) | + ((int32_t)MMES.RxData[DataPos+2])); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +uint8_t IdiBus_3Thermo1HT_ThermoDevice::getCJTemperature(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x04; //Get Value + uint8_t ChDataSize = 2; //int16_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Temperature = (int16_t) ( ((int16_t)MMES.RxData[1]<<8) | + ((int16_t)MMES.RxData[2]) ); + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_3Thermo1HT_ThermoDevice::getAllChCJTemperature(void) +{ + IdiBusMMES MMES(static_cast(this->Module)->getAddress()); + MMES.DEV.NUM = IDIBUS_3Thermo1HT_THERMO_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x04; + uint8_t ChDataSize = 2; //int24_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_3Thermo1HT_Thermo_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_3Thermo1HT_Thermo_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_3Thermo1HT_Thermo_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_3Thermo1HT_Thermo_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Temperature = (int16_t) ( ((int16_t)MMES.RxData[DataPos]<<8) | + ((int16_t)MMES.RxData[DataPos+1]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +void IdiBus_3Thermo1HT_ThermoDevice::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} diff --git a/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.h b/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.h new file mode 100644 index 0000000..110b08b --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_3Thermo1HT.h @@ -0,0 +1,68 @@ +#ifndef IDIBUS_3Thermo1HT_H_ +#define IDIBUS_3Thermo1HT_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_NO_SENSOR 100U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_NO_DATA 101U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_OVER_UNDER_VOLTAGE 102U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_TC_OUT_OF_RANGE 103U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_CJ_OUT_OF_RANGE 104U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_AUTOCONVERSION_ENABLED 105U +#define IDIBUS_CUSTDEF_ERCODE_THERMO_CHANNEL_ONESHOT_IN_PROGRESS 106U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_3Thermo1HT_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_3Thermo1HT_THERMO_DEV = 0, // Thermocouple device ID + IDIBUS_3Thermo1HT_HEATER_DEV, // Heater device ID + IDIBUS_3Thermo1HT_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (3Thermo1HT) + IDIBUS_3Thermo1HT_Thermo_CH0_NUM = 0, // Name for 1st channel + IDIBUS_3Thermo1HT_Thermo_CH1_NUM, // 2nd ... + IDIBUS_3Thermo1HT_Thermo_CH2_NUM, // + IDIBUS_3Thermo1HT_Thermo_CH_COUNT // Module channel count + }; + +class IdiBus_3Thermo1HT_ThermoChannel: public IdiBusChannel { + public: + IdiBus_3Thermo1HT_ThermoChannel(void); + int32_t Temperature; // Last temperature + int16_t CJTemperature; // Last temperature +}; + +class IdiBus_3Thermo1HT_ThermoDevice : public IdiBusDevice { + public: + IdiBus_3Thermo1HT_ThermoDevice (void *ParentModule) : IdiBusDevice (ParentModule) {} + + uint8_t startOneShotConversion(uint8_t ChNum); + uint8_t changeAutomatiocConversionState(uint8_t ChNum); + uint8_t getTemperature(uint8_t ChNum); + uint8_t getCJTemperature(uint8_t ChNum); + + void startAllOneShotConversions(); + void changeAllAutomatiocConversionStates(); + void getAllChTemperature(void); + void getAllChCJTemperature(void); + + + IdiBus_3Thermo1HT_ThermoChannel getChData(uint8_t ChNum); + + protected : + IdiBus_3Thermo1HT_ThermoChannel CH[IDIBUS_3Thermo1HT_Thermo_CH_COUNT]; + virtual IdiBusChannel *getChannel(uint8_t Num); + + void setAllChErr(uint8_t ErrCode); +}; + +class IdiBus_3Thermo1HT : public IdiBusSlave { + public: + IdiBus_3Thermo1HT (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress), THERMO_DEV(this)/*, INPV_DEV(this)*/ {} + IdiBus_3Thermo1HT() : IdiBusSlave(), THERMO_DEV(this)/*, INPV_DEV(this)*/ {} + + virtual IdiBusDevice *getDevice(uint8_t Num); + + IdiBus_3Thermo1HT_ThermoDevice THERMO_DEV; +}; + +#endif /* IDIBUS_43Thermo1HT_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_4ADC.cpp b/libraries/idiBusMaster/src/IDIBUS_4ADC.cpp new file mode 100644 index 0000000..6a97dc2 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4ADC.cpp @@ -0,0 +1,109 @@ +#include "IDIBUS_4ADC.h" + +IdiBus_4ADC::IdiBus_4ADC (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Voltage = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_4ADC::IdiBus_4ADC () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Voltage = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_4ADC_DATA_TYPE IdiBus_4ADC::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_4ADC_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_4ADC_CH4_NUM]; } +} + +uint8_t IdiBus_4ADC::getChVoltage(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_4ADC_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4ADC_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x02; //Get Value + uint8_t ChDataSize = 2; //uint16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Voltage = (uint16_t) ( ((uint16_t)MMES.RxData[1]<<8) | + ((uint16_t)MMES.RxData[2])); + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_4ADC::getAllAdcVoltage(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4ADC_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; + uint8_t ChDataSize = 2; //uint16_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4ADC_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4ADC_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4ADC_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4ADC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Voltage = (uint16_t) ( + (uint16_t)((uint16_t)MMES.RxData[DataPos] << 8) | + (uint16_t)((uint16_t)MMES.RxData[DataPos+1]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +void IdiBus_4ADC::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +void IdiBus_4ADC::initAllChs(void) +{ + //uint8_t TxData[2]; + //TxData[0] = (uint8_t)(CurrentLimit >> 8); + //TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4ADC_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4ADC_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4ADC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + } + } +} + diff --git a/libraries/idiBusMaster/src/IDIBUS_4ADC.h b/libraries/idiBusMaster/src/IDIBUS_4ADC.h new file mode 100644 index 0000000..6b99e61 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4ADC.h @@ -0,0 +1,46 @@ +#ifndef IDIBUS_4ADC_H_ +#define IDIBUS_4ADC_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_ADC_NOT_INITED 100U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_4ADC_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_4ADC_CNTRL_DEV = 0, // ADC device ID + IDIBUS_4ADC_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (ADC) + IDIBUS_4ADC_CH1_NUM = 0, // Name for 1st channel + IDIBUS_4ADC_CH2_NUM, // 2nd ... + IDIBUS_4ADC_CH3_NUM, // + IDIBUS_4ADC_CH4_NUM, // + IDIBUS_4ADC_12V_NUM, // Name for 12v adc channel + IDIBUS_4ADC_24V_NUM, // Name for 24v adc channel + IDIBUS_4ADC_CH_COUNT, // Channel count + }; + +typedef struct { + uint16_t Voltage; // Last voltage + uint8_t MMES_Error; // Last error +} IDIBUS_4ADC_DATA_TYPE; // Data structure for ADC channels + +class IdiBus_4ADC : public IdiBusSlave { // ADC module class + public: // Public methods + IdiBus_4ADC ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_4ADC(); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t getChVoltage(uint8_t ChNum); // Sends MMES and gets Ch voltage (writes to CH structure and return error code) + void getAllAdcVoltage(void); // Sends MMES and gets all Ch voltages (writes to CH structure) + + void initAllChs(void); // Inits all adc channels on module + + IDIBUS_4ADC_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure for ADC channel + + protected : // 'Private' methods and structures + IDIBUS_4ADC_DATA_TYPE CH[IDIBUS_4ADC_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; + +#endif /* IDIBUS_4ADC_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_4CNT.cpp b/libraries/idiBusMaster/src/IDIBUS_4CNT.cpp new file mode 100644 index 0000000..8b6d8c7 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4CNT.cpp @@ -0,0 +1,131 @@ +#include "IDIBUS_4CNT.h" + +IdiBus_4CNT::IdiBus_4CNT (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Counter = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_4CNT::IdiBus_4CNT () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Counter = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_4CNT_DATA_TYPE IdiBus_4CNT::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_4CNT_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_4CNT_CH4_NUM]; } +} + +uint8_t IdiBus_4CNT::getChCounts(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_4CNT_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNT_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x02; //Get Value + uint8_t ChDataSize = 4; //uint32_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Counter = (uint32_t) ( + ((uint32_t)MMES.RxData[1] << 24) | + ((uint32_t)MMES.RxData[2] << 16) | + ((uint32_t)MMES.RxData[3] << 8 ) | + ((uint32_t)MMES.RxData[4]) ); + //this->CH[ChNum].Counter = tmp; + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_4CNT::getAllChsCounts(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNT_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; + uint8_t ChDataSize = 4; //uint32_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4CNT_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4CNT_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4CNT_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4CNT_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { + this->CH[I].Counter = (uint32_t) ( + ((uint32_t)MMES.RxData[DataPos] << 24) | + ((uint32_t)MMES.RxData[DataPos+1] << 16) | + ((uint32_t)MMES.RxData[DataPos+2] << 8 ) | + ((uint32_t)MMES.RxData[DataPos+3]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} + +void IdiBus_4CNT::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +uint8_t IdiBus_4CNT::initCh(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_4CNT_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNT_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_4CNT::initAllChs(void) +{ + //uint8_t TxData[2]; + //TxData[0] = (uint8_t)(CurrentLimit >> 8); + //TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNT_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4CNT_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4CNT_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + } + } +} + diff --git a/libraries/idiBusMaster/src/IDIBUS_4CNT.h b/libraries/idiBusMaster/src/IDIBUS_4CNT.h new file mode 100644 index 0000000..277dd22 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4CNT.h @@ -0,0 +1,44 @@ +#ifndef IDIBUS_4CNT_H_ +#define IDIBUS_4CNT_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_COUNTER_CHANNEL_NOT_INITED 100U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_4CNT_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_4CNT_CNTRL_DEV = 0, // COUNTER device ID + IDIBUS_4CNT_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (COUNTER) + IDIBUS_4CNT_CH1_NUM = 0, // Name for 1st channel + IDIBUS_4CNT_CH2_NUM, // 2nd ... + IDIBUS_4CNT_CH3_NUM, // + IDIBUS_4CNT_CH4_NUM, // + IDIBUS_4CNT_CH_COUNT, // Channel count + }; + +typedef struct { + uint32_t Counter; // Last returned value + uint8_t MMES_Error; // Last error +} IDIBUS_4CNT_DATA_TYPE; // Data structure for COUNTER channels + +class IdiBus_4CNT : public IdiBusSlave { // COUNTER module class + public: // Public methods + IdiBus_4CNT ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_4CNT(); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t getChCounts(uint8_t ChNum); // Sends MMES and get Counts on ch (writes to CH structure and returns MMES error code) + void getAllChsCounts(void); // Sends MMES and get Counts on ch (writes to CH structure) + + uint8_t initCh(uint8_t ChNum); // Inits specified counter channel on module + void initAllChs(void); // Inits all counters on module + + IDIBUS_4CNT_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure + + protected: // 'Private' methods and structures + IDIBUS_4CNT_DATA_TYPE CH[IDIBUS_4CNT_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; +#endif diff --git a/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.cpp b/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.cpp new file mode 100644 index 0000000..c6fa476 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.cpp @@ -0,0 +1,158 @@ +#include "IDIBUS_4CNTtoPWM.h" + +IdiBus_4CNTtoPWM::IdiBus_4CNTtoPWM (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].PWM = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_4CNTtoPWM::IdiBus_4CNTtoPWM () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].PWM = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_4CNTtoPWM_DATA_TYPE IdiBus_4CNTtoPWM::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_4CNTtoPWM_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_4CNTtoPWM_CH4_NUM]; } +} + +uint8_t IdiBus_4CNTtoPWM::setChPwm(uint8_t ChNum, uint8_t pwm) +{ + if (ChNum >= IDIBUS_4CNTtoPWM_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + + uint8_t TxData[1]; + TxData[0] = pwm; + + IdiBusMMES MMES(this->Address, &TxData[0], 1); + MMES.DEV.NUM = IDIBUS_4CNTtoPWM_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x02; //Set Value + + uint8_t RequestError = this->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].PWM = pwm; + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_4CNTtoPWM::setAllChsPwm(void) +{ + uint8_t TxData[IDIBUS_4CNTtoPWM_CH_COUNT]; + + for (uint8_t I=0; ICH[I].PWM; + } + + IdiBusMMES MMES(this->Address, &TxData[0], IDIBUS_4CNTtoPWM_CH_COUNT); + MMES.DEV.NUM = IDIBUS_4CNTtoPWM_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4CNTtoPWM_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4CNTtoPWM_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].TargetVoltage = TargetV[I]; } + } + } +} + +/* +void IdiBus_4CNTtoPWM::setAllChsCounts(void) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNTtoPWM_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x02; + uint8_t ChDataSize = 4; //uint32_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4CNTtoPWM_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4CNTtoPWM_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4CNTtoPWM_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4CNTtoPWM_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { + this->CH[I].Counter = (uint32_t) ( + ((uint32_t)MMES.RxData[DataPos] << 24) | + ((uint32_t)MMES.RxData[DataPos+1] << 16) | + ((uint32_t)MMES.RxData[DataPos+2] << 8 ) | + ((uint32_t)MMES.RxData[DataPos+3]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} +*/ + +void IdiBus_4CNTtoPWM::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +uint8_t IdiBus_4CNTtoPWM::initCh(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_4CNTtoPWM_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNTtoPWM_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_4CNTtoPWM::initAllChs(void) +{ + //uint8_t TxData[2]; + //TxData[0] = (uint8_t)(CurrentLimit >> 8); + //TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_4CNTtoPWM_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; //init + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4CNTtoPWM_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4CNTtoPWM_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + } + } +} + diff --git a/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.h b/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.h new file mode 100644 index 0000000..cac96a3 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4CNTtoPWM.h @@ -0,0 +1,46 @@ +#ifndef IDIBUS_4CNTtoPWMPWM_H_ +#define IDIBUS_4CNTtoPWMPWM_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_COUNTER_CHANNEL_NOT_INITED 100U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_4CNTtoPWM_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_4CNTtoPWM_CNTRL_DEV = 0, // COUNTER device ID + IDIBUS_4CNTtoPWM_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (COUNTER) + IDIBUS_4CNTtoPWM_CH1_NUM = 0, // Name for 1st channel + IDIBUS_4CNTtoPWM_CH2_NUM, // 2nd ... + IDIBUS_4CNTtoPWM_CH3_NUM, // + IDIBUS_4CNTtoPWM_CH4_NUM, // + IDIBUS_4CNTtoPWM_CH_COUNT, // Channel count + }; + +typedef struct { + uint8_t PWM; // Last returned value + uint8_t MMES_Error; // Last error +} IDIBUS_4CNTtoPWM_DATA_TYPE; // Data structure for COUNTER channels + +class IdiBus_4CNTtoPWM : public IdiBusSlave { // COUNTER module class + public: // Public methods + IdiBus_4CNTtoPWM ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_4CNTtoPWM(); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t setChPwm(uint8_t ChNum, uint8_t pwm); // Sends MMES and get Counts on ch (writes to CH structure and returns MMES error code) + void setAllChsPwm(void); // Sends MMES and get Counts on ch (writes to CH structure) + + void setChPwmLocal(uint8_t ChNum, uint8_t pwm); + + uint8_t initCh(uint8_t ChNum); // Inits specified counter channel on module + void initAllChs(void); // Inits all counters on module + + IDIBUS_4CNTtoPWM_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure + + protected: // 'Private' methods and structures + IDIBUS_4CNTtoPWM_DATA_TYPE CH[IDIBUS_4CNTtoPWM_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; +#endif diff --git a/libraries/idiBusMaster/src/IDIBUS_4DC.cpp b/libraries/idiBusMaster/src/IDIBUS_4DC.cpp new file mode 100644 index 0000000..83518fd --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4DC.cpp @@ -0,0 +1,663 @@ +//############################################################################################################################################################################################################# +#include "IDIBUS_4DC.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusDevice *IdiBus_4DC::getDevice(uint8_t Num) +{ + switch (Num) + { + case ( IDIBUS_PSU_CNTRL_DEV ) : { return &this->PSUC_DEV; } + case ( IDIBUS_INPV_CNTRL_DEV ) : { return &this->INPV_DEV; } + default : { return NULL; } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +IdiBus_4DC_PSUChannel::IdiBus_4DC_PSUChannel(void) : IdiBusChannel() +{ + this->TargetVoltage = 0; + this->CurrentLimit = 0; + this->Voltage = 0; + this->Current = 0; + this->STATUS.Enable = 0; + this->STATUS.PowerGood = 0; + this->STATUS.ERRORS.Sum = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusChannel *IdiBus_4DC_PSUCDevice::getChannel(uint8_t Num) +{ + if ( Num < IDIBUS_4DC_CH_COUNT ) { return &this->CH[Num]; } + else { return NULL; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBus_4DC_PSUChannel IdiBus_4DC_PSUCDevice::getChData(uint8_t ChNum) +{ + if ( ChNum < IDIBUS_4DC_CH_COUNT ) { return this->CH[ChNum]; } + else { IdiBus_4DC_PSUChannel NewData; return NewData; } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +IdiBus_4DC_INPVChannel::IdiBus_4DC_INPVChannel(void) : IdiBusChannel() +{ + this->InpVoltage = 0; + this->STATUS.Sum = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusChannel *IdiBus_4DC_INPVCDevice::getChannel(uint8_t Num) +{ + if ( Num == 0 ) { return &this->INPV; } + else { return NULL; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBus_4DC_INPVChannel IdiBus_4DC_INPVCDevice::getChData(void) +{ + return this->INPV; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_INPVCDevice::getInputVoltage(void) +{ + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_INPV_CNTRL_DEV; + MMES.CHNL.NUM = 0; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_GET_INP_VOLTAGE; + uint8_t ChDataSize = IDIBUS_CUSTDEF_PSUINPV_MES_LENGTH; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->INPV.MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->INPV.MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->INPV.MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } +else { + this->INPV.MMES_Error = MMES.RxData[0]; + if ( this->INPV.MMES_Error == IDIER_NOPE ) + { + uint8_t *Data = &MMES.RxData[1]; + if ( (Data[IDIBUS_CUSTDEF_PSUINPV_STATE_Pos] & (1U << IDIBUS_CUSTDEF_PSUINPV_STATE_UV_Pos)) != 0 ) { this->INPV.STATUS.BF.Undervoltage = 1; } + else { this->INPV.STATUS.BF.Undervoltage = 0; } + if ( (Data[IDIBUS_CUSTDEF_PSUINPV_STATE_Pos] & (1U << IDIBUS_CUSTDEF_PSUINPV_STATE_OV_Pos)) != 0 ) { this->INPV.STATUS.BF.Overvoltage = 1; } + else { this->INPV.STATUS.BF.Overvoltage = 0; } + this->INPV.InpVoltage = (uint16_t)( (uint16_t)Data[IDIBUS_CUSTDEF_PSUINPV_VOLTAGE_Pos] << 8 ) | MMES.RxData[IDIBUS_CUSTDEF_PSUINPV_VOLTAGE_Pos + 1]; + } + } + return this->INPV.MMES_Error; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::getChOutputVoltage(uint8_t ChNum) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_VOLTAGE; + uint8_t ChDataSize = 4; //int32_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Voltage = (int32_t) ( + (uint32_t)((uint32_t)MMES.RxData[1] << 24) | + (uint32_t)((uint32_t)MMES.RxData[2] << 16) | + (uint32_t)((uint32_t)MMES.RxData[3] << 8) | + (uint32_t)((uint32_t)MMES.RxData[4]) ); + } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::getAllChOutputVoltage(void) +{ + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_VOLTAGE; + uint8_t ChDataSize = 4; //int32_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4DC_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4DC_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Voltage = (int32_t) ( + (uint32_t)((uint32_t)MMES.RxData[DataPos] << 24) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+1] << 16) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+2] << 8) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+3]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::getChOutputCurrent(uint8_t ChNum) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_CURRENT; + uint8_t ChDataSize = 4; //int32_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Current = (int32_t) ( + (uint32_t)((uint32_t)MMES.RxData[1] << 24) | + (uint32_t)((uint32_t)MMES.RxData[2] << 16) | + (uint32_t)((uint32_t)MMES.RxData[3] << 8) | + (uint32_t)((uint32_t)MMES.RxData[4]) ); + } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::getAllChOutputCurrent(void) +{ + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_CURRENT; + uint8_t ChDataSize = 4; //int32_t + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4DC_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4DC_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->CH[I].Current = (int32_t) ( + (uint32_t)((uint32_t)MMES.RxData[DataPos] << 24) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+1] << 16) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+2] << 8) | + (uint32_t)((uint32_t)MMES.RxData[DataPos+3]) ); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::getChStatus(uint8_t ChNum) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_STATUS; + uint8_t ChDataSize = IDIBUS_CUSTDEF_PSUCH_STBUF_SIZE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->parsePsuChState(&MMES.RxData[1], ChNum); } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::getAllChStatus(void) +{ + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_STATUS; + uint8_t ChDataSize = IDIBUS_CUSTDEF_PSUCH_STBUF_SIZE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4DC_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4DC_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->parsePsuChState(&MMES.RxData[DataPos], I); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::getChAllData(uint8_t ChNum) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_ALLDATA; + uint8_t ChDataSize = IDIBUS_CUSTDEF_PSUCH_STALLBUF_SIZE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->parsePsuChAllState(&MMES.RxData[1], ChNum); } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::getAllChAllData(void) +{ + IdiBusMMES MMES( static_cast(this->Module)->getAddress() ); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_ALLDATA; + uint8_t ChDataSize = IDIBUS_CUSTDEF_PSUCH_STALLBUF_SIZE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); return; } + else if (MMES.RxDataLength < IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + else { + uint8_t ExpDataLength = IDIBUS_4DC_CH_COUNT; // Errors + for (uint8_t I=0; IsetAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); return; } + } + uint8_t ErrorPos = 0; + uint8_t DataPos = ErrorPos + IDIBUS_4DC_CH_COUNT; + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[ErrorPos]; + ErrorPos = (uint8_t)(ErrorPos + 1); + if ( this->CH[I].MMES_Error == IDIER_NOPE ) + { + this->parsePsuChAllState(&MMES.RxData[DataPos], I); + DataPos = (uint8_t)(DataPos + ChDataSize); + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::setChState(uint8_t ChNum, uint8_t State) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + if ( State != 0 ) { State = IDIBUS_CUSTDEF_PSU_CH_STATE_ENABLE; } + else { State = IDIBUS_CUSTDEF_PSU_CH_STATE_DISABLE; } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &State, 1); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_STATE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->CH[ChNum].STATUS.Enable = State; } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChState(uint8_t State) +{ + if ( State != 0 ) { State = IDIBUS_CUSTDEF_PSU_CH_STATE_ENABLE; } + else { State = IDIBUS_CUSTDEF_PSU_CH_STATE_DISABLE; } + + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &State, 1); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.CHNL.ALLSAME = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_STATE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].STATUS.Enable = State; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChState(uint8_t Ch1State, uint8_t Ch2State, uint8_t Ch3State, uint8_t Ch4State) +{ + uint8_t State[IDIBUS_4DC_CH_COUNT] = { Ch1State, Ch2State, Ch3State, Ch4State }; + for (uint8_t I=0; I(this->Module)->getAddress() , &State[0], IDIBUS_4DC_CH_COUNT); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_STATE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].STATUS.Enable = State[I]; } + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::setChVoltage(uint8_t ChNum, uint16_t TargetVoltage) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + if ( (TargetVoltage < IDIBUS_PSU_CH_TARG_V_MIN_mV) || (TargetVoltage > IDIBUS_PSU_CH_TARG_V_MAX_mV) ) + { + this->CH[ChNum].MMES_Error = IDIERMST_INVALID_TX_PARAM; + return IDIERMST_INVALID_TX_PARAM; + } + uint8_t TxData[2]; + TxData[0] = (uint8_t)(TargetVoltage >> 8); + TxData[1] = (uint8_t)(TargetVoltage); + + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_TARGET_VOLTAGE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->CH[ChNum].TargetVoltage = TargetVoltage; } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChVoltage(uint16_t TargetVoltage) +{ + if ( (TargetVoltage < IDIBUS_PSU_CH_TARG_V_MIN_mV) || (TargetVoltage > IDIBUS_PSU_CH_TARG_V_MAX_mV) ) { this->setAllPsuChErr(IDIERMST_INVALID_TX_PARAM); return; } + uint8_t TxData[2]; + TxData[0] = (uint8_t)(TargetVoltage >> 8); + TxData[1] = (uint8_t)(TargetVoltage); + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.CHNL.ALLSAME = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_TARGET_VOLTAGE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].TargetVoltage = TargetVoltage; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChVoltage(uint16_t TV_CH1, uint16_t TV_CH2, uint16_t TV_CH3, uint16_t TV_CH4) +{ + uint16_t TargetV[IDIBUS_4DC_CH_COUNT] = { TV_CH1, TV_CH2, TV_CH3, TV_CH4 }; + uint8_t TxData[IDIBUS_4DC_CH_COUNT * 2]; + for (uint8_t I=0, J=0; I> 8); + TxData[J+1] = (uint8_t)(TargetV[I]); + } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], IDIBUS_4DC_CH_COUNT * 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_TARGET_VOLTAGE; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].TargetVoltage = TargetV[I]; } + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::setChCurrentLimit(uint8_t ChNum, uint16_t CurrentLimit) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + if ( (CurrentLimit < IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MIN) || (CurrentLimit > IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MAX) ) + { + this->CH[ChNum].MMES_Error = IDIERMST_INVALID_TX_PARAM; + return IDIERMST_INVALID_TX_PARAM; + } + uint8_t TxData[2]; + TxData[0] = (uint8_t)(CurrentLimit >> 8); + TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_CURRENT_LIMIT; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->CH[ChNum].CurrentLimit = CurrentLimit; } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChCurrentLimit(uint16_t CurrentLimit) +{ + if ( (CurrentLimit < IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MIN) || (CurrentLimit > IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MAX) ) { this->setAllPsuChErr(IDIERMST_INVALID_TX_PARAM); return; } + uint8_t TxData[2]; + TxData[0] = (uint8_t)(CurrentLimit >> 8); + TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.CHNL.ALLSAME = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_CURRENT_LIMIT; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].CurrentLimit = CurrentLimit; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChCurrentLimit(uint16_t CL_CH1, uint16_t CL_CH2, uint16_t CL_CH3, uint16_t CL_CH4) +{ + uint16_t CurrentL[IDIBUS_4DC_CH_COUNT] = { CL_CH1, CL_CH2, CL_CH3, CL_CH4 }; + uint8_t TxData[IDIBUS_4DC_CH_COUNT * 2]; + for (uint8_t I=0, J=0; I> 8); + TxData[J+1] = (uint8_t)(CurrentL[I]); + } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], IDIBUS_4DC_CH_COUNT * 2); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_CURRENT_LIMIT; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].CurrentLimit = CurrentL[I]; } + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBus_4DC_PSUCDevice::setChVoltageAndCurrentLimit(uint8_t ChNum, uint16_t TargetVoltage, uint16_t CurrentLimit) +{ + if ( ChNum >= IDIBUS_4DC_CH_COUNT ) { return IDIERMST_INVALID_CHN_NUM; } + if ( (CurrentLimit < IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MIN) || (CurrentLimit > IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MAX) || + (TargetVoltage < IDIBUS_PSU_CH_TARG_V_MIN_mV) || (TargetVoltage > IDIBUS_PSU_CH_TARG_V_MAX_mV) ) + { + this->CH[ChNum].MMES_Error = IDIERMST_INVALID_TX_PARAM; + return IDIERMST_INVALID_TX_PARAM; + } + uint8_t TxData[4]; + TxData[0] = (uint8_t)(TargetVoltage >> 8); + TxData[1] = (uint8_t)(TargetVoltage); + TxData[2] = (uint8_t)(CurrentLimit >> 8); + TxData[3] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 4); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_ALL; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) { this->CH[ChNum].TargetVoltage = TargetVoltage; this->CH[ChNum].CurrentLimit = CurrentLimit; } + } + return this->CH[ChNum].MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChVoltageAndCurrentLimit(uint16_t TargetVoltage, uint16_t CurrentLimit) +{ + if ( (CurrentLimit < IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MIN) || (CurrentLimit > IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MAX) || + (TargetVoltage < IDIBUS_PSU_CH_TARG_V_MIN_mV) || (TargetVoltage > IDIBUS_PSU_CH_TARG_V_MAX_mV) ) { this->setAllPsuChErr(IDIERMST_INVALID_TX_PARAM); return; } + uint8_t TxData[4]; + TxData[0] = (uint8_t)(TargetVoltage >> 8); + TxData[1] = (uint8_t)(TargetVoltage); + TxData[2] = (uint8_t)(CurrentLimit >> 8); + TxData[3] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], 4); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.CHNL.ALLSAME = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_ALL; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].TargetVoltage = TargetVoltage; this->CH[I].CurrentLimit = CurrentLimit; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllChVoltageAndCurrentLimit(uint16_t VCH1, uint16_t CCH1, uint16_t VCH2, uint16_t CCH2, uint16_t VCH3, uint16_t CCH3, uint16_t VCH4, uint16_t CCH4) +{ + uint16_t CurrentL[IDIBUS_4DC_CH_COUNT] = { CCH1, CCH2, CCH3, CCH4 }; + uint16_t TargetV[IDIBUS_4DC_CH_COUNT] = { VCH1, VCH2, VCH3, VCH4 }; + uint8_t TxData[IDIBUS_4DC_CH_COUNT * 4]; + for (uint8_t I=0, J=0; I> 8); + TxData[J+1] = (uint8_t)(TargetV[I]); + TxData[J+2] = (uint8_t)(CurrentL[I] >> 8); + TxData[J+3] = (uint8_t)(CurrentL[I]); + } + IdiBusMMES MMES( static_cast(this->Module)->getAddress() , &TxData[0], IDIBUS_4DC_CH_COUNT * 4); + MMES.DEV.NUM = IDIBUS_PSU_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_ALL; + uint8_t RequestError = static_cast(this->Module)->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllPsuChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_4DC_CH_COUNT) { this->setAllPsuChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_4DC_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + if ( this->CH[I].MMES_Error == IDIER_NOPE ) { this->CH[I].TargetVoltage = TargetV[I]; this->CH[I].CurrentLimit = CurrentL[I]; } + } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +void IdiBus_4DC_PSUCDevice::parsePsuChState(uint8_t *State, uint8_t ChNum) +{ + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_EN_Pos)) != 0 ) { this->CH[ChNum].STATUS.Enable = 1; } + else { this->CH[ChNum].STATUS.Enable = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_PG_Pos)) != 0 ) { this->CH[ChNum].STATUS.PowerGood = 1; } + else { this->CH[ChNum].STATUS.PowerGood = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_PERIPH_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.PeripheryError = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.PeripheryError = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OVERCURRENT_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.OvercurrentError = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.OvercurrentError = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OVERVOLTAGE_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.OvervoltageError = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.OvervoltageError = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OFFSTATELEAKAGE_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.OffStateCurrentLeakage = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.OffStateCurrentLeakage = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_MEMORY_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.MemoryError = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.MemoryError = 0; } + if ( (State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos] & (1U << IDIBUS_CUSTDEF_PSUCH_STBUF_ST_INPV_ERR_Pos)) != 0 ) { this->CH[ChNum].STATUS.ERRORS.BF.InpVoltageError = 1; } + else { this->CH[ChNum].STATUS.ERRORS.BF.InpVoltageError = 0; } + this->CH[ChNum].TargetVoltage = (uint16_t)( (uint16_t)((uint16_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_TARGET_VOLATGE_Pos] << 8) | State[IDIBUS_CUSTDEF_PSUCH_STBUF_TARGET_VOLATGE_Pos + 1] ); + this->CH[ChNum].CurrentLimit = (uint16_t)( (uint16_t)((uint16_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_CURRENT_LIMIT_Pos] << 8) | State[IDIBUS_CUSTDEF_PSUCH_STBUF_CURRENT_LIMIT_Pos + 1] ); +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::parsePsuChAllState(uint8_t *State, uint8_t ChNum) +{ + this->parsePsuChState(&State[IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos], ChNum); + this->CH[ChNum].Voltage = (int32_t) ( + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos] << 24) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos + 1] << 16) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos + 2] << 8) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos + 3]) ); + this->CH[ChNum].Current = (int32_t) ( + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos] << 24) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos + 1] << 16) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos + 2] << 8) | + (uint32_t)((uint32_t)State[IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos + 3]) ); +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBus_4DC_PSUCDevice::setAllPsuChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# + diff --git a/libraries/idiBusMaster/src/IDIBUS_4DC.h b/libraries/idiBusMaster/src/IDIBUS_4DC.h new file mode 100644 index 0000000..5806b2a --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_4DC.h @@ -0,0 +1,178 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_4DC_H_ +#define _IDIBUS_4DC_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSlave.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_PSU_CH_STATE_DISABLE 0x00 +#define IDIBUS_CUSTDEF_PSU_CH_STATE_ENABLE 0x01 + +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_STATE 0x01 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_VOLTAGE 0x02 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_CURRENT 0x03 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_STATUS 0x04 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_GET_ALLDATA 0x05 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_TARGET_VOLTAGE 0x06 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_CURRENT_LIMIT 0x07 +#define IDIBUS_CUSTDEF_FUNC_CODE_PSU_CH_SET_ALL 0x08 +#define IDIBUS_CUSTDEF_FUNC_CODE_GET_INP_VOLTAGE 0x09 +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_INVALID_DATA 100U +#define IDIBUS_CUSTDEF_ERCODE_NO_ACCESS 101U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_PSUCH_GET_CV_MES_LENGTH 4U + +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_Pos 00U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_EN_Pos 0U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_PG_Pos 1U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_PERIPH_ERR_Pos 2U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OVERCURRENT_ERR_Pos 3U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OVERVOLTAGE_ERR_Pos 4U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_OFFSTATELEAKAGE_ERR_Pos 5U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_MEMORY_ERR_Pos 6U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_ST_INPV_ERR_Pos 7U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_TARGET_VOLATGE_Pos 01U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_CURRENT_LIMIT_Pos 03U +#define IDIBUS_CUSTDEF_PSUCH_STBUF_SIZE 05U + +#define IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos IDIBUS_CUSTDEF_PSUCH_STBUF_SIZE +#define IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos (IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_VOLTAGE_Pos + 4) +#define IDIBUS_CUSTDEF_PSUCH_STALLBUF_SIZE (IDIBUS_CUSTDEF_PSUCH_STBUF_OUTPUT_CURRENT_Pos + 4) +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_PSUINPV_STATE_Pos 00U +#define IDIBUS_CUSTDEF_PSUINPV_STATE_UV_Pos 0U +#define IDIBUS_CUSTDEF_PSUINPV_STATE_OV_Pos 1U +#define IDIBUS_CUSTDEF_PSUINPV_VOLTAGE_Pos 01U +#define IDIBUS_CUSTDEF_PSUINPV_MES_LENGTH (IDIBUS_CUSTDEF_PSUINPV_VOLTAGE_Pos + 2) +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_PSU_CH_TARG_V_MIN_mV 1500UL +#define IDIBUS_PSU_CH_TARG_V_MAX_mV 48000UL +#define IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MAX 10200UL +#define IDIBUS_PSU_CH_AVERAGE_CURRENT_LIMIT_MIN 100UL +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define PSU_INV_UNDERVOLTAGE_ERR_THR 10000U +#define PSU_INV_OVERVOLTAGE_ERR_THR 50000U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_MODULE_DEVICES_LIST { + IDIBUS_PSU_CNTRL_DEV = 0x00, + IDIBUS_INPV_CNTRL_DEV, + IDIBUS_DEVICES_NUMBER +}; +enum { + IDIBUS_4DC_CH1_NUM = 0, + IDIBUS_4DC_CH2_NUM, + IDIBUS_4DC_CH3_NUM, + IDIBUS_4DC_CH4_NUM, + IDIBUS_4DC_CH_COUNT + }; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +class IdiBus_4DC_PSUChannel : public IdiBusChannel { + public: + IdiBus_4DC_PSUChannel(void); + int32_t Voltage; + int32_t Current; + uint16_t TargetVoltage; + uint16_t CurrentLimit; + struct { + uint8_t Enable; + uint8_t PowerGood; + union { + uint8_t Sum; + struct { + uint8_t PeripheryError : 1; + uint8_t OvercurrentError : 1; + uint8_t OvervoltageError : 1; + uint8_t OffStateCurrentLeakage : 1; + uint8_t MemoryError : 1; + uint8_t InpVoltageError : 1; + uint8_t Reserved : 2; + } BF; + } ERRORS; + } STATUS; +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBus_4DC_PSUCDevice : public IdiBusDevice { + public : + IdiBus_4DC_PSUCDevice(void *ParentModule) : IdiBusDevice (ParentModule) {} + + uint8_t getChStatus(uint8_t ChNum); + void getAllChStatus(void); + + uint8_t getChOutputVoltage(uint8_t ChNum); + void getAllChOutputVoltage(void); + + uint8_t getChOutputCurrent(uint8_t ChNum); + void getAllChOutputCurrent(void); + + uint8_t getChAllData(uint8_t ChNum); + void getAllChAllData(void); + + uint8_t setChState(uint8_t ChNum, uint8_t State); + void setAllChState(uint8_t State); + void setAllChState(uint8_t Ch1State, uint8_t Ch2State, uint8_t Ch3State, uint8_t Ch4State); + + uint8_t setChVoltage(uint8_t ChNum, uint16_t TargetVoltage); + void setAllChVoltage(uint16_t TargetVoltage); + void setAllChVoltage(uint16_t TV_CH1, uint16_t TV_CH2, uint16_t TV_CH3, uint16_t TV_CH4); + + uint8_t setChCurrentLimit(uint8_t ChNum, uint16_t CurrentLimit); + void setAllChCurrentLimit(uint16_t CurrentLimit); + void setAllChCurrentLimit(uint16_t CL_CH1, uint16_t CL_CH2, uint16_t CL_CH3, uint16_t CL_CH4); + + uint8_t setChVoltageAndCurrentLimit(uint8_t ChNum, uint16_t TargetVoltage, uint16_t CurrentLimit); + void setAllChVoltageAndCurrentLimit(uint16_t TargetVoltage, uint16_t CurrentLimit); + void setAllChVoltageAndCurrentLimit(uint16_t VCH1, uint16_t CCH1, uint16_t VCH2, uint16_t CCH2, uint16_t VCH3, uint16_t CCH3, uint16_t VCH4, uint16_t CCH4); + + IdiBus_4DC_PSUChannel getChData(uint8_t ChNum); + protected : + IdiBus_4DC_PSUChannel CH[IDIBUS_4DC_CH_COUNT]; + virtual IdiBusChannel *getChannel(uint8_t Num); + void parsePsuChState(uint8_t *State, uint8_t ChNum); + void parsePsuChAllState(uint8_t *State, uint8_t ChNum); + void setAllPsuChErr(uint8_t ErrCode); + +}; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +class IdiBus_4DC_INPVChannel : public IdiBusChannel { + public: + IdiBus_4DC_INPVChannel(void); + uint16_t InpVoltage; + union { + uint8_t Sum; + struct { + uint8_t Undervoltage : 1; + uint8_t Overvoltage : 1; + uint8_t Reserved : 6; + } BF; + } STATUS; +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBus_4DC_INPVCDevice : public IdiBusDevice { + public : + IdiBus_4DC_INPVCDevice (void *ParentModule) : IdiBusDevice (ParentModule) {} + uint8_t getInputVoltage(void); + IdiBus_4DC_INPVChannel getChData(void); + protected : + IdiBus_4DC_INPVChannel INPV; + virtual IdiBusChannel *getChannel(uint8_t Num); +}; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +class IdiBus_4DC : public IdiBusSlave { + public: + IdiBus_4DC (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress), PSUC_DEV(this), INPV_DEV(this) {} + IdiBus_4DC() : IdiBusSlave(), PSUC_DEV(this), INPV_DEV(this) {} + + virtual IdiBusDevice *getDevice(uint8_t Num); + + IdiBus_4DC_PSUCDevice PSUC_DEV; + IdiBus_4DC_INPVCDevice INPV_DEV; +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_4DC_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IDIBUS_CO2.cpp b/libraries/idiBusMaster/src/IDIBUS_CO2.cpp new file mode 100644 index 0000000..a57b693 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_CO2.cpp @@ -0,0 +1,81 @@ +#include "IDIBUS_CO2.h" + +IdiBus_CO2::IdiBus_CO2 (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Value = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_CO2::IdiBus_CO2 () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].Value = 0; + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_CO2_DATA_TYPE IdiBus_CO2::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_CO2_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_CO2_CH0_NUM]; } +} + +uint8_t IdiBus_CO2::getCO2(uint8_t ChNum) +{ + if (ChNum >= IDIBUS_CO2_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_CO2_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //Get Value + uint8_t ChDataSize = 4; //uint32_t + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + this->CH[ChNum].Value = (uint32_t) ( ((uint32_t)MMES.RxData[1]<<24) | + ((uint32_t)MMES.RxData[2]<<16) | + ((uint32_t)MMES.RxData[3]<<8) | + ((uint32_t)MMES.RxData[4]) ); + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_CO2::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +/* +void IdiBus_CO2::initAllChs(void) +{ + //uint8_t TxData[2]; + //TxData[0] = (uint8_t)(CurrentLimit >> 8); + //TxData[1] = (uint8_t)(CurrentLimit); + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_CO2_CNTRL_DEV; + MMES.DEV.ALLCH = 1; + MMES.ComFunc = 0x01; + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->setAllChErr(RequestError); } + else if (MMES.RxDataLength != IDIBUS_CO2_CH_COUNT) { this->setAllChErr(IDIERMST_INVALID_RX_REQUEST_FORMAT); } + else + { + for (uint8_t I = 0; I < IDIBUS_CO2_CH_COUNT; I++) + { + this->CH[I].MMES_Error = MMES.RxData[I]; + //if ( this->CH[I].MMES_Error == IDIER_NOPE ) + } + } +} +*/ diff --git a/libraries/idiBusMaster/src/IDIBUS_CO2.h b/libraries/idiBusMaster/src/IDIBUS_CO2.h new file mode 100644 index 0000000..f3d5595 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_CO2.h @@ -0,0 +1,41 @@ +#ifndef IDIBUS_CO2_H_ +#define IDIBUS_CO2_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_CUSTDEF_ERCODE_CO2_CRC_ERROR 100U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_CO2_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_CO2_CNTRL_DEV = 0, // ADC device ID + IDIBUS_CO2_DEVICES_NUMBER // Device count on module + }; + +enum { // Describe channel configuration for device (CO2) + IDIBUS_CO2_CH0_NUM = 0, // Name for 1st channel + IDIBUS_CO2_CH_COUNT, // Module channel count + }; + +typedef struct { + uint32_t Value; // Last voltage + uint8_t MMES_Error; // Last error +} IDIBUS_CO2_DATA_TYPE; // Data structure for ADC channels + +class IdiBus_CO2 : public IdiBusSlave { // ADC module class + public: // Public methods + IdiBus_CO2 ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_CO2 (); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t getCO2(uint8_t ChNum); // Sends MMES and gets Ch voltage (writes to CH structure and return error code) + //void getAllChTemperature(void); // Sends MMES and gets all Ch voltages (writes to CH structure) + + //void initAllChs(void); // Inits all adc channels on module + + IDIBUS_CO2_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure for ADC channel + + protected : // 'Private' methods and structures + IDIBUS_CO2_DATA_TYPE CH[IDIBUS_CO2_CH_COUNT]; // Data structure array + + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; + +#endif /* IDIBUS_4CO2_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_NEXTION.cpp b/libraries/idiBusMaster/src/IDIBUS_NEXTION.cpp new file mode 100644 index 0000000..d92682c --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_NEXTION.cpp @@ -0,0 +1,52 @@ +#include "IDIBUS_NEXTION.h" + +IdiBus_NEXTION::IdiBus_NEXTION (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + BRIDGE.MMES_Error = 0; + BRIDGE.RXlen = 0; +} + +IdiBus_NEXTION::IdiBus_NEXTION () : IdiBusSlave() +{ + BRIDGE.MMES_Error = 0; + BRIDGE.RXlen = 0; +} + +IDIBUS_NEXTION_DATA_TYPE IdiBus_NEXTION::getData(void) +{ + return this->BRIDGE; +} + +uint8_t IdiBus_NEXTION::getBuf() +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_NEXTION_CNTRL_DEV; + MMES.CHNL.NUM = 0; + MMES.ComFunc = 0x02; //Get Buf + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->BRIDGE.MMES_Error = RequestError; } + else { + this->BRIDGE.MMES_Error = MMES.RxData[0]; + this->BRIDGE.RXlen = MMES.RxDataLength-1; + memcpy(this->BRIDGE.RXbuf,&MMES.RxData[1],this->BRIDGE.RXlen); + } + return this->BRIDGE.MMES_Error; +} + +uint8_t IdiBus_NEXTION::sendBuf(uint8_t *buf, uint8_t len) +{ + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_NEXTION_CNTRL_DEV; + MMES.CHNL.NUM = 0; + MMES.ComFunc = 0x01; //Send Buf + MMES.TxDataLength = len; + MMES.TxData = buf; + uint8_t RequestError = this->SendRequestMMES(&MMES); + + if ( RequestError != IDIER_NOPE ) { this->BRIDGE.MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->BRIDGE.MMES_Error = MMES.RxData[0]; } + else { + this->BRIDGE.MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; + } + return this->BRIDGE.MMES_Error; +} diff --git a/libraries/idiBusMaster/src/IDIBUS_NEXTION.h b/libraries/idiBusMaster/src/IDIBUS_NEXTION.h new file mode 100644 index 0000000..79498e3 --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_NEXTION.h @@ -0,0 +1,31 @@ +#ifndef IDIBUS_NEXTION_H_ +#define IDIBUS_NEXTION_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_NEXTION_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_NEXTION_CNTRL_DEV = 0, // NEXTION device ID + IDIBUS_NEXTION_DEVICES_NUMBER // Device count on module + }; + +typedef struct { + uint8_t RXlen; + uint8_t RXbuf[255]; + uint8_t MMES_Error; // Last error +} IDIBUS_NEXTION_DATA_TYPE; + +class IdiBus_NEXTION : public IdiBusSlave { // NEXTION module class + public: // Public methods + IdiBus_NEXTION ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_NEXTION(); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t sendBuf(uint8_t *buf, uint8_t len); // Sends MMES and gets Ch voltage (writes to CH structure and return error code) + uint8_t getBuf(); + + IDIBUS_NEXTION_DATA_TYPE getData(void); + + protected : // 'Private' methods and structures + IDIBUS_NEXTION_DATA_TYPE BRIDGE; +}; + +#endif /* IDIBUS_NEXTION_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IDIBUS_Speedster.cpp b/libraries/idiBusMaster/src/IDIBUS_Speedster.cpp new file mode 100644 index 0000000..4eafeed --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_Speedster.cpp @@ -0,0 +1,81 @@ +#include "IDIBUS_Speedster.h" + +IdiBus_Test::IdiBus_Test (IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) : IdiBusSlave(SlaveLine, SlaveAddress) +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].state = 0x1040FF0A; //Start seed + for (uint16_t i = 0; i < 256; i++){ + this->CH[I].RNG[i] = 0; + this->CH[I].DATA[i] = 0; + } + //this->CH[I].STATUS.Enable = 0; + } +} + +IdiBus_Test::IdiBus_Test () : IdiBusSlave() +{ + for (uint8_t I=0; ICH[I].MMES_Error = 0; + this->CH[I].state = 0x1040FF0A; //Start seed + for (uint16_t i = 0; i < 256; i++){ + this->CH[I].RNG[i] = 0; + this->CH[I].DATA[i] = 0; + } + //this->CH[I].STATUS.Enable = 0; + } +} + +IDIBUS_Test_DATA_TYPE IdiBus_Test::getChData(uint8_t ChNum){ + if ( ChNum < IDIBUS_Test_CH_COUNT ) { return this->CH[ChNum]; } + else { return this->CH[IDIBUS_Test_CH_COUNT-1]; } +} + +uint8_t IdiBus_Test::sendPacket(uint8_t ChNum) +{ + //Generate random sequence + this->CH[ChNum].state = this->xorshift32(this->CH[ChNum].state,this->CH[ChNum].RNG); + + if (ChNum >= IDIBUS_Test_CH_COUNT) { return IDIERMST_INVALID_CHN_NUM; } + IdiBusMMES MMES(this->Address); + MMES.DEV.NUM = IDIBUS_Test_CNTRL_DEV; + MMES.CHNL.NUM = ChNum; + MMES.ComFunc = 0x01; //Send packet + MMES.TxDataLength = 256; + MMES.TxData = this->CH[ChNum].RNG; + uint16_t ChDataSize = 256; //uint8_t[256] + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { this->CH[ChNum].MMES_Error = RequestError; } + else if ( MMES.RxDataLength == 1 ) { this->CH[ChNum].MMES_Error = MMES.RxData[0]; } + else if ( MMES.RxDataLength != (ChDataSize + 1) ) { this->CH[ChNum].MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { + this->CH[ChNum].MMES_Error = MMES.RxData[0]; + if ( this->CH[ChNum].MMES_Error == IDIER_NOPE ) + { + for (uint16_t i = 0; i < 256; i++) + this->CH[ChNum].DATA[i] = MMES.RxData[i+1]; + } + } + return this->CH[ChNum].MMES_Error; + +} + +void IdiBus_Test::setAllChErr(uint8_t ErrCode) +{ + for (uint8_t I=0; ICH[I].MMES_Error = ErrCode; } +} + +uint32_t IdiBus_Test::xorshift32(uint32_t state,uint8_t *data) { + for (uint8_t i = 0; i < 256/4; i++){ + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + data[i*4+0] = (uint8_t)state; + data[i*4+1] = (uint8_t)(state>>8); + data[i*4+2] = (uint8_t)(state>>16); + data[i*4+3] = (uint8_t)(state>>24); + } + return state; +} diff --git a/libraries/idiBusMaster/src/IDIBUS_Speedster.h b/libraries/idiBusMaster/src/IDIBUS_Speedster.h new file mode 100644 index 0000000..6d521cb --- /dev/null +++ b/libraries/idiBusMaster/src/IDIBUS_Speedster.h @@ -0,0 +1,38 @@ +#ifndef IDIBUS_Test_H_ +#define IDIBUS_Test_H_ + +#include "IdiBusSlave.h" // Common slave defines +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_Test_CH_COUNT 1 + +enum IDIBUS_Test_MODULE_DEVICES_LIST { // Describe devices on module + IDIBUS_Test_CNTRL_DEV = 0, // ADC device ID + IDIBUS_Test_DEVICES_NUMBER // Device count on module + }; + +typedef struct { + uint8_t RNG[256]; + uint8_t DATA[256]; + uint8_t MMES_Error; // Last error + uint32_t state; +} IDIBUS_Test_DATA_TYPE; // Data structure for ADC channels + +class IdiBus_Test : public IdiBusSlave { // ADC module class + public: // Pulic methods + IdiBus_Test ( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); // Init object with specified bus line and addr + IdiBus_Test (); // init without specified line and addr (default value line=NULL,addr=1) + + uint8_t sendPacket(uint8_t ChNum); // Sends MMES and gets Ch voltage (writes to CH structure and return error code) + //void getAllChTemperature(void); // Sends MMES and gets all Ch voltages (writes to CH structure) + + //void initAllChs(void); // Inits all adc channels on module + + IDIBUS_Test_DATA_TYPE getChData(uint8_t ChNum); // Returns data structure for ADC channel + + protected : // 'Private' methods and structures + IDIBUS_Test_DATA_TYPE CH[IDIBUS_Test_CH_COUNT]; // Data structure array + uint32_t xorshift32(uint32_t state,uint8_t *data); + void setAllChErr(uint8_t ErrCode); // Set error code for all channels +}; + +#endif /* IDIBUS_41Wire_H_ */ \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IdiBusCallbackInterface.h b/libraries/idiBusMaster/src/IdiBusCallbackInterface.h new file mode 100644 index 0000000..8b31dad --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusCallbackInterface.h @@ -0,0 +1,29 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_CALLBACK_INTERFACE_H_ +#define _IDIBUS_CALLBACK_INTERFACE_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusCallbackInterface { + public: + static void USART_Callback_InterruptTX (IdiBusCallbackInterface *inst) { inst->USART_TxInterruptFunc(); } + static void USART_Callback_InterruptRX (IdiBusCallbackInterface *inst) { inst->USART_RxInterruptFunc(); } + static void USART_Callback_InterruptUDRE (IdiBusCallbackInterface *inst) { inst->USART_UdreInterruptFunc(); } + virtual void USART_RxInterruptFunc(void) {} + virtual void USART_TxInterruptFunc(void) {} + virtual void USART_UdreInterruptFunc(void) {} + + static void TIM_Callback_InterruptCOMPA (IdiBusCallbackInterface *inst) { inst->TIM_COMPA_InterruptFunc(); } + static void TIM_Callback_InterruptCOMPB (IdiBusCallbackInterface *inst) { inst->TIM_COMPB_InterruptFunc(); } + static void TIM_Callback_InterruptCOMPC (IdiBusCallbackInterface *inst) { inst->TIM_COMPC_InterruptFunc(); } + static void TIM_Callback_InterruptOVF (IdiBusCallbackInterface *inst) { inst->TIM_OVF_InterruptFunc(); } + virtual void TIM_COMPA_InterruptFunc (void) {} + virtual void TIM_COMPB_InterruptFunc (void) {} + virtual void TIM_COMPC_InterruptFunc (void) {} + virtual void TIM_OVF_InterruptFunc (void) {} + }; + +typedef void (*CallbackTypeFunction)(IdiBusCallbackInterface*); +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_CALLBACK_INTERFACE_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusDateTime.cpp b/libraries/idiBusMaster/src/IdiBusDateTime.cpp new file mode 100644 index 0000000..4685cac --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusDateTime.cpp @@ -0,0 +1,57 @@ +//############################################################################################################################################################################################################# +#include "IdiBusDateTime.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiDateFormat::IdiDateFormat(uint8_t day, uint8_t month, uint8_t century, uint8_t year99) +{ + this->Day = day; + this->Month = month; + this->Century = century; + this->Year99 = year99; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiDateFormat::isValid(void) +{ + if ( (this->Day == 0) || (this->Day > 31) || + (this->Month == 0) || (this->Month > 12) || + (this->Century > 99) || (this->Year99 > 99) ) { return 0; } +return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiDateFormat::getSize(void) { return IDIDATE_FORMAT_LENGTH; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiDateFormat::getBufForMMES(uint8_t *Data) +{ + Data[IDIDATE_FORMAT_DAY_Pos] = this->Day; + Data[IDIDATE_FORMAT_MONTH_Pos] = this->Month; + Data[IDIDATE_FORMAT_CENTURY_Pos] = this->Century; + Data[IDIDATE_FORMAT_YEAR99_Pos] = this->Year99; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +IdiTimeFormat::IdiTimeFormat(uint8_t seconds, uint8_t minutes, uint8_t hours, int8_t timezone) +{ + this->Seconds = seconds; + this->Minutes = minutes; + this->Hours = hours; + this->TimeZone = timezone; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiTimeFormat::isValid(void) +{ + if ( (this->Seconds > 60) || (this->Minutes > 60) || (this->Hours > 23) || + (this->TimeZone < IDITIME_FORMAT_TIMEZONE_MIN) || (this->TimeZone > IDITIME_FORMAT_TIMEZONE_MAX) ) { return 0; } + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiTimeFormat::getSize(void) { return IDITIME_FORMAT_LENGTH; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiTimeFormat::getBufForMMES(uint8_t *Data) +{ + Data[IDITIME_FORMAT_SECONDS_Pos] = this->Seconds; + Data[IDITIME_FORMAT_MINUTES_Pos] = this->Minutes; + Data[IDITIME_FORMAT_HOURS_Pos] = this->Hours; + Data[IDITIME_FORMAT_TIMEZONE_Pos] = (uint8_t)this->TimeZone; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusDateTime.h b/libraries/idiBusMaster/src/IdiBusDateTime.h new file mode 100644 index 0000000..0bebb1b --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusDateTime.h @@ -0,0 +1,32 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_DATE_TIME_H_ +#define _IDIBUS_DATE_TIME_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSystem.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiDateFormat { + public: + IdiDateFormat(uint8_t day, uint8_t month, uint8_t century, uint8_t year99); + uint8_t Day; + uint8_t Month; + uint8_t Century; + uint8_t Year99; + uint8_t isValid(void); + uint8_t getSize(void); + void getBufForMMES(uint8_t *Data); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiTimeFormat { + public: + IdiTimeFormat(uint8_t seconds, uint8_t minutes, uint8_t hours, int8_t timezone); + uint8_t Seconds; + uint8_t Minutes; + uint8_t Hours; + int8_t TimeZone; + uint8_t isValid(void); + uint8_t getSize(void); + void getBufForMMES(uint8_t *Data); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_DATE_TIME_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusInterruptsDisable.h b/libraries/idiBusMaster/src/IdiBusInterruptsDisable.h new file mode 100644 index 0000000..0eab7f2 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusInterruptsDisable.h @@ -0,0 +1,18 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_INTERRUPTS_DISABLE_H_ +#define _IDIBUS_INTERRUPTS_DISABLE_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_USART0_ISR_DISABLE +//#define IDIBUS_USART1_ISR_DISABLE +//#define IDIBUS_USART2_ISR_DISABLE +#define IDIBUS_USART3_ISR_DISABLE + +#define IDIBUS_TIM0_ISR_DISABLE +//#define IDIBUS_TIM1_ISR_DISABLE +//#define IDIBUS_TIM2_ISR_DISABLE +//#define IDIBUS_TIM3_ISR_DISABLE +#define IDIBUS_TIM4_ISR_DISABLE // for PWM +#define IDIBUS_TIM5_ISR_DISABLE // for PWM +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_INTERRUPTS_DISABLE_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusInterruptsList.cpp b/libraries/idiBusMaster/src/IdiBusInterruptsList.cpp new file mode 100644 index 0000000..0b9f7c5 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusInterruptsList.cpp @@ -0,0 +1,65 @@ +//############################################################################################################################################################################################################# +#include "IdiBusInterruptsList.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +struct IDIBUS_INTERRUPT_LISTENERS_STR InterruptListenersList; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#ifndef IDIBUS_USART0_ISR_DISABLE +ISR(USART0_RX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptRX(InterruptListenersList.USART0_CallbackInstance); } +ISR(USART0_TX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptTX(InterruptListenersList.USART0_CallbackInstance); } +ISR(USART0_UDRE_vect) { IdiBusCallbackInterface::USART_Callback_InterruptUDRE(InterruptListenersList.USART0_CallbackInstance); } +#endif +#ifndef IDIBUS_USART1_ISR_DISABLE +ISR(USART1_RX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptRX(InterruptListenersList.USART1_CallbackInstance); } +ISR(USART1_TX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptTX(InterruptListenersList.USART1_CallbackInstance); } +ISR(USART1_UDRE_vect) { IdiBusCallbackInterface::USART_Callback_InterruptUDRE(InterruptListenersList.USART1_CallbackInstance); } +#endif +#ifndef IDIBUS_USART2_ISR_DISABLE +ISR(USART2_RX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptRX(InterruptListenersList.USART2_CallbackInstance); } +ISR(USART2_TX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptTX(InterruptListenersList.USART2_CallbackInstance); } +ISR(USART2_UDRE_vect) { IdiBusCallbackInterface::USART_Callback_InterruptUDRE(InterruptListenersList.USART2_CallbackInstance); } +#endif +#ifndef IDIBUS_USART3_ISR_DISABLE +ISR(USART3_RX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptRX(InterruptListenersList.USART3_CallbackInstance); } +ISR(USART3_TX_vect) { IdiBusCallbackInterface::USART_Callback_InterruptTX(InterruptListenersList.USART3_CallbackInstance); } +ISR(USART3_UDRE_vect) { IdiBusCallbackInterface::USART_Callback_InterruptUDRE(InterruptListenersList.USART3_CallbackInstance); } +#endif +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#ifndef IDIBUS_TIM0_ISR_DISABLE +ISR(TIMER0_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM0_CallbackInstance); } +ISR(TIMER0_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM0_CallbackInstance); } +ISR(TIMER0_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM0_CallbackInstance); } +#endif +#ifndef IDIBUS_TIM1_ISR_DISABLE +ISR(TIMER1_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM1_CallbackInstance); } +ISR(TIMER1_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM1_CallbackInstance); } +ISR(TIMER1_COMPC_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPC(InterruptListenersList.TIM1_CallbackInstance); } +ISR(TIMER1_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM1_CallbackInstance); } +#endif +#ifndef IDIBUS_TIM2_ISR_DISABLE +ISR(TIMER2_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM2_CallbackInstance); } +ISR(TIMER2_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM2_CallbackInstance); } +ISR(TIMER2_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM2_CallbackInstance); } +#endif +#ifndef IDIBUS_TIM3_ISR_DISABLE +ISR(TIMER3_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM3_CallbackInstance); } +ISR(TIMER3_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM3_CallbackInstance); } +ISR(TIMER3_COMPC_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPC(InterruptListenersList.TIM3_CallbackInstance); } +ISR(TIMER3_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM3_CallbackInstance); } +#endif +#ifndef IDIBUS_TIM4_ISR_DISABLE +ISR(TIMER4_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM4_CallbackInstance); } +ISR(TIMER4_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM4_CallbackInstance); } +ISR(TIMER4_COMPC_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPC(InterruptListenersList.TIM4_CallbackInstance); } +ISR(TIMER4_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM4_CallbackInstance); } +#endif +#ifndef IDIBUS_TIM5_ISR_DISABLE +ISR(TIMER5_COMPA_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPA(InterruptListenersList.TIM5_CallbackInstance); } +ISR(TIMER5_COMPB_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPB(InterruptListenersList.TIM5_CallbackInstance); } +ISR(TIMER5_COMPC_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptCOMPC(InterruptListenersList.TIM5_CallbackInstance); } +ISR(TIMER5_OVF_vect) { IdiBusCallbackInterface::TIM_Callback_InterruptOVF(InterruptListenersList.TIM5_CallbackInstance); } +#endif +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusInterruptsList.h b/libraries/idiBusMaster/src/IdiBusInterruptsList.h new file mode 100644 index 0000000..d660d42 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusInterruptsList.h @@ -0,0 +1,48 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_INTERRUPTS_LIST_H_ +#define _IDIBUS_INTERRUPTS_LIST_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include +#include +#include +#include "IdiBusCallbackInterface.h" +#include "IdiBusInterruptsDisable.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +struct IDIBUS_INTERRUPT_LISTENERS_STR { + #ifndef IDIBUS_USART0_ISR_DISABLE + IdiBusCallbackInterface *USART0_CallbackInstance; + #endif + #ifndef IDIBUS_USART1_ISR_DISABLE + IdiBusCallbackInterface *USART1_CallbackInstance; + #endif + #ifndef IDIBUS_USART2_ISR_DISABLE + IdiBusCallbackInterface *USART2_CallbackInstance; + #endif + #ifndef IDIBUS_USART3_ISR_DISABLE + IdiBusCallbackInterface *USART3_CallbackInstance; + #endif + + #ifndef IDIBUS_TIM0_ISR_DISABLE + IdiBusCallbackInterface *TIM0_CallbackInstance; + #endif + #ifndef IDIBUS_TIM1_ISR_DISABLE + IdiBusCallbackInterface *TIM1_CallbackInstance; + #endif + #ifndef IDIBUS_TIM2_ISR_DISABLE + IdiBusCallbackInterface *TIM2_CallbackInstance; + #endif + #ifndef IDIBUS_TIM3_ISR_DISABLE + IdiBusCallbackInterface *TIM3_CallbackInstance; + #endif + #ifndef IDIBUS_TIM4_ISR_DISABLE + IdiBusCallbackInterface *TIM4_CallbackInstance; + #endif + #ifndef IDIBUS_TIM5_ISR_DISABLE + IdiBusCallbackInterface *TIM5_CallbackInstance; + #endif + }; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +extern struct IDIBUS_INTERRUPT_LISTENERS_STR InterruptListenersList; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //_IDIBUS_INTERRUPTS_LIST_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusMaster.h b/libraries/idiBusMaster/src/IdiBusMaster.h new file mode 100644 index 0000000..7aa794a --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusMaster.h @@ -0,0 +1,22 @@ +#include "IDIBUS_1Wire.h" +#include "IDIBUS_2SALTMETER.h" +#include "IDIBUS_3Thermo1HT.h" +#include "IDIBUS_4ADC.h" +#include "IDIBUS_4CNT.h" +#include "IDIBUS_4DC.h" +#include "IDIBUS_NEXTION.h" +#include "IDIBUS_Speedster.h" +#include "IDIBUS_1xGen.h" + +#include "IdiBusCallbackInterface.h" +#include "IdiBusDateTime.h" +#include "IdibusDefs.h" +#include "IdiBusInterruptsDisable.h" +#include "IdiBusInterruptsList.h" +#include "MODBUS_CRC.h" +#include "IdiBusMes.h" +#include "IdiBusSerial.h" +#include "IdiBusSerialDefs.h" +#include "IdiBusSerialLine.h" +#include "IdiBusSlave.h" +#include "IdiBusSystem.h" \ No newline at end of file diff --git a/libraries/idiBusMaster/src/IdiBusMes.cpp b/libraries/idiBusMaster/src/IdiBusMes.cpp new file mode 100644 index 0000000..af55832 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusMes.cpp @@ -0,0 +1,128 @@ +//############################################################################################################################################################################################################# +#include "IdiBusMes.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMessage::IsGroupAddr(void) { return ( (this->SlAddr >= IDIBUS_GROUP_0_ADDR)&&(this->SlAddr <= IDIBUS_GROUP_15_ADDR) ) ? 1 : 0; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMessage::IsCommand(void) { return ( this->ComFunc >= IDIMMES_COM_START_NUM ) ? 1 : 0; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMessage::IsModuleError(uint8_t ErrCode) { return ((ErrCode >= IDIER_MODULE_NUM_START)&&(ErrCode <= IDIER_MODULE_NUM_END)) ? 1 : 0; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMessage::IsModuleCommand(void) +{ + switch (this->ComFunc) + { + case(IDIMMES_COM_C_Init) : + case(IDIMMES_COM_C_ShtDown) : + case(IDIMMES_COM_C_Freeze) : + case(IDIMMES_COM_C_Resume) : + case(IDIMMES_COM_C_SetAlarmL12) : + case(IDIMMES_COM_C_SetAlarmL) : + case(IDIMMES_COM_C_SyncRead) : + case(IDIMMES_COM_C_SyncDoChnl) : + case(IDIMMES_COM_C_SyncDo) : + case(IDIMMES_COM_C_SyncClear) : + case(IDIMMES_COM_C_SendTimeDate) : + case(IDIMMES_COM_C_MkTimedMaster) : + case(IDIMMES_COM_C_FmwUpd) : + case(IDIMMES_COM_C_EndFmwUpd) : + case(IDIMMES_COM_C_FmwWrite) : + case(IDIMMES_COM_C_ReadDevFullSN_MS) : + case(IDIMMES_COM_C_WriteSnIPv4IPv6) : + case(IDIMMES_COM_C_WriteSnVerifyDates) : + case(IDIMMES_COM_C_WriteSnAES256) : + case(IDIMMES_COM_C_DummyModule) : + case(IDIMMES_COM_C_CheckModuleLongOp) : { return 1; } + default : {} + } + return 0; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +IdiBusMMES::IdiBusMMES (uint8_t SlAddr, uint8_t *TX_DataP, uint16_t TX_DataLength) +{ + this->SlAddr = SlAddr; + this->MMPS.LongMes = 0; + this->MMPS.AlarmMes = 0; + this->MMPS.EncryptedAes = 0; + this->DEV.NUM = 0; + this->DEV.ALLCH = 0; + this->CHNL.ALLSAME = 0; + this->CHNL.NUM = 0; + this->ComFunc = 1; + + this->TxData = TX_DataP; + this->TxDataLength = TX_DataLength; + this->RxData = NULL; + this->RxDataLength = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusMMES::IdiBusMMES (uint8_t SlAddr) +{ + this->SlAddr = SlAddr; + this->MMPS.LongMes = 0; + this->MMPS.AlarmMes = 0; + this->MMPS.EncryptedAes = 0; + this->DEV.NUM = 0; + this->DEV.ALLCH = 0; + this->CHNL.ALLSAME = 0; + this->CHNL.NUM = 0; + this->ComFunc = IDIMMES_COM_C_Dummy; + + this->TxData = NULL; + this->TxDataLength = 0; + this->RxData = NULL; + this->RxDataLength = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMMES::IsValid(void) +{ + if ( (this->ComFunc == 0) || this->IsModuleCommand() || this->IsGroupAddr() ) { return 0; } + return 1; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +IdiBusMMESG::IdiBusMMESG (uint8_t SlAddr, uint8_t *TX_DataP, uint16_t TX_DataLength) +{ + this->SlAddr = SlAddr; + this->MMPS.LongMes = 0; + this->MMPS.AlarmMes = 0; + this->MMPS.EncryptedAes = 0; + this->ComFunc = IDIMMES_COM_C_DummyModule; + + this->TxData = TX_DataP; + this->TxDataLength = TX_DataLength; + this->RxData = NULL; + this->RxDataLength = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusMMESG::IdiBusMMESG (uint8_t SlAddr) +{ + this->SlAddr = SlAddr; + this->MMPS.LongMes = 0; + this->MMPS.AlarmMes = 0; + this->MMPS.EncryptedAes = 0; + this->ComFunc = IDIMMES_COM_C_Dummy; + + this->TxData = NULL; + this->TxDataLength = 0; + this->RxData = NULL; + this->RxDataLength = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusMMESG::IsValid(void) +{ + uint8_t isModuleCommand = this->IsModuleCommand(); + uint8_t isGroupAddress = this->IsGroupAddr(); + + if ( this->ComFunc == IDIMMES_NOT_FAST_FUNC ) { return 0; } + if ( isGroupAddress ) + { + if ( isModuleCommand != 0 ) { return 0; } + } + else if ( isModuleCommand == 0 ) { return 0; } + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusMes.h b/libraries/idiBusMaster/src/IdiBusMes.h new file mode 100644 index 0000000..2576ccd --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusMes.h @@ -0,0 +1,50 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_MES_H_ +#define _IDIBUS_MES_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSystem.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusMessage { + public : + uint8_t SlAddr; + struct { + uint8_t LongMes : 1; + uint8_t AlarmMes : 1; + uint8_t EncryptedAes : 1; + } MMPS; + uint8_t ComFunc; + uint8_t *TxData; + uint16_t TxDataLength; + uint8_t *RxData; + uint16_t RxDataLength; + + uint8_t IsGroupAddr(void); + uint8_t IsModuleCommand(void); + uint8_t IsCommand(void); + uint8_t IsModuleError(uint8_t ErrCode); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusMMES : public IdiBusMessage { + public : + IdiBusMMES (uint8_t SlAddr, uint8_t *TX_DataP, uint16_t TX_DataLength); + IdiBusMMES (uint8_t SlAddr); + struct { + uint8_t NUM : 5; + uint8_t ALLCH : 1; + } DEV; + struct { + uint8_t NUM : 7; + uint8_t ALLSAME : 1; + } CHNL; + uint8_t IsValid(void); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusMMESG : public IdiBusMessage { + public : + IdiBusMMESG (uint8_t SlAddr, uint8_t *TX_DataP, uint16_t TX_DataLength); + IdiBusMMESG (uint8_t SlAddr); + uint8_t IsValid(void); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_MES_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSerial.cpp b/libraries/idiBusMaster/src/IdiBusSerial.cpp new file mode 100644 index 0000000..bc64f32 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSerial.cpp @@ -0,0 +1,621 @@ +//############################################################################################################################################################################################################# +#include "IdiBusSerial.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusSerial::IdiBusSerial(void) +{ + this->RS485_DIR_GPIO.DDR = (uint8_t*)&this->InitProtectionVar; + this->RS485_DIR_GPIO.PIN = (uint8_t*)&this->InitProtectionVar; + this->RS485_DIR_GPIO.PORT = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S0_Gpio.DDR = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S0_Gpio.PIN = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S0_Gpio.PORT = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S1_Gpio.DDR = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S1_Gpio.PIN = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S1_Gpio.PORT = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S2_Gpio.DDR = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S2_Gpio.PIN = (uint8_t*)&this->InitProtectionVar; + this->BAUDRATE_DIPSW.S2_Gpio.PORT = (uint8_t*)&this->InitProtectionVar; + this->USARTN.UCSRNA = (uint8_t*)&this->InitProtectionVar; + this->USARTN.UCSRNB = (uint8_t*)&this->InitProtectionVar; + this->USARTN.UCSRNC = (uint8_t*)&this->InitProtectionVar; + this->USARTN.UBRRN = (uint16_t*)&this->InitProtectionVar; + this->USARTN.UDRN = (uint8_t*)&this->InitProtectionVar; + this->TIMN.TCCRNA = (uint8_t*)&this->InitProtectionVar; + this->TIMN.TCCRNB = (uint8_t*)&this->InitProtectionVar; + this->TIMN.TIMSKN = (uint8_t*)&this->InitProtectionVar; + this->TIMN.TIFRN = (uint8_t*)&this->InitProtectionVar; + this->TIMN.OCRNA = (uint16_t*)&this->InitProtectionVar; + this->TIMN.OCRNB = (uint16_t*)&this->InitProtectionVar; + this->TIMN.TCNTN = (uint16_t*)&this->InitProtectionVar; + this->InitState = 0; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::Init(IDIBUS_SERIAL_INIT_TYPEDEF *InitT) +{ + if ( (this->GetGpioParamsArduino(InitT->RS485_DIR_PIN, &this->RS485_DIR_GPIO) == 0) || + (this->GetGpioParamsArduino(InitT->BAUDRATE_DIPSW_S0_PIN, &this->BAUDRATE_DIPSW.S0_Gpio) == 0) || + (this->GetGpioParamsArduino(InitT->BAUDRATE_DIPSW_S1_PIN, &this->BAUDRATE_DIPSW.S1_Gpio) == 0) || + (this->GetGpioParamsArduino(InitT->BAUDRATE_DIPSW_S2_PIN, &this->BAUDRATE_DIPSW.S2_Gpio) == 0) ) { return 0; } + + return this->BaseInit(InitT->USART_NUMBER, InitT->TIMER_NUMBER, InitT->CONFIG); +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::Init(IDIBUS_SERIAL_ALTERNATIVE_INIT_TYPEDEF *InitT) +{ + if ( (this->GetGpioParams(&InitT->RS485_DIR_GPIO, &this->RS485_DIR_GPIO) == 0) || + (this->GetGpioParams(&InitT->BAUDRATE_DIPSW.S0, &this->BAUDRATE_DIPSW.S0_Gpio) == 0) || + (this->GetGpioParams(&InitT->BAUDRATE_DIPSW.S1, &this->BAUDRATE_DIPSW.S1_Gpio) == 0) || + (this->GetGpioParams(&InitT->BAUDRATE_DIPSW.S2, &this->BAUDRATE_DIPSW.S2_Gpio) == 0) ) { return 0; } + + return this->BaseInit(InitT->USART_NUMBER, InitT->TIMER_NUMBER, InitT->CONFIG); +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::BaseInit( enum IDIBUS_SERIAL_USARTN_NUMBER USART_NUM, enum IDIBUS_SERIAL_TIMER_NUMBER TIMER_NUM, enum IDIBUS_SERIAL_USARTN_CONFIG CONF) +{ + // Pointers Init + switch (USART_NUM) + { + case (IDIBUS_SERIAL_USART0) : { + #if defined IDIBUS_USART0_ISR_DISABLE + return 0; + #else + this->USARTN.UCSRNA = &(UCSR0A); + this->USARTN.UCSRNB = &(UCSR0B); + this->USARTN.UCSRNC = &(UCSR0C); + this->USARTN.UBRRN = &(UBRR0); + this->USARTN.UDRN = &(UDR0); + InterruptListenersList.USART0_CallbackInstance = this; + #endif + break; } + case (IDIBUS_SERIAL_USART1) : { + #if defined IDIBUS_USART1_ISR_DISABLE + return 0; + #else + this->USARTN.UCSRNA = &(UCSR1A); + this->USARTN.UCSRNB = &(UCSR1B); + this->USARTN.UCSRNC = &(UCSR1C); + this->USARTN.UBRRN = &(UBRR1); + this->USARTN.UDRN = &(UDR1); + InterruptListenersList.USART1_CallbackInstance = this; + #endif + break; } + case (IDIBUS_SERIAL_USART2) : { + #if defined IDIBUS_USART2_ISR_DISABLE + return 0; + #else + this->USARTN.UCSRNA = &(UCSR2A); + this->USARTN.UCSRNB = &(UCSR2B); + this->USARTN.UCSRNC = &(UCSR2C); + this->USARTN.UBRRN = &(UBRR2); + this->USARTN.UDRN = &(UDR2); + InterruptListenersList.USART2_CallbackInstance = this; + #endif + break; } + case (IDIBUS_SERIAL_USART3) : { + #if defined IDIBUS_USART3_ISR_DISABLE + return 0; + #else + this->USARTN.UCSRNA = &(UCSR3A); + this->USARTN.UCSRNB = &(UCSR3B); + this->USARTN.UCSRNC = &(UCSR3C); + this->USARTN.UBRRN = &(UBRR3); + this->USARTN.UDRN = &(UDR3); + InterruptListenersList.USART3_CallbackInstance = this; + #endif + break; } + default : { return 0; } + } + + switch (TIMER_NUM) + { + case (IDIBUS_TIMER_1) : { + #if defined IDIBUS_TIM1_ISR_DISABLE + return 0; + #else + this->TIMN.TCCRNA = &(TCCR1A); + this->TIMN.TCCRNB = &(TCCR1B); + this->TIMN.TIMSKN = &(TIMSK1); + this->TIMN.TIFRN = &(TIFR1); + this->TIMN.OCRNA = &(OCR1A); + this->TIMN.OCRNB = &(OCR1B); + this->TIMN.TCNTN = &(TCNT1); + InterruptListenersList.TIM1_CallbackInstance = this; + #endif + break; } + case (IDIBUS_TIMER_3) : { + #if defined IDIBUS_TIM3_ISR_DISABLE + return 0; + #else + this->TIMN.TCCRNA = &(TCCR3A); + this->TIMN.TCCRNB = &(TCCR3B); + this->TIMN.TIMSKN = &(TIMSK3); + this->TIMN.TIFRN = &(TIFR3); + this->TIMN.OCRNA = &(OCR3A); + this->TIMN.OCRNB = &(OCR3B); + this->TIMN.TCNTN = &(TCNT3); + InterruptListenersList.TIM3_CallbackInstance = this; + #endif + break; } + case (IDIBUS_TIMER_4) : { + #if defined IDIBUS_TIM4_ISR_DISABLE + return 0; + #else + this->TIMN.TCCRNA = &(TCCR4A); + this->TIMN.TCCRNB = &(TCCR4B); + this->TIMN.TIMSKN = &(TIMSK4); + this->TIMN.TIFRN = &(TIFR4); + this->TIMN.OCRNA = &(OCR4A); + this->TIMN.OCRNB = &(OCR4B); + this->TIMN.TCNTN = &(TCNT4); + InterruptListenersList.TIM4_CallbackInstance = this; + #endif + break; } + case (IDIBUS_TIMER_5) : { + #if defined IDIBUS_TIM5_ISR_DISABLE + return 0; + #else + this->TIMN.TCCRNA = &(TCCR5A); + this->TIMN.TCCRNB = &(TCCR5B); + this->TIMN.TIMSKN = &(TIMSK5); + this->TIMN.TIFRN = &(TIFR5); + this->TIMN.OCRNA = &(OCR5A); + this->TIMN.OCRNB = &(OCR5B); + this->TIMN.TCNTN = &(TCNT5); + InterruptListenersList.TIM5_CallbackInstance = this; + #endif + break; } + default : { return 0; } + } + + // GPIO Init + uint8_t SREG_Temp = SREG; + cli(); + *this->RS485_DIR_GPIO.PORT &= ~this->RS485_DIR_GPIO.PIN_MASK; + *this->RS485_DIR_GPIO.DDR |= this->RS485_DIR_GPIO.PIN_MASK; + + *this->BAUDRATE_DIPSW.S0_Gpio.PORT &= ~this->BAUDRATE_DIPSW.S0_Gpio.PIN_MASK; + *this->BAUDRATE_DIPSW.S0_Gpio.DDR &= ~this->BAUDRATE_DIPSW.S0_Gpio.PIN_MASK; + + *this->BAUDRATE_DIPSW.S1_Gpio.PORT &= ~this->BAUDRATE_DIPSW.S1_Gpio.PIN_MASK; + *this->BAUDRATE_DIPSW.S1_Gpio.DDR &= ~this->BAUDRATE_DIPSW.S1_Gpio.PIN_MASK; + + *this->BAUDRATE_DIPSW.S2_Gpio.PORT &= ~this->BAUDRATE_DIPSW.S2_Gpio.PIN_MASK; + *this->BAUDRATE_DIPSW.S2_Gpio.DDR &= ~this->BAUDRATE_DIPSW.S2_Gpio.PIN_MASK; + if ( SREG_Temp & (0x01U << SREG_I) ) { sei(); } + + // USART Init + *this->USARTN.UBRRN = 0; + *this->USARTN.UCSRNA = (1U<USARTN.UCSRNB = 0; + this->BAUDRATE = 0; + switch (CONF) + { + case (IDIBUS_SERIAL_8N1) : { *this->USARTN.UCSRNC = (1U<USARTN.UCSRNC = (1U<USARTN.UCSRNC = (1U<USARTN.UCSRNC = (1U<USARTN.UCSRNC = (1U<USARTN.UCSRNC = (1U<TIMN.TCCRNB = 0; // Counter stop + *this->TIMN.TCCRNA = 0; // Normal Mode + *this->TIMN.TIMSKN = 0; + + this->InitState = 1; + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::SetBaudrate(enum IDIBUS_SERIAL_BAUDRATE Baudrate) +{ + switch ( Baudrate ) + { + case (IDIBUS_BAUDRATE_DSW_CODE_2400B) : { + this->SetBaudrateLL(2400); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_2400B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_2400B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint32_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_9600B) : { + this->SetBaudrateLL(9600); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_9600B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_9600B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint32_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_19200B) : { + this->SetBaudrateLL(19200); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_19200B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_19200B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_115200B) : { + this->SetBaudrateLL(115200); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_115200B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_115200B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_250K) : { + this->SetBaudrateLL(250000); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_250K_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_250K_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_500K) : { + this->SetBaudrateLL(500000); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_500K_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_500K_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_1M) : { + this->SetBaudrateLL(1000000); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_1M_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_1M_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_10M) : { + this->SetBaudrateLL(10000000); + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_10M_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_10M_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + default : { return; } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::UpdateBaudrateFromDipSwitch(void) +{ + uint8_t BaudrateCode = 0; + uint32_t BaudrateCodeValue; + if ( (*this->BAUDRATE_DIPSW.S2_Gpio.PIN & this->BAUDRATE_DIPSW.S2_Gpio.PIN_MASK) != 0 ) { BaudrateCode |= 0x04; } + if ( (*this->BAUDRATE_DIPSW.S1_Gpio.PIN & this->BAUDRATE_DIPSW.S1_Gpio.PIN_MASK) != 0 ) { BaudrateCode |= 0x02; } + if ( (*this->BAUDRATE_DIPSW.S0_Gpio.PIN & this->BAUDRATE_DIPSW.S0_Gpio.PIN_MASK) != 0 ) { BaudrateCode |= 0x01; } + + switch (BaudrateCode) + { + case (IDIBUS_BAUDRATE_DSW_CODE_2400B) : { BaudrateCodeValue = 2400U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_9600B) : { BaudrateCodeValue = 9600U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_19200B) : { BaudrateCodeValue = 19200U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_115200B) : { BaudrateCodeValue = 115200U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_250K) : { BaudrateCodeValue = 250000U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_500K) : { BaudrateCodeValue = 500000U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_1M) : { BaudrateCodeValue = 1000000U; break; } + case (IDIBUS_BAUDRATE_DSW_CODE_10M) : { BaudrateCodeValue = 10000000U; break; } + default : { return; } + } + if ( this->BAUDRATE != BaudrateCodeValue ) + { + this->SetBaudrateLL(BaudrateCodeValue); + switch ( BaudrateCode ) + { + case (IDIBUS_BAUDRATE_DSW_CODE_2400B) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_2400B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_2400B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_9600B) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_9600B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_9600B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_19200B) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_19200B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_19200B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_115200B) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_115200B_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_115200B_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_250K) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_250K_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_250K_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_500K) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_500K_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_500K_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_1M) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_1M_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_1M_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + case (IDIBUS_BAUDRATE_DSW_CODE_10M) : { + this->TimerInterFrameCompValue = (uint16_t)( ((uint64_t)IDIBUS_10M_INTERFRAME_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE ) / 1000000ULL ); + this->TimerTimeoutCompValue = (uint16_t)( ((uint64_t)IDIBUS_10M_MASTER_RESPONSE_TIMEOUT_US * (uint64_t)F_CPU / (uint64_t)IDIBUS_SERIAL_TIMER_PR_VALUE) / 1000000ULL ); + break; } + default : { return; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::SetBaudrateLL(uint32_t Baudrate) +{ + uint16_t UBRR_2X; + if ( ((F_CPU / 8U) % Baudrate) < (Baudrate / 2) ) + { + UBRR_2X = (uint16_t)( ((uint32_t)F_CPU / 8U) / Baudrate ); + if ( UBRR_2X != 0 ) { UBRR_2X--; } + } + else { UBRR_2X = (uint16_t)( ((uint32_t)F_CPU / 16U) / Baudrate ); } // WARNING CHANGED DIVIDER FROM 8 TO 16!! //(F_CPU / 8U) / Baudrate + 1 - 1 + *this->USARTN.UBRRN = UBRR_2X; + this->BAUDRATE = Baudrate; + if ( this->InitState == 1 ) { this->InitState = 2; } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +void IdiBusSerial::Start(void) +{ + *this->USARTN.UCSRNB = (1U<RS485_DIR_GPIO.PORT |= this->RS485_DIR_GPIO.PIN_MASK; // Dir to Tx + sei(); + this->TransferComplete = 1; // +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::Stop(void) +{ + *this->TIMN.TCCRNB = 0; // Timer Stop + cli(); + *this->RS485_DIR_GPIO.PORT &= ~this->RS485_DIR_GPIO.PIN_MASK; // Dir to RX + sei(); + *this->USARTN.UCSRNB = 0; // USART TX/RX Periph disable + this->TransferComplete = 1; // +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::SendRequestAsync(uint8_t *_TxBuf_, uint16_t _TxBufLength_) +{ + if ( (this->TransferComplete == 0) || (this->InitState != 2) ) { return 0; } + + this->ERROR_STATUS.SumVar = 0; + if ( _TxBufLength_ == 0 ) { this->ERROR_STATUS.BF.TxZerolength = 1; return 0; } + else if ( _TxBufLength_ > IDIBUS_SERIAL_TX_BUF_SIZE ) { this->ERROR_STATUS.BF.TxOverflow = 1; return 0; } + + memcpy(this->TxBuf, _TxBuf_, _TxBufLength_); + this->TransferComplete = 0; + this->RequestType = _IDIBUS_SERIAL_TX_RX_; + + *this->TIMN.OCRNA = this->TimerInterFrameCompValue; + if ( this->TimerTimeoutCompValue > (0xFFFFU - this->TimerInterFrameCompValue) ) + { + *this->TIMN.OCRNB = 0xFFFFU; + this->TimerTimeoutOverflow = (uint16_t)( this->TimerTimeoutCompValue - (0xFFFFU - this->TimerInterFrameCompValue) ); + } + else + { + *this->TIMN.OCRNB = (uint16_t)(this->TimerTimeoutCompValue + this->TimerInterFrameCompValue); + this->TimerTimeoutOverflow = 0; + } + *this->TIMN.TCNTN = 0; + *this->TIMN.TIFRN = (1U << USARTN_TIM_TIFR_OCFA_Pos) | (1U << USARTN_TIM_TIFR_OCFB_Pos); // Interframe and Response timeouts + *this->TIMN.TIMSKN = (1U << USRTN_TIM_TIMSK_OCIEA_Pos) | (1U << USRTN_TIM_TIMSK_OCIEB_Pos); + + this->TxBufCounter = 1; + this->TxBufLength = _TxBufLength_; + this->RxBufCounter = 0; + *this->USARTN.UDRN = this->TxBuf[0]; + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::SendRequestSync(uint8_t *_TxBuf_, uint16_t _TxBufLength_) +{ + if ( this->InitState != 2 ) { return 0; } + while ( this->TransferComplete == 0 ) { } + if ( this->SendRequestAsync(_TxBuf_, _TxBufLength_) == 0 ) { return 0; } + while ( this->TransferComplete == 0 ) { } + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::WriteAsync(uint8_t *_TxBuf_, uint16_t _TxBufLength_) +{ + if ( (this->TransferComplete == 0) || (this->InitState != 2) ) { return 0; } + + this->ERROR_STATUS.SumVar = 0; + if ( _TxBufLength_ == 0 ) { this->ERROR_STATUS.BF.TxZerolength = 1; return 0; } + else if ( _TxBufLength_ > IDIBUS_SERIAL_TX_BUF_SIZE ) { this->ERROR_STATUS.BF.TxOverflow = 1; return 0; } + + this->TransferComplete = 0; + memcpy(this->TxBuf, _TxBuf_, _TxBufLength_); + this->RequestType = _IDIBUS_SERIAL_TX_; + + *this->TIMN.OCRNA = this->TimerInterFrameCompValue; + *this->TIMN.TCNTN = 0; + *this->TIMN.TIFRN = (1U << USARTN_TIM_TIFR_OCFA_Pos) | (1U << USARTN_TIM_TIFR_OCFB_Pos); // Interframe Timeout + *this->TIMN.TIMSKN = (1U << USRTN_TIM_TIMSK_OCIEA_Pos); + + this->TxBufCounter = 1; + this->TxBufLength = _TxBufLength_; + *this->USARTN.UDRN = this->TxBuf[0]; + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::WriteSync(uint8_t *_TxBuf_, uint16_t _TxBufLength_) +{ + if ( this->InitState != 2 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + if ( this->WriteAsync(_TxBuf_, _TxBufLength_) == 0 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + return 1; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t *IdiBusSerial::getTxBufPointer(void) +{ + if ( this->TransferComplete ) { return this->TxBuf; } + else { return NULL; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t *IdiBusSerial::getRxBufPointer (void) +{ + return this->RxBuf; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::SendRequestAsync(uint16_t DataLength) +{ + if ( (this->TransferComplete == 0) || (this->InitState != 2) ) { return 0; } + this->ERROR_STATUS.SumVar = 0; + this->TransferComplete = 0; + this->RequestType = _IDIBUS_SERIAL_TX_RX_; + + *this->TIMN.OCRNA = this->TimerInterFrameCompValue; + if ( this->TimerTimeoutCompValue > (0xFFFFU - this->TimerInterFrameCompValue) ) + { + *this->TIMN.OCRNB = 0xFFFFU; + this->TimerTimeoutOverflow = (uint16_t)( this->TimerTimeoutCompValue - (0xFFFFU - this->TimerInterFrameCompValue) ); + } + else + { + *this->TIMN.OCRNB = (uint16_t)(this->TimerTimeoutCompValue + this->TimerInterFrameCompValue); + this->TimerTimeoutOverflow = 0; + } + *this->TIMN.TCNTN = 0; + *this->TIMN.TIFRN = (1U << USARTN_TIM_TIFR_OCFA_Pos) | (1U << USARTN_TIM_TIFR_OCFB_Pos); // Interframe and Response timeouts + *this->TIMN.TIMSKN = (1U << USRTN_TIM_TIMSK_OCIEA_Pos) | (1U << USRTN_TIM_TIMSK_OCIEB_Pos); + + this->TxBufCounter = 1; + this->TxBufLength = DataLength; + this->RxBufCounter = 0; + *this->USARTN.UDRN = this->TxBuf[0]; + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::SendRequestSync(uint16_t DataLength) +{ + if ( this->InitState != 2 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + if ( this->SendRequestAsync(DataLength) == 0 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::WriteAsync(uint16_t DataLength) +{ + if ( (this->TransferComplete == 0) || (this->InitState != 2) ) { return 0; } + this->ERROR_STATUS.SumVar = 0; + this->TransferComplete = 0; + this->RequestType = _IDIBUS_SERIAL_TX_; + + *this->TIMN.OCRNA = this->TimerInterFrameCompValue; + *this->TIMN.TCNTN = 0; + *this->TIMN.TIFRN = (1U << USARTN_TIM_TIFR_OCFA_Pos) | (1U << USARTN_TIM_TIFR_OCFB_Pos); // Interframe Timeout + *this->TIMN.TIMSKN = (1U << USRTN_TIM_TIMSK_OCIEA_Pos); + + this->TxBufCounter = 1; + this->TxBufLength = DataLength; + *this->USARTN.UDRN = this->TxBuf[0]; + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::WriteSync(uint16_t DataLength) +{ + if ( this->InitState != 2 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + if ( this->WriteAsync(DataLength) == 0 ) { return 0; } + while ( this->TransferComplete == 0 ) {} + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::IsTransferComplete(void) { return this->TransferComplete; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint16_t IdiBusSerial::GetRxBufCounter(void) { return this->RxBufCounter; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IDIBUS_SERIAL_ERROR_STATUS_TYPE IdiBusSerial::GetErrorStatus(void) { return this->ERROR_STATUS; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint32_t IdiBusSerial::GetBaudrate(void) { return this->BAUDRATE; } +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +void IdiBusSerial::USART_RxInterruptFunc (void) +{ + *this->TIMN.TCNTN = 0; + if ( (*this->USARTN.UCSRNA & ( (1U<RxBufCounter < IDIBUS_SERIAL_RX_BUF_SIZE ) { this->RxBuf[this->RxBufCounter++] = *this->USARTN.UDRN; } + else { this->ERROR_STATUS.BF.RxOverflow = 1; (void)*this->USARTN.UDRN; } + } + else { this->ERROR_STATUS.BF.LLComERR = 1; (void)*this->USARTN.UDRN; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::USART_TxInterruptFunc (void) +{ + if ( this->TxBufCounter != this->TxBufLength ) { *this->USARTN.UDRN = this->TxBuf[this->TxBufCounter++]; } + else + { + switch (this->RequestType) + { + case (_IDIBUS_SERIAL_TX_RX_) : { + *this->RS485_DIR_GPIO.PORT &= ~this->RS485_DIR_GPIO.PIN_MASK; + *this->USARTN.UCSRNB |= (1U<TIMN.TCCRNB = IDIBUS_SERIAL_TIMER_PR_REG; //Timer Start + break; } + case (_IDIBUS_SERIAL_TX_) : { + *this->TIMN.TCCRNB = IDIBUS_SERIAL_TIMER_PR_REG; //Timer Start + break; } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::TIM_COMPA_InterruptFunc (void) +{ + switch (this->RequestType) + { + case ( _IDIBUS_SERIAL_TX_RX_ ) : { + if ( this->RxBufCounter != 0 ) // Interframe Timeout < Response < Request Timeout + { + *this->TIMN.TCCRNB = 0; // Timer Stop + *this->USARTN.UCSRNB &= ~( (1U<RS485_DIR_GPIO.PORT |= this->RS485_DIR_GPIO.PIN_MASK; // Dir to TX + this->TransferComplete = 1; // Complete + } + break; } + case (_IDIBUS_SERIAL_TX_) : { + *this->TIMN.TCCRNB = 0; // Timer Stop + this->TransferComplete = 1; // Complete + break; } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSerial::TIM_COMPB_InterruptFunc (void) +{ + if ( this->TimerTimeoutOverflow != 0 ) + { + *this->TIMN.OCRNB = this->TimerTimeoutOverflow; + *this->TIMN.TCNTN = 0; + *this->TIMN.TIFRN = (1U << USARTN_TIM_TIFR_OCFB_Pos); + this->TimerTimeoutOverflow = 0; + } + else + { + *this->TIMN.TCCRNB = 0; //Timer Stop + *this->USARTN.UCSRNB &= ~( (1U<RS485_DIR_GPIO.PORT |= this->RS485_DIR_GPIO.PIN_MASK; + this->ERROR_STATUS.BF.TimeoutERR = 1; + this->TransferComplete = 1; + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSerial::GetGpioParams(IDIBUS_SERIAL_GPIO_INIT *InitSTR, IDIBUS_SERIAL_GPIO *gpio) +{ + if ( InitSTR->PIN_NUM > 7 ) { return 0; } + + if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTA ) { gpio->PORT = &PORTA; gpio->DDR = &DDRA; gpio->PIN = &PINA; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTB ) { gpio->PORT = &PORTB; gpio->DDR = &DDRB; gpio->PIN = &PINB; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTC ) { gpio->PORT = &PORTC; gpio->DDR = &DDRC; gpio->PIN = &PINC; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTD ) { gpio->PORT = &PORTD; gpio->DDR = &DDRD; gpio->PIN = &PIND; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTE ) { gpio->PORT = &PORTE; gpio->DDR = &DDRE; gpio->PIN = &PINE; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTF ) { gpio->PORT = &PORTF; gpio->DDR = &DDRF; gpio->PIN = &PINF; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTG ) { gpio->PORT = &PORTG; gpio->DDR = &DDRG; gpio->PIN = &PING; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTH ) { gpio->PORT = &PORTH; gpio->DDR = &DDRH; gpio->PIN = &PINH; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTJ ) { gpio->PORT = &PORTJ; gpio->DDR = &DDRJ; gpio->PIN = &PINJ; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTK ) { gpio->PORT = &PORTK; gpio->DDR = &DDRK; gpio->PIN = &PINK; } + else if ( (uint16_t)InitSTR->PORT == (uint16_t)&PORTL ) { gpio->PORT = &PORTL; gpio->DDR = &DDRL; gpio->PIN = &PINL; } + else { return 0; } + gpio->PIN_MASK = (0x01U << InitSTR->PIN_NUM); + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerial::GetGpioParamsArduino(uint8_t PinNum, IDIBUS_SERIAL_GPIO *gpio) +{ + uint8_t PortCode; + if ( (PinNum >= NUM_DIGITAL_PINS) || ((PortCode = digitalPinToPort(PinNum)) == NOT_A_PIN) ) { return 0; } + gpio->PIN_MASK = digitalPinToBitMask(PinNum); + gpio->PORT = portOutputRegister(PortCode); + gpio->DDR = portModeRegister(PortCode); + gpio->PIN = portInputRegister(PortCode); + return 1; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSerial.h b/libraries/idiBusMaster/src/IdiBusSerial.h new file mode 100644 index 0000000..d69c074 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSerial.h @@ -0,0 +1,123 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_SERIAL_H_ +#define _IDIBUS_SERIAL_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSystem.h" +#include "IdiBusSerialDefs.h" +#include "IdiBusInterruptsList.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_SERIAL_USARTN_NUMBER { + IDIBUS_SERIAL_USART0, + IDIBUS_SERIAL_USART1, + IDIBUS_SERIAL_USART2, + IDIBUS_SERIAL_USART3 + }; +enum IDIBUS_SERIAL_TIMER_NUMBER { + IDIBUS_TIMER_1 = 1, + IDIBUS_TIMER_3 = 3, + IDIBUS_TIMER_4, + IDIBUS_TIMER_5 + }; +enum IDIBUS_SERIAL_USARTN_CONFIG { + IDIBUS_SERIAL_8N1, + IDIBUS_SERIAL_8N2, + IDIBUS_SERIAL_8E1, + IDIBUS_SERIAL_8E2, + IDIBUS_SERIAL_8O1, + IDIBUS_SERIAL_8O2 + }; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + enum IDIBUS_SERIAL_USARTN_NUMBER USART_NUMBER; + enum IDIBUS_SERIAL_TIMER_NUMBER TIMER_NUMBER; + enum IDIBUS_SERIAL_USARTN_CONFIG CONFIG; + uint8_t RS485_DIR_PIN; + uint8_t BAUDRATE_DIPSW_S0_PIN; + uint8_t BAUDRATE_DIPSW_S1_PIN; + uint8_t BAUDRATE_DIPSW_S2_PIN; + } IDIBUS_SERIAL_INIT_TYPEDEF; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + enum IDIBUS_SERIAL_USARTN_NUMBER USART_NUMBER; + enum IDIBUS_SERIAL_TIMER_NUMBER TIMER_NUMBER; + enum IDIBUS_SERIAL_USARTN_CONFIG CONFIG; + IDIBUS_SERIAL_GPIO_INIT RS485_DIR_GPIO; + struct { + IDIBUS_SERIAL_GPIO_INIT S0; + IDIBUS_SERIAL_GPIO_INIT S1; + IDIBUS_SERIAL_GPIO_INIT S2; + } BAUDRATE_DIPSW; + } IDIBUS_SERIAL_ALTERNATIVE_INIT_TYPEDEF; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusSerial : public IdiBusCallbackInterface { + + public : + IdiBusSerial(void); + + uint8_t Init(IDIBUS_SERIAL_INIT_TYPEDEF *InitT); + uint8_t Init(IDIBUS_SERIAL_ALTERNATIVE_INIT_TYPEDEF *InitT); + void SetBaudrate(enum IDIBUS_SERIAL_BAUDRATE Baudrate); + void UpdateBaudrateFromDipSwitch(void); + + void Start (void); // Hold Line + void Stop (void); // TX/RX disable, Release Line + + uint8_t SendRequestAsync(uint8_t *_TxBuf_, uint16_t _TxBufLength_); + uint8_t SendRequestSync (uint8_t *_TxBuf_, uint16_t _TxBufLength_); + + uint8_t WriteSync (uint8_t *_TxBuf_, uint16_t _TxBufLength_); + uint8_t WriteAsync(uint8_t *_TxBuf_, uint16_t _TxBufLength_); + + uint8_t IsTransferComplete(void); + uint16_t GetRxBufCounter(void); + IDIBUS_SERIAL_ERROR_STATUS_TYPE GetErrorStatus(void); + uint32_t GetBaudrate(void); + + private : + IDIBUS_SERIAL_USART_HW_TYPE USARTN; + IDIBUS_SERIAL_USARTN_16BIT_TIMER TIMN; + + IDIBUS_SERIAL_GPIO RS485_DIR_GPIO; + IDIBUS_SERIAL_BAUDRATE_DIPSW_TYPE BAUDRATE_DIPSW; + uint32_t BAUDRATE; + + uint8_t TxBuf[IDIBUS_SERIAL_TX_BUF_SIZE]; + uint16_t TxBufLength; + uint16_t TxBufCounter; + + uint8_t RxBuf[IDIBUS_SERIAL_RX_BUF_SIZE]; + uint16_t RxBufCounter; + IDIBUS_SERIAL_ERROR_STATUS_TYPE ERROR_STATUS; + + uint16_t TimerInterFrameCompValue; + uint16_t TimerTimeoutCompValue; + uint16_t TimerTimeoutOverflow; + + enum IDIBUS_SERIAL_REQUEST_TYPE RequestType; + volatile uint8_t TransferComplete; + uint16_t InitProtectionVar; + uint8_t InitState; + + // Interrupt Vectors Pointers + virtual void USART_RxInterruptFunc(void) override; + virtual void USART_TxInterruptFunc(void) override; + virtual void TIM_COMPA_InterruptFunc(void) override; + virtual void TIM_COMPB_InterruptFunc(void) override; + + // Routine + uint8_t BaseInit(enum IDIBUS_SERIAL_USARTN_NUMBER USART_NUM, enum IDIBUS_SERIAL_TIMER_NUMBER TIMER_NUM, enum IDIBUS_SERIAL_USARTN_CONFIG CONF); + uint8_t GetGpioParams(IDIBUS_SERIAL_GPIO_INIT *InitSTR, IDIBUS_SERIAL_GPIO *gpio); + uint8_t GetGpioParamsArduino(uint8_t PinNum, IDIBUS_SERIAL_GPIO *gpio); + void SetBaudrateLL(uint32_t Baudrate); + + protected : + uint8_t *getTxBufPointer (void); + uint8_t *getRxBufPointer (void); + uint8_t SendRequestAsync(uint16_t DataLength); + uint8_t SendRequestSync(uint16_t DataLength); + uint8_t WriteAsync(uint16_t DataLength); + uint8_t WriteSync(uint16_t DataLength); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //_IDIBUS_SERIAL_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSerialDefs.h b/libraries/idiBusMaster/src/IdiBusSerialDefs.h new file mode 100644 index 0000000..778d4d8 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSerialDefs.h @@ -0,0 +1,138 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_SERIAL_COMDEFS_H_ +#define _IDIBUS_SERIAL_COMDEFS_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSystem.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#if defined (__AVR_ATmega2560__) +#define USARTN_UCSRNA_RXC_Pos 7U +#define USARTN_UCSRNA_TXC_Pos 6U +#define USARTN_UCSRNA_UDR_Pos 5U +#define USARTN_UCSRNA_FE_Pos 4U +#define USARTN_UCSRNA_DOR_Pos 3U +#define USARTN_UCSRNA_UPE_Pos 2U +#define USARTN_UCSRNA_U2X_Pos 1U +#define USARTN_UCSRNA_MPCM_Pos 0U + +#define USARTN_UCSRNB_RXCIE_Pos 7U +#define USARTN_UCSRNB_TXCIE_Pos 6U +#define USARTN_UCSRNB_UDRIE_Pos 5U +#define USARTN_UCSRNB_RXEN_Pos 4U +#define USARTN_UCSRNB_TXEN_Pos 3U +#define USARTN_UCSRNB_UCSZ2_Pos 2U +#define USARTN_UCSRNB_RXB8_Pos 1U +#define USARTN_UCSRNB_TXB8_Pos 0U + +#define USARTN_UCSRNC_UMSEL1 7U +#define USARTN_UCSRNC_UMSEL0 6U +#define USARTN_UCSRNC_UPM1 5U +#define USARTN_UCSRNC_UPM0 4U +#define USARTN_UCSRNC_USBS 3U +#define USARTN_UCSRNC_UCSZ1 2U +#define USARTN_UCSRNC_UCSZ0 1U +#define USARTN_UCSRNC_UCPOL 0U + +#define USARTN_TIM_TCCRA_WGMN1_Pos 1U + +#define USARTN_TIM_TCCRB_PR1 1U +#define USARTN_TIM_TCCRB_PR8 2U +#define USARTN_TIM_TCCRB_PR64 3U +#define USARTN_TIM_TCCRB_PR256 4U +#define USARTN_TIM_TCCRB_PR1024 5U + +#define USRTN_TIM_TIMSK_TOIE_Pos 0U +#define USRTN_TIM_TIMSK_OCIEA_Pos 1U +#define USRTN_TIM_TIMSK_OCIEB_Pos 2U +#define USRTN_TIM_TIMSK_OCIEC_Pos 3U + +#define USARTN_TIM_TIFR_TOV_Pos 0U +#define USARTN_TIM_TIFR_OCFA_Pos 1U +#define USARTN_TIM_TIFR_OCFB_Pos 2U +#define USARTN_TIM_TIFR_OCFC_Pos 3U + +#endif //#if defined (__AVR_ATmega2560__) +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_BAUDRATE_DSW_CODE_19200B 0x00 +#define IDIBUS_BAUDRATE_DSW_CODE_500K 0x01 +#define IDIBUS_BAUDRATE_DSW_CODE_2400B 0x02 +#define IDIBUS_BAUDRATE_DSW_CODE_9600B 0x03 +#define IDIBUS_BAUDRATE_DSW_CODE_115200B 0x04 +#define IDIBUS_BAUDRATE_DSW_CODE_250K 0x05 +#define IDIBUS_BAUDRATE_DSW_CODE_1M 0x06 +#define IDIBUS_BAUDRATE_DSW_CODE_10M 0x07 +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_SERIAL_TIMER_PR_REG USARTN_TIM_TCCRB_PR64 + +#if IDIBUS_SERIAL_TIMER_PR_REG == USARTN_TIM_TCCRB_PR1 +#define IDIBUS_SERIAL_TIMER_PR_VALUE 1U +#elif IDIBUS_SERIAL_TIMER_PR_REG == USARTN_TIM_TCCRB_PR8 +#define IDIBUS_SERIAL_TIMER_PR_VALUE 8U +#elif IDIBUS_SERIAL_TIMER_PR_REG == USARTN_TIM_TCCRB_PR64 +#define IDIBUS_SERIAL_TIMER_PR_VALUE 64U +#elif IDIBUS_SERIAL_TIMER_PR_REG == USARTN_TIM_TCCRB_PR256 +#define IDIBUS_SERIAL_TIMER_PR_VALUE 256U +#elif IDIBUS_SERIAL_TIMER_PR_REG == USARTN_TIM_TCCRB_PR1024 +#define IDIBUS_SERIAL_TIMER_PR_VALUE 1024U +#else +#error "Idibus Serial: Incorrect Timer Prescaler value !" +#endif //#if IDIBUS_T_CPU==CPU_ATmega2560 +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_SERIAL_TX_BUF_SIZE IDIMMES_MAX_MES_SIZE +#define IDIBUS_SERIAL_RX_BUF_SIZE IDISMES_MAX_MES_SIZE +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +enum IDIBUS_SERIAL_REQUEST_TYPE { + _IDIBUS_SERIAL_TX_RX_, + _IDIBUS_SERIAL_TX_, + //Alarm + }; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + volatile uint8_t *UCSRNA; + volatile uint8_t *UCSRNB; + volatile uint8_t *UCSRNC; + volatile uint16_t *UBRRN; + volatile uint8_t *UDRN; + } IDIBUS_SERIAL_USART_HW_TYPE; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + volatile uint8_t *TCCRNA; + volatile uint8_t *TCCRNB; + volatile uint8_t *TIMSKN; + volatile uint8_t *TIFRN; + volatile uint16_t *OCRNA; + volatile uint16_t *OCRNB; + volatile uint16_t *TCNTN; + } IDIBUS_SERIAL_USARTN_16BIT_TIMER; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef union { + uint8_t SumVar; + struct { + uint8_t TimeoutERR : 1; + uint8_t TxOverflow : 1; + uint8_t RxOverflow : 1; + uint8_t LLComERR : 1; + uint8_t TxZerolength : 1; + uint8_t Reserved : 3; + } BF; + } IDIBUS_SERIAL_ERROR_STATUS_TYPE; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + uint8_t PIN_MASK; + volatile uint8_t *PORT; + volatile uint8_t *DDR; + volatile uint8_t *PIN; + } IDIBUS_SERIAL_GPIO; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + uint8_t PIN_NUM; + volatile uint8_t *PORT; + } IDIBUS_SERIAL_GPIO_INIT; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +typedef struct { + IDIBUS_SERIAL_GPIO S0_Gpio; + IDIBUS_SERIAL_GPIO S1_Gpio; + IDIBUS_SERIAL_GPIO S2_Gpio; + } IDIBUS_SERIAL_BAUDRATE_DIPSW_TYPE; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //_IDIBUS_SERIAL_COMDEFS_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSerialLine.cpp b/libraries/idiBusMaster/src/IdiBusSerialLine.cpp new file mode 100644 index 0000000..8d9d6a9 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSerialLine.cpp @@ -0,0 +1,122 @@ +//############################################################################################################################################################################################################# +#include "IdiBusSerialLine.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusSerialLine::IdiBusSerialLine(void) : IdiBusSerial() +{ +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerialLine::sendMMES (IdiBusMMES *MMES) +{ + if ( MMES->IsValid() == 0 ) { return IDIERMST_INVALID_TX_REQUEST_FORMAT; } + + uint8_t *TxMesBuf = this->getTxBufPointer(); + if ( TxMesBuf == NULL ) { return IDIERMST_TX_MES; } + + uint8_t *RxMesBuf = this->getRxBufPointer(); + uint16_t DataPos; + + // Fill Header + TxMesBuf[IDIMMES_ADDR_Pos] = MMES->SlAddr; + TxMesBuf[IDIMMES_MMPS_Pos] = (MMES->MMPS.LongMes << IDIMMES_MMPS_LONG_MES_Pos) | + (IDIMMES_MMPS_MES_TYPE_MMES << IDIMMES_MMPS_MES_TYPE_Pos) | + (0x00U << IDIMMES_MMPS_FAST_FUNC_Pos) | + (MMES->MMPS.AlarmMes << IDIMMES_MMPS_ALARM_FRAME_Pos) | + (MMES->MMPS.EncryptedAes << IDIMMES_MMPS_ENCRYPTED_AES_Pos); + TxMesBuf[IDIMMES_DEV_Pos] = (MMES->DEV.NUM << IDIMMES_DEV_NUM_Pos) | (MMES->DEV.ALLCH << IDIMMES_DEV_ALLCH_Pos); + TxMesBuf[IDIMMES_CHNL_Pos] = (MMES->CHNL.NUM << IDIMMES_CHNL_NUM_Pos) | (MMES->CHNL.ALLSAME << IDIMMES_CHNL_ALLSAME_Pos); + DataPos = IDIMMES_CHNL_Pos + 1; + + // If Not Fast Function add command/function + if ( MMES->ComFunc < 16 ) { TxMesBuf[IDIMMES_MMPS_Pos] |= (MMES->ComFunc << IDIMMES_MMPS_FAST_FUNC_Pos); } + else { TxMesBuf[DataPos++] = MMES->ComFunc; } + + // Fill Data + if ( MMES->TxDataLength != 0 ) { memcpy(&TxMesBuf[DataPos], &MMES->TxData[0], MMES->TxDataLength); } + + // Add CRC + uint16_t CRC = MODBUS_CRC16::CRC16_TF(&TxMesBuf[0], (uint16_t)(DataPos + MMES->TxDataLength)); + TxMesBuf[DataPos + MMES->TxDataLength] = (uint8_t)(CRC >> 8); + TxMesBuf[DataPos + MMES->TxDataLength + 1] = (uint8_t) CRC; + + if ( this->SendRequestSync(DataPos + MMES->TxDataLength + MODBUS_CRC16_SIZE) == 0 ) { return IDIERMST_TX_MES; } + + IDIBUS_SERIAL_ERROR_STATUS_TYPE SerialError = this->GetErrorStatus(); + uint16_t RxMessageLength; + + if ( SerialError.BF.TimeoutERR != 0 ) { return IDIERMST_RCV_TIMEOUT; } + else if ( (SerialError.SumVar != 0) || ((RxMessageLength = this->GetRxBufCounter()) < IDISMES_MIN_MES_SIZE) ) { return IDIERMST_MES_RX_INTEGRITY; } + else + { + uint16_t CRC = MODBUS_CRC16::CRC16_TF( &RxMesBuf[0], (uint16_t)(RxMessageLength-MODBUS_CRC16_SIZE) ); + uint16_t ReceivedCRC = ((uint16_t)RxMesBuf[RxMessageLength-2] << 8) | RxMesBuf[RxMessageLength-1]; + if ( CRC != ReceivedCRC ) { return IDIERMST_CRC; } + else if ( RxMesBuf[IDISMES_ADDR_Pos] != MMES->SlAddr ) { return IDIERMST_INVALID_ADDR_NUM; } + else + { + MMES->RxDataLength = RxMessageLength - (IDISMES_ERROR_Pos + MODBUS_CRC16_SIZE); + MMES->RxData = &RxMesBuf[IDISMES_ERROR_Pos]; + return IDIER_NOPE; + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSerialLine::sendMMESG (IdiBusMMESG *MMESG) +{ + if ( MMESG->IsValid() == 0 ) { return IDIERMST_INVALID_TX_REQUEST_FORMAT ; } + + uint8_t *TxMesBuf = this->getTxBufPointer(); + if ( TxMesBuf == NULL ) { return IDIERMST_TX_MES; } + + uint8_t *RxMesBuf = this->getRxBufPointer(); + uint16_t DataPos; + + // Fill Header + TxMesBuf[IDIMMES_ADDR_Pos] = MMESG->SlAddr; + TxMesBuf[IDIMMES_MMPS_Pos] = (MMESG->MMPS.LongMes << IDIMMES_MMPS_LONG_MES_Pos) | + (IDIMMES_MMPS_MES_TYPE_MMESG << IDIMMES_MMPS_MES_TYPE_Pos) | + (0x00 << IDIMMES_MMPS_FAST_FUNC_Pos) | + (MMESG->MMPS.AlarmMes << IDIMMES_MMPS_ALARM_FRAME_Pos) | + (MMESG->MMPS.EncryptedAes << IDIMMES_MMPS_ENCRYPTED_AES_Pos); + DataPos = IDIMMES_MMPS_Pos + 1; + // If Not Fast Function add command/function + if ( MMESG->ComFunc < 16 ) { TxMesBuf[IDIMMES_MMPS_Pos] |= (MMESG->ComFunc << IDIMMES_MMPS_FAST_FUNC_Pos); } + else { TxMesBuf[DataPos++] = MMESG->ComFunc; } + + // Fill Data + if ( MMESG->TxDataLength != 0 ) { memcpy(&TxMesBuf[DataPos], &MMESG->TxData[0], MMESG->TxDataLength); } + + // Add CRC + uint16_t CRC = MODBUS_CRC16::CRC16_TF(&TxMesBuf[0], (uint16_t)(DataPos + MMESG->TxDataLength)); + TxMesBuf[DataPos + MMESG->TxDataLength] = (uint8_t)(CRC >> 8); + TxMesBuf[DataPos + MMESG->TxDataLength + 1] = (uint8_t) CRC; + + if ( MMESG->IsGroupAddr() != 0 ) + { + if ( this->WriteSync(DataPos + MMESG->TxDataLength + MODBUS_CRC16_SIZE) == 0 ) { return IDIERMST_TX_MES; } + return IDIER_NOPE; + } + else //Module commands + { + if ( this->SendRequestSync(DataPos + MMESG->TxDataLength + MODBUS_CRC16_SIZE) == 0 ) { return IDIERMST_TX_MES; } + + uint16_t RxMessageLength; + IDIBUS_SERIAL_ERROR_STATUS_TYPE SerialError = this->GetErrorStatus(); + + if ( SerialError.BF.TimeoutERR != 0 ) { return IDIERMST_RCV_TIMEOUT; } + else if ( (SerialError.SumVar != 0) || ((RxMessageLength = this->GetRxBufCounter()) < IDISMES_MIN_MES_SIZE) ) { return IDIERMST_TX_MES; } + else + { + uint16_t CRC = MODBUS_CRC16::CRC16_TF( &RxMesBuf[0], (uint16_t)(RxMessageLength-MODBUS_CRC16_SIZE) ); + uint16_t ReceivedCRC = ((uint16_t)RxMesBuf[RxMessageLength-2] << 8) | RxMesBuf[RxMessageLength-1]; + if ( CRC != ReceivedCRC ) { return IDIERMST_CRC; } + else if ( RxMesBuf[IDISMES_ADDR_Pos] != MMESG->SlAddr ) { return IDIERMST_INVALID_ADDR_NUM; } + { + MMESG->RxDataLength = RxMessageLength - (IDISMES_ERROR_Pos + MODBUS_CRC16_SIZE); + MMESG->RxData = &RxMesBuf[IDISMES_ERROR_Pos]; + return IDIER_NOPE; + } + } + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSerialLine.h b/libraries/idiBusMaster/src/IdiBusSerialLine.h new file mode 100644 index 0000000..2bcc69a --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSerialLine.h @@ -0,0 +1,17 @@ +//###################################################################################################################################################################################################### +#ifndef _IDIBUS_STD_LINE_H_ +#define _IDIBUS_STD_LINE_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +#include "IdiBusSystem.h" +#include "IdiBusSerial.h" +#include "IdiBusMes.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +class IdiBusSerialLine : public IdiBusSerial { + public : + IdiBusSerialLine(); + uint8_t sendMMES (IdiBusMMES *MMES); // return ErrorCode but not look into [data] + uint8_t sendMMESG (IdiBusMMESG *MMESG); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ +#endif //#define _IDIBUS_STD_LINE_H_ +//###################################################################################################################################################################################################### diff --git a/libraries/idiBusMaster/src/IdiBusSlave.cpp b/libraries/idiBusMaster/src/IdiBusSlave.cpp new file mode 100644 index 0000000..5188341 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSlave.cpp @@ -0,0 +1,351 @@ +//############################################################################################################################################################################################################# +#include "IdiBusSlave.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusSlave::IdiBusSlave( IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress ) +{ + this->Line = SlaveLine; + this->Address = SlaveAddress; + this->CRC_AVR.CRC_ErrAvrBufCounter = 0; + this->CRC_AVR.CRC_ErrAvrBufPos = 0; + this->CRC_AVR.CRC_ErrAvrBufSum = 0; + this->CRC_MultipleError = 0; + this->ModuleError = IDIER_NOPE; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusSlave::IdiBusSlave(void) +{ + this->Line = NULL; + this->Address = 1; + this->CRC_AVR.CRC_ErrAvrBufCounter = 0; + this->CRC_AVR.CRC_ErrAvrBufPos = 0; + this->CRC_AVR.CRC_ErrAvrBufSum = 0; + this->CRC_MultipleError = 0; + this->ModuleError = IDIER_NOPE; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSlave::Init(IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress) +{ + this->Line = SlaveLine; + this->Address = SlaveAddress; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::SendRequestMMES(IdiBusMMES *MMES) +{ + if ( this->Line == NULL ) { return IDIERMST_TX_MES; } + uint8_t RequestError = this->Line->sendMMES(MMES); + this->CrcAvrErrorCalc(RequestError); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if ( MMES->IsModuleError(MMES->RxData[0]) ) + { + if ( MMES->RxDataLength != 1 ) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { this->ModuleError = MMES->RxData[0]; return MMES->RxData[0]; } + } + this->ModuleError = IDIER_NOPE; + return IDIER_NOPE; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::SendRequestMMESG(IdiBusMMESG *MMESG) +{ + if ( this->Line == NULL ) { return IDIERMST_TX_MES; } + uint8_t RequestError = this->Line->sendMMESG(MMESG); + this->CrcAvrErrorCalc(RequestError); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if ( MMESG->IsGroupAddr() == 0 ) + { + if ( MMESG->IsModuleError(MMESG->RxData[0]) ) + { + if ( MMESG->RxDataLength != 1 ) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { this->ModuleError = MMESG->RxData[0]; return MMESG->RxData[0]; } + } + } + this->ModuleError = IDIER_NOPE; + return IDIER_NOPE; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::SendModuleSimpleSetCommand(uint8_t Command) +{ + IdiBusMMESG MMESG(this->Address); + MMESG.ComFunc = Command; + uint8_t RequestError = this->SendRequestMMESG(&MMESG); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if (MMESG.RxDataLength != 1) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { return MMESG.RxData[0]; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::SendModuleSimpleSetCommand(uint8_t Command, uint8_t *TxData, uint16_t TxDataLength) +{ + IdiBusMMESG MMESG(this->Address, TxData, TxDataLength); + MMESG.ComFunc = Command; + uint8_t RequestError = this->SendRequestMMESG(&MMESG); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if (MMESG.RxDataLength != 1) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { return MMESG.RxData[0]; } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSlave::CrcAvrErrorCalc(uint8_t ErrorCode) +{ + uint8_t ErrorBufCode; + if ( ErrorCode == IDIERMST_CRC ) { ErrorBufCode = 1; } + else { ErrorBufCode = 0; } + + this->CRC_AVR.CRC_ErrAvrBufSum = this->CRC_AVR.CRC_ErrAvrBufSum + ErrorBufCode - this->CRC_AVR.CRC_ErrAvrBuf[this->CRC_AVR.CRC_ErrAvrBufPos]; + this->CRC_AVR.CRC_ErrAvrBuf[this->CRC_AVR.CRC_ErrAvrBufPos] = ErrorBufCode; + if ( this->CRC_AVR.CRC_ErrAvrBufPos == (IDIER_MULTIPLE_CRC_AVRBUF_SIZE - 1) ) { this->CRC_AVR.CRC_ErrAvrBufPos = 0; } + else { this->CRC_AVR.CRC_ErrAvrBufPos++; } + + if ( this->CRC_AVR.CRC_ErrAvrBufCounter < IDIER_MULTIPLE_CRC_AVRBUF_SIZE ) { this->CRC_AVR.CRC_ErrAvrBufCounter++; } + else + { + if ( this->CRC_AVR.CRC_ErrAvrBufSum >= IDIER_MULTIPLE_CRC_AVRBUF_THR ) { this->CRC_MultipleError = 1; } + else { this->CRC_MultipleError = 0; } + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::c_Init(void) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_Init); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_Init(uint8_t Group) { return (Group > IDIBUS_GROUP_LAST_NUMBER) ? IDIERMST_INVALID_TX_PARAM : this->SendModuleSimpleSetCommand(IDIMMES_COM_C_Init, &Group, 1); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_ShtDown(void) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_ShtDown); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_Freeze(void) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_Freeze); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_Resume(void) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_Resume); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_DummyModule(void) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_DummyModule); } +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::c_SendTimeDate(IdiTimeFormat Time, IdiDateFormat Date) +{ + if ( (Time.isValid() == 0) || (Date.isValid() == 0) ) { return IDIERMST_INVALID_TX_PARAM; } + uint8_t TxData[IDIMMES_C_DATETIME_LENGTH]; + Time.getBufForMMES(&TxData[IDIMMES_C_DATETIME_TIME_Pos]); + Date.getBufForMMES(&TxData[IDIMMES_C_DATETIME_DATE_Pos]); + return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_SendTimeDate, TxData, IDIMMES_C_DATETIME_LENGTH ); +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_ReadDevFullSN_MS(void) +{ + IdiBusMMESG MMESG (this->Address); + MMESG.ComFunc = IDIMMES_COM_C_ReadDevFullSN_MS; + uint8_t RequestError = this->SendRequestMMESG(&MMESG); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if ( MMESG.RxDataLength != (IDISN_FULL_LENGTH - IDISN_VARP_AES256_Length + 2 + 1) ) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else if ( MMESG.RxData[0] != IDIER_NOPE ) { return MMESG.RxData[0]; } + else + { + this->ModuleST.parseSmesDataNoAes(&MMESG.RxData[1]); + return IDIER_NOPE; + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_WriteSnIPv4IPv6(uint32_t IPv4, uint32_t IPv6_B3, uint32_t IPv6_B2, uint32_t IPv6_B1, uint32_t IPv6_B0) +{ + uint8_t TxData[IDISN_VARP_IPv4_Length + IDISN_VARP_IPv6_Length]; + TxData[0] = (uint8_t)(IPv4 >> 24); + TxData[1] = (uint8_t)(IPv4 >> 16); + TxData[2] = (uint8_t)(IPv4 >> 8); + TxData[3] = (uint8_t)(IPv4); + TxData[4] = (uint8_t)(IPv6_B3 >> 24); + TxData[5] = (uint8_t)(IPv6_B3 >> 16); + TxData[6] = (uint8_t)(IPv6_B3 >> 8); + TxData[7] = (uint8_t)(IPv6_B3); + TxData[8] = (uint8_t)(IPv6_B2 >> 24); + TxData[9] = (uint8_t)(IPv6_B2 >> 16); + TxData[10] = (uint8_t)(IPv6_B2 >> 8); + TxData[11] = (uint8_t)(IPv6_B2); + TxData[12] = (uint8_t)(IPv6_B1 >> 24); + TxData[13] = (uint8_t)(IPv6_B1 >> 16); + TxData[14] = (uint8_t)(IPv6_B1 >> 8); + TxData[15] = (uint8_t)(IPv6_B1); + TxData[16] = (uint8_t)(IPv6_B0 >> 24); + TxData[17] = (uint8_t)(IPv6_B0 >> 16); + TxData[18] = (uint8_t)(IPv6_B0 >> 8); + TxData[19] = (uint8_t)(IPv6_B0); + uint8_t RequestError = this->SendModuleSimpleSetCommand(IDIMMES_COM_C_WriteSnIPv4IPv6, TxData, IDISN_VARP_IPv4_Length + IDISN_VARP_IPv6_Length ); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else + { + memcpy(this->ModuleST.SN.VAR.IPv4, &TxData[0], IDISN_VARP_IPv4_Length); + memcpy(this->ModuleST.SN.VAR.IPv6, &TxData[4], IDISN_VARP_IPv6_Length); + return IDIER_NOPE; + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_WriteSnVerifyDates(IdiDateFormat VDate, IdiDateFormat VEXPDate) +{ + if ( (VDate.isValid() == 0) || (VEXPDate.isValid() == 0) ) { return IDIERMST_INVALID_TX_PARAM; } + uint8_t TxData[IDIDATE_FORMAT_LENGTH * 2]; + VDate.getBufForMMES(&TxData[0]); + VEXPDate.getBufForMMES(&TxData[IDIDATE_FORMAT_LENGTH]); + uint8_t RequestError = this->SendModuleSimpleSetCommand(IDIMMES_COM_C_WriteSnVerifyDates, TxData, IDIDATE_FORMAT_LENGTH * 2 ); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else + { + this->ModuleST.SN.VAR.VerifDate = VDate; + this->ModuleST.SN.VAR.VerifExpDate = VEXPDate; + return IDIER_NOPE; + } +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_WriteSnAES256(uint8_t *AES256_key) { return this->SendModuleSimpleSetCommand(IDIMMES_COM_C_WriteSnAES256, AES256_key, IDISN_VARP_AES256_Length); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_CheckModuleLongOp(void) +{ + IdiBusMMESG MMESG (this->Address); + MMESG.ComFunc = IDIMMES_COM_C_CheckModuleLongOp; + uint8_t RequestError = this->SendRequestMMESG(&MMESG); + if ( RequestError != IDIER_NOPE ) { return RequestError; } + else if ( MMESG.RxDataLength != (IDILONGOP_MES_DATA_LENGTH + 1) ) { return IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else if ( MMESG.RxData[0] != IDIER_NOPE ) { return MMESG.RxData[0]; } + else + { + uint8_t DataPos = 1; + this->ModuleLongOpData.State = MMESG.RxData[DataPos + IDILONGOP_STATE_Pos]; + this->ModuleLongOpData.Timeout = ((uint32_t)MMESG.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 0] << 24) | + ((uint32_t)MMESG.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 1] << 16) | + ((uint32_t)MMESG.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 2] << 8 ) | + ((uint32_t)MMESG.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 3] ); + return IDIER_NOPE; + } +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::c_Dummy(uint8_t Device, uint8_t Channel) +{ + IdiBusDevice *DEV; + IdiBusChannel *CHNL; + if ( (DEV = this->getDevice(Device)) == NULL ) { return IDIERMST_INVALID_DEV_NUM; } + if ( (CHNL = DEV->getChannel(Channel)) == NULL ) { return IDIERMST_INVALID_CHN_NUM; } + + IdiBusMMES MMES(this->Address); + MMES.ComFunc = IDIMMES_COM_C_Dummy; + MMES.DEV.NUM = Device; + MMES.CHNL.NUM = Channel; + + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { CHNL->MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { CHNL->MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { CHNL->MMES_Error = MMES.RxData[0]; } + return CHNL->MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_AssignGroup(uint8_t Device, uint8_t Channel, uint8_t Group) +{ + IdiBusDevice *DEV; + IdiBusChannel *CHNL; + if ( (DEV = this->getDevice(Device)) == NULL ) { return IDIERMST_INVALID_DEV_NUM; } + if ( (CHNL = DEV->getChannel(Channel)) == NULL ) { return IDIERMST_INVALID_CHN_NUM; } + if ( Group > IDIBUS_GROUP_LAST_NUMBER ) { CHNL->MMES_Error = IDIERMST_INVALID_TX_PARAM; } + else + { + IdiBusMMES MMES(this->Address, &Group, 1); + MMES.ComFunc = IDIMMES_COM_C_AssignGroup; + MMES.DEV.NUM = Device; + MMES.CHNL.NUM = Channel; + + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { CHNL->MMES_Error = RequestError; } + else if ( MMES.RxDataLength != 1 ) { CHNL->MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else { CHNL->MMES_Error = MMES.RxData[0]; } + } + return CHNL->MMES_Error; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +uint8_t IdiBusSlave::c_CheckChannelLongOp(uint8_t Device, uint8_t Channel) +{ + IdiBusDevice *DEV; + IdiBusChannel *CHNL; + if ( (DEV = this->getDevice(Device)) == NULL ) { return IDIERMST_INVALID_DEV_NUM; } + if ( (CHNL = DEV->getChannel(Channel)) == NULL ) { return IDIERMST_INVALID_CHN_NUM; } + + IdiBusMMES MMES(this->Address); + MMES.ComFunc = IDIMMES_COM_C_CheckChannelLongOp; + MMES.DEV.NUM = Device; + MMES.CHNL.NUM = Channel; + + uint8_t RequestError = this->SendRequestMMES(&MMES); + if ( RequestError != IDIER_NOPE ) { CHNL->MMES_Error = RequestError; } + else if ( MMES.RxDataLength != (IDILONGOP_MES_DATA_LENGTH + 1) ) { CHNL->MMES_Error = IDIERMST_INVALID_RX_REQUEST_FORMAT; } + else if ( MMES.RxData[0] != IDIER_NOPE ) { CHNL->MMES_Error = MMES.RxData[0]; } + else + { + CHNL->MMES_Error = IDIER_NOPE; + uint8_t DataPos = 1; + CHNL->LONG_OP.State = MMES.RxData[DataPos + IDILONGOP_STATE_Pos]; + CHNL->LONG_OP.Timeout = ((uint32_t)MMES.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 0] << 24) | + ((uint32_t)MMES.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 1] << 16) | + ((uint32_t)MMES.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 2] << 8 ) | + ((uint32_t)MMES.RxData[DataPos + IDILONGOP_REMAIN_TIME_Pos + 3] ); + } + return CHNL->MMES_Error; +} +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +uint8_t IdiBusSlave::getAddress(void) { return this->Address; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +void IdiBusSlave::setAes256ToSn(uint8_t *AES256_key) { memcpy(this->ModuleST.SN.VAR.AES, AES256_key, IDISN_VARP_AES256_Length); } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusLongOpData IdiBusSlave::getModuleLongOpData(void) { return this->ModuleLongOpData; } +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiBusLongOpData IdiBusSlave::getChLongOpData(uint8_t Device, uint8_t Channel) +{ + IdiBusDevice *DEV; + IdiBusChannel *CHNL; + if ( ((DEV = this->getDevice(Device)) != NULL) && ((CHNL = DEV->getChannel(Channel)) != NULL) ) + { + return CHNL->LONG_OP; + } + IdiBusLongOpData NewData; + return NewData; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +IdiMasterState IdiBusSlave::getModuleSN(void) { return this->ModuleST; } +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +void IdiMasterState::parseSmesDataNoAes(uint8_t *Data) +{ + this->STATE.B0S.StError = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_ST_ERROR_Pos) & 0x01; + this->STATE.B0S.StState = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_ST_STATE_Pos) & IDISTATUS_B0S_ST_STATE_Msk; + this->STATE.B0S.AesSupported = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_AES_SUPPORTED_Pos) & 0x01; + this->STATE.B0S.AesInstalled = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_AES_INSTALLED_Pos) & 0x01; + this->STATE.B0S.SendAlarmL0 = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_SEND_ALARM_L0_Pos) & 0x01; + this->STATE.B0S.SendAlarmL1 = (Data[IDISTATUS_B0S_Pos] >> IDISTATUS_B0S_SEND_ALARM_L1_Pos) & 0x01; + + this->STATE.B1S.ModuleType = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_MODULE_TYPE_Pos) & IDISTATUS_B1S_MODULE_TYPE_Msk; + this->STATE.B1S.BridgeConnected = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_BRIDGE_CONNECTED_Pos) & 0x01; + this->STATE.B1S.SelfInit = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_SELF_INIT_Pos) & 0x01; + this->STATE.B1S.TimeoutLed = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_TIMEOUT_LED_Pos) & 0x01; + this->STATE.B1S.NoMMESTimeout = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_NO_MMES_TIMEOUT_Pos) & 0x01; + this->STATE.B1S.CatchAlarmL0 = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_CATCH_ALARM_L0_Pos) & 0x01; + this->STATE.B1S.CatchAlarmL1 = (Data[IDISTATUS_B1S_Pos] >> IDISTATUS_B1S_CATCH_ALARM_L1_Pos) & 0x01; + + memcpy(this->SN.FIX.GS1_country, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_GS1_COUNTRY_Pos], IDISN_FIXP_GS1_COUNTRY_Length); + memcpy(this->SN.FIX.GS1_company, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_GS1_COMPANY_Pos], IDISN_FIXP_GS1_COMPANY_Length); + memcpy(this->SN.FIX.ModuleType, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_MODULE_TYPE_Pos], IDISN_FIXP_MODULE_TYPE_Length); + memcpy(this->SN.FIX.HW_rev, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_HW_REV_Pos], IDISN_FIXP_HW_REV_Length); + memcpy(this->SN.FIX.Serial, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_SERIAL_Pos], IDISN_FIXP_SERIAL_Length); + memcpy(this->SN.FIX.MAC, &Data[IDISTATUS_SN_Pos + IDISN_FIXP_MAC_Pos], IDISN_FIXP_MAC_Length); + memcpy(this->SN.VAR.SW_rev, &Data[IDISTATUS_SN_Pos + IDISN_VARP_SW_REV_Pos], IDISN_VARP_SW_REV_Length); + memcpy(this->SN.VAR.IPv4, &Data[IDISTATUS_SN_Pos + IDISN_VARP_IPv4_Pos], IDISN_VARP_IPv4_Length); + memcpy(this->SN.VAR.IPv6, &Data[IDISTATUS_SN_Pos + IDISN_VARP_IPv6_Pos], IDISN_VARP_IPv6_Length); + + uint8_t *VerifDateP = &Data[IDISTATUS_SN_Pos + IDISN_VARP_VERIF_DATE_Pos]; + uint8_t *VerifExpDateP = &Data[IDISTATUS_SN_Pos + IDISN_VARP_EXPIR_DATE_Pos]; + IdiDateFormat verifDate(VerifDateP[0], VerifDateP[1], VerifDateP[2], VerifDateP[3]); + IdiDateFormat verifExpDate(VerifExpDateP[0], VerifExpDateP[1], VerifExpDateP[2], VerifExpDateP[3]); + this->SN.VAR.VerifDate = verifDate; + this->SN.VAR.VerifExpDate = verifExpDate; +} +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSlave.h b/libraries/idiBusMaster/src/IdiBusSlave.h new file mode 100644 index 0000000..731bf28 --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSlave.h @@ -0,0 +1,122 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_SLAVE_H_ +#define _IDIBUS_SLAVE_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "IdiBusSystem.h" +#include "IdiBusDateTime.h" +#include "IdiBusMes.h" +#include "IdiBusSerialLine.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiMasterState { + public : + IDISTATUS_STATE_TYPE STATE; + struct { + struct { + uint8_t GS1_country[3]; + uint8_t GS1_company[6]; + uint8_t ModuleType[3]; + uint8_t HW_rev[2]; + uint8_t Serial[7]; + uint8_t MAC[6]; + } FIX; + struct VAR { + VAR() : VerifDate(0,0,0,0), VerifExpDate(0,0,0,0) {} + uint8_t SW_rev[2]; + IdiDateFormat VerifDate; + IdiDateFormat VerifExpDate; + uint8_t IPv4[4]; + uint8_t IPv6[16]; + uint8_t AES[32]; + } VAR; + } SN; + void parseSmesDataNoAes(uint8_t *Data); +}; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +class IdiBusLongOpData { + public : + IdiBusLongOpData() { this->State = IDILONGOP_STATE_COMPLETE_NO_ERR; this->Timeout = 0; } + uint8_t State; + uint32_t Timeout; +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusChannel { + public : + IdiBusChannel() { this->MMES_Error = IDIER_NOPE; } + IdiBusLongOpData LONG_OP; + uint8_t MMES_Error; +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +class IdiBusDevice { + public : + IdiBusDevice (void *ParentModule) { this->Module = ParentModule; } + virtual IdiBusChannel *getChannel(uint8_t Num) { return NULL; } + protected : + void *Module; +}; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +class IdiBusSlave { + + public : + // Constructors and init + IdiBusSlave(IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); + IdiBusSlave(void); + void Init(IdiBusSerialLine *SlaveLine, uint8_t SlaveAddress); + + // Standard communication API + uint8_t SendRequestMMES(IdiBusMMES *MMES); + uint8_t SendRequestMMESG(IdiBusMMESG *MMESG); + + //Module commands (return ErrorCode) + uint8_t c_Init (void); + uint8_t c_Init (uint8_t Gpoup); + uint8_t c_ShtDown (void); + uint8_t c_Freeze (void); + uint8_t c_Resume (void); + uint8_t c_ReadDevFullSN_MS(void); + uint8_t c_WriteSnIPv4IPv6(uint32_t IPv4, uint32_t IPv6_B3, uint32_t IPv6_B2, uint32_t IPv6_B1, uint32_t IPv6_B0); + uint8_t c_WriteSnVerifyDates(IdiDateFormat VDate, IdiDateFormat VEXPDate); + uint8_t c_WriteSnAES256(uint8_t *AES256_key); + uint8_t c_SendTimeDate(IdiTimeFormat Time, IdiDateFormat Date); + uint8_t c_DummyModule(void); + uint8_t c_CheckModuleLongOp(void); + + // Channel Commands + uint8_t c_Dummy(uint8_t Device, uint8_t Channel); + uint8_t c_AssignGroup(uint8_t Device, uint8_t Channel, uint8_t Group); + uint8_t c_CheckChannelLongOp(uint8_t Device, uint8_t Channel); + + // Utils + uint8_t getAddress(void); + void setAes256ToSn(uint8_t *AES256_key); + IdiBusLongOpData getModuleLongOpData(void); + IdiBusLongOpData getChLongOpData(uint8_t Device, uint8_t Channel); + IdiMasterState getModuleSN(void); + + // Interface - must be overridden in derived classes + virtual IdiBusDevice *getDevice(uint8_t Num) { return NULL; } + + protected : + IdiBusSerialLine *Line; + uint8_t Address; + IdiMasterState ModuleST; + uint8_t ModuleError; + IdiBusLongOpData ModuleLongOpData; + uint8_t CRC_MultipleError; + struct { + uint8_t CRC_ErrAvrBuf[16]; + uint8_t CRC_ErrAvrBufCounter; + uint8_t CRC_ErrAvrBufPos; + uint8_t CRC_ErrAvrBufSum; + } CRC_AVR; + + uint8_t SendModuleSimpleSetCommand(uint8_t Command); + uint8_t SendModuleSimpleSetCommand(uint8_t Command, uint8_t *TxData, uint16_t TxDataLength); + void CrcAvrErrorCalc(uint8_t ErrorCode); +}; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _IDIBUS_SLAVE_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdiBusSystem.h b/libraries/idiBusMaster/src/IdiBusSystem.h new file mode 100644 index 0000000..979d5ff --- /dev/null +++ b/libraries/idiBusMaster/src/IdiBusSystem.h @@ -0,0 +1,25 @@ +//############################################################################################################################################################################################################# +#ifndef _IDIBUS_SYSTEM_H_ +#define _IDIBUS_SYSTEM_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include "Arduino.h" +#include "pins_arduino.h" + +#include +#include +#include +#include +#include "IdiBusDefs.h" +#include "MODBUS_CRC.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#ifndef F_CPU +#error "IdibusSerial: F_CPU not defined!" +#endif + +#if defined __AVR_ATmega2560__ +#else +#error "IdibusSerial: Unsupported ControllerType!" +#endif +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#ifndef _IDIBUS_SYSTEM_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/IdibusDefs.h b/libraries/idiBusMaster/src/IdibusDefs.h new file mode 100644 index 0000000..eb625e5 --- /dev/null +++ b/libraries/idiBusMaster/src/IdibusDefs.h @@ -0,0 +1,446 @@ +//############################################################################################################################################################################################################# +#ifndef _INC_IDIBUS_DEFS_H_ +#define _INC_IDIBUS_DEFS_H_ +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#include +#include "MODBUS_CRC.h" +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDIBUS_BAUDRATE_DSW_CODE_19200B 0x00 +#define IDIBUS_BAUDRATE_DSW_CODE_500K 0x01 +#define IDIBUS_BAUDRATE_DSW_CODE_2400B 0x02 +#define IDIBUS_BAUDRATE_DSW_CODE_9600B 0x03 +#define IDIBUS_BAUDRATE_DSW_CODE_115200B 0x04 +#define IDIBUS_BAUDRATE_DSW_CODE_250K 0x05 +#define IDIBUS_BAUDRATE_DSW_CODE_1M 0x06 +#define IDIBUS_BAUDRATE_DSW_CODE_10M 0x07 + +// Full timeout will be (InterframeTimeout + ResponseTimeout) for request(Write + Read) or InterframeTimeout for write(Write only) +#define IDIBUS_2400B_INTERFRAME_TIMEOUT_US 16042ULL //11 * 3.5 / Baudrate +#define IDIBUS_9600B_INTERFRAME_TIMEOUT_US 4011ULL +#define IDIBUS_19200B_INTERFRAME_TIMEOUT_US 2006ULL +#define IDIBUS_115200B_INTERFRAME_TIMEOUT_US 1750ULL +#define IDIBUS_250K_INTERFRAME_TIMEOUT_US 1750ULL +#define IDIBUS_500K_INTERFRAME_TIMEOUT_US 1750ULL +#define IDIBUS_1M_INTERFRAME_TIMEOUT_US 1750ULL +#define IDIBUS_10M_INTERFRAME_TIMEOUT_US 1750ULL + +#define IDIBUS_2400B_ALARM_TIMEOUT_US 34375ULL //11 * 3.5 / Baudrate +#define IDIBUS_9600B_ALARM_TIMEOUT_US 8594ULL +#define IDIBUS_19200B_ALARM_TIMEOUT_US 4297ULL +#define IDIBUS_115200B_ALARM_TIMEOUT_US 1750ULL +#define IDIBUS_250K_ALARM_TIMEOUT_US 1750ULL +#define IDIBUS_500K_ALARM_TIMEOUT_US 1750ULL +#define IDIBUS_1M_ALARM_TIMEOUT_US 1750ULL +#define IDIBUS_10M_ALARM_TIMEOUT_US 1750ULL + +#define IDIBUS_2400B_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_2400B_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_9600B_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_9600B_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_19200B_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_19200B_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_115200B_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_115200B_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_250K_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_250K_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_500K_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_500K_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_1M_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_1M_INTERFRAME_TIMEOUT_US * 3 / 2 ) +#define IDIBUS_10M_MASTER_RESPONSE_TIMEOUT_US (IDIBUS_10M_INTERFRAME_TIMEOUT_US * 3 / 2 ) + +#define IDIBUS_2400B_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_2400B_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_9600B_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_9600B_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_19200B_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_19200B_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_115200B_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_115200B_INTERFRAME_TIMEOUT_US) +#define IDIBUS_250K_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_250K_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_500K_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_500K_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_1M_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_1M_INTERFRAME_TIMEOUT_US ) +#define IDIBUS_10M_SLAVE_RESPONSE_TIMEOUT_US (IDIBUS_10M_INTERFRAME_TIMEOUT_US ) + + +enum IDIBUS_SERIAL_BAUDRATE { + IDIBUS_BAUDRATE_2400 = IDIBUS_BAUDRATE_DSW_CODE_2400B, + IDIBUS_BAUDRATE_9600 = IDIBUS_BAUDRATE_DSW_CODE_9600B, + IDIBUS_BAUDRATE_19200 = IDIBUS_BAUDRATE_DSW_CODE_19200B, + IDIBUS_BAUDRATE_115200 = IDIBUS_BAUDRATE_DSW_CODE_115200B, + IDIBUS_BAUDRATE_250K = IDIBUS_BAUDRATE_DSW_CODE_250K, + IDIBUS_BAUDRATE_500K = IDIBUS_BAUDRATE_DSW_CODE_500K, + IDIBUS_BAUDRATE_1M = IDIBUS_BAUDRATE_DSW_CODE_1M, + IDIBUS_BAUDRATE_10M = IDIBUS_BAUDRATE_DSW_CODE_10M +}; + +#define IDIBUS_LINK_LED_NO_MMES_TIMEOUT_0_SEC 60U +#define IDIBUS_LINK_LED_NO_MMES_TIMEOUT_0_MS ( IDIBUS_LINK_LED_NO_MMES_TIMEOUT_0_SEC * 1000U ) +#define IDIBUS_LINK_LED_NO_MMES_TIMEOUT_1_SEC 15U +#define IDIBUS_LINK_LED_NO_MMES_TIMEOUT_1_MS ( IDIBUS_LINK_LED_NO_MMES_TIMEOUT_1_SEC * 1000U ) + +enum IDIBUS_RXTIMER_TIMEOUT_MODE { + IDIBUS_TIMER_MODE_RX_TIMEOUT = 0x00, + IDIBUS_TIMER_MODE_ALARM_TIMEOUT, + IDIBUS_TIMER_MODE_RESPONSE_TIMEOUT +}; +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDISN_FIXP_Pos 0U +#define IDISN_FIXP_GS1_COUNTRY_Pos ( IDISN_FIXP_Pos + 0U) +#define IDISN_FIXP_GS1_COUNTRY_Length 3 +#define IDISN_FIXP_GS1_COMPANY_Pos ( IDISN_FIXP_GS1_COUNTRY_Pos + IDISN_FIXP_GS1_COUNTRY_Length ) +#define IDISN_FIXP_GS1_COMPANY_Length 6 +#define IDISN_FIXP_MODULE_TYPE_Pos ( IDISN_FIXP_GS1_COMPANY_Pos + IDISN_FIXP_GS1_COMPANY_Length ) +#define IDISN_FIXP_MODULE_TYPE_Length 3 +#define IDISN_FIXP_HW_REV_Pos ( IDISN_FIXP_MODULE_TYPE_Pos + IDISN_FIXP_MODULE_TYPE_Length ) +#define IDISN_FIXP_HW_REV_Length 2 +#define IDISN_FIXP_SERIAL_Pos ( IDISN_FIXP_HW_REV_Pos + IDISN_FIXP_HW_REV_Length ) +#define IDISN_FIXP_SERIAL_Length 7 +#define IDISN_FIXP_MAC_Pos ( IDISN_FIXP_SERIAL_Pos + IDISN_FIXP_SERIAL_Length ) +#define IDISN_FIXP_MAC_Length 6 +#define IDISN_FIXP_LENGTH ( IDISN_FIXP_GS1_COUNTRY_Length + IDISN_FIXP_GS1_COMPANY_Length + IDISN_FIXP_MODULE_TYPE_Length + \ +IDISN_FIXP_HW_REV_Length + IDISN_FIXP_SERIAL_Length + IDISN_FIXP_MAC_Length ) + +#define IDISN_VARP_Pos IDISN_FIXP_LENGTH +#define IDISN_VARP_SW_REV_Pos ( IDISN_VARP_Pos + 0U ) +#define IDISN_VARP_SW_REV_Length 2U +#define IDISN_VARP_VERIF_DATE_Pos ( IDISN_VARP_SW_REV_Pos + IDISN_VARP_SW_REV_Length ) +#define IDISN_VARP_VERIF_DATE_Length 4U +#define IDISN_VARP_EXPIR_DATE_Pos ( IDISN_VARP_VERIF_DATE_Pos + IDISN_VARP_VERIF_DATE_Length ) +#define IDISN_VARP_EXPIR_DATE_Length 4U +#define IDISN_VARP_IPv4_Pos ( IDISN_VARP_EXPIR_DATE_Pos + IDISN_VARP_EXPIR_DATE_Length ) +#define IDISN_VARP_IPv4_Length 4U +#define IDISN_VARP_IPv6_Pos ( IDISN_VARP_IPv4_Pos + IDISN_VARP_IPv4_Length ) +#define IDISN_VARP_IPv6_Length 16U +#define IDISN_VARP_AES256_Pos ( IDISN_VARP_IPv6_Pos + IDISN_VARP_IPv6_Length ) +#define IDISN_VARP_AES256_Length 32U +#define IDISN_VARP_LENGTH ( IDISN_VARP_SW_REV_Length + IDISN_VARP_VERIF_DATE_Length + IDISN_VARP_EXPIR_DATE_Length + \ +IDISN_VARP_IPv4_Length + IDISN_VARP_IPv6_Length + IDISN_VARP_AES256_Length ) + +#define IDISN_FULL_LENGTH ( IDISN_FIXP_LENGTH + IDISN_VARP_LENGTH ) +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define ISIBUS_MASTER_MAIN_ADDR 255U +#define ISIBUS_MASTER_SPARE_ADDR 254U +#define IDIBUS_SLAVE_ADDR_MIN 1U +#define IDIBUS_SLAVE_ADDR_MAX 229U +#define IDIBUS_DEVELOPER_ADDR_0 250U +#define IDIBUS_DEVELOPER_ADDR_1 251U +#define IDIBUS_DEVELOPER_ADDR_2 252U +#define IDIBUS_DEVELOPER_ADDR_3 253U +#define IDIBUS_GROUP_0_ADDR 230U +#define IDIBUS_GROUP_1_ADDR 231U +#define IDIBUS_GROUP_2_ADDR 232U +#define IDIBUS_GROUP_3_ADDR 233U +#define IDIBUS_GROUP_4_ADDR 234U +#define IDIBUS_GROUP_5_ADDR 235U +#define IDIBUS_GROUP_6_ADDR 236U +#define IDIBUS_GROUP_7_ADDR 237U +#define IDIBUS_GROUP_8_ADDR 238U +#define IDIBUS_GROUP_9_ADDR 239U +#define IDIBUS_GROUP_10_ADDR 240U +#define IDIBUS_GROUP_11_ADDR 241U +#define IDIBUS_GROUP_12_ADDR 242U +#define IDIBUS_GROUP_13_ADDR 243U +#define IDIBUS_GROUP_14_ADDR 244U +#define IDIBUS_GROUP_15_ADDR 245U + +#define IDIBUS_GROUPS_NUMBER 16U +#define IDIBUS_GROUP_FIRST_NUMBER 0U +#define IDIBUS_GROUP_LAST_NUMBER 15U +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDIMMES_ADDR_Pos 00U +#define IDIMMES_MMPS_Pos 01U +#define IDIMMES_MMPS_LONG_MES_Pos 0U +#define IDIMMES_MMPS_LONG_MES_Msk 0x01U +#define IDIMMES_MMPS_MES_TYPE_Pos 1U +#define IDIMMES_MMPS_MES_TYPE_Msk 0x02U +#define IDIMMES_MMPS_MES_TYPE_MMES 0x00U +#define IDIMMES_MMPS_MES_TYPE_MMESG 0x01U +#define IDIMMES_MMPS_FAST_FUNC_Pos 2U +#define IDIMMES_MMPS_FAST_FUNC_Msk 0x3CU +#define IDIMMES_MMPS_ALARM_FRAME_Pos 6U +#define IDIMMES_MMPS_ALARM_FRAME_Msk 0x40U +#define IDIMMES_MMPS_ENCRYPTED_AES_Pos 7U +#define IDIMMES_MMPS_ENCRYPTED_AES_Msk 0x80U +#define IDIMMES_DEV_Pos 02U +#define IDIMMES_DEV_NUM_Pos 0U +#define IDIMMES_DEV_NUM_Msk 0x1FU +#define IDIMMES_DEV_ALLCH_Pos 5U +#define IDIMMES_DEV_ALLCH_Msk 0x20U +#define IDIMMES_CHNL_Pos 03U +#define IDIMMES_CHNL_NUM_Pos 0U +#define IDIMMES_CHNL_NUM_Msk 0x7FU +#define IDIMMES_CHNL_ALLSAME_Pos 7U +#define IDIMMES_CHNL_ALLSAME_Msk 0x80U +#define IDIMMES_DATA_FUNC_COM_DATA_Pos 04U +#define IDIMMES_MAX_HEADER_LENGTH (IDIMMES_DATA_FUNC_COM_DATA_Pos + 1) +#define IDIMMES_MAX_DATA_SIZE 256U +#define IDIMMES_MAX_MES_SIZE (IDIMMES_MAX_DATA_SIZE + IDIMMES_MAX_HEADER_LENGTH + MODBUS_CRC16_SIZE) +#define IDIMMES_MIN_MES_SIZE (IDIMMES_DATA_FUNC_COM_DATA_Pos + MODBUS_CRC16_SIZE) + +#define IDIMMESG_DATA_COM_FUNC_Pos 02U +#define IDIMMESG_MAX_HEADER_LENGTH (IDIMMESG_DATA_COM_FUNC_Pos + 1) +#define IDIMMESG_MAX_DATA_SIZE IDIMMES_MAX_DATA_SIZE +#define IDIMMESG_MAX_MES_SIZE (IDIMMESG_MAX_DATA_SIZE + IDIMMESG_MAX_HEADER_LENGTH + MODBUS_CRC16_SIZE) +#define IDIMMESG_MODULE_MIN_MES_SIZE (IDIMMESG_DATA_COM_FUNC_Pos + 1 + MODBUS_CRC16_SIZE) +#define IDIMMESG_GROUP_MIN_MES_SIZE (IDIMMESG_DATA_COM_FUNC_Pos + MODBUS_CRC16_SIZE) + +#define IDIMMES_LMES_MSIZE_Pos 0U +#define IDIMMES_LMES_BSIZE_Pos (IDIMMES_LMES_MSIZE_Pos + 4U) +#define IDIMMES_LMES_IDENTIFIER_LENGTH (IDIMMES_LMES_BSIZE_Pos + 1U) +#define IDIMMES_LMES_BSIZE_256B 0U +#define IDIMMES_LMES_BSIZE_1K 1U +#define IDIMMES_LMES_BSIZE_4K 2U +#define IDIMMES_LMES_BSIZE_8K 3U +#define IDIMMES_LMES_BSIZE_16K 4U +#define IDIMMES_LMES_BSIZE_32K 5U +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDISMES_ADDR_Pos 00U +#define IDISMES_SMPS_Pos 01U +#define IDISMES_SMPS_ERROR_BIT_Pos 0U +#define IDISMES_SMPS_LONG_MES_Pos 1U +#define IDISMES_SMPS_LONG_OP_Pos 2U +#define IDISMES_ERROR_Pos 02U +#define IDISMES_DATA_Pos 03U +#define IDISMES_MAX_DATA_SIZE 256U +#define IDISMES_MIN_MES_SIZE (IDISMES_DATA_Pos + MODBUS_CRC16_SIZE) +#define IDISMES_MAX_MES_SIZE (IDISMES_DATA_Pos + IDISMES_MAX_DATA_SIZE + MODBUS_CRC16_SIZE) +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDILONGOP_STATE_COMPLETE_NO_ERR 0x00U +#define IDILONGOP_STATE_IN_PROC 0x01U +#define IDILONGOP_STATE_COMPLETE_WITH_ERR 0x02U +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#define IDILONGOP_MES_DATA_LENGTH 5U +#define IDILONGOP_STATE_Pos 0U +#define IDILONGOP_REMAIN_TIME_Pos 1U +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDISTATUS_B0S_Pos 00U +#define IDISTATUS_B0S_ST_ERROR_Pos 0U +#define IDISTATUS_B0S_ST_STATE_Pos 1U +#define IDISTATUS_B0S_ST_STATE_Msk 0x07U +#define IDISTATUS_B0S_ST_STATE_StNoInit 0x00U +#define IDISTATUS_B0S_ST_STATE_StOperate 0x01U +#define IDISTATUS_B0S_ST_STATE_StFreeze 0x02U +#define IDISTATUS_B0S_ST_STATE_StVirtual 0x03U +#define IDISTATUS_B0S_ST_STATE_StFirmwareUpd 0x04U +#define IDISTATUS_B0S_ST_STATE_StReservedMaster 0x05U +#define IDISTATUS_B0S_ST_STATE_StBroken 0x06U +#define IDISTATUS_B0S_ST_STATE_StReserved0 0x07U +#define IDISTATUS_B0S_AES_SUPPORTED_Pos 4U +#define IDISTATUS_B0S_AES_INSTALLED_Pos 5U +#define IDISTATUS_B0S_SEND_ALARM_L0_Pos 6U +#define IDISTATUS_B0S_SEND_ALARM_L1_Pos 7U + +#define IDISTATUS_B1S_Pos 01U +#define IDISTATUS_B1S_MODULE_TYPE_Pos 0U +#define IDISTATUS_B1S_MODULE_TYPE_Msk 0x03U +#define IDISTATUS_B1S_MODULE_TYPE_Master 0x00U +#define IDISTATUS_B1S_MODULE_TYPE_SpareMaster 0x01U +#define IDISTATUS_B1S_MODULE_TYPE_Slave 0x02U +#define IDISTATUS_B1S_MODULE_TYPE_Bridge 0x03U +#define IDISTATUS_B1S_BRIDGE_CONNECTED_Pos 2U +#define IDISTATUS_B1S_SELF_INIT_Pos 3U +#define IDISTATUS_B1S_TIMEOUT_LED_Pos 4U +#define IDISTATUS_B1S_NO_MMES_TIMEOUT_Pos 5U +#define IDISTATUS_B1S_CATCH_ALARM_L0_Pos 6U +#define IDISTATUS_B1S_CATCH_ALARM_L1_Pos 7U + +#define IDISTATUS_SN_Pos 02U +#define IDISTATUS_LENGTH ( IDISTATUS_SN_Pos + IDISN_FULL_LENGTH ) +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDIMMES_NOT_FAST_FUNC 0U +#define IDIMMES_MAX_FAST_FUNC_NUM 15U +#define IDIMMES_COM_START_NUM 220U +#define IDIMMES_COM_C_Init 220U +#define IDIMMES_COM_C_ShtDown 221U +#define IDIMMES_COM_C_Freeze 222U +#define IDIMMES_COM_C_Resume 223U +#define IDIMMES_COM_C_Dummy 224U +#define IDIMMES_COM_C_AssignGroup 225U +#define IDIMMES_COM_C_SetAlarmL12 226U +#define IDIMMES_COM_C_SetAlarmL 227U +#define IDIMMES_COM_C_Virtual 228U +#define IDIMMES_COM_C_SyncReadChnl 229U +#define IDIMMES_COM_C_SyncRead 230U +#define IDIMMES_COM_C_SyncDoChnl 231U +#define IDIMMES_COM_C_SyncDo 232U +#define IDIMMES_COM_C_SyncClear 233U +#define IDIMMES_COM_C_BurstReadCnt 234U +#define IDIMMES_COM_C_BurstReadTime 235U +#define IDIMMES_COM_C_SendTimeDate 236U +#define IDIMMES_COM_C_MkTimedMaster 237U +#define IDIMMES_COM_C_FmwUpd 238U +#define IDIMMES_COM_C_EndFmwUpd 239U +#define IDIMMES_COM_C_FmwWrite 240U +#define IDIMMES_COM_C_ReadDevFullSN_MS 241U +#define IDIMMES_COM_C_WriteSnIPv4IPv6 242U +#define IDIMMES_COM_C_WriteSnVerifyDates 243U +#define IDIMMES_COM_C_WriteSnAES256 244U +#define IDIMMES_COM_C_SendLongMessage 245U +#define IDIMMES_COM_C_GetLondMessage 246U +#define IDIMMES_COM_C_DummyModule 247U +#define IDIMMES_COM_C_CheckModuleLongOp 248U +#define IDIMMES_COM_C_CheckChannelLongOp 249U +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDIER_MODBUS_NUM_START 1U +#define IDIER_MODBUS_NUM_END 9U +#define IDIER_MODULE_MASTER_NUM_START 10U +#define IDIER_MODULE_MASTER_NUM_END 32U +#define IDIER_MODULE_SLAVE_NUM_START 33U +#define IDIER_MODULE_SLAVE_NUM_END 71U +#define IDIER_MODULE_NUM_START IDIER_MODULE_MASTER_NUM_START +#define IDIER_MODULE_NUM_END IDIER_MODULE_SLAVE_NUM_END +#define IDIER_DEVICE_NUM_START 72U +#define IDIER_DEVICE_NUM_END 89U + +#define IDIER_NOPE 0U +#define MODBUSER_WRONGFUNC 1U +#define MODBUSER_WRONGADDR 2U +#define MODBUSER_WRONGDATA 3U +#define MODBUSER_BROKE 4U +#define MODBUSER_LONGCOMMAND 5U +#define MODBUSER_BUSY 6U +#define MODBUSER_CANTDOFUNC 7U +#define MODBUSER_EXTMEMORYERROR 8U +#define MODBUSER_RESERVED 9U +#define IDIERMST_INVALID_ADDR_NUM 10U +#define IDIERMST_INVALID_DEV_NUM 11U +#define IDIERMST_INVALID_CHN_NUM 12U +#define IDIERMST_INVALID_FUNC_NUM 13U +#define IDIERMST_INVALID_TX_REQUEST_FORMAT 14U +#define IDIERMST_INVALID_TX_PARAM 15U +#define IDIERMST_TX_MES 16U +#define IDIERMST_RCV_TIMEOUT 17U +#define IDIERMST_MES_RX_INTEGRITY 18U +#define IDIERMST_CRC 19U +#define IDIERMST_MULTIPLE_CRC 20U +#define IDIERMST_INVALID_RX_REQUEST_FORMAT 21U +#define IDIERMST_INVALID_RX_PARAM 22U +#define IDIERMST_RESEVED_23 23U +#define IDIERMST_RESEVED_24 24U +#define IDIERMST_RESEVED_25 25U +#define IDIERMST_RESEVED_26 26U +#define IDIERMST_RESEVED_27 27U +#define IDIERMST_EXTRA_28 28U +#define IDIERMST_EXTRA_29 29U +#define IDIERMST_EXTRA_30 30U +#define IDIERMST_EXTRA_31 31U +#define IDIERMST_EXTRA_32 32U +#define IDIERSLV_RESEVED_33 33U +#define IDIERSLV_ENCRYPTION_NOT_SUPPORTED 34U +#define IDIERSLV_ENCRYPTION_NOT_INSTALLED 35U +#define IDIERSLV_JUMBO_NOT_SUPPORTED 36U +#define IDIERSLV_UNSUPPORTED_FUNC_NUM 37U +#define IDIERSLV_INVALID_RX_REQUEST_FORMAT 38U +#define IDIERSLV_INVALID_RX_PARAM 39U +#define IDIERSLV_IN_FREEZE 40U +#define IDIERSLV_RESERVED_41 41U +#define IDIERSLV_RESERVED_42 42U +#define IDIERSLV_RESERVED_43 43U +#define IDIERSLV_RESERVED_44 44U +#define IDIERSLV_RESERVED_45 45U +#define IDIERSLV_EXTRA_46 46U +#define IDIERSLV_EXTRA_47 47U +#define IDIERSLV_EXTRA_48 48U +#define IDIERSLV_EXTRA_49 49U +#define IDIERSLV_EXTRA_50 50U +#define IDIERSLV_BROKE 51U +#define IDIERSLV_NO_FIRMWARE 52U +#define IDIERSLV_NO_INIT 53U +#define IDIERSLV_OVERHEAT 54U +#define IDIERSLV_INP_VOLTAGE 55U +#define IDIERSLV_BRIDGE_OVERFLOW 56U +#define IDIERSLV_BRIDGE_NOT_CONF 57U +#define IDIERSLV_VERIF_DATE 58U +#define IDIERSLV_RTC 59U +#define IDIERSLV_LONG_OP_IN_PROC 60U +#define IDIERSLV_RESERVED_61 61U +#define IDIERSLV_RESERVED_62 62U +#define IDIERSLV_RESERVED_63 63U +#define IDIERSLV_RESERVED_64 64U +#define IDIERSLV_RESERVED_65 65U +#define IDIERSLV_RESERVED_66 66U +#define IDIERSLV_EXTRA_67 67U +#define IDIERSLV_EXTRA_68 68U +#define IDIERSLV_EXTRA_69 69U +#define IDIERSLV_EXTRA_70 70U +#define IDIERSLV_EXTRA_71 71U +#define IDIERDEV_INVALID_DEV_NUM 72U +#define IDIERDEV_INVALID_CHN_NUM 73U +#define IDIERDEV_INVALID_FUNC_NUM 74U +#define IDIERDEV_LONG_OP_IN_PROC 75U +#define IDIERDEV_RESERVED_76 76U +#define IDIERDEV_PARAM_LOW_ST_TIMEOUT 77U +#define IDIERDEV_PARAM_HIGH_ST_TIMEOUT 78U +#define IDIERDEV_PARAM_NOT_CHANGE_TIMEOUT 79U +#define IDIERDEV_RESERVED_80 80U +#define IDIERDEV_RESERVED_81 81U +#define IDIERDEV_RESERVED_82 82U +#define IDIERDEV_RESERVED_83 83U +#define IDIERDEV_RESERVED_84 84U +#define IDIERDEV_RESERVED_85 85U +#define IDIERDEV_RESERVED_86 86U +#define IDIERDEV_RESERVED_87 87U +#define IDIERDEV_RESERVED_88 88U +#define IDIERDEV_RESERVED_89 89U + +#define IDIER_MULTIPLE_CRC_AVRBUF_SIZE 16U +#define IDIER_MULTIPLE_CRC_AVRBUF_THR 5U +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +#define IDIDATE_FORMAT_DAY_Pos 0U +#define IDIDATE_FORMAT_MONTH_Pos 1U +#define IDIDATE_FORMAT_CENTURY_Pos 2U +#define IDIDATE_FORMAT_YEAR99_Pos 3U +#define IDIDATE_FORMAT_LENGTH 4U + +#define IDITIME_FORMAT_SECONDS_Pos 0U +#define IDITIME_FORMAT_MINUTES_Pos 1U +#define IDITIME_FORMAT_HOURS_Pos 2U +#define IDITIME_FORMAT_TIMEZONE_Pos 3U +#define IDITIME_FORMAT_LENGTH 4U + +#define IDITIME_FORMAT_TIMEZONE_MIN (-12) +#define IDITIME_FORMAT_TIMEZONE_MAX 14 + +#define IDIMMES_C_DATETIME_TIME_Pos 0 +#define IDIMMES_C_DATETIME_DATE_Pos (IDIMMES_C_DATETIME_TIME_Pos + IDIDATE_FORMAT_LENGTH) +#define IDIMMES_C_DATETIME_LENGTH (IDIDATE_FORMAT_LENGTH + IDITIME_FORMAT_LENGTH) +//============================================================================================================================================================================================================= +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//============================================================================================================================================================================================================= +typedef struct { + struct { + uint8_t StError : 1; + uint8_t StState : 3; + uint8_t AesSupported : 1; + uint8_t AesInstalled : 1; + uint8_t SendAlarmL0 : 1; + uint8_t SendAlarmL1 : 1; + } B0S; + struct { + uint8_t ModuleType : 2; + uint8_t BridgeConnected : 1; + uint8_t SelfInit : 1; + uint8_t TimeoutLed : 1; + uint8_t NoMMESTimeout : 1; + uint8_t CatchAlarmL0 : 1; + uint8_t CatchAlarmL1 : 1; + } B1S; +} IDISTATUS_STATE_TYPE; +//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------s +typedef struct { + IDISTATUS_STATE_TYPE STATE; + uint8_t SN[IDISN_FULL_LENGTH]; +} IDISTATUS_SLAVE_TYPE; +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //_INC_IDIBUS_DEFS_H_ +//############################################################################################################################################################################################################# diff --git a/libraries/idiBusMaster/src/MODBUS_CRC.cpp b/libraries/idiBusMaster/src/MODBUS_CRC.cpp new file mode 100644 index 0000000..ef339dd --- /dev/null +++ b/libraries/idiBusMaster/src/MODBUS_CRC.cpp @@ -0,0 +1,134 @@ +//################################################################################################################################################### +#include "MODBUS_CRC.h" +//--------------------------------------------------------------------------------------------------------------------------------------------------- +// Table of CRC values for high–order byte FLASH Variant +const uint8_t MODBUS_auchCRCHi_PR[] PROGMEM = { +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40 +}; +// Table of CRC values for low–order byte +const uint8_t MODBUS_auchCRCLo_PR[] PROGMEM = { +0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, +0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, +0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, +0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, +0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, +0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, +0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, +0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, +0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, +0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, +0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, +0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, +0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, +0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, +0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, +0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, +0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, +0x40 +}; +//--------------------------------------------------------------------------------------------------------------------------------------------------- +// Table of CRC values for high–order byte RAM Variant +uint8_t MODBUS_auchCRCHi_R[] = { +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, +0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, +0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, +0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, +0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, +0x40 +}; +// Table of CRC values for low–order byte +uint8_t MODBUS_auchCRCLo_R[] = { +0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, +0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, +0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, +0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, +0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, +0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, +0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, +0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, +0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, +0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, +0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, +0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, +0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, +0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, +0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, +0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, +0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, +0x40 +}; +//--------------------------------------------------------------------------------------------------------------------------------------------------- +uint16_t MODBUS_CRC16::CRC16_TF(uint8_t *MODBUS_puchMsg, uint16_t MODBUS_usDataLen) +{ + uint8_t MODBUS_uchCRCHi = 0xFF; // high byte of CRC initialized + uint8_t MODBUS_uchCRCLo = 0xFF; // low byte of CRC initialized + uint8_t MODBUS_uIndex; + while (MODBUS_usDataLen--) + { + MODBUS_uIndex = MODBUS_uchCRCLo ^ (*MODBUS_puchMsg++); // calculate the CRC + MODBUS_uchCRCLo = MODBUS_uchCRCHi ^ pgm_read_byte(&MODBUS_auchCRCHi_PR[MODBUS_uIndex]); + MODBUS_uchCRCHi = pgm_read_byte(&MODBUS_auchCRCLo_PR[MODBUS_uIndex]); + } + return ( ((uint16_t)MODBUS_uchCRCHi << 8 ) | MODBUS_uchCRCLo ); +} +//--------------------------------------------------------------------------------------------------------------------------------------------------- +uint16_t MODBUS_CRC16::CRC16_TR(uint8_t *MODBUS_puchMsg, uint16_t MODBUS_usDataLen) +{ + uint8_t MODBUS_uchCRCHi = 0xFF; // high byte of CRC initialized + uint8_t MODBUS_uchCRCLo = 0xFF; // low byte of CRC initialized + uint8_t MODBUS_uIndex; + while (MODBUS_usDataLen--) + { + MODBUS_uIndex = MODBUS_uchCRCLo ^ (*MODBUS_puchMsg++); // calculate the CRC + MODBUS_uchCRCLo = MODBUS_uchCRCHi ^ MODBUS_auchCRCHi_R[MODBUS_uIndex]; + MODBUS_uchCRCHi = MODBUS_auchCRCLo_R[MODBUS_uIndex]; + } + return (((uint16_t)MODBUS_uchCRCHi << 8) | MODBUS_uchCRCLo); +} +//--------------------------------------------------------------------------------------------------------------------------------------------------- +uint16_t MODBUS_CRC16::CRC16_S(uint8_t *MODBUS_buf, uint16_t MODBUS_len) +{ + uint16_t MODBUS_crc = 0xFFFF; + for (uint16_t MODBUS_pos = 0; MODBUS_pos < MODBUS_len; MODBUS_pos++) + { + MODBUS_crc ^= MODBUS_buf[MODBUS_pos]; // XOR byte into least sig. byte of crc + for (uint8_t MODBUS_i = 8; MODBUS_i != 0; MODBUS_i--) // Loop over each bit + { + if ((MODBUS_crc & 0x0001) != 0) { MODBUS_crc >>= 1; MODBUS_crc ^= 0xA001; } // If the LSB is set // Shift right and XOR 0xA001 + else { MODBUS_crc >>= 1; } // Else LSB is not set // Just shift right + } + } + + return MODBUS_crc; +} +//--------------------------------------------------------------------------------------------------------------------------------------------------- +//################################################################################################################################################### diff --git a/libraries/idiBusMaster/src/MODBUS_CRC.h b/libraries/idiBusMaster/src/MODBUS_CRC.h new file mode 100644 index 0000000..703e77d --- /dev/null +++ b/libraries/idiBusMaster/src/MODBUS_CRC.h @@ -0,0 +1,20 @@ +//################################################################################################################################################### +#ifndef _MODBUS_CRC_H_ +#define _MODBUS_CRC_H_ +//--------------------------------------------------------------------------------------------------------------------------------------------------- +#include "avr/io.h" +#include +#include +//--------------------------------------------------------------------------------------------------------------------------------------------------- +#define MODBUS_CRC16_SIZE 2 +//--------------------------------------------------------------------------------------------------------------------------------------------------- +class MODBUS_CRC16 { + public : + static uint16_t CRC16_TF(uint8_t *MODBUS_puchMsg, uint16_t MODBUS_usDataLen); + static uint16_t CRC16_TR(uint8_t *MODBUS_puchMsg, uint16_t MODBUS_usDataLen); + static uint16_t CRC16_S(uint8_t *MODBUS_buf, uint16_t MODBUS_len); + private : + }; +//--------------------------------------------------------------------------------------------------------------------------------------------------- +#endif //#define _CRC_MODBUS_ +//################################################################################################################################################### \ No newline at end of file diff --git a/package_idibus_index.json b/package_idibus_index.json new file mode 100644 index 0000000..660802c --- /dev/null +++ b/package_idibus_index.json @@ -0,0 +1,38 @@ +{ + "packages": [ + { + "name": "idiBus", + "maintainer": "OLIMP LLC", + "websiteURL": "https://olimp-z.ru/", + "email": "office@olimp-z.ru", + "platforms": [ + { + "name": "idiBus Master Board", + "architecture": "avr", + "version": "1.0.0", + "category": "idiBus", + "url": "idiBus-Master-Board-1.0.0.zip", + "archiveFileName": "idiBus-Master-Board-1.0.0.zip", + "checksum": "SHA-256:1bfe7707894907a2792884fc726e1fe9251842fac75eaf5ef451bba0c18cbb4e", + "size": "92824", + "boards": [ + {"name": "idiBus Master Board"} + ], + "toolsDependencies": [ + { + "packager": "arduino", + "name": "avr-gcc", + "version": "4.8.1-arduino5" + }, + { + "packager": "arduino", + "name": "avrdude", + "version": "6.0.1-arduino5" + } + ] + } + ], + "tools": [] + } + ] +} \ No newline at end of file diff --git a/programmers.txt b/programmers.txt new file mode 100644 index 0000000..40532f5 --- /dev/null +++ b/programmers.txt @@ -0,0 +1,130 @@ +avrisp.name=AVR ISP +avrisp.communication=serial +avrisp.protocol=stk500v1 +avrisp.program.protocol=stk500v1 +avrisp.program.tool=avrdude +avrisp.program.tool.default=avrdude +avrisp.program.extra_params=-P{serial.port} + +avrispmkii.name=AVRISP mkII +avrispmkii.communication=usb +avrispmkii.protocol=stk500v2 +avrispmkii.program.protocol=stk500v2 +avrispmkii.program.tool=avrdude +avrispmkii.program.tool.default=avrdude +avrispmkii.program.extra_params=-Pusb + +usbtinyisp.name=USBtinyISP +usbtinyisp.protocol=usbtiny +usbtinyisp.program.tool=avrdude +usbtinyisp.program.tool.default=avrdude +usbtinyisp.program.extra_params= + +arduinoisp.name=ArduinoISP +arduinoisp.protocol=arduinoisp +arduinoisp.program.tool=avrdude +arduinoisp.program.tool.default=avrdude +arduinoisp.program.extra_params= + +arduinoisporg.name=ArduinoISP.org +arduinoisporg.protocol=arduinoisporg +arduinoisporg.program.tool=avrdude +arduinoisporg.program.tool.default=avrdude +arduinoisporg.program.extra_params= + +usbasp.name=USBasp +usbasp.communication=usb +usbasp.protocol=usbasp +usbasp.program.protocol=usbasp +usbasp.program.tool=avrdude +usbasp.program.tool.default=avrdude +usbasp.program.extra_params=-Pusb + +parallel.name=Parallel Programmer +parallel.protocol=dapa +parallel.force=true +# parallel.delay=200 +parallel.program.tool=avrdude +parallel.program.tool.default=avrdude +parallel.program.extra_params=-F + +arduinoasisp.name=Arduino as ISP +arduinoasisp.communication=serial +arduinoasisp.protocol=stk500v1 +arduinoasisp.speed=19200 +arduinoasisp.program.protocol=stk500v1 +arduinoasisp.program.speed=19200 +arduinoasisp.program.tool=avrdude +arduinoasisp.program.tool.default=avrdude +arduinoasisp.program.extra_params=-P{serial.port} -b{program.speed} + +arduinoasispatmega32u4.name=Arduino as ISP (ATmega32U4) +arduinoasispatmega32u4.communication=serial +arduinoasispatmega32u4.protocol=arduino +arduinoasispatmega32u4.speed=19200 +arduinoasispatmega32u4.program.protocol=arduino +arduinoasispatmega32u4.program.speed=19200 +arduinoasispatmega32u4.program.tool=avrdude +arduinoasispatmega32u4.program.tool.default=avrdude +arduinoasispatmega32u4.program.extra_params=-P{serial.port} -b{program.speed} + +usbGemma.name=Arduino Gemma +usbGemma.protocol=arduinogemma +usbGemma.program.tool=avrdude +usbGemma.program.tool.default=avrdude +usbGemma.program.extra_params= +usbGemma.config.path={runtime.platform.path}/bootloaders/gemma/avrdude.conf + +## Notes about Dangerous Prototypes Bus Pirate as ISP +## Bus Pirate V3 need Firmware v5.10 or later +## Bus Pirate V4 need Firmware v6.3-r2151 or later +## Could happen that BP does not have enough current to power an Arduino board +## through the ICSP connector. In this case disconnect the +Vcc from ICSP connector +## and power Arduino board in the normal way. +buspirate.name=BusPirate as ISP +buspirate.communication=serial +buspirate.protocol=buspirate +buspirate.program.protocol=buspirate +buspirate.program.tool=avrdude +buspirate.program.tool.default=avrdude +buspirate.program.extra_params=-P{serial.port} + +# STK500 firmware version v1 and v2 use different serial protocols. +# Using the 'stk500' protocol tells avrdude to try and autodetect the +# firmware version. If this leads to problems, we might need to add +# stk500v1 and stk500v2 entries to allow explicitly selecting the +# firmware version. +stk500.name=Atmel STK500 development board +stk500.communication=serial +stk500.protocol=stk500 +stk500.program.protocol=stk500 +stk500.program.tool=avrdude +stk500.program.tool.default=avrdude +stk500.program.extra_params=-P{serial.port} + +jtag3isp.name=Atmel JTAGICE3 (ISP mode) +jtag3isp.communication=usb +jtag3isp.protocol=jtag3isp +jtag3isp.program.protocol=jtag3isp +jtag3isp.program.tool=avrdude +jtag3isp.program.tool.default=avrdude +jtag3isp.program.extra_params= + +jtag3.name=Atmel JTAGICE3 (JTAG mode) +jtag3.communication=usb +jtag3.protocol=jtag3 +jtag3.program.protocol=jtag3 +jtag3.program.tool=avrdude +jtag3.program.tool.default=avrdude +# Set a bitclock of 0.1us (the fastest supported value). This should +# work regardless of the crystal used, since JTAG doesn't use the MCU +# clock but dictates its own clock. +jtag3.program.extra_params=-B0.1 + +atmel_ice.name=Atmel-ICE (AVR) +atmel_ice.communication=usb +atmel_ice.protocol=atmelice_isp +atmel_ice.program.protocol=atmelice_isp +atmel_ice.program.tool=avrdude +atmel_ice.program.tool.default=avrdude +atmel_ice.program.extra_params=-Pusb diff --git a/variants/mega/pins_arduino.h b/variants/mega/pins_arduino.h new file mode 100644 index 0000000..5115c04 --- /dev/null +++ b/variants/mega/pins_arduino.h @@ -0,0 +1,413 @@ +/* + pins_arduino.h - Pin definition functions for Arduino + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2007 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA +*/ + +#ifndef Pins_Arduino_h +#define Pins_Arduino_h + +#include + +#define NUM_DIGITAL_PINS 70 +#define NUM_ANALOG_INPUTS 16 +#define analogInputToDigitalPin(p) ((p < 16) ? (p) + 54 : -1) +#define digitalPinHasPWM(p) (((p) >= 2 && (p) <= 13) || ((p) >= 44 && (p)<= 46)) + +#define PIN_SPI_SS (53) +#define PIN_SPI_MOSI (51) +#define PIN_SPI_MISO (50) +#define PIN_SPI_SCK (52) + +static const uint8_t SS = PIN_SPI_SS; +static const uint8_t MOSI = PIN_SPI_MOSI; +static const uint8_t MISO = PIN_SPI_MISO; +static const uint8_t SCK = PIN_SPI_SCK; + +#define PIN_WIRE_SDA (20) +#define PIN_WIRE_SCL (21) + +static const uint8_t SDA = PIN_WIRE_SDA; +static const uint8_t SCL = PIN_WIRE_SCL; + +#define LED_BUILTIN 13 + +#define PIN_A0 (54) +#define PIN_A1 (55) +#define PIN_A2 (56) +#define PIN_A3 (57) +#define PIN_A4 (58) +#define PIN_A5 (59) +#define PIN_A6 (60) +#define PIN_A7 (61) +#define PIN_A8 (62) +#define PIN_A9 (63) +#define PIN_A10 (64) +#define PIN_A11 (65) +#define PIN_A12 (66) +#define PIN_A13 (67) +#define PIN_A14 (68) +#define PIN_A15 (69) + +static const uint8_t A0 = PIN_A0; +static const uint8_t A1 = PIN_A1; +static const uint8_t A2 = PIN_A2; +static const uint8_t A3 = PIN_A3; +static const uint8_t A4 = PIN_A4; +static const uint8_t A5 = PIN_A5; +static const uint8_t A6 = PIN_A6; +static const uint8_t A7 = PIN_A7; +static const uint8_t A8 = PIN_A8; +static const uint8_t A9 = PIN_A9; +static const uint8_t A10 = PIN_A10; +static const uint8_t A11 = PIN_A11; +static const uint8_t A12 = PIN_A12; +static const uint8_t A13 = PIN_A13; +static const uint8_t A14 = PIN_A14; +static const uint8_t A15 = PIN_A15; + +// A majority of the pins are NOT PCINTs, SO BE WARNED (i.e. you cannot use them as receive pins) +// Only pins available for RECEIVE (TRANSMIT can be on any pin): +// (I've deliberately left out pin mapping to the Hardware USARTs - seems senseless to me) +// Pins: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 + +#define digitalPinToPCICR(p) ( (((p) >= 10) && ((p) <= 13)) || \ + (((p) >= 50) && ((p) <= 53)) || \ + (((p) >= 62) && ((p) <= 69)) ? (&PCICR) : ((uint8_t *)0) ) + +#define digitalPinToPCICRbit(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? 0 : \ + ( (((p) >= 62) && ((p) <= 69)) ? 2 : \ + 0 ) ) + +#define digitalPinToPCMSK(p) ( (((p) >= 10) && ((p) <= 13)) || (((p) >= 50) && ((p) <= 53)) ? (&PCMSK0) : \ + ( (((p) >= 62) && ((p) <= 69)) ? (&PCMSK2) : \ + ((uint8_t *)0) ) ) + +#define digitalPinToPCMSKbit(p) ( (((p) >= 10) && ((p) <= 13)) ? ((p) - 6) : \ + ( ((p) == 50) ? 3 : \ + ( ((p) == 51) ? 2 : \ + ( ((p) == 52) ? 1 : \ + ( ((p) == 53) ? 0 : \ + ( (((p) >= 62) && ((p) <= 69)) ? ((p) - 62) : \ + 0 ) ) ) ) ) ) + +#define digitalPinToInterrupt(p) ((p) == 2 ? 0 : ((p) == 3 ? 1 : ((p) >= 18 && (p) <= 21 ? 23 - (p) : NOT_AN_INTERRUPT))) + +#ifdef ARDUINO_MAIN + +const uint16_t PROGMEM port_to_mode_PGM[] = { + NOT_A_PORT, + (uint16_t) &DDRA, + (uint16_t) &DDRB, + (uint16_t) &DDRC, + (uint16_t) &DDRD, + (uint16_t) &DDRE, + (uint16_t) &DDRF, + (uint16_t) &DDRG, + (uint16_t) &DDRH, + NOT_A_PORT, + (uint16_t) &DDRJ, + (uint16_t) &DDRK, + (uint16_t) &DDRL, +}; + +const uint16_t PROGMEM port_to_output_PGM[] = { + NOT_A_PORT, + (uint16_t) &PORTA, + (uint16_t) &PORTB, + (uint16_t) &PORTC, + (uint16_t) &PORTD, + (uint16_t) &PORTE, + (uint16_t) &PORTF, + (uint16_t) &PORTG, + (uint16_t) &PORTH, + NOT_A_PORT, + (uint16_t) &PORTJ, + (uint16_t) &PORTK, + (uint16_t) &PORTL, +}; + +const uint16_t PROGMEM port_to_input_PGM[] = { + NOT_A_PIN, + (uint16_t) &PINA, + (uint16_t) &PINB, + (uint16_t) &PINC, + (uint16_t) &PIND, + (uint16_t) &PINE, + (uint16_t) &PINF, + (uint16_t) &PING, + (uint16_t) &PINH, + NOT_A_PIN, + (uint16_t) &PINJ, + (uint16_t) &PINK, + (uint16_t) &PINL, +}; + +const uint8_t PROGMEM digital_pin_to_port_PGM[] = { + // PORTLIST + // ------------------------------------------- + PE , // PE 0 ** 0 ** USART0_RX + PE , // PE 1 ** 1 ** USART0_TX + PE , // PE 4 ** 2 ** PWM2 + PE , // PE 5 ** 3 ** PWM3 + PG , // PG 5 ** 4 ** PWM4 + PE , // PE 3 ** 5 ** PWM5 + PH , // PH 3 ** 6 ** PWM6 + PH , // PH 4 ** 7 ** PWM7 + PH , // PH 5 ** 8 ** PWM8 + PH , // PH 6 ** 9 ** PWM9 + PB , // PB 4 ** 10 ** PWM10 + PB , // PB 5 ** 11 ** PWM11 + PB , // PB 6 ** 12 ** PWM12 + PB , // PB 7 ** 13 ** PWM13 + PJ , // PJ 1 ** 14 ** USART3_TX + PJ , // PJ 0 ** 15 ** USART3_RX + PH , // PH 1 ** 16 ** USART2_TX + PH , // PH 0 ** 17 ** USART2_RX + PD , // PD 3 ** 18 ** USART1_TX + PD , // PD 2 ** 19 ** USART1_RX + PD , // PD 1 ** 20 ** I2C_SDA + PD , // PD 0 ** 21 ** I2C_SCL + PA , // PA 0 ** 22 ** D22 + PA , // PA 1 ** 23 ** D23 + PA , // PA 2 ** 24 ** D24 + PA , // PA 3 ** 25 ** D25 + PA , // PA 4 ** 26 ** D26 + PA , // PA 5 ** 27 ** D27 + PA , // PA 6 ** 28 ** D28 + PA , // PA 7 ** 29 ** D29 + PC , // PC 7 ** 30 ** D30 + PC , // PC 6 ** 31 ** D31 + PC , // PC 5 ** 32 ** D32 + PC , // PC 4 ** 33 ** D33 + PC , // PC 3 ** 34 ** D34 + PC , // PC 2 ** 35 ** D35 + PC , // PC 1 ** 36 ** D36 + PC , // PC 0 ** 37 ** D37 + PD , // PD 7 ** 38 ** D38 + PG , // PG 2 ** 39 ** D39 + PG , // PG 1 ** 40 ** D40 + PG , // PG 0 ** 41 ** D41 + PL , // PL 7 ** 42 ** D42 + PL , // PL 6 ** 43 ** D43 + PL , // PL 5 ** 44 ** D44 + PL , // PL 4 ** 45 ** D45 + PL , // PL 3 ** 46 ** D46 + PL , // PL 2 ** 47 ** D47 + PL , // PL 1 ** 48 ** D48 + PL , // PL 0 ** 49 ** D49 + PB , // PB 3 ** 50 ** SPI_MISO + PB , // PB 2 ** 51 ** SPI_MOSI + PB , // PB 1 ** 52 ** SPI_SCK + PB , // PB 0 ** 53 ** SPI_SS + PF , // PF 0 ** 54 ** A0 + PF , // PF 1 ** 55 ** A1 + PF , // PF 2 ** 56 ** A2 + PF , // PF 3 ** 57 ** A3 + PF , // PF 4 ** 58 ** A4 + PF , // PF 5 ** 59 ** A5 + PF , // PF 6 ** 60 ** A6 + PF , // PF 7 ** 61 ** A7 + PK , // PK 0 ** 62 ** A8 + PK , // PK 1 ** 63 ** A9 + PK , // PK 2 ** 64 ** A10 + PK , // PK 3 ** 65 ** A11 + PK , // PK 4 ** 66 ** A12 + PK , // PK 5 ** 67 ** A13 + PK , // PK 6 ** 68 ** A14 + PK , // PK 7 ** 69 ** A15 +}; + +const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[] = { + // PIN IN PORT + // ------------------------------------------- + _BV( 0 ) , // PE 0 ** 0 ** USART0_RX + _BV( 1 ) , // PE 1 ** 1 ** USART0_TX + _BV( 4 ) , // PE 4 ** 2 ** PWM2 + _BV( 5 ) , // PE 5 ** 3 ** PWM3 + _BV( 5 ) , // PG 5 ** 4 ** PWM4 + _BV( 3 ) , // PE 3 ** 5 ** PWM5 + _BV( 3 ) , // PH 3 ** 6 ** PWM6 + _BV( 4 ) , // PH 4 ** 7 ** PWM7 + _BV( 5 ) , // PH 5 ** 8 ** PWM8 + _BV( 6 ) , // PH 6 ** 9 ** PWM9 + _BV( 4 ) , // PB 4 ** 10 ** PWM10 + _BV( 5 ) , // PB 5 ** 11 ** PWM11 + _BV( 6 ) , // PB 6 ** 12 ** PWM12 + _BV( 7 ) , // PB 7 ** 13 ** PWM13 + _BV( 1 ) , // PJ 1 ** 14 ** USART3_TX + _BV( 0 ) , // PJ 0 ** 15 ** USART3_RX + _BV( 1 ) , // PH 1 ** 16 ** USART2_TX + _BV( 0 ) , // PH 0 ** 17 ** USART2_RX + _BV( 3 ) , // PD 3 ** 18 ** USART1_TX + _BV( 2 ) , // PD 2 ** 19 ** USART1_RX + _BV( 1 ) , // PD 1 ** 20 ** I2C_SDA + _BV( 0 ) , // PD 0 ** 21 ** I2C_SCL + _BV( 0 ) , // PA 0 ** 22 ** D22 + _BV( 1 ) , // PA 1 ** 23 ** D23 + _BV( 2 ) , // PA 2 ** 24 ** D24 + _BV( 3 ) , // PA 3 ** 25 ** D25 + _BV( 4 ) , // PA 4 ** 26 ** D26 + _BV( 5 ) , // PA 5 ** 27 ** D27 + _BV( 6 ) , // PA 6 ** 28 ** D28 + _BV( 7 ) , // PA 7 ** 29 ** D29 + _BV( 7 ) , // PC 7 ** 30 ** D30 + _BV( 6 ) , // PC 6 ** 31 ** D31 + _BV( 5 ) , // PC 5 ** 32 ** D32 + _BV( 4 ) , // PC 4 ** 33 ** D33 + _BV( 3 ) , // PC 3 ** 34 ** D34 + _BV( 2 ) , // PC 2 ** 35 ** D35 + _BV( 1 ) , // PC 1 ** 36 ** D36 + _BV( 0 ) , // PC 0 ** 37 ** D37 + _BV( 7 ) , // PD 7 ** 38 ** D38 + _BV( 2 ) , // PG 2 ** 39 ** D39 + _BV( 1 ) , // PG 1 ** 40 ** D40 + _BV( 0 ) , // PG 0 ** 41 ** D41 + _BV( 7 ) , // PL 7 ** 42 ** D42 + _BV( 6 ) , // PL 6 ** 43 ** D43 + _BV( 5 ) , // PL 5 ** 44 ** D44 + _BV( 4 ) , // PL 4 ** 45 ** D45 + _BV( 3 ) , // PL 3 ** 46 ** D46 + _BV( 2 ) , // PL 2 ** 47 ** D47 + _BV( 1 ) , // PL 1 ** 48 ** D48 + _BV( 0 ) , // PL 0 ** 49 ** D49 + _BV( 3 ) , // PB 3 ** 50 ** SPI_MISO + _BV( 2 ) , // PB 2 ** 51 ** SPI_MOSI + _BV( 1 ) , // PB 1 ** 52 ** SPI_SCK + _BV( 0 ) , // PB 0 ** 53 ** SPI_SS + _BV( 0 ) , // PF 0 ** 54 ** A0 + _BV( 1 ) , // PF 1 ** 55 ** A1 + _BV( 2 ) , // PF 2 ** 56 ** A2 + _BV( 3 ) , // PF 3 ** 57 ** A3 + _BV( 4 ) , // PF 4 ** 58 ** A4 + _BV( 5 ) , // PF 5 ** 59 ** A5 + _BV( 6 ) , // PF 6 ** 60 ** A6 + _BV( 7 ) , // PF 7 ** 61 ** A7 + _BV( 0 ) , // PK 0 ** 62 ** A8 + _BV( 1 ) , // PK 1 ** 63 ** A9 + _BV( 2 ) , // PK 2 ** 64 ** A10 + _BV( 3 ) , // PK 3 ** 65 ** A11 + _BV( 4 ) , // PK 4 ** 66 ** A12 + _BV( 5 ) , // PK 5 ** 67 ** A13 + _BV( 6 ) , // PK 6 ** 68 ** A14 + _BV( 7 ) , // PK 7 ** 69 ** A15 +}; + +const uint8_t PROGMEM digital_pin_to_timer_PGM[] = { + // TIMERS + // ------------------------------------------- + NOT_ON_TIMER , // PE 0 ** 0 ** USART0_RX + NOT_ON_TIMER , // PE 1 ** 1 ** USART0_TX + TIMER3B , // PE 4 ** 2 ** PWM2 + TIMER3C , // PE 5 ** 3 ** PWM3 + TIMER0B , // PG 5 ** 4 ** PWM4 + TIMER3A , // PE 3 ** 5 ** PWM5 + TIMER4A , // PH 3 ** 6 ** PWM6 + TIMER4B , // PH 4 ** 7 ** PWM7 + TIMER4C , // PH 5 ** 8 ** PWM8 + TIMER2B , // PH 6 ** 9 ** PWM9 + TIMER2A , // PB 4 ** 10 ** PWM10 + TIMER1A , // PB 5 ** 11 ** PWM11 + TIMER1B , // PB 6 ** 12 ** PWM12 + TIMER0A , // PB 7 ** 13 ** PWM13 + NOT_ON_TIMER , // PJ 1 ** 14 ** USART3_TX + NOT_ON_TIMER , // PJ 0 ** 15 ** USART3_RX + NOT_ON_TIMER , // PH 1 ** 16 ** USART2_TX + NOT_ON_TIMER , // PH 0 ** 17 ** USART2_RX + NOT_ON_TIMER , // PD 3 ** 18 ** USART1_TX + NOT_ON_TIMER , // PD 2 ** 19 ** USART1_RX + NOT_ON_TIMER , // PD 1 ** 20 ** I2C_SDA + NOT_ON_TIMER , // PD 0 ** 21 ** I2C_SCL + NOT_ON_TIMER , // PA 0 ** 22 ** D22 + NOT_ON_TIMER , // PA 1 ** 23 ** D23 + NOT_ON_TIMER , // PA 2 ** 24 ** D24 + NOT_ON_TIMER , // PA 3 ** 25 ** D25 + NOT_ON_TIMER , // PA 4 ** 26 ** D26 + NOT_ON_TIMER , // PA 5 ** 27 ** D27 + NOT_ON_TIMER , // PA 6 ** 28 ** D28 + NOT_ON_TIMER , // PA 7 ** 29 ** D29 + NOT_ON_TIMER , // PC 7 ** 30 ** D30 + NOT_ON_TIMER , // PC 6 ** 31 ** D31 + NOT_ON_TIMER , // PC 5 ** 32 ** D32 + NOT_ON_TIMER , // PC 4 ** 33 ** D33 + NOT_ON_TIMER , // PC 3 ** 34 ** D34 + NOT_ON_TIMER , // PC 2 ** 35 ** D35 + NOT_ON_TIMER , // PC 1 ** 36 ** D36 + NOT_ON_TIMER , // PC 0 ** 37 ** D37 + NOT_ON_TIMER , // PD 7 ** 38 ** D38 + NOT_ON_TIMER , // PG 2 ** 39 ** D39 + NOT_ON_TIMER , // PG 1 ** 40 ** D40 + NOT_ON_TIMER , // PG 0 ** 41 ** D41 + NOT_ON_TIMER , // PL 7 ** 42 ** D42 + NOT_ON_TIMER , // PL 6 ** 43 ** D43 + TIMER5C , // PL 5 ** 44 ** D44 + TIMER5B , // PL 4 ** 45 ** D45 + TIMER5A , // PL 3 ** 46 ** D46 + NOT_ON_TIMER , // PL 2 ** 47 ** D47 + NOT_ON_TIMER , // PL 1 ** 48 ** D48 + NOT_ON_TIMER , // PL 0 ** 49 ** D49 + NOT_ON_TIMER , // PB 3 ** 50 ** SPI_MISO + NOT_ON_TIMER , // PB 2 ** 51 ** SPI_MOSI + NOT_ON_TIMER , // PB 1 ** 52 ** SPI_SCK + NOT_ON_TIMER , // PB 0 ** 53 ** SPI_SS + NOT_ON_TIMER , // PF 0 ** 54 ** A0 + NOT_ON_TIMER , // PF 1 ** 55 ** A1 + NOT_ON_TIMER , // PF 2 ** 56 ** A2 + NOT_ON_TIMER , // PF 3 ** 57 ** A3 + NOT_ON_TIMER , // PF 4 ** 58 ** A4 + NOT_ON_TIMER , // PF 5 ** 59 ** A5 + NOT_ON_TIMER , // PF 6 ** 60 ** A6 + NOT_ON_TIMER , // PF 7 ** 61 ** A7 + NOT_ON_TIMER , // PK 0 ** 62 ** A8 + NOT_ON_TIMER , // PK 1 ** 63 ** A9 + NOT_ON_TIMER , // PK 2 ** 64 ** A10 + NOT_ON_TIMER , // PK 3 ** 65 ** A11 + NOT_ON_TIMER , // PK 4 ** 66 ** A12 + NOT_ON_TIMER , // PK 5 ** 67 ** A13 + NOT_ON_TIMER , // PK 6 ** 68 ** A14 + NOT_ON_TIMER , // PK 7 ** 69 ** A15 +}; + +#endif + +// These serial port names are intended to allow libraries and architecture-neutral +// sketches to automatically default to the correct port name for a particular type +// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN, +// the first hardware serial port whose RX/TX pins are not dedicated to another use. +// +// SERIAL_PORT_MONITOR Port which normally prints to the Arduino Serial Monitor +// +// SERIAL_PORT_USBVIRTUAL Port which is USB virtual serial +// +// SERIAL_PORT_LINUXBRIDGE Port which connects to a Linux system via Bridge library +// +// SERIAL_PORT_HARDWARE Hardware serial port, physical RX & TX pins. +// +// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX +// pins are NOT connected to anything by default. +#define SERIAL_PORT_MONITOR Serial +#define SERIAL_PORT_HARDWARE Serial +#define SERIAL_PORT_HARDWARE1 Serial1 +#define SERIAL_PORT_HARDWARE2 Serial2 +#define SERIAL_PORT_HARDWARE3 Serial3 +#define SERIAL_PORT_HARDWARE_OPEN Serial1 +#define SERIAL_PORT_HARDWARE_OPEN1 Serial2 +#define SERIAL_PORT_HARDWARE_OPEN2 Serial3 + +#endif