File: mpsse_interf.h

package info (click to toggle)
ponyprog 3.1.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,904 kB
  • sloc: cpp: 35,932; python: 981; sh: 565; xml: 67; makefile: 45; ansic: 38
file content (226 lines) | stat: -rw-r--r-- 5,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//=========================================================================//
//                                                                         //
//  PonyProg - Serial Device Programmer                                    //
//                                                                         //
//  Copyright (C) 1997-2025   Claudio Lanconelli                           //
//                                                                         //
//  https://github.com/lancos/ponyprog                                        //
//                                                                         //
//-------------------------------------------------------------------------//
//                                                                         //
// This program is free software; you can redistribute it and/or           //
// modify it under the terms of the GNU  General Public License            //
// as published by the Free Software Foundation; either version2 of        //
// the License, or (at your option) any later version.                     //
//                                                                         //
// This program is distributed in the hope that it will be useful,         //
// but WITHOUT ANY WARRANTY; without even the implied warranty of          //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
// General Public License for more details.                                //
//                                                                         //
// You should have received a copy of the GNU  General Public License      //
// along with this program (see LICENSE);     if not, write to the         //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
//                                                                         //
//=========================================================================//

#ifndef _MPSSE_INTERFACE_H
#define _MPSSE_INTERFACE_H

#include "businter.h"
#include "ftdi.hpp"
#include "interfconv.h"

class MpsseCommandQueue
{
  public:
	MpsseCommandQueue()
	{
		clear();
	}

	void clear()
	{
		cmdidx = 0;
	}

	int getSize()
	{
		return cmdidx;
	}

	bool append(uint8_t dat)
	{
		if (cmdidx < sizeof(cmdbuf))
		{
			cmdbuf[cmdidx++] = dat;
			return true;
		}
		else
		{
			return false;
		}
	}

	uint8_t *getBuffer()
	{
		return cmdbuf;
	}

	bool isFull()
	{
		return (cmdidx >= sizeof(cmdbuf));
	}

	bool almostFull(int n = 6)
	{
		return (cmdidx >= sizeof(cmdbuf) - n);
	}

	bool isEmpty()
	{
		return (cmdidx == 0);
	}

  private:
	uint8_t cmdbuf[1024];
	unsigned int cmdidx;
};

class MpsseInterface : public BusInterface
{
  public:
	MpsseInterface();
	virtual ~MpsseInterface();

	virtual int Open(int port);
	virtual void Close();

	virtual void SetDataOut(int sda = 1);
	virtual void SetClock(int scl = 1);
	virtual int GetDataIn();
	virtual int GetDataIn(int val);
	virtual int GetClock();
	virtual int GetClock(int val);
	virtual void SetClockData();
	virtual void ClearClockData();
	virtual int IsClockDataUP(int val);
	virtual int IsClockDataUP();
	virtual int IsClockDataDOWN(int val);
	virtual int IsClockDataDOWN();
	virtual bool CheckDataLines(int len = 1, int sda = -1, int scl = -1);

	virtual int TestPort(int port);

	int SetPower(bool onoff);
	void SetControlLine(int res = 1);

	//void List();
	virtual int Flush();

	virtual int xferBit(int &err, int b, int mode = 0);
	virtual uint8_t xferByte(int &err, uint8_t by, int mode = 0, int bpw = 8, bool lsb_first = false);
	virtual unsigned long xferWord(int &err, unsigned long word_out, int mode = 0, int bpw = 8, bool lsb_first = false);

	virtual void SetDelay(int delay);
	virtual void ShotDelay(int n = 1);

	virtual void ConfigPins(int pinum_ctrl = -1, int pinum_datain = -1, int pinum_dataout = -1, int pinum_clock = -1, int pinum_clockin = -1, int pinum_poweron = -1, int pinum_enbus = -1, int pinnum_ctrlin = -1);

	static QStringList find_all(unsigned int vendor, unsigned int product)
	{
		QStringList retList;
		struct ftdi_device_list *dlist = 0;
		struct ftdi_context ftdi;

		ftdi_init(&ftdi);
		int count = ftdi_usb_find_all(&ftdi, &dlist, vendor, product);
		if (count > 0)
		{
			// Iterate list
			struct ftdi_device_list *l = dlist;
			for (int k = 0; l != 0 && k < count; l = l->next, k++)
			{
				char man[256];
				char desc[256];
				char ser[256];
				int rv = ftdi_usb_get_strings(&ftdi, l->dev,
											  man, sizeof(man),
											  desc, sizeof(desc),
											  ser, sizeof(ser));
				if (rv == 0)
				{
					qDebug() << "Man : " << QString(man)
							 << "Desc: " << QString(desc)
							 << "Ser : " << QString(ser);
					retList << QString(ser) + " / " + QString(desc);
				}
			}
			ftdi_list_free(&dlist);
		}
		ftdi_deinit(&ftdi);

		return retList;
	}

  protected:
	int GetPresence(int mask, int val);
	int GetCtrlIn(int val);

  private:
	int InitPins();
	void DeInitPins();

	int SetFrequency(uint32_t freq);
	int SendPins(int new_data, int new_directions = -1);
	int GetPins();
	int GetLowPinsMulti(int bufsiz, uint8_t *buf, int len);

	int TestPonyProgFTStandalone();

	//utility to set/reset/toggle a pin
	unsigned int OutDataMask(int old_val, int mask, int val)
	{
		if (val == 0)
		{
			return old_val & ~mask;
		}
		else if (val == 1)
		{
			return old_val | mask;
		}
		else
		{
			return old_val ^ mask;
		}
	}
	unsigned int OutDataMask(int mask, int val)
	{
		return OutDataMask(last_data, mask, val);
	}

	Ftdi::Context ctx;

	MpsseCommandQueue cmdbuf;

	int pin_directions;

	bool ignore_last_data;
	int last_data;
	int read_data;

	int pin_ctrl;
	int pin_ctrlin;		//0 if unused
	int pin_datain;
	int pin_dataout;
	int pin_clock;
	int pin_clockin;	//0 if unused
	int pin_poweron;	//0 if unused
	int pin_enbus;		//0 if unused

	int ftdi_port;

	int test_step;
};

#endif