embedded_components_0.2.0_37c39b23/src/motion/l3gd20/l3gd20.adb

  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
------------------------------------------------------------------------------
--                                                                          --
--                    Copyright (C) 2015, AdaCore                           --
--                                                                          --
--  Redistribution and use in source and binary forms, with or without      --
--  modification, are permitted provided that the following conditions are  --
--  met:                                                                    --
--     1. Redistributions of source code must retain the above copyright    --
--        notice, this list of conditions and the following disclaimer.     --
--     2. Redistributions in binary form must reproduce the above copyright --
--        notice, this list of conditions and the following disclaimer in   --
--        the documentation and/or other materials provided with the        --
--        distribution.                                                     --
--     3. Neither the name of STMicroelectronics nor the names of its       --
--        contributors may be used to endorse or promote products derived   --
--        from this software without specific prior written permission.     --
--                                                                          --
--   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    --
--   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      --
--   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  --
--   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   --
--   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, --
--   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       --
--   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  --
--   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  --
--   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    --
--   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  --
--   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   --
--                                                                          --
--                                                                          --
--  This file is based on:                                                  --
--                                                                          --
--   @file    l3gd20.c                                                      --
--   @author  MCD Application Team                                          --
--   @version V1.1.0                                                        --
--   @date    19-June-2014                                                  --
--   @brief   This file provides a set of functions needed to manage the    --
--            L3GD20, ST MEMS motion sensor,  3-axis digital output         --
--            gyroscope.                                                    --
--                                                                          --
--   COPYRIGHT(c) 2014 STMicroelectronics                                   --
------------------------------------------------------------------------------

with Ada.Unchecked_Conversion;
with System;

