├── COPYING ├── LICENSE ├── README.md ├── calibration.cpp ├── calibration.h ├── cc1101-ook-final.ino ├── cc1101.cpp ├── cc1101.h ├── ccpacket.h ├── commonregs.h ├── config.h ├── docs ├── CC1101模块.pdf ├── cc1101.pdf └── wiring.jpg ├── keywords.txt ├── nvolat.h ├── panstamp.cpp ├── panstamp.h ├── register.cpp ├── register.h ├── repeater.cpp ├── repeater.h ├── spi.cpp ├── spi.h ├── swcommand.cpp ├── swcommand.h ├── swpacket.cpp ├── swpacket.h ├── swquery.cpp ├── swquery.h ├── swstatus.cpp └── swstatus.h /COPYING: -------------------------------------------------------------------------------- 1 | GNU LESSER 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 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cc1101-ook 2 | 3 | Blog post can be found at (http://tamercelik.com/) 4 | Contact: (http://tamercelik.com/iletisim/) 5 | 6 | * this code provides a simple example to TX OOK with TI CC1101. 7 | * arduino sketch included in repo. 8 | * slightly modified version of panstamp cc1101 library. 9 | 10 | **check docs/ for wiring** 11 | 12 | ## thanks 13 | - [Mike](https://github.com/miek) 14 | - [Samy](http://samy.pl/) 15 | - #rfcat 16 | 17 | -------------------------------------------------------------------------------- /calibration.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 11/13/2012 23 | */ 24 | 25 | #include "calibration.h" 26 | 27 | /** 28 | * rcOscCalibrate 29 | * 30 | * Calibrate internal RC oscillator 31 | * 32 | * Return: 33 | * true : calibration successfully done 34 | * false : unable to complete calibration 35 | */ 36 | bool rcOscCalibrate(void) 37 | { 38 | uint8_t loopCount = (0x7F / 2); 39 | uint32_t checkCount = 100; 40 | 41 | // Save current OSCCAL value 42 | uint8_t oldOsccal = OSCCAL; 43 | 44 | // Save Timer 1 settings 45 | uint8_t oldTCCR1A = TCCR1A; 46 | uint8_t oldTCCR1B = TCCR1B; 47 | 48 | // Inital OSCCAL of half its maximum 49 | OSCCAL = (0x7F / 2); 50 | 51 | // Disable timer interrupts 52 | TIMSK1 = 0; 53 | TIMSK2 = 0; 54 | 55 | // Set timer 2 to asyncronous mode (32.768KHz crystal) 56 | ASSR = (1 << AS2); 57 | 58 | // Ensure timer 1 control register A is cleared 59 | TCCR1A = 0; 60 | 61 | // Timer 2 normal operation 62 | TCCR2A = 0x00; 63 | 64 | // Start Timer 2 with no prescaling 65 | TCCR2B = (1 << CS20); 66 | 67 | // Start Timer 1 with no prescaling 68 | TCCR1B = (1 << CS10); 69 | TIFR1 |= (1 << TOV1); // Clear timer 1 overflow flag 70 | TCNT1 = 0; // Reset Timer 1 count 71 | 72 | // Enter calibration loop 73 | while (loopCount--) 74 | { 75 | TIFR2 |= (1 << TOV2); // Clear timer 2 overflow flag 76 | TCNT2 = 0; // Reset Timer 2 count 77 | 78 | // Wait for the registers to be updated for Timer 2 79 | while (ASSR & (_BV(TCN2UB) | _BV(TCR2AUB) | _BV(TCR2BUB))) 80 | { 81 | // Use timer 1 to detect presence or lack of external crystal 82 | if (TIFR1 & (1 << TOV1)) 83 | { 84 | TIFR1 |= (1 << TOV1); // Clear timer 1 overflow flag 85 | TCNT1 = 0; // Reset Timer 1 count 86 | if (--checkCount == 0) 87 | { 88 | ASSR = 0; // Disable Timer 2 89 | OSCCAL = oldOsccal; // Set factory default OSCCAL 90 | return false; // 32.768 KHz crystal is not present 91 | } 92 | } 93 | } 94 | 95 | TCCR1B = (1 << CS10); // Start Timer 1 with no prescaling 96 | TCNT1 = 0; // Reset Timer 1 count 97 | 98 | // Wait until timer 2 overflows 99 | while (!(TIFR2 & (1 << TOV2))); 100 | 101 | // Stop timer 1 so it can be read 102 | TCCR1B = 0x00; 103 | 104 | // Check timer value against ideal constant 105 | if (TCNT1 > TARGETCOUNT_MAX) // Clock is running too fast 106 | OSCCAL--; 107 | else if (TCNT1 < TARGETCOUNT_MIN) // Clock is running too slow 108 | OSCCAL++; 109 | else // Clock is OK 110 | break; 111 | } 112 | 113 | ASSR = 0; // Disable Timer 2 114 | TIFR2 |= (1 << TOV2); // Clear timer 2 overflow flag 115 | TCNT2 = 0; // Reset Timer 2 count 116 | 117 | //Revert to original Timer 1 settings 118 | TCCR1A = oldTCCR1A; 119 | TCCR1B = oldTCCR1B; 120 | 121 | /* 122 | // Read OSCCAL from EEPROM 123 | uint8_t val = EEPROM.read(0x3FF); 124 | if (val != OSCCAL) 125 | { 126 | // Store OSCCAL in the last location in EEPROM 127 | EEPROM.write(0x3FF, OSCCAL); 128 | } 129 | */ 130 | 131 | return true; 132 | } 133 | 134 | -------------------------------------------------------------------------------- /calibration.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 11/13/2012 23 | */ 24 | 25 | #ifndef _CALIBRATION_H 26 | #define _CALIBRATION_H 27 | 28 | #include "EEPROM.h" 29 | #include "nvolat.h" 30 | 31 | /** 32 | * Calibration thresholds 33 | */ 34 | #define TARGETCOUNT_MIN 62450 // Calibration target MIN 35 | #define TARGETCOUNT_MAX 62550 // Calibration target MAX 36 | 37 | /** 38 | * rcOscCalibrate 39 | * 40 | * Calibrate internal RC oscillator 41 | * 42 | * Return: 43 | * true : calibration successfully done 44 | * false : unable to complete calibration 45 | */ 46 | bool rcOscCalibrate(void); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /cc1101-ook-final.ino: -------------------------------------------------------------------------------- 1 | #include "EEPROM.h" 2 | #include "cc1101.h" 3 | 4 | CC1101 cc1101; 5 | 6 | #define LEDOUTPUT 7 7 | 8 | byte counter; 9 | byte b; 10 | //byte syncWord = 1; 11 | 12 | byte syncWord[] = {0x55, 0x55}; 13 | 14 | 15 | void blinker(){ 16 | digitalWrite(LEDOUTPUT, HIGH); 17 | delay(100); 18 | digitalWrite(LEDOUTPUT, LOW); 19 | delay(100); 20 | } 21 | 22 | void setup() 23 | { 24 | Serial.begin(9600); 25 | Serial.println("Starting CC1101..."); 26 | 27 | 28 | pinMode(LEDOUTPUT, OUTPUT); 29 | digitalWrite(LEDOUTPUT, LOW); 30 | 31 | // blink once to signal the setup 32 | blinker(); 33 | 34 | // reset the counter 35 | counter=0; 36 | Serial.println("Initialize and set registers."); 37 | 38 | 39 | cc1101.init(); 40 | 41 | Serial.println("Setting PA_TABLE."); 42 | config2(); 43 | 44 | cc1101.setSyncWord(syncWord, false); 45 | cc1101.setCarrierFreq(CFREQ_433); 46 | cc1101.disableAddressCheck(); 47 | //cc1101.setTxPowerAmp(PA_LowPower); 48 | 49 | delay(1000); 50 | 51 | Serial.print("CC1101_PARTNUM "); 52 | Serial.println(cc1101.readReg(CC1101_PARTNUM, CC1101_STATUS_REGISTER)); 53 | Serial.print("CC1101_VERSION "); 54 | Serial.println(cc1101.readReg(CC1101_VERSION, CC1101_STATUS_REGISTER)); 55 | Serial.print("CC1101_MARCSTATE "); 56 | Serial.println(cc1101.readReg(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & 0x1f); 57 | 58 | Serial.println("device initialized"); 59 | } 60 | 61 | 62 | void send_data() { 63 | 64 | CCPACKET data; 65 | byte blinkCount=counter++; 66 | 67 | /* 68 | data.length = 8; 69 | data.data[0] = B00001110; 70 | data.data[1] = B01100011; 71 | data.data[2] = B10011100; 72 | data.data[3] = B11000111; 73 | data.data[4] = B00110001; 74 | data.data[5] = B10001110; 75 | data.data[6] = B01110011; 76 | data.data[7] = B00011000; 77 | */ 78 | 79 | 80 | data.length = 13; 81 | data.data[0] = 0b10001110; 82 | data.data[1] = 0b10001110; 83 | data.data[2] = 0b10001110; 84 | data.data[3] = 0b10001110; 85 | data.data[4] = 0b10001110; 86 | data.data[5] = 0b10001110; 87 | data.data[6] = 0b10001110; 88 | data.data[7] = 0b10001110; 89 | data.data[8] = 0b11101110; 90 | data.data[9] = 0b10001000; 91 | data.data[10] = 0b10001000; 92 | data.data[11] = 0b10001000; 93 | data.data[12] = 0b10000000; 94 | 95 | if(cc1101.sendData(data)){ 96 | Serial.print(blinkCount,HEX); 97 | Serial.println(" sent ok :)"); 98 | //blinker(); 99 | }else{ 100 | Serial.println("sent failed :("); 101 | } 102 | } 103 | 104 | void loop() 105 | { 106 | send_data(); 107 | send_data(); 108 | send_data(); 109 | send_data(); 110 | send_data(); 111 | send_data(); 112 | send_data(); 113 | send_data(); 114 | send_data(); 115 | delay(2000); 116 | } 117 | 118 | 119 | void config2() 120 | { 121 | byte PA_TABLE[]= {0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00}; 122 | cc1101.writeBurstReg(CC1101_PATABLE,PA_TABLE,8); 123 | } 124 | -------------------------------------------------------------------------------- /cc1101.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "cc1101.h" 26 | #include "nvolat.h" 27 | 28 | /** 29 | * Macros 30 | */ 31 | // Select (SPI) CC1101 32 | #define cc1101_Select() bitClear(PORT_SPI_SS, BIT_SPI_SS) 33 | // Deselect (SPI) CC1101 34 | #define cc1101_Deselect() bitSet(PORT_SPI_SS, BIT_SPI_SS) 35 | // Wait until SPI MISO line goes low 36 | #define wait_Miso() while(bitRead(PORT_SPI_MISO, BIT_SPI_MISO)) 37 | // Get GDO0 pin state 38 | #define getGDO0state() bitRead(PORT_GDO0, BIT_GDO0) 39 | // Wait until GDO0 line goes high 40 | #define wait_GDO0_high() while(!getGDO0state()) 41 | // Wait until GDO0 line goes low 42 | #define wait_GDO0_low() while(getGDO0state()) 43 | 44 | /** 45 | * PATABLE 46 | */ 47 | //const byte paTable[8] = {0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60}; 48 | 49 | /** 50 | * CC1101 51 | * 52 | * Class constructor 53 | */ 54 | CC1101::CC1101(void) 55 | { 56 | paTableByte = PA_LowPower; // Priority = Low power 57 | } 58 | 59 | /** 60 | * wakeUp 61 | * 62 | * Wake up CC1101 from Power Down state 63 | */ 64 | void CC1101::wakeUp(void) 65 | { 66 | cc1101_Select(); // Select CC1101 67 | wait_Miso(); // Wait until MISO goes low 68 | cc1101_Deselect(); // Deselect CC1101 69 | } 70 | 71 | /** 72 | * writeReg 73 | * 74 | * Write single register into the CC1101 IC via SPI 75 | * 76 | * 'regAddr' Register address 77 | * 'value' Value to be writen 78 | */ 79 | void CC1101::writeReg(byte regAddr, byte value) 80 | { 81 | cc1101_Select(); // Select CC1101 82 | wait_Miso(); // Wait until MISO goes low 83 | spi.send(regAddr); // Send register address 84 | spi.send(value); // Send value 85 | cc1101_Deselect(); // Deselect CC1101 86 | } 87 | 88 | /** 89 | * writeBurstReg 90 | * 91 | * Write multiple registers into the CC1101 IC via SPI 92 | * 93 | * 'regAddr' Register address 94 | * 'buffer' Data to be writen 95 | * 'len' Data length 96 | */ 97 | void CC1101::writeBurstReg(byte regAddr, byte* buffer, byte len) 98 | { 99 | byte addr, i; 100 | 101 | addr = regAddr | WRITE_BURST; // Enable burst transfer 102 | cc1101_Select(); // Select CC1101 103 | wait_Miso(); // Wait until MISO goes low 104 | spi.send(addr); // Send register address 105 | 106 | for(i=0 ; i 0) 426 | setDevAddress(bVal, false); 427 | } 428 | 429 | /** 430 | * setPowerDownState 431 | * 432 | * Put CC1101 into power-down state 433 | */ 434 | void CC1101::setPowerDownState() 435 | { 436 | // Comming from RX state, we need to enter the IDLE state first 437 | cmdStrobe(CC1101_SIDLE); 438 | // Enter Power-down state 439 | cmdStrobe(CC1101_SPWD); 440 | } 441 | 442 | /** 443 | * sendData 444 | * 445 | * Send data packet via RF 446 | * 447 | * 'packet' Packet to be transmitted. First byte is the destination address 448 | * 449 | * Return: 450 | * True if the transmission succeeds 451 | * False otherwise 452 | */ 453 | boolean CC1101::sendData(CCPACKET packet) 454 | { 455 | byte marcState; 456 | bool res = false; 457 | 458 | // Declare to be in Tx state. This will avoid receiving packets whilst 459 | // transmitting 460 | rfState = RFSTATE_TX; 461 | 462 | // Enter RX state 463 | setRxState(); 464 | 465 | // Check that the RX state has been entered 466 | while (((marcState = readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D) 467 | { 468 | if (marcState == 0x11) // RX_OVERFLOW 469 | flushRxFifo(); // flush receive queue 470 | } 471 | 472 | delayMicroseconds(500); 473 | 474 | // Set data length at the first position of the TX FIFO 475 | // writeReg(CC1101_TXFIFO, packet.length); 476 | // Write data into the TX FIFO 477 | writeBurstReg(CC1101_TXFIFO, packet.data, packet.length); 478 | 479 | // CCA enabled: will enter TX state only if the channel is clear 480 | setTxState(); 481 | 482 | // Check that TX state is being entered (state = RXTX_SETTLING) 483 | marcState = readStatusReg(CC1101_MARCSTATE) & 0x1F; 484 | if((marcState != 0x13) && (marcState != 0x14) && (marcState != 0x15)) 485 | { 486 | setIdleState(); // Enter IDLE state 487 | flushTxFifo(); // Flush Tx FIFO 488 | setRxState(); // Back to RX state 489 | 490 | // Declare to be in Rx state 491 | rfState = RFSTATE_RX; 492 | return false; 493 | } 494 | 495 | // Wait for the sync word to be transmitted 496 | wait_GDO0_high(); 497 | 498 | // Wait until the end of the packet transmission 499 | wait_GDO0_low(); 500 | 501 | // Check that the TX FIFO is empty 502 | if((readStatusReg(CC1101_TXBYTES) & 0x7F) == 0) 503 | res = true; 504 | 505 | setIdleState(); // Enter IDLE state 506 | flushTxFifo(); // Flush Tx FIFO 507 | 508 | // Enter back into RX state 509 | setRxState(); 510 | 511 | // Declare to be in Rx state 512 | rfState = RFSTATE_RX; 513 | 514 | return res; 515 | } 516 | 517 | /** 518 | * receiveData 519 | * 520 | * Read data packet from RX FIFO 521 | * 522 | * 'packet' Container for the packet received 523 | * 524 | * Return: 525 | * Amount of bytes received 526 | */ 527 | byte CC1101::receiveData(CCPACKET * packet) 528 | { 529 | byte val; 530 | byte rxBytes = readStatusReg(CC1101_RXBYTES); 531 | 532 | // Any byte waiting to be read and no overflow? 533 | if (rxBytes & 0x7F && !(rxBytes & 0x80)) 534 | { 535 | // Read data length 536 | packet->length = readConfigReg(CC1101_RXFIFO); 537 | // If packet is too long 538 | if (packet->length > CC1101_DATA_LEN) 539 | packet->length = 0; // Discard packet 540 | else 541 | { 542 | // Read data packet 543 | readBurstReg(packet->data, CC1101_RXFIFO, packet->length); 544 | // Read RSSI 545 | packet->rssi = readConfigReg(CC1101_RXFIFO); 546 | // Read LQI and CRC_OK 547 | val = readConfigReg(CC1101_RXFIFO); 548 | packet->lqi = val & 0x7F; 549 | packet->crc_ok = bitRead(val, 7); 550 | } 551 | } 552 | else 553 | packet->length = 0; 554 | 555 | setIdleState(); // Enter IDLE state 556 | flushRxFifo(); // Flush Rx FIFO 557 | //cmdStrobe(CC1101_SCAL); 558 | 559 | // Back to RX state 560 | setRxState(); 561 | 562 | return packet->length; 563 | } 564 | 565 | -------------------------------------------------------------------------------- /cc1101.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _CC1101_H 26 | #define _CC1101_H 27 | 28 | #include "Arduino.h" 29 | #include "spi.h" 30 | #include "ccpacket.h" 31 | 32 | /** 33 | * Carrier frequencies 34 | */ 35 | enum CFREQ 36 | { 37 | CFREQ_868 = 0, 38 | CFREQ_915, 39 | CFREQ_433, 40 | CFREQ_LAST 41 | }; 42 | 43 | /** 44 | * RF STATES 45 | */ 46 | enum RFSTATE 47 | { 48 | RFSTATE_IDLE = 0, 49 | RFSTATE_RX, 50 | RFSTATE_TX 51 | }; 52 | 53 | 54 | /** 55 | * Frequency channels 56 | */ 57 | #define NUMBER_OF_FCHANNELS 10 58 | 59 | /** 60 | * Type of transfers 61 | */ 62 | #define WRITE_BURST 0x40 63 | #define READ_SINGLE 0x80 64 | #define READ_BURST 0xC0 65 | 66 | /** 67 | * Type of register 68 | */ 69 | #define CC1101_CONFIG_REGISTER READ_SINGLE 70 | #define CC1101_STATUS_REGISTER READ_BURST 71 | 72 | /** 73 | * PATABLE & FIFO's 74 | */ 75 | #define CC1101_PATABLE 0x3E // PATABLE address 76 | #define CC1101_TXFIFO 0x3F // TX FIFO address 77 | #define CC1101_RXFIFO 0x3F // RX FIFO address 78 | 79 | /** 80 | * Command strobes 81 | */ 82 | #define CC1101_SRES 0x30 // Reset CC1101 chip 83 | #define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). If in RX (with CCA): 84 | // Go to a wait state where only the synthesizer is running (for quick RX / TX turnaround). 85 | #define CC1101_SXOFF 0x32 // Turn off crystal oscillator 86 | #define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off. SCAL can be strobed from IDLE mode without 87 | // setting manual calibration mode (MCSM0.FS_AUTOCAL=0) 88 | #define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and MCSM0.FS_AUTOCAL=1 89 | #define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if MCSM0.FS_AUTOCAL=1. 90 | // If in RX state and CCA is enabled: Only go to TX if channel is clear 91 | #define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit Wake-On-Radio mode if applicable 92 | #define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) as described in Section 19.5 if 93 | // WORCTRL.RC_PD=0 94 | #define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high 95 | #define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. Only issue SFRX in IDLE or RXFIFO_OVERFLOW states 96 | #define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. Only issue SFTX in IDLE or TXFIFO_UNDERFLOW states 97 | #define CC1101_SWORRST 0x3C // Reset real time clock to Event1 value 98 | #define CC1101_SNOP 0x3D // No operation. May be used to get access to the chip status byte 99 | 100 | /** 101 | * CC1101 configuration registers 102 | */ 103 | #define CC1101_IOCFG2 0x00 // GDO2 Output Pin Configuration 104 | #define CC1101_IOCFG1 0x01 // GDO1 Output Pin Configuration 105 | #define CC1101_IOCFG0 0x02 // GDO0 Output Pin Configuration 106 | #define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO Thresholds 107 | #define CC1101_SYNC1 0x04 // Sync Word, High Byte 108 | #define CC1101_SYNC0 0x05 // Sync Word, Low Byte 109 | #define CC1101_PKTLEN 0x06 // Packet Length 110 | #define CC1101_PKTCTRL1 0x07 // Packet Automation Control 111 | #define CC1101_PKTCTRL0 0x08 // Packet Automation Control 112 | #define CC1101_ADDR 0x09 // Device Address 113 | #define CC1101_CHANNR 0x0A // Channel Number 114 | #define CC1101_FSCTRL1 0x0B // Frequency Synthesizer Control 115 | #define CC1101_FSCTRL0 0x0C // Frequency Synthesizer Control 116 | #define CC1101_FREQ2 0x0D // Frequency Control Word, High Byte 117 | #define CC1101_FREQ1 0x0E // Frequency Control Word, Middle Byte 118 | #define CC1101_FREQ0 0x0F // Frequency Control Word, Low Byte 119 | #define CC1101_MDMCFG4 0x10 // Modem Configuration 120 | #define CC1101_MDMCFG3 0x11 // Modem Configuration 121 | #define CC1101_MDMCFG2 0x12 // Modem Configuration 122 | #define CC1101_MDMCFG1 0x13 // Modem Configuration 123 | #define CC1101_MDMCFG0 0x14 // Modem Configuration 124 | #define CC1101_DEVIATN 0x15 // Modem Deviation Setting 125 | #define CC1101_MCSM2 0x16 // Main Radio Control State Machine Configuration 126 | #define CC1101_MCSM1 0x17 // Main Radio Control State Machine Configuration 127 | #define CC1101_MCSM0 0x18 // Main Radio Control State Machine Configuration 128 | #define CC1101_FOCCFG 0x19 // Frequency Offset Compensation Configuration 129 | #define CC1101_BSCFG 0x1A // Bit Synchronization Configuration 130 | #define CC1101_AGCCTRL2 0x1B // AGC Control 131 | #define CC1101_AGCCTRL1 0x1C // AGC Control 132 | #define CC1101_AGCCTRL0 0x1D // AGC Control 133 | #define CC1101_WOREVT1 0x1E // High Byte Event0 Timeout 134 | #define CC1101_WOREVT0 0x1F // Low Byte Event0 Timeout 135 | #define CC1101_WORCTRL 0x20 // Wake On Radio Control 136 | #define CC1101_FREND1 0x21 // Front End RX Configuration 137 | #define CC1101_FREND0 0x22 // Front End TX Configuration 138 | #define CC1101_FSCAL3 0x23 // Frequency Synthesizer Calibration 139 | #define CC1101_FSCAL2 0x24 // Frequency Synthesizer Calibration 140 | #define CC1101_FSCAL1 0x25 // Frequency Synthesizer Calibration 141 | #define CC1101_FSCAL0 0x26 // Frequency Synthesizer Calibration 142 | #define CC1101_RCCTRL1 0x27 // RC Oscillator Configuration 143 | #define CC1101_RCCTRL0 0x28 // RC Oscillator Configuration 144 | #define CC1101_FSTEST 0x29 // Frequency Synthesizer Calibration Control 145 | #define CC1101_PTEST 0x2A // Production Test 146 | #define CC1101_AGCTEST 0x2B // AGC Test 147 | #define CC1101_TEST2 0x2C // Various Test Settings 148 | #define CC1101_TEST1 0x2D // Various Test Settings 149 | #define CC1101_TEST0 0x2E // Various Test Settings 150 | 151 | /** 152 | * Status registers 153 | */ 154 | #define CC1101_PARTNUM 0x30 // Chip ID 155 | #define CC1101_VERSION 0x31 // Chip ID 156 | #define CC1101_FREQEST 0x32 // Frequency Offset Estimate from Demodulator 157 | #define CC1101_LQI 0x33 // Demodulator Estimate for Link Quality 158 | #define CC1101_RSSI 0x34 // Received Signal Strength Indication 159 | #define CC1101_MARCSTATE 0x35 // Main Radio Control State Machine State 160 | #define CC1101_WORTIME1 0x36 // High Byte of WOR Time 161 | #define CC1101_WORTIME0 0x37 // Low Byte of WOR Time 162 | #define CC1101_PKTSTATUS 0x38 // Current GDOx Status and Packet Status 163 | #define CC1101_VCO_VC_DAC 0x39 // Current Setting from PLL Calibration Module 164 | #define CC1101_TXBYTES 0x3A // Underflow and Number of Bytes 165 | #define CC1101_RXBYTES 0x3B // Overflow and Number of Bytes 166 | #define CC1101_RCCTRL1_STATUS 0x3C // Last RC Oscillator Calibration Result 167 | #define CC1101_RCCTRL0_STATUS 0x3D // Last RC Oscillator Calibration Result 168 | 169 | //#define CC1101_DEFVAL_IOCFG2 0x29 // GDO2 Output Pin Configuration 170 | #define CC1101_DEFVAL_IOCFG2 0x2E // GDO2 Output Pin Configuration 171 | #define CC1101_DEFVAL_IOCFG1 0x2E // GDO1 Output Pin Configuration 172 | #define CC1101_DEFVAL_IOCFG0 0x06 // GDO0 Output Pin Configuration 173 | #define CC1101_DEFVAL_FIFOTHR 0x47 // RX FIFO and TX FIFO Thresholds 174 | #define CC1101_DEFVAL_SYNC1 0xD3 // Synchronization word, high byte 175 | #define CC1101_DEFVAL_SYNC0 0x91 // Synchronization word, low byte 176 | #define CC1101_DEFVAL_PKTLEN 0x12 // Packet Length 177 | #define CC1101_DEFVAL_PKTCTRL1 0x04 // Packet Automation Control 178 | #define CC1101_DEFVAL_PKTCTRL0 0x00 // Packet Automation Control 179 | #define CC1101_DEFVAL_ADDR 0x11 // Device Address 180 | #define CC1101_DEFVAL_CHANNR 0x00 // Channel Number 181 | #define CC1101_DEFVAL_FSCTRL1 0x06 // Frequency Synthesizer Control 182 | #define CC1101_DEFVAL_FSCTRL0 0x00 // Frequency Synthesizer Control 183 | // Carrier frequency = 868 MHz 184 | #define CC1101_DEFVAL_FREQ2_868 0x21 // Frequency Control Word, High Byte 185 | #define CC1101_DEFVAL_FREQ1_868 0x62 // Frequency Control Word, Middle Byte 186 | #define CC1101_DEFVAL_FREQ0_868 0x76 // Frequency Control Word, Low Byte 187 | // Carrier frequency = 902 MHz 188 | #define CC1101_DEFVAL_FREQ2_915 0x22 // Frequency Control Word, High Byte 189 | #define CC1101_DEFVAL_FREQ1_915 0xB1 // Frequency Control Word, Middle Byte 190 | #define CC1101_DEFVAL_FREQ0_915 0x3B // Frequency Control Word, Low Byte 191 | // Carrier frequency = 433 MHz 192 | #define CC1101_DEFVAL_FREQ2_433 0x10 // Frequency Control Word, High Byte 193 | #define CC1101_DEFVAL_FREQ1_433 0xA7 // Frequency Control Word, Middle Byte 194 | #define CC1101_DEFVAL_FREQ0_433 0x62 // Frequency Control Word, Low Byte 195 | 196 | #define CC1101_DEFVAL_MDMCFG4 0xE5 // Modem Configuration 197 | #define CC1101_DEFVAL_MDMCFG3 0xC3 // Modem Configuration 198 | #define CC1101_DEFVAL_MDMCFG2 0x30 // Modem Configuration 199 | #define CC1101_DEFVAL_MDMCFG1 0x22 // Modem Configuration 200 | #define CC1101_DEFVAL_MDMCFG0 0xF8 // Modem Configuration 201 | #define CC1101_DEFVAL_DEVIATN 0x15 // Modem Deviation Setting 202 | #define CC1101_DEFVAL_MCSM2 0x07 // Main Radio Control State Machine Configuration 203 | //#define CC1101_DEFVAL_MCSM1 0x30 // Main Radio Control State Machine Configuration 204 | #define CC1101_DEFVAL_MCSM1 0x20 // Main Radio Control State Machine Configuration 205 | #define CC1101_DEFVAL_MCSM0 0x18 // Main Radio Control State Machine Configuration 206 | #define CC1101_DEFVAL_FOCCFG 0x16 // Frequency Offset Compensation Configuration 207 | #define CC1101_DEFVAL_BSCFG 0x6C // Bit Synchronization Configuration 208 | #define CC1101_DEFVAL_AGCCTRL2 0x03 // AGC Control 209 | #define CC1101_DEFVAL_AGCCTRL1 0x00 // AGC Control 210 | #define CC1101_DEFVAL_AGCCTRL0 0x91 // AGC Control 211 | #define CC1101_DEFVAL_WOREVT1 0x87 // High Byte Event0 Timeout 212 | #define CC1101_DEFVAL_WOREVT0 0x6B // Low Byte Event0 Timeout 213 | #define CC1101_DEFVAL_WORCTRL 0xFB // Wake On Radio Control 214 | #define CC1101_DEFVAL_FREND1 0x56 // Front End RX Configuration 215 | #define CC1101_DEFVAL_FREND0 0x11 // Front End TX Configuration 216 | #define CC1101_DEFVAL_FSCAL3 0xE9 // Frequency Synthesizer Calibration 217 | #define CC1101_DEFVAL_FSCAL2 0x2A // Frequency Synthesizer Calibration 218 | #define CC1101_DEFVAL_FSCAL1 0x00 // Frequency Synthesizer Calibration 219 | #define CC1101_DEFVAL_FSCAL0 0x1F // Frequency Synthesizer Calibration 220 | #define CC1101_DEFVAL_RCCTRL1 0x41 // RC Oscillator Configuration 221 | #define CC1101_DEFVAL_RCCTRL0 0x00 // RC Oscillator Configuration 222 | #define CC1101_DEFVAL_FSTEST 0x59 // Frequency Synthesizer Calibration Control 223 | #define CC1101_DEFVAL_PTEST 0x7F // Production Test 224 | #define CC1101_DEFVAL_AGCTEST 0x3F // AGC Test 225 | #define CC1101_DEFVAL_TEST2 0x88 // Various Test Settings 226 | #define CC1101_DEFVAL_TEST1 0x31 // Various Test Settings 227 | #define CC1101_DEFVAL_TEST0 0x0B // Various Test Settings 228 | 229 | /** 230 | * Macros 231 | */ 232 | // Read CC1101 Config register 233 | #define readConfigReg(regAddr) readReg(regAddr, CC1101_CONFIG_REGISTER) 234 | // Read CC1101 Status register 235 | #define readStatusReg(regAddr) readReg(regAddr, CC1101_STATUS_REGISTER) 236 | // Enter Rx state 237 | #define setRxState() cmdStrobe(CC1101_SRX) 238 | // Enter Tx state 239 | #define setTxState() cmdStrobe(CC1101_STX) 240 | // Enter IDLE state 241 | #define setIdleState() cmdStrobe(CC1101_SIDLE) 242 | // Flush Rx FIFO 243 | #define flushRxFifo() cmdStrobe(CC1101_SFRX) 244 | // Flush Tx FIFO 245 | #define flushTxFifo() cmdStrobe(CC1101_SFTX) 246 | // Disable address check 247 | #define disableAddressCheck() writeReg(CC1101_PKTCTRL1, 0x04) 248 | // Enable address check 249 | #define enableAddressCheck() writeReg(CC1101_PKTCTRL1, 0x06) 250 | // Disable CCA 251 | #define disableCCA() writeReg(CC1101_MCSM1, 0) 252 | // Enable CCA 253 | #define enableCCA() writeReg(CC1101_MCSM1, CC1101_DEFVAL_MCSM1) 254 | // Set PATABLE single byte 255 | #define setTxPowerAmp(setting) paTableByte = setting 256 | // PATABLE values 257 | #define PA_LowPower 0x60 258 | #define PA_LongDistance 0xC0 259 | 260 | /** 261 | * Class: CC1101 262 | * 263 | * Description: 264 | * CC1101 interface 265 | */ 266 | class CC1101 267 | { 268 | private: 269 | /** 270 | * Atmega's SPI interface 271 | */ 272 | SPI spi; 273 | 274 | /** 275 | * writeBurstReg 276 | * 277 | * Write multiple registers into the CC1101 IC via SPI 278 | * 279 | * 'regAddr' Register address 280 | * 'buffer' Data to be writen 281 | * 'len' Data length 282 | */ 283 | //void writeBurstReg(byte regAddr, byte* buffer, byte len); 284 | 285 | /** 286 | * readBurstReg 287 | * 288 | * Read burst data from CC1101 via SPI 289 | * 290 | * 'buffer' Buffer where to copy the result to 291 | * 'regAddr' Register address 292 | * 'len' Data length 293 | */ 294 | void readBurstReg(byte * buffer, byte regAddr, byte len); 295 | 296 | /** 297 | * setDefaultRegs 298 | * 299 | * Configure CC1101 registers 300 | */ 301 | void setDefaultRegs(void); 302 | 303 | /** 304 | * setRegsFromEeprom 305 | * 306 | * Set registers from EEPROM 307 | */ 308 | void setRegsFromEeprom(void); 309 | 310 | public: 311 | /* 312 | * RF state 313 | */ 314 | byte rfState; 315 | 316 | /** 317 | * Tx Power byte (single PATABLE config) 318 | */ 319 | byte paTableByte; 320 | 321 | /** 322 | * Carrier frequency 323 | */ 324 | byte carrierFreq; 325 | 326 | /** 327 | * Frequency channel 328 | */ 329 | byte channel; 330 | 331 | /** 332 | * Synchronization word 333 | */ 334 | byte syncWord[2]; 335 | 336 | /** 337 | * Device address 338 | */ 339 | byte devAddress; 340 | 341 | /** 342 | * CC1101 343 | * 344 | * Class constructor 345 | */ 346 | CC1101(void); 347 | 348 | void writeBurstReg(byte regAddr, byte* buffer, byte len); 349 | 350 | /** 351 | * cmdStrobe 352 | * 353 | * Send command strobe to the CC1101 IC via SPI 354 | * 355 | * 'cmd' Command strobe 356 | */ 357 | void cmdStrobe(byte cmd); 358 | 359 | /** 360 | * wakeUp 361 | * 362 | * Wake up CC1101 from Power Down state 363 | */ 364 | void wakeUp(void); 365 | 366 | /** 367 | * readReg 368 | * 369 | * Read CC1101 register via SPI 370 | * 371 | * 'regAddr' Register address 372 | * 'regType' Type of register: CC1101_CONFIG_REGISTER or CC1101_STATUS_REGISTER 373 | * 374 | * Return: 375 | * Data byte returned by the CC1101 IC 376 | */ 377 | byte readReg(byte regAddr, byte regType); 378 | 379 | /** 380 | * writeReg 381 | * 382 | * Write single register into the CC1101 IC via SPI 383 | * 384 | * 'regAddr' Register address 385 | * 'value' Value to be writen 386 | */ 387 | void writeReg(byte regAddr, byte value); 388 | 389 | /** 390 | * reset 391 | * 392 | * Reset CC1101 393 | */ 394 | void reset(void); 395 | 396 | /** 397 | * init 398 | * 399 | * Initializa CC1101 400 | */ 401 | void init(void); 402 | 403 | /** 404 | * setSyncWord 405 | * 406 | * Set synchronization word 407 | * 408 | * 'syncH' Synchronization word - High byte 409 | * 'syncL' Synchronization word - Low byte 410 | * 'save' If TRUE, save parameter in EEPROM 411 | */ 412 | void setSyncWord(uint8_t syncH, uint8_t syncL, bool save=true); 413 | 414 | /** 415 | * setSyncWord (overriding method) 416 | * 417 | * Set synchronization word 418 | * 419 | * 'syncH' Synchronization word - pointer to 2-byte array 420 | * 'save' If TRUE, save parameter in EEPROM 421 | */ 422 | void setSyncWord(byte *sync, bool save=true); 423 | 424 | /** 425 | * setDevAddress 426 | * 427 | * Set device address 428 | * 429 | * 'addr' Device address 430 | * 'save' If TRUE, save parameter in EEPROM 431 | */ 432 | void setDevAddress(byte addr, bool save=true); 433 | 434 | /** 435 | * setCarrierFreq 436 | * 437 | * Set carrier frequency 438 | * 439 | * 'freq' New carrier frequency 440 | */ 441 | void setCarrierFreq(byte freq); 442 | 443 | /** 444 | * setChannel 445 | * 446 | * Set frequency channel 447 | * 448 | * 'chnl' Frequency channel 449 | * 'save' If TRUE, save parameter in EEPROM 450 | */ 451 | void setChannel(byte chnl, bool save=true); 452 | 453 | /** 454 | * setPowerDownState 455 | * 456 | * Put CC1101 into power-down state 457 | */ 458 | void setPowerDownState(); 459 | 460 | /** 461 | * sendData 462 | * 463 | * Send data packet via RF 464 | * 465 | * 'packet' Packet to be transmitted. First byte is the destination address 466 | * 467 | * Return: 468 | * True if the transmission succeeds 469 | * False otherwise 470 | */ 471 | boolean sendData(CCPACKET packet); 472 | 473 | /** 474 | * receiveData 475 | * 476 | * Read data packet from RX FIFO 477 | * 478 | * Return: 479 | * Amount of bytes received 480 | */ 481 | byte receiveData(CCPACKET *packet); 482 | }; 483 | 484 | #endif 485 | 486 | -------------------------------------------------------------------------------- /ccpacket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _CCPACKET_H 26 | #define _CCPACKET_H 27 | 28 | #include "Arduino.h" 29 | 30 | /** 31 | * Buffer and data lengths 32 | */ 33 | #define CC1101_BUFFER_LEN 64 34 | #define CC1101_DATA_LEN CC1101_BUFFER_LEN - 3 35 | 36 | /** 37 | * Class: CCPACKET 38 | * 39 | * Description: 40 | * CC1101 data packet class 41 | */ 42 | class CCPACKET 43 | { 44 | public: 45 | /** 46 | * Data length 47 | */ 48 | byte length; 49 | 50 | /** 51 | * Data buffer 52 | */ 53 | byte data[CC1101_DATA_LEN]; 54 | 55 | /** 56 | * CRC OK flag 57 | */ 58 | boolean crc_ok; 59 | 60 | /** 61 | * Received Strength Signal Indication 62 | */ 63 | byte rssi; 64 | 65 | /** 66 | * Link Quality Index 67 | */ 68 | byte lqi; 69 | }; 70 | 71 | #endif 72 | 73 | -------------------------------------------------------------------------------- /commonregs.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 07/06/2011 23 | */ 24 | 25 | #ifndef _COMMONREGS_H 26 | #define _COMMONREGS_H 27 | #include "swstatus.h" 28 | /** 29 | * Macros for the definition of common register indexes 30 | */ 31 | #define DEFINE_COMMON_REGINDEX_START() \ 32 | enum CUSTOM_REGINDEX \ 33 | { \ 34 | REGI_PRODUCTCODE = 0, \ 35 | REGI_HWVERSION, \ 36 | REGI_FWVERSION, \ 37 | REGI_SYSSTATE, \ 38 | REGI_FREQCHANNEL, \ 39 | REGI_SECUOPTION, \ 40 | REGI_PASSWORD, \ 41 | REGI_SECUNONCE, \ 42 | REGI_NETWORKID, \ 43 | REGI_DEVADDRESS, \ 44 | REGI_TXINTERVAL, 45 | 46 | #define DEFINE_COMMON_REGINDEX_END() }; 47 | 48 | #define DEFINE_REGINDEX_START() DEFINE_COMMON_REGINDEX_START() 49 | #define DEFINE_REGINDEX_END() DEFINE_COMMON_REGINDEX_END() 50 | 51 | /** 52 | * Macro for the definition of registers common to all SWAP devices 53 | */ 54 | #define DEFINE_COMMON_REGISTERS() \ 55 | /* Product code */ \ 56 | static byte dtProductCode[8] = {SWAP_MANUFACT_ID >> 24, SWAP_MANUFACT_ID >> 16 , SWAP_MANUFACT_ID >> 8, SWAP_MANUFACT_ID, \ 57 | SWAP_PRODUCT_ID >> 24, SWAP_PRODUCT_ID >> 16 , SWAP_PRODUCT_ID >> 8, SWAP_PRODUCT_ID}; \ 58 | REGISTER regProductCode(dtProductCode, sizeof(dtProductCode), NULL, NULL); \ 59 | /* Hardware version */ \ 60 | static byte dtHwVersion[4] = {HARDWARE_VERSION >> 24, HARDWARE_VERSION >> 16 , HARDWARE_VERSION >> 8, HARDWARE_VERSION}; \ 61 | REGISTER regHwVersion(dtHwVersion, sizeof(dtHwVersion), NULL, NULL); \ 62 | /* Firmware version */ \ 63 | static byte dtFwVersion[4] = {FIRMWARE_VERSION >> 24, FIRMWARE_VERSION >> 16 , FIRMWARE_VERSION >> 8, FIRMWARE_VERSION}; \ 64 | REGISTER regFwVersion(dtFwVersion, sizeof(dtFwVersion), NULL, NULL); \ 65 | /* System state */ \ 66 | REGISTER regSysState(&panstamp.systemState, sizeof(panstamp.systemState), NULL, &setSysState); \ 67 | /* Frequency channel */ \ 68 | REGISTER regFreqChannel(&panstamp.cc1101.channel, sizeof(panstamp.cc1101.channel), NULL, &setFreqChannel); \ 69 | /* Security option */ \ 70 | REGISTER regSecuOption(&panstamp.security, sizeof(panstamp.security), NULL, NULL); \ 71 | /* Security password (not implemented yet) */ \ 72 | static byte dtPassword[1]; \ 73 | REGISTER regPassword(dtPassword, sizeof(dtPassword), NULL, NULL); \ 74 | /* Security nonce */ \ 75 | REGISTER regSecuNonce(&panstamp.nonce, sizeof(panstamp.nonce), NULL, NULL); \ 76 | /* Network Id */ \ 77 | REGISTER regNetworkId(panstamp.cc1101.syncWord, sizeof(panstamp.cc1101.syncWord), NULL, &setNetworkId); \ 78 | /* Device address */ \ 79 | REGISTER regDevAddress(&panstamp.cc1101.devAddress, sizeof(panstamp.cc1101.devAddress), NULL, &setDevAddress); \ 80 | /* Periodic Tx interval */ \ 81 | REGISTER regTxInterval(panstamp.txInterval, sizeof(panstamp.txInterval), NULL, &setTxInterval); 82 | 83 | /** 84 | * Macros for the declaration of global table of registers 85 | */ 86 | #define DECLARE_REGISTERS_START() \ 87 | REGISTER *regTable[] = { \ 88 | ®ProductCode, \ 89 | ®HwVersion, \ 90 | ®FwVersion, \ 91 | ®SysState, \ 92 | ®FreqChannel, \ 93 | ®SecuOption, \ 94 | ®Password, \ 95 | ®SecuNonce, \ 96 | ®NetworkId, \ 97 | ®DevAddress, \ 98 | ®TxInterval, 99 | 100 | #define DECLARE_REGISTERS_END() \ 101 | }; \ 102 | /* Size of regTable */ \ 103 | byte regTableSize = sizeof(regTable)/sizeof(*regTable); 104 | 105 | /** 106 | * Macro for the declaration of getter/setter functions related to all common registers 107 | */ 108 | #define DECLARE_COMMON_CALLBACKS() \ 109 | const void setSysState(byte id, byte *state); \ 110 | const void setFreqChannel(byte id, byte *channel); \ 111 | const void setDevAddress(byte id, byte *addr); \ 112 | const void setNetworkId(byte rId, byte *nId); \ 113 | const void setTxInterval(byte id, byte *interval); 114 | 115 | /** 116 | * Macro for the definition of getter/setter functions related to all common registers 117 | */ 118 | #define DEFINE_COMMON_CALLBACKS() \ 119 | /** \ 120 | * setSysState \ 121 | * \ 122 | * Set system state \ 123 | * \ 124 | * 'id' Register ID \ 125 | * 'state' New system state \ 126 | */ \ 127 | const void setSysState(byte id, byte *state) \ 128 | { \ 129 | switch(state[0]) \ 130 | { \ 131 | case SYSTATE_RESTART: \ 132 | /* Send status message before restarting the mote */ \ 133 | panstamp.reset(); \ 134 | break; \ 135 | default: \ 136 | panstamp.systemState = state[0]; \ 137 | break; \ 138 | } \ 139 | } \ 140 | \ 141 | /** \ 142 | * setFreqChannel \ 143 | * \ 144 | * Set frequency channel \ 145 | * \ 146 | * 'id' Register ID \ 147 | * 'channel' New channel \ 148 | */ \ 149 | const void setFreqChannel(byte id, byte *channel) \ 150 | { \ 151 | if (channel[0] != regFreqChannel.value[0]) \ 152 | { \ 153 | /* Send status message before entering the new \ 154 | frequency channel */ \ 155 | SWSTATUS packet = SWSTATUS(regFreqChannel.id, channel, regFreqChannel.length); \ 156 | packet.send(); \ 157 | /* Update register value */ \ 158 | panstamp.cc1101.setChannel(channel[0], true); \ 159 | /* Restart device */ \ 160 | panstamp.reset(); \ 161 | } \ 162 | } \ 163 | \ 164 | \ 165 | /** \ 166 | * setDevAddress \ 167 | * \ 168 | * Set device address \ 169 | * \ 170 | * 'id' Register ID \ 171 | * 'addr' New device address \ 172 | */ \ 173 | const void setDevAddress(byte id, byte *addr) \ 174 | { \ 175 | if ((addr[0] > 0) && (addr[0] != regDevAddress.value[0])) \ 176 | { \ 177 | /* Send status before setting the new address */ \ 178 | SWSTATUS packet = SWSTATUS(regDevAddress.id, addr, regDevAddress.length); \ 179 | packet.send(); \ 180 | /* Update register value */ \ 181 | panstamp.cc1101.setDevAddress(addr[0], true); \ 182 | } \ 183 | } \ 184 | \ 185 | /** \ 186 | * setNetworkId \ 187 | * \ 188 | * Set network id \ 189 | * \ 190 | * 'rId' Register ID \ 191 | * 'nId' New network id \ 192 | */ \ 193 | const void setNetworkId(byte rId, byte *nId) \ 194 | { \ 195 | if ((nId[0] != regNetworkId.value[0]) || \ 196 | (nId[1] != regNetworkId.value[1])) \ 197 | { \ 198 | /* Send status before taking the new network ID */ \ 199 | SWSTATUS packet = SWSTATUS(regNetworkId.id, nId, regNetworkId.length); \ 200 | packet.send(); \ 201 | /* Update register value */ \ 202 | panstamp.cc1101.setSyncWord(nId, true); \ 203 | } \ 204 | } \ 205 | /** \ 206 | * setTxInterval \ 207 | * \ 208 | * Set periodic Tx interval \ 209 | * \ 210 | * 'id' Register ID \ 211 | * 'interval' New interval (in seconds) \ 212 | */ \ 213 | const void setTxInterval(byte id, byte *interval) \ 214 | { \ 215 | if ((interval[0] != regTxInterval.value[0]) || \ 216 | (interval[1] != regTxInterval.value[1])) \ 217 | { \ 218 | panstamp.setTxInterval(interval, true); \ 219 | } \ 220 | } 221 | #endif 222 | 223 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 10/03/2012 23 | */ 24 | 25 | #ifndef _CONFIG_H 26 | #define _CONFIG_H 27 | 28 | /** 29 | * Repeater options 30 | */ 31 | // Amount of transactions to be saved for evaluation before repeating a packet 32 | // Maximum depth = 255 33 | #define REPEATER_TABLE_DEPTH 20 34 | // Expiration time (in ms) for any transaction 35 | #define REPEATER_EXPIRATION_TIME 2000 36 | 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /docs/CC1101模块.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martyrs/cc1101-ook/fed9a5d80f4e5b390d4e0dfae741d4039510ae69/docs/CC1101模块.pdf -------------------------------------------------------------------------------- /docs/cc1101.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martyrs/cc1101-ook/fed9a5d80f4e5b390d4e0dfae741d4039510ae69/docs/cc1101.pdf -------------------------------------------------------------------------------- /docs/wiring.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martyrs/cc1101-ook/fed9a5d80f4e5b390d4e0dfae741d4039510ae69/docs/wiring.jpg -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map panStamp 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | CC1101 KEYWORD1 9 | CCPACKET KEYWORD1 10 | CARRIER_FREQ KEYWORD1 11 | SWPACKET KEYWORD1 12 | ENDPOINT KEYWORD1 13 | PANSTAMP KEYWORD1 14 | 15 | ####################################### 16 | # Methods and Functions (KEYWORD2) 17 | ####################################### 18 | init KEYWORD2 19 | reset KEYWORD2 20 | wakeUp KEYWORD2 21 | goToSleep KEYWORD2 22 | enterSystemState KEYWORD2 23 | setSyncWord KEYWORD2 24 | setDevAddress KEYWORD2 25 | setCarrierFreq KEYWORD2 26 | setChannel KEYWORD2 27 | setSecurity KEYWORD2 28 | setTxInterval KEYWORD2 29 | setSwapStatusCallBack KEYWORD2 30 | setRxState KEYWORD2 31 | setPowerDownState KEYWORD2 32 | sendData KEYWORD2 33 | receiveData KEYWORD2 34 | disableAddressCheck KEYWORD2 35 | sleepFor KEYWORD2 36 | getData KEYWORD2 37 | setData KEYWORD2 38 | sendSwapStatus KEYWORD2 39 | getRegister KEYWORD2 40 | rcOscCalibrate KEYWORD2 41 | 42 | ####################################### 43 | # Atributes (KEYWORD2) 44 | ####################################### 45 | carrierFreq KEYWORD2 46 | channel KEYWORD2 47 | syncWord KEYWORD2 48 | devAddress KEYWORD2 49 | 50 | ####################################### 51 | # Constants (LITERAL1) 52 | ####################################### 53 | CFREQ_868 LITERAL1 54 | CFREQ_915 LITERAL1 55 | 56 | -------------------------------------------------------------------------------- /nvolat.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 07/03/2011 23 | */ 24 | 25 | #ifndef _NVOLAT_H 26 | #define _NVOLAT_H 27 | 28 | #include "Arduino.h" 29 | #include 30 | 31 | /** 32 | * EEPROM addresses 33 | */ 34 | #define EEPROM_FREQ_CHANNEL 0x00 // 1-byte register 35 | #define EEPROM_NOT_USED 0x01 // 1-byte register 36 | #define EEPROM_SYNC_WORD 0x02 // 2-byte register 37 | #define EEPROM_DEVICE_ADDR 0x04 // 1-byte register 38 | #define EEPROM_TX_INTERVAL 0x05 // 2-byte register 39 | 40 | #define EEPROM_FIRST_CUSTOM 0x20 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /panstamp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "panstamp.h" 26 | #include "commonregs.h" 27 | #include "calibration.h" 28 | 29 | #define enableIRQ_GDO0() attachInterrupt(0, isrGDO0event, FALLING); 30 | #define disableIRQ_GDO0() detachInterrupt(0); 31 | 32 | DEFINE_COMMON_REGINDEX_START() 33 | DEFINE_COMMON_REGINDEX_END() 34 | 35 | /** 36 | * Array of registers 37 | */ 38 | extern REGISTER* regTable[]; 39 | extern byte regTableSize; 40 | 41 | /** 42 | * PANSTAMP 43 | * 44 | * Class constructor 45 | */ 46 | PANSTAMP::PANSTAMP(void) 47 | { 48 | statusReceived = NULL; 49 | repeater = NULL; 50 | } 51 | 52 | /** 53 | * enableRepeater 54 | * 55 | * Enable repeater mode 56 | * 57 | * 'maxHop' MAximum repeater count. Zero if omitted 58 | */ 59 | void PANSTAMP::enableRepeater(byte maxHop) 60 | { 61 | if (repeater == NULL) 62 | { 63 | static REPEATER repe; 64 | repeater = &repe; 65 | repeater->init(maxHop); 66 | } 67 | 68 | if (maxHop == 0) 69 | repeater->stop(); 70 | } 71 | 72 | /** 73 | * getRegister 74 | * 75 | * Return pointer to register with ID = regId 76 | * 77 | * 'regId' Register ID 78 | */ 79 | REGISTER * getRegister(byte regId) 80 | { 81 | if (regId >= regTableSize) 82 | return NULL; 83 | 84 | return regTable[regId]; 85 | } 86 | 87 | /** 88 | * isrGDO0event 89 | * 90 | * Event on GDO0 pin (INT0) 91 | */ 92 | void isrGDO0event(void) 93 | { 94 | // Disable interrupt 95 | disableIRQ_GDO0(); 96 | 97 | if (panstamp.cc1101.rfState == RFSTATE_RX) 98 | { 99 | static CCPACKET ccPacket; 100 | static SWPACKET swPacket; 101 | REGISTER *reg; 102 | static bool eval = true; 103 | 104 | if (panstamp.cc1101.receiveData(&ccPacket) > 0) 105 | { 106 | if (ccPacket.crc_ok) 107 | { 108 | swPacket = SWPACKET(ccPacket); 109 | 110 | // Repeater enabled? 111 | if (panstamp.repeater != NULL) 112 | panstamp.repeater->packetHandler(&swPacket); 113 | 114 | // Smart encryption locally enabled? 115 | if (panstamp.security & 0x02) 116 | { 117 | // OK, then incoming packets must be encrypted too 118 | if (!(swPacket.security & 0x02)) 119 | eval = false; 120 | } 121 | 122 | if (eval) 123 | { 124 | // Function 125 | switch(swPacket.function) 126 | { 127 | case SWAPFUNCT_CMD: 128 | // Command not addressed to us? 129 | if (swPacket.destAddr != panstamp.cc1101.devAddress) 130 | break; 131 | // Current version does not support data recording mode 132 | // so destination address and register address must be the same 133 | if (swPacket.destAddr != swPacket.regAddr) 134 | break; 135 | // Valid register? 136 | if ((reg = getRegister(swPacket.regId)) == NULL) 137 | break; 138 | // Anti-playback security enabled? 139 | if (panstamp.security & 0x01) 140 | { 141 | // Check received nonce 142 | if (panstamp.nonce != swPacket.nonce) 143 | { 144 | // Nonce missmatch. Transmit correct nonce. 145 | reg = getRegister(REGI_SECUNONCE); 146 | reg->sendSwapStatus(); 147 | break; 148 | } 149 | } 150 | // Filter incorrect data lengths 151 | if (swPacket.value.length == reg->length) 152 | reg->setData(swPacket.value.data); 153 | else 154 | reg->sendSwapStatus(); 155 | break; 156 | case SWAPFUNCT_QRY: 157 | // Only Product Code can be broadcasted 158 | if (swPacket.destAddr == SWAP_BCAST_ADDR) 159 | { 160 | if (swPacket.regId != REGI_PRODUCTCODE) 161 | break; 162 | } 163 | // Query not addressed to us? 164 | else if (swPacket.destAddr != panstamp.cc1101.devAddress) 165 | break; 166 | // Current version does not support data recording mode 167 | // so destination address and register address must be the same 168 | if (swPacket.destAddr != swPacket.regAddr) 169 | break; 170 | // Valid register? 171 | if ((reg = getRegister(swPacket.regId)) == NULL) 172 | break; 173 | reg->getData(); 174 | break; 175 | case SWAPFUNCT_STA: 176 | // User callback function declared? 177 | if (panstamp.statusReceived != NULL) 178 | panstamp.statusReceived(&swPacket); 179 | break; 180 | default: 181 | break; 182 | } 183 | } 184 | } 185 | } 186 | } 187 | // Enable interrupt 188 | enableIRQ_GDO0(); 189 | } 190 | 191 | /** 192 | * ISR(WDT_vect) 193 | * 194 | * Watchdog ISR. Called whenever a watchdog interrupt occurs 195 | */ 196 | ISR(WDT_vect) 197 | { 198 | } 199 | 200 | /** 201 | * setup_watchdog 202 | * 203 | * 'time' Watchdog timer value 204 | */ 205 | void PANSTAMP::setup_watchdog(byte time) 206 | { 207 | byte bb; 208 | 209 | bb = time & 7; 210 | if (time > 7) 211 | bb|= (1<<5); 212 | 213 | bb|= (1<sendSwapStatus(); 299 | 300 | // Reset panStamp 301 | wdt_disable(); 302 | wdt_enable(WDTO_15MS); 303 | while (1) {} 304 | } 305 | 306 | /** 307 | * sleepWd 308 | * 309 | * Put panStamp into Power-down state during "time". 310 | * This function uses the internal watchdog timer in order to exit (interrupt) 311 | * from the power-down state 312 | * 313 | * 'time' Sleeping time: 314 | * WDTO_15MS = 15 ms 315 | * WDTO_30MS = 30 ms 316 | * WDTO_60MS = 60 ms 317 | * WDTO_120MS = 120 ms 318 | * WDTO_250MS = 250 ms 319 | * WDTO_500MS = 500 ms 320 | * WDTO_1S = 1 s 321 | * WDTO_2S = 2 s 322 | * WDTO_4S = 4 s 323 | * WDTO_8S = 8 s 324 | */ 325 | void PANSTAMP::sleepWd(byte time) 326 | { 327 | // Power-down CC1101 328 | cc1101.setPowerDownState(); 329 | // Power-down panStamp 330 | set_sleep_mode(SLEEP_MODE_PWR_DOWN); 331 | sleep_enable(); 332 | setup_watchdog(time); 333 | delayMicroseconds(10); 334 | // Disable ADC 335 | ADCSRA &= ~(1 << ADEN); 336 | // Unpower functions 337 | PRR = 0xFF; 338 | //power_all_disable(); 339 | //clock_prescale_set(clock_div_8); 340 | // Enter sleep mode 341 | sleep_mode(); 342 | 343 | // ZZZZZZZZ... 344 | 345 | // Wake-up!! 346 | wakeUp(false); 347 | } 348 | 349 | /** 350 | * sleepRtc 351 | * 352 | * Put panStamp into Power-down state during "time". 353 | * This function uses Timer 2 connected to an external 32.768KHz crystal 354 | * in order to exit (interrupt) from the power-down state 355 | * 356 | * 'time' Sleeping time: 357 | * RTC_250MS = 250 ms 358 | * RTC_500MS = 500 ms 359 | * RTC_1S = 1 s 360 | * RTC_2S = 2 s 361 | * RTC_8S = 8 s 362 | */ 363 | void PANSTAMP::sleepRtc(byte time) 364 | { 365 | // Power-down CC1101 366 | cc1101.setPowerDownState(); 367 | // Power-down panStamp 368 | set_sleep_mode(SLEEP_MODE_PWR_SAVE); 369 | sleep_enable(); 370 | setup_rtc(time); 371 | delayMicroseconds(10); 372 | // Disable ADC 373 | ADCSRA &= ~(1 << ADEN); 374 | // Unpower functions 375 | PRR = 0xFF; 376 | // Enter sleep mode 377 | sleep_mode(); 378 | 379 | // ZZZZZZZZ... 380 | 381 | // Wake-up!! 382 | wakeUp(false); 383 | } 384 | 385 | /** 386 | * wakeUp 387 | * 388 | * Wake from sleep mode 389 | * 390 | * 'rxOn' Enter RX_ON state after waking up 391 | */ 392 | void PANSTAMP::wakeUp(bool rxOn) 393 | { 394 | // Exit from sleep 395 | sleep_disable(); 396 | //wdt_disable(); 397 | // Re-enable functions 398 | //clock_prescale_set(clock_div_1); 399 | power_all_enable(); 400 | // Enable ADC 401 | ADCSRA |= (1 << ADEN); 402 | 403 | // If 32.768 KHz crystal enabled 404 | if (rtcCrystal) 405 | { 406 | // Disable timer2A overflow interrupt 407 | TIMSK2 = 0x00; 408 | } 409 | 410 | // Reset CC1101 IC 411 | cc1101.wakeUp(); 412 | 413 | if (rxOn) 414 | systemState = SYSTATE_RXON; 415 | } 416 | 417 | /** 418 | * goToSleep 419 | * 420 | * Sleep whilst in power-down mode. This function currently uses sleepWd in a loop 421 | */ 422 | void PANSTAMP::goToSleep(void) 423 | { 424 | // Get the amount of seconds to sleep from the internal register 425 | int intInterval = txInterval[0] * 0x100 + txInterval[1]; 426 | int i, loops; 427 | byte minTime; 428 | 429 | // No interval? Then return 430 | if (intInterval == 0) 431 | return; 432 | 433 | // Search the maximum sleep time passed as argument to sleepWd that best 434 | // suits our desired interval 435 | if (intInterval % 8 == 0) 436 | { 437 | loops = intInterval / 8; 438 | 439 | if (rtcCrystal) 440 | minTime = RTC_8S; 441 | else 442 | minTime = WDTO_8S; 443 | } 444 | else if (intInterval % 4 == 0) 445 | { 446 | if (rtcCrystal) 447 | { 448 | loops = intInterval / 2; 449 | minTime = RTC_2S; 450 | } 451 | else 452 | { 453 | loops = intInterval / 4; 454 | minTime = WDTO_4S; 455 | } 456 | } 457 | else if (intInterval % 2 == 0) 458 | { 459 | loops = intInterval / 2; 460 | if (rtcCrystal) 461 | minTime = RTC_2S; 462 | else 463 | minTime = WDTO_2S; 464 | } 465 | else 466 | { 467 | loops = intInterval; 468 | if (rtcCrystal) 469 | minTime = RTC_1S; 470 | else 471 | minTime = WDTO_1S; 472 | } 473 | 474 | systemState = SYSTATE_RXOFF; 475 | 476 | // Sleep 477 | for (i=0 ; isetData(newState); 503 | } 504 | 505 | /** 506 | * getInternalTemp 507 | * 508 | * Read internal (ATMEGA328 only) temperature sensor 509 | * Reference: http://playground.arduino.cc/Main/InternalTemperatureSensor 510 | * 511 | * Return: 512 | * Temperature in degrees Celsius 513 | */ 514 | long PANSTAMP::getInternalTemp(void) 515 | { 516 | unsigned int wADC; 517 | long t; 518 | 519 | // The internal temperature has to be used 520 | // with the internal reference of 1.1V. 521 | // Channel 8 can not be selected with 522 | // the analogRead function yet. 523 | 524 | // Set the internal reference and mux. 525 | ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); 526 | ADCSRA |= _BV(ADEN); // enable the ADC 527 | 528 | delay(20); // wait for voltages to become stable. 529 | 530 | ADCSRA |= _BV(ADSC); // Start the ADC 531 | 532 | // Detect end-of-conversion 533 | while (bit_is_set(ADCSRA,ADSC)); 534 | 535 | // Reading register "ADCW" takes care of how to read ADCL and ADCH. 536 | wADC = ADCW; 537 | 538 | // The offset of 324.31 could be wrong. It is just an indication. 539 | t = (wADC - 324.31 ) / 1.22; 540 | 541 | // The returned temperature is in degrees Celcius. 542 | return (t); 543 | } 544 | 545 | /** 546 | * setTxInterval 547 | * 548 | * Set interval for periodic transmissions 549 | * 550 | * 'interval' New periodic interval. 0 for asynchronous devices 551 | * 'save' If TRUE, save parameter in EEPROM 552 | */ 553 | void PANSTAMP::setTxInterval(byte* interval, bool save) 554 | { 555 | memcpy(txInterval, interval, sizeof(txInterval)); 556 | 557 | // Save in EEPROM 558 | if (save) 559 | { 560 | EEPROM.write(EEPROM_TX_INTERVAL, interval[0]); 561 | EEPROM.write(EEPROM_TX_INTERVAL + 1, interval[1]); 562 | } 563 | } 564 | 565 | /** 566 | * setSmartPassword 567 | * 568 | * Set Smart Encryption password 569 | * 570 | * 'password' Encryption password 571 | */ 572 | void PANSTAMP::setSmartPassword(byte* password) 573 | { 574 | // Save password 575 | memcpy(encryptPwd, password, sizeof(encryptPwd)); 576 | // Enable Smart Encryption 577 | security |= 0x02; 578 | } 579 | 580 | /** 581 | * Pre-instantiate PANSTAMP object 582 | */ 583 | PANSTAMP panstamp; 584 | 585 | -------------------------------------------------------------------------------- /panstamp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _PANSTAMP_H 26 | #define _PANSTAMP_H 27 | 28 | #include "Arduino.h" 29 | #include "EEPROM.h" 30 | #include "cc1101.h" 31 | #include "nvolat.h" 32 | #include "register.h" 33 | #include "swpacket.h" 34 | #include "config.h" 35 | #include "repeater.h" 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | /** 42 | * RTC definitions 43 | */ 44 | #define RTC_250MS 0x03 // Timer 2 prescaler = 32 45 | #define RTC_500MS 0x04 // Timer 2 prescaler = 64 46 | #define RTC_1S 0x05 // Timer 2 prescaler = 128 47 | #define RTC_2S 0x06 // Timer 2 prescaler = 256 48 | #define RTC_8S 0x07 // Timer 2 prescaler = 1024 49 | 50 | /** 51 | * Macros 52 | */ 53 | #define setSwapStatusCallBack(ptrFunc) statusReceived = ptrFunc 54 | 55 | #define eepromToFactoryDefaults() \ 56 | EEPROM.write(EEPROM_SYNC_WORD, CC1101_DEFVAL_SYNC1); \ 57 | EEPROM.write(EEPROM_SYNC_WORD + 1, CC1101_DEFVAL_SYNC0); \ 58 | EEPROM.write(EEPROM_DEVICE_ADDR, CC1101_DEFVAL_ADDR); \ 59 | EEPROM.write(EEPROM_FREQ_CHANNEL, CC1101_DEFVAL_CHANNR); \ 60 | EEPROM.write(EEPROM_TX_INTERVAL, 0xFF); \ 61 | EEPROM.write(EEPROM_TX_INTERVAL + 1, 0xFF) 62 | 63 | #define setHighTxPower() cc1101.setTxPowerAmp(PA_LongDistance) 64 | #define setLowTxPower() cc1101.setTxPowerAmp(PA_LowPower) 65 | 66 | #define enableAntiPlayback() security |= 0x01 67 | 68 | /** 69 | * System states 70 | */ 71 | enum SYSTATE 72 | { 73 | SYSTATE_RESTART = 0, 74 | SYSTATE_RXON, 75 | SYSTATE_RXOFF, 76 | SYSTATE_SYNC, 77 | SYSTATE_LOWBAT 78 | }; 79 | 80 | 81 | /** 82 | * Class: PANSTAMP 83 | * 84 | * Description: 85 | * panStamp main class 86 | */ 87 | class PANSTAMP 88 | { 89 | private: 90 | /** 91 | * setup_watchdog 92 | * 93 | * 'time' Watchdog timer value 94 | */ 95 | void setup_watchdog(byte time); 96 | 97 | /** 98 | * setup_rtc 99 | * 100 | * Setup software (Timer 2) RTC 101 | * 102 | * 'time' Timer2 prescaler 103 | * 104 | * RTC_1S = 128 for 1 sec 105 | * RTC_2S = 256 for 2 sec 106 | * RTC_8S = 1024 for 8 sec 107 | */ 108 | void setup_rtc(byte time); 109 | 110 | public: 111 | /** 112 | * repeater 113 | * 114 | * Pointer to repeater object 115 | */ 116 | REPEATER *repeater; 117 | 118 | /** 119 | * True if the external 32.768 KHz crystal is enabled 120 | */ 121 | bool rtcCrystal; 122 | 123 | /** 124 | * CC1101 radio interface 125 | */ 126 | CC1101 cc1101; 127 | 128 | /** 129 | * Security options 130 | */ 131 | byte security; 132 | 133 | /** 134 | * Security cyclic nonce 135 | */ 136 | byte nonce; 137 | 138 | /** 139 | * System state 140 | */ 141 | byte systemState; 142 | 143 | /** 144 | * Interval between periodic transmissions. 0 for asynchronous transmissions 145 | */ 146 | byte txInterval[2]; 147 | 148 | /** 149 | * Smart encryption password 150 | */ 151 | byte encryptPwd[12]; 152 | 153 | /** 154 | * enableRepeater 155 | * 156 | * Enable repeater mode 157 | */ 158 | void enableRepeater(void); 159 | 160 | /** 161 | * enableRepeater 162 | * 163 | * Enable repeater mode 164 | * 165 | * 'maxHop' MAximum repeater count. Zero if omitted 166 | */ 167 | void enableRepeater(byte maxHop=0); 168 | 169 | /** 170 | * SWAP status packet received. Callaback function 171 | */ 172 | void (*statusReceived)(SWPACKET *status); 173 | 174 | /** 175 | * PANSTAMP 176 | * 177 | * Class constructor 178 | */ 179 | PANSTAMP(void); 180 | 181 | /** 182 | * init 183 | * 184 | * Initialize panStamp board 185 | */ 186 | void init(void); 187 | 188 | /** 189 | * reset 190 | * 191 | * Reset panStamp 192 | */ 193 | void reset(void); 194 | 195 | /** 196 | * sleepWd 197 | * 198 | * Put panStamp into Power-down state during "time". 199 | * This function uses the internal watchdog timer in order to exit (interrupt) 200 | * from the power-doen state 201 | * 202 | * 'time' Sleeping time: 203 | * WDTO_15MS = 15 ms 204 | * WDTO_30MS = 30 ms 205 | * WDTO_60MS = 60 ms 206 | * WDTO_120MS = 120 ms 207 | * WDTO_250MS = 250 ms 208 | * WDTO_500MS = 500 ms 209 | * WDTO_1S = 1 s 210 | * WDTO_2S = 2 s 211 | * WDTO_4S = 4 s 212 | * WDTO_8S = 8 s 213 | */ 214 | void sleepWd(byte time); 215 | 216 | /** 217 | * sleepRtc 218 | * 219 | * Put panStamp into Power-down state during "time". 220 | * This function uses Timer 2 connected to an external 32.768KHz crystal 221 | * in order to exit (interrupt) from the power-down state 222 | * 223 | * 'time' Sleeping time: 224 | * RTC_250MS = 250 ms 225 | * RTC_500MS = 500 ms 226 | * RTC_1S = 1 s 227 | * RTC_2S = 2 s 228 | * RTC_8S = 8 s 229 | */ 230 | void sleepRtc(byte time); 231 | 232 | /** 233 | * wakeUp 234 | * 235 | * Wake from sleep mode 236 | * 237 | * 'rxOn' Enter RX_ON state after waking up 238 | */ 239 | void wakeUp(bool rxOn=true); 240 | 241 | /** 242 | * goToSleep 243 | * 244 | * Sleep whilst in power-down mode. This function currently uses sleepWd in a loop 245 | * 246 | */ 247 | void goToSleep(void); 248 | 249 | /** 250 | * enterSystemState 251 | * 252 | * Enter system state 253 | * 254 | * 'state' New system state 255 | */ 256 | void enterSystemState(SYSTATE state); 257 | 258 | /** 259 | * getInternalTemp 260 | * 261 | * Read internal (ATMEGA328 only) temperature sensor 262 | * Reference: http://playground.arduino.cc/Main/InternalTemperatureSensor 263 | * 264 | * Return: 265 | * Temperature in degrees Celsius 266 | */ 267 | long getInternalTemp(void); 268 | 269 | /** 270 | * setTxInterval 271 | * 272 | * Set interval for periodic transmissions 273 | * 274 | * 'interval' New periodic interval. 0 for asynchronous devices 275 | * 'save' If TRUE, save parameter in EEPROM 276 | */ 277 | void setTxInterval(byte* interval, bool save); 278 | 279 | /** 280 | * setSmartPassword 281 | * 282 | * Set Smart Encryption password 283 | * 284 | * 'password' Encryption password 285 | */ 286 | void setSmartPassword(byte* password); 287 | }; 288 | 289 | /** 290 | * Global PANSTAMP object 291 | */ 292 | extern PANSTAMP panstamp; 293 | 294 | /** 295 | * getRegister 296 | * 297 | * Return pointer to register with ID = regId 298 | * 299 | * 'regId' Register ID 300 | */ 301 | REGISTER * getRegister(byte regId); 302 | 303 | #endif 304 | 305 | -------------------------------------------------------------------------------- /register.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 04/24/2011 23 | */ 24 | 25 | #include "register.h" 26 | #include "swstatus.h" 27 | 28 | byte regIndex = 0; 29 | 30 | /** 31 | * getData 32 | * 33 | * Update and get register value 34 | */ 35 | void REGISTER::getData(void) 36 | { 37 | // Update register value 38 | if (updateValue != NULL) 39 | updateValue(id); 40 | 41 | // Send SWAP status message about the new value 42 | sendSwapStatus(); 43 | } 44 | 45 | /** 46 | * setData 47 | * 48 | * Set register value 49 | * 50 | * 'data' New register value 51 | */ 52 | void REGISTER::setData(byte *data) 53 | { 54 | // Update register value 55 | if (setValue != NULL) 56 | setValue(id, data); 57 | 58 | // Send SWAP status message 59 | sendSwapStatus(); 60 | } 61 | 62 | /** 63 | * sendSwapStatus 64 | * 65 | * Send SWAP status message 66 | */ 67 | void REGISTER::sendSwapStatus(void) 68 | { 69 | SWSTATUS packet = SWSTATUS(id, value, length); 70 | packet.send(); 71 | } 72 | 73 | -------------------------------------------------------------------------------- /register.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 04/24/2011 23 | */ 24 | 25 | #ifndef _REGISTER_H 26 | #define _REGISTER_H 27 | 28 | #include "Arduino.h" 29 | 30 | extern byte regIndex; 31 | 32 | /** 33 | * Class: REGISTER 34 | * 35 | * Description: 36 | * Register class 37 | */ 38 | class REGISTER 39 | { 40 | private: 41 | /** 42 | * Pointer to the register "updater" function 43 | * 44 | * 'rId' Register ID 45 | */ 46 | const void (*updateValue)(byte rId); 47 | 48 | /** 49 | * Pointer to the register "setter" function 50 | * 51 | * 'rId' Register ID 52 | * 'v' New register value 53 | */ 54 | const void (*setValue)(byte rId, byte *v); 55 | 56 | public: 57 | /** 58 | * Register id 59 | */ 60 | const byte id; 61 | 62 | /** 63 | * Register value 64 | */ 65 | byte *value; 66 | 67 | /** 68 | * Data length 69 | */ 70 | const byte length; 71 | 72 | /** 73 | * REGISTER 74 | * 75 | * Constructor 76 | * 77 | * 'val' Pointer to the register value 78 | * 'len' Length of the register value 79 | * 'getValH' Pointer to the getter function 80 | * 'setValH' Pointer to the setter function 81 | */ 82 | REGISTER(byte *val, const byte len, const void (*updateValH)(byte rId), const void (*setValH)(byte rId, byte *v)):id(regIndex++), value(val), length(len), updateValue(updateValH), setValue(setValH) {}; 83 | 84 | /** 85 | * getData 86 | * 87 | * Update and get register value 88 | * 89 | */ 90 | void getData(); 91 | 92 | /** 93 | * setData 94 | * 95 | * Set register value 96 | * 97 | * 'data' New register value 98 | */ 99 | void setData(byte *data); 100 | 101 | /** 102 | * sendSwapStatus 103 | * 104 | * Send SWAP status message 105 | */ 106 | void sendSwapStatus(void); 107 | 108 | /** 109 | * setRegValue 110 | * 111 | * Set register value from different data formats 112 | * Use this method to simplify LE to BE conversion 113 | * 114 | * 'val' New register value 115 | */ 116 | template void setRegValue(T val) 117 | { 118 | uint8_t i; 119 | 120 | for(i=0 ; i>= 8; 124 | } 125 | } 126 | }; 127 | 128 | /** 129 | * Array of registers 130 | */ 131 | extern REGISTER* regTable[]; 132 | 133 | /** 134 | * Extern global functions 135 | */ 136 | extern void setupRegisters(void); 137 | 138 | #endif 139 | 140 | -------------------------------------------------------------------------------- /repeater.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 10/02/2012 23 | */ 24 | 25 | #include "repeater.h" 26 | #include "swpacket.h" 27 | #include "panstamp.h" 28 | 29 | /** 30 | * init 31 | * 32 | * Initialize repeater 33 | * 34 | * 'maxHop': maximum hop count 35 | */ 36 | void REPEATER::init(byte maxHop) 37 | { 38 | maxHopCount = maxHop; 39 | start(); 40 | } 41 | 42 | /** 43 | * Class constructor 44 | */ 45 | REPEATER::REPEATER(void) 46 | { 47 | } 48 | 49 | /** 50 | * packetHandler 51 | * 52 | * Handle incoming packet. Repeat if necessary 53 | * 54 | * 'packet': Pointer to the SWAP packet received 55 | */ 56 | void REPEATER::packetHandler(SWPACKET *packet) 57 | { 58 | static bool repeatPacket = true; 59 | static unsigned long currentTime; 60 | 61 | if (enabled) 62 | { 63 | // Don't repeat packets addressed to our device 64 | if (packet->destAddr != panstamp.cc1101.devAddress) 65 | { 66 | // Don't repeat beyond the maximum hop count 67 | if (packet->hop < maxHopCount) 68 | { 69 | byte i; 70 | 71 | // Check received packet against the latest transactions 72 | for(i=0 ; iregAddr) 76 | { 77 | // Same SWAP function? 78 | if (transactions[i].function == packet->function) 79 | { 80 | // Same cyclic nonce? 81 | if (transactions[i].nonce == packet->nonce) 82 | { 83 | currentTime = millis(); 84 | // Time stamp not expired? 85 | if ((currentTime - transactions[i].timeStamp) < REPEATER_EXPIRATION_TIME) 86 | repeatPacket = false; //Don't repeat packet 87 | } 88 | } 89 | } 90 | } 91 | 92 | // Repeat packet? 93 | if (repeatPacket) 94 | { 95 | packet->srcAddr = panstamp.cc1101.devAddress; // Modify source address 96 | packet->hop++; // Increment hop counter 97 | delay(SWAP_TX_DELAY); // Delay before sending 98 | if (packet->send()) // Repeat packet 99 | saveTransaction(packet); // Save transaction 100 | } 101 | } 102 | } 103 | } 104 | } 105 | 106 | /** 107 | * saveTransaction 108 | * 109 | * Save transaction in array 110 | * 111 | * 'packet': SWAP packet being repeated 112 | */ 113 | void REPEATER::saveTransaction(SWPACKET *packet) 114 | { 115 | byte i; 116 | 117 | // Move all packets one position forward 118 | for(i=REPEATER_TABLE_DEPTH-1 ; i>0 ; i--) 119 | transactions[i] = transactions[i-1]; 120 | 121 | // Save current transaction in first position 122 | transactions[0].timeStamp = millis(); // Current time stamp 123 | transactions[0].function = packet->function; // SWAP function 124 | transactions[0].srcAddr = packet->srcAddr; // Source address 125 | transactions[0].nonce = packet->nonce; // Cyclic nonce 126 | transactions[0].regAddr = packet->regAddr; // Register address 127 | } 128 | 129 | -------------------------------------------------------------------------------- /repeater.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 10/01/2012 23 | */ 24 | 25 | #ifndef _REPEATER_H 26 | #define _REPEATER_H 27 | 28 | #include "Arduino.h" 29 | #include "swpacket.h" 30 | #include "config.h" 31 | 32 | /** 33 | * Macros 34 | */ 35 | #define start() enabled = true 36 | #define stop() enabled = false 37 | 38 | /** 39 | * Transaction 40 | */ 41 | typedef struct 42 | { 43 | unsigned long timeStamp; // Transmission time stamp (ms) 44 | byte function; // SWAP function 45 | byte srcAddr; // Source address 46 | byte nonce; // Cyclic nonce 47 | byte regAddr; // Register address 48 | } Transaction; 49 | 50 | 51 | /** 52 | * Cñass declaration 53 | */ 54 | class REPEATER 55 | { 56 | private: 57 | /** 58 | * Maximum hop 59 | */ 60 | byte maxHopCount; 61 | 62 | /** 63 | * Array of latest transactions 64 | */ 65 | Transaction transactions[REPEATER_TABLE_DEPTH]; 66 | 67 | /** 68 | * saveTransaction 69 | * 70 | * Save transaction in array 71 | * 72 | * 'packet': SWAP packet being repeated 73 | */ 74 | void saveTransaction(SWPACKET *packet); 75 | 76 | public: 77 | /** 78 | * Enable flag 79 | */ 80 | bool enabled; 81 | 82 | /** 83 | * init 84 | * 85 | * Initialize repeater 86 | * 87 | * 'maxHop': maximum hop count 88 | */ 89 | void init(byte maxHop); 90 | 91 | /** 92 | * packetHandler 93 | * 94 | * Handle incoming packet. Repeat if necessary 95 | * 96 | * 'packet': Pointer to the SWAP packet received 97 | */ 98 | void packetHandler(SWPACKET *packet); 99 | 100 | /** 101 | * Class constructor 102 | */ 103 | REPEATER(void); 104 | }; 105 | 106 | #endif 107 | 108 | -------------------------------------------------------------------------------- /spi.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "spi.h" 26 | 27 | /** 28 | * init 29 | * 30 | * SPI initialization 31 | */ 32 | void SPI::init() 33 | { 34 | digitalWrite(SPI_SS, HIGH); 35 | 36 | // Configure SPI pins 37 | pinMode(SPI_SS, OUTPUT); 38 | pinMode(SPI_MOSI, OUTPUT); 39 | pinMode(SPI_MISO, INPUT); 40 | pinMode(SPI_SCK, OUTPUT); 41 | 42 | digitalWrite(SPI_SCK, HIGH); 43 | digitalWrite(SPI_MOSI, LOW); 44 | 45 | // SPI speed = clk/4 46 | SPCR = _BV(SPE) | _BV(MSTR); 47 | } 48 | 49 | /** 50 | * send 51 | * 52 | * Send byte via SPI 53 | * 54 | * 'value' Value to be sent 55 | * 56 | * Return: 57 | * Response received from SPI slave 58 | */ 59 | byte SPI::send(byte value) 60 | { 61 | SPDR = value; // Transfer byte via SPI 62 | wait_Spi(); // Wait until SPI operation is terminated 63 | 64 | return SPDR; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /spi.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _SPI_H 26 | #define _SPI_H 27 | 28 | #include "Arduino.h" 29 | 30 | /** 31 | * SPI pins 32 | */ 33 | #define SPI_SS 10 // PB2 = SPI_SS 34 | #define SPI_MOSI 11 // PB3 = MOSI 35 | #define SPI_MISO 12 // PB4 = MISO 36 | #define SPI_SCK 13 // PB5 = SCK 37 | #define GDO0 2 // PD2 = INT0 38 | 39 | #define PORT_SPI_MISO PINB 40 | #define BIT_SPI_MISO 4 41 | 42 | #define PORT_SPI_SS PORTB 43 | #define BIT_SPI_SS 2 44 | 45 | #define PORT_GDO0 PIND 46 | #define BIT_GDO0 2 47 | 48 | /** 49 | * Macros 50 | */ 51 | // Wait until SPI operation is terminated 52 | #define wait_Spi() while(!(SPSR & _BV(SPIF))) 53 | 54 | /** 55 | * Class: SPI 56 | * 57 | * Description: 58 | * Basic SPI class 59 | */ 60 | class SPI 61 | { 62 | public: 63 | /** 64 | * init 65 | * 66 | * SPI initialization 67 | */ 68 | void init(); 69 | 70 | /** 71 | * send 72 | * 73 | * Send byte via SPI 74 | * 75 | * 'value' Value to be sent 76 | * 77 | * Return: 78 | * Response received from SPI slave 79 | */ 80 | byte send(byte value); 81 | }; 82 | #endif 83 | -------------------------------------------------------------------------------- /swcommand.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "swcommand.h" 26 | #include "panstamp.h" 27 | 28 | /** 29 | * SWCOMMAND 30 | * 31 | * Class constructor 32 | * 33 | * 'dAddr' Destination address 34 | * 'secNonce' Security nonce 35 | * 'rAddr' Register address 36 | * 'rId' Register id 37 | * '*val' New value 38 | * 'len' Buffer length 39 | */ 40 | SWCOMMAND::SWCOMMAND(byte dAddr, byte secNonce, byte rAddr, byte rId, byte *val, byte len) 41 | { 42 | destAddr = dAddr; 43 | srcAddr = panstamp.cc1101.devAddress; 44 | hop = 0; 45 | security = panstamp.security & 0x0F; 46 | nonce = secNonce; 47 | function = SWAPFUNCT_CMD; 48 | regAddr = rAddr; 49 | regId = rId; 50 | value.data = val; 51 | value.length = len; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /swcommand.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _SWCOMMAND_H 26 | #define _SWCOMMAND_H 27 | 28 | #include "Arduino.h" 29 | #include "swpacket.h" 30 | 31 | 32 | class SWCOMMAND : public SWPACKET 33 | { 34 | public: 35 | /** 36 | * SWCOMMAND 37 | * 38 | * Class constructor 39 | * 40 | * 'destAddr' Destination address 41 | * 'secNonce' Security nonce 42 | * 'rAddr' Register address 43 | * 'rId' Register id 44 | * '*val' New value 45 | * 'len' Buffer length 46 | */ 47 | SWCOMMAND(byte destAddr, byte secNonce, byte rAddr, byte rId, byte *val, byte len); 48 | }; 49 | #endif 50 | -------------------------------------------------------------------------------- /swpacket.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "swpacket.h" 26 | #include "panstamp.h" 27 | 28 | /** 29 | * SWPACKET 30 | * 31 | * Class constructor 32 | * 33 | * 'packet' Raw CC1101 packet 34 | */ 35 | SWPACKET::SWPACKET(CCPACKET packet) 36 | { 37 | destAddr = packet.data[0]; 38 | srcAddr = packet.data[1]; 39 | hop = (packet.data[2] >> 4) & 0x0F; 40 | security = packet.data[2] & 0x0F; 41 | nonce = packet.data[3]; 42 | function = packet.data[4]; 43 | regAddr = packet.data[5]; 44 | regId = packet.data[6]; 45 | value.data = packet.data + 7; 46 | value.length = packet.length - SWAP_DATA_HEAD_LEN - 1; 47 | 48 | // Need to decrypt packet? 49 | if (security & 0x02) 50 | smartDecrypt(); 51 | } 52 | 53 | /** 54 | * SWPACKET 55 | * 56 | * Class constructor 57 | */ 58 | SWPACKET::SWPACKET(void) 59 | { 60 | } 61 | 62 | /** 63 | * send 64 | * 65 | * Send SWAP packet. Do up to 5 retries if necessary 66 | * 67 | * Return: 68 | * True if the transmission succeeds 69 | * False otherwise 70 | */ 71 | boolean SWPACKET::send(void) 72 | { 73 | CCPACKET packet; 74 | byte i; 75 | boolean res; 76 | 77 | // Need to encrypt packet? 78 | if (security & 0x02) 79 | smartEncrypt(); 80 | 81 | packet.length = value.length + SWAP_DATA_HEAD_LEN + 1; 82 | packet.data[0] = destAddr; 83 | packet.data[1] = srcAddr; 84 | packet.data[2] = (hop << 4) & 0xF0; 85 | packet.data[2] |= security & 0x0F; 86 | packet.data[3] = nonce; 87 | packet.data[4] = function; 88 | packet.data[5] = regAddr; 89 | packet.data[6] = regId; 90 | 91 | for(i=0 ; i1) 96 | { 97 | i--; 98 | delay(SWAP_TX_DELAY); 99 | } 100 | 101 | return res; 102 | } 103 | 104 | /** 105 | * smartEncrypt 106 | * 107 | * Apply Smart Encryption to the SWAP packet passed as argument 108 | * 109 | * 'decrypt': if true, Decrypt packet. Encrypt otherwise 110 | */ 111 | void SWPACKET::smartEncrypt(bool decrypt) 112 | { 113 | byte i, j = 0; 114 | static byte newData[CC1101_DATA_LEN]; 115 | 116 | if (decrypt) 117 | nonce ^= panstamp.encryptPwd[9]; 118 | 119 | function ^= panstamp.encryptPwd[11] ^ nonce; 120 | srcAddr ^= panstamp.encryptPwd[10] ^ nonce; 121 | regAddr ^= panstamp.encryptPwd[8] ^ nonce; 122 | regId ^= panstamp.encryptPwd[7] ^ nonce; 123 | 124 | for(i=0 ; i 0) 132 | value.data = newData; 133 | 134 | if (!decrypt) 135 | nonce ^= panstamp.encryptPwd[9]; 136 | } 137 | 138 | -------------------------------------------------------------------------------- /swpacket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _SWPACKET_H 26 | #define _SWPACKET_H 27 | 28 | #include "Arduino.h" 29 | #include "ccpacket.h" 30 | 31 | /** 32 | * SWAP definitions 33 | */ 34 | #define SWAP_DATA_HEAD_LEN 6 35 | #define SWAP_REG_VAL_LEN CC1101_DATA_LEN - SWAP_DATA_HEAD_LEN // SWAP data payload - max length 36 | #define SWAP_BCAST_ADDR 0x00 // SWAP broadcast address 37 | #define SWAP_NB_TX_TRIES 3 // Number of transmission retries 38 | #define SWAP_TX_DELAY panstamp.cc1101.devAddress * 2 // Delay before sending 39 | 40 | /** 41 | * SWAP message functions 42 | */ 43 | enum SWAPFUNCT 44 | { 45 | SWAPFUNCT_STA = 0x00, 46 | SWAPFUNCT_QRY, 47 | SWAPFUNCT_CMD 48 | }; 49 | 50 | /** 51 | * Macros 52 | */ 53 | #define smartDecrypt() smartEncrypt(true) 54 | 55 | /** 56 | * Structure: SWDATA 57 | * 58 | * Description: 59 | * SWAP data structure 60 | */ 61 | struct SWDATA 62 | { 63 | /** 64 | * Data buffer 65 | */ 66 | byte *data; 67 | 68 | /** 69 | * Data length 70 | */ 71 | byte length; 72 | }; 73 | 74 | class SWPACKET : public CCPACKET 75 | { 76 | private: 77 | /** 78 | * smartEncrypt 79 | * 80 | * Apply Smart Encryption to the SWAP packet passed as argument 81 | * 82 | * 'decrypt': if true, Decrypt packet. Encrypt otherwise 83 | */ 84 | void smartEncrypt(bool decrypt=false); 85 | 86 | public: 87 | /** 88 | * Destination address 89 | */ 90 | byte destAddr; 91 | 92 | /** 93 | * Source address 94 | */ 95 | byte srcAddr; 96 | 97 | /** 98 | * Hop counter. Incremented each time the message is repeated 99 | */ 100 | byte hop; 101 | 102 | /** 103 | * Security option byte 104 | */ 105 | byte security; 106 | 107 | /** 108 | * Security cyclic nonce 109 | */ 110 | byte nonce; 111 | 112 | /** 113 | * Function byte 114 | */ 115 | byte function; 116 | 117 | /** 118 | * Register address 119 | */ 120 | byte regAddr; 121 | 122 | /** 123 | * Register id 124 | */ 125 | byte regId; 126 | 127 | /** 128 | * Register value 129 | */ 130 | SWDATA value; 131 | 132 | /** 133 | * SWPACKET 134 | * 135 | * Class constructor 136 | * 137 | * 'packet' Raw CC1101 packet 138 | */ 139 | SWPACKET(CCPACKET packet); 140 | 141 | /** 142 | * SWPACKET 143 | * 144 | * Class constructor 145 | */ 146 | SWPACKET(void); 147 | 148 | /** 149 | * send 150 | * 151 | * Send SWAP packet 152 | * 153 | * Return: 154 | * True if the transmission succeeds 155 | * False otherwise 156 | */ 157 | boolean send(void); 158 | }; 159 | 160 | #endif 161 | -------------------------------------------------------------------------------- /swquery.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "swquery.h" 26 | #include "panstamp.h" 27 | 28 | /** 29 | * SWQUERY 30 | * 31 | * Class constructor 32 | * 33 | * 'dAddr' Destination address 34 | * 'rAddr' Register address 35 | * 'rId' Register id 36 | */ 37 | SWQUERY::SWQUERY(byte dAddr, byte rAddr, byte rId) 38 | { 39 | destAddr = dAddr; 40 | srcAddr = panstamp.cc1101.devAddress; 41 | hop = 0; 42 | security = panstamp.security & 0x0F; 43 | nonce = 0; 44 | function = SWAPFUNCT_QRY; 45 | regAddr = rAddr; 46 | regId = rId; 47 | value.data = NULL; 48 | value.length = 0; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /swquery.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _SWQUERY_H 26 | #define _SWQUERY_H 27 | 28 | #include "Arduino.h" 29 | #include "swpacket.h" 30 | 31 | class SWQUERY : public SWPACKET 32 | { 33 | public: 34 | /** 35 | * SWQUERY 36 | * 37 | * Class constructor 38 | * 39 | * 'destAddr' Destination address 40 | * 'rAddr' Register address 41 | * 'rId' Register id 42 | */ 43 | SWQUERY(byte destAddr, byte rAddr, byte rId); 44 | }; 45 | #endif 46 | -------------------------------------------------------------------------------- /swstatus.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #include "swstatus.h" 26 | #include "panstamp.h" 27 | 28 | /** 29 | * SWSTATUS 30 | * 31 | * Class constructor 32 | * 33 | * 'rId' Register id 34 | * '*val' New value 35 | * 'len' Buffer length 36 | */ 37 | SWSTATUS::SWSTATUS(byte rId, byte *val, byte len) 38 | { 39 | destAddr = SWAP_BCAST_ADDR; 40 | srcAddr = panstamp.cc1101.devAddress; 41 | hop = 0; 42 | security = panstamp.security & 0x0F; 43 | nonce = ++panstamp.nonce; 44 | function = SWAPFUNCT_STA; 45 | regAddr = panstamp.cc1101.devAddress; 46 | regId = rId; 47 | value.length = len; 48 | value.data = val; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /swstatus.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011 panStamp 3 | * 4 | * This file is part of the panStamp project. 5 | * 6 | * panStamp is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; either version 3 of the License, or 9 | * any later version. 10 | * 11 | * panStamp is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with panStamp; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19 | * USA 20 | * 21 | * Author: Daniel Berenguer 22 | * Creation date: 03/03/2011 23 | */ 24 | 25 | #ifndef _SWSTATUS_H 26 | #define _SWSTATUS_H 27 | 28 | #include "Arduino.h" 29 | #include "swpacket.h" 30 | 31 | 32 | class SWSTATUS : public SWPACKET 33 | { 34 | public: 35 | /** 36 | * SWSTATUS 37 | * 38 | * Class constructor 39 | * 40 | * 'rId' Register id 41 | * '*val' New value 42 | * 'len' Buffer length 43 | */ 44 | SWSTATUS(byte rId, byte *val, byte len); 45 | }; 46 | #endif 47 | --------------------------------------------------------------------------------