Skip to content

Commit 2cb6af7

Browse files
committed
1000Hz sync sampling works
1 parent 9ee4f22 commit 2cb6af7

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

radio/src/boards/generic_stm32/analog_inputs.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#include "ads79xx.h"
3131
#endif
3232

33+
#if defined(FLYSKY_GIMBAL)
34+
#include "flysky_gimbal_driver.h"
35+
#endif
36+
3337
#include "definitions.h"
3438

3539
#include "myeeprom.h"
@@ -66,6 +70,9 @@ static bool adc_start_read()
6670
if (n_ADC_spi > 0) {
6771
success = success && ads79xx_adc_start_read(&_ADC_spi[0], _ADC_inputs);
6872
}
73+
#endif
74+
#if defined(FLYSKY_GIMBAL)
75+
flysky_gimbal_start_read();
6976
#endif
7077
return success;
7178
}
@@ -78,6 +85,9 @@ static void adc_wait_completion()
7885
if (n_ADC_spi > 0) ads79xx_adc_wait_completion(&_ADC_spi[0], _ADC_inputs);
7986
#endif
8087
stm32_hal_adc_wait_completion(_ADC_adc, n_ADC, _ADC_inputs, n_inputs);
88+
#if defined(FLYSKY_GIMBAL)
89+
flysky_gimbal_wait_completion();
90+
#endif
8191
}
8292

8393
const etx_hal_adc_driver_t _adc_driver = {

radio/src/targets/common/arm/stm32/flysky_gimbal_driver.cpp

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@
2828
#include "hal/adc_driver.h"
2929
#include "hal/gpio.h"
3030

31+
#include "timers_driver.h"
32+
#include "debug.h"
33+
3134
#include "hal.h"
3235
#include "crc.h"
3336

3437
#include <string.h>
3538

39+
#define SAMPLING_TIMEOUT_US 500
40+
3641
static const stm32_usart_t fsUSART = {
3742
.USARTx = FLYSKY_HALL_SERIAL_USART,
3843
.txGPIO = FLYSKY_HALL_SERIAL_TX_GPIO,
@@ -62,6 +67,12 @@ static uint8_t _fs_hall_command[8] __DMA;
6267

6368
static void* _fs_usart_ctx = nullptr;
6469

70+
static volatile bool _fs_gimbal_detected;
71+
static uint8_t _fs_gimbal_version = GIMBAL_V1;
72+
static uint8_t _fs_gimbal_mode = V1_MODE;
73+
static uint8_t _fs_gimbal_mode_cmd = V1_MODE;
74+
static bool _fs_gimbal_read_finished = true;
75+
6576
static int _fs_get_byte(uint8_t* data)
6677
{
6778
return STM32SerialDriver.getByte(_fs_usart_ctx, data);
@@ -130,12 +141,12 @@ static void _fs_parse(STRUCT_HALL *hallBuffer, unsigned char ch)
130141
}
131142
}
132143

133-
void _fs_cmd_get_version()
144+
void _fs_send_cmd(uint8_t id, uint8_t payload)
134145
{
135146
_fs_hall_command[0] = FLYSKY_HALL_PROTOLO_HEAD;
136-
_fs_hall_command[1] = 0xb1;
147+
_fs_hall_command[1] = id;
137148
_fs_hall_command[2] = 0x01;
138-
_fs_hall_command[3] = 0x00;
149+
_fs_hall_command[3] = payload;
139150

140151
unsigned short crc = crc16(CRC_1021, _fs_hall_command, 4, 0xffff);
141152

@@ -145,8 +156,31 @@ void _fs_cmd_get_version()
145156
STM32SerialDriver.sendBuffer(_fs_usart_ctx, _fs_hall_command, 6);
146157
}
147158

148-
static volatile bool _fs_gimbal_detected;
149-
static volatile uint8_t _fs_gimbal_version = GIMBAL_V1;
159+
void _fs_cmd_get_version()
160+
{
161+
_fs_send_cmd(0xb1, 0x00);
162+
}
163+
164+
void _fs_cmd_set_mode(V2_GIMBAL_MODE mode)
165+
{
166+
if (_fs_gimbal_mode != _fs_gimbal_mode_cmd) {
167+
// Last command not responsed yet
168+
return;
169+
}
170+
171+
_fs_gimbal_mode_cmd = mode;
172+
_fs_send_cmd(0x41, mode);
173+
}
174+
175+
void _fs_cmd_start_read()
176+
{
177+
_fs_send_cmd(0xc1, 0x00);
178+
}
179+
180+
bool _fs_sync_enabled()
181+
{
182+
return _fs_gimbal_detected && _fs_gimbal_version > GIMBAL_V1 && _fs_gimbal_mode != V1_MODE;
183+
}
150184

151185
static void flysky_gimbal_loop(void*)
152186
{
@@ -169,12 +203,17 @@ static void flysky_gimbal_loop(void*)
169203
for (uint8_t i = 0; i < 4; i++) {
170204
adcValues[i] = FLYSKY_OFFSET_VALUE - p_values[i];
171205
}
206+
_fs_gimbal_read_finished = true;
172207
} else if (HallProtocol.hallID.hall_Id.packetID == FLYSKY_PACKET_VERSION_ID) {
173208
uint16_t minorVersion = p_values[6];
174209
uint16_t majorVersion = p_values[7];
175210
if (majorVersion == 2 && minorVersion >= 1) {
176211
_fs_gimbal_version = GIMBAL_V2;
212+
// Enable sync mode
213+
_fs_cmd_set_mode(SYNC_1000Hz);
177214
}
215+
} else if (HallProtocol.hallID.hall_Id.packetID == FLYSKY_PACKET_MODE_ID) {
216+
_fs_gimbal_mode = _fs_gimbal_mode_cmd;
178217
}
179218
break;
180219
}
@@ -221,9 +260,28 @@ bool flysky_gimbal_init(bool force)
221260
return false;
222261
}
223262

