From f519abd7d70263a221289fa345ef2cd26b98451b Mon Sep 17 00:00:00 2001 From: Jake Read <jake.read@cba.mit.edu> Date: Sat, 30 Dec 2023 02:09:49 -0500 Subject: [PATCH] prelim spi tests with mechanics intact --- rpi_spi/code/spi_controller/pi_spi.py | 48 ++++++++++++++++++---- rpi_spi/code/spi_controller/plot_stamps.py | 43 +++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 rpi_spi/code/spi_controller/plot_stamps.py diff --git a/rpi_spi/code/spi_controller/pi_spi.py b/rpi_spi/code/spi_controller/pi_spi.py index 78ec483..6c2c166 100644 --- a/rpi_spi/code/spi_controller/pi_spi.py +++ b/rpi_spi/code/spi_controller/pi_spi.py @@ -1,22 +1,54 @@ -import spidev +import spidev, time +# import numpy as np +# from plot_stamps import plot_stamps -bitrate = 1000000 +bitrate = 10000000 print(f'rate {bitrate/1e6}MBit/s bit period should be {1000000000/bitrate}ns') spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = bitrate -spi.mode = 0b00 +spi.mode = 0b10 -test_pck = bytearray(32) +pck_len = 64 +stamp_count = 10000 +# stamps = np.zeros(stamp_count) + +test_pck = bytearray(pck_len) for b in range(len(test_pck)): - test_pck[b] = b + test_pck[b] = b + 1 print(test_pck) -for i in range(1000): +# TODO will be to finish making these plots, +# need to install pandas and numpy here w a venv +# for now I'm just going to turn the rate up until things go wrong +bonked = False +for i in range(stamp_count): + if bonked: + break ret = spi.xfer(test_pck) - print(ret) + # stamps[i] = time.perf_counter() * 1e6 + start = -1 + for j in range(len(ret)): + if start == -1: + if ret[j] == 0: + start = j + continue + else: + if ret[j] != j - start: + print(f'BONK at {i} char {j} {start} {ret[j]}') + print(ret) + bonked = True + break + +spi.close() + +# plot_stamps(stamps, stamp_count, pck_len) -spi.close() \ No newline at end of file +# 1Mbit seems solid, +# 2.5Mbit and we have very occasional starvation +# 5Mbit and we have at least one bad packet every run (1/1000?) +# 5Mbit with more F_CPU (250MHz) it is obviously improved (1/5000?) +# 10Mbit at 250MHz we are back to ~ (1/2000) starvations, \ No newline at end of file diff --git a/rpi_spi/code/spi_controller/plot_stamps.py b/rpi_spi/code/spi_controller/plot_stamps.py new file mode 100644 index 0000000..805d3d0 --- /dev/null +++ b/rpi_spi/code/spi_controller/plot_stamps.py @@ -0,0 +1,43 @@ +import pandas as pd +import matplotlib.pyplot as plt + +def plot_stamps(stamps, stamp_count, pck_len): + # make df from stamps + df = pd.DataFrame({'timestamps': stamps}) + + # calculate deltas between stamps + df['deltas'] = df['timestamps'].diff() + + # clean NaN's + df = df.dropna() + + # wipe obviously-wrong deltas (i.e. the 1st, which goes 0-start-us) + df = df[df['deltas'] < 10000] + + # Plotting + fig, ax1 = plt.subplots(figsize=(11, 3)) + + ax1.set_xlim([50, 2000]) + + # Primary x-axis (time deltas) + df['deltas'].plot(kind='hist', bins=50, ax=ax1) + ax1.set_xlabel('Time-Stamp Deltas (us) and equivalent (MBits/s)') + ax1.set_ylabel(f'Frequency (of {stamp_count})') + + # get axis ticks to calculate equivalent bandwidths + x_ticks = ax1.get_xticks() + ax1.set_xticks(x_ticks) + bandwidths = [((pck_len * 8) * (1e6 / x)) / 1e6 for x in x_ticks] + ticks = [] + + for i in range(len(x_ticks)): + print(i, x_ticks[i], bandwidths[i]) + ticks.append(f"{x_ticks[i]:.0f} ({bandwidths[i]:.3f})") + + ax1.set_xticklabels(ticks) + + plt.title(f'Single-Source COBS Data Sink Deltas, pck_len={pck_len}') + + plt.tight_layout() + + plt.show() \ No newline at end of file -- GitLab