Skip to content

Commit df7d15d

Browse files
committedFeb 29, 2016
runtime: refactor spi into rt2wb
1 parent eb01b0b commit df7d15d

File tree

7 files changed

+60
-67
lines changed

7 files changed

+60
-67
lines changed
 

‎artiq/coredevice/rt2wb.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from artiq.language.core import *
2+
from artiq.language.types import *
3+
4+
5+
@syscall
6+
def rt2wb_write(time_mu: TInt64, channel: TInt32, addr: TInt32, data: TInt32
7+
) -> TNone:
8+
raise NotImplementedError("syscall not simulated")
9+
10+
11+
@syscall
12+
def rt2wb_read_sync(time_mu: TInt64, channel: TInt32, addr: TInt32,
13+
duration_mu: TInt32) -> TInt32:
14+
raise NotImplementedError("syscall not simulated")

‎artiq/coredevice/spi.py

+18-27
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
from artiq.language.core import *
22
from artiq.language.types import *
3-
4-
5-
@syscall
6-
def spi_write(time_mu: TInt64, channel: TInt32, addr: TInt32, data: TInt32
7-
) -> TNone:
8-
raise NotImplementedError("syscall not simulated")
9-
10-
11-
@syscall
12-
def spi_read(time_mu: TInt64, channel: TInt32, addr: TInt32) -> TInt32:
13-
raise NotImplementedError("syscall not simulated")
3+
from artiq.coredevice.rt2wb import *
144

155

