From f4369c917f0943bd8cc0783454ea9fecca8b956d Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 13 Sep 2012 11:34:19 +0200 Subject: [PATCH] add spi2Csr tools : Python Host & Arduino Uart<-->Spi bridge --- examples/de1/top.py | 6 +-- spi2Csr/tools/__init__.py | 0 spi2Csr/tools/arduino/spiFpga.cpp | 59 ++++++++++++++++++++++++++++++ spi2Csr/tools/arduino/spiFpga.h | 28 ++++++++++++++ spi2Csr/tools/arduino/uart2Spi.pde | 48 ++++++++++++++++++++++++ spi2Csr/tools/uart2Spi.py | 38 +++++++++++++++++++ 6 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 spi2Csr/tools/__init__.py create mode 100644 spi2Csr/tools/arduino/spiFpga.cpp create mode 100644 spi2Csr/tools/arduino/spiFpga.h create mode 100644 spi2Csr/tools/arduino/uart2Spi.pde create mode 100644 spi2Csr/tools/uart2Spi.py diff --git a/examples/de1/top.py b/examples/de1/top.py index e272edaf..b1ba04f7 100644 --- a/examples/de1/top.py +++ b/examples/de1/top.py @@ -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 index 00000000..e69de29b diff --git a/spi2Csr/tools/arduino/spiFpga.cpp b/spi2Csr/tools/arduino/spiFpga.cpp new file mode 100644 index 00000000..0787072e --- /dev/null +++ b/spi2Csr/tools/arduino/spiFpga.cpp @@ -0,0 +1,59 @@ +/* + * spiFpga + * Copyright (C) 2012 by Florent Kermarrec + * Copyright (C) 2011 by James Bowman + * + */ + +#include "WProgram.h" +#include +#include +#include + +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 index 00000000..c28837b2 --- /dev/null +++ b/spi2Csr/tools/arduino/spiFpga.h @@ -0,0 +1,28 @@ +/* + * spiFpga + * Copyright (C) 2012 by Florent Kermarrec + * Copyright (C) 2011 by James Bowman + * + */ + +#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 index 00000000..38cec1c4 --- /dev/null +++ b/spi2Csr/tools/arduino/uart2Spi.pde @@ -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 +#include + +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 index 00000000..8a60988e --- /dev/null +++ b/spi2Csr/tools/uart2Spi.py @@ -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 -- 2.30.2