Commit 9fb20278 authored by Jake Read's avatar Jake Read

apaport ready for unit tests

parent 3304b4cd
......@@ -365,6 +365,7 @@
<Value>../src/ASF/common/services/delay</Value>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize (-O1)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.OtherFlags>-fdata-sections</armgcc.compiler.optimization.OtherFlags>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.optimization.DebugLevel>Maximum (-g3)</armgcc.compiler.optimization.DebugLevel>
......
......@@ -7,7 +7,7 @@
#include "apaport.h"
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t *uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb){
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t **uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb){
apap->uart = uart;
apap->uarts = uarts;
apap->portnum = portnum;
......@@ -57,48 +57,71 @@ void apaport_scan(apaport_t *apap, uint32_t maxpackets){
while(apap->packets_ready > 0){
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(150);
delay_ms(50);
pin_set(apap->stlg);
delay_ms(150);
// now to handle: 1st, a reply for debug of what we received
/*
now, handle:
- packet is for us [1]th element is pointer
- route pointer movement & add port incoming
- next port, and fire on
- floods
*/
// now to handle
if(apap->packets[p][1] == APA_ADDR_POINTER){
// packet is ours! mark and somehow get to level up?
// application_apa_handler_t takes it?
} else if(apap->packets[p][1] == APA_ADDR_FLOOD){
// packet is flood, increment pointer and flood!
} else {
// increment pointer
for(int i = 2; i < apap->packets[p][0]; i ++){
// loop through bytes to find pointer
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
} 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);
// now ship it out on all ports
for(int i = 0; i < APAPORT_NUM_PORTS; i ++){
if(i == apap->portnum){
// don't flood back
} else {
uart_sendchars_buffered(apap->uarts[i], apap->packets[p], apap->packets[p][0]);
}
// [1]th element is port to forward on (and check that we have that port)
}
// check that the outgoing port exists on this hardware
// if not, send to topmost
// HERE: mod this so avoid the != APA_ADDR_POINTER (should store and pass before updating, in some cases
// we send to port where [p][1] is flood ... how else u gonna send?
// launches should take place at end of ifs
if(apap->packets[p][1] > APAPORT_NUM_PORTS - 1 && apap->packets[p][1] != APA_ADDR_POINTER){
apap->packets[p][1] = APAPORT_NUM_PORTS - 1;
} else {
// packet is for a particular port,
// we pull that out of the packet now in case the pointer decrements into [1]
uint8_t port = apap->packets[p][1];
// and check that we're sending it out on a port that exists
if (port >= APAPORT_NUM_PORTS){
port = APAPORT_NUM_PORTS - 1;
}
// now we can increment, safely moving pointer
apapacket_increment_pointer(apap->packets[p], apap->portnum);
// send on next port
// HERE: finish mod & rename for ports being 0-n not 1-n
uart_sendchars_buffered(&apap->uarts[apap->packets[p][1] - 1], apap->packets[p], apap->packets[p][0]);
// HERE: failing on send to other ports?
// double / triple check pointer to pointer
uart_sendchars_buffered(apap->uarts[port], apap->packets[p], apap->packets[p][0]);
}
uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);//apap->packets[p][0]);
// debug reply
uart_sendchars_buffered(apap->uart, apap->packets[p], apap->packets[p][0]);
apap->packets_ready --;
}
}
\ No newline at end of file
}
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){
packet[i-1] = APA_ADDR_POINTER; // increment pointer forwards
packet[i] = portnum; // port received on is this one
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 varying length
packets wrong length ? very hard to catch w/o hella state ... timeout?
packets no end addr delimiter?
packets to ports not existing
// next: write javascript terminal packet builder for unit tests!
*/
\ No newline at end of file
......@@ -25,7 +25,7 @@
typedef struct{
uartport_t *uart;
uartport_t *uarts;
uartport_t **uarts; // ptr to array of ptrs ?
pin_t *stlr;
pin_t *stlb;
pin_t *stlg;
......@@ -40,10 +40,12 @@ typedef struct{
uint8_t packets[APAPORT_NUM_STATIC_PACKETS][256]; // packets for handling by app
}apaport_t;
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t *uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb);
void apaport_build(apaport_t *apap, uint8_t portnum, uartport_t *uart, uartport_t **uarts, uint8_t numports, pin_t *stlr, pin_t *stlg, pin_t *stlb);
void apaport_reset(apaport_t *apap);
void apaport_scan(apaport_t *apap, uint32_t maxpackets);
void apapacket_increment_pointer(uint8_t *packet, uint8_t portnum);
#endif /* APAPORT_H_ */
\ No newline at end of file
......@@ -15,44 +15,50 @@
#include "apaport.h"
// status lights
pin_t np0stlr;
pin_t np0stlg;
pin_t np0stlb;
pin_t np1stlr;
pin_t np1stlg;
pin_t np1stlb;
pin_t np2stlr;
pin_t np2stlb;
pin_t np3stlr;
pin_t np3stlb;
pin_t np4stlr;
pin_t np4stlg;
pin_t np4stlb;
pin_t np5stlr;
pin_t np5stlg;
pin_t np5stlb;
pin_t *lights[] = {&np1stlr, &np1stlb, &np2stlr, &np2stlb, &np3stlr, &np3stlb, &np4stlr, &np4stlb, &np5stlr, &np5stlb};
pin_t *lights[] = {&np0stlr, &np0stlb, &np1stlr, &np1stlb, &np2stlr, &np2stlb, &np3stlr, &np3stlb, &np4stlr, &np4stlb};
// ringbuffers
ringbuffer_t np1rbrx;
ringbuffer_t np1rbtx;
ringbuffer_t np2rbrx;
ringbuffer_t np2rbtx;
ringbuffer_t np3rbrx;
ringbuffer_t np3rbtx;
ringbuffer_t np4rbrx;
ringbuffer_t np4rbtx;
ringbuffer_t np5rbrx;
ringbuffer_t np5rbtx;
ringbuffer_t up0rbrx;
ringbuffer_t up0rbtx;
ringbuffer_t up1rbrx;
ringbuffer_t up1rbtx;
ringbuffer_t up2rbrx;
ringbuffer_t up2rbtx;
ringbuffer_t up3rbrx;
ringbuffer_t up3rbtx;
ringbuffer_t up4rbrx;
ringbuffer_t up4rbtx;
// uartports
uartport_t up0;
uartport_t up1;
uartport_t up2;
uartport_t up3;
uartport_t up4;
uartport_t up5;
uartport_t *ups[] = {&up1, &up2, &up3, &up4, &up5};
// array of ptrs to uarts
uartport_t *ups[] = {&up0, &up1, &up2, &up3, &up4};
// apaports
apaport_t apap1;
apaport_t apap0;
#endif /* HARDWARE_H_ */
\ No newline at end of file
/**
* \file
*
* \brief Empty user application template
*
*/
/**
* \mainpage User Application template doxygen documentation
*
* \par Empty user application template
*
* Bare minimum empty user application template
*
* \par Content
*
* -# Include the ASF header files (through asf.h)
* -# "Insert system clock initialization code here" comment
* -# Minimal main function that starts with a call to board_init()
* -# "Insert application code here" comment
*
*/
/*
* Include header files for all drivers that have been imported from
* Atmel Software Framework (ASF).
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
TODO: doc
*/
#include <asf.h>
#include "hardware.h"
......@@ -76,34 +50,34 @@ void setupinterrupts(void){
}
void lightsetup(void){
pin_init(&np1stlr, PIOD, PIO_PER_P12);
pin_init(&np0stlr, PIOD, PIO_PER_P12);
pin_output(&np0stlr);
pin_init(&np0stlg, PIOA, PIO_PER_P3);
pin_output(&np0stlg);
pin_init(&np0stlb, PIOA, PIO_PER_P2);
pin_output(&np0stlb);
pin_init(&np1stlr, PIOA, PIO_PER_P11);
pin_output(&np1stlr);
pin_init(&np1stlg, PIOA, PIO_PER_P3);
pin_output(&np1stlg);
pin_init(&np1stlb, PIOA, PIO_PER_P2);
pin_init(&np1stlb, PIOD, PIO_PER_P20);
pin_output(&np1stlb);
pin_init(&np2stlr, PIOA, PIO_PER_P11);
pin_init(&np2stlr, PIOA, PIO_PER_P15);
pin_output(&np2stlr);
pin_init(&np2stlb, PIOD, PIO_PER_P20);
pin_init(&np2stlb, PIOD, PIO_PER_P27);
pin_output(&np2stlb);
pin_init(&np3stlr, PIOA, PIO_PER_P15);
pin_init(&np3stlr, PIOA, PIO_PER_P22);
pin_output(&np3stlr);
pin_init(&np3stlb, PIOD, PIO_PER_P27);
pin_init(&np3stlb, PIOA, PIO_PER_P8);
pin_output(&np3stlb);
pin_init(&np4stlr, PIOA, PIO_PER_P22);
pin_init(&np4stlr, PIOB, PIO_PER_P0);
pin_output(&np4stlr);
pin_init(&np4stlb, PIOA, PIO_PER_P8);
pin_init(&np4stlg, PIOA, PIO_PER_P20);
pin_output(&np4stlg);
pin_init(&np4stlb, PIOB, PIO_PER_P1);
pin_output(&np4stlb);
pin_init(&np5stlr, PIOB, PIO_PER_P0);
pin_output(&np5stlr);
pin_init(&np5stlg, PIOA, PIO_PER_P20);
pin_output(&np5stlg);
pin_init(&np5stlb, PIOB, PIO_PER_P1);
pin_output(&np5stlb);
}
void lightstoggle(void){
......@@ -114,39 +88,39 @@ void lightstoggle(void){
void initports(void){
// RBs 1
rb_init(&np1rbrx);
rb_init(&np1rbtx);
rb_init(&up0rbrx);
rb_init(&up0rbtx);
// UP1 on UART0, RX 9 TX 10 on PIOA
uart_build(&up1, UART0, PIOA, 9, 10, &np1rbrx, &np1rbtx, &np1stlr, &np1stlb);
uart_init(&up1, 81, UART_IS_PERIPHERAL_A);
uart_build(&up0, UART0, PIOA, 9, 10, &up0rbrx, &up0rbtx, &np0stlr, &np0stlb);
uart_init(&up0, 81, UART_IS_PERIPHERAL_A);
// RBs 2
rb_init(&np2rbrx);
rb_init(&np2rbtx);
rb_init(&up1rbrx);
rb_init(&up1rbtx);
// UP2 on UART1, RX 5 TX 4 on PIOA
uart_build(&up2, UART1, PIOA, 5, 4, &np2rbrx, &np2rbtx, &np2stlr, &np2stlb);
uart_init(&up2, 81, UART_IS_PERIPHERAL_C); // 81 for FTDI 115200 :|
uart_build(&up1, UART1, PIOA, 5, 4, &up1rbrx, &up1rbtx, &np1stlr, &np1stlb);
uart_init(&up1, 81, UART_IS_PERIPHERAL_C); // 81 for FTDI 115200 :|
// RBs 3
rb_init(&np3rbrx);
rb_init(&np3rbtx);
rb_init(&up2rbrx);
rb_init(&up2rbtx);
// UP3 on UART2, RX 25 TX 26 on PIOD
uart_build(&up3, UART2, PIOD, 25, 26, &np3rbrx, &np3rbtx, &np3stlr, &np3stlb);
uart_init(&up3, 81, UART_IS_PERIPHERAL_C);
uart_build(&up2, UART2, PIOD, 25, 26, &up2rbrx, &up2rbtx, &np2stlr, &np2stlb);
uart_init(&up2, 81, UART_IS_PERIPHERAL_C);
// RBs 4
rb_init(&np4rbrx);
rb_init(&np4rbtx);
rb_init(&up3rbrx);
rb_init(&up3rbtx);
// UP4 on UART3, RX 28 TX 30 on PIOD
uart_build(&up4, UART3, PIOD, 28, 30, &np4rbrx, &np4rbtx, &np4stlr, &np4stlb);
uart_init(&up4, 81, UART_IS_PERIPHERAL_A);
uart_build(&up3, UART3, PIOD, 28, 30, &up3rbrx, &up3rbtx, &np3stlr, &np3stlb);
uart_init(&up3, 81, UART_IS_PERIPHERAL_A);
// RBs 5
rb_init(&np5rbrx);
rb_init(&np5rbtx);
rb_init(&up4rbrx);
rb_init(&up4rbtx);
// UP5 on UART4, RX 18 TX 19 on PIOD
uart_build(&up5, UART4, PIOD, 18, 19, &np5rbrx, &np5rbtx, &np5stlr, &np5stlb);
uart_init(&up5, 81, UART_IS_PERIPHERAL_C);
uart_build(&up4, UART4, PIOD, 18, 19, &up4rbrx, &up4rbtx, &np4stlr, &np4stlb);
uart_init(&up4, 81, UART_IS_PERIPHERAL_C);
}
int main (void){
......@@ -157,14 +131,16 @@ int main (void){
lightsetup();
initports();
apaport_build(&apap1, 1, &up1, ups[0], 5, &np1stlr, &np1stlg, &np1stlb);
apaport_build(&apap0, 0, &up0, ups, 5, &np0stlr, &np0stlg, &np0stlb);
// does it use numports, or global?
apaport_reset(&apap1);
apaport_reset(&apap0);
while(1){
//lightstoggle();
apaport_scan(&apap1, 2);
pin_toggle(&np5stlg);
apaport_scan(&apap0, 2);
pin_toggle(&np4stlg);
/*
uint8_t hello[] = {'h', 'e', 'l', 'l', 'o'};
for(int i = 0; i < 5; i ++){
......@@ -179,21 +155,21 @@ int main (void){
}
void UART0_Handler(void){
uart_handler(&up1);
uart_handler(&up0);
}
void UART1_Handler(void){
uart_handler(&up2);
uart_handler(&up1);
}
void UART2_Handler(void){
uart_handler(&up3);
uart_handler(&up2);
}
void UART3_Handler(void){
uart_handler(&up4);
uart_handler(&up3);
}
void UART4_Handler(void){
uart_handler(&up5);
uart_handler(&up4);
}
\ No newline at end of file
......@@ -61,11 +61,13 @@ void uart_sendchar_polled(uartport_t *uart, uint8_t data){
void uart_sendchar_buffered(uartport_t *uart, uint8_t data){
rb_putchar(uart->rbtx, data);
pin_clear(uart->stlb);
uart->com->UART_IER = UART_IER_TXRDY;
}
void uart_sendchars_buffered(uartport_t *uart, uint8_t *data, uint8_t length){
rb_putdata(uart->rbtx, data, length);
pin_clear(uart->stlb);
uart->com->UART_IER = UART_IER_TXRDY;
}
......@@ -85,7 +87,6 @@ void uart_rxhandler(uartport_t *uart){
void uart_txhandler(uartport_t *uart){
if(!rb_empty(uart->rbtx)){
pin_clear(uart->stlb);
uart->com->UART_THR = rb_get(uart->rbtx); // transmit if non-empty
} else {
pin_set(uart->stlb);
......
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