Skip to content

Instantly share code, notes, and snippets.

@siddharthdeore
Last active December 27, 2023 03:27
Show Gist options
  • Select an option

  • Save siddharthdeore/b1aac400ec94ca53730291a92279ea0e to your computer and use it in GitHub Desktop.

Select an option

Save siddharthdeore/b1aac400ec94ca53730291a92279ea0e to your computer and use it in GitHub Desktop.
A host program to read bulk data from End Point 6 of Cypress CY7C68013A EZ-USB FX2LP USB2.0
:0400000002001132B7
:03000B000200DF11
:08006A00E4F508F50902000E9F
:03000E000201CD1F
:20007200AF82E50A60108FF005F0E50A800225E0D5F0FB42B2228FF005F0E50A800225E034
:20009200D5F0FBF452B222AF828FF005F07401800225E0D5F0FBFFE50A6004EF428022EFFF
:2000B200F4528022B200300012750A01758200120099750A00758201020099750A007582A8
:2000D20000120099750A01758201020099C021C0E0C0F0C082C083C007C006C005C004C0C4
:2000F20003C002C001C000C0D075D00074012508F508E43509F509740AB50806E4B509022F
:20011200800280081200B6E4F508F509C28C758AC0758C63D28CD0D0D000D001D002D00367
:20013200D004D005D006D007D083D082D0F0D0E0D02132C2AFC28C758E035389F0AE894314
:2001520006018E89758AC0758C63C2B9D2A9D28CD2AF2290E600E054E74410F090E601E029
:20017200FF7E0043074090E601EFF090E60B7403F00000000090E61474E2F00000000090C8
:20019200E6047480F00000000090E6047482F00000000090E6047484F00000000090E60443
:2001B2007486F00000000090E6047488F00000000090E604E4F00000000022120145750A96
:2001D20001758200120072750A0175820112007212016575B3000000000075B500000000CB
:2001F20000E5AA20E5FB7E007F00C3EF6480948250248E8274F82FF583E590F074012EFC1A
:20021200E43FFD8C8274F82DF583E5B0F074022EFEE43FFF80D490E6987402F090E699E489
:03023200F080BE9B
:06004000E478FFF6D8FD94
:20001E007900E94400601B7A009002397801759200E493F2A308B800020592D9F4DAF27569
:02003E0092FF2F
:200046007800E84400600A7901759200E4F309D8FC7800E84400600C7900900001E4F0A3C6
:04006600D8FCD9FAEF
:0D001100758121120235E582600302000EA8
:0402350075820022AC
:00000001FF
/**
@file fx2lp_ep6_in.cpp
@author siddharth deore (siddharthdeore@gmail.com)
@brief A host program to read bulk data from End Point 6 of FX2LP
Reads 512 bytes from the device.
Before running this program, the fw_ep6_fifo.ihx firmware file needs to be loaded
onto the FX2LP device using the cycfx2prog tool.
To load the firmware onto the device, run the following command:
cycfx2prog prg:fw_ep6_fifo.ihx run
To build the program, use the following command:
g++ fx2lp_ep6_in_fifo.cpp -o fx2lp_ep6_in_fifo -lusb-1.0
To run the program, use the following command:
./ep6_in_fx2lp
*/
#include <iostream>
#include <bitset> // cout bits
#include <csignal> // capture Ctrl+C
#include <thread> // sleep
#include <libusb-1.0/libusb.h> // USB
// signal handler function
volatile std::sig_atomic_t g_signalStatus;
void signalHandler(int signal)
{
g_signalStatus = signal;
}
int main()
{
// setup signal handling
std::signal(SIGINT, signalHandler);
g_signalStatus = 0;
// initialize libusb
libusb_context *ctx = nullptr;
if (libusb_init(&ctx) != 0)
{
std::cerr << "Failed: libusb_init\n";
return EXIT_FAILURE;
}
const int vid = 0x04b4;
const int pid = 0x8613;
// open USB device
libusb_device_handle *hndl = libusb_open_device_with_vid_pid(ctx, vid, pid);
if (hndl == nullptr)
{
std::cerr << "Failed: libusb_open_device_with_vid_pid\n";
libusb_exit(ctx);
return EXIT_FAILURE;
}
// claim interface
if (libusb_claim_interface(hndl, 0) < 0)
{
std::cerr << "Failed: libusb_claim_interface\n";
libusb_close(hndl);
libusb_exit(ctx);
return EXIT_FAILURE;
}
// set alternate setting
if (libusb_set_interface_alt_setting(hndl, 0, 1) < 0)
{
std::cerr << "Failed: libusb_set_interface_alt_setting\n";
libusb_release_interface(hndl, 0);
libusb_close(hndl);
libusb_exit(ctx);
return EXIT_FAILURE;
}
// read from USB device
const int timeout_ms = 1000;
const int pkt_size = 512;
int transferred;
unsigned char buf[pkt_size];
while (g_signalStatus == 0)
{
if (libusb_bulk_transfer(hndl, LIBUSB_ENDPOINT_IN | 6, buf, pkt_size, &transferred, timeout_ms) == 0)
{
for (int i = 0; i < pkt_size; i += 2)
{
std::cout << std::bitset<16>((buf[i] << 8) | buf[i + 1]) << "\n";
}
}
else
{
std::cerr << "Failed: libusb_bulk_transfer\n";
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
// release interface and close device
libusb_release_interface(hndl, 0);
libusb_close(hndl);
libusb_exit(ctx);
// handle signal
if (g_signalStatus == SIGINT)
{
std::cout << std::endl
<< "Ctrl+C captured, Exiting" << std::endl;
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment