Переглянути джерело

ground work for I2C shim for PIU communication

clementinecomputing 5 роки тому
батько
коміт
55b31654a3

+ 1 - 0
aux/convert-gps

@@ -135,6 +135,7 @@ elif infmt == "dmmc":
   if (len(tok[0][-4:-2])>0):
     ipart = float(tok[0][-4:-2])
   out_coord_dd = ipart + ((min_int_part + min_rem_part)/60.0)
+  if is_neg: out_coord_dd *= -1.0
 
 
 out_coord = []

+ 1 - 1
aux/deploy-busunit-interactive

@@ -123,7 +123,7 @@ read adddeps
 
 if [[ "$adddeps" =~ ^[yY]?$ ]]; then
 
-  deps="espeak mplayer git ppp ppp-dev sox alsa-utils unclutter fbi xinput-calibrator ffmpeg jq matchbox-window-manager "
+  deps="espeak mplayer git ppp ppp-dev sox alsa-utils unclutter fbi xinput-calibrator ffmpeg jq matchbox-window-manager libi2c-dev "
 
   echo ""
   echo "  Installing: $deps"

+ 120 - 0
busunit/PIU/firmware/ardworker_rcv.ino

@@ -0,0 +1,120 @@
+//
+// This code is in the public domain.
+//
+// License: CC0
+//
+
+#include <Wire.h>
+
+
+#define MSG_BUF_MAX 128
+#define ARDWORKER_I2C_ADDRESS 0x08
+
+
+char _tbuf[MSG_BUF_MAX];
+
+int   i2c_msg_buf_n = 0,
+      i2c_msg_buf_rdy = 0;
+char  i2c_msg_buf[MSG_BUF_MAX];
+
+int serial_buf_s=0,
+    serial_buf_n=0;
+char serial_buf[MSG_BUF_MAX];
+
+void setup() {
+  i2c_msg_buf_rdy = 0;
+  i2c_msg_buf[0] = '\0';
+  i2c_msg_buf_n = 0;
+
+  serial_buf_s=0;
+  serial_buf_n=0;
+  serial_buf[0] = '\0';
+  
+  Wire.begin(ARDWORKER_I2C_ADDRESS);
+  Wire.onReceive(receive_i2c_data);
+  Wire.onRequest(send_i2c_data);
+  Serial.begin(115200);
+}
+
+
+
+void loop() {
+  int i, _b, _cur;
+  int _ser_print_rdy = 0;
+  
+
+  noInterrupts();
+  if (Serial.available()) {
+    _b = Serial.read();
+    if (_b >= 0) {
+  
+      // Add to our circular serial_buf for later tranport
+      // through to the i2c line.
+      //
+      if (serial_buf_n < MSG_BUF_MAX) {
+        _cur = (serial_buf_s + serial_buf_n) % MSG_BUF_MAX;
+        serial_buf[_cur] = (char)_b;
+        serial_buf_n++;
+      }
+
+    }
+
+  }
+  interrupts();
+
+  noInterrupts();
+  if (i2c_msg_buf_rdy) {
+    for (i=0; i<MSG_BUF_MAX; i++) {
+      _tbuf[i] = i2c_msg_buf[i];
+    }
+    
+    i2c_msg_buf[0] = '\0';
+    i2c_msg_buf_n=0;
+    i2c_msg_buf_rdy=0;
+    _ser_print_rdy = 1;
+  }
+  interrupts();
+
+  if (_ser_print_rdy) {
+    Serial.print(_tbuf);
+  }
+
+}
+
+
+void send_i2c_data() {
+  int res;
+
+  if (serial_buf_n > 0) {
+    res = Wire.write((char)serial_buf[serial_buf_s]);
+    serial_buf_n--;
+    serial_buf_s++;
+    serial_buf_s %= MSG_BUF_MAX;
+  }
+
+}
+
+
+void receive_i2c_data(int howMany) {
+  char c;
+
+  while ( Wire.available()) {
+    c = Wire.read();
+
+    i2c_msg_buf[i2c_msg_buf_n++] = c;
+    if (i2c_msg_buf_n >= MSG_BUF_MAX) {
+      i2c_msg_buf_n = MSG_BUF_MAX-1;
+    }
+
+    if ((c == '\n') || (c == '\r')) {
+      i2c_msg_buf[i2c_msg_buf_n] = '\0';
+
+      // Our messages are terminated by '\n' and '\r'.
+      // Once a message is received, shuttle it to the
+      // serial line.
+      //
+      i2c_msg_buf_rdy=1;
+    }
+  }
+
+}

