add spi2Csr tools : Python Host & Arduino Uart<-->Spi bridge
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 13 Sep 2012 09:34:19 +0000 (11:34 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 13 Sep 2012 09:34:19 +0000 (11:34 +0200)
examples/de1/top.py
spi2Csr/tools/__init__.py [new file with mode: 0644]
spi2Csr/tools/arduino/spiFpga.cpp [new file with mode: 0644]
spi2Csr/tools/arduino/spiFpga.h [new file with mode: 0644]
spi2Csr/tools/arduino/uart2Spi.pde [new file with mode: 0644]
spi2Csr/tools/uart2Spi.py [new file with mode: 0644]

index e272edaf34e46592e22aabb098acc9eabe6c1484..b1ba04f77cea085ae6ea8248c7ff68ccdb3e2533 100644 (file)
@@ -76,7 +76,7 @@ def get():
 
        # migIo
        migIo0 = migIo.MigIo(8,"IO")
-
+       
        # Trigger
        term0 = trigger.Term(trig_width)
        trigger0 = trigger.Trigger(TRIGGER_ADDR, trig_width, dat_width, [term0])
@@ -86,7 +86,7 @@ def get():
        
        # Spi2Csr
        spi2csr0 = spi2Csr.Spi2Csr(16,8)
-
+       
        # Csr Interconnect
        csrcon0 = csr.Interconnect(spi2csr0.csr, 
                        [
@@ -124,7 +124,7 @@ def get():
                recorder0.trig_hit.eq(trigger0.hit)
        ]
        
-
+       
        # HouseKeeping
        in_clk = Signal()
        in_rst_n = Signal()
diff --git a/spi2Csr/tools/__init__.py b/spi2Csr/tools/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/spi2Csr/tools/arduino/spiFpga.cpp b/spi2Csr/tools/arduino/spiFpga.cpp
new file mode 100644 (file)
index 0000000..0787072
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * spiFpga
+ * Copyright (C) 2012 by Florent Kermarrec <florent@enjoy-digital.fr>
+ * Copyright (C) 2011 by James Bowman <jamesb@excamera.com> 
+ *
+ */
+#include "WProgram.h"
+#include <avr/pgmspace.h>
+#include <SPI.h>
+#include <spiFpga.h>
+
+SFClass SF;
+
+void SFClass::begin()
+{
+       pinMode(SS_PIN, OUTPUT);
+       SPI.begin();
+       SPI.setClockDivider(SPI_CLOCK_DIV2);
+       SPI.setBitOrder(MSBFIRST);
+       SPI.setDataMode(SPI_MODE0);
+       SPSR = (1 << SPI2X);
+       digitalWrite(SS_PIN, HIGH);
+}
+
+void SFClass::end() {
+}
+
+void SFClass::__start(unsigned int addr)
+{
+       digitalWrite(SS_PIN, LOW);
+       SPI.transfer(highByte(addr));
+       SPI.transfer(lowByte(addr));  
+}
+
+void SFClass::__wstart(unsigned int addr)
+{
+       __start(0x8000|addr);
+}
+
+void SFClass::__end()
+{
+       digitalWrite(SS_PIN, HIGH);
+}
+
+char SFClass::rd(unsigned int addr)
+{
+       __start(addr);
+       char r = SPI.transfer(0);
+       __end();
+       return r;
+}
+
+void SFClass::wr(unsigned int addr, char v)
+{
+       __wstart(addr);
+       SPI.transfer(v);
+       __end();
+}
\ No newline at end of file
diff --git a/spi2Csr/tools/arduino/spiFpga.h b/spi2Csr/tools/arduino/spiFpga.h
new file mode 100644 (file)
index 0000000..c28837b
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * spiFpga
+ * Copyright (C) 2012 by Florent Kermarrec <florent@enjoy-digital.fr>
+ * Copyright (C) 2011 by James Bowman <jamesb@excamera.com> 
+ *
+ */
+
+#ifndef _SF_H_INCLUDED
+#define _SF_H_INCLUDED
+
+#ifndef SS_PIN
+#define SS_PIN 10
+#endif
+
+class SFClass {
+public:
+  static void begin();
+  static void end();
+  static void __start(unsigned int addr);
+  static void __wstart(unsigned int addr);
+  static void __end(void);
+  static char rd(unsigned int addr);
+  static void wr(unsigned int addr, char v);
+};
+
+extern SFClass SF;
+
+#endif
diff --git a/spi2Csr/tools/arduino/uart2Spi.pde b/spi2Csr/tools/arduino/uart2Spi.pde
new file mode 100644 (file)
index 0000000..38cec1c
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+  Uart2Spi
+  Copyright 2012 - Florent Kermarrec - florent@enjoy-digital.fr
+  
+  Protocol:
+  -Write : 0x01 + 16b Addr + 8b Data
+  -Read  : 0x02 + 16b Addr + 8b Don't Care
+
+  Todo:
+  Support Spi Burst Mode
+  
+ */
+#include <SPI.h>
+#include <spiFpga.h>
+
+void setup() {
+  SF.begin();
+  SPI.setClockDivider(8);
+  Serial.begin(115200);
+}
+
+int  cmd = 0;
+
+void loop()
+{
+ if (Serial.available() == 4)
+ {
+   cmd = Serial.read();
+   //Write Cmd
+   if (cmd == 0x01)
+   {
+     char addrMsb = Serial.read();
+     char addrLsb = Serial.read();
+     char data = Serial.read();
+     SF.wr(addrMsb<<8|addrLsb, data);
+   }
+   //Read Cmd
+   if (cmd == 0x02)
+   {
+     char addrMsb = Serial.read();
+     char addrLsb = Serial.read();
+     Serial.read();
+     char data;
+     data = SF.rd(addrMsb<<8|addrLsb);
+     Serial.print(data);
+   }
+ }
+}
\ No newline at end of file
diff --git a/spi2Csr/tools/uart2Spi.py b/spi2Csr/tools/uart2Spi.py
new file mode 100644 (file)
index 0000000..8a60988
--- /dev/null
@@ -0,0 +1,38 @@
+import string
+import time
+import serial
+
+def write_ascii(uart, data):
+       uart.write(bytes("%c" % data, encoding="ascii"))
+
+class Uart2Spi:
+       def __init__(self, port, baudrate):
+               self.port = port
+               self.baudrate = baudrate
+               self.uart = serial.Serial(port, baudrate, timeout=0.01)
+       
+       def read(self, addr):
+               while True:
+                       write_ascii(self.uart, 0x02)
+                       write_ascii(self.uart, (addr>>8)&0xFF)
+                       write_ascii(self.uart, (addr&0xFF))
+                       write_ascii(self.uart, 0x00)
+                       read = self.uart.read()
+                       if len(read) == 1:
+                               break
+               return read[0]
+       
+       def write(self, addr, data):
+               write_ascii(self.uart, 0x01)
+               write_ascii(self.uart, (addr>>8)&0xFF)
+               write_ascii(self.uart, (addr&0xFF))
+               write_ascii(self.uart, data)
+
+def main():
+       csr = Uart2Spi(1,115200)
+       for i in range(100):
+               csr.write(0x0000,i)
+               print(csr.read(0x0000))
+
+if __name__ == '__main__':
+  main()
\ No newline at end of file