2014-08-28 16:56:48 +08:00
|
|
|
#include <generated/csr.h>
|
|
|
|
|
2014-09-25 12:55:50 +08:00
|
|
|
#include "exceptions.h"
|
2014-08-28 16:56:48 +08:00
|
|
|
#include "rtio.h"
|
|
|
|
|
2014-12-01 14:29:50 +08:00
|
|
|
#define RTIO_O_STATUS_FULL 1
|
|
|
|
#define RTIO_O_STATUS_UNDERFLOW 2
|
2014-12-01 17:32:36 +08:00
|
|
|
#define RTIO_O_STATUS_SEQUENCE_ERROR 4
|
2014-12-01 14:29:50 +08:00
|
|
|
#define RTIO_I_STATUS_EMPTY 1
|
|
|
|
#define RTIO_I_STATUS_OVERFLOW 2
|
|
|
|
|
2014-10-14 12:47:04 +08:00
|
|
|
long long int previous_fud_end_time;
|
|
|
|
|
2014-08-28 16:56:48 +08:00
|
|
|
void rtio_init(void)
|
|
|
|
{
|
2014-10-14 12:47:04 +08:00
|
|
|
previous_fud_end_time = 0;
|
2014-11-30 00:13:54 +08:00
|
|
|
rtio_reset_write(1);
|
|
|
|
rtio_reset_write(0);
|
2014-08-28 16:56:48 +08:00
|
|
|
}
|
|
|
|
|
2014-09-14 23:30:33 +08:00
|
|
|
void rtio_oe(int channel, int oe)
|
|
|
|
{
|
|
|
|
rtio_chan_sel_write(channel);
|
|
|
|
rtio_oe_write(oe);
|
|
|
|
}
|
|
|
|
|
2014-08-28 16:56:48 +08:00
|
|
|
void rtio_set(long long int timestamp, int channel, int value)
|
|
|
|
{
|
2014-12-01 14:29:50 +08:00
|
|
|
int status;
|
|
|
|
|
2014-09-05 12:03:22 +08:00
|
|
|
rtio_chan_sel_write(channel);
|
|
|
|
rtio_o_timestamp_write(timestamp);
|
|
|
|
rtio_o_value_write(value);
|
|
|
|
rtio_o_we_write(1);
|
2014-12-01 14:29:50 +08:00
|
|
|
status = rtio_o_status_read();
|
|
|
|
if(status) {
|
|
|
|
if(status & RTIO_O_STATUS_FULL)
|
|
|
|
while(rtio_o_status_read() & RTIO_O_STATUS_FULL);
|
|
|
|
if(status & RTIO_O_STATUS_UNDERFLOW) {
|
|
|
|
rtio_o_underflow_reset_write(1);
|
|
|
|
exception_raise(EID_RTIO_UNDERFLOW);
|
|
|
|
}
|
2014-12-01 17:32:36 +08:00
|
|
|
if(status & RTIO_O_STATUS_SEQUENCE_ERROR) {
|
|
|
|
rtio_o_sequence_error_reset_write(1);
|
|
|
|
exception_raise(EID_RTIO_SEQUENCE_ERROR);
|
|
|
|
}
|
2014-10-10 20:12:22 +08:00
|
|
|
}
|
2014-09-11 23:14:45 +08:00
|
|
|
}
|
|
|
|
|
2014-11-21 04:32:56 +08:00
|
|
|
long long int rtio_get_counter(void)
|
|
|
|
{
|
|
|
|
rtio_counter_update_write(1);
|
|
|
|
return rtio_counter_read();
|
|
|
|
}
|
|
|
|
|
2014-11-30 00:13:54 +08:00
|
|
|
long long int rtio_get(int channel, long long int time_limit)
|
2014-09-14 23:30:33 +08:00
|
|
|
{
|
|
|
|
long long int r;
|
2014-12-01 14:29:50 +08:00
|
|
|
int status;
|
2014-09-14 23:30:33 +08:00
|
|
|
|
|
|
|
rtio_chan_sel_write(channel);
|
2014-12-01 17:32:36 +08:00
|
|
|
while((status = rtio_i_status_read())) {
|
2014-12-01 14:29:50 +08:00
|
|
|
if(rtio_i_status_read() & RTIO_I_STATUS_OVERFLOW) {
|
2014-11-30 00:13:54 +08:00
|
|
|
rtio_i_overflow_reset_write(1);
|
2014-10-21 23:41:02 +08:00
|
|
|
exception_raise(EID_RTIO_OVERFLOW);
|
|
|
|
}
|
2014-12-01 14:29:50 +08:00
|
|
|
if(rtio_get_counter() >= time_limit) {
|
|
|
|
/* check empty flag again to prevent race condition.
|
|
|
|
* now we are sure that the time limit has been exceeded.
|
|
|
|
*/
|
|
|
|
if(rtio_i_status_read() & RTIO_I_STATUS_EMPTY)
|
|
|
|
return -1;
|
2014-09-14 23:30:33 +08:00
|
|
|
}
|
2014-12-01 14:29:50 +08:00
|
|
|
/* input FIFO is empty - keep waiting */
|
2014-09-14 23:30:33 +08:00
|
|
|
}
|
2014-12-01 14:29:50 +08:00
|
|
|
r = rtio_i_timestamp_read();
|
|
|
|
rtio_i_re_write(1);
|
|
|
|
return r;
|
2014-09-14 23:30:33 +08:00
|
|
|
}
|
2014-10-14 12:47:04 +08:00
|
|
|
|
2014-10-21 23:14:01 +08:00
|
|
|
int rtio_pileup_count(int channel)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
rtio_chan_sel_write(channel);
|
|
|
|
r = rtio_i_pileup_count_read();
|
|
|
|
rtio_i_pileup_reset_write(1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-01 17:31:35 +08:00
|
|
|
#define RTIO_FUD_CHANNEL 8
|
2014-10-14 12:47:04 +08:00
|
|
|
|
|
|
|
void rtio_fud_sync(void)
|
|
|
|
{
|
2014-11-30 00:13:54 +08:00
|
|
|
while(rtio_get_counter() < previous_fud_end_time);
|
2014-10-14 12:47:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rtio_fud(long long int fud_time)
|
|
|
|
{
|
|
|
|
long long int fud_end_time;
|
2014-12-01 17:32:36 +08:00
|
|
|
int status;
|
2014-10-14 12:47:04 +08:00
|
|
|
|
|
|
|
rtio_chan_sel_write(RTIO_FUD_CHANNEL);
|
|
|
|
fud_end_time = fud_time + 3*8;
|
|
|
|
previous_fud_end_time = fud_end_time;
|
|
|
|
|
|
|
|
rtio_o_timestamp_write(fud_time);
|
|
|
|
rtio_o_value_write(1);
|
|
|
|
rtio_o_we_write(1);
|
|
|
|
rtio_o_timestamp_write(fud_end_time);
|
|
|
|
rtio_o_value_write(0);
|
|
|
|
rtio_o_we_write(1);
|
2014-12-01 17:32:36 +08:00
|
|
|
status = rtio_o_status_read();
|
|
|
|
if(status) {
|
|
|
|
if(status & RTIO_O_STATUS_UNDERFLOW) {
|
|
|
|
rtio_o_underflow_reset_write(1);
|
|
|
|
exception_raise(EID_RTIO_UNDERFLOW);
|
|
|
|
}
|
|
|
|
if(status & RTIO_O_STATUS_SEQUENCE_ERROR) {
|
|
|
|
rtio_o_sequence_error_reset_write(1);
|
|
|
|
exception_raise(EID_RTIO_SEQUENCE_ERROR);
|
|
|
|
}
|
2014-10-14 12:47:04 +08:00
|
|
|
}
|
|
|
|
}
|