Commit c92c1c5d authored by Jake Read's avatar Jake Read

new doc, new xmega code

parent 37939ee2
# Machine Kit Router
# AutomataKit Router
This is a message passing / compute center for machines, robotics.
This is a message passing device and USB link for the [automatakit](https://gitlab.cba.mit.edu/jakeread/automatakit) architecture.
![board](/images/fab-front-rpi.jpg)
......@@ -8,50 +8,25 @@ This is a message passing / compute center for machines, robotics.
![board](/images/fab-back.jpg)
## Status
It works! If you have one, it should forward packets no problem. Let me know if you find weird errors...
## Development Notes
## Next Steps
Did a redesign for this using the ATSAMD51 and USB-Serial on-chip conversion so that networks can run at 1-3MBaud, not 0.1MBaud. That board is out to the house.
See [circuit chatter](/circuit) and [firmware chatter](/embedded).
# New Board
# Viable Commands
- is out to fab
### Test
![routed](/images/rpi-routed.png)
Keycode: **127**
- to test networking, this will reply with a payload packet containing ```127, 12, 24, 48``` and will toggle an LED on the board
![schematic](/images/rpi-schematic.png)
### Reset
# Board Design
Keycode: **128**
- issues a software microcontroller reset
- if the microcontroller is already hung up, this will not work
I use the ATSAMS70 micro, which is very fast but still simple enough. 300Mhz and the I/O we need.
# Reproducing This Work
That said, setting up the power schematic is messier than I would like. There's an internal regulator on the chip that is externally connected to a few 'vddcore' input pins (5), each of which needs a bypass cap. In addition, there are 5 vddio pins that supply voltage to the i/o lines. These also get bypass caps. This is 10 bypass caps. That is a lot to place right off the bat. In addition, there are some pesky vddpll lines that ask for filtered inputs from either the main line or the regulator, meaning I have now inductors and more bypass caps. Oh my. Here is the schematic checklist diagram:
All automatakit works are open source, and while we cannot sell you boards, if you have an interesting application, get in touch to ask about collaborating.
![schematic-power](https://gitlab.cba.mit.edu/jakeread/mkrouter/raw/master/images/schematic-power.png)
To do nice ADC things I want a well stabilized vrefp and vrefn, I will neglect doing this with this chip.
Setting up the USB successfully is nontrivial, as it can be a properly high-speed differential line. I have bricked a few of these chips trying to implement the usb operation, so I'm going to skip most of this as well... Maybe breaking out the USBDM and USBDP lines just in case I get bored later, but I kind of doubt that.
So, here it is. I didn't ask for this complexity. *grumbling*
![schematic-power](https://gitlab.cba.mit.edu/jakeread/mkrouter/raw/master/images/schematic-power-eagle.png)
Less talk more routing ... one note - I am putting some ground plane between all of the RX and TX lines, which I hope to be driving around-and-above 3MHz. So, I want them to not be coupling into eachother. Leaving some ground plane between each trace is a bit like shielding a wire... Here you can see two of the ports taking 'the dive' to the ground plane, where they have lots of room, and then being routed with some space between. I should update other boards using similar rules as well - I'm re-doing my BLDC driver and have started a stepper board that I'll be bringing up at the same time, so I can do that. Nice.
![routing-gnd](https://gitlab.cba.mit.edu/jakeread/mkrouter/raw/master/images/routing-ground-uart.png)
OK, here it is:
![schematic](https://gitlab.cba.mit.edu/jakeread/mkrouter/raw/master/images/schematic.png)
![board](https://gitlab.cba.mit.edu/jakeread/mkrouter/raw/master/images/board.png)
# Notes
PP7 is a miss, check others as well.
Silk kind of messy.
for all, make QFN64 footprint rock: the QFN for those tmc chips are awesome, follow suit
\ No newline at end of file
To reproduce boards and load code, see the document ['Reproducing Automatakit Work'](https://gitlab.cba.mit.edu/jakeread/automatakit/reproducing)
\ No newline at end of file
# AutomataKit Router Circuit Design
![schematic](https://gitlab.cba.mit.edu/jakeread/mkstepper/raw/master/images/schematic.png)
![routing](https://gitlab.cba.mit.edu/jakeread/mkstepper/raw/master/images/routed.png)
The circuit uses an ATxmega256A3U microcontroller and it's UART modules to chatter over 5 ports. It includes a CP2102n USB - UART bridge, acting as the 6th port, to receive serial messages from a computer.
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
[Eagle]
Version="09 00 00"
Platform="Windows"
Globals="Globals"
Desktop="Desktop"
[Globals]
AutoSaveProject=1
UsedLibraryUrn="urn:adsk.eagle:library:217"
UsedLibraryUrn="urn:adsk.eagle:library:325"
UsedLibraryUrn="urn:adsk.eagle:library:371"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/connector.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/fab.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/lights.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/microcontrollers.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/motors.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/passives.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/power.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries/eagle/sensor.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries_jake/eagle/sparkfun/SparkFun-Connectors.lbr"
UsedLibrary="D:/Dropbox (Personal)/CBA/doc/libraries_jake/eagle/jake/tag-connect-2050.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/Comparators.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/Digital.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/FilterProducts.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/Misc.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/Opamps.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/Optos.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/PAsystem.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/PowerProducts.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/References.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/SpecialFunctions.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/capmeter.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/diode.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/lt-spice-simulation.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/lt-supply.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/rload.lbr"
UsedLibrary="C:/EAGLE 8.3.2/lbr/ltspice/sym.lbr"
[Win_1]
Type="Board Editor"
Number=3
File="atkrouter-v01.brd"
View="-5.94496 21.7574 107.847 82.1959"
WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.2032 0.4064 0.6096 0.8128 1.27 2.54 0.508 0.254 0.3048 1.016 0.1524"
PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6"
ViaDiameters=" 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.3 0"
ViaDrills=" 0.2 0.25 0.4 0.5 0.6 0.7 0.75 0.8 0.85 0.9 1 0.35 0.55 0.45 0.65 0.3"
HoleDrills=" 0.2 0.25 0.3 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 1 0.35"
TextSizes=" 0.254 0.3048 0.4064 0.6096 1.016 1.27 1.4224 1.6764 1.9304 2.1844 2.54 3.81 5.08 6.4516 1.778 0.8128"
PolygonSpacings=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 5.08 6.4516 1.27"
PolygonIsolates=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
MiterRadiuss=" 0.254 0.3175 0.635 1.27 2.54 1 2 2.5 5 7.5 10 0"
DimensionWidths=" 0 0.127 0.254 0.1 0.26 0.13"
DimensionExtWidths=" 0.127 0.254 0.1 0.13 0.26 0"
DimensionExtLengths=" 1.27 2.54 1 2 3 0"
DimensionExtOffsets=" 1.27 2.54 1 2 3 0"
SmdSizes=" 0.3048 0.1524 0.4064 0.2032 0.6096 0.3048 0.8128 0.4064 1.016 0.508 1.27 0.6604 1.4224 0.7112 1.6764 0.8128 1.778 0.9144 1.9304 0.9652 2.1844 1.0668 2.54 1.27 3.81 1.9304 5.08 2.54 6.4516 3.2512 1.27 0.635"
WireBend=1
WireBendSet=0
WireCap=1
MiterStyle=0
PadShape=0
ViaShape=1
PolygonPour=0
PolygonRank=1
PolygonThermals=1
PolygonOrphans=0
TextRatio=8
DimensionUnit=1
DimensionPrecision=2
DimensionShowUnit=0
PinDirection=3
PinFunction=0
PinLength=2
PinVisible=3
SwapLevel=0
ArcDirection=0
AddLevel=2
PadsSameType=0
Layer=48
[Win_2]
Type="Schematic Editor"
Number=1
File="atkrouter-v01.sch"
View="-58.1526 -5.84485 208.12 165.332"
WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.2032 0.254 0.3048 0.4064 0.508 0.6096 0.8128 1.016 1.27 2.54 0.1524"
PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6"
ViaDiameters=" 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.3 0"
ViaDrills=" 0.2 0.25 0.3 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 1 0.35"
HoleDrills=" 0.2 0.25 0.3 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 1 0.35"
TextSizes=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.9304 2.1844 2.54 3.81 5.08 6.4516 1.778"
PolygonSpacings=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 5.08 6.4516 1.27"
PolygonIsolates=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
MiterRadiuss=" 0.254 0.3175 0.635 1.27 2.54 1 2 2.5 5 7.5 10 0"
DimensionWidths=" 0 0.127 0.254 0.1 0.26 0.13"
DimensionExtWidths=" 0.127 0.254 0.1 0.13 0.26 0"
DimensionExtLengths=" 1.27 2.54 1 2 3 0"
DimensionExtOffsets=" 1.27 2.54 1 2 3 0"
SmdSizes=" 0.3048 0.1524 0.4064 0.2032 0.6096 0.3048 0.8128 0.4064 1.016 0.508 1.27 0.6604 1.4224 0.7112 1.6764 0.8128 1.778 0.9144 1.9304 0.9652 2.1844 1.0668 2.54 1.27 3.81 1.9304 5.08 2.54 6.4516 3.2512 1.27 0.635"
WireBend=4
WireBendSet=31
WireCap=1
MiterStyle=0
PadShape=0
ViaShape=1
PolygonPour=0
PolygonRank=0
PolygonThermals=1
PolygonOrphans=0
TextRatio=8
DimensionUnit=1
DimensionPrecision=2
DimensionShowUnit=0
PinDirection=3
PinFunction=0
PinLength=2
PinVisible=3
SwapLevel=0
ArcDirection=0
AddLevel=2
PadsSameType=0
Layer=91
Views=" 1: -58.1526 -5.84485 208.12 165.332"
Sheet="1"
[Win_3]
Type="Control Panel"
Number=0
[Desktop]
Screen="3840 1080"
Window="Win_1"
Window="Win_2"
Window="Win_3"
......@@ -70,7 +70,7 @@ Package="RASPBERRYPI-3BPLUS"
Type="Schematic Editor"
Number=1
File="mkrouter-v06.sch"
View="18.324 124.475 115.773 217.342"
View="-39.971 209.905 100.356 343.634"
WireWidths=" 0.0762 0.1016 0.127 0.15 0.2 0.2032 0.254 0.3048 0.4064 0.508 0.6096 0.8128 1.016 1.27 2.54 0.1524"
PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
PadDrills=" 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6"
......@@ -109,14 +109,14 @@ ArcDirection=0
AddLevel=2
PadsSameType=0
Layer=91
Views=" 1: 18.324 124.475 115.773 217.342"
Views=" 1: -39.971 209.905 100.356 343.634"
Sheet="1"
[Win_3]
Type="Board Editor"
Number=2
File="mkrouter-v06.brd"
View="70.3133 23.0011 136.121 116.552"
View="13.4326 60.5944 28.7374 82.3514"
WireWidths=" 0.0762 0.1016 0.127 0.15 0.4064 0.508 0.6096 2.54 0.2 0.254 0.2032 0.3048 1.27 1.016 0.8128 0.1524"
PadDiameters=" 0.254 0.3048 0.4064 0.6096 0.8128 1.016 1.27 1.4224 1.6764 1.778 1.9304 2.1844 2.54 3.81 6.4516 0"
PadDrills=" 0.2 0.25 0.35 0.4 0.45 0.5 0.55 0.65 0.7 0.75 0.8 0.85 0.9 1 0.6 0.3"
......
# Programming the Router
# AutomataKit Router Firmware Notes
running on code from tinynet...
Firmware for the board follows the [automatakit](https://gitlab.cba.mit.edu/jakeread/automatakit) architecture, and is available here for perusal.
finding lock bit issue?
Communication is handled asynchronously: bytes received on the UART are loaded into a ringbuffer, where they are later parsed in to packets. Parsing happens as oven as possible.
setting that does nothing
so, likely that clock is configured incorrectly - will try using erase jumper
changing config from pllack to 8m_rc might have fixed this
some abortive work here trying to get the usb cdc example working again - I've become convinced again that I can and should do this. However, after a few brief moments bringin the example code up and configuring my clocks properly (and generally going abobut this with a more level head) I realize that the version of this board I'm currently debugging doesn't have USB DM / DP lines routed :'( haha so I'll wait until the new version arrives
http://asf.atmel.com/docs/latest/uc3l/html/udi_cdc_quickstart.html#udi_cdc_use_cases
but I started a new atmel studio project for this, so it shouldn't be too hard to hop back in later
\ No newline at end of file
...
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Atmel Studio Solution File, Format Version 11.00
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{54F91283-7BC4-4236-8FF9-10F437C3AD48}") = "atkrouter", "atkrouter\atkrouter.cproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|AVR = Debug|AVR
Release|AVR = Release|AVR
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
################################################################################
# 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 += \
../atkhandler.c \
../atkport.c \
../fastmath.c \
../main.c \
../pin.c \
../ringbuffer.c \
../uartport.c
PREPROCESSING_SRCS +=
ASM_SRCS +=
OBJS += \
atkhandler.o \
atkport.o \
fastmath.o \
main.o \
pin.o \
ringbuffer.o \
uartport.o
OBJS_AS_ARGS += \
atkhandler.o \
atkport.o \
fastmath.o \
main.o \
pin.o \
ringbuffer.o \
uartport.o
C_DEPS += \
atkhandler.d \
atkport.d \
fastmath.d \
main.d \
pin.d \
ringbuffer.d \
uartport.d
C_DEPS_AS_ARGS += \
atkhandler.d \
atkport.d \
fastmath.d \
main.d \
pin.d \
ringbuffer.d \
uartport.d
OUTPUT_FILE_PATH +=atkrouter.elf
OUTPUT_FILE_PATH_AS_ARGS +=atkrouter.elf
ADDITIONAL_DEPENDENCIES:=
OUTPUT_FILE_DEP:= ./makedep.mk
LIB_DEP+=
LINKER_SCRIPT_DEP+=
# AVR32/GNU C Compiler
./%.o: .././%.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 -I"C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\XMEGAA_DFP\1.1.68\include" -O1 -ffunction-sections -fdata-sections -fpack-struct -fshort-enums -g2 -Wall -mmcu=atxmega256a3u -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\XMEGAA_DFP\1.1.68\gcc\dev\atxmega256a3u" -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="atkrouter.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -Wl,--gc-sections -mmcu=atxmega256a3u -B "C:\Program Files (x86)\Atmel\Studio\7.0\Packs\atmel\XMEGAA_DFP\1.1.68\gcc\dev\atxmega256a3u"
@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 "atkrouter.elf" "atkrouter.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 "atkrouter.elf" "atkrouter.eep" || exit 0
"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-objdump.exe" -h -S "atkrouter.elf" > "atkrouter.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 "atkrouter.elf" "atkrouter.srec"
"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\avr8\avr8-gnu-toolchain\bin\avr-size.exe" "atkrouter.elf"
# Other Targets
clean:
-$(RM) $(OBJS_AS_ARGS) $(EXECUTABLES)
-$(RM) $(C_DEPS_AS_ARGS)
rm -rf "atkrouter.elf" "atkrouter.a" "atkrouter.hex" "atkrouter.lss" "atkrouter.eep" "atkrouter.map" "atkrouter.srec" "atkrouter.usersignatures"
\ No newline at end of file
################################################################################
# Automatically-generated file. Do not edit or delete the file
################################################################################
atkhandler.c
atkport.c
fastmath.c
main.c
pin.c
ringbuffer.c
uartport.c
/*
* atkhandler.c
*
* Created: 3/12/2018 11:55:30 AM
* Author: Jake
*/
#include "hardware.h"
#include "atkhandler.h"
int32_t steps;
void atk_handle_packet(uint8_t *packet, uint8_t length){
// dirty debug reply
//atk_return_packet(packet, length);
// through packet
int i = 0;
int atk_handler_state = ATK_HANDLER_OUTSIDE;
uint8_t testReply[4] = {127, 12, 24, 48};
while(i < length){ // prep for the messy double switch :|
switch (atk_handler_state){
case ATK_HANDLER_OUTSIDE:
if (packet[i] == ATK_END_ROUTE_DELIMITER){
atk_handler_state = ATK_HANDLER_INSIDE;
}
i ++;
break;
case ATK_HANDLER_INSIDE:
switch (packet[i]){
case DELIM_KEY_TEST:
// see the packet, make sure you're not doing something else with this light
atk_reply_packet(packet, testReply, 4);
i ++;
break;
case DELIM_KEY_RESET:
CCP = CCP_IOREG_gc;
RST.CTRL = RST_SWRST_bm;
break;
case DELIM_KEY_TRAPEZOID:
//pin_toggle(&stlerr);
break;
default:
// probably an error
i ++;
break;
} // end interior switch
break;
default :
// throw err
break;
} // end y/n switch
}
}
// HERE return with address header, or from old packet with new data
uint8_t ackPack[256];
void atk_reply_packet(uint8_t *opPacket, uint8_t *replyData, uint8_t replyLength){
// find address chunk in opPacket
int i = 2;
int stop = 0;
while(i < opPacket[0]){
if(opPacket[i] == ATK_END_ROUTE_DELIMITER){
stop = i;
break;
}
i ++;
}
uint8_t ackLength = stop + replyLength + 1;
ackPack[0] = ackLength;
if(stop){
// reverse the address header
for(int a = stop - 1, b = 1; a >= 1; a--, b++){
ackPack[b] = opPacket[a];
}
// and append the end block
ackPack[stop] = ATK_END_ROUTE_DELIMITER;
// now fill with provided reply data
for(int u = 0; u <= replyLength; u ++){
ackPack[u + stop + 1] = replyData[u];
}
// checking the port exists, send it out
if(ackPack[1] >= SYSTEM_NUM_UPS){
ackPack[1] = SYSTEM_NUM_UPS - 1;
}
uart_sendchars_buffered(ups[ackPack[1]], ackPack, ackPack[0]);//ups[ackPack[1]], ackPack, ackLength);
}
}
void atk_return_packet(uint8_t *packet, uint8_t length){
//uart_sendchar_buffered(ups[1], 120);
//uart_sendchars_buffered(ups[1], packet, length);
// using this placeholder
ackPack[0] = length;
// find route header
int i = 2;
int stop = 0;
while(i < length){
if(packet[i] == ATK_END_ROUTE_DELIMITER){
stop = i;
break;
}
i ++;
}
// do the business
if(stop){
// reverse the address header
for(int a = stop - 1, b = 1; a >= 1; a--, b++){
ackPack[b] = packet[a];
}
// fill the rest with same packet data
ackPack[stop] = ATK_END_ROUTE_DELIMITER;
for(int u = stop; u < length; u ++){
ackPack[u] = packet[u];