Put I2C functions into a separate file.
7 files changed, 356 insertions(+), 226 deletions(-)

M nfc_tags.adb
M pn532.adb
A => pn532_bus_i2c.adb
A => pn532_bus_i2c.ads
M pn532_twi.adb
M pn532_twi.ads
A => pn532_types.ads
M nfc_tags.adb +23 -22
@@ 24,24 24,25 @@ with AVR.Interrupts;
 with Interfaces;
 with Ada.Characters.Latin_1;
 with AVR.Programspace;
-
+with PN532_Types;
 
 
 package body NFC_Tags is
    use Interfaces;
-   
+   use PN532_Types;
+
    package PN532 renames PN532_TWI;
 
    procedure Print_Str (Place : System.Address);
 
-   procedure Read_Mifare_Classic_Tag (NFC_ID : PN532.PN532_Buf) is
+   procedure Read_Mifare_Classic_Tag (NFC_ID : PN532_Buf) is
       Status : Boolean;
-      Key_A : PN532.PN532_Buf_6 := (255, 255, 255, 255, 255, 255);
-      MAD_Key_A : PN532.PN532_Buf_6 :=
+      Key_A : PN532_Buf_6 := (255, 255, 255, 255, 255, 255);
+      MAD_Key_A : PN532_Buf_6 :=
         (16#A0#, 16#A1#,  16#A2#, 16#A3#, 16#A4#, 16#A5#);
-      Key_B : PN532.PN532_Buf_6 := (255, 255, 255, 255, 255, 255);
-      Data : PN532.PN532_Buf (1 .. 16);
-      NFC_Data : PN532.PN532_Buf (1 .. 48);
+      Key_B : PN532_Buf_6 := (255, 255, 255, 255, 255, 255);
+      Data : PN532_Buf (1 .. 16);
+      NFC_Data : PN532_Buf (1 .. 48);
       Len : Unsigned_8;
    begin
       AVR.UART.Put ("UID len: ");

          
@@ 133,8 134,8 @@ package body NFC_Tags is
       Card_Block_Count : Unsigned_8;
       Card_Size : Unsigned_16;
       Status : Boolean;
-      Data : PN532.PN532_Buf (1 .. 16);
-      NFC_Data : PN532.PN532_Buf (1 .. 48);
+      Data : PN532_Buf (1 .. 16);
+      NFC_Data : PN532_Buf (1 .. 48);
       NDEF_Loc : Unsigned_8 := 0;
    begin
       Print_Str (PM_Strings.Type_2_Tag_PM'Address); AVR.UART.CRLF;

          
@@ 212,11 213,11 @@ package body NFC_Tags is
 
    end Read_Forum_Type_2_Tag;
 
-   procedure Write_Forum_Type_2_Tag (Data : PN532.PN532_Buf) is
+   procedure Write_Forum_Type_2_Tag (Data : PN532_Buf) is
       use type PN532.NFC_Forum_Type_2_Block;
 
       Status : Boolean;
-      Block : PN532.PN532_Buf (1..4);
+      Block : PN532_Buf (1..4);
       I : Unsigned_8 := Data'First;
       Len : Unsigned_8;
       Current_Block : PN532.NFC_Forum_Type_2_Block := 4;

          
@@ 256,8 257,8 @@ package body NFC_Tags is
 
    procedure Read_Forum_Type_4_Tag is
       Status : Boolean;
-      Data : PN532.PN532_Buf (1 .. 16);
-      NFC_Data : PN532.PN532_Buf (1 .. 64);
+      Data : PN532_Buf (1 .. 16);
+      NFC_Data : PN532_Buf (1 .. 64);
       Len : Unsigned_8;
       NDEF_ID : Unsigned_16;
       NDEF_Len : Unsigned_16;

          
@@ 319,11 320,11 @@ package body NFC_Tags is
       AVR.UART.CRLF;
    end Read_Forum_Type_4_Tag;
 
-   procedure Update_Forum_Type_4_Tag (New_Content : PN532.PN532_Buf)
+   procedure Update_Forum_Type_4_Tag (New_Content : PN532_Buf)
    is
       Status   : Boolean;
-      Data     : PN532.PN532_Buf (1 .. 16);
-      NFC_Data : PN532.PN532_Buf (1 .. 2);
+      Data     : PN532_Buf (1 .. 16);
+      NFC_Data : PN532_Buf (1 .. 2);
       Len      : Unsigned_8;
       NDEF_ID  : Unsigned_16;
    begin

          
@@ 391,7 392,7 @@ package body NFC_Tags is
    procedure Read_Tag is
       Sel_Res  : Unsigned_8;
       Sens_Res : Unsigned_16;
-      NFC_ID   : PN532.PN532_Buf (1..16);
+      NFC_ID   : PN532_Buf (1..16);
       NFC_ID_Len : Unsigned_8;
       Status   : Boolean;
    begin

          
@@ 439,7 440,7 @@ package body NFC_Tags is
    procedure Write_Tag is
       use AVR.UART;
 
-      NDEF_Data : PN532.PN532_Buf (1..18) :=
+      NDEF_Data : PN532_Buf (1..18) :=
         (03, 15, -- len
          16#D1#, 16#01#, 16#09#, 16#54#,
          16#02#, -- plain text

          
@@ 457,8 458,8 @@ package body NFC_Tags is
 
       Ch : Character;
       Len : Unsigned_8 := 0;
-      My_String : PN532.PN532_Buf (1..8);
-      NFC_ID : PN532.PN532_Buf (1..7);
+      My_String : PN532_Buf (1..8);
+      NFC_ID : PN532_Buf (1..7);
       NFC_ID_Len : Unsigned_8;
       Sel_Res  : Unsigned_8;
       Sens_Res : Unsigned_16;

          
@@ 534,7 535,7 @@ package body NFC_Tags is
    end Write_Tag;
 
 
-   NDEF_Hello : constant PN532.PN532_Buf (1..14) :=
+   NDEF_Hello : constant PN532_Buf (1..14) :=
      (00, 12, -- len
       16#D1#, 16#01#, 16#08#, 16#54#,
       16#02#, -- plain text

          
M pn532.adb +4 -4
@@ 126,7 126,7 @@ package body PN532 is
       PN532_SPI_Disable;
    end PN532_Write;
 
-   function PN532_Status return Unsigned_8 is
+   function PN532_Busy return Boolean is
       Status : Unsigned_8;
    begin
       PN532_SPI_Enable;

          
@@ 135,17 135,17 @@ package body PN532 is
       Status := SPI.Read;
       PN532_SPI_Disable;
 
-      return Status;
+      return Status /= 1;
    end PN532_Status;
 
    function PN532_Wait_For_Ready (Timeout : Unsigned_32) return Boolean is
       Counter : Unsigned_32 := 0;
    begin
       loop
-         exit when PN532_Status = 1;
+         exit when not PN532_Busy;
          Counter := Counter + 1;
 
-         if Counter > Timeout then
+         if Counter >= Timeout then
             return False;
          end if;
       end loop;

          
A => pn532_bus_i2c.adb +183 -0
@@ 0,0 1,183 @@ 
+with AVR;
+with AVR.MCU;
+with AVR.I2C;
+with AVR.I2C.Master;
+
+package body PN532_BUS_I2C is
+   use AVR;
+
+   -- IRQ line
+   IRQ_Bit : Boolean renames AVR.MCU.PIND_Bits (2);
+   IRQ_Bit_Direction : Boolean renames AVR.MCU.DDRD_Bits (2);
+
+   -- Reset
+   Reset_Bit : Boolean renames AVR.MCU.PORTD_Bits (3);
+   Reset_Bit_Direction : Boolean renames AVR.MCU.DDRD_Bits (3);
+
+   PN532_I2C_ADDRESS : constant := (16#48# / 2);
+
+   procedure Init is
+   begin
+      delay 0.1;
+      AVR.I2C.Master.Init;
+
+      IRQ_Bit_Direction := DD_Input; -- IRQ, digital pin 2
+
+      Reset_Bit_Direction := DD_Output;
+      Reset_Bit := True;
+      Reset_Bit := False;
+      delay 0.4;
+      Reset_Bit := True;
+
+      delay 0.02;
+   end Init;
+
+   function PN532_Busy return Boolean is
+   begin
+      return IRQ_Bit;
+   end PN532_Busy;
+   pragma Inline_Always (PN532_Busy);
+
+   procedure PN532_Write (Cmd : PN532_Buf)  is
+      Buf      : AVR.I2C.Data_Buffer (1 .. 64);
+      Checksum : Unsigned_8 := 16#FF#;
+      Len      : Unsigned_8 := Cmd'Length + 1;
+      Index    : Unsigned_8;
+   begin
+      delay 0.002;
+      
+      if Cmd'Length > Buf'Length + 8 then
+         return;
+      end if;
+
+      Buf (1) := 0;
+      Buf (2) := 0;
+      Buf (3) := 16#FF#;
+
+      Buf (4) := Len;
+      Buf (5) := (not Len) + 1;
+
+      Buf (6) := HOST_TO_PN532;
+
+      Checksum := Checksum + HOST_TO_PN532;
+
+      Index := 7;
+      -- Buf (Index .. Index - 1 + Cmd'Length) := Cmd;
+      for I in Cmd'Range loop
+         Buf (Index) := Cmd (I);
+         Checksum := Checksum + Cmd (I);
+         Index := Index + 1;
+      end loop;
+
+      Buf (Index) := (not Checksum);
+      Index := Index + 1;
+      Buf (Index) := 0;
+
+      AVR.I2C.Master.Send (PN532_I2C_ADDRESS, Buf (1..Index));
+      -- AVR.I2C.Master.Finish_Send;
+   end PN532_Write;
+
+   procedure PN532_Read_Raw (Buf : out PN532_Buf) is
+   begin
+      for I in Buf'Range loop
+         exit when not AVR.I2C.Master.Data_Is_Available;
+         Buf (I) := AVR.I2C.Master.Get;
+      end loop;
+   end PN532_Read_Raw;
+
+   procedure PN532_Read_Reply (Reply : out Reply_Type; Code : out Unsigned_8) is
+      Buf : PN532_Buf (1..6);
+      Len : Unsigned_8;
+      Ok : Unsigned_8;
+   begin
+      AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 7);
+      Code := 0;
+
+      loop
+         exit when AVR.I2C.Master.Data_Is_Available;
+      end loop;
+      Ok := AVR.I2C.Master.Get;
+      if (Ok and 1) /= 1 then
+         Reply := REPLY_ERROR;
+         return;
+      end if;
+
+      for I in Buf'Range loop
+         loop
+            exit when AVR.I2C.Master.Data_Is_Available;
+         end loop;
+         Buf (I) := AVR.I2C.Master.Get;
+      end loop;
+
+      if Buf (1) = 0 and Buf (2) = 0 and Buf (3) = 16#FF# then
+         if Buf (4) = 0 and Buf (5) = 16#FF# and Buf (6) = 0 then -- ACK
+            Reply := REPLY_ACK;
+         elsif Buf(4) = 16#FF# and Buf(5) = 0 and Buf (6) = 0 then -- NACK
+            Reply := REPLY_NACK;
+         else -- ERROR
+            Reply := REPLY_ERROR;
+            Len := Buf (4);
+            if Buf (5) + Len = 0 then -- checksum ok
+               Code := Buf (6);
+            end if;
+         end if;
+      else
+         Reply := REPLY_ERROR;
+         Code := 255;
+      end if;
+   end PN532_Read_Reply;
+
+   procedure PN532_Read_Data
+     (Buf    : out PN532_Buf;
+      Len    : out Unsigned_8;
+      Status : out Boolean)
+   is
+      Header : PN532_Buf (1..6);
+      Checksum : Unsigned_8 := 16#FF#;
+      Msg_Len : Unsigned_8;
+      Ready_Status : Unsigned_8;
+   begin
+      AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 60);
+      loop
+         exit when AVR.I2C.Master.Data_Is_Available;
+      end loop;
+      Ready_Status := AVR.I2C.Master.Get;
+
+      for I in Header'Range loop
+         Header (I) := AVR.I2C.Master.Get;
+      end loop;
+
+      if not (Header (2) = 0 and Header (3) = 16#FF#) then
+         -- invalid header
+         Status := False;
+         return;
+      end if;
+
+      Msg_Len := Header (4);
+      if Msg_Len + Header (5) /= 0 then
+         Status := False;
+         return;
+      end if;
+
+      Checksum := Checksum + Header (6);
+
+      Msg_Len := Unsigned_8'Min (Buf'Length, Msg_Len);
+
+      for I in Unsigned_8 range 1 .. Msg_Len loop
+         Buf (I) := AVR.I2C.Master.Get;
+         Checksum := Checksum + Buf (I);
+      end loop;
+
+      Header (1) := AVR.I2C.Master.Get;
+      Header (2) := AVR.I2C.Master.Get;
+      Header (1) := not Header (1);
+
+      if Checksum /= Header (1) then
+         -- Log ("PN532_Read_Data: Invalid checksum");
+         Status := False;
+      else
+         Status := True;
+         Len := Msg_Len;
+      end if;
+   end PN532_Read_Data;
+end PN532_BUS_I2C;
  No newline at end of file

          
A => pn532_bus_i2c.ads +23 -0
@@ 0,0 1,23 @@ 
+with Interfaces;
+with PN532_Types;
+
+use PN532_Types;
+use Interfaces;
+
+package PN532_BUS_I2C is
+   procedure Init;
+   
+   function PN532_Busy return Boolean;
+
+   procedure PN532_Write (Cmd : PN532_Buf);
+   
+   procedure PN532_Read_Reply (Reply : out Reply_Type; Code : out Unsigned_8);
+   
+   procedure PN532_Read_Raw (Buf : out PN532_Buf);
+   
+   procedure PN532_Read_Data
+     (Buf    : out PN532_Buf;
+      Len    : out Unsigned_8;
+      Status : out Boolean);
+
+end PN532_BUS_I2C;
  No newline at end of file

          
M pn532_twi.adb +102 -195
@@ 17,24 17,27 @@ 
 
 with System;
 with Interfaces;
-with AVR.I2C;
-with AVR.I2C.Master;
 with AVR.Real_Time.Clock;
 pragma Unreferenced(AVR.Real_Time.Clock);
 with AVR.UART;
 with AVR.Strings;
 with AVR.Programspace;
-with AVR.MCU;
 with AVR;
+with PN532_BUS_I2C;
 
 use Interfaces;
 
 package body PN532_TWI is
    use AVR;
-   
-   IRQ_Bit : Boolean renames AVR.MCU.PIND_Bits (2);
 
-   PN532_I2C_ADDRESS : constant := (16#48# / 2);
+   function PN532_Busy return Boolean renames PN532_BUS_I2C.PN532_Busy;
+   procedure PN532_Write (Cmd : PN532_Buf) renames PN532_BUS_I2C.PN532_Write;
+   procedure PN532_Read_Raw (Buf : out PN532_Buf) renames PN532_BUS_I2C.PN532_Read_Raw;
+   procedure PN532_Read_Data
+     (Buf    : out PN532_Buf;
+      Len    : out Unsigned_8;
+      Status : out Boolean) renames PN532_BUS_I2C.PN532_Read_Data;
+
 
    PN532_FIRMWARE_VERSION : constant := 16#02#;
    PN532_SAMCONFIG        : constant := 16#14#;

          
@@ 44,9 47,7 @@ package body PN532_TWI is
    PN532_TG_GET_DATA : constant := 16#86#;
    PN532_TG_SET_DATA : constant := 16#8E#;
 
-   HOST_TO_PN532 : constant := 16#D4#;
-
-   PN532_TIMEOUT_VALUE : constant := 2000;
+   PN532_TIMEOUT_VALUE : constant := 20000;
 
    PN532_106_KBPS_ISOIEC_14443_A : constant := 0;
 

          
@@ 55,8 56,6 @@ package body PN532_TWI is
    MIFARE_AUTH_KEY_A_CMD : constant := 16#60#;
    MIFARE_AUTH_KEY_B_CMD : constant := 16#61#;
 
-   type Reply_Type is (REPLY_ACK, REPLY_NACK, REPLY_ERROR);
-
    type Buf_In_Progmem (Len : AVR.Nat8) is record
       Text : PN532_Buf (1..Len);
    end record;

          
@@ 81,114 80,70 @@ package body PN532_TWI is
       AVR.UART.CRLF;
    end Log;
 
-   procedure PN532_Delay is
-   begin
-      delay 0.002;
-   end PN532_Delay;
-   pragma Inline (PN532_Delay);
-
-   procedure PN532_Write (Cmd : PN532_Buf)  is
-      Buf : AVR.I2C.Data_Buffer (1 .. 64);
-      Checksum : Unsigned_8 := 16#FF#;
-      Len : Unsigned_8 := Cmd'Length + 1;
-      Index : Unsigned_8;
-   begin
-      PN532_Delay;
-      Buf (1) := 0;
-      Buf (2) := 0;
-      Buf (3) := 16#FF#;
-
-      Buf (4) := Len;
-      Buf (5) := (not Len) + 1;
-
-      Buf (6) := HOST_TO_PN532;
-
-      Checksum := Checksum + HOST_TO_PN532;
-
-      Index := 7;
-      -- Buf (Index .. Index - 1 + Cmd'Length) := Cmd;
-      for I in Cmd'Range loop
-         Buf (Index) := Cmd (I);
-         Checksum := Checksum + Cmd (I);
-         Index := Index + 1;
-      end loop;
-
-      Buf (Index) := (not Checksum);
-      Index := Index + 1;
-      Buf (Index) := 0;
-
-      AVR.I2C.Master.Send (PN532_I2C_ADDRESS, Buf (1..Index));
-      -- AVR.I2C.Master.Finish_Send;
-   end PN532_Write;
-   
-
    function PN532_Wait_For_Ready (Timeout : Unsigned_32) return Boolean is
       Counter : Unsigned_32 := 0;
    begin
       loop
-         exit when not IRQ_Bit;
+         exit when not PN532_Busy;
          Counter := Counter + 1;
+
          if Counter >= Timeout then
             return False;
          end if;
       end loop;
-      
+
       return True;
    end PN532_Wait_For_Ready;
 
-   procedure PN532_Read_Reply (Reply : out Reply_Type; Code : out Unsigned_8) is
-      Buf : PN532_Buf (1..6);
-      Len : Unsigned_8;
-      Ok : Unsigned_8;
-   begin
-      AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 7);
-      Code := 0;
-      
-      loop
-         exit when AVR.I2C.Master.Data_Is_Available;
-      end loop;
-      Ok := AVR.I2C.Master.Get;
-      if (Ok and 1) /= 1 then
-         Reply := REPLY_ERROR;
-         return;
-      end if;
-      
-      for I in Buf'Range loop
-         loop
-            exit when AVR.I2C.Master.Data_Is_Available;
-         end loop;
-         Buf (I) := AVR.I2C.Master.Get;
-      end loop;
-      Log ("ACK:");
-      Log (Buf);
-
-      if Buf (1) = 0 and Buf (2) = 0 and Buf (3) = 16#FF# then
-         if Buf (4) = 0 and Buf (5) = 16#FF# and Buf (6) = 0 then -- ACK
-            Reply := REPLY_ACK;
-         elsif Buf(4) = 16#FF# and Buf(5) = 0 and Buf (6) = 0 then -- NACK
-            Reply := REPLY_NACK;
-         else -- ERROR
-            Reply := REPLY_ERROR;
-            Len := Buf (4);
-            if Buf (5) + Len = 0 then -- checksum ok
-               AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 1);
-               loop
-                  exit when AVR.I2C.Master.Data_Is_Available;
-               end loop;
-               Code := AVR.I2C.Master.Get;
-            end if;
-         end if;
-      else
-         -- Log ("Unknown reply");
-         -- for I in Buf'Range loop
-         --    AVR.UART.Put (Data => Buf (I), Base => 16);
-         --    AVR.UART.Put (" ");
-         -- end loop;
-         -- AVR.UART.CRLF;
-         Reply := REPLY_ERROR;
-         Code := 255;
-      end if;
-   end PN532_Read_Reply;
+   procedure PN532_Read_Reply (Reply : out Reply_Type; Code : out Unsigned_8)
+     renames PN532_BUS_I2C.PN532_Read_Reply;
+--   is
+--      Buf : PN532_Buf (1..6);
+--      Len : Unsigned_8;
+--      Ok : Unsigned_8;
+--   begin
+--      AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 7);
+--      Code := 0;
+--
+--      loop
+--         exit when AVR.I2C.Master.Data_Is_Available;
+--      end loop;
+--      Ok := AVR.I2C.Master.Get;
+--      if (Ok and 1) /= 1 then
+--         Reply := REPLY_ERROR;
+--         return;
+--      end if;
+--
+--      for I in Buf'Range loop
+--         loop
+--            exit when AVR.I2C.Master.Data_Is_Available;
+--         end loop;
+--         Buf (I) := AVR.I2C.Master.Get;
+--      end loop;
+--
+--      if Buf (1) = 0 and Buf (2) = 0 and Buf (3) = 16#FF# then
+--         if Buf (4) = 0 and Buf (5) = 16#FF# and Buf (6) = 0 then -- ACK
+--            Reply := REPLY_ACK;
+--         elsif Buf(4) = 16#FF# and Buf(5) = 0 and Buf (6) = 0 then -- NACK
+--            Reply := REPLY_NACK;
+--         else -- ERROR
+--            Reply := REPLY_ERROR;
+--            Len := Buf (4);
+--            if Buf (5) + Len = 0 then -- checksum ok
+--               Code := Buf (6);
+--            end if;
+--         end if;
+--      else
+--         -- Log ("Unknown reply");
+--         -- for I in Buf'Range loop
+--         --    AVR.UART.Put (Data => Buf (I), Base => 16);
+--         --    AVR.UART.Put (" ");
+--         -- end loop;
+--         -- AVR.UART.CRLF;
+--         Reply := REPLY_ERROR;
+--         Code := 255;
+--      end if;
+--   end PN532_Read_Reply;
 
    function PN532_Send_Command (Cmd : PN532_Buf; Timeout : Unsigned_32)
      return Boolean

          
@@ 200,83 155,21 @@ package body PN532_TWI is
       PN532_Write (Cmd);
 
       Status := PN532_Wait_For_Ready (Timeout);
-      if not Status then -- timeout
+      if not Status then
+         Log ("PN532_Send_Command: timeout");
          return False;
       end if;
 
       PN532_Read_Reply (Reply, Error_Code);
 
       if Reply = REPLY_ERROR or Reply = REPLY_NACK then
-         Log ("PN532_Send_Command, invalid reply");
+         -- Log ("PN532_Send_Command, invalid reply");
          return False;
       end if;
 
       return True;
    end PN532_Send_Command;
 
-   procedure PN532_Read_Raw (Buf : out PN532_Buf) is
-   begin
-      
-      for I in Buf'Range loop
-         exit when not AVR.I2C.Master.Data_Is_Available;
-         Buf (I) := AVR.I2C.Master.Get;
-      end loop;
-   end PN532_Read_Raw;
-
-   procedure PN532_Read_Data
-     (Buf    : out PN532_Buf;
-      Len    : out Unsigned_8;
-      Status : out Boolean)
-   is
-      Header : PN532_Buf (1..6);
-      Checksum : Unsigned_8 := 16#FF#;
-      Msg_Len : Unsigned_8;
-      Ready_Status : Unsigned_8;
-   begin
-      AVR.I2C.Master.Request (PN532_I2C_ADDRESS, 60);
-      loop
-         exit when AVR.I2C.Master.Data_Is_Available;
-      end loop;
-      Ready_Status := AVR.I2C.Master.Get;
-      
-      for I in Header'Range loop
-         Header (I) := AVR.I2C.Master.Get;
-      end loop;
-
-      if not (Header (2) = 0 and Header (3) = 16#FF#) then
-         -- invalid header
-         Status := False;
-         return;
-      end if;
-
-      Msg_Len := Header (4);
-      if Msg_Len + Header (5) /= 0 then
-         Status := False;
-         Log ("PN532_Read_Data: Invalid header len checksum");
-         return;
-      end if;
-
-      Checksum := Checksum + Header (6);
-
-      Msg_Len := Unsigned_8'Min (Buf'Length, Msg_Len);
-
-      for I in Unsigned_8 range 1 .. Msg_Len loop
-         Buf (I) := AVR.I2C.Master.Get;
-         Checksum := Checksum + Buf (I);
-      end loop;
-
-      Header (1) := AVR.I2C.Master.Get;
-      Header (2) := AVR.I2C.Master.Get;
-      Header (1) := not Header (1);
-
-      if Checksum /= Header (1) then
-         -- Log ("PN532_Read_Data: Invalid checksum");
-         Status := False;
-      else
-         Status := True;
-         Len := Msg_Len;
-      end if;
-   end PN532_Read_Data;
 
    function PN532_SAM_Config return Boolean is
       Cmd : PN532_Buf := ( PN532_SAMCONFIG,

          
@@ 286,20 179,28 @@ package body PN532_TWI is
       Status : Boolean;
       Reply : PN532_Buf (1..1) := (1 => 0);
       Len : Unsigned_8;
-      Reply_Status : Reply_Type;
-      Error_Code : Unsigned_8;
    begin
       Status := PN532_Send_Command (Cmd, PN532_TIMEOUT_VALUE);
       if not Status then
+         Log ("timeout");
+         return False;
+      end if;
+
+      Status := PN532_Wait_For_Ready (PN532_TIMEOUT_VALUE);
+      if not Status then
+         Log ("timeout2");
          return False;
       end if;
-      
+
       PN532_Read_Data (Reply, Len, Status);
-      Log (Reply (1..Len));
       if Status and Reply (1) = 16#15# then
+         Log ("SAM config ok");
          return True;
       end if;
 
+      Log ("invalid reply");
+      Log (Reply (1..Len));
+
       return False;
    end PN532_SAM_Config;
 

          
@@ 342,26 243,32 @@ package body PN532_TWI is
       Ok := PN532_Send_Command (Cmd, PN532_TIMEOUT_VALUE);
 
       if not Ok then
+         Log ("PN532_Detect_Tag: PN532_Send_Command failed");
          Status := False;
          return;
       end if;
 
-      if not PN532_Wait_For_Ready (PN532_TIMEOUT_VALUE) then
+      if not PN532_Wait_For_Ready (PN532_TIMEOUT_VALUE * 30) then
+         Log ("PN532_Detect_Tag: PN532_Wait_For_Ready failed");
          Status := False;
          return;
       end if;
 
       PN532_Read_Data (Reply, Len, Ok);
       if not Ok or Len = 0 then -- Some error
+         Log ("PN532_Detect_Tag: some error");
          Status := False;
          return;
       end if;
 
       if Reply (2) = 0 then -- No tags found
+         Log ("PN532_Detect_Tag: no tags found");
          Status := False;
          return;
       end if;
 
+      -- Log (Reply (1..Len));
+
       Sens_Res := Unsigned_16 (Reply (5)) * 256 or Unsigned_16 (Reply (4));
       Sel_Res := Reply (6);
 

          
@@ 1031,22 938,22 @@ package body PN532_TWI is
       end loop;
    end PN532_NFC_Forum_Type_4_Emulate;
 
-   procedure Init is
-      Empty_Buf : AVR.I2C.Data_Buffer (1 .. 1) := (1 => 16#55#);
-   begin
-      Log ("PN532.Init");
-      AVR.I2C.Master.Init;
-      
-      AVR.MCU.DDRD_Bits (6) := DD_Output;
-      AVR.MCU.DDRD_Bits (3) := DD_Output; -- Reset pin
-      AVR.MCU.DDRD_Bits (2) := DD_Input; -- IRQ, digital pin 2
-      AVR.MCU.PORTD_Bits (3) := True;
-      AVR.MCU.PORTD_Bits (3) := False;
-      AVR.MCU.PORTD_Bits (6) := False;
-      delay 0.4;
-      AVR.MCU.PORTD_Bits (6) := True;
-      AVR.MCU.PORTD_Bits (3) := True;
-      
-      Log ("PN532.Init done");
-   end Init;
+   procedure Init renames PN532_BUS_I2C.Init;
+--      Empty_Buf : AVR.I2C.Data_Buffer (1 .. 1) := (1 => 16#55#);
+--   begin
+--      Log ("PN532.Init");
+--      AVR.I2C.Master.Init;
+--
+--      AVR.MCU.DDRD_Bits (6) := DD_Output;
+--      AVR.MCU.DDRD_Bits (3) := DD_Output; -- Reset pin
+--      AVR.MCU.DDRD_Bits (2) := DD_Input; -- IRQ, digital pin 2
+--      AVR.MCU.PORTD_Bits (3) := True;
+--      AVR.MCU.PORTD_Bits (3) := False;
+--      AVR.MCU.PORTD_Bits (6) := False;
+--      delay 0.4;
+--      AVR.MCU.PORTD_Bits (6) := True;
+--      AVR.MCU.PORTD_Bits (3) := True;
+--
+--      Log ("PN532.Init done");
+--   end Init;
 end PN532_TWI;

          
M pn532_twi.ads +8 -5
@@ 16,13 16,16 @@ 
 --
 
 with Interfaces;
+with PN532_Types;
+
+use PN532_Types;
 
 package PN532_TWI is
-   type PN532_Buf is array
-     (Interfaces.Unsigned_8 range <>) of Interfaces.Unsigned_8;
-
-   subtype PN532_Buf_4 is PN532_Buf (1..4);
-   subtype PN532_Buf_6 is PN532_Buf (1..6);
+--   type PN532_Buf is array
+--     (Interfaces.Unsigned_8 range <>) of Interfaces.Unsigned_8;
+--
+--   subtype PN532_Buf_4 is PN532_Buf (1..4);
+--   subtype PN532_Buf_6 is PN532_Buf (1..6);
 
    subtype NFC_Forum_Type_2_Block is Interfaces.Unsigned_8 range 0 .. 63;
    subtype Mifare_Classic_Block is Interfaces.Unsigned_8 range 0 .. 255;

          
A => pn532_types.ads +13 -0
@@ 0,0 1,13 @@ 
+with Interfaces;
+
+package PN532_Types is
+   type PN532_Buf is array
+     (Interfaces.Unsigned_8 range <>) of Interfaces.Unsigned_8;
+     
+   type Reply_Type is (REPLY_ACK, REPLY_NACK, REPLY_ERROR);
+     
+   subtype PN532_Buf_4 is PN532_Buf (1..4);
+   subtype PN532_Buf_6 is PN532_Buf (1..6);
+   
+   HOST_TO_PN532 : constant := 16#D4#;
+end PN532_Types;
  No newline at end of file