Rev 534 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
534 | lvd | 1 | // generates activity on avr SPI aimed to send bytes over SDcard SPI. |
2 | // |
||
3 | `ifdef SPITEST |
||
4 | |||
5 | `include "../include/tune.v" |
||
6 | |||
7 | |||
8 | |||
9 | `define AVR_HALF_PERIOD (45.2) |
||
10 | |||
11 | module spitest_avr( |
||
12 | |||
13 | output wire spick, |
||
14 | output reg spics_n, |
||
15 | output wire spido, |
||
16 | input wire spidi |
||
17 | |||
18 | ); |
||
19 | |||
20 | |||
21 | reg aclk; |
||
22 | |||
23 | |||
24 | reg spistart; |
||
25 | wire spirdy; |
||
26 | |||
27 | reg [7:0] spidin; |
||
28 | wire [7:0] spidout; |
||
29 | |||
30 | |||
31 | |||
32 | |||
33 | // clock gen |
||
34 | initial |
||
35 | begin |
||
36 | aclk = 1'b0; |
||
37 | |||
38 | forever #`AVR_HALF_PERIOD aclk = ~aclk; |
||
39 | end |
||
40 | |||
41 | |||
42 | // signals init |
||
43 | initial |
||
44 | begin |
||
45 | spics_n = 1'b1; |
||
46 | |||
47 | spistart = 1'b0; |
||
48 | end |
||
49 | |||
50 | |||
51 | |||
52 | |||
53 | // use standard spi2 module to send and receive over SPI. |
||
54 | // reverse bytes since spi2 sends and receives MSB first, |
||
55 | // while slavespi LSB first |
||
56 | spi2 spi2( |
||
57 | |||
58 | .clock(aclk), |
||
59 | |||
60 | .sck(spick), |
||
61 | .sdo(spido), |
||
62 | .sdi(spidi), |
||
63 | |||
64 | .bsync(), |
||
65 | |||
66 | .start(spistart), |
||
67 | .rdy (spirdy ), |
||
68 | |||
69 | .speed(2'b00), |
||
70 | |||
71 | .din ({spidin[0], spidin[1], spidin[2], spidin[3], |
||
72 | spidin[4], spidin[5], spidin[6], spidin[7]}), |
||
73 | |||
74 | .dout({spidout[0], spidout[1], spidout[2], spidout[3], |
||
75 | spidout[4], spidout[5], spidout[6], spidout[7]}) |
||
76 | ); |
||
77 | |||
78 | |||
79 | |||
80 | |||
81 | // test loop |
||
82 | initial |
||
83 | begin |
||
84 | repeat(2211) @(posedge aclk); |
||
85 | |||
86 | forever |
||
87 | begin |
||
88 | get_access(); |
||
89 | send_msg(); |
||
90 | release_access(); |
||
91 | |||
92 | repeat(1234) @(posedge aclk); |
||
93 | end |
||
94 | end |
||
95 | |||
96 | |||
97 | |||
98 | |||
99 | |||
100 | task get_access( |
||
101 | ); |
||
102 | reg [7:0] tmp; |
||
103 | |||
104 | reg_io( 8'h61, 8'h81, tmp ); |
||
105 | |||
106 | while( !tmp[7] ) |
||
107 | reg_io( 8'h61, 8'h81, tmp ); |
||
108 | endtask |
||
109 | |||
110 | task send_msg( |
||
111 | ); |
||
112 | reg [7:0] tmp; |
||
113 | reg [71:0] msg = "AVR SEND\n"; |
||
114 | integer i; |
||
115 | |||
116 | reg_io( 8'h61, 8'h80, tmp ); |
||
117 | |||
118 | for(i=8;i>=0;i=i-1) |
||
119 | begin |
||
120 | reg_io( 8'h60, msg[i*8 +: 8], tmp ); |
||
121 | end |
||
122 | |||
123 | reg_io( 8'h61, 8'h81, tmp ); |
||
124 | endtask |
||
125 | |||
126 | task release_access( |
||
127 | ); |
||
128 | reg [7:0] tmp; |
||
129 | |||
130 | reg_io( 8'h61, 8'h81, tmp ); |
||
131 | reg_io( 8'h61, 8'h01, tmp ); |
||
132 | endtask |
||
133 | |||
134 | task reg_io( |
||
135 | input [7:0] addr, |
||
136 | input [7:0] wrdata, |
||
137 | output [7:0] rddata |
||
138 | ); |
||
139 | |||
140 | reg [7:0] trash; |
||
141 | |||
142 | |||
143 | spics_n <= 1'b1; |
||
144 | @(posedge aclk); |
||
145 | |||
146 | spi_io( addr, trash ); |
||
147 | |||
148 | spics_n <= 1'b0; |
||
149 | @(posedge aclk); |
||
150 | |||
151 | spi_io( wrdata, rddata ); |
||
152 | |||
153 | spics_n <= 1'b1; |
||
154 | @(posedge aclk); |
||
155 | |||
156 | endtask |
||
157 | |||
158 | |||
159 | |||
160 | task spi_io( |
||
161 | input [7:0] wrdata, |
||
162 | output [7:0] rddata |
||
163 | ); |
||
164 | |||
165 | spidin <= wrdata; |
||
166 | spistart <= 1'b1; |
||
167 | |||
168 | @(posedge aclk); |
||
169 | |||
170 | spistart <= 1'b0; |
||
171 | |||
172 | @(posedge aclk); |
||
173 | |||
174 | wait(spirdy==1'b1); |
||
175 | |||
176 | @(posedge aclk); |
||
177 | |||
178 | |||
179 | rddata = spidout; |
||
180 | |||
181 | endtask |
||
182 | |||
183 | |||
184 | |||
185 | |||
186 | endmodule |
||
187 | `endif |