commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r6917 - gnuradio/branches/developers/matt/u2f/firmware


From: eb
Subject: [Commit-gnuradio] r6917 - gnuradio/branches/developers/matt/u2f/firmware
Date: Wed, 14 Nov 2007 20:49:17 -0700 (MST)

Author: eb
Date: 2007-11-14 20:49:17 -0700 (Wed, 14 Nov 2007)
New Revision: 6917

Added:
   gnuradio/branches/developers/matt/u2f/firmware/eeprom.c
   gnuradio/branches/developers/matt/u2f/firmware/i2c.c
   gnuradio/branches/developers/matt/u2f/firmware/i2c.h
Modified:
   gnuradio/branches/developers/matt/u2f/firmware/Makefile.am
   gnuradio/branches/developers/matt/u2f/firmware/hal_io.c
   gnuradio/branches/developers/matt/u2f/firmware/memory_map.h
Log:
work-in-progress i2c and eeprom

Modified: gnuradio/branches/developers/matt/u2f/firmware/Makefile.am
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/Makefile.am  2007-11-15 
02:23:31 UTC (rev 6916)
+++ gnuradio/branches/developers/matt/u2f/firmware/Makefile.am  2007-11-15 
03:49:17 UTC (rev 6917)
@@ -50,6 +50,7 @@
        eth_mac.c \
        hal_io.c \
        hal_uart.c \
+       i2c.c \
        pic.c \
        spi.c \
        u2_init.c       

Added: gnuradio/branches/developers/matt/u2f/firmware/eeprom.c
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/eeprom.c                     
        (rev 0)
+++ gnuradio/branches/developers/matt/u2f/firmware/eeprom.c     2007-11-15 
03:49:17 UTC (rev 6917)
@@ -0,0 +1,70 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+static const int EEPROM_PAGESIZE = 16;
+
+bool
+eeprom_write (int i2c_addr, int eeprom_offset, const void *buf, int len)
+{
+  unsigned char cmd[2];
+  const unsigned char *p = (unsigned char *) buf;
+  
+  // The simplest thing that could possibly work:
+  //   all writes are single byte writes.
+  //
+  // We could speed this up using the page write feature,
+  // but we write so infrequently, why bother...
+
+  while (len-- > 0){
+    cmd[0] = eeprom_offset++;
+    cmd[1] = *p++;
+    bool r = i2c_write (i2c_addr, cmd, sizeof (cmd));
+    mdelay (10);               // delay 10ms worst case write time
+    if (!r)
+      return false;
+  }
+  return true;
+}
+
+bool
+eeprom_read (int i2c_addr, int eeprom_offset, void *buf, int len)
+{
+  unsigned char *p = (unsigned char *) buf;
+
+  // We setup a random read by first doing a "zero byte write".
+  // Writes carry an address.  Reads use an implicit address.
+
+  unsigned char cmd[1];
+  cmd[0] = eeprom_offset;
+  if (!i2c_write (udh, i2c_addr, cmd, sizeof (cmd)))
+    return false;
+
+  while (len > 0){
+    // int n = std::min (len, MAX_EP0_PKTSIZE);
+    int n = len;
+    if (!i2c_read (i2c_addr, p, n))
+      return false;
+    len -= n;
+    p += n;
+  }
+  return true;
+}
+ 


Property changes on: gnuradio/branches/developers/matt/u2f/firmware/eeprom.c
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: gnuradio/branches/developers/matt/u2f/firmware/hal_io.c
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/hal_io.c     2007-11-15 
02:23:31 UTC (rev 6916)
+++ gnuradio/branches/developers/matt/u2f/firmware/hal_io.c     2007-11-15 
03:49:17 UTC (rev 6917)
@@ -23,6 +23,7 @@
 
 #include "hal_io.h"
 #include "memory_map.h"
+#include "hal_uart.h"
 
 unsigned int
 hal_gpio_mode_mask(int high_bitno, int low_bitno, int mode)

Added: gnuradio/branches/developers/matt/u2f/firmware/i2c.c
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/i2c.c                        
        (rev 0)
