Skip to content

Commit

Permalink
Preamble changes
Browse files Browse the repository at this point in the history
* Allow custom preamble values
* Custom preamble duration
* All commands return unified response code
* New unknown command response code
* Switch to SDCC's medium memory model
* delay() is more accurate
* flip manchester bits
  • Loading branch information
ps2 committed Feb 10, 2018
1 parent a516425 commit ad01850
Show file tree
Hide file tree
Showing 25 changed files with 504 additions and 209 deletions.
52 changes: 40 additions & 12 deletions commands.c
Expand Up @@ -28,11 +28,18 @@ CommandHandler handlers[] = {
/* 9 */ cmd_read_register,
/* 10 */ cmd_set_mode_registers,
/* 11 */ cmd_set_sw_encoding,
/* 12 */ cmd_set_preamble,
};

void do_cmd(uint8_t cmd) {
if (cmd > 0 && cmd < sizeof(handlers)/sizeof(handlers[0])) {
handlers[cmd]();
} else {
while(serial_rx_avail() > 0) {
serial_rx_byte();
}
serial_tx_byte(RESPONSE_CODE_UNKNOWN_COMMAND);
serial_flush();
}
}

Expand All @@ -50,10 +57,14 @@ void cmd_set_sw_encoding() {
EncodingType encoding_type;

encoding_type = serial_rx_byte();
set_encoding_type(encoding_type);
if (set_encoding_type(encoding_type)) {
serial_tx_byte(RESPONSE_CODE_SUCCESS);
} else {
serial_tx_byte(RESPONSE_CODE_PARAM_ERROR); // Error: encoding type not supported
}
serial_flush();
}


void cmd_set_mode_registers() {
uint8_t register_mode;
uint8_t count;
Expand Down Expand Up @@ -87,7 +98,7 @@ void cmd_set_mode_registers() {
mode_registers_add(registers, addr, value);
}
}
serial_tx_byte(0);
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_flush();
}

Expand All @@ -105,51 +116,57 @@ void cmd_get_packet() {
}

void cmd_get_state() {
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_tx_str("OK");
}

void cmd_get_version() {
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_tx_str("subg_rfspy 2.0");
}

void cmd_send_packet() {
uint8_t channel;
uint8_t repeat_count;
uint8_t delay_ms;
uint16_t delay_ms;
uint16_t preamble_extend_ms;
uint8_t len;
channel = serial_rx_byte();
repeat_count = serial_rx_byte();
delay_ms = serial_rx_byte();
delay_ms = serial_rx_word();
preamble_extend_ms = serial_rx_word();
len = serial_rx_avail();
send_packet_from_serial(channel, repeat_count, delay_ms, len);
serial_tx_byte(0);
send_packet_from_serial(channel, repeat_count, delay_ms, preamble_extend_ms, len);
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_flush();
}

