* Initial release for SX1302 CoreCell Reference Design.
This commit is contained in:
Michael Coracin 2019-07-12 15:40:13 +02:00
commit 4c61c5d48e
79 changed files with 30157 additions and 0 deletions

View file

@ -0,0 +1,61 @@
### get external defined data
include ../../target.cfg
### constant symbols
ARCH ?=
CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar
CFLAGS := -O2 -Wall -Wextra -std=c99 -I. -I../../libtools/inc
### linking options
LIBS := -ltinymt32
### general build targets
all: payload_crc payload_diff payload_gen
clean:
rm -f payload_crc payload_diff payload_gen
rm -f *.o
install:
ifneq ($(strip $(TARGET_IP)),)
ifneq ($(strip $(TARGET_DIR)),)
ifneq ($(strip $(TARGET_USR)),)
@echo "---- Copying payload tools files to $(TARGET_IP):$(TARGET_DIR)"
@ssh $(TARGET_USR)@$(TARGET_IP) "mkdir -p $(TARGET_DIR)"
@scp payload_crc $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR)
@scp payload_diff $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR)
@scp payload_gen $(TARGET_USR)@$(TARGET_IP):$(TARGET_DIR)
else
@echo "ERROR: TARGET_USR is not configured in target.cfg"
endif
else
@echo "ERROR: TARGET_DIR is not configured in target.cfg"
endif
else
@echo "ERROR: TARGET_IP is not configured in target.cfg"
endif
### rules
%.o : %.c
$(CC) -c $(CFLAGS) $< -o $@
### test programs
payload_crc: payload_crc.o
$(CC) $(CFLAGS) -o $@ $^
payload_diff: payload_diff.o
$(CC) $(CFLAGS) -o $@ $^
payload_gen: payload_gen.o
$(CC) $(CFLAGS) -L../../libtools -o $@ $^ $(LIBS)
### EOF

View file

