Skip to content

Commit

Permalink
Merge pull request #130 from davidgiven/fixing
Browse files Browse the repository at this point in the history
Fix a nasty firmware memory corruption bug
  • Loading branch information
davidgiven authored Jan 27, 2020
2 parents e517f28 + a8eca06 commit c4ef488
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 364 deletions.
620 changes: 310 additions & 310 deletions FluxEngine.cydsn/CortexM3/ARM_GCC_541/Release/FluxEngine.hex

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions FluxEngine.cydsn/FluxEngine.cyprj
Original file line number Diff line number Diff line change
Expand Up @@ -1533,20 +1533,20 @@
<CyGuid_ebc4f06d-207f-49c2-a540-72acf4adabc0 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFolderSerialize" version="3">
<CyGuid_2f73275c-45bf-46ba-b3b1-00a2fe0c8dd8 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtBaseContainerSerialize" version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep4" persistent="">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<CyGuid_0820c2e7-528d-4137-9a08-97257b946089 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemListSerialize" version="2">
<dependencies>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep4_dma.c" persistent="Generated_Source\PSoC5\USBFS_ep4_dma.c">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="SOURCE_C;CortexM3;;;" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep4_dma.h" persistent="Generated_Source\PSoC5\USBFS_ep4_dma.h">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="HEADER;;;;" />
<PropertyDeltas />
Expand All @@ -1559,20 +1559,20 @@
<CyGuid_ebc4f06d-207f-49c2-a540-72acf4adabc0 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFolderSerialize" version="3">
<CyGuid_2f73275c-45bf-46ba-b3b1-00a2fe0c8dd8 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtBaseContainerSerialize" version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep3" persistent="">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<CyGuid_0820c2e7-528d-4137-9a08-97257b946089 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemListSerialize" version="2">
<dependencies>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep3_dma.c" persistent="Generated_Source\PSoC5\USBFS_ep3_dma.c">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="SOURCE_C;CortexM3;;;" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep3_dma.h" persistent="Generated_Source\PSoC5\USBFS_ep3_dma.h">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="HEADER;;;;" />
<PropertyDeltas />
Expand All @@ -1585,20 +1585,20 @@
<CyGuid_ebc4f06d-207f-49c2-a540-72acf4adabc0 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFolderSerialize" version="3">
<CyGuid_2f73275c-45bf-46ba-b3b1-00a2fe0c8dd8 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtBaseContainerSerialize" version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep2" persistent="">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<CyGuid_0820c2e7-528d-4137-9a08-97257b946089 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemListSerialize" version="2">
<dependencies>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep2_dma.c" persistent="Generated_Source\PSoC5\USBFS_ep2_dma.c">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="SOURCE_C;CortexM3;;;" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep2_dma.h" persistent="Generated_Source\PSoC5\USBFS_ep2_dma.h">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="HEADER;;;;" />
<PropertyDeltas />
Expand All @@ -1611,20 +1611,20 @@
<CyGuid_ebc4f06d-207f-49c2-a540-72acf4adabc0 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFolderSerialize" version="3">
<CyGuid_2f73275c-45bf-46ba-b3b1-00a2fe0c8dd8 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtBaseContainerSerialize" version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep1" persistent="">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<CyGuid_0820c2e7-528d-4137-9a08-97257b946089 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemListSerialize" version="2">
<dependencies>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep1_dma.c" persistent="Generated_Source\PSoC5\USBFS_ep1_dma.c">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="SOURCE_C;CortexM3;;;" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFileSerialize" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItemSerialize" version="2" name="USBFS_ep1_dma.h" persistent="Generated_Source\PSoC5\USBFS_ep1_dma.h">
<Hidden v="False" />
<Hidden v="True" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="HEADER;;;;" />
<PropertyDeltas />
Expand Down Expand Up @@ -3347,6 +3347,6 @@
</ignored_deps>
</CyGuid_495451fe-d201-4d01-b22d-5d3f5609ac37>
<boot_component v="" />
<current_generation v="84" />
<current_generation v="114" />
</CyGuid_fec8f9e8-2365-4bdb-96d3-a4380222e01b>
</CyXmlSerializer>
2 changes: 1 addition & 1 deletion FluxEngine.cydsn/Sampler/Sampler.v
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ begin
STATE_OPCODE: /* opcode byte sent here */
state <= STATE_WAITING;

STATE_COUNTING:
STATE_COUNTING: /* counter changes here */
state <= STATE_WAITING;
endcase
end
Expand Down
Binary file modified FluxEngine.cydsn/TopDesign/TopDesign.cysch
Binary file not shown.
61 changes: 34 additions & 27 deletions FluxEngine.cydsn/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,15 @@ static void cmd_read(struct read_frame* f)
wait_until_writeable(FLUXENGINE_DATA_IN_EP_NUM);
init_capture_dma();

/* Wait for the beginning of a rotation. */
/* Wait for the beginning of a rotation, if requested. */