package body L3GD20 is

   --  the following are per Table 4 of the L3GD20 Datasheet, pg 9.
   --  the values are millidegrees per second, so we scale accordingly
   Sensitivity_250dps  : constant := 8.75 * 0.001; -- mdps/digit
   Sensitivity_500dps  : constant := 17.5 * 0.001; -- mdps/digit
   Sensitivity_2000dps : constant := 70.0 * 0.001; -- mdps/digit

   ReadWrite_CMD : constant := 16#80#;
   MultiUInt8_CMD       : constant := 16#40#;

   --  bit definitions for the CTRL_REG3 register

   INT1_Interrupt_Enable            : constant := 2#1000_0000#;
   Interrupt_Pin_Mode_Bit           : constant := 2#0001_0000#;
   INT2_Data_Ready_Interrupt_Enable : constant := 2#0000_1000#;
   INT2_Watermark_Interrupt_Enable  : constant := 2#0000_0100#;
   INT2_Overrun_Interrupt_Enable    : constant := 2#0000_0010#;
   INT2_FIFO_Empty_Interrupt_Enable : constant := 2#0000_0001#;

   --  bit definitions for the CTRL_REG4 register

   Endian_Selection_Mask    : constant := 2#0100_0000#;
   Fullscale_Selection_Bits : constant := 2#0011_0000#;

   --  bit definitions for the CTRL_REG5 register

   Boot_Bit               : constant := 2#1000_0000#;
   FIFO_Enable_Bit        : constant := 2#0100_0000#;
   HighPass_Filter_Enable : constant := 2#0001_0000#;
   LowPass_Filter_Enable  : constant := 2#0000_0010#;

   --  bit definitions for the FIFO_CTRL register

   FIFO_Mode_Bits           : constant := 2#1110_0000#;
   Watermark_Threshold_Bits : constant := 2#0001_1111#;

   --  bit definitions for the FIFO_SRC register

   Watermark_Status_Bit : constant := 2#1000_0000#;
   FIFO_Overrun_Bit     : constant := 2#0100_0000#;
   FIFO_Empty_Bit       : constant := 2#0010_0000#;
   FIFO_Depth_Bits      : constant := 2#0001_1111#;

   --  bit definitions for the INT1_CFG register

   Logically_And_Or_Events_Bit : constant := 2#1000_0000#;
   Int1_Latch_Enable_Bit       : constant := 2#0100_0000#;

   Axes_Interrupt_Enablers : constant array (Axes_Interrupts) of UInt8 :=
     (Z_High_Interrupt => 2#0010_0000#,
      Z_Low_Interrupt  => 2#0001_0000#,
      Y_High_Interrupt => 2#0000_1000#,
      Y_Low_Interrupt  => 2#0000_0100#,
      X_High_Interrupt => 2#0000_0010#,
      X_Low_Interrupt  => 2#0000_0001#);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => High_Pass_Filter_Mode, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => High_Pass_Cutoff_Frequency, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Power_Mode_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Output_Data_Rate_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Axes_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Bandwidth_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Block_Data_Update_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Endian_Data_Selection, Target => UInt8);

   function As_UInt8 is new Ada.Unchecked_Conversion
     (Source => Full_Scale_Selection, Target => UInt8);

   type Angle_Rate_Pointer is access all Angle_Rate with Storage_Size => 0;

   function As_Angle_Rate_Pointer is new Ada.Unchecked_Conversion
     (Source => System.Address, Target => Angle_Rate_Pointer);
   --  So that we can treat the address of a UInt8 as a pointer to a two-UInt8
   --  sequence representing a signed integer quantity.

   procedure Swap2 (Location : System.Address) with Inline;
   --  Swaps the two UInt8s at Location and Location+1

   procedure SPI_Mode (This : Three_Axis_Gyroscope; Enabled : Boolean);
   --  Enable or disable SPI mode communication with the device. This is named
   --  "chip select" in other demos/examples.

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (This        : in out Three_Axis_Gyroscope;
      Port        : Any_SPI_Port;
      Chip_Select : Any_GPIO_Point)
   is
   begin
      This.Port := Port;
      This.CS := Chip_Select;

      SPI_Mode (This, Enabled => False);
   end Initialize;

   -----------------
   -- SPI_Mode --
   -----------------

   procedure SPI_Mode (This : Three_Axis_Gyroscope; Enabled : Boolean) is
      --  When the pin is low (cleared), the device is in SPI mode.
      --  When the pin is high (set), the device is in I2C mode.
      --  We want SPI mode communication, so Enabled, when True,
      --  means we must drive the pin low.
   begin
      if Enabled then
         This.CS.Clear;
      else
         This.CS.Set;
      end if;
   end SPI_Mode;

   -----------
   -- Write --
   -----------

   procedure Write (This : Three_Axis_Gyroscope; Addr : Register;  Data : UInt8)
   is
      Status : SPI_Status;
   begin
      SPI_Mode (This, Enabled => True);

      This.Port.Transmit (SPI_Data_8b'(1 => UInt8 (Addr)), Status);
      if Status /= Ok then
         raise Program_Error;
      end if;

      This.Port.Transmit (SPI_Data_8b'(1 => Data), Status);
      if Status /= Ok then
         raise Program_Error;
      end if;

      SPI_Mode (This, Enabled => False);
   end Write;

   ----------
   -- Read --
   ----------

   procedure Read
     (This : Three_Axis_Gyroscope;
      Addr : Register;
      Data : out UInt8)
   is
      Status : SPI_Status;
      Tmp_Data : SPI_Data_8b (1 .. 1);
   begin
      SPI_Mode (This, Enabled => True);

      This.Port.Transmit (SPI_Data_8b'(1 => UInt8 (Addr) or ReadWrite_CMD),
                         Status);
      if Status /= Ok then
         raise Program_Error;
      end if;

      This.Port.Receive (Tmp_Data, Status);
      if Status /= Ok then
         raise Program_Error;
      end if;
      Data := Tmp_Data (Tmp_Data'First);

      SPI_Mode (This, Enabled => False);
   end Read;

   ----------------
   -- Read_UInt8s --
   ----------------

   procedure Read_UInt8s
     (This   : Three_Axis_Gyroscope;
      Addr   : Register;
      Buffer : out SPI_Data_8b;
      Count  : Natural)
   is
      Index : Natural := Buffer'First;
      Status : SPI_Status;
      Tmp_Data : SPI_Data_8b (1 .. 1);
   begin
      SPI_Mode (This, Enabled => True);

      This.Port.Transmit
        (SPI_Data_8b'(1 => UInt8 (Addr) or ReadWrite_CMD or MultiUInt8_CMD),
         Status);

      if Status /= Ok then
         raise Program_Error;
      end if;

      for K in 1 .. Count loop
         This.Port.Receive (Tmp_Data, Status);
         if Status /= Ok then
            raise Program_Error;
         end if;
         Buffer (Index) := Tmp_Data (Tmp_Data'First);
         Index := Index + 1;
      end loop;

      SPI_Mode (This, Enabled => False);
   end Read_UInt8s;

   ---------------
   -- Configure --
   ---------------

   procedure Configure
     (This             : in out Three_Axis_Gyroscope;
      Power_Mode       : Power_Mode_Selection;
      Output_Data_Rate : Output_Data_Rate_Selection;
      Axes_Enable      : Axes_Selection;
      Bandwidth        : Bandwidth_Selection;
      BlockData_Update : Block_Data_Update_Selection;
      Endianness       : Endian_Data_Selection;
      Full_Scale       : Full_Scale_Selection)
   is
      Ctrl1 : UInt8;
      Ctrl4 : UInt8;
   begin
      Ctrl1 := As_UInt8 (Power_Mode)       or
               As_UInt8 (Output_Data_Rate) or
               As_UInt8 (Axes_Enable)      or
               As_UInt8 (Bandwidth);

      Ctrl4 := As_UInt8 (BlockData_Update) or
               As_UInt8 (Endianness)       or
               As_UInt8 (Full_Scale);

      Write (This, CTRL_REG1, Ctrl1);
      Write (This, CTRL_REG4, Ctrl4);
   end Configure;

   -----------
   -- Sleep --
   -----------

   procedure Sleep (This : in out Three_Axis_Gyroscope) is
      Ctrl1      : UInt8;
      Sleep_Mode : constant := 2#1000#;  -- per the Datasheet, Table 22, pg 32
   begin
      Read (This, CTRL_REG1, Ctrl1);
      Ctrl1 := Ctrl1 or Sleep_Mode;
      Write (This, CTRL_REG1, Ctrl1);
   end Sleep;

   --------------------------------
   -- Configure_High_Pass_Filter --
   --------------------------------

   procedure Configure_High_Pass_Filter
     (This             : in out Three_Axis_Gyroscope;
      Mode_Selection   : High_Pass_Filter_Mode;
      Cutoff_Frequency : High_Pass_Cutoff_Frequency)
   is
      Ctrl2 : UInt8;
   begin
      --  note that the two high-order bits must remain zero, per the datasheet
      Ctrl2 := As_UInt8 (Mode_Selection) or As_UInt8 (Cutoff_Frequency);
      Write (This, CTRL_REG2, Ctrl2);
   end Configure_High_Pass_Filter;

   -----------------------------
   -- Enable_High_Pass_Filter --
   -----------------------------

   procedure Enable_High_Pass_Filter (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      --  set HPen bit
      Ctrl5 := Ctrl5 or HighPass_Filter_Enable;
      Write (This, CTRL_REG5, Ctrl5);
   end Enable_High_Pass_Filter;

   ------------------------------
   -- Disable_High_Pass_Filter --
   ------------------------------

   procedure Disable_High_Pass_Filter (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      --  clear HPen bit
      Ctrl5 := Ctrl5 and (not HighPass_Filter_Enable);
      Write (This, CTRL_REG5, Ctrl5);
   end Disable_High_Pass_Filter;

   ----------------------------
   -- Enable_Low_Pass_Filter --
   ----------------------------

   procedure Enable_Low_Pass_Filter (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      Ctrl5 := Ctrl5 or LowPass_Filter_Enable;
      Write (This, CTRL_REG5, Ctrl5);
   end Enable_Low_Pass_Filter;

   -----------------------------
   -- Disable_Low_Pass_Filter --
   -----------------------------

   procedure Disable_Low_Pass_Filter (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      --  clear HPen bit
      Ctrl5 := Ctrl5 and (not LowPass_Filter_Enable);
      Write (This, CTRL_REG5, Ctrl5);
   end Disable_Low_Pass_Filter;

   ---------------------
   -- Reference_Value --
   ---------------------

   function Reference_Value (This : Three_Axis_Gyroscope) return UInt8 is
      Result : UInt8;
   begin
      Read (This, Reference, Result);
      return Result;
   end Reference_Value;

   -------------------
   -- Set_Reference --
   -------------------

   procedure Set_Reference (This : in out Three_Axis_Gyroscope; Value : UInt8) is
   begin
      Write (This, Reference, Value);
   end Set_Reference;

   -----------------
   -- Data_Status --
   -----------------

   function Data_Status (This : Three_Axis_Gyroscope) return Gyro_Data_Status is
      Result : UInt8;
      function As_Gyro_Data_Status is
        new Ada.Unchecked_Conversion (Source => UInt8,
                                      Target => Gyro_Data_Status);
   begin
      Read (This, Status, Result);
      return As_Gyro_Data_Status (Result);
   end Data_Status;

   ---------------
   -- Device_Id --
   ---------------

   function Device_Id (This : Three_Axis_Gyroscope) return UInt8 is
      Result : UInt8;
   begin
      Read (This, Who_Am_I, Result);
      return Result;
   end Device_Id;

   -----------------
   -- Temperature --
   -----------------

   function Temperature (This : Three_Axis_Gyroscope) return UInt8 is
      Result : UInt8;
   begin
      Read (This, OUT_Temp, Result);
      return Result;
   end Temperature;

   ------------
   -- Reboot --
   ------------

   procedure Reboot (This : Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      --  set the boot bit
      Ctrl5 := Ctrl5 or Boot_Bit;
      Write (This, CTRL_REG5, Ctrl5);
   end Reboot;

   ----------------------------
   -- Full_Scale_Sensitivity --
   ----------------------------

   function Full_Scale_Sensitivity (This : Three_Axis_Gyroscope) return Float is
      Ctrl4  : UInt8;
      Result : Float;
      Fullscale_Selection : UInt8;
   begin
      Read (This, CTRL_REG4, Ctrl4);

      Fullscale_Selection := Ctrl4 and Fullscale_Selection_Bits;

      if Fullscale_Selection = L3GD20_Fullscale_250'Enum_Rep then
         Result := Sensitivity_250dps;
      elsif Fullscale_Selection = L3GD20_Fullscale_500'Enum_Rep then
         Result := Sensitivity_500dps;
      else
         Result := Sensitivity_2000dps;
      end if;

      return Result;
   end Full_Scale_Sensitivity;

   -------------------------
   -- Get_Raw_Angle_Rates --
   -------------------------

   procedure Get_Raw_Angle_Rates
     (This  : Three_Axis_Gyroscope;
      Rates : out Angle_Rates)
   is

      Ctrl4 : UInt8;

      UInt8s_To_Read : constant Integer := 6;  -- ie, three 2-UInt8 integers
      --  the number of UInt8s in an Angle_Rates record object

      Received : SPI_Data_8b (1 .. UInt8s_To_Read);

   begin
      Read (This, CTRL_REG4, Ctrl4);

      Read_UInt8s (This, OUT_X_L, Received, UInt8s_To_Read);
      --  The above has the effect of separate reads, as follows:
      --    Read (This, OUT_X_L, Received (1));
      --    Read (This, OUT_X_H, Received (2));
      --    Read (This, OUT_Y_L, Received (3));
      --    Read (This, OUT_Y_H, Received (4));
      --    Read (This, OUT_Z_L, Received (5));
      --    Read (This, OUT_Z_H, Received (6));

      if (Ctrl4 and Endian_Selection_Mask) = L3GD20_Big_Endian'Enum_Rep then
         Swap2 (Received (1)'Address);
         Swap2 (Received (3)'Address);
         Swap2 (Received (5)'Address);
      end if;

      Rates.X := As_Angle_Rate_Pointer (Received (1)'Address).all;
      Rates.Y := As_Angle_Rate_Pointer (Received (3)'Address).all;
      Rates.Z := As_Angle_Rate_Pointer (Received (5)'Address).all;
   end Get_Raw_Angle_Rates;

   --------------------------
   -- Configure_Interrupt1 --
   --------------------------

   procedure Configure_Interrupt1
     (This           : in out Three_Axis_Gyroscope;
      Triggers       : Threshold_Event_List;
      Latched        : Boolean := False;
      Active_Edge    : Interrupt1_Active_Edge := L3GD20_Interrupt1_High_Edge;
      Combine_Events : Boolean := False;
      Sample_Count   : Sample_Counter := 0)
   is
      Config : UInt8;
      Ctrl3  : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and 16#DF#;
      Ctrl3 := Ctrl3 or Active_Edge'Enum_Rep;
      Ctrl3 := Ctrl3 or INT1_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);

      if Sample_Count > 0 then
         Set_Duration_Counter (This, Sample_Count);
      end if;

      Config := 0;

      if Latched then
         Config := Config or Int1_Latch_Enable_Bit;
      end if;

      if Combine_Events then
         Config := Config or Logically_And_Or_Events_Bit;
      end if;

      for Event of Triggers loop
         Config := Config or Axes_Interrupt_Enablers (Event.Axis);
      end loop;

      Write (This, INT1_CFG, Config);

      for Event of Triggers loop
         Set_Threshold (This, Event.Axis, Event.Threshold);
      end loop;
   end Configure_Interrupt1;

   ----------------------------
   -- Set_Interrupt_Pin_Mode --
   ----------------------------

   procedure Set_Interrupt_Pin_Mode
     (This : in out Three_Axis_Gyroscope;
      Mode : Pin_Modes)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not Interrupt_Pin_Mode_Bit);
      Ctrl3 := Ctrl3 or Mode'Enum_Rep;
      Write (This, CTRL_REG3, Ctrl3);
   end Set_Interrupt_Pin_Mode;

   -----------------------
   -- Enable_Interrupt1 --
   -----------------------

   procedure Enable_Interrupt1 (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 or INT1_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);
   end Enable_Interrupt1;

   ------------------------
   -- Disable_Interrupt1 --
   ------------------------

   procedure Disable_Interrupt1 (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not INT1_Interrupt_Enable);
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_Interrupt1;

   -----------------------
   -- Interrupt1_Status --
   -----------------------

   function Interrupt1_Source (This : Three_Axis_Gyroscope) return Interrupt1_Sources is
      Result : UInt8;
      function As_Interrupt_Source is new Ada.Unchecked_Conversion
        (Source => UInt8, Target => Interrupt1_Sources);
   begin
      Read (This, INT1_SRC, Result);
      return As_Interrupt_Source (Result);
   end Interrupt1_Source;

   --------------------------
   -- Set_Duration_Counter --
   --------------------------

   procedure Set_Duration_Counter
     (This  : in out Three_Axis_Gyroscope;
      Value : Sample_Counter)
   is
      Wait_Bit : constant := 2#1000_0000#;
   begin
      Write (This, INT1_Duration, UInt8 (Value) or Wait_Bit);
   end Set_Duration_Counter;

   ------------------
   -- Enable_Event --
   ------------------

   procedure Enable_Event
     (This  : in out Three_Axis_Gyroscope;
      Event : Axes_Interrupts)
   is
      Config : UInt8;
   begin
      Read (This, INT1_CFG, Config);
      Config := Config or Axes_Interrupt_Enablers (Event);
      Write (This, INT1_CFG, Config);
   end Enable_Event;

   -------------------
   -- Disable_Event --
   -------------------

   procedure Disable_Event
     (This  : in out Three_Axis_Gyroscope;
      Event : Axes_Interrupts)
   is
      Config : UInt8;
   begin
      Read (This, INT1_CFG, Config);
      Config := Config and (not Axes_Interrupt_Enablers (Event));
      Write (This, INT1_CFG, Config);
   end Disable_Event;

   -------------------
   -- Set_Threshold --
   -------------------

   procedure Set_Threshold
     (This  : in out Three_Axis_Gyroscope;
      Event : Axes_Interrupts;
      Value : Axis_Sample_Threshold)
   is
   begin
      case Event is
         when Z_High_Interrupt | Z_Low_Interrupt =>
            Write (This, INT1_TSH_ZL, UInt8 (Value));
            Write (This, INT1_TSH_ZH, UInt8 (Shift_Right (Value, 8)));

         when Y_High_Interrupt | Y_Low_Interrupt =>
            Write (This, INT1_TSH_YL, UInt8 (Value));
            Write (This, INT1_TSH_YH, UInt8 (Shift_Right (Value, 8)));

         when X_High_Interrupt | X_Low_Interrupt =>
            Write (This, INT1_TSH_XL, UInt8 (Value));
            Write (This, INT1_TSH_XH, UInt8 (Shift_Right (Value, 8)));
      end case;
   end Set_Threshold;

   -------------------
   -- Set_FIFO_Mode --
   -------------------

   procedure Set_FIFO_Mode
     (This : in out Three_Axis_Gyroscope;
      Mode : FIFO_Modes)
   is
      FIFO  : UInt8;
   begin
      Read (This, FIFO_CTRL, FIFO);
      FIFO := FIFO and (not FIFO_Mode_Bits);  -- clear the current bits
      FIFO := FIFO or Mode'Enum_Rep;
      Write (This, FIFO_CTRL, FIFO);
   end Set_FIFO_Mode;

   -----------------
   -- Enable_FIFO --
   -----------------

   procedure Enable_FIFO (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      Ctrl5 := Ctrl5 or FIFO_Enable_Bit;
      Write (This, CTRL_REG5, Ctrl5);
   end Enable_FIFO;

   ------------------
   -- Disable_FIFO --
   ------------------

   procedure Disable_FIFO (This : in out Three_Axis_Gyroscope) is
      Ctrl5 : UInt8;
   begin
      Read (This, CTRL_REG5, Ctrl5);
      Ctrl5 := Ctrl5 and (not FIFO_Enable_Bit);
      Write (This, CTRL_REG5, Ctrl5);
   end Disable_FIFO;

   ------------------------
   -- Set_FIFO_Watermark --
   ------------------------

   procedure Set_FIFO_Watermark
     (This  : in out Three_Axis_Gyroscope;
      Level : FIFO_Level)
   is
      Value : UInt8;
   begin
      Read (This, FIFO_CTRL, Value);
      Value := Value and (not Watermark_Threshold_Bits); -- clear the bits
      Value := Value or UInt8 (Level);
      Write (This, FIFO_CTRL, Value);
   end Set_FIFO_Watermark;

   ------------------------------
   -- Get_Raw_Angle_Rates_FIFO --
   ------------------------------

   procedure Get_Raw_Angle_Rates_FIFO
     (This   : in out Three_Axis_Gyroscope;
      Buffer : out Angle_Rates_FIFO_Buffer)
   is
      Ctrl4 : UInt8;

      Angle_Rate_Size : constant Integer := 6;  -- UInt8s
      UInt8s_To_Read   : constant Integer := Buffer'Length * Angle_Rate_Size;
      Received        : SPI_Data_8b (0 .. UInt8s_To_Read - 1)
        with Alignment => 2;
   begin
      Read (This, CTRL_REG4, Ctrl4);

      Read_UInt8s (This, OUT_X_L, Received, UInt8s_To_Read);

      if (Ctrl4 and Endian_Selection_Mask) = L3GD20_Big_Endian'Enum_Rep then
         declare
            J : Integer := 0;
         begin
            for K in Received'First .. Received'Last / 2 loop
               Swap2 (Received (J)'Address);
               J := J + 2;
            end loop;
         end;
      end if;

      declare
         J : Integer := 0;
      begin
         for K in Buffer'Range loop
            Buffer (K).X := As_Angle_Rate_Pointer (Received (J)'Address).all;
            J := J + 2;
            Buffer (K).Y := As_Angle_Rate_Pointer (Received (J)'Address).all;
            J := J + 2;
            Buffer (K).Z := As_Angle_Rate_Pointer (Received (J)'Address).all;
            J := J + 2;
         end loop;
      end;
   end Get_Raw_Angle_Rates_FIFO;

   ------------------------
   -- Current_FIFO_Depth --
   ------------------------

   function Current_FIFO_Depth (This : Three_Axis_Gyroscope) return FIFO_Level is
      Result : UInt8;
   begin
      Read (This, FIFO_SRC, Result);
      Result := Result and FIFO_Depth_Bits;
      return FIFO_Level (Result);
   end Current_FIFO_Depth;

   --------------------------
   -- FIFO_Below_Watermark --
   --------------------------

   function FIFO_Below_Watermark (This : Three_Axis_Gyroscope) return Boolean is
      Result : UInt8;
   begin
      Read (This, FIFO_SRC, Result);
      Result := Result and Watermark_Status_Bit;
      return Result = 0;
   end FIFO_Below_Watermark;

   ----------------
   -- FIFO_Empty --
   ----------------

   function FIFO_Empty (This : Three_Axis_Gyroscope) return Boolean is
      Result : UInt8;
   begin
      Read (This, FIFO_SRC, Result);
      Result := Result and FIFO_Empty_Bit;
      return Result = FIFO_Empty_Bit;
   end FIFO_Empty;

   ------------------
   -- FIFO_Overrun --
   ------------------

   function FIFO_Overrun (This : Three_Axis_Gyroscope) return Boolean is
      Result : UInt8;
   begin
      Read (This, FIFO_SRC, Result);
      Result := Result and FIFO_Overrun_Bit;
      return Result = FIFO_Overrun_Bit;
   end FIFO_Overrun;

   ---------------------------------
   -- Enable_Data_Ready_Interrupt --
   ---------------------------------

   procedure Enable_Data_Ready_Interrupt (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 or INT2_Data_Ready_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);
   end Enable_Data_Ready_Interrupt;

   ----------------------------------
   -- Disable_Data_Ready_Interrupt --
   ----------------------------------

   procedure Disable_Data_Ready_Interrupt (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not INT2_Data_Ready_Interrupt_Enable);
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_Data_Ready_Interrupt;

   -------------------------------------
   -- Enable_FIFO_Watermark_Interrupt --
   -------------------------------------

   procedure Enable_FIFO_Watermark_Interrupt (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 or INT2_Watermark_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);
   end Enable_FIFO_Watermark_Interrupt;

   -----------------------------
   -- Disable_Int1_Interrupts --
   -----------------------------

   procedure Disable_Int1_Interrupts (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and 2#0001_1111#;
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_Int1_Interrupts;

   -----------------------------
   -- Disable_Int2_Interrupts --
   -----------------------------

   procedure Disable_Int2_Interrupts (This : in out Three_Axis_Gyroscope) is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and 2#1111_0000#;
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_Int2_Interrupts;

   --------------------------------------
   -- Disable_FIFO_Watermark_Interrupt --
   --------------------------------------

   procedure Disable_FIFO_Watermark_Interrupt
     (This : in out Three_Axis_Gyroscope)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not INT2_Watermark_Interrupt_Enable);
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_FIFO_Watermark_Interrupt;

   -----------------------------------
   -- Enable_FIFO_Overrun_Interrupt --
   -----------------------------------

   procedure Enable_FIFO_Overrun_Interrupt
     (This : in out Three_Axis_Gyroscope)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 or INT2_Overrun_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);
   end Enable_FIFO_Overrun_Interrupt;

   ------------------------------------
   -- Disable_FIFO_Overrun_Interrupt --
   ------------------------------------

   procedure Disable_FIFO_Overrun_Interrupt
     (This : in out Three_Axis_Gyroscope)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not INT2_Overrun_Interrupt_Enable);
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_FIFO_Overrun_Interrupt;

   ---------------------------------
   -- Enable_FIFO_Empty_Interrupt --
   ---------------------------------

   procedure Enable_FIFO_Empty_Interrupt
     (This : in out Three_Axis_Gyroscope)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 or INT2_FIFO_Empty_Interrupt_Enable;
      Write (This, CTRL_REG3, Ctrl3);
   end Enable_FIFO_Empty_Interrupt;

   ----------------------------------
   -- Disable_FIFO_Empty_Interrupt --
   ----------------------------------

   procedure Disable_FIFO_Empty_Interrupt
     (This : in out Three_Axis_Gyroscope)
   is
      Ctrl3 : UInt8;
   begin
      Read (This, CTRL_REG3, Ctrl3);
      Ctrl3 := Ctrl3 and (not INT2_FIFO_Empty_Interrupt_Enable);
      Write (This, CTRL_REG3, Ctrl3);
   end Disable_FIFO_Empty_Interrupt;

   -----------
   -- Swap2 --
   -----------

   procedure Swap2 (Location : System.Address) is
      X : UInt16;
      for X'Address use Location;
      function Bswap_16 (X : UInt16) return UInt16;
      pragma Import (Intrinsic, Bswap_16, "__builtin_bswap16");
   begin
      X := Bswap_16 (X);
   end Swap2;

   -----------
   -- Reset --
   -----------

   procedure Reset (This : in out Three_Axis_Gyroscope) is
   begin
      --  per the Datasheet, Table 17, pg 29
      Write (This, CTRL_REG1, 2#0000_0111#);
      Write (This, CTRL_REG2, 0);
      Write (This, CTRL_REG3, 0);
      Write (This, CTRL_REG4, 0);
      Write (This, CTRL_REG5, 0);
      Write (This, Reference, 0);
      Write (This, FIFO_CTRL, 0);
      Write (This, INT1_CFG, 0);
      Write (This, INT1_TSH_ZL, 0);
      Write (This, INT1_TSH_ZH, 0);
      Write (This, INT1_TSH_YL, 0);
      Write (This, INT1_TSH_YH, 0);
      Write (This, INT1_TSH_XL, 0);
      Write (This, INT1_TSH_XH, 0);
      Write (This, INT1_Duration, 0);
   end Reset;

end L3GD20;