166
SPI_DATA_ADDR, SPI_XFER_ADDR, SPI_CONFIG_ADDR = range(3)
@@ -34,55 +24,56 @@ class SPIMaster:
3424
"""
3525
def __init__(self, dmgr, ref_period, channel):
3626
self.core = dmgr.get("core")
37-
self.ref_period_mu = int(seconds_to_mu(ref_period, self.core), 64)
27+
self.ref_period_mu = seconds_to_mu(ref_period, self.core)
3828
self.channel = channel
3929
self.write_div = 0
4030
self.read_div = 0
4131
# a full transfer takes prep_mu + xfer_mu
42-
self.prep_mu = int(0, 64)
43-
# chaned transfers can happen every xfer_mu
44-
self.xfer_mu = int(0, 64)
32+
self.prep_mu = 0
33+
# chained transfers can happen every xfer_mu
34+
self.xfer_mu = 0
4535
# The second transfer of a chain be written ref_period_mu
4636
# after the first. Read data is available every xfer_mu starting
4737
# a bit before prep_mu + xfer_mu.
4838

4939
@portable
5040
def predict_xfer_mu(self, write_length, read_length):
5141
# this is only the intrinsic bit cycle duration
52-
return self.ref_period_mu*(
42+
return int(self.ref_period_mu*(
5343
write_length*self.write_div +
54-
read_length*self.read_div)
44+
read_length*self.read_div))
5545

5646
@portable
5747
def predict_prep_mu(self, write_div):
58-
return self.ref_period_mu*(
48+
return int(self.ref_period_mu*(
5949
2 + # intermediate transfers
6050
# one write_div for the wait+idle cycle
61-
self.write_div)
51+
self.write_div))
6252

6353
@kernel
6454
def set_config(self, flags=0, write_div=6, read_div=6):
6555
self.write_div = write_div
6656
self.read_div = read_div
6757
self.prep_mu = self.predict_prep_mu(write_div)
68-
spi_write(now_mu(), self.channel, SPI_CONFIG_ADDR, flags |
69-
((write_div - 2) << 8) | ((read_div - 2) << 20))
58+
rt2wb_write(now_mu(), self.channel, SPI_CONFIG_ADDR, flags |
59+
((write_div - 2) << 8) | ((read_div - 2) << 20))
7060
delay_mu(self.ref_period_mu)
7161

7262
@kernel
7363
def set_xfer(self, chip_select=0, write_length=0, read_length=0):
7464
self.xfer_mu = self.predict_xfer_mu(write_length, read_length)
75-
spi_write(now_mu(), self.channel, SPI_XFER_ADDR,
76-
chip_select | (write_length << 16) | (read_length << 24))
65+
rt2wb_write(now_mu(), self.channel, SPI_XFER_ADDR,
66+
chip_select | (write_length << 16) | (read_length << 24))
7767
delay_mu(self.ref_period_mu)
7868

7969
@kernel
8070
def write(self, data):
81-
spi_write(now_mu(), self.channel, SPI_DATA_ADDR, data)
82-
delay_mu(self.prep_mu + self.xfer_mu)
71+
rt2wb_write(now_mu(), self.channel, SPI_DATA_ADDR, data)
72+
delay_mu(int(self.prep_mu + self.xfer_mu))
8373

8474
@kernel
85-
def read(self):
86-
r = spi_read(now_mu(), self.channel, SPI_DATA_ADDR)
75+
def read_sync(self):
76+
r = rt2wb_read_sync(now_mu(), self.channel, SPI_DATA_ADDR,
77+
int(self.ref_period_mu))
8778
delay_mu(self.ref_period_mu)
8879
return r

‎artiq/runtime/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ OBJECTS := isr.o clock.o rtiocrg.o flash_storage.o mailbox.o \
77
session.o log.o analyzer.o moninj.o net_server.o bridge_ctl.o \
88
ksupport_data.o kloader.o test_mode.o main.o
99
OBJECTS_KSUPPORT := ksupport.o artiq_personality.o mailbox.o \
10-
bridge.o rtio.o ttl.o dds.o spi.o
10+
bridge.o rtio.o ttl.o dds.o rt2wb.o
1111

1212
CFLAGS += -I$(LIBALLOC_DIRECTORY) \
1313
-I$(MISOC_DIRECTORY)/software/include/dyld \

‎artiq/runtime/ksupport.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "artiq_personality.h"
1616
#include "ttl.h"
1717
#include "dds.h"
18-
#include "spi.h"
1918
#include "rtio.h"
19+
#include "rt2wb.h"
2020

2121
double round(double x);
2222

@@ -122,8 +122,8 @@ static const struct symbol runtime_exports[] = {
122122
{"dds_batch_exit", &dds_batch_exit},
123123
{"dds_set", &dds_set},
124124

125-
{"spi_write", &spi_write},
126-
{"spi_read", &spi_read},
125+
{"rt2wb_write", &rt2wb_write},
126+
{"rt2wb_read_sync", &rt2wb_read_sync},
127127

128128
{"cache_get", &cache_get},
129129
{"cache_put", &cache_put},
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#include <generated/csr.h>
2-
#include <stdio.h>
32

43
#include "artiq_personality.h"
54
#include "rtio.h"
6-
#include "log.h"
7-
#include "spi.h"
5+
#include "rt2wb.h"
86

97

10-
#define DURATION_WRITE (1 << CONFIG_RTIO_FINE_TS_WIDTH)
11-
12-
void spi_write(long long int timestamp, int channel, int addr,
8+
void rt2wb_write(long long int timestamp, int channel, int addr,
139
unsigned int data)
1410
{
1511
rtio_chan_sel_write(channel);
@@ -20,31 +16,33 @@ void spi_write(long long int timestamp, int channel, int addr,
2016
}
2117

2218

23-
unsigned int spi_read(long long int timestamp, int channel, int addr)
19+
unsigned int rt2wb_read_sync(long long int timestamp, int channel,
20+
int addr, int duration)
2421
{
2522
int status;
26-
long long int time_limit = timestamp + DURATION_WRITE;
27-
unsigned int r;
23+
unsigned int data;
2824

29-
spi_write(timestamp, channel, addr | SPI_WB_READ, 0);
25+
rt2wb_write(timestamp, channel, addr, 0);
3026

3127
while((status = rtio_i_status_read())) {
32-
if(rtio_i_status_read() & RTIO_I_STATUS_OVERFLOW) {
28+
if(status & RTIO_I_STATUS_OVERFLOW) {
3329
rtio_i_overflow_reset_write(1);
3430
artiq_raise_from_c("RTIOOverflow",
35-
"RTIO overflow at channel {0}",
31+
"RTIO WB overflow on channel {0}",
3632
channel, 0, 0);
3733
}
38-
if(rtio_get_counter() >= time_limit) {
34+
if(rtio_get_counter() >= timestamp + duration) {
3935
/* check empty flag again to prevent race condition.
4036
* now we are sure that the time limit has been exceeded.
4137
*/
4238
if(rtio_i_status_read() & RTIO_I_STATUS_EMPTY)
43-
return -1;
39+
artiq_raise_from_c("InternalError",
40+
"RTIO WB read failed on channel {0}",
41+
channel, 0, 0);
4442
}
4543
/* input FIFO is empty - keep waiting */
4644
}
47-
r = rtio_i_data_read();
45+
data = rtio_i_data_read();
4846
rtio_i_re_write(1);
49-
return r;
47+
return data;
5048
}

‎artiq/runtime/rt2wb.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __RT2WB_H
2+
#define __RT2WB_H
3+
4+
void rt2wb_write(long long int timestamp, int channel, int address,
5+
unsigned int data);
6+
unsigned int rt2wb_read_sync(long long int timestamp, int channel, int address,
7+
int duration);
8+
9+
#endif /* __RT2WB_H */
10+

‎artiq/runtime/spi.h

-20
This file was deleted.

4 commit comments

Comments
 (4)

sbourdeauducq commented on Feb 29, 2016

@sbourdeauducq
Member

Wishbone is a gateware implementation detail. I would make it transparent to the Python layers.

jordens commented on Feb 29, 2016

@jordens
MemberAuthor

RT2WB is not transparent because of how it handles reads. They can be pipelined or handled asynchronously.

sbourdeauducq commented on Feb 29, 2016

@sbourdeauducq
Member

Are you going to rewrite the DDS code as well so it uses this, then?

jordens commented on Feb 29, 2016

@jordens
MemberAuthor

yes

Please sign in to comment.