Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wicj_embeddedlab
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julia Scharsich
wicj_embeddedlab
Commits
473a10ea
Commit
473a10ea
authored
5 years ago
by
Julia Wichmann
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of
https://gitlab.hrz.tu-chemnitz.de/wicj--tu-chemnitz.de/wicj_embeddedlab
parents
5be55bbe
ccc5ed38
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
interrupts/crc/lay1+2.c
+6
-4
6 additions, 4 deletions
interrupts/crc/lay1+2.c
interrupts/crc/lay1+2_remastered.c
+252
-0
252 additions, 0 deletions
interrupts/crc/lay1+2_remastered.c
interrupts/crc/lay3.c
+19
-6
19 additions, 6 deletions
interrupts/crc/lay3.c
with
277 additions
and
10 deletions
interrupts/crc/lay1+2.c
+
6
−
4
View file @
473a10ea
...
...
@@ -54,7 +54,7 @@ void fillSend (byte* payload, byte size){
send
[
5
]
=
size
;
for
(
byte
i
=
6
;
i
<
size
+
8
;
i
++
){
// für
layer3 muss das noch um 2 erhöht werden!
for
(
byte
i
=
6
;
i
<
size
+
6
;
i
++
){
//
+6
für
preamble, crc, size,
send
[
i
]
=
payload
[
i
-
6
];
}
readyToSend
=
1
;
...
...
@@ -87,15 +87,16 @@ int main(void){
byte
action
;
// 0 - broadcast successful, to layer 4; 1 - receive, to layer 4; 2 - relay
puts
(
"Getting ready to send."
);
byte
sizeToSend
=
2
;
byte
*
payload
=
malloc
((
sizeToSend
+
2
)
*
sizeof
(
byte
));
payload
[
0
]
=
0x00
;
//dest
byte
sizeToSend
=
4
;
byte
*
payload
=
malloc
((
sizeToSend
)
*
sizeof
(
byte
));
payload
[
0
]
=
211
;
//dest
payload
[
1
]
=
SELF
;
//src
payload
[
2
]
=
0xB3
;
payload
[
3
]
=
0x10
;
ATOMIC_BLOCK
(
ATOMIC_FORCEON
){
fillSend
(
payload
,
sizeToSend
);
puts
(
"Sendbuffer filled."
);
}
while
(
1
){
...
...
@@ -133,6 +134,7 @@ int main(void){
}
else
if
(
receiveIndex
==
7
){
//check destination
action
=
l3_action
(
receivedFrame
[
6
],
receivedFrame
[
7
]);
receiveIndex
++
;
}
else
{
receiveIndex
++
;
...
...
This diff is collapsed.
Click to expand it.
interrupts/crc/lay1+2
copy
.c
→
interrupts/crc/lay1+2
_remastered
.c
+
252
−
0
View file @
473a10ea
#define F_CPU 12000000UL
#define BAUD 9600
#include
<avr/io.h>
#include
<stdio.h>
#include
<util/setbaud.h>
#include
<stdlib.h>
#include
<stdint.h>
#include
<avr/io.h>
#include
<avr/interrupt.h>
#include
<util/delay.h>
#include
<util/setbaud.h>
#include
<util/atomic.h>
#include
"julart.h"
// Uart Functions
#include
"crc.h"
//crc implementation
#include
"lay3.h"
#define ATOMIC_BLOCK(type)
#define ATOMIC_RESTORESTATE
#define ATOMIC_FORCEON
//typedef unsigned char byte;
typedef
unsigned
char
byte
;
typedef
uint32_t
uint32
;
#define MAXFRAMESIZE 256 //in byte
#define MAXFRAMESIZE 257 //in byte
// Init of Send-/Receive-Buffer
byte
send
[
MAXFRAMESIZE
]
=
{
0
};
// 0xB3, 0x10 = 10110011, 00010000
byte
frame
[
MAXFRAMESIZE
];
// 0 - Preambel, 1 to 4 - CRC checksum, 5 - size, 6 to n (n = max. 256) - Payload
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
**
queue
;
byte
flag
=
1
;
byte
bufferIndex
=
0
;
// Zählvariable für ISR
byte
sendIndex
=
0
;
uint16_t
bufferIndex
=
0
;
// Zählvariable für ISR
uint16_t
sendIndex
=
0
;
byte
bitIndex
=
7
;
byte
readyToReceive
=
0
;
byte
readyToSend
=
0
;
...
...
@@ -36,26 +36,28 @@ byte readyToSend = 0;
void
delay
(
int
ms
){
while
(
ms
>
0
){
_delay_ms
(
1
);
ms
--
;
_delay_ms
(
1
);
ms
--
;
}
return
;
}
void
fillSend
(
byte
*
payload
,
byte
size
){
//payload padd
en
(Preambel, crc berechnung, size, rückgabe, readyToSend)
send
[
0
]
=
0
b
01111110
;
//payload padd
ing
(Preambel, crc berechnung, size, rückgabe, readyToSend)
send
[
0
]
=
0
x7E
;
//
01111110
uint32_t
checksum
=
crc_comp
(
payload
,
size
);
for
(
int
i
=
1
;
i
<
5
;
i
++
){
for
(
byte
i
=
1
;
i
<
5
;
i
++
){
byte
x
=
32
-
i
*
8
;
send
[
i
]
=
(
byte
)((
checksum
&
((
uint32_t
)(
255
)
<<
(
x
)))
>>
x
);
}
send
[
5
]
=
size
;
for
(
int
i
=
6
;
i
<
size
+
5
;
i
++
){
for
(
byte
i
=
6
;
i
<
size
+
6
;
i
++
){
//+6 für preamble, crc, size,
send
[
i
]
=
payload
[
i
-
6
];
}
}
readyToSend
=
1
;
}
...
...
@@ -80,47 +82,78 @@ int main(void){
setup_PinC
();
sei
();
//set SREG (one)
byte
receiveIndex
=
0
;
byte
receivedFrame
[
MAXFRAMESIZE
];
byte
sizeToReceive
=
MAXFRAMESIZE
;
uint16_t
receiveIndex
=
0
;
byte
*
receivedFrame
=
malloc
(
MAXFRAMESIZE
*
sizeof
(
byte
));
uint16_t
sizeToReceive
=
MAXFRAMESIZE
;
byte
action
;
// 0 - broadcast successful, to layer 4; 1 - receive, to layer 4; 2 - relay
/*
* puts("Getting ready to send.");
* byte sizeToSend = 4;
* byte* payload = malloc((sizeToSend)*sizeof(byte));
* payload[0] = 211; //dest
* payload[1] = SELF; //src
* payload[2] = 0xB3;
* payload[3] = 0x10;
*
* ATOMIC_BLOCK(ATOMIC_FORCEON){
* fillSend(payload, sizeToSend);
* puts("Sendbuffer filled.");
* }
*/
while
(
1
){
if
(
!
readyToSend
){
byte
sizeToSend
=
2
;
byte
*
payload
=
malloc
(
sizeToSend
*
sizeof
(
byte
));
payload
[
0
]
=
0xB3
;
payload
[
1
]
=
0x10
;
ATOMIC_BLOCK
(
ATOMIC_FORCEON
){
fillSend
(
payload
,
size
);
}
}
ATOMIC_BLOCK
(
ATOMIC_FORCEON
){
if
(
readyToReceive
){
switch
(
receiveIndex
){
case
0
:
if
(
frame
[
receiveIndex
]
!=
0x7E
)
break
;
case
5
:
sizeToReceive
=
receivedFrame
[
5
];
receivedFrame
=
realloc
(
receivedFrame
,
sizeToReceive
*
sizeof
(
byte
));
case
(
sizeToReceive
-
1
):
if
(
!
crc_check
(
receivedFrame
,
sizeToReceive
)){
puts
(
"Fehler bei der Übertragung durch CRC-Check festgestellt"
);
switch
(
state
){
case
1
:
//USART interrupt/ receiving data
ATOMIC_BLOCK
(
ATOMIC_FORCEON
){
// Data Package: 0 Preamble, 1 - 4 CRC, 5 Length, 6 Destination, 7 Source, 8 - n Data
if
(
readyToReceive
){
if
(
bufferIndex
!=
0
)
receivedFrame
[
receiveIndex
]
=
frame
[
bufferIndex
-
1
];
else
receivedFrame
[
receiveIndex
]
=
frame
[
MAXFRAMESIZE
-
1
];
//printf("receivedFrame[%d] = %d, bufferIndex: %d \n", receiveIndex, receivedFrame[receiveIndex], bufferIndex);
if
(
receiveIndex
==
0
){
if
(
receivedFrame
[
receiveIndex
]
==
0x7E
)
receiveIndex
++
;
}
else
if
(
receiveIndex
==
5
){
sizeToReceive
=
(
uint16_t
)
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
++
)
printf
(
"%d "
,
receivedFrame
[
i
]
);
}
else
{
puts
(
"CRC-Check successful.
\n
Received: "
);
for
(
uint
i
=
0
;
i
<
sizeToReceive
+
6
;
i
++
)
printf
(
"%d "
,
receivedFrame
[
i
]
);
printf
(
"
\n
"
);
//Hier evtl. noch Speicherung und/oder Ausgabe einbauen.
}
l3_exec
(
action
,
receivedFrame
);
receiveIndex
=
0
;
}
else
if
(
receiveIndex
==
7
){
//check destination
action
=
l3_action
(
receivedFrame
[
6
],
receivedFrame
[
7
]);
receiveIndex
++
;
}
else
{
puts
(
"Übertragung erfolgreich abgeschlossen."
);
//Hier evtl. noch Speicherung und/oder Ausgabe einbauen.
receiveIndex
++
;
}
receiveIndex
=
0
;
break
;
default:
if
(
bufferIndex
!=
0
)
receivedFrame
[
receiveIndex
]
=
frame
[
bufferIndex
-
1
];
else
receivedFrame
[
receiveIndex
]
=
frame
[
sizeToReceive
-
1
];
receiveIndex
++
;
readyToReceive
=
0
;
}
}
readyToReceive
=
0
;
printf
(
"Received: %d
\n
"
,
receivedFrame
[
receiveIndex
]
);
}
if
(
action
!=
2
)
break
;
// wenn relayed werden soll, wird einfach in state 2 übergegangen
else
state
=
2
;
case
2
:
//relay
break
;
default:
memset
(
send
,
0
,
sizeof
(
send
));
break
;
}
}
return
0
;
...
...
@@ -145,7 +178,10 @@ int main(void){
PORTB
&=
~
(
1
<<
PB3
);
//PB3 löschen -> LED aus
}
if
(
bitIndex
==
0
)
sendIndex
++
;
if
(
sendIndex
==
send
[
5
]
+
5
)
sendIndex
=
0
;
if
(
sendIndex
==
(
uint16_t
)
send
[
5
]
+
6
)
{
sendIndex
=
0
;
readyToSend
=
0
;
}
}
}
else
PORTB
^=
(
1
<<
PB4
);
//Clock Signal -> Pin Change Interrupt
...
...
@@ -160,21 +196,18 @@ int main(void){
ISR
(
PCINT2_vect
){
frame
[
bufferIndex
]
=
(
frame
[
bufferIndex
]
<<
1
)
+
((
PIND
&
(
1
<<
PD5
))
>>
PD5
);
bitIndex
--
;
if
(
bitIndex
==
255
)
{
if
(
bufferIndex
==
0
&&
frame
[
bufferIndex
]
!=
0x7E
);
else
{
if
(
bufferIndex
!=
MAXFRAMESIZE
-
1
)
bufferIndex
++
;
else
bufferIndex
=
0
;
readyToReceive
=
1
;
}
frame
[
bufferIndex
]
=
(
frame
[
bufferIndex
]
<<
1
)
+
((
PIND
&
(
1
<<
PD5
))
>>
PD5
);
bitIndex
--
;
if
(
bitIndex
==
255
)
{
if
(
bufferIndex
!=
MAXFRAMESIZE
-
1
)
bufferIndex
++
;
else
bufferIndex
=
0
;
readyToReceive
=
1
;
bitIndex
=
7
;
}
}
//frame[bufferIndex] = (frame[bufferIndex] << 1) + ((PIND & (1 << PD5)) >> PD5);
//received |= (PIND & (1 << PD5)) << bitIndex;
//received = (received << 1) + ((PIND & ( 1 << PD5)) >> PD5);
//received |= (PIND & (1 << PD5)) << bitIndex;
//received = (received << 1) + ((PIND & ( 1 << PD5)) >> PD5);
}
...
...
@@ -189,7 +222,7 @@ void setup_Timer(){
* = 23436 (DEC)
* = 5B8C(HEX)
*/
OCR1A
=
0x
5B8C
;
OCR1A
=
0x
1D4
;
// = 0.01s //0x124F; // = 0.1s
// If TCNTn == OCR1A a timer overflow occurs which can trigger an interrupt.
TCCR1B
|=
(
1
<<
WGM12
);
...
...
@@ -205,11 +238,11 @@ void setup_PinC(){
// Enable checking PCMSK2
PCICR
|=
(
1
<<
PCIE2
);
// Clear Pin Change Interrupt Flag
PCIFR
|=
0x7
;
//Pin Change Interrupt enabled for Pin D4
PCMSK2
|=
(
1
<<
PCINT20
);
// Clear Pin Change Interrupt Flag
PCIFR
|=
0x7
;
//Pin Change Interrupt enabled for Pin D4
PCMSK2
|=
(
1
<<
PCINT20
);
}
/*
...
...
This diff is collapsed.
Click to expand it.
interrupts/crc/lay3.c
+
19
−
6
View file @
473a10ea
...
...
@@ -28,13 +28,17 @@
* -> An communication-error can be detected on the receivers end
*/
//#include "julart.h"
#include
"crc.h"
#include
"lay3.h"
#include
<stdio.h>
void
copyFrame
(
byte
*
from
,
byte
*
to
){
for
(
int
i
=
0
;
i
<
sizeof
(
to
)
&&
i
<
sizeof
(
from
);
i
++
){
to
[
i
]
=
from
[
i
];
}
return
;
}
void
broadcast_successful
(){
puts
(
"Broadcast successful.
\n
"
);
...
...
@@ -58,11 +62,20 @@ void l3_exec(byte action, byte* frame){
case
0
:
broadcast_successful
();
break
;
case
1
:
case
1
:
//receive
puts
(
"Handing package to layer 4.
\n
"
);
break
;
case
2
:
fillSend
(
frame
+
6
,
frame
[
5
]);
break
;
case
2
:
//relay
//fillSend(frame+6, frame[5]);
queue
=
realloc
(
queue
,
tail
+
1
);
queue
[
tail
]
=
realloc
(
queue
[
tail
],
sizeof
(
frame
));
copyFrame
(
frame
,
queue
[
tail
]);
tail
++
;
//evtl. state = 2 setzen für main -> weiß, dass jetzt priorisiert weitergeleitet werden muss
// hier noch head merken und/oder Index zum senden anpassen. -> Priority
break
;
default:
break
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment