Skip to content

Commit

Permalink
Crudely hack in a lock against writing, as I'll have to wait for the v3
Browse files Browse the repository at this point in the history
firmware.
  • Loading branch information
davidgiven committed Oct 1, 2024
1 parent d00fba0 commit 33f3c97
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/usb/applesauceusb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include "lib/decoders/fluxmapreader.h"
#include <unistd.h>

class ApplesauceUsb;
static ApplesauceUsb* instance;

static uint32_t ss_rand_next(uint32_t x)
{
return (x & 1) ? (x >> 1) ^ 0x80000062 : x >> 1;
Expand Down Expand Up @@ -38,8 +41,7 @@ static Bytes applesauceReadDataToFluxEngine(
return fluxmap.rawBytes();
}

static Bytes fluxEngineToApplesauceWriteData(
const Bytes& fldata, nanoseconds_t clock)
static Bytes fluxEngineToApplesauceWriteData(const Bytes& fldata)
{
Fluxmap fluxmap(fldata);
FluxmapReader fmr(fluxmap);
Expand All @@ -52,7 +54,7 @@ static Bytes fluxEngineToApplesauceWriteData(
if (!fmr.findEvent(F_BIT_PULSE, ticks))
break;

uint32_t applesauceTicks = (double)ticks * NS_PER_TICK / clock;
uint32_t applesauceTicks = (double)ticks * NS_PER_TICK;
while (applesauceTicks >= 0xffff)
{
bw.write_le16(0xffff);
Expand Down Expand Up @@ -88,6 +90,8 @@ class ApplesauceUsb : public USB
s);

doCommand("client:v2");

atexit([](){ delete instance; });
}

~ApplesauceUsb()
Expand Down Expand Up @@ -260,6 +264,8 @@ class ApplesauceUsb : public USB
error("cannot write --- disk is write protected");
if (sendrecv("?safe") == "+")
error("cannot write --- Applesauce 'safe' switch is on");
if (sendrecv("?vers") < "0300")
error("cannot write --- need Applesauce firmware 2.0 or above");
}

public:
Expand All @@ -279,7 +285,7 @@ class ApplesauceUsb : public USB
doCommand("disk:wclear");

Bytes asdata =
fluxEngineToApplesauceWriteData(fldata, _config.write_clock_ns());
fluxEngineToApplesauceWriteData(fldata);
doCommand(fmt::format("data:>{}", asdata.size()));
_serial->write(asdata);
checkCommandResult(_serial->readLine());
Expand Down Expand Up @@ -320,7 +326,8 @@ class ApplesauceUsb : public USB

USB* createApplesauceUsb(const std::string& port, const ApplesauceProto& config)
{
return new ApplesauceUsb(port, config);
instance = new ApplesauceUsb(port, config);
return instance;
}

// vim: sw=4 ts=4 et
4 changes: 2 additions & 2 deletions lib/usb/greaseweazleusb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ class GreaseweazleUsb : public USB

do_command({CMD_GET_FLUX_STATUS, 2});

Bytes fldata = greaseWeazleToFluxEngine(buffer, _clock);
Bytes fldata = greaseweazleToFluxEngine(buffer, _clock);
if (synced)
fldata = stripPartialRotation(fldata);
return fldata;reaseweazle
return fldata;
}

void write(int side,
Expand Down
2 changes: 0 additions & 2 deletions lib/usb/usb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ message ApplesauceProto {
[(help) = "Applesauce serial port to use"];
optional bool verbose = 2
[(help) = "Enable verbose protocol logging", default = false];
optional float write_clock_ns = 3
[(help) = "Write clock speed", default = 22.45];
}

message UsbProto {
Expand Down

0 comments on commit 33f3c97

Please sign in to comment.