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 | -- Copyright (C)2021-2023, Philip Munts dba Munts Technologies.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
WITH Ada.Strings.Fixed;
WITH Ada.Text_IO;
WITH Interfaces.C;
USE TYPE Interfaces.C.int;
USE TYPE Interfaces.C.unsigned;
PACKAGE BODY FTDI IS
-- Fetch last error message string
FUNCTION ErrorString(ctx : Context) RETURN String IS
BEGIN
RETURN Interfaces.C.Strings.Value(ftdi_get_error_string(ctx));
END ErrorString;
-- FTDI Device object constructor
FUNCTION Create
(Vendor : Integer;
Product : Integer) RETURN Device IS
ctx : CONSTANT Context := ftdi_new;
BEGIN
IF ctx = NullContext THEN
RAISE Error WITH "ftdi_new() failed";
END IF;
IF ftdi_usb_open(ctx, Interfaces.C.int(Vendor), Interfaces.C.int(Product)) /= 0 THEN
DECLARE
msg : CONSTANT String := ErrorString(ctx);
BEGIN
ftdi_free(ctx);
RAISE Error WITH "ftdi_usb_open() failed, " & msg;
END;
END IF;
RETURN NEW DeviceBaseClass'(ctx => ctx);
END Create;
-- Read data from a FTDI device
PROCEDURE Read
(Self : IN OUT DeviceBaseClass;
inbuf : OUT Data;
len : Positive) IS
ret : Interfaces.C.int;
BEGIN
IF Self.ctx = NullContext THEN
RAISE Error WITH "This object instance has not been created properly";
END IF;
LOOP
-- For some reason and on some targets, ftdi_read_data() sometimes
-- returns a zero byte count. So keep reading until we get a nonzero
-- byte count.
ret := ftdi_read_data(Self.ctx, inbuf'Address, Interfaces.C.int(len));
EXIT WHEN ret /= 0;
END LOOP;
IF ret /= Interfaces.C.int(len) THEN
Ada.Text_IO.Put_Line("DEBUG: ret =>" & Interfaces.C.int'Image(ret));
RAISE Error WITH "ftdi_read_data() failed, " & ErrorString(Self.ctx);
END IF;
END Read;
-- Write data to a FTDI device
PROCEDURE Write
(Self : IN OUT DeviceBaseClass;
outbuf : Data;
len : Positive) IS
BEGIN
IF Self.ctx = NullContext THEN
RAISE Error WITH "This object instance has not been created properly";
END IF;
IF ftdi_write_data(Self.ctx, outbuf'Address, Interfaces.C.int(len)) /= Interfaces.C.int(len) THEN
RAISE Error WITH "ftdi_write_data() failed, " & ErrorString(Self.ctx);
END IF;
END Write;
-- Get FTDI device chip ID as hexadecimal string
FUNCTION ChipID(Self : IN OUT DeviceBaseClass) RETURN String IS
id : Interfaces.C.unsigned;
hexchars : CONSTANT String := "0123456789ABCDEF";
BEGIN
IF Self.ctx = NullContext THEN
RAISE Error WITH "This object instance has not been created properly";
END IF;
IF ftdi_read_chipid(Self.ctx, id) /= 0 THEN
RAISE Error WITH "ftdi_read_chipid() failed, " & ErrorString(Self.ctx);
END IF;
RETURN hexchars(Natural(id / 2**28 MOD 16) + 1) &
hexchars(Natural(id / 2**24 MOD 16) + 1) &
hexchars(Natural(id / 2**20 MOD 16) + 1) &
hexchars(Natural(id / 2**16 MOD 16) + 1) &
hexchars(Natural(id / 2**12 MOD 16) + 1) &
hexchars(Natural(id / 2**8 MOD 16) + 1) &
hexchars(Natural(id / 2**4 MOD 16) + 1) &
hexchars(Natural(id / 2**0 MOD 16) + 1);
END ChipID;
-- Query FTDI library (libftdi1) version
FUNCTION LibraryVersion RETURN String IS
BEGIN
RETURN Interfaces.C.Strings.Value(ftdi_get_library_version.version_str);
END LibraryVersion;
-- Dump a data buffer in hexadecimal format
PROCEDURE Dump(src : Data) IS
PACKAGE ByteIO IS NEW Ada.Text_IO.Modular_IO(Byte);
buf : String(1 .. 6);
BEGIN
FOR b OF src LOOP
ByteIO.Put(buf, b, 16);
IF buf(1) = ' ' THEN
Ada.Strings.Fixed.Insert(buf, 5, "0", Ada.Strings.Left);
END IF;
Ada.Strings.Fixed.Replace_Slice(buf, 6, 6, " ");
Ada.Strings.Fixed.Replace_Slice(buf, 1, 3, " ");
Ada.Text_IO.Put(Ada.Strings.Fixed.Trim(buf, Ada.Strings.Both));
Ada.Text_IO.Put(" ");
END LOOP;
Ada.Text_IO.New_Line;
END Dump;
END FTDI;
|