+ 4 - 0
busunit/PIU/shim/cmp.sh

@@ -0,0 +1,4 @@
+#!/bin/bash
+#
+
+gcc -li2c ichthyic-passthrough.c -o ichthyic-passthrough

+ 440 - 0
busunit/PIU/shim/ichthyic-passthrough.c

@@ -0,0 +1,440 @@
+// License: CC0 (https://creativecommons.org/publicdomain/zero/1.0/)
+//
+// To the extent possible under law, all copyright and related or neighboring rights are waived
+// on this file.
+// This work is published from: United States. 
+//
+
+// Example usage:
+//
+// socat -d -d pty,raw,echo=0,link=/tmp/ttyPIUich pty,raw,echo=0,link=/tmp/ttyPIU
+// ./ichthyic-passthrough -i /tmp/ttyPIUich -I /dev/i2c-1
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/time.h>
+
+#include <linux/i2c-dev.h>
+#include <i2c/smbus.h>
+
+#include <errno.h>
+#include <getopt.h>
+
+#include <stdint.h>
+
+#define ICHTHYIC_MAX_BUF 256
+#define ICHTHYIC_VERSION "0.1.0"
+#define ICHTHYIC_DEFAULT_I2C_ADDR 0x08
+
+int g_verbose = 0;
+struct option g_long_option[] = {
+  {"pty",       required_argument,  0, 'i'},
+  {"i2c",       required_argument,  0, 'I'},
+  {"log",       required_argument,  0, 'L'},
+  {"verbose",   no_argument,        0, 'v'},
+  {"version",   no_argument,        0, 'V'},
+  {"help",      no_argument,        0, 'h'},
+  {0, 0, 0, 0}
+};
+
+typedef struct ichthyic_type  {
+  uint8_t *i2c_fn;
+  int     i2c_adapter;
+  int     i2c_worker_addr;
+  int     i2c_fd;
+
+  uint32_t i2c_from_buf_n;
+  uint8_t *i2c_from_buf;
+
+  uint32_t i2c_to_buf_n;
+  uint8_t *i2c_to_buf;
+
+} ichthyic_t;
+
+
+void ichthyic_init(ichthyic_t *ich) {
+
+  ich->i2c_fn       = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF);
+  ich->i2c_from_buf = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF);
+  ich->i2c_to_buf   = (uint8_t *)malloc(sizeof(uint8_t)*ICHTHYIC_MAX_BUF);
+
+  ich->i2c_from_buf[0]  = '\0';
+  ich->i2c_fn[0]        = '\0';
+  ich->i2c_adapter      = -1;
+  ich->i2c_worker_addr  = -1;
+  ich->i2c_fd           = -1;
+
+  ich->i2c_to_buf_n     = 0;
+  ich->i2c_to_buf[0]    = '\0';
+
+  ich->i2c_from_buf_n   = 0;
+  ich->i2c_from_buf[0]  = '\0';
+}
+
+void ichthyic_clear(ichthyic_t *ich) {
+  if (ich) {
+    if (ich->i2c_fn)        { free(ich->i2c_fn); }
+    if (ich->i2c_from_buf)  { free(ich->i2c_from_buf); }
+    if (ich->i2c_to_buf)    { free(ich->i2c_to_buf); }
+  }
+}
+
+void ichthyic_free(ichthyic_t *ich) {
+  ichthyic_clear(ich);
+  if (ich) { free(ich); }
+}
+
+ichthyic_t *ichthyic_alloc() {
+  ichthyic_t *ich;
+  ich = malloc(sizeof(ichthyic_t));
+  ichthyic_init(ich);
+  return ich;
+}
+
+int ichthyic_i2c_appendbuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) {
+  int i, p;
+  p = ich->i2c_to_buf_n;
+  for (i=0, p=ich->i2c_to_buf_n; (i<nbuf) && (p<(ICHTHYIC_MAX_BUF-1)); i++, p++) {
+    ich->i2c_to_buf[p] = buf[i];
+  }
+  ich->i2c_to_buf[p] = '\0';
+  ich->i2c_to_buf_n = p;
+}
+
+int ichthyic_i2c_queuebuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) {
+  int i;
+  for (i=0; i<nbuf; i++) {
+    ich->i2c_to_buf[i] = buf[i];
+  }
+  ich->i2c_to_buf[nbuf] = '\0';
+  ich->i2c_to_buf_n = nbuf;
+}
+
+
+int ichthyic_i2c_read(ichthyic_t *ich) {
+  int32_t res, dn=0;
+
+  res = i2c_smbus_read_byte(ich->i2c_fd);
+  if (res<0) { return -1; }
+  if (res==0) { return 0; }
+
+  dn=1;
+  while (ich->i2c_from_buf_n < (ICHTHYIC_MAX_BUF-1)) {
+
+    ich->i2c_from_buf[ich->i2c_from_buf_n] = res;
+    ich->i2c_from_buf_n++;
+    ich->i2c_from_buf[ich->i2c_from_buf_n] = '\0';
+
+    res = i2c_smbus_read_byte(ich->i2c_fd);
+    if (res<0) { return -1; }
+    if (res==0) { break; }
+
+    dn++;
+  }
+
+  return dn;
+}
+
+int ichthyic_i2c_write(ichthyic_t *ich) {
+  int i;
+  int32_t res;
+
+  for (i=0; ich->i2c_to_buf[i]; i++) {
+    res = i2c_smbus_write_byte(ich->i2c_fd, ich->i2c_to_buf[i]);
+  }
+  ich->i2c_to_buf_n = 0;
+  ich->i2c_to_buf[0] = '\0';
+}
+
+int ichthyic_i2c_writebuf(ichthyic_t *ich, uint8_t *buf, uint32_t nbuf) {
+  uint32_t i;
+  int32_t res;
+  for (i=0; i<nbuf; i++) {
+    res = i2c_smbus_write_byte(ich->i2c_fd, buf[i]);
+  }
+}
+
+void show_version(FILE *fp) {
+  fprintf(fp, "ichthyic-passthrough version: %s\n", ICHTHYIC_VERSION);
+}
+
+void show_help(FILE *fp, char *i2c_fn, char *pty_fn) {
+  show_version(fp);
+  fprintf(fp, "\nusage:\n\n    ichthyic-passthrough [-i pty] [-I i2c] [-L logfn] [-h] [-v] [-V]\n\n");
+  fprintf(fp, "  [-D]         use defaults (i2c:%s, pty:%s)\n", i2c_fn, pty_fn);
+  fprintf(fp, "  [-i pty]     pty (--pty)\n");
+  fprintf(fp, "  [-I i2c]     I2C device interface (--i2c)\n");
+  fprintf(fp, "  [-L logfn]   log file (--log)\n");
+  fprintf(fp, "  [-v]         verbosity level (can be used multiple times to increase verbosity) (--verbose)\n");
+  fprintf(fp, "  [-V]         print out version and exit (--version)\n");
+  fprintf(fp, "  [-h]         help (this screen) (--help)\n");
+  fprintf(fp, "\n");
+}
+
+void _print_buf(FILE *fp, char *buf, int buf_n) {
+  int i;
+
+  for (i=0; i<buf_n; i++) {
+    if (buf[i] == '\n')      { fprintf(fp, "`"); }
+    else if (buf[i] == '\r') { fprintf(fp, "~"); }
+    else if ((buf[i] >= ' ') &&
+             (buf[i] <= '~')) {
+      fprintf(fp,"%c", buf[i]);
+    }
+    else { fprintf(fp, "<%x>", buf[i]); }
+  }
+  fprintf(fp, "\n");
+  fflush(fp);
+}
+
+int main(int argc, char **argv) {
+  int i, ret, fd, adapter_nr, ch, dn;
+  int opt_idx;
+  char fn[32];
+  int worker_addr = 0x08;
+
+  struct timespec _sleepy, _sleepy_rem;
+
+  char *pty_fn;
+  int pty_fd=-1, pty_fd_flag = 0;
+  int _n=0, _buf_n=32, _s=0;
+  char _buf[32];
+
+  FILE *log_fp = stdout;
+  char *log_fn;
+
+  int _ok = 0;
+
+  ichthyic_t ich;
+
+  pty_fn = (char *)malloc(sizeof(char)*ICHTHYIC_MAX_BUF);
+  log_fn = (char *)malloc(sizeof(char)*ICHTHYIC_MAX_BUF);
+
+  ichthyic_init(&ich);
+  ich.i2c_worker_addr = ICHTHYIC_DEFAULT_I2C_ADDR;
+  ich.i2c_adapter = 1;
+  snprintf(ich.i2c_fn, 31, "/dev/i2c-%i", ich.i2c_adapter);
+
+  g_verbose = 0;
+  pty_fn[0] = '\0';
+  log_fn[0] = '\0';
+
+  while ((ch = getopt_long(argc, argv, "hi:I:L:vVD", g_long_option, &opt_idx)) >= 0) {
+    switch(ch) {
+      case 0:
+        break;
+
+      case 'D':
+        _ok = 1;
+        break;
+
+      case 'i':
+        strncpy(pty_fn, optarg, ICHTHYIC_MAX_BUF-1);
+        pty_fn[ICHTHYIC_MAX_BUF-1] = '\0';
+        _ok=1;
+        break;
+
+      case 'I':
+        strncpy(ich.i2c_fn, optarg, ICHTHYIC_MAX_BUF-1);
+        ich.i2c_fn[ICHTHYIC_MAX_BUF-1] = '\0';
+        _ok=1;
+        break;
+
+      case 'L':
+        strncpy(log_fn, optarg, ICHTHYIC_MAX_BUF-1);
+        log_fn[ICHTHYIC_MAX_BUF-1] = '\0';
+        break;
+
+      case 'v':
+        g_verbose++;
+        _ok=1;
+        break;
+
+      case 'V':
+        show_version(stdout);
+        exit(0);
+      case 'h':
+        show_help(stdout, ich.i2c_fn, pty_fn);
+        exit(0);
+      default:
+        show_help(stderr, ich.i2c_fn, pty_fn);
+        exit(-1);
+        break;
+
+    }
+  }
+
+  if (_ok==0) {
+    show_help(stderr, ich.i2c_fn, pty_fn);
+    exit(-1);
+  }
+
+  if ((g_verbose > 0) && (strlen(log_fn) > 0)) {
+    log_fp = fopen(log_fn, "w");
+    if (!log_fp) {
+      perror(log_fn);
+      exit(-1);
+    }
+  }
+
+  // 1000000000 ns in 1 sec
+  //
+  _sleepy.tv_sec = 0;
+  _sleepy.tv_nsec = 1000000;
+
+  // stdin
+  //
+  if (strlen((const char *)pty_fn) == 0) {
+    pty_fd = 0;
+  }
+  else {
+    pty_fd = open(pty_fn, O_RDWR);
+
+    if (pty_fd < 0) {
+      perror(pty_fn);
+      exit(-1);
+    }
+
+  }
+  pty_fd_flag = fcntl(pty_fd, F_GETFL, 0);
+  fcntl(pty_fd, F_SETFL, pty_fd_flag | O_NONBLOCK);
+
+
+  ich.i2c_fd = open(ich.i2c_fn, O_RDWR);
+  if (ich.i2c_fd < 0) {
+    perror(ich.i2c_fn);
+    exit(-1);
+  }
+
+  if (ioctl(ich.i2c_fd, I2C_SLAVE, ich.i2c_worker_addr) < 0) {
+    perror(ich.i2c_fn);
+    exit(-2);
+  }
+
+  while (1) {
+
+    _n = read(pty_fd, _buf, _buf_n);
+    if (_n<0) {
+
+      // pty_fd is non blocking, so only non-EAGAIN
+      // errors are real
+      //
+      if (errno != EAGAIN) {
+        perror("stdin:");
+      }
+    }
+
+    // A 0-byte read means the file descriptor has been closed
+    //
+    else if (_n==0) {
+      if (g_verbose>0) {
+        fprintf(log_fp, "# input closed, cleaning up\n");
+        fflush(log_fp);
+      }
+      break;
+    }
+
+    // Otherwise we have actual data that we want to append
+    // to our buffer
+    //
+    if (_n>0) {
+
+      if (g_verbose > 0) {
+        fprintf(log_fp, "#[%i]:", _n);
+        for (i=0; i<_n; i++) { fprintf(log_fp, "%c", _buf[i]); }
+        fprintf(log_fp, "\n");
+        fflush(log_fp);
+      }
+
+      ichthyic_i2c_appendbuf(&ich, _buf, _n);
+    }
+
+    // Write any data to our I2C Line
+    //
+    ichthyic_i2c_write(&ich);
+
+    // And read any I2C data that might be ready
+    //
+    ret = ichthyic_i2c_read(&ich);
+    if (ret<0) { perror("i2c read"); }
+    else if (ret > 0) {
+
+      if (g_verbose > 1) {
+        fprintf(log_fp, "## read +%i (%i,%i) i2c bytes\n", ret, ret, ich.i2c_from_buf_n);
+        fflush(log_fp);
+      }
+    }
+
+
+    if (ich.i2c_from_buf_n > 0) {
+
+      // If our buffer has a lf or cr at the end, flush it
+      //
+      if ((ich.i2c_from_buf[ ich.i2c_from_buf_n-1 ] == '\r') ||
+          (ich.i2c_from_buf[ ich.i2c_from_buf_n-1 ] == '\n')) {
+
+        _n = ich.i2c_from_buf_n;
+        _s = 0;
+        do {
+          dn = write(pty_fd, ich.i2c_from_buf + _s, _n);
+          if (dn<0) {
+            perror("write error");
+            exit(-1);
+          }
+          _n -= dn;
+          _s += dn;
+        } while (_n > 0);
+
+        if (g_verbose > 0) {
+          fprintf(log_fp, "# [%i]>:", ich.i2c_from_buf_n);
+          _print_buf(log_fp, ich.i2c_from_buf, ich.i2c_from_buf_n);
+        }
+
+        ich.i2c_from_buf[0] = '\0';
+        ich.i2c_from_buf_n = 0;
+      }
+
+      // If we've hit the maximum size of our buffer, flush it regardless
+      //
+      else if (ich.i2c_from_buf_n >= (ICHTHYIC_MAX_BUF-1)) {
+        _n = ich.i2c_from_buf_n;
+        _s = 0;
+        do {
+          dn = write(pty_fd, ich.i2c_from_buf + _s, _n);
+          if (dn<0) {
+            perror("write error while flushing");
+            exit(-1);
+          }
+          _n -= dn;
+          _s += dn;
+        } while (_n > 0);
+
+        if (g_verbose > 0) {
+          fprintf(log_fp, "# i2c_from_buf_n (%i) == %i, flushing: ", ich.i2c_from_buf_n, ICHTHYIC_MAX_BUF);
+          _print_buf(log_fp, ich.i2c_from_buf, ich.i2c_from_buf_n);
+        }
+
+        ich.i2c_from_buf[0] = '\0';
+        ich.i2c_from_buf_n = 0;
+
+      }
+
+    }
+
+    nanosleep(&_sleepy, &_sleepy_rem);
+
+  }
+
+  close(ich.i2c_fd);
+  ichthyic_clear(&ich);
+
+  if (log_fp != stdout) { fclose(log_fp); }
+}

+ 8 - 0
busunit/scripts/check-i2c-speed

@@ -0,0 +1,8 @@
+#!/bin/bash
+#
+# License: CC0
+#
+
+var="$(xxd /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency | awk -F': ' '{print $2}')"
+var=${var//[[:blank:].\}]/}
+printf "%d\n" 0x$var