diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c index 216b13949ca0f0f35d9d23e18a9bae57e10cbd06..f692ee9031df5fe9b9c41ecbe52ed89af70b0fce 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/main.c @@ -73,33 +73,33 @@ void setupperipherals(void){ PMC->PMC_PCER0 = 1 << ID_UART1; // UART1 PMC->PMC_PCER1 = 1 << 12; // UART2 PMC->PMC_PCER1 = 1 << 14; // UART4 go clock go - + // tp1, uart2 & uart 4 PIOD->PIO_ABCDSR[0] = ~(PIO_PER_P25 | PIO_PER_P18); PIOD->PIO_ABCDSR[0] = ~(PIO_PER_P26 | PIO_PER_P19); PIOD->PIO_ABCDSR[1] = PIO_PER_P25; PIOD->PIO_ABCDSR[1] = PIO_PER_P26; - + //tp2, uart0 //PIOA->PIO_ABCDSR[0] = ~PIO_PER_P9; //PIOA->PIO_ABCDSR[0] = ~PIO_PER_P10; //PIOA->PIO_ABCDSR[1] = ~PIO_PER_P9; //PIOA->PIO_ABCDSR[1] = ~PIO_PER_P10; - - + + //tp3, uart1 //PIOA->PIO_ABCDSR[0] = ~PIO_PER_P5; //PIOA->PIO_ABCDSR[0] = ~PIO_PER_P4; PIOA->PIO_ABCDSR[1] |= PIO_PER_P5; PIOA->PIO_ABCDSR[1] |= PIO_PER_P4; - - + + //tp4, uart4 //PIOD->PIO_ABCDSR[0] &= ~PIO_PER_P18; //PIOD->PIO_ABCDSR[0] &= ~PIO_PER_P19; PIOD->PIO_ABCDSR[1] |= PIO_PER_P18; PIOD->PIO_ABCDSR[1] |= PIO_PER_P19; - + } void killwatchdog(void){ @@ -111,17 +111,17 @@ void setupinterrupts(void){ NVIC_ClearPendingIRQ(UART2_IRQn); NVIC_SetPriority(UART2_IRQn, 8); NVIC_EnableIRQ(UART2_IRQn); - + NVIC_DisableIRQ(UART0_IRQn); NVIC_ClearPendingIRQ(UART0_IRQn); NVIC_SetPriority(UART0_IRQn, 8); NVIC_EnableIRQ(UART0_IRQn); - + NVIC_DisableIRQ(UART1_IRQn); NVIC_ClearPendingIRQ(UART1_IRQn); NVIC_SetPriority(UART1_IRQn, 8); NVIC_EnableIRQ(UART1_IRQn); - + NVIC_DisableIRQ(UART4_IRQn); NVIC_ClearPendingIRQ(UART4_IRQn); NVIC_SetPriority(UART4_IRQn, 8); @@ -129,35 +129,35 @@ void setupinterrupts(void){ } void setupstatus(void){ - + stlb = pin_new(PIOA, PIO_PER_P1); pin_output(&stlb); stlr = pin_new(PIOD, PIO_PER_P11); pin_output(&stlr); button = pin_new(PIOA, PIO_PER_P15); pin_input(&button); - + p1lr = pin_new(PIOA, PIO_PER_P22); pin_output(&p1lr); p1lg = pin_new(PIOA, PIO_PER_P8); pin_output(&p1lg); p1lb = pin_new(PIOA, PIO_PER_P13); pin_output(&p1lb); - + p2lr = pin_new(PIOA, PIO_PER_P30); pin_output(&p2lr); p2lg = pin_new(PIOD, PIO_PER_P9); pin_output(&p2lg); p2lb = pin_new(PIOA, PIO_PER_P28); pin_output(&p2lb); - + p3lr = pin_new(PIOD, PIO_PER_P10); pin_output(&p3lr); p3lg = pin_new(PIOA, PIO_PER_P0); pin_output(&p3lg); p3lb = pin_new(PIOD, PIO_PER_P15); pin_output(&p3lb); - + p4lr = pin_new(PIOD, PIO_PER_P13); pin_output(&p4lr); p4lg = pin_new(PIOD, PIO_PER_P14); @@ -204,37 +204,49 @@ void setallstatus(void){ int main (void) { - board_init(); // asf + node_t* n = (node_t*)malloc(sizeof(node_t)); + n->myAddress = ADDRESS; + n->portBufferSizes = (uint8_t*)malloc(32); + for (int port = 0; port < 4; port++) { + n->portBufferSizes[port] = 0; + } + nb->LUT = (uint8_t**)malloc(4096); + for (int i = 0; i < 4; i++) { + for (int port = 0; port < 4; port++) { + n->LUT[i][port] = 255; + } + } + //board_init(); // asf sysclk_init(); // asf clock - + setupperipherals(); // peripheral clocks killwatchdog(); // no thanks - + setupstatus(); // pin setup - + tp1 = tinyport_new(UART2, PIOD, PERIPHERAL_C, PIO_PER_P25, PIO_PER_P26, &p1rbrx, &p1rbtx, &p1lr, &p1lg, &p1lb); tp2 = tinyport_new(UART0, PIOA, PERIPHERAL_A, PIO_PER_P9, PIO_PER_P10, &p2rbrx, &p2rbtx, &p2lr, &p2lg, &p2lb); tp3 = tinyport_new(UART1, PIOA, PERIPHERAL_C, PIO_PER_P5, PIO_PER_P4, &p3rbrx, &p3rbtx, &p3lr, &p3lg, &p3lb); tp4 = tinyport_new(UART4, PIOD, PERIPHERAL_C, PIO_PER_P18, PIO_PER_P19, &p4rbrx, &p4rbtx, &p4lr, &p4lg, &p4lb); - + tp_init(&tp1); tp_init(&tp2); tp_init(&tp3); tp_init(&tp4); - + tinyport_t ports[4] = {tp1, tp2, tp3, tp4}; - + setupinterrupts(); // turns interrupt NVICs on - + setallstatus(); // lights off pin_set(&stlr); pin_clear(&stlb); - + tp_testlights(&tp1); // fancy tp_testlights(&tp3); tp_testlights(&tp2); tp_testlights(&tp4); - + packet_t packetlooper; while(1){ @@ -243,30 +255,30 @@ int main (void) for(int i = 0; i < 4; i++){ tp_packetparser(&ports[i]); } - + for(int i = 0; i < 4; i++){ // loop over ports and check for packets, add packets to packet buffer if(ports[i].haspacket){ - + packetlooper = ports[i].packet; // pull into buffer packet_clean(&ports[i].packet); // reset packet states - ports[i].haspacket = TP_NO_PACKET; + ports[i].haspacket = TP_NO_PACKET; // TODO: update heartbeat / buffer depth - + pin_clear(ports[i].stlb); // for debugging: we have seen a packet on this port - + for(int c = 0; c < packetlooper.size; c ++){ // blocking echo tp_putchar(&ports[i], packetlooper.raw[c]); } - + handle_packet(); // put data in block, error if returns 0 b/c overfull ringbuffer /* if(!tp_putdata(&ports[i], packetlooper.raw, packetlooper.size)){ - pin_clear(ports[i].stlr); + pin_clear(ports[i].stlr); } */ } - + // packet handler // pull a packet from the buffer, // handle_packet() @@ -276,9 +288,9 @@ int main (void) } */ } - + // loop over packet buffer and handle packets - + delay_cycles(1); // one clock tick to relax interrupt scheduler } } @@ -287,7 +299,7 @@ void UART2_Handler(){ if(UART2->UART_SR & UART_SR_RXRDY){ tp_rxhandler(&tp1); } - + /* if(UART2->UART_SR & UART_SR_TXRDY){ tp_txhandler(&tp1); @@ -311,4 +323,4 @@ void UART4_Handler(){ if(UART4->UART_SR & UART_SR_RXRDY){ tp_rxhandler(&tp4); } -} \ No newline at end of file +} diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h index 96c9cc9d48249cd59166334ab7972b68ef2e1fe2..3452b30b08d5c0b24bafabd800d553fd385e3097 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/node.h @@ -1,5 +1,7 @@ +#define ADDRESS 0 + typedef struct { - uint8_t portBufferSizes[4]; - uint16_t LUT[1024][4]; // TODO: fix representation + uint8_t* portBufferSizes; + uint8_t** LUT; // 1024 x 4 uint16_t myAddress; } node_t; diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h index 63c966187c25ccbaa4a0c448cb39c8273cfb8dd0..a57def3e1f62b190af9c3e0a961d7a24660bee98 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet.h @@ -9,7 +9,8 @@ #ifndef PACKET_H_ #define PACKET_H_ -#include "asf.h" +//#include "asf.h" +#include <stdint.h> typedef struct{ uint8_t raw[255]; diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c index 65cd0bb90a7207687af7fa4aab79477eb4c98cb4..e67e246383f97ac0ae784d9fbc4fb0a7b82a5d27 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.c @@ -2,6 +2,9 @@ #include "node.h" #include "packet_handling.h" #include <string.h> +#include <stdlib.h> + +#define MAX_HOPCOUNT 6 int parse_type(packet_t* p) { return p->raw[0]; @@ -12,7 +15,7 @@ void update_LUT(node_t* n, uint16_t src, uint8_t hopCount, uint8_t port) { } void send_packet(packet_t* p, uint8_t port) { - // TODO: + // TODO: } void broadcast_packet(packet_t* p, uint8_t exclude) { @@ -22,21 +25,21 @@ void broadcast_packet(packet_t* p, uint8_t exclude) { if (exclude != 3) send_packet(p, 3); } -packet_t* turn_to_standard_flood(node_t* n, packet_t* p) { - packet_t* newPacket = (packet_t*)malloc(sizeof(packet_t)); - memcpy((void*)newPacket->raw, (const void*)p->raw, (size_t)256); - newPacket->destination = p->destination; - newPacket->source = n->myAddress; - newPacket->hopcount = p->hopcount; - newPacket->size = p->size; - newPacket->counter = p->counter; - return newPacket; +int in_table(node_t* n, uint8_t dest) { + return !(n->LUT[dest][0] != MAX_HOPCOUNT && n->LUT[dest][1] != MAX_HOPCOUNT && + n->LUT[dest][2] != MAX_HOPCOUNT && n->LUT[dest][3] != MAX_HOPCOUNT); } void handle_packet(node_t* n, packet_t* p, uint8_t port) { + if (p->hopcount > MAX_HOPCOUNT) { + free((void*)p); + return; + } + if (parse_type(p) != BUFFER_UPDATE) { update_LUT(n, p->source, p->hopcount, port); } + switch (parse_type(p)) { case STANDARD: if (p->destination == n->myAddress) { @@ -44,7 +47,7 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { //reply } else { p->hopcount++; - if (n->LUT[p->destination]) { + if (in_table(n, p->destination)) { int bestPort = 0; int bestHopCount = n->LUT[p->destination][0]; for (int i = 0; i < 4; i++) { @@ -55,7 +58,8 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { } send_packet(p, bestPort); } else { - broadcast_packet(turn_to_standard_flood(n, p), port); + p->raw[0] = STANDARD_FLOOD; + broadcast_packet(p, 4); } } break; @@ -64,7 +68,7 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { //process } else { p->hopcount++; - if (n->LUT[p->destination]) { + if (in_table(n, p->destination)) { int bestPort = 0; int bestHopCount = n->LUT[p->destination][0]; for (int i = 0; i < 4; i++) { @@ -75,12 +79,13 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { } send_packet(p, bestPort); } else { - broadcast_packet(turn_to_standard_flood(n, p), port); + p->raw[0] = STANDARD_FLOOD; + broadcast_packet(p, 4); } } break; case STANDARD_FLOOD: - n->LUT[p->destination][port] = 255; + n->LUT[p->destination][port] = MAX_HOPCOUNT; if (p->destination == n->myAddress) { //process //reply @@ -97,12 +102,13 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { } send_packet(p, bestPort); } else { - broadcast_packet(turn_to_standard_flood(n, p), port); + p->raw[0] = STANDARD_FLOOD; + broadcast_packet(p, port); } } break; case ACK_FLOOD: - n->LUT[p->destination][port] = 255; + n->LUT[p->destination][port] = MAX_HOPCOUNT; if (p->destination == n->myAddress) { //process } else { @@ -118,7 +124,8 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { } send_packet(p, bestPort); } else { - broadcast_packet(turn_to_standard_flood(n, p), port); + p->raw[0] = STANDARD_FLOOD; + broadcast_packet(p, port); } } break; @@ -127,3 +134,8 @@ void handle_packet(node_t* n, packet_t* p, uint8_t port) { break; } } + +int main() { + + return 0; +} diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h index 7f8c316b1093c5d93e888b9eba3b5b3919b54ee4..b0cb2ec383ed0c9a5abf6a67d12bc4fb27acf130 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/packet_handling.h @@ -3,7 +3,7 @@ * * Created: 12/7/2017 2:14:08 PM * Author: Jake - */ + */ #ifndef PACKET_HANDLING_H_ @@ -19,10 +19,8 @@ void send_packet(packet_t* p, uint8_t port); void broadcast_packet(packet_t* p, uint8_t exclude); -packet_t* turn_to_standard_flood(node_t* n, packet_t* p); - void handle_packet(node_t* n, packet_t* p, uint8_t port); +int in_table(node_t* n, uint8_t dest); - -#endif /* PACKET_HANDLING_H_ */ \ No newline at end of file +#endif /* PACKET_HANDLING_H_ */ diff --git a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/pin.h b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/pin.h index 134b176b0f36964a646e98b75e2953c8b9784d8a..08ef9fbb42d01f1eaa4654fe334a1013fd15b9a0 100644 --- a/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/pin.h +++ b/embedded/atsams70-tinyrouter/atsams70-tinyrouter/src/pin.h @@ -3,7 +3,7 @@ * * Created: 11/23/2017 1:11:45 PM * Author: Jake - */ + */ #ifndef PIN_H_ @@ -29,4 +29,4 @@ void pin_clear(pin_t *pin); void pin_input(pin_t *pin); bool pin_get_state(pin_t *pin); -#endif /* PIN_H_ */ \ No newline at end of file +#endif /* PIN_H_ */