add spi2Csr tools : Python Host & Arduino Uart<-->Spi bridge

This commit is contained in:
Florent Kermarrec 2012-09-13 11:34:19 +02:00
parent c7e2b0c43e
commit f4369c917f
6 changed files with 176 additions and 3 deletions

View 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()

View file

View file

@ -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();
}

View file

@ -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

View file

@ -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);
}
}
}

38
spi2Csr/tools/uart2Spi.py Normal file
View file

@ -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()