У меня FTDI C232HM, который не реагирует на неправильную команду (0xAA)
Мой код голый. Код, который работает последовательно при компиляции в CentOS 6, редко запускается при компиляции в CentOS 7. Ни один из них не выполняется 100% или 0% времени. Я боюсь, что я не понимаю время (задержки, скрытая настройка, последовательность), которые могут потребоваться. Я, по сути, преобразую пример FTDI из AN-129 в пример LibFTDI (Intra2net) для тестирования.
Документация FTDI использует их библиотеки, и в их таблицах данных, похоже, отсутствует конкретность, которая покажет мою ошибку.
Я могу включить код, и он может помочь кому-то, очень знакомому с этой частью, но я действительно надеюсь, что это приведет к документации, предназначенной для определения времени.
Один пример из кода FTDI AN-129 имеет 50 секунд, да 50 секунд, задержку для установки микросхемы. Процитирую:" Sleep(50); // Дождемся, пока все USB-устройство завершится и заработает"
Есть несколько примеров арендодателей из FTDI и LibFTDI, но я хочу использовать режим MPSSE, а их очень мало.
Уве Бонн представил несколько примеров кода, но я также не смог заставить их работать. Похоже, они "в процессе" без последних примеров.
Я думал об этом в течение ночи, и я собираюсь опубликовать код.
/*
* Using LibFTDI and LibUSB
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ftdi.h>
#define bufSize 1024
#define TRST 0x80 // active low
#define TMS 8 // active high
#define TDO 4 // input, unused, for completeness
#define TDI 2 // active high
#define TCK 1 // active high
typedef enum{false,true} bool;
int Transmit(struct ftdi_context *ftdi, unsigned char *outputBuffer, short *outBytes);
int Receive(struct ftdi_context *ftdi, unsigned char *inputBuffer, short *inBytes);
int Transmit(struct ftdi_context *ftdi, unsigned char *outputBuffer, short *outBytes)
{
int status = 0;
status = ftdi_write_data(ftdi, outputBuffer, *outBytes);
/* Get ready for the next Transmit */
*outBytes = 0;
if(status < 0) printf("*** Error in Transmit, status = %d\n",status);
return (status);
}
int Receive(struct ftdi_context *ftdi, unsigned char *inputBuffer, short *inBytes)
{
int status = 0;
status = ftdi_read_data(ftdi, inputBuffer, *inBytes);
if(status < 0)printf("Status error %d in file %s func %s() line %d\n",status,__FILE__,__func__,__LINE__-1);
if(status == 0)printf("No data available! Expected %d bytes\n",*inBytes);
*inBytes = 0;
return (status);
}
/* Arguments are ignored for now */
int main(int argc, char **argv)
{
struct ftdi_context *ftdi;
int status;
unsigned char outputBuffer[bufSize], inputBuffer[bufSize];
short outBytes, inBytes;
outBytes = inBytes = 0;
unsigned short modemStatus;
setbuf(stdout,NULL); // print immediate
/* Value of clock divisor, SCL Frequency = 60/((1+0x0258)*2) (MHz) = 50khz */
long int clockDivisor = 0x0258; //50 Khz
/* Initialize the primary and only wired channel by creating a pointer
to the ftdi_context structure */
if ((ftdi = ftdi_new()) == 0)
{
printf("ERROR: FTDI_NEW failed\n");
return EXIT_FAILURE;
}
ftdi_set_interface(ftdi, INTERFACE_A);
status = ftdi_usb_open(ftdi, 0x0403, 0x6014);
if (status < 0 && status != -5)
{
fprintf(stderr, "ERROR: Unable to open ftdi device: %d (%s)\n", status, ftdi_get_error_string(ftdi));
ftdi_free(ftdi);
exit(-1);
}
printf("\n\nFTDI open succeeded on 0403:6014 with %d\n",status);
if(ftdi_usb_reset(ftdi) < 0)
{
printf("ERROR: Unable to reset FTDI device");
return(-1);
}
printf("Enabling MPSSE (Multi-Protocol Synchronous Serial Engine) on C232HM\n");
/* Set the Bit Mask to enable JTAG manipulation.
* 0b10001101 means:
*
* Bit 7 will control TRST as an output
* Bit 6-4 are unused inputs
* Bit 3 is Test Mode, an output
* Bit 2 is Test Data Out (Data In), an input
* Bit 1 is Test Data In (Data Out), an output
* Bit 0 is Test Clock, an output
*/
/* Unless I'm mistaken, I can now talk to the FTDI chip */
/* Clear out anything left over */
status |= ftdi_usb_reset(ftdi);
status |= ftdi_usb_purge_buffers(ftdi);
status |= ftdi_set_event_char(ftdi, 0, 0); // 2533 in ftdi.c
status |= ftdi_set_error_char(ftdi, 0, 0); // 2562 in ftdi.c
status |= ftdi_set_latency_timer(ftdi, 16); // 16 milliseconds, default
/* Here is where FTDI code example put a.... */
// Sleep(50);
if (status != 0)
{
printf("ERROR: In setup, abort!\n");
ftdi_usb_reset(ftdi);
ftdi_usb_close(ftdi);
ftdi_deinit(ftdi);
return 1;
}
if (1)
{
printf("*** Illegal command test, sending 0xAA, bad command! ***\n");
outputBuffer[outBytes++] = 0xAA; // error illegal command
status = Transmit(ftdi, outputBuffer, &outBytes);
if(status < 0)
{
printf("ERROR: Illegal command detected? %d %s\n",status,ftdi_get_error_string(ftdi));
}
usleep(500); //5ms also tried .5ms and 50 ms
/* Try something with the Modem Status reg
*
* Is there data waiting?
*/
status = ftdi_poll_modem_status(ftdi,&modemStatus);
if(status == 0)printf("All Fine\n");
if(status == -1)printf("Unable to retrieve status info\n");
if(status == -2)printf("USB device unavailable\n");
/* and now you have to read into the buffer! */
inBytes = 2;
status = Receive(ftdi, inputBuffer, &inBytes);
/* this works, receive buffer contains 0xFA, 0xAA meaning bad command 0xAA */
if(status < 0) //|| (inputBuffer[0] != 0xFA))
{
printf("ERROR: Read %d bytes with code %d, %s\n",inBytes,status,ftdi_get_error_string(ftdi));
}
printf("*** Illegal command test, end of test! ***\n\n");
}
/* note: no such thing as ftdi_unset_bitmode or ftdi_disable_bitmode */
printf("disabling mpsse mode\n");
/* Write data to flush FTDI chip??? */
status = ftdi_usb_purge_buffers(ftdi);
status |= ftdi_usb_reset(ftdi);
status |= ftdi_usb_close(ftdi);
if(status != 0) printf("ERROR: Failure at exit, status = %d\n",status);
/* No status for deinit() it works or it doesn't */
ftdi_free(ftdi); // and deinit...
printf("\n\n");
return 0;
}
Хм... копия не поддерживает регулярное форматирование и отбрасывает содержимое включений, но это не главное, поэтому я надеюсь, что кто-то увидит ошибку.