224-
bool is_flysky_gimbal_sync_supported()
263+
void flysky_gimbal_start_read()
225264
{
226-
return _fs_gimbal_version > GIMBAL_V1;
265+
if(_fs_sync_enabled()) {
266+
if (_fs_gimbal_read_finished) {
267+
_fs_gimbal_read_finished = false;
268+
_fs_cmd_start_read();
269+
}
270+
}
271+
}
272+
273+
void flysky_gimbal_wait_completion()
274+
{
275+
if(_fs_sync_enabled()) {
276+
auto timeout = timersGetUsTick();
277+
while(!_fs_gimbal_read_finished) {
278+
// busy wait
279+
if ((uint32_t)(timersGetUsTick() - timeout) >= SAMPLING_TIMEOUT_US) {
280+
TRACE("Gimbal timeout");
281+
return;
282+
}
283+
}
284+
}
227285
}
228286

229287
const etx_serial_port_t* flysky_gimbal_get_port()

radio/src/targets/common/arm/stm32/flysky_gimbal_driver.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define FLYSKY_OFFSET_VALUE ( 1 << 12 )
3939

4040
#define FLYSKY_HALL_PROTOLO_HEAD 0x55
41+
#define FLYSKY_PACKET_MODE_ID 0x04
4142
#define FLYSKY_PACKET_VERSION_ID 0x0b
4243
#define FLYSKY_PACKET_CHANNEL_ID 0x0c
4344

@@ -125,18 +126,26 @@ enum TRANSFER_DIR_E {
125126
TRANSFER_DIR_RFMODULE,
126127
};
127128

128-
enum FLYSKY_GIMBAL_VERSION {
129+
enum GIMBAL_VERSION {
129130
GIMBAL_V1,
130131
GIMBAL_V2
131132
};
132133

134+
enum V2_GIMBAL_MODE {
135+
V1_MODE = 0,
136+
SYNC_400Hz = 1,
137+
SYNC_1000Hz = 2
138+
};
139+
133140
extern signed short hall_raw_values[FLYSKY_HALL_CHANNEL_COUNT];
134141
extern unsigned short hall_adc_values[FLYSKY_HALL_CHANNEL_COUNT];
135142

136143
// returns true if the gimbals were detected properly
137144
bool flysky_gimbal_init(bool force = false);
138145

139146
void flysky_gimbal_deinit();
140-
bool is_flysky_gimbal_sync_supported();
147+
148+
void flysky_gimbal_start_read();
149+
void flysky_gimbal_wait_completion();
141150

142151
const etx_serial_port_t* flysky_gimbal_get_port();

0 commit comments

Comments
 (0)