index_irq = false;
while (!index_irq)
;
index_irq = false;
if (f->synced)
{
index_irq = false;
while (!index_irq)
;
index_irq = false;
}

crunch_state_t cs = {};
cs.outputptr = usb_buffer;
Expand All @@ -365,32 +368,27 @@ static void cmd_read(struct read_frame* f)
/* Wait for the first DMA transfer to complete, after which we can start the
* USB transfer. */

while ((dma_writing_to_td == 0) && !index_irq)
while (dma_writing_to_td == 0)
;
dma_reading_from_td = 0;

/* Start transferring. */

int revolutions = f->revolutions;
uint32_t start_time = clock;
while (!dma_underrun)
{
CyWdtClear();

/* Have we reached the index pulse? */
if (index_irq)
{
index_irq = false;
revolutions--;
if (revolutions == 0)
break;
}

/* Wait for the next block to be read. */
while (dma_reading_from_td == dma_writing_to_td)
{
/* On an underrun, give up immediately. */
if (dma_underrun)
goto abort;

/* Also finish if the sample session is over. */
if ((clock - start_time) >= f->milliseconds)
goto abort;
}

uint8_t dma_buffer_usage = 0;
Expand All @@ -401,39 +399,48 @@ static void cmd_read(struct read_frame* f)
crunch(&cs);
dma_buffer_usage += BUFFER_SIZE - cs.inputlen;
count++;

/* If there is no available space in the output buffer, flush the buffer via
* USB and go again. */
if (cs.outputlen == 0)
{
while (USBFS_GetEPState(FLUXENGINE_DATA_IN_EP_NUM) != USBFS_IN_BUFFER_EMPTY)
{
if (index_irq || dma_underrun)
goto abort;
}

wait_until_writeable(FLUXENGINE_DATA_IN_EP_NUM);
USBFS_LoadInEP(FLUXENGINE_DATA_IN_EP_NUM, usb_buffer, BUFFER_SIZE);

cs.outputptr = usb_buffer;
cs.outputlen = BUFFER_SIZE;
}
}
dma_reading_from_td = NEXT_BUFFER(dma_reading_from_td);
}
abort:;
bool saved_dma_underrun = dma_underrun;
CyDmaChSetRequest(dma_channel, CY_DMA_CPU_TERM_CHAIN);
while (CyDmaChGetRequest(dma_channel))
;

donecrunch(&cs);
wait_until_writeable(FLUXENGINE_DATA_IN_EP_NUM);
if (!dma_underrun)
/* If there's a complete packet waiting, send it. */
if (cs.outputlen != BUFFER_SIZE)
{
if (cs.outputlen != BUFFER_SIZE)
USBFS_LoadInEP(FLUXENGINE_DATA_IN_EP_NUM, usb_buffer, BUFFER_SIZE-cs.outputlen);
USBFS_LoadInEP(FLUXENGINE_DATA_IN_EP_NUM, usb_buffer, BUFFER_SIZE);
wait_until_writeable(FLUXENGINE_DATA_IN_EP_NUM);
}
if ((cs.outputlen == BUFFER_SIZE) || (cs.outputlen == 0))
if ((cs.outputlen != 0) && (cs.outputlen != BUFFER_SIZE))
{
/* If there's a partial packet waiting, send it; this will also terminate the transfer. */
USBFS_LoadInEP(FLUXENGINE_DATA_IN_EP_NUM, usb_buffer, BUFFER_SIZE-cs.outputlen);
}
else
{
/* Otherwise just terminate the transfer. */
USBFS_LoadInEP(FLUXENGINE_DATA_IN_EP_NUM, NULL, 0);
}
wait_until_writeable(FLUXENGINE_DATA_IN_EP_NUM);
deinit_dma();

if (dma_underrun)
if (saved_dma_underrun)
{
print("underrun after %d packets");
send_error(F_ERROR_UNDERRUN);
Expand Down
3 changes: 2 additions & 1 deletion lib/decoders/decoders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void AbstractDecoder::decodeToSectors(Track& track)
r = advanceToNextRecord();
if (r != UNKNOWN_RECORD)
break;
fmr.readNextMatchingOpcode(F_OP_PULSE);
if (fmr.readNextMatchingOpcode(F_OP_PULSE) == 0)
break;
}
recordStart = fmr.tell();
if (r == DATA_RECORD)
Expand Down
3 changes: 2 additions & 1 deletion lib/fluxsource/fluxsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class FluxSource
virtual bool retryable() { return false; }
};

extern void setHardwareFluxSourceRevolutions(int revolutions);
extern void setHardwareFluxSourceRevolutions(double revolutions);
extern void setHardwareFluxSourceDensity(bool high_density);
extern void setHardwareFluxSourceSynced(bool synced);

#endif

24 changes: 20 additions & 4 deletions lib/fluxsource/hardwarefluxsource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
#include "fluxmap.h"
#include "usb.h"
#include "fluxsource/fluxsource.h"
#include "fmt/format.h"

FlagGroup hardwareFluxSourceFlags;

static IntFlag revolutions(
static DoubleFlag revolutions(
{ "--revolutions" },
"read this many revolutions of the disk",
1);
1.25);

static BoolFlag synced(
{ "--sync-with-index" },
"whether to wait for an index pulse before started to read",
false);

static IntFlag indexMode(
{ "--index-mode" },
Expand All @@ -29,6 +35,10 @@ class HardwareFluxSource : public FluxSource
HardwareFluxSource(unsigned drive):
_drive(drive)
{
usbSetDrive(_drive, high_density, indexMode);
std::cerr << "Measuring rotational speed... " << std::flush;
_oneRevolution = usbGetRotationalPeriod();
std::cerr << fmt::format("{}ms\n", _oneRevolution / 1e6);
}

~HardwareFluxSource()
Expand All @@ -40,7 +50,7 @@ class HardwareFluxSource : public FluxSource
{
usbSetDrive(_drive, high_density, indexMode);
usbSeek(track);
Bytes crunched = usbRead(side, revolutions);
Bytes crunched = usbRead(side, synced, revolutions * _oneRevolution);
auto fluxmap = std::make_unique<Fluxmap>();
fluxmap->appendBytes(crunched.uncrunch());
return fluxmap;
Expand All @@ -59,13 +69,19 @@ class HardwareFluxSource : public FluxSource
private:
unsigned _drive;
unsigned _revolutions;
nanoseconds_t _oneRevolution;
};

void setHardwareFluxSourceRevolutions(int revolutions)
void setHardwareFluxSourceRevolutions(double revolutions)
{
::revolutions.setDefaultValue(revolutions);
}

void setHardwareFluxSourceSynced(bool synced)
{
::synced.setDefaultValue(synced);
}

std::unique_ptr<FluxSource> FluxSource::createHardwareFluxSource(unsigned drive)
{
return std::unique_ptr<FluxSource>(new HardwareFluxSource(drive));
Expand Down
9 changes: 6 additions & 3 deletions lib/usb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ nanoseconds_t usbGetRotationalPeriod(void)
usb_cmd_send(&f, f.f.size);

auto r = await_reply<struct speed_frame>(F_FRAME_MEASURE_SPEED_REPLY);
return r->period_ms * 1000;
return r->period_ms * 1000000;
}

static int large_bulk_transfer(int ep, Bytes& bytes)
Expand Down Expand Up @@ -202,13 +202,16 @@ void usbTestBulkTransport()
await_reply<struct any_frame>(F_FRAME_BULK_TEST_REPLY);
}

Bytes usbRead(int side, int revolutions)
Bytes usbRead(int side, bool synced, nanoseconds_t readTime)
{
struct read_frame f = {
.f = { .type = F_FRAME_READ_CMD, .size = sizeof(f) },
.side = (uint8_t) side,
.revolutions = (uint8_t) revolutions
.synced = (uint8_t) synced
};
uint16_t milliseconds = readTime / 1e6;
((uint8_t*)&f.milliseconds)[0] = milliseconds;
((uint8_t*)&f.milliseconds)[1] = milliseconds >> 8;
usb_cmd_send(&f, f.f.size);

auto fluxmap = std::unique_ptr<Fluxmap>(new Fluxmap);
Expand Down
2 changes: 1 addition & 1 deletion lib/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern void usbRecalibrate();
extern void usbSeek(int track);
extern nanoseconds_t usbGetRotationalPeriod();
extern void usbTestBulkTransport();
extern Bytes usbRead(int side, int revolutions);
extern Bytes usbRead(int side, bool synced, nanoseconds_t readTime);
extern void usbWrite(int side, const Bytes& bytes);
extern void usbErase(int side);
extern void usbSetDrive(int drive, bool high_density, int index_mode);
Expand Down
5 changes: 3 additions & 2 deletions protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

enum
{
FLUXENGINE_VERSION = 10,
FLUXENGINE_VERSION = 11,

FLUXENGINE_VID = 0x1209,
FLUXENGINE_PID = 0x6e00,
Expand Down Expand Up @@ -133,7 +133,8 @@ struct read_frame
{
struct frame_header f;
uint8_t side;
uint8_t revolutions;
uint8_t synced;
uint16_t milliseconds;
};

struct write_frame
Expand Down
2 changes: 1 addition & 1 deletion src/fe-rpm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int mainRpm(int argc, const char* argv[])
usbSetDrive(spec.drive, false, F_INDEX_REAL);
nanoseconds_t period = usbGetRotationalPeriod();
if (period != 0)
std::cout << "Rotational period is " << period/1000 << " ms (" << 60e6/period << " rpm)" << std::endl;
std::cout << "Rotational period is " << period/1000000 << " ms (" << 60e3/period << " rpm)" << std::endl;
else
{
std::cout << "No index pulses detected from the disk. Common causes of this are:\n"
Expand Down

0 comments on commit c4ef488

Please sign in to comment.