Repository navigation
Expand file tree
/
Copy pathusbiss.h
More file actions
425 lines (348 loc) · 13 KB
/
Copy pathusbiss.h
File metadata and controls
425 lines (348 loc) · 13 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/***********************************************************************
@copyright : Siemens AG
@license : GPLv3
@author : Andreas Kaeberlein
@address : Clemens-Winkler-Strasse 3, 09116 Chemnitz
@maintainer : Andreas Kaeberlein
@telephone : +49 371 4810-2108
@email : andreas.kaeberlein@siemens.com
@file : usbiss.h
@date : 2023-06-29
@see : https://github.com/andkae/usbiss
@see : http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm
@brief : USB-ISS driver
Driver for USB-ISS:
* I2C access functions
***********************************************************************/
// Define Guard
#ifndef __USB_ISS_H
#define __USB_ISS_H
/**
* @defgroup USBISS_MISC
* help constants for programm
* @{
*/
#define USBISS_UART_BAUD_RATE (230400) /**< default baudrate */
#define USBISS_ID (0x07) /**< module ID */
#define USBISS_FW_MIN (8) /**< minimal USB-ISS firmware version for proper operation */
#define USBISS_VCP_VID "04d8" /**< USB-ISS Virtual COM Port Vendor ID */
#define USBISS_VCP_PID "ffee" /**< USB-ISS Virtual COM Port Device/Product ID */
#define USBISS_I2C_SCAN_RETRY (3) /**< In I2C Scan numb er of retries before giving up, f.e. bus is occupied by another master */
/** @} */ // USBISS_MISC
/**
* @defgroup USBISS_CMD
* USBISS setup commands
*
* @see http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm
*
* @{
*/
#define USBISS_CMD 0x5A /**< Send Setup command */
#define USBISS_I2C_DIRECT 0x57 /**< I2C direct command */
#define USBISS_ISS_VERSION 0x01 /**< Returns 3 bytes, the module ID (7), firmware version (currently 2), and the current operating mode. */
#define USBISS_SET_ISS_MODE 0x02 /**< Sets operating mode, I2C/SPI/Serial etc. */
#define USBISS_GET_SER_NUM 0x03 /**< Returns the modules unique 8 byte USB serial number. */
#define USBISS_CMD_ACK 0xFF /**< mode setting frames accepted */
#define USBISS_CMD_NCK 0x00 /**< mode setting frame not accepted */
/** @} */ // USBISS_CMD
/**
* @defgroup USBISS_MODE
* USBISS setup commands
*
* @see http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm
*
* @{
*/
#define USBISS_IO_MODE 0x00
#define USBISS_IO_CHANGE 0x10
#define USBISS_I2C_S_20KHZ 0x20
#define USBISS_I2C_S_50KHZ 0x30
#define USBISS_I2C_S_100KHZ 0x40
#define USBISS_I2C_S_400KHZ 0x50
#define USBISS_I2C_H_100KHZ 0x60
#define USBISS_I2C_H_400KHZ 0x70
#define USBISS_I2C_H_1000KHZ 0x80
#define USBISS_SPI_MODE 0x90
#define USBISS_SERIAL 0x01
/** @} */ // USBISS_MODE
/**
* @defgroup USBISS_I2C_DIRECT
* USBISS setup commands
*
* @see http://www.robot-electronics.co.uk/htm/usb_iss_i2c_tech.htm
*
* @{
*/
#define USBISS_I2C_CHUNK (16) /**< Maximum chunk size for packet */
#define USBISS_I2C_START (0x01) /**< send start sequence */
#define USBISS_I2C_RESTART (0x02) /**< send restart sequence */
#define USBISS_I2C_STOP (0x03) /**< send stop sequence */
#define USBISS_I2C_NCK (0x04) /**< send NACK after next read */
#define USBISS_I2C_READ (0x20) /**< 0x2n reads n+1 bytes, f.e 0x20 reads one byte */
#define USBISS_I2C_WRITE (0x30) /**< 0x3n writes n+1 bytes */
/** @} */ // USBISS_I2C_DIRECT
/**
* @defgroup USBISS_I2C
*
* Direction (Read or Write) of I2C communication
*
* @{
*/
#define USBISS_I2C_WR (0x00) /**< I2C Write Bit */
#define USBISS_I2C_RD (0x01) /**< I2C Read Bit */
/** @} */
/**
* @defgroup USBISS_ERROR
* USBISS Error Codes
*
* @see http://www.robot-electronics.co.uk/htm/usb_iss_i2c_tech.htm
*
* @{
*/
#define USBISS_ERO_ID1 (0x01) /**< Ero 1 */
#define USBISS_ERO_ID1_STR "No ACK from device" /**< Ero 1 String */
#define USBISS_ERO_ID2 (0x02) /**< Ero 2 */
#define USBISS_ERO_ID2_STR "Buffer Overflow, You must limit the frame to < 60 bytes" /**< Ero 2 String */
#define USBISS_ERO_ID3 (0x03) /**< Ero 3 */
#define USBISS_ERO_ID3_STR "Buffer Underflow, More write data was expected than sent" /**< Ero 3 String */
#define USBISS_ERO_ID4 (0x04) /**< Ero 4 */
#define USBISS_ERO_ID4_STR "Unknown command" /**< Ero 4 String */
#define USBISS_ERO_ID5 (0x05) /**< Ero 5 */
#define USBISS_ERO_ID5_STR "Unknown command" /**< Ero 5 String */
#define USBISS_ERO_ID6 (0x06) /**< Ero 6 */
#define USBISS_ERO_ID6_STR "Internal Error 1" /**< Ero 6 String */
#define USBISS_ERO_ID7 (0x07) /**< Ero 7 */
#define USBISS_ERO_ID7_STR "Internal Error 2" /**< Ero 7 String */
/** @} */ // USBISS_ERROR
/**
* @defgroup MIN_MAX
*
* @brief MIN/MAX
*
* Calculates MIN/MAX of two numbers
*
* @since 2023-07-07
* @see https://stackoverflow.com/questions/3437404/min-and-max-in-c
*/
#define usbiss_max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define usbiss_min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
/** @} */ // MIN_MAX
/**
* @typedef t_usbiss
*
* @brief common handles
*
* handles all related information for usbiss operation
*
* @since June 29, 2023
* @author Andreas Kaeberlein
*/
typedef struct t_usbiss {
uint8_t uint8MsgLevel; /**< mesage level */
char charPort[128]; /**< path to UART part */
uint32_t uint32BaudRate; /**< UART baud rate */
struct simple_uart *uart; /**< handle to simple uart */
uint8_t uint8Fw; /**< firmware version */
uint8_t uint8Mode; /**< current mode */
char charSerial[10]; /**< serial number */
uint8_t uint8IsOpen; /**< connection to usbiss is open */
} t_usbiss;
/* C++ compatibility */
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
/**
* @brief mode-to-human
*
* converts USBISS mode to human readable string
*
* @param[in,out] self common handle #t_usbiss
* @return string sstring with human readable transfer mode
* @since June 30, 2023
* @author Andreas Kaeberlein
*/
char *usbiss_mode_to_human(uint8_t mode);
/**
* @brief ero-to-str
*
* converts USBISS error code into human readable string
*
* @param[in,out] self common handle #t_usbiss
* @return string sstring with human readable error string
* @since July 7, 2023
* @author Andreas Kaeberlein
*/
char *usbiss_ero_str(uint8_t error);
/**
* @brief Is I2C
*
* Check if USB-ISS has I2C mode
*
* @param[in] mode USB-ISS mode
* @return int I2C mode active?
* @retval 0 I2C mode
* @retval -1 Non I2C mode
* @since July 18, 2023
* @author Andreas Kaeberlein
*/
int usbiss_is_i2c_mode( uint8_t mode );
/**
* @brief list UART ports
*
* Lists system UART ports
*
* @param[in,out] uart USB-ISS matching UART ports
* @param[in] len maximum string length
* @param[in] sep separator between suitable ports
* @return int number of USB-ISS matching ports
* @since July 21, 2023
* @author Andreas Kaeberlein
*/
int usbiss_list_uart( char *uart, size_t len, const char sep[] );
/**
* @brief init
*
* initializes common data structure
*
* @param[in,out] self common handle #t_usbiss
* @return int state
* @retval 0 OK
* @since June 29, 2023
* @author Andreas Kaeberlein
*/
int usbiss_init( t_usbiss *self );
/**
* @brief verbose
*
* set verbose level
*
* @param[in,out] self common handle #t_usbiss
* @param[in] verbose 0: debug messages disabled, 1: debug messages enabled
* @since June 30, 2023
* @author Andreas Kaeberlein
*/
void usbiss_set_verbose( t_usbiss *self, uint8_t verbose );
/**
* @brief open
*
* open connection to USBISS
*
* @param[in,out] self common handle #t_usbiss
* @param[in] port System Path to UART terminal of USBISS
* @param[in] baud desired UART transfer rate
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since June 30, 2023
* @author Andreas Kaeberlein
*/
int usbiss_open( t_usbiss *self, char* port, uint32_t baud );
/**
* @brief close
*
* close connection to USBISS
*
* @param[in,out] self common handle #t_usbiss
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since July 13, 2023
* @author Andreas Kaeberlein
*/
int usbiss_close( t_usbiss *self );
/**
* @brief Transfer Mode
*
* set up USB-ISS transfer mode
*
* @param[in,out] self common handle #t_usbiss
* @param[in] mode USBISS transfer mode
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since July 3, 2023
* @author Andreas Kaeberlein
*/
int usbiss_set_mode( t_usbiss *self, const char* mode );
/**
* @brief Scan I2C devices
*
* Scan on I2C bus for devices, uses write access for probe
* but now byte is written
* 1) apply Startbit with write flag
* 2) apply Stopbit
*
* @param[in,out] self common handle #t_usbiss
* @param[in] start start address of i2c scan
* @param[in] stop stop address of i2c scan
* @param[in] *i2c present i2c devices on bus
* @param[in] len max numb er of entries in *i2c
* @return int number of found devices or error
* @retval >= 0 present I2C devices
* @retval < 0 FAIL
* @since Novembre 22, 2023
* @author Andreas Kaeberlein
*/
int usbiss_i2c_scan( t_usbiss *self, int8_t start, int8_t stop, int8_t* i2c, uint8_t len );
/**
* @brief i2c-write
*
* write arbitrary sized data packet to i2c device
*
* @param[in,out] self common handle #t_usbiss
* @param[in] adr7 Seven Bit I2C address
* @param[in,out] data write data
* @param[in] len number of bytes in data
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since July 7, 2023
* @author Andreas Kaeberlein
*/
int usbiss_i2c_wr( t_usbiss *self, uint8_t adr7, void* data, size_t len );
/**
* @brief i2c-read
*
* read arbitrary sized data packet from i2c device
*
* @param[in,out] self common handle #t_usbiss
* @param[in] adr7 Seven Bit I2C address
* @param[in,out] data read data
* @param[in] len number of requested read bytes
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since July 11, 2023
* @author Andreas Kaeberlein
*/
int usbiss_i2c_rd( t_usbiss *self, uint8_t adr7, void* data, size_t len );
/**
* @brief i2c-write-read
*
* writes to i2c devices, sents repeated start for direction change and reads from i2c device
* write and read data takes places in the same buffer, that meamns the write buffer
* will be overwritten by read data.
*
* @param[in,out] self common handle #t_usbiss
* @param[in] adr7 Seven Bit I2C address
* @param[in,out] data read data
* @param[in] wrLen number of bytes to write to slave
* @param[in] len number of requested bytes from slave
* @return int state
* @retval 0 OK
* @retval -1 FAIL
* @since July 11, 2023
* @author Andreas Kaeberlein
*/
int usbiss_i2c_wr_rd( t_usbiss *self, uint8_t adr7, void* data, size_t wrLen, size_t rdLen );
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __USB_ISS_H