home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / gpio.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  4KB  |  171 lines

  1. /* -*-C-*-
  2.  
  3. Copyright (c) 1990-1999 Massachusetts Institute of Technology
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or (at
  8. your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* $Id: gpio.c,v 1.11 1999/01/02 06:11:34 cph Exp $ */
  21.  
  22. /* Scheme primitives for GPIO */
  23.  
  24. #include "scheme.h"
  25. #include "prims.h"
  26. #include "ux.h"
  27. #include "uxio.h"
  28.  
  29. #include <stdio.h>
  30. #include <fcntl.h> 
  31. #include <dvio.h>
  32.  
  33. DEFINE_PRIMITIVE ("GPIO-OPEN", Prim_gpio_open, 1, 1, 0)
  34. {
  35.   int gpio_channel;
  36.  
  37.   PRIMITIVE_HEADER (1);
  38.  
  39.   gpio_channel = (open (STRING_ARG (1), (O_RDWR | O_NDELAY)));
  40.   if (gpio_channel == -1)
  41.   {
  42.     error_external_return();
  43.   }
  44.   if (!(LONG_TO_FIXNUM_P( gpio_channel)))
  45.   {
  46.     /* This is a crock, but guarantees that we can assume fixnum
  47.        in all the other primitives.
  48.      */
  49.     close (gpio_channel);
  50.     error_external_return();
  51.   }
  52.  
  53.   /* Reset interface */
  54.  
  55.   io_reset (gpio_channel);
  56.  
  57.   /* Timeout in 5 sec. */
  58.   io_timeout_ctl (gpio_channel, 5000000);
  59.  
  60.   /* Guarantee exclusive access. */
  61.   io_lock (gpio_channel);
  62.  
  63. #if 1
  64.   /* Map into address space. */
  65.   io_burst (gpio_channel, 1);
  66. #endif
  67.  
  68.   /* Set data width to 16 bits. */
  69.   io_width_ctl (gpio_channel, 16);
  70.   
  71.   PRIMITIVE_RETURN( LONG_TO_FIXNUM (gpio_channel));
  72. }
  73.  
  74.  
  75. DEFINE_PRIMITIVE ("GPIO-CLOSE", Prim_gpio_close, 1, 1, 0)
  76. {
  77.   int gpio_channel;
  78.  
  79.   PRIMITIVE_HEADER (1);
  80.  
  81.   gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
  82.  
  83. #if 1
  84.   io_burst (gpio_channel, 0);
  85. #endif
  86.  
  87.   io_unlock (gpio_channel);
  88.   close (gpio_channel);
  89.   
  90.   PRIMITIVE_RETURN( long_to_integer( gpio_channel ));
  91. }
  92.  
  93.  
  94. DEFINE_PRIMITIVE ("GPIO-READ-STATUS", Prim_gpio_read_status, 1, 1, 0)
  95. {
  96.   int gpio_channel;
  97.  
  98.   PRIMITIVE_HEADER (1);
  99.  
  100.   gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
  101.   
  102.   PRIMITIVE_RETURN( LONG_TO_FIXNUM( gpio_get_status( gpio_channel)));
  103. }
  104.  
  105.  
  106. DEFINE_PRIMITIVE ("GPIO-WRITE-CONTROL", Prim_gpio_write_control, 2, 2, 0)
  107. {
  108.   int gpio_channel, control_value;
  109.  
  110.   PRIMITIVE_HEADER (2);
  111.  
  112.   gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
  113.   control_value = (UNSIGNED_FIXNUM_ARG (2));
  114.   
  115.   PRIMITIVE_RETURN( LONG_TO_FIXNUM( gpio_set_ctl( gpio_channel, control_value)));
  116. }
  117.  
  118. /* Both of the following return the number of bytes transferred. */
  119.  
  120.  
  121. DEFINE_PRIMITIVE ("GPIO-READ-STRING!", Prim_gpio_read_string, 4, 4, 0)
  122. {
  123.   int gpio_channel, count;
  124.   char *data;
  125.  
  126.   PRIMITIVE_HEADER (4);
  127.  
  128.   gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
  129.   data = ((char *) (STRING_LOC ((ARG_REF (2)), (UNSIGNED_FIXNUM_ARG (3)))));
  130.   count = (UNSIGNED_FIXNUM_ARG (4));
  131.  
  132.   while (1)
  133.   {
  134.     long scr = (read (gpio_channel, data, count));
  135.     if (scr < 0)
  136.     {
  137.       UX_prim_check_errno (syscall_read);
  138.       continue;
  139.     }
  140.     if (scr > count)
  141.       error_external_return ();
  142.     PRIMITIVE_RETURN( LONG_TO_FIXNUM (scr));
  143.   }
  144. }
  145.  
  146.  
  147. DEFINE_PRIMITIVE ("GPIO-WRITE-STRING", Prim_gpio_write_string, 4, 4, 0)
  148. {
  149.   int gpio_channel, count;
  150.   char *data;
  151.  
  152.   PRIMITIVE_HEADER (4);
  153.  
  154.   gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
  155.   data = ((char *) (STRING_LOC ((ARG_REF (2)), (UNSIGNED_FIXNUM_ARG (3)))));
  156.   count = (UNSIGNED_FIXNUM_ARG (4));
  157.  
  158.   while (1)
  159.   {
  160.     long scr = (write (gpio_channel, data, count));
  161.     if (scr < 0)
  162.     {
  163.       UX_prim_check_errno (syscall_write);
  164.       continue;
  165.     }
  166.     if (scr > count)
  167.       error_external_return ();
  168.     PRIMITIVE_RETURN( LONG_TO_FIXNUM (scr));
  169.   }
  170. }
  171.