Ignore tags files, merge.
[jelmer/at89prog.git] / pins-ftdi.c
1 /* 
2         at89prog - support for FTDI
3         (c) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
4         (c) 2006 Stefan Lievens <zatalian@gmail.com>
5
6         This program is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program; if not, write to the Free Software
18         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdio.h>
22 #include "pins.h"
23 #include <sys/termios.h>
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <string.h>
27 #include <sys/io.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/time.h>
31 #include <time.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/ioctl.h>
35 #include <usb.h>
36 #include <ftdi.h>
37
38 static struct ftdi_context ftdic;
39 static int ftdi_data = 0;
40
41 static char *available_pins[] = { "P0", "P1", "P2", "P3", "P4", "P5", "P6", 
42                                                                   "P7", NULL };
43
44 static int ftdi_at98prog_init(char *location) 
45 {
46         int fd, i;
47         char bitmask = 0;
48         
49         ftdi_init(&ftdic);
50         fd = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
51         if(fd < 0 && fd != -5) {
52                 perror("Error opening ftdi device");
53                 return -1;
54         }
55
56         i = ftdi_enable_bitbang(&ftdic, bitmask);
57         i = ftdi_set_baudrate(&ftdic, 9600);
58         
59         ftdi_data = 0;
60         
61         return 0;
62 }
63
64 static int ftdi_at98prog_close()
65 {
66         ftdi_disable_bitbang(&ftdic);
67         ftdi_usb_close(&ftdic);
68         ftdi_deinit(&ftdic);
69         return 0;
70 }
71
72 static void ftdi_at98prog_set(int p)
73 {
74         ftdi_data |= (0x01 << p);
75         ftdi_write_data(&ftdic, (unsigned char *) &ftdi_data, 1);
76 }
77
78 static void ftdi_at98prog_clear(int p)
79 {
80
81         ftdi_data &= ~(0x01 << p);
82         ftdi_write_data(&ftdic, (unsigned char *) &ftdi_data, 1);
83 }
84
85 static int ftdi_at98prog_get(int p)
86 {
87         int status;
88         
89         ftdi_read_pins(&ftdic, (unsigned char *) &status);
90         if (status & (0x01 << p)) return 1; else return 0;
91 }
92
93 struct pins_backend ftdi = {
94         "ftdi",
95         available_pins,
96         ftdi_at98prog_init,
97         ftdi_at98prog_set,
98         ftdi_at98prog_clear,
99         ftdi_at98prog_get,
100         ftdi_at98prog_close
101 };