aboutsummaryrefslogtreecommitdiff
path: root/center_fw
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2019-04-17 16:32:43 +0900
committerjaseg <git@jaseg.net>2019-04-17 16:32:43 +0900
commit9eef62547efdf8e18009b6d6d6b3f7c80bf64e13 (patch)
tree8594db8c91ddc47614859e4eabbc9e7cf36d0d09 /center_fw
parentf6b959086671fb1bf8b2003d5c5384c7311a7735 (diff)
download8seg-9eef62547efdf8e18009b6d6d6b3f7c80bf64e13.tar.gz
8seg-9eef62547efdf8e18009b6d6d6b3f7c80bf64e13.tar.bz2
8seg-9eef62547efdf8e18009b6d6d6b3f7c80bf64e13.zip
center/fw: Add backchannel infrastructure
Untested!
Diffstat (limited to 'center_fw')
-rw-r--r--center_fw/8seg_protocol.c61
-rw-r--r--center_fw/8seg_protocol.h26
-rw-r--r--center_fw/Makefile2
-rw-r--r--center_fw/adc.c53
-rw-r--r--center_fw/adc.h16
-rw-r--r--center_fw/global.h1
-rw-r--r--center_fw/main.c91
-rw-r--r--center_fw/notes10
-rw-r--r--center_fw/protocol.c101
-rw-r--r--center_fw/protocol.h8
-rw-r--r--center_fw/protocol_test.c1
-rw-r--r--center_fw/transmit.c53
-rw-r--r--center_fw/transmit.h18
13 files changed, 360 insertions, 81 deletions
diff --git a/center_fw/8seg_protocol.c b/center_fw/8seg_protocol.c
new file mode 100644
index 0000000..0d0a2eb
--- /dev/null
+++ b/center_fw/8seg_protocol.c
@@ -0,0 +1,61 @@
+
+#include "global.h"
+#include "adc.h"
+#include "transmit.h"
+#include "8seg_protocol.h"
+
+struct cmd_if_struct {
+ struct command_if_def cmd_if;
+ int payload_len[PKT_TYPE_MAX];
+};
+
+const struct cmd_if_struct cmd_if = {{.packet_type_max=PKT_TYPE_MAX}, {
+ [PKT_TYPE_RESERVED] = 0,
+ [PKT_TYPE_SET_OUTPUTS_BINARY] = 1,
+ [PKT_TYPE_SET_GLOBAL_BRIGHTNESS] = 1,
+ [PKT_TYPE_SET_OUTPUTS] = 8,
+ [PKT_TYPE_GET_STATUS] = 0 }
+};
+
+volatile union {
+ struct status_tx status_tx;
+ uint8_t p[0];
+} tx_buf;
+
+void protocol_init() {
+ adc_configure_monitor_mode(&cmd_if.cmd_if, 20 /*us*/);
+ tx_init((uint8_t *)&tx_buf);
+}
+
+
+void handle_command(int command, uint8_t *args) {
+ static int global_brightness = 0xff;
+ switch (command) {
+ case PKT_TYPE_SET_OUTPUTS_BINARY:
+ set_outputs_binary(args[0], global_brightness);
+ break;
+
+ case PKT_TYPE_SET_GLOBAL_BRIGHTNESS:
+ global_brightness = args[0];
+ break;
+
+ case PKT_TYPE_SET_OUTPUTS:
+ set_outputs(args);
+ break;
+
+ case PKT_TYPE_GET_STATUS:
+ tx_buf.status_tx.temp_tenths_C = adc_data.temp_celsius_tenths;
+ tx_buf.status_tx.uptime_s = sys_time_seconds;
+ tx_buf.status_tx.decoding_error_cnt = decoding_error_cnt;
+ tx_buf.status_tx.protocol_error_cnt = protocol_error_cnt;
+ tx_buf.status_tx.vcc_mv = adc_data.vcc_mv;
+ tx_buf.status_tx.vin_mv = adc_data.mean_a_mv;
+ tx_buf.status_tx.vskew_mv = adc_data.mean_diff_mv;
+ tx_buf.status_tx.jitter_meas_avg_ns = jitter_meas_avg_ns;
+
+ /* Initialize transmission here, *after* all data has been copied to the buffer */
+ tx_transmit(sizeof(tx_buf.status_tx));
+ break;
+ }
+}
+
diff --git a/center_fw/8seg_protocol.h b/center_fw/8seg_protocol.h
new file mode 100644
index 0000000..2da42c4
--- /dev/null
+++ b/center_fw/8seg_protocol.h
@@ -0,0 +1,26 @@
+#ifndef __8SEG_PROTOCOL_H__
+#define __8SEG_PROTOCOL_H__
+
+enum packet_type {
+ PKT_TYPE_RESERVED = 0,
+ PKT_TYPE_SET_OUTPUTS_BINARY = 1,
+ PKT_TYPE_SET_GLOBAL_BRIGHTNESS = 2,
+ PKT_TYPE_SET_OUTPUTS = 3,
+ PKT_TYPE_GET_STATUS = 4,
+ PKT_TYPE_MAX
+};
+
+struct status_tx {
+ int16_t temp_tenths_C;
+ uint32_t uptime_s;
+ uint32_t decoding_error_cnt, protocol_error_cnt;
+ int16_t vcc_mv, vin_mv, vskew_mv;
+ uint16_t jitter_meas_avg_ns;
+};
+
+extern void set_outputs(uint8_t val[8]);
+extern void set_outputs_binary(int mask, int global_brightness);
+
+void protocol_init(void);
+
+#endif /* __8SEG_PROTOCOL_H__ */
diff --git a/center_fw/Makefile b/center_fw/Makefile
index 16cf5c7..2123b18 100644
--- a/center_fw/Makefile
+++ b/center_fw/Makefile
@@ -76,7 +76,7 @@ sources.tar.xz.zip: sources.tar.xz
sources.c: sources.tar.xz.zip
xxd -i $< | head -n -1 | sed 's/=/__attribute__((section(".source_tarball"))) =/' > $@
-main.elf: main.o startup_stm32f030x6.o system_stm32f0xx.o $(HAL_PATH)/Src/stm32f0xx_ll_utils.o base.o cmsis_exports.o ../common/8b10b.o adc.o protocol.o
+main.elf: main.c startup_stm32f030x6.s system_stm32f0xx.c $(HAL_PATH)/Src/stm32f0xx_ll_utils.c base.c cmsis_exports.c ../common/8b10b.c adc.c protocol.c 8seg_protocol.c transmit.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
$(OBJCOPY) -O ihex $@ $(@:.elf=.hex)
$(OBJCOPY) -O binary $@ $(@:.elf=.bin)
diff --git a/center_fw/adc.c b/center_fw/adc.c
index eb5d4b5..0fb8251 100644
--- a/center_fw/adc.c
+++ b/center_fw/adc.c
@@ -78,7 +78,7 @@ void adc_configure_scope_mode(uint8_t channel_mask, int sampling_interval_ns) {
}
/* Regular operation receiver mode */
-void adc_configure_monitor_mode(struct command_if_def *cmd_if, int ivl_us) {
+void adc_configure_monitor_mode(const struct command_if_def *cmd_if, int ivl_us) {
/* First, disable trigger timer, DMA and ADC in case we're reconfiguring on the fly. */
TIM1->CR1 &= ~TIM_CR1_CEN;
ADC1->CR &= ~ADC_CR_ADSTART;
@@ -168,6 +168,11 @@ static void adc_timer_init(int psc, int ivl) {
static void gdb_dump(void) {
}
+/* Called on reception of a bit. This feeds the bit to the 8b10b state machine. When the 8b10b state machine recognizes
+ * a received symbol, this in turn calls receive_symbol. Since this is called at sampling time roughly halfway into a
+ * bit being received, receive_symbol is called roughly half-way through the last bit of the symbol, just before the
+ * symbol's end.
+ */
void receive_bit(struct bit_detector_st *st, int bit) {
int symbol = xfr_8b10b_feed_bit((struct state_8b10b_dec *)&st->rx8b10b, bit);
if (symbol == -K28_1)
@@ -180,9 +185,9 @@ void receive_bit(struct bit_detector_st *st, int bit) {
st->sync = 0;
/* Fall through so we also pass the error to receive_symbol */
- GPIOA->BSRR = 1<<9;
+ GPIOA->BSRR = 1<<9; /* debug */
receive_symbol(&st->rx_st, symbol);
- GPIOA->BRR = 1<<9;
+ GPIOA->BRR = 1<<9; /* debug */
/* Debug scope logic */
/*
@@ -205,23 +210,43 @@ void receive_bit(struct bit_detector_st *st, int bit) {
*/
}
+/* From a series of detected line levels, extract discrete bits. This self-synchronizes to signal transitions. This
+ * expects base_interval_cycles to be set correctly. When a bit is detected, this calls receive_bit(st, bit). The call
+ * to receive_bit happens at the sampling point about half-way through the bit being received.
+ */
void bit_detector(struct bit_detector_st *st, int a) {
int new_bit = st->last_bit;
- int diff = a-5500;
+ int diff = a-5500; /* FIXME extract constants */
if (diff < - st->hysteresis_mv/2)
new_bit = 0;
else if (diff > st->hysteresis_mv/2)
new_bit = 1;
else
- blank();
+ blank(); /* Safety, in case we get an unexpected transition */
st->len_ctr++;
- if (new_bit != st->last_bit) {
+ if (new_bit != st->last_bit) { /* On transition */
st->last_bit = new_bit;
st->len_ctr = 0;
- st->committed_len_ctr = st->base_interval_cycles>>1;
+ st->committed_len_ctr = st->base_interval_cycles>>1; /* Commit first half of bit */
} else if (st->len_ctr >= st->committed_len_ctr) {
+ /* The line stayed constant for a longer interval than the commited length. Interpret this as a transmitted bit.
+ *
+ * +-- Master clock edges -->| - - - - |<-- One bit period
+ * | | |
+ * 1 X X X X X X X X
+ * ____/^^^^*^^^^\_______________________________________/^^^^*^^^^^^^^^*^^^^\__________________________________
+ * 0 v ^ v ^
+ * | | | |
+ * | +-------------------------------+ +---------+
+ * | | |
+ * At this point, commit 1/2 bit (until here). This When we arrive at the committed value, commit next
+ * happens in the block above. full bit as we're now right in the middle of the
+ * first bit. This happens in the line below.
+ */
+
+ /* Commit second half of this and first half of possible next bit */
st->committed_len_ctr += st->base_interval_cycles;
receive_bit(st, st->last_bit);
}
@@ -238,24 +263,28 @@ void DMA1_Channel1_IRQHandler(void) {
if (st.adc_mode == ADC_SCOPE)
return;
+ /* FIXME This code section currently is a mess since I left it as soon as it worked. Re-work this and try to get
+ * back all the useful monitoring stuff, in particular temperature. */
+
/* This has been copied from the code examples to section 12.9 ADC>"Temperature sensor and internal reference
* voltage" in the reference manual with the extension that we actually measure the supply voltage instead of
* hardcoding it. This is not strictly necessary since we're running off a bored little LDO but it's free and
* the current supply voltage is a nice health value.
*/
- // FIXME DEBUG adc_data.adc_vcc_mv = (3300 * VREFINT_CAL)/(st.adc_aggregate[VREF_CH]);
+ // FIXME DEBUG adc_data.vcc_mv = (3300 * VREFINT_CAL)/(st.adc_aggregate[VREF_CH]);
int64_t vcc = 3300;
/* FIXME debug
- int64_t vcc = adc_data.adc_vcc_mv;
+ int64_t vcc = adc_data.vcc_mv;
int64_t read = st.adc_aggregate[TEMP_CH] * 10 * 10000;
int64_t cal = TS_CAL1 * 10 * 10000;
- adc_data.adc_temp_celsius_tenths = 300 + ((read/4096 * vcc) - (cal/4096 * 3300))/43000;
+ adc_data.temp_celsius_tenths = 300 + ((read/4096 * vcc) - (cal/4096 * 3300))/43000;
*/
+ /* Calculate the line voltage from the measured ADC voltage and the used resistive divider ratio */
const long vmeas_r_total = VMEAS_R_HIGH + VMEAS_R_LOW;
- //int a = adc_data.adc_vmeas_a_mv = (st.adc_aggregate[VMEAS_A]*(vmeas_r_total * vcc / VMEAS_R_LOW)) >> 12;
- int a = adc_data.adc_vmeas_a_mv = (adc_buf[VMEAS_A]*13300) >> 12;
+ //int a = adc_data.vmeas_a_mv = (st.adc_aggregate[VMEAS_A]*(vmeas_r_total * vcc / VMEAS_R_LOW)) >> 12;
+ int a = adc_data.vmeas_a_mv = (adc_buf[VMEAS_A]*13300) >> 12;
bit_detector((struct bit_detector_st *)&st.det_st, a);
/* ISR timing measurement for debugging */
diff --git a/center_fw/adc.h b/center_fw/adc.h
index a3e9d53..105fde5 100644
--- a/center_fw/adc.h
+++ b/center_fw/adc.h
@@ -23,13 +23,13 @@
#include "protocol.h"
struct adc_measurements {
- int16_t adc_vcc_mv;
- int16_t adc_temp_celsius_tenths;
- int16_t adc_vmeas_a_mv;
- int16_t adc_vmeas_b_mv;
- int16_t adc_mean_a_mv;
- int16_t adc_mean_b_mv;
- int16_t adc_mean_diff_mv;
+ int16_t vcc_mv;
+ int16_t temp_celsius_tenths;
+ int16_t vmeas_a_mv;
+ int16_t vmeas_b_mv;
+ int16_t mean_a_mv;
+ int16_t mean_b_mv;
+ int16_t mean_diff_mv;
};
enum channel_mask {
@@ -85,7 +85,7 @@ extern volatile struct adc_measurements adc_data;
void adc_init(void);
void adc_configure_scope_mode(uint8_t channel_mask, int sampling_interval_ns);
-void adc_configure_monitor_mode(struct command_if_def *cmd_if, int ivl_us);
+void adc_configure_monitor_mode(const struct command_if_def *cmd_if, int ivl_us);
void bit_detector(struct bit_detector_st *st, int a);
void receive_bit(struct bit_detector_st *st, int bit);
diff --git a/center_fw/global.h b/center_fw/global.h
index 15a5e87..413c22b 100644
--- a/center_fw/global.h
+++ b/center_fw/global.h
@@ -47,5 +47,6 @@
extern volatile unsigned int sys_time;
extern volatile unsigned int sys_time_seconds;
+extern uint16_t jitter_meas_avg_ns;
#endif/*__GLOBAL_H__*/
diff --git a/center_fw/main.c b/center_fw/main.c
index 2340ecb..a2c6eaa 100644
--- a/center_fw/main.c
+++ b/center_fw/main.c
@@ -18,36 +18,20 @@
#include "global.h"
#include "adc.h"
+#include "8seg_protocol.h"
+#include "transmit.h"
volatile unsigned int sys_time = 0;
volatile unsigned int sys_time_seconds = 0;
+uint16_t jitter_meas_avg_ns = 0;
void TIM1_BRK_UP_TRG_COM_Handler() {
TIM1->SR &= ~TIM_SR_UIF_Msk;
}
-enum packet_type {
- PKT_TYPE_RESERVED = 0,
- PKT_TYPE_SET_OUTPUTS_BINARY = 1,
- PKT_TYPE_SET_GLOBAL_BRIGHTNESS = 2,
- PKT_TYPE_SET_OUTPUTS = 3,
- PKT_TYPE_MAX
-};
-
-struct {
- struct command_if_def cmd_if;
- int payload_len[PKT_TYPE_MAX];
-} cmd_if = {{.packet_type_max=PKT_TYPE_MAX}, {
- [PKT_TYPE_RESERVED] = 0,
- [PKT_TYPE_SET_OUTPUTS_BINARY] = 1,
- [PKT_TYPE_SET_GLOBAL_BRIGHTNESS] = 1,
- [PKT_TYPE_SET_OUTPUTS] = 8 }
-};
-
void set_drv_gpios(uint8_t val) {
int a=!!(val&1), b=!!(val&2), c=!!(val&4), d=!!(val&8);
- GPIOA->ODR &= ~(!a<<3 | !b<<7 | c<<6 | d<<4);
- GPIOA->ODR |= a<<3 | b<<7 | !c<<6 | !d<<4;
+ GPIOA->BSRR = ((!a<<3 | !b<<7 | c<<6 | d<<4)<<16) | (a<<3 | b<<7 | !c<<6 | !d<<4);
}
uint8_t out_state = 0x01;
@@ -67,44 +51,39 @@ void set_outputs_binary(int mask, int global_brightness) {
set_outputs(val);
}
+void set_load(bool load) {
+ GPIOA->BSRR = (1<<2) << (load ? 0 : 16);
+}
+
void blank(void) {
set_drv_gpios(0);
}
volatile int bit; /* FIXME */
void unblank_low(void) {
- if (bit)
- set_drv_gpios(out_state & 0xf);
- else
- set_drv_gpios(out_state >> 4);
+ if (backchannel_frame) { /* Set from protocol.c */
+ if (tx_next_bit() == 1)
+ set_load(1);
+ else /* 0; but also TX_IDLE */
+ set_load(0);
+
+ } else {
+ if (bit)
+ set_drv_gpios(out_state & 0xf);
+ else
+ set_drv_gpios(out_state >> 4);
+ }
}
void TIM3_IRQHandler(void) {
- GPIOA->BSRR = 1<<10;
+ GPIOA->BSRR = 1<<10; /* debug */
if (TIM3->SR & TIM_SR_UIF)
unblank_low();
else
blank();
TIM3->SR = 0;
- GPIOA->BRR = 1<<10;
-}
-
-void handle_command(int command, uint8_t *args) {
- static int global_brightness = 0xff;
- switch (command) {
- case PKT_TYPE_SET_OUTPUTS_BINARY:
- set_outputs_binary(args[0], global_brightness);
- break;
-
- case PKT_TYPE_SET_GLOBAL_BRIGHTNESS:
- global_brightness = args[0];
- break;
-
- case PKT_TYPE_SET_OUTPUTS:
- set_outputs(args);
- break;
- }
+ GPIOA->BRR = 1<<10; /* debug */
}
int main(void) {
@@ -127,9 +106,9 @@ int main(void) {
TIM3->CCMR2 = (6<<TIM_CCMR2_OC4M_Pos); /* PWM Mode 1 to get a clean trigger signal */
TIM3->CCER = TIM_CCER_CC4E; /* Enable capture/compare unit 4 connected to ADC */
- TIM3->PSC = 48-1;
- TIM3->CCR4 = 170-1;
- TIM3->ARR = 200-1;
+ TIM3->PSC = 48-1; /* 48MHz -> 1MHz */
+ TIM3->CCR4 = 170-1; /* CC4 is ADC trigger, fire 30us before end of cycle. */
+ TIM3->ARR = 200-1; /* 1MHz -> 5kHz */
TIM3->DIER |= TIM_DIER_UIE | TIM_DIER_CC4IE;
@@ -159,14 +138,28 @@ int main(void) {
set_drv_gpios(0);
- adc_configure_monitor_mode(&cmd_if.cmd_if, 20 /*us*/);
+ protocol_init();
+ uint32_t jitter_meas_sum = 0, jitter_meas_cnt = 0;
while (42) {
- int new = GPIOA->IDR & (1<<0);
- if (new != bit) {
+ int new = GPIOA->IDR & (1<<0); /* Sample current polarity */
+ if (new != bit) { /* Zero-crossing detected */
bit = new;
+
+ /* Store old counter value for jitter measurement. Let it overflow to handle negative offsets. */
+ int16_t cnt = (int16_t)TIM3->CNT;
+ /* Re-initialize the counter to align it with the signal edge */
TIM3->EGR |= TIM_EGR_UG;
+ /* Unblank since the update interrupt will not fire this time */
unblank_low();
+
+ /* Don't handle overflow of _sum here since this value is only for monitoring anyway */
+ jitter_meas_sum += (cnt >= 0) ? cnt : -cnt;
+ if (++jitter_meas_cnt == 4000) { /* One measurement roughly every 800ms */
+ /* Divide aggregate over 4000 us-resolution measurements by 4 -> ns-resolution average */
+ uint32_t divided = jitter_meas_sum>>2;
+ jitter_meas_avg_ns = (divided < UINT16_MAX) ? divided : UINT16_MAX;
+ }
}
/* idle */
}
diff --git a/center_fw/notes b/center_fw/notes
new file mode 100644
index 0000000..99a8b2a
--- /dev/null
+++ b/center_fw/notes
@@ -0,0 +1,10 @@
+Pink led strip details:
+at Vcc = 11.88V, the voltage across the 68R dropper resistor is 2.06V.
+Thus, I is 30mA, the forward voltage of the LEDs is 9.82V/3 = 3.27V.
+A single group of 3 LEDs dissipates 0.36W. At 60LEDs/m that's 7.2W/m.
+
+We loose 50% brightness due to the 2:1 AC multiplexing and another ~15% due to the generous blanking periods. To make up
+for that we need to scale the current by a factor of 2.35. This can be done by increasing VCC from 12.0V to about 14.5V.
+Assuming the multiplexing doesn't stop that's not a problem as the power dissipation of the LEDs and dropper resistors
+will remain the same as before. We need a safety cutoff in the circle firmware for that though so the LED tape doesn't
+burn up if something crashes (FIXME).
diff --git a/center_fw/protocol.c b/center_fw/protocol.c
index 87d46e4..dfa0d3e 100644
--- a/center_fw/protocol.c
+++ b/center_fw/protocol.c
@@ -1,59 +1,142 @@
+/* Control protocol receiver sitting between 8b10b.c and logical protocol handlers */
#include <unistd.h>
#include "protocol.h"
#include "8b10b.h"
-void reset_receiver(struct proto_rx_st *st, struct command_if_def *cmd_if) {
+volatile uint32_t decoding_error_cnt = 0, protocol_error_cnt = 0;
+volatile bool backchannel_frame = 0;
+
+/* Reset the given protocol state and register the command definition given with it. */
+void reset_receiver(struct proto_rx_st *st, const struct command_if_def *cmd_if) {
st->rxpos = -1;
st->address = 5; /* FIXME debug code */
st->cmd_if = cmd_if;
}
+/* Receive an 8b10b symbol using the given protocol state. Handle any packets matching the enclosed command definition.
+ *
+ * This method is called from adc.c during the last bit period of the symbol, just before the actual end of the symbol
+ * and start of the next symbol.
+ */
void receive_symbol(struct proto_rx_st *st, int symbol) {
+
+ if (symbol == -K28_2) { /* Backchannel marker */
+ /* This symbol is inserted into the symbol stream at regular intervals. It is not passed to the higher protocol
+ * layers but synchronizes the backchannel logic through all nodes. The backchannel works by a node putting a
+ * specified additional load of about 100mA (FIXME) on the line (1) or not (0) with all other nodes being
+ * silent. The master can detect this additional current. The backchannel is synchronized to the 8b10b frame
+ * being sent from the master, and the data is also 8b10b encoded. This means the backchannel is independent
+ * from the forward-channel.
+ *
+ * This means while the forward-channel (the line voltage) might go like the upper trace, the back-channel (the
+ * line current drawn by the node) might simultaneously look like the lower trace:
+ *
+ * Zoomed in on two master frames:
+ *
+ * |<--- D31.1 --->| |<--- D03.6 --->|
+ * Master -> Node 1 0 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 0
+ * Voltage (V) .../^^\__/^^\__/^^^^^^^^\_____/^^^^^^^^\________/^^\__/^^^^^\___...
+ *
+ * Current (I) ...\_____________________________/^^^^^V^^^^^^^^V^^V^^V^^^^^V^^\...
+ * Node -> Master 0 1
+ *
+ *
+ * Zoomed out on two node frames, or twenty master frames:
+ *
+ * Master -> Node | | | | | | | | | | | | | | | | | | |<- symbols, one after another
+ * Voltage (V) ...XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...
+ *
+ * Current (I) ...___/^^^^^\__/^^\_____/^^\__/^^^^^\_____/^^\__/^^^^^\__/^^\___...
+ * Node -> Master 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 0
+ * |<--- D22.2 --->| |<--- D09.5 --->|
+ *
+ * Note that during backchannel transmissions only one node transmits at a time, and all nodes including the
+ * transmitter keep their LEDs blanked to allow the master to more easily demodulate the transmission.
+ *
+ * This means that:
+ * * backchannel transmissions should be sparse (one per several regular symbols) to not affect brightness
+ * too much
+ * * backchannel transmissions should be spaced-out evenly and frequent enough to not cause visible flicker
+ *
+ * A consequence of this is that the backchannel has a bandwidth of only a fraction of the forward-channel. The
+ * master can dynamically adjust the frequency of the forward-channel and spacing of the backchannel markers.
+ * For 5kHz and 10% backchannel data (every tenth symbol being a backchannel symbol) the bandwidth works out to:
+ *
+ * BW(forward-channel) = 5 [kHz] / 10 [8b10b] = 500 byte/s
+ * BW(backchannel) = 5 [kHz] / 10 [8b10b] / 10 [every 10th symbol] / 10 [8b10b again] = 5 byte/s
+ *
+ * Luckily, we only use the backchannel for monitoring anyway and at ~20byte per monitoring frame we can easily
+ * monitor a bus-load (heh!) of nodes once a minute, which is enough for our purposes.
+ */
+
+ /* Blank the LEDs for the next frame to keep the bus quiet during backchannel transmission. This happens on all
+ * nodes. */
+ backchannel_frame = true;
+ return; /* We're done handling this symbol */
+ } else {
+ /* On anything else than a backchannel marker, turn off backchannel blanking for the next frame */
+ backchannel_frame = false;
+ }
+
if (symbol == -K28_1) { /* Comma/frame delimiter */
st->rxpos = 0;
/* Fall through and return and just ignore incomplete packets */
} else if (symbol == -DECODING_ERROR) {
+ if (decoding_error_cnt < UINT32_MAX)
+ decoding_error_cnt++;
goto reset;
- } else if (symbol < 0) { /* Unknown comma symbol or error */
+ } else if (symbol < 0) { /* Unknown comma symbol */
+ if (protocol_error_cnt < UINT32_MAX)
+ protocol_error_cnt++;
goto reset;
- } else if (st->rxpos == -1) {
+ } else if (st->rxpos == -1) { /* Receiver freshly reset and no comma seen yet */
return;
} else if (st->rxpos == 0) { /* First data symbol, and not an error or comma symbol */
st->packet_type = symbol & ~PKT_TYPE_BULK_FLAG;
if (st->packet_type >= st->cmd_if->packet_type_max)
- goto reset;
+ goto reset; /* Not a protocol error */
+ /* If this a bulk packet, calculate and store the offset of our portion of it. Otherwise just prime the state
+ * for receiving the indidual packet by setting the offset to the first packet byte after the address. */
int payload_len = st->cmd_if->payload_len[st->packet_type];
st->is_bulk = symbol & PKT_TYPE_BULK_FLAG;
st->offset = (st->is_bulk) ? (st->address*payload_len + 1) : 2;
st->rxpos++;
if (payload_len == 0 && st->is_bulk) {
+ /* Length-0 packet type, handle now for bulk packets as we don't know when the master will send the next
+ * comma or other symbol. For individually addressed packets, wait for the address byte. */
handle_command(st->packet_type, NULL);
goto reset;
}
- } else if (!st->is_bulk && st->rxpos == 1) {
- if (symbol != st->address)
+ } else if (!st->is_bulk && st->rxpos == 1) { /* First byte (address byte) of individually adressed packet */
+ if (symbol != st->address) /* A different node is adressed */
goto reset;
if (st->cmd_if->payload_len[st->packet_type] == 0) {
+ /* Length-0 packet type, handle now as we don't know when the master will send the next comma or other
+ * symbol. */
handle_command(st->packet_type, NULL);
goto reset;
}
- st->rxpos = 2;
+ st->rxpos++;
- } else {
- if (st->rxpos - st->offset >= 0)
+ } else { /* Receiving packet body */
+ if (st->rxpos - st->offset >= 0) {
+ /* Either we're receiving an individually adressed packet adressed to us, or we're in the middle of a bulk
+ * packet at our offset */
st->argbuf[st->rxpos - st->offset] = symbol;
+ }
st->rxpos++;
if (st->rxpos - st->offset == st->cmd_if->payload_len[st->packet_type]) {
+ /* We're at the end of either an individual packet or our portion of a bulk packet. Handle packet here. */
handle_command(st->packet_type, (uint8_t *)st->argbuf);
goto reset;
}
diff --git a/center_fw/protocol.h b/center_fw/protocol.h
index 9178c42..89c93e2 100644
--- a/center_fw/protocol.h
+++ b/center_fw/protocol.h
@@ -2,6 +2,7 @@
#define __PROTOCOL_H__
#include <stdint.h>
+#include <stdbool.h>
#define PKT_TYPE_BULK_FLAG 0x80
@@ -12,7 +13,7 @@ struct proto_rx_st {
int address;
uint8_t argbuf[8];
int offset;
- struct command_if_def *cmd_if;
+ const struct command_if_def *cmd_if;
};
struct command_if_def {
@@ -20,10 +21,13 @@ struct command_if_def {
int payload_len[0];
};
+extern volatile uint32_t decoding_error_cnt, protocol_error_cnt;
+extern volatile bool backchannel_frame;
+
/* Callback */
void handle_command(int command, uint8_t *args);
void receive_symbol(struct proto_rx_st *st, int symbol);
-void reset_receiver(struct proto_rx_st *st, struct command_if_def *cmd_if);
+void reset_receiver(struct proto_rx_st *st, const struct command_if_def *cmd_if);
#endif
diff --git a/center_fw/protocol_test.c b/center_fw/protocol_test.c
index d417a1b..4a12ef5 100644
--- a/center_fw/protocol_test.c
+++ b/center_fw/protocol_test.c
@@ -1,3 +1,4 @@
+/* Unit test file testing protocol.c */
#include <string.h>
#include <assert.h>
diff --git a/center_fw/transmit.c b/center_fw/transmit.c
new file mode 100644
index 0000000..c31c833
--- /dev/null
+++ b/center_fw/transmit.c
@@ -0,0 +1,53 @@
+
+#include "global.h"
+#include "transmit.h"
+#include "8b10b.h"
+
+struct {
+ uint8_t *buf;
+ size_t pos, len;
+ uint16_t current_symbol;
+ struct state_8b10b_enc enc;
+} tx_state = {0};
+
+volatile uint32_t tx_overflow_cnt = 0;
+
+void tx_init(uint8_t *tx_buf) {
+ tx_state.buf = tx_buf;
+ tx_state.pos = 0;
+ tx_state.current_symbol = 0;
+ xfr_8b10b_encode_reset(&tx_state.enc);
+}
+
+int tx_transmit(size_t len) {
+ if (!tx_state.buf)
+ return TX_ERR_UNINITIALIZED;
+
+ if (tx_state.len) {
+ tx_overflow_cnt++;
+ return TX_ERR_BUSY;
+ }
+
+ tx_state.len = len;
+ tx_state.current_symbol = 1;
+ return 0;
+}
+
+int tx_next_bit() {
+ if (!tx_state.len)
+ return TX_IDLE;
+
+ int sym = tx_state.current_symbol;
+ if (sym == 1) /* We're transmitting the first bit of a new frame now. */
+ sym = xfr_8b10b_encode(&tx_state.enc, tx_state.buf[tx_state.pos++]) | (1<<10);
+
+ int bit = sym&1;
+ sym >>= 1;
+
+ if (sym == 1 && tx_state.pos == tx_state.len)
+ /* We're transmitting the last bit of a transmission now. Reset state. */
+ tx_state.pos = tx_state.len = sym = 0;
+
+ tx_state.current_symbol = sym;
+ return bit;
+}
diff --git a/center_fw/transmit.h b/center_fw/transmit.h
new file mode 100644
index 0000000..dd9bcb9
--- /dev/null
+++ b/center_fw/transmit.h
@@ -0,0 +1,18 @@
+#ifndef __TRANSMIT_H__
+#define __TRANSMIT_H__
+
+#include "global.h"
+#include "8b10b.h"
+
+#define TX_IDLE (-1)
+
+#define TX_ERR_BUSY -1
+#define TX_ERR_UNINITIALIZED -2
+
+extern volatile uint32_t tx_overflow_cnt;
+
+void tx_init(uint8_t *tx_buf);
+int tx_transmit(size_t len);
+int tx_next_bit(void);
+
+#endif /* __TRANSMIT_H__ */