├── .gitignore ├── CMakeLists.txt ├── Kconfig.uwb ├── LICENSE ├── README.md ├── VERSION ├── drivers ├── CMakeLists.txt ├── Kconfig ├── battery │ ├── CMakeLists.txt │ ├── Kconfig │ └── battery.c ├── dw1000 │ ├── CMakeLists.txt │ ├── Kconfig │ ├── decadriver │ │ ├── CMakeLists.txt │ │ ├── deca_device.c │ │ └── deca_params_init.c │ └── platform │ │ ├── CMakeLists.txt │ │ ├── deca_mutex.c │ │ ├── deca_range_tables.c │ │ ├── deca_sleep.c │ │ ├── deca_spi.c │ │ └── port.c └── sensor │ ├── CMakeLists.txt │ ├── Kconfig │ ├── ms8607 │ ├── CMakeLists.txt │ ├── Kconfig │ ├── ms8607.c │ ├── ms8607_i2c.c │ └── ms8607_i2c.h │ └── veml6030 │ ├── CMakeLists.txt │ ├── Kconfig │ └── veml6030.c ├── include ├── drivers │ └── dw1000 │ │ ├── compiler.h │ │ ├── deca_device_api.h │ │ ├── deca_param_types.h │ │ ├── deca_regs.h │ │ ├── deca_spi.h │ │ ├── deca_types.h │ │ ├── deca_version.h │ │ └── port.h ├── json.hpp └── meshposition.h ├── media └── forum.png ├── samples ├── 01_hello_leds │ ├── .gitignore │ ├── CMakeLists.txt │ ├── prj.conf │ ├── readme.md │ ├── sample.yaml │ └── src │ │ └── main.c ├── 02_listener │ ├── CMakeLists.txt │ ├── README.md │ ├── prj.conf │ └── src │ │ └── main.c ├── 03_twr_initiator │ ├── CMakeLists.txt │ ├── README.md │ ├── overlay-debug.conf │ ├── overlay-tracing.conf │ ├── overlay-usb.conf │ ├── prj.conf │ └── src │ │ └── main.cpp ├── 04_twr_responder │ ├── CMakeLists.txt │ ├── README.md │ ├── captures │ │ ├── 1 MHz, 5 M Samples [10].logicdata │ │ ├── 1 MHz, 5 M Samples [13].logicdata │ │ ├── SFD timeout.logicdata │ │ ├── default-.logicdata │ │ ├── initiator.logicdata │ │ ├── optimized delay 1000-1000.logicdata │ │ ├── reduced to 1000 us ok.logicdata │ │ ├── sfd errors.logicdata │ │ └── twr init-resp.logicdata │ ├── logs │ │ ├── bad_init_alone.txt │ │ ├── bad_init_alone_2.txt │ │ ├── bad_init_alone_3.txt │ │ ├── badfix_init_alone.txt │ │ ├── good_init_alone.txt │ │ ├── good_init_reg.txt │ │ └── good_resp_reg.txt │ ├── overlay-debug.conf │ ├── overlay-tracing.conf │ ├── overlay-usb.conf │ ├── prj.conf │ └── src │ │ └── main.cpp ├── 05_listener_sm │ ├── .gitignore │ ├── CMakeLists.txt │ ├── overlay-debug.conf │ ├── overlay-tracing.conf │ ├── overlay-usb.conf │ ├── prj.conf │ ├── readme.md │ ├── sample.yaml │ └── src │ │ └── main.cpp ├── 06_uwb_node_sm │ ├── .gitignore │ ├── CMakeLists.txt │ ├── overlay-debug.conf │ ├── overlay-tracing.conf │ ├── overlay-usb.conf │ ├── prj.conf │ ├── readme.md │ ├── sample.yaml │ └── src │ │ └── main.cpp ├── CMakeLists.txt └── Kconfig ├── subsys ├── CMakeLists.txt ├── Kconfig └── meshposition │ ├── CMakeLists.txt │ ├── Kconfig │ ├── mesh_twr.cpp │ └── meshposition.cpp ├── west.yml └── zephyr └── module.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.d 3 | *.cmd 4 | *.log 5 | *.pyc 6 | *.swp 7 | *.swo 8 | *~ 9 | build 10 | build-* 11 | build_* 12 | cscope.* 13 | .dir 14 | 15 | /*.patch 16 | 17 | tags 18 | .project 19 | .cproject 20 | .xxproject 21 | .envrc 22 | .vscode 23 | sanity-out* 24 | twister-out* 25 | 26 | doc/_build 27 | doc/nrf/_doxygen/ 28 | doc/nrf/_samples/ 29 | doc/*_warnings.txt 30 | doc/*_doc.log 31 | 32 | ext/lib/bin/supl 33 | ext/lib/bin/libsupl.zip 34 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Point to root directory. 2 | set(HSM_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "root directory") 3 | 4 | zephyr_include_directories(include) 5 | 6 | add_subdirectory(samples) 7 | add_subdirectory(drivers) 8 | add_subdirectory(subsys) 9 | -------------------------------------------------------------------------------- /Kconfig.uwb: -------------------------------------------------------------------------------- 1 | 2 | menu "Home Smart Mesh" 3 | 4 | rsource "samples/Kconfig" 5 | rsource "drivers/Kconfig" 6 | rsource "subsys/Kconfig" 7 | 8 | endmenu 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sdk-uwb-zephyr 2 | Zephyr sdk for uwb module DWM1001C based on nRF52832 and DW1000 Transceiver 3 | # discussions 4 | get support, give feedback or simply chat to brainstorm about ideas on the forum 5 | 6 | https://github.com/nRFMesh/sdk-uwb-zephyr/discussions 7 | # Framework doc 8 | https://www.homesmartmesh.com/docs/networks/ultrawideband/ 9 | 10 | # usage 11 | ```bash 12 | west init -m https://github.com/nRFMesh/sdk-uwb-zephyr --mr main 13 | ``` 14 | 15 | # takeover 16 | driver zephyr port from https://github.com/foldedtoad/dwm1001.git 17 | 18 | * `uwb\drivers\dw1000\decadriver` 19 | * `uwb\drivers\dw1000\platform` 20 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.0.0 2 | -------------------------------------------------------------------------------- /drivers/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory_ifdef(CONFIG_SENSOR sensor) 2 | add_subdirectory_ifdef(CONFIG_BATTERY battery) 3 | add_subdirectory_ifdef(CONFIG_DW1000 dw1000) 4 | -------------------------------------------------------------------------------- /drivers/Kconfig: -------------------------------------------------------------------------------- 1 | menu "Device Drivers" 2 | 3 | rsource "sensor/Kconfig" 4 | 5 | rsource "battery/Kconfig" 6 | 7 | rsource "dw1000/Kconfig" 8 | 9 | endmenu 10 | -------------------------------------------------------------------------------- /drivers/battery/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: Apache-2.0 2 | 3 | zephyr_library() 4 | 5 | zephyr_library_sources_ifdef(CONFIG_BATTERY battery.c) 6 | -------------------------------------------------------------------------------- /drivers/battery/Kconfig: -------------------------------------------------------------------------------- 1 | config BATTERY 2 | bool "BATTERY ADC VDD" 3 | depends on ADC 4 | help 5 | Enable driver for measuring Battery through ADC VDD Channel. 6 | -------------------------------------------------------------------------------- /drivers/battery/battery.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | LOG_MODULE_REGISTER(battery, LOG_LEVEL_INF); 9 | 10 | #define ADC_CHANNEL 0 11 | #define ADC_RESOLUTION 12 12 | #define ADC_GAIN ADC_GAIN_1_6 13 | #define ADC_REFERENCE ADC_REF_INTERNAL 14 | #define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS,40) 15 | 16 | const struct device *dev_adc; 17 | static int16_t sample; 18 | int32_t adc_vref; 19 | struct adc_channel_cfg channel_cfg = { 20 | .gain = ADC_GAIN, 21 | .reference = ADC_REFERENCE, 22 | .acquisition_time = ADC_ACQUISITION_TIME, 23 | .channel_id = ADC_CHANNEL, 24 | #ifdef CONFIG_ADC_CONFIGURABLE_INPUTS 25 | .differential = 0, 26 | .input_positive = SAADC_CH_PSELN_PSELN_VDD 27 | #else 28 | .differential = 0 29 | #endif 30 | }; 31 | 32 | struct adc_sequence sequence = { 33 | .channels = BIT(ADC_CHANNEL), 34 | .buffer = &sample, 35 | /* buffer size in bytes, not number of samples */ 36 | .buffer_size = sizeof(sample), 37 | .resolution = ADC_RESOLUTION, 38 | }; 39 | 40 | void battery_init() 41 | { 42 | dev_adc = device_get_binding(DT_LABEL(DT_NODELABEL(adc))); 43 | if (!device_is_ready(dev_adc)) { 44 | LOG_ERR("ADC device not found\n"); 45 | return; 46 | } 47 | 48 | adc_channel_setup(dev_adc, &channel_cfg); 49 | adc_vref = adc_ref_internal(dev_adc); 50 | 51 | LOG_INF("battery_init() vref = %d",adc_vref); 52 | } 53 | 54 | void battery_start() 55 | { 56 | int err = adc_read(dev_adc, &sequence); 57 | if (err != 0) { 58 | LOG_ERR("ADC reading failed with error %d.\n", err); 59 | return; 60 | }else{ 61 | LOG_INF("battery_start> adc_read()"); 62 | } 63 | } 64 | 65 | int32_t battery_get_mv() 66 | { 67 | int32_t raw_value = sample; 68 | LOG_INF("battery_get_mv() raw = %d",raw_value); 69 | int32_t mv_value = raw_value; 70 | adc_raw_to_millivolts(adc_vref, ADC_GAIN,ADC_RESOLUTION, &mv_value); 71 | return mv_value; 72 | } 73 | -------------------------------------------------------------------------------- /drivers/dw1000/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | add_subdirectory(decadriver) 3 | add_subdirectory(platform) 4 | -------------------------------------------------------------------------------- /drivers/dw1000/Kconfig: -------------------------------------------------------------------------------- 1 | menuconfig DW1000 2 | bool "DW1000 Tranceiver" 3 | help 4 | Include DW1000 Tranceiver drivers in config 5 | -------------------------------------------------------------------------------- /drivers/dw1000/decadriver/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: Apache-2.0 2 | 3 | zephyr_library() 4 | 5 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_device.c) 6 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_params_init.c) 7 | -------------------------------------------------------------------------------- /drivers/dw1000/decadriver/deca_params_init.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_params_init.c 3 | * @brief DW1000 configuration parameters 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * 12 | * ------------------------------------------------------------------------------------------------------------------- 13 | **/ 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | 22 | //----------------------------------------- 23 | // map the channel number to the index in the configuration arrays below 24 | // 0th element is chan 1, 1st is chan 2, 2nd is chan 3, 3rd is chan 4, 4th is chan 5, 5th is chan 7 25 | const uint8 chan_idx[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 3, 4, 0, 5}; 26 | 27 | //----------------------------------------- 28 | const uint32 tx_config[NUM_CH] = 29 | { 30 | RF_TXCTRL_CH1, 31 | RF_TXCTRL_CH2, 32 | RF_TXCTRL_CH3, 33 | RF_TXCTRL_CH4, 34 | RF_TXCTRL_CH5, 35 | RF_TXCTRL_CH7, 36 | }; 37 | 38 | //Frequency Synthesiser - PLL configuration 39 | const uint32 fs_pll_cfg[NUM_CH] = 40 | { 41 | FS_PLLCFG_CH1, 42 | FS_PLLCFG_CH2, 43 | FS_PLLCFG_CH3, 44 | FS_PLLCFG_CH4, 45 | FS_PLLCFG_CH5, 46 | FS_PLLCFG_CH7 47 | }; 48 | 49 | //Frequency Synthesiser - PLL tuning 50 | const uint8 fs_pll_tune[NUM_CH] = 51 | { 52 | FS_PLLTUNE_CH1, 53 | FS_PLLTUNE_CH2, 54 | FS_PLLTUNE_CH3, 55 | FS_PLLTUNE_CH4, 56 | FS_PLLTUNE_CH5, 57 | FS_PLLTUNE_CH7 58 | }; 59 | 60 | //bandwidth configuration 61 | const uint8 rx_config[NUM_BW] = 62 | { 63 | RF_RXCTRLH_NBW, 64 | RF_RXCTRLH_WBW 65 | }; 66 | 67 | 68 | const agc_cfg_struct agc_config = 69 | { 70 | AGC_TUNE2_VAL, 71 | { AGC_TUNE1_16M , AGC_TUNE1_64M } //adc target 72 | }; 73 | 74 | //DW non-standard SFD length for 110k, 850k and 6.81M 75 | const uint8 dwnsSFDlen[NUM_BR] = 76 | { 77 | DW_NS_SFD_LEN_110K, 78 | DW_NS_SFD_LEN_850K, 79 | DW_NS_SFD_LEN_6M8 80 | }; 81 | 82 | // SFD Threshold 83 | const uint16 sftsh[NUM_BR][NUM_SFD] = 84 | { 85 | { 86 | DRX_TUNE0b_110K_STD, 87 | DRX_TUNE0b_110K_NSTD 88 | }, 89 | { 90 | DRX_TUNE0b_850K_STD, 91 | DRX_TUNE0b_850K_NSTD 92 | }, 93 | { 94 | DRX_TUNE0b_6M8_STD, 95 | DRX_TUNE0b_6M8_NSTD 96 | } 97 | }; 98 | 99 | const uint16 dtune1[NUM_PRF] = 100 | { 101 | DRX_TUNE1a_PRF16, 102 | DRX_TUNE1a_PRF64 103 | }; 104 | 105 | const uint32 digital_bb_config[NUM_PRF][NUM_PACS] = 106 | { 107 | { 108 | DRX_TUNE2_PRF16_PAC8, 109 | DRX_TUNE2_PRF16_PAC16, 110 | DRX_TUNE2_PRF16_PAC32, 111 | DRX_TUNE2_PRF16_PAC64 112 | }, 113 | { 114 | DRX_TUNE2_PRF64_PAC8, 115 | DRX_TUNE2_PRF64_PAC16, 116 | DRX_TUNE2_PRF64_PAC32, 117 | DRX_TUNE2_PRF64_PAC64 118 | } 119 | }; 120 | 121 | const uint16 lde_replicaCoeff[PCODES] = 122 | { 123 | 0, // No preamble code 0 124 | LDE_REPC_PCODE_1, 125 | LDE_REPC_PCODE_2, 126 | LDE_REPC_PCODE_3, 127 | LDE_REPC_PCODE_4, 128 | LDE_REPC_PCODE_5, 129 | LDE_REPC_PCODE_6, 130 | LDE_REPC_PCODE_7, 131 | LDE_REPC_PCODE_8, 132 | LDE_REPC_PCODE_9, 133 | LDE_REPC_PCODE_10, 134 | LDE_REPC_PCODE_11, 135 | LDE_REPC_PCODE_12, 136 | LDE_REPC_PCODE_13, 137 | LDE_REPC_PCODE_14, 138 | LDE_REPC_PCODE_15, 139 | LDE_REPC_PCODE_16, 140 | LDE_REPC_PCODE_17, 141 | LDE_REPC_PCODE_18, 142 | LDE_REPC_PCODE_19, 143 | LDE_REPC_PCODE_20, 144 | LDE_REPC_PCODE_21, 145 | LDE_REPC_PCODE_22, 146 | LDE_REPC_PCODE_23, 147 | LDE_REPC_PCODE_24 148 | }; 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: Apache-2.0 2 | 3 | zephyr_library() 4 | 5 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_mutex.c) 6 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_range_tables.c) 7 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_sleep.c) 8 | zephyr_library_sources_ifdef(CONFIG_DW1000 deca_spi.c) 9 | zephyr_library_sources_ifdef(CONFIG_DW1000 port.c) 10 | 11 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/deca_mutex.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_mutex.c 3 | * @brief IRQ interface / mutex implementation 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #include 14 | #include 15 | // --------------------------------------------------------------------------- 16 | // 17 | // NB: The purpose of this file is to provide for microprocessor interrupt enable/disable, this is used for 18 | // controlling mutual exclusion from critical sections in the code where interrupts and background 19 | // processing may interact. The code using this is kept to a minimum and the disabling time is also 20 | // kept to a minimum, so blanket interrupt disable may be the easiest way to provide this. But at a 21 | // minimum those interrupts coming from the decawave device should be disabled/re-enabled by this activity. 22 | // 23 | // In porting this to a particular microprocessor, the implementer may choose to use #defines in the 24 | // deca_irq.h include file to map these calls transparently to the target system. Alternatively the 25 | // appropriate code may be embedded in the functions provided below. 26 | // 27 | // This mutex dependent on HW port. 28 | // If HW port uses EXT_IRQ line to receive ready/busy status from DW1000 then mutex should use this signal 29 | // If HW port not use EXT_IRQ line (i.e. SW polling) then no necessary for decamutex(on/off) 30 | // 31 | // For critical section use this mutex instead 32 | // __save_intstate() 33 | // __restore_intstate() 34 | // --------------------------------------------------------------------------- 35 | 36 | 37 | /*! ------------------------------------------------------------------------------------------------------------------ 38 | * Function: decamutexon() 39 | * 40 | * Description: This function should disable interrupts. This is called at the start of a critical section 41 | * It returns the irq state before disable, this value is used to re-enable in decamutexoff call 42 | * 43 | * Note: The body of this function is defined in deca_mutex.c and is platform specific 44 | * 45 | * input parameters: 46 | * 47 | * output parameters 48 | * 49 | * returns the state of the DW1000 interrupt 50 | */ 51 | decaIrqStatus_t decamutexon(void) 52 | { 53 | //TODO 54 | return 0 ; // return state before disable, value is used to re-enable in decamutexoff call 55 | } 56 | 57 | /*! ------------------------------------------------------------------------------------------------------------------ 58 | * Function: decamutexoff() 59 | * 60 | * Description: This function should re-enable interrupts, or at least restore their state as returned(&saved) by decamutexon 61 | * This is called at the end of a critical section 62 | * 63 | * Note: The body of this function is defined in deca_mutex.c and is platform specific 64 | * 65 | * input parameters: 66 | * @param s - the state of the DW1000 interrupt as returned by decamutexon 67 | * 68 | * output parameters 69 | * 70 | * returns the state of the DW1000 interrupt 71 | */ 72 | void decamutexoff(decaIrqStatus_t s) // put a function here that re-enables the interrupt at the end of the critical section 73 | { 74 | //TODO 75 | } 76 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/deca_range_tables.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_range_tables.c 3 | * @brief DW1000 range correction tables 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | 18 | #define NUM_16M_OFFSET (37) 19 | #define NUM_16M_OFFSETWB (68) 20 | #define NUM_64M_OFFSET (26) 21 | #define NUM_64M_OFFSETWB (59) 22 | 23 | const uint8 chan_idxnb[NUM_CH_SUPPORTED] = {0, 0, 1, 2, 0, 3, 0, 0}; //only channels 1,2,3 and 5 are in the narrow band tables 24 | const uint8 chan_idxwb[NUM_CH_SUPPORTED] = {0, 0, 0, 0, 0, 0, 0, 1}; //only channels 4 and 7 are in in the wide band tables 25 | 26 | //--------------------------------------------------------------------------------------------------------------------------- 27 | // Range Bias Correction TABLES of range values in integer units of 25 CM, for 8-bit unsigned storage, MUST END IN 255 !!!!!! 28 | //--------------------------------------------------------------------------------------------------------------------------- 29 | 30 | // offsets to nearest centimeter for index 0, all rest are +1 cm per value 31 | 32 | #define CM_OFFSET_16M_NB (-23) // for normal band channels at 16 MHz PRF 33 | #define CM_OFFSET_16M_WB (-28) // for wider band channels at 16 MHz PRF 34 | #define CM_OFFSET_64M_NB (-17) // for normal band channels at 64 MHz PRF 35 | #define CM_OFFSET_64M_WB (-30) // for wider band channels at 64 MHz PRF 36 | 37 | 38 | //--------------------------------------------------------------------------------------------------------------------------- 39 | // range25cm16PRFnb: Range Bias Correction table for narrow band channels at 16 MHz PRF, NB: !!!! each MUST END IN 255 !!!! 40 | //--------------------------------------------------------------------------------------------------------------------------- 41 | 42 | const uint8 range25cm16PRFnb[4][NUM_16M_OFFSET] = 43 | { 44 | // ch 1 - range25cm16PRFnb 45 | { 46 | 1, 47 | 3, 48 | 4, 49 | 5, 50 | 7, 51 | 9, 52 | 11, 53 | 12, 54 | 13, 55 | 15, 56 | 18, 57 | 20, 58 | 23, 59 | 25, 60 | 28, 61 | 30, 62 | 33, 63 | 36, 64 | 40, 65 | 43, 66 | 47, 67 | 50, 68 | 54, 69 | 58, 70 | 63, 71 | 66, 72 | 71, 73 | 76, 74 | 82, 75 | 89, 76 | 98, 77 | 109, 78 | 127, 79 | 155, 80 | 222, 81 | 255, 82 | 255 83 | }, 84 | 85 | // ch 2 - range25cm16PRFnb 86 | { 87 | 1, 88 | 2, 89 | 4, 90 | 5, 91 | 6, 92 | 8, 93 | 9, 94 | 10, 95 | 12, 96 | 13, 97 | 15, 98 | 18, 99 | 20, 100 | 22, 101 | 24, 102 | 27, 103 | 29, 104 | 32, 105 | 35, 106 | 38, 107 | 41, 108 | 44, 109 | 47, 110 | 51, 111 | 55, 112 | 58, 113 | 62, 114 | 66, 115 | 71, 116 | 78, 117 | 85, 118 | 96, 119 | 111, 120 | 135, 121 | 194, 122 | 240, 123 | 255 124 | }, 125 | 126 | // ch 3 - range25cm16PRFnb 127 | { 128 | 1, 129 | 2, 130 | 3, 131 | 4, 132 | 5, 133 | 7, 134 | 8, 135 | 9, 136 | 10, 137 | 12, 138 | 14, 139 | 16, 140 | 18, 141 | 20, 142 | 22, 143 | 24, 144 | 26, 145 | 28, 146 | 31, 147 | 33, 148 | 36, 149 | 39, 150 | 42, 151 | 45, 152 | 49, 153 | 52, 154 | 55, 155 | 59, 156 | 63, 157 | 69, 158 | 76, 159 | 85, 160 | 98, 161 | 120, 162 | 173, 163 | 213, 164 | 255 165 | }, 166 | 167 | // ch 5 - range25cm16PRFnb 168 | { 169 | 1, 170 | 1, 171 | 2, 172 | 3, 173 | 4, 174 | 5, 175 | 6, 176 | 6, 177 | 7, 178 | 8, 179 | 9, 180 | 11, 181 | 12, 182 | 14, 183 | 15, 184 | 16, 185 | 18, 186 | 20, 187 | 21, 188 | 23, 189 | 25, 190 | 27, 191 | 29, 192 | 31, 193 | 34, 194 | 36, 195 | 38, 196 | 41, 197 | 44, 198 | 48, 199 | 53, 200 | 59, 201 | 68, 202 | 83, 203 | 120, 204 | 148, 205 | 255 206 | } 207 | }; // end range25cm16PRFnb 208 | 209 | 210 | //--------------------------------------------------------------------------------------------------------------------------- 211 | // range25cm16PRFwb: Range Bias Correction table for wide band channels at 16 MHz PRF, NB: !!!! each MUST END IN 255 !!!! 212 | //--------------------------------------------------------------------------------------------------------------------------- 213 | 214 | const uint8 range25cm16PRFwb[2][NUM_16M_OFFSETWB] = 215 | { 216 | // ch 4 - range25cm16PRFwb 217 | { 218 | 7, 219 | 7, 220 | 8, 221 | 9, 222 | 9, 223 | 10, 224 | 11, 225 | 11, 226 | 12, 227 | 13, 228 | 14, 229 | 15, 230 | 16, 231 | 17, 232 | 18, 233 | 19, 234 | 20, 235 | 21, 236 | 22, 237 | 23, 238 | 24, 239 | 26, 240 | 27, 241 | 28, 242 | 30, 243 | 31, 244 | 32, 245 | 34, 246 | 36, 247 | 38, 248 | 40, 249 | 42, 250 | 44, 251 | 46, 252 | 48, 253 | 50, 254 | 52, 255 | 55, 256 | 57, 257 | 59, 258 | 61, 259 | 63, 260 | 66, 261 | 68, 262 | 71, 263 | 74, 264 | 78, 265 | 81, 266 | 85, 267 | 89, 268 | 94, 269 | 99, 270 | 104, 271 | 110, 272 | 116, 273 | 123, 274 | 130, 275 | 139, 276 | 150, 277 | 164, 278 | 182, 279 | 207, 280 | 238, 281 | 255, 282 | 255, 283 | 255, 284 | 255, 285 | 255 286 | }, 287 | 288 | // ch 7 - range25cm16PRFwb 289 | { 290 | 4, 291 | 5, 292 | 5, 293 | 5, 294 | 6, 295 | 6, 296 | 7, 297 | 7, 298 | 7, 299 | 8, 300 | 9, 301 | 9, 302 | 10, 303 | 10, 304 | 11, 305 | 11, 306 | 12, 307 | 13, 308 | 13, 309 | 14, 310 | 15, 311 | 16, 312 | 17, 313 | 17, 314 | 18, 315 | 19, 316 | 20, 317 | 21, 318 | 22, 319 | 23, 320 | 25, 321 | 26, 322 | 27, 323 | 29, 324 | 30, 325 | 31, 326 | 32, 327 | 34, 328 | 35, 329 | 36, 330 | 38, 331 | 39, 332 | 40, 333 | 42, 334 | 44, 335 | 46, 336 | 48, 337 | 50, 338 | 52, 339 | 55, 340 | 58, 341 | 61, 342 | 64, 343 | 68, 344 | 72, 345 | 75, 346 | 80, 347 | 85, 348 | 92, 349 | 101, 350 | 112, 351 | 127, 352 | 147, 353 | 168, 354 | 182, 355 | 194, 356 | 205, 357 | 255 358 | } 359 | }; // end range25cm16PRFwb 360 | 361 | //--------------------------------------------------------------------------------------------------------------------------- 362 | // range25cm64PRFnb: Range Bias Correction table for narrow band channels at 64 MHz PRF, NB: !!!! each MUST END IN 255 !!!! 363 | //--------------------------------------------------------------------------------------------------------------------------- 364 | 365 | const uint8 range25cm64PRFnb[4][NUM_64M_OFFSET] = 366 | { 367 | // ch 1 - range25cm64PRFnb 368 | { 369 | 1, 370 | 2, 371 | 2, 372 | 3, 373 | 4, 374 | 5, 375 | 7, 376 | 10, 377 | 13, 378 | 16, 379 | 19, 380 | 22, 381 | 24, 382 | 27, 383 | 30, 384 | 32, 385 | 35, 386 | 38, 387 | 43, 388 | 48, 389 | 56, 390 | 78, 391 | 101, 392 | 120, 393 | 157, 394 | 255 395 | }, 396 | 397 | // ch 2 - range25cm64PRFnb 398 | { 399 | 1, 400 | 2, 401 | 2, 402 | 3, 403 | 4, 404 | 4, 405 | 6, 406 | 9, 407 | 12, 408 | 14, 409 | 17, 410 | 19, 411 | 21, 412 | 24, 413 | 26, 414 | 28, 415 | 31, 416 | 33, 417 | 37, 418 | 42, 419 | 49, 420 | 68, 421 | 89, 422 | 105, 423 | 138, 424 | 255 425 | }, 426 | 427 | // ch 3 - range25cm64PRFnb 428 | { 429 | 1, 430 | 1, 431 | 2, 432 | 3, 433 | 3, 434 | 4, 435 | 5, 436 | 8, 437 | 10, 438 | 13, 439 | 15, 440 | 17, 441 | 19, 442 | 21, 443 | 23, 444 | 25, 445 | 27, 446 | 30, 447 | 33, 448 | 37, 449 | 44, 450 | 60, 451 | 79, 452 | 93, 453 | 122, 454 | 255 455 | }, 456 | 457 | // ch 5 - range25cm64PRFnb 458 | { 459 | 1, 460 | 1, 461 | 1, 462 | 2, 463 | 2, 464 | 3, 465 | 4, 466 | 6, 467 | 7, 468 | 9, 469 | 10, 470 | 12, 471 | 13, 472 | 15, 473 | 16, 474 | 17, 475 | 19, 476 | 21, 477 | 23, 478 | 26, 479 | 30, 480 | 42, 481 | 55, 482 | 65, 483 | 85, 484 | 255 485 | } 486 | }; // end range25cm64PRFnb 487 | 488 | //--------------------------------------------------------------------------------------------------------------------------- 489 | // range25cm64PRFwb: Range Bias Correction table for wide band channels at 64 MHz PRF, NB: !!!! each MUST END IN 255 !!!! 490 | //--------------------------------------------------------------------------------------------------------------------------- 491 | 492 | const uint8 range25cm64PRFwb[2][NUM_64M_OFFSETWB] = 493 | { 494 | // ch 4 - range25cm64PRFwb 495 | { 496 | 7, 497 | 8, 498 | 8, 499 | 9, 500 | 9, 501 | 10, 502 | 11, 503 | 12, 504 | 13, 505 | 13, 506 | 14, 507 | 15, 508 | 16, 509 | 16, 510 | 17, 511 | 18, 512 | 19, 513 | 19, 514 | 20, 515 | 21, 516 | 22, 517 | 24, 518 | 25, 519 | 27, 520 | 28, 521 | 29, 522 | 30, 523 | 32, 524 | 33, 525 | 34, 526 | 35, 527 | 37, 528 | 39, 529 | 41, 530 | 43, 531 | 45, 532 | 48, 533 | 50, 534 | 53, 535 | 56, 536 | 60, 537 | 64, 538 | 68, 539 | 74, 540 | 81, 541 | 89, 542 | 98, 543 | 109, 544 | 122, 545 | 136, 546 | 146, 547 | 154, 548 | 162, 549 | 178, 550 | 220, 551 | 249, 552 | 255, 553 | 255, 554 | 255 555 | }, 556 | 557 | // ch 7 - range25cm64PRFwb 558 | { 559 | 4, 560 | 5, 561 | 5, 562 | 5, 563 | 6, 564 | 6, 565 | 7, 566 | 7, 567 | 8, 568 | 8, 569 | 9, 570 | 9, 571 | 10, 572 | 10, 573 | 10, 574 | 11, 575 | 11, 576 | 12, 577 | 13, 578 | 13, 579 | 14, 580 | 15, 581 | 16, 582 | 16, 583 | 17, 584 | 18, 585 | 19, 586 | 19, 587 | 20, 588 | 21, 589 | 22, 590 | 23, 591 | 24, 592 | 25, 593 | 26, 594 | 28, 595 | 29, 596 | 31, 597 | 33, 598 | 35, 599 | 37, 600 | 39, 601 | 42, 602 | 46, 603 | 50, 604 | 54, 605 | 60, 606 | 67, 607 | 75, 608 | 83, 609 | 90, 610 | 95, 611 | 100, 612 | 110, 613 | 135, 614 | 153, 615 | 172, 616 | 192, 617 | 255 618 | } 619 | }; // end range25cm64PRFwb 620 | 621 | 622 | /*! ------------------------------------------------------------------------------------------------------------------ 623 | * Function: dwt_getrangebias() 624 | * 625 | * Description: This function is used to return the range bias correction need for TWR with DW1000 units. 626 | * 627 | * input parameters: 628 | * @param chan - specifies the operating channel (e.g. 1, 2, 3, 4, 5, 6 or 7) 629 | * @param range - the calculated distance before correction 630 | * @param prf - this is the PRF e.g. DWT_PRF_16M or DWT_PRF_64M 631 | * 632 | * output parameters 633 | * 634 | * returns correction needed in meters 635 | */ 636 | double dwt_getrangebias(uint8 chan, float range, uint8 prf) 637 | { 638 | //first get the lookup index that corresponds to given range for a particular channel at 16M PRF 639 | int i = 0 ; 640 | int chanIdx ; 641 | int cmoffseti ; // integer number of CM offset 642 | 643 | double mOffset ; // final offset result in metres 644 | 645 | // NB: note we may get some small negitive values e.g. up to -50 cm. 646 | 647 | int rangeint25cm = (int) (range * 4.00) ; // convert range to integer number of 25cm values. 648 | 649 | if (rangeint25cm > 255) rangeint25cm = 255 ; // make sure it matches largest value in table (all tables end in 255 !!!!) 650 | 651 | if (prf == DWT_PRF_16M) 652 | { 653 | switch(chan) 654 | { 655 | case 4: 656 | case 7: 657 | { 658 | chanIdx = chan_idxwb[chan]; 659 | while (rangeint25cm > range25cm16PRFwb[chanIdx][i]) i++ ; // find index in table corresponding to range 660 | cmoffseti = i + CM_OFFSET_16M_WB ; // nearest centimeter correction 661 | } 662 | break; 663 | default: 664 | { 665 | chanIdx = chan_idxnb[chan]; 666 | while (rangeint25cm > range25cm16PRFnb[chanIdx][i]) i++ ; // find index in table corresponding to range 667 | cmoffseti = i + CM_OFFSET_16M_NB ; // nearest centimeter correction 668 | } 669 | }//end of switch 670 | } 671 | else // 64M PRF 672 | { 673 | switch(chan) 674 | { 675 | case 4: 676 | case 7: 677 | { 678 | chanIdx = chan_idxwb[chan]; 679 | while (rangeint25cm > range25cm64PRFwb[chanIdx][i]) i++ ; // find index in table corresponding to range 680 | cmoffseti = i + CM_OFFSET_64M_WB ; // nearest centimeter correction 681 | } 682 | break; 683 | default: 684 | { 685 | chanIdx = chan_idxnb[chan]; 686 | while (rangeint25cm > range25cm64PRFnb[chanIdx][i]) i++ ; // find index in table corresponding to range 687 | cmoffseti = i + CM_OFFSET_64M_NB ; // nearest centimeter correction 688 | } 689 | }//end of switch 690 | } // end else 691 | 692 | 693 | mOffset = (float) cmoffseti ; // offset result in centimmetres 694 | 695 | mOffset *= 0.01 ; // convert to metres 696 | 697 | return (mOffset) ; 698 | } 699 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/deca_sleep.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_sleep.c 3 | * @brief platform dependent sleep implementation 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | /* Wrapper function to be used by decadriver. Declared in deca_device_api.h */ 20 | void deca_sleep(unsigned int time_ms) 21 | { 22 | k_msleep(time_ms); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/deca_spi.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_spi.c 3 | * @brief SPI access functions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * Copyright 2019 (c) Frederic Mes, RTLOC. 9 | * Copyright 2019 (c) Callender-Consulting LLC. 10 | * 11 | * All rights reserved. 12 | * 13 | * @author DecaWave 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #define LOG_LEVEL 3 28 | #include 29 | LOG_MODULE_REGISTER(deca_spi); 30 | 31 | const struct device * spi; 32 | struct spi_config * spi_cfg; 33 | struct spi_config spi_cfgs [4] = {0}; 34 | 35 | #define SPI_CFGS_COUNT ((sizeof(spi_cfgs)/sizeof(spi_cfgs[0]))) 36 | 37 | uint8_t tx_buf [255]; 38 | uint8_t rx_buf [255]; 39 | 40 | struct spi_buf bufs [2]; 41 | 42 | struct spi_buf_set tx; 43 | struct spi_buf_set rx; 44 | 45 | static struct spi_cs_control cs_ctrl; 46 | 47 | /* 48 | ***************************************************************************** 49 | * 50 | * DW1000 SPI section 51 | * 52 | ***************************************************************************** 53 | */ 54 | 55 | /* 56 | * Function: openspi() 57 | * 58 | * Low level abstract function to open and initialise access to the SPI device. 59 | * returns 0 for success, or -1 for error 60 | */ 61 | int openspi(void) 62 | { 63 | /* Propagate CS config into all spi_cfgs[] elements */ 64 | cs_ctrl.gpio_dev = device_get_binding(DT_LABEL(DT_PHANDLE_BY_IDX(DT_NODELABEL(spi2), cs_gpios, 0))); 65 | if (!cs_ctrl.gpio_dev) { 66 | printk("%s: GPIO binding failed.\n", __func__); 67 | return -1; 68 | } 69 | cs_ctrl.gpio_pin = DT_PHA(DT_NODELABEL(spi2), cs_gpios, pin); 70 | cs_ctrl.delay = 0U; 71 | cs_ctrl.gpio_dt_flags = DT_PHA(DT_NODELABEL(spi2), cs_gpios, flags); 72 | for (int i=0; i < SPI_CFGS_COUNT; i++) { 73 | spi_cfgs[i].cs = &cs_ctrl; 74 | } 75 | 76 | spi_cfg = &spi_cfgs[0]; 77 | 78 | spi = device_get_binding(DT_LABEL(DT_NODELABEL(spi2))); 79 | if (!spi) { 80 | printk("%s: SPI binding failed.\n", __func__); 81 | return -1; 82 | } 83 | spi_cfg->operation = SPI_WORD_SET(8); 84 | spi_cfg->frequency = 2000000; 85 | 86 | memset(&tx_buf[0], 0, 255); 87 | memset(&rx_buf[0], 0, 255); 88 | bufs[0].buf = &tx_buf[0]; 89 | bufs[1].buf = &rx_buf[0]; 90 | tx.buffers = &bufs[0]; 91 | rx.buffers = &bufs[1]; 92 | tx.count = 1; 93 | rx.count = 1; 94 | 95 | return 0; 96 | } 97 | 98 | void set_spi_speed_slow(void) 99 | { 100 | spi_cfg = &spi_cfgs[0]; 101 | spi_cfg->operation = SPI_WORD_SET(8); 102 | spi_cfg->frequency = 2000000; 103 | 104 | memset(&tx_buf[0], 0, 255); 105 | memset(&rx_buf[0], 0, 255); 106 | } 107 | 108 | void set_spi_speed_fast(void) 109 | { 110 | spi_cfg = &spi_cfgs[1]; 111 | spi_cfg->operation = SPI_WORD_SET(8); 112 | spi_cfg->frequency = 8000000; 113 | 114 | memset(&tx_buf[0], 0, 255); 115 | memset(&rx_buf[0], 0, 255); 116 | } 117 | 118 | /* 119 | * Function: closespi() 120 | * 121 | * Low level abstract function to close the the SPI device. 122 | * returns 0 for success, or -1 for error 123 | */ 124 | int closespi(void) 125 | { 126 | //TODO 127 | return 0; 128 | } 129 | 130 | /* 131 | * Function: writetospi() 132 | * 133 | * Low level abstract function to write to the SPI 134 | * Takes two separate byte buffers for write header and write data 135 | * returns 0 for success 136 | */ 137 | int writetospi(uint16 headerLength, 138 | const uint8 * headerBuffer, 139 | uint32 bodyLength, 140 | const uint8 * bodyBuffer) 141 | { 142 | decaIrqStatus_t stat; 143 | 144 | #if 0 145 | LOG_HEXDUMP_INF(headerBuffer, headerLength, "writetospi: Header"); 146 | LOG_HEXDUMP_INF(bodyBuffer, bodyLength, "writetospi: Body"); 147 | #endif 148 | 149 | stat = decamutexon(); 150 | 151 | memcpy(&tx_buf[0], headerBuffer, headerLength); 152 | memcpy(&tx_buf[headerLength], bodyBuffer, bodyLength); 153 | 154 | bufs[0].len = headerLength + bodyLength; 155 | bufs[1].len = headerLength + bodyLength; 156 | 157 | spi_transceive(spi, spi_cfg, &tx, &rx); 158 | 159 | decamutexoff(stat); 160 | 161 | return 0; 162 | } 163 | 164 | /* 165 | * Function: readfromspi() 166 | * 167 | * Low level abstract function to read from the SPI 168 | * Takes two separate byte buffers for write header and read data 169 | * returns the offset into read buffer where first byte of read data 170 | * may be found, or returns 0 171 | */ 172 | int readfromspi(uint16 headerLength, 173 | const uint8 * headerBuffer, 174 | uint32 readLength, 175 | uint8 * readBuffer) 176 | { 177 | decaIrqStatus_t stat; 178 | 179 | stat = decamutexon(); 180 | 181 | memset(&tx_buf[0], 0, headerLength + readLength); 182 | memcpy(&tx_buf[0], headerBuffer, headerLength); 183 | 184 | bufs[0].len = headerLength + readLength; 185 | bufs[1].len = headerLength + readLength; 186 | 187 | spi_transceive(spi, spi_cfg, &tx, &rx); 188 | 189 | memcpy(readBuffer, rx_buf + headerLength, readLength); 190 | 191 | decamutexoff(stat); 192 | 193 | #if 0 194 | LOG_HEXDUMP_INF(headerBuffer, headerLength, "readfromspi: Header"); 195 | LOG_HEXDUMP_INF(readBuffer, readLength, "readfromspi: Body"); 196 | #endif 197 | 198 | return 0; 199 | } 200 | -------------------------------------------------------------------------------- /drivers/dw1000/platform/port.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file port.c 3 | * @brief HW specific definitions and functions for portability 4 | * 5 | * @attention 6 | * 7 | * Copyright 2016 (c) DecaWave Ltd, Dublin, Ireland. 8 | * Copyright 2019 (c) Frederic Mes, RTLOC. 9 | * 10 | * 11 | * All rights reserved. 12 | * 13 | * @author DecaWave 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | // zephyr includes 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | static const struct device * gpio_dev; 30 | static struct gpio_callback gpio_cb; 31 | 32 | /****************************************************************************//** 33 | * 34 | * APP global variables 35 | * 36 | *******************************************************************************/ 37 | 38 | /****************************************************************************//** 39 | * 40 | * Port private variables and function prototypes 41 | * 42 | *******************************************************************************/ 43 | static volatile uint32_t signalResetDone; 44 | 45 | /****************************************************************************//** 46 | * 47 | * Time section 48 | * 49 | *******************************************************************************/ 50 | 51 | /* @fn usleep 52 | * @brief precise usleep() delay 53 | * */ 54 | #pragma GCC optimize ("O0") 55 | int usleep(unsigned long usec) 56 | { 57 | int i,j; 58 | #pragma GCC ivdep 59 | for(i=0;i 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | #include "ms8607_i2c.h" 27 | 28 | LOG_MODULE_REGISTER(MS8607, CONFIG_SENSOR_LOG_LEVEL); 29 | 30 | #define SENSOR_LOG_LEVEL_OFF 0 31 | #define SENSOR_LOG_LEVEL_ERROR 1 32 | #define SENSOR_LOG_LEVEL_WARNING 2 33 | #define SENSOR_LOG_LEVEL_INFO 3 34 | #define SENSOR_LOG_LEVEL_DEBUG 4 35 | 36 | 37 | 38 | struct ms8607_data *drv_data; 39 | 40 | void delay_ms(uint32_t time) 41 | { 42 | k_sleep(K_MSEC(time)); 43 | } 44 | 45 | void i2c_master_init(const struct device *dev) 46 | { 47 | drv_data = dev->data; 48 | LOG_INF("ms8607_init> i2c_master_init()"); 49 | } 50 | 51 | enum status_code i2c_master_read_packet_wait(struct i2c_master_packet *const packet) 52 | { 53 | if (i2c_read(drv_data->i2c , packet->data, packet->data_length, packet->address)) { 54 | return STATUS_ERR_TIMEOUT; 55 | } 56 | 57 | return STATUS_OK; 58 | } 59 | 60 | enum status_code i2c_master_write_packet_wait(struct i2c_master_packet *const packet) 61 | { 62 | if(i2c_write(drv_data->i2c , packet->data, packet->data_length, packet->address)){ 63 | return STATUS_ERR_TIMEOUT; 64 | } 65 | 66 | return STATUS_OK; 67 | } 68 | 69 | enum status_code i2c_master_write_packet_wait_no_stop(struct i2c_master_packet *const packet) 70 | { 71 | struct i2c_msg msg; 72 | 73 | msg.buf = packet->data; 74 | msg.len = packet->data_length; 75 | msg.flags = I2C_MSG_WRITE;// | I2C_MSG_STOP removed 76 | 77 | if(i2c_transfer(drv_data->i2c, &msg, 1, packet->address)){ 78 | return STATUS_ERR_TIMEOUT; 79 | } 80 | 81 | return STATUS_OK; 82 | } 83 | 84 | 85 | #ifdef __cplusplus 86 | } 87 | #endif 88 | -------------------------------------------------------------------------------- /drivers/sensor/ms8607/ms8607_i2c.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \brief MS8607 Temperature sensor driver source file 3 | * 4 | * Copyright (c) 2016 Measurement Specialties. All rights reserved. 5 | * 6 | * For details on programming, refer to ms8607 datasheet : 7 | * http://www.meas-spec.com/downloads/MS8607D.pdf 8 | * 9 | */ 10 | 11 | #ifndef MS8607_I2C_H_INCLUDED 12 | #define MS8607_I2C_H_INCLUDED 13 | 14 | #include 15 | #include 16 | 17 | enum i2c_transfer_direction { 18 | I2C_TRANSFER_WRITE = 0, 19 | I2C_TRANSFER_READ = 1, 20 | }; 21 | 22 | enum status_code { 23 | STATUS_OK = 0x00, 24 | STATUS_ERR_OVERFLOW = 0x01, 25 | STATUS_ERR_TIMEOUT = 0x02, 26 | }; 27 | 28 | struct i2c_master_packet { 29 | // Address to slave device 30 | uint16_t address; 31 | // Length of data array 32 | uint16_t data_length; 33 | // Data array containing all data to be transferred 34 | uint8_t *data; 35 | }; 36 | 37 | struct ms8607_data { 38 | const struct device *i2c; 39 | uint16_t sample; 40 | float humidity; 41 | float pressure; 42 | float temperature; 43 | }; 44 | 45 | 46 | void delay_ms(uint32_t time); 47 | void i2c_master_init(const struct device *dev); 48 | enum status_code i2c_master_read_packet_wait(struct i2c_master_packet *const packet); 49 | enum status_code i2c_master_write_packet_wait(struct i2c_master_packet *const packet); 50 | enum status_code i2c_master_write_packet_wait_no_stop(struct i2c_master_packet *const packet); 51 | 52 | 53 | 54 | #endif /* MS8607_I2C_H_INCLUDED */ 55 | -------------------------------------------------------------------------------- /drivers/sensor/veml6030/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: Apache-2.0 2 | 3 | zephyr_library() 4 | 5 | zephyr_library_sources_ifdef(CONFIG_VEML6030 veml6030.c) 6 | -------------------------------------------------------------------------------- /drivers/sensor/veml6030/Kconfig: -------------------------------------------------------------------------------- 1 | # VEML6030 light sensor configuration options 2 | 3 | # Copyright (c) 2016 Intel Corporation 4 | # SPDX-License-Identifier: Apache-2.0 5 | 6 | config VEML6030 7 | bool "VEML6030 Light Sensor" 8 | depends on I2C 9 | help 10 | Enable driver for VEML6030 light sensors. 11 | -------------------------------------------------------------------------------- /drivers/sensor/veml6030/veml6030.c: -------------------------------------------------------------------------------- 1 | #define DT_DRV_COMPAT vishay_veml6030 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | LOG_MODULE_REGISTER(VEML6030, CONFIG_SENSOR_LOG_LEVEL); 13 | 14 | #define SENSOR_LOG_LEVEL_OFF 0 15 | #define SENSOR_LOG_LEVEL_ERROR 1 16 | #define SENSOR_LOG_LEVEL_WARNING 2 17 | #define SENSOR_LOG_LEVEL_INFO 3 18 | #define SENSOR_LOG_LEVEL_DEBUG 4 19 | 20 | const float ref_gain_25_ms_1_8 = 1.8432; 21 | float lut_gain[] = {1,2,0.125,0.25}; 22 | uint16_t lut_integration_ms[] = {100,200,400,800,0,0,0,0,50,0,0,0,25,0,0,0}; 23 | 24 | // * Strategy tries to diminish gain first 25 | // * avoids edges by roughly ~ 2%, the last value within range would not tell if it is correct or saturated 26 | // * (8) takes UINT32_MAX because that's the lowest possible gain and integration time 27 | float modes_illum[] = { 28 | 230.0, // (0) 29 | 460.0, // (1) 30 | 1840.0, // (2) 31 | 3680.0, // (3) 32 | 7360.0, // (4) 33 | 14720.0, // (5) 34 | 29440.0, // (6) 35 | 58880.0, // (7) 36 | 200000.0, // (8) 37 | }; 38 | uint16_t modes_flags[] = { 39 | VEML6030_ALS_CONF_ALS_IT_800_ms | VEML6030_ALS_CONF_ALS_GAIN_x2, // (0) 40 | VEML6030_ALS_CONF_ALS_IT_800_ms | VEML6030_ALS_CONF_ALS_GAIN_x1, // (1) 41 | VEML6030_ALS_CONF_ALS_IT_800_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_4, // (2) 42 | VEML6030_ALS_CONF_ALS_IT_800_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (3) 43 | VEML6030_ALS_CONF_ALS_IT_400_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (4) 44 | VEML6030_ALS_CONF_ALS_IT_200_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (5) 45 | VEML6030_ALS_CONF_ALS_IT_100_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (6) 46 | VEML6030_ALS_CONF_ALS_IT_50_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (7) 47 | VEML6030_ALS_CONF_ALS_IT_25_ms | VEML6030_ALS_CONF_ALS_GAIN_x1_8, // (8) 48 | }; 49 | 50 | float reg_to_lum_lux(uint16_t val, uint16_t it_ms, float gain) 51 | { 52 | float resolution = 1.8432 / ((gain/0.125) * (it_ms/25)); 53 | //uint32_t res = resolution * 1000; 54 | //if(gain == 0.125)LOG_DBG("gain = 0.125"); 55 | //else if(gain == 0.25)LOG_DBG("gain = 0.25"); 56 | //else if(gain == 1)LOG_DBG("gain = 1"); 57 | //else if(gain == 2)LOG_DBG("gain = 2"); 58 | //else LOG_DBG("gain = %d uGain",(int)(gain*1000)); 59 | //LOG_DBG("integration time = %d ; resolution = %d mRes",it_ms,res); 60 | float lum_lux = val; 61 | float x = lum_lux * resolution; 62 | //y = 6.0135E-13x4 - 9.3924E-09x3 + 8.1488E-05x2 + 1.0023E+00x 63 | //correction will always be applied to avoid edges 64 | const float C0 = 1.0023; 65 | const float C1 = 8.1488E-05; 66 | const float C2 = -9.3924E-09; 67 | const float C3 = 6.0135E-13; 68 | float x_2 = x * x; 69 | float x_3 = x_2 * x; 70 | float x_4 = x_3 * x; 71 | float y = x_4 * C3 + x_3 * C2 + x_2 * C1 + x * C0; 72 | //printf("measure = %f ; corrected measure = %f",x,y); 73 | return y; 74 | } 75 | 76 | uint8_t get_optimal_mode(uint16_t sample,float lum_lux) 77 | { 78 | //actually, on saturation, set the highest mode to avoid slow raming up 79 | if(sample == 65535){ 80 | return 8; 81 | } 82 | else 83 | { 84 | uint8_t index = 0; 85 | while((lum_lux > modes_illum[index])&&(index<8)) 86 | { 87 | index++; 88 | } 89 | return index; 90 | } 91 | } 92 | 93 | static int veml6030_reg_read(struct veml6030_data *drv_data, uint8_t reg,uint16_t *val) 94 | { 95 | uint8_t valb[2]; 96 | if (i2c_burst_read(drv_data->i2c , VEML6030_I2C_ADDRESS, reg, valb, 2)) { 97 | return -EIO; 98 | } 99 | 100 | (*val) = valb[1]; 101 | (*val) = ((*val)<<8) + valb[0]; 102 | 103 | return 0; 104 | } 105 | 106 | static int veml6030_reg_write(struct veml6030_data *drv_data, uint8_t reg,uint16_t val) 107 | { 108 | uint8_t bdata[2]; 109 | bdata[0] = (uint8_t)(val&0xFF); 110 | bdata[1] = (uint8_t)(val>>8); 111 | 112 | return i2c_burst_write(drv_data->i2c , VEML6030_I2C_ADDRESS, reg, bdata, 2); 113 | } 114 | 115 | static int veml6030_reg_update(struct veml6030_data *drv_data, uint8_t reg,uint8_t mask, uint8_t val) 116 | { 117 | uint16_t old_val = 0U; 118 | uint16_t new_val = 0U; 119 | 120 | if (veml6030_reg_read(drv_data, reg, &old_val) != 0) { 121 | return -EIO; 122 | } 123 | 124 | new_val = old_val & ~mask; 125 | new_val |= val & mask; 126 | 127 | return veml6030_reg_write(drv_data, reg, new_val); 128 | } 129 | 130 | static int veml6030_attr_set( const struct device *dev,enum sensor_channel chan, 131 | enum sensor_attribute attr,const struct sensor_value *val) 132 | { 133 | struct veml6030_data *drv_data = dev->data; 134 | 135 | if (veml6030_reg_update(drv_data, VEML6030_REG_ALS_CONF, VEML6030_ALS_CONF_ALS_SD_MASK, VEML6030_ALS_CONF_ALS_SD_ON)) 136 | { 137 | return -EIO; 138 | } 139 | 140 | return 0; 141 | } 142 | 143 | static int veml6030_sample_fetch(const struct device *dev,enum sensor_channel chan) 144 | { 145 | struct veml6030_data *drv_data = dev->data; 146 | uint16_t val; 147 | 148 | __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_LIGHT); 149 | 150 | drv_data->sample = 0U; 151 | 152 | if(veml6030_reg_read(drv_data , VEML6030_REG_ALS, &val)){ 153 | return -EIO; 154 | } 155 | drv_data->sample = val; 156 | return 0; 157 | } 158 | 159 | static int veml6030_channel_get(const struct device *dev,enum sensor_channel chan,struct sensor_value *val) 160 | { 161 | struct veml6030_data *drv_data = dev->data; 162 | 163 | if (chan != SENSOR_CHAN_LIGHT) { 164 | return -ENOTSUP; 165 | } 166 | float resolution = 1.8432 / ((drv_data->gain/0.125) * (drv_data->integration_ms/25)); 167 | val->val1 = drv_data->sample * resolution; //tunc 168 | val->val2 = (drv_data->sample * resolution * 1000000) - (val->val1 * 1000000); //fraction - millionth 169 | 170 | return 0; 171 | } 172 | 173 | static const struct sensor_driver_api veml6030_driver_api = { 174 | .attr_set = veml6030_attr_set, 175 | .sample_fetch = veml6030_sample_fetch, 176 | .channel_get = veml6030_channel_get, 177 | }; 178 | 179 | static int veml6030_power_update(struct veml6030_data *drv_data, uint16_t val) 180 | { 181 | uint16_t old_config = 0U; 182 | uint16_t new_config = 0U; 183 | 184 | if (veml6030_reg_read(drv_data, VEML6030_REG_ALS_CONF, &old_config) != 0) { 185 | return -EIO; 186 | } 187 | //update params needed in every config update - here in power with existing config from sensors only 188 | uint16_t integration_bits = old_config & VEML6030_ALS_CONF_ALS_IT_MASK; 189 | uint16_t gain_bits = old_config & VEML6030_ALS_CONF_ALS_GAIN_MASK; 190 | drv_data->config_it_gain = integration_bits | gain_bits; 191 | drv_data->integration_ms = lut_integration_ms[integration_bits>>6]; 192 | drv_data->gain = lut_gain[gain_bits>>11]; 193 | 194 | new_config = old_config & ~VEML6030_ALS_CONF_ALS_SD_MASK; 195 | new_config |= val & VEML6030_ALS_CONF_ALS_SD_MASK; 196 | 197 | return veml6030_reg_write(drv_data, VEML6030_REG_ALS_CONF, new_config); 198 | } 199 | 200 | static int veml6030_it_gain_update(struct veml6030_data *drv_data, uint16_t val) 201 | { 202 | uint16_t old_config = 0U; 203 | uint16_t new_config = 0U; 204 | int ret = 0; 205 | 206 | if (veml6030_reg_read(drv_data, VEML6030_REG_ALS_CONF, &old_config) != 0) { 207 | LOG_ERR("veml6030_reg_read() failed"); 208 | return -EIO; 209 | } 210 | uint16_t mask = (VEML6030_ALS_CONF_ALS_IT_MASK | VEML6030_ALS_CONF_ALS_GAIN_MASK); 211 | new_config = old_config & ~mask; 212 | new_config |= val & mask; 213 | 214 | uint16_t integration_bits = new_config & VEML6030_ALS_CONF_ALS_IT_MASK; 215 | uint16_t gain_bits = new_config & VEML6030_ALS_CONF_ALS_GAIN_MASK; 216 | drv_data->config_it_gain = integration_bits | gain_bits; 217 | drv_data->integration_ms = lut_integration_ms[integration_bits>>6]; 218 | drv_data->gain = lut_gain[gain_bits>>11]; 219 | 220 | ret = veml6030_reg_write(drv_data, VEML6030_REG_ALS_CONF, new_config); 221 | if(ret){ 222 | LOG_ERR("veml6030_reg_write() failed"); 223 | } 224 | return ret; 225 | } 226 | 227 | int veml6030_power_on(const struct device *dev) 228 | { 229 | struct veml6030_data *drv_data = dev->data; 230 | int ret = veml6030_power_update(drv_data,VEML6030_ALS_CONF_ALS_SD_ON); 231 | if (ret) { 232 | LOG_ERR("veml6030_power_update() failed"); 233 | } 234 | return ret; 235 | } 236 | 237 | int veml6030_power_off(const struct device *dev) 238 | { 239 | struct veml6030_data *drv_data = dev->data; 240 | int ret = veml6030_power_update(drv_data,VEML6030_ALS_CONF_ALS_SD_OFF); 241 | if (ret) { 242 | LOG_ERR("veml6030_power_update() failed"); 243 | } 244 | return ret; 245 | } 246 | 247 | int veml6030_init(const struct device *dev) 248 | { 249 | struct veml6030_data *drv_data = dev->data; 250 | LOG_INF("veml6030_init()"); 251 | drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0)); 252 | if (drv_data->i2c == NULL) { 253 | LOG_ERR("Failed to get pointer to %s device!",DT_INST_BUS_LABEL(0)); 254 | return -EINVAL; 255 | } 256 | 257 | return 0; 258 | } 259 | 260 | static struct veml6030_data veml6030_drv_data; 261 | 262 | DEVICE_DT_INST_DEFINE(0, veml6030_init, device_pm_control_nop, 263 | &veml6030_drv_data, NULL, POST_KERNEL, 264 | CONFIG_SENSOR_INIT_PRIORITY, &veml6030_driver_api); 265 | 266 | float veml6030_fetch_lux(const struct device *dev) 267 | { 268 | struct veml6030_data *drv_data = dev->data; 269 | if (veml6030_sample_fetch(dev, SENSOR_CHAN_LIGHT) != 0) { 270 | LOG_ERR("sensor: sample fetch fail."); 271 | return 0; 272 | } 273 | 274 | return reg_to_lum_lux(drv_data->sample,drv_data->integration_ms, drv_data->gain); 275 | } 276 | 277 | //a measure might not be good (e.g. saturated) if not taken with optimal config that is checked after the measure has taken place 278 | float veml6030_auto_measure(const struct device *dev) 279 | { 280 | struct veml6030_data *drv_data = dev->data; 281 | float measure_lux = 0; 282 | 283 | veml6030_power_on(dev);//4 ms min after power on => will update the drv_data ->integration_ms and ->gain 284 | 285 | bool optimal_measure = false;// no measure yet 286 | while(!optimal_measure) 287 | { 288 | k_sleep(K_MSEC(2*drv_data->integration_ms+5));//wait the integration time +5 ms clock drift safety : x2 as x1 fail with reading 0 289 | measure_lux = veml6030_fetch_lux(dev); 290 | uint8_t optimal_mode = get_optimal_mode(drv_data->sample,measure_lux); 291 | if(modes_flags[optimal_mode] == drv_data->config_it_gain)//optimal mode already selected 292 | { 293 | optimal_measure = true; 294 | } 295 | else 296 | { 297 | printf("auto_measure>sample %d not optimal ; gain = %.3f ; it = %d ms\n", 298 | drv_data->sample, drv_data->gain, drv_data->integration_ms); 299 | //update both integration and gain to the new optimal value 300 | veml6030_it_gain_update(drv_data,modes_flags[optimal_mode]); 301 | printf("auto_measure>new params => gain = %f ; it = %d\n",drv_data->gain,drv_data->integration_ms); 302 | optimal_measure = false; 303 | } 304 | } 305 | 306 | veml6030_power_off(dev); 307 | drv_data->lum_lux = measure_lux; 308 | drv_data->lum_lux = measure_lux; 309 | return measure_lux; 310 | } 311 | -------------------------------------------------------------------------------- /include/drivers/dw1000/compiler.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file compiler.h 3 | * @brief 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | #ifndef COMPILER_H_ 15 | #define COMPILER_H_ 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | 22 | #ifdef __GNUC__ 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | // #include 29 | #include 30 | #include 31 | 32 | #ifndef CLOCKS_PER_SEC 33 | #define CLOCKS_PER_SEC 1000 34 | #endif 35 | 36 | #ifndef TRUE 37 | #define TRUE true 38 | #endif 39 | #ifndef FALSE 40 | #define FALSE false 41 | #endif 42 | 43 | #ifndef __align4 44 | #define __align4 __attribute__((aligned (4))) 45 | #endif 46 | #ifndef __weak 47 | #define __weak __attribute__((weak)) 48 | #endif 49 | #ifndef __always_inline 50 | #define __always_inline __attribute__((always_inline)) 51 | #endif 52 | 53 | #endif 54 | 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* COMPILER_H_ */ 61 | -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_device_api.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/include/drivers/dw1000/deca_device_api.h -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_param_types.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_param_types.h 3 | * @brief Decawave general type definitions for configuration structures 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | #ifndef _DECA_PARAM_TYPES_H_ 13 | #define _DECA_PARAM_TYPES_H_ 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | #include "deca_types.h" 19 | 20 | #define NUM_BR 3 21 | #define NUM_PRF 2 22 | #define NUM_PACS 4 23 | #define NUM_BW 2 //2 bandwidths are supported 24 | #define NUM_SFD 2 //supported number of SFDs - standard = 0, non-standard = 1 25 | #define NUM_CH 6 //supported channels are 1, 2, 3, 4, 5, 7 26 | #define NUM_CH_SUPPORTED 8 //supported channels are '0', 1, 2, 3, 4, 5, '6', 7 27 | #define PCODES 25 //supported preamble codes 28 | 29 | 30 | typedef struct { 31 | uint32 lo32; 32 | uint16 target[NUM_PRF]; 33 | } agc_cfg_struct ; 34 | 35 | extern const agc_cfg_struct agc_config ; 36 | 37 | //SFD threshold settings for 110k, 850k, 6.8Mb standard and non-standard 38 | extern const uint16 sftsh[NUM_BR][NUM_SFD]; 39 | 40 | extern const uint16 dtune1[NUM_PRF]; 41 | 42 | #define XMLPARAMS_VERSION (1.17f) 43 | 44 | extern const uint32 fs_pll_cfg[NUM_CH]; 45 | extern const uint8 fs_pll_tune[NUM_CH]; 46 | extern const uint8 rx_config[NUM_BW]; 47 | extern const uint32 tx_config[NUM_CH]; 48 | extern const uint8 dwnsSFDlen[NUM_BR]; //length of SFD for each of the bitrates 49 | extern const uint32 digital_bb_config[NUM_PRF][NUM_PACS]; 50 | extern const uint8 chan_idx[NUM_CH_SUPPORTED]; 51 | 52 | #define TEMP_COMP_FACTOR_CH2 (327) //(INT) (0.0798 * 4096) 53 | #define TEMP_COMP_FACTOR_CH5 (607) //(INT) (0.1482 * 4096) 54 | #define SAR_TEMP_TO_CELCIUS_CONV (1.14) 55 | #define SAR_VBAT_TO_VOLT_CONV (1.0/173) 56 | 57 | #define DCELCIUS_TO_SAR_TEMP_CONV ((int)((0.10/1.14)*256)) 58 | #define MVOLT_TO_SAR_VBAT_CONV (173.0/1000) 59 | 60 | #define MIXER_GAIN_STEP (0.5) 61 | #define DA_ATTN_STEP (2.5) 62 | 63 | #define MIX_DA_FACTOR (DA_ATTN_STEP/MIXER_GAIN_STEP) 64 | 65 | #define PEAK_MULTPLIER (0x60) //3 -> (0x3 * 32) & 0x00E0 66 | #define N_STD_FACTOR (13) 67 | #define LDE_PARAM1 (PEAK_MULTPLIER | N_STD_FACTOR) 68 | 69 | #define LDE_PARAM3_16 (0x1607) 70 | #define LDE_PARAM3_64 (0x0607) 71 | 72 | extern const uint16 lde_replicaCoeff[PCODES]; 73 | 74 | #ifdef __cplusplus 75 | } 76 | #endif 77 | 78 | #endif 79 | 80 | 81 | -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_regs.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/include/drivers/dw1000/deca_regs.h -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_spi.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_spi.h 3 | * @brief SPI access functions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | #ifndef _DECA_SPI_H_ 15 | #define _DECA_SPI_H_ 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include "deca_types.h" 22 | 23 | #define DECA_MAX_SPI_HEADER_LENGTH (3) // max number of bytes in header (for formating & sizing) 24 | 25 | /*! ------------------------------------------------------------------------------------------------------------------ 26 | * Function: openspi() 27 | * 28 | * Low level abstract function to open and initialise access to the SPI device. 29 | * returns 0 for success, or -1 for error 30 | */ 31 | int openspi(void) ; 32 | 33 | /*! ------------------------------------------------------------------------------------------------------------------ 34 | * Function: closespi() 35 | * 36 | * Low level abstract function to close the the SPI device. 37 | * returns 0 for success, or -1 for error 38 | */ 39 | int closespi(void) ; 40 | 41 | void set_spi_speed_slow(); 42 | void set_spi_speed_fast(); 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | 48 | #endif /* _DECA_SPI_H_ */ 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_types.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_types.h 3 | * @brief Decawave general type definitions 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #ifndef _DECA_TYPES_H_ 14 | #define _DECA_TYPES_H_ 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | #ifndef uint8 21 | #ifndef _DECA_UINT8_ 22 | #define _DECA_UINT8_ 23 | typedef unsigned char uint8; 24 | #endif 25 | #endif 26 | 27 | #ifndef uint16 28 | #ifndef _DECA_UINT16_ 29 | #define _DECA_UINT16_ 30 | typedef unsigned short uint16; 31 | #endif 32 | #endif 33 | 34 | #ifndef uint32 35 | #ifndef _DECA_UINT32_ 36 | #define _DECA_UINT32_ 37 | typedef unsigned long uint32; 38 | #endif 39 | #endif 40 | 41 | #ifndef int8 42 | #ifndef _DECA_INT8_ 43 | #define _DECA_INT8_ 44 | typedef signed char int8; 45 | #endif 46 | #endif 47 | 48 | #ifndef int16 49 | #ifndef _DECA_INT16_ 50 | #define _DECA_INT16_ 51 | typedef signed short int16; 52 | #endif 53 | #endif 54 | 55 | #ifndef int32 56 | #ifndef _DECA_INT32_ 57 | #define _DECA_INT32_ 58 | typedef signed long int32; 59 | #endif 60 | #endif 61 | 62 | #ifndef NULL 63 | #define NULL ((void *)0UL) 64 | #endif 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif /* DECA_TYPES_H_ */ 71 | 72 | 73 | -------------------------------------------------------------------------------- /include/drivers/dw1000/deca_version.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file deca_version.h 3 | * @brief Defines the version info for the DW1000 device driver including its API 4 | * 5 | * @attention 6 | * 7 | * Copyright 2013 (c) Decawave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | */ 12 | 13 | #ifndef _DECA_VERSION_H_ 14 | #define _DECA_VERSION_H_ 15 | 16 | // 17 | // The DW1000 device driver is separately version numbered to any version the application using it may have 18 | // 19 | // Two symbols are defined here: one hexadecimal value and one string that includes the hex bytes. 20 | // Both should be updated together in a consistent way when the software is being modified. 21 | // 22 | // The format of the hex version is 0xAABBCC and the string ends with AA.BB.CC, where... 23 | // 24 | // Quantity CC is updated for minor changes/bug fixes that should not need user code changes 25 | // Quantity BB is updated for changes/bug fixes that may need user code changes 26 | // Quantity AA is updated for major changes that will need user code changes 27 | // 28 | 29 | #define DW1000_DRIVER_VERSION 0x050100 30 | #define DW1000_DEVICE_DRIVER_VER_STRING "DW1000 Device Driver Version 05.01.00" 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /include/drivers/dw1000/port.h: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file port.h 3 | * @brief HW specific definitions and functions for portability 4 | * 5 | * @attention 6 | * 7 | * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. 8 | * 9 | * All rights reserved. 10 | * 11 | * @author DecaWave 12 | */ 13 | 14 | 15 | #ifndef PORT_H_ 16 | #define PORT_H_ 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #include 23 | #include 24 | #include "compiler.h" 25 | 26 | /* DW1000 IRQ handler declaration. */ 27 | // static port_deca_isr_t port_deca_isr; 28 | 29 | /* DW1000 IRQ handler type. */ 30 | typedef void (*port_deca_isr_t)(void); 31 | 32 | 33 | /*! ------------------------------------------------------------------------------------------------------------------ 34 | * @fn port_set_deca_isr() 35 | * 36 | * @brief This function is used to install the handling function for DW1000 IRQ. 37 | * 38 | * NOTE: 39 | * 40 | * @param deca_isr function pointer to DW1000 interrupt handler to install 41 | * 42 | * @return none 43 | */ 44 | void port_set_deca_isr(port_deca_isr_t deca_isr); 45 | 46 | 47 | 48 | /*****************************************************************************************************************//* 49 | **/ 50 | 51 | /****************************************************************************//** 52 | * 53 | * Types definitions 54 | * 55 | *******************************************************************************/ 56 | typedef uint64_t uint64 ; 57 | 58 | typedef int64_t int64 ; 59 | 60 | 61 | #ifndef FALSE 62 | #define FALSE 0 63 | #endif 64 | 65 | #ifndef TRUE 66 | #define TRUE 1 67 | #endif 68 | 69 | typedef enum 70 | { 71 | LED_PC6, //LED_P0.x 72 | LED_PC7, //LED_P0.x 73 | LED_PC8, //LED_P0.x 74 | LED_PC9, //LED_P0.x 75 | LED_ALL, 76 | LEDn 77 | } led_t; 78 | 79 | /****************************************************************************//** 80 | * 81 | * MACRO 82 | * 83 | *******************************************************************************/ 84 | 85 | //TODO 86 | #define DW1000_RSTn 87 | #define DW1000_RSTn_GPIO 88 | 89 | 90 | #define DECAIRQ 91 | #define DECAIRQ_GPIO 92 | 93 | #define TA_BOOT1 94 | #define TA_BOOT1_GPIO 95 | 96 | #define TA_RESP_DLY 97 | #define TA_RESP_DLY_GPIO 98 | 99 | #define TA_SW1_3 100 | #define TA_SW1_4 101 | #define TA_SW1_5 102 | #define TA_SW1_6 103 | #define TA_SW1_7 104 | #define TA_SW1_8 105 | #define TA_SW1_GPIO 106 | 107 | /****************************************************************************//** 108 | * 109 | * MACRO function 110 | * 111 | *******************************************************************************/ 112 | 113 | //TODO 114 | #define GPIO_ResetBits(x,y) 115 | #define GPIO_SetBits(x,y) 116 | #define GPIO_ReadInputDataBit(x,y) 117 | 118 | 119 | /* NSS pin is SW controllable */ 120 | #define port_SPIx_set_chip_select() 121 | #define port_SPIx_clear_chip_select() 122 | 123 | /* NSS pin is SW controllable */ 124 | #define port_SPIy_set_chip_select() 125 | #define port_SPIy_clear_chip_select() 126 | 127 | /****************************************************************************//** 128 | * 129 | * port function prototypes 130 | * 131 | *******************************************************************************/ 132 | 133 | #define S1_SWITCH_ON (1) 134 | #define S1_SWITCH_OFF (0) 135 | 136 | 137 | void port_set_dw1000_slowrate(void); 138 | void port_set_dw1000_fastrate(void); 139 | 140 | #ifdef __cplusplus 141 | } 142 | #endif 143 | 144 | #endif /* PORT_H_ */ 145 | 146 | /* 147 | * Taken from the Linux Kernel 148 | * 149 | */ 150 | 151 | #ifndef _LINUX_CIRC_BUF_H 152 | #define _LINUX_CIRC_BUF_H 1 153 | 154 | struct circ_buf { 155 | char *buf; 156 | int head; 157 | int tail; 158 | }; 159 | 160 | /* Return count in buffer. */ 161 | #define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1)) 162 | 163 | /* Return space available, 0..size-1. We always leave one free char 164 | as a completely full buffer has head == tail, which is the same as 165 | empty. */ 166 | #define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size)) 167 | 168 | /* Return count up to the end of the buffer. Carefully avoid 169 | accessing head and tail more than once, so they can change 170 | underneath us without returning inconsistent results. */ 171 | #define CIRC_CNT_TO_END(head,tail,size) \ 172 | ({int end = (size) - (tail); \ 173 | int n = ((head) + end) & ((size)-1); \ 174 | n < end ? n : end;}) 175 | 176 | /* Return space available up to the end of the buffer. */ 177 | #define CIRC_SPACE_TO_END(head,tail,size) \ 178 | ({int end = (size) - 1 - (head); \ 179 | int n = (end + (tail)) & ((size)-1); \ 180 | n <= end ? n : end+1;}) 181 | 182 | #endif /* _LINUX_CIRC_BUF_H */ 183 | 184 | -------------------------------------------------------------------------------- /include/meshposition.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | using json = nlohmann::json; 7 | 8 | enum struct msg_id_t: uint8_t{ 9 | ping = 1, // 10 | reset = 2, //no payload 11 | alive = 3, //uint32_t counter 12 | address_request = 4, // 13 | address_assign = 5, //uint8_t address 14 | string = 6, //std::string 15 | twr_1_poll = 7, // struct 16 | twr_2_resp = 8, // struct 17 | twr_3_final = 9, // struct 18 | ack = 255 // 19 | }; 20 | 21 | typedef struct 22 | { 23 | msg_id_t id; 24 | uint8_t sequence; 25 | uint8_t source; 26 | uint8_t dest; 27 | }header_t; 28 | 29 | typedef struct 30 | { 31 | header_t header; 32 | uint16_t crc; 33 | }msg_header_t; 34 | 35 | typedef struct 36 | { 37 | header_t header; 38 | uint32_t poll_tx_ts; 39 | uint32_t resp_rx_ts; 40 | uint32_t final_tx_ts; 41 | uint16_t crc; 42 | }msg_twr_final_t; 43 | 44 | void mp_start(); 45 | 46 | void mp_conf_to_json(dwt_config_t &conf,json &jconf); 47 | void mp_json_to_conf(json &jconf,dwt_config_t &conf); 48 | 49 | json mp_status_to_json(uint32_t status_reg); 50 | void mp_status_print(uint32_t status_reg); 51 | 52 | void mp_start(dwt_config_t &config); 53 | uint32_t mp_get_status(); 54 | void mp_rx_now(uint16_t timeout = 0); 55 | void mp_rx_after_tx(uint32_t delay_us,uint16_t timeout); 56 | 57 | void mp_request(msg_header_t &header); 58 | void mp_request(uint8_t* data, uint16_t size); 59 | 60 | bool mp_receive(msg_id_t id); 61 | bool mp_receive(msg_id_t id,msg_header_t&header); 62 | bool mp_receive(msg_id_t id,msg_twr_final_t& final_msg); 63 | bool mp_receive(uint8_t* data, uint16_t expected_size); 64 | 65 | void mp_send(uint8_t* data, uint16_t size); 66 | bool mp_send_at(uint8_t* data, uint16_t size, uint64_t tx_time, uint8_t flag=0); 67 | bool mp_request_at(uint8_t* data, uint16_t size, uint64_t tx_time); 68 | 69 | //low level API 70 | uint32_t mp_poll_rx(); 71 | uint64_t get_tx_timestamp_u64(void); 72 | uint64_t get_rx_timestamp_u64(void); 73 | 74 | //------------------------------------- twr ------------------------------------- 75 | void uwb_ping(uint8_t sequence,uint8_t pinger,uint8_t target); 76 | void uwb_ping_rx(uint8_t sequence,uint8_t pinger,uint8_t target,json &res); 77 | void uwb_cir_acc(uint8_t source); 78 | 79 | #ifdef CONFIG_MP_GPIO_DEBUG 80 | void twr_gpio_init(const struct device *gpio_dev); 81 | #endif 82 | void twr_respond(uint8_t sequence,uint8_t source_initiator,uint8_t dest_responder,json &res); 83 | void twr_intiate(uint8_t sequence,uint8_t source_initiator,uint8_t dest_responder); 84 | -------------------------------------------------------------------------------- /media/forum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/media/forum.png -------------------------------------------------------------------------------- /samples/01_hello_leds/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /samples/01_hello_leds/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.13.1) 3 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 4 | project(adc_battery) 5 | 6 | target_sources(app PRIVATE src/main.c) 7 | -------------------------------------------------------------------------------- /samples/01_hello_leds/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_GPIO=y 2 | CONFIG_SERIAL=n 3 | 4 | CONFIG_NEWLIB_LIBC=y 5 | CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y 6 | 7 | # Logging 8 | CONFIG_LOG=y 9 | CONFIG_LOG_BACKEND_RTT=y 10 | CONFIG_LOG_BACKEND_UART=n 11 | CONFIG_BOOT_BANNER=y 12 | CONFIG_USE_SEGGER_RTT=y 13 | CONFIG_CONSOLE=y 14 | CONFIG_UART_CONSOLE=n 15 | CONFIG_RTT_CONSOLE=y 16 | 17 | -------------------------------------------------------------------------------- /samples/01_hello_leds/readme.md: -------------------------------------------------------------------------------- 1 | ## usage 2 | ```bash 3 | west build -t guiconfig 4 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 5 | west flash 6 | ``` 7 | -------------------------------------------------------------------------------- /samples/01_hello_leds/sample.yaml: -------------------------------------------------------------------------------- 1 | sample: 2 | name: Hello leds etst sample 3 | -------------------------------------------------------------------------------- /samples/01_hello_leds/src/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); 8 | 9 | #define LED0_NODE DT_ALIAS(led0) 10 | #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios) 11 | #define LED0_PIN DT_GPIO_PIN(LED0_NODE, gpios) 12 | 13 | #define LED1_NODE DT_ALIAS(led1) 14 | #define LED1 DT_GPIO_LABEL(LED1_NODE, gpios) 15 | #define LED1_PIN DT_GPIO_PIN(LED1_NODE, gpios) 16 | 17 | #define LED2_NODE DT_ALIAS(led2) 18 | #define LED2 DT_GPIO_LABEL(LED2_NODE, gpios) 19 | #define LED2_PIN DT_GPIO_PIN(LED2_NODE, gpios) 20 | 21 | #define LED3_NODE DT_ALIAS(led3) 22 | #define LED3 DT_GPIO_LABEL(LED3_NODE, gpios) 23 | #define LED3_PIN DT_GPIO_PIN(LED3_NODE, gpios) 24 | 25 | 26 | 27 | void main(void) 28 | { 29 | LOG_INF("Hello world leds sample"); 30 | 31 | const struct device *dev = device_get_binding(LED0); 32 | 33 | gpio_pin_configure(dev, LED0_PIN, GPIO_OUTPUT_ACTIVE); 34 | gpio_pin_configure(dev, LED1_PIN, GPIO_OUTPUT_ACTIVE); 35 | gpio_pin_configure(dev, LED2_PIN, GPIO_OUTPUT_ACTIVE); 36 | gpio_pin_configure(dev, LED3_PIN, GPIO_OUTPUT_ACTIVE); 37 | gpio_pin_set(dev, LED0_PIN, 0); 38 | gpio_pin_set(dev, LED1_PIN, 0); 39 | gpio_pin_set(dev, LED2_PIN, 0); 40 | gpio_pin_set(dev, LED3_PIN, 0); 41 | k_sleep(K_MSEC(1000)); 42 | gpio_pin_set(dev, LED0_PIN, 1); 43 | gpio_pin_set(dev, LED1_PIN, 1); 44 | gpio_pin_set(dev, LED2_PIN, 1); 45 | gpio_pin_set(dev, LED3_PIN, 1); 46 | k_sleep(K_MSEC(1000)); 47 | 48 | int count = 0; 49 | while (1) { 50 | LOG_INF("leds> hello %d",count++); 51 | gpio_pin_set(dev, LED0_PIN, 0); 52 | k_sleep(K_MSEC(1000)); 53 | gpio_pin_set(dev, LED0_PIN, 1); 54 | gpio_pin_set(dev, LED1_PIN, 0); 55 | k_sleep(K_MSEC(1000)); 56 | gpio_pin_set(dev, LED1_PIN, 1); 57 | gpio_pin_set(dev, LED2_PIN, 0); 58 | k_sleep(K_MSEC(1000)); 59 | gpio_pin_set(dev, LED2_PIN, 1); 60 | gpio_pin_set(dev, LED3_PIN, 0); 61 | k_sleep(K_MSEC(1000)); 62 | gpio_pin_set(dev, LED3_PIN, 1); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /samples/02_listener/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.13.1) 3 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 4 | project(uwb_listener) 5 | 6 | target_sources(app PRIVATE src/main.c) 7 | -------------------------------------------------------------------------------- /samples/02_listener/README.md: -------------------------------------------------------------------------------- 1 | ## usage 2 | ```bash 3 | west build -t guiconfig 4 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 5 | west flash 6 | ``` 7 | -------------------------------------------------------------------------------- /samples/02_listener/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_DW1000=y 2 | 3 | CONFIG_DEBUG=y 4 | CONFIG_SPI=y 5 | CONFIG_GPIO=y 6 | CONFIG_PRINTK=y 7 | 8 | CONFIG_USE_SEGGER_RTT=y 9 | CONFIG_SEGGER_RTT_MAX_NUM_UP_BUFFERS=3 10 | CONFIG_SEGGER_RTT_MAX_NUM_DOWN_BUFFERS=3 11 | CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=1024 12 | CONFIG_SEGGER_RTT_BUFFER_SIZE_DOWN=16 13 | CONFIG_SEGGER_RTT_PRINTF_BUFFER_SIZE=64 14 | CONFIG_SEGGER_RTT_MODE_NO_BLOCK_SKIP=y 15 | 16 | CONFIG_LOG_BACKEND_RTT=y 17 | CONFIG_LOG_BACKEND_RTT_MODE_BLOCK=y 18 | CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE=16 19 | CONFIG_LOG_BACKEND_RTT_RETRY_CNT=4 20 | CONFIG_LOG_BACKEND_RTT_RETRY_DELAY_MS=5 21 | CONFIG_LOG_BACKEND_RTT_BUFFER=0 22 | 23 | CONFIG_LOG=y 24 | CONFIG_LOG_DEFAULT_LEVEL=3 25 | CONFIG_LOG_OVERRIDE_LEVEL=0 26 | CONFIG_LOG_MAX_LEVEL=4 27 | CONFIG_LOG_FUNC_NAME_PREFIX_DBG=y 28 | 29 | CONFIG_LOG_PRINTK=y 30 | CONFIG_LOG_PRINTK_MAX_STRING_LENGTH=128 31 | CONFIG_LOG_MODE_OVERFLOW=y 32 | CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD=10 33 | CONFIG_LOG_PROCESS_THREAD=y 34 | CONFIG_LOG_PROCESS_THREAD_SLEEP_MS=1000 35 | CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=768 36 | CONFIG_LOG_BUFFER_SIZE=6144 37 | CONFIG_LOG_DETECT_MISSED_STRDUP=y 38 | CONFIG_LOG_STRDUP_MAX_STRING=32 39 | CONFIG_LOG_STRDUP_BUF_COUNT=4 40 | 41 | CONFIG_LOG_BACKEND_SHOW_COLOR=n 42 | -------------------------------------------------------------------------------- /samples/02_listener/src/main.c: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file main.c 3 | * @brief RX using SNIFF mode example code 4 | * 5 | * @attention 6 | * 7 | * Copyright 2016 (c) Decawave Ltd, Dublin, Ireland. 8 | * Copyright 2019 (c) Frederic Mes, RTLOC. 9 | * 10 | * All rights reserved. 11 | * 12 | * @author Decawave 13 | */ 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | // zephyr includes 22 | #include 23 | #include 24 | 25 | #define STACKSIZE 1024 26 | #define PRIORITY 99 27 | #define DELAY_TIME K_MSEC(1000) 28 | 29 | #define LOG_LEVEL 3 30 | #include 31 | LOG_MODULE_REGISTER(main); 32 | 33 | /* Example application name and version to display on console. */ 34 | #define APP_NAME "SIMPLE RX v1.3\n" 35 | 36 | static dwt_config_t config = { 37 | 5, /* Channel number. */ 38 | DWT_PRF_64M, /* Pulse repetition frequency. */ 39 | DWT_PLEN_128, /* Preamble length. Used in TX only. */ 40 | DWT_PAC8, /* Preamble acquisition chunk size. Used in RX only. */ 41 | 9, /* TX preamble code. Used in TX only. */ 42 | 9, /* RX preamble code. Used in RX only. */ 43 | 1, /* 0 to use standard SFD, 1 to use non-standard SFD. */ 44 | DWT_BR_6M8, /* Data rate. */ 45 | DWT_PHRMODE_EXT, /* PHY header mode. */ 46 | (129) /* SFD timeout (preamble length + 1 + SFD length - PAC size). 47 | * Used in RX only. */ 48 | }; 49 | 50 | /* Buffer to store received frame. See NOTE 1 below. */ 51 | #define FRAME_LEN_MAX 127 52 | static uint8 rx_buffer[FRAME_LEN_MAX]; 53 | 54 | /* Hold copy of status register state here for reference so that it can be 55 | examined at a debug breakpoint. 56 | */ 57 | static uint32 status_reg = 0; 58 | 59 | /* Hold copy of frame length of frame received (if good) so that it can be 60 | examined at a debug breakpoint. 61 | */ 62 | static uint16 frame_len = 0; 63 | 64 | void sniff_ms(int time) 65 | { 66 | int start = k_uptime_get(); 67 | int finish = start + time; 68 | 69 | while (k_uptime_get() < finish) { 70 | for (int i = 0 ; i < FRAME_LEN_MAX; i++ ) 71 | { 72 | rx_buffer[i] = 0; 73 | } 74 | dwt_rxenable(DWT_START_RX_IMMEDIATE); 75 | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & 76 | (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { /* spin */ }; 77 | 78 | if (status_reg & SYS_STATUS_RXFCG) { 79 | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023; 80 | if (frame_len <= FRAME_LEN_MAX) { 81 | dwt_readrxdata(rx_buffer, frame_len, 0); 82 | int time_stamp = k_uptime_get(); 83 | printf("%d> ",time_stamp); 84 | int i; 85 | for (i = 0 ; i < frame_len; i++ ) 86 | { 87 | printf("%02x ",rx_buffer[i]); 88 | } 89 | printf("\r\n"); 90 | } 91 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); 92 | } 93 | else { 94 | printf("0x%04lX\r\n",status_reg); 95 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); 96 | } 97 | } 98 | 99 | } 100 | 101 | void main_thread(void * id, void * unused1, void * unused2) 102 | { 103 | printk("%s\n", __func__); 104 | printk(APP_NAME); 105 | openspi(); 106 | 107 | port_set_dw1000_slowrate(); 108 | 109 | if (dwt_initialise(DWT_LOADNONE) == DWT_ERROR) { 110 | printk("INIT FAILED"); 111 | k_sleep(K_MSEC(500)); 112 | while (1) { /* spin */}; 113 | } 114 | port_set_dw1000_fastrate(); 115 | 116 | dwt_configure(&config); 117 | 118 | printf("configured : chan %u, rxCode %u, nsSFD %u\r\n", 119 | config.chan, config.rxCode, config.nsSFD); 120 | 121 | dwt_setleds(1); 122 | 123 | k_yield(); 124 | 125 | while (1) { 126 | sniff_ms(10000); 127 | } 128 | } 129 | 130 | K_THREAD_DEFINE(main_id, STACKSIZE, main_thread, 131 | NULL, NULL, NULL, PRIORITY, 0, 0); 132 | 133 | 134 | /***************************************************************************** 135 | * NOTES: 136 | * 137 | * 1. The device ID is a hard coded constant in the blink to keep the example 138 | * simple but for a real product every device should have a unique ID. 139 | * For development purposes it is possible to generate a DW1000 unique ID 140 | * by combining the Lot ID & Part Number values programmed into the 141 | * DW1000 during its manufacture. However there is no guarantee this will 142 | * not conflict with someone else's implementation. We recommended that 143 | * customers buy a block of addresses from the IEEE Registration Authority 144 | * for their production items. See "EUI" in the DW1000 User Manual. 145 | * 2. In this example, LDE microcode is not loaded upon calling dwt_initialise(). 146 | * This will prevent the IC from generating an RX timestamp. 147 | * If time-stamping is required, DWT_LOADUCODE parameter should be used. 148 | * See two-way ranging examples (e.g. examples 5a/5b). 149 | * 3. In a real application, for optimum performance within regulatory limits, 150 | * it may be necessary to set TX pulse bandwidth and TX power, (using 151 | * the dwt_configuretxrf API call) to per device calibrated values saved in 152 | * the target system or the DW1000 OTP memory. 153 | * 4. dwt_writetxdata() takes the full size of tx_msg as a parameter but only 154 | * copies (size - 2) bytes as the check-sum at the end of the frame is 155 | * automatically appended by the DW1000. This means that our tx_msg could 156 | * be two bytes shorter without losing any data (but the sizeof would not 157 | * work anymore then as we would still have to indicate the full length of 158 | * the frame to dwt_writetxdata()). 159 | * 5. We use polled mode of operation here to keep the example as simple as 160 | * possible but the TXFRS status event can be used to generate an interrupt. 161 | * Please refer to DW1000 User Manual for more details on "interrupts". 162 | * 6. The user is referred to DecaRanging ARM application (distributed with 163 | * EVK1000 product) for additional practical example of usage, and to the 164 | * DW1000 API Guide for more details on the DW1000 driver functions. 165 | ****************************************************************************/ 166 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13.1) 2 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 3 | project(twr_initiator) 4 | 5 | target_sources(app PRIVATE src/main.cpp) 6 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/README.md: -------------------------------------------------------------------------------- 1 | # initiator 2 | ## usage 3 | ```bash 4 | west build -t guiconfig -b decawave_dwm1001_dev 5 | 6 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 7 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-debug.conf" 8 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-tracing.conf" 9 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-usb.conf" 10 | 11 | west flash 12 | 13 | west flash --snr 760130093 14 | ``` 15 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/overlay-debug.conf: -------------------------------------------------------------------------------- 1 | CONFIG_BOOT_BANNER=y 2 | CONFIG_USE_SEGGER_RTT=y 3 | CONFIG_RTT_CONSOLE=y 4 | # Log => RTT, Shell => RTT 5 | 6 | # Log 7 | CONFIG_LOG=y 8 | CONFIG_LOG_BACKEND_RTT=n 9 | CONFIG_LOG_BACKEND_UART=y 10 | 11 | # Shell 12 | CONFIG_SHELL=y 13 | CONFIG_SHELL_BACKEND_RTT=y 14 | CONFIG_SHELL_BACKEND_SERIAL=n 15 | 16 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/overlay-tracing.conf: -------------------------------------------------------------------------------- 1 | #Tracing 2 | CONFIG_TRACING=y 3 | CONFIG_SEGGER_SYSTEMVIEW=y 4 | CONFIG_THREAD_NAME=y 5 | CONFIG_SEGGER_SYSTEMVIEW_BOOT_ENABLE=y 6 | 7 | # careful adjustment as long as there is enough SRAM use it for tracing 128 KB 8 | CONFIG_SEGGER_SYSVIEW_RTT_BUFFER_SIZE=131072 9 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/overlay-usb.conf: -------------------------------------------------------------------------------- 1 | # Logging 2 | CONFIG_LOG=y 3 | CONFIG_LOG_BACKEND_RTT=n 4 | CONFIG_LOG_BACKEND_UART=y 5 | CONFIG_BOOT_BANNER=y 6 | CONFIG_USE_SEGGER_RTT=n 7 | CONFIG_CONSOLE=y 8 | CONFIG_UART_CONSOLE=n 9 | CONFIG_RTT_CONSOLE=n 10 | 11 | #USB related configs 12 | CONFIG_USB=y 13 | CONFIG_USB_DEVICE_STACK=y 14 | CONFIG_USB_DEVICE_PRODUCT="Zephyr testusb sample" 15 | CONFIG_USB_UART_CONSOLE=y 16 | 17 | CONFIG_UART_INTERRUPT_DRIVEN=y 18 | CONFIG_UART_LINE_CTRL=y 19 | CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" 20 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_SPEED_OPTIMIZATIONS=y 2 | 3 | #UWB Tranceiver 4 | CONFIG_MESHPOSITION=y 5 | CONFIG_DW1000=y 6 | CONFIG_SPI=y 7 | CONFIG_GPIO=y 8 | 9 | #nRF Backend Networking 10 | CONFIG_ESB=y 11 | CONFIG_SIMPLEMESH=y 12 | 13 | #C++ 14 | CONFIG_CPLUSPLUS=y 15 | CONFIG_LIB_CPLUSPLUS=y 16 | CONFIG_STD_CPP11=n 17 | CONFIG_STD_CPP17=y 18 | 19 | # Console (printk printf) 20 | CONFIG_CONSOLE=y 21 | CONFIG_UART_CONSOLE=y 22 | CONFIG_RTT_CONSOLE=n 23 | CONFIG_STDOUT_CONSOLE=y 24 | 25 | #printf 26 | CONFIG_NEWLIB_LIBC=y 27 | CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y 28 | CONFIG_PRINTK=y 29 | 30 | #Stack 31 | CONFIG_MAIN_STACK_SIZE=2048 32 | -------------------------------------------------------------------------------- /samples/03_twr_initiator/src/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 - Frederic Mes, RTLOC 3 | * Copyright (c) 2015 - Decawave Ltd, Dublin, Ireland. 4 | * Copyright (c) 2021 - Home Smart Mesh 5 | * 6 | * This file is part of Zephyr-DWM1001. 7 | * 8 | * Zephyr-DWM1001 is free software: you can redistribute it and/or modify 9 | * it under the terms of the GNU General Public License as published by 10 | * the Free Software Foundation, either version 3 of the License, or 11 | * (at your option) any later version. 12 | * 13 | * Zephyr-DWM1001 is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with Zephyr-DWM1001. If not, see . 20 | * 21 | */ 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | // zephyr includes 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | using json = nlohmann::json; 37 | 38 | #include 39 | 40 | #define LOG_LEVEL 3 41 | #include 42 | LOG_MODULE_REGISTER(main, LOG_LEVEL_ERR); 43 | 44 | //[N] P0.13 => M_PIN17 => J7 pin 8 45 | #define DEBUG_PIN_APP 13 46 | 47 | //0.625 us per toggle 48 | #define APP_SET gpio_pin_set(gpio_dev, DEBUG_PIN_APP, 1) 49 | #define APP_CLEAR gpio_pin_set(gpio_dev, DEBUG_PIN_APP, 0) 50 | 51 | const struct device *gpio_dev; 52 | void gpio_pin_init() 53 | { 54 | gpio_dev = device_get_binding(DT_LABEL(DT_NODELABEL(gpio0))); 55 | int ret = gpio_pin_configure(gpio_dev, DEBUG_PIN_APP, GPIO_OUTPUT_ACTIVE); 56 | if (ret < 0) { 57 | LOG_ERR("gpio_pin_configure() failed"); 58 | } 59 | } 60 | 61 | /* Inter-ranging delay period, in milliseconds. */ 62 | #define RNG_DELAY_MS 500 63 | 64 | /* Default communication configuration. */ 65 | static dwt_config_t config = {5,DWT_PRF_64M,DWT_PLEN_128,DWT_PAC8,9,9,1,DWT_BR_6M8,DWT_PHRMODE_EXT,(129)}; 66 | 67 | uint8_t this_initiator_node_id = 1; 68 | uint8_t responder_node_id = 2; 69 | 70 | #define TX_ANT_DLY 16436 71 | 72 | /* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) 73 | * conversion factor. 74 | * 1 uus = 512 / 499.2 usec and 1 usec = 499.2 * 128 dtu. */ 75 | #define UUS_TO_DWT_TIME 65536 76 | 77 | 78 | 79 | /*rx twr_2_resp after tx twr_1_poll 80 | protected by responder's mp_request_at(twr_2_resp):POLL_RX_TO_RESP_TX_DLY_UUS 81 | */ 82 | #define POLL_TX_TO_RESP_RX_DLY_UUS 300 83 | 84 | #define RESP_RX_TO_FINAL_TX_DLY_UUS 1000 85 | 86 | #define STACKSIZE 2048 87 | 88 | void initiator_thread(); 89 | K_THREAD_DEFINE(initiator_main, STACKSIZE, initiator_thread, NULL, NULL, NULL, 99, 0, 0); 90 | static uint32_t reg;//force read status 91 | void initiator_thread(void) 92 | { 93 | gpio_pin_init(); 94 | APP_CLEAR; 95 | LOG_INF("initiator_thread> starting with status 0x%08x",reg); 96 | 97 | mp_start(config); 98 | APP_SET; 99 | reg = mp_get_status();//reading reg takes 40.75 us 100 | APP_CLEAR; 101 | 102 | dwt_setleds(1); 103 | k_yield(); 104 | uint8_t sequence = 0; 105 | while (1) { 106 | //APP_SET_CLEAR 107 | // - pulse1: 'request-receive : tx 1st till rx resp' ; 108 | // - pulse2: 'send_at : tx final delayed till sent' 109 | uint32_t reg1 = mp_get_status(); 110 | LOG_INF("initiator> sequence(%u) starting ; statusreg = 0x%08x",sequence,reg1); 111 | mp_rx_after_tx(POLL_TX_TO_RESP_RX_DLY_UUS,10000); 112 | 113 | msg_header_t twr_poll = {msg_id_t::twr_1_poll, sequence, this_initiator_node_id , responder_node_id,0}; 114 | APP_SET; 115 | mp_request(twr_poll); 116 | 117 | if(mp_receive(msg_id_t::twr_2_resp)) 118 | { 119 | APP_CLEAR; 120 | uint64_t poll_tx_ts = get_tx_timestamp_u64();// DWT_TIME 121 | uint64_t resp_rx_ts = get_rx_timestamp_u64();// DWT_TIME 122 | //tx res 9 bits (8 bits shit and 1 bit mask) 123 | uint32_t final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8; //tranceiver time format 124 | //prorammed TX + antenna delay 125 | uint64_t final_tx_ts = (((uint64_t)(final_tx_time & 0xFFFFFFFEUL)) << 8) + TX_ANT_DLY; //host time format 126 | 127 | msg_twr_final_t twr_final; 128 | twr_final.header = twr_poll.header;//keep same source and dest 129 | twr_final.header.id = msg_id_t::twr_3_final; 130 | twr_final.poll_tx_ts = (uint32_t)poll_tx_ts;//trunc 64 bits to 32 bits 131 | twr_final.resp_rx_ts = (uint32_t)resp_rx_ts;//trunc 64 bits to 32 bits 132 | twr_final.final_tx_ts = (uint32_t)final_tx_ts;//trunc 64 bits to 32 bits 133 | APP_SET; 134 | if(mp_send_at((uint8_t*)&twr_final, sizeof(msg_twr_final_t), final_tx_time)) 135 | { 136 | APP_CLEAR; 137 | printk("initiator> success with frame %u\n", sequence); 138 | LOG_DBG("initiator> poll_tx= 0x%08llx ; resp_rx= 0x%08llx\n", poll_tx_ts, resp_rx_ts); 139 | LOG_DBG("initiator> final_tx(ant)= 0x%08llx ; final_tx(chip)= 0x%04x\n", final_tx_ts, final_tx_time); 140 | }else{ 141 | LOG_WRN("mp_send_at(twr_3_final) fail at sequence %u",sequence); 142 | APP_CLEAR; 143 | } 144 | }else{ 145 | LOG_WRN("mp_receive(twr_2_resp) fail at sequence %u",sequence); 146 | APP_CLEAR; 147 | } 148 | uint32_t reg2 = mp_get_status(); 149 | LOG_INF("initiator> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 150 | sequence++; 151 | k_sleep(K_MSEC(RNG_DELAY_MS)); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /samples/04_twr_responder/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13.1) 2 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 3 | project(twr_listener) 4 | 5 | target_sources(app PRIVATE src/main.cpp) 6 | -------------------------------------------------------------------------------- /samples/04_twr_responder/README.md: -------------------------------------------------------------------------------- 1 | # responder 2 | ## usage 3 | ```bash 4 | west build -t guiconfig -b decawave_dwm1001_dev 5 | 6 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 7 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-debug.conf" 8 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-tracing.conf" 9 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-usb.conf" 10 | 11 | west flash 12 | 13 | west flash --snr 760130128 14 | ``` 15 | -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/1 MHz, 5 M Samples [10].logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/1 MHz, 5 M Samples [10].logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/1 MHz, 5 M Samples [13].logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/1 MHz, 5 M Samples [13].logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/SFD timeout.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/SFD timeout.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/default-.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/default-.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/initiator.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/initiator.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/optimized delay 1000-1000.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/optimized delay 1000-1000.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/reduced to 1000 us ok.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/reduced to 1000 us ok.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/sfd errors.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/sfd errors.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/captures/twr init-resp.logicdata: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/04_twr_responder/captures/twr init-resp.logicdata -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/bad_init_alone.txt: -------------------------------------------------------------------------------- 1 | *** Booting Zephyr OS build v2.6.0-rc1-120-g41e885947e4f *** 2 | 3 | 4 | rtt:~$ device_id: deca0130 5 | mp_receive(): 6 | Clock PLL Lock 7 | Transmit Frame Begins 8 | Transmit Preamble Sent 9 | Transmit PHY Header Sent 10 | Transmit Frame Sent 11 | Any TX events 12 | Receive Frame Wait Timeout 13 | User defined RX timeouts 14 | SLEEP to INIT 15 | initiator> sequence(1) over; reg1 = 0x00800002 ; reg2 = 0x008000f2 16 | Clock PLL Lock 17 | Transmit Frame Begins 18 | Transmit Preamble Sent 19 | Transmit PHY Header Sent 20 | Transmit Frame Sent 21 | Any TX events 22 | SLEEP to INIT 23 | [00:00:00.314,300] main: initiator_thread> starting 24 | rtt:~$ [00:00:00.333,709] meshposition: mp_receive() not RXFCG 25 | rtt:~$ [00:00:00.335,815] meshposition: mp_receive(8) fail 26 | rtt:~$ rtt:~$ mp_receive(): 27 | Clock PLL Lock 28 | Transmit Frame Begins 29 | Transmit Preamble Sent 30 | Transmit PHY Header Sent 31 | Transmit Frame Sent 32 | Any TX events 33 | Receive Frame Wait Timeout 34 | User defined RX timeouts 35 | SLEEP to INIT 36 | initiator> sequence(2) over; reg1 = 0x008000f2 ; reg2 = 0x008000f2 37 | Clock PLL Lock 38 | Transmit Frame Begins 39 | Transmit Preamble Sent 40 | Transmit PHY Header Sent 41 | Transmit Frame Sent 42 | Any TX events 43 | SLEEP to INIT 44 | [00:00:03.349,334] meshposition: mp_receive() not RXFCG 45 | rtt:~$ [00:00:03.351,440] meshposition: mp_receive(8) fail 46 | rtt:~$ rtt:~$ -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/bad_init_alone_2.txt: -------------------------------------------------------------------------------- 1 | *** Booting Zephyr OS build v2.6.0-rc1-120-g41e885947e4f *** 2 | 3 | 4 | rtt:~$ device_id: deca0130 5 | mp_receive(): 6 | Clock PLL Lock 7 | Transmit Frame Begins 8 | Transmit Preamble Sent 9 | Transmit PHY Header Sent 10 | Transmit Frame Sent 11 | Any TX events 12 | Receive Frame Wait Timeout 13 | User defined RX timeouts 14 | SLEEP to INIT 15 | initiator> sequence(1) over; reg1 = 0x00800002 ; reg2 = 0x008000f2 16 | Clock PLL Lock 17 | Transmit Frame Begins 18 | Transmit Preamble Sent 19 | Transmit PHY Header Sent 20 | Transmit Frame Sent 21 | Any TX events 22 | SLEEP to INIT 23 | [00:00:00.314,300] main: initiator_thread> starting 24 | rtt:~$ [00:00:00.333,709] meshposition: mp_receive() not RXFCG 25 | rtt:~$ [00:00:00.335,815] meshposition: mp_receive(8) fail 26 | rtt:~$ rtt:~$ mp_receive(): 27 | Clock PLL Lock 28 | Transmit Frame Begins 29 | Transmit Preamble Sent 30 | Transmit PHY Header Sent 31 | Transmit Frame Sent 32 | Any TX events 33 | Receive Frame Wait Timeout 34 | User defined RX timeouts 35 | SLEEP to INIT 36 | initiator> sequence(2) over; reg1 = 0x008000f2 ; reg2 = 0x008000f2 37 | Clock PLL Lock 38 | Transmit Frame Begins 39 | Transmit Preamble Sent 40 | Transmit PHY Header Sent 41 | Transmit Frame Sent 42 | Any TX events 43 | SLEEP to INIT 44 | [00:00:03.349,334] meshposition: mp_receive() not RXFCG 45 | rtt:~$ [00:00:03.351,440] meshposition: mp_receive(8) fail 46 | rtt:~$ rtt:~$ -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/bad_init_alone_3.txt: -------------------------------------------------------------------------------- 1 | *** Booting Zephyr OS build v2.6.0-rc1-120-g41e885947e4f *** 2 | 3 | 4 | rtt:~$ device_id: deca0130 5 | mp_receive() reg1 = 0x008200f2 ; reg2 = 0x008000f2 6 | Clock PLL Lock 7 | Transmit Frame Begins 8 | Transmit Preamble Sent 9 | Transmit PHY Header Sent 10 | Transmit Frame Sent 11 | Any TX events 12 | SLEEP to INIT 13 | initiator> sequence(1) over; reg1 = 0x00800002 ; reg2 = 0x008000f2 14 | Clock PLL Lock 15 | Transmit Frame Begins 16 | Transmit Preamble Sent 17 | Transmit PHY Header Sent 18 | Transmit Frame Sent 19 | Any TX events 20 | SLEEP to INIT 21 | [00:00:00.356,231] main: initiator_thread> starting 22 | rtt:~$ [00:00:00.375,610] meshposition: mp_receive() not RXFCG 23 | rtt:~$ [00:00:00.377,624] meshposition: mp_receive(8) fail 24 | rtt:~$ rtt:~$ mp_receive() reg1 = 0x008200f2 ; reg2 = 0x008000f2 25 | Clock PLL Lock 26 | Transmit Frame Begins 27 | Transmit Preamble Sent 28 | Transmit PHY Header Sent 29 | Transmit Frame Sent 30 | Any TX events 31 | SLEEP to INIT 32 | initiator> sequence(2) over; reg1 = 0x008000f2 ; reg2 = 0x008000f2 33 | Clock PLL Lock 34 | Transmit Frame Begins 35 | Transmit Preamble Sent 36 | Transmit PHY Header Sent 37 | Transmit Frame Sent 38 | Any TX events 39 | SLEEP to INIT 40 | [00:00:03.391,052] meshposition: mp_receive() not RXFCG 41 | rtt:~$ [00:00:03.393,035] meshposition: mp_receive(8) fail 42 | rtt:~$ rtt:~$ 43 | -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/badfix_init_alone.txt: -------------------------------------------------------------------------------- 1 | *** Booting Zephyr OS build v2.6.0-rc1-120-g41e885947e4f *** 2 | 3 | 4 | rtt:~$ device_id: deca0130 5 | mp_receive() reg1 = 0x008200f2 ; reg2 = 0x008000f2 6 | Clock PLL Lock 7 | Transmit Frame Begins 8 | Transmit Preamble Sent 9 | Transmit PHY Header Sent 10 | Transmit Frame Sent 11 | Any TX events 12 | SLEEP to INIT 13 | initiator> sequence(1) over; reg1 = 0x00800002 ; reg2 = 0x008000f2 14 | Clock PLL Lock 15 | Transmit Frame Begins 16 | Transmit Preamble Sent 17 | Transmit PHY Header Sent 18 | Transmit Frame Sent 19 | Any TX events 20 | SLEEP to INIT 21 | [00:00:00.274,780] main: initiator_thread> starting 22 | rtt:~$ [00:00:00.294,189] meshposition: mp_receive() not RXFCG 23 | rtt:~$ [00:00:00.296,203] meshposition: mp_receive(8) fail 24 | rtt:~$ rtt:~$ mp_receive() reg1 = 0x008200f2 ; reg2 = 0x008000f2 25 | Clock PLL Lock 26 | Transmit Frame Begins 27 | Transmit Preamble Sent 28 | Transmit PHY Header Sent 29 | Transmit Frame Sent 30 | Any TX events 31 | SLEEP to INIT 32 | initiator> sequence(2) over; reg1 = 0x00800[00:00:03.309,692] meshposition: mp_receive() not RXFCG 33 | rtt:~$ [00:00:03.311,737] meshposition: mp_receive(8) fail 34 | rtt:~$ rtt:~$ mp_receive() reg1 = 0x008200f2 ; reg2 = 0x008000f2 35 | Clock PLL Lock 36 | Transmit Frame Begins 37 | Transmit Preamble Sent 38 | Transmit PHY Header Sent 39 | Transmit Frame Sent 40 | Any TX events 41 | SLEEP to INIT 42 | initiator> sequence(3) over; reg1 = 0x008000f2 ; reg2 = 0x008000f2 43 | Clock PLL Lock 44 | Transmit Frame Begins 45 | Transmit Preamble Sent 46 | Transmit PHY Header Sent 47 | Transmit Frame Sent 48 | Any TX events 49 | SLEEP to INIT 50 | [00:00:06.329,193] meshposition: mp_receive() not RXFCG 51 | rtt:~$ [00:00:06.331,207] meshposition: mp_receive(8) fail 52 | rtt:~$ rtt:~$ -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/good_init_alone.txt: -------------------------------------------------------------------------------- 1 | *** Booting Zephyr OS build v2.6.0-rc1-120-g41e885947e4f *** 2 | 3 | 4 | rtt:~$ device_id: deca0130 5 | timeout: (rx) 6 | initiator> sequence(1) over; reg1 = 0x02800002 ; reg2 = 0x028000f2 7 | Clock PLL Lock 8 | Transmit Frame Begins 9 | Transmit Preamble Sent 10 | Transmit PHY Header Sent 11 | Transmit Frame Sent 12 | Any TX events 13 | SLEEP to INIT 14 | Clock PLL Losing Lock 15 | [00:00:00.428,710] main: initiator_thread> starting 16 | rtt:~$ rtt:~$ timeout: (rx) 17 | sequence(2) over; reg1 = 0x028000f2 ; reg2 = 0x028000f2 18 | Clock PLL Lock 19 | Transmit Frame Begins 20 | Transmit Preamble Sent 21 | Transmit PHY Header Sent 22 | Transmit Frame Sent 23 | Any TX events 24 | SLEEP to INIT 25 | Clock PLL Losing Lock 26 | timeout: (rx) 27 | -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/good_init_reg.txt: -------------------------------------------------------------------------------- 1 | 0x008000f2 2 | Clock PLL Lock 3 | Transmit Frame Begins 4 | Transmit Preamble Sent 5 | Transmit PHY Header Sent 6 | Transmit Frame Sent 7 | Any TX events 8 | SLEEP to INIT 9 | 10 | -------------------------------------------------------------------------------- /samples/04_twr_responder/logs/good_resp_reg.txt: -------------------------------------------------------------------------------- 1 | 0x00802f72 2 | Clock PLL Lock 3 | Transmit Frame Begins 4 | Transmit Preamble Sent 5 | Transmit PHY Header Sent 6 | Any TX events 7 | Receiver Preamble Detected status 8 | Receiver Start Frame Delimiter Detected. 9 | LDE processing done 10 | Transmit Error TXPUTE or HPDWARN 11 | Receiver PHY Header Detect 12 | Receiver Data Frame Ready 13 | Any double buffer events 14 | Any RX events 15 | SLEEP to INIT 16 | -------------------------------------------------------------------------------- /samples/04_twr_responder/overlay-debug.conf: -------------------------------------------------------------------------------- 1 | CONFIG_BOOT_BANNER=y 2 | CONFIG_USE_SEGGER_RTT=y 3 | CONFIG_RTT_CONSOLE=y 4 | # Log => RTT, Shell => RTT 5 | 6 | # Log 7 | CONFIG_LOG=y 8 | CONFIG_LOG_BACKEND_RTT=n 9 | CONFIG_LOG_BACKEND_UART=y 10 | 11 | # Shell 12 | CONFIG_SHELL=y 13 | CONFIG_SHELL_BACKEND_RTT=y 14 | CONFIG_SHELL_BACKEND_SERIAL=n 15 | 16 | -------------------------------------------------------------------------------- /samples/04_twr_responder/overlay-tracing.conf: -------------------------------------------------------------------------------- 1 | #Tracing 2 | CONFIG_TRACING=y 3 | CONFIG_SEGGER_SYSTEMVIEW=y 4 | CONFIG_THREAD_NAME=y 5 | CONFIG_SEGGER_SYSTEMVIEW_BOOT_ENABLE=y 6 | 7 | # careful adjustment as long as there is enough SRAM use it for tracing 128 KB 8 | CONFIG_SEGGER_SYSVIEW_RTT_BUFFER_SIZE=131072 9 | -------------------------------------------------------------------------------- /samples/04_twr_responder/overlay-usb.conf: -------------------------------------------------------------------------------- 1 | # Logging 2 | CONFIG_LOG=y 3 | CONFIG_LOG_BACKEND_RTT=n 4 | CONFIG_LOG_BACKEND_UART=y 5 | CONFIG_BOOT_BANNER=y 6 | CONFIG_USE_SEGGER_RTT=n 7 | CONFIG_CONSOLE=y 8 | CONFIG_UART_CONSOLE=n 9 | CONFIG_RTT_CONSOLE=n 10 | 11 | #USB related configs 12 | CONFIG_USB=y 13 | CONFIG_USB_DEVICE_STACK=y 14 | CONFIG_USB_DEVICE_PRODUCT="Zephyr testusb sample" 15 | CONFIG_USB_UART_CONSOLE=y 16 | 17 | CONFIG_UART_INTERRUPT_DRIVEN=y 18 | CONFIG_UART_LINE_CTRL=y 19 | CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" 20 | -------------------------------------------------------------------------------- /samples/04_twr_responder/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_SPEED_OPTIMIZATIONS=y 2 | 3 | #UWB Tranceiver 4 | CONFIG_MESHPOSITION=y 5 | CONFIG_DW1000=y 6 | CONFIG_SPI=y 7 | CONFIG_GPIO=y 8 | 9 | #nRF Backend Networking 10 | CONFIG_ESB=y 11 | CONFIG_SIMPLEMESH=y 12 | 13 | #C++ 14 | CONFIG_CPLUSPLUS=y 15 | CONFIG_LIB_CPLUSPLUS=y 16 | CONFIG_STD_CPP11=n 17 | CONFIG_STD_CPP17=y 18 | 19 | # Console (printk printf) 20 | CONFIG_CONSOLE=y 21 | CONFIG_UART_CONSOLE=y 22 | CONFIG_RTT_CONSOLE=n 23 | CONFIG_STDOUT_CONSOLE=y 24 | 25 | #printf 26 | CONFIG_NEWLIB_LIBC=y 27 | CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y 28 | CONFIG_PRINTK=y 29 | 30 | #Stack 31 | CONFIG_MAIN_STACK_SIZE=2048 32 | -------------------------------------------------------------------------------- /samples/04_twr_responder/src/main.cpp: -------------------------------------------------------------------------------- 1 | /*! ---------------------------------------------------------------------------- 2 | * @file main.c 3 | * @brief Double-sided two-way ranging (DS TWR) responder example code 4 | * 5 | * This is a simple code example which acts as the responder in a 6 | * DS TWR distance measurement exchange. This application waits for 7 | * a "poll" message (recording the RX time-stamp of the poll) 8 | * expected from the "DS TWR initiator" example code (companion to 9 | * this application), and then sends a "response" message recording 10 | * its TX time-stamp, after which it waits for a "final" message from 11 | * the initiator to complete the exchange. The final message contains 12 | * the remote initiator's time-stamps of poll TX, response RX and 13 | * final TX. With this data and the local time-stamps, (of poll RX, 14 | * response TX and final RX), this example application works out a 15 | * value for the time-of-flight over-the-air and, thus, the 16 | * estimated distance between the two devices, which it writes 17 | * to console. 18 | * 19 | * @attention 20 | * 21 | * Copyright 2015 (c) Decawave Ltd, Dublin, Ireland. 22 | * Copyright 2019 (c) Frederic Mes, RTLOC. 23 | * 24 | * All rights reserved. 25 | * 26 | * @author Decawave 27 | */ 28 | 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | // zephyr includes 39 | #include 40 | #include 41 | 42 | #include 43 | 44 | #define LOG_LEVEL 3 45 | #include 46 | //note that log level info produces more rx errors, not clear how to log tasks influences the runtime 47 | LOG_MODULE_REGISTER(main, LOG_LEVEL_ERR); 48 | 49 | //[N] P0.13 => M_PIN17 => J7 pin 8 50 | #define DEBUG_PIN_APP 13 51 | 52 | //0.625 us per toggle 53 | #define APP_SET gpio_pin_set(gpio_dev, DEBUG_PIN_APP, 1) 54 | #define APP_CLEAR gpio_pin_set(gpio_dev, DEBUG_PIN_APP, 0) 55 | 56 | const struct device *gpio_dev; 57 | void gpio_pin_init() 58 | { 59 | gpio_dev = device_get_binding(DT_LABEL(DT_NODELABEL(gpio0))); 60 | int ret = gpio_pin_configure(gpio_dev, DEBUG_PIN_APP, GPIO_OUTPUT_ACTIVE); 61 | if (ret < 0) { 62 | LOG_ERR("gpio_pin_configure() failed"); 63 | } 64 | } 65 | 66 | /* Default communication configuration. */ 67 | static dwt_config_t config = {5, DWT_PRF_64M, DWT_PLEN_128, DWT_PAC8, 9, 9, 1, DWT_BR_6M8, DWT_PHRMODE_EXT, (129) }; 68 | 69 | /* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) conversion 70 | * factor. 71 | * 1 uus = 512 / 499.2 usec and 1 usec = 499.2 * 128 dtu. 72 | */ 73 | #define UUS_TO_DWT_TIME 65536 74 | 75 | #define POLL_RX_TO_RESP_TX_DLY_UUS 1000 76 | #define RESP_TX_TO_FINAL_RX_DLY_UUS 500 77 | 78 | /* Speed of light in air, in metres per second. */ 79 | #define SPEED_OF_LIGHT 299702547 80 | 81 | #define STACKSIZE 2048 82 | 83 | void responder_thread(); 84 | K_THREAD_DEFINE(responder_main, STACKSIZE, responder_thread, NULL, NULL, NULL, 99, 0, 0); 85 | 86 | void responder_thread(void) 87 | { 88 | gpio_pin_init(); 89 | APP_CLEAR; 90 | APP_SET; 91 | APP_CLEAR; 92 | LOG_INF("responder_thread> starting"); 93 | mp_start(config); 94 | 95 | k_yield(); 96 | uint32_t sequence = 0; 97 | while (1) { 98 | //APP_SET_CLEAR 99 | // - pulse1: 'request_at : start request resp_2 tx till sent' ; 100 | // - pulse2: 'receive : pending for receive final_3' 101 | // - pulse3: 'computing distance' 102 | uint32_t reg1 = mp_get_status(); 103 | LOG_INF("responder> sequence(%u) starting ; statusreg = 0x%08x",sequence,reg1); 104 | mp_rx_now(); 105 | msg_header_t rx_poll_msg; 106 | if(mp_receive(msg_id_t::twr_1_poll,rx_poll_msg)){ 107 | uint64_t poll_rx_ts = get_rx_timestamp_u64(); 108 | 109 | mp_rx_after_tx(RESP_TX_TO_FINAL_RX_DLY_UUS,10000); 110 | 111 | uint32_t resp_tx_time = (poll_rx_ts + (POLL_RX_TO_RESP_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8; 112 | 113 | msg_header_t tx_resp_msg = { 114 | msg_id_t::twr_2_resp, (uint8_t)(rx_poll_msg.header.sequence + 1), 115 | rx_poll_msg.header.dest , rx_poll_msg.header.source,0 116 | }; 117 | APP_SET; 118 | if(mp_request_at((uint8_t*)&tx_resp_msg, sizeof(msg_header_t), resp_tx_time)){ 119 | APP_CLEAR; 120 | msg_twr_final_t final_msg; 121 | APP_SET; 122 | if(mp_receive(msg_id_t::twr_3_final,final_msg)){ 123 | APP_CLEAR; 124 | k_sleep(K_USEC(10)); 125 | APP_SET; 126 | uint64_t resp_tx_ts = get_tx_timestamp_u64(); 127 | uint64_t final_rx_ts = get_rx_timestamp_u64(); 128 | 129 | uint32_t poll_rx_ts_32 = (uint32_t)poll_rx_ts; 130 | uint32_t resp_tx_ts_32 = (uint32_t)resp_tx_ts; 131 | uint32_t final_rx_ts_32 = (uint32_t)final_rx_ts; 132 | 133 | double Ra = (double)(final_msg.resp_rx_ts - final_msg.poll_tx_ts); 134 | double Rb = (double)(final_rx_ts_32 - resp_tx_ts_32); 135 | double Da = (double)(final_msg.final_tx_ts - final_msg.resp_rx_ts); 136 | double Db = (double)(resp_tx_ts_32 - poll_rx_ts_32); 137 | int64_t tof_dtu = (int64_t)((Ra * Rb - Da * Db) / (Ra + Rb + Da + Db)); 138 | 139 | double tof = tof_dtu * DWT_TIME_UNITS; 140 | double distance = tof * SPEED_OF_LIGHT; 141 | APP_CLEAR; 142 | char dist_str[30]; 143 | sprintf(dist_str, "responder> dist (%u): %3.2lf m\n",rx_poll_msg.header.sequence, distance); 144 | printk("%s", dist_str); 145 | }else{ 146 | LOG_WRN("mp_receive(twr_3_final) fail at rx frame %u",rx_poll_msg.header.sequence); 147 | APP_CLEAR; 148 | } 149 | }else{ 150 | APP_CLEAR; 151 | LOG_WRN("mp_request_at(twr_2_resp) fail at rx frame %u",rx_poll_msg.header.sequence); 152 | } 153 | } 154 | 155 | uint32_t reg2 = mp_get_status(); 156 | LOG_INF("responder> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 157 | sequence++; 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /samples/05_listener_sm/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /samples/05_listener_sm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13.1) 2 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 3 | project(adc_battery) 4 | 5 | target_sources(app PRIVATE src/main.cpp) 6 | -------------------------------------------------------------------------------- /samples/05_listener_sm/overlay-debug.conf: -------------------------------------------------------------------------------- 1 | CONFIG_BOOT_BANNER=y 2 | CONFIG_USE_SEGGER_RTT=y 3 | CONFIG_RTT_CONSOLE=y 4 | # Log => RTT, Shell => RTT 5 | 6 | # Log 7 | CONFIG_LOG=y 8 | CONFIG_LOG_BACKEND_RTT=n 9 | CONFIG_LOG_BACKEND_UART=y 10 | 11 | # Shell 12 | CONFIG_SHELL=y 13 | CONFIG_SHELL_BACKEND_RTT=y 14 | CONFIG_SHELL_BACKEND_SERIAL=n 15 | -------------------------------------------------------------------------------- /samples/05_listener_sm/overlay-tracing.conf: -------------------------------------------------------------------------------- 1 | #Tracing 2 | CONFIG_TRACING=y 3 | CONFIG_SEGGER_SYSTEMVIEW=y 4 | CONFIG_THREAD_NAME=y 5 | CONFIG_SEGGER_SYSTEMVIEW_BOOT_ENABLE=y 6 | 7 | # careful adjustment as long as there is enough SRAM use it for tracing 128 KB 8 | CONFIG_SEGGER_SYSVIEW_RTT_BUFFER_SIZE=131072 9 | -------------------------------------------------------------------------------- /samples/05_listener_sm/overlay-usb.conf: -------------------------------------------------------------------------------- 1 | # Logging 2 | CONFIG_LOG=y 3 | CONFIG_LOG_BACKEND_RTT=n 4 | CONFIG_LOG_BACKEND_UART=y 5 | CONFIG_BOOT_BANNER=y 6 | CONFIG_USE_SEGGER_RTT=n 7 | CONFIG_CONSOLE=y 8 | CONFIG_UART_CONSOLE=n 9 | CONFIG_RTT_CONSOLE=n 10 | 11 | #USB related configs 12 | CONFIG_USB=y 13 | CONFIG_USB_DEVICE_STACK=y 14 | CONFIG_USB_DEVICE_PRODUCT="Zephyr testusb sample" 15 | CONFIG_USB_UART_CONSOLE=y 16 | 17 | CONFIG_UART_INTERRUPT_DRIVEN=y 18 | CONFIG_UART_LINE_CTRL=y 19 | CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" 20 | -------------------------------------------------------------------------------- /samples/05_listener_sm/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_SPEED_OPTIMIZATIONS=y 2 | 3 | #UWB Tranceiver 4 | CONFIG_MESHPOSITION=y 5 | CONFIG_DW1000=y 6 | CONFIG_SPI=y 7 | CONFIG_GPIO=y 8 | 9 | #nRF Backend Networking 10 | CONFIG_ESB=y 11 | CONFIG_SIMPLEMESH=y 12 | CONFIG_SM_COORDINATOR=n 13 | CONFIG_SM_SNIFFER=y 14 | 15 | #C++ 16 | CONFIG_CPLUSPLUS=y 17 | CONFIG_LIB_CPLUSPLUS=y 18 | CONFIG_STD_CPP11=n 19 | CONFIG_STD_CPP17=y 20 | 21 | # Console (printk printf) 22 | CONFIG_CONSOLE=y 23 | CONFIG_UART_CONSOLE=y 24 | CONFIG_RTT_CONSOLE=n 25 | CONFIG_STDOUT_CONSOLE=y 26 | 27 | #printf 28 | CONFIG_NEWLIB_LIBC=y 29 | CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y 30 | 31 | #Stack 32 | CONFIG_MAIN_STACK_SIZE=2048 33 | 34 | #safe silence of ABI change warning as using gcc > 7 https://github.com/nlohmann/json/issues/1861 35 | CONFIG_COMPILER_OPT="-Wno-psabi" 36 | -------------------------------------------------------------------------------- /samples/05_listener_sm/readme.md: -------------------------------------------------------------------------------- 1 | # Listener Simple Mesh 2 | ## usage 3 | ```bash 4 | west build -t guiconfig -b decawave_dwm1001_dev 5 | 6 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 7 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-debug.conf" 8 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-tracing.conf" 9 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-usb.conf" 10 | 11 | west flash 12 | 13 | 14 | west flash --snr 760130125 15 | ``` 16 | 17 | ## uwb json config 18 | sm/E2F96EB1D7A476CC{"dwt_config":{"chan":5}} 19 | 20 | `sm/E2F96EB1D7A476CC` 21 | ```json 22 | { 23 | "dwt_config":{ 24 | "chan":4 25 | } 26 | } 27 | ``` 28 | -------------------------------------------------------------------------------- /samples/05_listener_sm/sample.yaml: -------------------------------------------------------------------------------- 1 | sample: 2 | name: Simple Mesh Listener 3 | -------------------------------------------------------------------------------- /samples/05_listener_sm/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | std::string uid; 17 | std::string full_topic; 18 | json j_mesh_state, j_uwb_state,jconfig; 19 | bool do_reconfigure = false; 20 | 21 | #define STACKSIZE 2048 22 | LOG_MODULE_REGISTER(listener_main, LOG_LEVEL_DBG); 23 | 24 | void listener_thread(); 25 | K_THREAD_DEFINE(listener_main, STACKSIZE, listener_thread, NULL, NULL, NULL, 99, 0, 0); 26 | 27 | void rx_topic_json_handler(std::string &topic, json &data) 28 | { 29 | int time_stamp = k_uptime_get(); 30 | printf("%d> %s %s\n",time_stamp,topic.c_str(),data.dump().c_str()); 31 | if(data.contains("dwt_config")){ 32 | for (auto& [key, value] : data["dwt_config"].items()) { 33 | jconfig[key] = value; 34 | } 35 | do_reconfigure = true; 36 | } 37 | } 38 | 39 | void mesh_start() 40 | { 41 | #ifdef CONFIG_USB 42 | int ret; 43 | ret = usb_enable(NULL); 44 | if (ret != 0) { 45 | LOG_ERR("Failed to enable USB"); 46 | return; 47 | } 48 | #endif 49 | 50 | sm_start(); 51 | uid = sm_get_uid(); 52 | full_topic = sm_get_topic(); 53 | printf("mesh>simplemesh listener on DWM1001 UID (%s)\n",uid.c_str()); 54 | sm_set_callback_rx_json(rx_topic_json_handler); 55 | 56 | } 57 | 58 | 59 | static uint16 frame_len = 0; 60 | static uint32 status_reg = 0; 61 | #define FRAME_LEN_MAX 127 62 | static uint8 rx_buffer[FRAME_LEN_MAX]; 63 | 64 | void print_time_buffer(uint8 *rx_buffer, uint16 len) 65 | { 66 | int time_stamp = k_uptime_get(); 67 | printf("%d> ",time_stamp); 68 | for (int i = 0 ; i < frame_len; i++ ) 69 | { 70 | printf("%02x ",rx_buffer[i]); 71 | } 72 | printf("\r\n"); 73 | } 74 | 75 | void sniff_ms(int time_sec) 76 | { 77 | int start = k_uptime_get();//in msec 78 | int finish = start + (time_sec*1000); 79 | 80 | while (k_uptime_get() < finish) { 81 | memset(rx_buffer,0,FRAME_LEN_MAX); 82 | dwt_rxenable(DWT_START_RX_IMMEDIATE); 83 | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & 84 | (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) { /* spin */ }; 85 | 86 | if (status_reg & SYS_STATUS_RXFCG) { 87 | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023; 88 | if (frame_len <= FRAME_LEN_MAX) { 89 | dwt_readrxdata(rx_buffer, frame_len, 0); 90 | print_time_buffer(rx_buffer, frame_len); 91 | } 92 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); 93 | } 94 | else { 95 | json jstat = mp_status_to_json(status_reg); 96 | int time_stamp = k_uptime_get(); 97 | printf("%d> 0x%04lX :\n%s\n",time_stamp,status_reg,jstat.dump(2).c_str()); 98 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); 99 | } 100 | } 101 | } 102 | 103 | void listener_thread(void) 104 | { 105 | static dwt_config_t config = {5,DWT_PRF_64M,DWT_PLEN_128,DWT_PAC8,9,9,1,DWT_BR_6M8,DWT_PHRMODE_EXT,(129)}; 106 | 107 | mesh_start(); 108 | 109 | mp_start(config); 110 | mp_conf_to_json(config,jconfig); 111 | std::string topic = sm_get_topic(); 112 | printf("uwb_config>%s %s\n",topic.c_str(),jconfig.dump().c_str()); 113 | 114 | int loop = 0; 115 | while (1) { 116 | if(do_reconfigure){ 117 | mp_json_to_conf(jconfig,config); 118 | dwt_configure(&config); 119 | printf("new dwt config :\n%s\n",jconfig.dump(2).c_str()); 120 | mesh_bcast_json(jconfig); 121 | do_reconfigure = false; 122 | } 123 | k_sleep(K_MSEC(100)); 124 | loop++; 125 | sniff_ms(30); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13.1) 2 | find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) 3 | project(adc_battery) 4 | 5 | target_sources(app PRIVATE src/main.cpp) 6 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/overlay-debug.conf: -------------------------------------------------------------------------------- 1 | CONFIG_BOOT_BANNER=y 2 | CONFIG_USE_SEGGER_RTT=y 3 | CONFIG_RTT_CONSOLE=y 4 | # Log => RTT, Shell => RTT 5 | 6 | # Log 7 | CONFIG_LOG=y 8 | CONFIG_LOG_BACKEND_RTT=n 9 | CONFIG_LOG_BACKEND_UART=y 10 | 11 | # Shell 12 | CONFIG_SHELL=y 13 | CONFIG_SHELL_BACKEND_RTT=y 14 | CONFIG_SHELL_BACKEND_SERIAL=n 15 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/overlay-tracing.conf: -------------------------------------------------------------------------------- 1 | #Tracing 2 | CONFIG_TRACING=y 3 | CONFIG_SEGGER_SYSTEMVIEW=y 4 | CONFIG_THREAD_NAME=y 5 | CONFIG_SEGGER_SYSTEMVIEW_BOOT_ENABLE=y 6 | 7 | # careful adjustment as long as there is enough SRAM use it for tracing 128 KB 8 | CONFIG_SEGGER_SYSVIEW_RTT_BUFFER_SIZE=32768 9 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/overlay-usb.conf: -------------------------------------------------------------------------------- 1 | # Logging 2 | CONFIG_LOG=y 3 | CONFIG_LOG_BACKEND_RTT=n 4 | CONFIG_LOG_BACKEND_UART=y 5 | CONFIG_BOOT_BANNER=y 6 | CONFIG_USE_SEGGER_RTT=n 7 | CONFIG_CONSOLE=y 8 | CONFIG_UART_CONSOLE=n 9 | CONFIG_RTT_CONSOLE=n 10 | 11 | #USB related configs 12 | CONFIG_USB=y 13 | CONFIG_USB_DEVICE_STACK=y 14 | CONFIG_USB_DEVICE_PRODUCT="Zephyr testusb sample" 15 | CONFIG_USB_UART_CONSOLE=y 16 | 17 | CONFIG_UART_INTERRUPT_DRIVEN=y 18 | CONFIG_UART_LINE_CTRL=y 19 | CONFIG_UART_CONSOLE_ON_DEV_NAME="CDC_ACM_0" 20 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/prj.conf: -------------------------------------------------------------------------------- 1 | CONFIG_SPEED_OPTIMIZATIONS=y 2 | CONFIG_REBOOT=y 3 | 4 | #UWB Tranceiver 5 | CONFIG_MESHPOSITION=y 6 | CONFIG_DW1000=y 7 | CONFIG_SPI=y 8 | CONFIG_GPIO=y 9 | 10 | #nRF Backend Networking 11 | CONFIG_ESB=y 12 | CONFIG_SIMPLEMESH=y 13 | CONFIG_SM_COORDINATOR=n 14 | 15 | #C++ 16 | CONFIG_CPLUSPLUS=y 17 | CONFIG_LIB_CPLUSPLUS=y 18 | CONFIG_STD_CPP11=n 19 | CONFIG_STD_CPP17=y 20 | CONFIG_EXCEPTIONS=y 21 | 22 | # Console (printk printf) 23 | CONFIG_CONSOLE=y 24 | CONFIG_UART_CONSOLE=y 25 | CONFIG_RTT_CONSOLE=n 26 | CONFIG_STDOUT_CONSOLE=y 27 | 28 | #printf 29 | CONFIG_NEWLIB_LIBC=y 30 | CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y 31 | CONFIG_PRINTK=y 32 | 33 | #safe silence of ABI change warning as using gcc > 7 https://github.com/nlohmann/json/issues/1861 34 | CONFIG_COMPILER_OPT="-Wno-psabi" 35 | 36 | #GPIO debug are relatively low power so enabled by default 37 | CONFIG_SM_GPIO_DEBUG=y 38 | CONFIG_MP_GPIO_DEBUG=y 39 | 40 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/readme.md: -------------------------------------------------------------------------------- 1 | # Listener Simple Mesh 2 | ## usage 3 | ```bash 4 | west build -b decawave_dwm1001_dev -- -DCONF_FILE=prj.conf 5 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-debug.conf" 6 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-tracing.conf" 7 | west build -b decawave_dwm1001_dev -- -DCONF_FILE="prj.conf overlay-usb.conf" 8 | 9 | west build -t guiconfig -b decawave_dwm1001_dev 10 | 11 | west flash 12 | 13 | west flash --snr 760130093 14 | west flash --snr 760130128 15 | 16 | nrfjprog --reset --snr 760130093 17 | nrfjprog --reset --snr 760130128 18 | ``` 19 | 20 | - 760130093 => CBC216DC164B1DE8 21 | - 760130128 => 1CF6567337562176 22 | ### sys commands 23 | ```shell 24 | sm/CBC216DC164B1DE8{"sys_cmd":"reboot"} 25 | 26 | 27 | ``` 28 | 29 | ### rf commands 30 | ```shell 31 | sm/DC1119997A56350D{"rf_cmd":"ping"} 32 | sm/1CF6567337562176{"rf_cmd":"sid"} 33 | sm/Tag{"rf_cmd":"ping"} 34 | sm/2{"rf_cmd":"ping"} 35 | 36 | sm/1CF6567337562176{"rf_cmd":"target_ping","target":"CBC216DC164B1DE8"} 37 | 38 | sm/1CF6567337562176{"rf_cmd":"target_ping","target":"CBC216DC164B1DE8"} 39 | sm/E8D81FEE52C283EB{"rf_cmd":"ping"} 40 | 41 | get: 42 | sm/CBC216DC164B1DE8{"rf_cmd":"sid"} 43 | set: 44 | sm/CBC216DC164B1DE8{"rf_cmd":"sid","sid":1} 45 | 46 | ``` 47 | 48 | ### uwb commands 49 | ```shell 50 | get: 51 | sm/1CF6567337562176{"uwb_cmd":"config"} 52 | sm{"uwb_cmd":"config","chan":5} 53 | 54 | set: 55 | sm{"uwb_cmd":"config"} 56 | sm/1CF6567337562176{"uwb_cmd":"config","chan":5} 57 | 58 | sm/1CF6567337562176{"rf_cmd":"ping"} 59 | sm/1CF6567337562176{"rf_cmd":"target_ping","target":"CBC216DC164B1DE8"} 60 | sm/E8D81FEE52C283EB{"rf_cmd":"ping"} 61 | 62 | sm{"uwb_cmd":"twr","initiator":0,"responder":1,"at_ms":200} 63 | sm{"uwb_cmd":"twr","initiator":0,"responder":3,"at_ms":100} 64 | 65 | sm{"uwb_cmd":"twr","initiator":0,"responder":1,"at_ms":100,"count":3,"count_ms":20} 66 | 67 | sm{"uwb_cmd":"twr","initiator":0,"responders":[1,1],"at_ms":100,"step_ms":10} 68 | sm{"uwb_cmd":"twr","initiator":0,"responders":[1,2],"at_ms":100,"step_ms":10} 69 | sm{"uwb_cmd":"twr","initiator":0,"responders":[1,1],"at_ms":100,"step_ms":10,"count":3,"count_ms":50} 70 | 71 | 72 | sm{"uwb_cmd":"ping", "pinger":0,"target":1,"at_ms":100} 73 | sm{"uwb_cmd":"ping", "pinger":0,"target":1,"at_ms":100,"count":3,"count_ms":6} 74 | 75 | sm{"uwb_cmd":"ping", "pinger":4,"target":1,"at_ms":100} 76 | sm{"uwb_cmd":"twr","initiator":4,"responders":[0,1,2,3],"at_ms":100,"step_ms":10} 77 | sm{"uwb_cmd":"twr","initiator":4,"responders":[0,1,2,3],"at_ms":100,"step_ms":10,"count":3,"count_ms":50} 78 | 79 | sm{"uwb_cmd":"twr","initiator":0,"responders":[1],"at_ms":100,"step_ms":10} 80 | sm{"uwb_cmd":"twr","initiator":1,"responders":[8,2,7,3,9,5,4,6],"at_ms":100,"step_ms":10} 81 | sm{"uwb_cmd":"ping", "pinger":6,"target":5,"at_ms":100} 82 | 83 | 84 | sm{"uwb_cmd":"ping", "pinger":2,"target":1,"at_ms":100} 85 | resp 86 | sm/1CF6567337562176{"uwb_cmd":"cir_acc"} 87 | 88 | pinger 2 89 | sm/530BE91D3559D690{"rf_cmd":"ping"} 90 | 91 | target 1 92 | sm/1CF6567337562176{"rf_cmd":"ping"} 93 | ``` 94 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/sample.yaml: -------------------------------------------------------------------------------- 1 | sample: 2 | name: Simple Mesh Listener 3 | -------------------------------------------------------------------------------- /samples/06_uwb_node_sm/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | std::string uid,uwb_cmd; 15 | json j_uwb_cmd,jconfig,jresponse,j_sw; 16 | uint8_t source_uwb_cmd; 17 | bool do_reconfigure = false; 18 | const uint32_t g_mesh_alive_loop_sec = 20; 19 | extern bool critical_parse; 20 | 21 | #define STACKSIZE 8192 22 | LOG_MODULE_REGISTER(uwb_main, LOG_LEVEL_DBG); 23 | 24 | void uwb_thread(); 25 | K_SEM_DEFINE(sem_uwb_cmd, 0, 1); 26 | K_THREAD_DEFINE(uwb_main, STACKSIZE, uwb_thread, NULL, NULL, NULL, 10, 0, 0); 27 | 28 | #if (CONFIG_MP_GPIO_DEBUG || CONFIG_SM_GPIO_DEBUG) 29 | #include 30 | const struct device *gpio_dev; 31 | 32 | #if CONFIG_MP_GPIO_DEBUG 33 | //0.625 us per toggle 34 | #define PIN_MP_SET gpio_pin_set(gpio_dev, CONFIG_MP_PIN_APP, 1) 35 | #define PIN_MP_CLEAR gpio_pin_set(gpio_dev, CONFIG_MP_PIN_APP, 0) 36 | #else 37 | #define PIN_MP_SET 38 | #define PIN_MP_CLEAR 39 | #endif 40 | 41 | #if CONFIG_SM_GPIO_DEBUG 42 | //0.625 us per toggle 43 | #define PIN_SM_SET gpio_pin_set(gpio_dev, CONFIG_SM_PIN_APP, 1) 44 | #define PIN_SM_CLEAR gpio_pin_set(gpio_dev, CONFIG_SM_PIN_APP, 0) 45 | #else 46 | #define PIN_SM_SET 47 | #define PIN_SM_CLEAR 48 | #endif 49 | #endif 50 | 51 | void app_gpio_init() 52 | { 53 | #if (CONFIG_MP_GPIO_DEBUG || CONFIG_SM_GPIO_DEBUG) 54 | gpio_dev = device_get_binding(DT_LABEL(DT_NODELABEL(gpio0))); 55 | #if CONFIG_MP_GPIO_DEBUG 56 | gpio_pin_configure(gpio_dev, CONFIG_MP_PIN_APP, GPIO_OUTPUT_ACTIVE); 57 | twr_gpio_init(gpio_dev); 58 | PIN_MP_CLEAR; 59 | PIN_MP_SET; 60 | PIN_MP_CLEAR; 61 | #endif 62 | #if CONFIG_SM_GPIO_DEBUG 63 | gpio_pin_configure(gpio_dev, CONFIG_SM_PIN_APP, GPIO_OUTPUT_ACTIVE); 64 | sm_gpio_init(gpio_dev); 65 | PIN_SM_CLEAR; 66 | PIN_SM_SET; 67 | PIN_SM_CLEAR; 68 | #endif 69 | #endif 70 | } 71 | 72 | void report_sw_version() 73 | { 74 | mesh_bcast_json(j_sw); 75 | printf("sw>%s\n",j_sw.dump().c_str()); 76 | } 77 | 78 | void rx_topic_json_handler(uint8_t source, std::string &topic, json &data) 79 | { 80 | source_uwb_cmd = source; 81 | if(data.contains("uwb_cmd")){ 82 | uwb_cmd = data["uwb_cmd"]; 83 | j_uwb_cmd = data; 84 | k_sem_give(&sem_uwb_cmd);//giving sem to higher prio 85 | }else if(data.contains("rf_cmd")){ 86 | if(is_self(topic)){ 87 | sm_diag(data);//giving sem to higher prio 88 | } 89 | }else if(data.contains("sys_cmd")){ 90 | if(is_self(topic)){ 91 | if(data["sys_cmd"] == "reboot"){ 92 | sys_reboot(SYS_REBOOT_WARM);//param unused on ARM-M 93 | }else if(data["sys_cmd"] == "sw_version"){ 94 | report_sw_version(); 95 | } 96 | } 97 | } 98 | } 99 | 100 | void mesh_start() 101 | { 102 | std::string uid = sm_get_uid(); 103 | printf("sm>DWM1001-Dev nRF-UID (%s)\n",uid.c_str()); 104 | 105 | sm_start();//assigns uid, sid 106 | sm_set_callback_rx_json(rx_topic_json_handler); 107 | 108 | j_sw["date"] = std::string(__DATE__); 109 | j_sw["time"] = std::string(__TIME__); 110 | j_sw["commit"] = std::string(CONFIG_COMMIT_VAR); 111 | report_sw_version(); 112 | 113 | printf("sm> started\n"); 114 | } 115 | 116 | uint64_t get_default(json &data,const char*field,uint64_t default_val) 117 | { 118 | if(data.contains(field)){ 119 | return data[field]; 120 | }else{ 121 | return default_val; 122 | } 123 | } 124 | 125 | void command_twr(json &data,int sequence) 126 | { 127 | if(data.contains("initiator")){ 128 | data["initiators"].push_back(data["initiator"]); 129 | } 130 | if(data.contains("responder")){ 131 | data["responders"].push_back(data["responder"]); 132 | } 133 | uint64_t rx_delta_ms = get_default(data,"at_ms",0); 134 | uint64_t count = get_default(data,"count",1); 135 | uint64_t count_ms = get_default(data,"count_ms",0); 136 | uint64_t step_ms = get_default(data,"step_ms",0); 137 | uint8_t this_node_id = sm_get_sid(); 138 | sm_stop_rx(); 139 | int64_t start = sm_rx_sync_ms(sequence,rx_delta_ms); 140 | int lseq = 0;//local sequence for the whole request including all counts 141 | for(uint64_t i=0;itwr_command done\n"); 171 | } 172 | 173 | void command_ping(json &data,int sequence) 174 | { 175 | uint8_t pinger = data["pinger"]; 176 | uint8_t target = data["target"]; 177 | uint64_t rx_delta_ms = 0; 178 | if(data.contains("at_ms")){ 179 | rx_delta_ms = data["at_ms"]; 180 | } 181 | uint64_t count = 1; 182 | if(data.contains("count")){ 183 | count = data["count"]; 184 | } 185 | uint64_t count_ms = 0; 186 | if(data.contains("count_ms")){ 187 | count_ms = data["count_ms"]; 188 | } 189 | int lseq = 0; 190 | uint8_t this_node_id = sm_get_sid(); 191 | sm_stop_rx(); 192 | if(target == this_node_id){ 193 | int64_t start = sm_rx_sync_ms(lseq,rx_delta_ms); 194 | for(uint64_t i=0;itwr_command done (%u)->(%u)\n",pinger,target); 217 | } 218 | 219 | void uwb_thread(void) 220 | { 221 | app_gpio_init(); 222 | mesh_start();//start mesh first to get short id 223 | 224 | static dwt_config_t config = {5,DWT_PRF_64M,DWT_PLEN_128,DWT_PAC8,9,9,1,DWT_BR_6M8,DWT_PHRMODE_EXT,(129)}; 225 | mp_start(config); 226 | mp_conf_to_json(config,jconfig); 227 | printf("dwt_config>%s\n",jconfig.dump(2).c_str()); 228 | 229 | int cmd_count = 0; 230 | while (1) { 231 | if(k_sem_take(&sem_uwb_cmd,K_MSEC(100)) == 0){ 232 | if(uwb_cmd.compare("config") == 0){ 233 | for (auto& [key, value] : j_uwb_cmd.items()) { 234 | if(jconfig.contains(key)){ 235 | jconfig[key] = value; 236 | } 237 | } 238 | mp_json_to_conf(jconfig,config); 239 | dwt_configure(&config); 240 | printf("new dwt config :\n%s\n",jconfig.dump(2).c_str()); 241 | mesh_bcast_json(jconfig); 242 | } 243 | else if(uwb_cmd.compare("twr") == 0){ 244 | command_twr(j_uwb_cmd,cmd_count); 245 | }else if(uwb_cmd.compare("ping") == 0){ 246 | command_ping(j_uwb_cmd,cmd_count); 247 | }else if(uwb_cmd.compare("cir_acc") == 0){ 248 | printf("before cir_acc()\n"); 249 | uwb_cir_acc(source_uwb_cmd);//send acc to source read one more : DW1000_Software_API_Guide 250 | } 251 | cmd_count++; 252 | } 253 | if(critical_parse){ 254 | sys_reboot(SYS_REBOOT_WARM);//param unused on ARM-M 255 | } 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /samples/CMakeLists.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nRFMesh/sdk-uwb-zephyr/284334860839b077e5aa922071986c837ac0e0e3/samples/CMakeLists.txt -------------------------------------------------------------------------------- /samples/Kconfig: -------------------------------------------------------------------------------- 1 | config UWB_SAMPLES_DEFAULTS 2 | bool "Enable HSM sample default settings" 3 | default n 4 | imply LOG 5 | imply LOG_MINIMAL 6 | imply ASSERT 7 | imply ASSERT_NO_COND_INFO 8 | imply ASSERT_NO_MSG_INFO 9 | imply HW_STACK_PROTECTION if ARCH_HAS_STACK_PROTECTION 10 | help 11 | Use the default configuration for HSM samples. 12 | 13 | if LOG 14 | 15 | # LOG_DEFAULT_LEVEL is declared in Zephyr, 16 | # we declare it here for a second time, to set a different default. 17 | # Omit the `prompt` to prevent it from appearing twice in two diffent places. 18 | 19 | config LOG_DEFAULT_LEVEL 20 | default 1 if UWB_SAMPLES_DEFAULTS 21 | 22 | endif # LOG 23 | 24 | config COMMIT_VAR 25 | string "the commit" 26 | default "2af70c8037" 27 | #git rev-parse HEAD 28 | #option env="COMMIT_VAR" 29 | -------------------------------------------------------------------------------- /subsys/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory_ifdef(CONFIG_MESHPOSITION meshposition) 2 | -------------------------------------------------------------------------------- /subsys/Kconfig: -------------------------------------------------------------------------------- 1 | rsource "meshposition/Kconfig" 2 | -------------------------------------------------------------------------------- /subsys/meshposition/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | zephyr_library() 2 | zephyr_library_sources_ifdef(CONFIG_MESHPOSITION meshposition.cpp) 3 | zephyr_library_sources_ifdef(CONFIG_MESHPOSITION mesh_twr.cpp) 4 | -------------------------------------------------------------------------------- /subsys/meshposition/Kconfig: -------------------------------------------------------------------------------- 1 | menuconfig MESHPOSITION 2 | bool "Mesh Positioning Remote call library" 3 | default n 4 | depends on SIMPLEMESH 5 | depends on DW1000 6 | depends on CPLUSPLUS 7 | help 8 | Enable MESHPOSITION functionality. 9 | 10 | if MESHPOSITION 11 | 12 | config MP_GPIO_DEBUG 13 | bool "uwb and ranging gpio debug" 14 | default n 15 | help 16 | Enable gpio debug for uwb and ranging debug 17 | 18 | if MP_GPIO_DEBUG 19 | config MP_PIN_APP 20 | int "gpio debug for uwb app" 21 | default 23 22 | help 23 | The default used pin is the DWM1001-Dev kit [nRF] P0.23 => M_PIN16 => J7 pin 7 24 | endif # MP_GPIO_DEBUG 25 | endif # MESHPOSITION 26 | -------------------------------------------------------------------------------- /subsys/meshposition/mesh_twr.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | LOG_MODULE_REGISTER(mesh_twr, LOG_LEVEL_ERR); 15 | 16 | #define acc_read_bytes 4064+1 17 | uint8_t acc_buffer[acc_read_bytes];//1016 x 4 +1 18 | 19 | #ifdef CONFIG_MP_GPIO_DEBUG 20 | #include 21 | 22 | const struct device *twr_gpio_dev; 23 | //0.625 us per toggle 24 | #define PIN_MP_SET gpio_pin_set(twr_gpio_dev, CONFIG_MP_PIN_APP, 1) 25 | #define PIN_MP_CLEAR gpio_pin_set(twr_gpio_dev, CONFIG_MP_PIN_APP, 0) 26 | 27 | void twr_gpio_init(const struct device *gpio_dev) 28 | { 29 | twr_gpio_dev = gpio_dev; 30 | } 31 | #else 32 | #define PIN_MP_SET 33 | #define PIN_MP_CLEAR 34 | #endif 35 | 36 | //-------- responder params -------- 37 | /* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) conversion 38 | * factor. 39 | * 1 uus = 512 / 499.2 usec and 1 usec = 499.2 * 128 dtu. 40 | */ 41 | #define UUS_TO_DWT_TIME 65536 42 | 43 | #define POLL_RX_TO_RESP_TX_DLY_UUS 1000 44 | #define RESP_TX_TO_FINAL_RX_DLY_UUS 500 45 | 46 | /* Speed of light in air, in metres per second. */ 47 | #define SPEED_OF_LIGHT 299702547 48 | 49 | //-------- intiator params -------- 50 | #define TX_ANT_DLY 16436 51 | /*rx twr_2_resp after tx twr_1_poll 52 | protected by responder's mp_request_at(twr_2_resp):POLL_RX_TO_RESP_TX_DLY_UUS 53 | */ 54 | #define POLL_TX_TO_RESP_RX_DLY_UUS 300 55 | 56 | #define RESP_RX_TO_FINAL_TX_DLY_UUS 1000 57 | 58 | #define MAX_RX_TIMEOUT 65535 59 | #define RX_TIMEOUT_1_MS 1000 60 | 61 | //starting this sequence waits for max 65 ms 62 | void twr_respond(uint8_t sequence,uint8_t source_initiator,uint8_t dest_responder,json &res) 63 | { 64 | //PIN_MP_SET_CLEAR 65 | // - pulse1: '1st rx : entrance -> receive pending done' ; 66 | // - pulse2: 'request_at: start request resp_2 tx till sent' ; 67 | // - pulse3: 'final rx : pending for receive final_3' 68 | // - pulse4: 'computing : distance' 69 | PIN_MP_SET; 70 | //uint32_t reg1 = mp_get_status(); 71 | //LOG_INF("responder> sequence(%u) starting ; statusreg = 0x%08x",sequence,reg1); 72 | mp_rx_now(RX_TIMEOUT_1_MS); 73 | msg_header_t rx_poll_msg; 74 | if(mp_receive(msg_id_t::twr_1_poll,rx_poll_msg)){ 75 | PIN_MP_CLEAR; 76 | if(rx_poll_msg.header.dest != dest_responder){ 77 | LOG_ERR("not for this node poll responder dest(%d) expected dest(%d)",rx_poll_msg.header.dest,dest_responder); 78 | return; 79 | } 80 | if(rx_poll_msg.header.source != source_initiator){ 81 | LOG_ERR("not for this node poll responder dest(%d) expected dest(%d)",rx_poll_msg.header.dest,dest_responder); 82 | return; 83 | } 84 | uint64_t poll_rx_ts = get_rx_timestamp_u64(); 85 | 86 | mp_rx_after_tx(RESP_TX_TO_FINAL_RX_DLY_UUS,RX_TIMEOUT_1_MS); 87 | 88 | uint32_t resp_tx_time = (poll_rx_ts + (POLL_RX_TO_RESP_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8; 89 | 90 | msg_header_t tx_resp_msg = { 91 | msg_id_t::twr_2_resp, (uint8_t)(rx_poll_msg.header.sequence + 1), 92 | rx_poll_msg.header.dest , rx_poll_msg.header.source,0 93 | }; 94 | PIN_MP_SET; 95 | if(mp_request_at((uint8_t*)&tx_resp_msg, sizeof(msg_header_t), resp_tx_time)){ 96 | PIN_MP_CLEAR; 97 | msg_twr_final_t final_msg; 98 | PIN_MP_SET; 99 | if(mp_receive(msg_id_t::twr_3_final,final_msg)){ 100 | PIN_MP_CLEAR; 101 | k_sleep(K_USEC(10)); 102 | PIN_MP_SET; 103 | uint64_t resp_tx_ts = get_tx_timestamp_u64(); 104 | uint64_t final_rx_ts = get_rx_timestamp_u64(); 105 | 106 | uint32_t poll_rx_ts_32 = (uint32_t)poll_rx_ts; 107 | uint32_t resp_tx_ts_32 = (uint32_t)resp_tx_ts; 108 | uint32_t final_rx_ts_32 = (uint32_t)final_rx_ts; 109 | 110 | double Ra = (double)(final_msg.resp_rx_ts - final_msg.poll_tx_ts); 111 | double Rb = (double)(final_rx_ts_32 - resp_tx_ts_32); 112 | double Da = (double)(final_msg.final_tx_ts - final_msg.resp_rx_ts); 113 | double Db = (double)(resp_tx_ts_32 - poll_rx_ts_32); 114 | int64_t tof_dtu = (int64_t)((Ra * Rb - Da * Db) / (Ra + Rb + Da + Db)); 115 | 116 | double tof = tof_dtu * DWT_TIME_UNITS; 117 | double distance = tof * SPEED_OF_LIGHT; 118 | PIN_MP_CLEAR; 119 | char dist_str[30]; 120 | sprintf(dist_str, "%3.3lf",distance); 121 | res["range"] = dist_str; 122 | printf("responder> dist (%u): %s m\n", rx_poll_msg.header.sequence, dist_str); 123 | }else{ 124 | LOG_WRN("mp_receive(twr_3_final) fail at rx frame %u",rx_poll_msg.header.sequence); 125 | res["error"] = "twr_3_final_failed"; 126 | PIN_MP_CLEAR; 127 | } 128 | }else{ 129 | PIN_MP_CLEAR; 130 | LOG_WRN("mp_request_at(twr_2_resp) fail at rx frame %u",rx_poll_msg.header.sequence); 131 | res["error"] = "twr_2_resp_failed"; 132 | } 133 | }else{ 134 | PIN_MP_CLEAR; 135 | res["error"] = "mp_receive_1_failed"; 136 | } 137 | 138 | uint32_t reg2 = mp_get_status(); 139 | LOG_INF("responder> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 140 | } 141 | 142 | void twr_intiate(uint8_t sequence,uint8_t source_initiator,uint8_t dest_responder) 143 | { 144 | //PIN_MP_SET_CLEAR 145 | // - pulse1: 'entrance : reg read and request start tx no-wait' ; 146 | // - pulse2: 'request-receive : tx 1st till rx resp' ; 147 | // - pulse3: 'send_at : tx final delayed till sent' 148 | PIN_MP_SET; 149 | k_busy_wait(100);//allow other side reception to start (+100 us offset over sync event) 150 | //uint32_t reg1 = mp_get_status(); 151 | //LOG_INF("initiator> sequence(%u) starting ; statusreg = 0x%08x",sequence,reg1); 152 | mp_rx_after_tx(POLL_TX_TO_RESP_RX_DLY_UUS,RX_TIMEOUT_1_MS); 153 | 154 | msg_header_t twr_poll = {msg_id_t::twr_1_poll, sequence, source_initiator , dest_responder,0}; 155 | mp_request(twr_poll); 156 | PIN_MP_CLEAR; 157 | PIN_MP_SET; 158 | if(mp_receive(msg_id_t::twr_2_resp)) 159 | { 160 | PIN_MP_CLEAR; 161 | uint64_t poll_tx_ts = get_tx_timestamp_u64();// DWT_TIME 162 | uint64_t resp_rx_ts = get_rx_timestamp_u64();// DWT_TIME 163 | //tx res 9 bits (8 bits shit and 1 bit mask) 164 | uint32_t final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8; //tranceiver time format 165 | //prorammed TX + antenna delay 166 | uint64_t final_tx_ts = (((uint64_t)(final_tx_time & 0xFFFFFFFEUL)) << 8) + TX_ANT_DLY; //host time format 167 | 168 | msg_twr_final_t twr_final; 169 | twr_final.header = twr_poll.header;//keep same source and dest 170 | twr_final.header.id = msg_id_t::twr_3_final; 171 | twr_final.poll_tx_ts = (uint32_t)poll_tx_ts;//trunc 64 bits to 32 bits 172 | twr_final.resp_rx_ts = (uint32_t)resp_rx_ts;//trunc 64 bits to 32 bits 173 | twr_final.final_tx_ts = (uint32_t)final_tx_ts;//trunc 64 bits to 32 bits 174 | PIN_MP_SET; 175 | if(mp_send_at((uint8_t*)&twr_final, sizeof(msg_twr_final_t), final_tx_time)) 176 | { 177 | PIN_MP_CLEAR; 178 | printf("initiator> success with frame %u\n", sequence); 179 | LOG_DBG("initiator> poll_tx= 0x%08llx ; resp_rx= 0x%08llx\n", poll_tx_ts, resp_rx_ts); 180 | LOG_DBG("initiator> final_tx(ant)= 0x%08llx ; final_tx(chip)= 0x%04x\n", final_tx_ts, final_tx_time); 181 | }else{ 182 | LOG_WRN("mp_send_at(twr_3_final) fail at sequence %u",sequence); 183 | PIN_MP_CLEAR; 184 | } 185 | }else{ 186 | LOG_WRN("mp_receive(twr_2_resp) fail at sequence %u",sequence); 187 | PIN_MP_CLEAR; 188 | } 189 | uint32_t reg2 = mp_get_status(); 190 | LOG_INF("initiator> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 191 | } 192 | 193 | void uwb_ping(uint8_t sequence,uint8_t pinger,uint8_t target) 194 | { 195 | PIN_MP_SET; 196 | msg_header_t ping = {msg_id_t::ping, sequence, pinger , target,0}; 197 | k_busy_wait(100);//allow reception to start 198 | mp_send((uint8_t*)&ping,sizeof(msg_header_t)); 199 | uint32_t reg2 = mp_get_status(); 200 | LOG_INF("uwb_ping> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 201 | printf("uwb_ping> seq(%u)\n", sequence); 202 | PIN_MP_CLEAR; 203 | } 204 | 205 | void uwb_ping_rx(uint8_t sequence,uint8_t pinger,uint8_t target,json &res) 206 | { 207 | PIN_MP_SET; 208 | mp_rx_now(RX_TIMEOUT_1_MS); 209 | msg_header_t rx_ping_msg; 210 | if(mp_receive(msg_id_t::ping,rx_ping_msg)){ 211 | if(rx_ping_msg.header.dest != target){ 212 | LOG_ERR("ping target mismatch target(%d) expected target(%d)",rx_ping_msg.header.dest,target); 213 | return; 214 | } 215 | if(rx_ping_msg.header.source != pinger){ 216 | LOG_ERR("ping source mismatch pinger(%d) expected pinger(%d)",rx_ping_msg.header.source,pinger); 217 | return; 218 | } 219 | 220 | dwt_rxdiag_t rx_diag; 221 | dwt_readdiagnostics(&rx_diag); 222 | res["diag"]["firstPath"] = rx_diag.firstPath; 223 | res["diag"]["stdNoise"] = rx_diag.stdNoise; 224 | res["diag"]["maxNoise"] = rx_diag.maxNoise; 225 | res["diag"]["rxPreamCount"] = rx_diag.rxPreamCount; 226 | res["diag"]["maxGrowthCIR"] = rx_diag.maxGrowthCIR; 227 | res["diag"]["fpAmp1"] = rx_diag.firstPathAmp1; 228 | res["diag"]["fpAmp2"] = rx_diag.firstPathAmp2; 229 | res["diag"]["fpAmp3"] = rx_diag.firstPathAmp3; 230 | //res["seq"] = rx_ping_msg.header.sequence; 231 | printf("uwb_ping_rx> seq(%u)->(%u)\n", rx_ping_msg.header.sequence,sequence); 232 | 233 | }else{ 234 | res["diag"]["error"] = "mp_receive_ping_failed"; 235 | } 236 | 237 | uint32_t reg2 = mp_get_status(); 238 | LOG_INF("responder> sequence(%u) over; statusreg = 0x%08x",sequence,reg2); 239 | PIN_MP_CLEAR; 240 | } 241 | 242 | void uwb_cir_acc(uint8_t source) 243 | { 244 | printf("uwb_cir_acc> calling dwt function\n"); 245 | const uint16_t read_size_total = 4064; 246 | const uint16_t read_size_section = 200; 247 | 248 | PIN_MP_SET; 249 | uint8_t under_dummy_backup = 0; 250 | for(int i=0;i<20;i++){ 251 | uint16_t offset = read_size_section*i; 252 | under_dummy_backup = acc_buffer[offset]; 253 | dwt_readaccdata(acc_buffer+offset, read_size_section+1, offset); 254 | acc_buffer[offset] = under_dummy_backup; 255 | } 256 | uint16_t offset = 20*200; 257 | under_dummy_backup = acc_buffer[offset]; 258 | dwt_readaccdata(acc_buffer+offset, 64+1, offset);//last 64 from 4064 259 | acc_buffer[offset] = under_dummy_backup; 260 | PIN_MP_CLEAR; 261 | 262 | uint8_t dest = source;//response dest = request source 263 | printf("uwb_cir_acc> sending file \n"); 264 | 265 | PIN_MP_SET; 266 | mesh_send_file("uwb_cir_acc",dest,acc_buffer+1,read_size_total); 267 | PIN_MP_CLEAR; 268 | 269 | printf("uwb_cir_acc> file with %u bytes sent\n", read_size_total); 270 | } 271 | -------------------------------------------------------------------------------- /subsys/meshposition/meshposition.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | LOG_MODULE_REGISTER(meshposition, LOG_LEVEL_ERR); 16 | 17 | #define TX_ANT_DLY 16436 18 | #define RX_ANT_DLY 16436 19 | 20 | static uint32_t status_reg = 0; 21 | 22 | 23 | std::map invmap_dataRate,invmap_rxPAC,invmap_txPreambLength; 24 | 25 | std::map map_reg_status { 26 | {SYS_STATUS_IRQS, "Interrupt Request Status READ ONLY"}, 27 | {SYS_STATUS_CPLOCK, "Clock PLL Lock"}, 28 | {SYS_STATUS_ESYNCR, "External Sync Clock Reset"}, 29 | {SYS_STATUS_AAT, "Automatic Acknowledge Trigger"}, 30 | {SYS_STATUS_TXFRB, "Transmit Frame Begins"}, 31 | {SYS_STATUS_TXPRS, "Transmit Preamble Sent"}, 32 | {SYS_STATUS_TXPHS, "Transmit PHY Header Sent"}, 33 | {SYS_STATUS_TXFRS, "Transmit Frame Sent"}, 34 | {SYS_STATUS_RXPRD, "Receiver Preamble Detected status"}, 35 | {SYS_STATUS_RXSFDD, "Receiver Start Frame Delimiter Detected."}, 36 | {SYS_STATUS_LDEDONE, "LDE processing done"}, 37 | {SYS_STATUS_RXPHD, "Receiver PHY Header Detect"}, 38 | {SYS_STATUS_RXPHE, "Receiver PHY Header Error"}, 39 | {SYS_STATUS_RXDFR, "Receiver Data Frame Ready"}, 40 | {SYS_STATUS_RXFCG, "Receiver FCS Good"}, 41 | {SYS_STATUS_RXFCE, "Receiver FCS Error"}, 42 | {SYS_STATUS_RXRFSL, "Receiver Reed Solomon Frame Sync Loss"}, 43 | {SYS_STATUS_RXRFTO, "Receive Frame Wait Timeout"}, 44 | {SYS_STATUS_LDEERR, "Leading edge detection processing error"}, 45 | {SYS_STATUS_reserved, "bit19 reserved"}, 46 | {SYS_STATUS_RXOVRR, "Receiver Overrun"}, 47 | {SYS_STATUS_RXPTO, "Preamble detection timeout"}, 48 | {SYS_STATUS_GPIOIRQ, "GPIO interrupt"}, 49 | {SYS_STATUS_SLP2INIT, "SLEEP to INIT"}, 50 | {SYS_STATUS_RFPLL_LL, "RF PLL Losing Lock"}, 51 | {SYS_STATUS_CLKPLL_LL, "Clock PLL Losing Lock"}, 52 | {SYS_STATUS_RXSFDTO, "Receive SFD timeout"}, 53 | {SYS_STATUS_HPDWARN, "Half Period Delay Warning"}, 54 | {SYS_STATUS_TXBERR, "Transmit Buffer Error"}, 55 | {SYS_STATUS_AFFREJ, "Automatic Frame Filtering rejection"}, 56 | {SYS_STATUS_HSRBP, "Host Side Receive Buffer Pointer"}, 57 | {SYS_STATUS_ICRBP, "IC side Receive Buffer Pointer READ ONLY"}, 58 | {SYS_STATUS_RXRSCS, "Receiver Reed-Solomon Correction Status"}, 59 | {SYS_STATUS_RXPREJ, "Receiver Preamble Rejection"}, 60 | {SYS_STATUS_TXPUTE, "Transmit power up time error"}, 61 | {SYS_STATUS_TXERR, "Transmit Error TXPUTE or HPDWARN"}, 62 | {SYS_STATUS_ALL_RX_GOOD, "Any RX events"}, 63 | {SYS_STATUS_ALL_DBLBUFF, "Any double buffer events"}, 64 | {SYS_STATUS_ALL_RX_ERR, "Any RX errors"}, 65 | {SYS_STATUS_ALL_RX_TO, "User defined RX timeouts"}, 66 | {SYS_STATUS_ALL_TX, "Any TX events"} 67 | }; 68 | 69 | std::map map_txPreambLength { 70 | {0x0C,"DWT_PLEN_4096"}, 71 | {0x28,"DWT_PLEN_2048"}, 72 | {0x18,"DWT_PLEN_1536"}, 73 | {0x08,"DWT_PLEN_1024"}, 74 | {0x34,"DWT_PLEN_512"}, 75 | {0x24,"DWT_PLEN_256"}, 76 | {0x14,"DWT_PLEN_128"}, 77 | {0x04,"DWT_PLEN_64"} 78 | }; 79 | 80 | std::map map_rxPAC { 81 | {0,"DWT_PAC8"}, 82 | {1,"DWT_PAC16"}, 83 | {2,"DWT_PAC32"}, 84 | {3,"DWT_PAC64"} 85 | }; 86 | 87 | std::map map_dataRate { 88 | {0,"DWT_BR_110K"}, 89 | {1,"DWT_BR_850K"}, 90 | {2,"DWT_BR_6M8"} 91 | }; 92 | 93 | void inverse_map(std::map &map,std::map &invmap) 94 | { 95 | for (auto& [key, value] : map) { 96 | invmap.insert(std::make_pair(value,key)); 97 | } 98 | } 99 | 100 | std::string set_from_map_str(uint8_t &key,std::map &map) 101 | { 102 | if(map.find(key)!=map.end()){ 103 | return map[key]; 104 | }else{ 105 | return "undefined"; 106 | } 107 | } 108 | 109 | json mp_status_to_json(uint32_t status_reg) 110 | { 111 | std::list flags; 112 | for (auto& [key, value] : map_reg_status) { 113 | if(status_reg & key)flags.push_back(value); 114 | } 115 | return json(flags); 116 | } 117 | 118 | void mp_status_print(uint32_t status_reg) 119 | { 120 | for (auto& [key, value] : map_reg_status) { 121 | if(status_reg & key) 122 | { 123 | printk("%s; ",value.c_str()); 124 | } 125 | } 126 | printk("\n"); 127 | } 128 | 129 | void mp_conf_to_json(dwt_config_t &conf,json &jconf) 130 | { 131 | jconf["chan"] = conf.chan; 132 | jconf["prf"] = (conf.prf == DWT_PRF_16M)?"DWT_PRF_16M":"DWT_PRF_64M"; 133 | jconf["txPreambLength"] = set_from_map_str(conf.txPreambLength,map_txPreambLength); 134 | jconf["rxPAC"] = set_from_map_str(conf.rxPAC,map_rxPAC); 135 | jconf["txCode"] = conf.txCode; 136 | jconf["rxCode"] = conf.rxCode; 137 | jconf["nsSFD"] = (conf.nsSFD)?"NonStandard":"Standard"; 138 | jconf["dataRate"] = set_from_map_str(conf.dataRate,map_dataRate); 139 | jconf["phrMode"] = (conf.phrMode == DWT_PHRMODE_STD)?"DWT_PHRMODE_STD":"DWT_PHRMODE_EXT"; 140 | jconf["sfdTO"] = conf.sfdTO; 141 | } 142 | 143 | void mp_json_to_conf(json &jconf,dwt_config_t &conf) 144 | { 145 | conf.chan = (uint8) jconf["chan"]; 146 | conf.prf = (uint8) (jconf["prf"] == "DWT_PRF_16M")?DWT_PRF_16M:DWT_PRF_64M; 147 | conf.txPreambLength = (uint8) invmap_txPreambLength[jconf["txPreambLength"]]; 148 | conf.rxPAC = (uint8) invmap_rxPAC[jconf["rxPAC"]]; 149 | conf.txCode = (uint8) jconf["txCode"]; 150 | conf.rxCode = (uint8) jconf["rxCode"]; 151 | conf.nsSFD = (uint8) (jconf["nsSFD"]=="NonStandard")?true:false; 152 | conf.dataRate = (uint8) invmap_dataRate[jconf["dataRate"]]; 153 | conf.phrMode = (uint8) (jconf["phrMode"] == "DWT_PHRMODE_STD")?DWT_PHRMODE_STD:DWT_PHRMODE_EXT; 154 | conf.sfdTO = (uint8) jconf["sfdTO"]; 155 | } 156 | 157 | void mp_start(dwt_config_t &config) 158 | { 159 | openspi(); 160 | k_sleep(K_MSEC(2)); 161 | port_set_dw1000_slowrate(); 162 | 163 | if (dwt_initialise(DWT_LOADUCODE) == DWT_ERROR) { 164 | LOG_WRN("dwt_initialise failed"); 165 | return; 166 | } 167 | port_set_dw1000_fastrate(); 168 | 169 | dwt_configure(&config); 170 | dwt_setrxantennadelay(RX_ANT_DLY); 171 | dwt_settxantennadelay(TX_ANT_DLY); 172 | 173 | inverse_map(map_txPreambLength,invmap_txPreambLength); 174 | inverse_map(map_rxPAC,invmap_rxPAC); 175 | inverse_map(map_txPreambLength,invmap_dataRate); 176 | 177 | dwt_setleds(0); 178 | } 179 | 180 | /* RX can be started 181 | - Immediatley mp_rx_now() 182 | - After TX with a given delay mp_rx_after_tx() 183 | - At a given timestamp not api provided. uses dwt_setdelayedtrxtime() and dwt_rxenable() with DWT_START_RX_DELAYED 184 | */ 185 | void mp_rx_now(uint16_t timeout) 186 | { 187 | dwt_setrxtimeout(timeout); // 0 : disable timeout 188 | dwt_rxenable(DWT_START_RX_IMMEDIATE); 189 | } 190 | 191 | void mp_rx_after_tx(uint32_t delay_us,uint16_t timeout) 192 | { 193 | dwt_setrxaftertxdelay(delay_us); 194 | dwt_setrxtimeout(timeout); 195 | } 196 | 197 | uint32_t mp_poll_rx() 198 | { 199 | uint32_t l_status_reg; 200 | while (!((l_status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR))) 201 | { 202 | }; 203 | return l_status_reg; 204 | } 205 | 206 | uint32_t mp_get_status() 207 | { 208 | return dwt_read32bitreg(SYS_STATUS_ID); 209 | } 210 | 211 | //TODO check that we're on listening state 212 | bool mp_receive(uint8_t* data, uint16_t expected_size) 213 | { 214 | bool result = false; 215 | status_reg = mp_poll_rx(); 216 | if(status_reg & SYS_STATUS_RXFCG){ 217 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//clearing any rx and tx 218 | uint16_t size = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK; 219 | if (size == expected_size) { 220 | dwt_readrxdata(data, size, 0); 221 | result = true; 222 | }else{ 223 | LOG_ERR("rx size %u != expected_size %u",size,expected_size); 224 | } 225 | }else{ 226 | dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR);//clear errors 227 | dwt_rxreset(); 228 | LOG_WRN("mp_receive() no 'RX Frame Checksum Good'"); 229 | uint32_t reg2 = mp_get_status(); 230 | uint32_t reg_removed = (status_reg ^ reg2); 231 | LOG_WRN("mp_receive() reg1 = 0x%08x ; reg2 = 0x%08x ; removed = 0x%08x",status_reg,reg2,reg_removed); 232 | mp_status_print(reg_removed); 233 | status_reg = reg2; 234 | } 235 | 236 | return result; 237 | } 238 | 239 | bool mp_receive(msg_id_t id) 240 | { 241 | bool result = false; 242 | msg_header_t message; 243 | if(mp_receive((uint8_t*)&message, sizeof(msg_header_t))){ 244 | if(message.header.id == id){//TODO check that dest is self when id is moved as global here 245 | result = true; 246 | }else{ 247 | LOG_ERR("mp_receive() id mismatch id(%u/%u)",(uint8_t)id,(uint8_t)message.header.id); 248 | } 249 | }else{ 250 | LOG_ERR("mp_receive(%u) fail",(uint8_t)id); 251 | } 252 | return result; 253 | } 254 | 255 | bool mp_receive(msg_id_t id,msg_header_t& message) 256 | { 257 | bool result = false; 258 | if(mp_receive((uint8_t*)&message, sizeof(msg_header_t))){ 259 | if(message.header.id == id){//TODO check that dest is self when id is moved as global here 260 | result = true; 261 | }else{ 262 | LOG_ERR("mp_receive() mismatch id(%u/%u)",(uint8_t)id,(uint8_t)message.header.id); 263 | } 264 | }else{ 265 | LOG_ERR("mp_receive(%u,header) fail",(uint8_t)id); 266 | } 267 | return result; 268 | } 269 | 270 | bool mp_receive(msg_id_t id,msg_twr_final_t& final_msg) 271 | { 272 | bool result = false; 273 | if(mp_receive((uint8_t*)&final_msg, sizeof(msg_twr_final_t))){ 274 | if(final_msg.header.id == id){//TODO check that dest is self when id is moved as global here 275 | result = true; 276 | }else{ 277 | LOG_ERR("mp_receive() id mismatch(%u/%u)",(uint8_t)id,(uint8_t)final_msg.header.id); 278 | } 279 | }else{ 280 | LOG_ERR("mp_receive(%u,final_msg) fail",(uint8_t)id); 281 | } 282 | return result; 283 | } 284 | 285 | 286 | //a request expects a response 287 | void mp_request(uint8_t* data, uint16_t size) 288 | { 289 | dwt_writetxdata(size, data, 0);//0 offset 290 | dwt_writetxfctrl(size, 0, 1);//ranging bit unused by DW1000 291 | dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//switch to rx after `setrxaftertxdelay` 292 | } 293 | 294 | void mp_request(msg_header_t &message) 295 | { 296 | mp_request((uint8_t*)&message,(uint16_t)sizeof(msg_header_t)); 297 | } 298 | 299 | //no response expected 300 | void mp_send(uint8_t* data, uint16_t size) 301 | { 302 | dwt_writetxdata(size, data, 0); 303 | dwt_writetxfctrl(size, 0, 1); 304 | dwt_starttx(DWT_START_TX_IMMEDIATE); 305 | } 306 | 307 | void recover_tx_errors() 308 | { 309 | uint32_t status = dwt_read32bitreg(SYS_STATUS_ID); 310 | if(status & SYS_STATUS_TXERR) 311 | { 312 | LOG_INF("recovering TX errors 0x%08x", (status & SYS_STATUS_TXERR)); 313 | dwt_write8bitoffsetreg(SYS_CTRL_ID, SYS_CTRL_OFFSET, (uint8)SYS_CTRL_TRXOFF); 314 | } 315 | } 316 | 317 | /*! @fn mp_send_at() 318 | * @param tx_time : higher 31 bits (bit 0 ignored) from system time which is 40 bits 319 | * unit is ~ 8 ns or (1/124,8) us ~= 0.00801282... us 320 | * all _at() functions : 321 | * - The Frame RMARKER 322 | * - do not include antenna delay 323 | * - do not consider preamble time (starts earlier) 324 | */ 325 | bool mp_send_at(uint8_t* data, uint16_t size, uint64_t tx_time, uint8_t flag) 326 | { 327 | uint32_t reg1 = dwt_read32bitreg(SYS_STATUS_ID); 328 | //next tansmission timestamp (although same reg can be used for delayed reception) 329 | dwt_setdelayedtrxtime(tx_time); 330 | dwt_writetxdata(size, data, 0); 331 | dwt_writetxfctrl(size, 0, 1); 332 | bool late = dwt_starttx(DWT_START_TX_DELAYED | flag); 333 | if(late == DWT_SUCCESS){ 334 | while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS)) 335 | { 336 | }; 337 | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS); 338 | return true; 339 | }else{ 340 | uint32_t reg2 = dwt_read32bitreg(SYS_STATUS_ID); 341 | recover_tx_errors(); 342 | status_reg = dwt_read32bitreg(SYS_STATUS_ID); 343 | LOG_WRN("mp_send_at() - dwt_starttx() late"); 344 | LOG_WRN("reg1 = 0x%08x ; reg2 = 0x%08x; regnow = 0x%08x",reg1,reg2,status_reg); 345 | return false; 346 | } 347 | } 348 | 349 | bool mp_request_at(uint8_t* data, uint16_t size, uint64_t tx_time) 350 | { 351 | return mp_send_at(data,size,tx_time,DWT_RESPONSE_EXPECTED); 352 | } 353 | 354 | /*! -------------------------------------------------------------------------- 355 | * @fn get_tx_timestamp_u64() 356 | * 357 | * @brief Get the TX time-stamp in a 64-bit variable. 358 | * /!\ This function assumes that length of time-stamps is 40 bits, 359 | * for both TX and RX! 360 | * 361 | * @param none 362 | * 363 | * @return 64-bit value of the read time-stamp. 364 | */ 365 | uint64_t get_tx_timestamp_u64(void) 366 | { 367 | uint8_t ts_tab[5]; 368 | uint64_t ts = 0; 369 | int i; 370 | 371 | dwt_readtxtimestamp(ts_tab); 372 | for (i = 4; i >= 0; i--) { 373 | ts <<= 8; 374 | ts |= ts_tab[i]; 375 | } 376 | return ts; 377 | } 378 | 379 | /*! -------------------------------------------------------------------------- 380 | * @fn get_rx_timestamp_u64() 381 | * 382 | * @brief Get the RX time-stamp in a 64-bit variable. 383 | * /!\ This function assumes that length of time-stamps is 40 bits, 384 | * for both TX and RX! 385 | * 386 | * @param none 387 | * 388 | * @return 64-bit value of the read time-stamp. 389 | */ 390 | uint64_t get_rx_timestamp_u64(void) 391 | { 392 | uint8_t ts_tab[5]; 393 | uint64_t ts = 0; 394 | int i; 395 | 396 | dwt_readrxtimestamp(ts_tab); 397 | for (i = 4; i >= 0; i--) { 398 | ts <<= 8; 399 | ts |= ts_tab[i]; 400 | } 401 | return ts; 402 | } 403 | 404 | -------------------------------------------------------------------------------- /west.yml: -------------------------------------------------------------------------------- 1 | manifest: 2 | version: 0.9 3 | remotes: 4 | # Home Smart Mesh GitHub organization. 5 | - name: uwb 6 | url-base: https://github.com/nRFMesh 7 | - name: hsm 8 | url-base: https://github.com/HomeSmartMesh 9 | - name: zephyrproject 10 | url-base: https://github.com/zephyrproject-rtos 11 | 12 | defaults: 13 | remote: uwb 14 | 15 | projects: 16 | - name: zephyr 17 | repo-path: zephyr 18 | remote: zephyrproject 19 | revision: 41e885947e4f48aae0866df9e5fee657afb7c1cc 20 | import: 21 | name-whitelist: 22 | - cmsis 23 | - edtt 24 | - fatfs 25 | - hal_nordic 26 | - libmetal 27 | - littlefs 28 | - lvgl 29 | - mbedtls 30 | - mcumgr 31 | - mipi-sys-t 32 | - net-tools 33 | - nrf_hw_models 34 | - openthread 35 | - segger 36 | - name: hsm 37 | repo-path: sdk-hsm-sensortag 38 | remote: hsm 39 | revision: 3e5e872fef99f6dda543f488db864d95bc7e13bc 40 | path: modules/lib/simplemesh 41 | 42 | self: 43 | path: uwb 44 | -------------------------------------------------------------------------------- /zephyr/module.yml: -------------------------------------------------------------------------------- 1 | build: 2 | cmake: . 3 | kconfig: Kconfig.uwb 4 | --------------------------------------------------------------------------------