OpenCores
URL https://opencores.org/ocsvn/ftdi_wb_bridge/ftdi_wb_bridge/trunk

Subversion Repositories ftdi_wb_bridge

[/] [ftdi_wb_bridge/] [trunk/] [sw/] [ftdi_hw.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ultra_embe
#include <stdio.h>
2
#include <ftdi.h>
3
 
4
#include "ftdi_hw.h"
5
 
6
//-----------------------------------------------------------------
7
// Defines:
8
//-----------------------------------------------------------------
9
#define CMD_NOP        0x0
10
#define CMD_WR         0x1
11
#define CMD_RD         0x2
12
#define CMD_GP_WR      0x3
13
#define CMD_GP_RD      0x4
14
 
15
#define MAX_TX_SIZE    2048
16
#define HDR_SIZE       6
17
 
18
#define FTDI_PID       0x0403
19
#define FTDI_VID       0x6010
20
 
21
//-----------------------------------------------------------------
22
// Locals:
23
//-----------------------------------------------------------------
24
static struct ftdi_context *_handle;
25
 
26
//-----------------------------------------------------------------
27
// ftdi_hw_init:
28
//-----------------------------------------------------------------
29
int ftdi_hw_init(int interface)
30
{
31
    int status;
32
 
33
    _handle = ftdi_new();
34
    if (!_handle)
35
        return -1;
36
 
37
    // Open channel
38
    ftdi_set_interface(_handle, interface ? INTERFACE_B : INTERFACE_A);
39
    status = ftdi_usb_open(_handle, FTDI_PID, FTDI_VID);
40
    if (status != 0)
41
    {
42
        ftdi_free(_handle);
43
        _handle = NULL;
44
        return -1;
45
    }
46
 
47
    // Reset FTDI
48
    status = ftdi_usb_reset(_handle);
49
    if (status != 0)
50
    {
51
        ftdi_usb_close(_handle);
52
        ftdi_free(_handle);
53
        _handle = NULL;
54
        return -1;
55
    }
56
 
57
    // Flush buffers
58
    status = ftdi_usb_purge_buffers(_handle);
59
    if (status != 0)
60
    {
61
        ftdi_usb_close(_handle);
62
        ftdi_free(_handle);
63
        _handle = NULL;
64
        return -1;
65
    }
66
 
67
    // Set transfer mode
68
    status = ftdi_set_bitmode(_handle, 0xFF, BITMODE_RESET);
69
    if (status != 0)
70
    {
71
        ftdi_usb_close(_handle);
72
        ftdi_free(_handle);
73
        _handle = NULL;
74
        return -1;
75
    }
76
 
77
    return 0;
78
}
79
//-----------------------------------------------------------------
80
// ftdi_hw_close:
81
//-----------------------------------------------------------------
82
int ftdi_hw_close(void)
83
{
84
    if (_handle)
85
    {
86
        ftdi_usb_close(_handle);
87
        ftdi_free(_handle);
88
        _handle = NULL;
89
    }
90
 
91
    return 0;
92
}
93
//-----------------------------------------------------------------
94
// ftdi_hw_mem_write:
95
//-----------------------------------------------------------------
96
int ftdi_hw_mem_write(uint32_t addr, uint8_t *data, int length)
97
{
98
    int i;
99
    int sent = 0;
100
    int size = length;
101
    int res;
102
    uint8_t buffer[MAX_TX_SIZE + HDR_SIZE];
103
    uint8_t *p;
104
 
105
    while (sent < length)
106
    {
107
        size = (length - sent);
108
        if (size > MAX_TX_SIZE)
109
            size = MAX_TX_SIZE;
110
 
111
        // Build packet header
112
        p = buffer;
113
        *p++ = (((size >> 8) & 0xF) << 4) | CMD_WR;
114
        *p++ = (size & 0xFF);
115
 
116
        *p++ = (addr >> 24);
117
        *p++ = (addr >> 16);
118
        *p++ = (addr >> 8);
119
        *p++ = (addr >> 0);
120
 
121
        // Fill packet payload
122
        for (i=0;i<size;i++)
123
            *p++ = *data++;
124
 
125
        // Write request + data to FTDI device
126
        res = ftdi_write_data(_handle, buffer, (size + HDR_SIZE));
127
        if (res != (size + HDR_SIZE))
128
        {
129
            fprintf(stderr, "ftdi_hw_mem_write: Failed to send\n");
130
            return -1;
131
        }
132
 
133
        sent += size;
134
        addr += size;
135
    }
136
 
137
    return sent;
138
}
139
//-----------------------------------------------------------------
140
// ftdi_hw_mem_read:
141
//-----------------------------------------------------------------
142
int ftdi_hw_mem_read(uint32_t addr, uint8_t *data, int length)
143
{
144
    int i;
145
    int received = 0;
146
    int size = length;
147
    int remain;
148
    int res;
149
    uint8_t buffer[HDR_SIZE];
150
    uint8_t *p;
151
 
152
    while (received < length)
153
    {
154
        size = (length - received);
155
        if (size > MAX_TX_SIZE)
156
            size = MAX_TX_SIZE;
157
 
158
        // Round up to nearest 4 byte multiple
159
        size = (size + 3) & ~3;
160
 
161
        // Build packet header
162
        p = buffer;
163
        *p++ = (((size >> 8) & 0xF) << 4) | CMD_RD;
164
        *p++ = (size & 0xFF);
165
 
166
        *p++ = (addr >> 24);
167
        *p++ = (addr >> 16);
168
        *p++ = (addr >> 8);
169
        *p++ = (addr >> 0);
170
 
171
        // Write request to FTDI device
172
        res = ftdi_write_data(_handle, buffer, HDR_SIZE);
173
        if (res != HDR_SIZE)
174
        {
175
            fprintf(stderr, "ftdi_hw_mem_read: Failed to send request\n");
176
            return -1;
177
        }
178
 
179
        remain = size;
180
        do
181
        {
182
            res = ftdi_read_data(_handle, data, remain);
183
            if (res < 0)
184
            {
185
                fprintf(stderr, "ftdi_hw_mem_read: Failed to read data\n");
186
                return -1;
187
            }
188
 
189
            remain -= res;
190
            data += res;
191
        }
192
        while (remain > 0);
193
 
194
        received += size;
195
        addr     += size;
196
    }
197
 
198
    return received;
199
}
200
//-----------------------------------------------------------------
201
// ftdi_hw_mem_write_word:
202
//-----------------------------------------------------------------
203
int ftdi_hw_mem_write_word(uint32_t addr, uint32_t data)
204
{
205
    uint8_t buffer[4];
206
 
207
    buffer[3] = (data >> 24);
208
    buffer[2] = (data >> 16);
209
    buffer[1] = (data >> 8);
210
    buffer[0] = (data >> 0);
211
 
212
    return ftdi_hw_mem_write(addr, buffer, 4);
213
}
214
//-----------------------------------------------------------------
215
// ftdi_hw_mem_read_word:
216
//-----------------------------------------------------------------
217
int ftdi_hw_mem_read_word(uint32_t addr, uint32_t *data)
218
{
219
    uint8_t buffer[4];
220
 
221
    int res = ftdi_hw_mem_read(addr, buffer, 4);
222
    if (res > 0)
223
    {
224
        (*data) = ((uint32_t)buffer[3]) << 24;
225
        (*data)|= ((uint32_t)buffer[2]) << 16;
226
        (*data)|= ((uint32_t)buffer[1]) << 8;
227
        (*data)|= ((uint32_t)buffer[0]) << 0;
228
    }
229
    return res;
230
}
231
//-----------------------------------------------------------------
232
// ftdi_hw_gpio_write:
233
//-----------------------------------------------------------------
234
int ftdi_hw_gpio_write(uint8_t value)
235
{
236
    uint8_t buffer[2] = { CMD_GP_WR, value };
237
 
238
    // Write request to FTDI device
239
    int res = ftdi_write_data(_handle, buffer, sizeof(buffer));
240
    if (res != sizeof(buffer))
241
    {
242
        fprintf(stderr, "ftdi_hw_mem_write: Failed to send\n");
243
        return -1;
244
    }
245
 
246
    return 0;
247
}
248
//-----------------------------------------------------------------
249
// ftdi_hw_gpio_read:
250
//-----------------------------------------------------------------
251
int ftdi_hw_gpio_read(uint8_t *value)
252
{
253
    // Write request to FTDI device
254
    uint8_t request = CMD_GP_RD;
255
    int res = ftdi_write_data(_handle, &request, 1);
256
    if (res != 1)
257
    {
258
        fprintf(stderr, "ftdi_hw_mem_write: Failed to send\n");
259
        return -1;
260
    }
261
 
262
    // Poll for response
263
    do
264
    {
265
        res = ftdi_read_data(_handle, value, 1);
266
        if (res < 0)
267
        {
268
            fprintf(stderr, "ftdi_hw_mem_read: Failed to read data\n");
269
            return -1;
270
        }
271
    }
272
    while (res != 1);
273
 
274
    return 0;
275
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.