Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
cc2500_compat.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_compat.h"
26 
27 #include "state.h"
28 #include "mcu_periph/adc.h"
29 #include "mcu_periph/gpio.h"
30 #include "mcu_periph/sys_time.h"
32 
33 #include <stdbool.h>
34 #include <assert.h>
35 #include <math.h>
36 
37 
38 // (unknown):
39 struct attitude_t bf_attitude = { { 0, 0, 0 } }; // Dummy values
40 
41 
42 // main/config/config.h:
43 struct pidProfile_s *currentPidProfile; // Dummy values
44 
45 
46 // main/config/feature.h:
47 bool bf_featureIsEnabled(const uint32_t mask) {
49  return features & mask;
50 }
51 
52 
53 // main/common/filters.h:
54 #define M_PI_FLOAT 3.14159265358979323846f
55 float pt1FilterGain(float f_cut, float dT)
56 {
57  float RC = 1 / ( 2 * M_PI_FLOAT * f_cut);
58  return dT / (RC + dT);
59 }
60 
61 void pt1FilterInit(pt1Filter_t *filter, float k) {
62  filter->state = 0.0f;
63  filter->k = k;
64 }
65 
66 void pt1FilterUpdateCutoff(pt1Filter_t *filter, float k) {
67  filter->k = k;
68 }
69 
70 float pt1FilterApply(pt1Filter_t *filter, float input) {
71  filter->state = filter->state + filter->k * (input - filter->state);
72  return filter->state;
73 }
74 
75 
76 // main/drivers/time.h:
78  sys_time_usleep(us);
79 }
80 
81 void bf_delay(timeMs_t ms) {
82  bf_delayMicroseconds(ms * 1000);
83 }
84 
86  return get_sys_time_usec();
87 }
88 
90  return get_sys_time_msec();
91 }
92 
93 
94 // main/drivers/adc.h:
96  (void) channel;
97  // Return current, as voltage is already reported by getLegacyBatteryVoltage
98  return (uint16_t)(electrical.current * 10000.0); // Assumed in 0.1mA
99 }
100 
101 
102 // main/drivers/rx_spi.h:
103 bool bf_rxSpiDeviceInit(void) {
104  return TRUE;
105 }
106 
107 
108 // main/drivers/io.h:
110  return (IO_t)io;
111 }
112 
113 void bf_IOInit(IO_t io, uint8_t owner, uint8_t index) {
114  (void) io;
115  (void) owner;
116  (void) index;
117 }
118 
119 void bf_IOConfigGPIO(IO_t io, enum ioconfig_t cfg) {
120  if (!io) return;
121  switch(cfg) {
122  case IOCFG_OUT_PP:
123  gpio_setup_output(io->port, io->pin);
124  break;
125  case IOCFG_IN_FLOATING:
126  gpio_setup_input(io->port, io->pin);
127  break;
128  case IOCFG_IPU:
129  gpio_setup_input_pullup(io->port, io->pin);
130  break;
131  default:
132  assert("Invalid IO config" == NULL);
133  break;
134  }
135 }
136 
137 bool bf_IORead(IO_t gpio) {
138  if (!gpio) return 0;
139  return gpio_get(gpio->port, gpio->pin);
140 }
141 
142 void bf_IOHi(IO_t io) {
143  if (!io) return;
144  io->hi(io->port, io->pin);
145 }
146 
147 void bf_IOLo(IO_t io) {
148  if (!io) return;
149  io->lo(io->port, io->pin);
150 }
151 
152 void bf_IOToggle(IO_t io) {
153  if (!io) return;
154  gpio_toggle(io->port, io->pin);
155 }
156 
157 
158 // main/fc/controlrate_profile.h:
160 
161 
162 // main/flight/position.h:
164  return (int32_t)(stateGetPositionEnu_f()->z * 100.0);
165 }
166 
168  return (int16_t)(stateGetSpeedEnu_f()->z * 100.0);
169 }
170 
171 
172 // main/sensors/battery.h
174  return TRUE;
175 }
176 
178  return (uint16_t)((electrical.vsupply * 100.0 + 5) / 10.0); // ???
179 }
180 
182  return (uint16_t)(electrical.vsupply * 100.0); // 0.01V
183 }
184 
186  return 0;
187 }
188 
190  return TRUE;
191 }
192 
194  return (int32_t)(electrical.current * 100.0);
195 }
196 
198  return (int32_t)(electrical.charge * 1000.0);
199 }
currentControlRateProfile
controlRateConfig_t * currentControlRateProfile
Definition: cc2500_compat.c:159
electrical.h
bf_isBatteryVoltageConfigured
bool bf_isBatteryVoltageConfigured(void)
Definition: cc2500_compat.c:173
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
FEATURE_RX_SPI
@ FEATURE_RX_SPI
Definition: cc2500_compat.h:155
pt1Filter_s::state
float state
Definition: cc2500_compat.h:108
pt1FilterUpdateCutoff
void pt1FilterUpdateCutoff(pt1Filter_t *filter, float k)
Definition: cc2500_compat.c:66
IOCFG_OUT_PP
@ IOCFG_OUT_PP
Definition: cc2500_compat.h:216
pt1FilterInit
void pt1FilterInit(pt1Filter_t *filter, float k)
Definition: cc2500_compat.c:61
sys_time_usleep
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:104
bf_IOHi
void bf_IOHi(IO_t io)
Definition: cc2500_compat.c:142
pt1FilterGain
float pt1FilterGain(float f_cut, float dT)
Definition: cc2500_compat.c:55
gpio_t::pin
uint16_t pin
Definition: cc2500_compat.h:201
stateGetPositionEnu_f
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
gpio_setup_output
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
bf_IOConfigGPIO
void bf_IOConfigGPIO(IO_t io, enum ioconfig_t cfg)
Definition: cc2500_compat.c:119
bf_getEstimatedAltitudeCm
int32_t bf_getEstimatedAltitudeCm(void)
Definition: cc2500_compat.c:163
bf_IOToggle
void bf_IOToggle(IO_t io)
Definition: cc2500_compat.c:152
timeUs_t
uint32_t timeUs_t
Definition: cc2500_compat.h:93
adc.h
arch independent ADC (Analog to Digital Converter) API
ioconfig_t
ioconfig_t
Definition: cc2500_compat.h:215
bf_getAmperage
int32_t bf_getAmperage(void)
Definition: cc2500_compat.c:193
bf_millis
timeMs_t bf_millis(void)
Definition: cc2500_compat.c:89
Electrical::vsupply
float vsupply
supply voltage in V
Definition: electrical.h:45
bf_getEstimatedVario
int16_t bf_getEstimatedVario(void)
Definition: cc2500_compat.c:167
bf_getMAhDrawn
int32_t bf_getMAhDrawn(void)
Definition: cc2500_compat.c:197
EnuCoor_f::z
float z
in meters
Definition: pprz_geodetic_float.h:75
bf_IORead
bool bf_IORead(IO_t gpio)
Definition: cc2500_compat.c:137
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
int16_t
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
bf_featureIsEnabled
bool bf_featureIsEnabled(const uint32_t mask)
Definition: cc2500_compat.c:47
FEATURE_TELEMETRY
@ FEATURE_TELEMETRY
Definition: cc2500_compat.h:144
Electrical::charge
float charge
consumed electric charge in Ah
Definition: electrical.h:47
pidProfile_s
Definition: cc2500_compat.h:128
bf_delayMicroseconds
void bf_delayMicroseconds(timeUs_t us)
Definition: cc2500_compat.c:77
gpio_setup_input
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition: gpio_arch.c:40
bf_IOGetByTag
IO_t bf_IOGetByTag(ioTag_t io)
Definition: cc2500_compat.c:109
gpio_t::lo
gpiofnptr_t lo
Definition: cc2500_compat.h:203
pt1Filter_s::k
float k
Definition: cc2500_compat.h:109
gpio_t::hi
gpiofnptr_t hi
Definition: cc2500_compat.h:202
bf_IOInit
void bf_IOInit(IO_t io, uint8_t owner, uint8_t index)
Definition: cc2500_compat.c:113
sys_time.h
Architecture independent timing functions.
bf_getBatteryCellCount
uint8_t bf_getBatteryCellCount(void)
Definition: cc2500_compat.c:185
get_sys_time_msec
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:86
M_PI_FLOAT
#define M_PI_FLOAT
Definition: cc2500_compat.c:54
bf_attitude
struct attitude_t bf_attitude
Definition: cc2500_compat.c:39
IOCFG_IPU
@ IOCFG_IPU
Definition: cc2500_compat.h:218
bf_IOLo
void bf_IOLo(IO_t io)
Definition: cc2500_compat.c:147
pt1Filter_s
Definition: cc2500_compat.h:107
bf_isAmperageConfigured
bool bf_isAmperageConfigured(void)
Definition: cc2500_compat.c:189
gpio_toggle
static void gpio_toggle(ioportid_t port, uint16_t pin)
Toggle a gpio output to low level.
Definition: gpio_arch.h:118
pt1FilterApply
float pt1FilterApply(pt1Filter_t *filter, float input)
Definition: cc2500_compat.c:70
gpio_t
Definition: cc2500_compat.h:199
bf_getBatteryVoltage
uint16_t bf_getBatteryVoltage(void)
Definition: cc2500_compat.c:181
bf_delay
void bf_delay(timeMs_t ms)
Definition: cc2500_compat.c:81
gpio_get
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition: gpio_arch.h:88
bf_getLegacyBatteryVoltage
uint16_t bf_getLegacyBatteryVoltage(void)
Definition: cc2500_compat.c:177
int32_t
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
attitude_t
Definition: cc2500_compat.h:75
bf_micros
timeUs_t bf_micros(void)
Definition: cc2500_compat.c:85
bf_adcGetChannel
uint16_t bf_adcGetChannel(uint8_t channel)
Definition: cc2500_compat.c:95
currentPidProfile
struct pidProfile_s * currentPidProfile
Definition: cc2500_compat.c:43
bf_rxSpiDeviceInit
bool bf_rxSpiDeviceInit(void)
Definition: cc2500_compat.c:103
gpio.h
stateGetSpeedEnu_f
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:917
controlRateConfig_t
Definition: cc2500_compat.h:244
gpio_t::port
uint32_t port
Definition: cc2500_compat.h:200
gpio_setup_input_pullup
void gpio_setup_input_pullup(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs with pull up resistor enabled.
Definition: gpio_arch.c:47
electrical
struct Electrical electrical
Definition: electrical.c:66
state.h
TRUE
#define TRUE
Definition: std.h:4
uint16_t
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
IOCFG_IN_FLOATING
@ IOCFG_IN_FLOATING
Definition: cc2500_compat.h:217
cc2500_compat.h
timeMs_t
uint32_t timeMs_t
Definition: cc2500_compat.h:92
Electrical::current
float current
current in A
Definition: electrical.h:46