Blake Leverington
1 year ago
52 changed files with 8328 additions and 5 deletions
-
4.gitignore
-
674LICENSE
-
28README.md
-
BINscripts/marchposter.png
-
BINscripts/oscilloscope_15ps_320Hz_singlechannelsplit.png
-
BINscripts/oscilloscope_20ps_320Hz.png
-
BINscripts/oscilloscope_20ps_320Hz_2v5input.png
-
35scripts/plottdc.py
-
49scripts/plottdc_dump.py
-
2source/.gitignore
-
14source/CMakeLists.txt
-
2source/gpx2-raspi-pigpiod-lib-deprecated/.gitignore
-
126source/gpx2-raspi-pigpiod-lib-deprecated/CMakeLists.txt
-
2source/gpx2-raspi-pigpiod-lib-deprecated/README.md
-
3source/gpx2-raspi-pigpiod-lib-deprecated/cmake/version.cmake
-
5source/gpx2-raspi-pigpiod-lib-deprecated/config/changelog
-
24source/gpx2-raspi-pigpiod-lib-deprecated/config/copyright
-
24source/gpx2-raspi-pigpiod-lib-deprecated/config/license
-
1source/gpx2-raspi-pigpiod-lib-deprecated/config/triggers
-
10source/gpx2-raspi-pigpiod-lib-deprecated/config/version.h
-
2495source/gpx2-raspi-pigpiod-lib-deprecated/doxygen/Doxyfile.in
-
58source/gpx2-raspi-pigpiod-lib-deprecated/include/config.h
-
46source/gpx2-raspi-pigpiod-lib-deprecated/include/gpx2.h
-
10source/gpx2-raspi-pigpiod-lib-deprecated/include/version.h
-
138source/gpx2-raspi-pigpiod-lib-deprecated/src/config.cpp
-
196source/gpx2-raspi-pigpiod-lib-deprecated/src/gpx2.cpp
-
2source/gpx2-raspi-readout-program/.gitignore
-
72source/gpx2-raspi-readout-program/CMakeLists.txt
-
3source/gpx2-raspi-readout-program/cmake/version.cmake
-
10source/gpx2-raspi-readout-program/config/version.h
-
128source/gpx2-raspi-readout-program/include/gpio.h
-
56source/gpx2-raspi-readout-program/include/readout.h
-
10source/gpx2-raspi-readout-program/include/version.h
-
290source/gpx2-raspi-readout-program/src/gpio.cpp
-
27source/gpx2-raspi-readout-program/src/main.cpp
-
249source/gpx2-raspi-readout-program/src/readout.cpp
-
131source/spi/CMakeLists.txt
-
3source/spi/cmake/version.cmake
-
5source/spi/config/changelog
-
24source/spi/config/copyright
-
24source/spi/config/license
-
1source/spi/config/triggers
-
10source/spi/config/version.h
-
2495source/spi/doxygen/Doxyfile.in
-
62source/spi/include/spidevices/gpx2/config.h
-
119source/spi/include/spidevices/gpx2/gpx2.h
-
73source/spi/include/spidevices/spidevice.h
-
10source/spi/include/version.h
-
4source/spi/spidevices.h
-
169source/spi/src/spidevices/gpx2/config.cpp
-
226source/spi/src/spidevices/gpx2/gpx2.cpp
-
184source/spi/src/spidevices/spidevice.cpp
@ -0,0 +1,674 @@ |
|||||
|
GNU GENERAL PUBLIC LICENSE |
||||
|
Version 3, 29 June 2007 |
||||
|
|
||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> |
||||
|
Everyone is permitted to copy and distribute verbatim copies |
||||
|
of this license document, but changing it is not allowed. |
||||
|
|
||||
|
Preamble |
||||
|
|
||||
|
The GNU General Public License is a free, copyleft license for |
||||
|
software and other kinds of works. |
||||
|
|
||||
|
The licenses for most software and other practical works are designed |
||||
|
to take away your freedom to share and change the works. By contrast, |
||||
|
the GNU General Public License is intended to guarantee your freedom to |
||||
|
share and change all versions of a program--to make sure it remains free |
||||
|
software for all its users. We, the Free Software Foundation, use the |
||||
|
GNU General Public License for most of our software; it applies also to |
||||
|
any other work released this way by its authors. You can apply it to |
||||
|
your programs, too. |
||||
|
|
||||
|
When we speak of free software, we are referring to freedom, not |
||||
|
price. Our General Public Licenses are designed to make sure that you |
||||
|
have the freedom to distribute copies of free software (and charge for |
||||
|
them if you wish), that you receive source code or can get it if you |
||||
|
want it, that you can change the software or use pieces of it in new |
||||
|
free programs, and that you know you can do these things. |
||||
|
|
||||
|
To protect your rights, we need to prevent others from denying you |
||||
|
these rights or asking you to surrender the rights. Therefore, you have |
||||
|
certain responsibilities if you distribute copies of the software, or if |
||||
|
you modify it: responsibilities to respect the freedom of others. |
||||
|
|
||||
|
For example, if you distribute copies of such a program, whether |
||||
|
gratis or for a fee, you must pass on to the recipients the same |
||||
|
freedoms that you received. You must make sure that they, too, receive |
||||
|
or can get the source code. And you must show them these terms so they |
||||
|
know their rights. |
||||
|
|
||||
|
Developers that use the GNU GPL protect your rights with two steps: |
||||
|
(1) assert copyright on the software, and (2) offer you this License |
||||
|
giving you legal permission to copy, distribute and/or modify it. |
||||
|
|
||||
|
For the developers' and authors' protection, the GPL clearly explains |
||||
|
that there is no warranty for this free software. For both users' and |
||||
|
authors' sake, the GPL requires that modified versions be marked as |
||||
|
changed, so that their problems will not be attributed erroneously to |
||||
|
authors of previous versions. |
||||
|
|
||||
|
Some devices are designed to deny users access to install or run |
||||
|
modified versions of the software inside them, although the manufacturer |
||||
|
can do so. This is fundamentally incompatible with the aim of |
||||
|
protecting users' freedom to change the software. The systematic |
||||
|
pattern of such abuse occurs in the area of products for individuals to |
||||
|
use, which is precisely where it is most unacceptable. Therefore, we |
||||
|
have designed this version of the GPL to prohibit the practice for those |
||||
|
products. If such problems arise substantially in other domains, we |
||||
|
stand ready to extend this provision to those domains in future versions |
||||
|
of the GPL, as needed to protect the freedom of users. |
||||
|
|
||||
|
Finally, every program is threatened constantly by software patents. |
||||
|
States should not allow patents to restrict development and use of |
||||
|
software on general-purpose computers, but in those that do, we wish to |
||||
|
avoid the special danger that patents applied to a free program could |
||||
|
make it effectively proprietary. To prevent this, the GPL assures that |
||||
|
patents cannot be used to render the program non-free. |
||||
|
|
||||
|
The precise terms and conditions for copying, distribution and |
||||
|
modification follow. |
||||
|
|
||||
|
TERMS AND CONDITIONS |
||||
|
|
||||
|
0. Definitions. |
||||
|
|
||||
|
"This License" refers to version 3 of the GNU General Public License. |
||||
|
|
||||
|
"Copyright" also means copyright-like laws that apply to other kinds of |
||||
|
works, such as semiconductor masks. |
||||
|
|
||||
|
"The Program" refers to any copyrightable work licensed under this |
||||
|
License. Each licensee is addressed as "you". "Licensees" and |
||||
|
"recipients" may be individuals or organizations. |
||||
|
|
||||
|
To "modify" a work means to copy from or adapt all or part of the work |
||||
|
in a fashion requiring copyright permission, other than the making of an |
||||
|
exact copy. The resulting work is called a "modified version" of the |
||||
|
earlier work or a work "based on" the earlier work. |
||||
|
|
||||
|
A "covered work" means either the unmodified Program or a work based |
||||
|
on the Program. |
||||
|
|
||||
|
To "propagate" a work means to do anything with it that, without |
||||
|
permission, would make you directly or secondarily liable for |
||||
|
infringement under applicable copyright law, except executing it on a |
||||
|
computer or modifying a private copy. Propagation includes copying, |
||||
|
distribution (with or without modification), making available to the |
||||
|
public, and in some countries other activities as well. |
||||
|
|
||||
|
To "convey" a work means any kind of propagation that enables other |
||||
|
parties to make or receive copies. Mere interaction with a user through |
||||
|
a computer network, with no transfer of a copy, is not conveying. |
||||
|
|
||||
|
An interactive user interface displays "Appropriate Legal Notices" |
||||
|
to the extent that it includes a convenient and prominently visible |
||||
|
feature that (1) displays an appropriate copyright notice, and (2) |
||||
|
tells the user that there is no warranty for the work (except to the |
||||
|
extent that warranties are provided), that licensees may convey the |
||||
|
work under this License, and how to view a copy of this License. If |
||||
|
the interface presents a list of user commands or options, such as a |
||||
|
menu, a prominent item in the list meets this criterion. |
||||
|
|
||||
|
1. Source Code. |
||||
|
|
||||
|
The "source code" for a work means the preferred form of the work |
||||
|
for making modifications to it. "Object code" means any non-source |
||||
|
form of a work. |
||||
|
|
||||
|
A "Standard Interface" means an interface that either is an official |
||||
|
standard defined by a recognized standards body, or, in the case of |
||||
|
interfaces specified for a particular programming language, one that |
||||
|
is widely used among developers working in that language. |
||||
|
|
||||
|
The "System Libraries" of an executable work include anything, other |
||||
|
than the work as a whole, that (a) is included in the normal form of |
||||
|
packaging a Major Component, but which is not part of that Major |
||||
|
Component, and (b) serves only to enable use of the work with that |
||||
|
Major Component, or to implement a Standard Interface for which an |
||||
|
implementation is available to the public in source code form. A |
||||
|
"Major Component", in this context, means a major essential component |
||||
|
(kernel, window system, and so on) of the specific operating system |
||||
|
(if any) on which the executable work runs, or a compiler used to |
||||
|
produce the work, or an object code interpreter used to run it. |
||||
|
|
||||
|
The "Corresponding Source" for a work in object code form means all |
||||
|
the source code needed to generate, install, and (for an executable |
||||
|
work) run the object code and to modify the work, including scripts to |
||||
|
control those activities. However, it does not include the work's |
||||
|
System Libraries, or general-purpose tools or generally available free |
||||
|
programs which are used unmodified in performing those activities but |
||||
|
which are not part of the work. For example, Corresponding Source |
||||
|
includes interface definition files associated with source files for |
||||
|
the work, and the source code for shared libraries and dynamically |
||||
|
linked subprograms that the work is specifically designed to require, |
||||
|
such as by intimate data communication or control flow between those |
||||
|
subprograms and other parts of the work. |
||||
|
|
||||
|
The Corresponding Source need not include anything that users |
||||
|
can regenerate automatically from other parts of the Corresponding |
||||
|
Source. |
||||
|
|
||||
|
The Corresponding Source for a work in source code form is that |
||||
|
same work. |
||||
|
|
||||
|
2. Basic Permissions. |
||||
|
|
||||
|
All rights granted under this License are granted for the term of |
||||
|
copyright on the Program, and are irrevocable provided the stated |
||||
|
conditions are met. This License explicitly affirms your unlimited |
||||
|
permission to run the unmodified Program. The output from running a |
||||
|
covered work is covered by this License only if the output, given its |
||||
|
content, constitutes a covered work. This License acknowledges your |
||||
|
rights of fair use or other equivalent, as provided by copyright law. |
||||
|
|
||||
|
You may make, run and propagate covered works that you do not |
||||
|
convey, without conditions so long as your license otherwise remains |
||||
|
in force. You may convey covered works to others for the sole purpose |
||||
|
of having them make modifications exclusively for you, or provide you |
||||
|
with facilities for running those works, provided that you comply with |
||||
|
the terms of this License in conveying all material for which you do |
||||
|
not control copyright. Those thus making or running the covered works |
||||
|
for you must do so exclusively on your behalf, under your direction |
||||
|
and control, on terms that prohibit them from making any copies of |
||||
|
your copyrighted material outside their relationship with you. |
||||
|
|
||||
|
Conveying under any other circumstances is permitted solely under |
||||
|
the conditions stated below. Sublicensing is not allowed; section 10 |
||||
|
makes it unnecessary. |
||||
|
|
||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law. |
||||
|
|
||||
|
No covered work shall be deemed part of an effective technological |
||||
|
measure under any applicable law fulfilling obligations under article |
||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or |
||||
|
similar laws prohibiting or restricting circumvention of such |
||||
|
measures. |
||||
|
|
||||
|
When you convey a covered work, you waive any legal power to forbid |
||||
|
circumvention of technological measures to the extent such circumvention |
||||
|
is effected by exercising rights under this License with respect to |
||||
|
the covered work, and you disclaim any intention to limit operation or |
||||
|
modification of the work as a means of enforcing, against the work's |
||||
|
users, your or third parties' legal rights to forbid circumvention of |
||||
|
technological measures. |
||||
|
|
||||
|
4. Conveying Verbatim Copies. |
||||
|
|
||||
|
You may convey verbatim copies of the Program's source code as you |
||||
|
receive it, in any medium, provided that you conspicuously and |
||||
|
appropriately publish on each copy an appropriate copyright notice; |
||||
|
keep intact all notices stating that this License and any |
||||
|
non-permissive terms added in accord with section 7 apply to the code; |
||||
|
keep intact all notices of the absence of any warranty; and give all |
||||
|
recipients a copy of this License along with the Program. |
||||
|
|
||||
|
You may charge any price or no price for each copy that you convey, |
||||
|
and you may offer support or warranty protection for a fee. |
||||
|
|
||||
|
5. Conveying Modified Source Versions. |
||||
|
|
||||
|
You may convey a work based on the Program, or the modifications to |
||||
|
produce it from the Program, in the form of source code under the |
||||
|
terms of section 4, provided that you also meet all of these conditions: |
||||
|
|
||||
|
a) The work must carry prominent notices stating that you modified |
||||
|
it, and giving a relevant date. |
||||
|
|
||||
|
b) The work must carry prominent notices stating that it is |
||||
|
released under this License and any conditions added under section |
||||
|
7. This requirement modifies the requirement in section 4 to |
||||
|
"keep intact all notices". |
||||
|
|
||||
|
c) You must license the entire work, as a whole, under this |
||||
|
License to anyone who comes into possession of a copy. This |
||||
|
License will therefore apply, along with any applicable section 7 |
||||
|
additional terms, to the whole of the work, and all its parts, |
||||
|
regardless of how they are packaged. This License gives no |
||||
|
permission to license the work in any other way, but it does not |
||||
|
invalidate such permission if you have separately received it. |
||||
|
|
||||
|
d) If the work has interactive user interfaces, each must display |
||||
|
Appropriate Legal Notices; however, if the Program has interactive |
||||
|
interfaces that do not display Appropriate Legal Notices, your |
||||
|
work need not make them do so. |
||||
|
|
||||
|
A compilation of a covered work with other separate and independent |
||||
|
works, which are not by their nature extensions of the covered work, |
||||
|
and which are not combined with it such as to form a larger program, |
||||
|
in or on a volume of a storage or distribution medium, is called an |
||||
|
"aggregate" if the compilation and its resulting copyright are not |
||||
|
used to limit the access or legal rights of the compilation's users |
||||
|
beyond what the individual works permit. Inclusion of a covered work |
||||
|
in an aggregate does not cause this License to apply to the other |
||||
|
parts of the aggregate. |
||||
|
|
||||
|
6. Conveying Non-Source Forms. |
||||
|
|
||||
|
You may convey a covered work in object code form under the terms |
||||
|
of sections 4 and 5, provided that you also convey the |
||||
|
machine-readable Corresponding Source under the terms of this License, |
||||
|
in one of these ways: |
||||
|
|
||||
|
a) Convey the object code in, or embodied in, a physical product |
||||
|
(including a physical distribution medium), accompanied by the |
||||
|
Corresponding Source fixed on a durable physical medium |
||||
|
customarily used for software interchange. |
||||
|
|
||||
|
b) Convey the object code in, or embodied in, a physical product |
||||
|
(including a physical distribution medium), accompanied by a |
||||
|
written offer, valid for at least three years and valid for as |
||||
|
long as you offer spare parts or customer support for that product |
||||
|
model, to give anyone who possesses the object code either (1) a |
||||
|
copy of the Corresponding Source for all the software in the |
||||
|
product that is covered by this License, on a durable physical |
||||
|
medium customarily used for software interchange, for a price no |
||||
|
more than your reasonable cost of physically performing this |
||||
|
conveying of source, or (2) access to copy the |
||||
|
Corresponding Source from a network server at no charge. |
||||
|
|
||||
|
c) Convey individual copies of the object code with a copy of the |
||||
|
written offer to provide the Corresponding Source. This |
||||
|
alternative is allowed only occasionally and noncommercially, and |
||||
|
only if you received the object code with such an offer, in accord |
||||
|
with subsection 6b. |
||||
|
|
||||
|
d) Convey the object code by offering access from a designated |
||||
|
place (gratis or for a charge), and offer equivalent access to the |
||||
|
Corresponding Source in the same way through the same place at no |
||||
|
further charge. You need not require recipients to copy the |
||||
|
Corresponding Source along with the object code. If the place to |
||||
|
copy the object code is a network server, the Corresponding Source |
||||
|
may be on a different server (operated by you or a third party) |
||||
|
that supports equivalent copying facilities, provided you maintain |
||||
|
clear directions next to the object code saying where to find the |
||||
|
Corresponding Source. Regardless of what server hosts the |
||||
|
Corresponding Source, you remain obligated to ensure that it is |
||||
|
available for as long as needed to satisfy these requirements. |
||||
|
|
||||
|
e) Convey the object code using peer-to-peer transmission, provided |
||||
|
you inform other peers where the object code and Corresponding |
||||
|
Source of the work are being offered to the general public at no |
||||
|
charge under subsection 6d. |
||||
|
|
||||
|
A separable portion of the object code, whose source code is excluded |
||||
|
from the Corresponding Source as a System Library, need not be |
||||
|
included in conveying the object code work. |
||||
|
|
||||
|
A "User Product" is either (1) a "consumer product", which means any |
||||
|
tangible personal property which is normally used for personal, family, |
||||
|
or household purposes, or (2) anything designed or sold for incorporation |
||||
|
into a dwelling. In determining whether a product is a consumer product, |
||||
|
doubtful cases shall be resolved in favor of coverage. For a particular |
||||
|
product received by a particular user, "normally used" refers to a |
||||
|
typical or common use of that class of product, regardless of the status |
||||
|
of the particular user or of the way in which the particular user |
||||
|
actually uses, or expects or is expected to use, the product. A product |
||||
|
is a consumer product regardless of whether the product has substantial |
||||
|
commercial, industrial or non-consumer uses, unless such uses represent |
||||
|
the only significant mode of use of the product. |
||||
|
|
||||
|
"Installation Information" for a User Product means any methods, |
||||
|
procedures, authorization keys, or other information required to install |
||||
|
and execute modified versions of a covered work in that User Product from |
||||
|
a modified version of its Corresponding Source. The information must |
||||
|
suffice to ensure that the continued functioning of the modified object |
||||
|
code is in no case prevented or interfered with solely because |
||||
|
modification has been made. |
||||
|
|
||||
|
If you convey an object code work under this section in, or with, or |
||||
|
specifically for use in, a User Product, and the conveying occurs as |
||||
|
part of a transaction in which the right of possession and use of the |
||||
|
User Product is transferred to the recipient in perpetuity or for a |
||||
|
fixed term (regardless of how the transaction is characterized), the |
||||
|
Corresponding Source conveyed under this section must be accompanied |
||||
|
by the Installation Information. But this requirement does not apply |
||||
|
if neither you nor any third party retains the ability to install |
||||
|
modified object code on the User Product (for example, the work has |
||||
|
been installed in ROM). |
||||
|
|
||||
|
The requirement to provide Installation Information does not include a |
||||
|
requirement to continue to provide support service, warranty, or updates |
||||
|
for a work that has been modified or installed by the recipient, or for |
||||
|
the User Product in which it has been modified or installed. Access to a |
||||
|
network may be denied when the modification itself materially and |
||||
|
adversely affects the operation of the network or violates the rules and |
||||
|
protocols for communication across the network. |
||||
|
|
||||
|
Corresponding Source conveyed, and Installation Information provided, |
||||
|
in accord with this section must be in a format that is publicly |
||||
|
documented (and with an implementation available to the public in |
||||
|
source code form), and must require no special password or key for |
||||
|
unpacking, reading or copying. |
||||
|
|
||||
|
7. Additional Terms. |
||||
|
|
||||
|
"Additional permissions" are terms that supplement the terms of this |
||||
|
License by making exceptions from one or more of its conditions. |
||||
|
Additional permissions that are applicable to the entire Program shall |
||||
|
be treated as though they were included in this License, to the extent |
||||
|
that they are valid under applicable law. If additional permissions |
||||
|
apply only to part of the Program, that part may be used separately |
||||
|
under those permissions, but the entire Program remains governed by |
||||
|
this License without regard to the additional permissions. |
||||
|
|
||||
|
When you convey a copy of a covered work, you may at your option |
||||
|
remove any additional permissions from that copy, or from any part of |
||||
|
it. (Additional permissions may be written to require their own |
||||
|
removal in certain cases when you modify the work.) You may place |
||||
|
additional permissions on material, added by you to a covered work, |
||||
|
for which you have or can give appropriate copyright permission. |
||||
|
|
||||
|
Notwithstanding any other provision of this License, for material you |
||||
|
add to a covered work, you may (if authorized by the copyright holders of |
||||
|
that material) supplement the terms of this License with terms: |
||||
|
|
||||
|
a) Disclaiming warranty or limiting liability differently from the |
||||
|
terms of sections 15 and 16 of this License; or |
||||
|
|
||||
|
b) Requiring preservation of specified reasonable legal notices or |
||||
|
author attributions in that material or in the Appropriate Legal |
||||
|
Notices displayed by works containing it; or |
||||
|
|
||||
|
c) Prohibiting misrepresentation of the origin of that material, or |
||||
|
requiring that modified versions of such material be marked in |
||||
|
reasonable ways as different from the original version; or |
||||
|
|
||||
|
d) Limiting the use for publicity purposes of names of licensors or |
||||
|
authors of the material; or |
||||
|
|
||||
|
e) Declining to grant rights under trademark law for use of some |
||||
|
trade names, trademarks, or service marks; or |
||||
|
|
||||
|
f) Requiring indemnification of licensors and authors of that |
||||
|
material by anyone who conveys the material (or modified versions of |
||||
|
it) with contractual assumptions of liability to the recipient, for |
||||
|
any liability that these contractual assumptions directly impose on |
||||
|
those licensors and authors. |
||||
|
|
||||
|
All other non-permissive additional terms are considered "further |
||||
|
restrictions" within the meaning of section 10. If the Program as you |
||||
|
received it, or any part of it, contains a notice stating that it is |
||||
|
governed by this License along with a term that is a further |
||||
|
restriction, you may remove that term. If a license document contains |
||||
|
a further restriction but permits relicensing or conveying under this |
||||
|
License, you may add to a covered work material governed by the terms |
||||
|
of that license document, provided that the further restriction does |
||||
|
not survive such relicensing or conveying. |
||||
|
|
||||
|
If you add terms to a covered work in accord with this section, you |
||||
|
must place, in the relevant source files, a statement of the |
||||
|
additional terms that apply to those files, or a notice indicating |
||||
|
where to find the applicable terms. |
||||
|
|
||||
|
Additional terms, permissive or non-permissive, may be stated in the |
||||
|
form of a separately written license, or stated as exceptions; |
||||
|
the above requirements apply either way. |
||||
|
|
||||
|
8. Termination. |
||||
|
|
||||
|
You may not propagate or modify a covered work except as expressly |
||||
|
provided under this License. Any attempt otherwise to propagate or |
||||
|
modify it is void, and will automatically terminate your rights under |
||||
|
this License (including any patent licenses granted under the third |
||||
|
paragraph of section 11). |
||||
|
|
||||
|
However, if you cease all violation of this License, then your |
||||
|
license from a particular copyright holder is reinstated (a) |
||||
|
provisionally, unless and until the copyright holder explicitly and |
||||
|
finally terminates your license, and (b) permanently, if the copyright |
||||
|
holder fails to notify you of the violation by some reasonable means |
||||
|
prior to 60 days after the cessation. |
||||
|
|
||||
|
Moreover, your license from a particular copyright holder is |
||||
|
reinstated permanently if the copyright holder notifies you of the |
||||
|
violation by some reasonable means, this is the first time you have |
||||
|
received notice of violation of this License (for any work) from that |
||||
|
copyright holder, and you cure the violation prior to 30 days after |
||||
|
your receipt of the notice. |
||||
|
|
||||
|
Termination of your rights under this section does not terminate the |
||||
|
licenses of parties who have received copies or rights from you under |
||||
|
this License. If your rights have been terminated and not permanently |
||||
|
reinstated, you do not qualify to receive new licenses for the same |
||||
|
material under section 10. |
||||
|
|
||||
|
9. Acceptance Not Required for Having Copies. |
||||
|
|
||||
|
You are not required to accept this License in order to receive or |
||||
|
run a copy of the Program. Ancillary propagation of a covered work |
||||
|
occurring solely as a consequence of using peer-to-peer transmission |
||||
|
to receive a copy likewise does not require acceptance. However, |
||||
|
nothing other than this License grants you permission to propagate or |
||||
|
modify any covered work. These actions infringe copyright if you do |
||||
|
not accept this License. Therefore, by modifying or propagating a |
||||
|
covered work, you indicate your acceptance of this License to do so. |
||||
|
|
||||
|
10. Automatic Licensing of Downstream Recipients. |
||||
|
|
||||
|
Each time you convey a covered work, the recipient automatically |
||||
|
receives a license from the original licensors, to run, modify and |
||||
|
propagate that work, subject to this License. You are not responsible |
||||
|
for enforcing compliance by third parties with this License. |
||||
|
|
||||
|
An "entity transaction" is a transaction transferring control of an |
||||
|
organization, or substantially all assets of one, or subdividing an |
||||
|
organization, or merging organizations. If propagation of a covered |
||||
|
work results from an entity transaction, each party to that |
||||
|
transaction who receives a copy of the work also receives whatever |
||||
|
licenses to the work the party's predecessor in interest had or could |
||||
|
give under the previous paragraph, plus a right to possession of the |
||||
|
Corresponding Source of the work from the predecessor in interest, if |
||||
|
the predecessor has it or can get it with reasonable efforts. |
||||
|
|
||||
|
You may not impose any further restrictions on the exercise of the |
||||
|
rights granted or affirmed under this License. For example, you may |
||||
|
not impose a license fee, royalty, or other charge for exercise of |
||||
|
rights granted under this License, and you may not initiate litigation |
||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that |
||||
|
any patent claim is infringed by making, using, selling, offering for |
||||
|
sale, or importing the Program or any portion of it. |
||||
|
|
||||
|
11. Patents. |
||||
|
|
||||
|
A "contributor" is a copyright holder who authorizes use under this |
||||
|
License of the Program or a work on which the Program is based. The |
||||
|
work thus licensed is called the contributor's "contributor version". |
||||
|
|
||||
|
A contributor's "essential patent claims" are all patent claims |
||||
|
owned or controlled by the contributor, whether already acquired or |
||||
|
hereafter acquired, that would be infringed by some manner, permitted |
||||
|
by this License, of making, using, or selling its contributor version, |
||||
|
but do not include claims that would be infringed only as a |
||||
|
consequence of further modification of the contributor version. For |
||||
|
purposes of this definition, "control" includes the right to grant |
||||
|
patent sublicenses in a manner consistent with the requirements of |
||||
|
this License. |
||||
|
|
||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free |
||||
|
patent license under the contributor's essential patent claims, to |
||||
|
make, use, sell, offer for sale, import and otherwise run, modify and |
||||
|
propagate the contents of its contributor version. |
||||
|
|
||||
|
In the following three paragraphs, a "patent license" is any express |
||||
|
agreement or commitment, however denominated, not to enforce a patent |
||||
|
(such as an express permission to practice a patent or covenant not to |
||||
|
sue for patent infringement). To "grant" such a patent license to a |
||||
|
party means to make such an agreement or commitment not to enforce a |
||||
|
patent against the party. |
||||
|
|
||||
|
If you convey a covered work, knowingly relying on a patent license, |
||||
|
and the Corresponding Source of the work is not available for anyone |
||||
|
to copy, free of charge and under the terms of this License, through a |
||||
|
publicly available network server or other readily accessible means, |
||||
|
then you must either (1) cause the Corresponding Source to be so |
||||
|
available, or (2) arrange to deprive yourself of the benefit of the |
||||
|
patent license for this particular work, or (3) arrange, in a manner |
||||
|
consistent with the requirements of this License, to extend the patent |
||||
|
license to downstream recipients. "Knowingly relying" means you have |
||||
|
actual knowledge that, but for the patent license, your conveying the |
||||
|
covered work in a country, or your recipient's use of the covered work |
||||
|
in a country, would infringe one or more identifiable patents in that |
||||
|
country that you have reason to believe are valid. |
||||
|
|
||||
|
If, pursuant to or in connection with a single transaction or |
||||
|
arrangement, you convey, or propagate by procuring conveyance of, a |
||||
|
covered work, and grant a patent license to some of the parties |
||||
|
receiving the covered work authorizing them to use, propagate, modify |
||||
|
or convey a specific copy of the covered work, then the patent license |
||||
|
you grant is automatically extended to all recipients of the covered |
||||
|
work and works based on it. |
||||
|
|
||||
|
A patent license is "discriminatory" if it does not include within |
||||
|
the scope of its coverage, prohibits the exercise of, or is |
||||
|
conditioned on the non-exercise of one or more of the rights that are |
||||
|
specifically granted under this License. You may not convey a covered |
||||
|
work if you are a party to an arrangement with a third party that is |
||||
|
in the business of distributing software, under which you make payment |
||||
|
to the third party based on the extent of your activity of conveying |
||||
|
the work, and under which the third party grants, to any of the |
||||
|
parties who would receive the covered work from you, a discriminatory |
||||
|
patent license (a) in connection with copies of the covered work |
||||
|
conveyed by you (or copies made from those copies), or (b) primarily |
||||
|
for and in connection with specific products or compilations that |
||||
|
contain the covered work, unless you entered into that arrangement, |
||||
|
or that patent license was granted, prior to 28 March 2007. |
||||
|
|
||||
|
Nothing in this License shall be construed as excluding or limiting |
||||
|
any implied license or other defenses to infringement that may |
||||
|
otherwise be available to you under applicable patent law. |
||||
|
|
||||
|
12. No Surrender of Others' Freedom. |
||||
|
|
||||
|
If conditions are imposed on you (whether by court order, agreement or |
||||
|
otherwise) that contradict the conditions of this License, they do not |
||||
|
excuse you from the conditions of this License. If you cannot convey a |
||||
|
covered work so as to satisfy simultaneously your obligations under this |
||||
|
License and any other pertinent obligations, then as a consequence you may |
||||
|
not convey it at all. For example, if you agree to terms that obligate you |
||||
|
to collect a royalty for further conveying from those to whom you convey |
||||
|
the Program, the only way you could satisfy both those terms and this |
||||
|
License would be to refrain entirely from conveying the Program. |
||||
|
|
||||
|
13. Use with the GNU Affero General Public License. |
||||
|
|
||||
|
Notwithstanding any other provision of this License, you have |
||||
|
permission to link or combine any covered work with a work licensed |
||||
|
under version 3 of the GNU Affero General Public License into a single |
||||
|
combined work, and to convey the resulting work. The terms of this |
||||
|
License will continue to apply to the part which is the covered work, |
||||
|
but the special requirements of the GNU Affero General Public License, |
||||
|
section 13, concerning interaction through a network will apply to the |
||||
|
combination as such. |
||||
|
|
||||
|
14. Revised Versions of this License. |
||||
|
|
||||
|
The Free Software Foundation may publish revised and/or new versions of |
||||
|
the GNU 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 |
||||
|
Program specifies that a certain numbered version of the GNU General |
||||
|
Public License "or any later version" applies to it, you have the |
||||
|
option of following the terms and conditions either of that numbered |
||||
|
version or of any later version published by the Free Software |
||||
|
Foundation. If the Program does not specify a version number of the |
||||
|
GNU General Public License, you may choose any version ever published |
||||
|
by the Free Software Foundation. |
||||
|
|
||||
|
If the Program specifies that a proxy can decide which future |
||||
|
versions of the GNU General Public License can be used, that proxy's |
||||
|
public statement of acceptance of a version permanently authorizes you |
||||
|
to choose that version for the Program. |
||||
|
|
||||
|
Later license versions may give you additional or different |
||||
|
permissions. However, no additional obligations are imposed on any |
||||
|
author or copyright holder as a result of your choosing to follow a |
||||
|
later version. |
||||
|
|
||||
|
15. Disclaimer of Warranty. |
||||
|
|
||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY |
||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT |
||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY |
||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, |
||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM |
||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF |
||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
||||
|
|
||||
|
16. Limitation of Liability. |
||||
|
|
||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS |
||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY |
||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE |
||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF |
||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD |
||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), |
||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
||||
|
SUCH DAMAGES. |
||||
|
|
||||
|
17. Interpretation of Sections 15 and 16. |
||||
|
|
||||
|
If the disclaimer of warranty and limitation of liability provided |
||||
|
above cannot be given local legal effect according to their terms, |
||||
|
reviewing courts shall apply local law that most closely approximates |
||||
|
an absolute waiver of all civil liability in connection with the |
||||
|
Program, unless a warranty or assumption of liability accompanies a |
||||
|
copy of the Program in return for a fee. |
||||
|
|
||||
|
END OF TERMS AND CONDITIONS |
||||
|
|
||||
|
How to Apply These Terms to Your New Programs |
||||
|
|
||||
|
If you develop a new program, and you want it to be of the greatest |
||||
|
possible use to the public, the best way to achieve this is to make it |
||||
|
free software which everyone can redistribute and change under these terms. |
||||
|
|
||||
|
To do so, attach the following notices to the program. It is safest |
||||
|
to attach them to the start of each source file to most effectively |
||||
|
state the exclusion of warranty; and each file should have at least |
||||
|
the "copyright" line and a pointer to where the full notice is found. |
||||
|
|
||||
|
<one line to give the program's name and a brief idea of what it does.> |
||||
|
Copyright (C) <year> <name of author> |
||||
|
|
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU General Public License as published by |
||||
|
the Free Software Foundation, either version 3 of the License, or |
||||
|
(at your option) any later version. |
||||
|
|
||||
|
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. |
||||
|
|
||||
|
You should have received a copy of the GNU General Public License |
||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
|
|
||||
|
Also add information on how to contact you by electronic and paper mail. |
||||
|
|
||||
|
If the program does terminal interaction, make it output a short |
||||
|
notice like this when it starts in an interactive mode: |
||||
|
|
||||
|
<program> Copyright (C) <year> <name of author> |
||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. |
||||
|
This is free software, and you are welcome to redistribute it |
||||
|
under certain conditions; type `show c' for details. |
||||
|
|
||||
|
The hypothetical commands `show w' and `show c' should show the appropriate |
||||
|
parts of the General Public License. Of course, your program's commands |
||||
|
might be different; for a GUI interface, you would use an "about box". |
||||
|
|
||||
|
You should also get your employer (if you work as a programmer) or school, |
||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary. |
||||
|
For more information on this, and how to apply and follow the GNU GPL, see |
||||
|
<https://www.gnu.org/licenses/>. |
||||
|
|
||||
|
The GNU General Public License does not permit incorporating your program |
||||
|
into proprietary programs. If your program is a subroutine library, you |
||||
|
may consider it more useful to permit linking proprietary applications with |
||||
|
the library. If this is what you want to do, use the GNU Lesser General |
||||
|
Public License instead of this License. But first, please read |
||||
|
<https://www.gnu.org/licenses/why-not-lgpl.html>. |
@ -1,6 +1,24 @@ |
|||||
# tdc_gpx2 |
|
||||
|
# Software for RPi compatible shield for TDC-GPX2 chip |
||||
|
The TDC-GPX2 is a time-to-digital converter from ScioSense with up to 10ps timing accuracy. This software uses the linux kernel for GPIO and SPI-Bus to readout the GPX2 and calculate time intervals between signals. There is also the possibility of reading out the TDC using a FPGA which will then give much faster readout. |
||||
|
|
||||
readout software for 2 channels [0 and 1] of the tdc-gpx2 board with raspberry pi 3B SPI readout. |
|
||||
|
|
||||
This code is a fork of the original design by marvin.peter@physik.uni-giessen.de |
|
||||
https://github.com/marvin5300/tdc-gpx2_software |
|
||||
|
# TDC-GPX2_Software |
||||
|
Files for building readout software for the TDC-GPX2 from ScioSense. |
||||
|
| Folder | Description | |
||||
|
| ------ | ----------- | |
||||
|
| source/gpx2-raspi-readout-program| Contains a readout loop, buffer and calculations to measure time intervals between two signals with good accuracy. |
||||
|
| source/spi | Folder containing gpx2 source files for making the tdc board library containing all options and an high level access for readout. |
||||
|
| source/gpx2-fpga | tba | |
||||
|
<br> |
||||
|
<strong>Readout via SPI:</strong> |
||||
|
|
||||
|
Required: |
||||
|
- linux operating system (preferrably raspberry pi OS) |
||||
|
- up-to-date c++ compiler (C++17 compatible), tested with clang 11 and newer |
||||
|
|
||||
|
How to build: |
||||
|
- This software packages is build with cmake |
||||
|
- builds the gpx2 library and optionally the readout program |
||||
|
|
||||
|
<br> |
||||
|
<strong>Readout via LVDS using an FPGA:</strong><br> |
||||
|
coming soon |
Binary file not shown.
After Width: 650 | Height: 921 | Size: 69 KiB |
Binary file not shown.
After Width: 640 | Height: 480 | Size: 20 KiB |
Binary file not shown.
After Width: 640 | Height: 480 | Size: 21 KiB |
Binary file not shown.
After Width: 640 | Height: 480 | Size: 18 KiB |
@ -0,0 +1,35 @@ |
|||||
|
import numpy as np |
||||
|
import matplotlib.pyplot as plt |
||||
|
|
||||
|
# Open the file and read in the data |
||||
|
with open('../data/test02.dat', 'r') as f: |
||||
|
lines = f.readlines() |
||||
|
|
||||
|
# Extract the second column of data |
||||
|
data = [] |
||||
|
for line in lines: |
||||
|
values = line.strip().split() |
||||
|
data.append(float(values[1])) |
||||
|
|
||||
|
#determine histrange from data |
||||
|
hist_range = 5 * np.std(data) |
||||
|
hist_mean = np.mean(data) |
||||
|
hist_start = hist_mean - hist_range |
||||
|
hist_end = hist_mean + hist_range |
||||
|
|
||||
|
# Create the histogram with range |
||||
|
hist, bins, _ = plt.hist(data, bins=100, range=(hist_start,hist_end)) |
||||
|
|
||||
|
# Calculate the mean and RMS |
||||
|
bin_centers = 0.5 * (bins[:-1] + bins[1:]) |
||||
|
|
||||
|
# Set the x-axis label and title |
||||
|
plt.xlabel('Data') |
||||
|
plt.title('Histogram of Data') |
||||
|
|
||||
|
# Show the plot |
||||
|
plt.show() |
||||
|
|
||||
|
# Print the mean and RMS |
||||
|
print(f"Mean: {np.mean(data):.3g}") |
||||
|
print(f"RMS: {np.std(data):.3g}") |
@ -0,0 +1,49 @@ |
|||||
|
import argparse |
||||
|
import numpy as np |
||||
|
import matplotlib.pyplot as plt |
||||
|
|
||||
|
|
||||
|
#parse the command line arguments |
||||
|
parser = argparse.ArgumentParser(description="Plot a histogram") |
||||
|
parser.add_argument("input_file",help="Path to the input data set with 7 columns") |
||||
|
args = parser.parse_args() |
||||
|
|
||||
|
# Open the file and read in the data |
||||
|
with open(args.input_file, 'r') as f: |
||||
|
lines = f.readlines() |
||||
|
|
||||
|
# Extract the second column of data |
||||
|
data = [] |
||||
|
for line in lines: |
||||
|
values = line.strip().split() |
||||
|
if len(values) >= 7 and float(values[5]) == 0: |
||||
|
data.append(float(values[6])) |
||||
|
|
||||
|
#determine histrange from data |
||||
|
hist_range = 10 * np.std(data) |
||||
|
hist_mean = np.mean(data) |
||||
|
hist_start = hist_mean - hist_range |
||||
|
hist_end = hist_mean + hist_range |
||||
|
|
||||
|
# Create the histogram with range |
||||
|
hist, bins, _ = plt.hist(data, bins=200, range=(hist_start,hist_end)) |
||||
|
|
||||
|
# Calculate the mean and RMS |
||||
|
bin_centers = 0.5 * (bins[:-1] + bins[1:]) |
||||
|
mean = np.average(bin_centers, weights=hist) |
||||
|
rms = np.sqrt(np.average((bin_centers - mean)**2, weights=hist )) |
||||
|
|
||||
|
# Set the x-axis label and title |
||||
|
plt.xlabel('time difference / seconds') |
||||
|
plt.title(' - ') |
||||
|
|
||||
|
plt.annotate(f"Mean: {mean:.3g}\nRMS; {rms:.3g}", |
||||
|
xy=(0.75, 0.85), |
||||
|
xycoords='axes fraction', |
||||
|
fontsize=12, |
||||
|
bbox=dict(boxstyle="round,pad=0.3", edgecolor="gray", facecolor="lightgray")) |
||||
|
|
||||
|
# Show the plot |
||||
|
plt.show() |
||||
|
|
||||
|
|
@ -0,0 +1,2 @@ |
|||||
|
.vs/ |
||||
|
out/ |
@ -0,0 +1,14 @@ |
|||||
|
cmake_minimum_required (VERSION 3.10) |
||||
|
project (gpx2) |
||||
|
|
||||
|
option(BUILD_TIDY "Use clang tidy to build" OFF) |
||||
|
option(BUILD_TOOLS "Build additional tools (like readout program) in addition to the library" ON) |
||||
|
option(BUILD_DOCS "Build doxygen files" OFF) |
||||
|
|
||||
|
set(CMAKE_BUILD_TYPE Release) |
||||
|
message("${CMAKE_HOST_SYSTEM_PROCESSOR}") |
||||
|
add_subdirectory(spi) |
||||
|
if (BUILD_TOOLS) |
||||
|
add_subdirectory(gpx2-raspi-readout-program) |
||||
|
endif (BUILD_TOOLS) |
||||
|
|
@ -0,0 +1,2 @@ |
|||||
|
.vs/ |
||||
|
out/ |
@ -0,0 +1,126 @@ |
|||||
|
cmake_minimum_required(VERSION 3.10) |
||||
|
project(gpx2 LANGUAGES CXX C) |
||||
|
|
||||
|
set(PROJECT_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") |
||||
|
set(PROJECT_HEADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") |
||||
|
set(PROJECT_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config") |
||||
|
|
||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../lib") |
||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../lib") |
||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin") |
||||
|
|
||||
|
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake") |
||||
|
|
||||
|
set(CMAKE_CXX_STANDARD 17) |
||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
set(CMAKE_C_STANDARD 11) |
||||
|
set(CMAKE_C_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
# check if Doxygen is installed (from https://vicrucann.github.io/tutorials/quick-cmake-doxygen/) |
||||
|
find_package(Doxygen) |
||||
|
if (DOXYGEN_FOUND AND BUILD_DOCS) |
||||
|
# set input and output files |
||||
|
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile.in) |
||||
|
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/doxygen/Doxyfile) |
||||
|
|
||||
|
# request to configure the file |
||||
|
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) |
||||
|
|
||||
|
# note the option ALL which allows to build the docs together with the application |
||||
|
add_custom_target( doc_doxygen ALL |
||||
|
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} |
||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
||||
|
COMMENT "Generating API documentation with Doxygen" |
||||
|
VERBATIM ) |
||||
|
elseif (BUILD_DOCS) |
||||
|
message("doxygen need to be installed to generate the doxygen documentation. for generating tree views graphviz is needed.") |
||||
|
else () |
||||
|
message("Selected option doxygen build OFF. Doxygen will not be build.") |
||||
|
endif () |
||||
|
|
||||
|
find_library(PIGPIOD_IF2 pigpiod_if2 REQUIRED) |
||||
|
|
||||
|
add_compile_options( |
||||
|
-Wall |
||||
|
-Wextra |
||||
|
-Wshadow |
||||
|
-Wpedantic |
||||
|
-O3 |
||||
|
) |
||||
|
|
||||
|
configure_file( |
||||
|
"${PROJECT_CONFIG_DIR}/version.h" |
||||
|
"${PROJECT_HEADER_DIR}/version.h" |
||||
|
) |
||||
|
|
||||
|
set(GPX2_SOURCE_FILES |
||||
|
"${PROJECT_SRC_DIR}/gpx2.cpp" |
||||
|
"${PROJECT_SRC_DIR}/config.cpp" |
||||
|
) |
||||
|
|
||||
|
set(GPX2_HEADER_FILES |
||||
|
"${PROJECT_HEADER_DIR}/gpx2.h" |
||||
|
"${PROJECT_HEADER_DIR}/config.h" |
||||
|
) |
||||
|
|
||||
|
add_definitions(-Dgpx2_objlib_LIBRARY_EXPORT) |
||||
|
|
||||
|
add_library(gpx2_objlib OBJECT ${GPX2_SOURCE_FILES} ${GPX2_HEADER_FILES}) |
||||
|
|
||||
|
add_library(gpx2 SHARED $<TARGET_OBJECTS:gpx2_objlib>) |
||||
|
add_library(gpx2_static STATIC $<TARGET_OBJECTS:gpx2_objlib>) |
||||
|
|
||||
|
target_include_directories(gpx2_objlib PUBLIC |
||||
|
${PROJECT_HEADER_DIR} |
||||
|
) |
||||
|
|
||||
|
set_property(TARGET gpx2_objlib PROPERTY POSITION_INDEPENDENT_CODE 1) |
||||
|
|
||||
|
target_link_libraries(gpx2_objlib |
||||
|
pigpiod_if2 |
||||
|
) |
||||
|
|
||||
|
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s") |
||||
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") |
||||
|
|
||||
|
set_target_properties( |
||||
|
gpx2 |
||||
|
PROPERTIES |
||||
|
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" |
||||
|
) |
||||
|
|
||||
|
include(GNUInstallDirs) |
||||
|
set(GPX2_PACKAGE_NAME "gpx2") |
||||
|
add_custom_target(changelog ALL COMMAND gzip -cn9 "${PROJECT_CONFIG_DIR}/changelog" > "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz") |
||||
|
install(TARGETS gpx2 DESTINATION lib COMPONENT "${GPX2_PACKAGE_NAME}") |
||||
|
install(FILES ${GPX2_HEADER_FILES} DESTINATION include COMPONENT "${GPX2_PACKAGE_NAME}") |
||||
|
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz" DESTINATION "${CMAKE_INSTALL_DOCDIR}" COMPONENT "${GPX2_PACKAGE_NAME}") |
||||
|
install(FILES "${PROJECT_CONFIG_DIR}/copyright" DESTINATION "${CMAKE_INSTALL_DOCDIR}" COMPONENT "${GPX2_PACKAGE_NAME}") |
||||
|
|
||||
|
set(CPACK_GENERATOR "DEB") |
||||
|
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) |
||||
|
set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_CONFIG_DIR}/copyright") |
||||
|
set(CPACK_DEBIAN_PACKAGE_DEPENDS "pigpiod") |
||||
|
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_CONFIG_DIR}/license") |
||||
|
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_CONFIG_DIR}/triggers") |
||||
|
set(CPACK_PACKAGE_VENDOR "Marvin Peter") |
||||
|
set(CPACK_DEBIAN_PACKAGE_SECTION "libs") |
||||
|
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/marvin5300/GPX2-TDC") |
||||
|
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") |
||||
|
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}") |
||||
|
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}") |
||||
|
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}") |
||||
|
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Library for GPX2 TDC control") |
||||
|
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION " It makes connecting to TDC board possible. |
||||
|
It is licensed under the GNU Lesser General Public License version 3 (LGPL v3).") |
||||
|
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Marvin Peter <marvin.peter@physik.uni-giessen.de>") |
||||
|
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../packages/") |
||||
|
set(CPACK_STRIP_FILES "${CMAKE_CURRENT_BINARY_DIR}/../lib/${GPX2_PACKAGE_NAME}") |
||||
|
set(CPACK_PACKAGE_NAME "${GPX2_PACKAGE_NAME}") |
||||
|
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${GPX2_PACKAGE_NAME}") |
||||
|
set(CPACK_SYSTEM_NAME "Linux-armv7l") |
||||
|
set(CPACK_TOPLEVEL_TAG "Linux-armv7l") |
||||
|
set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON) |
||||
|
|
||||
|
include(CPack) |
@ -0,0 +1,2 @@ |
|||||
|
This library is deprecated, it uses pigpiod to communicate via spi. |
||||
|
It is recommended to use the new spi library with gpx2 device implemented inside using spidev c api. |
@ -0,0 +1,3 @@ |
|||||
|
set(PROJECT_VERSION_MAJOR 1) |
||||
|
set(PROJECT_VERSION_MINOR 0) |
||||
|
set(PROJECT_VERSION_PATCH 0) |
@ -0,0 +1,5 @@ |
|||||
|
gpx2 (1.0.0) ; urgency=low |
||||
|
|
||||
|
* Initial release. |
||||
|
|
||||
|
-- Marvin Peter <marvin.peter@physik.uni-giessen.de> Fri, 12 Feb 2021 19:20:00 +0100 |
@ -0,0 +1,24 @@ |
|||||
|
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
||||
|
#Upstream-Name: gpx2 |
||||
|
#Source: https://github.com/marvin5300/GPX2-TDC |
||||
|
|
||||
|
Files: * |
||||
|
Copyright: 2021 Marvin Peter <marvin.peter@physik.uni-giessen.de> |
||||
|
License: GPL-3.0+ |
||||
|
|
||||
|
License: GPL-3.0+ |
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU General Public License as published by |
||||
|
the Free Software Foundation, either version 3 of the License, or |
||||
|
(at your option) any later version. |
||||
|
. |
||||
|
This package 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. |
||||
|
. |
||||
|
You should have received a copy of the GNU General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
. |
||||
|
On Debian systems, the complete text of the GNU General |
||||
|
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". |
@ -0,0 +1,24 @@ |
|||||
|
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
||||
|
#Upstream-Name: gpx2 |
||||
|
#Source: https://github.com/marvin5300/GPX2-TDC |
||||
|
|
||||
|
Files: * |
||||
|
Copyright: 2021 Marvin Peter <marvin.peter@physik.uni-giessen.de> |
||||
|
License: GPL-3.0+ |
||||
|
|
||||
|
License: GPL-3.0+ |
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU General Public License as published by |
||||
|
the Free Software Foundation, either version 3 of the License, or |
||||
|
(at your option) any later version. |
||||
|
. |
||||
|
This package 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. |
||||
|
. |
||||
|
You should have received a copy of the GNU General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
. |
||||
|
On Debian systems, the complete text of the GNU General |
||||
|
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". |
@ -0,0 +1 @@ |
|||||
|
activate-noawait ldconfig |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef GPX2_VERSION_H |
||||
|
#define GPX2_VERSION_H |
||||
|
|
||||
|
namespace GPX2_TDC::CMake::Version { |
||||
|
constexpr int major { @PROJECT_VERSION_MAJOR@ }; |
||||
|
constexpr int minor { @PROJECT_VERSION_MINOR@ }; |
||||
|
constexpr int patch { @PROJECT_VERSION_PATCH@ }; |
||||
|
} |
||||
|
|
||||
|
#endif // GPX2_VERSION_H |
2495
source/gpx2-raspi-pigpiod-lib-deprecated/doxygen/Doxyfile.in
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,58 @@ |
|||||
|
#ifndef CONFIG_H |
||||
|
#define CONFIG_H |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
#include <string> |
||||
|
|
||||
|
namespace GPX2_TDC { |
||||
|
struct Config { |
||||
|
// reg_addr 0 |
||||
|
uint8_t PIN_ENA_RSTIDX : 1; |
||||
|
uint8_t PIN_ENA_DISABLE : 1; |
||||
|
uint8_t PIN_ENA_LVDS_OUT : 1; |
||||
|
uint8_t PIN_ENA_REFCLK : 1; |
||||
|
uint8_t PIN_ENA_STOP4 : 1; |
||||
|
uint8_t PIN_ENA_STOP3 : 1; |
||||
|
uint8_t PIN_ENA_STOP2 : 1; |
||||
|
uint8_t PIN_ENA_STOP1 : 1; |
||||
|
|
||||
|
// reg_addr 1 |
||||
|
uint8_t HIGH_RESOLUTION : 2; |
||||
|
uint8_t CHANNEL_COMBINE : 2; |
||||
|
uint8_t HIT_ENA_STOP4 : 1; |
||||
|
uint8_t HIT_ENA_STOP3 : 1; |
||||
|
uint8_t HIT_ENA_STOP2 : 1; |
||||
|
uint8_t HIT_ENA_STOP1 : 1; |
||||
|
|
||||
|
// reg_addr 2 |
||||
|
uint8_t BLOCKWISE_FIFO_READ : 1; |
||||
|
uint8_t COMMON_FIFO_READ : 1; |
||||
|
uint8_t LVS_DOUBLE_DATA_RATE : 1; |
||||
|
uint8_t STOP_DATA_BITWIDTH : 2; |
||||
|
uint8_t REF_INDEX_BITWIDTH : 3; |
||||
|
|
||||
|
// reg_addr 3 |
||||
|
uint8_t REFCLK_DIVISIONS_LOWER; // (Lower byte) |
||||
|
|
||||
|
// reg_addr 4 |
||||
|
uint8_t REFCLK_DIVISIONS_MIDDLE; // (Middle byte) |
||||
|
|
||||
|
// reg_addr 5 |
||||
|
uint8_t REFCLK_DIVISIONS_UPPER : 4; // (Upper bits) |
||||
|
|
||||
|
// reg_addr 6 |
||||
|
uint8_t LVDS_TEST_PATTERN : 1; |
||||
|
|
||||
|
// reg_addr 7 |
||||
|
uint8_t REFCLK_BY_XOSC : 1; |
||||
|
uint8_t LVDS_DATA_VALID_ADJUST : 2; |
||||
|
|
||||
|
// reg_addr 16 |
||||
|
uint8_t CMOS_INPUT : 1; |
||||
|
|
||||
|
Config(); |
||||
|
void loadDefaultConfig(); |
||||
|
std::string str() const; |
||||
|
}; |
||||
|
} |
||||
|
#endif // !CONFIG_H |
@ -0,0 +1,46 @@ |
|||||
|
#ifndef GPX2_H |
||||
|
#define GPX2_H |
||||
|
|
||||
|
#include "config.h" |
||||
|
#include <stdint.h> |
||||
|
#include <string> |
||||
|
|
||||
|
namespace GPX2_TDC { |
||||
|
|
||||
|
static const std::uint8_t spiopc_power = 0x30; // Power on reset and stop measurement |
||||
|
static const std::uint8_t spiopc_init = 0x18; // Initializes Chip and starts measurement |
||||
|
static const std::uint8_t spiopc_write_config = 0x80; // Write to configuration register X=0..17 |
||||
|
static const std::uint8_t spiopc_read_results = 0x60; // Read opcode for result and status register X=8..31 |
||||
|
static const std::uint8_t spiopc_read_config = 0x40; // Readout of configuration register X=0..17 |
||||
|
|
||||
|
class GPX2 { |
||||
|
public: |
||||
|
GPX2(); |
||||
|
~GPX2(); |
||||
|
|
||||
|
Config config; |
||||
|
|
||||
|
void power_on_reset(); |
||||
|
void init_reset(); |
||||
|
auto write_config()->bool; |
||||
|
auto write_config(const Config& data)->bool; |
||||
|
auto write_config(const std::string& data)->bool; |
||||
|
auto write_config(const std::uint8_t reg_addr, const std::uint8_t data)->bool; |
||||
|
auto read_config()->std::string; |
||||
|
auto read_config(const std::uint8_t reg_addr)->std::uint8_t; |
||||
|
auto read_results()->std::string; |
||||
|
|
||||
|
auto writeSpi(const std::uint8_t command, const std::string& data)->bool; |
||||
|
auto readSpi(const std::uint8_t command, const std::size_t bytesToRead)->std::string; |
||||
|
auto isSpiInitialised()->bool; |
||||
|
private: |
||||
|
int pi = -1; |
||||
|
int spiHandle = -1; |
||||
|
unsigned int spi_freq = 61035; |
||||
|
uint32_t spi_flags = 0b10; |
||||
|
bool spiInitialised = false; |
||||
|
|
||||
|
auto spiInitialise()->bool; |
||||
|
}; |
||||
|
} |
||||
|
#endif // !GPX2_H |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef GPX2_VERSION_H |
||||
|
#define GPX2_VERSION_H |
||||
|
|
||||
|
namespace GPX2_TDC::CMake::Version { |
||||
|
constexpr int major { 1 }; |
||||
|
constexpr int minor { 0 }; |
||||
|
constexpr int patch { 0 }; |
||||
|
} |
||||
|
|
||||
|
#endif // GPX2_VERSION_H |
@ -0,0 +1,138 @@ |
|||||
|
#include "config.h"
|
||||
|
#include <stdint.h>
|
||||
|
#include <string>
|
||||
|
|
||||
|
using namespace GPX2_TDC; |
||||
|
|
||||
|
// default config defined by the author of this program
|
||||
|
Config::Config() |
||||
|
: PIN_ENA_RSTIDX{ 0 } |
||||
|
, PIN_ENA_DISABLE{ 0 } |
||||
|
, PIN_ENA_LVDS_OUT{ 0 } |
||||
|
, PIN_ENA_REFCLK{ 0 } |
||||
|
, PIN_ENA_STOP4{ 0 } |
||||
|
, PIN_ENA_STOP3{ 0 } |
||||
|
, PIN_ENA_STOP2{ 0 } |
||||
|
, PIN_ENA_STOP1{ 0 } // reg_addr 0
|
||||
|
, HIGH_RESOLUTION{ 0 } |
||||
|
, CHANNEL_COMBINE{ 0 } |
||||
|
, HIT_ENA_STOP4{ 0 } |
||||
|
, HIT_ENA_STOP3{ 0 } |
||||
|
, HIT_ENA_STOP2{ 0 } |
||||
|
, HIT_ENA_STOP1{ 0 } // reg_addr 1
|
||||
|
, BLOCKWISE_FIFO_READ{ 0 } |
||||
|
, COMMON_FIFO_READ{ 0 } |
||||
|
, LVS_DOUBLE_DATA_RATE{ 0 } |
||||
|
, STOP_DATA_BITWIDTH{ 0 } |
||||
|
, REF_INDEX_BITWIDTH{ 0 } // reg_addr 2
|
||||
|
, REFCLK_DIVISIONS_LOWER{ 0 } // reg_addr 3
|
||||
|
, REFCLK_DIVISIONS_MIDDLE{ 0 } // reg_addr 4
|
||||
|
, REFCLK_DIVISIONS_UPPER{ 0 } // reg_addr 5
|
||||
|
, LVDS_TEST_PATTERN{ 0 } // reg_addr 6
|
||||
|
, REFCLK_BY_XOSC{ 0 } |
||||
|
, LVDS_DATA_VALID_ADJUST{ 0 } // reg_addr 7
|
||||
|
, CMOS_INPUT{ 0 } // reg_addr 16
|
||||
|
{} |
||||
|
|
||||
|
void Config::loadDefaultConfig() { |
||||
|
// reg_addr 0
|
||||
|
PIN_ENA_DISABLE = 0; |
||||
|
PIN_ENA_RSTIDX = 0; |
||||
|
PIN_ENA_LVDS_OUT = 0; |
||||
|
PIN_ENA_REFCLK = 0; |
||||
|
PIN_ENA_STOP4 = 1; |
||||
|
PIN_ENA_STOP3 = 1; |
||||
|
PIN_ENA_STOP2 = 1; |
||||
|
PIN_ENA_STOP1 = 1; |
||||
|
|
||||
|
// reg_addr 1
|
||||
|
HIGH_RESOLUTION = 2; |
||||
|
CHANNEL_COMBINE = 0; |
||||
|
HIT_ENA_STOP4 = 1; |
||||
|
HIT_ENA_STOP3 = 1; |
||||
|
HIT_ENA_STOP2 = 1; |
||||
|
HIT_ENA_STOP1 = 1; |
||||
|
|
||||
|
// reg_addr 2
|
||||
|
BLOCKWISE_FIFO_READ = 1; |
||||
|
COMMON_FIFO_READ = 1; |
||||
|
LVS_DOUBLE_DATA_RATE = 0; |
||||
|
STOP_DATA_BITWIDTH = 0b11; |
||||
|
REF_INDEX_BITWIDTH = 0b010; |
||||
|
|
||||
|
// reg_addr 3
|
||||
|
REFCLK_DIVISIONS_LOWER = 0x40; // 5MHz clk => T = 200ns = 200,000ps
|
||||
|
|
||||
|
// reg_addr 4
|
||||
|
REFCLK_DIVISIONS_MIDDLE = 0x0d; // set REFCLK_DIVISIONS to 200,000 for LSB to be 1ps
|
||||
|
|
||||
|
// reg_addr 5
|
||||
|
REFCLK_DIVISIONS_UPPER = 0x03; // 200,000 = 0x030d40
|
||||
|
|
||||
|
// reg_addr 6
|
||||
|
LVDS_TEST_PATTERN = 0; |
||||
|
|
||||
|
// reg_addr 7
|
||||
|
REFCLK_BY_XOSC = 1; // use external quartz, ignore external clock => 1
|
||||
|
LVDS_DATA_VALID_ADJUST = 1; |
||||
|
|
||||
|
// reg_addr 16
|
||||
|
CMOS_INPUT = 1; |
||||
|
} |
||||
|
|
||||
|
// pack bits in the correct order and return a string.
|
||||
|
std::string Config::str() const { |
||||
|
std::string data; |
||||
|
uint8_t byte; |
||||
|
|
||||
|
byte = PIN_ENA_RSTIDX << 7; |
||||
|
byte |= PIN_ENA_DISABLE << 6; |
||||
|
byte |= PIN_ENA_LVDS_OUT << 5; |
||||
|
byte |= PIN_ENA_REFCLK << 4; |
||||
|
byte |= PIN_ENA_STOP4 << 3; |
||||
|
byte |= PIN_ENA_STOP3 << 2; |
||||
|
byte |= PIN_ENA_STOP2 << 1; |
||||
|
byte |= PIN_ENA_STOP1; |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = HIGH_RESOLUTION << 6; |
||||
|
byte |= CHANNEL_COMBINE << 4; |
||||
|
byte |= HIT_ENA_STOP4 << 3; |
||||
|
byte |= HIT_ENA_STOP3 << 2; |
||||
|
byte |= HIT_ENA_STOP2 << 1; |
||||
|
byte |= HIT_ENA_STOP1; |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = BLOCKWISE_FIFO_READ << 7; |
||||
|
byte |= COMMON_FIFO_READ << 6; |
||||
|
byte |= LVS_DOUBLE_DATA_RATE << 5; |
||||
|
byte |= STOP_DATA_BITWIDTH << 3; |
||||
|
byte |= REF_INDEX_BITWIDTH; |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
data += REFCLK_DIVISIONS_LOWER; |
||||
|
data += REFCLK_DIVISIONS_MIDDLE; |
||||
|
data += REFCLK_DIVISIONS_UPPER; |
||||
|
|
||||
|
byte = (0b110 << 5) | (LVDS_TEST_PATTERN << 4); |
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = (REFCLK_BY_XOSC << 8) | (0b1 << 7) | (LVDS_DATA_VALID_ADJUST << 4) | 0b0011; |
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
data += static_cast<char>(0b10100001); |
||||
|
data += static_cast<char>(0b00010011); |
||||
|
data += static_cast<char>(0b00000000); |
||||
|
data += static_cast<char>(0b00001010); |
||||
|
data += static_cast<char>(0b11001100); |
||||
|
data += static_cast<char>(0b11001100); |
||||
|
data += static_cast<char>(0b11110001); |
||||
|
data += static_cast<char>(0b01111101); |
||||
|
|
||||
|
byte = (CMOS_INPUT << 2); |
||||
|
data += static_cast<char>(byte); |
||||
|
return data; |
||||
|
} |
@ -0,0 +1,196 @@ |
|||||
|
#include "gpx2.h"
|
||||
|
#include "config.h"
|
||||
|
#include <iostream>
|
||||
|
#include <iomanip>
|
||||
|
|
||||
|
extern "C" { |
||||
|
#include <pigpiod_if2.h>
|
||||
|
} |
||||
|
|
||||
|
using namespace GPX2_TDC; |
||||
|
|
||||
|
GPX2::GPX2() { |
||||
|
pi = pigpio_start((char*)"127.0.0.1", (char*)"8888"); |
||||
|
if (pi < 0) { |
||||
|
std::cerr << "Could not connect to pigpio daemon. Is pigpiod running? Start with sudo pigpiod\n"; |
||||
|
return; |
||||
|
} |
||||
|
if (!spiInitialise()) { |
||||
|
std::cerr << "Could not initialise spi connection.\n"; |
||||
|
exit(-1); |
||||
|
return; |
||||
|
} |
||||
|
power_on_reset(); |
||||
|
} |
||||
|
|
||||
|
GPX2::~GPX2() { |
||||
|
if (spiHandle >= 0) { |
||||
|
bool status = spi_close(pi, spiHandle); |
||||
|
if (status != 0) { |
||||
|
std::cerr << "pi bad handle"; |
||||
|
} |
||||
|
} |
||||
|
if (pi >= 0) { |
||||
|
pigpio_stop(pi); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void GPX2::power_on_reset() { |
||||
|
writeSpi(spiopc_power, ""); |
||||
|
} |
||||
|
|
||||
|
void GPX2::init_reset() { |
||||
|
writeSpi(spiopc_init, ""); |
||||
|
} |
||||
|
|
||||
|
bool GPX2::write_config() { |
||||
|
return write_config(config); |
||||
|
} |
||||
|
|
||||
|
bool GPX2::write_config(const Config& data) { |
||||
|
return write_config(data.str()); |
||||
|
} |
||||
|
|
||||
|
bool GPX2::write_config(const std::string& data) { |
||||
|
if (data.size() != 17) { |
||||
|
std::cerr << "write all of the config only possible if 17 bytes of data provided\n"; |
||||
|
return false; |
||||
|
} |
||||
|
return writeSpi(spiopc_write_config, data); |
||||
|
} |
||||
|
|
||||
|
bool GPX2::write_config(const std::uint8_t reg_addr, const std::uint8_t data) { |
||||
|
if (reg_addr > 16) { |
||||
|
std::cerr << "write config is only possible on register addr. 0...16\n"; |
||||
|
return false; |
||||
|
} |
||||
|
std::string conf_str = std::string({static_cast<char>(data)}); |
||||
|
return writeSpi(spiopc_write_config | reg_addr, conf_str); |
||||
|
} |
||||
|
|
||||
|
std::string GPX2::read_config() { |
||||
|
return readSpi(spiopc_read_config, 17); |
||||
|
} |
||||
|
|
||||
|
std::uint8_t GPX2::read_config(const std::uint8_t reg_addr) { |
||||
|
if (reg_addr > 16) { |
||||
|
std::cerr << "read config is only possible on register addr. 0...16\n"; |
||||
|
return 0; |
||||
|
} |
||||
|
std::string data = readSpi(spiopc_read_config | reg_addr, 1); |
||||
|
if (data.empty()) { |
||||
|
return 0; |
||||
|
} |
||||
|
return data[0]; |
||||
|
} |
||||
|
|
||||
|
std::string GPX2::read_results() { |
||||
|
return readSpi(spiopc_read_results | 0x08, 26); |
||||
|
} |
||||
|
|
||||
|
bool GPX2::writeSpi(const std::uint8_t command, const std::string& data) { |
||||
|
if (!spiInitialised) { |
||||
|
if (!spiInitialise()) { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
const std::size_t n = data.size() + 1; |
||||
|
|
||||
|
auto txBuf{ std::make_unique<char[]>(n) }; |
||||
|
auto rxBuf{ std::make_unique<char[]>(n) }; |
||||
|
txBuf[0] = static_cast<char>(command); |
||||
|
for (std::size_t i = 1; i < n; i++) { |
||||
|
txBuf[i] = data[i - 1]; |
||||
|
} |
||||
|
|
||||
|
auto status = spi_xfer(pi, spiHandle, txBuf.get(), rxBuf.get(), n); |
||||
|
if (status != static_cast<decltype(status)>(n)) { |
||||
|
if (status == PI_BAD_HANDLE) { |
||||
|
std::cerr << "writeSpi(...) : PI_BAD_HANDLE\n"; |
||||
|
} |
||||
|
else if (status == PI_SPI_XFER_FAILED) { |
||||
|
std::cerr << "writeSpi(...) : PI_SPI_XFER_FAILED\n"; |
||||
|
} |
||||
|
else if (status == PI_BAD_SPI_COUNT) { |
||||
|
std::cerr << "writeSpi(...): PI_BAD_SPI_COUNT\n"; |
||||
|
} |
||||
|
else { |
||||
|
std::cerr << "writeSpi(...) : wrong number of bytes transfered\n"; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
return status; |
||||
|
} |
||||
|
|
||||
|
std::string GPX2::readSpi(const std::uint8_t command, const std::size_t bytesToRead) { |
||||
|
if (!spiInitialised) { |
||||
|
if (!spiInitialise()) { |
||||
|
return ""; |
||||
|
} |
||||
|
} |
||||
|
const std::size_t n = bytesToRead + 1; |
||||
|
|
||||
|
auto txBuf{ std::make_unique<char[]>(n) }; |
||||
|
auto rxBuf{ std::make_unique<char[]>(n) }; |
||||
|
txBuf[0] = static_cast<char>(command); |
||||
|
|
||||
|
auto status = spi_xfer(pi, spiHandle, txBuf.get(), rxBuf.get(), n); |
||||
|
if (status != static_cast<decltype(status)>(n)) { |
||||
|
if (status == PI_BAD_HANDLE) { |
||||
|
std::cerr << "readSpi(...) : PI_BAD_HANDLE\n"; |
||||
|
} |
||||
|
else if (status == PI_SPI_XFER_FAILED) { |
||||
|
std::cerr << "readSpi(...) : PI_SPI_XFER_FAILED\n"; |
||||
|
} |
||||
|
else if (status == PI_BAD_SPI_COUNT) { |
||||
|
std::cerr << "readSpi(...) : PI_BAD_SPI_COUNT\n"; |
||||
|
} |
||||
|
else { |
||||
|
std::cerr << "readSpi(...) : wrong number of bytes transfered\n"; |
||||
|
} |
||||
|
return ""; |
||||
|
} |
||||
|
|
||||
|
std::string data; |
||||
|
for (std::size_t i = 1; i < n; i++) { |
||||
|
data += rxBuf[i]; |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
bool GPX2::isSpiInitialised() { |
||||
|
return spiInitialised; |
||||
|
} |
||||
|
|
||||
|
bool GPX2::spiInitialise() { |
||||
|
if (spiInitialised) { |
||||
|
return true; |
||||
|
} |
||||
|
spiHandle = spi_open(pi, 0, spi_freq, spi_flags); |
||||
|
if (spiHandle < 0) { |
||||
|
std::string errstr = ""; |
||||
|
switch (spiHandle) { |
||||
|
case PI_BAD_CHANNEL: |
||||
|
errstr = "DMA channel not 0-15"; |
||||
|
break; |
||||
|
case PI_BAD_SPI_SPEED: |
||||
|
errstr = "bad SPI speed"; |
||||
|
break; |
||||
|
case PI_BAD_FLAGS: |
||||
|
errstr = "bad spi open flags"; |
||||
|
break; |
||||
|
case PI_NO_AUX_SPI: |
||||
|
errstr = "no auxiliary SPI on Pi A or B"; |
||||
|
break; |
||||
|
case PI_SPI_OPEN_FAILED: |
||||
|
errstr = "can't open SPI device"; |
||||
|
break; |
||||
|
default: |
||||
|
break; |
||||
|
} |
||||
|
std::cerr << "Could not initialise SPI bus:" << errstr << "\n"; |
||||
|
return false; |
||||
|
} |
||||
|
spiInitialised = true; |
||||
|
return true; |
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
.vs/ |
||||
|
out/ |
@ -0,0 +1,72 @@ |
|||||
|
cmake_minimum_required(VERSION 3.10) |
||||
|
project(readout LANGUAGES CXX C) |
||||
|
|
||||
|
set(PROJECT_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") |
||||
|
set(PROJECT_HEADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") |
||||
|
set(PROJECT_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config") |
||||
|
set(LIBRARY_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../gpx2-raspi-lib/include/") |
||||
|
|
||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin") |
||||
|
|
||||
|
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake") |
||||
|
|
||||
|
set(CMAKE_CXX_STANDARD 17) |
||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
set(CMAKE_C_STANDARD 11) |
||||
|
set(CMAKE_C_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
if(${BUILD_TIDY}) |
||||
|
set(CMAKE_CXX_CLANG_TIDY |
||||
|
clang-tidy; |
||||
|
-header-filter=^global; |
||||
|
-checks=-*,readability-*,bugprone-*,performace-*,clang-analyzer-*,modernize-*,hicpp-*; |
||||
|
) |
||||
|
endif(${BUILD_TIDY}) |
||||
|
|
||||
|
add_compile_options( |
||||
|
-Wall |
||||
|
-Wextra |
||||
|
-Wshadow |
||||
|
-Wpedantic |
||||
|
-O3 |
||||
|
) |
||||
|
|
||||
|
configure_file( |
||||
|
"${PROJECT_CONFIG_DIR}/version.h" |
||||
|
"${PROJECT_HEADER_DIR}/version.h" |
||||
|
) |
||||
|
|
||||
|
set(READOUT_HEADER_FILES |
||||
|
"${PROJECT_HEADER_DIR}/gpio.h" |
||||
|
"${PROJECT_HEADER_DIR}/readout.h" |
||||
|
) |
||||
|
set(READOUT_SOURCE_FILES |
||||
|
"${PROJECT_SRC_DIR}/main.cpp" |
||||
|
"${PROJECT_SRC_DIR}/gpio.cpp" |
||||
|
"${PROJECT_SRC_DIR}/readout.cpp" |
||||
|
) |
||||
|
|
||||
|
add_executable(readout ${READOUT_SOURCE_FILES} ${READOUT_HEADER_FILES}) |
||||
|
|
||||
|
target_include_directories(readout PUBLIC |
||||
|
${PROJECT_HEADER_DIR} |
||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/../spi/include/spidevices/gpx2/" |
||||
|
) |
||||
|
|
||||
|
target_link_directories(readout PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/../lib/") |
||||
|
|
||||
|
if (!WIN32) |
||||
|
find_library(LIBGPIOD libgpiod REQUIRED) |
||||
|
endif() |
||||
|
set(THREADS_PREFER_PTHREAD_FLAG ON) |
||||
|
find_package(Threads REQUIRED) |
||||
|
|
||||
|
target_link_libraries(readout |
||||
|
Threads::Threads |
||||
|
spi_static |
||||
|
gpiod |
||||
|
) |
||||
|
|
||||
|
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s") |
||||
|
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") |
@ -0,0 +1,3 @@ |
|||||
|
set(PROJECT_VERSION_MAJOR 1) |
||||
|
set(PROJECT_VERSION_MINOR 0) |
||||
|
set(PROJECT_VERSION_PATCH 0) |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef GPX2_RASPI_READOUT_VERSION_H |
||||
|
#define GPX2_RASPI_READOUT_VERSION_H |
||||
|
|
||||
|
namespace gpx2_raspi_readout::CMake::Version { |
||||
|
constexpr int major { @PROJECT_VERSION_MAJOR@ }; |
||||
|
constexpr int minor { @PROJECT_VERSION_MINOR@ }; |
||||
|
constexpr int patch { @PROJECT_VERSION_PATCH@ }; |
||||
|
} |
||||
|
|
||||
|
#endif // GPX2_RASPI_READOUT_VERSION_H |
@ -0,0 +1,128 @@ |
|||||
|
#ifndef GPIO_H |
||||
|
#define GPIO_H |
||||
|
#include <vector> |
||||
|
#include <mutex> |
||||
|
#include <shared_mutex> |
||||
|
#include <chrono> |
||||
|
#include <future> |
||||
|
#include <map> |
||||
|
#include <memory> |
||||
|
#include <linux/gpio.h> |
||||
|
#include <gpiod.h> |
||||
|
|
||||
|
// TODO: check correctness of mutex' |
||||
|
// TODO: implement and check init, step, shutdown, write functions |
||||
|
|
||||
|
enum class PinBias : std::uint8_t { |
||||
|
OpenDrain = 0x01, |
||||
|
OpenSource = 0x02, |
||||
|
ActiveLow = 0x04, |
||||
|
PullDown = 0x10, |
||||
|
PullUp = 0x20 |
||||
|
}; |
||||
|
|
||||
|
class gpio |
||||
|
{ |
||||
|
|
||||
|
public: |
||||
|
constexpr static auto standard_timeout = std::chrono::seconds{10}; |
||||
|
|
||||
|
struct event |
||||
|
{ |
||||
|
enum Type |
||||
|
{ |
||||
|
Invalid, |
||||
|
Rising, |
||||
|
Falling |
||||
|
} type{ Invalid }; |
||||
|
|
||||
|
std::size_t pin{}; |
||||
|
timespec ts; |
||||
|
|
||||
|
inline operator bool() |
||||
|
{ |
||||
|
return type != Invalid; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
struct setting |
||||
|
{ |
||||
|
[[nodiscard]] auto matches(const event& e) const -> bool; |
||||
|
std::vector<unsigned> gpio_pins; |
||||
|
}; |
||||
|
|
||||
|
class callback |
||||
|
{ |
||||
|
friend class gpio; |
||||
|
public: |
||||
|
[[nodiscard]] auto wait_async(std::chrono::milliseconds timeout)->std::future<event>; |
||||
|
[[nodiscard]] auto wait(std::chrono::milliseconds timeout)->event; |
||||
|
|
||||
|
[[nodiscard]] auto write_async(const event& e)->std::future<bool>; |
||||
|
[[nodiscard]] auto write(const event& e) -> bool; |
||||
|
[[nodiscard]] auto read(unsigned pin_num) -> int; |
||||
|
|
||||
|
callback(setting s, gpio& handler); |
||||
|
|
||||
|
private: |
||||
|
void notify(const event& e); |
||||
|
|
||||
|
setting m_setting{}; |
||||
|
event m_event {}; |
||||
|
std::condition_variable m_wait{}; |
||||
|
std::mutex m_wait_mutex{}; |
||||
|
std::shared_mutex m_access_mutex{}; |
||||
|
gpio& m_handler; |
||||
|
}; |
||||
|
|
||||
|
gpio(std::string consumer = "gpio", std::string chipname = "gpiochip0") |
||||
|
: m_consumer{std::move(consumer)} |
||||
|
, m_chipname{std::move(chipname)} |
||||
|
{}; |
||||
|
virtual ~gpio(); |
||||
|
|
||||
|
void start(); |
||||
|
|
||||
|
void stop(); |
||||
|
|
||||
|
void join(); |
||||
|
|
||||
|
[[nodiscard]] auto list_callback(setting s)->std::shared_ptr<callback>; |
||||
|
void remove_callback(std::size_t id); |
||||
|
|
||||
|
private: |
||||
|
[[nodiscard]] auto setup() -> int; |
||||
|
[[nodiscard]] auto step() -> int; |
||||
|
[[nodiscard]] auto shutdown() -> int; |
||||
|
[[nodiscard]] auto write(const event& e) -> bool; |
||||
|
[[nodiscard]] auto read(unsigned pin_num) -> int; |
||||
|
|
||||
|
void notify_all(event e); |
||||
|
|
||||
|
std::string m_consumer; |
||||
|
std::string m_chipname; |
||||
|
|
||||
|
const timespec c_wait_timeout{1,0}; |
||||
|
|
||||
|
//std::shared_ptr<::gpiod_chip> chip{nullptr}; |
||||
|
//std::shared_ptr<::gpiod_line_bulk> lines{nullptr}; |
||||
|
gpiod_chip* chip{nullptr}; |
||||
|
gpiod_line_bulk* lines{nullptr}; |
||||
|
|
||||
|
std::map< unsigned, gpiod_line* > other_lines{}; // map of other initialized lines which are requested but have no active event listening going on |
||||
|
std::vector<unsigned> pins_used_by_listeners{}; |
||||
|
|
||||
|
//inline static std::size_t global_id_counter{ 0 }; |
||||
|
std::size_t global_id_counter{ 0 }; |
||||
|
std::map < std::size_t, std::shared_ptr<callback> > m_callback{}; |
||||
|
|
||||
|
std::future<int> m_result{}; |
||||
|
|
||||
|
std::chrono::microseconds m_timeout{ 10 }; |
||||
|
|
||||
|
std::atomic<bool> m_run{ true }; |
||||
|
|
||||
|
std::vector<setting> m_settings{}; |
||||
|
}; |
||||
|
|
||||
|
#endif // GPIO_H |
@ -0,0 +1,56 @@ |
|||||
|
#ifndef READOUT_H |
||||
|
#define READOUT_H |
||||
|
|
||||
|
#include "gpx2.h" |
||||
|
#include "gpio.h" |
||||
|
#include <future> |
||||
|
#include <thread> |
||||
|
#include <iostream> |
||||
|
#include <iomanip> |
||||
|
#include <mutex> |
||||
|
#include <shared_mutex> |
||||
|
#include <condition_variable> |
||||
|
#include <chrono> |
||||
|
|
||||
|
inline static void print_hex(const std::string& str) { |
||||
|
for (auto byte : str) { |
||||
|
std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(byte) << " "; |
||||
|
} |
||||
|
std::cout << std::endl; |
||||
|
} |
||||
|
|
||||
|
class Readout { |
||||
|
public: |
||||
|
Readout(double max_ref_diff = 100e-9, unsigned interrupt_pin = 20); |
||||
|
~Readout(); |
||||
|
|
||||
|
void stop(); |
||||
|
|
||||
|
private: |
||||
|
[[nodiscard]] auto setup()->int; |
||||
|
[[nodiscard]] auto read_tdc()->int; |
||||
|
|
||||
|
[[nodiscard]] auto process_loop()->int; |
||||
|
|
||||
|
void process_queue(bool ignore_max_queue = false); |
||||
|
void dump_raw_tdc(); |
||||
|
|
||||
|
std::unique_ptr<gpio> handler{}; |
||||
|
std::shared_ptr<gpio::callback> callback{}; |
||||
|
const std::chrono::milliseconds process_loop_timeout{ std::chrono::milliseconds(100) }; |
||||
|
size_t max_queue_size{ 1000 }; |
||||
|
size_t min_queue_size{ 50 }; |
||||
|
const std::chrono::microseconds readout_loop_timeout{ std::chrono::microseconds(1) }; |
||||
|
double m_max_interval{}; |
||||
|
unsigned m_interrupt_pin{}; |
||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> start_time{}; |
||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> end_time{}; |
||||
|
std::condition_variable queue_condition; |
||||
|
std::array<std::queue<SPI::GPX2_TDC::Meas>,4> tdc_stop{}; |
||||
|
std::unique_ptr<SPI::GPX2_TDC::GPX2> gpx2{}; |
||||
|
std::atomic<uint64_t> evt_count{}; |
||||
|
std::atomic<bool> m_run{ true }; |
||||
|
std::thread gpio_thread; |
||||
|
std::thread analysis_thread; |
||||
|
}; |
||||
|
#endif // READOUT_H |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef GPX2_RASPI_READOUT_VERSION_H |
||||
|
#define GPX2_RASPI_READOUT_VERSION_H |
||||
|
|
||||
|
namespace gpx2_raspi_readout::CMake::Version { |
||||
|
constexpr int major { 1 }; |
||||
|
constexpr int minor { 0 }; |
||||
|
constexpr int patch { 0 }; |
||||
|
} |
||||
|
|
||||
|
#endif // GPX2_RASPI_READOUT_VERSION_H |
@ -0,0 +1,290 @@ |
|||||
|
#include "gpio.h"
|
||||
|
#include <shared_mutex>
|
||||
|
#include <vector>
|
||||
|
#include <algorithm>
|
||||
|
#include <chrono>
|
||||
|
#include <future>
|
||||
|
#include <memory>
|
||||
|
#include <iostream>
|
||||
|
#include <condition_variable>
|
||||
|
#include <gpiod.h>
|
||||
|
#include <ctime>
|
||||
|
|
||||
|
void gpio::start() |
||||
|
{ |
||||
|
if (m_result.valid()) { |
||||
|
std::cout << "gpio manager already started\n"; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
m_result = std::async(std::launch::async, [&] { |
||||
|
auto result{ setup() }; |
||||
|
if (result != 0) { |
||||
|
return result; |
||||
|
} |
||||
|
while (m_run) { |
||||
|
if (lines != nullptr) { |
||||
|
result = step(); |
||||
|
if (result != 0) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
else { |
||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result + shutdown(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
void gpio::stop() |
||||
|
{ |
||||
|
m_run = false; |
||||
|
} |
||||
|
|
||||
|
void gpio::join() |
||||
|
{ |
||||
|
if (!m_result.valid()) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
m_result.wait(); |
||||
|
} |
||||
|
|
||||
|
gpio::~gpio() |
||||
|
{ |
||||
|
if (m_result.valid()) { |
||||
|
stop(); |
||||
|
m_result.wait(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
auto gpio::list_callback(setting s) -> std::shared_ptr<callback> |
||||
|
{ |
||||
|
if (m_result.valid()) { |
||||
|
return nullptr; |
||||
|
} |
||||
|
|
||||
|
m_settings.emplace_back(s); |
||||
|
|
||||
|
m_callback.emplace(global_id_counter, std::make_shared<callback>(std::move(s),*this)); |
||||
|
global_id_counter++; |
||||
|
|
||||
|
return m_callback[global_id_counter - 1U]; |
||||
|
} |
||||
|
|
||||
|
void gpio::remove_callback(std::size_t id) |
||||
|
{ |
||||
|
if (m_callback.find(id) == m_callback.end()) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
m_callback.erase(id); |
||||
|
} |
||||
|
|
||||
|
void gpio::notify_all(event e) |
||||
|
{ |
||||
|
for (auto& [id, cb] : m_callback) { |
||||
|
cb->notify(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
auto gpio::setting::matches(const event& e)const -> bool { |
||||
|
// if pin of the event is in m_setting gpio_pins list then it matches
|
||||
|
// maybe more efficient way of doing this
|
||||
|
return (std::find(gpio_pins.begin(), gpio_pins.end(), e.pin) != gpio_pins.end()); |
||||
|
} |
||||
|
|
||||
|
gpio::callback::callback(setting s, gpio& handler) |
||||
|
: m_setting{ std::move(s) } |
||||
|
, m_handler{ handler } |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
auto gpio::callback::wait_async(std::chrono::milliseconds timeout) -> std::future<event> |
||||
|
{ |
||||
|
return std::async(std::launch::async, [&] {return wait(timeout); }); |
||||
|
} |
||||
|
|
||||
|
auto gpio::callback::wait(std::chrono::milliseconds timeout) -> event |
||||
|
{ |
||||
|
{ |
||||
|
std::unique_lock<std::mutex> lock{ m_wait_mutex }; |
||||
|
|
||||
|
if (m_wait.wait_for(lock, timeout) == std::cv_status::timeout) { |
||||
|
return event{}; |
||||
|
} |
||||
|
} |
||||
|
std::unique_lock<std::shared_mutex> lock{ m_access_mutex }; |
||||
|
return m_event; |
||||
|
} |
||||
|
|
||||
|
auto gpio::callback::write_async(const event& e) -> std::future<bool> |
||||
|
{ |
||||
|
return std::async(std::launch::async, [&] { return write(e); }); |
||||
|
} |
||||
|
|
||||
|
auto gpio::callback::write(const event& e) -> bool |
||||
|
{ |
||||
|
return m_handler.write(e); |
||||
|
} |
||||
|
|
||||
|
auto gpio::callback::read(unsigned pin_num) -> int { |
||||
|
return m_handler.read(pin_num); |
||||
|
} |
||||
|
|
||||
|
void gpio::callback::notify(const event& e) |
||||
|
{ |
||||
|
if (!m_setting.matches(e)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
std::unique_lock<std::shared_mutex> lock{ m_access_mutex }; |
||||
|
m_event = e; |
||||
|
m_wait.notify_all(); |
||||
|
} |
||||
|
|
||||
|
auto gpio::setup() -> int { |
||||
|
chip = gpiod_chip_open_by_name(m_chipname.c_str()); |
||||
|
if (chip==nullptr) { |
||||
|
std::cerr << "Open gpio chip failed" << std::endl; |
||||
|
return -1; |
||||
|
} |
||||
|
for (auto s : m_settings) { |
||||
|
for (auto pin : s.gpio_pins) { |
||||
|
pins_used_by_listeners.push_back(pin); |
||||
|
} |
||||
|
} |
||||
|
std::sort(pins_used_by_listeners.begin(), pins_used_by_listeners.end()); |
||||
|
pins_used_by_listeners.erase(std::unique(pins_used_by_listeners.begin(), pins_used_by_listeners.end()), pins_used_by_listeners.end()); |
||||
|
|
||||
|
if (pins_used_by_listeners.empty()) { |
||||
|
return 0; |
||||
|
} |
||||
|
lines = new gpiod_line_bulk{}; |
||||
|
gpiod_line_bulk_init(lines); |
||||
|
int status = gpiod_chip_get_lines(chip, pins_used_by_listeners.data(), pins_used_by_listeners.size(),lines); |
||||
|
if (status<0){ |
||||
|
std::cerr << "Chip get lines failed" << std::endl; |
||||
|
return status; |
||||
|
} |
||||
|
status = gpiod_line_request_bulk_both_edges_events(lines, m_consumer.c_str()); |
||||
|
if (status<0){ |
||||
|
std::cerr << "Line request bulk both edges failed" << std::endl; |
||||
|
return status; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
auto gpio::step() -> int |
||||
|
{ |
||||
|
auto* fired = new gpiod_line_bulk{}; |
||||
|
gpiod_line_bulk_init(fired); |
||||
|
int status = gpiod_line_event_wait_bulk(lines, &c_wait_timeout, fired); |
||||
|
if (status <= 0){ |
||||
|
// 0 -> timeout
|
||||
|
// -1 -> error
|
||||
|
delete fired; |
||||
|
//std::cout << "leaving step, " << status << std::endl;
|
||||
|
return status; |
||||
|
} |
||||
|
for (unsigned i = 0; i < gpiod_line_bulk_num_lines(fired); i++){ |
||||
|
gpiod_line* line = gpiod_line_bulk_get_line(fired, i); |
||||
|
gpiod_line_event gpio_e{}; |
||||
|
status = gpiod_line_event_read(line, &gpio_e); |
||||
|
if (status!=0){ |
||||
|
delete fired; |
||||
|
std::cout << "leaving step, line event read error" << std::endl; |
||||
|
return status; |
||||
|
} |
||||
|
gpio::event e; |
||||
|
e.pin = gpiod_line_offset(line); |
||||
|
e.ts = gpio_e.ts; |
||||
|
if (gpio_e.event_type==GPIOD_LINE_EVENT_RISING_EDGE){ |
||||
|
e.type = event::Type::Rising; |
||||
|
}else if (gpio_e.event_type==GPIOD_LINE_EVENT_FALLING_EDGE){ |
||||
|
e.type = event::Type::Falling; |
||||
|
} |
||||
|
notify_all(e); |
||||
|
} |
||||
|
delete fired; |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
auto gpio::shutdown() -> int |
||||
|
{ |
||||
|
if (lines != nullptr){ |
||||
|
gpiod_line_request_bulk_input(lines, m_consumer.c_str()); |
||||
|
gpiod_line_release_bulk(lines); |
||||
|
//delete lines;
|
||||
|
lines = nullptr; |
||||
|
} |
||||
|
for (auto [id, ptr] : other_lines){ |
||||
|
if (ptr != nullptr){ |
||||
|
gpiod_line_release(ptr); |
||||
|
ptr = nullptr; |
||||
|
} |
||||
|
} |
||||
|
other_lines.clear(); |
||||
|
if (chip != nullptr){ |
||||
|
gpiod_chip_close(chip); |
||||
|
//delete chip;
|
||||
|
chip = nullptr; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
auto gpio::write(const gpio::event& e) -> bool |
||||
|
{ |
||||
|
if (!m_result.valid()) { |
||||
|
return false; |
||||
|
} |
||||
|
std::cout << "requested write pin " << e.pin << " to " << e.type; |
||||
|
//gpiod_line* line =
|
||||
|
//gpiod_line_set_value(line, 1);
|
||||
|
//gpiod_line_request_output();
|
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
auto gpio::read(unsigned pin_num) -> int |
||||
|
{ |
||||
|
if (chip==nullptr) { |
||||
|
return -1; |
||||
|
} |
||||
|
if (std::find(pins_used_by_listeners.begin(), pins_used_by_listeners.end(), pin_num) != pins_used_by_listeners.end()) { |
||||
|
std::cerr << "Error: tried to read from pin that has already an active event listener." << std::endl; |
||||
|
m_run = false; |
||||
|
return -1; |
||||
|
} |
||||
|
gpiod_line* line = nullptr; |
||||
|
if (other_lines.count(pin_num)==0) { |
||||
|
//std::cout << other_lines.count(pin_num) << std::endl;
|
||||
|
line = gpiod_chip_get_line(chip, pin_num); |
||||
|
if (!line) { |
||||
|
std::cerr << "Chip get lines failed for line " << pin_num << std::endl; |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
int ret = gpiod_line_request_input(line, m_consumer.c_str()); |
||||
|
if (ret < 0) { |
||||
|
std::cerr << "Request line as input failed" << std::endl; |
||||
|
gpiod_line_release(line); |
||||
|
return -1; |
||||
|
} |
||||
|
other_lines[pin_num] = line; |
||||
|
} |
||||
|
else { |
||||
|
line = other_lines.at(pin_num); |
||||
|
} |
||||
|
int val = gpiod_line_get_value(line); |
||||
|
if (val < 0) { |
||||
|
std::cerr << "Read line input failed" << std::endl; |
||||
|
other_lines.erase(pin_num); |
||||
|
gpiod_line_release(line); |
||||
|
return -1; |
||||
|
} |
||||
|
return val; |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
#include "readout.h"
|
||||
|
#include <chrono>
|
||||
|
#include <csignal>
|
||||
|
#include <condition_variable>
|
||||
|
#include <mutex>
|
||||
|
|
||||
|
constexpr double max_interval { 200e-9 }; // maximum interval between two stop bits to count as hit
|
||||
|
//constexpr unsigned wait_timeout { 10000 }; // if no signal from detector for wait_timeout amount of time before restarting the waiting...
|
||||
|
constexpr unsigned interrupt_pin { 20 }; // interrupt pin for the falling edge signal coming from GPX2 chip
|
||||
|
//constexpr size_t min_buffer = 200; // minimum number of events stored in buffer
|
||||
|
|
||||
|
std::condition_variable main_run; |
||||
|
void signalHandler(int signum) { |
||||
|
std::cerr << "received signal " << signum << std::endl; |
||||
|
main_run.notify_all(); |
||||
|
} |
||||
|
|
||||
|
auto main()->int { |
||||
|
std::signal(SIGTERM, signalHandler); |
||||
|
std::signal(SIGQUIT, signalHandler); |
||||
|
std::signal(SIGINT, signalHandler); |
||||
|
Readout readout{max_interval, interrupt_pin}; |
||||
|
std::mutex mx; |
||||
|
std::unique_lock<std::mutex> lock{mx}; |
||||
|
main_run.wait(lock); |
||||
|
return 0; |
||||
|
} |
@ -0,0 +1,249 @@ |
|||||
|
#include "readout.h"
|
||||
|
#include "gpio.h"
|
||||
|
#include <future>
|
||||
|
#include <string>
|
||||
|
#include <iostream>
|
||||
|
#include <iomanip>
|
||||
|
#include <queue>
|
||||
|
#include <cmath>
|
||||
|
#include <omp.h>
|
||||
|
#include <chrono>
|
||||
|
#include <csignal>
|
||||
|
#include <algorithm>
|
||||
|
|
||||
|
Readout::Readout(double max_ref_diff, unsigned interrupt_pin) |
||||
|
: m_max_interval{max_ref_diff} |
||||
|
, m_interrupt_pin{interrupt_pin} |
||||
|
, gpio_thread{ |
||||
|
[&] { |
||||
|
auto result{ setup() }; |
||||
|
if (result != 0) { |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
while (m_run) { |
||||
|
result = read_tdc(); |
||||
|
if (result != 0) { |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
,analysis_thread{ |
||||
|
[&] { |
||||
|
start_time = std::chrono::high_resolution_clock::now(); |
||||
|
while (m_run) { |
||||
|
// std::this_thread::sleep_for(process_loop_timeout);
|
||||
|
dump_raw_tdc(); |
||||
|
// process_queue();
|
||||
|
} |
||||
|
if (!tdc_stop[1].empty() && !tdc_stop[0].empty()) { |
||||
|
dump_raw_tdc(); |
||||
|
//process_queue(true);
|
||||
|
} |
||||
|
end_time = std::chrono::high_resolution_clock::now(); |
||||
|
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count(); |
||||
|
std::cerr << evt_count << " events, "; |
||||
|
std::cerr << duration << " ms, "; |
||||
|
auto rate = static_cast<double>(evt_count) / static_cast<double>(duration) * 1e6; |
||||
|
std::cerr << "rate is " << rate << "/s, " << rate * 60 << "/min, " << rate * 60 * 60 << "/h, " << rate * 60 * 60 * 24 << "/d, " << rate * 60 * 60 * 24 * 7 << "/w" << std::endl; |
||||
|
std::cerr << "non processed events in queues: " << tdc_stop[0].size() << " " << tdc_stop[1].size() << " (should not be greate than " << max_queue_size << ")" << std::endl; |
||||
|
return 0; |
||||
|
} |
||||
|
}{} |
||||
|
|
||||
|
Readout::~Readout() { |
||||
|
stop(); |
||||
|
gpio_thread.join(); |
||||
|
analysis_thread.join(); |
||||
|
} |
||||
|
|
||||
|
void Readout::stop() |
||||
|
{ |
||||
|
m_run = false; |
||||
|
} |
||||
|
|
||||
|
auto Readout::setup()->int { |
||||
|
if (gpx2) { |
||||
|
std::cerr << "tried to create new gpx2 device but it is already created." << std::endl; |
||||
|
return -1; |
||||
|
} |
||||
|
gpx2 = std::make_unique<SPI::GPX2_TDC::GPX2>(); |
||||
|
SPI::GPX2_TDC::Config conf{}; |
||||
|
conf.loadDefaultConfig(); |
||||
|
conf.BLOCKWISE_FIFO_READ = 1U; |
||||
|
conf.COMMON_FIFO_READ = 0U; |
||||
|
|
||||
|
//conf.PIN_ENA_STOP3 = 0;
|
||||
|
//conf.PIN_ENA_STOP4 = 0;
|
||||
|
//conf.HIT_ENA_STOP3 = 0;
|
||||
|
//conf.HIT_ENA_STOP4 = 0;
|
||||
|
|
||||
|
gpx2->init(); |
||||
|
|
||||
|
int verbosity = 0; |
||||
|
|
||||
|
bool status = gpx2->write_config(conf); |
||||
|
std::string config = gpx2->read_config(); |
||||
|
if (status && (config == conf.str())) { |
||||
|
if (verbosity > 1) { |
||||
|
std::cerr << "config written..." << std::endl; |
||||
|
} |
||||
|
} |
||||
|
else { |
||||
|
std::cerr << "tried to write:" << std::endl; |
||||
|
print_hex(conf.str()); |
||||
|
std::cerr << "read back:" << std::endl; |
||||
|
print_hex(config); |
||||
|
std::cerr << "failed to write config!" << std::endl; |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
|
handler = std::make_unique<gpio>(); |
||||
|
gpio::setting pin_setting{}; |
||||
|
|
||||
|
callback = handler->list_callback(pin_setting); |
||||
|
|
||||
|
handler->start(); |
||||
|
|
||||
|
gpx2->init_reset(); |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
auto Readout::read_tdc()->int { |
||||
|
// checks if data is available on the gpx2 (by checking if interrupt pin is low)
|
||||
|
// then reads out data from the gpx2-tdc and puts it in the queue
|
||||
|
while (callback->read(m_interrupt_pin) != 0) { |
||||
|
// while interrupt pin is high, do not readout (since there is no data available)
|
||||
|
// std::this_thread::sleep_for(std::chrono::microseconds(1));
|
||||
|
} |
||||
|
for (unsigned i = 0; i < 4; i++) {//why was this only 4?
|
||||
|
auto now = std::chrono::system_clock::now(); |
||||
|
auto measurements = gpx2->read_results(); |
||||
|
for (unsigned j = 0; j < 4; j++) { |
||||
|
if (measurements[j]) { |
||||
|
measurements[j].ts = now; |
||||
|
tdc_stop[j].emplace(std::move(measurements[j])); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
// std::cout << std::endl;
|
||||
|
queue_condition.notify_all(); |
||||
|
|
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
void Readout::dump_raw_tdc(){ |
||||
|
|
||||
|
std::mutex mutex; |
||||
|
std::unique_lock<std::mutex> lock {mutex}; |
||||
|
if (queue_condition.wait_for(lock,std::chrono::seconds(1))==std::cv_status::timeout) { |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
std::array<std::queue<SPI::GPX2_TDC::Meas>,4> tdc_it(tdc_stop); |
||||
|
std::cout << std::scientific << std::setprecision(5) ; //set output to 5 decimal places.
|
||||
|
|
||||
|
std::cout << "\n ----- Full data set:" <<std::endl; |
||||
|
|
||||
|
for (std::size_t j{0}; j<4; j++){ |
||||
|
while(!tdc_it[j].empty()){ |
||||
|
auto ref = static_cast<int32_t>(tdc_it[j].front().ref_index); |
||||
|
auto fasttime = SPI::GPX2_TDC::inPicoseconds(tdc_it[j].front()); |
||||
|
tdc_it[j].pop(); |
||||
|
std::cout << j << " " << ref << " " << fasttime<< " " << std::endl; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
std::cout <<" ----- Matched Pairs:" <<std::endl; |
||||
|
for (std::size_t i{0}; i<4; i=i+2){ |
||||
|
while(!tdc_stop[i].empty() && !tdc_stop[i+1].empty()){ |
||||
|
auto ref0 = static_cast<int32_t>(tdc_stop[i].front().ref_index); |
||||
|
auto fasttime0 = SPI::GPX2_TDC::inPicoseconds(tdc_stop[i].front()); |
||||
|
auto ref1 = static_cast<int32_t>(tdc_stop[i+1].front().ref_index); |
||||
|
auto fasttime1 = SPI::GPX2_TDC::inPicoseconds(tdc_stop[i+1].front()); |
||||
|
tdc_stop[i].pop();tdc_stop[i+1].pop(); |
||||
|
if (ref0-ref1==0) std::cout << i << " " << ref0 << " " << fasttime0<< " " << ref1 << " " << fasttime1<< " " << ref0-ref1 << " " << fasttime0-fasttime1<< std::endl; |
||||
|
} |
||||
|
} |
||||
|
// auto result{ setup() };
|
||||
|
|
||||
|
//remove remaining elements
|
||||
|
for (std::size_t i{0}; i<4; i++){ |
||||
|
while(!tdc_stop[i].empty()){ |
||||
|
tdc_stop[i].pop(); |
||||
|
} |
||||
|
} |
||||
|
gpx2->init_reset(); |
||||
|
evt_count += 8; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
void Readout::process_queue(bool ignore_max_queue) { |
||||
|
// checks ref_diff between ordered tdc_stop[0] and tdc_stop[1] channels.
|
||||
|
// Compares a few values from the queues, removes not matching timestamps and prints matching ones
|
||||
|
|
||||
|
std::mutex mutex; |
||||
|
std::unique_lock<std::mutex> lock {mutex}; |
||||
|
if (queue_condition.wait_for(lock,std::chrono::seconds(1))==std::cv_status::timeout || (tdc_stop[0].size() < max_queue_size && tdc_stop[1].size() < max_queue_size && !ignore_max_queue)) { |
||||
|
return; |
||||
|
} |
||||
|
std::array<std::vector<SPI::GPX2_TDC::Meas>,4> data{}; |
||||
|
for (std::size_t i{0}; i<4; i++){ |
||||
|
while(tdc_stop[i].size()>min_queue_size || (ignore_max_queue && !tdc_stop[i].empty())){ |
||||
|
data[i].emplace_back(tdc_stop[i].front()); |
||||
|
tdc_stop[i].pop(); |
||||
|
} |
||||
|
} |
||||
|
// at this point one queue is full and we can check the content for
|
||||
|
std::sort(data[0].begin(), data[0].end()); |
||||
|
std::unique(data[0].begin(), data[0].end()); |
||||
|
std::sort(data[1].begin(), data[1].end()); |
||||
|
std::unique(data[1].begin(), data[1].end()); |
||||
|
|
||||
|
auto reset_ref = [&](){return static_cast<int32_t>(data[1].front().ref_index) - static_cast<int32_t>(data[0].front().ref_index);}; |
||||
|
while (!data[0].empty() && !data[1].empty()) { |
||||
|
auto ref_diff = reset_ref(); |
||||
|
while (ref_diff > 1) { |
||||
|
//std::cout << "ref_diff " << ref_diff << std::endl;
|
||||
|
data[0].erase(data[0].begin()); |
||||
|
//evt_count += 1U;
|
||||
|
if (data[0].empty()) { |
||||
|
return; |
||||
|
} |
||||
|
ref_diff = reset_ref(); |
||||
|
} |
||||
|
while (ref_diff < 1) { |
||||
|
//std::cout << "ref_diff " << ref_diff << std::endl;
|
||||
|
data[1].erase(data[1].begin()); |
||||
|
//evt_count += 1U;
|
||||
|
if (data[1].empty()) { |
||||
|
return; |
||||
|
} |
||||
|
ref_diff = reset_ref(); |
||||
|
} |
||||
|
if (ref_diff == 0) { |
||||
|
auto diff = SPI::GPX2_TDC::diff(data[0].front(), data[1].front()); |
||||
|
auto result0 = SPI::GPX2_TDC::inPicoseconds(data[0].front()); |
||||
|
auto result1 = SPI::GPX2_TDC::inPicoseconds(data[1].front()); |
||||
|
if (fabs(diff) < m_max_interval) { |
||||
|
evt_count += 1; |
||||
|
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(data[0].front().ts.time_since_epoch()).count(); |
||||
|
// std::cout << result0 << " ";
|
||||
|
// std::cout << result1 << " ";
|
||||
|
std::cout << " "; |
||||
|
std::cout << diff; |
||||
|
// std::cout << " " << data[0].front().ref_index << " " << data[0].front().stop_result;
|
||||
|
// std::cout << " " << data[1].front().ref_index << " " << data[1].front().stop_result;
|
||||
|
std::cout << "\n"; |
||||
|
} |
||||
|
data[0].erase(data[0].begin()); |
||||
|
data[1].erase(data[1].begin()); |
||||
|
} |
||||
|
} |
||||
|
gpx2->init_reset(); |
||||
|
|
||||
|
} |
@ -0,0 +1,131 @@ |
|||||
|
cmake_minimum_required(VERSION 3.10) |
||||
|
project(spi LANGUAGES CXX C) |
||||
|
|
||||
|
set(PROJECT_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src") |
||||
|
set(PROJECT_HEADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include") |
||||
|
set(PROJECT_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config") |
||||
|
|
||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../lib") |
||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../lib") |
||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin") |
||||
|
|
||||
|
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake") |
||||
|
|
||||
|
set(CMAKE_CXX_STANDARD 17) |
||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
set(CMAKE_C_STANDARD 11) |
||||
|
set(CMAKE_C_STANDARD_REQUIRED ON) |
||||
|
|
||||
|
if(${BUILD_TIDY}) |
||||
|
set(CMAKE_CXX_CLANG_TIDY |
||||
|
clang-tidy; |
||||
|
-header-filter=^global; |
||||
|
-checks=-*,readability-*,bugprone-*,performace-*,clang-analyzer-*,modernize-*,hicpp-*,-hicpp-vararg,-hicpp-avoid-c-arrays,-readability-magic-numbers; |
||||
|
) |
||||
|
endif(${BUILD_TIDY}) |
||||
|
|
||||
|
# check if Doxygen is installed (from https://vicrucann.github.io/tutorials/quick-cmake-doxygen/) |
||||
|
find_package(Doxygen) |
||||
|
if (DOXYGEN_FOUND AND BUILD_DOCS) |
||||
|
# set input and output files |
||||
|
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile.in) |
||||
|
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/doxygen/Doxyfile) |
||||
|
|
||||
|
# request to configure the file |
||||
|
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY) |
||||
|
|
||||
|
# note the option ALL which allows to build the docs together with the application |
||||
|
add_custom_target( spi_doc_doxygen ALL |
||||
|
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT} |
||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
||||
|
COMMENT "Generating API documentation with Doxygen" |
||||
|
VERBATIM ) |
||||
|
elseif (BUILD_DOCS) |
||||
|
message("doxygen need to be installed to generate the doxygen documentation. for generating tree views graphviz is needed.") |
||||
|
else () |
||||
|
message("Selected option doxygen build OFF. Doxygen will not be build.") |
||||
|
endif () |
||||
|
|
||||
|
add_compile_options( |
||||
|
-Wall |
||||
|
-Wextra |
||||
|
-Wshadow |
||||
|
-Wpedantic |
||||
|
-O3 |
||||
|
) |
||||
|
|
||||
|
configure_file( |
||||
|
"${PROJECT_CONFIG_DIR}/version.h" |
||||
|
"${PROJECT_HEADER_DIR}/version.h" |
||||
|
) |
||||
|
|
||||
|
set(PROJECT_SOURCE_FILES |
||||
|
"${PROJECT_SRC_DIR}/spidevices/spidevice.cpp" |
||||
|
"${PROJECT_SRC_DIR}/spidevices/gpx2/gpx2.cpp" |
||||
|
"${PROJECT_SRC_DIR}/spidevices/gpx2/config.cpp" |
||||
|
) |
||||
|
|
||||
|
set(PROJECT_HEADER_FILES |
||||
|
"${PROJECT_HEADER_DIR}/spidevices/spidevice.h" |
||||
|
"${PROJECT_HEADER_DIR}/spidevices/gpx2/gpx2.h" |
||||
|
"${PROJECT_HEADER_DIR}/spidevices/gpx2/config.h" |
||||
|
) |
||||
|
|
||||
|
add_definitions(-Dspi_objlib_LIBRARY_EXPORT) |
||||
|
|
||||
|
add_library(spi_objlib OBJECT ${PROJECT_SOURCE_FILES} ${PROJECT_HEADER_FILES}) |
||||
|
|
||||
|
add_library(spi SHARED $<TARGET_OBJECTS:spi_objlib>) |
||||
|
add_library(spi_static STATIC $<TARGET_OBJECTS:spi_objlib>) |
||||
|
|
||||
|
target_include_directories(spi_objlib PUBLIC |
||||
|
${PROJECT_HEADER_DIR} |
||||
|
) |
||||
|
|
||||
|
set_property(TARGET spi_objlib PROPERTY POSITION_INDEPENDENT_CODE 1) |
||||
|
|
||||
|
#set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s") |
||||
|
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s") |
||||
|
|
||||
|
set_target_properties( |
||||
|
spi |
||||
|
PROPERTIES |
||||
|
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" |
||||
|
) |
||||
|
|
||||
|
include(GNUInstallDirs) |
||||
|
set(SPI_PACKAGE_NAME "spi") |
||||
|
add_custom_target(changelog ALL COMMAND gzip -cn9 "${PROJECT_CONFIG_DIR}/changelog" > "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz") |
||||
|
install(TARGETS spi DESTINATION lib COMPONENT "${SPI_PACKAGE_NAME}") |
||||
|
install(FILES ${PROJECT_HEADER_FILES} DESTINATION include COMPONENT "${SPI_PACKAGE_NAME}") |
||||
|
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/changelog.gz" DESTINATION "${CMAKE_INSTALL_DOCDIR}" COMPONENT "${SPI_PACKAGE_NAME}") |
||||
|
install(FILES "${PROJECT_CONFIG_DIR}/copyright" DESTINATION "${CMAKE_INSTALL_DOCDIR}" COMPONENT "${SPI_PACKAGE_NAME}") |
||||
|
|
||||
|
set(CPACK_GENERATOR "DEB") |
||||
|
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) |
||||
|
set(CPACK_PACKAGE_DESCRIPTION_FILE "${PROJECT_CONFIG_DIR}/copyright") |
||||
|
set(CPACK_DEBIAN_PACKAGE_DEPENDS "") |
||||
|
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_CONFIG_DIR}/license") |
||||
|
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${PROJECT_CONFIG_DIR}/triggers") |
||||
|
set(CPACK_PACKAGE_VENDOR "Marvin Peter") |
||||
|
set(CPACK_DEBIAN_PACKAGE_SECTION "libs") |
||||
|
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/marvin5300/SPI-TDC") |
||||
|
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") |
||||
|
set(CPACK_PACKAGE_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}") |
||||
|
set(CPACK_PACKAGE_VERSION_MINOR "${PROJECT_VERSION_MINOR}") |
||||
|
set(CPACK_PACKAGE_VERSION_PATCH "${PROJECT_VERSION_PATCH}") |
||||
|
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Library for SPI control") |
||||
|
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION " It makes connecting to SPI devices possible. |
||||
|
It uses the linux spidev c api. |
||||
|
It is licensed under the GNU Lesser General Public License version 3 (LGPL v3).") |
||||
|
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Marvin Peter <marvin.peter@physik.uni-giessen.de>") |
||||
|
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../packages/") |
||||
|
set(CPACK_STRIP_FILES "${CMAKE_CURRENT_BINARY_DIR}/../lib/${SPI_PACKAGE_NAME}") |
||||
|
set(CPACK_PACKAGE_NAME "${SPI_PACKAGE_NAME}") |
||||
|
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${SPI_PACKAGE_NAME}") |
||||
|
set(CPACK_SYSTEM_NAME "Linux-armv7l") |
||||
|
set(CPACK_TOPLEVEL_TAG "Linux-armv7l") |
||||
|
set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON) |
||||
|
|
||||
|
include(CPack) |
@ -0,0 +1,3 @@ |
|||||
|
set(PROJECT_VERSION_MAJOR 1) |
||||
|
set(PROJECT_VERSION_MINOR 0) |
||||
|
set(PROJECT_VERSION_PATCH 0) |
@ -0,0 +1,5 @@ |
|||||
|
spi (1.0.0) ; urgency=low |
||||
|
|
||||
|
* Initial release. |
||||
|
|
||||
|
-- Marvin Peter <marvin.peter@physik.uni-giessen.de> Fri, 12 Feb 2021 19:20:00 +0100 |
@ -0,0 +1,24 @@ |
|||||
|
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
||||
|
#Upstream-Name: spi |
||||
|
#Source: https://github.com/marvin5300/GPX2-TDC |
||||
|
|
||||
|
Files: * |
||||
|
Copyright: 2021 Marvin Peter <marvin.peter@physik.uni-giessen.de> |
||||
|
License: GPL-3.0+ |
||||
|
|
||||
|
License: GPL-3.0+ |
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU General Public License as published by |
||||
|
the Free Software Foundation, either version 3 of the License, or |
||||
|
(at your option) any later version. |
||||
|
. |
||||
|
This package 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. |
||||
|
. |
||||
|
You should have received a copy of the GNU General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
. |
||||
|
On Debian systems, the complete text of the GNU General |
||||
|
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". |
@ -0,0 +1,24 @@ |
|||||
|
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
||||
|
#Upstream-Name: spi |
||||
|
#Source: https://github.com/marvin5300/GPX2-TDC |
||||
|
|
||||
|
Files: * |
||||
|
Copyright: 2021 Marvin Peter <marvin.peter@physik.uni-giessen.de> |
||||
|
License: GPL-3.0+ |
||||
|
|
||||
|
License: GPL-3.0+ |
||||
|
This program is free software: you can redistribute it and/or modify |
||||
|
it under the terms of the GNU General Public License as published by |
||||
|
the Free Software Foundation, either version 3 of the License, or |
||||
|
(at your option) any later version. |
||||
|
. |
||||
|
This package 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. |
||||
|
. |
||||
|
You should have received a copy of the GNU General Public License |
||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
|
. |
||||
|
On Debian systems, the complete text of the GNU General |
||||
|
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". |
@ -0,0 +1 @@ |
|||||
|
activate-noawait ldconfig |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef SPI_H |
||||
|
#define SPI_H |
||||
|
|
||||
|
namespace SPI::CMake::Version { |
||||
|
constexpr int major{ @PROJECT_VERSION_MAJOR@ }; |
||||
|
constexpr int minor{ @PROJECT_VERSION_MINOR@ }; |
||||
|
constexpr int patch{ @PROJECT_VERSION_PATCH@ }; |
||||
|
} |
||||
|
|
||||
|
#endif // SPI_H |
2495
source/spi/doxygen/Doxyfile.in
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,62 @@ |
|||||
|
#ifndef CONFIG_H |
||||
|
#define CONFIG_H |
||||
|
|
||||
|
#include <stdint.h> |
||||
|
#include <string> |
||||
|
|
||||
|
namespace SPI { |
||||
|
namespace GPX2_TDC { |
||||
|
struct Config { |
||||
|
// reg_addr 0 |
||||
|
uint8_t PIN_ENA_RSTIDX : 1; |
||||
|
uint8_t PIN_ENA_DISABLE : 1; |
||||
|
uint8_t PIN_ENA_LVDS_OUT : 1; |
||||
|
uint8_t PIN_ENA_REFCLK : 1; |
||||
|
uint8_t PIN_ENA_STOP4 : 1; |
||||
|
uint8_t PIN_ENA_STOP3 : 1; |
||||
|
uint8_t PIN_ENA_STOP2 : 1; |
||||
|
uint8_t PIN_ENA_STOP1 : 1; |
||||
|
|
||||
|
// reg_addr 1 |
||||
|
uint8_t HIGH_RESOLUTION : 2; |
||||
|
uint8_t CHANNEL_COMBINE : 2; |
||||
|
uint8_t HIT_ENA_STOP4 : 1; |
||||
|
uint8_t HIT_ENA_STOP3 : 1; |
||||
|
uint8_t HIT_ENA_STOP2 : 1; |
||||
|
uint8_t HIT_ENA_STOP1 : 1; |
||||
|
|
||||
|
// reg_addr 2 |
||||
|
uint8_t BLOCKWISE_FIFO_READ : 1; |
||||
|
uint8_t COMMON_FIFO_READ : 1; |
||||
|
uint8_t LVS_DOUBLE_DATA_RATE : 1; |
||||
|
uint8_t STOP_DATA_BITWIDTH : 2; |
||||
|
uint8_t REF_INDEX_BITWIDTH : 3; |
||||
|
|
||||
|
// reg_addr 3 |
||||
|
uint8_t REFCLK_DIVISIONS_LOWER; // (Lower byte) |
||||
|
|
||||
|
// reg_addr 4 |
||||
|
uint8_t REFCLK_DIVISIONS_MIDDLE; // (Middle byte) |
||||
|
|
||||
|
// reg_addr 5 |
||||
|
uint8_t REFCLK_DIVISIONS_UPPER : 4; // (Upper bits) |
||||
|
|
||||
|
// reg_addr 6 |
||||
|
uint8_t LVDS_TEST_PATTERN : 1; |
||||
|
|
||||
|
// reg_addr 7 |
||||
|
uint8_t REFCLK_BY_XOSC : 1; |
||||
|
uint8_t LVDS_DATA_VALID_ADJUST : 2; |
||||
|
|
||||
|
// reg_addr 16 |
||||
|
uint8_t CMOS_INPUT : 1; |
||||
|
|
||||
|
Config(); |
||||
|
void loadDefaultConfig(); |
||||
|
double refclk_freq{5e6}; |
||||
|
uint32_t refclk_divisions(); |
||||
|
std::string str() const; |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
#endif // !CONFIG_H |
@ -0,0 +1,119 @@ |
|||||
|
#ifndef GPX2_H |
||||
|
#define GPX2_H |
||||
|
|
||||
|
#include "config.h" |
||||
|
#include "../spidevice.h" |
||||
|
#include <stdint.h> |
||||
|
#include <limits> |
||||
|
#include <string> |
||||
|
#include <vector> |
||||
|
#include <queue> |
||||
|
#include <chrono> |
||||
|
#include <cmath> |
||||
|
|
||||
|
|
||||
|
namespace SPI { |
||||
|
namespace GPX2_TDC { |
||||
|
static constexpr std::uint8_t spiopc_power = 0x30; // Power on reset and stop measurement |
||||
|
static constexpr std::uint8_t spiopc_init = 0x18; // Initializes Chip and starts measurement |
||||
|
static constexpr std::uint8_t spiopc_write_config = 0x80; // Write to configuration register X=0..17 |
||||
|
static constexpr std::uint8_t spiopc_read_results = 0x60; // Read opcode for result and status register X=8..31 |
||||
|
static constexpr std::uint8_t spiopc_read_config = 0x40; // Readout of configuration register X=0..17 |
||||
|
|
||||
|
enum class StopChannel{ |
||||
|
channel_0, |
||||
|
channel_1, |
||||
|
channel_2, |
||||
|
channel_3 |
||||
|
}; |
||||
|
|
||||
|
struct Meas{ |
||||
|
enum Status{ |
||||
|
Invalid, |
||||
|
Valid |
||||
|
}status{Invalid}; |
||||
|
StopChannel stop_channel{}; |
||||
|
uint32_t ref_index{}; |
||||
|
uint32_t stop_result{}; |
||||
|
double lsb_ps{1.}; |
||||
|
double refclk_freq{std::numeric_limits<double>::quiet_NaN()}; |
||||
|
std::chrono::time_point<std::chrono::system_clock> ts{}; |
||||
|
operator bool(); |
||||
|
auto operator < (const Meas& other) const -> bool; |
||||
|
auto operator > (const Meas& other) const -> bool; |
||||
|
auto operator == (const Meas& other) const -> bool; |
||||
|
auto operator != (const Meas& other) const -> bool; |
||||
|
auto operator <= (const Meas& other) const -> bool; |
||||
|
auto operator >= (const Meas& other) const -> bool; |
||||
|
}; |
||||
|
|
||||
|
constexpr double pico_second { 1e-12 }; // value for one pico second in seconds |
||||
|
constexpr double max_plausible_interval { }; |
||||
|
|
||||
|
inline static auto diff(const SPI::GPX2_TDC::Meas& first, const SPI::GPX2_TDC::Meas& second) -> double{ |
||||
|
if (first.status == SPI::GPX2_TDC::Meas::Invalid || second.status == SPI::GPX2_TDC::Meas::Invalid || first.refclk_freq != second.refclk_freq || first.refclk_freq == 0.) { |
||||
|
return std::nan("invalid");// std::cout << "measurement not valid" << std::endl; |
||||
|
} |
||||
|
// int32_t is possible since maximum number of bits in register is 24 |
||||
|
int32_t ref0{ static_cast<int32_t>(first.ref_index) }; |
||||
|
int32_t ref1{ static_cast<int32_t>(second.ref_index) }; |
||||
|
double refclk_period = 1 / first.refclk_freq; |
||||
|
double stop_first = first.lsb_ps * static_cast<double>(first.stop_result); |
||||
|
double stop_second = second.lsb_ps * static_cast<double>(second.stop_result); |
||||
|
double result{}; |
||||
|
result = refclk_period * static_cast<double>(ref1 - ref0); |
||||
|
result += (stop_second - stop_first) * pico_second; |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
inline static auto inPicoseconds(const SPI::GPX2_TDC::Meas& first) -> double{ |
||||
|
if (first.status == SPI::GPX2_TDC::Meas::Invalid || first.refclk_freq == 0.){ |
||||
|
return std::nan("invalid");// std::cout << "measurement not valid" << std::endl; |
||||
|
} |
||||
|
// int32_t is possible since maximum number of bits in register is 24 |
||||
|
// int32_t ref0{ static_cast<int32_t>(first.ref_index) }; |
||||
|
// double refclk_period = 1 / first.refclk_freq; |
||||
|
double stop_first = first.lsb_ps * static_cast<double>(first.stop_result); |
||||
|
double result{}; |
||||
|
// result = refclk_period * static_cast<double>(ref0); |
||||
|
result = (stop_first) * pico_second; |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
/*inline double operator-(const Meas& first, const Meas& second){ |
||||
|
if (!first || !second || first.refclk_freq != second.refclk_freq || first.refclk_freq==0.){ |
||||
|
std::cout << "measurement not valid" << std::endl; |
||||
|
} |
||||
|
double refclk_period = 1/first.refclk_freq; |
||||
|
double stop_first = first.lsb_ps*first.stop_result; |
||||
|
double stop_second = second.lsb_ps*second.stop_result; |
||||
|
double result{}; |
||||
|
result = refclk_period*static_cast<double>(second.ref_index - first.ref_index); |
||||
|
result += (stop_second - stop_first)*pico_second; |
||||
|
return result; |
||||
|
}*/ |
||||
|
|
||||
|
class GPX2 : public spiDevice { |
||||
|
public: |
||||
|
using spiDevice::spiDevice; |
||||
|
|
||||
|
void init(std::string busAddress = "/dev/spidev0.0", std::uint32_t speed = 61035, Mode mode = SPI::Mode::spi_mode_1, std::uint8_t bits = 8); |
||||
|
void power_on_reset(); |
||||
|
void init_reset(); |
||||
|
[[nodiscard]] auto write_config()->bool; |
||||
|
[[nodiscard]] auto write_config(const Config& data)->bool; |
||||
|
[[nodiscard]] auto read_config()->std::string; |
||||
|
[[nodiscard]] auto get_filtered_intervals(double max_interval)->std::vector<double>; |
||||
|
[[nodiscard]] auto read_results()->std::vector<Meas>; |
||||
|
private: |
||||
|
[[nodiscard]] auto write_config(const std::string& data)->bool; |
||||
|
[[nodiscard]] auto write_config(const std::uint8_t reg_addr, const std::uint8_t data)->bool; |
||||
|
[[nodiscard]] auto read_config(const std::uint8_t reg_addr)->std::uint8_t; |
||||
|
std::queue<Meas> readout_buffer; |
||||
|
Config config; |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
#endif // !GPX2_H |
@ -0,0 +1,73 @@ |
|||||
|
#ifndef SPI_DEVICE_H |
||||
|
#define SPI_DEVICE_H |
||||
|
|
||||
|
#include <string> |
||||
|
#include <vector> |
||||
|
|
||||
|
namespace SPI { |
||||
|
enum class Mode { |
||||
|
spi_mode_0, |
||||
|
spi_mode_1, |
||||
|
spi_mode_2, |
||||
|
spi_mode_3 |
||||
|
}; |
||||
|
|
||||
|
class spiDevice { |
||||
|
public: |
||||
|
spiDevice() = default; |
||||
|
virtual ~spiDevice(); |
||||
|
|
||||
|
auto init(std::string busAddress = "/dev/spidev0.0", std::uint32_t speed = 61035, Mode mode = Mode::spi_mode_0, uint8_t bits = 8)->bool; |
||||
|
|
||||
|
/** |
||||
|
* Writes to physical device |
||||
|
* @param command |
||||
|
* @param data |
||||
|
* @return |
||||
|
*/ |
||||
|
auto write(const std::uint8_t command, const std::string& data)->bool; |
||||
|
|
||||
|
/** |
||||
|
* Reads from physical device |
||||
|
* @param command |
||||
|
* @param nBytes |
||||
|
* @return |
||||
|
*/ |
||||
|
auto read(const std::uint8_t command, const std::size_t nBytes)->std::string; |
||||
|
|
||||
|
/** |
||||
|
* Writes one byte to a register |
||||
|
* @param regAddr |
||||
|
* @param byte |
||||
|
* @return |
||||
|
*/ |
||||
|
//virtual auto writeReg(const std::uint8_t regAddr, const std::uint8_t byte)->bool = 0; |
||||
|
|
||||
|
/** |
||||
|
* Reads one byte from a register |
||||
|
* @param regAddr |
||||
|
* @param byte |
||||
|
* @return |
||||
|
*/ |
||||
|
//virtual auto readReg(const std::uint8_t regAddr, std::uint8_t& byte)->bool = 0; |
||||
|
|
||||
|
virtual auto devicePresent()->bool; |
||||
|
|
||||
|
protected: |
||||
|
static auto spi_xfer(const int handle, const uint32_t speed, const uint8_t mode, const uint8_t bits, const uint8_t* tx, uint8_t* rx, uint32_t nBytes)->int; |
||||
|
|
||||
|
int fHandle{ -1 }; |
||||
|
unsigned long int fNrBytesWritten{ 0 }; |
||||
|
unsigned long int fNrBytesRead{ 0 }; |
||||
|
static unsigned long int fGlobalNrBytesWritten; |
||||
|
static unsigned long int fGlobalNrBytesRead; |
||||
|
static std::vector<spiDevice*> fGlobalDeviceList; |
||||
|
static unsigned int fNrDevices; |
||||
|
|
||||
|
std::uint8_t fAddress{}; |
||||
|
std::uint8_t fMode{}; |
||||
|
std::uint8_t fNrBits{}; |
||||
|
std::uint32_t fSpeed{}; |
||||
|
}; |
||||
|
} |
||||
|
#endif // SPI_DEVICE_H |
@ -0,0 +1,10 @@ |
|||||
|
#ifndef SPI_H |
||||
|
#define SPI_H |
||||
|
|
||||
|
namespace SPI::CMake::Version { |
||||
|
constexpr int major{ 1 }; |
||||
|
constexpr int minor{ 0 }; |
||||
|
constexpr int patch{ 0 }; |
||||
|
} |
||||
|
|
||||
|
#endif // SPI_H |
@ -0,0 +1,4 @@ |
|||||
|
#ifndef _SPIDEVICES_H_ |
||||
|
#define _SPIDEVICES_H_ |
||||
|
#include "spidevices/gpx2/gpx2.h" |
||||
|
#endif // !_SPIDEVICES_H_ |
@ -0,0 +1,169 @@ |
|||||
|
#include "spidevices/gpx2/config.h"
|
||||
|
#include <cstdint>
|
||||
|
#include <string>
|
||||
|
|
||||
|
using namespace SPI::GPX2_TDC; |
||||
|
|
||||
|
// default config defined by the author of this program
|
||||
|
Config::Config() |
||||
|
: PIN_ENA_RSTIDX{ 0 } |
||||
|
, PIN_ENA_DISABLE{ 0 } |
||||
|
, PIN_ENA_LVDS_OUT{ 0 } |
||||
|
, PIN_ENA_REFCLK{ 0 } |
||||
|
, PIN_ENA_STOP4{ 0 } |
||||
|
, PIN_ENA_STOP3{ 0 } |
||||
|
, PIN_ENA_STOP2{ 0 } |
||||
|
, PIN_ENA_STOP1{ 0 } // reg_addr 0
|
||||
|
, HIGH_RESOLUTION{ 0 } |
||||
|
, CHANNEL_COMBINE{ 0 } |
||||
|
, HIT_ENA_STOP4{ 0 } |
||||
|
, HIT_ENA_STOP3{ 0 } |
||||
|
, HIT_ENA_STOP2{ 0 } |
||||
|
, HIT_ENA_STOP1{ 0 } // reg_addr 1
|
||||
|
, BLOCKWISE_FIFO_READ{ 0 } |
||||
|
, COMMON_FIFO_READ{ 0 } |
||||
|
, LVS_DOUBLE_DATA_RATE{ 0 } |
||||
|
, STOP_DATA_BITWIDTH{ 0 } |
||||
|
, REF_INDEX_BITWIDTH{ 0 } // reg_addr 2
|
||||
|
, REFCLK_DIVISIONS_LOWER{ 0 } // reg_addr 3
|
||||
|
, REFCLK_DIVISIONS_MIDDLE{ 0 } // reg_addr 4
|
||||
|
, REFCLK_DIVISIONS_UPPER{ 0 } // reg_addr 5
|
||||
|
, LVDS_TEST_PATTERN{ 0 } // reg_addr 6
|
||||
|
, REFCLK_BY_XOSC{ 0 } |
||||
|
, LVDS_DATA_VALID_ADJUST{ 0 } // reg_addr 7
|
||||
|
, CMOS_INPUT{ 0 } // reg_addr 16
|
||||
|
{} |
||||
|
|
||||
|
void Config::loadDefaultConfig() { |
||||
|
// reg_addr 0
|
||||
|
PIN_ENA_DISABLE = 0; |
||||
|
PIN_ENA_RSTIDX = 0; |
||||
|
PIN_ENA_LVDS_OUT = 0; |
||||
|
PIN_ENA_REFCLK = 0; |
||||
|
PIN_ENA_STOP4 = 1; |
||||
|
PIN_ENA_STOP3 = 1; |
||||
|
PIN_ENA_STOP2 = 1; |
||||
|
PIN_ENA_STOP1 = 1; |
||||
|
|
||||
|
// reg_addr 1
|
||||
|
//constexpr std::uint8_t high_resolution_mode_0 = 0;
|
||||
|
constexpr std::uint8_t high_resolution_mode_2 = 2; |
||||
|
//constexpr std::uint8_t channel_combine_none = 0;
|
||||
|
//constexpr std::uint8_t channel_combine_pulsewidth = 2;
|
||||
|
constexpr std::uint8_t channel_combine_pulsedistance = 1; |
||||
|
HIGH_RESOLUTION = high_resolution_mode_2; |
||||
|
CHANNEL_COMBINE = channel_combine_pulsedistance; |
||||
|
HIT_ENA_STOP4 = 1; |
||||
|
HIT_ENA_STOP3 = 1; |
||||
|
HIT_ENA_STOP2 = 1; |
||||
|
HIT_ENA_STOP1 = 1; |
||||
|
|
||||
|
// reg_addr 2
|
||||
|
constexpr std::uint8_t stop_data_bitwidth_3 = 0b11; |
||||
|
constexpr std::uint8_t ref_index_bitwidth_2 = 0b010; |
||||
|
BLOCKWISE_FIFO_READ = 1; |
||||
|
COMMON_FIFO_READ = 1; |
||||
|
LVS_DOUBLE_DATA_RATE = 0; |
||||
|
STOP_DATA_BITWIDTH = stop_data_bitwidth_3; |
||||
|
REF_INDEX_BITWIDTH = ref_index_bitwidth_2; |
||||
|
|
||||
|
// reg_addr 3
|
||||
|
constexpr std::uint8_t refclk_div_200k_lower = 0x40; |
||||
|
REFCLK_DIVISIONS_LOWER = refclk_div_200k_lower; // 5MHz clk => T = 200ns = 200,000ps
|
||||
|
|
||||
|
// reg_addr 4
|
||||
|
constexpr std::uint8_t refclk_div_200k_middle = 0x0d; |
||||
|
REFCLK_DIVISIONS_MIDDLE = refclk_div_200k_middle; // set REFCLK_DIVISIONS to 200,000 for LSB to be 1ps
|
||||
|
|
||||
|
// reg_addr 5
|
||||
|
constexpr std::uint8_t refclk_div_200k_upper = 0x03; |
||||
|
REFCLK_DIVISIONS_UPPER = refclk_div_200k_upper; // 200,000 = 0x030d40
|
||||
|
|
||||
|
// reg_addr 6
|
||||
|
LVDS_TEST_PATTERN = 0; |
||||
|
|
||||
|
// reg_addr 7
|
||||
|
REFCLK_BY_XOSC = 1; // use external quartz, ignore external clock => 1
|
||||
|
LVDS_DATA_VALID_ADJUST = 1; |
||||
|
|
||||
|
// reg_addr 16
|
||||
|
CMOS_INPUT = 1; |
||||
|
} |
||||
|
|
||||
|
// pack bits in the correct order and return a string.
|
||||
|
auto Config::str() const->std::string { |
||||
|
std::string data; |
||||
|
uint8_t byte; |
||||
|
|
||||
|
byte = static_cast<uint8_t>(PIN_ENA_RSTIDX << 7U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_DISABLE << 6U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_LVDS_OUT << 5U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_REFCLK << 4U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_STOP4 << 3U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_STOP3 << 2U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_STOP2 << 1U); |
||||
|
byte |= static_cast<uint8_t>(PIN_ENA_STOP1); |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = static_cast<uint8_t>(HIGH_RESOLUTION << 6U); |
||||
|
byte |= static_cast<uint8_t>(CHANNEL_COMBINE << 4U); |
||||
|
byte |= static_cast<uint8_t>(HIT_ENA_STOP4 << 3U); |
||||
|
byte |= static_cast<uint8_t>(HIT_ENA_STOP3 << 2U); |
||||
|
byte |= static_cast<uint8_t>(HIT_ENA_STOP2 << 1U); |
||||
|
byte |= static_cast<uint8_t>(HIT_ENA_STOP1); |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = static_cast<uint8_t>(BLOCKWISE_FIFO_READ << 7U); |
||||
|
byte |= static_cast<uint8_t>(COMMON_FIFO_READ << 6U); |
||||
|
byte |= static_cast<uint8_t>(LVS_DOUBLE_DATA_RATE << 5U); |
||||
|
byte |= static_cast<uint8_t>(STOP_DATA_BITWIDTH << 3U); |
||||
|
byte |= static_cast<uint8_t>(REF_INDEX_BITWIDTH); |
||||
|
|
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
data += static_cast<uint8_t>(REFCLK_DIVISIONS_LOWER); |
||||
|
data += static_cast<uint8_t>(REFCLK_DIVISIONS_MIDDLE); |
||||
|
data += static_cast<uint8_t>(REFCLK_DIVISIONS_UPPER); |
||||
|
|
||||
|
byte = static_cast<uint8_t>(0b110U << 5U); |
||||
|
byte |= static_cast<uint8_t>(LVDS_TEST_PATTERN << 4U); |
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
byte = static_cast<uint8_t>(REFCLK_BY_XOSC << 8U); |
||||
|
byte |= static_cast<uint8_t>(0b1U << 7U); |
||||
|
byte |= static_cast<uint8_t>(LVDS_DATA_VALID_ADJUST << 4U); |
||||
|
byte |= static_cast<uint8_t>(0b0011U); |
||||
|
data += static_cast<char>(byte); |
||||
|
|
||||
|
constexpr uint8_t reg_default_08 = 0b10100001U; |
||||
|
constexpr uint8_t reg_default_09 = 0b00010011U; |
||||
|
constexpr uint8_t reg_default_10 = 0b00000000U; |
||||
|
constexpr uint8_t reg_default_11 = 0b00001010U; |
||||
|
constexpr uint8_t reg_default_12 = 0b11001100U; |
||||
|
constexpr uint8_t reg_default_13 = 0b11001100U; |
||||
|
constexpr uint8_t reg_default_14 = 0b11110001U; |
||||
|
constexpr uint8_t reg_default_15 = 0b01111101U; |
||||
|
|
||||
|
data += static_cast<char>(reg_default_08); |
||||
|
data += static_cast<char>(reg_default_09); |
||||
|
data += static_cast<char>(reg_default_10); |
||||
|
data += static_cast<char>(reg_default_11); |
||||
|
data += static_cast<char>(reg_default_12); |
||||
|
data += static_cast<char>(reg_default_13); |
||||
|
data += static_cast<char>(reg_default_14); |
||||
|
data += static_cast<char>(reg_default_15); |
||||
|
|
||||
|
byte = ((CMOS_INPUT) << 2U); |
||||
|
data += static_cast<char>(byte); |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
uint32_t Config::refclk_divisions(){ |
||||
|
uint32_t dat{}; |
||||
|
dat |= (static_cast<uint32_t>(REFCLK_DIVISIONS_UPPER) << 16U); |
||||
|
dat |= (static_cast<uint32_t>(REFCLK_DIVISIONS_MIDDLE) << 8U); |
||||
|
dat |= static_cast<uint32_t>(REFCLK_DIVISIONS_LOWER); |
||||
|
return dat; |
||||
|
} |
@ -0,0 +1,226 @@ |
|||||
|
#include "spidevices/gpx2/gpx2.h"
|
||||
|
#include "spidevices/gpx2/config.h"
|
||||
|
#include <iostream>
|
||||
|
#include <iomanip>
|
||||
|
#include <cmath>
|
||||
|
|
||||
|
using namespace SPI::GPX2_TDC; |
||||
|
|
||||
|
void GPX2::init(std::string busAddress, std::uint32_t speed, Mode mode, std::uint8_t bits) { |
||||
|
spiDevice::init(busAddress, speed, mode, bits); |
||||
|
power_on_reset(); |
||||
|
} |
||||
|
|
||||
|
void GPX2::power_on_reset() { |
||||
|
write(spiopc_power, ""); |
||||
|
} |
||||
|
|
||||
|
void GPX2::init_reset() { |
||||
|
write(spiopc_init, ""); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::write_config()->bool { |
||||
|
return write_config(config); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::write_config(const Config& data)->bool { |
||||
|
config = data; |
||||
|
return write_config(data.str()); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::write_config(const std::string& data)->bool { |
||||
|
if (data.size() != 17) { |
||||
|
std::cerr << "write all of the config only possible if 17 bytes of data provided\n"; |
||||
|
return false; |
||||
|
} |
||||
|
return write(spiopc_write_config, data); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::write_config(const std::uint8_t reg_addr, const std::uint8_t data)->bool { |
||||
|
if (reg_addr > 16) { |
||||
|
std::cerr << "write config is only possible on register addr. 0...16\n"; |
||||
|
return false; |
||||
|
} |
||||
|
std::string conf_str = std::string({ static_cast<char>(data) }); |
||||
|
return write(spiopc_write_config | reg_addr, conf_str); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::read_config()->std::string { |
||||
|
return read(spiopc_read_config, 17); |
||||
|
} |
||||
|
|
||||
|
auto GPX2::read_config(const std::uint8_t reg_addr)->std::uint8_t { |
||||
|
if (reg_addr > 16) { |
||||
|
std::cerr << "read config is only possible on register addr. 0...16\n"; |
||||
|
return 0; |
||||
|
} |
||||
|
std::string data = read(spiopc_read_config | reg_addr, 1); |
||||
|
if (data.empty()) { |
||||
|
return 0; |
||||
|
} |
||||
|
return data[0]; |
||||
|
} |
||||
|
|
||||
|
auto GPX2::read_results()->std::vector<Meas> { |
||||
|
std::string readout = read(spiopc_read_results | 0x08U, 24); |
||||
|
/*for (unsigned i = 0; i < 24; i++) {
|
||||
|
std::cout << std::setw(2) << std::dec << i << ": " << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned>(readout[i]) << std::endl; |
||||
|
}*/ |
||||
|
/*uint32_t test_ref_index = 0U;
|
||||
|
uint32_t test_stop_result = 0U; |
||||
|
test_ref_index = (static_cast<uint32_t>(readout[0]) << 16U); |
||||
|
test_ref_index |= (static_cast<uint32_t>(readout[1]) << 8U); |
||||
|
test_ref_index |= static_cast<uint32_t>(readout[2]); |
||||
|
test_stop_result = (static_cast<uint32_t>(readout[3]) << 16U); |
||||
|
test_stop_result |= (static_cast<uint32_t>(readout[4]) << 8U); |
||||
|
test_stop_result |= static_cast<uint32_t>(readout[5]); |
||||
|
std::cout << "first 6 byte: " <<std::dec<< test_ref_index << " " << test_stop_result << std::endl;;*/ |
||||
|
|
||||
|
std::vector<Meas> measurements{}; |
||||
|
if (readout.empty()) { |
||||
|
return measurements; |
||||
|
} |
||||
|
double lsb_ps = 1e12 / (config.refclk_divisions() * config.refclk_freq); |
||||
|
for (std::size_t i = 0; i < 4; i++) { |
||||
|
Meas meas; |
||||
|
meas.status = Meas::Valid; |
||||
|
meas.lsb_ps = lsb_ps; |
||||
|
meas.refclk_freq = config.refclk_freq; |
||||
|
|
||||
|
meas.ref_index = 0U; |
||||
|
meas.stop_result = 0U; |
||||
|
|
||||
|
meas.ref_index = (static_cast<uint32_t>(readout[i * 6U]) << 16U); |
||||
|
meas.ref_index |= (static_cast<uint32_t>(readout[i * 6U + 1U]) << 8U); |
||||
|
meas.ref_index |= static_cast<uint32_t>(readout[i * 6U + 2U]); |
||||
|
|
||||
|
meas.stop_result = (static_cast<uint32_t>(readout[i * 6U + 3U]) << 16U); |
||||
|
meas.stop_result |= (static_cast<uint32_t>(readout[i * 6U + 4U]) << 8U); |
||||
|
meas.stop_result |= static_cast<uint32_t>(readout[i * 6U + 5U]); |
||||
|
|
||||
|
if (meas.ref_index == 0xffffffU && meas.stop_result == 0xffffffU) { |
||||
|
meas.status = Meas::Invalid; |
||||
|
} |
||||
|
//std::cout << std::setw(2) << std::setfill('0') << std::dec << "meas_stop_" << i << ": ref_index=" << meas.ref_index << " stop_result=" << meas.stop_result << std::endl;
|
||||
|
//<< " refclk_freq=" <<meas.refclk_freq << " lsb_ps=" << meas.lsb_ps << std::endl;
|
||||
|
measurements.push_back(meas); |
||||
|
} |
||||
|
//std::cout << std::endl;
|
||||
|
return measurements; |
||||
|
} |
||||
|
|
||||
|
auto GPX2::get_filtered_intervals(double max_interval) -> std::vector<double>{ |
||||
|
//std::cout << "gpio " << event.pin << " falling edge" << std::endl;
|
||||
|
std::vector<double> diffs{}; |
||||
|
std::vector<std::vector<SPI::GPX2_TDC::Meas> > readout; |
||||
|
for (int i = 0; i < 4; i++) { |
||||
|
readout.push_back(read_results()); |
||||
|
if (readout.size() < 4) { |
||||
|
std::cerr << "readout vector has less than 4 channels"; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
for (int i = 0; i < 4; i++) { |
||||
|
for (int j = 0; j < 4; j++) { |
||||
|
double some_readout = 0; |
||||
|
SPI::GPX2_TDC::Meas val0; |
||||
|
SPI::GPX2_TDC::Meas val1; |
||||
|
val0 = readout[i][j]; |
||||
|
val1 = readout[i][(j + 1) % 4]; |
||||
|
if (val0.status == SPI::GPX2_TDC::Meas::Invalid || val1.status == SPI::GPX2_TDC::Meas::Invalid) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
some_readout = diff(val0, val1); // combinations are possible for 1+2, 2+3, 3+4, 1
|
||||
|
|
||||
|
//std::cout << "first loop: i=" << i << " j="<< j << " " << some_readout << std::endl;
|
||||
|
|
||||
|
if (j % 2 == 1) { |
||||
|
some_readout = -some_readout; |
||||
|
} |
||||
|
if (fabs(some_readout) < max_interval) { |
||||
|
diffs.push_back(some_readout); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
for (int i = 0; i < 3; i++) { |
||||
|
for (int j = 0; j < 4; j++) { |
||||
|
double some_readout = 0; |
||||
|
SPI::GPX2_TDC::Meas val0; |
||||
|
SPI::GPX2_TDC::Meas val1; |
||||
|
val0 = readout[i][j]; |
||||
|
val1 = readout[i + 1][(j + 1) % 4]; |
||||
|
if (val0.status == SPI::GPX2_TDC::Meas::Invalid || val1.status == SPI::GPX2_TDC::Meas::Invalid) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
some_readout = diff(val0, val1); // combinations are possible for 1+2, 2+3, 3+4, 1+4
|
||||
|
//std::cout << "second loop: i=" << i << "j=" << j << " " << some_readout << std::endl;
|
||||
|
|
||||
|
if (j % 2 == 1) { |
||||
|
some_readout = -some_readout; |
||||
|
} |
||||
|
if (fabs(some_readout) < max_interval) { |
||||
|
diffs.push_back(some_readout); |
||||
|
} |
||||
|
|
||||
|
val0 = readout[i+1][j]; |
||||
|
val1 = readout[i][(j + 1) % 4]; |
||||
|
some_readout = diff(readout[i + 1][j], readout[i][(j + 1) % 4]); // combinations are possible for 1+2, 2+3, 3+4, 1+4
|
||||
|
|
||||
|
//std::cout << "second loop: i=" << i << "j=" << j << " " << some_readout << std::endl;
|
||||
|
|
||||
|
if (j % 2 == 1) { |
||||
|
some_readout = -some_readout; |
||||
|
} |
||||
|
|
||||
|
if (fabs(some_readout) < max_interval) { |
||||
|
diffs.push_back(some_readout); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
//std::cout << std::endl;
|
||||
|
return diffs; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
Meas::operator bool() |
||||
|
{ |
||||
|
return status != Invalid; |
||||
|
} |
||||
|
bool Meas::operator < (const Meas& other) const |
||||
|
{ |
||||
|
if (ref_index < other.ref_index) { |
||||
|
return true; |
||||
|
} |
||||
|
if (ref_index == other.ref_index) { |
||||
|
return stop_result < other.stop_result; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
bool Meas::operator > (const Meas& other) const |
||||
|
{ |
||||
|
if (ref_index > other.ref_index) { |
||||
|
return true; |
||||
|
} |
||||
|
if (ref_index == other.ref_index) { |
||||
|
return stop_result > other.stop_result; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
auto Meas::operator == (const Meas& other) const -> bool |
||||
|
{ |
||||
|
return (ref_index == other.ref_index && stop_result == other.stop_result); |
||||
|
} |
||||
|
auto Meas::operator != (const Meas& other) const -> bool |
||||
|
{ |
||||
|
return !(*this == other); |
||||
|
} |
||||
|
auto Meas::operator >= (const Meas& other) const -> bool |
||||
|
{ |
||||
|
return (*this == other) || (*this > other); |
||||
|
} |
||||
|
auto Meas::operator <= (const Meas& other) const -> bool |
||||
|
{ |
||||
|
return (*this == other) || (*this < other); |
||||
|
} |
@ -0,0 +1,184 @@ |
|||||
|
#include "spidevices/spidevice.h"
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
#include <algorithm>
|
||||
|
#include <iostream>
|
||||
|
#include <memory>
|
||||
|
#include <unistd.h>
|
||||
|
#include <fcntl.h>
|
||||
|
#include <sys/ioctl.h>
|
||||
|
#include <linux/types.h>
|
||||
|
#include <linux/spi/spidev.h>
|
||||
|
|
||||
|
using namespace SPI; |
||||
|
|
||||
|
unsigned int spiDevice::fNrDevices{ 0 }; |
||||
|
unsigned long int spiDevice::fGlobalNrBytesWritten{ 0 }; |
||||
|
unsigned long int spiDevice::fGlobalNrBytesRead{ 0 }; |
||||
|
std::vector<spiDevice*> spiDevice::fGlobalDeviceList; |
||||
|
|
||||
|
spiDevice::~spiDevice() { |
||||
|
if (fHandle > 0) { |
||||
|
fNrDevices--; |
||||
|
} |
||||
|
close(fHandle); |
||||
|
std::vector<spiDevice*>::iterator it; |
||||
|
it = std::find(fGlobalDeviceList.begin(), fGlobalDeviceList.end(), this); |
||||
|
if (it != fGlobalDeviceList.end()) { |
||||
|
fGlobalDeviceList.erase(it); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
auto spiDevice::init(std::string busAddress, std::uint32_t speed, Mode mode, uint8_t bits)->bool { |
||||
|
fNrBits = bits; |
||||
|
fSpeed = speed; |
||||
|
|
||||
|
// enum class spi_mode -> csyle spi_mode
|
||||
|
switch (mode) { |
||||
|
case Mode::spi_mode_0: |
||||
|
fMode = SPI_MODE_0; |
||||
|
break; |
||||
|
case Mode::spi_mode_1: |
||||
|
fMode = SPI_MODE_1; |
||||
|
break; |
||||
|
case Mode::spi_mode_2: |
||||
|
fMode = SPI_MODE_2; |
||||
|
break; |
||||
|
case Mode::spi_mode_3: |
||||
|
fMode = SPI_MODE_3; |
||||
|
break; |
||||
|
default: |
||||
|
fMode = SPI_MODE_0; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
fHandle = open(busAddress.c_str(), O_RDWR); |
||||
|
if (fHandle < 0) { |
||||
|
std::cerr << "Could not open spi device" << std::endl; |
||||
|
return false; |
||||
|
} |
||||
|
fNrDevices++; |
||||
|
fGlobalDeviceList.push_back(this); |
||||
|
|
||||
|
/*
|
||||
|
* spi mode |
||||
|
*/ |
||||
|
int ret{}; |
||||
|
ret = ioctl(fHandle, SPI_IOC_WR_MODE, &fMode); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't set spi mode" << std::endl; |
||||
|
return false; |
||||
|
} |
||||
|
ret = ioctl(fHandle, SPI_IOC_RD_MODE, &fMode); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't get spi mode" << std::endl; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* bits per word |
||||
|
*/ |
||||
|
ret = ioctl(fHandle, SPI_IOC_WR_BITS_PER_WORD, &bits); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't set bits per word" << std::endl; |
||||
|
} |
||||
|
|
||||
|
ret = ioctl(fHandle, SPI_IOC_RD_BITS_PER_WORD, &bits); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't get bits per word" << std::endl; |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/*
|
||||
|
* max speed hz |
||||
|
*/ |
||||
|
ret = ioctl(fHandle, SPI_IOC_WR_MAX_SPEED_HZ, &fSpeed); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't set max speed hz" << std::endl; |
||||
|
} |
||||
|
|
||||
|
ret = ioctl(fHandle, SPI_IOC_RD_MAX_SPEED_HZ, &fSpeed); |
||||
|
if (ret == -1) { |
||||
|
std::cerr << "can't get max speed hz" << std::endl; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
auto spiDevice::devicePresent()->bool { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
auto spiDevice::write(const std::uint8_t command, const std::string& data)->bool { |
||||
|
if (fHandle==-1) { |
||||
|
std::cerr << "tried to write to spi without initialising (calling init(..) first)." << std::endl; |
||||
|
return false; |
||||
|
} |
||||
|
const std::size_t n = data.size() + 1; |
||||
|
|
||||
|
auto txBuf{ std::make_unique<std::uint8_t[]>(n) }; |
||||
|
auto rxBuf{ std::make_unique<std::uint8_t[]>(n) }; |
||||
|
txBuf[0] = command; |
||||
|
for (std::size_t i = 1; i < n; i++) { |
||||
|
txBuf[i] = data[i - 1]; |
||||
|
} |
||||
|
|
||||
|
auto status = spi_xfer(fHandle, fSpeed, fMode, fNrBits, txBuf.get(), rxBuf.get(), n); |
||||
|
if (status != static_cast<decltype(status)>(n)) { |
||||
|
std::cerr << "transfer size mismatch: spi_xfer returned " << status << " bytes transfered but should write" << n << " bytes." << std::endl; |
||||
|
return false; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
auto spiDevice::read(const std::uint8_t command, const std::size_t nBytes)->std::string { |
||||
|
if (fHandle == -1) { |
||||
|
std::cerr << "tried to read from spi without initialising." << std::endl; |
||||
|
return ""; |
||||
|
} |
||||
|
const std::size_t n = nBytes + 1; |
||||
|
|
||||
|
auto txBuf{ std::make_unique<std::uint8_t[]>(n) }; |
||||
|
auto rxBuf{ std::make_unique<std::uint8_t[]>(n) }; |
||||
|
txBuf[0] = command; |
||||
|
|
||||
|
auto status = spi_xfer(fHandle, fSpeed, fMode, fNrBits, txBuf.get(), rxBuf.get(), n); |
||||
|
if (status != static_cast<decltype(status)>(n)) { |
||||
|
std::cerr << "transfer size mismatch: spi_xfer returned " << status <<" bytes but should read" << n <<" bytes." << std::endl; |
||||
|
return ""; |
||||
|
} |
||||
|
std::string data; |
||||
|
for (std::size_t i = 1; i < n; i++) { |
||||
|
data += rxBuf[i]; |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
auto spiDevice::spi_xfer(const int handle, const uint32_t speed, const uint8_t mode, const uint8_t bits, const uint8_t* tx, uint8_t* rx, uint32_t nBytes)->int { |
||||
|
int ret{}; |
||||
|
uint16_t delay{}; |
||||
|
spi_ioc_transfer tr = { |
||||
|
(unsigned long)tx, // tx_buf
|
||||
|
(unsigned long)rx, // rx_buf
|
||||
|
nBytes, // len
|
||||
|
speed, // speed_hz
|
||||
|
delay, // delay_usecs
|
||||
|
bits, // bits_per_word
|
||||
|
0, // cs_change
|
||||
|
bits, // rx_nbits
|
||||
|
bits, // tx_nbits
|
||||
|
mode // mode
|
||||
|
//0 // cs
|
||||
|
|
||||
|
/**
|
||||
|
* Note: |
||||
|
* Depending on spidev version there are different parameters in struct |
||||
|
*/ |
||||
|
}; |
||||
|
|
||||
|
ret = ioctl(handle, SPI_IOC_MESSAGE(1), &tr); |
||||
|
if (ret < 1) { |
||||
|
std::cerr << "can't send spi message" << std::endl; |
||||
|
} |
||||
|
return ret; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue