Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <ftdi.h>
- #define FTDI_VID 0x0403
- #define FTDI_PID 0x6010
- #define CLOCK_5X 0
- #define DIVIDE_BY 3
- #define BITMODE_BITBANG_NORMAL 1
- #define BITMODE_BITBANG_SPI 2
- #define MPSSE_CLK 12.0
- //static struct ftdi_context;
- static int get_buf(struct ftdi_context *, const unsigned char *, int);
- static int send_buf(struct ftdi_context *, const unsigned char *, int);
- int ft2232_spi_send_command(unsigned int, unsigned int, const unsigned char *, unsigned char *, int);
- int ft2232_spi_init(void);
- uint8_t cs_bits = 0x08;
- static uint8_t pindir = 0x0b;
- static struct ftdi_context ftdic_context;
- int main(void) {
- int ret;
- unsigned int i, j;
- unsigned char reg;
- unsigned char command;
- unsigned char *commandtwo = malloc(2);
- char registers[65] = "\x00\x60\x02\x8c\x04\x0b\x06\x13\x08\x0c\x0a\xcf\x0c\x7d\x0e\x68\x10\x46\x12\x7d\x14\x68\x16\x46\x18\x38\x1a\x00\x1c\x18\x1e\x00\x20\xa3\x22\x30\x24\x1e\x26\x07\x28\x00\x2a\x80\x2c\x69\x2e\x81\x30\x7e\x32\x96\x34\x70\x36\xbc\x38\x3f\x3a\x00\x3c\xe8\x3e\xc0";
- // char registers[33] = "\x60\x8c\x0b\x13\x0c\xcf\x7d\x68\x46\x7d\x68\x46\x38\x00\x18\x00\xa3\x30\x1e\x07\x00\x80\x69\x81\x7e\x96\x70\xbc\x3f\x00\xe8\xc0";
- // Initialize SPI.
- ret = ft2232_spi_init();
- // Set the registers to the correct state.
- ft2232_spi_send_command(64, 0, registers, NULL, 0);
- // Read back the registers that we've written. This works, sort of, but it's answering the request sent 2 requests ago.
- for (i = 0; i < 32; i++) {
- j = (i << 1);
- sprintf(&command, "%c", j);
- ft2232_spi_send_command(1, 1, &command, ®, 0);
- printf("%u 0x%02x: 0x%02x\n", i, j, reg);
- }
- // Trying to read out of the fifo here.
- printf("\nNow let's try to see some data...\n");
- for (i = 0; i < 80000; i++) {
- ft2232_spi_send_command(0, 1, NULL, ®, 1);
- printf("%02x\n", reg);
- }
- return EXIT_SUCCESS;
- }
- static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size) {
- int r;
- r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
- if (r < 0) {
- printf("ftdi_read_data: %d, %s\n", r,
- ftdi_get_error_string(ftdic));
- return 1;
- }
- return 0;
- }
- static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size) {
- int r;
- r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
- if (r < 0) {
- printf("ftdi_write_data: %d, %s\n", r,
- ftdi_get_error_string(ftdic));
- return 1;
- }
- return 0;
- }
- int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr, int config_or_data) {
- struct ftdi_context *ftdic = &ftdic_context;
- static unsigned char *buf = NULL;
- /* failed is special. We use bitwise ops, but it is essentially bool. */
- int i = 0, ret = 0, failed = 0;
- int bufsize;
- static int oldbufsize = 0;
- if (writecnt > 65536 || readcnt > 65536)
- return -1;
- /* buf is not used for the response from the chip. */
- if ((writecnt + 9) > (269)) bufsize = writecnt + 9;
- else bufsize = 269;
- /* Never shrink. realloc() calls are expensive. */
- if (bufsize > oldbufsize) {
- buf = realloc(buf, bufsize);
- if (!buf) {
- exit(1);
- }
- oldbufsize = bufsize;
- }
- /*
- * Minimize USB transfers by packing as many commands as possible
- * together. If we're not expecting to read, we can assert CS#, write,
- * and deassert CS# all in one shot. If reading, we do three separate
- * operations.
- */
- buf[i++] = SET_BITS_LOW;
- buf[i++] = 0 & ~cs_bits; /* assertive */
- buf[i++] = pindir;
- if (writecnt) {
- buf[i++] = 0x11;
- buf[i++] = (writecnt - 1) & 0xff;
- buf[i++] = ((writecnt - 1) >> 8) & 0xff;
- memcpy(buf + i, writearr, writecnt);
- i += writecnt;
- }
- /*
- * Optionally terminate this batch of commands with a
- * read command, then do the fetch of the results.
- */
- if (readcnt) {
- buf[i++] = 0x20;
- buf[i++] = (readcnt - 1) & 0xff;
- buf[i++] = ((readcnt - 1) >> 8) & 0xff;
- ret = send_buf(ftdic, buf, i);
- failed = ret;
- /* We can't abort here, we still have to deassert CS#. */
- i = 0;
- if (ret == 0) {
- /*
- * FIXME: This is unreliable. There's no guarantee that
- * we read the response directly after sending the read
- * command. We may be scheduled out etc.
- */
- ret = get_buf(ftdic, readarr, readcnt);
- failed |= ret;
- /* We can't abort here either. */
- }
- }
- buf[i++] = SET_BITS_LOW;
- buf[i++] = cs_bits;
- buf[i++] = pindir;
- ret = send_buf(ftdic, buf, i);
- failed |= ret;
- return failed ? -1 : 0;
- }
- int ft2232_spi_init(void) {
- int f;
- struct ftdi_context *ftdic = &ftdic_context;
- unsigned char buf[512];
- enum ftdi_interface ft2232_interface = INTERFACE_A;
- // cs_bits = 0x18;
- // pindir = 0x1b;
- if (ftdi_init(ftdic) < 0) {
- printf("ftdi_init failed\n");
- return EXIT_FAILURE; // TODO
- }
- f = ftdi_usb_open(ftdic, FTDI_VID, FTDI_PID);
- if (f < 0 && f != -5) {
- printf("Unable to open FTDI device: %d (%s)\n", f, ftdi_get_error_string(ftdic));
- exit(-1); // TODO
- }
- if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
- printf("Unable to select interface: %s\n", ftdic->error_str);
- }
- if (ftdi_usb_reset(ftdic) < 0) {
- printf("Unable to reset FTDI device\n");
- }
- if (ftdi_set_latency_timer(ftdic, 2) < 0) {
- printf("Unable to set latency timer\n");
- }
- if (ftdi_write_data_set_chunksize(ftdic, 512)) {
- printf("Unable to set chunk size\n");
- }
- if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
- printf("Unable to set bitmode to SPI\n");
- }
- buf[0] = 0x86; /* command "set divisor" */
- /* valueL/valueH are (desired_divisor - 1) */
- buf[1] = (DIVIDE_BY - 1) & 0xff;
- buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff;
- if (send_buf(ftdic, buf, 3))
- return -1;
- /* Disconnect TDI/DO to TDO/DI for loopback. */
- buf[0] = 0x85;
- if (send_buf(ftdic, buf, 1))
- return -1;
- buf[0] = SET_BITS_LOW;
- buf[1] = cs_bits;
- buf[2] = pindir;
- if (send_buf(ftdic, buf, 3))
- return -1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement