28
28
#include " hal/adc_driver.h"
29
29
#include " hal/gpio.h"
30
30
31
+ #include " timers_driver.h"
32
+ #include " debug.h"
33
+
31
34
#include " hal.h"
32
35
#include " crc.h"
33
36
34
37
#include < string.h>
35
38
39
+ #define SAMPLING_TIMEOUT_US 500
40
+
36
41
static const stm32_usart_t fsUSART = {
37
42
.USARTx = FLYSKY_HALL_SERIAL_USART,
38
43
.txGPIO = FLYSKY_HALL_SERIAL_TX_GPIO,
@@ -62,6 +67,12 @@ static uint8_t _fs_hall_command[8] __DMA;
62
67
63
68
static void * _fs_usart_ctx = nullptr ;
64
69
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
+
65
76
static int _fs_get_byte (uint8_t * data)
66
77
{
67
78
return STM32SerialDriver.getByte (_fs_usart_ctx, data);
@@ -130,12 +141,12 @@ static void _fs_parse(STRUCT_HALL *hallBuffer, unsigned char ch)
130
141
}
131
142
}
132
143
133
- void _fs_cmd_get_version ( )
144
+ void _fs_send_cmd ( uint8_t id, uint8_t payload )
134
145
{
135
146
_fs_hall_command[0 ] = FLYSKY_HALL_PROTOLO_HEAD;
136
- _fs_hall_command[1 ] = 0xb1 ;
147
+ _fs_hall_command[1 ] = id ;
137
148
_fs_hall_command[2 ] = 0x01 ;
138
- _fs_hall_command[3 ] = 0x00 ;
149
+ _fs_hall_command[3 ] = payload ;
139
150
140
151
unsigned short crc = crc16 (CRC_1021, _fs_hall_command, 4 , 0xffff );
141
152
@@ -145,8 +156,31 @@ void _fs_cmd_get_version()
145
156
STM32SerialDriver.sendBuffer (_fs_usart_ctx, _fs_hall_command, 6 );
146
157
}
147
158
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
+ }
150
184
151
185
static void flysky_gimbal_loop (void *)
152
186
{
@@ -169,12 +203,17 @@ static void flysky_gimbal_loop(void*)
169
203
for (uint8_t i = 0 ; i < 4 ; i++) {
170
204
adcValues[i] = FLYSKY_OFFSET_VALUE - p_values[i];
171
205
}
206
+ _fs_gimbal_read_finished = true ;
172
207
} else if (HallProtocol.hallID .hall_Id .packetID == FLYSKY_PACKET_VERSION_ID) {
173
208
uint16_t minorVersion = p_values[6 ];
174
209
uint16_t majorVersion = p_values[7 ];
175
210
if (majorVersion == 2 && minorVersion >= 1 ) {
176
211
_fs_gimbal_version = GIMBAL_V2;
212
+ // Enable sync mode
213
+ _fs_cmd_set_mode (SYNC_1000Hz);
177
214
}
215
+ } else if (HallProtocol.hallID .hall_Id .packetID == FLYSKY_PACKET_MODE_ID) {
216
+ _fs_gimbal_mode = _fs_gimbal_mode_cmd;
178
217
}
179
218
break ;
180
219
}
@@ -221,9 +260,28 @@ bool flysky_gimbal_init(bool force)
221
260
return false ;
222
261
}
223
262
224
- bool is_flysky_gimbal_sync_supported ()
263
+ void flysky_gimbal_start_read ()
225
264
{
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
+ }
227
285
}
228
286
229
287
const etx_serial_port_t * flysky_gimbal_get_port ()
0 commit comments