text
stringlengths
0
234
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