+++ gnuradio/branches/developers/matt/u2f/firmware/i2c.c        2007-11-15 
03:49:17 UTC (rev 6917)
@@ -0,0 +1,130 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "i2c.h"
+#include "memory_map.h"
+#include "stdint.h"
+
+#define MAX_WB_DIV 4   // maximum wishbone divisor (from 100 MHz MASTER_CLK)
+
+// prescaler divisor values for 100 kHz I2C [uses 5 * SCLK internally]
+
+#define PRESCALER(wb_div) (((MASTER_CLK_RATE/(wb_div)) / (5 * 100000)) - 1)
+
+static uint16_t prescaler_values[MAX_WB_DIV+1] = {
+  0xffff,      // 0: can't happen
+  PRESCALER(1),        // 1: 100 MHz
+  PRESCALER(2), // 2:  50 MHz
+  PRESCALER(3), // 3:  33.333 MHz
+  PRESCALER(4), // 4:  25 MHz
+};
+
+void
+i2c_init(void)
+{
+  i2c_regs->ctrl = 0;          // disable core
+  
+  // setup prescaler depending on wishbone divisor
+  int wb_div = hwconfig_wishbone_divisor();
+  if (wb_div > MAX_WB_DIV)
+    wb_div = MAX_WB_DIV;
+
+  i2c_regs->prescaler_lo = prescaler_values[wb_div] & 0xff;
+  i2c_regs->prescaler_hi = (prescaler_values[wb_div] >> 8) & 0xff;
+
+  i2c_regs->ctrl = I2C_CTRL_EN;        // enable core
+
+  // FIXME interrupt driven?
+}
+
+static inline void
+wait_for_xfer(void)
+{
+  while (i2c_regs->cmd_status & I2C_ST_TIP)    // wait for xfer to complete
+    ;
+}
+
+static inline bool
+wait_chk_ack(void)
+{
+  wait_for_xfer();
+
+  if ((i2c_regs->cmd_status & I2C_ST_RXACK) != 0){     // target NAK'd
+    return false;
+  }
+  return true;
+}
+
+bool 
+i2c_read (unsigned char i2c_addr, unsigned char *buf, unsigned int len)
+{
+  if (len == 0)                        // reading zero bytes always works
+    return true;
+
+  while (i2c_regs->cmd_status & I2C_ST_BUSY)
+    ;
+
+  i2c_regs->data = (i2c_addr << 1) | 1;         // 7 bit address and read bit 
(1)
+  // generate START and write addr
+  i2c_regs->cmd_status = I2C_CMD_WR | I2C_CMD_START;
+  if (!wait_chk_ack())
+    goto fail;
+
+  for (; len > 0; buf++, len--){
+    i2c_regs->cmd_status = I2C_CMD_RD | (len == 1 ? (I2C_CMD_NACK | 
I2C_CMD_STOP) : 0);
+    wait_for_xfer();
+    *buf = i2c_regs->data;
+  }
+  return true;
+
+ fail:
+  i2c_regs->cmd_status = I2C_CMD_STOP;  // generate STOP
+  return false;
+}
+
+
+bool 
+i2c_write(unsigned char i2c_addr, const unsigned char *buf, unsigned int len)
+{
+  while (i2c_regs->cmd_status & I2C_ST_BUSY)
+    ;
+
+  i2c_regs->data = (i2c_addr << 1) | 0;         // 7 bit address and write bit 
(0)
+
+  // generate START and write addr (and maybe STOP)
+  i2c_regs->cmd_status = I2C_CMD_WR | I2C_CMD_START | (len == 0 ? I2C_CMD_STOP 
: 0);  
+  if (!wait_chk_ack())
+    goto fail;
+
+  for (; len > 0; buf++, len--){
+    i2c_regs->data = *buf;
+    i2c_regs->cmd_status = I2C_CMD_WR | (len == 1 ? I2C_CMD_STOP : 0);
+    if (!wait_chk_ack())
+      goto fail;
+  }
+  return true;
+
+ fail:
+  i2c_regs->cmd_status = I2C_CMD_STOP;  // generate STOP
+  return false;
+}
+
+  


Property changes on: gnuradio/branches/developers/matt/u2f/firmware/i2c.c
___________________________________________________________________
Name: svn:eol-style
   + native

Added: gnuradio/branches/developers/matt/u2f/firmware/i2c.h
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/i2c.h                        
        (rev 0)
+++ gnuradio/branches/developers/matt/u2f/firmware/i2c.h        2007-11-15 
03:49:17 UTC (rev 6917)
@@ -0,0 +1,41 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef INCLUDED_I2C_H
+#define INCLUDED_I2C_H
+
+#include "bool.h"
+
+void i2c_init(void);
+bool i2c_read (unsigned char i2c_addr, unsigned char *buf, unsigned int len);
+bool i2c_write(unsigned char i2c_addr, const unsigned char *buf, unsigned int 
len);
+
+
+// Write 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
+// Which EEPROM is determined by i2c_addr.  See i2c_addr.h
+
+bool eeprom_write (int i2c_addr, int eeprom_offset, const void *buf, int len);
+
+// Read 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
+// Which EEPROM is determined by i2c_addr.  See i2c_addr.h
+
+bool eeprom_read (int i2c_addr, int eeprom_offset, void *buf, int len);
+
+#endif /* INCLUDED_I2C_H */


Property changes on: gnuradio/branches/developers/matt/u2f/firmware/i2c.h
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: gnuradio/branches/developers/matt/u2f/firmware/memory_map.h
===================================================================
--- gnuradio/branches/developers/matt/u2f/firmware/memory_map.h 2007-11-15 
02:23:31 UTC (rev 6916)
+++ gnuradio/branches/developers/matt/u2f/firmware/memory_map.h 2007-11-15 
03:49:17 UTC (rev 6917)
@@ -104,7 +104,7 @@
   volatile unsigned char  cmd_status;  // wr = command reg;  rd = status reg
 } i2c_regs_t;
 
-#define i2c_regs ((i2c_regs_regs_t *) I2C_BASE)
+#define i2c_regs ((i2c_regs_t *) I2C_BASE)
 
 #define        I2C_CTRL_EN     (1 << 7)        // core enable
 #define        I2C_CTRL_IE     (1 << 6)        // interrupt enable
@@ -112,11 +112,11 @@
 //
 // STA, STO, RD, WR, and IACK bits are cleared automatically
 //
-#define        I2C_CMD_STA     (1 << 7)        // generate (repeated) start 
condition
-#define I2C_CMD_STO    (1 << 6)        // generate stop condition
+#define        I2C_CMD_START   (1 << 7)        // generate (repeated) start 
condition
+#define I2C_CMD_STOP   (1 << 6)        // generate stop condition
 #define        I2C_CMD_RD      (1 << 5)        // read from slave
 #define I2C_CMD_WR     (1 << 4)        // write to slave
-#define        I2C_CMD_ACK     (1 << 3)        // when a rcvr, send ACK 
(ACK=0) or NACK (ACK=1)
+#define        I2C_CMD_NACK    (1 << 3)        // when a rcvr, send ACK 
(ACK=0) or NACK (ACK=1)
 #define I2C_CMD_RSVD_2 (1 << 2)        // reserved
 #define        I2C_CMD_RSVD_1  (1 << 1)        // reserved
 #define I2C_CMD_IACK   (1 << 0)        // set to clear pending interrupt





reply via email to

[Prev in Thread] Current Thread [Next in Thread]