Commit 520bc41f by Sebastian Renner

Added jlink flash to STM middleware

parent 6d431eef
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -43,11 +43,15 @@
#include <stdint.h>
#include "crypto_aead.h"
#include "api.h"
#include "SEGGER_RTT.h"
#include "SEGGER_RTT_Conf.h"
#define DEBUG
#define MAX_LEN 1400
#define dbg_printf(...) SEGGER_RTT_printf(0, __VA_ARGS__)
#ifdef DEBUG
#include "SEGGER_RTT.h"
#include "SEGGER_RTT_Conf.h"
#define dbg_printf(...) SEGGER_RTT_printf(0, __VA_ARGS__)
#endif
unsigned char c[MAX_LEN];
unsigned long long clen = 0;
......@@ -73,36 +77,53 @@ static void write_variable_serial(unsigned char target[], uint32_t len);
static void read_serial(void *dst, unsigned int len) {
unsigned char *buf = dst;
#ifdef DEBUG
dbg_printf("Reading %d serial bytes\n", len);
#endif
for (int i = 0; i < len; i++) {
while (!LL_USART_IsActiveFlag_RXNE(USART1));
buf[i] = LL_USART_ReceiveData8(USART1);
}
#ifdef DEBUG
dbg_printf("done.\n", len);
#endif
}
static void write_serial(const void *src, unsigned int len) {
const unsigned char *buf = src;
for (int i = 0; i < len; i++) {
#ifdef DEBUG
dbg_printf("Write to serial: %02x\n", buf[i]);
#endif
while (!(LL_USART_IsActiveFlag_TXE(USART1)));
LL_USART_TransmitData8(USART1, buf[i]);
#ifdef DEBUG
dbg_printf("Done writing!\n");
#endif
}
}
static void read_variable_serial(unsigned char action) {
uint32_t len;
#ifdef DEBUG
dbg_printf("Let's read our variable: %c\n", action);
read_serial(&len, sizeof(len));
#endif
read_serial(&len, sizeof(len));
#ifdef DEBUG
dbg_printf("with length %lu \n", len);
switch(action) {
#endif
switch(action) {
case 'k':
while (len != CRYPTO_KEYBYTES);
read_serial(k, len);
break;
case 'p':
if (len != CRYPTO_NPUBBYTES) { dbg_printf("Assert failed %d != %d\n", len, CRYPTO_NPUBBYTES); for(;;); }
if (len != CRYPTO_NPUBBYTES) {
#ifdef DEBUG
dbg_printf("Assert failed %d != %d\n", len, CRYPTO_NPUBBYTES);
#endif
for(;;);
}
read_serial(npub, len);
break;
case 's':
......@@ -182,7 +203,9 @@ int main(void)
while (1) {
read_serial(&rcv, 1);
#ifdef DEBUG
dbg_printf("Received action: %c\n", rcv);
#endif
switch (rcv) {
case 'c':
case 'm':
......@@ -205,11 +228,15 @@ int main(void)
break;
case 'e':
#ifdef DEBUG
dbg_printf("m: "); for (int i = 0; i < mlen; i++) dbg_printf("%02x", m[i]); dbg_printf("\n");
dbg_printf("a: "); for (int i = 0; i < adlen; i++) dbg_printf("%02x", ad[i]); dbg_printf("\n");
dbg_printf("p: "); for (int i = 0; i < nplen; i++) dbg_printf("%02x", npub[i]); dbg_printf("\n");
dbg_printf("k: "); for (int i = 0; i < klen; i++) dbg_printf("%02x", k[i]); dbg_printf("\n");
#endif
LL_GPIO_TogglePin(GPIOB, LL_GPIO_PIN_12);
res = crypto_aead_encrypt(c, &clen, m, mlen, ad, adlen, nsec, npub, k);
LL_GPIO_TogglePin(GPIOB, LL_GPIO_PIN_12);
break;
default:
......
#!/bin/bash
mv $HDR_FILES Inc/
mv "middleware.py" "test"
......@@ -4,3 +4,4 @@ device STM32F103C8
loadbin build/blackpill.bin 0x8000000
r
g
exit
......@@ -5,14 +5,16 @@ import sys
import time
import struct
import serial
from subprocess import Popen, PIPE, run
import subprocess
f = open("subprocess.log", "w+")
ser = serial.Serial('/dev/ttyUSB0', 115200)
def flash():
run("JLinkExe flash.jlink", shell=True, check=True)
pipe = subprocess.PIPE
p = subprocess.Popen(['JLinkExe', 'flash.jlink'], stdout=sys.stderr, stdin=pipe)
stdout, stderr = p.communicate("")
def read(channel, l):
if channel == 'ser':
......@@ -53,8 +55,12 @@ def submit(channel, action, data):
write(channel, data)
time.sleep(0.1)
def main():
#flash()
def main(argv):
script_dir = os.path.split(argv[0])[0]
if len(script_dir ) > 0:
os.chdir(script_dir)
f.write(os.getcwd())
flash()
print("Hello, World!")
while(1):
action = read('std', 1)
......@@ -76,4 +82,4 @@ def main():
raise Exception("no capiche aczione %s" % (action))
if __name__ == "__main__":
sys.exit(main())
sys.exit(main(sys.argv))
......@@ -25,7 +25,7 @@ def main(argv):
data = p.stdout.read(l)
if len(data) == 0:
print("Unexpected end of stream", file=sys.stderr)
sys.exit(1)
#sys.exit(1)
if len(data) != l:
raise Exception("could not read %d bytes of data (got %d)" % (l, len(data)))
return data
......
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 sign in to comment