/* Combined send and receive */
void cmd_send_and_listen() {
uint8_t send_channel;
uint8_t repeat_count;
uint8_t delay_ms;
uint16_t delay_ms;
uint8_t listen_channel;
uint32_t timeout_ms;
uint8_t retry_count;
uint16_t preamble_extend_ms;
uint8_t result;
uint8_t len;

send_channel = serial_rx_byte();
repeat_count = serial_rx_byte();
delay_ms = serial_rx_byte();
delay_ms = serial_rx_word();
listen_channel = serial_rx_byte();
timeout_ms = serial_rx_long();
retry_count = serial_rx_byte();
preamble_extend_ms = serial_rx_word();
len = serial_rx_avail();

send_packet_from_serial(send_channel, repeat_count, delay_ms, len);
send_packet_from_serial(send_channel, repeat_count, delay_ms, preamble_extend_ms, len);
result = get_packet_and_write_to_serial(listen_channel, timeout_ms, use_pktlen);

while (result == ERROR_RX_TIMEOUT && retry_count > 0) {
resend_from_tx_buf(send_channel);
while (result == RESPONSE_CODE_RX_TIMEOUT && retry_count > 0) {
send_from_tx_buf(send_channel, preamble_extend_ms);
result = get_packet_and_write_to_serial(listen_channel, timeout_ms, use_pktlen);
retry_count--;
}
Expand All @@ -166,6 +183,7 @@ void cmd_read_register() {
uint8_t value;
addr = serial_rx_byte();
value = get_register(addr);
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_tx_byte(value);
serial_flush();
}
Expand Down Expand Up @@ -198,4 +216,14 @@ void cmd_led() {
led = serial_rx_byte();
mode = serial_rx_byte();
led_set_mode(led, mode);//0, 1, 2 = Off, On, Auto
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_flush();
}

void cmd_set_preamble() {
uint16_t preamble_word;
preamble_word = serial_rx_word();
radio_set_preamble(preamble_word);
serial_tx_byte(RESPONSE_CODE_SUCCESS);
serial_flush();
}
7 changes: 5 additions & 2 deletions commands.h
@@ -1,8 +1,11 @@
#ifndef COMMANDS_H
#define COMMANDS_H

#define ERROR_RX_TIMEOUT 0xaa
#define ERROR_CMD_INTERRUPTED 0xbb
#define RESPONSE_CODE_RX_TIMEOUT 0xaa
#define RESPONSE_CODE_CMD_INTERRUPTED 0xbb
#define RESPONSE_CODE_SUCCESS 0xdd
#define RESPONSE_CODE_PARAM_ERROR 0x11
#define RESPONSE_CODE_UNKNOWN_COMMAND 0x22

enum RegisterMode {
RegisterModeTx = 0x01,
Expand Down
7 changes: 4 additions & 3 deletions common.mk
Expand Up @@ -12,13 +12,14 @@ TARGET_BUILD := ${SERIAL_TYPE}_${BOARD_TYPE}_${RADIO_LOCALE}_${CODE_LOC_NAME}

CC=sdcc

LDFLAGS=--xram-loc 0xf000 --xram-size 0x1000 --code-loc ${CODE_LOC} --code-size 0x8000
CFLAGS=-I. -I${SERIAL_TYPE} --verbose ${RADIO_LOCALE_DEF} -D${BOARD_TYPE} ${BOARD_PARAMS} ${SERIAL_PARAMS}
LDFLAGS=--xram-loc 0xf000 --model-medium --xram-size 0x1000 --code-loc ${CODE_LOC} --code-size 0x8000
CFLAGS=-I. -I${SERIAL_TYPE} --model-medium --verbose ${RADIO_LOCALE_DEF} -D${BOARD_TYPE} ${BOARD_PARAMS} ${SERIAL_PARAMS}

default: output output/${TARGET_BUILD} output/${TARGET_BUILD}/${TARGET_BUILD}.hex

common_modules = radio.rel main.rel timer.rel encoding.rel manchester.rel \
fourbsixb.rel commands.rel delay.rel hardware.rel
fourbsixb.rel commands.rel hardware.rel packet_buffer.rel \
fifo.rel

clean:
rm -rf output/${TARGET_BUILD}
Expand Down
21 changes: 0 additions & 21 deletions delay.c

This file was deleted.

6 changes: 0 additions & 6 deletions delay.h

This file was deleted.

5 changes: 4 additions & 1 deletion encoding.c
Expand Up @@ -13,7 +13,7 @@ static void passthrough_add_raw_byte(EncoderState *state, uint8_t raw) __reentra
state->passthrough.data = raw;
}

static uint8_t passthrough_next_encoded_byte(EncoderState *state, uint8_t *encoded) __reentrant {
static uint8_t passthrough_next_encoded_byte(EncoderState *state, uint8_t *encoded, bool flush) __reentrant {
if (state->passthrough.count < 1) {
return 0;
}
Expand All @@ -30,6 +30,9 @@ static void passthrough_init_decoder(DecoderState *state) {
static uint8_t passthrough_add_encoded_byte(DecoderState *state, uint8_t encoded) __reentrant {
state->passthrough.data = encoded;
state->passthrough.count = 1;
if (encoded == 0) {
return 1;
}
return 0;
}

Expand Down
10 changes: 7 additions & 3 deletions encoding.h
Expand Up @@ -2,13 +2,15 @@
#define ENCODING_H

#include <stdint.h>
#include <stdbool.h>
#include "manchester_state.h"
#include "fourbsixb_state.h"

typedef enum {
EncodingTypeNone = 0x0,
EncodingTypeManchester = 0x1,
EncodingTypeFourbSixb = 0x02
EncodingTypeFourbSixb = 0x02,
MaxEncodingTypeValue = EncodingTypeFourbSixb
} EncodingType;

typedef struct {
Expand Down Expand Up @@ -37,14 +39,16 @@ typedef struct {
// Adds a byte to be encoded
void (*add_raw_byte)(EncoderState *state, uint8_t raw) __reentrant;
// encoded will be set to next available encoded byte.
// If flush is true, then encoder should return any partial bytes
// Return value is 0 if no more bytes are available.
uint8_t (*next_encoded_byte)(EncoderState *state, uint8_t *encoded) __reentrant;
uint8_t (*next_encoded_byte)(EncoderState *state, uint8_t *encoded, bool flush) __reentrant;
} Encoder;

typedef struct {
// Adds a byte for decoding. The return value will be 1 if the byte
// contains encoding errors, otherwise it will be 0.
uint8_t (*add_encoded_byte)(DecoderState *state, uint8_t encoded) __reentrant;
uint8_t (*add_encoded_byte)(DecoderState *state, uint8_t encoded)
__reentrant;
// decoded will be set to the next available decoded byte.
// Return value is 0 if no more bytes are available.
uint8_t (*next_decoded_byte)(DecoderState *state, uint8_t *decoded) __reentrant;
Expand Down
58 changes: 58 additions & 0 deletions fifo.c
@@ -0,0 +1,58 @@


#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "fifo.h"

static volatile uint8_t fifo_count(fifo_buffer const *b) {
return (b ? (b->head - b->tail) : 0);
}

bool fifo_full(fifo_buffer const *b) {
return (b ? (fifo_count(b) == b->buffer_len) : true);
}

bool fifo_empty(fifo_buffer const *b) {
return (b ? (fifo_count(b) == 0) : true);
}

uint8_t fifo_peek(fifo_buffer const *b) {
if (b) {
return (b->buffer[b->tail % b->buffer_len]);
}
return 0;
}

uint8_t fifo_get(fifo_buffer * b) {
uint8_t data_byte = 0;

if (!fifo_empty(b)) {
data_byte = b->buffer[b->tail % b->buffer_len];
b->tail++;
}
return data_byte;
}

bool fifo_put(fifo_buffer * b, uint8_t data_byte) {
bool status = false; /* return value */

if (b) {
/* limit the ring to prevent overwriting */
if (!fifo_full(b)) {
b->buffer[b->head % b->buffer_len] = data_byte;
b->head++;
status = true;
}
}
return status;
}

void fifo_init(fifo_buffer * b, volatile uint8_t *buffer, uint8_t size) {
if (b) {
b->head = 0;
b->tail = 0;
b->buffer = buffer;
b->buffer_len = size;
}
}
31 changes: 31 additions & 0 deletions fifo.h
@@ -0,0 +1,31 @@

/* fifo queue for use with interrupt routines. Size must be power of 2 */

#ifndef FIFO_H
#define FIFO_H

#include <stdint.h>
#include <stdbool.h>

struct fifo_buffer_t {
volatile uint8_t head; /* first byte of data */
volatile uint8_t tail; /* last byte of data */
volatile uint8_t *buffer; /* block of memory or array of data */
uint8_t buffer_len; /* length of the data */
};
typedef struct fifo_buffer_t fifo_buffer;

bool fifo_empty(fifo_buffer const *b);

bool fifo_full(fifo_buffer const *b);

uint8_t fifo_peek(fifo_buffer const *b);

uint8_t fifo_get(fifo_buffer *b);

bool fifo_put(fifo_buffer *b, uint8_t data_byte);

/* size must be a power of 2 */
void fifo_init(fifo_buffer *b, volatile uint8_t *buffer, uint8_t size);

#endif // FIFO_H
16 changes: 12 additions & 4 deletions fourbsixb.c
Expand Up @@ -16,12 +16,20 @@ void fourbsixb_add_raw_byte(EncoderState *state, uint8_t raw) __reentrant {
state->fourbsixb.bits_avail += 12;
}

uint8_t fourbsixb_next_encoded_byte(EncoderState *state, uint8_t *encoded) __reentrant {
if (state->fourbsixb.bits_avail < 8) {
uint8_t fourbsixb_next_encoded_byte(EncoderState *state, uint8_t *encoded, bool flush) __reentrant {
if (state->fourbsixb.bits_avail < 8 && !flush) {
return 0;
}
*encoded = state->fourbsixb.acc >> (8-state->fourbsixb.bits_avail);
state->fourbsixb.bits_avail -= 8;
if (state->fourbsixb.bits_avail == 0) {
return 0;
}
if (flush && state->fourbsixb.bits_avail < 8) {
*encoded = state->fourbsixb.acc << (8-state->fourbsixb.bits_avail);
state->fourbsixb.bits_avail = 0;
} else {
*encoded = state->fourbsixb.acc >> (state->fourbsixb.bits_avail-8);
state->fourbsixb.bits_avail -= 8;
}
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion fourbsixb.h
Expand Up @@ -5,7 +5,7 @@

void fourbsixb_init_encoder(EncoderState *state);
void fourbsixb_add_raw_byte(EncoderState *state, uint8_t raw) __reentrant;
uint8_t fourbsixb_next_encoded_byte(EncoderState *state, uint8_t *encoded) __reentrant;
uint8_t fourbsixb_next_encoded_byte(EncoderState *state, uint8_t *encoded, bool flush) __reentrant;
void fourbsixb_init_decoder(DecoderState *state);
uint8_t fourbsixb_add_encoded_byte(DecoderState *state, uint8_t raw) __reentrant;
uint8_t fourbsixb_next_decoded_byte(DecoderState *state, uint8_t *decoded) __reentrant;
Expand Down

0 comments on commit ad01850

Please sign in to comment.