ftdipp_mpsse: fix clk dividor to support ft2232c (and sipeed tangNano clone). Read potential return (error) from converter

This commit is contained in:
Gwenhael Goavec-Merou 2020-01-12 16:46:24 +01:00
parent f7f13ad3a7
commit bc1f11fe63
2 changed files with 36 additions and 17 deletions

View File

@ -191,15 +191,27 @@ int FTDIpp_MPSSE::setClkFreq(uint32_t clkHZ, char use_divide_by_5)
{ {
_clkHZ = clkHZ; _clkHZ = clkHZ;
uint8_t buffer[4] = { 0x08A, 0x86, 0x00, 0x00}; int ret;
uint32_t base_freq = 60000000; uint8_t buffer[4] = { TCK_DIVISOR, 0x00, 0x00};
uint32_t real_freq = 0; uint32_t base_freq;
uint16_t presc; uint32_t real_freq = 0;
uint16_t presc;
if (use_divide_by_5) { /* FT2232C has no divide by 5 instruction
base_freq /= 5; * and default freq is 12MHz
buffer[0] = 0x8B; */
} if (_ftdi->type != TYPE_2232C) {
base_freq = 60000000;
if (use_divide_by_5) {
base_freq /= 5;
mpsse_store(EN_DIV_5);
} else {
mpsse_store(DIS_DIV_5);
}
} else {
base_freq = 12000000;
use_divide_by_5 = false;
}
if ((use_divide_by_5 && _clkHZ > 6000000) || _clkHZ > 30000000) { if ((use_divide_by_5 && _clkHZ > 6000000) || _clkHZ > 30000000) {
fprintf(stderr, "Error: too fast frequency\n"); fprintf(stderr, "Error: too fast frequency\n");
@ -207,16 +219,22 @@ int FTDIpp_MPSSE::setClkFreq(uint32_t clkHZ, char use_divide_by_5)
} }
presc = (base_freq /(_clkHZ * 2)) -1; presc = (base_freq /(_clkHZ * 2)) -1;
real_freq = base_freq / ((1+presc)*2);
if (real_freq > clkHZ)
presc ++;
real_freq = base_freq / ((1+presc)*2); real_freq = base_freq / ((1+presc)*2);
display("presc : %d input freq : %d requested freq : %d real freq : %d\n", display("presc : %d input freq : %d requested freq : %d real freq : %d\n",
presc, base_freq, _clkHZ, real_freq); presc, base_freq, _clkHZ, real_freq);
buffer[2] = presc & 0xff; buffer[1] = presc & 0xff;
buffer[3] = (presc >> 8) & 0xff; buffer[2] = (presc >> 8) & 0xff;
if (ftdi_write_data(_ftdi, buffer, 4) != 4) { mpsse_store(buffer, 3);
fprintf(stderr, "Error: write for frequency\n"); ret = mpsse_write();
if (ret < 0) {
fprintf(stderr, "Error: write for frequency return %d\n", ret);
return -1; return -1;
} }
ret = ftdi_read_data(_ftdi, buffer, 4);
return real_freq; return real_freq;
} }
@ -255,6 +273,7 @@ int FTDIpp_MPSSE::mpsse_store(unsigned char *buff, int len)
int FTDIpp_MPSSE::mpsse_write() int FTDIpp_MPSSE::mpsse_write()
{ {
int ret;
if (_num == 0) if (_num == 0)
return 0; return 0;
@ -262,13 +281,13 @@ int FTDIpp_MPSSE::mpsse_write()
display("%s %d\n", __func__, _num); display("%s %d\n", __func__, _num);
#endif #endif
if (ftdi_write_data(_ftdi, _buffer, _num) != _num) { if ((ret = ftdi_write_data(_ftdi, _buffer, _num)) != _num) {
cout << "write error" << endl; cout << "write error: " << ret << " instead of " << _num << endl;
return -1; return ret;
} }
_num = 0; _num = 0;
return 0; return ret;
} }
int FTDIpp_MPSSE::mpsse_read(unsigned char *rx_buff, int len) int FTDIpp_MPSSE::mpsse_read(unsigned char *rx_buff, int len)

View File

@ -39,6 +39,7 @@ class FTDIpp_MPSSE {
unsigned int udevstufftoint(const char *udevstring, int base); unsigned int udevstufftoint(const char *udevstring, int base);
bool search_with_dev(const std::string &device); bool search_with_dev(const std::string &device);
bool _verbose; bool _verbose;
struct ftdi_context *_ftdi;
private: private:
int _vid; int _vid;
@ -51,7 +52,6 @@ class FTDIpp_MPSSE {
int _buffer_size; int _buffer_size;
int _num; int _num;
unsigned char *_buffer; unsigned char *_buffer;
struct ftdi_context *_ftdi;
}; };
#endif #endif