Paparazzi UAS  v6.0_unstable-92-g17422e4-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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"
31 #include "subsystems/electrical.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 }
#define M_PI_FLOAT
Definition: cc2500_compat.c:54
int32_t bf_getMAhDrawn(void)
struct pidProfile_s * currentPidProfile
Definition: cc2500_compat.c:43
uint16_t bf_getLegacyBatteryVoltage(void)
void pt1FilterUpdateCutoff(pt1Filter_t *filter, float k)
Definition: cc2500_compat.c:66
gpiofnptr_t lo
controlRateConfig_t * currentControlRateProfile
uint32_t timeUs_t
Definition: cc2500_compat.h:93
ioconfig_t
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:82
void bf_IOInit(IO_t io, uint8_t owner, uint8_t index)
Some architecture independent helper functions for GPIOs.
IO_t bf_IOGetByTag(ioTag_t io)
uint8_t bf_getBatteryCellCount(void)
bool bf_isBatteryVoltageConfigured(void)
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
float vsupply
supply voltage in V
Definition: electrical.h:45
bool bf_isAmperageConfigured(void)
uint16_t pin
void bf_IOHi(IO_t io)
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:719
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
void bf_IOConfigGPIO(IO_t io, enum ioconfig_t cfg)
static void gpio_toggle(ioportid_t port, uint16_t pin)
Toggle a gpio output to low level.
Definition: gpio_arch.h:118
uint32_t timeMs_t
Definition: cc2500_compat.h:92
arch independent ADC (Analog to Digital Converter) API
bool bf_rxSpiDeviceInit(void)
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
int16_t bf_getEstimatedVario(void)
bool bf_featureIsEnabled(const uint32_t mask)
Definition: cc2500_compat.c:47
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
#define TRUE
Definition: std.h:4
Interface for electrical status: supply voltage, current, battery status, etc.
Architecture independent timing functions.
float charge
consumed electric charge in Ah
Definition: electrical.h:47
void bf_delay(timeMs_t ms)
Definition: cc2500_compat.c:81
gpiofnptr_t hi
uint32_t port
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
float pt1FilterApply(pt1Filter_t *filter, float input)
Definition: cc2500_compat.c:70
void bf_IOToggle(IO_t io)
void bf_IOLo(IO_t io)
void pt1FilterInit(pt1Filter_t *filter, float k)
Definition: cc2500_compat.c:61
uint16_t bf_getBatteryVoltage(void)
uint16_t bf_adcGetChannel(uint8_t channel)
Definition: cc2500_compat.c:95
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition: gpio_arch.h:88
float pt1FilterGain(float f_cut, float dT)
Definition: cc2500_compat.c:55
API to get/set the generic vehicle states.
bool bf_IORead(IO_t gpio)
static struct EnuCoor_f * stateGetSpeedEnu_f(void)
Get ground speed in local ENU coordinates (float).
Definition: state.h:917
float z
in meters
timeMs_t bf_millis(void)
Definition: cc2500_compat.c:89
timeUs_t bf_micros(void)
Definition: cc2500_compat.c:85
int32_t bf_getEstimatedAltitudeCm(void)
struct Electrical electrical
Definition: electrical.c:66
float current
current in A
Definition: electrical.h:46
void bf_delayMicroseconds(timeUs_t us)
Definition: cc2500_compat.c:77
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
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
int32_t bf_getAmperage(void)
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
struct attitude_t bf_attitude
Definition: cc2500_compat.c:39
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78