├── .gitignore ├── ChangeLog.md ├── README.md ├── examples ├── si5351_mcu │ └── si5351_mcu.ino ├── si5351_mcu_simple │ └── si5351_mcu_simple.ino └── si5351_serial_console │ └── si5351_serial_console.ino ├── keywords.txt ├── library.properties ├── licence.txt ├── src ├── si5351mcu.cpp └── si5351mcu.h └── version /.gitignore: -------------------------------------------------------------------------------- 1 | .amake 2 | .gch 3 | ~* -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Si5351mcu Changelog File # 2 | 3 | ## v0.6.2 (January 21, 2020) ## 4 | 5 | * Fix a type mismatch that causes an error when you compile it under ESP8266/ESP32 6 | 7 | ## v0.6.1 (Augus 1, 2018) ## 8 | 9 | * Documentation update and completion to match code features and tricks 10 | 11 | ## v0.6 (February 10, 2018) ## 12 | 13 | * Feature: you can now OVERCLOCK the Si5351 to get up to 250 MHz from it, see the overclock subject on the README.md 14 | * Documentation improvements and re-arrangement. 15 | 16 | ## v0.5 (February 7, 2018) ## 17 | 18 | * Feature: All integer math now, induced error must be at worst +/- 2 Hz. 19 | * Feature: Clock status via clkOn[clk] public var. 20 | * Bug Fix: Output divider low limit safe guard in place (it make some trouble under some circumstances) 21 | * New super simple example. 22 | 23 | ## v0.4 (August 2, 2017) ## 24 | 25 | * Bug Fix: Triaged a strange level problem with freqs above ~112 Mhz, the signal level start dropping around 112MHz and ~150 MHz suddenly go beyond limits (high) to slowly drop again up to the end. Fact: the lib needs a reset() on EVERY frequency change above VCO/8 (~112MHz). Remember that datasheet specs are 8KHz to 160MHz, but we are pushing it up to ~225 Mhz (max_vco/4) 26 | * Code refractory on some points to match correct behavior, as previous code has little bugs introduced by bad documentation from Silicon Labs (+1 for the chip; -5 for the docs errors). See Bitx20 mail-lits archives for June-August 2017 for more info and the debate. 27 | 28 | ## v0.3 (June 14, 2017) ## 29 | 30 | * Feature: the lib now handle the include and start of the I2C (Wire) library internally via the init procedures 31 | * Added a new generic init() procedure to handle the default parameters 32 | * The init() function is required from now on (MANDATORY) 33 | * Fixed the way we handled the base xtal and the correction factor 34 | 35 | ## v0.2rc (April 23, 2017) ## 36 | 37 | * Added power level support for each output independently, watch out!: setting the power level will enable the output. 38 | * Set default power to the lowest one (2mA) from the maximun possible (8mA). 39 | * Fixed the need for a reset after each correction, it does it now automatically 40 | * Added a init function to pass a custom xtal 41 | * Modified the example to show no need for a init unless you use a different xtal 42 | * Improved the keywords.txt file to improve edition on the Arduino IDE 43 | * Included a "features" section on the README.md 44 | 45 | ## v0.1rc (April 20, 2017) ## 46 | 47 | * Added enable(), disable() and off() functions. 48 | * Added support for handling all the three outputs of the Si5351A, (CLK1 & CLK2 are mutually-exclusive) 49 | * Updated the example with the new functions. 50 | * Improved library logic by reusing and optimizing functions. 51 | * Improved the documentation and comments (lib code, README and example) 52 | * The compiled code is slightly smaller now (~1% on an ATMega328p) 53 | * Added Changelog and version files. 54 | * Extensive tests made to validate every function. 55 | 56 | ## Initial Release, v0.0 (April 9, 2017) ## 57 | 58 | * Basic functionality. 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino Si5351 Library tuned for size and click noise free # 2 | 3 | This library is tuned for size on the Arduino platform, it will control CLK0, CLK1 and CLK2 outputs for the Si5351A (the version with just 3 clocks out, but you will not be able to use the three at once). 4 | 5 | ## Inspiration ## 6 | 7 | This work is based on the previous work of these great people: 8 | 9 | * [Etherkit/NT7S:](https://github.com/etherkit/Si5351Arduino) The mainstream full featured lib, with big code as well (based on Linux kernel code) 10 | * [QRP Labs demo code from Hans Summers:](http://qrp-labs.com/synth/si5351ademo.html) The smallest and simple ones on the net 11 | * [DK7IH demo code:](https://radiotransmitter.wordpress.com/category/si5351a/) The first clickless noise code on the wild 12 | * [Jerry Gaffke](https://github.com/afarhan/ubitx.git) integer routines for the Raduino and ubitx 13 | 14 | ## Set your Goal and make an estrategy ## 15 | 16 | There is a few routines in the Internet to manage the Si5351 chip, all of them has a few distinct feature set because they use different strategies (different goals) that make them unique in some way. 17 | 18 | My goal is this: 19 | 20 | * Keep it as small as possible (Smallest firmware footprint) 21 | * Less phase and click noise possible (Playing with every trick possible) 22 | * Make it as fast as possible (thanks to @birdwes for I2C busrt mode write) 23 | 24 | The main purpose is to be used in Radio receiver projects, so this two mentioned goals are the golden rule. 25 | 26 | Let's list some of goals achievements and bonuses: 27 | 28 | **Small firmware footprint:** 29 | 30 | A basic sketch to set just only one clock out to a given frequency with a change in power and correcting the XTAL ppm error is only ~3.3 kBytes of firmware (~10% of an Arduino Uno) 31 | 32 | The same settings with the [Si5351Arduino library (Etherkit)](https://github.com/etherkit/Si5351Arduino) will give you a bigger firmware space of ~10 kBytes or 31% of an Arduino Uno. 33 | 34 | [Jerry Gaffke](https://github.com/afarhan/ubitx.git) embedded routines in the ubitx transceiver has the smallest footprint in the Arduino platform I have seen, but has worst phase noise and smallest frequency range. 35 | 36 | **Phase noise to the minimum:** 37 | 38 | We use every trick on the datasheet, OEM Application Notes or the Internet to minimize phase noise. (Even a few ones learned on the process) 39 | 40 | For example the [Etherkit](https://github.com/etherkit/Si5351Arduino) library and [Jerry Gaffke](https://github.com/afarhan/ubitx.git) embedded routines uses some but not all the tricks to minimize phase noise (Etherkit one gives control over all features, Jerry Gaffke has small footprint and in the process he sacrifice phase noise and frequency range) 41 | 42 | **Click noise free:** 43 | 44 | If you play from the book (Datasheet and Application Notes) you will have a "click-like" noise burst every time you change the output frequency. 45 | 46 | That's not a problem if you intend to use only fixed frequencies at the output, but if you plan to sweep or use it on any application that moves the frequency that click-like noise will haunt you. (like in a radio receiver or transceiver) 47 | 48 | I have learned a few tricks from many sources in the Internet and after some local testing I have came across a scheme that make this lib virtually click-noise-less; see the "Click noise free" section below for details. 49 | 50 | **Fast frequency changes:** 51 | 52 | This was a side effect of the last trick to minimize the click noise, see the "Click noise free" section below for details; also with the I2C busrt write contribution from @birdwes even the I2C writes takes a lot less time (implemented since version 0.7.0) 53 | 54 | Summary: other routines write all registers for every frequency change, one byte at a time; I write half of them most of the time and in a bust mode speeding up the process a lot. 55 | 56 | **Two of three** 57 | 58 | Yes, there is no such thing as free lunch, to get all the above features and the ones that follow I have to make some sacrifices, in this case spare one of the outputs. See "Two of three" section below. 59 | 60 | ## Features ## 61 | 62 | This are so far the implemented features (Any particular wish? use the Issues tab for that): 63 | 64 | * Custom XTAL passing on init (Default is 27.000 MHz (See _Si.init()_ ) 65 | * You can pass a correction to the xtal while running (See _Si.correction()_ ) 66 | * You have a fast way to power off all outputs of the Chip at once. (See _Si.off()_ ) 67 | * You can enable/disable any output at any time (See _Si.enable(clk) and Si.disable(clk)_ ) 68 | * By default all outputs are off after the Si.init() procedure. You has to enable them by hand. 69 | * You can only have 2 of the 3 outputs running at any moment (See "Two of three" section below) 70 | * Power control on each output independently (See _Si.setPower(clk, level)_ on the lib header) 71 | * Initial power defaults to the lowest level (2mA) for all outputs. 72 | * You don't need to include and configure the Wire (I2C) library, this lib do that for you already. 73 | * I2C writes are handled in busrt mode, just init the I2C once per frequency change and dump the registers content and close; saving the init for each byte sent as normal. 74 | * Frequency limits are not hard coded on the lib, so you can stress your hardware to it's particular limit (_You can move usually from ~3kHz to ~225 MHz, far away from the 8kHz to 160 MHz limits from the datasheet_) 75 | * You has a way to verify the status of a particular clock (_Enabled/Disabled by the Si.clkOn[clk] var_) 76 | * From v0.5 and beyond we saved more than 1 kbyte of your precious firmware space due to the use of all integer math now (Worst induced error is below +/- 1 Hz) 77 | * Overclock, yes, you can move the limits upward up to ~250MHz (see the "OVERCLOCK" section below) 78 | * Improved the click noise algorithm to get even more click noise reduction (see Click noise free section below) 79 | * Fast frequency changes as part of the improved click noise algorithm (see Click noise free section below) & I2C writes in burst mode. 80 | 81 | ## How to use the lib ## 82 | 83 | Get the lib by cloning this git repository or get it by clicking the green "Download button" on the page. 84 | 85 | Move it or extract it on your library directory 86 | 87 | Include the lib in your code: 88 | 89 | 90 | ``` 91 | (... your code here ...) 92 | 93 | // now include the library 94 | #include "si5351mcu.h" 95 | 96 | // lib instantiation as "Si" 97 | Si5351mcu Si; 98 | 99 | (... more of your code here ...) 100 | 101 | ``` 102 | 103 | Follow this sequence on you setup() procedure: 104 | 105 | * Initialize the library with the default or optional Xtal Clock. 106 | * Apply correction factor (if needed) 107 | * Set some frequencies to the desired outputs. 108 | * Enable the desired outputs 109 | 110 | Here you have an example code ("Si" is the lib instance): 111 | 112 | ``` 113 | setup() { 114 | (... your code here ...) 115 | 116 | ////////////////////////////////// 117 | // Si5351 functions / 118 | ////////////////////////////////// 119 | 120 | // Init the library, in this case with the default 27.000 Mhz Xtal 121 | Si.init(); 122 | 123 | // commented Init procedure for a not default 25.000 MHz xtal 124 | //Si.init(25000000L); 125 | 126 | // Optional, apply a pre-calculated correction factor 127 | Si.correction(-150); // Xtal is low by 150 Hz 128 | 129 | // Enable the desired outputs with some frequencies 130 | Si.setFreq(0, 25000000); // CLK0 output 25.000 MHz 131 | Si.setFreq(1, 145000000); // CLK1 output 145.000 MHz 132 | 133 | // enable the outputs 134 | Si.enable(0); 135 | Si.enable(1); 136 | 137 | (... more of your code here ...) 138 | } 139 | 140 | ``` 141 | 142 | If you need to apply/vary the correction factor **after** the setup process you will get a click noise on the next setFreq() to apply the changes. 143 | 144 | Use it, you can enable, disable, change the power or frequency, see this code fragment with some examples: 145 | 146 | ``` 147 | loop() { 148 | (... your code here ...) 149 | 150 | // disable clk1 151 | Si.disable(1); 152 | 153 | // change the power of clk0 to 4mA 154 | Si.setPower(0, SIOUT_4mA); 155 | 156 | // apply a correction factor of 300 Hz (correction will be applied on the next Si.setFreq() call) 157 | Si.correction(300); 158 | 159 | // change the clk0 output frequency 160 | Si.setFreq(0, 7110000); 161 | 162 | // power of all outputs 163 | Si.off(); 164 | 165 | (... more of your code here ...) 166 | } 167 | ``` 168 | 169 | 170 | ## OVERCLOCK ## 171 | 172 | Yes, you can overclock the Si5351, the datasheet states that the VCO moves from 600 to 900 MHz and that gives us a usable range from ~3 kHz to 225 MHz. 173 | 174 | But what if we can move the VCO frequency to a higher values? 175 | 176 | The overclock feature does just that, use a higher top limit for the VCO on the calculations. In my test with two batch of the Si5351A I can get safely up to 1.000 GHz without trouble; in one batch the PLL unlocks around 1.1 GHz and in the other about 1.050 GHz; so I recommend not going beyond 1.000 GHz. 177 | 178 | With a maximum VCO of 1.000 GHz and a lower division factor of 4 we have jumped from a 225 MHz to 250 MHz top frequency that can be generated with our cheap chip. 179 | 180 | **Some "must include" WARNINGS:** 181 | 182 | * The chip was not intended to go that high, so, use it with caution and test it on your hardware moving the overclock limit in steps of 10 MHz starting with 900 MHz and testing with every change until it does not work; then go down by 10 MHz to be in a safe zone. 183 | * Moving the upper limit has its penalty on the lower level, your bottom frequency will move from the ~3 kHz to ~10 kHz range. 184 | * The phase noise of the output if worst as you use a higher frequency, at a _**fixed**_ 250 MHz it's noticeable but no so bad for a TTL or CMOS clock application. 185 | * The phase noise is specially bad if you make a sweep or move action beyond 180 MHz; the phase noise from the unlock state to next lock of the PLL is very noticeable in a spectrum analyzer, even on a cheap RTL-SDR one. 186 | * I recommend to only use the outputs beyond 150 MHz as fixed value and don't move them if you cares about phase noise. 187 | 188 | **How to do it?** 189 | 190 | You need to declare a macro with the overclock value **BEFORE** the library include, just like this: 191 | 192 | ``` 193 | (... your code here ...) 194 | 195 | // Using the overclock feature for the Si5351mcu library 196 | #define SI_OVERCLOCK 1000000000L // 1.0 GHz in Hz 197 | 198 | // now include the library 199 | #include "si5351mcu.h" 200 | 201 | // lib instantiation as "Si" 202 | Si5351mcu Si; 203 | 204 | // now you can generate frequencies from ~10 kHz up to 250 MHz. 205 | 206 | (... more of your code here ...) 207 | 208 | ``` 209 | 210 | ## Click noise free ## 211 | 212 | Click-like noise came from a few sources as per my testing: 213 | 214 | * Turn off then on the CLKx outputs (Register 3. Output Enable Control) 215 | * Power down then on the Msynths (CLKx_PDN bits for every Msynth) 216 | * Reset the PLL (Register 177: PLL Reset) 217 | 218 | We are concerned about click noise only when changing from one frequency to the other, so if we don't touch the output enable control or the power down msynth registers once activated; then we are set to avoid click from this two first sources. 219 | 220 | The last one is tricky, in theory a PLL does not need to be reseted every time you change it's output frequency as it's a **P**hase **L**ocked **L**oop and it's a self correcting algorithm/hardware. 221 | 222 | That last idea was put to test on a simple scheme: what if I set a fixed output divider Msynth and move the VCO for it's entire range without resetting it on any point? 223 | 224 | If the "PLL reset" is in deed needed I will have some strange behavior at some point, right? 225 | 226 | But practice confirmed my idea, I can set a output Msynth of 6 and move the VCO (PLL) for the entire range (600 to 900 MHz) and get a continuous and stable output from 100 to 150 MHz. 227 | 228 | Then what is for the "PLL reset" (Register 177) in practice? 229 | 230 | Some further test showed that this "reset" function is applied no to the PLL Msynth, but to the output Msynth and not in every case, yes, it has a bad name or a bad explained name. 231 | 232 | After some test I find that you need the "PLL reset" (Register 177) trick only on some cases when you change the value of the output divider Msynth. 233 | 234 | Implementing that in code was easy, an array to keep track of the actual output divider Msynth and only write it to the chip and reset "the PLL" when it's needed. 235 | 236 | Hey! that leads to a I2C time reduction by half (most of the time) as a side effect! 237 | 238 | Most of the time when you are making a sweep the output divider Msynth has a constant value and you only moves the VCO (PLL) Then I wrote just 8 bytes to the I2C bus (to control the VCO/PLL) instead of 16 (8 for the VCO/PLL & 8 more for the output divider Msynth) or 17 (16 + reset byte) most of the time, cutting time between writes to half making frequency changes 2x fast as before. 239 | 240 | ## Two of three ## 241 | 242 | Yes, there is a tittle catch here with CLK1 and CLK2: both share PLL_B and as we use our own algorithm to calculate the frequencies and minimize phase noise you can only use one of them at a time. 243 | 244 | Note: _In practice you can, but the other will move from the frequency you set, which is an unexpected behavior, so I made them mutually exclusive (CLK1 and CLK2)._ 245 | 246 | This are the valid combinations for independent clocks output. 247 | 248 | * CLK0 and CLK1 249 | * CLK0 and CLK2 250 | 251 | Again: You can't use CLK1 and CLK2 at the same time, as soon as you set one of them the other will shut off. That's why you get two of three and one of them must be always CLK0. 252 | 253 | ## Author & contributors ## 254 | 255 | The main author is Pavel Milanes, CO7WT, a cuban amateur radio operator; reachable at pavelmc@gmail.com, Until now I have no contributors or sponsors. 256 | 257 | But I have received the contributions stated below: 258 | 259 | * @birdwes: 260 | - I2C busrt mode 261 | - Found & fixed a bug about freq calculations. 262 | 263 | ## Where to download the latest version? ## 264 | 265 | Always download the latest version from the [github repository](https://github.com/pavelmc/Si5351mcu/) 266 | 267 | See ChangeLog.md and Version files on this repository to know what are the latest changes and versions. 268 | 269 | ## If you like to give thanks... ## 270 | 271 | No payment of whatsoever is required to use this code: this is [Free/Libre Software](https://en.wikipedia.org/wiki/Software_Libre), nevertheless donations are very welcomed. 272 | 273 | I live in Cuba island and the Internet is very expensive here (USD $10.00 per 1GB of data), you can donate anonymously internet time or cell mobile data to me via [Ding Topups](https://www.ding.com/) to keep me connected and developing for the homebrew/DIY community. 274 | 275 | If you like to do so, please go to Ding, select Cuba, select Cubacel and use my number `(+53) 538-478-19` (feel free to call me if you like) 276 | 277 | Thanks! 278 | -------------------------------------------------------------------------------- /examples/si5351_mcu/si5351_mcu.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * si5351mcu - Si5351 library for Arduino, MCU tuned for size and click-less 3 | * 4 | * This is the packed example. 5 | * 6 | * Copyright (C) 2017 Pavel Milanes 7 | * 8 | * Many chunk of codes are derived-from/copied from other libs 9 | * all GNU GPL licenced: 10 | * - Linux Kernel (www.kernel.org) 11 | * - Hans Summers libs and demo code (qrp-labs.com) 12 | * - Etherkit (NT7S) Si5351 libs on github 13 | * - DK7IH example. 14 | * - Jerry Gaffke integer routines for the bitx20 group 15 | * 16 | * This program is free software: you can redistribute it and/or modify 17 | * it under the terms of the GNU General Public License as published by 18 | * the Free Software Foundation, either version 3 of the License, or 19 | * (at your option) any later version. 20 | * 21 | * This program is distributed in the hope that it will be useful, 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | * GNU General Public License for more details. 25 | * 26 | * You should have received a copy of the GNU General Public License 27 | * along with this program. If not, see . 28 | */ 29 | 30 | 31 | /*************************************************************************** 32 | * This example is meant to be monitored with an RTL-SDR receiver 33 | * but you can change the frequency and other vars to test with your hardware. 34 | * 35 | * Set your SDR software to monitor from 60 to 62 Mhz. 36 | * 37 | * This will set 60.0 Mhz in clock 0, put and alternating frequencies 38 | * at 60.5 and 61.0 Mhz on CLK1 and CLK2 to show they are mutually exclusive. 39 | * 40 | * Then make a sweep from 60 to 62 Mhz on CLK2, with an stop every 200Khz 41 | * and then a train of one second pulses will follow with varying power levels 42 | * 43 | * Take into account your XTAL error, see Si.correction(###) below 44 | * 45 | ***************************************************************************/ 46 | 47 | #include "si5351mcu.h" 48 | 49 | // lib instantiation as "Si" 50 | Si5351mcu Si; 51 | 52 | // Stop every X Mhz for Y seconds to measure 53 | #define EVERY 200000 // stop every 200khz 54 | #define DANCE 3 // 3 seconds 55 | 56 | // some variables 57 | long freqStart = 60000000; // 60.0 MHz 58 | long freqStop = 62000000; // 62.0 MHz 59 | long step = 10000; // 10.0 kHz 60 | long freq = freqStart; 61 | 62 | 63 | void setup() { 64 | // init the Si5351 lib 65 | Si.init(); 66 | 67 | // For a different xtal (from the default of 27.00000 Mhz) 68 | // just pass it on the init procedure, just like this 69 | // Si.init(26570000); 70 | 71 | // set & apply my calculated correction factor 72 | Si.correction(-1250); 73 | 74 | // pre-load some sweet spot freqs 75 | Si.setFreq(0, freqStart); 76 | Si.setFreq(1, freqStart); 77 | 78 | // reset the PLLs 79 | Si.reset(); 80 | 81 | // put a tone in the start freq on CLK0 82 | Si.setFreq(0, freqStart); 83 | Si.enable(0); 84 | 85 | // make the dance on the two outputs 86 | Si.setFreq(1, freqStart + 500000); // CLK1 output 87 | Si.enable(1); 88 | delay(3000); 89 | // Si.disable(1); // no need to disable, enabling CLK2 disable this 90 | 91 | Si.setFreq(2, freqStart + 1000000); // CLK2 output 92 | Si.enable(2); 93 | delay(3000); 94 | //Si.disable(2); // no need to disable, enabling CLK1 disable this 95 | 96 | Si.setFreq(1, freqStart + 500000); // CLK1 output 97 | Si.enable(1); 98 | delay(3000); 99 | //Si.disable(1); // no need to disable, enabling CLK2 disable this 100 | 101 | Si.setFreq(2, freqStart + 1000000); // CLK2 output 102 | Si.enable(2); 103 | delay(3000); 104 | Si.disable(2); // this is the last in the dance, disable it 105 | 106 | // shut down CLK0 107 | Si.disable(0); 108 | 109 | // set CLK2 to the start freq 110 | Si.setFreq(2, freqStart); // it's disabled by now 111 | } 112 | 113 | 114 | void loop() { 115 | // check for the stop to measure 116 | if ((freq % EVERY) == 0) { 117 | // it's time to flip-flop it 118 | 119 | for (byte i = 0; i < 4; i++) { 120 | // power off the clk2 output 121 | Si.disable(2); 122 | delay(500); 123 | // power mod, the lib define some macros for that: 124 | // SIOUT_2mA, SIOUT_4mA, SIOUT_6mA and SIOUT_8mA 125 | // 126 | // But they are incidentally matching the 0 to 3 count.. 127 | // so I will use the cycle counter for that (i) 128 | // 129 | // moreover, setting the power on an output will enable it 130 | // so I will explicit omit the enable here 131 | Si.setPower(2, i); 132 | //Si.enable(2); 133 | 134 | delay(1000); 135 | } 136 | 137 | // reset the power to low 138 | Si.setPower(2, SIOUT_2mA); 139 | 140 | // set it for the new cycle 141 | freq += step; 142 | } else { 143 | // check if we are on the limits 144 | if (freq <= freqStop) { 145 | // no, set and increment 146 | Si.setFreq(2,freq); // but it can be with CLK0 or CLK1 instead 147 | 148 | // set it for the new cycle 149 | freq += step; 150 | 151 | // a short delay to slow things a little. 152 | delay(50); 153 | } else { 154 | // we reached the limit, reset to start 155 | freq = freqStart; 156 | } 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /examples/si5351_mcu_simple/si5351_mcu_simple.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * si5351mcu - Si5351 library for Arduino, MCU tuned for size and click-less 3 | * 4 | * This is the packed simplest example. 5 | * 6 | * Copyright (C) 2017 Pavel Milanes 7 | * 8 | * Many chunk of codes are derived-from/copied from other libs 9 | * all GNU GPL licenced: 10 | * - Linux Kernel (www.kernel.org) 11 | * - Hans Summers libs and demo code (qrp-labs.com) 12 | * - Etherkit (NT7S) Si5351 libs on github 13 | * - DK7IH example. 14 | * - Jerry Gaffke integer routines for the bitx20 group 15 | * 16 | * This program is free software: you can redistribute it and/or modify 17 | * it under the terms of the GNU General Public License as published by 18 | * the Free Software Foundation, either version 3 of the License, or 19 | * (at your option) any later version. 20 | * 21 | * This program is distributed in the hope that it will be useful, 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | * GNU General Public License for more details. 25 | * 26 | * You should have received a copy of the GNU General Public License 27 | * along with this program. If not, see . 28 | */ 29 | 30 | 31 | /*************************************************************************** 32 | * Just put two frequencies out in clk0 and clk1 at different power levels 33 | * 34 | * Take into account your XTAL error, see Si.correction(###) below 35 | ***************************************************************************/ 36 | 37 | #include "si5351mcu.h" 38 | 39 | // lib instantiation as "Si" 40 | Si5351mcu Si; 41 | 42 | // some variables 43 | long F1 = 60000000; // 60.0 MHz to CLK0 44 | long F2 = 60500000; // 60.5 MHz to CLK1 45 | 46 | void setup() { 47 | // init the Si5351 lib 48 | Si.init(); 49 | 50 | // For a different xtal (from the default of 27.00000 Mhz) 51 | // just pass it on the init procedure, just like this 52 | // Si.init(26570000); 53 | 54 | // set & apply my calculated correction factor 55 | Si.correction(-1250); 56 | 57 | // set max power to both outputs 58 | Si.setPower(0, SIOUT_8mA); 59 | Si.setPower(1, SIOUT_2mA); 60 | 61 | // Set frequencies 62 | Si.setFreq(0, F1); 63 | Si.setFreq(1, F2); 64 | 65 | // reset the PLLs 66 | Si.reset(); 67 | } 68 | 69 | 70 | void loop() { 71 | // do nothing 72 | } 73 | -------------------------------------------------------------------------------- /examples/si5351_serial_console/si5351_serial_console.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * si5351mcu - Si5351 library for Arduino, MCU tuned for size and click-less 3 | * 4 | * Serial Console Test Harness. 5 | * 6 | * Copyright (C) 2017 Pavel Milanes 7 | * 8 | * Many chunk of codes are derived-from/copied from other libs 9 | * all GNU GPL licenced: 10 | * - Linux Kernel (www.kernel.org) 11 | * - Hans Summers libs and demo code (qrp-labs.com) 12 | * - Etherkit (NT7S) Si5351 libs on github 13 | * - DK7IH example. 14 | * - Jerry Gaffke integer routines for the bitx20 group 15 | * 16 | * This program is free software: you can redistribute it and/or modify 17 | * it under the terms of the GNU General Public License as published by 18 | * the Free Software Foundation, either version 3 of the License, or 19 | * (at your option) any later version. 20 | * 21 | * This program is distributed in the hope that it will be useful, 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24 | * GNU General Public License for more details. 25 | * 26 | * You should have received a copy of the GNU General Public License 27 | * along with this program. If not, see . 28 | */ 29 | 30 | /* 31 | * Serial Console Test Harness code added 32 | * 2021-10-01 - Iain W. Bird, 2E0WZR 33 | * 34 | */ 35 | 36 | /* 37 | * CAUTION: As this is for testing purposes only, there is little 38 | * validation or error checking. 39 | * 40 | */ 41 | 42 | /*************************************************************************** 43 | * Type HELP into the Arduino Serial Console line for a list of commands. 44 | * 45 | * Take into account your XTAL error, see Si.correction(###) below 46 | ***************************************************************************/ 47 | 48 | #include "si5351mcu.h" 49 | 50 | // lib instantiation as "Si" 51 | Si5351mcu Si; 52 | 53 | int cmdHelp( void ); 54 | void resetInput( void ); 55 | float getPllFraction( uint8_t pllIndex ); 56 | void printDecimalByte( int val ); 57 | 58 | uint8_t devicePresent = 0; 59 | 60 | void setup() { 61 | 62 | 63 | // init the Si5351 lib 64 | Si.init(25000000); 65 | 66 | // For a different xtal (from the default of 27.00000 Mhz) 67 | // just pass it on the init procedure, just like this 68 | // Si.init(26570000); 69 | 70 | // set & apply my calculated correction factor 71 | Si.correction(-1250); 72 | 73 | // Set frequencies 74 | Si.setFreq(0, 100000); 75 | Si.setFreq(1, 1000000); 76 | 77 | // reset the PLLs 78 | Si.reset(); 79 | Si.off(); 80 | 81 | Serial.begin(115200); 82 | while (!Serial); // Wait for devices such as Leonardo to start Serial 83 | 84 | Serial.println(F("")); 85 | Serial.println(F("**********************************************************")); 86 | Serial.println(F(" Si5351a Serial Console Test Bench")); 87 | Serial.println(F("")); 88 | Serial.println(F(" Please refer to document AN619 for register definitions.")); 89 | Serial.println(F("")); 90 | Serial.println(F(" If you choose to modify undocumented registers, ")); 91 | Serial.println(F(" or go beyond specification, it is at your own risk.")); 92 | Serial.println(F("")); 93 | Serial.println(F("**********************************************************")); 94 | Serial.println(F("")); 95 | 96 | if ( devicePresent ) { 97 | cmdHelp(); 98 | } 99 | else { 100 | Serial.println(F("DEVICE MISSING")); 101 | 102 | } 103 | resetInput(); 104 | } 105 | 106 | #define MAXINPUT 127 107 | 108 | char inputBuffer[ MAXINPUT + 1] = { 0 }; 109 | int inputIndex = 0; 110 | char *args[ MAXINPUT + 1] = { 0 }; 111 | uint8_t output_chan = 0; 112 | 113 | void resetInput( void ) { 114 | memset( inputBuffer, 0, MAXINPUT + 1 ); 115 | memset( args, 0, MAXINPUT + 1 ); 116 | inputIndex = 0; 117 | Serial.print( output_chan, DEC ); 118 | Serial.print(F(":> ")); 119 | } 120 | 121 | void printError( char *str ) { 122 | Serial.print(F("ERROR: ")); 123 | Serial.println( str ); 124 | } 125 | 126 | void printWarning( char *str ) { 127 | Serial.print(F("WARNING: ")); 128 | Serial.println( str ); 129 | } 130 | 131 | int8_t displayClockSource( uint8_t clkIndex ) { 132 | int16_t val; 133 | uint32_t uval; 134 | int8_t result = -1; 135 | uint8_t intdiv = 1; 136 | bool blnIntDiv = false; 137 | bool blnIntDiv4 = false; 138 | float pllMult = 1; 139 | float pllFreq = 1; 140 | float msynDiv = 1; 141 | float active = 1; 142 | 143 | do { // while(0) - Artificial construct to allow break; 144 | 145 | uval = val = Si.i2cRead( 16 + clkIndex ); 146 | if ( val < 0 ) { 147 | break; 148 | } 149 | Serial.print( clkIndex, DEC ); 150 | Serial.print( F(": PD=") ); 151 | Serial.print( (uval & 0x80) >> 7, DEC ); // Power down state 152 | active = !((uval & 0x80) >> 7); 153 | 154 | val = Si.i2cRead( 44 + clkIndex * 8 ); 155 | if ( val >= 0 ) { 156 | switch ( ( val & 0x0c ) >> 2 ) { 157 | case 3: 158 | blnIntDiv4 = true; 159 | // drop through is intentional - no break; 160 | case 0: 161 | intdiv = (val & 0x70) >> 4; 162 | intdiv = 1 << intdiv; 163 | blnIntDiv = true; 164 | break; 165 | default: 166 | intdiv = 0; // Unknown state 167 | break; 168 | } 169 | } 170 | Serial.print( F(", R=" ) ); 171 | Serial.print( intdiv , DEC ); 172 | 173 | Serial.print( F(", Div=") ); 174 | if ( uval & 0x40 || clkIndex >= 6 ) { 175 | Serial.print(F("Int, PLL=")); 176 | } 177 | else { 178 | Serial.print( F("Frac, PLL=") ); 179 | } 180 | // PLL 181 | Serial.print( (char) ('A' + (( uval & 0x20 ) >> 5) )); // PLL 'A' or 'B' 182 | Serial.print( F(", INV=" ) ); 183 | 184 | pllMult = getPllFraction( ( uval & 0x20 ) >> 5 ); 185 | pllFreq = pllMult * Si.getXtalCurrent(); 186 | 187 | Serial.print( (( uval & 0x10 ) >> 4), DEC ); 188 | Serial.print( F(", Src=" ) ); 189 | switch( ( uval & 0x0C ) >> 2 ) { 190 | case 0: 191 | Serial.print( F("XTAL, ") ); 192 | active = 0; 193 | break; 194 | case 1: 195 | Serial.print( F("CLKIN, ") ); 196 | active = 0; 197 | break; 198 | case 2: 199 | if ( clkIndex == 0 ) { 200 | Serial.print( F("MSYN0/Undefined:") ); 201 | } 202 | else { 203 | Serial.print( F("MSYN0/R:") ); 204 | } 205 | Serial.print( msynDiv = getMsynthFraction( 0 )); 206 | Serial.print( F(", ") ); 207 | break; 208 | case 3: 209 | default: 210 | Serial.print( F("MSYN" ) ); 211 | Serial.print( clkIndex, DEC ); 212 | Serial.print( F(":") ); 213 | Serial.print( msynDiv = getMsynthFraction( clkIndex )); 214 | Serial.print( F(", ") ); 215 | break; 216 | } 217 | Serial.print( F("Pwr=") ); 218 | Serial.print( uval & 0x03, DEC ); 219 | Serial.print( F(", Frequency=") ); 220 | if ( blnIntDiv4 ) { 221 | // Special divide by 4 222 | // msynDiv = 1.0; 223 | } 224 | 225 | Serial.print( active * pllFreq / ( msynDiv * intdiv )); 226 | Serial.println(F("")); 227 | 228 | } while ( 0 ); 229 | 230 | 231 | return result; 232 | } 233 | 234 | // 235 | // CAUTION - USE OF DOUBLE OR FLOAT GREATLY INCREASES BUILD SIZE 236 | // This is for diagnostic purposes only. 237 | // 238 | // 'float' does not have sufficient precision for values to be accurate 239 | // 240 | 241 | // The same register offsets work for both the PLL fractions 242 | // and the multi-synth dividers (in integer mode) 243 | // 244 | 245 | float getFractionalRatio( uint8_t register_base ) { 246 | uint32_t MSNx_P1; // 18 bit 247 | uint32_t MSNx_P2; // 20 bit 248 | uint32_t MSNx_P3; // 20 bit 249 | int16_t val; 250 | uint32_t uval; 251 | uint8_t reg = register_base; 252 | float result = -1.0; // returns -1.0 on error 253 | float abc; 254 | 255 | do { // while(0) - Artificial construct to allow break; 256 | 257 | uval = val = Si.i2cRead( reg++ ); // + 0 258 | if ( val < 0 ) { 259 | break; 260 | } 261 | MSNx_P3 = uval << 8; 262 | 263 | uval = val = Si.i2cRead( reg++ ); // + 1 264 | if ( val < 0 ) { 265 | break; 266 | } 267 | MSNx_P3 |= (uval & 0xFF); 268 | 269 | uval = val = Si.i2cRead( reg++ ); // + 2 270 | if ( val < 0 ) { 271 | break; 272 | } 273 | MSNx_P1 = ( uval & 0x03) << 16; 274 | 275 | uval = val = Si.i2cRead( reg++ ); // + 3 276 | if ( val < 0 ) { 277 | break; 278 | } 279 | MSNx_P1 |= ((uval & 0xFF) << 8); 280 | 281 | uval = val = Si.i2cRead( reg++ ); // + 4 282 | if ( val < 0 ) { 283 | break; 284 | } 285 | MSNx_P1 |= (uval & 0xFF); 286 | 287 | uval = val = Si.i2cRead( reg++ ); // + 5 288 | if ( val < 0 ) { 289 | break; 290 | } 291 | MSNx_P3 |= ((uval & 0xF0) << 12); 292 | MSNx_P2 = ((uval & 0x0F) << 16); 293 | 294 | uval = val = Si.i2cRead( reg++ ); // + 6 295 | if ( val < 0 ) { 296 | break; 297 | } 298 | MSNx_P2 |= ((uval & 0xFF) << 8); 299 | 300 | uval = val = Si.i2cRead( reg++ ); // + 7 301 | if ( val < 0 ) { 302 | break; 303 | } 304 | MSNx_P2 |= (uval & 0xFF); 305 | 306 | // Re-arranging equations to solve ( a + b/c ) 307 | // (a + b/c) = ( P1 + P2/c + 512.0 ) / 128.0 308 | 309 | if ( MSNx_P3 == 0 ) { 310 | // divide by zero error - c == 0 311 | break; 312 | } 313 | 314 | else // above is the last if() that can fail/break, calculate result. 315 | { 316 | abc = ( MSNx_P1 + ( (float) MSNx_P2 ) / MSNx_P3 + 512 ) / 128.0; 317 | 318 | result = abc; 319 | } 320 | 321 | } while(0); 322 | 323 | return result; 324 | } 325 | 326 | // PLL Fractions are Multipliers of the clock frequency 327 | float getPllFraction( uint8_t pllIndex ) { 328 | // this is a test harness - no validation 329 | 330 | return getFractionalRatio( 26 + 8 * pllIndex ); 331 | } 332 | 333 | // Multisynth fractions are Dividers of the providing PLL 334 | float getMsynthFraction( uint8_t msIndex ) { 335 | // this is a test harness - no validation 336 | 337 | float result; 338 | if ( msIndex < 6 ) { 339 | result = getFractionalRatio( 42 + 8 * msIndex ); 340 | } 341 | else { 342 | // different equations for integer only MSYTH 6 and 7 343 | // registers 90/92 and 91/93 344 | result = -1.0; // not implemented yet 345 | } 346 | return result; 347 | } 348 | 349 | // 350 | void printByte( int16_t val ) { 351 | char buf[6] = { 0 }; 352 | 353 | if ( val < 0 ) { 354 | sprintf( buf, "??" ); 355 | } 356 | else { 357 | sprintf( buf, "%02X", (uint8_t) val); 358 | } 359 | Serial.print( buf ); 360 | } 361 | 362 | // 363 | void printDecimalByte( int16_t val ) { 364 | char buf[6] = { 0 }; 365 | 366 | if ( val < 0 ) { 367 | sprintf( buf, "???" ); 368 | } 369 | else { 370 | sprintf( buf, "%3d", (uint8_t) val); 371 | } 372 | Serial.print( buf ); 373 | } 374 | 375 | // 376 | // Displays a hexadecimal data dump of a byte range of registers 377 | // 378 | int cmdSubsetDump( int start, int count, int stride ) { 379 | int j, k; 380 | int val; 381 | 382 | for ( j = start; j < (start + stride * count); j+= stride ) { 383 | printByte( j ); 384 | Serial.print(": "); 385 | for ( k = 0; k < stride; k++ ) { 386 | val = Si.i2cRead( j + k ); 387 | printByte( val ); 388 | Serial.print(" "); 389 | } 390 | Serial.println(F("")); 391 | } 392 | return 0; 393 | } 394 | 395 | // 396 | // Show in HEX, all registers 397 | // 398 | int cmdRegDump( void ) { 399 | return cmdSubsetDump( 0, 16, 16 ); 400 | } 401 | 402 | // 403 | // Show in HEX, the registers for a PLL multiplier 404 | // 405 | int cmdPllDump( void ) { 406 | return cmdSubsetDump( 26, 2, 8 ); 407 | } 408 | 409 | // 410 | // Show in HEX, the registers for a MSYNTH divider 411 | // 412 | int cmdSynthDump( void ) { 413 | return cmdSubsetDump( 42, SICHANNELS, 8 ); 414 | } 415 | 416 | // 417 | // Show general characteristics from the register state; 418 | // floats will be inaccurate and are for guidance only 419 | // 420 | int cmdShow( void ) { 421 | int j; 422 | float frq; 423 | 424 | Serial.print("Selected Command Channel Output: "); 425 | Serial.println( output_chan, DEC ); 426 | Serial.println(F("")); 427 | Serial.println(F("XTAL:")); 428 | Serial.print(F(" Base: ")); 429 | Serial.println( Si.getXtalBase(), DEC ); 430 | Serial.print(F(" Current: ")); 431 | Serial.println( Si.getXtalCurrent(), DEC ); 432 | Serial.println(F("")); 433 | Serial.println(F("PLL ratios:")); 434 | Serial.print(F(" A ")); 435 | Serial.print( frq = getPllFraction( 0 )); 436 | Serial.print(F(" ")); 437 | Serial.println( frq * Si.getXtalCurrent() ); 438 | Serial.print(F(" B ")); 439 | Serial.print( frq = getPllFraction( 1 )); 440 | Serial.print(F(" ")); 441 | Serial.println( frq * Si.getXtalCurrent() ); 442 | Serial.println(F("")); 443 | 444 | for( j = 0; j < SICHANNELS; j++ ) { 445 | displayClockSource( j ); 446 | } 447 | return 0; 448 | } 449 | 450 | /* 451 | int cmdCal() { 452 | // Does not seem to work 453 | Si.off(); 454 | Si.enable(0); 455 | Si.i2cWrite( 16, 0 ); // Send XTAL direct to Output 0 456 | 457 | return 0; 458 | } 459 | */ 460 | 461 | int cmdClkChanSet( int32_t chan ) { 462 | if ( chan < SICHANNELS && chan >= 0 ) { 463 | output_chan = chan; 464 | } 465 | else { 466 | printError("Invalid output channel chosen."); 467 | } 468 | return 0; 469 | 470 | } 471 | 472 | int cmdFrqSet( int32_t frq ) { 473 | if ( frq <= 0 ) { 474 | Si.disable(output_chan); 475 | printError("Disabled - Frequency <= 0."); 476 | } 477 | else { 478 | Si.setFreq(output_chan, frq); 479 | Serial.print(F("FRQ: Frequency set to ")); 480 | Serial.print( frq , DEC ); 481 | Serial.println(F(" Hz")); 482 | 483 | if ( frq < 8000 ) { 484 | printWarning(("Frequencies below around 8000 Hz may not function correctly.")); 485 | printWarning(("The exact value depends on your XTAL frequency.")); 486 | } 487 | if ( frq > 200000000 ) { 488 | printWarning(("Frequencies above 200MHz may not function correctly.")); 489 | printWarning(("The exact value depends on your XTAL frequency and overclock settings.")); 490 | } 491 | } 492 | return 0; 493 | } 494 | 495 | int cmdPwrSet( int32_t power ) { 496 | Si.setPower(output_chan, power); 497 | return 0; 498 | } 499 | 500 | int cmdEna( void ) { 501 | Si.enable(output_chan); 502 | return 0; 503 | } 504 | 505 | int cmdDis( void ) { 506 | Si.disable(output_chan); 507 | return 0; 508 | } 509 | 510 | int cmdOff( void ) { 511 | Si.off(); 512 | return 0; 513 | } 514 | 515 | int cmdReset( void ) { 516 | Si.reset(); 517 | return 0; 518 | } 519 | 520 | int cmdClkSetState( uint8_t clk, uint8_t state ) { 521 | return 0; 522 | } 523 | 524 | int cmdRegSet( int32_t val, int32_t val2 ) { 525 | int result = 0; 526 | 527 | if ( val < 0 || val > 255 || val2 < 0 || val2 > 255 ) { 528 | result = -1; 529 | } 530 | else 531 | { 532 | printWarning("Use of SET may cause the library and this tool to " \ 533 | "display results inconsistent with the actual output."); 534 | 535 | Si.i2cWrite( val, val2 ); 536 | } 537 | 538 | return result; 539 | } 540 | 541 | int cmdXtal( int32_t val, int32_t val2 ) { 542 | int result = -1; 543 | 544 | if ( val < 10000000 || val > 32000000 ) { 545 | printError("XTAL frequency should be between 10 MHz and 32 MHz."); 546 | } 547 | else if ( val2 < -1000000 || val2 > 1000000 ) { 548 | printError("XTAL error should be +/- 1 MHz."); 549 | } 550 | else 551 | { 552 | printWarning("Si.init() will be called."); 553 | Si.init( val ); 554 | Si.correction( val2 ); 555 | result = 0; 556 | } 557 | 558 | return result; 559 | } 560 | 561 | int cmdHelp( void ) { 562 | Serial.println(F("Usage:")); 563 | Serial.println(F(" HELP - Displays this help message.")); 564 | // Serial.println(F(" CAL - Calibration Check - Output direct XTAL on Channel 0.")); 565 | Serial.println(F(" CHAN <0|1|2> - Select output parameter channel.")); 566 | Serial.println(F(" DIS - Disable selected output.")); 567 | Serial.println(F(" ENA - Enable selected output.")); 568 | Serial.println(F(" FRQ - Set selected parameter channel frequency to N Hz.")); 569 | Serial.println(F(" OFF - Disable all outputs.")); 570 | Serial.println(F(" PLLDUMP - Display Si5351 PLL registers.")); 571 | Serial.println(F(" PWR <0-3> - Set output current for selected output channel.")); 572 | Serial.println(F(" REGDUMP - Display Si5351 all registers (hex).")); 573 | Serial.println(F(" RESET - PLL Reset Si5351.")); 574 | Serial.println(F(" SET - Set Si5351 register 'R' to 'N' (decimal).")); 575 | Serial.println(F(" SHOW - Show present parameters.")); 576 | Serial.println(F(" MSYNDUMP - Display Si5351 MSynth registers.")); 577 | Serial.println(F(" XTAL - Set XTAL Clock Frequency, to Hz, with error offset Hz.")); 578 | 579 | return 0; 580 | } 581 | 582 | // 583 | // Take a command that came from the serial input, and 584 | // execute it, if accepted. 585 | // 586 | int cmdProcess( int nargs ) { 587 | int done = -1; 588 | int32_t val = 0; 589 | int32_t val2 = 0; 590 | 591 | switch( nargs ) 592 | { 593 | case 1: 594 | if ( !strcmp( "HELP", args[0] ) || !strcmp("?", args[0] ) ) { 595 | done = cmdHelp(); 596 | } 597 | /* 598 | else if ( !strcmp( "CAL", args[0] ) ) { 599 | done = cmdCal(); 600 | } 601 | */ 602 | else if ( !strcmp( "ENA", args[0] ) ) { 603 | done = cmdEna(); 604 | } 605 | else if ( !strcmp( "DIS", args[0] ) ) { 606 | done = cmdDis(); 607 | } 608 | else if ( !strcmp( "OFF", args[0] ) ) { 609 | done = cmdOff(); 610 | } 611 | else if ( !strcmp( "RESET", args[0] ) ) { 612 | done = cmdReset(); 613 | } 614 | else if ( !strcmp( "SHOW", args[0] ) ) { 615 | done = cmdShow(); 616 | } 617 | else if ( !strcmp( "REGDUMP", args[0] ) ) { 618 | done = cmdRegDump(); 619 | } 620 | else if ( !strcmp( "PLLDUMP", args[0] ) ) { 621 | done = cmdPllDump(); 622 | } 623 | else if ( !strcmp( "MSYNDUMP", args[0] ) ) { 624 | done = cmdSynthDump(); 625 | } 626 | break; 627 | 628 | case 2: 629 | val = atol( args[1] ); 630 | 631 | if ( !strcmp( "CHAN", args[0] ) ) { 632 | done = cmdClkChanSet( val ); 633 | } 634 | else if ( !strcmp( "FRQ", args[0] ) ) { 635 | done = cmdFrqSet( val ); 636 | } 637 | else if ( !strcmp( "PWR", args[0] ) ) { 638 | done = cmdPwrSet( val ); 639 | } 640 | break; 641 | 642 | case 3: 643 | val = atol( args[1] ); 644 | val2 = atol( args[2] ); 645 | if ( !strcmp( "SET", args[0] ) ) { 646 | done = cmdRegSet( val, val2 ); 647 | } 648 | else if ( !strcmp( "XTAL", args[0] ) ) { 649 | done = cmdXtal( val, val2 ); 650 | } 651 | 652 | break; 653 | 654 | default: 655 | break; 656 | 657 | } 658 | if ( done != 0 ) { 659 | printError("FAIL: Command not recognised. Not processed."); 660 | } 661 | else { 662 | Serial.println(F("OK")); 663 | } 664 | } 665 | 666 | // 667 | // take a command from serial input, and send to cmdProcess() 668 | // 669 | void cmdParseAndProcess(void) { 670 | char *p; 671 | int j = 0; 672 | int k; 673 | 674 | p = strtok( inputBuffer, " "); 675 | while ( *p ) 676 | { 677 | args[j++] = p; 678 | p = strtok( 0, " "); 679 | } 680 | for ( k = 0; k < j; k++ ) 681 | { 682 | p = args[k]; 683 | while ( *p ) { 684 | *p++ = toupper( *p ); 685 | } 686 | } 687 | if ( j ) { 688 | cmdProcess( j ); 689 | } 690 | resetInput(); 691 | } 692 | 693 | void loop() { 694 | char c; 695 | 696 | if ( !devicePresent ) { 697 | return ; 698 | } 699 | 700 | while ( Serial.available() > 0 ) { 701 | c = Serial.read(); 702 | if ( c == 13 ) 703 | { 704 | inputBuffer[ inputIndex ] = '\0'; 705 | Serial.println(inputBuffer); 706 | cmdParseAndProcess(); 707 | } 708 | else 709 | { 710 | inputBuffer[ inputIndex++ ] = c; 711 | } 712 | if ( inputIndex > MAXINPUT ) 713 | { 714 | printError("Input overflow."); 715 | } 716 | } 717 | } 718 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Si5351mcu KEYWORD1 2 | 3 | init KEYWORD2 4 | correction KEYWORD2 5 | i2cWrite KEYWORD2 6 | reset KEYWORD2 7 | setFreq KEYWORD2 8 | enable KEYWORD2 9 | disable KEYWORD2 10 | setPower KEYWORD2 11 | clkOn KEYWORD2 12 | 13 | SIXTAL LITERAL1 14 | SIADDR LITERAL1 15 | SIOUT_2mA LITERAL1 16 | SIOUT_4mA LITERAL1 17 | SIOUT_6mA LITERAL1 18 | SIOUT_8mA LITERAL1 19 | SICLK0_R LITERAL1 20 | SICLK12_R LITERAL1 21 | SI_OVERCLOCK LITERAL1 22 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Si5351mcu 2 | version=0.7.1 3 | author=Pavel Milanes 4 | maintainer=Pavel Milanes 5 | sentence=A MCU friendly library for the Si5351A clock generator ICs from Silicon Labs. 6 | paragraph=This library is optimized for size over the Arduino platform; will allow you to control any of the three out of the Si5351 outputs (but only two at the same time). It's click FREE while tuning and really small. 7 | category=Device Control 8 | url=https://github.com/pavelmc/Si5351mcu 9 | architectures=* 10 | -------------------------------------------------------------------------------- /licence.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/si5351mcu.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * si5351mcu - Si5351 library for Arduino MCU tuned for size and click-less 3 | * 4 | * Copyright (C) 2017 Pavel Milanes 5 | * 6 | * Many chunk of codes are derived-from/copied from other libs 7 | * all GNU GPL licenced: 8 | * - Linux Kernel (www.kernel.org) 9 | * - Hans Summers libs and demo code (qrp-labs.com) 10 | * - Etherkit (NT7S) Si5351 libs on github 11 | * - DK7IH example. 12 | * - Jerry Gaffke integer routines for the bitx20 group 13 | * 14 | * This program is free software: you can redistribute it and/or modify 15 | * it under the terms of the GNU General Public License as published by 16 | * the Free Software Foundation, either version 3 of the License, or 17 | * (at your option) any later version. 18 | * 19 | * This program is distributed in the hope that it will be useful, 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | * GNU General Public License for more details. 23 | * 24 | * You should have received a copy of the GNU General Public License 25 | * along with this program. If not, see . 26 | */ 27 | 28 | #include "Arduino.h" 29 | #include "si5351mcu.h" 30 | 31 | // wire library loading, if not defined 32 | #ifndef WIRE_H 33 | #include "Wire.h" 34 | #endif 35 | 36 | /***************************************************************************** 37 | * This is the default init procedure, it set the Si5351 with this params: 38 | * XTAL 27.000 Mhz 39 | *****************************************************************************/ 40 | void Si5351mcu::init(void) { 41 | // init with the default freq 42 | init(int_xtal); 43 | } 44 | 45 | 46 | /***************************************************************************** 47 | * This is the custom init procedure, it's used to pass a custom xtal 48 | * and has the duty of init the I2C protocol handshake 49 | *****************************************************************************/ 50 | void Si5351mcu::init(uint32_t nxtal) { 51 | // set the new base xtal freq 52 | base_xtal = int_xtal = nxtal; 53 | 54 | // start I2C (wire) procedures 55 | Wire.begin(); 56 | 57 | // shut off the spread spectrum by default, DWaite contibuted code 58 | uint8_t regval; 59 | regval = i2cRead(149); 60 | regval &= ~0x80; // set bit 7 LOW to turn OFF spread spectrum mode 61 | i2cWrite(149, regval); 62 | 63 | // power off all the outputs 64 | off(); 65 | } 66 | 67 | 68 | /***************************************************************************** 69 | * This function set the freq of the corresponding clock. 70 | * 71 | * In my tests my Si5351 can work between 7,8 Khz and ~225 Mhz [~250 MHz with 72 | * overclocking] as usual YMMV 73 | * 74 | * Click noise: 75 | * - The lib has a reset programmed [aka: click noise] every time it needs to 76 | * change the output divider of a particular MSynth, if you move in big steps 77 | * this can lead to an increased rate of click noise per tunning step. 78 | * - If you move at a pace of a few Hz each time the output divider will 79 | * change at a low rate, hence less click noise per tunning step. 80 | * - The output divider moves [change] faster at high frequencies, so at HF the 81 | * clikc noise is at the real minimum possible. 82 | * 83 | * [See the README.md file for other details] 84 | ****************************************************************************/ 85 | void Si5351mcu::setFreq(uint8_t clk, uint32_t freq) { 86 | uint8_t a, R = 1, pll_stride = 0, msyn_stride = 0; 87 | uint32_t b, c, f, fvco, outdivider; 88 | uint32_t MSx_P1, MSNx_P1, MSNx_P2, MSNx_P3; 89 | 90 | // Overclock option 91 | #ifdef SI_OVERCLOCK 92 | // user a overclock setting for the VCO, max value in my hardware 93 | // was 1.05 to 1.1 GHz, as usual YMMV [See README.md for details] 94 | outdivider = SI_OVERCLOCK / freq; 95 | #else 96 | // normal VCO from the datasheet and AN 97 | // With 900 MHz beeing the maximum internal PLL-Frequency 98 | outdivider = 900000000 / freq; 99 | #endif 100 | 101 | // use additional Output divider ("R") 102 | while (outdivider > 900) { 103 | R = R * 2; 104 | outdivider = outdivider / 2; 105 | } 106 | 107 | // finds the even divider which delivers the intended Frequency 108 | if (outdivider % 2) outdivider--; 109 | 110 | // Calculate the PLL-Frequency (given the even divider) 111 | fvco = outdivider * R * freq; 112 | 113 | // Convert the Output Divider to the bit-setting required in register 44 114 | switch (R) { 115 | case 1: R = 0; break; 116 | case 2: R = 16; break; 117 | case 4: R = 32; break; 118 | case 8: R = 48; break; 119 | case 16: R = 64; break; 120 | case 32: R = 80; break; 121 | case 64: R = 96; break; 122 | case 128: R = 112; break; 123 | } 124 | 125 | // we have now the integer part of the output msynth 126 | // the b & c is fixed below 127 | 128 | MSx_P1 = 128 * outdivider - 512; 129 | 130 | // calc the a/b/c for the PLL Msynth 131 | /*************************************************************************** 132 | * We will use integer only on the b/c relation, and will >> 5 (/32) both 133 | * to fit it on the 1048 k limit of C and keep the relation 134 | * the most accurate possible, this works fine with xtals from 135 | * 24 to 28 Mhz. 136 | * 137 | * This will give errors of about +/- 2 Hz maximum 138 | * as per my test and simulations in the worst case, well below the 139 | * XTAl ppm error... 140 | * 141 | * This will free more than 1K of the final eeprom 142 | * 143 | ****************************************************************************/ 144 | a = fvco / int_xtal; 145 | b = (fvco % int_xtal) >> 5; // Integer part of the fraction 146 | // scaled to match "c" limits 147 | c = int_xtal >> 5; // "c" scaled to match it's limits 148 | // in the register 149 | 150 | // f is (128*b)/c to mimic the Floor(128*(b/c)) from the datasheet 151 | f = (128 * b) / c; 152 | 153 | // build the registers to write 154 | MSNx_P1 = 128 * a + f - 512; 155 | MSNx_P2 = 128 * b - f * c; 156 | MSNx_P3 = c; 157 | 158 | // PLLs and CLK# registers are allocated with a stride, we handle that with 159 | // the stride var to make code smaller 160 | if (clk > 0 ) pll_stride = 8; 161 | 162 | // HEX makes it easier to human read on bit shifts 163 | uint8_t reg_bank_26[] = { 164 | (MSNx_P3 & 0xFF00) >> 8, // Bits [15:8] of MSNx_P3 in register 26 165 | MSNx_P3 & 0xFF, 166 | (MSNx_P1 & 0x030000L) >> 16, 167 | (MSNx_P1 & 0xFF00) >> 8, // Bits [15:8] of MSNx_P1 in register 29 168 | MSNx_P1 & 0xFF, // Bits [7:0] of MSNx_P1 in register 30 169 | ((MSNx_P3 & 0x0F0000L) >> 12) | ((MSNx_P2 & 0x0F0000) >> 16), // Parts of MSNx_P3 and MSNx_P1 170 | (MSNx_P2 & 0xFF00) >> 8, // Bits [15:8] of MSNx_P2 in register 32 171 | MSNx_P2 & 0xFF // Bits [7:0] of MSNx_P2 in register 33 172 | }; 173 | 174 | // We could do this here - but move it next to the reg_bank_42 write 175 | // i2cWriteBurst(26 + pll_stride, reg_bank_26, sizeof(reg_bank_26)); 176 | 177 | // Write the output divider msynth only if we need to, in this way we can 178 | // speed up the frequency changes almost by half the time most of the time 179 | // and the main goal is to avoid the nasty click noise on freq change 180 | if (omsynth[clk] != outdivider || o_Rdiv[clk] != R ) { 181 | 182 | // CLK# registers are exactly 8 * clk# bytes stride from a base register. 183 | msyn_stride = clk * 8; 184 | 185 | // keep track of the change 186 | omsynth[clk] = (uint16_t) outdivider; 187 | o_Rdiv[clk] = R; // cache it now, before we OR mask up R for special divide by 4 188 | 189 | // See datasheet, special trick when MSx == 4 190 | // MSx_P1 is always 0 if outdivider == 4, from the above equations, so there is 191 | // no need to set it to 0. ... MSx_P1 = 128 * outdivider - 512; 192 | // 193 | // See para 4.1.3 on the datasheet. 194 | // 195 | 196 | if ( outdivider == 4 ) { 197 | R |= 0x0C; // bit set OR mask for MSYNTH divide by 4, for reg 44 {3:2] 198 | } 199 | 200 | // HEX makes it easier to human read on bit shifts 201 | uint8_t reg_bank_42[] = { 202 | 0, // Bits [15:8] of MS0_P3 (always 0) in register 42 203 | 1, // Bits [7:0] of MS0_P3 (always 1) in register 43 204 | ((MSx_P1 & 0x030000L ) >> 16) | R, // Bits [17:16] of MSx_P1 in bits [1:0] and R in [7:4] | [3:2] 205 | (MSx_P1 & 0xFF00) >> 8, // Bits [15:8] of MSx_P1 in register 45 206 | MSx_P1 & 0xFF, // Bits [7:0] of MSx_P1 in register 46 207 | 0, // Bits [19:16] of MS0_P2 and MS0_P3 are always 0 208 | 0, // Bits [15:8] of MS0_P2 are always 0 209 | 0 // Bits [7:0] of MS0_P2 are always 0 210 | }; 211 | 212 | // Get the two write bursts as close together as possible, 213 | // to attempt to reduce any more click glitches. This is 214 | // at the expense of only 24 increased bytes compilation size in AVR 328. 215 | // Everything is already precalculated above, reducing any delay, 216 | // by not doing calculations between the burst writes. 217 | 218 | i2cWriteBurst(26 + pll_stride, reg_bank_26, sizeof(reg_bank_26)); 219 | i2cWriteBurst(42 + msyn_stride, reg_bank_42, sizeof(reg_bank_42)); 220 | 221 | // 222 | // https://www.silabs.com/documents/public/application-notes/Si5350-Si5351%20FAQ.pdf 223 | // 224 | // 11.1 "The Int, R and N register values inside the Interpolative Dividers are updated 225 | // when the LSB of R is written via I2C." - Q. does this mean reg 44 or 49 (misprint ?) ??? 226 | // 227 | // 10.1 "All outputs are within +/- 500ps of one another at power up (if pre-programmed) 228 | // or if PLLA and PLLB are reset simultaneously via register 177." 229 | // 230 | // 17.1 "The PLL can track any abrupt input frequency changes of 3–4% without losing 231 | // lock to it. Any input frequency changes greater than this amount will not 232 | // necessarily track from the input to the output 233 | 234 | // must reset the so called "PLL", in fact the output msynth 235 | reset(); 236 | 237 | } 238 | else { 239 | i2cWriteBurst(26 + pll_stride, reg_bank_26, sizeof(reg_bank_26)); 240 | } 241 | 242 | } 243 | 244 | 245 | /***************************************************************************** 246 | * Reset of the PLLs and multisynths output enable 247 | * 248 | * This must be called to soft reset the PLLs and cycle the output of the 249 | * multisynths: this is the "click" noise source in the RF spectrum. 250 | * 251 | * So it must be avoided at all costs, so this lib just call it at the 252 | * initialization of the PLLs and when a correction is applied 253 | * 254 | * If you are concerned with accuracy you can implement a reset every 255 | * other Mhz to be sure it get exactly on spot. 256 | ****************************************************************************/ 257 | void Si5351mcu::reset(void) { 258 | // This soft-resets PLL A & B (32 + 128) in just one step 259 | i2cWrite(177, 0xA0); 260 | } 261 | 262 | 263 | /***************************************************************************** 264 | * Function to disable all outputs 265 | * 266 | * The PLL are kept running, just the m-synths are powered off. 267 | * 268 | * This allows to keep the chip warm and exactly on freq the next time you 269 | * enable an output. 270 | ****************************************************************************/ 271 | void Si5351mcu::off(void) { 272 | // This disable all the CLK outputs 273 | for (byte i=0; i < SICHANNELS; i++) { 274 | disable(i); 275 | } 276 | } 277 | 278 | 279 | /***************************************************************************** 280 | * Function to set the correction in Hz over the Si5351 XTAL. 281 | * 282 | * This will call a reset of the PLLs and multi-synths so it will produce a 283 | * click every time it's called 284 | ****************************************************************************/ 285 | void Si5351mcu::correction(int32_t diff) { 286 | // apply some corrections to the xtal 287 | int_xtal = base_xtal + diff; 288 | 289 | // reset the PLLs to apply the correction 290 | reset(); 291 | } 292 | 293 | 294 | /***************************************************************************** 295 | * This function enables the selected output 296 | * 297 | * Beware: ZERO is clock output enabled, in register 16+CLK 298 | *****************************************************************************/ 299 | void Si5351mcu::enable(uint8_t clk) { 300 | // var to handle the mask of the registers value 301 | uint8_t m = SICLK0_R; 302 | 303 | if (clk > 0) { 304 | m = SICLK12_R; 305 | } 306 | 307 | // write the register value 308 | i2cWrite(16 + clk, m + clkpower[clk]); 309 | 310 | // 1 & 2 are mutually exclusive 311 | if (clk == 1) disable(2); 312 | if (clk == 2) disable(1); 313 | 314 | // update the status of the clk 315 | clkOn[clk] = 1; 316 | } 317 | 318 | 319 | /***************************************************************************** 320 | * This function disables the selected output 321 | * 322 | * Beware: ONE is clock output disabled, in register 16+CLK 323 | * *****************************************************************************/ 324 | void Si5351mcu::disable(uint8_t clk) { 325 | // send 326 | i2cWrite(16 + clk, 0x80); 327 | 328 | // update the status of the clk 329 | clkOn[clk] = 0; 330 | } 331 | 332 | 333 | /**************************************************************************** 334 | * Set the power output for each output independently 335 | ***************************************************************************/ 336 | void Si5351mcu::setPower(uint8_t clk, uint8_t power) { 337 | // set the power to the correct var 338 | clkpower[clk] = power; 339 | 340 | // now enable the output to get it applied 341 | enable(clk); 342 | } 343 | 344 | /**************************************************************************** 345 | * method to send multi-byte burst register data. 346 | ***************************************************************************/ 347 | uint8_t Si5351mcu::i2cWriteBurst( const uint8_t start_register, 348 | const uint8_t *data, 349 | const uint8_t numbytes) { 350 | 351 | // This method saves the massive overhead of having to keep opening 352 | // and closing the I2C bus for consecutive register writes. It 353 | // also saves numbytes - 1 writes for register address selection. 354 | Wire.beginTransmission(SIADDR); 355 | 356 | Wire.write(start_register); 357 | Wire.write(data, numbytes); 358 | // All of the bytes queued up in the above write() calls are buffered 359 | // up and will be sent to the slave in one "burst", on the call to 360 | // endTransmission(). This also sends the I2C STOP to the Slave. 361 | return Wire.endTransmission(); 362 | // returns non zero on error 363 | } 364 | 365 | /**************************************************************************** 366 | * function to send the register data to the Si5351, arduino way. 367 | ***************************************************************************/ 368 | void Si5351mcu::i2cWrite( const uint8_t regist, const uint8_t value) { 369 | // Using the "burst" method instead of 370 | // doing it longhand saves a few bytes 371 | i2cWriteBurst( regist, &value, 1); 372 | 373 | } 374 | 375 | /**************************************************************************** 376 | * Read i2C register, returns -1 on error or timeout 377 | ***************************************************************************/ 378 | int16_t Si5351mcu::i2cRead( const uint8_t regist ) { 379 | int value; 380 | 381 | Wire.beginTransmission(SIADDR); 382 | Wire.write(regist); 383 | Wire.endTransmission(); 384 | 385 | Wire.requestFrom(SIADDR, 1); 386 | if ( Wire.available() ) { 387 | value = Wire.read(); 388 | } 389 | else { 390 | value = -1; // "EOF" in C 391 | } 392 | 393 | return value; 394 | } 395 | -------------------------------------------------------------------------------- /src/si5351mcu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * si5351mcu - Si5351 library for Arduino MCU tuned for size and click-less 3 | * 4 | * Copyright (C) 2017 Pavel Milanes 5 | * 6 | * Many chunk of codes are derived-from/copied from other libs 7 | * all GNU GPL licenced: 8 | * - Linux Kernel (www.kernel.org) 9 | * - Hans Summers libs and demo code (qrp-labs.com) 10 | * - Etherkit (NT7S) Si5351 libs on github 11 | * - DK7IH example. 12 | * - Jerry Gaffke integer routines for the bitx20 group 13 | * 14 | * This program is free software: you can redistribute it and/or modify 15 | * it under the terms of the GNU General Public License as published by 16 | * the Free Software Foundation, either version 3 of the License, or 17 | * (at your option) any later version. 18 | * 19 | * This program is distributed in the hope that it will be useful, 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | * GNU General Public License for more details. 23 | * 24 | * You should have received a copy of the GNU General Public License 25 | * along with this program. If not, see . 26 | */ 27 | 28 | 29 | /**************************************************************************** 30 | * This lib tricks: 31 | * 32 | * CLK0 will use PLLA 33 | * CLK1 will use PLLB 34 | * CLK2 will use PLLB 35 | * 36 | * Lib defaults 37 | * - XTAL is 27 Mhz. 38 | * - Always put the internal 8pF across the xtal legs to GND 39 | * - lowest power output (2mA) 40 | * - After the init all outputs are off, you need to enable them in your code. 41 | * 42 | * The correction procedure is not for the PPM as other libs, this 43 | * is just +/- Hz to the XTAL freq, you may get a click noise after 44 | * applying a correction 45 | * 46 | * The init procedure is mandatory as it set the Xtal (the default or a custom 47 | * one) and prepare the Wire (I2C) library for operation. 48 | ****************************************************************************/ 49 | 50 | #ifndef SI5351MCU_H 51 | #define SI5351MCU_H 52 | 53 | // rigor includes 54 | #include "Arduino.h" 55 | #include "Wire.h" 56 | 57 | // default I2C address of the Si5351A - other variants may differ 58 | #define SIADDR 0x60 59 | 60 | // The number of output channels - 3 for Si5351A 10 pin 61 | #define SICHANNELS 3 62 | 63 | // register's power modifiers 64 | #define SIOUT_2mA 0 65 | #define SIOUT_4mA 1 66 | #define SIOUT_6mA 2 67 | #define SIOUT_8mA 3 68 | 69 | // registers base (2mA by default) 70 | #define SICLK0_R 76 // 0b01001100 71 | #define SICLK12_R 108 // 0b01101100 72 | 73 | class Si5351mcu { 74 | private: 75 | // base xtal freq, over this we apply the correction factor 76 | // by default 27 MHz 77 | uint32_t base_xtal = 27000000L; 78 | 79 | // this is the work value, with the correction applied 80 | // via the correction() procedure 81 | uint32_t int_xtal = base_xtal; 82 | 83 | // clk# power holders (2ma by default) 84 | uint8_t clkpower[SICHANNELS] = { 0 }; 85 | 86 | // local var to keep track of when to reset the "pll" 87 | /********************************************************* 88 | * BAD CONCEPT on the datasheet and AN: 89 | * 90 | * The chip has a soft-reset for PLL A & B but in 91 | * practice the PLL does not need to be reseted. 92 | * 93 | * Test shows that if you fix the Msynth output 94 | * dividers and move any of the VCO from bottom to top 95 | * the frequency moves smooth and clean, no reset needed 96 | * 97 | * The reset is needed when you changes the value of the 98 | * Msynth output divider, even so it's not always needed 99 | * so we use this var to keep track of all three and only 100 | * reset the "PLL" when this value changes to be sure 101 | * 102 | * It's a word (16 bit) because the final max value is 900 103 | *********************************************************/ 104 | uint16_t omsynth[SICHANNELS] = { 0 }; 105 | uint8_t o_Rdiv[SICHANNELS] = { 0 }; 106 | 107 | public: 108 | // var to check the clock state 109 | bool clkOn[SICHANNELS] = { 0 }; // This should not really be public - use isEnabled() 110 | 111 | public: 112 | // default init procedure 113 | void init(void); 114 | 115 | // custom init procedure (XTAL in Hz); 116 | void init(uint32_t); 117 | 118 | // reset all PLLs 119 | static void reset(void); 120 | 121 | // set CLKx(0..2) to freq (Hz) 122 | void setFreq(uint8_t, uint32_t); 123 | 124 | // pass a correction factor 125 | void correction(int32_t); 126 | 127 | // enable some CLKx output 128 | void enable(uint8_t); 129 | 130 | // disable some CLKx output 131 | void disable(uint8_t); 132 | 133 | // disable all outputs 134 | void off(void); 135 | 136 | // set power output to a specific clk 137 | void setPower(uint8_t, uint8_t); 138 | 139 | // used to talk with the chip, via Arduino Wire lib 140 | // 141 | // declared as static, since they do not reference any this-> class attributes 142 | // 143 | static void i2cWrite( const uint8_t reg, const uint8_t val ); 144 | static uint8_t i2cWriteBurst( const uint8_t start_register, const uint8_t *data, const uint8_t numbytes ); 145 | static int16_t i2cRead( const uint8_t reg ); 146 | 147 | inline const bool isEnabled( const uint8_t channel ) { 148 | return channel < SICHANNELS && clkOn[ channel ] != 0; 149 | }; 150 | 151 | inline const uint8_t getPower( const uint8_t channel ) { 152 | return channel < SICHANNELS ? clkpower[ channel ] : 0; 153 | }; 154 | 155 | inline const uint32_t getXtalBase( void ) { 156 | return base_xtal; 157 | }; 158 | 159 | inline const uint32_t getXtalCurrent( void ) { 160 | return int_xtal; 161 | }; 162 | 163 | }; 164 | 165 | 166 | #endif //SI5351MCU_H 167 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | 0.7.1 2 | --------------------------------------------------------------------------------