diff --git a/project/.DS_Store b/project/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 Binary files /dev/null and b/project/.DS_Store differ diff --git a/project/Makefile b/project/Makefile index 5b29082f86c58288e8797241917766195dad585c..ff803352f1c5b570f78819a9121fa9a94c58f58c 100644 --- a/project/Makefile +++ b/project/Makefile @@ -6,5 +6,5 @@ flash: lay3.hex lay3.hex: lay3.o avr-objcopy -j .text -j .data -O ihex lay3.o lay3.hex -lay3.o: lay1+2.c crc.c lay3.c - avr-gcc lay1+2.c crc.c lay3.c -o lay3.o -Wall -Wextra -Wpedantic -mmcu=atmega328p -DF_CPU=$(F_CPU) -Os -std=c99 +lay3.o: lay1+2_remastered.c julart.c crc.c lay3.c + avr-gcc lay1+2_remastered.c julart.c crc.c lay3.c -o lay3.o -Wall -Wextra -Wpedantic -mmcu=atmega328p -DF_CPU=$(F_CPU) -Os -std=c99 diff --git a/project/crc.c b/project/crc.c index be343393e74eff77223f213e005122fedb854783..271633009d70565cc830451e7fd0c8366491b416 100644 --- a/project/crc.c +++ b/project/crc.c @@ -3,6 +3,7 @@ /* Implementing CRC as an Left-Shift-Register * Input not reflected, Result not reflected, Initial Value = 0, Final XOR Value = 0 */ + uint32_t crc_comp (byte* input, byte size){ // brauche nur unsigned char weil size maximal 255 byte ist. //byte crc_key [5] = {1, 0b00000100, 0b11000001, 0b00011101, 0b10110111}; //0x104C11DB7 uint32_t crc_key = 0x04C11DB7; @@ -44,7 +45,7 @@ byte crc_check(byte* input){ } byte* payload = malloc (input[5]*sizeof(byte)); - for(uint i = 6; i < (uint)input[5]+6; i++){ + for(uint32_t i = 6; i < (uint32_t)input[5]+6; i++){ payload[i-6] = input[i]; } diff --git a/project/crc.h b/project/crc.h index 07f1c10b19547ccd133ee61b39fc4e8e0bfca977..4e256a368b123f9afb01c8b994d682b310dd9678 100644 --- a/project/crc.h +++ b/project/crc.h @@ -1,7 +1,6 @@ #include <stdint.h> #include <stdlib.h> -typedef unsigned int uint; typedef unsigned char byte; uint32_t crc_comp (byte* input, byte size); byte crc_check(byte* input); diff --git a/project/julart.c b/project/julart.c new file mode 100644 index 0000000000000000000000000000000000000000..ef2fa512f09503ab1059ee2523f4bdee36339e4d --- /dev/null +++ b/project/julart.c @@ -0,0 +1,55 @@ +#include "julart.h" + +/* + * Gewohnter UART stuff für Ein- und Ausgabe + * uart_init() komplett aus [1] + * *_putchar(), *_getchar() größtenteils aus [1] +*/ + +int uart_putchar(char c, FILE *stream) { + if (c == '\n') { + uart_putchar('\r', stream); + } + loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register is empty */ + UDR0 = c; + return 0; +} + +char uart_getchar(FILE *stream) { + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ + char c = UDR0; + if(c == '\r') c = '\n'; + return c; +} + +static FILE uart_io = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); + +//Init Input-/Output-Stream +void my_uart_init(void){ + uart_init(); + + stdout = &uart_io; + stdin = &uart_io; + + return; +} + +void uart_init(void){ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= _BV(U2X0); +#else + UCSR0A &= ~(_BV(U2X0)); +#endif + + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ + UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ +} + +/* + * Eigene Funktion zum flushen des Streams + */ +void jflush(){ + while(getchar() != '\n') puts("Eingabe beenden mit Enter"); +} \ No newline at end of file diff --git a/project/julart.h b/project/julart.h index e8e3070f8ade701b4509c75f94a8446181626ef1..36cdee314e96f3d95342942bb650b45bde4a2ca5 100644 --- a/project/julart.h +++ b/project/julart.h @@ -1,46 +1,15 @@ - - - - -/* - * Gewohnter UART stuff für Ein- und Ausgabe - * uart_init() komplett aus [1] - * *_putchar(), *_getchar() größtenteils aus [1] -*/ - -void uart_init(void){ - UBRR0H = UBRRH_VALUE; - UBRR0L = UBRRL_VALUE; -#if USE_2X - UCSR0A |= _BV(U2X0); -#else - UCSR0A &= ~(_BV(U2X0)); +#ifndef F_CPU +#define F_CPU 12000000UL #endif - - UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ - UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ -} - -void uart_putchar(char c, FILE *stream) { - if (c == '\n') { - uart_putchar('\r', stream); - } - loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register is empty */ - UDR0 = c; -} - -char uart_getchar(FILE *stream) { - loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ - char c = UDR0; - if(c == '\r') c = '\n'; - return c; -} - -FILE uart_io = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); - -/* - * Eigene Funktion zum flushen des Streams - */ -void jflush(){ - while(getchar() != '\n') puts("Eingabe beenden mit Enter"); -} +#define BAUD 9600 + +#include <avr/io.h> +#include <stdio.h> +#include <stdlib.h> +#include <util/setbaud.h> + +void my_uart_init(void); +void uart_init(void); +int uart_putchar(char c, FILE *stream); +char uart_getchar(FILE *stream); +void jflush(); \ No newline at end of file diff --git a/project/lay1+2_remastered.c b/project/lay1+2_remastered.c index c4bff7eaa3edcdf560b3b4424b88367f75cfcc4d..979e2dccaf9620c9d792f71e5a1920d429caf2ca 100644 --- a/project/lay1+2_remastered.c +++ b/project/lay1+2_remastered.c @@ -1,9 +1,10 @@ +#ifndef F_CPU #define F_CPU 12000000UL -#define BAUD 9600 +#endif -#include <avr/io.h> +//#include <avr/io.h> #include <stdio.h> -#include <util/setbaud.h> +//#include <util/setbaud.h> #include <stdlib.h> #include <stdint.h> @@ -23,9 +24,7 @@ // Init of Send-/Receive-Buffer byte send[MAXFRAMESIZE] = {0}; // Testdata: 0xB3, 0x10 = 10110011, 00010000 byte frame[MAXFRAMESIZE] = {0}; // 0 - Preambel, 1 to 4 - CRC checksum, 5 - size, 6 to n (n = max. 256) - Payload -byte** msgQueue; -byte head = 0; -byte tail = 0; + byte state = 0; byte flag = 1; @@ -35,7 +34,6 @@ byte sendBitIndex = 8; byte bitIndex = 7; byte readyToReceive = 0; byte readyToSend = 0; -byte msgIndex = 0; //frame = {0b01111110, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001, 0b10110011}; @@ -67,8 +65,9 @@ void fillSend (byte* payload, byte size){ } void sendZeroes(){ - free(send); - send = calloc(6, sizeof(byte)); + for(int i = 0; i < MAXFRAMESIZE; i++){ + send[i] = 0; + } sendIndex = 5; readyToSend = 1; return; @@ -82,10 +81,7 @@ void setup_Timer(); void setup_PinC(); int main(void){ - uart_init(); - //Init Input-/Output-Stream - stdout = &uart_io; - stdin = &uart_io; + my_uart_init(); //Data Direction DDRD = 0x0; @@ -99,7 +95,7 @@ int main(void){ byte receiveIndex = 0; byte* receivedFrame = malloc(MAXFRAMESIZE*sizeof(byte)); - byte sizeToReceive = MAXFRAMESIZE; + byte sizeToReceive = MAXFRAMESIZE-1; byte action; // 0 - broadcast successful, to layer 4; 1 - receive, to layer 4; 2 - relay /* @@ -137,19 +133,19 @@ int main(void){ else state = 0; } else if(receiveIndex==5){ - sizeToReceive = (uint16_t)receivedFrame[receiveIndex]; + sizeToReceive = receivedFrame[receiveIndex]; //receivedFrame = (byte*) realloc (receivedFrame, (sizeToReceive+6)*sizeof(byte)); receiveIndex++; } else if(receiveIndex==sizeToReceive+5){ if(!crc_check(receivedFrame)){ puts("CRC-Check failed"); - for(uint i=0; i < sizeToReceive+6; i++) + for(byte i=0; i < sizeToReceive+6; i++) printf("%d ", receivedFrame[i] ); } else{ puts("CRC-Check successful.\nReceived: "); - for(uint i=0; i < sizeToReceive+6; i++) + for(byte i=0; i < sizeToReceive+6; i++) printf("%d ", receivedFrame[i] ); printf("\n"); //Hier evtl. noch Speicherung und/oder Ausgabe einbauen. @@ -173,18 +169,12 @@ int main(void){ } case 2: //send / relay - if(msgQueue[msgIndex][0] != 0x7E) state = 0; - else{ - if(!readyToSend) { - l3_send(); - - - } - } - + l3_send(&state); break; case 3: //USART Interrupt happened, add msg to queue + l3_send(&state); + break; } } diff --git a/project/lay3.c b/project/lay3.c index 57670e96228b1fb98c484564d03c95cb78eaa080..08520080293d5cb10007383f91e12c799bdc9c5b 100644 --- a/project/lay3.c +++ b/project/lay3.c @@ -27,14 +27,19 @@ * If the destination differs from self, begin relaying the read Bytes to the next node, while still receiving the remaining ones. * -> An communication-error can be detected on the receivers end */ +#define MAXQUEUESIZE 8 #include "crc.h" #include "lay3.h" #include<stdio.h> +byte** msgQueue; +byte head = 0; +byte tail = 0; + void copyFrame(byte* from, byte* to){ - for(int i = 0; i < sizeof(to) && i < sizeof(from); i++){ + for(unsigned int i = 0; i < sizeof(to) && i < sizeof(from); i++){ to[i] = from[i]; } return; @@ -90,19 +95,20 @@ void l3_exec(byte action, byte* frame){ return; } -void l3_send(){ - if(state == 2){ +void l3_send(byte* state){ + if(msgQueue[head][0] != 0x7E) *state = 0; + else if(*state == 2){ fillSend(msgQueue[head]+6, msgQueue[head][5]); head++; } - if(state == 3){ + else if(*state == 3){ if(tail == head-1 || (head == 0 && tail == MAXQUEUESIZE)) puts("Message Queue full."); else{ - byte* msg = getUSARTmsg(); - msgQueue[tail] = copyFrame(msg, msgQueue[tail]); + byte* msg = NULL; //getUSARTmsg(); + msgQueue[tail] = NULL; //copyFrame(msg, msgQueue[tail]); tail++; } - + return; // weiß noch nicht ob ich das so sinnvoll finde :( } } diff --git a/project/lay3.h b/project/lay3.h index f8f5e12172736a5651ff99ec10135dadb03c5c50..129adf915ba28d18da4b9fca66263854cd38ec47 100644 --- a/project/lay3.h +++ b/project/lay3.h @@ -3,4 +3,5 @@ void broadcast_successful(); byte l3_action(byte dest, byte src); void l3_exec(byte action, byte* frame); -void fillSend (byte* payload, byte size); \ No newline at end of file +void fillSend (byte* payload, byte size); +void l3_send(byte* state); \ No newline at end of file