@ -0,0 +1,110 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/* -------------------------------------------------------------------------- */
/* --- SUBFUNCTIONS DECLARATION --------------------------------------------- */
static void usage(void);
uint16_t sx1302_lora_payload_crc(const uint8_t * data, uint8_t size);
void remove_spaces(char *str);
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
int main(int argc, char ** argv)
{
int j;
uint8_t payload[255];
uint8_t payload_size;
uint16_t crc;
char hexstr[1024];
if (argc < 2) {
usage();
return -1;
}
/* Get payload hex string from command line */
memcpy(hexstr, argv[1], strlen(argv[1]));
hexstr[strlen(argv[1])] = '\0';
printf("Input hex string: %s\n", hexstr);
/* Remove spaces from the string if any */
remove_spaces(hexstr);
hexstr[strlen(hexstr)] = '\0';
printf("Removing spaces: %s\n", hexstr);
/* Convert hex string to byte array */
payload_size = strlen(hexstr) / 2;
for (j = 0; j < payload_size; j++) {
sscanf(hexstr + 2*j, "%02hhx", &payload[j]);
}
/* Compute CRC */
crc = sx1302_lora_payload_crc(payload, payload_size);
printf("Payload CRC_16: %04X\n", crc);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void usage(void) {
printf("Missing payload hex string\n");
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void remove_spaces(char *str)
{
/* To keep track of non-space character count */
int count = 0;
/* Traverse the given string. If current character
is not space, then place it at index 'count++' */
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[count++] = str[i]; /* here count is incremented */
}
}
str[count] = '\0';
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void lora_crc16(const char data, int *crc) {
int next = 0;
next = (((data>>0)&1) ^ ((*crc>>12)&1) ^ ((*crc>> 8)&1) ) ;
next += ((((data>>1)&1) ^ ((*crc>>13)&1) ^ ((*crc>> 9)&1) )<<1 ) ;
next += ((((data>>2)&1) ^ ((*crc>>14)&1) ^ ((*crc>>10)&1) )<<2 ) ;
next += ((((data>>3)&1) ^ ((*crc>>15)&1) ^ ((*crc>>11)&1) )<<3 ) ;
next += ((((data>>4)&1) ^ ((*crc>>12)&1) )<<4 ) ;
next += ((((data>>5)&1) ^ ((*crc>>13)&1) ^ ((*crc>>12)&1) ^ ((*crc>> 8)&1))<<5 ) ;
next += ((((data>>6)&1) ^ ((*crc>>14)&1) ^ ((*crc>>13)&1) ^ ((*crc>> 9)&1))<<6 ) ;
next += ((((data>>7)&1) ^ ((*crc>>15)&1) ^ ((*crc>>14)&1) ^ ((*crc>>10)&1))<<7 ) ;
next += ((((*crc>>0)&1) ^ ((*crc>>15)&1) ^ ((*crc>>11)&1) )<<8 ) ;
next += ((((*crc>>1)&1) ^ ((*crc>>12)&1) )<<9 ) ;
next += ((((*crc>>2)&1) ^ ((*crc>>13)&1) )<<10) ;
next += ((((*crc>>3)&1) ^ ((*crc>>14)&1) )<<11) ;
next += ((((*crc>>4)&1) ^ ((*crc>>15)&1) ^ ((*crc>>12)&1) ^ ((*crc>> 8)&1))<<12) ;
next += ((((*crc>>5)&1) ^ ((*crc>>13)&1) ^ ((*crc>> 9)&1) )<<13) ;
next += ((((*crc>>6)&1) ^ ((*crc>>14)&1) ^ ((*crc>>10)&1) )<<14) ;
next += ((((*crc>>7)&1) ^ ((*crc>>15)&1) ^ ((*crc>>11)&1) )<<15) ;
(*crc) = next;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
uint16_t sx1302_lora_payload_crc(const uint8_t * data, uint8_t size) {
int i;
int crc = 0;
for (i = 0; i < size; i++) {
lora_crc16(data[i], &crc);
}
//printf("CRC16: 0x%02X 0x%02X (%X)\n", (uint8_t)(crc >> 8), (uint8_t)crc, crc);
return (uint16_t)crc;
}

View file

@ -0,0 +1,116 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/* -------------------------------------------------------------------------- */
/* --- MACROS --------------------------------------------------------------- */
#define TAKE_N_BITS_FROM(b, p, n) (((b) >> (p)) & ((1 << (n)) - 1))
/* -------------------------------------------------------------------------- */
/* --- SUBFUNCTIONS DECLARATION --------------------------------------------- */
static void usage(void);
void remove_spaces(char *str);
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
int main(int argc, char ** argv)
{
int i, j;
uint8_t payload_a[255];
uint8_t payload_b[255];
uint8_t payload_diff[255];
uint8_t payload_size;
char hexstr[1024];
uint16_t nb_bits_diff = 0;
if (argc < 3) {
usage();
return -1;
}
if (strlen(argv[1]) != strlen(argv[2])) {
printf("ERROR: payloads A & B must have same size\n");
return -1;
}
/* Get payload A hex string from command line */
memcpy(hexstr, argv[1], strlen(argv[1]));
hexstr[strlen(argv[1])] = '\0';
printf("Input hex string: %s\n", hexstr);
/* Remove spaces from the string if any */
remove_spaces(hexstr);
hexstr[strlen(hexstr)] = '\0';
printf("Removing spaces: %s\n", hexstr);
/* Convert hex string to byte array */
payload_size = strlen(hexstr) / 2;
for (j = 0; j < payload_size; j++) {
sscanf(hexstr + 2*j, "%02hhx", &payload_a[j]);
}
/* Get payload B hex string from command line */
memcpy(hexstr, argv[2], strlen(argv[2]));
hexstr[strlen(argv[2])] = '\0';
printf("Input hex string: %s\n", hexstr);
/* Remove spaces from the string if any */
remove_spaces(hexstr);
hexstr[strlen(hexstr)] = '\0';
printf("Removing spaces: %s\n", hexstr);
/* Convert hex string to byte array */
for (j = 0; j < payload_size; j++) {
sscanf(hexstr + 2*j, "%02hhx", &payload_b[j]);
}
/* Count how many bits differs */
printf("Diff: ");
for (j = 0; j < payload_size; j++) {
payload_diff[j] = payload_a[j] ^ payload_b[j];
printf("%02X ", payload_diff[j]);
}
printf("\n");
for (j = 0; j < payload_size; j++) {
for (i = 7; i >= 0; i--) {
printf("%u", TAKE_N_BITS_FROM(payload_diff[j], i, 1));
if (TAKE_N_BITS_FROM(payload_diff[j], i, 1) == 1) {
nb_bits_diff += 1;
}
}
printf(" ");
}
printf("\n");
printf("%u bits flipped\n", nb_bits_diff);
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void usage(void) {
printf("Missing payload hex strings for a & b\n");
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void remove_spaces(char *str)
{
/* To keep track of non-space character count */
int count = 0;
/* Traverse the given string. If current character
is not space, then place it at index 'count++' */
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[count++] = str[i]; /* here count is incremented */
}
}
str[count] = '\0';
}

View file

@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "tinymt32.h"
/* -------------------------------------------------------------------------- */
/* --- SUBFUNCTIONS DECLARATION --------------------------------------------- */
static void usage(void);
void remove_spaces(char *str);
/* -------------------------------------------------------------------------- */
/* --- MAIN FUNCTION -------------------------------------------------------- */
int main(int argc, char ** argv)
{
int j;
uint8_t dev_id[4];
uint8_t payload[255];
uint8_t payload_size;
unsigned int packet_cnt;
tinymt32_t tinymt;
char hexstr[32];
if (argc < 4) {
usage();
return -1;
}
/* Get dev_id hex string from command line */
memcpy(hexstr, argv[1], strlen(argv[1]));
hexstr[strlen(argv[1])] = '\0';
/* Remove spaces from the string if any */
remove_spaces(hexstr);
hexstr[strlen(hexstr)] = '\0';
printf("Dev_id: %s\n", hexstr);
/* Convert hex string to byte array */
payload_size = strlen(hexstr) / 2;
for (j = 0; j < 4; j++) {
sscanf(hexstr + 2*j, "%02hhx", &dev_id[j]);
}
/* Get packet count from which generate the random payload */
packet_cnt = atoi(argv[2]);
/* Get packet payload size */
payload_size = (uint8_t)atoi(argv[3]);
/* Initialize the pseudo-random generator */
tinymt.mat1 = 0x8f7011ee;
tinymt.mat2 = 0xfc78ff1f;
tinymt.tmat = 0x3793fdff;
tinymt32_init(&tinymt, packet_cnt);
/* Construct packet */
payload[0] = dev_id[0];
payload[1] = dev_id[1];
payload[2] = dev_id[2];
payload[3] = dev_id[3];
payload[4] = (uint8_t)(packet_cnt >> 24);
payload[5] = (uint8_t)(packet_cnt >> 16);
payload[6] = (uint8_t)(packet_cnt >> 8);
payload[7] = (uint8_t)(packet_cnt >> 0);
for (j = 8; j < payload_size; j++) {
payload[j] = (uint8_t)tinymt32_generate_uint32(&tinymt);
}
for (j = 0; j < payload_size; j++) {
printf("%02X ", payload[j]);
}
printf("\n");
#if 0
for (packet_cnt = 0; packet_cnt < 10; packet_cnt++) {
tinymt32_init(&tinymt, (int)packet_cnt);
payload[0] = 0xCA;
payload[1] = 0xFE;
payload[2] = 0x12;
payload[3] = 0x34;
payload[4] = (uint8_t)(packet_cnt >> 24);
payload[5] = (uint8_t)(packet_cnt >> 16);
payload[6] = (uint8_t)(packet_cnt >> 8);
payload[7] = (uint8_t)(packet_cnt >> 0);
for (j = 8; j < 16; j++) {
payload[j] = (uint8_t)tinymt32_generate_uint32(&tinymt);
}
for (j = 0; j < 16; j++) {
printf("%02X ", payload[j]);
}
printf("\n");
}
#endif
return 0;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void usage(void) {
printf("Missing parameters: ./payload_gen dev_id pkt_cnt pkt_size\n");
printf(" dev_id: hex string for 4-bytes dev_id\n");
printf(" pkt_cnt: unsigned int used to initialize the pseudo-random generator\n");
printf(" pkt_size: paylaod size in bytes [0..255]\n");
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
void remove_spaces(char *str)
{
/* To keep track of non-space character count */
int count = 0;
/* Traverse the given string. If current character
is not space, then place it at index 'count++' */
for (int i = 0; str[i]; i++) {
if (str[i] != ' ') {
str[count++] = str[i]; /* here count is incremented */
}
}
str[count] = '\0';
}