GPIO Interface for Linux in the Ada-Programming language
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpio.adb 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. with Ada.Text_IO;
  2. with Interfaces.C;
  3. use Interfaces.C;
  4. use Ada.Text_IO;
  5. package body gpio is
  6. procedure export_inner ( pin: in integer; filename : in String ) is
  7. file : File_Type;
  8. begin
  9. -- exports the pin by using the file under /sys/class/gpio ...
  10. open ( file, mode => Out_File,
  11. Name => filename );
  12. if (pin<10) then
  13. declare
  14. text : String := "" & integer'image (pin)(2);
  15. begin
  16. put (file, text);
  17. end;
  18. elsif (pin<100) then
  19. declare
  20. text : String := "" & integer'image(pin)(2) & integer'image(pin)(3);
  21. begin
  22. put (file, text);
  23. end;
  24. end if;
  25. close ( file );
  26. delay DURATION(0.1);
  27. end;
  28. procedure export ( pin : in integer ) is
  29. begin
  30. export_inner ( pin, "/sys/class/gpio/export" );
  31. end;
  32. procedure unexport ( pin : in integer ) is
  33. begin
  34. export_inner ( pin, "/sys/class/gpio/unexport" );
  35. end;
  36. procedure pinMode_inner ( filename: in String; mode: in String ) is
  37. file : File_Type;
  38. begin
  39. open ( file, mode => Out_File,
  40. Name => filename );
  41. put ( file, item => mode );
  42. close ( file );
  43. end;
  44. procedure pinMode ( pin : in integer; output: in Boolean ) is
  45. begin
  46. if (pin < 10) then
  47. declare
  48. filename: String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & "/direction";
  49. begin
  50. if (output = true) then
  51. pinMode_inner ( filename, "out" );
  52. else
  53. pinMode_inner ( filename, "in" );
  54. end if;
  55. end;
  56. elsif (pin < 100) then
  57. declare
  58. filename: String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & integer'image(pin)(3) & "/direction";
  59. begin
  60. if (output = true) then
  61. pinMode_inner ( filename, "out" );
  62. else
  63. pinMode_inner ( filename, "in" );
  64. end if;
  65. end;
  66. end if;
  67. end;
  68. function bcm2Wpi ( pin: In integer ) return integer is
  69. pins : array ( 1 .. 28 ) of Integer :=
  70. ( 30, -- BCM PIN 0!!!,
  71. 31, 8, 9, 7, 21, 22, 11, 10, 13, 12, -- 1 .. 10
  72. 14, 26, 23, 15, 16, 27, 0, 1, 24, 28, -- 11 .. 20
  73. 29, 3, 4, 5, 6, 25, 2 ); -- 21 .. 28
  74. begin
  75. if ((pin+1) in pins'range) then
  76. return pins(pin+1);
  77. else
  78. return -1;
  79. end if;
  80. end;
  81. -- using the C-system function to call "gpio mode <pin> up"
  82. procedure pullUp ( pin: In integer ) is
  83. function Sys ( ARg: Char_Array) return Integer;
  84. pragma Import ( C, Sys, "system" );
  85. ret_val : Integer;
  86. p : Integer;
  87. begin
  88. p := bcm2Wpi ( pin );
  89. declare
  90. text : String := "gpio mode "&integer'image(p) & " up";
  91. begin
  92. ret_val := sys ( To_C ( text ) );
  93. end;
  94. end;
  95. -- using the C-system function to call "gpio mode <pin> down"
  96. procedure pullDown ( pin: In integer ) is
  97. function Sys ( ARg: Char_Array) return Integer;
  98. pragma Import ( C, Sys, "system" );
  99. ret_val : Integer;
  100. p : Integer;
  101. begin
  102. p := bcm2Wpi ( pin );
  103. declare
  104. text : String := "gpio mode "&integer'image(p) & " down";
  105. begin
  106. ret_val := sys ( To_C ( text ) );
  107. end;
  108. end;
  109. procedure digitalWrite ( pin : in integer; value: in boolean ) is
  110. file : File_type;
  111. outval : Integer;
  112. begin
  113. if (value = True) then
  114. outval := 1;
  115. else
  116. outval := 0;
  117. end if;
  118. if (pin <10) then
  119. declare
  120. filename : String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & "/value";
  121. begin
  122. open ( file, mode => Out_file,
  123. name => filename );
  124. put ( file, item => integer'image ( outval )(2) );
  125. close ( file );
  126. end;
  127. elsif (pin <100) then
  128. declare
  129. filename : String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & integer'image(pin)(3) & "/value";
  130. begin
  131. open ( file, mode => Out_file,
  132. name => filename );
  133. put ( file, item => integer'image ( outval )(2) );
  134. close ( file );
  135. end;
  136. end if;
  137. end;
  138. function digitalRead ( pin : in integer ) return boolean is
  139. file : File_type;
  140. outval : Character;
  141. begin
  142. if (pin < 10) then
  143. declare
  144. filename : String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & "/value";
  145. begin
  146. open ( file, mode => In_file,
  147. name => filename );
  148. get ( file, outval );
  149. close ( file );
  150. if (outval = '1') then return True;
  151. else return False;
  152. end if;
  153. end;
  154. elsif (pin < 100) then
  155. declare
  156. filename : String := "/sys/class/gpio/gpio" & integer'image(pin)(2) & integer'image(pin)(3) & "/value";
  157. begin
  158. open ( file, mode => In_file,
  159. name => filename );
  160. get ( file, outval );
  161. close ( file );
  162. if (outval = '1') then return True;
  163. else return False;
  164. end if;
  165. end;
  166. end if;
  167. return False;
  168. end;
  169. end gpio;