New SPI API supporting DMA
[betaflight.git] / src / main / rx / rx_spi_common.c
blob808fa1699912158b2a6131ee0d919768cdecd3d1
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #ifdef USE_RX_SPI
28 #include "drivers/io.h"
29 #include "drivers/time.h"
31 #include "rx/rx_spi.h"
33 #include "rx_spi_common.h"
35 static IO_t ledPin;
36 static bool ledInversion = false;
38 static IO_t bindPin;
39 static bool bindRequested;
40 static bool lastBindPinStatus;
42 void rxSpiCommonIOInit(const rxSpiConfig_t *rxSpiConfig)
44 if (rxSpiConfig->ledIoTag) {
45 ledPin = IOGetByTag(rxSpiConfig->ledIoTag);
46 IOInit(ledPin, OWNER_LED, 0);
47 IOConfigGPIO(ledPin, IOCFG_OUT_PP);
48 ledInversion = rxSpiConfig->ledInversion;
49 rxSpiLedOff();
50 } else {
51 ledPin = IO_NONE;
54 if (rxSpiConfig->bindIoTag) {
55 bindPin = IOGetByTag(rxSpiConfig->bindIoTag);
56 IOInit(bindPin, OWNER_RX_SPI_BIND, 0);
57 IOConfigGPIO(bindPin, IOCFG_IPU);
58 lastBindPinStatus = IORead(bindPin);
59 } else {
60 bindPin = IO_NONE;
64 void rxSpiLedOn(void)
66 if (ledPin) {
67 ledInversion ? IOLo(ledPin) : IOHi(ledPin);
71 void rxSpiLedOff(void)
73 if (ledPin) {
74 ledInversion ? IOHi(ledPin) : IOLo(ledPin);
78 void rxSpiLedToggle(void)
80 if (ledPin) {
81 IOToggle(ledPin);
85 void rxSpiLedBlink(timeMs_t blinkMs)
87 static timeMs_t ledBlinkMs = 0;
89 if ((ledBlinkMs + blinkMs) > millis()) {
90 return;
92 ledBlinkMs = millis();
94 rxSpiLedToggle();
97 void rxSpiLedBlinkRxLoss(rx_spi_received_e result)
99 static timeMs_t rxLossMs = 0;
101 if (ledPin) {
102 if (result == RX_SPI_RECEIVED_DATA) {
103 rxSpiLedOn();
104 } else {
105 if ((rxLossMs + INTERVAL_RX_LOSS_MS) > millis()) {
106 return;
108 rxSpiLedToggle();
110 rxLossMs = millis();
114 void rxSpiLedBlinkBind(void)
116 rxSpiLedBlink(INTERVAL_RX_BIND_MS);
119 void rxSpiBind(void)
121 bindRequested = true;
124 bool rxSpiCheckBindRequested(bool reset)
126 if (bindPin) {
127 bool bindPinStatus = IORead(bindPin);
128 if (lastBindPinStatus && !bindPinStatus) {
129 bindRequested = true;
131 lastBindPinStatus = bindPinStatus;
134 if (!bindRequested) {
135 return false;
136 } else {
137 if (reset) {
138 bindRequested = false;
141 return true;
144 #endif