File size: 1,374 Bytes
7df9186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
`timescale 1ns / 1ns
module ad5662 #(
	parameter nch=1  // number of channels (chips)
) (
	input clk,
	input tick,  // pacing gate
	// application (software at first)
	input [15:0] data,
	input [1:0] ctl,  // {PD1, PD0}, see fig. 34 of ad5662.pdf
	input [nch-1:0] sel,  // chip select
	input send,  // single-cycle gate
	output busy,
	// hardware pins
	output sclk,  // peak rate is half that of input tick
	output [nch-1:0] sync_,
	output sdo
);

// Primary persistent state is a simple counter
// send must be in clk domain
reg [5:0] count=0;
reg busy_r=0;
reg ending=0;
always @(posedge clk) begin
	if (send & ~busy) count <= 13;
	if (send) busy_r <= 1;
	if (ending) busy_r <= 0;
	if (tick & (count != 0)) count <= count+1;
	ending <= tick & (count==63);
end
assign busy = busy_r;

reg [nch-1:0] sel_r=0, sync_r=0;
always @(posedge clk) begin
	if (send) sel_r <= sel;
	if (count == 13) sync_r <= sel_r;
	if ((count == 61) && tick) sync_r <= {nch{1'b0}};
end

// Decode that state
reg sclk_r=0, shift=0;
wire running = (14 < count) && (count < 62);
always @(posedge clk) begin
	sclk_r <= ~running | ~count[0];
	shift <= tick & ~sclk_r;
end

// data path
reg [23:0] sr=0;
always @(posedge clk) begin
	if (send) sr <= {6'b0, ctl, data};
	if (shift) sr <= {sr[22:0], 1'b0};
end

// Output mapping
assign sclk = sclk_r;
assign sync_ = ~sync_r;
assign sdo = sr[23];

endmodule