Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lwc
/
compare
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
31cff805
authored
Jul 13, 2019
by
Enrico Pozzobon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
arduino uno working bench, better
parent
01418953
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
170 additions
and
133 deletions
+170
-133
templates/uno/include/uartp.h
+11
-0
templates/uno/src/main.ino
+22
-87
templates/uno/src/uartp.c
+76
-0
templates/uno/test
+61
-46
No files found.
templates/uno/include/uartp.h
0 → 100644
View file @
31cff805
#pragma once
#ifdef __cplusplus
extern
"C"
{
#endif
void
uartp_send
(
const
void
*
src
,
uint16_t
len
);
uint16_t
uartp_recv
(
void
*
dst
,
uint16_t
buf_len
);
#ifdef __cplusplus
}
#endif
templates/uno/src/main.ino
View file @
31cff805
#include "crypto_aead.h"
#include "api.h"
#include "uartp.h"
#define MAX_BYTES 100
#define DEBUG
//
#define DEBUG
uint8_t
npub
[
CRYPTO_NPUBBYTES
];
uint8_t
nsec
[
CRYPTO_NSECBYTES
];
...
...
@@ -15,6 +16,10 @@ unsigned long long int mlen;
uint8_t
c
[
MAX_BYTES
];
unsigned
long
long
int
clen
;
#ifdef __cplusplus
extern
"C"
{
#endif
void
uart_write
(
uint8_t
x
)
{
Serial
.
write
((
uint8_t
)
(
x
));
}
...
...
@@ -27,75 +32,9 @@ uint8_t uart_read() {
return
(
uint8_t
)
(
0xff
&
r
);
}
// Simple serial protocol with packets and checksum
const
uint8_t
AMUX_TAG
=
0xf9
;
const
uint8_t
AMUX_END
=
0xf3
;
const
uint8_t
AMUX_EXT
=
0x80
;
void
uartp_send
(
const
void
*
src
,
uint16_t
len
)
{
uint8_t
len_ind_0
,
len_ind_1
,
fcs
,
info
;
const
uint8_t
*
buf
=
(
const
uint8_t
*
)
src
;
uart_write
(
AMUX_TAG
);
len_ind_0
=
(
uint8_t
)
(
0xff
&
len
);
len_ind_1
=
(
uint8_t
)
(
0xff
&
(
len
>>
7
));
if
(
len
<
128
)
{
uart_write
(
len_ind_0
);
}
else
{
uart_write
(
len_ind_0
|
AMUX_EXT
);
uart_write
(
len_ind_1
);
}
fcs
=
0
;
for
(
uint16_t
i
=
0
;
i
<
len
;
i
++
)
{
info
=
buf
[
i
];
fcs
+=
info
;
uart_write
(
buf
[
i
]);
}
fcs
=
255
-
fcs
;
uart_write
(
fcs
);
uart_write
(
AMUX_END
);
}
uint16_t
uartp_recv
(
void
*
dst
,
uint16_t
buf_len
)
{
uint8_t
*
buf
=
(
uint8_t
*
)
dst
;
uint8_t
tag_old
,
tag
,
info
,
cs
;
uint16_t
len
;
tag
=
AMUX_END
;
while
(
1
)
{
do
{
tag_old
=
tag
;
tag
=
uart_read
();
}
while
(
tag
!=
AMUX_TAG
||
tag_old
!=
AMUX_END
);
len
=
(
uint16_t
)
uart_read
();
if
(
len
&
AMUX_EXT
)
{
len
&=
(
~
AMUX_EXT
);
len
|=
(
uint16_t
)
(
uart_read
()
<<
7
);
}
if
(
len
>
buf_len
)
{
return
len
;
}
uint16_t
i
=
0
;
cs
=
0
;
for
(
i
=
0
;
i
<
len
;
i
++
)
{
info
=
uart_read
();
buf
[
i
]
=
info
;
cs
+=
info
;
}
cs
+=
uart_read
();
tag
=
uart_read
();
if
(
0xff
==
cs
)
{
if
(
AMUX_END
==
tag
)
{
return
len
;
}
}
}
#ifdef __cplusplus
}
#endif
#ifdef DEBUG
#define DEBUG_BUF_LEN 80
...
...
@@ -118,7 +57,7 @@ int dbg_printf(const char *format, ...) {
return
r
;
}
#else
#define dbg_printf(...) (0)
#define dbg_printf(...) (0)
#endif
void
assert
(
bool
b
)
{
...
...
@@ -134,7 +73,7 @@ void setup() {
pinMode
(
LED_BUILTIN
,
OUTPUT
);
digitalWrite
(
LED_BUILTIN
,
HIGH
);
delay
(
100
);
Serial
.
print
ln
(
"Hello, World!"
);
Serial
.
print
(
"Hello, World!"
);
}
...
...
@@ -146,8 +85,8 @@ void loop() {
if
(
len
==
0
||
len
>
255
)
return
;
dbg_printf
(
"Received command %c
\r\n
"
,
action
);
uint16_t
l
=
len
-
1
;
uint16_t
rl
=
0
;
uint8_t
*
var
=
buf
+
1
;
switch
(
action
)
{
case
'm'
:
assert
(
l
<=
MAX_BYTES
);
memcpy
(
m
,
var
,
l
);
mlen
=
l
;
break
;
...
...
@@ -166,21 +105,17 @@ void loop() {
res
=
crypto_aead_decrypt
(
m
,
&
mlen
,
nsec
,
c
,
clen
,
ad
,
adlen
,
npub
,
k
);
digitalWrite
(
LED_BUILTIN
,
HIGH
);
break
;
}
switch
(
action
)
{
case
'a'
:
case
'c'
:
case
'k'
:
case
'm'
:
case
'p'
:
case
's'
:
case
'd'
:
case
'e'
:
l
=
0
;
break
;
case
'M'
:
var
=
m
;
l
=
mlen
;
break
;
case
'C'
:
var
=
c
;
l
=
clen
;
break
;
case
'A'
:
var
=
ad
;
l
=
adlen
;
break
;
case
'K'
:
var
=
k
;
l
=
CRYPTO_KEYBYTES
;
break
;
case
'P'
:
var
=
npub
;
l
=
CRYPTO_NPUBBYTES
;
break
;
case
'S'
:
var
=
nsec
;
l
=
CRYPTO_NSECBYTES
;
break
;
case
'M'
:
var
=
m
;
rl
=
mlen
;
break
;
case
'C'
:
var
=
c
;
rl
=
clen
;
break
;
case
'A'
:
var
=
ad
;
rl
=
adlen
;
break
;
case
'K'
:
var
=
k
;
rl
=
CRYPTO_KEYBYTES
;
break
;
case
'P'
:
var
=
npub
;
rl
=
CRYPTO_NPUBBYTES
;
break
;
case
'S'
:
var
=
nsec
;
rl
=
CRYPTO_NSECBYTES
;
break
;
default
:
dbg_printf
(
"Unknown command
\r\n
"
);
assert
(
false
);
}
buf
[
0
]
=
action
;
memcpy
(
buf
+
1
,
var
,
l
);
uartp_send
(
buf
,
l
+
1
);
memcpy
(
buf
+
1
,
var
,
r
l
);
uartp_send
(
buf
,
r
l
+
1
);
}
templates/uno/src/uartp.c
0 → 100644
View file @
31cff805
#include <stdint.h>
#include "uartp.h"
extern
void
uart_write
(
uint8_t
x
);
extern
uint8_t
uart_read
();
// Simple serial protocol with packets and checksum
const
uint8_t
AMUX_TAG
=
0xf9
;
const
uint8_t
AMUX_END
=
0xf3
;
const
uint8_t
AMUX_EXT
=
0x80
;
void
uartp_send
(
const
void
*
src
,
uint16_t
len
)
{
uint8_t
len_ind_0
,
len_ind_1
,
fcs
,
info
;
const
uint8_t
*
buf
=
(
const
uint8_t
*
)
src
;
uart_write
(
AMUX_TAG
);
len_ind_0
=
(
uint8_t
)
(
0xff
&
len
);
len_ind_1
=
(
uint8_t
)
(
0xff
&
(
len
>>
7
));
if
(
len
<
128
)
{
uart_write
(
len_ind_0
);
}
else
{
uart_write
(
len_ind_0
|
AMUX_EXT
);
uart_write
(
len_ind_1
);
}
fcs
=
0
;
for
(
uint16_t
i
=
0
;
i
<
len
;
i
++
)
{
info
=
buf
[
i
];
fcs
+=
info
;
uart_write
(
buf
[
i
]);
}
fcs
=
255
-
fcs
;
uart_write
(
fcs
);
uart_write
(
AMUX_END
);
}
uint16_t
uartp_recv
(
void
*
dst
,
uint16_t
buf_len
)
{
uint8_t
*
buf
=
(
uint8_t
*
)
dst
;
uint8_t
tag_old
,
tag
,
info
,
cs
;
uint16_t
len
;
tag
=
AMUX_END
;
while
(
1
)
{
do
{
tag_old
=
tag
;
tag
=
uart_read
();
}
while
(
tag
!=
AMUX_TAG
||
tag_old
!=
AMUX_END
);
len
=
(
uint16_t
)
uart_read
();
if
(
len
&
AMUX_EXT
)
{
len
&=
(
~
AMUX_EXT
);
len
|=
(
uint16_t
)
(
uart_read
()
<<
7
);
}
if
(
len
>
buf_len
)
{
return
len
;
}
uint16_t
i
=
0
;
cs
=
0
;
for
(
i
=
0
;
i
<
len
;
i
++
)
{
info
=
uart_read
();
buf
[
i
]
=
info
;
cs
+=
info
;
}
cs
+=
uart_read
();
tag
=
uart_read
();
if
(
0xff
==
cs
)
{
if
(
AMUX_END
==
tag
)
{
return
len
;
}
}
}
}
templates/uno/test
View file @
31cff805
...
...
@@ -6,6 +6,8 @@ import struct
import
serial
import
subprocess
def
eprint
(
*
args
,
**
kargs
):
print
(
*
args
,
file
=
sys
.
stderr
,
**
kargs
)
...
...
@@ -23,78 +25,91 @@ def get_serial():
devices
.
sort
()
return
devices
[
-
1
]
def
main
(
argv
):
eprint
(
argv
[
0
])
script_dir
=
os
.
path
.
split
(
argv
[
0
])[
0
]
if
len
(
script_dir
)
>
0
:
os
.
chdir
(
script_dir
)
flash
()
dev
=
get_serial
()
ser
=
serial
.
Serial
(
dev
,
baudrate
=
115200
,
timeout
=
5
)
class
UARTP
:
def
__init__
(
self
,
ser
):
UARTP
.
SYN
=
0xf9
UARTP
.
FIN
=
0xf3
self
.
ser
=
ser
def
uart_read
():
r
=
ser
.
read
(
1
)
def
uart_read
(
self
):
r
=
se
lf
.
se
r
.
read
(
1
)
if
len
(
r
)
!=
1
:
raise
Exception
(
"Serial read error"
)
return
r
[
0
]
def
uart_write
(
c
):
def
uart_write
(
self
,
c
):
b
=
struct
.
pack
(
"B"
,
c
)
r
=
ser
.
write
(
b
)
r
=
se
lf
.
se
r
.
write
(
b
)
if
r
!=
len
(
b
):
raise
Exception
(
"Serial write error"
)
return
r
def
uart_recvframe
():
def
send
(
self
,
buf
):
self
.
uart_write
(
UARTP
.
SYN
)
len_ind_0
=
0xff
&
len
(
buf
)
len_ind_1
=
0xff
&
(
len
(
buf
)
>>
7
)
if
len
(
buf
)
<
128
:
self
.
uart_write
(
len_ind_0
)
else
:
self
.
uart_write
(
len_ind_0
|
0x80
)
self
.
uart_write
(
len_ind_1
)
fcs
=
0
for
i
in
range
(
len
(
buf
)):
info
=
buf
[
i
]
fcs
=
(
fcs
+
info
)
&
0xff
self
.
uart_write
(
buf
[
i
])
fcs
=
(
0xff
-
fcs
)
&
0xff
self
.
uart_write
(
fcs
)
self
.
uart_write
(
UARTP
.
FIN
)
eprint
(
"sent frame '
%
s'"
%
buf
.
hex
())
def
recv
(
self
):
tag_old
=
UARTP
.
FIN
while
1
:
tag_old
=
0
tag
=
0xf3
while
tag_old
!=
0xf3
or
tag
!=
0xf9
:
tag
=
tag_old
while
1
:
if
tag_old
==
UARTP
.
FIN
:
if
tag
==
UARTP
.
SYN
:
break
tag_old
=
tag
tag
=
uart_read
()
tag
=
self
.
uart_read
()
tag_old
=
tag
l
=
uart_read
()
l
=
self
.
uart_read
()
if
l
&
0x80
:
l
&=
0x7f
l
|=
uart_read
()
<<
7
l
|=
self
.
uart_read
()
<<
7
fcs
=
0
buf
=
[]
for
i
in
range
(
l
):
info
=
uart_read
()
info
=
self
.
uart_read
()
buf
.
append
(
info
)
fcs
=
(
fcs
+
info
)
&
0xff
fcs
=
(
fcs
+
uart_read
())
&
0xff
fcs
=
(
fcs
+
self
.
uart_read
())
&
0xff
tag
=
uart_read
()
tag
=
self
.
uart_read
()
if
fcs
==
0xff
:
if
tag
==
0xf3
:
if
tag
==
UARTP
.
FIN
:
buf
=
bytes
(
buf
)
eprint
(
"rcvd frame '
%
s'"
%
buf
.
hex
())
if
len
(
buf
)
>=
1
and
buf
[
0
]
==
0xde
:
sys
.
stderr
.
buffer
.
write
(
buf
[
1
:])
sys
.
stderr
.
flush
()
else
:
return
buf
def
uart_sendframe
(
buf
):
uart_write
(
0xf9
)
len_ind_0
=
0xff
&
len
(
buf
)
len_ind_1
=
0xff
&
(
len
(
buf
)
>>
7
)
if
len
(
buf
)
<
128
:
uart_write
(
len_ind_0
)
else
:
uart_write
(
len_ind_0
|
0x80
)
uart_write
(
len_ind_1
)
fcs
=
0
for
i
in
range
(
len
(
buf
)):
info
=
buf
[
i
]
fcs
=
(
fcs
+
info
)
&
0xff
uart_write
(
buf
[
i
])
fcs
=
(
0xff
-
fcs
)
&
0xff
uart_write
(
fcs
)
uart_write
(
0xf3
)
def
main
(
argv
):
eprint
(
argv
[
0
])
script_dir
=
os
.
path
.
split
(
argv
[
0
])[
0
]
if
len
(
script_dir
)
>
0
:
os
.
chdir
(
script_dir
)
flash
()
dev
=
get_serial
()
ser
=
serial
.
Serial
(
dev
,
baudrate
=
115200
,
timeout
=
5
)
uartp
=
UARTP
(
ser
)
def
stdin_read
(
n
):
b
=
sys
.
stdin
.
buffer
.
read
(
n
)
...
...
@@ -123,16 +138,16 @@ def main(argv):
if
action
in
b
"ackmps"
:
v
=
stdin_readvar
()
uart
_sendframe
(
struct
.
pack
(
"B"
,
action
)
+
v
)
ack
=
uart
_recvframe
()
uart
p
.
send
(
struct
.
pack
(
"B"
,
action
)
+
v
)
ack
=
uart
p
.
recv
()
if
len
(
ack
)
!=
1
or
ack
[
0
]
!=
action
:
raise
Exception
(
"Unacknowledged variable transfer"
)
eprint
(
"Var
%
c successfully sent to board"
%
action
)
elif
action
in
b
"ACKMPS"
:
c
=
struct
.
pack
(
"B"
,
action
)
uart
_sendframe
(
c
)
v
=
uart
_recvframe
()
uart
p
.
send
(
c
)
v
=
uart
p
.
recv
()
if
len
(
v
)
<
1
or
v
[
0
]
!=
action
:
raise
Exception
(
"Could not obtain variable from board"
)
v
=
v
[
1
:]
...
...
@@ -143,8 +158,8 @@ def main(argv):
elif
action
in
b
"ed"
:
c
=
struct
.
pack
(
"B"
,
action
)
uart
_sendframe
(
c
)
ack
=
uart
_recvframe
()
uart
p
.
send
(
c
)
ack
=
uart
p
.
recv
()
if
len
(
ack
)
!=
1
or
ack
[
0
]
!=
action
:
raise
Exception
(
"Unacknowledged variable transfer"
)
eprint
(
"Operation
%
c completed successfully"
%
action
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment