Commit 8c063d60 authored by Jake Read's avatar Jake Read

update with packet handling architecture

parent d40decd6
......@@ -78,6 +78,7 @@ SUBDIRS := \
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../src/apahandler.c \
../src/apaport.c \
../src/pin.c \
../src/ringbuffer.c \
......@@ -101,6 +102,7 @@ ASM_SRCS +=
OBJS += \
src/apahandler.o \
src/apaport.o \
src/pin.o \
src/ringbuffer.o \
......@@ -117,6 +119,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.o \
src/main.o
OBJS_AS_ARGS += \
src/apahandler.o \
src/apaport.o \
src/pin.o \
src/ringbuffer.o \
......@@ -133,6 +136,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.o \
src/main.o
C_DEPS += \
src/apahandler.d \
src/apaport.d \
src/pin.d \
src/ringbuffer.d \
......@@ -149,6 +153,7 @@ src/ASF/sam/utils/syscalls/gcc/syscalls.d \
src/main.d
C_DEPS_AS_ARGS += \
src/apahandler.d \
src/apaport.d \
src/pin.d \
src/ringbuffer.d \
......@@ -205,6 +210,8 @@ LINKER_SCRIPT_DEP+= \
......
......@@ -2,6 +2,8 @@
# Automatically-generated file. Do not edit or delete the file
################################################################################
src\apahandler.c
src\apaport.c
src\pin.c
......
......@@ -475,6 +475,12 @@
<Folder Include="src\config\" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\apahandler.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="src\apahandler.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="src\apaport.c">
<SubType>compile</SubType>
</Compile>
......
/*
* apahandler.c
*
* Created: 3/12/2018 11:55:30 AM
* Author: Jake
*/
#include "hardware.h"
#include "apahandler.h"
void apa_handle_packet(uint8_t *packet, uint8_t length){
// dirty debug reply
uart_sendchars_buffered(&up0, packet, length);
// through packet
for(int i = 0; i < length; i ++){
if(packet[i] == APA_END_ADDR_DELIMITER){
// data begins
// check we have data after addr delimiter
if(i + 1 < length){
int thelight = packet[i+1] % 10;
pin_toggle(lights[thelight]);
break;
} else {
break;
}
}
}
}
\ No newline at end of file
/*
* apahandler.h
*
* Created: 3/12/2018 11:55:40 AM
* Author: Jake
*/
#ifndef APAHANDLER_H_
#define APAHANDLER_H_
#include <asf.h>
void apa_handle_packet(uint8_t *packet, uint8_t length);
#endif /* APAHANDLER_H_ */
\ No newline at end of file
......@@ -31,40 +31,36 @@ void apaport_reset(apaport_t *apap){
void apaport_scan(apaport_t *apap, uint32_t maxpackets){
// scan through for completely received packets
while(apap->packets_ready <= maxpackets){
// check that we have bytes to read out of the buffer
if(rb_empty(apap->uart->rbrx)){
pin_set(apap->stlr);
break;
}
// pull bytes out of buffer and segment into packets
pin_clear(apap->stlg);
delay_ms(1);
pin_set(apap->stlg);
delay_ms(1);
// pull bytes out of buffer into the packet structure
apap->packets[apap->packet_num][apap->packet_position] = rb_get(apap->uart->rbrx);
apap->packet_position ++;
// now segment, point to them
if(apap->packet_position >= apap->packets[apap->packet_num][0]){
// length is 1st byte, like array[n] not array[n-1]
// now volley for next pass
// packet_num is index of head of packet buffer (just an array)
apap->packet_num = (apap->packet_num + 1) % APAPORT_NUM_STATIC_PACKETS; // inc. and loop
// packets_ready is the number of ready-state packets in that buffer (array)
apap->packets_ready ++;
// the position, in bytes, where we are currently operating.
// at this point, we have come to the end, so we're resetting counter for the next
apap->packet_position = 0;
}
} // end 1st scan for packets, now we know we have apaport->packet_num packets completely received
// handle packets
}
// end 1st scan for packets, now we know we have apaport->packet_num packets completely received
// now we handle those packets
while(apap->packets_ready > 0){
// the particular packet index
uint32_t p = (apap->packet_num + APAPORT_NUM_STATIC_PACKETS - apap->packets_ready) % APAPORT_NUM_STATIC_PACKETS;
// handle these!
delay_ms(50);
pin_clear(apap->stlg);
delay_ms(50);
pin_set(apap->stlg);
// now to handle
// [p][0] is length of packet
if(apap->packets[p][1] == APA_ADDR_POINTER){
// packet is ours! mark and somehow get to level up?
// application_apa_handler_t takes it?
apa_handle_packet(apap->packets[p], apap->packets[p][0]);
} else if(apap->packets[p][1] == APA_ADDR_FLOOD){
// loop through bytes to find pointer and increment
apapacket_increment_pointer(apap->packets[p], apap->portnum);
......@@ -91,14 +87,13 @@ void apaport_scan(apaport_t *apap, uint32_t maxpackets){
// double / triple check pointer to pointer
uart_sendchars_buffered(apap->uarts[port], apap->packets[p], apap->packets[p][0]);
}
// debug reply
uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);
// debug reply (at the moment, reply is handled in apa_handle_packet
// uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);
apap->packets_ready --;
}
}
void apapacket_increment_pointer(uint8_t *packet, uint8_t portnum){
// implement after debug
for(int i = 2; i < packet[0]; i ++){
// loop through bytes to find pointer and increment
if(packet[i] == APA_ADDR_POINTER){
......@@ -107,18 +102,13 @@ void apapacket_increment_pointer(uint8_t *packet, uint8_t portnum){
break; // we can finish scan now, not looping senselessly through rest of packet
}
}
/*
if(apap->packets[p][i] == APA_ADDR_POINTER){
apap->packets[p][i-1] = APA_ADDR_POINTER; // increment pointer forwards
apap->packets[p][i] = apap->portnum; // port received on is this one
}
*/
}
// UNIT TESTS:
/*
flood packets
multiple receptions? handle in app?
packets no end addr bar delimiter, packets no pointer, general white noise
packets varying length
packets wrong length ? very hard to catch w/o hella state ... timeout?
packets no end addr delimiter?
......
......@@ -10,6 +10,7 @@
#define APAPORT_H_
#include <asf.h>
#include "apahandler.h"
#include "uartport.h"
#include "pin.h"
......
......@@ -32,7 +32,10 @@ pin_t np4stlr;
pin_t np4stlg;
pin_t np4stlb;
pin_t *lights[] = {&np0stlr, &np0stlb, &np1stlr, &np1stlb, &np2stlr, &np2stlb, &np3stlr, &np3stlb, &np4stlr, &np4stlb};
// array of ptrs to lights
// init in main.c
#define NUM_LIGHTS 12
pin_t *lights[NUM_LIGHTS];
// ringbuffers
ringbuffer_t up0rbrx;
......@@ -53,11 +56,12 @@ uartport_t up2;
uartport_t up3;
uartport_t up4;
// array of ptrs to uarts
uartport_t *ups[] = {&up0, &up1, &up2, &up3, &up4};
// apaports
// array of ptrs to uartports
// inits in main.c
#define NUM_UPS 5
uartport_t *ups[NUM_UPS];
// apaports
apaport_t apap0;
......
......@@ -123,6 +123,27 @@ void initports(void){
uart_init(&up4, 81, UART_IS_PERIPHERAL_C);
}
void listsetup(void){
// array of ptrs to lights
lights[0] = &np0stlr;
lights[1] = &np0stlb;
lights[2] = &np1stlr;
lights[3] = &np1stlb;
lights[4] = &np2stlr;
lights[5] = &np2stlb;
lights[6] = &np3stlr;
lights[7] = &np3stlb;
lights[8] = &np4stlr;
lights[9] = &np4stlb;
// array of ptrs to uarts
ups[0] = &up0;
ups[1] = &up1;
ups[2] = &up2;
ups[3] = &up3;
ups[4] = &up4;
}
int main (void){
sysclk_init();
board_init();
......@@ -130,26 +151,15 @@ int main (void){
setupinterrupts();
lightsetup();
initports();
listsetup();
apaport_build(&apap0, 0, &up0, ups, 5, &np0stlr, &np0stlg, &np0stlb);
// does it use numports, or global?
apaport_reset(&apap0);
while(1){
//lightstoggle();
apaport_scan(&apap0, 2);
pin_toggle(&np4stlg);
/*
uint8_t hello[] = {'h', 'e', 'l', 'l', 'o'};
for(int i = 0; i < 5; i ++){
if(!rb_empty(ups[i]->rbrx)){
uart_sendchars_buffered(ups[i], hello, 5);
uart_sendchar_buffered(ups[i], rb_get(ups[i]->rbrx));
}
}
*/
delay_ms(75);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment