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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
/***************************************************************************
octopus.c - description
-------------------
begin : Aug 2007
author : Benedikt Sauter, sauter@embedded-projects.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software, you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License *
* version 2.1 as published by the Free Software Foundation. *
* *
***************************************************************************/
/**
\mainpage liboctopus API documentation
Library to talk to octopus devices. You find the latest versions of liboctopus at
http://www.embedded-projects.net/octopus
The library is easy to use. Have a look at this short example:
\include simple.c
More examples can be found in the "examples" directory.
*/
/** \addtogroup liboctopus */
/* @{ */
#include <usb.h>
#include <string.h>
#include <errno.h>
#include "octopus.h"
#include "../firmware/protocol.h"
#define F_CPU 16000000UL
#define octopus_error_return(code, str) do { \
octopus->error_str = str; \
return code; \
} while(0);
/*
* initial octopus handle before use octopus_open
*/
int octopus_init(struct octopus_context *octopus)
{
if (octopus == NULL)
octopus_error_return(-1,"octopus not valid");
octopus->error_str = NULL;
octopus->usb_handle = NULL;
return 1;
}
/*
* open first octopus on usb
*/
int octopus_open(struct octopus_context *octopus)
{
if (octopus_open_id(octopus,VID,PID) < 0)
octopus_error_return(-1,"could not found octopus device with pid and vid");
return 1;
}
/*
* open octopus with a given usb vendor and product id
*/
int octopus_open_id(struct octopus_context *octopus, int vendor, int product)
{
struct usb_bus *busses;
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
if (usb_find_busses() < 0)
octopus_error_return(-1, "usb_find_busses() failed");
if (usb_find_devices() < 0)
octopus_error_return(-2, "usb_find_devices() failed");
busses = usb_get_busses();
if (busses == NULL)
octopus_error_return(-3, "usb_get_busses() failed");
for (bus = busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product)
{
octopus_open_dev(octopus,dev);
return 1;
}
}
}
octopus_error_return(-4,"could not found octopus device with pid and vid");
return -1;
}
/*
* open octopus with a given usb name description (not tested!)
*/
int octopus_open_serial(struct octopus_context *octopus, char * serial)
{
struct usb_bus *busses;
struct usb_dev_handle* usb_handle;
struct usb_bus *bus;
struct usb_device *dev;
char usbserial[64];
int length=0;
usb_init();
if (usb_find_busses() < 0)
octopus_error_return(-1, "usb_find_busses() failed");
if (usb_find_devices() < 0)
octopus_error_return(-2, "usb_find_devices() failed");
busses = usb_get_busses();
if(busses==NULL)
octopus_error_return(-3, "usb_get_busses() failed");
for (bus = busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
usb_handle = usb_open(dev);
usb_set_configuration(usb_handle,0);
length = usb_get_string_simple(usb_handle,3,usbserial,64);
usb_close(usb_handle);
if (strncmp(serial,usbserial,length) == 0)
{
octopus_open_dev(octopus,dev);
return 1;
}
}
}
octopus_error_return(-4,"could not found octopus device with serial number");
return -1;
}
/*
* open octopus with a libusb dev handle
*/
int octopus_open_dev(struct octopus_context *octopus, struct usb_device *dev)
{
if (octopus == NULL)
octopus_error_return(-1,"octopus handle is wrong or missing");
if (dev == NULL)
octopus_error_return(-2,"device handle is wrong or missing");
if (!(octopus->usb_handle = usb_open(dev)))
octopus_error_return(-3,"can't open usb device");
if (usb_set_configuration (octopus->usb_handle,dev->config[0].bConfigurationValue) < 0)
octopus_error_return(-4,"can't set configuration for given usb device");
if (usb_claim_interface(octopus->usb_handle, 0) < 0)
octopus_error_return(-5,"can't claim interface for given usb device");
if (usb_set_altinterface(octopus->usb_handle,0) < 0)
octopus_error_return(-6,"can't set altinterface for given usb device");
return 1;
}
/*
* close a open octopus handle
*/
int octopus_close(struct octopus_context *octopus)
{
if (octopus == NULL)
octopus_error_return(-1,"octopus is not a valid handle");
if (usb_close(octopus->usb_handle) < 0)
octopus_error_return(-2,usb_strerror());
return 1;
}
/*
* send message to octopus
*/
int octopus_message(struct octopus_context *octopus, char *msg,
int msglen, char *answer, int answerlen)
{
int timeout=0;
if (usb_bulk_write(octopus->usb_handle,1,msg,msglen,100) < msglen)
octopus_error_return(-1,"transfer error occurred");
if (answerlen>0){
while(1)
{
if(usb_bulk_read(octopus->usb_handle,0x81,(char*)answer,answerlen,100)>0)
break;
if(timeout++ >= 1000)
octopus_error_return(-2,"receive error occurred");
}
if(answer[1]==RSP_OK)
{
return 1;
}
else if (answer[1]==RSP_UNKOWN_CMD)
{
octopus_error_return(-1,"unkown command");
}
else if (answer[1]==RSP_UNKOWN_PIN)
{
octopus_error_return(-2,"unkown pin");
}
else if (answer[1]==RSP_WRONG_PIN_CONFIG)
{
octopus_error_return(-3,"pin is in wrong configuration");
}
else if (answer[1]==RSP_ERROR)
{
octopus_error_return(-4,"error in usb device");
}
else if (answer[1]==RSP_TIMEOUT)
{
octopus_error_return(-4,"timeout error in usb device");
}
else
{
octopus_error_return(-5,"unknown error");
}
}
return 1;
}
/*
* get internal device description of octopus (octopus_1, octocan_01)
*/
char * octopus_get_hwdesc(struct octopus_context *octopus, char *desc)
{
char msg[] = {CMD_GET_HW_ID,0x00,0x00};
int i;
char answer[64];
for (i=0;i<64;i++)
answer[i]=0;
if (octopus_message(octopus,msg,3,answer,14) == RSP_OK)
{
if ((int)answer[2] > 0)
memcpy(desc,answer+3,(int)answer[2]);
return desc;
}
else
{
octopus_error_return(NULL,octopus->error_str);
}
return desc;
}
/*
* use pin as gpio
*/
int octopus_io_init(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_IO_INIT_PIN,0x01,0x00};
msg[2] = (char)pin;
if (octopus_message(octopus,msg,3,answer,3) > 0)
return 1;
else return -1;
}
int octopus_io_init_port(struct octopus_context *octopus, int port)
{
char msg[] = {CMD_IO_INIT_PORT,0x01,0x00};
char answer[3];
msg[2] = (char)port;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_io_set_port_direction_out(struct octopus_context *octopus, int port,unsigned char mask)
{
char answer[3];
char msg[] = {CMD_IO_PORT_DIRECTION_OUT,0x02,0x00,0x00};
msg[2] = (char)port;
msg[3] = mask;
if(octopus_message(octopus,msg,4,answer,3))
return 1;
else return -1;
}
int octopus_io_set_port_direction_in(struct octopus_context *octopus, int port, unsigned char mask)
{
char answer[3];
char msg[] = {CMD_IO_PORT_DIRECTION_IN,0x02,0x00,0x00};
msg[2] = (char)port;
msg[3] = mask;
if(octopus_message(octopus,msg,4,answer,3))
return 1;
else return -1;
}
int octopus_io_set_port_direction_tri(struct octopus_context *octopus, int port, unsigned char mask)
{
char answer[3];
char msg[] = {CMD_IO_PORT_DIRECTION_TRI,0x02,0x00,0x00};
msg[2] = (char)port;
msg[3] = mask;
if(octopus_message(octopus,msg,4,answer,3))
return 1;
else return -1;
}
/*
* use pin as output
*/
int octopus_io_set_pin_direction_out(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_IO_PIN_DIRECTION_OUT,0x01,0x00};
msg[2] = (char)pin;
if (octopus_message(octopus,msg,3,answer,3) == RSP_OK)
return 1;
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_io_set_pin_direction_in(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_IO_PIN_DIRECTION_IN,0x01,0x00};
msg[2] = (char)pin;
if(octopus_message(octopus,msg,3,answer,3)==RSP_OK)
return 1;
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_io_set_pin_direction_tri(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_IO_PIN_DIRECTION_TRI,0x01,0x00};
msg[2] = (char)pin;
if(octopus_message(octopus,msg,3,answer,3)==RSP_OK)
return 1;
else
octopus_error_return(-1,octopus->error_str);
}
unsigned char octopus_io_get_port(struct octopus_context *octopus, int port)
{
char answer[3];
char msg[] = {CMD_IO_PORT_GET,0x01,0x00};
msg[2] = (char)port;
if(octopus_message(octopus,msg,3,answer,3)==RSP_OK)
return (unsigned char) answer[2];
else
return 0;
//octopus_error_return((unsigned char)NULL,octopus->error_str);
}
int octopus_io_set_port(struct octopus_context *octopus, int port, unsigned char value)
{
char answer[3];
char msg[] = {CMD_IO_PORT_SET,0x01,0x00,0x00};
msg[2] = (char)port;
msg[3] = (char)value;
if(octopus_message(octopus,msg,4,answer,3))
return 1;
else return -1;
}
int octopus_io_set_pin(struct octopus_context *octopus, int pin, int value)
{
char answer[3];
char msg[] = {CMD_IO_PIN_SET,0x01,0x00,0x00};
msg[2] = (char)pin;
msg[3] = (char)value;
if(octopus_message(octopus,msg,4,answer,3)==RSP_OK)
return 1;
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_io_get_pin(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_IO_PIN_GET,0x01,0x00};
msg[2] = (char)pin;
//msg[3] = 0x00;
if(octopus_message(octopus,msg,3,answer,3)==RSP_OK)
return (int)answer[2];
else
octopus_error_return(-1,octopus->error_str);
}
/// part: adc
int octopus_adc_init(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_ADC_INIT_PIN,0x01,0x00};
msg[2] = (char)pin;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_adc_get(struct octopus_context *octopus, int pin)
{
unsigned char answer[4];
char msg[] = { CMD_ADC_GET, 0x01, 0x00 };
msg[2] = (char) pin;
//XXX ????
if (octopus_message (octopus, msg, 3, (char *)answer, sizeof (answer)) >0)
{
unsigned int low = (unsigned int)answer[3];
unsigned int high = (unsigned int)answer[2];
return((int)((unsigned int)(low | (high << 8))));
}
else
return -1;
}
int octopus_adc_ref(struct octopus_context *octopus, int ref)
{
char answer[3];
char msg[] = {CMD_ADC_REF,0x01,0x00};
msg[2] = (char)ref;
if(octopus_message(octopus,msg,3,answer,3)>0)
return (int)answer[1];
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_adc_get_bulk(struct octopus_context *octopus,
int pin, int number, int * buf)
{
return 1;
}
int octopus_i2c_init(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_I2C_INIT,0x01,0x00};
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_i2c_deinit(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_I2C_DEINIT,0x01,0x00};
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_i2c_set_bitrate(struct octopus_context *octopus, int scl_freq) {
char answer[3];
char msg[] = {CMD_I2C_SET_BITRATE, 0x01, 0x00};
//XXX rounding XXX
int bitrate = ((F_CPU/scl_freq)-16) / 2;
msg[2] = bitrate;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_i2c_send_byte(struct octopus_context *octopus, char data)
{
char answer[3];
// cmd = cmd,len,timeout,address,data1,data2,..)
char msg[] = {CMD_I2C_SEND_BYTE,0x01,0x00};
msg[2] = (char)data;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_i2c_send_bytes(struct octopus_context *octopus, char *buf, int len)
{
while(len > 0) {
if(octopus_i2c_send_byte(octopus,buf[len--]) <= 0)
return 0;
}
return 1;
}
unsigned char octopus_i2c_receive_byte(struct octopus_context *octopus,
int address,int timeout)
{
char tmp[1];
if(octopus_i2c_receive_bytes(octopus,address,tmp,1,1)>0)
return tmp[1];
else
return -1;
}
int octopus_i2c_receive_bytes(struct octopus_context *octopus,
int address,char *buf, int len, int timeout)
{
if(len>60){
return -1; // max 60 bytes
}
char msg[64] = {CMD_I2C_RECV_BYTES,0x01,0x00};
msg[1] = (char)(len+4);
msg[2] = (char)timeout;
msg[3] = (char)address;
char answer[64];
int i;
if(octopus_message(octopus,msg,4,answer,len+3)>0){
for(i=0;i<len;i++)
buf[i]=answer[i+3];
return (int)answer[2];
}
else
octopus_error_return(-1,octopus->error_str);
return 1;
}
int octopus_i2c_send_start(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_I2C_SEND_START,0x01,0x00};
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
return 1;
}
int octopus_i2c_send_stop(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_I2C_SEND_STOP,0x00,0x00};
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
return 1;
}
/// part: SPI
int octopus_spi_init(struct octopus_context *octopus)
{
char answer[2];
char msg[] = {CMD_SPI_INIT,0x00,0x00};
if(octopus_message(octopus,msg,3,answer,2)>0)
return 1;
else return -1;
}
int octopus_spi_deinit(struct octopus_context *octopus)
{
char answer[2];
char msg[] = {CMD_SPI_DEINIT,0x00,0x00};
if(octopus_message(octopus,msg,3,answer,2)>0)
return 1;
else return -1;
}
int octopus_spi_speed(struct octopus_context *octopus, int speed)
{
return 0;
}
int octopus_spi_send(struct octopus_context *octopus, unsigned char * buf, int length)
{
if(length>60){
return -1; // max 60 bytes
}
char answer[2];
// cmd = cmd,len,timeout,address,data1,data2,..)
char msg[64] = {CMD_SPI_SEND,0x01,0x00};
msg[1] = (char)(length);
// build send array
int i;
for(i=0;i<length;i++)
msg[i+2]=(char)buf[i];
if(octopus_message(octopus,msg,2+length,answer,2)>0)
return 1;
else
octopus_error_return(-1,octopus->error_str);
return 1;
}
int octopus_spi_recv(struct octopus_context *octopus, unsigned char * buf, int length)
{
if(length>60){
return -1; // max 60 bytes
}
char answer[64];
char msg[64] = {CMD_SPI_RECV,0x01,0x00};
msg[1] = (char)(length);
int i;
if(octopus_message(octopus,msg,2,answer,2+length)>0) {
for(i=0;i<length;i++)
buf[i]=answer[i+2];
return 1;
}
else
octopus_error_return(-1,octopus->error_str);
}
int octopus_spi_send_and_recv(struct octopus_context *octopus, unsigned char * buf, int length)
{
if(length>60){
return -1; // max 60 bytes
}
char answer[2];
// cmd = cmd,len,timeout,address,data1,data2,..)
char msg[64] = {CMD_SPI_SEND_AND_RECV,0x01,0x00};
msg[1] = (char)(length);
// build send array
int i;
for(i=0;i<length;i++)
msg[i+2]=(char)buf[i];
if(octopus_message(octopus,msg,2+length,answer,2+length)>0) {
for(i=0;i<length;i++)
buf[i]=answer[i+2];
return 1;
}
else
octopus_error_return(-1,octopus->error_str);
return 1;
}
/// part: Flash 93c46
int octopus_93c46_init(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_EXTERNAL_DEVICE,0x00,CMD_EXTERNAL_93C46,CMD_93C46_INIT};
msg[1] = 4;
if(octopus_message(octopus,msg,4,answer,3)>0)
return 1;
else return -1;
}
int octopus_93c46_deinit(struct octopus_context *octopus)
{
char answer[3];
char msg[] = {CMD_EXTERNAL_DEVICE,CMD_EXTERNAL_93C46,CMD_93C46_DEINIT};
if (octopus_message(octopus,msg,3,answer,3) > 0)
return 1;
else return -1;
}
int octopus_93c46_write(struct octopus_context *octopus, unsigned char address, int length, unsigned char * buf)
{
/*
if(length>59){
return -1; // max 60 bytes
}
*/
char answer[3];
// cmd = cmd,len,timeout,address,data1,data2,..)
char msg[150] = {CMD_EXTERNAL_DEVICE,0x00,CMD_EXTERNAL_93C46,CMD_93C46_WRITE};
msg[1] = (unsigned char)(6 + length);
msg[4] = (unsigned char)(address);
msg[5] = (unsigned char)(length);
// build send array
int i;
for(i=0;i<length;i++)
msg[i+6]=(char)buf[i];
if(octopus_message(octopus,msg,6+length,answer,3)>0)
return 1;
else
octopus_error_return(-1,octopus->error_str);
return 1;
}
int octopus_93c46_read(struct octopus_context *octopus, unsigned char address, int length, unsigned char * buf)
{
/*
if(length>59)
{
return -1; // max 60 bytes
}*/
char answer[150];
char msg[6] = {CMD_EXTERNAL_DEVICE,0x00,CMD_EXTERNAL_93C46,CMD_93C46_READ};
msg[1] = 6;
msg[4] = (unsigned char)(address);
msg[5] = (unsigned char)(length);
int i;
if (octopus_message(octopus, msg, 6, answer, 2 + length) > 0)
{
for(i=0;i<length;i++)
buf[i]=answer[i+2];
return 1;
}
else
octopus_error_return(-1,octopus->error_str);
}
/// part: PWM
int octopus_pwm_init(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_PWM_INIT_PIN,0x01,0x00};
msg[2] = (char)pin;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_pwm_deinit(struct octopus_context *octopus, int pin)
{
char answer[3];
char msg[] = {CMD_PWM_DEINIT_PIN,0x01,0x00};
msg[2] = (char)pin;
if(octopus_message(octopus,msg,3,answer,3)>0)
return 1;
else return -1;
}
int octopus_pwm_speed(struct octopus_context *octopus, int pin, int speed)
{
char answer[3];
char msg[] = {CMD_PWM_SPEED,0x02,0x00,0x00};
msg[2] = (char)pin;
msg[3] = (char)speed;
if(octopus_message(octopus,msg,4,answer,3)>0)
return 1;
else return -1;
}
int octopus_pwm_value(struct octopus_context *octopus, int pin, unsigned char value)
{
char answer[3];
char msg[] = {CMD_PWM_VALUE,0x02,0x00,0x00};
msg[2] = (char)pin;
msg[3] = (char)value;
if(octopus_message(octopus,msg,4,answer,3)>0)
return 1;
else return -1;
}
;
|