Initial Commit

This commit is contained in:
cnlohr
2024-03-24 17:33:38 -07:00
commit 0cc3b139d0
82 changed files with 22400 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
*.h linguist-language=c
CMakeLists.txt linguist-language=idf
+6
View File
@@ -0,0 +1,6 @@
[submodule "esp8266/nosdk8266"]
path = esp8266/nosdk8266
url = https://github.com/cnlohr/nosdk8266
[submodule "ch32v/ch32v003fun"]
path = ch32v/ch32v003fun
url = https://github.com/cnlohr/ch32v003fun
+108
View File
@@ -0,0 +1,108 @@
PLEASE NOTE: Files in this project are under multiple licenses.
Most files are under a regular MIT license, BUT not the RF-related files.
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
FOR FILES MARKED WITH A NO-AI-TRAINING TAG, THEY ARE UNDER THIS LICENSE:
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
FOR THE AES LIBRARY:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
and for portions of LoRa-SDR-Code.h that have not yet been rewritten:
// Copyright (c) 2016-2016 Lime Microsystems
// Copyright (c) 2016-2016 Arne Hennig
// SPDX-License-Identifier: BSL-1.0
// from https://github.com/myriadrf/LoRa-SDR/blob/master/LoRaEncoder.cpp
+259
View File
@@ -0,0 +1,259 @@
# LoLRa
*Transmit 900MHz LoRa frames surprisingly far without a radio*
* [Introduction](#introduction)
* [Background](#background)
* [LoRaWAN](#lorawan)
* [Limitations](#limitations)
* [Future Work](#future_Work)
* [Resources](#resources)
* [Special Thanks](#special_thanks)
* [Range Tests](#range_tests)
## Introduction
Firmware-only LoRa transmission, for a variety of processors. Send LoRa packets, without any radio, chips, external hardware or built-in radios at all on a variety of common, inexpensive processors. While not truly bit banging, this repository shows how using either a shift register (i.e. I2S or SPI port) or an APLL, you can send lora packets that can be decoded by commercial off the shelf LoRa gateways and other chips.
> [!NOTE]
> This repo is designed for use with ITU Region 2 (The Americas) tageting 902-928MHz. Code changes are needed for use in Region 1 (EU, Russia, Afraica) to target 863-870MHz or 3 (Australia, China, India) to target 920-923MHz.
> [!CAUTION]
> Because we rely on harmdonics and aliasing, the primary frequency components emitted by your microcontroller are going to be in portions of the RF spectrum where RF transmissions are banned. Please filter your output or perform your tests in an area where you are unlikely to leak significant RF. The overall EIRP output is genreally ≪300uW across the whole spectrum spread out over hundreds of emission frequencies, but there is virtually no way a device deliberately transmitting on these frequencies could ever pass FCC part 15 compliance, even with filtering.
There are two major modes that this repository works with.
1. Transmission using a tunable PLL, creating a square wave, then using a harmonic (the 13th harmonic in the case of the ESP32-S2) and then transmitting the signal out a clock out pin.
2. Direct synthesis on a bitstream, a much more versatile method able to work on a myriad of microcontrollers. This creates an image of the intended bitstream up at 900 MHz, even in situations where the bitstream itself can be as low as 7.2 MSPS. But for most platforms, I've targeted between 24 and 173 MSPS.
Click Below for the Youtube Video version of this page:
[![LoRa Without A Radio](https://i3.ytimg.com/vi/eIdHBDSQHyw/maxresdefault.jpg)](https://www.youtube.com/watch?v=eIdHBDSQHyw)
> [!NOTE]
> Portions of code in this repo are under various licenses and cannot be trivially incorporated into other libraries without a bit of a mess, including a no-ai-training license for any of the RF-specific portions of the project. Be cautious when using code from this repo. See ![LICENSE](LICENSE) for more information.
## Background
### Square waves, and images
Any time a signal changes state from low to high or high to low, a disturbance is created in the electromagnetic fields surrounding that wire. Any time. The difference is, are you, as an engineer going to fear it, squelching it waddling, being afraid of whatever EMI it might cause, or are you going to grab the bull by its horns and emit some artesinally crafted signals? The major principles you will need to understand are:
* The actual emission of a wave is made from several frequency components at different frequencies and phases.
![Square 220 Hz](https://github.com/cnlohr/lolra/blob/master/resources/SquareHarmonics.png?raw=true)
* A pure sinewave has only its principle output wave in frequency space.
![Sine 220 Hz](https://github.com/cnlohr/lolra/blob/master/resources/SineHarmonics.png?raw=true)
* You can create a high frequency tone by generating a low frequency tone (Pictured is a 13th harmonic of at 69.420MHz signal)
![Single Tone](https://github.com/cnlohr/lolra/blob/master/resources/SineTop.png?raw=true)
* You can spread a signal over an area of spectrum by scanning it between frequencies. (There are other ways, but that isn't LoRa)
![Signal Spread](https://github.com/cnlohr/lolra/blob/master/resources/SquareTop.png?raw=true)
* Square waves are actually several "odd harmonic" frequencies all added together.
![Signal Going Up Spectrum](https://github.com/cnlohr/lolra/blob/master/resources/36mhz-up-the-spectrum.png?raw=true)
* It is important to note that many more cheap micros frequencies cut off rather quickly at high frequencies.
![Up Spectrum Sad](https://github.com/cnlohr/lolra/blob/master/resources/UpSpectrumSad.png?raw=true)
The second principle is signal mixing. If you create a signal, then "mix" it with a high frequency, you get an "image" wrapped around that high frequency. Mixing can be done with diodes, special RF equipment, or even just sampling, like that done by using a shift register, outputting bits at a steady rate.
![Signal Mixing](https://github.com/cnlohr/lolra/blob/master/resources/UpSpectrumImages.png?raw=true)
Now, the real magic happens, when you realize these two principles actually work together. You get an image at the base band, a reflected image around the sampling frequency, then around ×3 the sampling frequency, you get another 2 images, the forward and reverse. And ×5, and ×7, etc.
With this, with a precise enough clock, we can arbitrarily generate any frequency we wish, provided there's enough bandwidth left on the GPIO of our micro to generate it, even if the "actual" signal we're generating is much much lower in frequency.
#### A side-note: RC/RLC oscllators vs crystal oscillators.
Internal oscillators in microcontrollers aren't only inacacurate, but they also jitter around in frequency. You might think this a negative, but in fact, using the internal oscillator built into micros can often be a lifesaver, getting you past EMI/EMC. Because the internal oscillators aren't just imprecise but jittery, they prevent harmonics of individual frequencies higher up in the spectrum because the clock rate drifts around so heavily.
Crystal Output:
![Crystal Output](resources/CrystalOut.png)
RC Output:
![RC Output](resources/RCOut.png)
### LoRa signals (40,000' view)
See the section below for the nitty gritty of how LoRa signals *actually work* or the things that none of the other PhDs on the internet ever were willing to tell you about it.
LoRa typically can operate in 433MHz or the 900MHz spectrum, usually with 125kHz channels. In principle, LoRa creates chirps, starting at one frequency, 62.5kHz below the channel center, then over a short period of time (1.024uS at SF7) the tone creeps up to 62.5kHz above the channel center.
While LoRa can be used with many different channel widths, 125kHz and 500kHz are both very well supported, whereas other channel widths are not configurable with routers like the LR9.
![Bitstream Analysis](resources/bitstreamdiagram.png)
You can see from the above diagram in:
* **𝔸** our output on a platform like the CH32V203 is not perfect, that's because the SPI bus on the CH32V203 glitches by parts of a bit here and there. This causes other, weaker images in the output. But, largely we can produce totally valid and readable (at a long distance) LoRa packets.
* **𝔹** LoRa consists of several upchirps some in a preamble.
* **** two more upchirps with a phase offset indicating a sync word if we select 0x43 (or 0x34 depending on endain) for the upchirps here our packets will be decoded and potentially forwarded by commercial LoRa gateways.
* **𝔻** 2.25 down chrips in. That extra .25 causes some pain 😈 (the minimum logical unit is a quarter chirp, not a whole chirp).
* Then a payload where each upchirp is offset by a phase to convey information in **𝔼**.
Conveniently the window for a given chirp is stable depending on the spreading factor. For the above packet, with SF7, it works out to 1,024us per symbol, or for SF8, 2,048us per symbol. Each symbol/chirp can represent a number of bits, by a phase offset.
The raw "phase" of a chirp is grey-coded in order to better spread the bit error between bits to higher layers of the process. For instance, if you are off-by-one with regards to the phase you believe the chirp is at, it could be over a boundary from say 0b1111 and 0b10000 and would cause 5 bit errors. By grey coding it, it minimizes the bit errors created by an off-by-one or even a few of the phase.
After this raw bitstream is decoded from the individual chirps and de-grey-coded (see `encodeHamming84sx`) in [`LoRa-SDR-Code.h`](https://github.com/cnlohr/lolra/blob/master/lib/LoRa-SDR-Code.h), then we transpose/interleve the bits so any one symbol that could get taken out (see `diagonalInterleaveSx`) to spread any errors out so a single lost symbol can be recovered, whitened (I believe this is actually a worthless step in this protocol, correct me if I'm wrong) see `Sx1272ComputeWhitening`. Above whitening is an error correction layer to help fix any of the bit errors taht could happen at a lower layer (see `encodeFec`).
Overall the messages have a header, and a payload. Note that this can be a little tricky, because the header sometimes uses different encoding settings than the payload. And that's it.
Once you generate properly formatted packets, you can encode them into chirps and transmit them out the wire.
A more detailed view of the protocol can be found [here, for a more academic view](https://dl.acm.org/doi/10.1145/3546869) and [here for some better examples](https://chrisye-liu.github.io/files/yang22emu.pdf) (though I have found some issues with the correctness/clarity of both documents).
### Starting the Project
I started the project with an ESP32-S2 to see if I could output a signal using the internal built-in APLL, and routing the APLL/2 clock out via the IOMUX, and the answer was I could. Because this generates a simple square wave, and square waves have harmonics at F×3, F×5, F×7, etc... up the spectrum, if I set the APLL to 139.06 MHz, it outputs 69.53MHz. The 13th harmonic is 903.9 MHz, or the first 125kHz LoRa channel. Then by tuning the least significant PLL control bits, we can tune it from 903.9 MHz - 62.5kHz to 903.9 + 62.5kHz, by tuning the APLL to 139.06 MHz - 9.62kHz to 139.06 MHz + 9.62 kHz. This lets us generate the characteristic LoRa chirps and indeed this is receivable!
The ESP32-S2 also has another trick - the gpio mux is capable of outputting a signal or the inverse of that signal. That way we can differentially create the 139.06MHz signal, boosting the power output by 3dB!
There are issues with the ESP32-S2, however. Notably that:
1. The APLL is quite rough since it's actually an analog device fundamentally and struggles to keep tight control on the output signal.
2. Its output looks very unusual, and is something that fundamentally limits the performance of the sending of frames.
3. Because it is operating down at the F÷13 node, it has to operate over a very, very small window, ±9.62 kHz, which is quite challenging.
Additioanlly, very few processors even have an APLL, so in spite of this fast success, I decided to move onto direct synthesis.
### Direct bitstream synthesis.
Several years ago, I did a number of projects that used direct bitstream synthesis to do a few things, like [Broadcasting RF Color NTSC television on Channel 3 with an ESP8266](https://www.youtube.com/watch?v=bcez5pcp55w) or [Using Ethernet Packets to transmit AM radio](https://www.youtube.com/watch?v=-7jlRfqaYuY). One of the neat tricks is, if you transmit a bitstream out on an SPI or I2S shift register, it causes aliasing at the sample rate, with images at F×3, F×5, F×7, etc. But, the neat part is it preserves the size/shape of the transmitted waveform at images/aliases up the spectrum. For Channel 3, the 65MHz signal was being refelcted around the 40MHz sampling rate. Harry Nyquist can go bite a lemon.
This technique gives an incredible amount of fidelity even in extremely poor situations, unintuitively so. You can create surprisingly precise signals way outside anywhere you would expect them.
There are several ways to accomplish this, but typically it's easiest with a shift register. A shift register like that in an I2S or SPI bus. And, if you use DMA, you can easily feed the shift register with more data without waking the CPU up every cycle. There are other ways, though, such as directly toggling an IO, or using a timer to turn an IO on and off at the right time, but it's easiest to write code to generate a bitstream and shift it out.
For shift registers, a few considerations must be made, such as making sure that the endianness and bit widths and memory arrangements are corret, but, in general, you can keep up, and unless there is lag, like time between each word, they are typically able to faithfully-enough represent a bit pattern on output to be transferred and shifted out of a pin.
The "lohrcut" described in the video involves writing a function that, given a point in time determines the amplitude of a signal. This function can be to determine the amplitude of a very high frequency signal, then, the sample rate can be whatever physically realizable sample rate that's avaialble. This will create an image of the high frequency signal at a much lower frequency signal, building it out of power between 0 and Fs/2.
Another concern is flash on some systems accesses inconsistently or doesn't work well at certain frequencies. In those cases, like on the ESP8266, the tables must be read into RAM and played from there.
## LoRaWAN
LoRa frames are totally encapsulated. If you wanted, we could stop here. You could even use a commercial gateway, but the frames could not be set to brokers like The Things Network. For instance, if you ran a raspberry pi gateway, you could just accept whatever old LoRa frames you wanted, but, we took this a step further by hleping the packets get forwarded around the world. LoRaWAN is "end to end" encryption, in that none of your neighbors, or gateways can read the messages. Though, it is curious - the things network CAN read youe messages because they have the encryption keys.
Conveniently, there is a single function call, `GenerateLoRaWANPacket` in [`lib/lorawan_simple.h`](https://github.com/cnlohr/lolra/blob/master/lib/lorawan_simple.h) that handles all of the required encapsulation. Simply use this function to getnerate your frames, and broadcast them!
### The LoRa Gateway
We can transmit these messages. Cool. But now to receive them, we either will need a devices like a [LILYGO® T-Beam Meshtastic](https://www.lilygo.cc/products/t-beam-v1-1-esp32-lora-module) or a gateway like a [MikroTik LR9](https://mikrotik.com/product/wap_lr9_kit). The latter is really interesting here because there are thousands of these set up all over the world, and connected to [The Things Network](https://www.thethingsnetwork.org/). That means if we transmit a properly formatted LoRaWAN packet within earshot of one of those gateways, we can get the frame elsewhere on the planet!
Setup is pretty starightforward. You need to:
1. Create an account (they are free for personal/academic use)
2. Go to your console.
3. Create a new application.
4. Add a new device.
5. Select "Enter end device specifics manually"
6. Select the appropriate frequency plan. NOTE: This repository is designed around US FSB 2 (used by TTN)
7. Use LoRaWAN 1.0.0
8. "Show advanced activation, LoRaWAN class and cluster settings"
9. Select "Activation by personalization (ABP)"
10. Generate "Device Address", "AppSKey", and "NwkSKey"
11. Select "Register end device"
12. Place these credentials into the file under the LoRaWAN definition.
13. Unless you can guarantee increasing frame IDs, you will need to go to your device and:
14. Select "General Settings"
15. Select "Network Layer"
16. Select "Advanced MAC settings"
17. Select "Resets frame counters"
> [!NOTE]
> We only focus on LoRa frames that reset frame counters, however, if you can store the last transmitted packet id to non-volatile flash within your part, you can avoid this step. Without this, your device will be subject to replay attacks.
> [!NOTE]
> If using the MikroTik LR9, be sure to physically open it and plug in the internal antenna. From the factory, it is shipped connected to the outside port.
## Limitations
* SF <= 6, SF >= 11 are unavailable. I spent 10+ hours trying to figure them out and gave up.
* There seems to be something not quite perfect for some sizes. As in I was getting CRC errors in some places that my code "should" work but doesn't.
* I never got LDRO to work correctly. I believe it might have to do with the LoRa Code Word (prefixing the 2 downchirps)
* The SPI on the ch32v203 still isn't perfect. I was unable to get the I2S engine working, perhaps it doesn't work on the ch32v203?
* The licenses in this project are a hodgepodge. I encourage people to just use this project as a starting point for other work.
PR's are open if you can figure any of these three out! I just spent all the time I plan to spend on this project before I got here.
## Future Work
For LoRa specifically, waves are very well behaved and should be completely creatable with timer circuitry on the fly and should not need any precomputation, but I haven't gotten around to it yet. This would forego the need to have a large table for the chirps flashed into a device.
I want to also try performing this project using bespoke bit patterns that are more easily created on-the-fly. Also, depending on the exact codes used (possibly by employing technologies related to gold codes) one could even make reception of many signals with the exact same coding possible concurrently. Doing this would make it quite appropriate to implement "printf" on microcontrollers that can be wirelessly received by a central SDR with very low processor space (and performance) overhead.
Additionally, it would be fun to add a filter, or maybe try to build a filter into a PCB with no components.
Additionally, additionally, it would be very cool to try to build a Class C amplifier for the 900MHz signal. This would be very cool because it could be efficient, incredibly cheap and simple and also provide as much as 10-20dB of gain!
## Resources
### LoRa Software
* [The C++ Library I based my LoRa Frames On](https://github.com/myriadrf/LoRa-SDR)
* [A gnuradio LoRa library](https://github.com/tapparelj/gr-lora_sdr)
* [Another gnuradio LoRa library](https://github.com/rpp0/gr-lora)
* [GQRX](https://www.gqrx.dk/)
* [gnuradio](https://www.gnuradio.org/)
* [LoRa Baud Rate Calculator](https://unsigned.io/understanding-lora-parameters/) << be sure to select 125 or 500k Bandwidths, the default bandwidth is intenable.
### Papers and other resources
* [A Great LoRa Introduction](https://medium.com/@prajzler/what-is-lora-the-fundamentals-79a5bb3e6dec)
* [A more grounded paper on LoRa](https://chrisye-liu.github.io/files/yang22emu.pdf)
* [Reversing LoRa by Matt Knight](https://github.com/matt-knight/research/blob/master/2016_05_20_jailbreak/Reversing-Lora-Knight.pdf)
* [An academic paper on LoRa](https://dl.acm.org/doi/10.1145/3546869)
### Software Resources Directly Used
* [ch32v003fun](https://github.com/cnlohr/ch32v003fun)
* [esputil](https://github.com/cpq/esputil) dependency-free ESP programming
* [nosdk8266](github.com/cnlohr/nosdk8266)
Hardware
* [MikroTik LR9](https://mikrotik.com/product/wap_lr9_kit)
* [Airspy Mini SDR](https://v3.airspy.us/product/a-airspy-mini/)
* [LILYGO® T-Beam Meshtastic](https://www.lilygo.cc/products/t-beam-v1-1-esp32-lora-module)
## Special Thanks
* @MustardTiger for a crazy amount of support work in this project
* Willmore for the editing work
* My girlfriend for testing help and auxiliary camera work
* Everyone who helped out with my various open source projects.
## Range Tests
Urban testing was performed on 2024-02-23, Suburban on 2022-02-26 and Rural testing was performed on 2022-02-27.
![Range Test](https://github.com/cnlohr/lolra/blob/master/resources/Rangetest.png?raw=true)
Ranges are peak ranges. Reliable operation ends much earlier.
For TTGO Lora32, there was a +3dBi antenna added. For the MikroTik LR9, it used the internal antenna.
| Date | Sender | Receiver | SF/CR | BW | Note | Range | Avg End RSSI/SNR | Basis |
|------------|---------------------|--------------|-----------|-----|----------------------------------------------------|---------------|------------------|--------|
| 2024-02-23 | CH32V203 | MikroTik LR9 | SF8/CR48 | 125 | Downtown Bellevue (Urban) | 435' 132m | -98 / -9 | Ground |
| 2024-02-23 | CH32V203 | MikroTik LR9 | SF10/CR48 | 500 | Downtown Bellevue (Urban) | 435' 132m | -90 / -18 | Ground |
| 2024-02-26 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Mirramont Park (Light Suburban + Woods) | >576' >176m | -134 / -12 | Ground |
| 2024-02-26 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Poo Poo Point Trailhead (Rural) | >1117' >340m | -123 / -6 | Ground |
| 2024-02-26 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Issaquah Suburb (+Light Trees) | 2200' 669m | -133 / -10 | Ground |
| 2024-02-27 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Meadowbrook (Rural) Red Longer Antenna | 2220' 677m | -135 / -13 | Drone |
| 2024-02-27 | CH32V203 | TTGO Lora32 | SF10/CR48 | 500 | Meadowbrook (Rural) Red Longer Antenna | 1752' 534m | -132 / -16 | Drone |
| 2024-02-27 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Meadowbrook (Rural) OVERVOLT 5V Red Longer Antenna | 3996' 1218m | -131 / -12 | Drone |
| 2024-02-27 | CH32V203 | TTGO Lora32 | SF8/CR48 | 125 | Meadowbrook (Rural) Grey VNA Matched Antenna | 2719' 829m | -131 / -11 | Drone |
| 2024-02-27 | ESP8266 @ 80MHz | TTGO Lora32 | SF8/CR48 | 125 | Meadowbrook (Rural) Grey VNA Matched Antenna | 2789' 850m | -138 / -13 | Drone |
| 2024-02-27 | ESP8266 @ 173MHz | TTGO Lora32 | SF7/CR48 | 125 | Meadowbrook (Rural) Grey VNA Matched Antenna | 2812' 857m | -131 / -8 | Drone |
| 2024-02-27 | ESP32-S2 + Bitenna | TTGO Lora32 | SF10/CR48 | 125 | Meadowbrook (Rural) (Note 1) | 3428' 1044m | -137 / -13 | Ground |
| 2024-02-27 | ESP32-S2 + Bitenna | TTGO Lora32 | SF10/CR48 | 125 | Meadowbrook (Rural) Light Percipitation | >4895' >1492m | -130 / -8 | Drone |
| 2024-02-27 | ESP32-S2 + Funtenna | TTGO Lora32 | SF10/CR48 | 125 | Meadow brook (Rural) Light Percipitation | 705' / 215m | -139 / -15 | Drone |
| 2024-02-27 | ESP32-S2 + Bitenna | TTGO Lora32 | SF10/CR48 | 125 | Snoqualmie Trail, Dog Park to Ribary Creek (Rural) Light Percipitation | 8460' / 2580m | -141 / -16 | Drone |
1. There were two ground ESP32-S2 + Bitenna Tests. The one mentioned in the video is not recorded here, since I didn't get accurate results.
2. The bitenna ESP32-S2 test are done as a dipole antenna, connected to two different GPIOs, each set as the inversion of the other.
3. On the CH32V203, there is a 60 uA difference at 3.3V with anteanna on/off (some is going to be capacitive / inductive loading / reflected back because of SWR). The EIRP is **less** than this, based on the antenna stub SWR, but this is the total power consumption, so it is probably less than 120uW of EIRP.
+23
View File
@@ -0,0 +1,23 @@
all : flash
TARGET:=loratest
TARGET_MCU:=CH32V003
CH32V003FUN:=../ch32v003fun/ch32v003fun
EXTRA_ELF_DEPENDENCIES:=chirpbuff.h
EXTRA_CFLAGS:=-Wno-unused-function -I../../lib
include ../ch32v003fun/ch32v003fun/ch32v003fun.mk
chirpbuff.h : rf_data_gen
./rf_data_gen
rf_data_gen : rf_data_gen.c
gcc -o $@ $^ -lm
flash : cv_flash
clean : cv_clean
rm -rf rf_data_gen chirpbuff.dat chirpbuff.h chirpbuffinfo.h
+7
View File
@@ -0,0 +1,7 @@
# CH32V003 Firmware-Only 900MHz LoRa Transmitter
This only barely works, and at short ranges.
Be sure to provide a 24MHz clock (not crystal unless you disable the bypass) on PA1.
Generally you cannot use the built-in oscillator circuit becuase it's too spread-y.
+9
View File
@@ -0,0 +1,9 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H
#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_USE_UARTPRINTF 0
#endif
+440
View File
@@ -0,0 +1,440 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
// XXX WARNING: Something is wrong with this -
// The output isn't perfectly time aligned
// And as such there are extra images in weird places.
// TODO: Investigate the DMA+SPI Port jankyness.
#include "ch32v003fun.h"
#include <stdio.h>
#include <string.h>
#include "chirpbuff.h"
#include "chirpbuffinfo.h"
#include "LoRa-SDR-Code.h"
//#define LORAWAN
//#define TEST_TONE
#ifdef LORAWAN
#include "lorawan_simple.h"
#endif
#define SENDBUFF_WORDS (DMA_SIZE_WORDS*2)
uint16_t sendbuff[SENDBUFF_WORDS];
#if !defined( FOUND_PERFECT_DIVISOR )
#error Code only written for perfect division right now.
#endif
// Bits are shifted out MSBit first, then to LSBit
#define MAX_BYTES 25
#define MAX_SYMBOLS (MAX_BYTES*2+16)
// Our table is bespoke for the specific SF.
#define CHIPSSPREAD CHIRPLENGTH_WORDS// QUARTER_CHIRP_LENGTH_WORDS (TODO: Use the quater value elsewhere in the code)
#define MARK_FROM_SF0 (1<<SF_NUMBER) // SF7
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
uint32_t quadsetcount;
int16_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int runningcount_bits = 0;
volatile int fxcycle;
volatile int quadsetplace = -1;
volatile uint32_t temp;
static uint16_t lora_symbols[MAX_SYMBOLS];
int lora_symbols_count;
int16_t * AddChirp( int16_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
// This IRQ is called periodically to fill the output buffer that is shifted to SPI
void DMA1_Channel3_IRQHandler( void ) __attribute__((interrupt)) __attribute__((section(".srodata")));
void DMA1_Channel3_IRQHandler( void )
{
//GPIOD->BSHR = 1; // Turn on GPIOD0 for profiling
// Backup flags.
volatile int intfr = DMA1->INTFR;
do
{
// Clear all possible flags.
DMA1->INTFCR = DMA1_IT_GL3;
//int place = DMA1_Channel3->CNTR;
// CNTR says that there are THIS MANY bytes left in the transfer.
// So a high CNTR value indicates a very early place in the buffer.
uint16_t * sb = 0;
temp++;
if( intfr & DMA1_IT_HT3 )
{
sb = sendbuff;
}
else if( intfr & DMA1_IT_TC3 )
{
sb = sendbuff + SENDBUFF_WORDS/2;
}
if( sb )
{
if( quadsetplace < 0 )
{
// Abort Send
memset( sb, 0, SENDBUFF_WORDS*2/2 );
DMA1_Channel3->CFGR &= ~DMA_CFGR1_EN;
goto complete;
}
fxcycle += DMA_SIZE_WORDS;
if( fxcycle == NUM_DMAS_PER_QUARTER_CHIRP*DMA_SIZE_WORDS )
{
fxcycle = 0;
// Advance to next quarter word.
quadsetplace++;
if( quadsetplace > quadsetcount )
{
#ifdef TEST_TONE
quadsetplace = 0;
#else
quadsetplace = -1;
#endif
memset( sb, 0, SENDBUFF_WORDS*2/2 );
goto complete;
}
}
int symbol = quadsets[quadsetplace]; // Actually 0...CHIRPLENGTHWORDS
const uint16_t * tsb = 0;
// Select down- or up-chirp.
if( symbol < 0 )
{
int word = fxcycle - symbol - 1;
if( word >= CHIRPLENGTH_WORDS ) word -= CHIRPLENGTH_WORDS;
word++;
tsb = (&chirpbuff[word+REVERSE_START_OFFSET_BYTES/4]);
}
else
{
int word = fxcycle + symbol;
if( word >= CHIRPLENGTH_WORDS ) word -= CHIRPLENGTH_WORDS;
tsb = (&chirpbuff[word]);
}
// I tried using the DMA to do the copy - but that didn't work for some reason.
//while( DMA1_Channel2->CFGR & 1 );
// DMA1_Channel2->CNTR = DMA_SIZE_WORDS/2;
// DMA1_Channel2->MADDR = (uint32_t)tsb;
// DMA1_Channel2->PADDR = (uint32_t)sb;
// DMA1_Channel2->CFGR |= DMA_CFGR1_EN;
int cpy = DMA_SIZE_WORDS/2;
#ifdef TEST_TONE
// Test tone
memset( sb, 0xaa, cpy*2 );
#else
#if DMA_SIZE_WORDS_DIVISIBLE_BY_FOUR == 0
#error need divisibiltiy by 2.
#else
//while( cpy-- ) { *(sb++) = wordo; }
// Guarantee aligned access.
uint32_t * sbw = (uint32_t*)(((uint32_t)sb)&~3);
uint32_t * tsbw = (uint32_t*)(((uint32_t)tsb)&~3);
//while( cpy-- ) { *(sbw++) = *(tsbw++); }
// Align the data copy if needed
if( cpy & 1 )
{
*(sbw++) = *(tsbw++);
}
cpy /= 2; // Doubled up per loop
asm volatile("\
1:\
c.lw a3, 0(%[from])\n\
c.lw a4, 4(%[from])\n\
c.addi %[from], 8\n\
c.addi %[cpy], -1\n\
c.sw a3, 0(%[to])\n\
c.sw a4, 4(%[to])\n\
c.addi %[to], 8\n\
c.bnez %[cpy], 1b\n\
" : [cpy]"+r"(cpy), [from]"+r"(tsbw), [to]"+r"(sbw) : : "a3", "a4", "memory");
#endif
#endif
}
complete:
intfr = DMA1->INTFR;
} while( intfr );
//GPIOD->BSHR = 1<<16; // Turn off GPIOD0 for profiling
}
int main()
{
SystemInit();
funGpioInitAll();
// Set a wait state (1 = normal <= 48MHz)
FLASH->ACTLR = 1;
// MCO for testing.
// funPinMode( PA8, GPIO_CFGLR_OUT_50Mhz_AF_PP ); RCC->CFGR0 |= RCC_CFGR0_MCO_PLL;
printf( "Switching to HSE\n" );
Delay_Ms( 10 );
// Disable clock security system.
RCC->CTLR &= ~RCC_CSSON;
// Enable external crystal
RCC->CTLR |= RCC_HSEON;
// XXX NOTE: This is only used if you have a clock, not an oscillator.
// RCC->CTLR |= RCC_HSEBYP;
// Set System Clock Source to be 0.
RCC->CFGR0 = (RCC->CFGR0 & ~RCC_SW) | 0;
// Disable PLL
RCC->CTLR &= ~RCC_PLLON;
// Switch to HSE
RCC->CFGR0 |= RCC_PLLSRC;
// Enable PLL
RCC->CTLR |= RCC_PLLON;
// Wait for HSE to become ready.
while( !( RCC->CTLR & RCC_HSERDY) );
while( !( RCC->CTLR & RCC_PLLRDY) );
RCC->CFGR0 |= RCC_SW_1; // Switch system clock to PLL
printf( "HSE Switched\n" );
Delay_Ms( 10 );
RCC->CTLR &= ~RCC_HSION;
Delay_Ms( 10 );
//printf( "HSI Off [%08lx %08lx]\n", RCC->CTLR, RCC->CFGR0 ); HSI Off [03035180 0001000a]
funPinMode( PC6, GPIO_CFGLR_OUT_50Mhz_AF_PP ); // MOSI
RCC->APB2PCENR |= RCC_APB2Periph_SPI1;
RCC->AHBPCENR |= RCC_AHBPeriph_DMA1;
// Configure SPI
SPI1->CTLR1 =
SPI_NSS_Soft | SPI_CPHA_1Edge | SPI_CPOL_Low | SPI_DataSize_16b |
SPI_Mode_Master | SPI_Direction_1Line_Tx |
0 |
(SPI_DIV-1)<<3; // Divisior = 0
// If using DMA may need this.
SPI1->CTLR2 = SPI_CTLR2_TXDMAEN;
SPI1->HSCR = 1; // High-speed enable.
SPI1->CTLR1 |= CTLR1_SPE_Set;
//SPI1->DATAR = 0x55aa; // Set SPI line Low.
#if 0
// I tried using DMA Channel 4 to do a mem-mem copy.
// Doesn't seem to work.
DMA1_Channel2->PADDR = (uint32_t)sendbuff; // SOURCE
DMA1_Channel2->MADDR = (uint32_t)sendbuff; // DEST
DMA1_Channel2->CNTR = DMA_SIZE_WORDS;
DMA1_Channel2->CFGR =
DMA_M2M_Enable |
DMA_Priority_Low |
DMA_MemoryDataSize_HalfWord |
DMA_PeripheralDataSize_HalfWord |
DMA_MemoryInc_Enable |
DMA_PeripheralInc_Enable |
DMA_Mode_Normal | // DMA_Mode_Circular or DMA_Mode_Normal
DMA_DIR_PeripheralDST;
#endif
//DMA1_Channel3 is for SPI1TX
DMA1_Channel3->PADDR = (uint32_t)&SPI1->DATAR;
DMA1_Channel3->MADDR = (uint32_t)sendbuff;
DMA1_Channel3->CNTR = 0;// sizeof( bufferset )/2; // Number of unique copies. (Don't start, yet!)
DMA1_Channel3->CFGR =
DMA_M2M_Disable |
DMA_Priority_VeryHigh |
DMA_MemoryDataSize_HalfWord |
DMA_PeripheralDataSize_HalfWord |
DMA_MemoryInc_Enable |
DMA_Mode_Circular | // OR DMA_Mode_Circular or DMA_Mode_Normal
DMA_DIR_PeripheralDST |
DMA_IT_TC | DMA_IT_HT; // Transmission Complete + Half Empty Interrupts.
NVIC_EnableIRQ( DMA1_Channel3_IRQn );
memset( sendbuff, 0x00, sizeof( sendbuff ) );
// Enter critical section.
DMA1_Channel3->MADDR = (uint32_t)sendbuff;
DMA1_Channel3->CNTR = SENDBUFF_WORDS; // Number of unique uint16_t entries.
DMA1_Channel3->CFGR |= DMA_CFGR1_EN;
uint32_t frame = 0;
while(1)
{
Delay_Ms( 1000 );
#ifdef LORAWAN
// Optionally generate a LoRaWAN Packet.
const static uint8_t raw_pl[4] = { 0xaa, 0xaa, 0xaa, 0xaa, };
// Just some random data.
static uint8_t raw_payload_with_b0[MAX_BYTES+8] = { 0 };
uint8_t * payload_in = raw_payload_with_b0 + 16;
int payload_in_size;
// Be sure to provide your appropriate keys and address here.
static const uint8_t payload_key[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // AppSKey Big Endian
static const uint8_t network_skey[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // NwkSKey Big Endian
static const uint8_t devaddress[4] = { 0x00, 0x00, 0x00, 0x00 }; // Device address Little Endian LSB (Written backwards from Device address default view)
payload_in_size = GenerateLoRaWANPacket( raw_payload_with_b0, raw_pl, sizeof(raw_pl), payload_key, network_skey, devaddress, frame );
#else
uint8_t payload_in[15] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
int payload_in_size = 15;
#endif
lora_symbols_count = 0;
int r = CreateMessageFromPayload( lora_symbols, &lora_symbols_count, MAX_SYMBOLS, SF_NUMBER, 4, payload_in, payload_in_size );
if( r < 0 )
{
printf( "Failed to generate message (%d)\n", r );
// Failed
continue;
}
frame++;
int j;
quadsetcount = 0;
int16_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#if SF_NUMBER == 6
#define CODEWORD_SHIFT 2
#else
#define CODEWORD_SHIFT 3
#endif
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0);
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
qso = AddChirp( qso, 0, 0 );
}
for( j = 0; j < lora_symbols_count; j++ )
{
int ofs = lora_symbols[j];
qso = AddChirp( qso, ofs, 0 );
}
runningcount_bits = 0;
// This tells the interrupt we have data.
quadsetcount = qso - quadsets + 0;
printf( "- %d -- %d [%d] %d; %d\n", (int)temp, (int)lora_symbols_count, (int)quadsetcount, CHIPSSPREAD/4, sendbuff[0] );
quadsetplace = 0;
DMA1_Channel3->CFGR |= DMA_CFGR1_EN;
while( quadsetplace >= 0 );
}
}
+58
View File
@@ -0,0 +1,58 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdint.h>
//#define KHZ500
#ifdef KHZ500
// Tested, works - be sure to set your radio to be able to receive this.
#define SF_NUMBER 10
const double center_frequency = 904.6;
const double bw = .5;
#define SF_SYMBOL_TIME 0.00000025
#else
// Tested, works.
const double center_frequency = 904.5;
#define SF_NUMBER 8
const double bw = .125;
#endif
const uint32_t memory_offset = 0x00000;
#define SPI_DIV 2
const double sample_rate = (48.0/2)/SPI_DIV;
#define ATTRIBUTE_FOR_DATA "__attribute__((section(\".text\"),aligned(32)))"
#define USE_16_BITS
#include "../../lib/rf_data_gen.h"
int main()
{
return gen_buffer_files();
}
+1
Submodule ch32v/ch32v003fun added at 87e4ee59d9
+23
View File
@@ -0,0 +1,23 @@
all : flash
TARGET:=loratest
TARGET_MCU:=CH32V203
CH32V003FUN:=../ch32v003fun/ch32v003fun
EXTRA_ELF_DEPENDENCIES:=chirpbuff.h
EXTRA_CFLAGS:=-Wno-unused-function -I../../lib
include ../ch32v003fun/ch32v003fun/ch32v003fun.mk
chirpbuff.h : rf_data_gen
./rf_data_gen
rf_data_gen : rf_data_gen.c
gcc -o $@ $^ -lm
flash : cv_flash
clean : cv_clean
rm -rf rf_data_gen chirpbuff.dat chirpbuff.h chirpbuffinfo.h
+5
View File
@@ -0,0 +1,5 @@
# CH32V203 example
Flash this on a CH32V203 with a 8MHz external crystal, then connect a short length of wire to PB15.
It will transmit SF8 Lora on 904.5MHz
+9
View File
@@ -0,0 +1,9 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H
#define FUNCONF_USE_DEBUGPRINTF 0
#define FUNCONF_USE_UARTPRINTF 1
#endif
+441
View File
@@ -0,0 +1,441 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include "ch32v003fun.h"
#include <stdio.h>
#include <string.h>
#include "chirpbuff.h"
#include "chirpbuffinfo.h"
#include "../../lib/LoRa-SDR-Code.h"
// NOTE: The SPI engine in the 203 isn't perfect, and I wasn't able to get the I2S engine working.
// Optionally send LoRaWAN messages. Be sure to copy your keys from the things network.
#define LORAWAN
#ifdef LORAWAN
#include "lorawan_simple.h"
static const uint8_t payload_key[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // AppSKey Big Endian
static const uint8_t network_skey[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // NwkSKey Big Endian
static const uint8_t devaddress[4] = { 0x00, 0x00, 0x00, 0x00 }; // Device address Little Endian LSB (Written backwards from Device address default view)
#endif
#define SENDBUFF_WORDS (DMA_SIZE_WORDS*2)
uint16_t sendbuff[SENDBUFF_WORDS];
#if !defined( FOUND_PERFECT_DIVISOR )
#error Code only written for perfect division right now.
#endif
// Bits are shifted out MSBit first, then to LSBit
#define MAX_BYTES 60
#define MAX_SYMBOLS (MAX_BYTES*2+16)
// Our table is bespoke for the specific SF.
#define CHIPSSPREAD CHIRPLENGTH_WORDS// QUARTER_CHIRP_LENGTH_WORDS (TODO: Use the quater value elsewhere in the code)
#define MARK_FROM_SF0 (1<<SF_NUMBER) // SF7
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
uint32_t quadsetcount;
int16_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int runningcount_bits = 0;
volatile int fxcycle;
volatile int quadsetplace = -1;
int16_t * AddChirp( int16_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
// This IRQ is called peridocally to fill the buffer that is going to the DMA.
void DMA1_Channel5_IRQHandler( void ) __attribute__((interrupt)) __attribute__((section(".srodata")));
void DMA1_Channel5_IRQHandler( void )
{
//GPIOD->BSHR = 1; // Turn on GPIOD0 for profiling
// Backup flags.
volatile int intfr = DMA1->INTFR;
do
{
// Clear all possible flags.
DMA1->INTFCR = DMA1_IT_GL5;
int place = DMA1_Channel5->CNTR;
// CNTR says that there are THIS MANY bytes left in the transfer.
// So a high CNTR value indicates a very early place in the buffer.
uint16_t * sb = 0;
//if( intfr & DMA1_IT_HT5 )
if( place < SENDBUFF_WORDS/2 )
{
sb = sendbuff + SENDBUFF_WORDS/2;
}
//else if( intfr & DMA1_IT_TC5 )
else
{
sb = sendbuff;
}
if( sb )
{
if( quadsetplace < 0 )
{
// Abort Send
memset( sb, 0, SENDBUFF_WORDS*2/2 ); //in bytes, but only half the buffer.
DMA1_Channel5->CFGR &= ~DMA_Mode_Circular; // disable transfer.
goto complete;
}
if( 0 ) // Generate test tone
{
int remain = SENDBUFF_WORDS/2;
do
{
uint16_t v = chirpbuff[quadsetplace++];
if( quadsetplace >= QUARTER_CHIRP_LENGTH_WORDS*4 ) quadsetplace = 0;
*(sb++) = v;
} while( --remain );
}
else
{
fxcycle += DMA_SIZE_WORDS;
if( fxcycle == NUM_DMAS_PER_QUARTER_CHIRP*DMA_SIZE_WORDS )
{
fxcycle = 0;
// Advance to next quarter word.
quadsetplace++;
if( quadsetplace > quadsetcount )
{
quadsetplace = -1;
memset( sb, 0, SENDBUFF_WORDS*2/2 );
goto complete;
}
}
int symbol = quadsets[quadsetplace]; // Actually 0...CHIRPLENGTHWORDS
const uint16_t * tsb = 0;
// Select down- or up-chirp.
if( symbol < 0 )
{
int word = fxcycle - symbol - 1;
if( word >= CHIRPLENGTH_WORDS ) word -= CHIRPLENGTH_WORDS;
word++;
tsb = (&chirpbuff[word+REVERSE_START_OFFSET_BYTES/2]);
}
else
{
int word = fxcycle + symbol;
if( word >= CHIRPLENGTH_WORDS ) word -= CHIRPLENGTH_WORDS;
tsb = (&chirpbuff[word]);
}
int cpy = DMA_SIZE_WORDS;
// Very fast half-word-copy.
// Make sure the data is aligned to groups-of-two bytes.
if( cpy & 1 )
{
*(sb++) = *(tsb++);
cpy--;
}
cpy /= 2; // Doubled up per loop
asm volatile("\
1:\
lh a3, 0(%[from])\n\
lh a4, 2(%[from])\n\
c.addi %[from], 4\n\
c.addi %[cpy], -1\n\
sh a3, 0(%[to])\n\
sh a4, 2(%[to])\n\
c.addi %[to], 4\n\
c.bnez %[cpy], 1b\n\
" : [cpy]"+r"(cpy), [from]"+r"(tsb), [to]"+r"(sb) : : "a3", "a4", "memory");
// while( cpy-- ) { *(sb++) = *(tsb++); } // Doesn't help. GCC Still tries to use memcpy.
//memcpy( sb, tsb, DMA_SIZE_WORDS * 2 );
}
}
complete:
intfr = DMA1->INTFR;
} while( intfr );
//GPIOD->BSHR = 1<<16; // Turn off GPIOD0 for profiling
}
int main()
{
SystemInit();
funGpioInitAll();
// Force HCLK to be nodiv. ch32vfun sets it to be a reasonbly high div.
RCC->CFGR0 &= ~RCC_PPRE1_DIV16;
// MCO for testing.
// funPinMode( PA8, GPIO_CFGLR_OUT_50Mhz_AF_PP ); RCC->CFGR0 |= RCC_CFGR0_MCO_PLL;
printf( "Switching to HSE\n" );
Delay_Ms( 100 );
// Disable clock security system.
RCC->CTLR &= ~RCC_CSSON;
#ifdef USE_EXTERNAL_CLOCK
// No crystal - use clock.
RCC->CTLR |= RCC_HSEBYP;
#endif
// Enable external crystal
RCC->CTLR |= RCC_HSEON;
// Set System Clock Source to be 0.
RCC->CFGR0 = (RCC->CFGR0 & ~RCC_SW) | 0;
// Disable PLL
RCC->CTLR &= ~RCC_PLLON;
#ifdef USE_EXTERNAL_CLOCK
// Set PLL to 9x not 18x
RCC->CFGR0 = ( RCC->CFGR0 & ~RCC_PLLMULL18 ) | RCC_PLLMULL9;
#endif
// Switch to HSE
RCC->CFGR0 |= RCC_PLLSRC;
// Enable PLL
RCC->CTLR |= RCC_PLLON;
// Wait for HSE to become ready.
while( !( RCC->CTLR & RCC_HSERDY) );
while( !( RCC->CTLR & RCC_PLLRDY) );
RCC->CFGR0 |= RCC_SW_1; // Switch system clock to PLL
printf( "HSE Switched\n" );
Delay_Ms( 10 );
RCC->CTLR &= ~RCC_HSION;
Delay_Ms( 10 );
printf( "HSI Off [%08lx %08lx]\n", RCC->CTLR, RCC->CFGR0 );
// funPinMode( PB12, GPIO_CFGLR_OUT_50Mhz_AF_PP ); // NSS
// funPinMode( PB13, GPIO_CFGLR_OUT_50Mhz_AF_PP ); // SCK
// funPinMode( PB14, GPIO_CFGLR_OUT_50Mhz_AF_PP ); // MISO
funPinMode( PB15, GPIO_CFGLR_OUT_50Mhz_AF_PP ); // MOSI
RCC->APB1PRSTR = RCC_SPI2RST;
RCC->APB1PRSTR = 0;
RCC->APB1PCENR |= RCC_APB1Periph_SPI2;
RCC->AHBPCENR |= RCC_AHBPeriph_DMA1;
// Configure SPI
SPI2->CTLR1 =
SPI_NSS_Soft | SPI_CPHA_1Edge | SPI_CPOL_Low | SPI_DataSize_16b |
SPI_Mode_Master | SPI_Direction_1Line_Tx |
0 |
0<<3; // Divisior = 0
// If using DMA may need this.
SPI2->CTLR2 = SPI_CTLR2_TXDMAEN;
SPI2->HSCR = 1; // High-speed enable.
SPI2->CTLR1 |= CTLR1_SPE_Set;
//SPI2->DATAR = 0x55aa; // Set SPI line Low.
//DMA1_Channel5 is for SPI2TX
DMA1_Channel5->PADDR = (uint32_t)&SPI2->DATAR;
DMA1_Channel5->MADDR = (uint32_t)sendbuff;
DMA1_Channel5->CNTR = 0;// sizeof( bufferset )/2; // Number of unique copies. (Don't start, yet!)
DMA1_Channel5->CFGR =
DMA_M2M_Disable |
DMA_Priority_VeryHigh |
DMA_MemoryDataSize_HalfWord |
DMA_PeripheralDataSize_HalfWord |
DMA_MemoryInc_Enable |
DMA_Mode_Normal | // OR DMA_Mode_Circular or DMA_Mode_Normal
DMA_DIR_PeripheralDST |
DMA_IT_TC | DMA_IT_HT; // Transmission Complete + Half Empty Interrupts.
NVIC_EnableIRQ( DMA1_Channel5_IRQn );
DMA1_Channel5->CFGR |= DMA_CFGR1_EN;
memset( sendbuff, 0x00, sizeof( sendbuff ) );
// Enter critical section.
DMA1_Channel5->CNTR = 0;
DMA1_Channel5->MADDR = (uint32_t)sendbuff;
DMA1_Channel5->CNTR = SENDBUFF_WORDS; // Number of unique uint16_t entries.
DMA1_Channel5->CFGR |= DMA_Mode_Circular;
uint16_t lora_symbols[MAX_SYMBOLS];
int lora_symbols_count;
while(1)
{
Delay_Ms( 1000 );
#ifdef LORAWAN
Delay_Ms( 1000 );
static uint32_t frame = 0;
// Send a message with LoraWan. Formatted specifically for thethings.network.
uint8_t inner_payload_raw[24];
int inner_payload_len = snprintf( (char*)inner_payload_raw, 24, "meow%lu ", frame%10 );
inner_payload_len = 5;
// Just some random data.
uint8_t raw_payload_with_b0[259+8] = { };
uint8_t * payload_in = raw_payload_with_b0 + 16;
uint8_t * pl = payload_in;
int payload_in_size = 0;
pl += GenerateLoRaWANPacket( raw_payload_with_b0, inner_payload_raw, inner_payload_len, payload_key, network_skey, devaddress, frame++);
payload_in_size = pl - payload_in;
lora_symbols_count = 0;
#else
// Just some random data.
uint8_t payload_in[MAX_BYTES] = { 0xbb, 0xcc, 0xde, 0x55, 0x22,};
int payload_in_size = 6;
static int msgno = 0;
payload_in[4] = msgno++;
#endif
lora_symbols_count = 0;
int r = CreateMessageFromPayload( lora_symbols, &lora_symbols_count, MAX_SYMBOLS, SF_NUMBER, 4, payload_in, payload_in_size );
if( r < 0 )
{
printf( "Failed to generate message (%d)\n", r );
// Failed
continue;
}
int j;
quadsetcount = 0;
int16_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#define CODEWORD_SHIFT 3
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0);
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
qso = AddChirp( qso, 0, 0 );
}
for( j = 0; j < lora_symbols_count; j++ )
{
int ofs = lora_symbols[j];
//ofs = ofs ^ ((MARK_FROM_SF6<<6) -1);
//ofs &= (MARK_FROM_SF6<<6) -1;
qso = AddChirp( qso, ofs, 0 );
printf( "%03x ", ofs );
}
printf( "\n" );
runningcount_bits = 0;
// This tells the interrupt we have data.
quadsetcount = qso - quadsets + 0;
printf( "--- %d [%d] %d\n", (int)lora_symbols_count, (int)quadsetcount, CHIPSSPREAD/4 );
quadsetplace = 0;
DMA1_Channel5->CFGR |= DMA_Mode_Circular;
}
}
+91
View File
@@ -0,0 +1,91 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// TEST 1: 125kHz with -.028 675
// TEST 2: 500kHz with 138 kHz correction. 534??
// TEST 3: 500kHz with 0 kHz correction. FAIL
// TEST 4: 125kHz with -.028 5V 1210
//END OF RED ANTENNA
// TEST 5: 125kHz with -.027 3.3V, shorter antenna? 829
// TEST 6: 125k ESP 80 GREY antenna ???/??? 850 /
#include <stdint.h>
// Transmit a 500kHz width LoRa
//#define KHZ500
// Expects a 24MHz external clock.
//#define USE_EXTERNAL_CLOCK
#ifdef KHZ500
const double tune_mhz = -0.000;
const double center_frequency = 904.6 + tune_mhz;
const double bw = .5;
#ifdef USE_EXTERNAL_CLOCK
#define SF_NUMBER 9
#else
#define SF_NUMBER 10
#endif
#define SF_SYMBOL_TIME 0.00000025
#else // 125kHz channel
const double bw = 0.125;
#ifdef USE_EXTERNAL_CLOCK
const double tune_mhz = 0.00;
#define SF_NUMBER 7
#else
const double tune_mhz = -0.028; //XXX ??? Per device tuning? Or area the load caps just wrong?
#define SF_NUMBER 8
#endif
const double center_frequency = 904.5 + tune_mhz;
#endif
#define SPI_DIV 1
#ifdef USE_EXTERNAL_CLOCK
const double sample_rate = (24*9/2)/SPI_DIV; //216MHz PLL
#else
const double sample_rate = (72)/SPI_DIV;
#endif
#define ATTRIBUTE_FOR_DATA "__attribute__((section(\".text\")))"
#define USE_16_BITS
const uint32_t memory_offset = 0x00000;
#include "../../lib/rf_data_gen.h"
int main()
{
return gen_buffer_files();
}
+2
View File
@@ -0,0 +1,2 @@
.vscode
build
+23
View File
@@ -0,0 +1,23 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
add_custom_target(stub_bootload
# If the swadge is running reboot it into the bootloader.
COMMAND make -C ../tools/reboot_into_bootloader reboot
COMMAND sleep 0.5
)
#add_dependencies(flash before_flash)
add_custom_target(stub_run
#Sometimes the swadge needs a kick to get rebooted
COMMAND sleep 0.5
COMMAND esptool.py --before no_reset --after no_reset load_ram ../tools/bootload_reboot_stub/bootload_reboot_stub.bin
)
project(usb_sandbox)
+4
View File
@@ -0,0 +1,4 @@
idf_component_register(SRCS "main.c" "app.c"
INCLUDE_DIRS "" )
+454
View File
@@ -0,0 +1,454 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "hal/gpio_types.h"
#include "esp_log.h"
#include "soc/efuse_reg.h"
#include "soc/soc.h"
#include "soc/system_reg.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/gpio_sig_map.h"
#include "soc/io_mux_reg.h"
#include "soc/dedic_gpio_reg.h"
// Funtenna will output on GPIO1 and 14. Otherwise it will be 17 and 18
//#define FUNTENNA
int global_i = 100;
static inline uint32_t getCycleCount()
{
uint32_t ccount;
asm volatile("rsr %0,ccount":"=a" (ccount));
return ccount;
}
#define IO_MUX_REG(x) XIO_MUX_REG(x)
#define XIO_MUX_REG(x) IO_MUX_GPIO##x##_REG
#define GPIO_NUM(x) XGPIO_NUM(x)
#define XGPIO_NUM(x) GPIO_NUM_##x
#ifdef FUNTENNA
#define RF1_PIN 1
#define RF2_PIN 14
#else
#define RF1_PIN 17
#define RF2_PIN 18
#endif
#include "hal/gpio_types.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
#include "soc/i2s_reg.h"
#include "soc/periph_defs.h"
#include "rom/lldesc.h"
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc.h"
#include "soc/regi2c_apll.h"
//#include "components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h
#include "hal/regi2c_ctrl_ll.h"
#include "esp_private/periph_ctrl.h"
#include "esp_private/regi2c_ctrl.h"
#include "hal/clk_tree_ll.h"
#include "siggen.h"
#define I2C_RTC_WIFI_CLK_EN (SYSCON_WIFI_CLK_EN_REG)
#define I2C_RTC_CLK_GATE_EN (BIT(18))
#define I2C_RTC_CLK_GATE_EN_M (BIT(18))
#define I2C_RTC_CLK_GATE_EN_V 0x1
#define I2C_RTC_CLK_GATE_EN_S 18
#define I2C_RTC_CONFIG0 0x6000e048
#define I2C_RTC_MAGIC_CTRL 0x00001FFF
#define I2C_RTC_MAGIC_CTRL_M ((I2C_RTC_MAGIC_CTRL_V)<<(I2C_RTC_MAGIC_CTRL_S))
#define I2C_RTC_MAGIC_CTRL_V 0x1FFF
#define I2C_RTC_MAGIC_CTRL_S 4
#define I2C_RTC_CONFIG1 0x6000e044
#define I2C_RTC_BOD_MASK (BIT(22))
#define I2C_RTC_BOD_MASK_M (BIT(22))
#define I2C_RTC_BOD_MASK_V 0x1
#define I2C_RTC_BOD_MASK_S 22
#define I2C_RTC_SAR_MASK (BIT(18))
#define I2C_RTC_SAR_MASK_M (BIT(18))
#define I2C_RTC_SAR_MASK_V 0x1
#define I2C_RTC_SAR_MASK_S 18
#define I2C_RTC_BBPLL_MASK (BIT(17))
#define I2C_RTC_BBPLL_MASK_M (BIT(17))
#define I2C_RTC_BBPLL_MASK_V 0x1
#define I2C_RTC_BBPLL_MASK_S 17
#define I2C_RTC_APLL_MASK (BIT(14))
#define I2C_RTC_APLL_MASK_M (BIT(14))
#define I2C_RTC_APLL_MASK_V 0x1
#define I2C_RTC_APLL_MASK_S 14
#define I2C_RTC_ALL_MASK 0x00007FFF
#define I2C_RTC_ALL_MASK_M ((I2C_RTC_ALL_MASK_V)<<(I2C_RTC_ALL_MASK_S))
#define I2C_RTC_ALL_MASK_V 0x7FFF
#define I2C_RTC_ALL_MASK_S 8
#define I2C_RTC_CONFIG2 0x6000e000
#define I2C_RTC_BUSY (BIT(25))
#define I2C_RTC_BUSY_M (BIT(25))
#define I2C_RTC_BUSY_V 0x1
#define I2C_RTC_BUSY_S 25
#define I2C_RTC_WR_CNTL (BIT(24))
#define I2C_RTC_WR_CNTL_M (BIT(24))
#define I2C_RTC_WR_CNTL_V 0x1
#define I2C_RTC_WR_CNTL_S 24
#define I2C_RTC_DATA 0x000000FF
#define I2C_RTC_DATA_M ((I2C_RTC_DATA_V)<<(I2C_RTC_DATA_S))
#define I2C_RTC_DATA_V 0xFF
#define I2C_RTC_DATA_S 16
#define I2C_RTC_ADDR 0x000000FF
#define I2C_RTC_ADDR_M ((I2C_RTC_ADDR_V)<<(I2C_RTC_ADDR_S))
#define I2C_RTC_ADDR_V 0xFF
#define I2C_RTC_ADDR_S 8
#define I2C_RTC_SLAVE_ID 0x000000FF
#define I2C_RTC_SLAVE_ID_M ((I2C_RTC_SLAVE_ID_V)<<(I2C_RTC_SLAVE_ID_S))
#define I2C_RTC_SLAVE_ID_V 0xFF
#define I2C_RTC_SLAVE_ID_S 0
#define I2C_RTC_MAGIC_DEFAULT (0x1c40)
#define I2C_BOD 0x61
#define I2C_BBPLL 0x66
#define I2C_SAR_ADC 0X69
#define I2C_APLL 0X6D
int lastend = 0;
// Configures APLL = 480 / 4 = 120
// 40 * (SDM2 + SDM1/(2^8) + SDM0/(2^16) + 4) / ( 2 * (ODIV+2) );
// Datasheet recommends that numerator does not exceed 500MHz.
void IRAM_ATTR local_rtc_clk_apll_enable(bool enable, uint32_t sdm0, uint32_t sdm1, uint32_t sdm2, uint32_t o_div)
{
REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PD, enable ? 0 : 1);
REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PU, enable ? 1 : 0);
if (enable) {
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM2, sdm2);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM0, sdm0);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM1, sdm1);
REGI2C_WRITE(I2C_APLL, I2C_APLL_SDM_STOP, CLK_LL_APLL_SDM_STOP_VAL_1);
REGI2C_WRITE(I2C_APLL, I2C_APLL_SDM_STOP, CLK_LL_APLL_SDM_STOP_VAL_2_REV1);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OR_OUTPUT_DIV, o_div );
}
// Settings determined experimentally.
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_TSCHGP, 0 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_EN_FAST_CAL, 1 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DHREF_SEL, 0 ); // 0..3
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DLREF_SEL, 0 ); // 0..3
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_SDM_DITHER, 1 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DVDD, 25 ); // 0 .. 31
}
void IRAM_ATTR regi2c_write_reg_raw_local(uint8_t block, uint8_t host_id, uint8_t reg_add, uint8_t data)
{
uint32_t temp = ((block & I2C_RTC_SLAVE_ID_V) << I2C_RTC_SLAVE_ID_S)
| ((reg_add & I2C_RTC_ADDR_V) << I2C_RTC_ADDR_S)
| ((0x1 & I2C_RTC_WR_CNTL_V) << I2C_RTC_WR_CNTL_S)
| (((uint32_t)data & I2C_RTC_DATA_V) << I2C_RTC_DATA_S);
while (REG_GET_BIT(I2C_RTC_CONFIG2, I2C_RTC_BUSY));
REG_WRITE(I2C_RTC_CONFIG2, temp);
}
void apll_quick_update( uint32_t sdm )
{
uint8_t sdm2 = sdm>>16;
uint8_t sdm1 = (sdm>>8)&0xff;
uint8_t sdm0 = (sdm>>0)&0xff;
static int last_sdm_0 = -1;
static int last_sdm_1 = -1;
static int last_sdm_2 = -1;
if( sdm2 != last_sdm_2 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM2, sdm2);
if( sdm0 != last_sdm_0 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM0, sdm0);
if( sdm1 != last_sdm_1 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM1, sdm1);
last_sdm_2 = sdm2;
last_sdm_1 = sdm1;
last_sdm_0 = sdm0;
}
void sandbox_main()
{
printf( "sandbox_main()\n" );
DPORT_SET_PERI_REG_MASK( DPORT_CPU_PERI_CLK_EN_REG, DPORT_CLK_EN_DEDICATED_GPIO );
DPORT_CLEAR_PERI_REG_MASK( DPORT_CPU_PERI_RST_EN_REG, DPORT_RST_EN_DEDICATED_GPIO);
// Setup GPIO39 to be the PRO ALONE output (For LEDs)
REG_WRITE( GPIO_OUT1_W1TC_REG, 1<<(39-32) );
REG_WRITE( GPIO_ENABLE1_W1TS_REG, 1<<(39-32) );
REG_WRITE( IO_MUX_GPIO39_REG, 2<<FUN_DRV_S );
REG_WRITE( GPIO_FUNC39_OUT_SEL_CFG_REG, PRO_ALONEGPIO_OUT0_IDX );
REG_WRITE( DEDIC_GPIO_OUT_CPU_REG, 0x01 ); // Enable CPU instruction output
// Output clock on P2.
// Maximize the drive strength.
gpio_set_drive_capability( GPIO_NUM(RF1_PIN), GPIO_DRIVE_CAP_3 );
gpio_set_drive_capability( GPIO_NUM(RF2_PIN), GPIO_DRIVE_CAP_3 );
// Use the IO matrix to create the inverse of TX on pin 17.
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 0 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 0 );
periph_module_enable(PERIPH_I2S0_MODULE);
int use_apll = 1;
// Start with a default tone.
int sdm0 = 100;
int sdm1 = 230;
int sdm2 = 8;
int odiv = 0;
local_rtc_clk_apll_enable( use_apll, sdm0, sdm1, sdm2, odiv );
if( use_apll )
{
WRITE_PERI_REG( I2S_CLKM_CONF_REG(0), (1<<I2S_CLK_SEL_S) | (1<<I2S_CLK_EN_S) | (0<<I2S_CLKM_DIV_A_S) | (0<<I2S_CLKM_DIV_B_S) | (1<<I2S_CLKM_DIV_NUM_S) );
}
else
{
// fI2S = fCLK / ( N + B/A )
// DIV_NUM = N
// Note I2S_CLKM_DIV_NUM minimum = 2 by datasheet. Less than that and it will ignoreeee you.
WRITE_PERI_REG( I2S_CLKM_CONF_REG(0), (2<<I2S_CLK_SEL_S) | (1<<I2S_CLK_EN_S) | (0<<I2S_CLKM_DIV_A_S) | (0<<I2S_CLKM_DIV_B_S) | (1<<I2S_CLKM_DIV_NUM_S) ); // Minimum reduction, 2:1
}
SigSetupTest( );
lastend = getCycleCount();
}
#define DisableISR() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0)
#define EnableISR() do { portbenchmarkINTERRUPT_RESTORE(0); XTOS_SET_INTLEVEL(0); } while (0)
uint32_t frame = 0;
void sandbox_tick()
{
//uprintf( "%08x\n", REGI2C_READ(I2C_APLL, I2C_APLL_DSDM2 ));
// 40 * (SDM2 + SDM1/(2^8) + SDM0/(2^16) + 4) / ( 2 * (ODIV+2) );\n
// 13rd harmonic.
const float fRadiator = 904.50 + 0.00; // 0.02 is specific to this device.
const float fBandwidth = .125;
const float fHarmonic = 13.0;
const float fOffset = -(fBandwidth/2);
const float fXTAL = 40;
const float fTarg = (fRadiator+fOffset)/fHarmonic;
const float fAPLL = fTarg * 2;
const uint32_t sdmBaseTarget = ( fAPLL * 4 / fXTAL - 4 ) * 65536 + 0.5; // ~649134
const float fRange = (fBandwidth)/fHarmonic;
const float fAPLLRange = fRange * 2;
const float sdmRange = ( fAPLLRange * 4 / fXTAL ) * 65536; // ~126
// 491520 clocks per chip @ SF8 (2.048ms per chirp)
const uint32_t sdmDivisor = ( CHIPSSPREAD / sdmRange ) + 0.5;
#if 0
// For DEBUGGING ONLY
int k;
//DisableISR();
for( k = 0; k < 33; k++ )
{
int fplv = 0;
// Send every second
// If you want to dialate time, do it here.
frame = (getCycleCount()/200) % 24000000;
fplv = SigGen( frame, codeTarg );
uint32_t codeTargUse = codeTarg + fplv;
uint32_t sdm = (codeTargUse * 2 / 40 - 4 * 65536); //XXX WARNING CHARLES, why does this move in pairs?
apll_quick_update( sdm );
if( fplv < 0 ) break;
if( fplv > 0 )
uprintf( "%d %d\n", (int)sdm, (int)fplv );
}
//EnableISR();
#else
uint32_t start = getCycleCount();
frame = (start) % 24000000;
int do_send = false;
if( (uint32_t)(start - lastend) > 240000000 )
{
do_send = true;
}
{
int spin;
gpio_matrix_out( 39, PRO_ALONEGPIO_OUT0_IDX, 0, 0 );
gpio_set_drive_capability( 39, GPIO_DRIVE_CAP_3 );
// Create an RGB Rainbow value
uint32_t ws_out = 0xff00ff;//do_send? 0 : 0xff;
#define nop __asm__ __volatile__("nop\n")
int i;
// Shift out all 24 bits, note the 1 and 3 for timing is baesed off an XTAL clock.
for( i = 0; i < 24; i++ )
{
__asm__ __volatile__ ("set_bit_gpio_out 0x1");
int high_for = ( ws_out & 0x800000 )?3:1;
for( spin = 0; spin < high_for; spin++ ) nop;
__asm__ __volatile__ ("clr_bit_gpio_out 0x1");
int low_for = ( ws_out & 0x800000 )?1:3;
for( spin = 0; spin < low_for; spin++ ) nop;
ws_out<<=1;
}
esp_rom_delay_us( 5000 );
}
if( do_send )
{
SigSetupTest();
int iterct = 0;
int dither = 0;
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 0 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 0 );
//REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PD, 0);
//REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PU, 1);
//DisableISR();
while(1)
{
int fplv = 0;
// Send every second
// If you want to dialate time, do it here.
frame = (getCycleCount()) - start;
fplv = SigGen( frame, sdmBaseTarget );
//XXX TODO: Experiment more with dither. It doesn't appear to have an immediate effect?
//dither = ( dither + sdmDivisor/4) % sdmDivisor;
uint32_t sdm = sdmBaseTarget + ( fplv + dither ) / sdmDivisor;
if( fplv < 0 ) break;
apll_quick_update( sdm );
iterct++;
}
//EnableISR();
//REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PU, 0);
//REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PD, 1);
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 1 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 1 );
printf( "Iter: %d\n", iterct );
lastend = getCycleCount();
}
#endif
// vTaskDelay( 1 );
}
struct SandboxStruct
{
void (*fnIdle)();
};
struct SandboxStruct sandbox_mode =
{
.fnIdle = sandbox_tick,
};
+94
View File
@@ -0,0 +1,94 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "spi_flash_mmap.h"
#include "esp_efuse.h"
#include "esp_efuse_table.h" // or "esp_efuse_custom_table.h"
#include "esp_task_wdt.h"
#include "esp_log.h"
#include "soc/dedic_gpio_reg.h"
#include "driver/gpio.h"
#include "soc/soc.h"
#include "soc/system_reg.h"
#include "soc/usb_reg.h"
#include "ulp_riscv.h"
#include "driver/rtc_io.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
#include "soc/rtc.h"
#define SOC_DPORT_USB_BASE 0x60080000
struct SandboxStruct
{
void (*fnIdle)();
};
struct SandboxStruct * g_SandboxStruct;
void esp_sleep_enable_timer_wakeup();
volatile void * keep_symbols[] = { 0, vTaskDelay, ulp_riscv_halt,
ulp_riscv_timer_resume, ulp_riscv_timer_stop, ulp_riscv_load_binary,
ulp_riscv_run, ulp_riscv_config_and_run, esp_sleep_enable_timer_wakeup,
ulp_set_wakeup_period, rtc_gpio_init, rtc_gpio_set_direction,
rtc_gpio_set_level, gpio_config, gpio_matrix_out, gpio_matrix_in,
rtc_clk_cpu_freq_get_config, rtc_clk_cpu_freq_set_config_fast,
rtc_clk_apb_freq_get };
extern struct SandboxStruct sandbox_mode;
void app_main(void)
{
printf("Hello world! Keep table at %p\n", &keep_symbols );
g_SandboxStruct = &sandbox_mode;
// esp_efuse_set_rom_log_scheme(ESP_EFUSE_ROM_LOG_ALWAYS_OFF);
esp_efuse_set_rom_log_scheme(ESP_EFUSE_ROM_LOG_ALWAYS_ON);
printf("Minimum free heap size: %d bytes\n", (int)esp_get_minimum_free_heap_size());
void sandbox_main();
sandbox_main();
do
{
if( g_SandboxStruct && g_SandboxStruct->fnIdle ) { g_SandboxStruct->fnIdle(); }
esp_task_wdt_reset();
taskYIELD();
} while( 1 );
// printf("Restarting now.\n");
// fflush(stdout);
// esp_restart();
}
+287
View File
@@ -0,0 +1,287 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#ifndef _SIGGEN_H
#define _SIGGEN_H
#include <string.h>
#include "../../../lib/LoRa-SDR-Code.h"
#define SF_NUMBER 10
// This is MEGA overkill. No way would we ever use them all.
#define MAX_SYMBOLS 2070
// https://electronics.stackexchange.com/questions/278192/understanding-the-relationship-between-lora-chips-chirps-symbols-and-bits
// Has some good hints
// At 125kHz
// https://medium.com/@prajzler/what-is-lora-the-fundamentals-79a5bb3e6dec
// With SF5, You get 3906 chips/second "Symbol Rate" (Included to help with math division)
// With SF7, You get 976 chips/second / 5469 bits/sec
// 7 bits per symbol (I think)
// 7 * 4/5 = 5.6 data bits per symbol.
// https://wirelesspi.com/understanding-lora-phy-long-range-physical-layer/ says 7 for SF7
#define MARK_FROM_SF0 (1<<SF_NUMBER)
// Determined experimentally, but this is the amount you have to divide the chip by to
// Fully use the 125000 Hz channel Bandwidth.
//#define DESPREAD (50*MARK_FROM_SF6)
#define CHIPRATE 8 // chirp length for SF0, in us
#define CHIPSSPREAD ((uint32_t)(240ULL*MARK_FROM_SF0*CHIPRATE))
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
int symbols_len = 1;
uint16_t symbols[MAX_SYMBOLS];
uint32_t quadsetcount;
int32_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int32_t * AddChirp( int32_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
static void SigSetupTest()
{
memset( symbols, 0, sizeof( symbols ) );
uint8_t payload_in[258] = { 0x40/*0x48*/, 0xcc/*0x45*/, 0xde, 0x55, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22};
int payload_in_size = 6;
static uint8_t uctr;
payload_in[4] = uctr++;
int _rdd = 0; // 1 = 4/5, 4 = 4/8 Coding Rate
int r = CreateMessageFromPayload( symbols, &symbols_len, MAX_SYMBOLS, SF_NUMBER, 4, payload_in, payload_in_size );
if( r < 0 )
{
printf( "Error generating stream: %d\n", r );
quadsetcount = 0;
return;
}
int j;
//for( j = 0; j < symbols_len; j++ )
// symbols[j] = 255 - symbols[j];
quadsetcount = 0;
int32_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#if SF_NUMBER <= 6
#define CODEWORD_SHIFT 2 // XXX TODO: No idea what this would do here! XXX This is probably wrong.
#elif SF_NUMBER >= 11
#define CODEWORD_SHIFT 5 // XXX TODO: Unknown for SF11, SF12 Might be 3?
#else
#define CODEWORD_SHIFT 3
#endif
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0 );
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
}
for( j = 0; j < symbols_len; j++ )
{
int ofs = symbols[j];
//ofs = ofs ^ ((MARK_FROM_SF6<<6) -1);
//ofs &= (MARK_FROM_SF6<<6) -1;
qso = AddChirp( qso, ofs, 0 );
}
quadsetcount = qso - quadsets;
// printf( "--- %d %d %d\n", symbols_len, quadsetcount, CHIPSSPREAD/4 );
}
static int32_t SigGen( uint32_t Frame240MHz, uint32_t codeTarg )
{
// TODO: Get some of these encode things going: https://github.com/myriadrf/LoRa-SDR/blob/master/LoRaCodes.hpp
// frame = 0...240000000
uint32_t sectionQuarterNumber = Frame240MHz / (CHIPSSPREAD/4);
if( sectionQuarterNumber >= quadsetcount )
return -codeTarg;
int32_t quadValue = quadsets[sectionQuarterNumber];
uint32_t placeInQuad = Frame240MHz % (CHIPSSPREAD/4);
if( quadValue >= 0 )
return ( ( quadValue + placeInQuad ) % CHIPSSPREAD); // Up-Chirp
else
return ( ( quadValue - placeInQuad + CHIPSSPREAD ) % CHIPSSPREAD ); // Down-Chirp
#if 0
// Let's say 1ms per sweep.
int32_t sectionQuarterNumber = Frame240MHz / (CHIPSSPREAD/4);
uint32_t placeInSweep = Frame240MHz % CHIPSSPREAD;
// 2400 edge-to-edge =
if( sectionQuarterNumber < 0 ) return -codeTarg;
sectionQuarterNumber -= PREAMBLE_CHIRPS*4;
// Preamble Start
if( sectionQuarterNumber < 0 )
{
return ((placeInSweep /*+ 240000/2*/) % CHIPSSPREAD) / DESPREAD;
}
// Last 2 codes here are for the sync word.
#define SYNC_WORD
#ifdef SYNC_WORD
// https://static1.squarespace.com/static/54cecce7e4b054df1848b5f9/t/57489e6e07eaa0105215dc6c/1464376943218/Reversing-Lora-Knight.pdf
// Says that this does not exist. but, it does seem to exist in some of their waterfalls.
sectionQuarterNumber -= 4*2;
// Two symbols
if( sectionQuarterNumber < 0 )
{
int32_t chirp = (8+sectionQuarterNumber)/4;
uint32_t SYNCWORD = (((0x34)>>(4-chirp*4)) & 0xf)<<3; //0x34 for some vendors?
int32_t offset = SYNCWORD * CHIPSSPREAD / (MARK_FROM_SF7*128);
return (( placeInSweep + offset) % CHIPSSPREAD) / DESPREAD;
}
#endif
/*
EXTRA NOTES: lora-grc says to look for sync word 18
*/
sectionQuarterNumber -= 9;
if( sectionQuarterNumber < 0 )
{
// Down-Sweeps for Sync
return ((CHIPSSPREAD-placeInSweep/*+240000/2*/) % CHIPSSPREAD) / DESPREAD;
}
uint32_t chirp = (sectionQuarterNumber)/4;
if( chirp < symbols_len )
{
placeInSweep += (CHIPSSPREAD*3/4);
uint32_t offset = ( symbols[chirp] + 0 ) * CHIPSSPREAD / (MARK_FROM_SF7*128);
fplv = ((placeInSweep + offset) % CHIPSSPREAD) / DESPREAD;
return fplv;
}
else
{
return -codeTarg;
//return -1;
}
#endif
}
/*
A real semtech SX1276 will produce 16 symbols at SF8 if
Given 2 byte payload, CRC explicit header.
Given 3 bytes of payload, that goes up to 24 symbols.
[HEADER] [HEADER*0.5] [HEADER] **An extra byte** [PAYLOAD] [PAYLOAD] [PAYLOAD] [CRC] [CRC] << Does not fit.
Payload sizes:
-5 would be 8 (Considering CRC would be -3 bytes)
-4 would be 8
-3 would be 8
-2 would be 8
-1 would be 16
0 would be 16
1 byte: 16 symbols. (Include CRC so 3 bytes)
2 byte: 16 symbols. (Include CRC so 4 bytes)
// theoreteically, 2 bytes + 2 crc should fit nicely into 16 symbols (or 8 bytes) BUT 3+2 (5) does not.
3 byte: 24 symbols. (Include CRC so 5 bytes)
4 byte: 24 symbols
5 byte: 24 symbols.
6 byte: 24 symbols.
7 byte: 32 symbols.
8 byte: 32 symbols.
9 byte: 32 symbols.
10 byte: 32 symbols.
11 byte: 40 symbols.
That means there's
*/
#endif
+7
View File
@@ -0,0 +1,7 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
# Note: if you change the size of the nvs partition, make sure to update NVS_PARTITION_SIZE in `components/hdw-nvs/nvs_manager.h`
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, spiffs, , 0xF0000,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 # Note: if you change the size of the nvs partition, make sure to update NVS_PARTITION_SIZE in `components/hdw-nvs/nvs_manager.h`
4 nvs, data, nvs, 0x9000, 0x6000,
5 phy_init, data, phy, 0xf000, 0x1000,
6 factory, app, factory, 0x10000, 1M,
7 storage, data, spiffs, , 0xF0000,
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
.vscode
build
+23
View File
@@ -0,0 +1,23 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
add_custom_target(stub_bootload
# If the swadge is running reboot it into the bootloader.
COMMAND make -C ../tools/reboot_into_bootloader reboot
COMMAND sleep 0.5
)
#add_dependencies(flash before_flash)
add_custom_target(stub_run
#Sometimes the swadge needs a kick to get rebooted
COMMAND sleep 0.5
COMMAND esptool.py --before no_reset --after no_reset load_ram ../tools/bootload_reboot_stub/bootload_reboot_stub.bin
)
project(usb_sandbox)
+4
View File
@@ -0,0 +1,4 @@
idf_component_register(SRCS "main.c" "app.c"
INCLUDE_DIRS "" )
+357
View File
@@ -0,0 +1,357 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "hal/gpio_types.h"
#include "esp_log.h"
#include "soc/efuse_reg.h"
#include "soc/soc.h"
#include "soc/system_reg.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/gpio_sig_map.h"
#include "soc/io_mux_reg.h"
#include "soc/dedic_gpio_reg.h"
#include "image.h"
// Funtenna will output on GPIO1 and 14. Otherwise it will be 17 and 18
//#define FUNTENNA
int global_i = 100;
static inline uint32_t getCycleCount()
{
uint32_t ccount;
asm volatile("rsr %0,ccount":"=a" (ccount));
return ccount;
}
#define IO_MUX_REG(x) XIO_MUX_REG(x)
#define XIO_MUX_REG(x) IO_MUX_GPIO##x##_REG
#define GPIO_NUM(x) XGPIO_NUM(x)
#define XGPIO_NUM(x) GPIO_NUM_##x
#ifdef FUNTENNA
#define RF1_PIN 1
#define RF2_PIN 14
#else
#define RF1_PIN 17
#define RF2_PIN 18
#endif
#include "hal/gpio_types.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
#include "soc/i2s_reg.h"
#include "soc/periph_defs.h"
#include "rom/lldesc.h"
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc.h"
#include "soc/regi2c_apll.h"
//#include "components/hal/esp32s2/include/hal/regi2c_ctrl_ll.h
#include "hal/regi2c_ctrl_ll.h"
#include "esp_private/periph_ctrl.h"
#include "esp_private/regi2c_ctrl.h"
#include "hal/clk_tree_ll.h"
#include "siggen.h"
#define I2C_RTC_WIFI_CLK_EN (SYSCON_WIFI_CLK_EN_REG)
#define I2C_RTC_CLK_GATE_EN (BIT(18))
#define I2C_RTC_CLK_GATE_EN_M (BIT(18))
#define I2C_RTC_CLK_GATE_EN_V 0x1
#define I2C_RTC_CLK_GATE_EN_S 18
#define I2C_RTC_CONFIG0 0x6000e048
#define I2C_RTC_MAGIC_CTRL 0x00001FFF
#define I2C_RTC_MAGIC_CTRL_M ((I2C_RTC_MAGIC_CTRL_V)<<(I2C_RTC_MAGIC_CTRL_S))
#define I2C_RTC_MAGIC_CTRL_V 0x1FFF
#define I2C_RTC_MAGIC_CTRL_S 4
#define I2C_RTC_CONFIG1 0x6000e044
#define I2C_RTC_BOD_MASK (BIT(22))
#define I2C_RTC_BOD_MASK_M (BIT(22))
#define I2C_RTC_BOD_MASK_V 0x1
#define I2C_RTC_BOD_MASK_S 22
#define I2C_RTC_SAR_MASK (BIT(18))
#define I2C_RTC_SAR_MASK_M (BIT(18))
#define I2C_RTC_SAR_MASK_V 0x1
#define I2C_RTC_SAR_MASK_S 18
#define I2C_RTC_BBPLL_MASK (BIT(17))
#define I2C_RTC_BBPLL_MASK_M (BIT(17))
#define I2C_RTC_BBPLL_MASK_V 0x1
#define I2C_RTC_BBPLL_MASK_S 17
#define I2C_RTC_APLL_MASK (BIT(14))
#define I2C_RTC_APLL_MASK_M (BIT(14))
#define I2C_RTC_APLL_MASK_V 0x1
#define I2C_RTC_APLL_MASK_S 14
#define I2C_RTC_ALL_MASK 0x00007FFF
#define I2C_RTC_ALL_MASK_M ((I2C_RTC_ALL_MASK_V)<<(I2C_RTC_ALL_MASK_S))
#define I2C_RTC_ALL_MASK_V 0x7FFF
#define I2C_RTC_ALL_MASK_S 8
#define I2C_RTC_CONFIG2 0x6000e000
#define I2C_RTC_BUSY (BIT(25))
#define I2C_RTC_BUSY_M (BIT(25))
#define I2C_RTC_BUSY_V 0x1
#define I2C_RTC_BUSY_S 25
#define I2C_RTC_WR_CNTL (BIT(24))
#define I2C_RTC_WR_CNTL_M (BIT(24))
#define I2C_RTC_WR_CNTL_V 0x1
#define I2C_RTC_WR_CNTL_S 24
#define I2C_RTC_DATA 0x000000FF
#define I2C_RTC_DATA_M ((I2C_RTC_DATA_V)<<(I2C_RTC_DATA_S))
#define I2C_RTC_DATA_V 0xFF
#define I2C_RTC_DATA_S 16
#define I2C_RTC_ADDR 0x000000FF
#define I2C_RTC_ADDR_M ((I2C_RTC_ADDR_V)<<(I2C_RTC_ADDR_S))
#define I2C_RTC_ADDR_V 0xFF
#define I2C_RTC_ADDR_S 8
#define I2C_RTC_SLAVE_ID 0x000000FF
#define I2C_RTC_SLAVE_ID_M ((I2C_RTC_SLAVE_ID_V)<<(I2C_RTC_SLAVE_ID_S))
#define I2C_RTC_SLAVE_ID_V 0xFF
#define I2C_RTC_SLAVE_ID_S 0
#define I2C_RTC_MAGIC_DEFAULT (0x1c40)
#define I2C_BOD 0x61
#define I2C_BBPLL 0x66
#define I2C_SAR_ADC 0X69
#define I2C_APLL 0X6D
int lastend = 0;
// Configures APLL = 480 / 4 = 120
// 40 * (SDM2 + SDM1/(2^8) + SDM0/(2^16) + 4) / ( 2 * (ODIV+2) );
// Datasheet recommends that numerator does not exceed 500MHz.
void IRAM_ATTR local_rtc_clk_apll_enable(bool enable, uint32_t sdm0, uint32_t sdm1, uint32_t sdm2, uint32_t o_div)
{
REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PD, enable ? 0 : 1);
REG_SET_FIELD(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_PLLA_FORCE_PU, enable ? 1 : 0);
if (enable) {
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM2, sdm2);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM0, sdm0);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_DSDM1, sdm1);
REGI2C_WRITE(I2C_APLL, I2C_APLL_SDM_STOP, CLK_LL_APLL_SDM_STOP_VAL_1);
REGI2C_WRITE(I2C_APLL, I2C_APLL_SDM_STOP, CLK_LL_APLL_SDM_STOP_VAL_2_REV1);
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OR_OUTPUT_DIV, o_div );
}
// Settings determined experimentally.
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_TSCHGP, 0 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_EN_FAST_CAL, 1 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DHREF_SEL, 0 ); // 0..3
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DLREF_SEL, 0 ); // 0..3
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_SDM_DITHER, 1 ); // 0 or 1
REGI2C_WRITE_MASK(I2C_APLL, I2C_APLL_OC_DVDD, 25 ); // 0 .. 31
}
void IRAM_ATTR regi2c_write_reg_raw_local(uint8_t block, uint8_t host_id, uint8_t reg_add, uint8_t data)
{
uint32_t temp = ((block & I2C_RTC_SLAVE_ID_V) << I2C_RTC_SLAVE_ID_S)
| ((reg_add & I2C_RTC_ADDR_V) << I2C_RTC_ADDR_S)
| ((0x1 & I2C_RTC_WR_CNTL_V) << I2C_RTC_WR_CNTL_S)
| (((uint32_t)data & I2C_RTC_DATA_V) << I2C_RTC_DATA_S);
while (REG_GET_BIT(I2C_RTC_CONFIG2, I2C_RTC_BUSY));
REG_WRITE(I2C_RTC_CONFIG2, temp);
}
void apll_quick_update( uint32_t sdm )
{
uint8_t sdm2 = sdm>>16;
uint8_t sdm1 = (sdm>>8)&0xff;
uint8_t sdm0 = (sdm>>0)&0xff;
static int last_sdm_0 = -1;
static int last_sdm_1 = -1;
static int last_sdm_2 = -1;
if( sdm2 != last_sdm_2 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM2, sdm2);
if( sdm0 != last_sdm_0 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM0, sdm0);
if( sdm1 != last_sdm_1 )
regi2c_write_reg_raw_local(I2C_APLL, I2C_APLL_HOSTID, I2C_APLL_DSDM1, sdm1);
last_sdm_2 = sdm2;
last_sdm_1 = sdm1;
last_sdm_0 = sdm0;
}
void sandbox_main()
{
printf( "sandbox_main()\n" );
DPORT_SET_PERI_REG_MASK( DPORT_CPU_PERI_CLK_EN_REG, DPORT_CLK_EN_DEDICATED_GPIO );
DPORT_CLEAR_PERI_REG_MASK( DPORT_CPU_PERI_RST_EN_REG, DPORT_RST_EN_DEDICATED_GPIO);
// Setup GPIO39 to be the PRO ALONE output (For LEDs)
REG_WRITE( GPIO_OUT1_W1TC_REG, 1<<(39-32) );
REG_WRITE( GPIO_ENABLE1_W1TS_REG, 1<<(39-32) );
REG_WRITE( IO_MUX_GPIO39_REG, 2<<FUN_DRV_S );
REG_WRITE( GPIO_FUNC39_OUT_SEL_CFG_REG, PRO_ALONEGPIO_OUT0_IDX );
REG_WRITE( DEDIC_GPIO_OUT_CPU_REG, 0x01 ); // Enable CPU instruction output
// Output clock on P2.
// Maximize the drive strength.
gpio_set_drive_capability( GPIO_NUM(RF1_PIN), GPIO_DRIVE_CAP_3 );
gpio_set_drive_capability( GPIO_NUM(RF2_PIN), GPIO_DRIVE_CAP_3 );
// Use the IO matrix to create the inverse of TX on pin 17.
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 0 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 0 );
periph_module_enable(PERIPH_I2S0_MODULE);
int use_apll = 1;
int sdm0 = 100;
int sdm1 = 230;
int sdm2 = 8;
int odiv = 0;
local_rtc_clk_apll_enable( use_apll, sdm0, sdm1, sdm2, odiv );
if( use_apll )
{
WRITE_PERI_REG( I2S_CLKM_CONF_REG(0), (1<<I2S_CLK_SEL_S) | (1<<I2S_CLK_EN_S) | (0<<I2S_CLKM_DIV_A_S) | (0<<I2S_CLKM_DIV_B_S) | (1<<I2S_CLKM_DIV_NUM_S) );
}
else
{
// fI2S = fCLK / ( N + B/A )
// DIV_NUM = N
// Note I2S_CLKM_DIV_NUM minimum = 2 by datasheet. Less than that and it will ignoreeee you.
WRITE_PERI_REG( I2S_CLKM_CONF_REG(0), (2<<I2S_CLK_SEL_S) | (1<<I2S_CLK_EN_S) | (0<<I2S_CLKM_DIV_A_S) | (0<<I2S_CLKM_DIV_B_S) | (1<<I2S_CLKM_DIV_NUM_S) ); // Minimum reduction, 2:1
}
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 0 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 0 );
// SigSetupTest( );
lastend = getCycleCount();
}
#define DisableISR() do { XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); portbenchmarkINTERRUPT_DISABLE(); } while (0)
#define EnableISR() do { portbenchmarkINTERRUPT_RESTORE(0); XTOS_SET_INTLEVEL(0); } while (0)
uint32_t frame = 0;
void sandbox_tick()
{
int iterations = 0;
for( iterations = 0; iterations < 10000; iterations++ )
{
uint32_t start = getCycleCount() & 0x7fffffff;
int cell = start / (uint32_t)(( 0x7fffffff / (IMAGEOH*IMAGEOPL) ));
// Determine what value we should actually be outputting to the APLL.
// By blasting through our image table.
int value = imageout[IMAGEOH*IMAGEOPL - 1 - cell];
uint32_t base = 648985;
if( value == 0 )
{
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 1 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 1 );
}
else
{
apll_quick_update( base + value*6 /* Controls width */ ); // (cell % 4000) );
gpio_matrix_out( GPIO_NUM(RF1_PIN), CLK_I2S_MUX_IDX, 1, 0 );
gpio_matrix_out( GPIO_NUM(RF2_PIN), CLK_I2S_MUX_IDX, 0, 0 );
}
}
}
struct SandboxStruct
{
void (*fnIdle)();
};
struct SandboxStruct sandbox_mode =
{
.fnIdle = sandbox_tick,
};
+421
View File
@@ -0,0 +1,421 @@
// This is a rendition of an image Leonardo Dicaprio in the movie The Great Gadsby.
// It is a derivative work, and copyright is to the respective owner.
#include <stdint.h>
#define IMAGEOH 412
#define IMAGEOPL 32
uint16_t imageout[IMAGEOH*IMAGEOPL] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
235, 236, 237, 238, 256, 257, 258, 259, 260, 261, 273, 274, 275, 276, 277, 278, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232, 233, 234, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 278, 279, 280, 281, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
230, 231, 232, 282, 283, 284, 285, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
228, 229, 285, 286, 287, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
226, 227, 228, 287, 288, 289, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222, 223, 224, 225, 226, 289, 290, 291, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220, 221, 222, 291, 292, 293, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218, 219, 294, 295, 296, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216, 217, 296, 297, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213, 214, 215, 298, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211, 212, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 299, 300, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
209, 210, 228, 229, 230, 231, 246, 247, 248, 249, 301, 302, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
207, 208, 223, 224, 225, 226, 227, 250, 251, 252, 253, 254, 302, 303, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
204, 205, 206, 207, 220, 221, 222, 223, 254, 255, 256, 304, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
202, 203, 204, 217, 218, 219, 256, 257, 258, 259, 294, 295, 305, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
201, 202, 213, 214, 215, 216, 259, 260, 261, 295, 296, 297, 306, 307, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
199, 200, 210, 211, 212, 213, 261, 262, 297, 307, 308, 309, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
198, 199, 208, 209, 210, 262, 263, 264, 265, 297, 298, 309, 310, 311, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
197, 198, 266, 267, 268, 298, 311, 312, 313, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196, 197, 269, 270, 271, 298, 313, 314, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196, 271, 272, 273, 299, 314, 315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195, 196, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 274, 275, 299, 316, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
195, 231, 232, 233, 234, 235, 246, 247, 248, 275, 276, 277, 278, 299, 300, 317, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194, 229, 230, 231, 249, 250, 251, 278, 279, 280, 281, 300, 318, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194, 226, 227, 228, 229, 251, 252, 253, 281, 282, 283, 300, 318, 319, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193, 224, 225, 226, 253, 254, 255, 284, 301, 319, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192, 193, 221, 222, 223, 255, 256, 257, 258, 285, 286, 301, 302, 320, 321, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192, 218, 219, 220, 258, 259, 287, 288, 302, 321, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191, 192, 213, 214, 215, 216, 217, 259, 260, 261, 262, 288, 289, 302, 321, 322, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189, 190, 210, 211, 212, 213, 262, 263, 290, 291, 302, 322, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188, 189, 207, 208, 209, 264, 292, 293, 302, 303, 322, 323, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187, 188, 204, 205, 206, 265, 294, 295, 303, 323, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186, 187, 203, 204, 266, 296, 303, 323, 324, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 186, 200, 201, 202, 267, 304, 324, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183, 184, 199, 267, 304, 324, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182, 183, 196, 197, 198, 268, 304, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180, 181, 182, 193, 194, 195, 269, 304, 305, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180, 270, 305, 325, 326, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179, 271, 305, 326, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178, 179, 227, 228, 229, 230, 272, 305, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178, 222, 223, 224, 225, 226, 230, 231, 232, 233, 272, 273, 305, 306, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178, 218, 219, 220, 221, 234, 235, 236, 273, 274, 306, 327, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177, 178, 215, 216, 217, 218, 236, 237, 238, 239, 274, 275, 306, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177, 213, 214, 215, 240, 241, 275, 306, 329, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177, 209, 210, 211, 212, 213, 241, 242, 243, 276, 307, 329, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176, 177, 207, 208, 209, 243, 244, 276, 277, 307, 329, 330, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176, 205, 206, 207, 245, 246, 277, 307, 330, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176, 203, 204, 247, 277, 278, 307, 331, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176, 202, 203, 248, 249, 278, 307, 331, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175, 176, 200, 201, 249, 250, 278, 279, 308, 331, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175, 199, 200, 215, 216, 217, 218, 219, 220, 221, 222, 223, 250, 251, 279, 280, 308, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175, 195, 196, 197, 198, 199, 212, 213, 214, 215, 224, 225, 226, 252, 253, 280, 308, 333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 175, 193, 194, 195, 211, 212, 226, 227, 228, 229, 254, 280, 281, 309, 333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 191, 192, 193, 209, 210, 229, 230, 231, 254, 255, 281, 309, 334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 188, 189, 190, 207, 208, 209, 231, 232, 233, 256, 282, 309, 335, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 181, 182, 185, 186, 187, 205, 206, 207, 233, 234, 256, 257, 282, 309, 335, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 182, 183, 184, 204, 205, 235, 257, 258, 283, 309, 335, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 203, 204, 235, 236, 258, 259, 284, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 202, 203, 236, 259, 284, 285, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 201, 236, 259, 285, 286, 311, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 199, 200, 237, 260, 286, 309, 310, 311, 312, 337, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 199, 237, 260, 286, 287, 308, 309, 312, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 174, 197, 198, 238, 261, 287, 307, 308, 312, 338, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 197, 238, 262, 306, 312, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 196, 238, 262, 306, 312, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 195, 239, 262, 263, 306, 312, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 194, 195, 239, 263, 305, 306, 312, 313, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 193, 194, 240, 303, 304, 305, 313, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 191, 192, 193, 299, 300, 301, 302, 303, 313, 314, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 189, 190, 191, 296, 297, 298, 314, 339, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 180, 181, 182, 183, 184, 185, 186, 187, 188, 219, 220, 221, 222, 223, 295, 296, 315, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 218, 219, 224, 225, 294, 295, 316, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 217, 225, 292, 293, 317, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 216, 226, 291, 292, 317, 318, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 214, 215, 226, 289, 290, 318, 319, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 213, 214, 226, 288, 289, 319, 340, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 212, 213, 226, 227, 285, 286, 287, 288, 319, 320, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 212, 227, 228, 284, 285, 320, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 211, 228, 229, 230, 281, 282, 283, 320, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 210, 230, 231, 232, 280, 281, 320, 321, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 210, 232, 233, 234, 278, 279, 321, 341, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 209, 235, 236, 277, 278, 321, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 208, 209, 236, 237, 238, 275, 276, 321, 322, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 207, 238, 239, 240, 241, 244, 245, 246, 247, 248, 249, 250, 251, 275, 322, 323, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 207, 241, 242, 243, 251, 252, 273, 274, 323, 343, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 206, 207, 252, 253, 273, 324, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 205, 253, 254, 272, 273, 325, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 205, 254, 255, 272, 326, 327, 342, 343, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 204, 255, 271, 327, 342, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 204, 255, 271, 294, 295, 296, 297, 328, 341, 342, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 203, 255, 256, 257, 270, 289, 290, 291, 292, 293, 294, 329, 341, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 203, 257, 269, 287, 288, 329, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 202, 257, 258, 267, 268, 286, 287, 330, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 201, 202, 259, 260, 266, 267, 284, 285, 330, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 201, 260, 261, 262, 263, 264, 265, 266, 282, 283, 302, 303, 304, 305, 330, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 200, 281, 299, 300, 301, 302, 331, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 200, 279, 280, 295, 296, 297, 298, 331, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 199, 200, 277, 278, 279, 292, 293, 294, 331, 340, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 199, 276, 277, 291, 331, 340, 341, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 172, 199, 274, 275, 331, 341, 343, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 273, 274, 331, 340, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 272, 331, 340, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 270, 271, 331, 340, 344, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 238, 239, 240, 241, 242, 255, 256, 257, 258, 259, 260, 261, 267, 268, 269, 270, 331, 332, 339, 340, 343, 344, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 237, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 261, 262, 263, 264, 265, 266, 267, 295, 296, 297, 298, 332, 339, 343, 0,
172, 199, 290, 291, 292, 293, 294, 295, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 285, 286, 287, 288, 289, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 282, 283, 284, 285, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 199, 200, 279, 280, 281, 282, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 200, 276, 277, 278, 279, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 200, 274, 275, 276, 297, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 200, 271, 272, 273, 274, 295, 296, 297, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 200, 241, 242, 243, 244, 254, 255, 256, 257, 258, 259, 260, 261, 262, 270, 271, 294, 295, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
200, 235, 236, 238, 239, 240, 245, 246, 247, 252, 253, 263, 264, 265, 266, 267, 268, 269, 270, 292, 293, 332, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0,
172, 173, 174, 200, 201, 236, 237, 248, 249, 250, 251, 267, 268, 289, 290, 291, 332, 338, 339, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 175, 201, 285, 286, 287, 288, 289, 332, 338, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 172, 176, 201, 280, 281, 282, 283, 284, 285, 298, 299, 332, 333, 338, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 176, 177, 201, 277, 278, 279, 296, 297, 333, 337, 338, 342, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 177, 201, 275, 276, 295, 333, 337, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 178, 201, 259, 260, 261, 262, 274, 275, 293, 294, 333, 334, 337, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 178, 201, 263, 264, 272, 273, 291, 292, 334, 335, 336, 337, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 179, 201, 264, 265, 266, 267, 268, 269, 270, 271, 272, 290, 291, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 179, 180, 201, 289, 290, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 180, 181, 201, 242, 243, 244, 245, 246, 288, 289, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 181, 201, 241, 242, 246, 247, 248, 249, 250, 287, 288, 309, 310, 311, 312, 313, 314, 315, 342, 343, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171, 181, 201, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 286, 304, 305, 306, 307, 308, 309, 316, 317, 318, 342, 0, 0, 0, 0, 0, 0, 0, 0,
171, 172, 181, 182, 201, 284, 285, 300, 301, 302, 303, 304, 318, 319, 320, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 182, 201, 283, 284, 298, 299, 300, 320, 321, 322, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 182, 183, 201, 295, 296, 297, 322, 323, 324, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172, 173, 183, 200, 201, 293, 294, 295, 316, 317, 318, 319, 320, 321, 324, 325, 341, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 183, 200, 292, 293, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 321, 322, 323, 324, 325, 341, 0, 0, 0, 0, 0, 0, 0, 0,
173, 184, 200, 291, 300, 301, 302, 303, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 184, 185, 199, 200, 290, 291, 297, 298, 299, 300, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173, 185, 199, 289, 290, 295, 296, 297, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 185, 198, 199, 289, 294, 295, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 185, 198, 288, 292, 293, 294, 342, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174, 175, 185, 186, 197, 198, 238, 239, 240, 241, 242, 243, 244, 245, 286, 287, 291, 292, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175, 186, 187, 197, 232, 233, 234, 235, 236, 237, 238, 245, 246, 247, 248, 285, 290, 291, 340, 341, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175, 187, 188, 197, 226, 227, 228, 229, 230, 231, 232, 249, 250, 284, 288, 289, 290, 305, 306, 307, 308, 309, 310, 311, 340, 0, 0, 0, 0, 0, 0, 0,
176, 177, 188, 189, 197, 220, 221, 222, 223, 224, 225, 237, 238, 239, 240, 241, 242, 251, 252, 284, 285, 286, 287, 288, 303, 304, 312, 313, 339, 340, 0, 0,
177, 189, 190, 197, 217, 218, 219, 232, 233, 234, 235, 236, 243, 244, 245, 253, 254, 300, 301, 302, 303, 313, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177, 178, 190, 191, 196, 217, 229, 230, 231, 246, 247, 248, 255, 256, 257, 258, 297, 298, 299, 300, 302, 313, 314, 339, 0, 0, 0, 0, 0, 0, 0, 0,
178, 192, 196, 216, 225, 226, 227, 228, 229, 248, 249, 250, 251, 252, 253, 259, 260, 297, 300, 301, 302, 303, 314, 339, 0, 0, 0, 0, 0, 0, 0, 0,
179, 192, 196, 216, 222, 223, 224, 225, 253, 254, 255, 256, 259, 260, 295, 296, 299, 300, 303, 304, 314, 338, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179, 180, 193, 195, 215, 216, 220, 221, 222, 256, 257, 258, 259, 295, 297, 298, 304, 314, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180, 193, 194, 195, 215, 216, 218, 219, 294, 297, 298, 303, 304, 311, 312, 313, 314, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180, 194, 216, 217, 218, 293, 298, 299, 300, 301, 303, 309, 310, 311, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181, 292, 301, 302, 303, 306, 307, 308, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181, 292, 304, 305, 306, 337, 338, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181, 182, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 292, 293, 302, 303, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182, 235, 236, 239, 240, 241, 242, 243, 244, 247, 248, 249, 250, 293, 294, 295, 296, 297, 298, 299, 300, 301, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0,
182, 183, 234, 238, 239, 244, 245, 250, 251, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183, 233, 237, 238, 245, 251, 252, 282, 283, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184, 232, 233, 237, 244, 245, 253, 282, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184, 230, 231, 232, 237, 244, 253, 254, 282, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184, 185, 228, 229, 230, 237, 238, 239, 240, 241, 242, 243, 254, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 226, 227, 228, 254, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 227, 228, 229, 230, 253, 254, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185, 231, 232, 252, 253, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186, 231, 232, 233, 234, 235, 236, 237, 238, 239, 246, 247, 248, 249, 250, 251, 252, 281, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186, 240, 241, 242, 243, 244, 245, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186, 187, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187, 281, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187, 188, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188, 281, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189, 281, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189, 190, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190, 191, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 105, 106, 107, 108, 109, 110, 111, 112, 191, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102, 103, 104, 112, 113, 192, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101, 102, 113, 114, 192, 193, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 101, 114, 115, 193, 194, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 115, 116, 173, 174, 175, 176, 177, 194, 195, 196, 197, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 116, 117, 166, 167, 168, 169, 170, 171, 172, 177, 178, 179, 180, 197, 198, 199, 281, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 100, 117, 118, 161, 162, 163, 164, 165, 166, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 199, 200, 281, 296, 297, 337, 0, 0, 0, 0, 0,
99, 118, 119, 159, 160, 161, 191, 192, 193, 194, 195, 200, 281, 298, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 99, 119, 120, 156, 157, 158, 196, 197, 200, 202, 203, 204, 205, 206, 207, 281, 299, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 120, 121, 153, 154, 155, 156, 198, 199, 200, 201, 208, 209, 210, 211, 281, 282, 300, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 121, 122, 150, 151, 152, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 282, 301, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 99, 122, 123, 147, 148, 149, 150, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 260, 282, 301, 302, 337, 0, 0,
99, 123, 145, 146, 238, 239, 240, 241, 258, 259, 282, 302, 303, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 124, 142, 143, 144, 242, 243, 257, 258, 282, 283, 298, 303, 304, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 124, 125, 138, 139, 140, 141, 243, 244, 256, 283, 297, 298, 304, 305, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 125, 136, 137, 138, 245, 255, 283, 296, 297, 305, 306, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 125, 126, 132, 133, 134, 135, 246, 254, 255, 283, 296, 306, 307, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 126, 127, 128, 129, 130, 131, 132, 247, 254, 257, 284, 296, 307, 308, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 126, 247, 248, 253, 257, 284, 295, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 126, 248, 253, 258, 284, 294, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 101, 126, 127, 248, 252, 259, 284, 293, 313, 319, 320, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101, 127, 248, 251, 260, 284, 285, 292, 313, 314, 321, 322, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101, 126, 127, 128, 248, 251, 261, 285, 291, 314, 315, 323, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101, 102, 124, 125, 128, 249, 250, 261, 262, 290, 315, 316, 323, 324, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102, 123, 129, 249, 250, 262, 263, 289, 315, 316, 324, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102, 122, 129, 130, 249, 263, 264, 288, 314, 315, 335, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102, 121, 249, 265, 266, 287, 310, 311, 312, 313, 314, 335, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103, 120, 121, 249, 250, 266, 267, 285, 286, 307, 308, 309, 310, 334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103, 120, 250, 268, 269, 284, 285, 306, 307, 322, 334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103, 104, 119, 250, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 282, 283, 284, 305, 306, 321, 322, 333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 119, 250, 278, 279, 280, 281, 282, 304, 321, 333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 119, 250, 302, 303, 320, 321, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 250, 300, 301, 320, 331, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 106, 118, 250, 298, 299, 300, 319, 330, 331, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 118, 249, 250, 296, 297, 298, 319, 330, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 249, 295, 296, 318, 329, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 249, 293, 294, 295, 318, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 249, 291, 292, 293, 317, 327, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 249, 289, 290, 291, 317, 326, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 248, 249, 285, 286, 287, 288, 289, 316, 317, 326, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 248, 249, 250, 251, 258, 259, 278, 279, 280, 281, 282, 283, 284, 285, 316, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 248, 251, 252, 253, 254, 255, 256, 257, 258, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 316, 324,
106, 117, 248, 316, 323, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106, 117, 247, 248, 322, 323, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 247, 322, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 247, 321, 322, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 246, 321, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 246, 320, 321, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105, 118, 119, 245, 320, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 119, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 244, 245, 319, 320, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 119, 139, 140, 141, 142, 143, 155, 156, 244, 319, 323, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 119, 120, 135, 136, 137, 138, 157, 244, 318, 323, 324, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 157, 158, 243, 318, 321, 324, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 158, 159, 243, 317, 318, 321, 325, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 159, 243, 317, 321, 325, 326, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 160, 161, 243, 316, 317, 321, 326, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 161, 242, 316, 321, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104, 162, 242, 315, 321, 327, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103, 104, 162, 163, 242, 315, 321, 327, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103, 163, 241, 314, 321, 328, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102, 103, 163, 164, 241, 313, 314, 321, 329, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101, 164, 240, 313, 321, 329, 330, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100, 164, 240, 313, 321, 330, 331, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99, 164, 239, 240, 312, 313, 321, 331, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 99, 163, 164, 239, 312, 321, 332, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98, 163, 239, 240, 241, 311, 312, 321, 332, 333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97, 162, 163, 239, 240, 241, 242, 311, 321, 333, 334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96, 161, 162, 238, 239, 242, 243, 244, 310, 321, 334, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96, 159, 160, 161, 237, 238, 244, 245, 309, 321, 334, 335, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96, 155, 156, 157, 158, 237, 245, 246, 308, 309, 321, 335, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95, 96, 147, 148, 149, 150, 151, 152, 153, 154, 155, 236, 246, 307, 308, 321, 336, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95, 140, 141, 142, 143, 144, 145, 146, 235, 236, 246, 247, 306, 307, 321, 336, 337, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95, 138, 139, 140, 183, 184, 185, 186, 187, 188, 234, 235, 237, 238, 247, 248, 306, 321, 337, 338, 339, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94, 138, 174, 175, 176, 177, 178, 179, 180, 181, 182, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 234, 236, 239, 240, 248, 305, 321, 339, 0, 0, 0,
94, 137, 138, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 198, 199, 233, 234, 236, 241, 242, 248, 249, 304, 305, 320, 321, 339, 340, 0, 0, 0,
94, 137, 155, 156, 157, 158, 159, 160, 161, 162, 163, 199, 200, 232, 237, 243, 244, 249, 250, 303, 304, 310, 311, 312, 313, 314, 315, 317, 318, 320, 340, 341,
94, 137, 138, 139, 151, 152, 153, 154, 155, 200, 201, 230, 231, 237, 244, 245, 246, 250, 251, 303, 305, 306, 307, 308, 309, 316, 317, 318, 319, 320, 342, 343,
94, 137, 138, 139, 146, 147, 148, 149, 150, 151, 201, 202, 229, 238, 246, 247, 248, 252, 253, 302, 304, 305, 320, 321, 322, 323, 343, 344, 0, 0, 0, 0,
94, 95, 136, 140, 141, 142, 143, 144, 145, 146, 202, 203, 228, 229, 239, 248, 249, 250, 251, 252, 254, 255, 256, 300, 301, 320, 323, 324, 345, 346, 347, 0,
89, 90, 91, 92, 93, 95, 135, 203, 204, 208, 226, 227, 239, 240, 252, 253, 254, 255, 256, 257, 299, 300, 324, 325, 347, 348, 0, 0, 0, 0, 0, 0,
83, 84, 85, 86, 87, 88, 95, 134, 204, 205, 207, 208, 212, 213, 214, 215, 216, 217, 226, 241, 251, 252, 257, 258, 298, 325, 349, 0, 0, 0, 0, 0,
81, 82, 95, 96, 133, 134, 205, 206, 207, 208, 209, 210, 211, 217, 218, 219, 225, 241, 242, 251, 258, 259, 260, 297, 298, 325, 349, 350, 351, 0, 0, 0,
77, 78, 79, 80, 81, 96, 131, 132, 133, 207, 208, 219, 224, 242, 243, 251, 260, 261, 262, 296, 297, 325, 351, 352, 353, 0, 0, 0, 0, 0, 0, 0,
77, 96, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 207, 219, 220, 223, 224, 243, 244, 251, 262, 263, 264, 265, 294, 295, 296, 326, 353, 354, 355,
75, 76, 77, 97, 207, 220, 221, 222, 244, 251, 265, 266, 267, 268, 269, 270, 271, 293, 294, 326, 355, 356, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 71, 72, 73, 74, 75, 97, 98, 207, 221, 222, 244, 245, 250, 272, 273, 274, 291, 292, 293, 326, 356, 357, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68, 69, 70, 98, 207, 221, 222, 223, 245, 246, 250, 274, 275, 276, 277, 288, 289, 290, 291, 326, 357, 358, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66, 67, 98, 207, 221, 246, 250, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 326, 327, 358, 359, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64, 65, 98, 206, 221, 247, 250, 283, 287, 327, 359, 360, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, 63, 98, 205, 206, 221, 247, 250, 284, 288, 327, 360, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61, 62, 98, 204, 205, 220, 221, 248, 249, 250, 284, 288, 327, 361, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61, 98, 202, 203, 204, 220, 249, 284, 288, 327, 361, 362, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59, 60, 98, 200, 201, 202, 220, 249, 284, 288, 327, 362, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59, 98, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 219, 249, 250, 284, 288, 327, 362, 363, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58, 98, 186, 187, 188, 192, 219, 249, 250, 284, 288, 327, 363, 364, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57, 58, 98, 183, 184, 185, 186, 191, 192, 218, 249, 250, 284, 288, 327, 364, 365, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57, 98, 180, 181, 182, 183, 191, 218, 249, 250, 284, 288, 326, 365, 366, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56, 57, 98, 176, 177, 178, 179, 180, 191, 217, 218, 249, 285, 288, 289, 326, 367, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56, 98, 99, 173, 174, 175, 191, 216, 217, 250, 285, 289, 326, 368, 369, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55, 56, 98, 170, 171, 172, 173, 178, 179, 191, 215, 216, 250, 285, 289, 326, 370, 371, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55, 97, 98, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 179, 191, 212, 213, 214, 215, 250, 286, 289, 326, 371, 372, 373, 0, 0, 0, 0,
54, 55, 96, 98, 156, 157, 158, 159, 179, 191, 210, 211, 212, 250, 286, 289, 326, 374, 375, 376, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54, 96, 98, 131, 132, 133, 150, 151, 152, 153, 154, 155, 179, 191, 192, 208, 209, 210, 250, 286, 289, 326, 377, 378, 379, 380, 0, 0, 0, 0, 0, 0,
54, 95, 96, 98, 133, 134, 135, 136, 137, 138, 139, 140, 141, 147, 148, 149, 150, 179, 192, 207, 208, 250, 286, 289, 326, 380, 381, 382, 383, 0, 0, 0,
54, 95, 98, 142, 143, 144, 145, 146, 179, 192, 205, 206, 250, 251, 286, 287, 289, 325, 383, 384, 385, 386, 387, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53, 94, 95, 98, 179, 192, 204, 205, 251, 287, 289, 325, 387, 388, 389, 390, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53, 93, 94, 97, 179, 192, 202, 203, 251, 287, 289, 325, 390, 391, 392, 393, 394, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53, 92, 93, 97, 178, 193, 200, 201, 251, 287, 288, 325, 394, 395, 396, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53, 92, 97, 178, 193, 199, 200, 251, 286, 287, 288, 324, 397, 398, 399, 400, 401, 402, 403, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53, 91, 92, 97, 178, 194, 197, 198, 251, 284, 285, 286, 288, 289, 298, 299, 300, 301, 324, 403, 404, 405, 406, 407, 0, 0, 0, 0, 0, 0, 0, 0,
52, 53, 91, 97, 178, 194, 197, 252, 283, 284, 289, 290, 296, 297, 298, 301, 302, 324, 407, 408, 409, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52, 91, 96, 97, 178, 195, 196, 197, 252, 282, 283, 290, 291, 292, 293, 294, 295, 296, 303, 304, 323, 324, 410, 411, 0, 0, 0, 0, 0, 0, 0, 0,
52, 91, 96, 178, 195, 252, 253, 281, 282, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 323, 412, 413, 0, 0, 0, 0, 0, 0, 0, 0,
52, 90, 91, 95, 96, 178, 195, 253, 281, 315, 316, 317, 321, 322, 323, 413, 414, 415, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52, 90, 95, 178, 195, 254, 255, 280, 317, 318, 320, 415, 416, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52, 90, 95, 179, 195, 255, 256, 279, 280, 318, 319, 320, 416, 417, 418, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52, 89, 90, 95, 179, 195, 256, 257, 277, 278, 279, 319, 320, 418, 419, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52, 89, 95, 155, 156, 157, 158, 159, 160, 161, 162, 163, 179, 195, 258, 274, 275, 276, 320, 419, 420, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51, 52, 88, 94, 152, 153, 154, 163, 164, 165, 166, 167, 178, 179, 195, 259, 272, 273, 274, 320, 421, 422, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51, 88, 94, 167, 168, 169, 170, 171, 177, 179, 195, 260, 270, 271, 272, 320, 422, 423, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50, 51, 88, 94, 95, 171, 172, 176, 177, 179, 195, 260, 261, 264, 265, 269, 270, 320, 423, 424, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50, 88, 95, 172, 173, 175, 179, 195, 261, 262, 263, 264, 265, 266, 268, 269, 320, 425, 426, 427, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50, 88, 95, 173, 174, 175, 179, 195, 262, 263, 266, 267, 320, 427, 428, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49, 50, 87, 95, 96, 174, 179, 195, 267, 320, 429, 430, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49, 87, 96, 170, 171, 172, 173, 179, 195, 267, 320, 431, 432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49, 87, 97, 168, 169, 170, 179, 195, 268, 320, 432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49, 87, 97, 168, 179, 195, 268, 319, 320, 433, 434, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 49, 87, 98, 168, 179, 195, 269, 319, 434, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 87, 98, 168, 180, 194, 269, 270, 319, 435, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 87, 98, 99, 167, 168, 180, 194, 270, 319, 436, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 87, 99, 167, 180, 194, 270, 319, 436, 437, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 87, 99, 167, 180, 194, 270, 319, 437, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 87, 99, 167, 180, 194, 270, 319, 438, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 99, 167, 180, 194, 271, 319, 439, 440, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 99, 167, 180, 194, 272, 319, 440, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 99, 167, 180, 194, 272, 273, 318, 319, 440, 441, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 99, 100, 166, 167, 180, 194, 195, 273, 318, 441, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 100, 165, 166, 180, 195, 274, 275, 318, 441, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 100, 165, 180, 195, 275, 276, 317, 318, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 100, 164, 180, 195, 276, 277, 317, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48, 88, 100, 101, 163, 180, 195, 278, 317, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47, 48, 88, 101, 162, 180, 195, 279, 317, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47, 88, 101, 160, 161, 162, 180, 195, 279, 316, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47, 88, 101, 160, 180, 194, 195, 280, 316, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47, 89, 101, 102, 158, 159, 160, 180, 195, 280, 281, 316, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46, 47, 89, 102, 155, 156, 157, 158, 180, 195, 281, 316, 442, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46, 89, 102, 153, 154, 155, 157, 180, 195, 282, 316, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46, 89, 102, 103, 150, 151, 152, 153, 156, 180, 195, 283, 316, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45, 46, 89, 103, 148, 149, 150, 156, 180, 195, 283, 284, 316, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45, 89, 103, 147, 156, 180, 194, 195, 284, 285, 316, 442, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45, 89, 103, 146, 155, 180, 194, 285, 286, 316, 442, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45, 89, 90, 103, 145, 155, 180, 194, 286, 315, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 90, 104, 144, 145, 154, 155, 180, 194, 286, 315, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 90, 104, 144, 154, 180, 194, 286, 287, 315, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 90, 104, 105, 143, 153, 154, 180, 194, 287, 315, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 90, 91, 105, 143, 153, 180, 194, 287, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 91, 105, 142, 152, 180, 194, 288, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 92, 105, 142, 151, 152, 181, 194, 288, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 92, 106, 141, 142, 151, 181, 194, 288, 289, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44, 92, 106, 107, 141, 150, 181, 194, 289, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43, 92, 93, 107, 141, 149, 150, 181, 194, 290, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43, 93, 107, 140, 141, 148, 181, 194, 290, 314, 443, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43, 93, 107, 139, 140, 148, 181, 194, 290, 314, 443, 444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43, 93, 107, 108, 139, 146, 147, 148, 181, 194, 291, 314, 444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43, 94, 108, 109, 138, 139, 145, 146, 181, 194, 291, 314, 444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42, 43, 94, 109, 110, 137, 138, 144, 181, 194, 291, 292, 314, 444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42, 94, 95, 110, 111, 112, 136, 143, 180, 181, 194, 292, 314, 444, 445, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41, 95, 112, 113, 135, 142, 143, 180, 194, 292, 313, 314, 445, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40, 41, 95, 96, 114, 115, 134, 135, 141, 142, 180, 194, 292, 293, 313, 445, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40, 96, 116, 133, 140, 180, 194, 195, 293, 313, 446, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40, 96, 97, 116, 117, 131, 132, 133, 140, 179, 180, 195, 293, 294, 313, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39, 40, 97, 117, 131, 139, 140, 179, 195, 294, 312, 313, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38, 39, 97, 98, 118, 129, 130, 138, 139, 179, 195, 196, 294, 295, 312, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38, 98, 99, 118, 129, 138, 179, 196, 295, 296, 312, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37, 99, 118, 128, 137, 138, 179, 196, 296, 312, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36, 37, 99, 118, 127, 137, 179, 196, 297, 311, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35, 99, 100, 118, 126, 136, 137, 178, 196, 297, 311, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34, 35, 100, 118, 126, 136, 178, 197, 298, 311, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33, 34, 100, 118, 125, 136, 178, 197, 298, 299, 311, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33, 100, 101, 118, 124, 125, 135, 177, 178, 197, 299, 310, 311, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32, 33, 101, 118, 124, 134, 135, 173, 174, 175, 176, 177, 198, 199, 200, 299, 300, 310, 447, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32, 101, 119, 123, 134, 168, 169, 170, 171, 172, 173, 200, 201, 202, 203, 300, 310, 447, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31, 32, 102, 119, 123, 133, 163, 164, 165, 166, 167, 204, 205, 206, 207, 208, 300, 301, 310, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31, 102, 119, 120, 121, 122, 123, 133, 162, 163, 208, 209, 210, 211, 212, 213, 214, 301, 310, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31, 102, 120, 121, 132, 133, 160, 161, 162, 214, 215, 216, 217, 301, 310, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30, 31, 80, 102, 132, 157, 158, 159, 217, 218, 219, 301, 309, 448, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30, 80, 81, 102, 103, 131, 155, 156, 157, 220, 221, 301, 302, 309, 449, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30, 80, 81, 82, 103, 104, 130, 131, 152, 153, 154, 155, 221, 222, 223, 302, 309, 449, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30, 81, 82, 83, 104, 105, 106, 130, 149, 150, 151, 223, 302, 309, 310, 449, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 81, 82, 83, 84, 85, 86, 106, 107, 108, 129, 130, 147, 148, 149, 223, 302, 303, 309, 449, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 82, 86, 108, 109, 110, 111, 112, 128, 129, 147, 223, 303, 309, 449, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 82, 83, 86, 112, 113, 114, 115, 126, 127, 128, 147, 222, 223, 303, 304, 309, 310, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 83, 84, 85, 86, 115, 116, 117, 125, 126, 147, 222, 304, 310, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 117, 118, 119, 123, 124, 125, 147, 148, 149, 150, 222, 304, 310, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 29, 119, 120, 121, 122, 150, 151, 152, 221, 305, 310, 415, 416, 417, 418, 419, 420, 421, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 152, 153, 154, 155, 221, 305, 306, 310, 415, 421, 422, 423, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 76, 77, 78, 156, 157, 158, 220, 221, 306, 310, 415, 423, 424, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 76, 78, 79, 158, 159, 216, 217, 218, 219, 220, 306, 310, 415, 424, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 75, 76, 80, 159, 160, 161, 208, 209, 210, 211, 212, 213, 214, 215, 307, 309, 415, 416, 424, 425, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28, 75, 81, 161, 162, 163, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 307, 309, 416, 425, 450, 0, 0, 0, 0, 0, 0,
27, 28, 75, 82, 163, 164, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 308, 309, 416, 426, 450, 0,
26, 27, 75, 81, 82, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 308, 309, 416, 427, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26, 75, 76, 77, 79, 80, 81, 309, 416, 427, 428, 429, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25, 26, 77, 78, 79, 416, 429, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24, 25, 416, 429, 430, 450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24, 65, 66, 67, 68, 69, 70, 416, 430, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23, 63, 64, 65, 70, 415, 416, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22, 23, 62, 63, 70, 415, 431, 432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22, 62, 70, 414, 415, 431, 432, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22, 62, 63, 64, 65, 66, 67, 68, 69, 70, 413, 414, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21, 412, 413, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21, 412, 430, 431, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20, 21, 411, 412, 429, 430, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20, 411, 428, 429, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20, 410, 427, 428, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20, 68, 69, 409, 426, 427, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19, 20, 67, 68, 69, 70, 409, 424, 425, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19, 66, 67, 70, 71, 72, 409, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19, 66, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19, 65, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19, 64, 65, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18, 64, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18, 63, 69, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17, 18, 63, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17, 62, 67, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 17, 62, 66, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 62, 63, 64, 65, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
+94
View File
@@ -0,0 +1,94 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "spi_flash_mmap.h"
#include "esp_efuse.h"
#include "esp_efuse_table.h" // or "esp_efuse_custom_table.h"
#include "esp_task_wdt.h"
#include "esp_log.h"
#include "soc/dedic_gpio_reg.h"
#include "driver/gpio.h"
#include "soc/soc.h"
#include "soc/system_reg.h"
#include "soc/usb_reg.h"
#include "ulp_riscv.h"
#include "driver/rtc_io.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
#include "soc/rtc.h"
#define SOC_DPORT_USB_BASE 0x60080000
struct SandboxStruct
{
void (*fnIdle)();
};
struct SandboxStruct * g_SandboxStruct;
void esp_sleep_enable_timer_wakeup();
volatile void * keep_symbols[] = { 0, vTaskDelay, ulp_riscv_halt,
ulp_riscv_timer_resume, ulp_riscv_timer_stop, ulp_riscv_load_binary,
ulp_riscv_run, ulp_riscv_config_and_run, esp_sleep_enable_timer_wakeup,
ulp_set_wakeup_period, rtc_gpio_init, rtc_gpio_set_direction,
rtc_gpio_set_level, gpio_config, gpio_matrix_out, gpio_matrix_in,
rtc_clk_cpu_freq_get_config, rtc_clk_cpu_freq_set_config_fast,
rtc_clk_apb_freq_get };
extern struct SandboxStruct sandbox_mode;
void app_main(void)
{
printf("Hello world! Keep table at %p\n", &keep_symbols );
g_SandboxStruct = &sandbox_mode;
// esp_efuse_set_rom_log_scheme(ESP_EFUSE_ROM_LOG_ALWAYS_OFF);
esp_efuse_set_rom_log_scheme(ESP_EFUSE_ROM_LOG_ALWAYS_ON);
printf("Minimum free heap size: %d bytes\n", (int)esp_get_minimum_free_heap_size());
void sandbox_main();
sandbox_main();
do
{
if( g_SandboxStruct && g_SandboxStruct->fnIdle ) { g_SandboxStruct->fnIdle(); }
esp_task_wdt_reset();
taskYIELD();
} while( 1 );
// printf("Restarting now.\n");
// fflush(stdout);
// esp_restart();
}
+1
View File
@@ -0,0 +1 @@
// This file is unused in the video ending test
+12
View File
@@ -0,0 +1,12 @@
IMAGEOUT:=../main/image.h
all : $(IMAGEOUT)
$(IMAGEOUT) : image.png imagegen
./imagegen $< > $@
imagegen : imagegen.c
gcc -o $@ $^ -Os -lm
clean :
rm -rf *.o imagegen $(IMAGEOUT)
@@ -0,0 +1,4 @@
Place a:
* Black and white (solid lines only, black background white lines, 1px wide, no grey) "image.png" file in this folder.
* I recently tested with a 461x412 pixel file so around there should be fine.
* Say `make` and it should produce `../main/image.h` from your image.
+81
View File
@@ -0,0 +1,81 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define IMAGEOPL 32
int main( int argc, char ** argv )
{
if( argc != 2 )
{
fprintf( stderr, "Error: Need input image file\n" );
return -9;
}
int w, h, n;
unsigned char *data = stbi_load( argv[1], &w, &h, &n, 1 );
if( !data )
{
fprintf( stderr, "Error: Failed to load image \"%s\"\n", argv[1] );
}
int i;
printf( "#include <stdint.h>\n" );
printf( "\n" );
printf( "#define IMAGEOH %d\n", h );
printf( "#define IMAGEOPL %d\n", IMAGEOPL );
printf( "uint16_t imageout[IMAGEOH*IMAGEOPL] = {\n" );
int x, y;
int o;
for( y = 0; y < h; y++ )
{
o = 0;
for( x = 0; x < w; x++ )
{
uint8_t c = data[(x+y*w)];
if( c > 128 )
{
if( o < IMAGEOPL )
{
printf( "%d, ", x ); o++;
}
else
{
fprintf( stderr, "Warning: overload on line %d\n", y );
}
}
}
for( ; o < IMAGEOPL; o++ )
{
printf( "0, " );
}
printf( "\n" );
}
printf( "};\n" );
}
File diff suppressed because it is too large Load Diff
+7
View File
@@ -0,0 +1,7 @@
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
# Note: if you change the size of the nvs partition, make sure to update NVS_PARTITION_SIZE in `components/hdw-nvs/nvs_manager.h`
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, spiffs, , 0xF0000,
1 # Name, Type, SubType, Offset, Size, Flags
2 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3 # Note: if you change the size of the nvs partition, make sure to update NVS_PARTITION_SIZE in `components/hdw-nvs/nvs_manager.h`
4 nvs, data, nvs, 0x9000, 0x6000,
5 phy_init, data, phy, 0xf000, 0x1000,
6 factory, app, factory, 0x10000, 1M,
7 storage, data, spiffs, , 0xF0000,
File diff suppressed because it is too large Load Diff
+134
View File
@@ -0,0 +1,134 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _ESP8266_I2S_SETUP
#define _ESP8266_I2S_SETUP
#include "slc_register.h"
#include "dmastuff.h"
/* Need something like:
#define DMABUFFERDEPTH 3
#define WS_I2S_BCK SPI_DIV //Can't be less than 1.
#define WS_I2S_DIV 1
*/
static struct sdio_queue i2sBufDescTX[DMABUFFERDEPTH] __attribute__((aligned(128)));;
void slc_isr(void * v);
static void testi2s_init( uint32_t * initial_data );
//Initialize I2S subsystem for DMA circular buffer use
static void testi2s_init( uint32_t * default_data ) {
int x, y;
//Bits are shifted out
//Initialize DMA buffer descriptors in such a way that they will form a circular
//buffer.
for (x=0; x<DMABUFFERDEPTH; x++) {
i2sBufDescTX[x].owner=1;
i2sBufDescTX[x].eof=1; // Trigger interrupt on packet complete.
i2sBufDescTX[x].sub_sof=0;
i2sBufDescTX[x].datalen=DMA_SIZE_WORDS*4;
i2sBufDescTX[x].blocksize=4;
i2sBufDescTX[x].buf_ptr= ((uint32_t)default_data);
i2sBufDescTX[x].unused=0;
i2sBufDescTX[x].next_link_ptr=(int)((x<(DMABUFFERDEPTH-1))?(&i2sBufDescTX[x+1]):(&i2sBufDescTX[0]));
}
//Reset DMA )
//SLC_TX_LOOP_TEST = IF this isn't set, SO will occasionally get unrecoverable errors when you underflow.
//Originally this little tidbit was found at https://github.com/pvvx/esp8266web/blob/master/info/libs/bios/sip_slc.c
//
//I have not tried without SLC_AHBM_RST | SLC_AHBM_FIFO_RST. I just assume they are useful?
SET_PERI_REG_MASK(SLC_CONF0, SLC_TX_LOOP_TEST |SLC_RXLINK_RST|SLC_TXLINK_RST|SLC_AHBM_RST | SLC_AHBM_FIFO_RST);
CLEAR_PERI_REG_MASK(SLC_CONF0, SLC_RXLINK_RST|SLC_TXLINK_RST|SLC_AHBM_RST | SLC_AHBM_FIFO_RST);
//Clear DMA int flags
SET_PERI_REG_MASK(SLC_INT_CLR, 0xffffffff);
CLEAR_PERI_REG_MASK(SLC_INT_CLR, 0xffffffff);
//Enable and configure DMA
CLEAR_PERI_REG_MASK(SLC_CONF0, (SLC_MODE<<SLC_MODE_S));
SET_PERI_REG_MASK(SLC_CONF0,(1<<SLC_MODE_S));
// We have to do this, otherwise, when the end of a "RX" packet is hit, it will skip outputting a few random frames.
SET_PERI_REG_MASK(SLC_RX_DSCR_CONF,SLC_INFOR_NO_REPLACE|SLC_TOKEN_NO_REPLACE);
CLEAR_PERI_REG_MASK(SLC_RX_DSCR_CONF, SLC_RX_FILL_EN|SLC_RX_EOF_MODE | SLC_RX_FILL_MODE);
CLEAR_PERI_REG_MASK(SLC_RX_LINK,SLC_RXLINK_DESCADDR_MASK);
SET_PERI_REG_MASK(SLC_RX_LINK, ((uint32)&i2sBufDescTX[0]) & SLC_RXLINK_DESCADDR_MASK);
//Attach the DMA interrupt
ets_isr_attach(ETS_SLC_INUM, slc_isr, 0);
WRITE_PERI_REG(SLC_INT_ENA, SLC_RX_EOF_INT_ENA ); // Not including SLC_RX_UDF_INT_ENA
//clear any interrupt flags that are set
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
///enable DMA intr in cpu
ets_isr_unmask(1<<ETS_SLC_INUM);
//Start transmission
SET_PERI_REG_MASK(SLC_RX_LINK, SLC_RXLINK_START);
//Init pins to i2s functions
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_I2SO_DATA); // GPIO3
// PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_I2SO_WS); // GPIO2
// PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_I2SO_BCK); // GPIO15
//Enable clock to i2s subsystem
i2c_writeReg_Mask_def(i2c_bbpll, i2c_bbpll_en_audio_clock_out, 1);
//Reset I2S subsystem
CLEAR_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
SET_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
CLEAR_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
CLEAR_PERI_REG_MASK(I2S_FIFO_CONF, I2S_I2S_DSCR_EN|(I2S_I2S_RX_FIFO_MOD<<I2S_I2S_RX_FIFO_MOD_S));
SET_PERI_REG_MASK(I2S_FIFO_CONF, I2S_I2S_DSCR_EN);
WRITE_PERI_REG(I2SRXEOF_NUM, DMA_SIZE_WORDS*4);
CLEAR_PERI_REG_MASK(I2SCONF_CHAN, (I2S_RX_CHAN_MOD<<I2S_RX_CHAN_MOD_S));
CLEAR_PERI_REG_MASK(I2SCONF, I2S_TRANS_SLAVE_MOD|I2S_RECE_SLAVE_MOD|
(I2S_BITS_MOD<<I2S_BITS_MOD_S)|
(I2S_BCK_DIV_NUM <<I2S_BCK_DIV_NUM_S)|
(I2S_CLKM_DIV_NUM<<I2S_CLKM_DIV_NUM_S));
SET_PERI_REG_MASK(I2SCONF, I2S_RIGHT_FIRST|I2S_MSB_RIGHT|
I2S_RECE_MSB_SHIFT|I2S_TRANS_MSB_SHIFT|
((WS_I2S_BCK&I2S_BCK_DIV_NUM )<<I2S_BCK_DIV_NUM_S)|
((WS_I2S_DIV&I2S_CLKM_DIV_NUM)<<I2S_CLKM_DIV_NUM_S) );
//Start transmission
SET_PERI_REG_MASK(I2SCONF,I2S_I2S_TX_START|I2S_I2S_RX_START);
}
#endif
+31
View File
@@ -0,0 +1,31 @@
all : chirpbuff.dat.flash
SUBMODULE=YES
SRCPREFIX=../nosdk8266/
MAIN_MHZ=52
SRCS=main.c
DEPS=chirpbuffinfo.h
COMPILECOUNT=$(shell cat compilecount.txt || true)
BUILDCOUNTCMD=echo 1+0$(COMPILECOUNT) | bc > compilecount.txt | true Compiled $(COMPILECOUNT) times
CFLAGS=-I../../lib -I..
include ../nosdk8266/Makefile
rf_data_gen : rf_data_gen.c
gcc -o $@ $^ -lm -DMAIN_MHZ=$(MAIN_MHZ)
chirpbuffinfo.h : chirpbuff.dat
teststrap : main.c chirpbuff.dat
gcc -o teststrap main.c -DTESTSTRAP -I../nosdk8266/include
chirpbuff.dat : rf_data_gen
./rf_data_gen
chirpbuff.dat.flash : chirpbuff.dat
$(ESPUTIL) -b 115200 flash 0x20000 chirpbuff.dat
cleanall : clean
rm -rf chirpbuff.h rf_data_gen chirpbuff.dat chirpbuffinfo.h
+6
View File
@@ -0,0 +1,6 @@
# ESP8266 / ESP8285 Lora Transmitter
Connect a small length of wire to the "RX" pin on your ESP8266, then it will transmit SF7 LoRa messages at 904.1MHz.
This is designed to work at 173MHz, but, you can set `MAIN_MHZ=80` in the Makefile, and run it without any overclocking.
@@ -0,0 +1 @@
45
+378
View File
@@ -0,0 +1,378 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
// This was my first test, it still contains a number of TESTSTRAP flags.
// Teststrap was used when I compiled and ran this code on a host PC.
// In general, don't use this, code is just left around in case I run into
// any really weird bugs.
#ifdef TESTSTRAP
#include <stdint.h>
#include <stdio.h>
#include "chirpbuff.h"
#include <stdlib.h>
#define uint32 uint32_t
#else
#include "esp8266_auxrom.h"
#include "eagle_soc.h"
#include "nosdk8266.h"
#include "esp8266_rom.h"
// TODO: Use float number (related to 8) to fix the drift
#define call_delay_us(time) { asm volatile("mov.n a2, %0\n_call0 delay4clk" : : "r"(time * (MAIN_MHZ / 8)) : "a2" ); }
#include "ets_sys.h"
#include "pin_mux_register.h"
#endif
#include "slc_register.h"
#include "dmastuff.h"
#include "chirpbuffinfo.h"
#include "LoRa-SDR-Code.h"
#define DMABUFFERDEPTH 3
//These contol the speed at which the bus comms.
#define WS_I2S_BCK SPI_DIV //Can't be less than 1.
#define WS_I2S_DIV 1
#ifndef TESTSTRAP
#include "esp8266_i2s_setup.h"
#endif
uint32_t chirpbuffUP[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t chirpbuffDOWN[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t dummy[DMA_SIZE_WORDS];
volatile int fxcycle;
int etx;
// Limit because of RAM usage, but this should be able to hold a practical limit of a 255 byte LoRa packet.
#define MAX_SYMBOLS 532
// Our table is bespoke for the specific SF.
#define CHIPSSPREAD CHIRPLENGTH_WORDS// QUARTER_CHIRP_LENGTH_WORDS (TODO: Use the quater value elsewhere in the code)
#define MARK_FROM_SF0 (1<<SF_NUMBER) // SF7
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
uint32_t quadsetcount;
int32_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int32_t * AddChirp( int32_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
volatile int quadsetplace = -1;
int runningcount_bits = 0;
void slc_isr(void * v) {
uint32_t * sendbuff = 0;
uint32_t sendlen = 0;
struct sdio_queue *finishedDesc;
// slc_intr_status = READ_PERI_REG(SLC_INT_STATUS); -> We should check to make sure we are SLC_RX_EOF_INT_ST, but we are only getting one interrupt.
#ifdef TESTSTRAP
struct sdio_queue tmp;
finishedDesc = &tmp;
#else
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
finishedDesc=(struct sdio_queue*)READ_PERI_REG(SLC_RX_EOF_DES_ADDR);
#endif
etx++;
if( quadsetplace < 0 )
{
goto dump0;
}
// LoRa symbols are in quarters of a chirp.
if( fxcycle>= NUM_DMAS_PER_QUARTER_CHIRP )
{
fxcycle = 0;
quadsetplace++;
if( quadsetplace >= quadsetcount ) goto dump0;
}
int symbol = quadsets[quadsetplace];
// Select down- or up-chirp.
if( symbol < 0 )
{
int word = fxcycle * DMA_SIZE_WORDS - symbol - 1;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
word++;
sendbuff = (chirpbuffDOWN + word);
}
else
{
int word = fxcycle * DMA_SIZE_WORDS + symbol;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
sendbuff = (chirpbuffUP + word);
}
#ifndef FOUND_PERFECT_DIVISOR
// Sometimes we do the full length, of all of the needed DMAs
// Sometimes we overshoot the time window, so we peel off 4 bytes.
//
// Very few combinations of clock rate, divisor, etc can produce
// perfect divisors. Most notably 52MHz, /2 SF9 can produce a perfect
// divisor. While this is very tidy and beautiful that the
// words would align perfectly, the actual difference it makes on
// LoRa's ability to receive the message is minimal.
//
// Additionally, 80MHz /2 SF7 can produce a perfect divisor.
int running_bits_after = runningcount_bits + DMA_SIZE_WORDS*32;
int overflow = running_bits_after - IDEAL_QUARTER_CHIRP_LENGTH_BITS;
if( overflow >= 0 )
{
int overflow_amount = overflow / 32;
int overflow_remainder = overflow % 32;
sendlen = DMA_SIZE_WORDS*4 - 4*overflow_amount;
runningcount_bits = overflow_remainder;
// XXX TODO: Why can't I put the logic for advancing the group in here?
}
else
{
sendlen = DMA_SIZE_WORDS*4;
runningcount_bits = running_bits_after;
}
#else
sendlen = DMA_SIZE_WORDS*4;
#endif
#ifdef TESTSTRAP
static FILE * fappendlog;
if( !fappendlog ) fappendlog = fopen( "fappendlog.csv", "w" );
{
if( symbol < 0 )
fprintf( fappendlog, "2, %d, %d\n", (int)(CHIRPLENGTH_WORDS - (sendbuff - chirpbuffDOWN) - 1), sendlen );
else
fprintf( fappendlog, "1, %d, %d\n", (int)(sendbuff - chirpbuffUP), sendlen );
}
#else
finishedDesc->buf_ptr = (uint32_t)sendbuff;
finishedDesc->datalen = sendlen;
#endif
fxcycle++;
return;
dump0:
#ifdef TESTSTRAP
printf( "Hit dummy %d %d\n", quadsetplace, quadsetcount );
exit( 0 );
#else
// This location just always reads as zeroes.
finishedDesc->buf_ptr = (uint32_t)dummy;
quadsetplace = -1;
#endif
return;
}
#ifdef TESTSTRAP
void SPIRead( uint32_t pos, uint32_t * buff, int len )
{
memcpy( buff, (pos - 0x00020000) + (uint8_t*)chirpbuff, len );
}
void nosdk8266_init()
{
}
void testi2s_init( uint32_t * default_address )
{
}
#endif
int main()
{
// We store the bit pattern at flash:0x20000, so we don't have to constantly
// re-write it when working on code.
SPIRead( MEMORY_START_OFFSET_BYTES, chirpbuffUP, sizeof( chirpbuffUP ) );
SPIRead( REVERSE_START_OFFSET_BYTES, chirpbuffDOWN, sizeof( chirpbuffDOWN ) );
memset( dummy, 0, sizeof( dummy ) );
// Don't crank up clock speed til we're done with flash.
nosdk8266_init();
int i = 0;
fxcycle = 0;
etx = 0;
#ifndef TESTSTRAP
// Configure GPIO5 (TX) and GPIO2 (LED)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U,FUNC_GPIO2);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U,FUNC_GPIO5);
PIN_DIR_OUTPUT = _BV(2); //Enable GPIO2 light off.
// Run the I2S bus at 1040/6 = 173.333 MHz.
// It looks like, at least on my part, if I try running
// hotter it can get to 1040/5.1 but not all the way to
// 5 so it's unstable there.
#endif
testi2s_init( dummy );
int frame = 0;
uint16_t lora_symbols[MAX_SYMBOLS];
int lora_symbols_count;
while(1) {
//12x this speed.
frame++;
#ifndef TESTSTRAP
PIN_OUT_SET = _BV(2); //Turn GPIO2 light off.
//call_delay_us(1000000);
printf("ETX: %d %08x\n", fxcycle, chirpbuffUP[10] );
PIN_OUT_CLEAR = _BV(2); //Turn GPIO2 light off.
call_delay_us(500000);
#endif
call_delay_us(2000000);
// Just some random data.
uint8_t payload_in[259] = { 0xbb, 0xcc, 0xde, 0x55, 0x22,};
int payload_in_size = 6;
static int msgno = 0;
payload_in[4] = msgno++;
lora_symbols_count = 0;
int r = CreateMessageFromPayload( lora_symbols, &lora_symbols_count, MAX_SYMBOLS, SF_NUMBER, 4, payload_in, payload_in_size );
if( r < 0 )
{
printf( "Failed to generate message (%d)\n", r );
// Failed
continue;
}
int j;
quadsetcount = 0;
int32_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#if SF_NUMBER == 6
#define CODEWORD_SHIFT 2
#else
#define CODEWORD_SHIFT 3
#endif
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0);
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
qso = AddChirp( qso, 0, 0 );
}
for( j = 0; j < lora_symbols_count; j++ )
{
int ofs = lora_symbols[j];
//ofs = ofs ^ ((MARK_FROM_SF6<<6) -1);
//ofs &= (MARK_FROM_SF6<<6) -1;
qso = AddChirp( qso, ofs, 0 );
printf( "%02x ", ofs );
}
printf( "\n" );
runningcount_bits = 0;
// This tells the interrupt we have data.
quadsetcount = qso - quadsets + 0;
printf( "--- %d [%d] %d\n", lora_symbols_count, quadsetcount, CHIPSSPREAD/4 );
quadsetplace = 0;
#ifdef TESTSTRAP
while(1)
{
slc_isr( 0 );
}
#endif
}
}
+65
View File
@@ -0,0 +1,65 @@
/**
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include <stdint.h>
const double center_frequency = 904.5;
const double bw = .125;
const uint32_t memory_offset = 0x20000;
#define SF_NUMBER 9
#define SF_SYMBOL_TIME 0.000001
#define MEM_MAX_BYTES 68000
#if SF_NUMBER < 7
#warning SF6 still does not work :(
#endif
#define SPI_DIV 1
// Funny modes:
/// 80MHz, SF8, SPI_DIV 5 @903.9/904.1 produces hilarious mirror images around 904.0
#if MAIN_MHZ == 80
const double sample_rate = 1040.0/13.0/SPI_DIV;
#elif MAIN_MHZ == 115
const double sample_rate = 1040.0/9.0/SPI_DIV;
#elif MAIN_MHZ == 52
const double sample_rate = 1040.0/20.0/SPI_DIV;
#elif MAIN_MHZ == 173
const double sample_rate = 1040.0/6.0/SPI_DIV;
#else
#error Unknown Clock Rate
#endif
#include "../../lib/rf_data_gen.h"
int main()
{
return gen_buffer_files();
}
+31
View File
@@ -0,0 +1,31 @@
all : chirpbuff.dat.flash
SUBMODULE=YES
SRCPREFIX=../nosdk8266/
MAIN_MHZ=80
SRCS=main.c
DEPS=chirpbuffinfo.h
COMPILECOUNT=$(shell cat compilecount.txt || true)
BUILDCOUNTCMD=echo 1+0$(COMPILECOUNT) | bc > compilecount.txt | true Compiled $(COMPILECOUNT) times
CFLAGS=-I../../lib -I..
include ../nosdk8266/Makefile
rf_data_gen : rf_data_gen.c
gcc -o $@ $^ -lm -DMAIN_MHZ=$(MAIN_MHZ)
chirpbuffinfo.h : chirpbuff.dat
teststrap : main.c chirpbuff.dat
gcc -o teststrap main.c -DTESTSTRAP -I../nosdk8266/include
chirpbuff.dat : rf_data_gen
./rf_data_gen
chirpbuff.dat.flash : chirpbuff.dat
$(ESPUTIL) -b 115200 flash 0x20000 chirpbuff.dat
cleanall : clean
rm -rf chirpbuff.h rf_data_gen chirpbuff.dat chirpbuffinfo.h
+5
View File
@@ -0,0 +1,5 @@
# ESP8266 / ESP8285 Lora Transmitter
Connect a small length of wire to the "RX" pin on your ESP8266, then it will transmit SF10 LoRa messages at 904.6MHz, with channel width of 500kHz.
Be sure to configure your receiver to specifically use SF10, 500kHz, on 904.6MHz. Many commercial receivers cannot automatically negotiate 500kHz channels.
@@ -0,0 +1 @@
46
+294
View File
@@ -0,0 +1,294 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include "esp8266_auxrom.h"
#include "eagle_soc.h"
#include "nosdk8266.h"
#include "esp8266_rom.h"
// TODO: Use float number (related to 8) to fix the drift
#define call_delay_us(time) { asm volatile("mov.n a2, %0\n_call0 delay4clk" : : "r"(time * (MAIN_MHZ / 8)) : "a2" ); }
#include "ets_sys.h"
#include "pin_mux_register.h"
#include "slc_register.h"
#include "dmastuff.h"
#include "chirpbuffinfo.h"
#include "LoRa-SDR-Code.h"
#define DMABUFFERDEPTH 3
//These contol the speed at which the bus comms.
#define WS_I2S_BCK SPI_DIV //Can't be less than 1.
#define WS_I2S_DIV 1
#ifndef TESTSTRAP
#include "esp8266_i2s_setup.h"
#endif
uint32_t chirpbuffUP[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t chirpbuffDOWN[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t dummy[DMA_SIZE_WORDS];
volatile int fxcycle;
int etx;
// This "should" be big enough to store up to 255 bytes of payload in a LoRa message.
#define MAX_SYMBOLS 532
// Our table is bespoke for the specific SF.
#define CHIPSSPREAD CHIRPLENGTH_WORDS// QUARTER_CHIRP_LENGTH_WORDS (TODO: Use the quater value elsewhere in the code)
#define MARK_FROM_SF0 (1<<SF_NUMBER)
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
uint32_t quadsetcount;
int32_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int32_t * AddChirp( int32_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
volatile int quadsetplace = -1;
int runningcount_bits = 0;
void slc_isr(void * v) {
uint32_t * sendbuff = 0;
uint32_t sendlen = 0;
struct sdio_queue *finishedDesc;
// slc_intr_status = READ_PERI_REG(SLC_INT_STATUS); -> We should check to make sure we are SLC_RX_EOF_INT_ST, but we are only getting one interrupt.
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
finishedDesc=(struct sdio_queue*)READ_PERI_REG(SLC_RX_EOF_DES_ADDR);
etx++;
if( quadsetplace < 0 )
{
goto dump0;
}
// LoRa symbols are in quarters of a chirp.
if( fxcycle>= NUM_DMAS_PER_QUARTER_CHIRP )
{
fxcycle = 0;
quadsetplace++;
if( quadsetplace >= quadsetcount ) goto dump0;
}
int symbol = quadsets[quadsetplace];
// Select down- or up-chirp.
if( symbol < 0 )
{
int word = fxcycle * DMA_SIZE_WORDS - symbol - 1;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
word++;
sendbuff = (chirpbuffDOWN + word);
}
else
{
int word = fxcycle * DMA_SIZE_WORDS + symbol;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
sendbuff = (chirpbuffUP + word);
}
#ifndef FOUND_PERFECT_DIVISOR
// Sometimes we do the full length, of all of the needed DMAs
// Sometimes we overshoot the time window, so we peel off 4 bytes.
//
// Very few combinations of clock rate, divisor, etc can produce
// perfect divisors. Most notably 52MHz, /2 SF9 can produce a perfect
// divisor. While this is very tidy and beautiful that the
// words would align perfectly, the actual difference it makes on
// LoRa's ability to receive the message is minimal.
//
// Additionally, 80MHz /2 SF7 can produce a perfect divisor.
int running_bits_after = runningcount_bits + DMA_SIZE_WORDS*32;
int overflow = running_bits_after - IDEAL_QUARTER_CHIRP_LENGTH_BITS;
if( overflow >= 0 )
{
int overflow_amount = overflow / 32;
int overflow_remainder = overflow % 32;
sendlen = DMA_SIZE_WORDS*4 - 4*overflow_amount;
runningcount_bits = overflow_remainder;
// XXX TODO: Why can't I put the logic for advancing the group in here?
}
else
{
sendlen = DMA_SIZE_WORDS*4;
runningcount_bits = running_bits_after;
}
#else
sendlen = DMA_SIZE_WORDS*4;
#endif
finishedDesc->buf_ptr = (uint32_t)sendbuff;
finishedDesc->datalen = sendlen;
fxcycle++;
return;
dump0:
// This location just always reads as zeroes.
finishedDesc->buf_ptr = (uint32_t)dummy;
quadsetplace = -1;
return;
}
int main()
{
// We store the bit pattern at flash:0x20000, so we don't have to constantly
// re-write it when working on code.
SPIRead( MEMORY_START_OFFSET_BYTES, chirpbuffUP, sizeof( chirpbuffUP ) );
SPIRead( REVERSE_START_OFFSET_BYTES, chirpbuffDOWN, sizeof( chirpbuffDOWN ) );
memset( dummy, 0, sizeof( dummy ) );
// Don't crank up clock speed til we're done with flash.
nosdk8266_init();
int i = 0;
fxcycle = 0;
etx = 0;
testi2s_init( dummy );
int frame = 0;
uint16_t lora_symbols[MAX_SYMBOLS];
int lora_symbols_count;
while(1) {
//12x this speed.
frame++;
PIN_OUT_SET = _BV(2); //Turn GPIO2 light off.
//call_delay_us(1000000);
printf("ETX: %d %08x\n", fxcycle, chirpbuffUP[10] );
PIN_OUT_CLEAR = _BV(2); //Turn GPIO2 light on.
call_delay_us(1000000);
// Just some random data.
uint8_t payload_in[259] = { 0xbb, 0xcc, 0xde, 0x55, 0x22,};
int payload_in_size = 6;
static int msgno = 0;
payload_in[4] = msgno++;
lora_symbols_count = 0;
int r = CreateMessageFromPayload( lora_symbols, &lora_symbols_count, MAX_SYMBOLS, SF_NUMBER, 4, payload_in, payload_in_size );
if( r < 0 )
{
printf( "Failed to generate message (%d)\n", r );
// Failed
continue;
}
int j;
quadsetcount = 0;
int32_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#define CODEWORD_SHIFT 3
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0);
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
qso = AddChirp( qso, 0, 0 );
}
for( j = 0; j < lora_symbols_count; j++ )
{
int ofs = lora_symbols[j];
qso = AddChirp( qso, ofs, 0 );
printf( "%02x ", ofs );
}
printf( "\n" );
runningcount_bits = 0;
// This tells the interrupt we have data.
quadsetcount = qso - quadsets + 0;
printf( "--- %d [%d] %d\n", lora_symbols_count, quadsetcount, CHIPSSPREAD/4 );
quadsetplace = 0;
}
}
+65
View File
@@ -0,0 +1,65 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdint.h>
const double center_frequency = 904.6;
const double bw = .5;
const uint32_t memory_offset = 0x20000;
#define SF_NUMBER 10
#define SF_SYMBOL_TIME 0.00000025
#define MEM_MAX_BYTES 68000
#if SF_NUMBER < 7
#warning SF6 still does not work :(
#endif
#define SPI_DIV 1
// Funny modes:
/// 80MHz, SF8, SPI_DIV 5 @903.9/904.1 produces hilarious mirror images around 904.0
#if MAIN_MHZ == 80
const double sample_rate = 1040.0/13.0/SPI_DIV;
#elif MAIN_MHZ == 115
const double sample_rate = 1040.0/9.0/SPI_DIV;
#elif MAIN_MHZ == 52
const double sample_rate = 1040.0/20.0/SPI_DIV;
#elif MAIN_MHZ == 173
const double sample_rate = 1040.0/6.0/SPI_DIV;
#else
#error Unknown Clock Rate
#endif
#include "../../lib/rf_data_gen.h"
int main()
{
return gen_buffer_files();
}
+31
View File
@@ -0,0 +1,31 @@
all : chirpbuff.dat.flash
SUBMODULE=YES
SRCPREFIX=../nosdk8266/
MAIN_MHZ=173
SRCS=main.c
DEPS=chirpbuffinfo.h
COMPILECOUNT=$(shell cat compilecount.txt || true)
BUILDCOUNTCMD=echo 1+0$(COMPILECOUNT) | bc > compilecount.txt | true Compiled $(COMPILECOUNT) times
CFLAGS=-I../../lib -I..
include ../nosdk8266/Makefile
rf_data_gen : rf_data_gen.c
gcc -o $@ $^ -lm -DMAIN_MHZ=$(MAIN_MHZ)
chirpbuffinfo.h : chirpbuff.dat
teststrap : main.c chirpbuff.dat
gcc -o teststrap main.c -DTESTSTRAP -I../nosdk8266/include
chirpbuff.dat : rf_data_gen
./rf_data_gen
chirpbuff.dat.flash : chirpbuff.dat
$(ESPUTIL) -b 115200 flash 0x20000 chirpbuff.dat
cleanall : clean
rm -rf chirpbuff.h rf_data_gen chirpbuff.dat chirpbuffinfo.h
+27
View File
@@ -0,0 +1,27 @@
# ESP8266 / ESP8285 TheThings Network LoRaWAN transmitter.
Same as the others - connect a small length of wire to the "RX" pin on your ESP8266, then it will transmit SF7 LoRa messages at 904.1MHz.
But this, has the code in main() to send frames to thethings.network as valid LoRaWAN frames.
On thethings.network,
1. Generate a new app.
2. Register a new device.
3. Select `Enter end device specifics manually`
4. Select US Plan 902-928, FSB2 (Used by TTN)
5. LoRaWAN Specification 1.0.0
6. TS001 Technical Specificaiton 1.0.0
7. "Show Advanced activation, LoRaWAN class and cluster settings"
8. Activation by Personalization
9. No addtional LoRaWAN class capabilities (class A only)
10. Use Network's default MAC settings.
11. Generate new EUI.
12. Generate new Device ID.
Add the device, it may make sense to back out to the menu so you can copy the LSB for the address, and the code for MSB for keys.
Replace the `payload_key`, `network_skey`, and `devaddress` in `main.c`
+1
View File
@@ -0,0 +1 @@
117
+333
View File
@@ -0,0 +1,333 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
// Transmit LoRaWAN Packets from an ESP8266 - be sure to fill in your
// payload_key, network_skey and devaddress
#include "esp8266_auxrom.h"
#include "eagle_soc.h"
#include "nosdk8266.h"
#include "esp8266_rom.h"
#include "ets_sys.h"
#include "pin_mux_register.h"
#include "chirpbuffinfo.h"
#include "LoRa-SDR-Code.h"
#define DMABUFFERDEPTH 3
//These contol the speed at which the bus comms.
#define WS_I2S_BCK SPI_DIV //Can't be less than 1.
#define WS_I2S_DIV 1
#include "esp8266_i2s_setup.h"
#include "lorawan_simple.h"
uint32_t chirpbuffUP[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t chirpbuffDOWN[CHIRPLENGTH_WORDS_WITH_PADDING];
uint32_t dummy[DMA_SIZE_WORDS];
volatile int fxcycle;
int etx;
// Practical limit because of RAM usage. But this should be able to hold a full-sized (255-byte) LoRa packet.
#define MAX_SYMBOLS 532
// Our table is bespoke for the specific SF.
#define CHIPSSPREAD CHIRPLENGTH_WORDS// QUARTER_CHIRP_LENGTH_WORDS (TODO: Use the quater value elsewhere in the code)
#define MARK_FROM_SF0 (1<<SF_NUMBER) // SF7
#define PREAMBLE_CHIRPS 10
#define CODEWORD_LENGTH 2
uint32_t quadsetcount;
int32_t quadsets[MAX_SYMBOLS*4+PREAMBLE_CHIRPS*4+9+CODEWORD_LENGTH*4];
int32_t * AddChirp( int32_t * qso, int offset, int verneer )
{
offset = offset * CHIPSSPREAD / (MARK_FROM_SF0);
offset += verneer;
*(qso++) = (CHIPSSPREAD * 0 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 1 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 2 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
*(qso++) = (CHIPSSPREAD * 3 / 4 + offset + CHIPSSPREAD ) % CHIPSSPREAD;
return qso;
}
volatile int quadsetplace = -1;
int runningcount_bits = 0;
// TODO: Use float number (related to 8) to fix the drift
#define call_delay_us(time) { asm volatile("mov.n a2, %0\n_call0 delay4clk" : : "r"(time * (MAIN_MHZ / 8)) : "a2" ); }
void slc_isr(void * v) {
uint32_t * sendbuff = 0;
uint32_t sendlen = 0;
struct sdio_queue *finishedDesc;
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
finishedDesc=(struct sdio_queue*)READ_PERI_REG(SLC_RX_EOF_DES_ADDR);
etx++;
if( quadsetplace < 0 )
{
goto dump0;
}
// LoRa symbols are in quarters of a chirp.
if( fxcycle>= NUM_DMAS_PER_QUARTER_CHIRP )
{
fxcycle = 0;
quadsetplace++;
if( quadsetplace >= quadsetcount ) goto dump0;
}
int symbol = quadsets[quadsetplace];
// Select down- or up-chirp.
if( symbol < 0 )
{
int word = fxcycle * DMA_SIZE_WORDS - symbol - 1;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
word++;
sendbuff = (chirpbuffDOWN + word);
}
else
{
int word = fxcycle * DMA_SIZE_WORDS + symbol;
if( word >= CHIPSSPREAD ) word -= CHIPSSPREAD;
sendbuff = (chirpbuffUP + word);
}
#ifndef FOUND_PERFECT_DIVISOR
// Sometimes we do the full length, of all of the needed DMAs
// Sometimes we overshoot the time window, so we peel off 4 bytes.
//
// Very few combinations of clock rate, divisor, etc can produce
// perfect divisors. Most notably 52MHz, /2 SF9 can produce a perfect
// divisor. While this is very tidy and beautiful that the
// words would align perfectly, the actual difference it makes on
// LoRa's ability to receive the message is minimal.
//
// Additionally, 80MHz /2 SF7 can produce a perfect divisor.
int running_bits_after = runningcount_bits + DMA_SIZE_WORDS*32;
int overflow = running_bits_after - IDEAL_QUARTER_CHIRP_LENGTH_BITS;
if( overflow >= 0 )
{
int overflow_amount = overflow / 32;
int overflow_remainder = overflow % 32;
sendlen = DMA_SIZE_WORDS*4 - 4*overflow_amount;
runningcount_bits = overflow_remainder;
// XXX TODO: Why can't I put the logic for advancing the group in here?
}
else
{
sendlen = DMA_SIZE_WORDS*4;
runningcount_bits = running_bits_after;
}
#else
sendlen = DMA_SIZE_WORDS*4;
#endif
finishedDesc->buf_ptr = (uint32_t)sendbuff;
finishedDesc->datalen = sendlen;
fxcycle++;
return;
dump0:
// This location just always reads as zeroes.
finishedDesc->buf_ptr = (uint32_t)dummy;
quadsetplace = -1;
return;
}
int main()
{
// We store the bit pattern at flash:0x20000, so we don't have to constantly
// re-write it when working on code.
SPIRead( MEMORY_START_OFFSET_BYTES, chirpbuffUP, sizeof( chirpbuffUP ) );
SPIRead( REVERSE_START_OFFSET_BYTES, chirpbuffDOWN, sizeof( chirpbuffDOWN ) );
memset( dummy, 0, sizeof( dummy ) );
// Don't crank up clock speed til we're done with flash.
nosdk8266_init();
int i = 0;
fxcycle = 0;
etx = 0;
// Configure GPIO5 (TX) and GPIO2 (LED)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U,FUNC_GPIO2);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U,FUNC_GPIO5);
PIN_DIR_OUTPUT = _BV(2); //Enable GPIO2 light off.
// Run the I2S bus at 1040/6 = 173.333 MHz.
// It looks like, at least on my part, if I try running
// hotter it can get to 1040/5.1 but not all the way to
// 5 so it's unstable there.
testi2s_init( dummy );
int frame = 0;
uint16_t lora_symbols[MAX_SYMBOLS];
int lora_symbols_count;
frame = 0;
while(1) {
//12x this speed.
frame++;
PIN_OUT_SET = _BV(2); //Turn GPIO2 light off.
printf("ETX: %d %08x\n", fxcycle, chirpbuffUP[10] );
PIN_OUT_CLEAR = _BV(2); //Turn GPIO2 light on.
call_delay_us(5000000);
// Send a message with LoraWan. Formatted specifically for thethings.network.
uint8_t inner_payload_raw[20] = { 'T', 'e', 's', 't', '0', '0', '0', '0', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
inner_payload_raw[4] = ((frame/1000)%10)+'0';
inner_payload_raw[5] = ((frame/100)%10)+'0';
inner_payload_raw[6] = ((frame/10)%10)+'0';
inner_payload_raw[7] = (frame%10)+'0';
int inner_payload_len = 20;
// Get these from your thethings.network console.
// GENERAL NOTE: When addinga new device, go to General Settings -> Network Layer -> Advanced MAC Settings -> Resets frame counters [check]
static const uint8_t payload_key[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // AppSKey Big Endian
static const uint8_t network_skey[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // NwkSKey Big Endian
static const uint8_t devaddress[4] = { 0x00, 0x00, 0x00, 0x00 }; // Device address Little Endian LSB (Written backwards from Device address default view)
#warning Please set payload_key, network_skey and devaddress
// Just some random data.
uint8_t raw_payload_with_b0[259+8] = { };
uint8_t * raw_payload = raw_payload_with_b0 + 16;
uint8_t * pl = raw_payload;
int raw_payload_size = 0;
pl += GenerateLoRaWANPacket( raw_payload_with_b0, inner_payload_raw, inner_payload_len, payload_key, network_skey, devaddress, frame);
raw_payload_size = pl - raw_payload;
printf( "Length: %d\n", raw_payload_size );
for( i = 0; i < raw_payload_size; i++ )
printf( "%02x ", raw_payload[i] );
printf( "\n" );
lora_symbols_count = 0;
int r = CreateMessageFromPayload( lora_symbols, &lora_symbols_count, MAX_SYMBOLS, SF_NUMBER, 4, raw_payload, raw_payload_size );
if( r < 0 )
{
printf( "Failed to generate message (%d)\n", r );
// Failed
continue;
}
int j;
quadsetcount = 0;
int32_t * qso = quadsets;
for( j = 0; j < PREAMBLE_CHIRPS; j++ )
{
qso = AddChirp( qso, 0, 0 );
}
uint8_t syncword = 0x43;
#define CODEWORD_SHIFT 3
if( CODEWORD_LENGTH > 0 )
qso = AddChirp( qso, ( ( syncword & 0xf ) << CODEWORD_SHIFT ), 0 );
if( CODEWORD_LENGTH > 1 )
qso = AddChirp( qso, ( ( ( syncword & 0xf0 ) >> 4 ) << CODEWORD_SHIFT ), 0);
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 1 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 2 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 3 / 4 )-1;
*(qso++) = -(CHIPSSPREAD * 0 / 4 )-1;
if( SF_NUMBER <= 6 )
{
// Two additional upchirps with SF6 https://github.com/tapparelj/gr-lora_sdr/issues/74#issuecomment-1891569580
for( j = 0; j < 2; j++ )
qso = AddChirp( qso, 0, 0 );
}
for( j = 0; j < lora_symbols_count; j++ )
{
int ofs = lora_symbols[j];
//ofs = ofs ^ ((MARK_FROM_SF6<<6) -1);
//ofs &= (MARK_FROM_SF6<<6) -1;
qso = AddChirp( qso, ofs, 0 );
printf( "%02x ", ofs );
}
printf( "\n" );
runningcount_bits = 0;
// This tells the interrupt we have data.
quadsetcount = qso - quadsets + 0;
printf( "--- %d [%d] %d frame %08x\n", lora_symbols_count, quadsetcount, CHIPSSPREAD/4, frame );
quadsetplace = 0;
}
}
+189
View File
@@ -0,0 +1,189 @@
Known Reference
QF/ZDCaAGwAB1Rwa9Hh7AF9mVtq3 -> (CRC 41445, SF10 CR 4/5, 904.5MHz) -> App Payload 1Rwa9Hh7AF8= -> Mikrotik address 26 0C D9 5F ->
From the things network: QF/ZDCaAGwAB1Rwa9Hh7AF9mVtq3 "frm_payload": "1Rwa9Hh7AF8=" // "mic": "Zlbatw==",
// 8 A's. 0x41 x 8
// Full raw packet
405fd90c26801b0001
d51c1af4787b005f
6656dab7
// My test
405fd90c26801b0001
d51c1af4787b005f
6656dab7
{
"name": "as.up.data.forward",
"time": "2024-02-18T03:25:33.806043236Z",
"identifiers": [
{
"device_ids": {
"device_id": "eui-70b3d57ed00651da",
"application_ids": {
"application_id": "cursed-esp8266"
},
"dev_eui": "70B3D57ED00651DA",
"dev_addr": "260CD95F"
}
}
],
"data": {
"@type": "type.googleapis.com/ttn.lorawan.v3.ApplicationUp",
"end_device_ids": {
"device_id": "eui-70b3d57ed00651da",
"application_ids": {
"application_id": "cursed-esp8266"
},
"dev_eui": "70B3D57ED00651DA",
"dev_addr": "260CD95F"
},
"correlation_ids": [
"gs:uplink:01HPX3YSGKBGH71N3Q8T2BGJCM"
],
"received_at": "2024-02-18T03:25:33.803070864Z",
"uplink_message": {
"f_port": 1,
"f_cnt": 27,
"frm_payload": "QUFBQUFBQUE=",
"rx_metadata": [
{
"gateway_ids": {
"gateway_id": "eui-343632383e002300",
"eui": "343632383E002300"
},
"time": "2024-02-18T03:25:25.274488Z",
"timestamp": 1948262652,
"rssi": -33,
"channel_rssi": -33,
"snr": 12.5,
"uplink_token": "CiIKIAoUZXVpLTM0MzYzMjM4M2UwMDIzMDASCDQ2Mjg+ACMAEPzBgKEHGgwIrfHFrgYQ+IemmAIg4LCD7Nk4",
"channel_index": 3,
"received_at": "2024-02-18T03:25:33.573997023Z"
}
],
"settings": {
"data_rate": {
"lora": {
"bandwidth": 125000,
"spreading_factor": 10,
"coding_rate": "4/5"
}
},
"frequency": "904500000",
"timestamp": 1948262652,
"time": "2024-02-18T03:25:25.274488Z"
},
"received_at": "2024-02-18T03:25:33.589092783Z",
"consumed_airtime": "0.370688s",
"network_ids": {
"net_id": "000013",
"ns_id": "EC656E0000000182",
"tenant_id": "ttn",
"cluster_id": "nam1",
"cluster_address": "nam1.cloud.thethings.network"
}
}
},
"correlation_ids": [
"gs:uplink:01HPX3YSGKBGH71N3Q8T2BGJCM"
],
"origin": "ip-10-101-7-121.us-west-1.compute.internal",
"context": {
"tenant-id": "CgN0dG4="
},
"visibility": {
"rights": [
"RIGHT_APPLICATION_TRAFFIC_READ"
]
},
"unique_id": "01HPX3YSQE3XHB4JGNEBQYAB2E"
}
{
"name": "gs.up.receive",
"time": "2024-02-18T03:25:33.588003627Z",
"identifiers": [
{
"gateway_ids": {
"gateway_id": "eui-343632383e002300",
"eui": "343632383E002300"
}
}
],
"data": {
"@type": "type.googleapis.com/ttn.lorawan.v3.GatewayUplinkMessage",
"message": {
"raw_payload": "QF/ZDCaAGwAB1Rwa9Hh7AF9mVtq3",
"payload": {
"m_hdr": {
"m_type": "UNCONFIRMED_UP"
},
"mic": "Zlbatw==",
"mac_payload": {
"f_hdr": {
"dev_addr": "260CD95F",
"f_ctrl": {
"adr": true
},
"f_cnt": 27
},
"f_port": 1,
"frm_payload": "1Rwa9Hh7AF8="
}
},
"settings": {
"data_rate": {
"lora": {
"bandwidth": 125000,
"spreading_factor": 10,
"coding_rate": "4/5"
}
},
"frequency": "904500000",
"timestamp": 1948262652,
"time": "2024-02-18T03:25:25.274488Z"
},
"rx_metadata": [
{
"gateway_ids": {
"gateway_id": "eui-343632383e002300",
"eui": "343632383E002300"
},
"time": "2024-02-18T03:25:25.274488Z",
"timestamp": 1948262652,
"rssi": -33,
"channel_rssi": -33,
"snr": 12.5,
"uplink_token": "CiIKIAoUZXVpLTM0MzYzMjM4M2UwMDIzMDASCDQ2Mjg+ACMAEPzBgKEHGgwIrfHFrgYQ+IemmAIg4LCD7Nk4",
"channel_index": 3,
"received_at": "2024-02-18T03:25:33.573997023Z"
}
],
"received_at": "2024-02-18T03:25:33.587826168Z",
"correlation_ids": [
"gs:uplink:01HPX3YSGKBGH71N3Q8T2BGJCM"
],
"crc_status": true
},
"band_id": "US_902_928"
},
"correlation_ids": [
"gs:uplink:01HPX3YSGKBGH71N3Q8T2BGJCM"
],
"origin": "ip-10-101-4-161.us-west-1.compute.internal",
"context": {
"tenant-id": "CgN0dG4="
},
"visibility": {
"rights": [
"RIGHT_GATEWAY_TRAFFIC_READ"
]
},
"unique_id": "01HPX3YSGMTSH0JBWT8BEZQJRC"
}
+65
View File
@@ -0,0 +1,65 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdint.h>
const double center_frequency = 904.1;
const double bw = .125;
const uint32_t memory_offset = 0x20000;
#define SF_NUMBER 7
#define SF_SYMBOL_TIME 0.000001
#define MEM_MAX_BYTES 68000
#if SF_NUMBER < 7
#warning SF6 still does not work :(
#endif
#define SPI_DIV 1
// Funny modes:
/// 80MHz, SF8, SPI_DIV 5 @903.9/904.1 produces hilarious mirror images around 904.0
#if MAIN_MHZ == 80
const double sample_rate = 1040.0/13.0/SPI_DIV;
#elif MAIN_MHZ == 115
const double sample_rate = 1040.0/9.0/SPI_DIV;
#elif MAIN_MHZ == 52
const double sample_rate = 1040.0/20.0/SPI_DIV;
#elif MAIN_MHZ == 173
const double sample_rate = 1040.0/6.0/SPI_DIV;
#else
#error Unknown Clock Rate
#endif
#include "../../lib/rf_data_gen.h"
int main()
{
return gen_buffer_files();
}
+25
View File
@@ -0,0 +1,25 @@
all : flash
SUBMODULE=YES
SRCPREFIX=../nosdk8266/
MAIN_MHZ=173
SRCS=main.c
DEPS=samples.h
COMPILECOUNT=$(shell cat compilecount.txt || true)
BUILDCOUNTCMD=echo 1+0$(COMPILECOUNT) | bc > compilecount.txt | true Compiled $(COMPILECOUNT) times
CFLAGS=-I../../lib
include ../nosdk8266/Makefile
rf_data_gen : rf_data_gen.c
gcc -g -Og -o $@ $^ -lm -DMAIN_MHZ=$(MAIN_MHZ)
samples.h : rf_data_gen
./rf_data_gen
#chirpbuff.dat.flash : chirpbuff.dat
# $(ESPUTIL) -b 115200 flash 0x20000 chirpbuff.dat
cleanall : clean
rm -rf rf_data_gen samples.h
+11
View File
@@ -0,0 +1,11 @@
# ESP8266 / ESP8285 310 MHz Transmitter
Transmits a 310 (or configurable) OOK pulse coded signal out the RX pin on the ESP8266/ESP8286.
You can also test it with an SDR and define
```c
#define CONTINUOUS_TONE
```
Frequency / word length can be configured in `rf_data_gen.c`
@@ -0,0 +1 @@
117
+294
View File
@@ -0,0 +1,294 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include "esp8266_auxrom.h"
#include "eagle_soc.h"
#include "nosdk8266.h"
#include "esp8266_rom.h"
#include <string.h>
#include "ets_sys.h"
#include "pin_mux_register.h"
#include "slc_register.h"
#include "dmastuff.h"
#include "samples.h"
//#define CONTINUOUS_TONE
// The speed at which bits are shifted out - this is done with trial and error. Try one speed
// and figure out how it aligns to your target system. The units are too messy for me to be
// troubled with thinking of.
#define BIT_RATE 245
#define num_replays (( SAMPLE_RATE / (DMA_WORDS*NUMBITS) ) / BIT_RATE)
uint8_t output_buffer[] = {
// OOK, map to time_limits_for_bits, 0 is short pulse, 1 is long pulse, 2 is no-signal.
// You will need to manually use your 310MHz device and record the pulse timing
// To fill out this array with thecorrect code for your opener.
0, 1, 0, 0, 0, 1, 1, 2, 2, 2, 2, 1, 0, 1, 0, 1, 0, 0, 1, 1,
};
uint32_t time_limits_for_bits[3] = { num_replays/8, num_replays/2, 0 };
uint32_t dummy[DMA_WORDS];
void testi2s_init();
//These contol the speed at which the bus comms.
#define DMABUFFERDEPTH 3
#define WS_I2S_BCK SPI_DIV //Can't be less than 1.
#define WS_I2S_DIV 1
#define call_delay_us(time) { asm volatile("mov.n a2, %0\n_call0 delay4clk" : : "r"(time * (MAIN_MHZ / 8)) : "a2" ); }
//I2S DMA buffer descriptors
static struct sdio_queue i2sBufDescTX[DMABUFFERDEPTH] __attribute__((aligned(128)));;
volatile int output_buffer_place = 1000000;
volatile int count_out_this_bit = 0;
volatile int etx = 0;
void slc_isr(void * v) {
uint32_t * sendbuff = 0;
struct sdio_queue *finishedDesc;
// slc_intr_status = READ_PERI_REG(SLC_INT_STATUS); -> We should check to make sure we are SLC_RX_EOF_INT_ST, but we are only getting one interrupt.
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
finishedDesc=(struct sdio_queue*)READ_PERI_REG(SLC_RX_EOF_DES_ADDR);
#ifdef CONTINUOUS_TONE
return;
#endif
etx++;
if( output_buffer_place >= sizeof( output_buffer ) / sizeof( output_buffer[0] ) )
{
goto dump0;
}
count_out_this_bit++;
if( count_out_this_bit >= num_replays )
{
count_out_this_bit = 0;
output_buffer_place++;
if( output_buffer_place >= sizeof( output_buffer ) / sizeof( output_buffer[0] ) )
{
goto dump0;
}
}
int symbol = output_buffer[output_buffer_place];
if( count_out_this_bit < time_limits_for_bits[symbol] )
{
sendbuff = samples;
}
else
{
sendbuff = dummy;
}
finishedDesc->buf_ptr = (uint32_t)sendbuff;
finishedDesc->datalen = DMA_WORDS*4;
return;
dump0:
finishedDesc->buf_ptr = (uint32_t)dummy;
finishedDesc->datalen = DMA_WORDS*4;
return;
}
#ifdef TESTSTRAP
void SPIRead( uint32_t pos, uint32_t * buff, int len )
{
memcpy( buff, (pos - 0x00020000) + (uint8_t*)chirpbuff, len );
}
void nosdk8266_init()
{
}
void testi2s_init()
{
}
#else
//Initialize I2S subsystem for DMA circular buffer use
void testi2s_init() {
int x, y;
//Bits are shifted out
//Initialize DMA buffer descriptors in such a way that they will form a circular
//buffer.
for (x=0; x<DMABUFFERDEPTH; x++) {
i2sBufDescTX[x].owner=1;
i2sBufDescTX[x].eof=1; // Trigger interrupt on packet complete.
i2sBufDescTX[x].sub_sof=0;
i2sBufDescTX[x].datalen=DMA_WORDS*4;
i2sBufDescTX[x].blocksize=4;
#ifdef CONTINUOUS_TONE
i2sBufDescTX[x].buf_ptr= ((uint32_t)samples);
#else
i2sBufDescTX[x].buf_ptr= ((uint32_t)dummy);
#endif
i2sBufDescTX[x].unused=0;
i2sBufDescTX[x].next_link_ptr=(int)((x<(DMABUFFERDEPTH-1))?(&i2sBufDescTX[x+1]):(&i2sBufDescTX[0]));
}
//Reset DMA )
//SLC_TX_LOOP_TEST = IF this isn't set, SO will occasionally get unrecoverable errors when you underflow.
//Originally this little tidbit was found at https://github.com/pvvx/esp8266web/blob/master/info/libs/bios/sip_slc.c
//
//I have not tried without SLC_AHBM_RST | SLC_AHBM_FIFO_RST. I just assume they are useful?
SET_PERI_REG_MASK(SLC_CONF0, SLC_TX_LOOP_TEST |SLC_RXLINK_RST|SLC_TXLINK_RST|SLC_AHBM_RST | SLC_AHBM_FIFO_RST);
CLEAR_PERI_REG_MASK(SLC_CONF0, SLC_RXLINK_RST|SLC_TXLINK_RST|SLC_AHBM_RST | SLC_AHBM_FIFO_RST);
//Clear DMA int flags
SET_PERI_REG_MASK(SLC_INT_CLR, 0xffffffff);
CLEAR_PERI_REG_MASK(SLC_INT_CLR, 0xffffffff);
//Enable and configure DMA
CLEAR_PERI_REG_MASK(SLC_CONF0, (SLC_MODE<<SLC_MODE_S));
SET_PERI_REG_MASK(SLC_CONF0,(1<<SLC_MODE_S));
// We have to do this, otherwise, when the end of a "RX" packet is hit, it will skip outputting a few random frames.
SET_PERI_REG_MASK(SLC_RX_DSCR_CONF,SLC_INFOR_NO_REPLACE|SLC_TOKEN_NO_REPLACE);
CLEAR_PERI_REG_MASK(SLC_RX_DSCR_CONF, SLC_RX_FILL_EN|SLC_RX_EOF_MODE | SLC_RX_FILL_MODE);
CLEAR_PERI_REG_MASK(SLC_RX_LINK,SLC_RXLINK_DESCADDR_MASK);
SET_PERI_REG_MASK(SLC_RX_LINK, ((uint32)&i2sBufDescTX[0]) & SLC_RXLINK_DESCADDR_MASK);
//Attach the DMA interrupt
ets_isr_attach(ETS_SLC_INUM, slc_isr, 0);
WRITE_PERI_REG(SLC_INT_ENA, SLC_RX_EOF_INT_ENA ); // Not including SLC_RX_UDF_INT_ENA
//clear any interrupt flags that are set
WRITE_PERI_REG(SLC_INT_CLR, 0xffffffff);
///enable DMA intr in cpu
ets_isr_unmask(1<<ETS_SLC_INUM);
//Start transmission
SET_PERI_REG_MASK(SLC_RX_LINK, SLC_RXLINK_START);
//Init pins to i2s functions
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_I2SO_DATA); // GPIO3
// PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_I2SO_WS); // GPIO2
// PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, FUNC_I2SO_BCK); // GPIO15
//Enable clock to i2s subsystem
i2c_writeReg_Mask_def(i2c_bbpll, i2c_bbpll_en_audio_clock_out, 1);
//Reset I2S subsystem
CLEAR_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
SET_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
CLEAR_PERI_REG_MASK(I2SCONF,I2S_I2S_RESET_MASK);
CLEAR_PERI_REG_MASK(I2S_FIFO_CONF, I2S_I2S_DSCR_EN|(I2S_I2S_RX_FIFO_MOD<<I2S_I2S_RX_FIFO_MOD_S));
SET_PERI_REG_MASK(I2S_FIFO_CONF, I2S_I2S_DSCR_EN);
WRITE_PERI_REG(I2SRXEOF_NUM, DMA_WORDS*4);
CLEAR_PERI_REG_MASK(I2SCONF_CHAN, (I2S_RX_CHAN_MOD<<I2S_RX_CHAN_MOD_S));
CLEAR_PERI_REG_MASK(I2SCONF, I2S_TRANS_SLAVE_MOD|I2S_RECE_SLAVE_MOD|
(I2S_BITS_MOD<<I2S_BITS_MOD_S)|
(I2S_BCK_DIV_NUM <<I2S_BCK_DIV_NUM_S)|
(I2S_CLKM_DIV_NUM<<I2S_CLKM_DIV_NUM_S));
SET_PERI_REG_MASK(I2SCONF, I2S_RIGHT_FIRST|I2S_MSB_RIGHT|
I2S_RECE_MSB_SHIFT|I2S_TRANS_MSB_SHIFT|
((WS_I2S_BCK&I2S_BCK_DIV_NUM )<<I2S_BCK_DIV_NUM_S)|
((WS_I2S_DIV&I2S_CLKM_DIV_NUM)<<I2S_CLKM_DIV_NUM_S) );
//Start transmission
SET_PERI_REG_MASK(I2SCONF,I2S_I2S_TX_START|I2S_I2S_RX_START);
}
#endif
int main()
{
// We store the bit pattern at flash:0x20000, so we don't have to constantly
// re-write it when working on code.
memset( dummy, 0, sizeof( dummy ) );
int frame = 0;
output_buffer_place = 1000000;
count_out_this_bit = 0;
etx = 0;
// Don't crank up clock speed til we're done with flash.
nosdk8266_init();
testi2s_init();
#ifdef CONTINUOUS_TONE
while(1);
#endif
while(1) {
//12x this speed.
frame++;
PIN_OUT_SET = _BV(2); //Turn GPIO2 light off.
//call_delay_us(1000000);
printf("ETX: %d %d %d %d ( %d / %d ) / %d \n", etx, output_buffer_place, count_out_this_bit, num_replays, SAMPLE_RATE, BIT_RATE, DMA_WORDS );
PIN_OUT_CLEAR = _BV(2); //Turn GPIO2 light off.
output_buffer_place = 0;
count_out_this_bit = 0;
call_delay_us(5000000);
}
}
+153
View File
@@ -0,0 +1,153 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
// This file is for generated a 310MHz CW Tone that lines up with itself so
// It can be repeated many times in-phase.
#include <stdint.h>
#include <stdio.h>
#include <math.h>
const double rf_frequency = 310;
//const double spread = 0.000001;
const double spread = 0.0;
#define SPI_DIV 1
#if MAIN_MHZ == 80
const double sample_rate = 1040.0/13.0/SPI_DIV;
#elif MAIN_MHZ == 115
const double sample_rate = 1040.0/9.0/SPI_DIV;
#elif MAIN_MHZ == 52
const double sample_rate = 1040.0/20.0/SPI_DIV;
#elif MAIN_MHZ == 173
const double sample_rate = 1040.0/6.0/SPI_DIV;
#else
#error Unknown Clock Rate
#endif
#define MAX_WORDS_DMA 511
#define NUMBITS 32
int main()
{
int i;
FILE * fcba = fopen( "samples.h", "w" );
double phase = 0;
uint32_t samplect = 0;
uint32_t words = 0;
uint32_t sample_word = 0;
int32_t best_words = -1;
double best_phase = 1000;
double current_frequency = rf_frequency;
for( i = 0; i < MAX_WORDS_DMA*NUMBITS; i++ )
{
phase += 3.1415926 * 2 * current_frequency / sample_rate;
current_frequency += spread;
while( phase > 3.1415926 * 2.0 ) phase -= 3.1415926 * 2.0;
samplect++;
if( samplect == NUMBITS )
{
words++;
printf( "Phase Delta at %d: %f\n", words, phase );
if( words > 64 ) // Minimum number of words
{
double phasediff = ( phase > 3.1415926 ) ? (3.1415926*2.0 - phase) : phase;
if( phasediff < best_phase )
{
best_phase = phasediff;
best_words = words;
}
}
samplect = 0;
}
}
uint32_t num_words = best_words;
printf( "Selected %d words\n", num_words );
samplect = 0;
words = 0;
sample_word = 0;
phase = 0;
current_frequency = rf_frequency;
fprintf( fcba, "uint32_t samples[%d] = { \n\t", num_words );
for( i = 0; i < num_words*NUMBITS; i++ )
{
phase += 3.1415926 * 2 * current_frequency / sample_rate;
while( phase > 3.1415926 * 2.0 ) phase -= 3.1415926 * 2.0;
current_frequency += spread;
// This is the magic line - this is what decides if you are emitting a `1` bit or a `0` bit.
int bit = sin( phase ) > 0.0; // HINT: if you want to mix multiple signals, add a DC offset here.
sample_word |= !!bit;
samplect++;
if( samplect == NUMBITS )
{
#ifdef USE_16_BITS
fprintf( fcba, "0x%04x,%s", sample_word, ((words & 0xf) != 0xf)?" ":"\n\t" );
#else
fprintf( fcba, "0x%08x,%s", sample_word, ((words & 0xf) != 0xf)?" ":"\n\t" );
#endif
//fwrite( &sample_word, 1, NUMBITS/8, fd );
words++;
sample_word = 0;
samplect = 0;
}
sample_word <<= 1;
}
fprintf( fcba, "};\n" );
fprintf( fcba, "#define DMA_WORDS %d\n", num_words );
fprintf( fcba, "#define SPI_DIV %d\n", SPI_DIV );
fprintf( fcba, "#define NUMBITS %d\n", NUMBITS );
fprintf( fcba, "#define SAMPLE_RATE %d\n", (int)(sample_rate*1000000) );
}
+1
Submodule esp8266/nosdk8266 added at c1628f15ee
+626
View File
@@ -0,0 +1,626 @@
// NOTE: https://github.com/cnlohr/lorasoft/lib/LoRa-SDR-Code.h - IS UPSTREAM VERSION
/* This code is lifted from LoRa-SDR. I found this copyright at the top
of one of their files:
// Copyright (c) 2016-2016 Lime Microsystems
// Copyright (c) 2016-2016 Arne Hennig
// SPDX-License-Identifier: BSL-1.0
// from https://github.com/myriadrf/LoRa-SDR/blob/master/LoRaEncoder.cpp
Hopefully that covers some of the other stuff.
For any portions of this file not covered under the above licensing terms, it
they shall be governed by the MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _LORAENCODER_H
#define _LORAENCODER_H
// From LoRaCodes.hpp
#include <string.h>
/***********************************************************************
* Defines
**********************************************************************/
#define HEADER_RDD 4
#define N_HEADER_SYMBOLS (HEADER_RDD + 4)
/***********************************************************************
* Round functions
**********************************************************************/
static unsigned roundUp(unsigned num, unsigned factor)
{
return ((num + factor - 1) / factor) * factor;
}
/***********************************************************************
* Simple 8-bit checksum routine
**********************************************************************/
static uint8_t checksum8(const uint8_t *p, const size_t len)
{
uint8_t acc = 0;
for (size_t i = 0; i < len; i++)
{
acc = (acc >> 1) + ((acc & 0x1) << 7); //rotate
acc += p[i]; //add
}
return acc;
}
static uint8_t headerChecksum(const uint8_t *h) {
int a0 = (h[0] >> 4) & 0x1;
int a1 = (h[0] >> 5) & 0x1;
int a2 = (h[0] >> 6) & 0x1;
int a3 = (h[0] >> 7) & 0x1;
int b0 = (h[0] >> 0) & 0x1;
int b1 = (h[0] >> 1) & 0x1;
int b2 = (h[0] >> 2) & 0x1;
int b3 = (h[0] >> 3) & 0x1;
int c0 = (h[1] >> 0) & 0x1;
int c1 = (h[1] >> 1) & 0x1;
int c2 = (h[1] >> 2) & 0x1;
int c3 = (h[1] >> 3) & 0x1;
uint8_t res;
res = (a0 ^ a1 ^ a2 ^ a3) << 4;
res |= (a3 ^ b1 ^ b2 ^ b3 ^ c0) << 3;
res |= (a2 ^ b0 ^ b3 ^ c1 ^ c3) << 2;
res |= (a1 ^ b0 ^ b2 ^ c0 ^ c1 ^ c2) << 1;
res |= a0 ^ b1 ^ c0 ^ c1 ^ c2 ^ c3;
return res;
}
static uint16_t crc16sx(uint16_t crc, const uint16_t poly) {
for (int i = 0; i < 8; i++) {
if (crc & 0x8000) {
crc = (crc << 1) ^ poly;
}
else {
crc <<= 1;
}
}
return crc;
}
static uint8_t xsum8(uint8_t t) {
t ^= t >> 4;
t ^= t >> 2;
t ^= t >> 1;
return (t & 1);
}
/***********************************************************************
* CRC reverse engineered from Sx1272 data stream.
* Modified CCITT crc with masking of the output with an 8bit lfsr
**********************************************************************/
static uint16_t sx1272DataChecksum(const uint8_t *data, int length) {
uint16_t res = 0;
uint8_t v = 0xff;
uint16_t crc = 0;
for (int i = 0; i < length; i++) {
crc = crc16sx(res, 0x1021);
v = xsum8(v & 0xB8) | (v << 1);
res = crc ^ data[i];
}
res ^= v;
v = xsum8(v & 0xB8) | (v << 1);
res ^= v << 8;
return res;
}
/***********************************************************************
* http://www.semtech.com/images/datasheet/AN1200.18_AG.pdf
**********************************************************************/
static void SX1232RadioComputeWhitening( uint8_t *buffer, uint16_t bufferSize )
{
uint8_t WhiteningKeyMSB; // Global variable so the value is kept after starting the
uint8_t WhiteningKeyLSB; // de-whitening process
WhiteningKeyMSB = 0x01; // Init value for the LFSR, these values should be initialize only
WhiteningKeyLSB = 0xFF; // at the start of a whitening or a de-whitening process
// *buffer is a char pointer indicating the data to be whiten / de-whiten
// buffersize is the number of char to be whiten / de-whiten
// >> The whitened / de-whitened data are directly placed into the pointer
uint8_t i = 0;
uint16_t j = 0;
uint8_t WhiteningKeyMSBPrevious = 0; // 9th bit of the LFSR
for( j = 0; j < bufferSize; j++ ) // byte counter
{
buffer[j] ^= WhiteningKeyLSB; // XOR between the data and the whitening key
for( i = 0; i < 8; i++ ) // 8-bit shift between each byte
{
WhiteningKeyMSBPrevious = WhiteningKeyMSB;
WhiteningKeyMSB = ( WhiteningKeyLSB & 0x01 ) ^ ( ( WhiteningKeyLSB >> 5 ) & 0x01 );
WhiteningKeyLSB= ( ( WhiteningKeyLSB >> 1 ) & 0xFF ) | ( ( WhiteningKeyMSBPrevious << 7 ) & 0x80 );
}
}
}
/***********************************************************************
* Whitening generator reverse engineered from Sx1272 data stream.
* Each bit of a codeword is combined with the output from a different position in the whitening sequence.
**********************************************************************/
static void Sx1272ComputeWhitening(uint8_t *buffer, uint16_t bufferSize, const int bitOfs, const int RDD) {
static const int ofs0[8] = {6,4,2,0,-112,-114,-302,-34 }; // offset into sequence for each bit
static const int ofs1[5] = {6,4,2,0,-360 }; // different offsets used for single parity mode (1 == RDD)
static const int whiten_len = 510; // length of whitening sequence
static const uint64_t whiten_seq[8] = { // whitening sequence
0x0102291EA751AAFFL,0xD24B050A8D643A17L,0x5B279B671120B8F4L,0x032B37B9F6FB55A2L,
0x994E0F87E95E2D16L,0x7CBCFC7631984C26L,0x281C8E4F0DAEF7F9L,0x1741886EB7733B15L
};
const int *ofs = (1 == RDD) ? ofs1 : ofs0;
int i, j;
for (j = 0; j < bufferSize; j++) {
uint8_t x = 0;
for (i = 0; i < 4 + RDD; i++) {
int t = (ofs[i] + j + bitOfs + whiten_len) % whiten_len;
if (whiten_seq[t >> 6] & ((uint64_t)1 << (t & 0x3F))) {
x |= 1 << i;
}
}
buffer[j] ^= x;
}
}
/***********************************************************************
* Whitening generator reverse engineered from Sx1272 data stream.
* Same as above but using the actual interleaved LFSRs.
**********************************************************************/
static void Sx1272ComputeWhiteningLfsr(uint8_t *buffer, uint16_t bufferSize, const int bitOfs, const size_t RDD) {
static const uint64_t seed1[2] = {0x6572D100E85C2EFF,0xE85C2EFFFFFFFFFF}; // lfsr start values
static const uint64_t seed2[2] = {0x05121100F8ECFEEF,0xF8ECFEEFEFEFEFEF}; // lfsr start values for single parity mode (1 == RDD)
const uint8_t m = 0xff >> (4 - RDD);
uint64_t r[2] = {(1 == RDD)?seed2[0]:seed1[0],(1 == RDD)?seed2[1]:seed1[1]};
int i,j;
for (i = 0; i < bitOfs;i++){
r[i & 1] = (r[i & 1] >> 8) | (((r[i & 1] >> 32) ^ (r[i & 1] >> 24) ^ (r[i & 1] >> 16) ^ r[i & 1]) << 56); // poly: 0x1D
}
for (j = 0; j < bufferSize; j++,i++) {
buffer[j] ^= r[i & 1] & m;
r[i & 1] = (r[i & 1] >> 8) | (((r[i & 1] >> 32) ^ (r[i & 1] >> 24) ^ (r[i & 1] >> 16) ^ r[i & 1]) << 56);
}
}
/***********************************************************************
* https://en.wikipedia.org/wiki/Gray_code
**********************************************************************/
/*
* This function converts an unsigned binary
* number to reflected binary Gray code.
*
* The operator >> is shift right. The operator ^ is exclusive or.
*/
static unsigned short binaryToGray16(unsigned short num)
{
return num ^ (num >> 1);
}
/*
* A more efficient version, for Gray codes of 16 or fewer bits.
*/
static unsigned short grayToBinary16(unsigned short num)
{
num = num ^ (num >> 8);
num = num ^ (num >> 4);
num = num ^ (num >> 2);
num = num ^ (num >> 1);
return num;
}
/***********************************************************************
* Encode a 4 bit word into a 8 bits with parity
* Non standard version used in sx1272.
* https://en.wikipedia.org/wiki/Hamming_code
**********************************************************************/
static unsigned char encodeHamming84sx(const unsigned char x)
{
int d0 = (x >> 0) & 0x1;
int d1 = (x >> 1) & 0x1;
int d2 = (x >> 2) & 0x1;
int d3 = (x >> 3) & 0x1;
int b = x & 0xf;
b |= (d0 ^ d1 ^ d2) << 4;
b |= (d1 ^ d2 ^ d3) << 5;
b |= (d0 ^ d1 ^ d3) << 6;
b |= (d0 ^ d2 ^ d3) << 7;
return b;
}
/***********************************************************************
* Decode 8 bits into a 4 bit word with single bit correction.
* Non standard version used in sx1272.
* Set error to true when a parity error was detected
* Set bad to true when the result could not be corrected
**********************************************************************/
static unsigned char decodeHamming84sx(const unsigned char b, int * error, int * bad)
{
int b0 = (b >> 0) & 0x1;
int b1 = (b >> 1) & 0x1;
int b2 = (b >> 2) & 0x1;
int b3 = (b >> 3) & 0x1;
int b4 = (b >> 4) & 0x1;
int b5 = (b >> 5) & 0x1;
int b6 = (b >> 6) & 0x1;
int b7 = (b >> 7) & 0x1;
int p0 = (b0 ^ b1 ^ b2 ^ b4);
int p1 = (b1 ^ b2 ^ b3 ^ b5);
int p2 = (b0 ^ b1 ^ b3 ^ b6);
int p3 = (b0 ^ b2 ^ b3 ^ b7);
int parity = (p0 << 0) | (p1 << 1) | (p2 << 2) | (p3 << 3);
if (parity != 0) *error = 1;
switch (parity & 0xf)
{
case 0xD: return (b ^ 1) & 0xf;
case 0x7: return (b ^ 2) & 0xf;
case 0xB: return (b ^ 4) & 0xf;
case 0xE: return (b ^ 8) & 0xf;
case 0x0:
case 0x1:
case 0x2:
case 0x4:
case 0x8: return b & 0xf;
default: *bad = 1; return b & 0xf;
}
}
/***********************************************************************
* Encode a 4 bit word into a 7 bits with parity.
* Non standard version used in sx1272.
**********************************************************************/
static unsigned char encodeHamming74sx(const unsigned char x)
{
int d0 = (x >> 0) & 0x1;
int d1 = (x >> 1) & 0x1;
int d2 = (x >> 2) & 0x1;
int d3 = (x >> 3) & 0x1;
unsigned char b = x & 0xf;
b |= (d0 ^ d1 ^ d2) << 4;
b |= (d1 ^ d2 ^ d3) << 5;
b |= (d0 ^ d1 ^ d3) << 6;
return b;
}
/***********************************************************************
* Decode 7 bits into a 4 bit word with single bit correction.
* Non standard version used in sx1272.
* Set error to true when a parity error was detected
**********************************************************************/
static unsigned char decodeHamming74sx(const unsigned char b, int * error)
{
int b0 = (b >> 0) & 0x1;
int b1 = (b >> 1) & 0x1;
int b2 = (b >> 2) & 0x1;
int b3 = (b >> 3) & 0x1;
int b4 = (b >> 4) & 0x1;
int b5 = (b >> 5) & 0x1;
int b6 = (b >> 6) & 0x1;
int p0 = (b0 ^ b1 ^ b2 ^ b4);
int p1 = (b1 ^ b2 ^ b3 ^ b5);
int p2 = (b0 ^ b1 ^ b3 ^ b6);
int parity = (p0 << 0) | (p1 << 1) | (p2 << 2);
if (parity != 0) *error = 1;
switch (parity)
{
case 0x5: return (b ^ 1) & 0xf;
case 0x7: return (b ^ 2) & 0xf;
case 0x3: return (b ^ 4) & 0xf;
case 0x6: return (b ^ 8) & 0xf;
case 0x0:
case 0x1:
case 0x2:
case 0x4: return b & 0xF;
}
return b & 0xf;
}
/***********************************************************************
* Check parity for 5/4 code.
* return true if parity is valid.
**********************************************************************/
static unsigned char checkParity54(const unsigned char b, int *error) {
int x = b ^ (b >> 2);
x = x ^ (x >> 1) ^ (b >> 4);
if (x & 1) *error = 1;
return b & 0xf;
}
static unsigned char encodeParity54(const unsigned char b) {
int x = b ^ (b >> 2);
x = x ^ (x >> 1);
return (b & 0xf) | ((x << 4) & 0x10);
}
/***********************************************************************
* Check parity for 6/4 code.
* return true if parity is valid.
**********************************************************************/
static unsigned char checkParity64(const unsigned char b, int *error) {
int x = b ^ (b >> 1) ^ (b >> 2);
int y = x ^ b ^ (b >> 3);
x ^= b >> 4;
y ^= b >> 5;
if ((x | y) & 1) *error = 1;
return b & 0xf;
}
static unsigned char encodeParity64(const unsigned char b) {
int x = b ^ (b >> 1) ^ (b >> 2);
int y = x ^ b ^ (b >> 3);
return ((x & 1) << 4) | ((y & 1) << 5) | (b & 0xf);
}
/***********************************************************************
* Diagonal interleaver + deinterleaver
**********************************************************************/
static void diagonalInterleaveSx(const uint8_t *codewords, const size_t numCodewords, uint16_t *symbols, const size_t PPM, const size_t RDD){
for (size_t x = 0; x < numCodewords / PPM; x++) {
const size_t cwOff = x*PPM;
const size_t symOff = x*(4 + RDD);
for (size_t k = 0; k < 4 + RDD; k++){
uint16_t s = symbols[symOff + k];
for (size_t m = 0; m < PPM; m++){
const size_t i = (m + k + PPM) % PPM;
const int bit = (codewords[cwOff + i] >> k) & 0x1;
s |= (bit << m);
}
symbols[symOff + k] = s;
}
}
}
static void diagonalDeterleaveSx(const uint16_t *symbols, const size_t numSymbols, uint8_t *codewords, const size_t PPM, const size_t RDD)
{
for (size_t x = 0; x < numSymbols / (4 + RDD); x++)
{
const size_t cwOff = x*PPM;
const size_t symOff = x*(4 + RDD);
for (size_t k = 0; k < 4 + RDD; k++)
{
for (size_t m = 0; m < PPM; m++)
{
const size_t i = (m + k) % PPM;
const int bit = (symbols[symOff + k] >> m) & 0x1;
codewords[cwOff + i] |= (bit << k);
}
}
}
}
static void diagonalDeterleaveSx2(const uint16_t *symbols, const size_t numSymbols, uint8_t *codewords, const size_t PPM, const size_t RDD)
{
size_t nb = RDD + 4;
for (size_t x = 0; x < numSymbols / nb; x++) {
const size_t cwOff = x*PPM;
const size_t symOff = x*nb;
for (size_t m = 0; m < PPM; m++) {
size_t i = m;
int sym = symbols[symOff + m];
for (size_t k = 0; k < PPM; k++, sym >>= 1) {
codewords[cwOff + i] |= (sym & 1) << m;
if (++i == PPM) i = 0;
}
}
}
}
static void encodeFec(uint8_t * codewords, const size_t RDD, size_t * cOfs, size_t * dOfs, const uint8_t *bytes, const size_t count)
{
if (RDD == 0) for (size_t i = 0; i < count; i++, (*dOfs)++) {
if ((*dOfs) & 1)
codewords[(*cOfs)++] = bytes[(*dOfs) >> 1] >> 4;
else
codewords[(*cOfs)++] = bytes[(*dOfs) >> 1] & 0xf;
} else if (RDD == 1) for (size_t i = 0; i < count; i++, (*dOfs)++) {
if ((*dOfs) & 1)
codewords[(*cOfs)++] = encodeParity54(bytes[(*dOfs) >> 1] >> 4);
else
codewords[(*cOfs)++] = encodeParity54(bytes[(*dOfs) >> 1] & 0xf);
} else if (RDD == 2) for (size_t i = 0; i < count; i++, (*dOfs)++) {
if ((*dOfs) & 1)
codewords[(*cOfs)++] = encodeParity64(bytes[(*dOfs) >> 1] >> 4);
else
codewords[(*cOfs)++] = encodeParity64(bytes[(*dOfs) >> 1] & 0xf);
} else if (RDD == 3) for (size_t i = 0; i < count; i++, (*dOfs)++) {
if ((*dOfs) & 1)
codewords[(*cOfs)++] = encodeHamming74sx(bytes[(*dOfs) >> 1] >> 4);
else
codewords[(*cOfs)++] = encodeHamming74sx(bytes[(*dOfs) >> 1] & 0xf);
} else if (RDD == 4) for (size_t i = 0; i < count; i++, (*dOfs)++) {
if ((*dOfs) & 1)
codewords[(*cOfs)++] = encodeHamming84sx(bytes[(*dOfs) >> 1] >> 4);
else
codewords[(*cOfs)++] = encodeHamming84sx(bytes[(*dOfs) >> 1] & 0xf);
}
}
// This function has been HEAVILY modified from the original.
// Note that there are references to SF6, 11 and 12, but they do not currently work.
static int CreateMessageFromPayload( uint16_t * symbols, int * symbol_out_count, int max_symbols, int _sf, int _rdd, uint8_t * payload_plus_two_extra_crc_bytes, int payload_length )
{
//static int uctr = 0;
int _whitening = 1; // Enable whitening
int _crc = 1; // Enable CRC.
size_t PPM = _sf;
int _explicit = 1;
// TODO: https://dl.acm.org/doi/fullHtml/10.1145/3546869#sec-9 -- what about the corner cases for SF6, etc.
// SF6 does NOT WORK
int nHeaderCodewords = 0;
if( _sf == 6 ) nHeaderCodewords = 6;
if( _sf == 7 ) nHeaderCodewords = 5; // CORRECT VALIDATED
if( _sf == 8 ) nHeaderCodewords = 6; // CORRECT VALIDATED
if( _sf == 9 ) nHeaderCodewords = 7; // CORRECT VALIDATED
if( _sf == 10 ) nHeaderCodewords = 8; // CORRECT VALIDATED
if( _sf == 11 ) nHeaderCodewords = 9; // ???? Probably Wrong
if( _sf == 12 ) nHeaderCodewords = 10; // ???? Probably Wrong
int header_ppm_shift_up_by = 2;
// SF6 still isn't working.
// header does not reduce SF on SF <= 6 https://github.com/tapparelj/gr-lora_sdr/compare/master...feature/sf5_6_sx126x#diff-1821161335c7f28236eb88e3b8a3c84cce6997861cd847e87fbc9e270d338a37R68
// Indirectly, to note, for SF > 6, the header is implicitly LDRO (the bottom 2 bits are ignored)
if( _sf < 7 ) header_ppm_shift_up_by = 0;
int extra_codewords_due_to_header_padding = ( _sf == 7 || _sf == 6 /* CHECKME */ ) ? 1 : 0;
int header_ppm = ( _sf - header_ppm_shift_up_by );
int data_ppm = _sf;
// XXX TODO: Investigate: I thought SF12 had an LDRO mode which made the PPM only 10.
// TODO: Compare to https://github.com/jkadbear/LoRaPHY/blob/master/LoRaPHY.m
const size_t numCodewords = roundUp( ( payload_length + 2 * _crc ) * 2 + (_explicit ? nHeaderCodewords:0) + extra_codewords_due_to_header_padding, PPM);
const size_t numSymbols = N_HEADER_SYMBOLS + (numCodewords / PPM - 1) * (4 + _rdd); // header is always coded with 8/4
uint8_t codewords[numCodewords];
memset( codewords, 0, sizeof( codewords ) );
if( numSymbols >= max_symbols )
{
//uprintf( "Error: Too many symbols to fit (%d/%d)\n", numSymbols, max_symbols );
return -1;
}
memset( symbols, 0, numSymbols * sizeof( symbols[0] ) );
size_t cOfs = 0;
size_t dOfs = 0;
//std::vector<uint8_t> codewords(numCodewords);
if (_crc)
{
uint16_t crc = sx1272DataChecksum( payload_plus_two_extra_crc_bytes, payload_length );
payload_plus_two_extra_crc_bytes[payload_length] = crc & 0xff;
payload_plus_two_extra_crc_bytes[payload_length+1] = (crc >> 8) & 0xff;
}
// Why does this disagree? https://www.Carloalbertoboano.Com/Documents/Yang22emu.Pdf
if (_explicit) {
uint8_t hdr[3];
hdr[0] = payload_length;
hdr[1] = (_crc ? 1 : 0) | (_rdd << 1);
//static int k;
hdr[2] =
//k++;
headerChecksum(hdr);
codewords[cOfs++] = encodeHamming84sx(hdr[0] >> 4);
codewords[cOfs++] = encodeHamming84sx(hdr[0] & 0xf); // length
codewords[cOfs++] = encodeHamming84sx(hdr[1] & 0xf); // crc / fec info
codewords[cOfs++] = encodeHamming84sx(hdr[2] >> 4); // checksum
codewords[cOfs++] = encodeHamming84sx(hdr[2] & 0xf);
if( extra_codewords_due_to_header_padding > 1 )
{
codewords[cOfs++] = 0;
}
if( extra_codewords_due_to_header_padding > 2 )
{
codewords[cOfs++] = 0;
}
if( extra_codewords_due_to_header_padding > 3 )
{
codewords[cOfs++] = 0;
}
}
size_t cOfs1 = cOfs;
// Header is encoded at 8/4 (4)
encodeFec( codewords, 4, &cOfs, &dOfs, payload_plus_two_extra_crc_bytes, PPM - cOfs );
// Whitening for the data that lives inside the header block.
if( _whitening )
{
Sx1272ComputeWhitening(codewords + cOfs1, PPM - cOfs1, 0, HEADER_RDD);
}
if (numCodewords > PPM) {
size_t cOfs2 = cOfs;
encodeFec(codewords, _rdd, &cOfs, &dOfs, payload_plus_two_extra_crc_bytes, numCodewords-PPM);
if (_whitening) {
Sx1272ComputeWhitening(codewords + cOfs2, numCodewords - PPM, PPM - cOfs1, _rdd);
}
}
//interleave the codewords into symbols
int symbols_size = numSymbols;
// TRICKY: Header is forced to a particularly slow mode.
diagonalInterleaveSx(codewords, header_ppm, symbols, header_ppm, HEADER_RDD);
int i;
if (numCodewords > header_ppm) {
diagonalInterleaveSx(codewords + nHeaderCodewords, numCodewords-nHeaderCodewords, symbols+N_HEADER_SYMBOLS, data_ppm, _rdd);
}
//gray decode, when SF > PPM, pad out LSBs
uint16_t sym;
for( i = 0; i < symbols_size; i++ )
{
//int is_header = (i < 8);
sym = symbols[i];
sym = grayToBinary16(sym);
sym <<= (_sf - PPM);
symbols[i] = sym; // OR +1
}
// The first header symbols are all shifted by 2.
// XXX TODO Look here for SF6 stuff.
for( i = 0; i < N_HEADER_SYMBOLS; i++ )
{
symbols[i] <<= header_ppm_shift_up_by;
}
*symbol_out_count = symbols_size;
return 0;
}
#endif
+193
View File
@@ -0,0 +1,193 @@
/* Clean implementation of RFC4493 AES-CMAC in C, Public Domain by cnlohr
WARNING: This is not cryptographically hardened.
DO NOT USE THIS IN AN ENVIRONMENT WHERE SECURITY MATTERS
THIS IMPLEMENTATION IS HIGHLY SUCCEPTABLE TO TIMING ATTACKS
THIS IMPLEMENTATION DOES NOT USE HARDENED MEMORY.
You may license this code under the unlicense, MIT-x11 or NewBSD licenses.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
Assumes a tiny-AES-c implementation for AES_init_ctx and AES_ECB_encrypt.
#define TINY_AES_ECB 1
#include "tiny-AES-c.h"
*/
#include <string.h>
static void BlockLeftShift( uint8_t * blockout, const uint8_t * blockin )
{
int i;
int carry = 0;
for( i = 15; i >= 0; i-- )
{
uint8_t bi = blockin[i];
blockout[i] = (bi << 1) | carry;
carry = !!(bi & 0x80);
}
}
static void BlockXor( uint8_t * out, const uint8_t * in, const uint8_t * inb )
{
int i;
for( i = 0; i < 16; i++ )
out[i] = in[i] ^ inb[i];
}
static void AES_CMAC( const uint8_t *key, const uint8_t *input, int length,
uint8_t *mac )
{
struct AES_ctx K;
AES_init_ctx( &K, key);
int i;
// Based on Figure 2.2, GenerateSubKey
/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Algorithm Generate_Subkey +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ Input : K (128-bit key) +
+ Output : K1 (128-bit first subkey) +
+ K2 (128-bit second subkey) +
+-------------------------------------------------------------------+
+ +
+ Constants: const_Zero is 0x00000000000000000000000000000000 +
+ const_Rb is 0x00000000000000000000000000000087 +
+ Variables: L for output of AES-128 applied to 0^128 +
+ +
+ Step 1. L := AES-128(K, const_Zero); +
+ Step 2. if MSB(L) is equal to 0 +
+ then K1 := L << 1; +
+ else K1 := (L << 1) XOR const_Rb; +
+ Step 3. if MSB(K1) is equal to 0 +
+ then K2 := K1 << 1; +
+ else K2 := (K1 << 1) XOR const_Rb; +
+ Step 4. return K1, K2; +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
const uint8_t const_Rb[AES_BLOCKLEN] = { 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87 };
uint8_t K1[AES_BLOCKLEN];
uint8_t K2[AES_BLOCKLEN];
{
uint8_t L[AES_BLOCKLEN] = { 0 };
AES_ECB_encrypt( &K, L );
BlockLeftShift( K1, L );
if( L[0] & 0x80 )
BlockXor( K1, K1, const_Rb );
BlockLeftShift( K2, K1 );
if( K1[0] & 0x80 )
BlockXor( K2, K2, const_Rb );
}
// Based on figure 2.3, AES-CMAC
/*
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Algorithm AES-CMAC +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ Input : K ( 128-bit key ) +
+ : M ( message to be authenticated ) +
+ : len ( length of the message in octets ) +
+ Output : T ( message authentication code ) +
+ +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Constants: const_Zero is 0x00000000000000000000000000000000 +
+ const_Bsize is 16 +
+ +
+ Variables: K1, K2 for 128-bit subkeys +
+ M_i is the i-th block (i=1..ceil(len/const_Bsize)) +
+ M_last is the last block xor-ed with K1 or K2 +
+ n for number of blocks to be processed +
+ r for number of octets of last block +
+ flag for denoting if last block is complete or not +
+ +
+ Step 1. (K1,K2) := Generate_Subkey(K); +
+ Step 2. n := ceil(len/const_Bsize); +
+ Step 3. if n = 0 +
+ then +
+ n := 1; +
+ flag := false; +
+ else +
+ if len mod const_Bsize is 0 +
+ then flag := true; +
+ else flag := false; +
+ +
+ Step 4. if flag is true +
+ then M_last := M_n XOR K1; +
+ else M_last := padding(M_n) XOR K2; +
+ Step 5. X := const_Zero; +
+ Step 6. for i := 1 to n-1 do +
+ begin +
+ Y := X XOR M_i; +
+ X := AES-128(K,Y); +
+ end +
+ Y := M_last XOR X; +
+ T := AES-128(K,Y); +
+ Step 7. return T; +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
uint8_t M_last[AES_BLOCKLEN];
int remainder = length % AES_BLOCKLEN;
int flag = !(remainder); // If evenly divisible, set to true.
int n = ( length + AES_BLOCKLEN - 1 ) / AES_BLOCKLEN;
if( n == 0 )
{
n = 1;
flag = 0; // Unless 0 size payload.
}
const uint8_t * in_last = &input[(n-1)*AES_BLOCKLEN];
if( flag )
{
BlockXor( M_last, in_last, K1 );
}
else
{
memcpy( M_last, in_last, remainder );
memset( M_last + remainder + 1, 0, AES_BLOCKLEN - remainder - 1 );
M_last[remainder] = 0x80;
BlockXor( M_last, M_last, K2 );
}
uint8_t X[AES_BLOCKLEN] = { 0 };
for( i = 0; i < n-1; i++ )
{
BlockXor( X, X, &input[i*AES_BLOCKLEN] );
AES_ECB_encrypt( &K, X );
}
BlockXor( mac, M_last, X );
AES_ECB_encrypt( &K, mac );
}
+160
View File
@@ -0,0 +1,160 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _LORAWAN_SIMPLE_H
#define _LORAWAN_SIMPLE_H
#include <string.h>
#include <unistd.h>
// Configuration needed for aes-cbc-cmac.h
#define TINY_AES_C_IMPLEMENTATION
#define TINY_AES_ECB 1
#include "tiny-AES-c.h"
#include "aes-cbc-cmac.h"
static int GenerateLoRaWANPacket( uint8_t * raw_payload_with_b0, const uint8_t * inner_payload_raw, int inner_payload_len, const uint8_t * payload_key, const uint8_t * network_skey, const uint8_t * devaddress, int frame )
{
int i;
uint8_t * raw_payload = raw_payload_with_b0 + 16;
uint8_t * pl = raw_payload;
struct AES_ctx payload_key_ctx;
AES_init_ctx( &payload_key_ctx, payload_key );
// MAC header
// 010 Unconfirmed data uplink.
// 000 RFU
// 00 Major Version
*(pl++) = ( 0b01000000 );
// Frame header
// Device address (4 bytes)
memcpy( pl, devaddress, 4 );
pl += 4;
// Frame Control
// MSB
// 0 = ADR (cannot do rate adaptation)
// 0 = Not confirming ADR.
// 0 = ACK
// 0 = Class B (we are not a class B device because we can't schedule downlinks)
// 0000 = fopts length (no fopts)
// LSB
*(pl++) = 0b00000000;
// Frame Count
*(pl++) = frame;
*(pl++) = frame>>8;
// Frame Port
*(pl++) = 1; // Port (1-223 are application specific)
int padded_pad_length = ( inner_payload_len + AES_BLOCKLEN - 1 ) & (~(AES_BLOCKLEN-1));
int nr_blocks = padded_pad_length / AES_BLOCKLEN;
uint8_t pad[padded_pad_length];
for( i = 0; i < nr_blocks; i++ )
{
// Add CMAC. Construct b0
uint8_t * Ai = &pad[i*AES_BLOCKLEN];
*(Ai++) = 0x01;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0;
*(Ai++) = 0; // Uplink = 0
*(Ai++) = devaddress[0];
*(Ai++) = devaddress[1];
*(Ai++) = devaddress[2];
*(Ai++) = devaddress[3];
*(Ai++) = (frame>>0) & 0xff;
*(Ai++) = (frame>>8) & 0xff;
*(Ai++) = (frame>>16) & 0xff;
*(Ai++) = (frame>>24) & 0xff;
*(Ai++) = 0;
*(Ai++) = i+1;
}
for( i = 0; i < nr_blocks; i++ )
{
AES_ECB_encrypt( &payload_key_ctx, &pad[i*AES_BLOCKLEN] );
}
// uint8_t * start_of_payload = pl;
for( i = 0; i < inner_payload_len; i++ )
{
*(pl++) = pad[i] ^ inner_payload_raw[i];
}
int to_cmac_size = pl - raw_payload_with_b0;
int length_of_frame_without_b0 = pl - raw_payload;
uint8_t * b0 = raw_payload_with_b0;
*(b0++) = 0x49;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = 0x00;
*(b0++) = devaddress[0];
*(b0++) = devaddress[1];
*(b0++) = devaddress[2];
*(b0++) = devaddress[3];
*(b0++) = (frame>>0) & 0xff;
*(b0++) = (frame>>8) & 0xff;
*(b0++) = (frame>>16) & 0xff;
*(b0++) = (frame>>24) & 0xff;
*(b0++) = 0x00;
*(b0++) = length_of_frame_without_b0;
uint8_t mac[AES_BLOCKLEN];
//printf( "To CMAC: %d\n", to_cmac_size );
//for ( i = 0; i < to_cmac_size; i++ )
//{
// printf( "%02x ", raw_payload_with_b0[i] );
//}
//printf( "\n" );
AES_CMAC( network_skey, raw_payload_with_b0, to_cmac_size, mac );
*(pl++) = mac[0];
*(pl++) = mac[1];
*(pl++) = mac[2];
*(pl++) = mac[3];
//printf( "MAC %02x%02x%02x%02x %02x%02x%02x%02x\n", mac[0], mac[1], mac[2], mac[3], mac[12], mac[13], mac[14], mac[15] );
return pl - raw_payload_with_b0 - 16;
}
#endif
+344
View File
@@ -0,0 +1,344 @@
/**
MIT-like-non-ai-license
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the two following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
In addition the following restrictions apply:
1. The Software and any modifications made to it may not be used for the
purpose of training or improving machine learning algorithms, including but not
limited to artificial intelligence, natural language processing, or data
mining. This condition applies to any derivatives, modifications, or updates
based on the Software code. Any usage of the Software in an AI-training dataset
is considered a breach of this License.
2. The Software may not be included in any dataset used for training or
improving machine learning algorithms, including but not limited to artificial
intelligence, natural language processing, or data mining.
3. Any person or organization found to be in violation of these restrictions
will be subject to legal action and may be held liable for any damages
resulting from such use.
If any term is unenforcable, other terms remain in-force.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// This file is code, but intended to be included into a .c file that
// does #define's for a specific fitness. Below is an example .c file
// that this could be included into to generate the bit table. It is
// from an ESP8266 example, but other specifics can be specified on a
// per-processor basis.
/*
const double center_frequency = 904.1;
const double bw = .125;
const uint32_t memory_offset = 0x20000;
#define SF_NUMBER 7
#if SF_NUMBER < 7
#warning SF6 still does not work :(
#endif
#define SPI_DIV 1
// Funny modes:
/// 80MHz, SF8, SPI_DIV 5 @903.9/904.1 produces hilarious mirror images around 904.0
#if MAIN_MHZ == 80
const double sample_rate = 1040.0/13.0/SPI_DIV;
#if ( SF_NUMBER > 8 )
#error Not enough ram for chirp table
#endif
#elif MAIN_MHZ == 115
const double sample_rate = 1040.0/9.0/SPI_DIV;
#if ( SF_NUMBER > 8 )
#error Not enough ram for chirp table
#endif
#elif MAIN_MHZ == 52
const double sample_rate = 1040.0/20.0/SPI_DIV;
#if ( SF_NUMBER > 9 )
#error Not enough ram for chirp table
#endif
#elif MAIN_MHZ == 173
const double sample_rate = 1040.0/6.0/SPI_DIV;
#if ( SF_NUMBER > 7 )
#error Not enough ram for chirp table
#endif
#else
#error Unknown Clock Rate
#endif
EXTRA OPTIONS
#define USE_16_BITS
*/
#include <stdio.h>
#include <stdint.h>
#include <math.h>
const double chirp_begin = center_frequency-bw/2;
const double chirp_end = center_frequency+bw/2;
#ifndef MEM_MAX_BYTES
#define MEM_MAX_BYTES 2000000
#endif
#ifndef SF_SYMBOL_TIME
#define SF_SYMBOL_TIME 0.000001
#endif
const double chirp_length_seconds = (8<<SF_NUMBER) * SF_SYMBOL_TIME;
const double sampletotal = ( chirp_length_seconds * sample_rate * 1000000 );
int words = 0;
int words_nominal = 0;
uint32_t bleedover = 0; // Will be tailored based on the needed buffers.
FILE * fcba;
FILE * fd;
FILE * fCBI;
#ifdef USE_16_BITS
#define NUMBITS 16
#else
#define NUMBITS 32
#endif
static uint32_t flipBits( uint32_t w, int len )
{
int i;
uint32_t ret = 0;
for( i = 0; i < len; i++ )
{
if( w & (1<<(len-1)) )
ret |= 1;
ret <<= 1;
w <<= 1;
}
return ret;
}
void GenChirp( double fStart, double fEnd )
{
uint32_t sample_word = 0;
int samplect = 0;
double phase = 0.0001;
int samples = 0;
int ic;
{
for( samples = 0; ; samples++ )
{
double placeInSamples = samples / sampletotal;
if( placeInSamples >= 1 )
{
// When going off the top frequency, start over at the bottom.
placeInSamples -= 1;
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// These are the key lines here. This is what actually decides the current
// frequency and figures out what the given bit in a position should be.
// I use 0.1 as an offset because it seems to have some beneficial behaviors
// but 0.0 also seems to work really well. You can artifically reduce the
// amplituide of the output signal by changing the offset in the comparison
// on the last line of this section.
double current_f = ( placeInSamples ) * ( fEnd - fStart ) + fStart;
phase += 3.1415926 * 2.0 * current_f / sample_rate;
int bit = sin( phase ) > 0.1; // HINT: if you want to mix multiple signals, add a DC offset here.
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
sample_word |= !!bit;
samplect++;
if( samplect == NUMBITS )
{
#ifdef USE_16_BITS
// sample_word = ((sample_word>>8)&0xff) | ((sample_word<<8)&0xff00);
// sample_word = flipBits( sample_word, 16 );
fprintf( fcba, "0x%04x,%c", sample_word, ((words & 0xf) != 0xf)?' ':'\n' );
#else
fprintf( fcba, "0x%08x,%c", sample_word, ((words & 0xf) != 0xf)?' ':'\n' );
#endif
fwrite( &sample_word, 1, NUMBITS/8, fd );
words++;
sample_word = 0;
samplect = 0;
if( samples < sampletotal )
words_nominal = words;
if( samples >= sampletotal + bleedover*32 )
{
break;
}
}
sample_word <<= 1;
}
}
}
#ifndef ATTRIBUTE_FOR_DATA
#define ATTRIBUTE_FOR_DATA
#endif
int gen_buffer_files()
{
fcba = fopen( "chirpbuff.h", "w" );
fd = fopen( "chirpbuff.dat", "w" );
fCBI = fopen( "chirpbuffinfo.h", "w" );
#ifdef USE_16_BITS
fprintf( fcba, "const uint16_t chirpbuff[] " ATTRIBUTE_FOR_DATA " = {\n" );
#else
fprintf( fcba, "const uint32_t chirpbuff[] " ATTRIBUTE_FOR_DATA " = {\n" );
#endif
int quarter_chirp_length_bits = (int)(sampletotal/4.0+0.5);
int factor = 0;
int is_perfect_divisor = 0;
int i;
// DMA size words, when multiplied out should be SMALLER than the
// overall length of our message, so that we can trim off the last few
// words sometimes, to average out to the right bitrate.
// So, we need to pick a number that when multiplied out, ends up
// slightly larger than IDEAL_CHIRP_LENGTH_BITS. But not too much so.
// First try to find a perfect divisor, if we can find a perfect divisor,
// then no weird fixup stuff needs to be done.
int seeking_perfect_divisor = 1;
bleedover = 0;
retry_without_seek:
printf( "Searching for a divisor for %d\n", quarter_chirp_length_bits );
for( i = 511; i > 100; i-- )
{
int nr_to_div = (quarter_chirp_length_bits / 32 + 32) / i;
int num_bits_default = i * 32 * nr_to_div;
int leftover = num_bits_default - quarter_chirp_length_bits;
//fprintf( stderr, "%d %d %d [%d] --> %d\n", i, nr_to_div, num_bits_default, quarter_chirp_length_bits, leftover );
if( leftover < 32*nr_to_div &&
( seeking_perfect_divisor ? (leftover == 0) : (leftover >= 0)) )
{
// Make sure we aren't more than 1 32-bit word too large
fprintf( stderr, "Found %s divisor: %d (%d - %d = %d)\n",
(leftover == 0)?"PERFECT" : "OK",
i,
num_bits_default,
quarter_chirp_length_bits,
num_bits_default - quarter_chirp_length_bits );
factor = i;
//XXX TODO: I think this bleedover calc is wrong. Should investigate?
bleedover = num_bits_default/32;
is_perfect_divisor = leftover == 0;
break;
}
}
if( seeking_perfect_divisor && !factor )
{
seeking_perfect_divisor = 0;
goto retry_without_seek;
}
if( !factor )
{
fprintf( stderr, "Error: No factor of %d found, you may have to do something clever in rf_data_gen.c\n", quarter_chirp_length_bits );
return -9;
}
// For a given word, it is shifted out MSB (Bit and byte) first
GenChirp( chirp_begin, chirp_end );
int sample_word_median = words;
int quarter_chirp_length = ((words_nominal+2)/4);
int reverse_start = words;
words = 0;
GenChirp( chirp_end, chirp_begin );
fprintf( fcba, "};\n" );
fclose( fcba );
fclose( fd );
#ifdef USE_16_BITS
int bytes_total = 2*(words+sample_word_median);
fprintf( stderr, "Wrote out %d uint16_t's.\n", words + sample_word_median );
#else
int bytes_total = 4*(words+sample_word_median);
fprintf( stderr, "Wrote out %d uint32_t's.\n", words + sample_word_median );
#endif
if( bytes_total > MEM_MAX_BYTES )
{
fprintf( stderr, "ERROR: Your table is too big (trying to write out %d bytes, please adjust SF/BW/clock rate settings)\n", bytes_total );
}
fprintf( fCBI, "#define CHIRPLENGTH_WORDS (%d)\n", words_nominal );
fprintf( fCBI, "#define MEMORY_START_OFFSET_BYTES (0x%08x)\n", memory_offset );
fprintf( fCBI, "#define REVERSE_START_OFFSET_BYTES (0x%08x)\n", memory_offset + reverse_start * NUMBITS/8 );
fprintf( fCBI, "#define QUARTER_CHIRP_LENGTH_WORDS (%d)\n", (int)(quarter_chirp_length) );
fprintf( fCBI, "#define IDEAL_QUARTER_CHIRP_LENGTH_BITS (%d)\n", quarter_chirp_length_bits );
fprintf( fCBI, "#define CHIRPLENGTH_WORDS_WITH_PADDING (%d)\n", sample_word_median );
fprintf( fCBI, "#define STRIPE_BLEEDOVER_WORDS (%d)\n", bleedover );
fprintf( fCBI, "#define TARGET_SAMPLE_COUNT_BITS (%d)\n", (int)sampletotal );
fprintf( fCBI, "#define DMA_SIZE_WORDS (%d)\n", factor );
fprintf( fCBI, "#define NUM_DMAS_PER_QUARTER_CHIRP (%d)\n", quarter_chirp_length/factor+((is_perfect_divisor)?0:1) );
fprintf( fCBI, "#define SF_NUMBER %d\n", SF_NUMBER );
fprintf( fCBI, "#define SPI_DIV %d\n", SPI_DIV );
if( factor & 1 )
fprintf( fCBI, "#define DMA_SIZE_WORDS_DIVISIBLE_BY_TWO 0\n" );
else
fprintf( fCBI, "#define DMA_SIZE_WORDS_DIVISIBLE_BY_TWO 1\n" );
if( factor & 3 )
fprintf( fCBI, "#define DMA_SIZE_WORDS_DIVISIBLE_BY_FOUR 0\n" );
else
fprintf( fCBI, "#define DMA_SIZE_WORDS_DIVISIBLE_BY_FOUR 1\n" );
#ifdef USE_EXTERNAL_CLOCK
fprintf( fCBI, "#define USE_EXTERNAL_CLOCK\n" );
#endif
if( is_perfect_divisor )
fprintf( fCBI, "#define FOUND_PERFECT_DIVISOR\n" );
fclose( fCBI );
}
+704
View File
@@ -0,0 +1,704 @@
/*
Header-only version of tiny-AES-c https://github.com/kokke/tiny-AES-c/blob/master/aes.c
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#ifndef _TINY_AES_C_H_
#define _TINY_AES_C_H_
#include <stdint.h>
#include <stddef.h>
// If you want to use this library:
// #define TINY_AES_C_IMPLEMENTATION
// #define the macros below to 1/0 to enable/disable the mode of operation.
//
// CBC enables AES encryption in CBC-mode of operation.
// CTR enables encryption in counter-mode.
// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
#ifndef TINY_AES_CBC
#define TINY_AES_CBC 1
#endif
#ifndef TINY_AES_ECB
#define TINY_AES_ECB 1
#endif
#ifndef TINY_AES_CTR
#define TINY_AES_CTR 1
#endif
#if !defined( AES128 ) && !defined( AES192 ) && !defined( AES256 )
#define AES128 1
//#define AES192 1
//#define AES256 1
#endif
#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
#if defined(AES256) && (AES256 == 1)
#define AES_KEYLEN 32
#define AES_keyExpSize 240
#elif defined(AES192) && (AES192 == 1)
#define AES_KEYLEN 24
#define AES_keyExpSize 208
#else
#define AES_KEYLEN 16 // Key length in bytes
#define AES_keyExpSize 176
#endif
struct AES_ctx
{
uint8_t RoundKey[AES_keyExpSize];
#if (defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)) || (defined(TINY_AES_CTR) && (TINY_AES_CTR == 1))
uint8_t Iv[AES_BLOCKLEN];
#endif
};
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
#if (defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)) || (defined(TINY_AES_CTR) && (TINY_AES_CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
#endif
#if defined(TINY_AES_ECB) && (TINY_AES_ECB == 1)
// buffer size is exactly AES_BLOCKLEN bytes;
// you need only AES_init_ctx as IV is not used in ECB
// NB: ECB is considered insecure for most uses
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
#endif // #if defined(TINY_AES_ECB) && (TINY_AES_ECB == !)
#if defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)
// buffer size MUST be mutile of AES_BLOCKLEN;
// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
// no IV should ever be reused with the same key
void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
#endif // #if defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)
#if defined(TINY_AES_CTR) && (TINY_AES_CTR == 1)
// Same function for encrypting as for decrypting.
// IV is incremented for every block, and used after encryption as XOR-compliment for output
// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
// no IV should ever be reused with the same key
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
#endif // #if defined(TINY_AES_CTR) && (TINY_AES_CTR == 1)
#ifdef TINY_AES_C_IMPLEMENTATION
/*
This is an implementation of the AES algorithm, specifically ECB, TINY_AES_CTR and CBC mode.
Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
The implementation is verified against the test vectors in:
National Institute of Standards and Technology Special Publication 800-38A 2001 ED
ECB-AES128
----------
plain-text:
6bc1bee22e409f96e93d7e117393172a
ae2d8a571e03ac9c9eb76fac45af8e51
30c81c46a35ce411e5fbc1191a0a52ef
f69f2445df4f9b17ad2b417be66c3710
key:
2b7e151628aed2a6abf7158809cf4f3c
resulting cipher
3ad77bb40d7a3660a89ecaf32466ef97
f5d3d58503b9699de785895a96fdbaaf
43b1cd7f598ece23881b00e3ed030688
7b0c785e27e8ad3f8223207104725dd4
NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
You should pad the end of the string with zeros if this is not the case.
For AES192/256 the key size is proportionally larger.
*/
/*****************************************************************************/
/* Includes: */
/*****************************************************************************/
#include <string.h> // CBC mode, for memset
/*****************************************************************************/
/* Defines: */
/*****************************************************************************/
// The number of columns comprising a state in AES. This is a constant in AES. Value=4
#define Nb 4
#if defined(AES256) && (AES256 == 1)
#define Nk 8
#define Nr 14
#elif defined(AES192) && (AES192 == 1)
#define Nk 6
#define Nr 12
#else
#define Nk 4 // The number of 32 bit words in a key.
#define Nr 10 // The number of rounds in AES Cipher.
#endif
// jcallan@github points out that declaring Multiply as a function
// reduces code size considerably with the Keil ARM compiler.
// See this link for more information: https://github.com/kokke/tiny-AES-C/pull/3
#ifndef MULTIPLY_AS_A_FUNCTION
#define MULTIPLY_AS_A_FUNCTION 0
#endif
/*****************************************************************************/
/* Private variables: */
/*****************************************************************************/
// state - array holding the intermediate results during decryption.
typedef uint8_t state_t[4][4];
// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM
// The numbers below can be computed dynamically trading ROM for RAM -
// This can be useful in (embedded) bootloader applications, where ROM is often limited.
static const uint8_t sbox[256] = {
//0 1 2 3 4 5 6 7 8 9 A B C D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
#if (defined(TINY_AES_CBC) && TINY_AES_CBC == 1) || (defined(TINY_AES_ECB) && TINY_AES_ECB == 1)
static const uint8_t rsbox[256] = {
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
#endif
// The round constant word array, Rcon[i], contains the values given by
// x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
static const uint8_t Rcon[11] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
/*
* Jordan Goulder points out in PR #12 (https://github.com/kokke/tiny-AES-C/pull/12),
* that you can remove most of the elements in the Rcon array, because they are unused.
*
* From Wikipedia's article on the Rijndael key schedule @ https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
*
* "Only the first some of these constants are actually used up to rcon[10] for AES-128 (as 11 round keys are needed),
* up to rcon[8] for AES-192, up to rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
*/
/*****************************************************************************/
/* Private functions: */
/*****************************************************************************/
/*
static uint8_t getSBoxValue(uint8_t num)
{
return sbox[num];
}
*/
#define getSBoxValue(num) (sbox[(num)])
// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states.
static void KeyExpansion(uint8_t* RoundKey, const uint8_t* Key)
{
unsigned i, j, k;
uint8_t tempa[4]; // Used for the column/row operations
// The first round key is the key itself.
for (i = 0; i < Nk; ++i)
{
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
}
// All other round keys are found from the previous round keys.
for (i = Nk; i < Nb * (Nr + 1); ++i)
{
{
k = (i - 1) * 4;
tempa[0]=RoundKey[k + 0];
tempa[1]=RoundKey[k + 1];
tempa[2]=RoundKey[k + 2];
tempa[3]=RoundKey[k + 3];
}
if (i % Nk == 0)
{
// This function shifts the 4 bytes in a word to the left once.
// [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
// Function RotWord()
{
const uint8_t u8tmp = tempa[0];
tempa[0] = tempa[1];
tempa[1] = tempa[2];
tempa[2] = tempa[3];
tempa[3] = u8tmp;
}
// SubWord() is a function that takes a four-byte input word and
// applies the S-box to each of the four bytes to produce an output word.
// Function Subword()
{
tempa[0] = getSBoxValue(tempa[0]);
tempa[1] = getSBoxValue(tempa[1]);
tempa[2] = getSBoxValue(tempa[2]);
tempa[3] = getSBoxValue(tempa[3]);
}
tempa[0] = tempa[0] ^ Rcon[i/Nk];
}
#if defined(AES256) && (AES256 == 1)
if (i % Nk == 4)
{
// Function Subword()
{
tempa[0] = getSBoxValue(tempa[0]);
tempa[1] = getSBoxValue(tempa[1]);
tempa[2] = getSBoxValue(tempa[2]);
tempa[3] = getSBoxValue(tempa[3]);
}
}
#endif
j = i * 4; k=(i - Nk) * 4;
RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
}
}
void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key)
{
KeyExpansion(ctx->RoundKey, key);
}
#if (defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)) || (defined(TINY_AES_CTR) && (TINY_AES_CTR == 1))
void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv)
{
KeyExpansion(ctx->RoundKey, key);
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
}
void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv)
{
memcpy (ctx->Iv, iv, AES_BLOCKLEN);
}
#endif
// This function adds the round key to state.
// The round key is added to the state by an XOR function.
static void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey)
{
uint8_t i,j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
}
}
}
// The SubBytes Function Substitutes the values in the
// state matrix with values in an S-box.
static void SubBytes(state_t* state)
{
uint8_t i, j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[j][i] = getSBoxValue((*state)[j][i]);
}
}
}
// The ShiftRows() function shifts the rows in the state to the left.
// Each row is shifted with different offset.
// Offset = Row number. So the first row is not shifted.
static void ShiftRows(state_t* state)
{
uint8_t temp;
// Rotate first row 1 columns to left
temp = (*state)[0][1];
(*state)[0][1] = (*state)[1][1];
(*state)[1][1] = (*state)[2][1];
(*state)[2][1] = (*state)[3][1];
(*state)[3][1] = temp;
// Rotate second row 2 columns to left
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp;
// Rotate third row 3 columns to left
temp = (*state)[0][3];
(*state)[0][3] = (*state)[3][3];
(*state)[3][3] = (*state)[2][3];
(*state)[2][3] = (*state)[1][3];
(*state)[1][3] = temp;
}
static uint8_t xtime(uint8_t x)
{
return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
}
// MixColumns function mixes the columns of the state matrix
static void MixColumns(state_t* state)
{
uint8_t i;
uint8_t Tmp, Tm, t;
for (i = 0; i < 4; ++i)
{
t = (*state)[i][0];
Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
}
}
// Multiply is used to multiply numbers in the field GF(2^8)
// Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary
// The compiler seems to be able to vectorize the operation better this way.
// See https://github.com/kokke/tiny-AES-c/pull/34
#if MULTIPLY_AS_A_FUNCTION
static uint8_t Multiply(uint8_t x, uint8_t y)
{
return (((y & 1) * x) ^
((y>>1 & 1) * xtime(x)) ^
((y>>2 & 1) * xtime(xtime(x))) ^
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */
}
#else
#define Multiply(x, y) \
( ((y & 1) * x) ^ \
((y>>1 & 1) * xtime(x)) ^ \
((y>>2 & 1) * xtime(xtime(x))) ^ \
((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
#endif
#if (defined(TINY_AES_CBC) && TINY_AES_CBC == 1) || (defined(TINY_AES_ECB) && TINY_AES_ECB == 1)
/*
static uint8_t getSBoxInvert(uint8_t num)
{
return rsbox[num];
}
*/
#define getSBoxInvert(num) (rsbox[(num)])
// MixColumns function mixes the columns of the state matrix.
// The method used to multiply may be difficult to understand for the inexperienced.
// Please use the references to gain more information.
static void InvMixColumns(state_t* state)
{
int i;
uint8_t a, b, c, d;
for (i = 0; i < 4; ++i)
{
a = (*state)[i][0];
b = (*state)[i][1];
c = (*state)[i][2];
d = (*state)[i][3];
(*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
(*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
(*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
(*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
}
}
// The SubBytes Function Substitutes the values in the
// state matrix with values in an S-box.
static void InvSubBytes(state_t* state)
{
uint8_t i, j;
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 4; ++j)
{
(*state)[j][i] = getSBoxInvert((*state)[j][i]);
}
}
}
static void InvShiftRows(state_t* state)
{
uint8_t temp;
// Rotate first row 1 columns to right
temp = (*state)[3][1];
(*state)[3][1] = (*state)[2][1];
(*state)[2][1] = (*state)[1][1];
(*state)[1][1] = (*state)[0][1];
(*state)[0][1] = temp;
// Rotate second row 2 columns to right
temp = (*state)[0][2];
(*state)[0][2] = (*state)[2][2];
(*state)[2][2] = temp;
temp = (*state)[1][2];
(*state)[1][2] = (*state)[3][2];
(*state)[3][2] = temp;
// Rotate third row 3 columns to right
temp = (*state)[0][3];
(*state)[0][3] = (*state)[1][3];
(*state)[1][3] = (*state)[2][3];
(*state)[2][3] = (*state)[3][3];
(*state)[3][3] = temp;
}
#endif // #if (defined(TINY_AES_CBC) && TINY_AES_CBC == 1) || (defined(TINY_AES_ECB) && TINY_AES_ECB == 1)
// Cipher is the main function that encrypts the PlainText.
static void Cipher(state_t* state, const uint8_t* RoundKey)
{
uint8_t round = 0;
// Add the First round key to the state before starting the rounds.
AddRoundKey(0, state, RoundKey);
// There will be Nr rounds.
// The first Nr-1 rounds are identical.
// These Nr rounds are executed in the loop below.
// Last one without MixColumns()
for (round = 1; ; ++round)
{
SubBytes(state);
ShiftRows(state);
if (round == Nr) {
break;
}
MixColumns(state);
AddRoundKey(round, state, RoundKey);
}
// Add round key to last round
AddRoundKey(Nr, state, RoundKey);
}
#if (defined(TINY_AES_CBC) && TINY_AES_CBC == 1) || (defined(TINY_AES_ECB) && TINY_AES_ECB == 1)
static void InvCipher(state_t* state, const uint8_t* RoundKey)
{
uint8_t round = 0;
// Add the First round key to the state before starting the rounds.
AddRoundKey(Nr, state, RoundKey);
// There will be Nr rounds.
// The first Nr-1 rounds are identical.
// These Nr rounds are executed in the loop below.
// Last one without InvMixColumn()
for (round = (Nr - 1); ; --round)
{
InvShiftRows(state);
InvSubBytes(state);
AddRoundKey(round, state, RoundKey);
if (round == 0) {
break;
}
InvMixColumns(state);
}
}
#endif // #if (defined(TINY_AES_CBC) && TINY_AES_CBC == 1) || (defined(TINY_AES_ECB) && TINY_AES_ECB == 1)
/*****************************************************************************/
/* Public functions: */
/*****************************************************************************/
#if defined(TINY_AES_ECB) && (TINY_AES_ECB == 1)
void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf)
{
// The next function call encrypts the PlainText with the Key using AES algorithm.
Cipher((state_t*)buf, ctx->RoundKey);
}
void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf)
{
// The next function call decrypts the PlainText with the Key using AES algorithm.
InvCipher((state_t*)buf, ctx->RoundKey);
}
#endif // #if defined(TINY_AES_ECB) && (TINY_AES_ECB == 1)
#if defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)
static void XorWithIv(uint8_t* buf, const uint8_t* Iv)
{
uint8_t i;
for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
{
buf[i] ^= Iv[i];
}
}
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, size_t length)
{
size_t i;
uint8_t *Iv = ctx->Iv;
for (i = 0; i < length; i += AES_BLOCKLEN)
{
XorWithIv(buf, Iv);
Cipher((state_t*)buf, ctx->RoundKey);
Iv = buf;
buf += AES_BLOCKLEN;
}
/* store Iv in ctx for next call */
memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
}
void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
{
size_t i;
uint8_t storeNextIv[AES_BLOCKLEN];
for (i = 0; i < length; i += AES_BLOCKLEN)
{
memcpy(storeNextIv, buf, AES_BLOCKLEN);
InvCipher((state_t*)buf, ctx->RoundKey);
XorWithIv(buf, ctx->Iv);
memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
buf += AES_BLOCKLEN;
}
}
#endif // #if defined(TINY_AES_CBC) && (TINY_AES_CBC == 1)
#if defined(TINY_AES_CTR) && (TINY_AES_CTR == 1)
/* Symmetrical operation: same function for encrypting as for decrypting. Note any IV/nonce should never be reused with the same key */
void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length)
{
uint8_t buffer[AES_BLOCKLEN];
size_t i;
int bi;
for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
{
if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
{
memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
Cipher((state_t*)buffer,ctx->RoundKey);
/* Increment Iv and handle overflow */
for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
{
/* inc will overflow */
if (ctx->Iv[bi] == 255)
{
ctx->Iv[bi] = 0;
continue;
}
ctx->Iv[bi] += 1;
break;
}
bi = 0;
}
buf[i] = (buf[i] ^ buffer[bi]);
}
}
#endif // #if defined(TINY_AES_CTR) && (TINY_AES_CTR == 1)
#endif
#endif // _AES_H_
Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 KiB

+2
View File
@@ -0,0 +1,2 @@
# Realtek Semiconductor Corp. RTL2838 DVB-T
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", MODE:="0666", GROUP="adm", SYMLINK+="rtl_sdr"
+17
View File
@@ -0,0 +1,17 @@
all : complex_magsink_to_image image_color.png image.png
magsink_to_image : magsink_to_image.c
gcc -o $@ $^ -O3
complex_magsink_to_image : complex_magsink_to_image.c
gcc -o $@ $^ -O3 -lm
image_color.png : complex_magsink_to_image /tmp/samplelog_I.dat /tmp/samplelog_Q.dat
./complex_magsink_to_image
image.png : magsink_to_image /tmp/samplelog.dat
./magsink_to_image
clean :
rm -rf magsink_to_image complex_magsink_to_image
+14
View File
@@ -0,0 +1,14 @@
# LoRasoft tools
testloradec.grc uses https://github.com/tapparelj/gr-lora_sdr
and is currently configured for an airspy.
But you can configure it for whatever.
If you do a run, it also records a file to /tmp/samplelog.dat
Which if you only record for about 1 second, you can convert into an image with `magsink_to_image.c` which outputs `image.png` so you can examine the stream.
If you want to compromise the signal, making it wider to get better granularity at the cost of temporal resolution, you can change FFTSIZE (in grc) to a different power of two. Then you can update the `int width = 512;` in `magsink_to_image.c` to convert it to the right width for examination.
+196
View File
@@ -0,0 +1,196 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
uint32_t HSVtoHEX( float hue, float sat, float value );
int main()
{
FILE * f = fopen( "/tmp/samplelog_I.dat", "rb" );
if( !f ) { fprintf( stderr, "Error: can't open /tmp/samplelog_I.dat\n" ); exit( -59 ); }
fseek( f, 0, SEEK_END );
int len = ftell( f );
fseek( f, 0, SEEK_SET );
len/=sizeof(float);
float * data_I = malloc( len * sizeof( float ) );
int i;
for( i = 0; i < len; i++ )
{
float val;
int r = fread( &val, 1, sizeof(float), f );
data_I[i] = val;
}
f = fopen( "/tmp/samplelog_Q.dat", "rb" );
if( !f ) { fprintf( stderr, "Error: can't open /tmp/samplelog_Q.dat\n" ); exit( -59 ); }
fseek( f, 0, SEEK_END );
len = ftell( f );
fseek( f, 0, SEEK_SET );
len/=sizeof(float);
float * data_Q = malloc( len * sizeof( float ) );
float * data_M = malloc( len * sizeof( float ) );
for( i = 0; i < len; i++ )
{
float val;
int r = fread( &val, 1, sizeof(float), f );
data_Q[i] = val;
}
int width = 1024;
int height = len / width;
uint32_t * outimage = malloc( sizeof( uint32_t ) * width * height );
int y, x;
float hueofs = 0.0;
for( y = 0; y < height; y++ )
{
float linemin = 1e20;
float linemax = -1e20;
for( x = 0; x < width; x++ )
{
float I = data_I[x+y*width];
float Q = data_Q[x+y*width];
//I= sqrtf(I*I);
//Q= sqrtf(Q*Q);
data_I[x+y*width] = I;
data_Q[x+y*width] = Q;
data_M[x+y*width] =
//log( sqrtf( I*I + Q*Q ) );
sqrtf( I*I + Q*Q );
}
int highestindex = 0;
for( x = 0; x < width; x++ )
{
float M = data_M[x+y*width];
if( M < linemin ) linemin = M;
if( M > linemax ) { linemax = M; highestindex = x; }
}
for( x = 0; x < width; x++ )
{
float I = data_I[x+y*width];
float Q = data_Q[x+y*width];
float M = data_M[x+y*width];
float Mm = ( M - linemin ) / (linemax - linemin);
float hue = (((atan2( I, Q ) / 3.1415926)+1)/2.0);
// hue = phase (0 to 1)
//hue += y * 0.16666666666;
// Wat? Why 18? I literally can't figure it out.
hue *= 18; // It can be 9 if we add an offset of 0.5 per pixel, but then things get out of sync.
// /hue *= 3;
hueofs += 0.466*2.0/1024.0; // .46666 "would" be perfect but striation helps us see the signal.
hue += hueofs;
if( hueofs >= 1.0 ) hueofs -= 1.0;
outimage[x+y*width] = HSVtoHEX( hue, 1.0, Mm ) | 0xff000000;
// if( x == highestindex && M > 1.0 ) printf( "%f %f %08x\n", M, hue, outimage[x+y*width] );
}
}
stbi_write_png( "image_color.png", width, height, 4, outimage, width*4 );
}
uint32_t HSVtoHEX( float hue, float sat, float value )
{
float pr = 0.0;
float pg = 0.0;
float pb = 0.0;
short ora = 0.0;
short og = 0.0;
short ob = 0.0;
float ro = fmod( hue * 6.0, 6.0 );
float avg = 0.0;
ro = fmod( ro + 6.0 + 1.0, 6.0 ); //Hue was 60* off...
if( ro < 1.0 ) //yellow->red
{
pr = 1.0;
pg = 1.0 - ro;
} else if( ro < 2.0 )
{
pr = 1.0;
pb = ro - 1.0;
} else if( ro < 3.0 )
{
pr = 3.0 - ro;
pb = 1.0;
} else if( ro < 4.0 )
{
pb = 1.0;
pg = ro - 3.0;
} else if( ro < 5.0 )
{
pb = 5.0 - ro;
pg = 1.0;
} else
{
pg = 1.0;
pr = ro - 5.0;
}
//Actually, above math is backwards, oops!
pr *= value;
pg *= value;
pb *= value;
avg += pr;
avg += pg;
avg += pb;
pr = pr * sat + avg * (1.0-sat);
pg = pg * sat + avg * (1.0-sat);
pb = pb * sat + avg * (1.0-sat);
ora = pr * 255;
og = pb * 255;
ob = pg * 255;
if( ora < 0 ) ora = 0;
if( ora > 255 ) ora = 255;
if( og < 0 ) og = 0;
if( og > 255 ) og = 255;
if( ob < 0 ) ob = 0;
if( ob > 255 ) ob = 255;
// Pack bits in RGBA format
return (ora << 0) | (og << 8) | (ob << 16);
}
+82
View File
@@ -0,0 +1,82 @@
/*
MIT License
Copyright (c) 2024 Charles Lohr "CNLohr"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main()
{
FILE * f = fopen( "/tmp/samplelog.dat", "rb" );
if( !f ) { fprintf( stderr, "Error: can't open /tmp/samplelog.dat\n" ); exit( -59 ); }
fseek( f, 0, SEEK_END );
int len = ftell( f );
fseek( f, 0, SEEK_SET );
len/=sizeof(float);
float * data = malloc( len * sizeof( float ) );
float fMin = 1e20;
float fMax = -1e20;
int i;
for( i = 0; i < len; i++ )
{
float val;
int r = fread( &val, 1, sizeof(float), f );
if( val > fMax ) fMax = val;
if( val < fMin ) fMin = val;
data[i] = val;
}
int width = 1024;
int height = len / width;
uint32_t * outimage = malloc( sizeof( uint32_t ) * width * height );
int y, x;
for( y = 0; y < height; y++ )
{
float linemin = 1e20;
float linemax = -1e20;
for( x = 0; x < width; x++ )
{
float v = data[x+y*width];
if( v < linemin ) linemin = v;
if( v > linemax ) linemax = v;
data[x+y*width] = v;
}
for( x = 0; x < width; x++ )
{
float v = data[x+y*width];
int intensity = (v - linemin) / (linemax - linemin) * 255.5 * 2 - 256;
if( intensity < 0 ) intensity = 0;
if( intensity > 255 ) intensity = 255;
outimage[x+y*width] = (intensity) | (intensity<<8) | (intensity<<16) | 0xff000000;
}
}
stbi_write_png( "image.png", width, height, 4, outimage, width*4 );
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff