/
feiyu_imu.c
1036 lines (826 loc) · 22.5 KB
/
feiyu_imu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Feiyu gimbal hack
*
* Copyright (C) 2016 Adam Williams <broadcast at earthling dot net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// MPU6050 driver for the Feiyu
#include "feiyu_mane.h"
#include "feiyu_imu.h"
#include "feiyu_hall.h"
#include "arm_linux.h"
#include "feiyu_uart.h"
#include "stm32f1xx_hal_i2c.h"
#include "stm32f1xx_hal_gpio.h"
#include "stm32f1xx_hal_rcc.h"
#include "arm_math.h"
imu_t imu;
#ifdef BOARD0
// must be level so it doesn't tilt when panning
// really need variable roll/pitch
//#define ROLL_OFFSET FIXED(5)
#define ROLL_OFFSET FIXED(0)
#define PITCH_OFFSET FIXED(2.5)
#define GYRO_RATIO (IMU_HZ / 50)
#define ACCEL_BANDWIDTH 16
#define GYRO_CENTER_MAX 200
#define GYRO_CENTER_TOTAL IMU_HZ
#define MAX_GYRO_DRIFT 256
//#define BLEND_DOWNSAMPLE (IMU_HZ / 32)
#define BLEND_DOWNSAMPLE (IMU_HZ / 256)
// make it infinity to test the gyros
//#define BLEND_DOWNSAMPLE 0x7fffffff
// normal blending
#define ATTITUDE_BLEND 256
// when the gyros saturate
#define ATTITUDE_BLEND2 64
// time to use ATTITUDE_BLEND2
#define RECOVER_TIME (5 * IMU_HZ / BLEND_DOWNSAMPLE)
#define ANGLE_TO_GYRO (500 * IMU_HZ / 1000)
static int fix_gyro_angle(int gyro_angle)
{
if(gyro_angle > 180 * ANGLE_TO_GYRO * FRACTION)
gyro_angle -= 360 * ANGLE_TO_GYRO * FRACTION;
else
if(gyro_angle < -180 * ANGLE_TO_GYRO * FRACTION)
gyro_angle += 360 * ANGLE_TO_GYRO * FRACTION;
return gyro_angle;
}
void do_ahrs(unsigned char *imu_buffer)
{
/*
* TRACE
* print_number(&uart, fei.hall0);
* print_number(&uart, fei.hall1);
* print_number(&uart, fei.hall2);
*/
static int blink_counter = 0;
fei.gyro_count++;
if(fei.gyro_count >= GYRO_RATIO)
{
fei.gyro_count = 0;
int accel_x = -(int16_t)((imu_buffer[6] << 8) | imu_buffer[7]);
int accel_y = -(int16_t)((imu_buffer[2] << 8) | imu_buffer[3]);
int accel_z = (int16_t)((imu_buffer[4] << 8) | imu_buffer[5]);
fei.accel_x = (fei.accel_x * (FRACTION - ACCEL_BANDWIDTH) +
accel_x * FRACTION * ACCEL_BANDWIDTH) / FRACTION;
fei.accel_y = (fei.accel_y * (FRACTION - ACCEL_BANDWIDTH) +
accel_y * FRACTION * ACCEL_BANDWIDTH) / FRACTION;
fei.accel_z = (fei.accel_z * (FRACTION - ACCEL_BANDWIDTH) +
accel_z * FRACTION * ACCEL_BANDWIDTH) / FRACTION;
// handle flip
if(!fei.flip)
{
if(fei.accel_z < -FLIP_THRESHOLD)
{
fei.flip_counter++;
if(fei.flip_counter >= FLIP_COUNT / GYRO_RATIO)
{
fei.flip = 1;
fei.flip_counter = 0;
}
}
else
{
fei.flip_counter = 0;
}
}
else
{
if(fei.accel_z > FLIP_THRESHOLD)
{
fei.flip_counter++;
if(fei.flip_counter >= FLIP_COUNT / GYRO_RATIO)
{
fei.flip = 0;
fei.flip_counter = 0;
}
}
else
{
fei.flip_counter = 0;
}
}
// absolute angles
if(abs_fixed(fei.accel_z) < 256)
{
fei.abs_roll = 0;
fei.abs_pitch = 0;
}
else
{
fei.abs_roll = -atan2_fixed(fei.accel_x / FRACTION, fei.accel_z / FRACTION);
fei.abs_pitch = -atan2_fixed(-fei.accel_y / FRACTION, fei.accel_z / FRACTION);
if(fei.flip)
{
fei.abs_roll += FIXED(180);
fei.abs_pitch = FIXED(180) - fei.abs_pitch;
}
fei.abs_roll += ROLL_OFFSET;
fei.abs_pitch += PITCH_OFFSET;
fei.abs_roll = fix_angle(fei.abs_roll);
fei.abs_pitch = fix_angle(fei.abs_pitch);
}
}
fei.gyro_x = -(int16_t)((imu_buffer[10] << 8) | imu_buffer[11]);
fei.gyro_y = -(int16_t)((imu_buffer[14] << 8) | imu_buffer[15]);
fei.gyro_z = -(int16_t)((imu_buffer[12] << 8) | imu_buffer[13]);
if(fei.calibrate_imu)
{
// blink the LED
blink_counter++;
if(blink_counter >= IMU_HZ / 10)
{
blink_counter = 0;
send_uart(&uart, ".", 1);
}
fei.gyro_x_accum += fei.gyro_x;
fei.gyro_y_accum += fei.gyro_y;
fei.gyro_z_accum += fei.gyro_z;
if(fei.total_gyro == 0)
{
fei.gyro_x_min = fei.gyro_x;
fei.gyro_y_min = fei.gyro_y;
fei.gyro_z_min = fei.gyro_z;
fei.gyro_x_max = fei.gyro_x;
fei.gyro_y_max = fei.gyro_y;
fei.gyro_z_max = fei.gyro_z;
}
else
{
fei.gyro_x_min = MIN(fei.gyro_x, fei.gyro_x_min);
fei.gyro_y_min = MIN(fei.gyro_y, fei.gyro_y_min);
fei.gyro_z_min = MIN(fei.gyro_z, fei.gyro_z_min);
fei.gyro_x_max = MAX(fei.gyro_x, fei.gyro_x_max);
fei.gyro_y_max = MAX(fei.gyro_y, fei.gyro_y_max);
fei.gyro_z_max = MAX(fei.gyro_z, fei.gyro_z_max);
}
fei.total_gyro++;
if(ABS(fei.gyro_x_max - fei.gyro_x_min) > GYRO_CENTER_MAX ||
ABS(fei.gyro_y_max - fei.gyro_y_min) > GYRO_CENTER_MAX ||
ABS(fei.gyro_z_max - fei.gyro_z_min) > GYRO_CENTER_MAX)
{
/*
* TRACE2
* print_text(&uart, "center too big ");
* print_number(&uart, ABS(fei.gyro_x_max - fei.gyro_x_min));
* print_number(&uart, ABS(fei.gyro_y_max - fei.gyro_y_min));
* print_number(&uart, ABS(fei.gyro_z_max - fei.gyro_z_min));
*/
fei.total_gyro = 0;
fei.gyro_x_accum = 0;
fei.gyro_y_accum = 0;
fei.gyro_z_accum = 0;
fei.gyro_x_min = 0;
fei.gyro_y_min = 0;
fei.gyro_z_min = 0;
fei.gyro_x_max = 0;
fei.gyro_y_max = 0;
fei.gyro_z_max = 0;
}
else
if(fei.total_gyro >= GYRO_CENTER_TOTAL)
{
fei.prev_gyro_x_center = fei.gyro_x_center;
fei.prev_gyro_y_center = fei.gyro_y_center;
fei.prev_gyro_z_center = fei.gyro_z_center;
fei.gyro_x_center = fei.gyro_x_accum * FRACTION / fei.total_gyro;
fei.gyro_y_center = fei.gyro_y_accum * FRACTION / fei.total_gyro;
fei.gyro_z_center = fei.gyro_z_accum * FRACTION / fei.total_gyro;
// test if calculation didn't drift
print_lf(&uart);
TRACE2
print_text(&uart, "spread=");
print_number(&uart, fei.gyro_x_max - fei.gyro_x_min);
print_number(&uart, fei.gyro_y_max - fei.gyro_y_min);
print_number(&uart, fei.gyro_z_max - fei.gyro_z_min);
print_text(&uart, "center=");
print_number(&uart, fei.gyro_x_center);
print_number(&uart, fei.gyro_y_center);
print_number(&uart, fei.gyro_z_center);
print_text(&uart, "drift=");
print_number(&uart, fei.prev_gyro_x_center - fei.gyro_x_center);
print_number(&uart, fei.prev_gyro_y_center - fei.gyro_y_center);
print_number(&uart, fei.prev_gyro_z_center - fei.gyro_z_center);
if(ABS(fei.prev_gyro_x_center) > 0 &&
ABS(fei.prev_gyro_y_center) > 0 &&
ABS(fei.prev_gyro_z_center) > 0 &&
ABS(fei.prev_gyro_x_center - fei.gyro_x_center) < MAX_GYRO_DRIFT &&
ABS(fei.prev_gyro_y_center - fei.gyro_y_center) < MAX_GYRO_DRIFT &&
ABS(fei.prev_gyro_z_center - fei.gyro_z_center) < MAX_GYRO_DRIFT)
{
TRACE2
print_text(&uart, "got center ");
print_number(&uart, fei.gyro_x_center);
print_number(&uart, fei.gyro_y_center);
print_number(&uart, fei.gyro_z_center);
fei.calibrate_imu = 0;
}
else
// try again
{
print_lf(&uart);
fei.total_gyro = 0;
fei.gyro_x_accum = 0;
fei.gyro_y_accum = 0;
fei.gyro_z_accum = 0;
fei.gyro_x_min = 65535;
fei.gyro_y_min = 65535;
fei.gyro_z_min = 65535;
fei.gyro_x_max = -65535;
fei.gyro_y_max = -65535;
fei.gyro_z_max = -65535;
}
}
// predict gyro accumulation
fei.current_roll = fei.abs_roll;
fei.current_pitch = fei.abs_pitch;
fei.current_heading = 0;
fei.gyro_roll = fei.abs_roll * ANGLE_TO_GYRO;
fei.gyro_pitch = fei.abs_pitch * ANGLE_TO_GYRO;
fei.gyro_heading = 0;
}
else
// not calibrating
{
fei.gyro_x2 = (fei.gyro_x * FRACTION - fei.gyro_x_center) / FRACTION;
fei.gyro_y2 = (fei.gyro_y * FRACTION - fei.gyro_y_center) / FRACTION;
fei.gyro_z2 = (fei.gyro_z * FRACTION - fei.gyro_z_center) / FRACTION;
if(fei.flip)
{
fei.gyro_y2 = -fei.gyro_y2;
fei.gyro_z2 = -fei.gyro_z2;
}
// only use integer part to avoid saturation
fei.gyro_roll += fei.gyro_x2;
fei.gyro_pitch += fei.gyro_y2;
fei.gyro_heading += fei.gyro_z2;
fei.gyro_roll = fix_gyro_angle(fei.gyro_roll);
fei.gyro_pitch = fix_gyro_angle(fei.gyro_pitch);
fei.gyro_heading = fix_gyro_angle(fei.gyro_heading);
// detect overload
if(ABS(fei.gyro_x) >= 32767 ||
ABS(fei.gyro_y) >= 32767 ||
ABS(fei.gyro_z) >= 32767)
{
imu.recover_count = RECOVER_TIME;
}
fei.blend_counter++;
if(fei.blend_counter >= BLEND_DOWNSAMPLE)
{
int blend_factor = ATTITUDE_BLEND;
if(imu.recover_count > 0)
{
blend_factor = ATTITUDE_BLEND2;
imu.recover_count--;
}
fei.blend_counter = 0;
int error = get_angle_change_fixed(fei.gyro_roll / ANGLE_TO_GYRO,
fei.abs_roll);
fei.gyro_roll += error * ANGLE_TO_GYRO / blend_factor;
error = get_angle_change_fixed(fei.gyro_pitch / ANGLE_TO_GYRO,
fei.abs_pitch);
fei.gyro_pitch += error * ANGLE_TO_GYRO / blend_factor;
}
fei.current_roll = fei.gyro_roll / ANGLE_TO_GYRO;
fei.current_pitch = fei.gyro_pitch / ANGLE_TO_GYRO;
fei.current_heading = fei.gyro_heading / ANGLE_TO_GYRO;
fei.current_roll = fix_angle(fei.current_roll);
fei.current_pitch = fix_angle(fei.current_pitch);
fei.current_heading = fix_angle(fei.current_heading);
// debug
fei.imu_count++;
// if(mane_time - fei.debug_time > HZ / 10)
// if(mane_time - fei.debug_time >= HZ)
// {
// TRACE
// print_number(&uart, fei.gyro_x);
// print_number(&uart, fei.gyro_y);
// print_number(&uart, fei.gyro_z);
// print_fixed(&uart, fei.accel_x);
// print_fixed(&uart, fei.accel_y);
// print_fixed(&uart, fei.accel_z);
// print_fixed(&uart, fei.abs_roll);
// print_fixed(&uart, fei.abs_pitch);
// print_fixed(&uart, fei.current_roll);
// print_fixed(&uart, fei.current_pitch);
// print_fixed(&uart, fei.current_heading);
// print_fixed(&uart, fei.current_roll - fei.abs_roll);
// print_fixed(&uart, fei.current_pitch - fei.abs_pitch);
// print_text(&uart, "fei.imu_count=");
// print_number(&uart, fei.imu_count);
// fei.debug_time = mane_time;
// fei.imu_count = 0;
// }
}
}
#endif // BOARD0
#ifdef BOARD2
#define I2C_ADDRESS (0x68 << 1)
#define CLOCK_GPIO GPIOB
#define DATA_GPIO GPIOB
#define CLOCK_PIN GPIO_PIN_10
#define DATA_PIN GPIO_PIN_11
#define I2C_DELAY udelay(1);
I2C_HandleTypeDef I2cHandle;
// returns 1 when the address flag is set
int wait_address_flag()
{
// address sent
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR))
{
//TRACE
/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
return 1;
}
else
{
// ACK failure
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF))
{
TRACE
print_text(&uart, "ACK failure");
/* Generate Stop */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_STOP);
/* Clear AF Flag */
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);
imu.error = 1;
return 1;
}
}
return 0;
}
#ifdef SOFT_I2C
void i2c_write(uint8_t value)
{
int i = 0;
for(i = 0; i < 8; i++)
{
if((value & 0x80))
{
SET_PIN(DATA_GPIO, DATA_PIN);
}
else
{
CLEAR_PIN(DATA_GPIO, DATA_PIN);
}
I2C_DELAY
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
CLEAR_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
value <<= 1;
}
// read ACK
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
// wait for clock to rise
while(!PIN_IS_SET(CLOCK_GPIO, CLOCK_PIN))
{
;
}
int ack = PIN_IS_SET(DATA_GPIO, DATA_PIN);
CLEAR_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
}
void i2c_read(int bytes)
{
int i, j;
for(i = 0; i < bytes; i++)
{
uint8_t value = 0;
/* data must rise before clock to read the byte */
SET_PIN(DATA_GPIO, DATA_PIN);
I2C_DELAY;
for(j = 0; j < 8; j++)
{
value <<= 1;
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY;
while(!PIN_IS_SET(CLOCK_GPIO, CLOCK_PIN))
{
}
value |= PIN_IS_SET(DATA_GPIO, DATA_PIN);
CLEAR_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
}
imu.i2c_buffer[i] = value;
// write ACK
if(i >= bytes - 1)
{
SET_PIN(DATA_GPIO, DATA_PIN);
}
else
{
CLEAR_PIN(DATA_GPIO, DATA_PIN);
}
I2C_DELAY
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
CLEAR_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
}
}
void i2c_start()
{
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
SET_PIN(DATA_GPIO, DATA_PIN);
I2C_DELAY
CLEAR_PIN(DATA_GPIO, DATA_PIN);
I2C_DELAY
CLEAR_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
}
void i2c_stop()
{
CLEAR_PIN(DATA_GPIO, DATA_PIN);
I2C_DELAY
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
I2C_DELAY
SET_PIN(DATA_GPIO, DATA_PIN);
I2C_DELAY
}
#endif // SOFT_I2C
void i2c_read_device(unsigned char reg, int bytes)
{
int i;
for(i = 0; i < bytes; i++)
{
imu.i2c_buffer[i] = 0xff;
}
#ifdef SOFT_I2C
i2c_start();
// write device address & reg
i2c_write(I2C_7BIT_ADD_WRITE(I2C_ADDRESS));
i2c_write(reg);
i2c_start();
i2c_write(I2C_7BIT_ADD_READ(I2C_ADDRESS));
i2c_read(bytes);
i2c_stop();
#else // SOFT_I2C
imu.error = 0;
imu.reg = reg;
imu.bytes = bytes;
imu.current_byte = 0;
while(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY))
{
}
/* Disable Pos */
CLEAR_BIT(I2cHandle.Instance->CR1, I2C_CR1_POS);
/* Enable Acknowledge */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_ACK);
/* Generate Start */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_START);
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB))
{
}
/* Send slave address */
I2cHandle.Instance->DR = I2C_7BIT_ADD_WRITE(I2C_ADDRESS);
while(!wait_address_flag())
{
}
if(imu.error) return;
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE))
{
}
/* Send Memory Address */
I2cHandle.Instance->DR = I2C_MEM_ADD_LSB(imu.reg);
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE))
{
}
/* Generate Restart */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_START);
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB))
{
}
/* Send slave address */
I2cHandle.Instance->DR = I2C_7BIT_ADD_READ(I2C_ADDRESS);
while(!wait_address_flag())
{
}
if(imu.error) return;
if(imu.bytes == 1)
{
/* Disable Acknowledge */
CLEAR_BIT(I2cHandle.Instance->CR1, I2C_CR1_ACK);
/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
/* Generate Stop */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_STOP);
}
else
{
/* Enable Acknowledge */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_ACK);
/* Clear ADDR flag */
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
}
while(imu.current_byte < imu.bytes)
{
// wait for the byte
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_RXNE))
{
}
imu.i2c_buffer[imu.current_byte] = I2cHandle.Instance->DR;
imu.current_byte++;
if(imu.current_byte < imu.bytes)
{
/* Enable Acknowledge */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_ACK);
}
else
{
/* Disable Acknowledge */
CLEAR_BIT(I2cHandle.Instance->CR1, I2C_CR1_ACK);
/* Generate Stop */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_STOP);
}
}
#endif // !SOFT_I2C
}
void i2c_write_device(unsigned char reg, unsigned char value)
{
#ifdef SOFT_I2C
// start
i2c_start();
// write device address
i2c_write(I2C_7BIT_ADD_WRITE(I2C_ADDRESS));
i2c_write(reg);
i2c_write(value);
i2c_stop();
#else // SOFT_I2C
imu.error = 0;
imu.reg = reg;
imu.bytes = 1;
imu.current_byte = 0;
imu.i2c_buffer[0] = value;
while(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY))
{
}
//TRACE
/* Disable Pos */
CLEAR_BIT(I2cHandle.Instance->CR1, I2C_CR1_POS);
/* Generate Start */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_START);
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_SB))
{
}
//TRACE
/* Send slave address */
I2cHandle.Instance->DR = I2C_7BIT_ADD_WRITE(I2C_ADDRESS);
while(!wait_address_flag())
{
}
if(imu.error) return;
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE))
{
}
/* Send Memory Address */
I2cHandle.Instance->DR = I2C_MEM_ADD_LSB(imu.reg);
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE))
{
}
/* Write data to DR */
I2cHandle.Instance->DR = imu.i2c_buffer[imu.current_byte];
while(!__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE))
{
}
/* Generate Stop */
SET_BIT(I2cHandle.Instance->CR1, I2C_CR1_STOP);
#endif // !SOFT_I2C
}
void imu_idle()
{
}
void test_status();
void send_imu_result()
{
int i;
//TRACE
// send data to BOARD1
int size = IMU_PACKET_SIZE;
unsigned char buffer[size];
buffer[0] = 0xff;
buffer[1] = SYNC_CODE;
memcpy(buffer + 2, imu.i2c_buffer, 14);
buffer[16] = hall.value & 0xff;
buffer[17] = (hall.value >> 8) & 0xff;
buffer[18] = 0;
buffer[19] = 0;
send_uart(&uart, buffer, size);
// TRACE
/*
* for(i = 0; i < 14; i++)
* {
* print_hex2(&uart, imu.i2c_buffer[i]);
* }
*/
/*
* int offset = 0;
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
* offset += 2;
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
* print_number(&uart, READ_INT16BE(imu.i2c_buffer, offset));
*/
imu.count++;
if(mane_time - imu.time2 >= HZ)
{
// TRACE
// print_text(&uart, "imu.count=");
// print_number(&uart, imu.count);
// print_text(&uart, "hall.count=");
// print_number(&uart, hall.count);
// print_text(&uart, "imu.count2=");
// print_number(&uart, imu.count2);
imu.count = 0;
imu.count2 = 0;
hall.count = 0;
imu.time2 = mane_time;
}
// imu.got_readout = 1;
// test_status();
}
#if 0
void test_status3()
{
i2c_read_device(0x3b, 14);
send_imu_result();
}
void test_status2()
{
//TRACE
//print_hex2(&uart, imu.i2c_buffer[0]);
if((imu.i2c_buffer[0] & 0x1))
{
imu.time = mane_time;
imu.imu_function = test_status3;
}
else
{
test_status();
}
}
void test_status1()
{
i2c_read_device(0x3a, 1);
imu.imu_function = test_status2;
}
// set the function pointer, but don't read I2C
void test_status()
{
//TRACE
imu.time = mane_time;
imu.imu_function = test_status1;
}
#endif // 0
// get 1 readout
void do_imu()
{
while(1)
{
i2c_read_device(0x3a, 1);
if((imu.i2c_buffer[0] & 0x1))
{
i2c_read_device(0x3b, 14);
send_imu_result();
break;
}
}
}
void imu_init3()
{
//TRACE
// sample rate divider
// 2khz is as fast as the UART pipeline can go
// i2c_write_device(0x19, 3);
// 1khz
// i2c_write_device(0x19, 6);
// minimum latency
i2c_write_device(0x19, 0);
// imu.imu_function = test_status;
imu.initialized = 1;
}
void imu_init2()
{
//TRACE
// accel config accel scale
i2c_write_device(0x1c, 0x00);
imu.imu_function = imu_init3;
}
void imu_init1()
{
//TRACE
// gyro config gyro scale
i2c_write_device(0x1b, 0x0);
imu.imu_function = imu_init2;
}
void imu_init0()
{
// wait for the other boards to start
if(mane_time - imu.time > HZ / 10)
{
//TRACE
// sleep mode & clock source
i2c_write_device(0x6b, 1);
imu.imu_function = imu_init1;
}
}
#ifdef SOFT_I2C
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
}
#else // SOFT_I2C
// callback from HAL_I2C_Init
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* Enable I2Cx clock */
__HAL_RCC_I2C2_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* I2C CLK GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
// GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C DATA GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*##-3- Configure the NVIC for I2C ########################################*/
/* NVIC for I2Cx */
// HAL_NVIC_SetPriority(I2C2_ER_IRQn, 0, 1);
// HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
// HAL_NVIC_SetPriority(I2C2_EV_IRQn, 0, 2);
// HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
}
#endif // !SOFT_I2C
void init_imu()
{
#ifdef SOFT_I2C
GPIO_InitTypeDef GPIO_InitStruct;
/* Enable GPIO TX/RX clock */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* I2C CLK GPIO pin configuration */
GPIO_InitStruct.Pin = CLOCK_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(CLOCK_GPIO, &GPIO_InitStruct);
/* I2C DATA GPIO pin configuration */
GPIO_InitStruct.Pin = DATA_PIN;
HAL_GPIO_Init(DATA_GPIO, &GPIO_InitStruct);
SET_PIN(CLOCK_GPIO, CLOCK_PIN);
SET_PIN(DATA_GPIO, DATA_PIN);
#else // SOFT_I2C
I2cHandle.Instance = I2C2;
// dutycycle 2 can do 923076Hz or 1Mhz
I2cHandle.Init.ClockSpeed = 1000000;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
// dutycycle 16:9 can do 720khz or 1440000Hz
// I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;