diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..cff03c9 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,18 @@ +[![Release Status](https://img.shields.io/github/release/homewsn/candleLight_fw-SavvyCAN-Windows-plugin.svg)](https://github.com/homewsn/candleLight_fw-SavvyCAN-Windows-plugin/releases) + +### Qt5 plugin for the candleLight firmware compatible USB devices. + +This project contains the source code for the Qt5 serial bus module plug-in for interfacing the Windows version of [SavvyCAN](https://github.com/collin80/SavvyCAN) with USB devices flashed with [candleLight_fw](https://github.com/candle-usb/candleLight_fw).
+ +#### Building (Windows only): +* Download [the latest stable version of Qt5](https://www.qt.io/download/), for example 5.15.2 (you need 5.14.x or later). +* Open the project file. +* Select the kit that matches your version of SavvyCAN. For example, if you are using the [SavvyCAN-Windows_x64_CIBuild.zip V213](https://github.com/collin80/SavvyCAN/releases/tag/V213), it will be Desktop 5.15.2 MSVC2019 64bit. Then choose a Release build configuration. +* Build the project. +* Copy the built qtcandlelightcanbus.dll to the canbus folder of your SavvyCAN installation. + +#### Usage: +* Connect your device or devices to Windows via USB. +* The connected USB devices can be found in the New Connection dialog, Connection Type is QT SerialBus devices, SerialBus Device Type is candleLight_fw, Port is gs_usb0 etc. + +Ready plug-in for [SavvyCAN-Windows_x64_CIBuild.zip V213](https://github.com/collin80/SavvyCAN/releases/tag/V213) can be downloaded from [Releases](https://github.com/homewsn/candleLight_fw-SavvyCAN-Windows-plugin/releases). \ No newline at end of file diff --git a/api/LICENSE b/api/LICENSE new file mode 100644 index 0000000..cf8eac1 --- /dev/null +++ b/api/LICENSE @@ -0,0 +1,66 @@ +GNU LESSER GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright © 2007 Free Software Foundation, Inc. + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + +This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. +0. Additional Definitions. + +As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License. + +“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. + +An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. + +A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”. + +The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. + +The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. +1. Exception to Section 3 of the GNU GPL. + +You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. +2. Conveying Modified Versions. + +If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: + + a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or + b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. + +3. Object Code Incorporating Material from Library Header Files. + +The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the object code with a copy of the GNU GPL and this license document. + +4. Combined Works. + +You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: + + a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. + b) Accompany the Combined Work with a copy of the GNU GPL and this license document. + c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. + d) Do one of the following: + 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. + 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. + e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) + +5. Combined Libraries. + +You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. + b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. + +6. Revised Versions of the GNU Lesser General Public License. + +The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. + +If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. + diff --git a/api/candle.c b/api/candle.c new file mode 100644 index 0000000..9008421 --- /dev/null +++ b/api/candle.c @@ -0,0 +1,588 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#include "candle.h" +#include + +#include "candle_defs.h" +#include "candle_ctrl_req.h" + +static bool candle_dev_interal_open(candle_handle hdev); + +static bool candle_read_di(HDEVINFO hdi, SP_DEVICE_INTERFACE_DATA interfaceData, candle_device_t *dev) +{ + /* get required length first (this call always fails with an error) */ + ULONG requiredLength=0; + SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, NULL, 0, &requiredLength, NULL); + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { + dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS; + return false; + } + + PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = + (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, requiredLength); + + if (detail_data != NULL) { + detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); + } else { + dev->last_error = CANDLE_ERR_MALLOC; + return false; + } + + bool retval = true; + ULONG length = requiredLength; + if (!SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, detail_data, length, &requiredLength, NULL) ) { + dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS2; + retval = false; + } else if (FAILED(StringCchCopy(dev->path, sizeof(dev->path), detail_data->DevicePath))) { + dev->last_error = CANDLE_ERR_PATH_LEN; + retval = false; + } + + LocalFree(detail_data); + + if (!retval) { + return false; + } + + /* try to open to read device infos and see if it is avail */ + if (candle_dev_interal_open(dev)) { + dev->state = CANDLE_DEVSTATE_AVAIL; + candle_dev_close(dev); + } else { + dev->state = CANDLE_DEVSTATE_INUSE; + } + + dev->last_error = CANDLE_ERR_OK; + return true; +} + +bool __stdcall candle_list_scan(candle_list_handle *list) +{ + if (list==NULL) { + return false; + } + + candle_list_t *l = (candle_list_t *)calloc(1, sizeof(candle_list_t)); + *list = l; + if (l==NULL) { + return false; + } + + GUID guid; + if (CLSIDFromString(L"{c15b4308-04d3-11e6-b3ea-6057189e6443}", &guid) != NOERROR) { + l->last_error = CANDLE_ERR_CLSID; + return false; + } + + HDEVINFO hdi = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); + if (hdi == INVALID_HANDLE_VALUE) { + l->last_error = CANDLE_ERR_GET_DEVICES; + return false; + } + + bool rv = false; + for (unsigned i=0; idev[i])) { + l->last_error = l->dev[i].last_error; + rv = false; + break; + } + + } else { + + DWORD err = GetLastError(); + if (err==ERROR_NO_MORE_ITEMS) { + l->num_devices = i; + l->last_error = CANDLE_ERR_OK; + rv = true; + } else { + l->last_error = CANDLE_ERR_SETUPDI_IF_ENUM; + rv = false; + } + break; + + } + + } + + SetupDiDestroyDeviceInfoList(hdi); + + return rv; + +} + +bool __stdcall DLL candle_list_free(candle_list_handle list) +{ + free(list); + return true; +} + +bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len) +{ + candle_list_t *l = (candle_list_t *)list; + *len = l->num_devices; + return true; +} + +bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev) +{ + candle_list_t *l = (candle_list_t *)list; + if (l==NULL) { + return false; + } + + if (dev_num >= CANDLE_MAX_DEVICES) { + l->last_error = CANDLE_ERR_DEV_OUT_OF_RANGE; + return false; + } + + candle_device_t *dev = calloc(1, sizeof(candle_device_t)); + *hdev = dev; + if (dev==NULL) { + l->last_error = CANDLE_ERR_MALLOC; + return false; + } + + memcpy(dev, &l->dev[dev_num], sizeof(candle_device_t)); + l->last_error = CANDLE_ERR_OK; + dev->last_error = CANDLE_ERR_OK; + return true; +} + + +bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state) +{ + if (hdev==NULL) { + return false; + } else { + candle_device_t *dev = (candle_device_t*)hdev; + *state = dev->state; + return true; + } +} + +wchar_t * __stdcall DLL candle_dev_get_path(candle_handle hdev) +{ + if (hdev==NULL) { + return NULL; + } else { + candle_device_t *dev = (candle_device_t*)hdev; + return dev->path; + } +} + +static bool candle_dev_interal_open(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + memset(dev->rxevents, 0, sizeof(dev->rxevents)); + memset(dev->rxurbs, 0, sizeof(dev->rxurbs)); + + dev->deviceHandle = CreateFile( + dev->path, + GENERIC_WRITE | GENERIC_READ, + FILE_SHARE_WRITE | FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, + NULL + ); + + if (dev->deviceHandle == INVALID_HANDLE_VALUE) { + dev->last_error = CANDLE_ERR_CREATE_FILE; + return false; + } + + if (!WinUsb_Initialize(dev->deviceHandle, &dev->winUSBHandle)) { + dev->last_error = CANDLE_ERR_WINUSB_INITIALIZE; + goto close_handle; + } + + USB_INTERFACE_DESCRIPTOR ifaceDescriptor; + if (!WinUsb_QueryInterfaceSettings(dev->winUSBHandle, 0, &ifaceDescriptor)) { + dev->last_error = CANDLE_ERR_QUERY_INTERFACE; + goto winusb_free; + } + + dev->interfaceNumber = ifaceDescriptor.bInterfaceNumber; + unsigned pipes_found = 0; + + for (uint8_t i=0; iwinUSBHandle, 0, i, &pipeInfo)) { + dev->last_error = CANDLE_ERR_QUERY_PIPE; + goto winusb_free; + } + + if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) { + dev->bulkInPipe = pipeInfo.PipeId; + pipes_found++; + } else if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) { + dev->bulkOutPipe = pipeInfo.PipeId; + pipes_found++; + } else { + dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; + goto winusb_free; + } + + } + + if (pipes_found != 2) { + dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; + goto winusb_free; + } + + char use_raw_io = 1; + if (!WinUsb_SetPipePolicy(dev->winUSBHandle, dev->bulkInPipe, RAW_IO, sizeof(use_raw_io), &use_raw_io)) { + dev->last_error = CANDLE_ERR_SET_PIPE_RAW_IO; + goto winusb_free; + } + + if (!candle_ctrl_set_host_format(dev)) { + goto winusb_free; + } + + if (!candle_ctrl_get_config(dev, &dev->dconf)) { + goto winusb_free; + } + + if (!candle_ctrl_get_capability(dev, 0, &dev->bt_const)) { + dev->last_error = CANDLE_ERR_GET_BITTIMING_CONST; + goto winusb_free; + } + + dev->last_error = CANDLE_ERR_OK; + return true; + +winusb_free: + WinUsb_Free(dev->winUSBHandle); + +close_handle: + CloseHandle(dev->deviceHandle); + return false; + +} + +static bool candle_prepare_read(candle_device_t *dev, unsigned urb_num) +{ + bool rc = WinUsb_ReadPipe( + dev->winUSBHandle, + dev->bulkInPipe, + dev->rxurbs[urb_num].buf, + sizeof(dev->rxurbs[urb_num].buf), + NULL, + &dev->rxurbs[urb_num].ovl + ); + + if (rc || (GetLastError()!=ERROR_IO_PENDING)) { + dev->last_error = CANDLE_ERR_PREPARE_READ; + return false; + } else { + dev->last_error = CANDLE_ERR_OK; + return true; + } +} + +static bool candle_close_rxurbs(candle_device_t *dev) +{ + for (unsigned i=0; irxevents[i] != NULL) { + CloseHandle(dev->rxevents[i]); + } + } + return true; +} + + +bool __stdcall DLL candle_dev_open(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + if (candle_dev_interal_open(dev)) { + for (unsigned i=0; irxevents[i] = ev; + dev->rxurbs[i].ovl.hEvent = ev; + if (!candle_prepare_read(dev, i)) { + candle_close_rxurbs(dev); + return false; // keep last_error from prepare_read call + } + } + dev->last_error = CANDLE_ERR_OK; + return true; + } else { + return false; // keep last_error from open_device call + } + +} + +bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us) +{ + return candle_ctrl_get_timestamp(hdev, timestamp_us); +} + +bool __stdcall DLL candle_dev_close(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + + candle_close_rxurbs(dev); + + WinUsb_Free(dev->winUSBHandle); + dev->winUSBHandle = NULL; + CloseHandle(dev->deviceHandle); + dev->deviceHandle = NULL; + + dev->last_error = CANDLE_ERR_OK; + return true; +} + +bool __stdcall DLL candle_dev_free(candle_handle hdev) +{ + free(hdev); + return true; +} + +candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev) +{ + candle_device_t *dev = (candle_device_t*)hdev; + return dev->last_error; +} + +bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels) +{ + // TODO check if info was already read from device; try to do so; throw error... + candle_device_t *dev = (candle_device_t*)hdev; + *num_channels = dev->dconf.icount+1; + return true; +} + +bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap) +{ + // TODO check if info was already read from device; try to do so; throw error... + candle_device_t *dev = (candle_device_t*)hdev; + memcpy(cap, &dev->bt_const.feature, sizeof(candle_capability_t)); + return true; +} + +bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + return candle_ctrl_set_bittiming(dev, ch, data); +} + +bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + + if (dev->bt_const.fclk_can != 48000000) { + /* this function only works for the candleLight base clock of 48MHz */ + dev->last_error = CANDLE_ERR_BITRATE_FCLK; + return false; + } + + candle_bittiming_t t; + t.prop_seg = 1; + t.sjw = 1; + t.phase_seg1 = 13 - t.prop_seg; + t.phase_seg2 = 2; + + switch (bitrate) { + case 10000: + t.brp = 300; + break; + + case 20000: + t.brp = 150; + break; + + case 50000: + t.brp = 60; + break; + + case 83333: + t.brp = 36; + break; + + case 100000: + t.brp = 30; + break; + + case 125000: + t.brp = 24; + break; + + case 250000: + t.brp = 12; + break; + + case 500000: + t.brp = 6; + break; + + case 800000: + t.brp = 4; + t.phase_seg1 = 12 - t.prop_seg; + t.phase_seg2 = 2; + break; + + case 1000000: + t.brp = 3; + break; + + default: + dev->last_error = CANDLE_ERR_BITRATE_UNSUPPORTED; + return false; + } + + return candle_ctrl_set_bittiming(dev, ch, &t); +} + +bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + flags |= CANDLE_MODE_HW_TIMESTAMP; + return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_START, flags); +} + +bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_RESET, 0); +} + +bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame) +{ + // TODO ensure device is open, check channel count.. + candle_device_t *dev = (candle_device_t*)hdev; + + unsigned long bytes_sent = 0; + + frame->echo_id = 0; + frame->channel = ch; + + bool rc = WinUsb_WritePipe( + dev->winUSBHandle, + dev->bulkOutPipe, + (uint8_t*)frame, + sizeof(*frame), + &bytes_sent, + 0 + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SEND_FRAME; + return rc; + +} + +bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms) +{ + // TODO ensure device is open.. + candle_device_t *dev = (candle_device_t*)hdev; + + DWORD wait_result = WaitForMultipleObjects(CANDLE_URB_COUNT, dev->rxevents, false, timeout_ms); + if (wait_result == WAIT_TIMEOUT) { + dev->last_error = CANDLE_ERR_READ_TIMEOUT; + return false; + } + + if ( (wait_result < WAIT_OBJECT_0) || (wait_result >= WAIT_OBJECT_0 + CANDLE_URB_COUNT) ) { + dev->last_error = CANDLE_ERR_READ_WAIT; + return false; + } + + DWORD urb_num = wait_result - WAIT_OBJECT_0; + DWORD bytes_transfered; + + if (!WinUsb_GetOverlappedResult(dev->winUSBHandle, &dev->rxurbs[urb_num].ovl, &bytes_transfered, false)) { + candle_prepare_read(dev, urb_num); + dev->last_error = CANDLE_ERR_READ_RESULT; + return false; + } + + if (bytes_transfered < sizeof(*frame)-4) { + candle_prepare_read(dev, urb_num); + dev->last_error = CANDLE_ERR_READ_SIZE; + return false; + } + + if (bytes_transfered < sizeof(*frame)) { + frame->timestamp_us = 0; + } + + memcpy(frame, dev->rxurbs[urb_num].buf, sizeof(*frame)); + + return candle_prepare_read(dev, urb_num); +} + +candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame) +{ + if (frame->echo_id != 0xFFFFFFFF) { + return CANDLE_FRAMETYPE_ECHO; + }; + + if (frame->can_id & CANDLE_ID_ERR) { + return CANDLE_FRAMETYPE_ERROR; + } + + return CANDLE_FRAMETYPE_RECEIVE; +} + +uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame) +{ + return frame->can_id & 0x1FFFFFFF; +} + +bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame) +{ + return (frame->can_id & CANDLE_ID_EXTENDED) != 0; +} + +bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame) +{ + return (frame->can_id & CANDLE_ID_RTR) != 0; +} + +uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame) +{ + return frame->can_dlc; +} + +uint8_t * __stdcall DLL candle_frame_data(candle_frame_t *frame) +{ + return frame->data; +} + +uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame) +{ + return frame->timestamp_us; +} diff --git a/api/candle.h b/api/candle.h new file mode 100644 index 0000000..0f5499b --- /dev/null +++ b/api/candle.h @@ -0,0 +1,166 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void* candle_list_handle; +typedef void* candle_handle; + +typedef enum { + CANDLE_DEVSTATE_AVAIL, + CANDLE_DEVSTATE_INUSE +} candle_devstate_t; + +typedef enum { + CANDLE_FRAMETYPE_UNKNOWN, + CANDLE_FRAMETYPE_RECEIVE, + CANDLE_FRAMETYPE_ECHO, + CANDLE_FRAMETYPE_ERROR, + CANDLE_FRAMETYPE_TIMESTAMP_OVFL +} candle_frametype_t; + +enum { + CANDLE_ID_EXTENDED = 0x80000000, + CANDLE_ID_RTR = 0x40000000, + CANDLE_ID_ERR = 0x20000000 +}; + +typedef enum { + CANDLE_MODE_NORMAL = 0x00, + CANDLE_MODE_LISTEN_ONLY = 0x01, + CANDLE_MODE_LOOP_BACK = 0x02, + CANDLE_MODE_TRIPLE_SAMPLE = 0x04, + CANDLE_MODE_ONE_SHOT = 0x08, + CANDLE_MODE_HW_TIMESTAMP = 0x10, +} candle_mode_t; + +typedef enum { + CANDLE_ERR_OK = 0, + CANDLE_ERR_CREATE_FILE = 1, + CANDLE_ERR_WINUSB_INITIALIZE = 2, + CANDLE_ERR_QUERY_INTERFACE = 3, + CANDLE_ERR_QUERY_PIPE = 4, + CANDLE_ERR_PARSE_IF_DESCR = 5, + CANDLE_ERR_SET_HOST_FORMAT = 6, + CANDLE_ERR_GET_DEVICE_INFO = 7, + CANDLE_ERR_GET_BITTIMING_CONST = 8, + CANDLE_ERR_PREPARE_READ = 9, + CANDLE_ERR_SET_DEVICE_MODE = 10, + CANDLE_ERR_SET_BITTIMING = 11, + CANDLE_ERR_BITRATE_FCLK = 12, + CANDLE_ERR_BITRATE_UNSUPPORTED = 13, + CANDLE_ERR_SEND_FRAME = 14, + CANDLE_ERR_READ_TIMEOUT = 15, + CANDLE_ERR_READ_WAIT = 16, + CANDLE_ERR_READ_RESULT = 17, + CANDLE_ERR_READ_SIZE = 18, + CANDLE_ERR_SETUPDI_IF_DETAILS = 19, + CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20, + CANDLE_ERR_MALLOC = 21, + CANDLE_ERR_PATH_LEN = 22, + CANDLE_ERR_CLSID = 23, + CANDLE_ERR_GET_DEVICES = 24, + CANDLE_ERR_SETUPDI_IF_ENUM = 25, + CANDLE_ERR_SET_TIMESTAMP_MODE = 26, + CANDLE_ERR_DEV_OUT_OF_RANGE = 27, + CANDLE_ERR_GET_TIMESTAMP = 28, + CANDLE_ERR_SET_PIPE_RAW_IO = 29 +} candle_err_t; + +#pragma pack(push,1) + +typedef struct { + uint32_t echo_id; + uint32_t can_id; + uint8_t can_dlc; + uint8_t channel; + uint8_t flags; + uint8_t reserved; + uint8_t data[8]; + uint32_t timestamp_us; +} candle_frame_t; + +typedef struct { + uint32_t feature; + uint32_t fclk_can; + uint32_t tseg1_min; + uint32_t tseg1_max; + uint32_t tseg2_min; + uint32_t tseg2_max; + uint32_t sjw_max; + uint32_t brp_min; + uint32_t brp_max; + uint32_t brp_inc; +} candle_capability_t; + +typedef struct { + uint32_t prop_seg; + uint32_t phase_seg1; + uint32_t phase_seg2; + uint32_t sjw; + uint32_t brp; +} candle_bittiming_t; + +#pragma pack(pop) + +#define DLL + +bool __stdcall DLL candle_list_scan(candle_list_handle *list); +bool __stdcall DLL candle_list_free(candle_list_handle list); +bool __stdcall DLL candle_list_length(candle_list_handle list, uint8_t *len); + +bool __stdcall DLL candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev); +bool __stdcall DLL candle_dev_get_state(candle_handle hdev, candle_devstate_t *state); +wchar_t * __stdcall DLL candle_dev_get_path(candle_handle hdev); +bool __stdcall DLL candle_dev_open(candle_handle hdev); +bool __stdcall DLL candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us); +bool __stdcall DLL candle_dev_close(candle_handle hdev); +bool __stdcall DLL candle_dev_free(candle_handle hdev); + +bool __stdcall DLL candle_channel_count(candle_handle hdev, uint8_t *num_channels); +bool __stdcall DLL candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap); +bool __stdcall DLL candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data); +bool __stdcall DLL candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate); +bool __stdcall DLL candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags); +bool __stdcall DLL candle_channel_stop(candle_handle hdev, uint8_t ch); + +bool __stdcall DLL candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame); +bool __stdcall DLL candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms); + +candle_frametype_t __stdcall DLL candle_frame_type(candle_frame_t *frame); +uint32_t __stdcall DLL candle_frame_id(candle_frame_t *frame); +bool __stdcall DLL candle_frame_is_extended_id(candle_frame_t *frame); +bool __stdcall DLL candle_frame_is_rtr(candle_frame_t *frame); +uint8_t __stdcall DLL candle_frame_dlc(candle_frame_t *frame); +uint8_t * __stdcall DLL candle_frame_data(candle_frame_t *frame); +uint32_t __stdcall DLL candle_frame_timestamp_us(candle_frame_t *frame); + +candle_err_t __stdcall DLL candle_dev_last_error(candle_handle hdev); + +#ifdef __cplusplus +} +#endif diff --git a/api/candle_ctrl_req.c b/api/candle_ctrl_req.c new file mode 100644 index 0000000..f44d3e1 --- /dev/null +++ b/api/candle_ctrl_req.c @@ -0,0 +1,152 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#include "candle_ctrl_req.h" +#include "ch_9.h" + +enum { + CANDLE_BREQ_HOST_FORMAT = 0, + CANDLE_BREQ_BITTIMING, + CANDLE_BREQ_MODE, + CANDLE_BREQ_BERR, + CANDLE_BREQ_BT_CONST, + CANDLE_BREQ_DEVICE_CONFIG, + CANDLE_TIMESTAMP_GET, +}; + +static bool usb_control_msg(WINUSB_INTERFACE_HANDLE hnd, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size) +{ + WINUSB_SETUP_PACKET packet; + memset(&packet, 0, sizeof(packet)); + + packet.Request = request; + packet.RequestType = requesttype; + packet.Value = value; + packet.Index = index; + packet.Length = size; + + unsigned long bytes_sent = 0; + return WinUsb_ControlTransfer(hnd, packet, (uint8_t*)data, size, &bytes_sent, 0); +} + +bool candle_ctrl_set_host_format(candle_device_t *dev) +{ + candle_host_config_t hconf; + hconf.byte_order = 0x0000beef; + + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_HOST_FORMAT, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + &hconf, + sizeof(hconf) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_HOST_FORMAT; + return rc; +} + +bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags) +{ + candle_device_mode_t dm; + dm.mode = mode; + dm.flags = flags; + + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_MODE, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + dev->interfaceNumber, + &dm, + sizeof(dm) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_DEVICE_MODE; + return rc; +} + + +bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_DEVICE_CONFIG, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + dconf, + sizeof(*dconf) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_DEVICE_INFO; + return rc; +} + +bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_TIMESTAMP_GET, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + 1, + dev->interfaceNumber, + current_timestamp, + sizeof(*current_timestamp) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_TIMESTAMP; + return rc; +} + +bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_BT_CONST, + USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0, + data, + sizeof(*data) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_BITTIMING_CONST; + return rc; +} + +bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data) +{ + bool rc = usb_control_msg( + dev->winUSBHandle, + CANDLE_BREQ_BITTIMING, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, + channel, + 0, + data, + sizeof(*data) + ); + + dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_BITTIMING; + return rc; +} diff --git a/api/candle_ctrl_req.h b/api/candle_ctrl_req.h new file mode 100644 index 0000000..3d0cf21 --- /dev/null +++ b/api/candle_ctrl_req.h @@ -0,0 +1,37 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#include "candle_defs.h" + +enum { + CANDLE_DEVMODE_RESET = 0, + CANDLE_DEVMODE_START = 1 +}; + +bool candle_ctrl_set_host_format(candle_device_t *dev); +bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags); +bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf); +bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data); +bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data); +bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp); + diff --git a/api/candle_defs.h b/api/candle_defs.h new file mode 100644 index 0000000..e09a49e --- /dev/null +++ b/api/candle_defs.h @@ -0,0 +1,90 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#undef __CRT__NO_INLINE +#include +#define __CRT__NO_INLINE + +#include "candle.h" + +#define CANDLE_MAX_DEVICES 32 +#define CANDLE_URB_COUNT 30 + +#pragma pack(push,1) + +typedef struct { + uint32_t byte_order; +} candle_host_config_t; + +typedef struct { + uint8_t reserved1; + uint8_t reserved2; + uint8_t reserved3; + uint8_t icount; + uint32_t sw_version; + uint32_t hw_version; +} candle_device_config_t; + +typedef struct { + uint32_t mode; + uint32_t flags; +} candle_device_mode_t; + +#pragma pack(pop) + + +typedef struct { + OVERLAPPED ovl; + uint8_t buf[128]; +} canlde_rx_urb; + +typedef struct { + wchar_t path[256]; + candle_devstate_t state; + candle_err_t last_error; + + HANDLE deviceHandle; + WINUSB_INTERFACE_HANDLE winUSBHandle; + UCHAR interfaceNumber; + UCHAR bulkInPipe; + UCHAR bulkOutPipe; + + candle_device_config_t dconf; + candle_capability_t bt_const; + canlde_rx_urb rxurbs[CANDLE_URB_COUNT]; + HANDLE rxevents[CANDLE_URB_COUNT]; +} candle_device_t; + +typedef struct { + uint8_t num_devices; + candle_err_t last_error; + candle_device_t dev[CANDLE_MAX_DEVICES]; +} candle_list_t; diff --git a/api/ch_9.h b/api/ch_9.h new file mode 100644 index 0000000..ba1994b --- /dev/null +++ b/api/ch_9.h @@ -0,0 +1,37 @@ +/* + + Copyright (c) 2016 Hubert Denkmair + + This file is part of the candle windows API. + + This library is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation, either + version 3 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . + +*/ + +#pragma once + +#define USB_DIR_OUT 0 /* to device */ +#define USB_DIR_IN 0x80 /* to host */ + +#define USB_TYPE_MASK (0x03 << 5) +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +#define USB_RECIP_MASK 0x1f +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 diff --git a/device.cpp b/device.cpp new file mode 100644 index 0000000..bd9f2c0 --- /dev/null +++ b/device.cpp @@ -0,0 +1,213 @@ +#include "device.hpp" +#include "interface.hpp" +#include "listener.hpp" +#include "timing.hpp" +#include +#include +#include +#include +#include "api/candle.h" + +CandleApiDevice::CandleApiDevice(QString name) +{ + qDebug() << "New CandleApiDevice is created:" << name; + _iface = findInterface(name); + _hostOffsetStart = QDateTime::currentMSecsSinceEpoch(); +} + +CandleApiDevice::~CandleApiDevice() +{ + qDebug() << "CandleApiDevice is destoyed."; +} + +bool CandleApiDevice::open() +{ + setState(CanBusDeviceState::ConnectingState); + qDebug() << "CandleApiDevice::open"; + + _lstn = new CandleApiListener(*this); + connect(_lstn, SIGNAL(readyRead()), this, SLOT(dataAvailable())); + + candle_handle handle = _iface->getHandle(); + uint32_t flags = 0; + + if (!candle_dev_open(handle)) + { + setState(CanBusDeviceState::UnconnectedState); + return false; + } + + QVector keys = configurationKeys(); + foreach (int key, keys) + { + if (key == QCanBusDevice::BitRateKey) + { + QVariant var = configurationParameter(key); + bool ok; + uint32_t bitrate = var.toInt(&ok); + if (ok) + { + if (!setBitTiming(bitrate, 875)) + { + close(); + return false; + } + } + } + } + + uint32_t t_dev; + if (candle_dev_get_timestamp_us(handle, &t_dev)) + { + _deviceTicksStart = t_dev; + } + + candle_channel_start(handle, 0, flags); + + _lstn->startThread(); + + setState(CanBusDeviceState::ConnectedState); + return true; +} + +void CandleApiDevice::close() +{ + setState(CanBusDeviceState::ClosingState); + qDebug() << "CandleApiDevice::close"; + candle_handle handle = _iface->getHandle(); + candle_channel_stop(handle, 0); + + _lstn->stopThread(); + delete _lstn; + + setState(CanBusDeviceState::UnconnectedState); + candle_dev_close(handle); +} + +bool CandleApiDevice::writeFrame(const QCanBusFrame &msg) +{ + candle_frame_t frame; + frame.can_id = msg.frameId() & 0x1FFFFFFF; //?? + + qDebug() << "CandleApiDevice::writeFrame"; + switch (msg.frameType()) + { + case QCanBusFrame::DataFrame: + break; + case QCanBusFrame::RemoteRequestFrame: + frame.can_id |= CANDLE_ID_RTR; + break; + case QCanBusFrame::ErrorFrame: + frame.can_id |= CANDLE_ID_ERR; + break; + default: + qDebug() << "CandleApiDevice::writeFrame -> unknown or error frame."; + return true; + } + + if (msg.hasExtendedFrameFormat()) + { + frame.can_id |= CANDLE_ID_EXTENDED; + } + + frame.can_dlc = msg.payload().length(); + for (int cnt = 0; cnt < 8 && cnt < frame.can_dlc; cnt++) + { + frame.data[cnt] = msg.payload().at(cnt); + } + + candle_handle handle = _iface->getHandle(); + if (!candle_frame_send(handle, 0, &frame)) + { + return false; + } + return true; +} + +QString CandleApiDevice::interpretErrorFrame(const QCanBusFrame &errorFrame) +{ + Q_UNUSED(errorFrame); + qDebug() << "CandleApiDevice::interpretErrorFrame"; + return QString(); +} + +bool CandleApiDevice::setBitTiming(uint32_t bitrate, uint32_t samplePoint) +{ + candle_handle handle = _iface->getHandle(); + + candle_capability_t caps; + if (!candle_channel_get_capabilities(handle, 0, &caps)) + { + return false; + } + + foreach (const CandleApiTiming t, _timings) + { + if ( (t.getBaseClk() == caps.fclk_can) + && (t.getBitrate()==bitrate) + && (t.getSamplePoint()==samplePoint) ) + { + candle_bittiming_t timing = t.getTiming(); + return candle_channel_set_timing(handle, 0, &timing); + } + } + + // no valid timing found + return false; +} + +void CandleApiDevice::readFrames() +{ + candle_frame_t frame; + unsigned int timeout_ms = 10; + candle_handle handle = _iface->getHandle(); + QVector msg_list; + + while (_lstn->_shouldBeRunning) + { + if (candle_frame_read(handle, &frame, timeout_ms)) + { + if (candle_frame_type(&frame) == CANDLE_FRAMETYPE_RECEIVE) + { + QCanBusFrame msg; + + if (candle_frame_is_rtr(&frame)) + { + msg.setFrameType(QCanBusFrame::RemoteRequestFrame); + } + else + { + msg.setFrameType(QCanBusFrame::DataFrame); + } + if (candle_frame_is_extended_id(&frame)) + { + msg.setExtendedFrameFormat(true); + } + else + { + msg.setExtendedFrameFormat(false); + } + msg.setFrameId(candle_frame_id(&frame) & 0x1FFFFFFF); //?? + + uint8_t dlc = candle_frame_dlc(&frame); + QByteArray payload = QByteArray(reinterpret_cast(candle_frame_data(&frame)), dlc); + msg.setPayload(payload); + + uint32_t dev_ts = candle_frame_timestamp_us(&frame) - _deviceTicksStart; + uint64_t ts_us = 0/*_hostOffsetStart*/ + dev_ts; + uint64_t us_since_start = QDateTime::currentMSecsSinceEpoch() - _hostOffsetStart; + if (us_since_start > 0x180000000) + { + // device timestamp overflow + ts_us += us_since_start & 0xFFFFFFFF00000000; + } + auto ts = QCanBusFrame::TimeStamp(ts_us/1000000, ts_us % 1000000); + msg.setTimeStamp(ts); + + msg_list.append(msg); + enqueueReceivedFrames(msg_list); + msg_list.clear(); + } + } + } +} diff --git a/device.hpp b/device.hpp new file mode 100644 index 0000000..000f1cd --- /dev/null +++ b/device.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include "devices.hpp" + +class CandleApiListener; + +class CandleApiDevice : public CandleApiDevices +{ +public: + explicit CandleApiDevice(QString name); + virtual ~CandleApiDevice(); + +public: + bool open() override; + void close() override; + bool writeFrame(const QCanBusFrame &msg) override; + QString interpretErrorFrame(const QCanBusFrame &errorFrame) override; + + bool setBitTiming(uint32_t bitrate, uint32_t samplePoint); + void readFrames(); + +private: + CandleApiListener *_lstn; + CandleApiInterface *_iface; + uint32_t _deviceTicksStart; + uint64_t _hostOffsetStart; +}; diff --git a/devices.cpp b/devices.cpp new file mode 100644 index 0000000..29ddc97 --- /dev/null +++ b/devices.cpp @@ -0,0 +1,225 @@ +#include "devices.hpp" +#include "interface.hpp" +#include "timing.hpp" +#include +#include +#include + +static QList initFunction_ifaces() +{ + QList _ifaces; + return _ifaces; +} +QList CandleApiDevices::_ifaces = initFunction_ifaces(); + +static QList initFunction_timings() +{ + QList _timings; + + // Timings for 170MHz processors (CANable 2.0) + // Tseg1: 2..256 Tseg2: 2..128 sjw: 1..128 brp: 1..512 + // Note: as expressed below, Tseg1 does not include 1 count for prop phase + _timings + << CandleApiTiming(170000000, 10000, 875, 68, 217, 31) + << CandleApiTiming(170000000, 20000, 875, 34, 217, 31) + << CandleApiTiming(170000000, 50000, 875, 17, 173, 25) + << CandleApiTiming(170000000, 83333, 875, 8, 221, 32) + << CandleApiTiming(170000000, 100000, 875, 10, 147, 21) + << CandleApiTiming(170000000, 125000, 875, 8, 147, 21) + << CandleApiTiming(170000000, 250000, 875, 4, 147, 21) + << CandleApiTiming(170000000, 500000, 875, 2, 147, 21) + << CandleApiTiming(170000000, 1000000, 875, 1, 147, 21); + + + // Timings for 48MHz processors (CANable 0.X) + _timings + // sample point: 50.0% + << CandleApiTiming(48000000, 10000, 500, 300, 6, 8) + << CandleApiTiming(48000000, 20000, 500, 150, 6, 8) + << CandleApiTiming(48000000, 50000, 500, 60, 6, 8) + << CandleApiTiming(48000000, 83333, 500, 36, 6, 8) + << CandleApiTiming(48000000, 100000, 500, 30, 6, 8) + << CandleApiTiming(48000000, 125000, 500, 24, 6, 8) + << CandleApiTiming(48000000, 250000, 500, 12, 6, 8) + << CandleApiTiming(48000000, 500000, 500, 6, 6, 8) + << CandleApiTiming(48000000, 800000, 500, 3, 8, 9) + << CandleApiTiming(48000000, 1000000, 500, 3, 6, 8) + + // sample point: 62.5% + << CandleApiTiming(48000000, 10000, 625, 300, 8, 6) + << CandleApiTiming(48000000, 20000, 625, 150, 8, 6) + << CandleApiTiming(48000000, 50000, 625, 60, 8, 6) + << CandleApiTiming(48000000, 83333, 625, 36, 8, 6) + << CandleApiTiming(48000000, 100000, 625, 30, 8, 6) + << CandleApiTiming(48000000, 125000, 625, 24, 8, 6) + << CandleApiTiming(48000000, 250000, 625, 12, 8, 6) + << CandleApiTiming(48000000, 500000, 625, 6, 8, 6) + << CandleApiTiming(48000000, 800000, 600, 4, 7, 6) + << CandleApiTiming(48000000, 1000000, 625, 3, 8, 6) + + // sample point: 75.0% + << CandleApiTiming(48000000, 10000, 750, 300, 10, 4) + << CandleApiTiming(48000000, 20000, 750, 150, 10, 4) + << CandleApiTiming(48000000, 50000, 750, 60, 10, 4) + << CandleApiTiming(48000000, 83333, 750, 36, 10, 4) + << CandleApiTiming(48000000, 100000, 750, 30, 10, 4) + << CandleApiTiming(48000000, 125000, 750, 24, 10, 4) + << CandleApiTiming(48000000, 250000, 750, 12, 10, 4) + << CandleApiTiming(48000000, 500000, 750, 6, 10, 4) + << CandleApiTiming(48000000, 800000, 750, 3, 13, 5) + << CandleApiTiming(48000000, 1000000, 750, 3, 10, 4) + + // sample point: 87.5% + << CandleApiTiming(48000000, 10000, 875, 300, 12, 2) + << CandleApiTiming(48000000, 20000, 875, 150, 12, 2) + << CandleApiTiming(48000000, 50000, 875, 60, 12, 2) + << CandleApiTiming(48000000, 83333, 875, 36, 12, 2) + << CandleApiTiming(48000000, 100000, 875, 30, 12, 2) + << CandleApiTiming(48000000, 125000, 875, 24, 12, 2) + << CandleApiTiming(48000000, 250000, 875, 12, 12, 2) + << CandleApiTiming(48000000, 500000, 875, 6, 12, 2) + << CandleApiTiming(48000000, 800000, 867, 4, 11, 2) + << CandleApiTiming(48000000, 1000000, 875, 3, 12, 2); + + + _timings + // sample point: 50.0% + << CandleApiTiming(16000000, 10000, 520, 64, 11, 12) + << CandleApiTiming(16000000, 20000, 500, 50, 6, 8) + << CandleApiTiming(16000000, 50000, 500, 20, 6, 8) + << CandleApiTiming(16000000, 83333, 500, 12, 6, 8) + << CandleApiTiming(16000000, 100000, 500, 10, 6, 8) + << CandleApiTiming(16000000, 125000, 500, 8, 6, 8) + << CandleApiTiming(16000000, 250000, 500, 4, 6, 8) + << CandleApiTiming(16000000, 500000, 500, 2, 6, 8) + << CandleApiTiming(16000000, 800000, 500, 1, 8, 10) + << CandleApiTiming(16000000, 1000000, 500, 1, 6, 8) + + // sample point: 62.5% + << CandleApiTiming(16000000, 10000, 625, 64, 14, 9) + << CandleApiTiming(16000000, 20000, 625, 50, 8, 6) + << CandleApiTiming(16000000, 50000, 625, 20, 8, 6) + << CandleApiTiming(16000000, 83333, 625, 12, 8, 6) + << CandleApiTiming(16000000, 100000, 625, 10, 8, 6) + << CandleApiTiming(16000000, 125000, 625, 8, 8, 6) + << CandleApiTiming(16000000, 250000, 625, 4, 8, 6) + << CandleApiTiming(16000000, 500000, 625, 2, 8, 6) + << CandleApiTiming(16000000, 800000, 625, 1, 11, 7) + << CandleApiTiming(16000000, 1000000, 625, 1, 8, 6) + + // sample point: 75.0% + << CandleApiTiming(16000000, 20000, 750, 50, 10, 4) + << CandleApiTiming(16000000, 50000, 750, 20, 10, 4) + << CandleApiTiming(16000000, 83333, 750, 12, 10, 4) + << CandleApiTiming(16000000, 100000, 750, 10, 10, 4) + << CandleApiTiming(16000000, 125000, 750, 8, 10, 4) + << CandleApiTiming(16000000, 250000, 750, 4, 10, 4) + << CandleApiTiming(16000000, 500000, 750, 2, 10, 4) + << CandleApiTiming(16000000, 800000, 750, 1, 13, 5) + << CandleApiTiming(16000000, 1000000, 750, 1, 10, 4) + + // sample point: 87.5% + << CandleApiTiming(16000000, 20000, 875, 50, 12, 2) + << CandleApiTiming(16000000, 50000, 875, 20, 12, 2) + << CandleApiTiming(16000000, 83333, 875, 12, 12, 2) + << CandleApiTiming(16000000, 100000, 875, 10, 12, 2) + << CandleApiTiming(16000000, 125000, 875, 8, 12, 2) + << CandleApiTiming(16000000, 250000, 875, 4, 12, 2) + << CandleApiTiming(16000000, 500000, 875, 2, 12, 2) + << CandleApiTiming(16000000, 800000, 900, 2, 7, 1) + << CandleApiTiming(16000000, 1000000, 875, 1, 12, 2); + + return _timings; +} +QList CandleApiDevices::_timings = initFunction_timings(); + +QList CandleApiDevices::interfaces() +{ + QList result; + static QString const description("candleLight_fw compatible device"); + static QString const name("gs_usb"); + + candle_list_handle clist; + uint8_t num_ifaces; + candle_handle dev; + + if (candle_list_scan(&clist)) + { + if (candle_list_length(clist, &num_ifaces)) + { + for (uint8_t cnt = 0; cnt < num_ifaces; cnt++) + { + if (candle_dev_get(clist, cnt, &dev)) + { + candle_devstate_t state; + if (candle_dev_get_state(dev, &state)) + { + if (state == CANDLE_DEVSTATE_AVAIL) + { + QString dev_name = name + QString::number(cnt); + qDebug() << "Found CandleLight device:" << dev_name; + CandleApiInterface *iface = findInterface(dev); + if (iface == nullptr) + { + iface = new CandleApiInterface(dev, dev_name); + addInterface(iface); + } + else + { + iface->update(dev, dev_name); + } + result.append( + createDeviceInfo( + dev_name, + QString::number(cnt), + description, + 0, + false, + false + ) + ); + } + } + } + } + } + candle_list_free(clist); + } + return result; +} + +void CandleApiDevices::addInterface(CandleApiInterface *iface) +{ + _ifaces.append(iface); +} + +QList CandleApiDevices::getInterfaces() +{ + return _ifaces; +} + +CandleApiInterface *CandleApiDevices::findInterface(candle_handle dev) +{ + CandleApiInterface* iface; + foreach (iface, _ifaces) + { + if (iface->getPath() == std::wstring(candle_dev_get_path(dev))) + { + return iface; + } + } + return nullptr; +} + +CandleApiInterface *CandleApiDevices::findInterface(QString name) +{ + CandleApiInterface* iface; + foreach (iface, _ifaces) + { + if (iface->getName() == name) + { + return iface; + } + } + return nullptr; +} diff --git a/devices.hpp b/devices.hpp new file mode 100644 index 0000000..1a250dc --- /dev/null +++ b/devices.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "api/candle.h" + +class CandleApiListener; +class CandleApiInterface; +class CandleApiTiming; + +class CandleApiDevices : public QCanBusDevice +{ +public: + static QList interfaces(); + static CandleApiInterface *findInterface(QString name); + +private: + static CandleApiInterface *findInterface(candle_handle dev); + static void deleteAllInterfaces(); + static QList getInterfaces(); + static void addInterface(CandleApiInterface *iface); + +private: + static QList _ifaces; +protected: + static QList _timings; +}; diff --git a/interface.cpp b/interface.cpp new file mode 100644 index 0000000..eb3ac81 --- /dev/null +++ b/interface.cpp @@ -0,0 +1,33 @@ +#include "interface.hpp" + +CandleApiInterface::CandleApiInterface(candle_handle handle, QString name) : + _handle(handle), + _name(name) +{ +} + +CandleApiInterface::~CandleApiInterface() +{ +} + +std::wstring CandleApiInterface::getPath() const +{ + return std::wstring(candle_dev_get_path(_handle)); +} + +void CandleApiInterface::update(candle_handle handle, QString name) +{ + candle_dev_free(_handle); + _handle = handle; + _name = name; +} + +QString CandleApiInterface::getName() const +{ + return _name; +} + +candle_handle CandleApiInterface::getHandle() const +{ + return _handle; +} diff --git a/interface.hpp b/interface.hpp new file mode 100644 index 0000000..488a1e9 --- /dev/null +++ b/interface.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include "api/candle.h" + +class CandleApiDevices; + +class CandleApiInterface: public QObject +{ +public: + CandleApiInterface(candle_handle handle, QString name); + virtual ~CandleApiInterface(); + + std::wstring getPath() const; + void update(candle_handle handle, QString name); + QString getName() const; + candle_handle getHandle() const; + +private: + candle_handle _handle; + QString _name; +}; diff --git a/listener.cpp b/listener.cpp new file mode 100644 index 0000000..bd91776 --- /dev/null +++ b/listener.cpp @@ -0,0 +1,40 @@ +#include "listener.hpp" +#include "device.hpp" +#include +#include +#include + +CandleApiListener::CandleApiListener(CandleApiDevice &dev) : + _shouldBeRunning(false), + _dev(dev) +{ + _thread = new QThread(); +} + +CandleApiListener::~CandleApiListener() +{ + delete _thread; +} + +void CandleApiListener::run() +{ + _dev.readFrames(); + _thread->quit(); +} + +void CandleApiListener::startThread() +{ + _shouldBeRunning = true; + moveToThread(_thread); + connect(_thread, SIGNAL(started()), this, SLOT(run())); + _thread->start(); +} + +void CandleApiListener::stopThread() +{ + if (_shouldBeRunning) + { + _shouldBeRunning = false; + _thread->wait(); + } +} diff --git a/listener.hpp b/listener.hpp new file mode 100644 index 0000000..255cfe9 --- /dev/null +++ b/listener.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +class CandleApiDevice; + +class CandleApiListener : public QObject +{ + Q_OBJECT + +public: + explicit CandleApiListener(CandleApiDevice &dev); + virtual ~CandleApiListener(); + +public slots: + void run(); + void startThread(); + void stopThread(); + +public: + bool _shouldBeRunning; + +private: + CandleApiDevice &_dev; + QThread *_thread; +}; diff --git a/plugin.cpp b/plugin.cpp new file mode 100644 index 0000000..3bc6bb1 --- /dev/null +++ b/plugin.cpp @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2023 Vladimir Alemasov +* All rights reserved +* +* This program and the accompanying materials are distributed under +* the terms of GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include "plugin.hpp" +#include "device.hpp" +#include +#include +#include + +QList CandleLightCanBusPlugin::availableDevices(QString *errorMessage) const +{ + Q_UNUSED(errorMessage); + return CandleApiDevices::interfaces(); +} + +QCanBusDevice *CandleLightCanBusPlugin::createDevice(const QString &interfaceName, QString *errorMessage) const +{ + Q_UNUSED(errorMessage); + auto const info = CandleLightCanBusPlugin::availableDevices(errorMessage); + for (auto const& entry: info) + { + if(entry.name() == interfaceName) + { + return new CandleApiDevice(interfaceName); + } + } + return nullptr; +} diff --git a/plugin.hpp b/plugin.hpp new file mode 100644 index 0000000..7565582 --- /dev/null +++ b/plugin.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include + +class CandleApiInterface; + +class CandleLightCanBusPlugin : public QObject, public QCanBusFactoryV2 +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QCanBusFactory" FILE "plugin.json") + Q_INTERFACES(QCanBusFactoryV2) + + friend class QCanBusDevice; + +public: + QList availableDevices(QString *errorMessage) const override; + QCanBusDevice *createDevice(const QString &interfaceName, QString *errorMessage) const override; + +private: + static QList getInterfaces(); + static QList _ifaces; +}; diff --git a/plugin.json b/plugin.json new file mode 100644 index 0000000..b72b33d --- /dev/null +++ b/plugin.json @@ -0,0 +1,3 @@ +{ + "Key": "candleLight_fw" +} diff --git a/qtcandlelightcanbus.pro b/qtcandlelightcanbus.pro new file mode 100644 index 0000000..ea5df2b --- /dev/null +++ b/qtcandlelightcanbus.pro @@ -0,0 +1,35 @@ +QT += core serialbus +CONFIG += c++11 + +TARGET = qtcandlelightcanbus +TEMPLATE = lib +CONFIG += plugin + +SOURCES += plugin.cpp\ + device.cpp\ + devices.cpp\ + interface.cpp \ + listener.cpp\ + timing.cpp\ + +HEADERS += \ + device.hpp \ + devices.hpp\ + interface.hpp \ + listener.hpp \ + plugin.hpp \ + timing.hpp\ + +#CandleLight API +SOURCES += \ + api/candle.c \ + api/candle_ctrl_req.c + +HEADERS += \ + api/candle.h \ + api/candle_defs.h \ + api/ch_9.h + +win32: LIBS += -lSetupApi +win32: LIBS += -lOle32 +win32: LIBS += -lwinusb diff --git a/timing.cpp b/timing.cpp new file mode 100644 index 0000000..c326ac7 --- /dev/null +++ b/timing.cpp @@ -0,0 +1,33 @@ +#include "timing.hpp" + +CandleApiTiming::CandleApiTiming(uint32_t baseClk, uint32_t bitrate, uint32_t samplePoint, uint32_t brp, uint32_t phase_seg1, uint32_t phase_seg2) + : _baseClk(baseClk), + _bitrate(bitrate), + _samplePoint(samplePoint) +{ + _timing.brp = brp; + _timing.phase_seg1 = phase_seg1; + _timing.phase_seg2 = phase_seg2; + _timing.sjw = 1; + _timing.prop_seg = 1; +} + +uint32_t CandleApiTiming::getBaseClk() const +{ + return _baseClk; +} + +uint32_t CandleApiTiming::getBitrate() const +{ + return _bitrate; +} + +uint32_t CandleApiTiming::getSamplePoint() const +{ + return _samplePoint; +} + +candle_bittiming_t CandleApiTiming::getTiming() const +{ + return _timing; +} diff --git a/timing.hpp b/timing.hpp new file mode 100644 index 0000000..4398a01 --- /dev/null +++ b/timing.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "api/candle.h" + +class CandleApiTiming +{ +public: + CandleApiTiming( + uint32_t baseClk, + uint32_t bitrate, + uint32_t samplePoint, + uint32_t brp, + uint32_t phase_seg1, + uint32_t phase_seg2 + ); + + uint32_t getBaseClk() const; + uint32_t getBitrate() const; + uint32_t getSamplePoint() const; + candle_bittiming_t getTiming() const; + +private: + uint32_t _baseClk; + uint32_t _bitrate; + uint32_t _samplePoint; + candle_bittiming_t _timing; + +};