Paparazzi UAS  v5.17_devel-24-g2ae834f
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
cc2500_settings.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Tom van Dijk <tomvand@users.noreply.github.com>
3  *
4  * This code is based on the betaflight cc2500 and FrskyX implementation.
5  * https://github.com/betaflight/betaflight
6  *
7  * This file is part of paparazzi.
8  *
9  * paparazzi is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * paparazzi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with paparazzi; see the file COPYING. If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24 
25 #include "cc2500_settings.h"
26 
27 #include "cc2500_rx_spi.h"
28 
29 #include BOARD_CONFIG
30 #include "mcu_periph/gpio.h"
31 #include "generated/airframe.h"
32 #include "subsystems/settings.h"
33 
34 #include <string.h>
35 
36 
37 #define _LED_GPIO(l) l ## _GPIO
38 #define LED_GPIO(l) _LED_GPIO(l)
39 #define _LED_GPIO_PIN(l) l ## _GPIO_PIN
40 #define LED_GPIO_PIN(l) _LED_GPIO_PIN(l)
41 #define _LED_GPIO_ON(l) l ## _GPIO_ON
42 #define LED_GPIO_ON(l) _LED_GPIO_ON(l)
43 #define _LED_GPIO_OFF(l) l ## _GPIO_OFF
44 #define LED_GPIO_OFF(l) _LED_GPIO_OFF(l)
45 
46 #define _BUTTON_GPIO(b) b ## _GPIO
47 #define BUTTON_GPIO(b) _BUTTON_GPIO(b)
48 #define _BUTTON_PIN(b) b ## _PIN
49 #define BUTTON_PIN(b) _BUTTON_PIN(b)
50 
51 
52 #ifndef CC2500_RX_SPI_PROTOCOL
53 #define CC2500_RX_SPI_PROTOCOL RX_SPI_FRSKY_X_LBT
54 #endif
55 
56 #ifndef CC2500_AUTOBIND
57 #define CC2500_AUTOBIND FALSE
58 #endif
59 
60 #ifndef CC2500_TELEMETRY_SENSORS
61 #define CC2500_TELEMETRY_SENSORS (SENSOR_VOLTAGE | SENSOR_CURRENT | SENSOR_FUEL | SENSOR_ALTITUDE | SENSOR_VARIO)
62 #endif
63 
64 static void cc2500_persistent_write(void);
65 
66 
67 // main/config/config.h:
68 void bf_writeEEPROM(void) {
69  // Settings storage handled by paparazzi's persistent settings
72 }
73 
74 
75 // main/pg/rx.h:
77 const rxConfig_t* rxConfig(void) {
78  return &rxconfig;
79 }
80 
81 
82 // main/pg/rx_spi.h:
83 static struct gpio_t extiIo;
84 static struct gpio_t ledIo;
85 static struct gpio_t bindIo;
87 const rxSpiConfig_t* rxSpiConfig(void) {
88  return &spiconfig;
89 }
90 
91 
92 // main/pg/rx_spi_cc2500.h:
95  return &cc2500spiconfig;
96 }
98  return &cc2500spiconfig;
99 }
100 
101 
102 // main/telemetry/telemetry.h:
104 
106  return &telemetryconfig;
107 }
108 
109 
110 // main/telemetry/telemetry.h:
112  return sensor & CC2500_TELEMETRY_SENSORS;
113 }
114 
115 
116 // Paparazzi code
118 
119 static void cc2500_persistent_write(void) {
121  cc2500spiconfig.bindTxId[0] |
122  cc2500spiconfig.bindTxId[1] << 8 |
123  cc2500spiconfig.bindOffset << 16 |
124  cc2500spiconfig.rxNum << 24;
125  for (int i = 0; i < 48; i += 4) {
127  cc2500spiconfig.bindHopData[i] |
128  cc2500spiconfig.bindHopData[i + 1] << 8 |
129  cc2500spiconfig.bindHopData[i + 2] << 16 |
130  cc2500spiconfig.bindHopData[i + 3] << 24;
131  }
133  cc2500spiconfig.bindHopData[48] |
134  cc2500spiconfig.bindHopData[49] << 8 |
135  0xFF << 24;
136 }
137 
138 static void cc2500_persistent_read(void) {
139  // Check that persistent data is loaded
140  // highest bindHopData byte is initialized 0 at boot
141  if (cc2500_settings_persistent.bindHopData[12] >> 24) {
142  cc2500spiconfig.bindTxId[0] = cc2500_settings_persistent.bindVars & 0xFF;
143  cc2500spiconfig.bindTxId[1] = (cc2500_settings_persistent.bindVars >> 8) & 0xFF;
144  cc2500spiconfig.bindOffset = (cc2500_settings_persistent.bindVars >> 16) & 0xFF;
145  cc2500spiconfig.rxNum = (cc2500_settings_persistent.bindVars >> 24) & 0xFF;
146  }
147  for (int i = 0; i < 48; i += 4) {
148  cc2500spiconfig.bindHopData[i] = cc2500_settings_persistent.bindHopData[i / 4] & 0xFF;
149  cc2500spiconfig.bindHopData[i + 1] = (cc2500_settings_persistent.bindHopData[i / 4] >> 8) & 0xFF;
150  cc2500spiconfig.bindHopData[i + 2] = (cc2500_settings_persistent.bindHopData[i / 4] >> 16) & 0xFF;
151  cc2500spiconfig.bindHopData[i + 3] = (cc2500_settings_persistent.bindHopData[i / 4] >> 24) & 0xFF;
152  }
153  cc2500spiconfig.bindHopData[48] = cc2500_settings_persistent.bindHopData[12] & 0xFF;
154  cc2500spiconfig.bindHopData[49] = (cc2500_settings_persistent.bindHopData[12] >> 8) & 0xFF;
155 }
156 
157 
158 
160  // rxConfig
161  rxconfig.rssi_channel = 0;
162  rxconfig.midrc = 1500;
163  rxconfig.max_aux_channel = 0; // TODO
164  rxconfig.rssi_src_frame_lpf_period = 30;
165 
166  // rxSpiConfig
170  spiconfig.extiIoTag = &extiIo;
171 
172 #ifdef CC2500_RX_LED
173  ledIo.port = LED_GPIO(CC2500_RX_LED);
174  ledIo.pin = LED_GPIO_PIN(CC2500_RX_LED);
175  ledIo.hi = LED_GPIO_ON(CC2500_RX_LED);
176  ledIo.lo = LED_GPIO_OFF(CC2500_RX_LED);
177  spiconfig.ledIoTag = &ledIo;
178 #else
179  (void) ledIo;
180  spiconfig.ledIoTag = NULL;
181 #endif
182  spiconfig.ledInversion = FALSE; // Handled by paparazzi LED_X_GPIO_ON|_OFF
183 
184 #ifdef CC2500_BIND_BUTTON
185  bindIo.port = BUTTON_GPIO(CC2500_BIND_BUTTON);
186  bindIo.pin = BUTTON_PIN(CC2500_BIND_BUTTON);
187  spiconfig.bindIoTag = &bindIo;
188 #else
189  (void) bindIo;
190  spiconfig.bindIoTag = NULL;
191 #endif
192 
193  // rxCc2500SpiConfig
194  cc2500spiconfig.autoBind = CC2500_AUTOBIND;
195  cc2500spiconfig.bindTxId[0] = 0;
196  cc2500spiconfig.bindTxId[1] = 0;
197  cc2500spiconfig.bindOffset = 0;
198  memset(cc2500spiconfig.bindHopData, 0, sizeof(cc2500spiconfig.bindHopData));
199  cc2500spiconfig.rxNum = 0;
200  cc2500spiconfig.a1Source = FRSKY_SPI_A1_SOURCE_VBAT;
201  cc2500spiconfig.chipDetectEnabled = TRUE;
202 
203  settings_init();
205 
206  // telemetryConfig
207  telemetryconfig.pidValuesAsTelemetry = FALSE;
208  telemetryconfig.report_cell_voltage = FALSE;
209 }
const rxSpiConfig_t * rxSpiConfig(void)
void cc2500_settings_init(void)
gpiofnptr_t lo
static struct gpio_t ledIo
static void cc2500_persistent_read(void)
Some architecture independent helper functions for GPIOs.
uint8_t rx_spi_protocol
#define LED_GPIO_PIN(l)
#define CC2500_TELEMETRY_SENSORS
uint16_t midrc
static rxConfig_t rxconfig
uint8_t rssi_src_frame_lpf_period
uint8_t ledInversion
uint8_t pidValuesAsTelemetry
#define CC2500_AUTOBIND
#define CC2500_GDO0_GPIO
uint16_t pin
#define FALSE
Definition: std.h:5
void settings_init(void)
Definition: settings.c:43
#define TRUE
Definition: std.h:4
bool telemetryIsSensorEnabled(sensor_e sensor)
static telemetryConfig_t telemetryconfig
sensor_e
struct cc2500_settings_persistent_s cc2500_settings_persistent
#define BUTTON_PIN(b)
#define CC2500_RX_SPI_PROTOCOL
gpiofnptr_t hi
static struct gpio_t bindIo
uint32_t port
static void cc2500_persistent_write(void)
#define CC2500_GDO0_PIN
const rxCc2500SpiConfig_t * rxCc2500SpiConfig(void)
Persistent settings interface.
#define LED_GPIO_ON(l)
const telemetryConfig_t * telemetryConfig(void)
#define LED_GPIO_OFF(l)
const rxConfig_t * rxConfig(void)
#define BUTTON_GPIO(b)
rxCc2500SpiConfig_t * rxCc2500SpiConfigMutable(void)
static rxSpiConfig_t spiconfig
uint8_t max_aux_channel
static rxCc2500SpiConfig_t cc2500spiconfig
void bf_writeEEPROM(void)
static struct gpio_t extiIo
#define settings_StoreSettings(_v)
Definition: settings.h:40
#define LED_GPIO(l)
uint8_t rssi_channel