Add new style primitives (multiple byte transfers, string based).
authorGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Wed, 13 Jun 1990 20:49:58 +0000 (20:49 +0000)
committerGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Wed, 13 Jun 1990 20:49:58 +0000 (20:49 +0000)
Add control primitives.
Add locking of the interface.

v7/src/microcode/gpio.c

index bec52b3ff4888a0ae984d5e76537799bfcd3a9cd..1776ce0fdd24518b40e6017b7d84870a66adf8fd 100644 (file)
@@ -30,8 +30,8 @@ Technology nor of any adaptation thereof in any advertising,
 promotional, or sales literature without prior written consent from
 MIT in each case. */
 
-/* $Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v7/src/microcode/Attic/gpio.c,v 1.1 1990/06/12 16:35:28 jinx Exp $ */
-\f
+/* $Header: /Users/cph/tmp/foo/mit-scheme/mit-scheme/v7/src/microcode/Attic/gpio.c,v 1.2 1990/06/13 20:49:58 jinx Exp $ */
+
 /* Scheme primitives for GPIO */
 
 #include "scheme.h"
@@ -40,7 +40,10 @@ MIT in each case. */
 #include <stdio.h>
 #include <fcntl.h> 
 #include <dvio.h>
-
+\f
+/* "Old style" primitives.
+   Should be flushed.
+ */
 
 DEFINE_PRIMITIVE ("OPEN-GPIO", Prim_open_gpio, 1, 1, 0)
 {
@@ -58,6 +61,19 @@ DEFINE_PRIMITIVE ("OPEN-GPIO", Prim_open_gpio, 1, 1, 0)
 }
 
 
+DEFINE_PRIMITIVE ("CLOSE-GPIO", Prim_close_gpio, 1, 1, 0)
+{
+  int gpio_channel;
+
+  PRIMITIVE_HEADER (1);
+
+  gpio_channel = (arg_integer (1));
+  close ( gpio_channel );
+  
+  PRIMITIVE_RETURN( long_to_integer( gpio_channel ));
+}
+
+
 DEFINE_PRIMITIVE ("READ-GPIO", Prim_read_gpio, 1, 1, 0)
 {
   int gpio_channel;
@@ -87,7 +103,7 @@ DEFINE_PRIMITIVE ("WRITE-GPIO", Prim_write_gpio, 2, 2, 0)
 {
   int gpio_channel;
   unsigned long output;
-  unsigned char buffer[4];
+  unsigned char buffer[2];
   int xfer;
 
   PRIMITIVE_HEADER (2);
@@ -100,19 +116,114 @@ DEFINE_PRIMITIVE ("WRITE-GPIO", Prim_write_gpio, 2, 2, 0)
 
   xfer = write (gpio_channel, &buffer[0], 2);
 
- /* xfer is 2 if successfull */
 /* xfer is 2 if successfull */
  
   PRIMITIVE_RETURN( long_to_integer( xfer ));
 }
+\f
+/* "New style" primitives. */
 
-DEFINE_PRIMITIVE ("CLOSE-GPIO", Prim_close_gpio, 1, 1, 0)
+
+DEFINE_PRIMITIVE ("GPIO-OPEN", Prim_gpio_open, 1, 1, 0)
 {
   int gpio_channel;
 
   PRIMITIVE_HEADER (1);
 
-  gpio_channel = (arg_integer (1));
+  gpio_channel = (open (STRING_ARG (1), (O_RDWR | O_NDELAY)));
+  if (gpio_channel == -1)
+  {
+    error_external_return();
+  }
+  if (!(LONG_TO_FIXNUM_P( gpio_channel)))
+  {
+    /* This is a crock, but guarantees that we can assume fixnum
+       in all the other primitives.
+     */
+    close (gpio_channel);
+    error_external_return;
+  }
+
+  io_lock (gpio_channel);
+  io_reset (gpio_channel);
+  io_width_ctl (gpio_channel, 16);
+  
+  PRIMITIVE_RETURN( LONG_TO_FIXNUM (gpio_channel));
+}
+
+
+DEFINE_PRIMITIVE ("GPIO-CLOSE", Prim_gpio_close, 1, 1, 0)
+{
+  int gpio_channel;
+
+  PRIMITIVE_HEADER (1);
+
+  gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
+
+  io_unlock (gpio_channel);
   close ( gpio_channel );
   
   PRIMITIVE_RETURN( long_to_integer( gpio_channel ));
 }
+
+
+DEFINE_PRIMITIVE ("GPIO-READ-STATUS", Prim_gpio_read_status, 1, 1, 0)
+{
+  int gpio_channel;
+
+  PRIMITIVE_HEADER (1);
+
+  gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
+  
+  PRIMITIVE_RETURN( LONG_TO_FIXNUM( gpio_get_status( gpio_channel)));
+}
+
+
+DEFINE_PRIMITIVE ("GPIO-WRITE-CONTROL", Prim_gpio_write_control, 2, 2, 0)
+{
+  int gpio_channel, control_value;
+
+  PRIMITIVE_HEADER (2);
+
+  gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
+  control_value = (UNSIGNED_FIXNUM_ARG (2));
+  
+  PRIMITIVE_RETURN( LONG_TO_FIXNUM( gpio_set_ctl( gpio_channel, control_value)));
+}
+\f
+/* Both of the following return the number of bytes transferred. */
+
+
+DEFINE_PRIMITIVE ("GPIO-READ-STRING!", Prim_gpio_read_string, 3, 3, 0)
+{
+  int gpio_channel, count, result;
+  char *data;
+
+  PRIMITIVE_HEADER (3);
+
+  gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
+  data = (STRING_ARG (2));
+  count = (UNSIGNED_FIXNUM_ARG (3));
+
+  result = (read (gpio_channel, &data[0], count));
+
+  PRIMITIVE_RETURN( LONG_TO_FIXNUM (result));
+}
+
+
+DEFINE_PRIMITIVE ("GPIO-WRITE-STRING", Prim_gpio_write_string, 3, 3, 0)
+{
+  int gpio_channel, count, result;
+  char *data;
+
+  PRIMITIVE_HEADER (3);
+
+  gpio_channel = (UNSIGNED_FIXNUM_ARG (1));
+  data = (STRING_ARG (2));
+  count = (UNSIGNED_FIXNUM_ARG (3));
+
+  result = (write (gpio_channel, &data[0], count));
+
+  PRIMITIVE_RETURN( LONG_TO_FIXNUM ( result ));
+}
+