platform_type: platform_type now in mraa_board_t
[contrib/mraa.git] / src / usb / ftdi_ft4222.c
1 /*
2  * Author: Henry Bruce <henry.bruce@intel.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "linux/i2c-dev.h"
28 #include "common.h"
29 #include "ftd2xx.h"
30 #include "libft4222.h"
31 #include "usb/ftdi_ft4222.h"
32
33 #define PLATFORM_NAME "FTDI FT4222"
34 #define I2CM_ERROR(status) (((status) & 0x02) != 0)
35
36
37 static FT_HANDLE ftHandle = (FT_HANDLE)NULL;
38 static int bus_speed = 400;
39
40
41 mraa_result_t
42 mraa_ftdi_ft4222_init()
43 {
44     mraa_result_t mraaStatus = MRAA_SUCCESS;
45     FT_STATUS ftStatus;
46     FT_DEVICE_LIST_INFO_NODE *devInfo = NULL;
47     DWORD numDevs = 0;
48     int i;
49     int retCode = 0;
50     
51     ftStatus = FT_CreateDeviceInfoList(&numDevs);
52     if (ftStatus != FT_OK) 
53     {
54         syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus);
55         mraaStatus = MRAA_ERROR_NO_RESOURCES;
56         goto init_exit;
57     }
58     
59     if (numDevs == 0)
60     {
61         syslog(LOG_ERR, "No FT4222 devices connected.\n");
62         goto init_exit;
63     }
64
65     devInfo = calloc((size_t)numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE));
66     if (devInfo == NULL)
67     {
68         syslog(LOG_ERR, "FT4222 allocation failure.\n");
69         mraaStatus = MRAA_ERROR_NO_RESOURCES;
70         goto init_exit;
71     }
72     
73     ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
74     if (ftStatus != FT_OK)
75     {
76         syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int)ftStatus);
77         mraaStatus = MRAA_ERROR_NO_RESOURCES;
78         goto init_exit;
79     }
80
81     syslog(LOG_INFO, "FT_GetDeviceInfoList returned %d devices\n", numDevs);
82     DWORD locationId;
83     int bus = 0;
84     if (numDevs > bus && devInfo[bus].Type == FT_DEVICE_4222H_3) {
85        locationId = devInfo[bus].LocId;
86     } else {
87         syslog(LOG_ERR, "FT_GetDeviceInfoList contains no valid devices\n");
88         mraaStatus = MRAA_ERROR_NO_RESOURCES;
89         goto init_exit;
90     }
91
92     ftStatus = FT_OpenEx((PVOID)(uintptr_t)locationId, FT_OPEN_BY_LOCATION, &ftHandle);
93     if (ftStatus != FT_OK)
94     {
95         syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int)ftStatus);
96         mraaStatus = MRAA_ERROR_NO_RESOURCES;
97         goto init_exit;
98     }
99
100 init_exit:
101     if (devInfo != NULL);
102         free(devInfo);
103     if (mraaStatus == MRAA_SUCCESS)
104         syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n");
105     return mraaStatus;
106 }
107
108
109 mraa_result_t
110 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
111 {
112     if (ftHandle != NULL) {
113         FT4222_Version ft4222Version;
114         FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
115         if (FT4222_OK == ft4222Status){
116             *versionChip = (unsigned int)ft4222Version.chipVersion;
117             *versionLib = (unsigned int)ft4222Version.dllVersion;
118             syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", *versionChip, *versionLib);
119             return MRAA_SUCCESS;
120         } else {
121             syslog(LOG_ERR, "libmraa: FT4222_GetVersion failed (error %d)\n", (int)ft4222Status);
122             return MRAA_ERROR_NO_RESOURCES;
123         }
124     } else {
125         syslog(LOG_ERR, "libmraa: bad FT4222 handle\n");
126         return MRAA_ERROR_INVALID_HANDLE;
127     }
128 }
129
130
131 mraa_i2c_context
132 mraa_ftdi_ft4222_i2c_init_raw(unsigned int bus)
133 {
134     // Tell the FT4222 to be an I2C Master.
135     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
136     if (FT4222_OK != ft4222Status)
137     {
138         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
139         return NULL;
140     }
141     
142     // Reset the I2CM registers to a known state.
143     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
144     if (FT4222_OK != ft4222Status)
145     {
146         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
147         return NULL;
148     }
149
150     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
151     if (dev == NULL) {
152         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
153         return NULL;
154     }
155
156     dev->handle = ftHandle;
157     dev->fh = -1; // We don't use file descriptors
158     dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per https://www.kernel.org/doc/Documentation/i2c/functionality
159     return dev;
160 }
161
162 static mraa_result_t
163 mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
164 {
165     // Tell the FT4222 to be an I2C Master.
166     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, 400);
167     if (FT4222_OK != ft4222Status)
168     {
169         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
170         return MRAA_ERROR_NO_RESOURCES;
171     }
172     
173     // Reset the I2CM registers to a known state.
174     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
175     if (FT4222_OK != ft4222Status)
176     {
177         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
178         return MRAA_ERROR_NO_RESOURCES;
179     }
180
181     dev->handle = ftHandle;
182     dev->fh = -1; // We don't use file descriptors
183     dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per https://www.kernel.org/doc/Documentation/i2c/functionality
184     return MRAA_SUCCESS;
185 }
186
187
188 static mraa_result_t
189 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
190 {
191     return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
192 }
193
194
195 static mraa_result_t
196 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
197 {
198     dev->addr = (int) addr;
199     return FT4222_I2CMaster_Init(ftHandle, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
200 }
201
202
203 static int
204 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
205 {
206     uint16 bytesRead = 0;
207         uint8 controllerStatus;
208     syslog(LOG_NOTICE, "FT4222_I2CMaster_Read(%02X, %d)", dev->addr, length);
209     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Read(dev->handle, dev->addr, data, length, &bytesRead);
210         ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
211     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus))
212     {
213         syslog(LOG_ERR, "FT4222_I2CMaster_Read failed (error %d)\n", (int)ft4222Status);
214         return 0;
215     }
216     return bytesRead;
217 }
218
219 static uint8_t
220 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
221 {
222     return 0;
223 }
224
225 static uint8_t
226 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
227 {
228     return 0;
229 }
230
231 static uint16_t
232 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
233 {
234     return 0;
235 }
236
237 static int
238 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
239 {
240     return -1;
241 }
242
243
244 static mraa_result_t
245 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
246 {
247     uint16 bytesWritten = 0;
248         uint8 controllerStatus;
249     syslog(LOG_NOTICE, "FT4222_I2CMaster_Write(%#02X, %#02X, %d)", dev->addr, *data, bytesToWrite);
250     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Write(dev->handle, dev->addr, (uint8_t*)data, bytesToWrite, &bytesWritten);
251         ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
252     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus))
253     {
254         syslog(LOG_ERR, "FT4222_I2CMaster_Write failed (error %d)\n", (int)ft4222Status);
255         return MRAA_ERROR_INVALID_HANDLE;
256     }
257
258     if (bytesWritten != bytesToWrite)
259         syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
260
261    return bytesToWrite == bytesWritten ? MRAA_SUCCESS : MRAA_ERROR_INVALID_HANDLE;
262 }
263
264
265 static mraa_result_t
266 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
267 {
268     return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
269 }
270
271 static mraa_result_t
272 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
273 {
274     mraa_result_t status = mraa_ftdi_ft4222_i2c_write_byte(dev, command);
275     if (status == MRAA_SUCCESS)
276         return mraa_ftdi_ft4222_i2c_write_byte(dev, data);
277     else
278         return status;
279 }
280
281 static mraa_result_t
282 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
283 {
284     mraa_result_t status = mraa_ftdi_ft4222_i2c_write_byte(dev, command);
285     if (status == MRAA_SUCCESS) 
286         return mraa_ftdi_ft4222_i2c_write(dev, (const uint8_t*)&data, 2);
287     else
288         return status;
289
290 }
291
292 static mraa_result_t
293 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
294 {
295     return MRAA_SUCCESS;
296 }
297
298
299
300
301 mraa_adv_func_t*
302 mraa_i2c_ft4222_create_func_table()
303 {
304     mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
305     if (func_table != NULL) {
306         // func_table->i2c_init_raw_replace = &mraa_ftdi_ft4222_i2c_init_raw;
307         func_table->i2c_init_bus_replace = &mraa_ftdi_ft4222_i2c_init_bus_replace;
308         func_table->i2c_set_frequency_replace = &mraa_ftdi_ft4222_i2c_frequency;
309         func_table->i2c_address_replace = &mraa_ftdi_ft4222_i2c_address;
310         func_table->i2c_read_replace = &mraa_ftdi_ft4222_i2c_read;
311         func_table->i2c_read_byte_replace = &mraa_ftdi_ft4222_i2c_read_byte;
312         func_table->i2c_read_byte_data_replace = &mraa_ftdi_ft4222_i2c_read_byte_data;
313         func_table->i2c_read_word_data_replace = &mraa_ftdi_ft4222_i2c_read_word_data;
314         func_table->i2c_read_bytes_data_replace = &mraa_ftdi_ft4222_i2c_read_bytes_data;
315         func_table->i2c_write_replace = &mraa_ftdi_ft4222_i2c_write;
316         func_table->i2c_write_byte_replace = &mraa_ftdi_ft4222_i2c_write_byte;
317         func_table->i2c_write_byte_data_replace = &mraa_ftdi_ft4222_i2c_write_byte_data;
318         func_table->i2c_write_word_data_replace = &mraa_ftdi_ft4222_i2c_write_word_data;
319         func_table->i2c_stop_replace = &mraa_ftdi_ft4222_i2c_stop;    
320     }
321     return func_table;
322 }
323
324 mraa_board_t*
325 mraa_ftdi_ft4222(mraa_board_t* board)
326 {
327     if (plat == NULL)
328         return NULL;
329     mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
330     if (sub_plat == NULL)
331         return NULL;
332     int pinIndex = 0;
333     int numUsbGpio = 0;
334     int numUsbPins = numUsbGpio + 2; // Add SDA and SCL
335     sub_plat->platform_type = MRAA_FTDI_FT4222;
336     sub_plat->platform_name = PLATFORM_NAME;
337     sub_plat->phy_pin_count = numUsbPins;
338     sub_plat->gpio_count = numUsbGpio; 
339     mraa_pininfo_t* pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * numUsbPins);
340     if (pins == NULL) {
341         return NULL;
342     }
343     sub_plat->pins = pins;
344
345     // Virtual gpio pins on i2c I/O expander.
346     mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
347     for (pinIndex=0; pinIndex < numUsbGpio; ++pinIndex) {
348         char name[8];
349         sprintf(name, "Pin%d", pinIndex);
350         strncpy(sub_plat->pins[pinIndex].name, name, 8);
351         sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
352     }
353
354     int bus = 0;
355     sub_plat->i2c_bus_count = 1;
356     sub_plat->def_i2c_bus = bus;
357     sub_plat->i2c_bus[bus].bus_id = bus;
358     // sub_plat->i2c_bus[bus].drv_type = drv_type;
359
360     // i2c pins (these are virtual, entries are required to configure i2c layer)
361     mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
362     strncpy(sub_plat->pins[pinIndex].name, "SDA", 8);
363     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
364     sub_plat->pins[pinIndex].i2c.mux_total = 0;
365     sub_plat->i2c_bus[bus].sda = pinIndex;
366     pinIndex++;
367     strncpy(sub_plat->pins[pinIndex].name, "SCL", 8);
368     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
369     sub_plat->pins[pinIndex].i2c.mux_total = 0;
370     sub_plat->i2c_bus[bus].scl = pinIndex;
371
372     // Set override functions
373     sub_plat->adv_func = mraa_i2c_ft4222_create_func_table();
374
375     board->sub_platform = sub_plat;    
376     return sub_plat;
377 }
378