|
| 1 | +## SubGhz library |
| 2 | + |
| 3 | +This library gives some low-level control over the SubGhz radio module |
| 4 | +present in the STM32WL series of microcontrollers. |
| 5 | + |
| 6 | +This radio module is integrated into the microcontroller chip. It is |
| 7 | +essentially a Semtech SX126x radio chip (a combination of the SX1261 and |
| 8 | +SX1262 really), connected to a dedicated internal SPI bus. |
| 9 | + |
| 10 | +This library offers an Arduino-style API to access this module on a low |
| 11 | +level, where the user is responsible for composing and sending the right |
| 12 | +SPI commands to operate the module. It does *not* offer a high-level |
| 13 | +interface for sending or receiving data. |
| 14 | + |
| 15 | +### Using this library |
| 16 | +This library defines a single class and predefines an instance of that |
| 17 | +class called `SubGhz`. To use this library, call methods on this |
| 18 | +instance, e.g.: |
| 19 | + |
| 20 | +``` |
| 21 | +SubGhz.setNssActive(true); |
| 22 | +``` |
| 23 | + |
| 24 | +#### SPI access |
| 25 | +Access to the dedicated SPI bus happens through the `SubGhz.SPI` object. |
| 26 | +This behaves just like the normal Arduino SPI object (it is in fact |
| 27 | +a customized subclass of the normal SPIClass, provided by the normal SPI |
| 28 | +library). |
| 29 | + |
| 30 | +If you use the `beginTransaction()` method, you can pass the |
| 31 | +`SubGhz.spi_settings` variable to get the right settings and run at the |
| 32 | +maximum supported speed. If not using `beginTransaction()`, the default |
| 33 | +settings are also fine, just a bit slower. |
| 34 | + |
| 35 | +#### "GPIO" signals |
| 36 | +Some of the signals which are normally (on an actual external SX126x |
| 37 | +radio chip) available externally and controlled by GPIO are now |
| 38 | +internally connected to registers in the RCC and PWR modules. These |
| 39 | +signals (Nss, Reset and Busy) can be controlled and/or read through this |
| 40 | +library. |
| 41 | + |
| 42 | +The Nss signal can be written (and read back) using |
| 43 | +`SubGhz.setNssActive(bool)` and `SubGhz.isNssActive()`, the Reset signal |
| 44 | +using `SubGhz.setResetActive(bool)` and `SubGhz.isResetActive()` and the |
| 45 | +busy signal can only be read using `SubGhz.isBusy()`. |
| 46 | + |
| 47 | +There is no need to configure these signals before using them (i.e. |
| 48 | +nothing like `pinMode()` is needed). |
| 49 | + |
| 50 | +Note that there is no way to read the DIO signals directly, since they |
| 51 | +are not directly available to the MCU anywhere (only through the |
| 52 | +interrupt controller, so with some care the DIO signal status could be |
| 53 | +derived from the interrupt pending status if needed). |
| 54 | + |
| 55 | +#### Interrupts |
| 56 | +In addition, the DIO signals produced by the radio module are connected |
| 57 | +together in an "OR" configuration and drive the radio interrupt. This |
| 58 | +interrupt can be enabled and configured using various methods in this |
| 59 | +library. |
| 60 | + |
| 61 | +The interrupt can be attached (and enabled) using the |
| 62 | +`SubGhz.attachInterrupt(callback)` method. You can pass any callable here that |
| 63 | +`std::function` accepts (global function, method bound with `std::bind`, |
| 64 | +callable object or a lambda). Attaching an interrupt clears any |
| 65 | +previously pending interrupts and enables the interrupt. |
| 66 | + |
| 67 | +The `SubGhz.detachInterrupt()` method can be used to disable the |
| 68 | +interrupt and clear the handler and the `SubGhz.hasInterrupt()` method |
| 69 | +can be used to query if a handler was attached (regardless of whether it |
| 70 | +is currently enabled). |
| 71 | + |
| 72 | +The `SubGhz.disableInterrupt()` and `SubGhz.enableInterrupt()` method |
| 73 | +can be used to temporarily disable the interrupt. If the interrupt is |
| 74 | +triggered while it is disabled, it will become pending (indicated by the |
| 75 | +`SubGhz.isInterruptPending()` method) and the interrupt handler will run |
| 76 | +directly when the interrupt is enabled again (unless cleared with |
| 77 | +`SubGhz.clearPendingInterrupt()`). Note that there is no method to query |
| 78 | +whether the interrupt is currently enabled, as the interrupt controller |
| 79 | +hardware does allow reading back this value. |
| 80 | + |
| 81 | +Note that there is there is hardly any point in having multiple DIO |
| 82 | +signals, since they are all wired together into a single interrupt and |
| 83 | +cannot be individually read, but this is just how the original Semtech |
| 84 | +radio was designed. |
| 85 | + |
| 86 | +Also note that the DIO lines are directly connected to the MCU interrupt |
| 87 | +controller (NVIC), which is level sensitive. When the ISR is triggered, |
| 88 | +it should always either clear the interrupt flag in the radio, or |
| 89 | +disable the interrupt in the NVIC (using `SubGhz.disableInterrupt()` in |
| 90 | +this library) to prevent the ISR from triggering again (and again and |
| 91 | +again etc.) after it completes. |
| 92 | + |
| 93 | +### Example |
| 94 | + |
| 95 | +This is a basic example of initializing the radio and SPI bus and |
| 96 | +reading a register through an SPI command. See the examples folder for |
| 97 | +a full example sketch. |
| 98 | + |
| 99 | +```C++ |
| 100 | + // initialize SPI: |
| 101 | + SubGhz.SPI.begin(); |
| 102 | + |
| 103 | + // clear reset to |
| 104 | + SubGhz.setResetActive(false); |
| 105 | + |
| 106 | + // Start SPI transaction and wait for the radio to wake up (it starts |
| 107 | + // in sleep mode with its busy signal active). |
| 108 | + SubGhz.SPI.beginTransaction(SubGhz.spi_settings); |
| 109 | + SubGhz.setNssActive(true); |
| 110 | + while (SubGhz.isBusy()) /* wait */; |
| 111 | + |
| 112 | + // Write a command and read the result |
| 113 | + SubGhz.SPI.transfer(0x1D); // Write command: Read_Register() |
| 114 | + SubGhz.SPI.transfer(0x07); // Write MSB of register address: SUBGHZ_LSYNCRH |
| 115 | + SubGhz.SPI.transfer(0x40); // Write LSB of register address: SUBGHZ_LSYNCRH |
| 116 | + uint8_t status = SubGhz.SPI.transfer(0x0); // Read status |
| 117 | + uint8_t value = SubGhz.SPI.transfer(0x0); // Read register value |
| 118 | + |
| 119 | + // End transaction |
| 120 | + SubGhz.setNssActive(false); |
| 121 | + SubGhz.SPI.endTransaction(); |
| 122 | + |
| 123 | + // value now has the register value read |
| 124 | +``` |
| 125 | + |
| 126 | +### License |
| 127 | +Copyright (c) 2022, STMicroelectronics |
| 128 | +All rights reserved. |
| 129 | + |
| 130 | +This software component is licensed by ST under BSD 3-Clause license, |
| 131 | +the "License"; You may not use this file except in compliance with the |
| 132 | +License. You may obtain a copy of the License at: |
| 133 | + opensource.org/licenses/BSD-3-Clause |
0 commit comments