fw: Tie together all parts for an end-to-end demo

This commit is contained in:
jaseg 2020-05-05 17:50:09 +02:00
parent 9918eb5053
commit db50711ba4
13 changed files with 234 additions and 45 deletions

View file

@ -26,7 +26,7 @@ void debug_hexdump(const char *name, const uint8_t *buf, size_t len) {
DEBUG_PRINTN("\n");
}
/* Returns 1 for correct trigger */
/* Returns trigger sig height for correct trigger */
int verify_trigger_dom(const uint8_t inkey[PRESIG_MSG_LEN],
const char *domain_string, const uint8_t refkey[PRESIG_MSG_LEN]) {
uint8_t key[crypto_auth_hmacsha512_KEYBYTES];

View file

@ -3,7 +3,8 @@
#include <stdint.h>
#define PRESIG_MSG_LEN 16
/* Presig message length: 15 byte = 120 bit ^= 20 * 6-bit symbols of 5-bit bipolar DSSS */
#define PRESIG_MSG_LEN 15
#define OOB_TRIGGER_LEN PRESIG_MSG_LEN
enum trigger_domain {

View file

@ -15,6 +15,7 @@
#include "dsss_demod.h"
#include "con_usart.h"
#include "mspdebug_wrapper.h"
#include "crypto.h"
static struct spi_flash_if spif;
@ -28,6 +29,7 @@ unsigned int apb2_timer_speed = 0;
struct leds leds;
ssize_t jt_spi_flash_read_block(void *usr, int addr, size_t len, uint8_t *out);
static void update_image_flash_counter(void);
void __libc_init_array(void) { /* we don't need this. */ }
void __assert_func (unused_a const char *file, unused_a int line, unused_a const char *function, unused_a const char *expr) {
@ -208,7 +210,7 @@ ssize_t jt_spi_flash_read_block(void *usr, int addr, size_t len, uint8_t *out) {
return len;
}
void update_image_flash_counter(void) {
void update_image_flash_counter() {
static int flash_counter = 0;
flash_counter ++;
fw_dump[row2_offx + 0] = flash_counter/10000 + '0';
@ -222,6 +224,24 @@ void update_image_flash_counter(void) {
fw_dump[row2_offx + 4] = flash_counter + '0';
}
/* Callback from crypto.c:oob_message_received */
void oob_trigger_activated(enum trigger_domain domain, int serial) {
con_printf("oob_trigger_activated(%d, %d)\r\n", domain, serial);
con_printf("Attempting to flash meter...\r\n");
update_image_flash_counter();
int flash_tries = 0;
while (flash_tries++ < 25) {
mspd_jtag_init();
if (!mspd_jtag_flash_and_reset(jtag_img.devmem_img_start, jtag_img.img_len, jt_spi_flash_read_block, &jtag_img))
break;
for (int j=0; j<168*1000*5; j++)
asm volatile ("nop");
}
if (flash_tries == 25)
con_printf("Giving up.\r\n");
}
static unsigned int measurement_errors = 0;
static struct dsss_demod_state demod_state;
static uint32_t freq_sample_ts = 0;
@ -264,17 +284,24 @@ int main(void)
dsss_demod_init(&demod_state);
con_printf("Booted.\r\n");
con_printf("Attempting to flash meter...\r\n");
int flash_tries = 0;
while (flash_tries++ < 25) {
mspd_jtag_init();
if (!mspd_jtag_flash_and_reset(jtag_img.devmem_img_start, jtag_img.img_len, jt_spi_flash_read_block, &jtag_img))
break;
for (int j=0; j<168*1000*5; j++)
asm volatile ("nop");
}
if (flash_tries == 25)
con_printf("Giving up.\r\n");
/* FIXME DEBUG */
#if 0
uint8_t test_data[TRANSMISSION_SYMBOLS] = {
0
};
con_printf("Test 0\r\n");
handle_dsss_received(test_data);
uint8_t test_data2[TRANSMISSION_SYMBOLS] = {
0x24, 0x0f, 0x3b, 0x10, 0x27, 0x0e, 0x22, 0x30, 0x01, 0x2c, 0x1c, 0x0b, 0x35, 0x0a, 0x12, 0x27, 0x11, 0x20,
0x0c, 0x10, 0xc0, 0x08, 0xa4, 0x72, 0xa9, 0x9b, 0x7b, 0x27, 0xee, 0xcd
};
con_printf("Test 1\r\n");
handle_dsss_received(test_data2);
#endif
/* END DEBUG */
while (23) {
if (adc_fft_buf_ready_idx != -1) {

View file

@ -1,9 +1,14 @@
#include <assert.h>
#include "sr_global.h"
#include "dsss_demod.h"
#include "con_usart.h"
#include "rslib.h"
#include "crypto.h"
void handle_dsss_received(uint8_t data[static TRANSMISSION_SYMBOLS]) {
/* Console status output */
con_printf("DSSS data received: ");
for (int i=0; i<TRANSMISSION_SYMBOLS; i++) {
int x = (data[i]>>1) * (data[i]&1 ? 1 : -1);
@ -11,5 +16,29 @@ void handle_dsss_received(uint8_t data[static TRANSMISSION_SYMBOLS]) {
}
con_printf("\r\n");
update_image_flash_counter();
/* Run reed-solomon error correction */
const int sym_bits = DSSS_GOLD_CODE_NBITS + 1; /* +1 for amplitude sign bit */
/* TODO identify erasures in DSSS demod layer */
(void) rslib_decode(sym_bits, TRANSMISSION_SYMBOLS, (char *)data);
/* TODO error detection & handling */
/* Re-bit-pack data buffer to be bit-continuous:
* [ . . a b c d e f ] [ . . g h i j k l ] [ . . m n o p q r ] ...
* ==> [ a b c d e f g h ] [ i j k l m n o p ] [ q r ... ] ...
*/
static_assert((TRANSMISSION_SYMBOLS - NPAR) * (DSSS_GOLD_CODE_NBITS + 1) == OOB_TRIGGER_LEN * 8);
for (uint8_t i=0, j=0; i < TRANSMISSION_SYMBOLS - NPAR; i++, j += sym_bits) {
uint32_t sym = data[i]; /* [ ... | . . X X X X X X ] for 5-bit dsss */
data[i] = 0; /* clear for output */
sym <<= 8-sym_bits; /* left-align: [ ... | X X X X X X . . ] */
sym <<= 8; /* shift to second byte: [ ... | X X X X X X . . | . . . . . . . . ]*/
sym >>= (j%8); /* shift to bit write offset: [ ... | . . . . X X X X | X X . . . . . . ] for offset 4 */
data[j/8] |= sym >> 8; /* write upper byte */
data[j/8 + 1] |= sym & 0xff; /* write lower byte */
}
/* hand off to crypto.c */
oob_message_received(data);
}

View file

@ -3,7 +3,6 @@
#ifndef __RSCODE_CONFIG_H__
#define __RSCODE_CONFIG_H__
#define NPAR 4
#define NBITS 6
#define NPAR 10
#endif /* __RSCODE_CONFIG_H__ */

View file

@ -6,20 +6,19 @@
#include "rslib.h"
static struct rscode_driver driver;
void rslib_encode(int nbits, size_t msglen, char msg[static msglen], char out[msglen + NPAR]) {
struct rscode_driver driver;
rscode_init(&driver, nbits);
rscode_encode(&driver, (unsigned char *)msg, msglen, (unsigned char *)out);
}
int rslib_decode(int nbits, size_t msglen, char msg_inout[static msglen]) {
struct rscode_driver driver;
rscode_init(&driver, nbits);
return rscode_decode(&driver, (unsigned char *)msg_inout, msglen);
}
int rslib_gexp(int z, int nbits) {
struct rscode_driver driver;
rscode_init(&driver, nbits);
return gexp(&driver, z);
}

View file

@ -1,9 +1,12 @@
#ifndef __RSLIB_H__
#define __RSLIB_H__
/* parity length configuration */
#include "rscode-config.h"
void rslib_encode(int nbits, size_t msglen, char msg[static msglen], char out[msglen + NPAR]);
int rslib_decode(int nbits, size_t msglen, char msg_inout[static msglen]);
int rslib_gexp(int z, int nbits);
size_t rslib_npar();
size_t rslib_npar(void);
#endif /* __RSLIB_H__ */