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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// -----------------------------------------------------------------------
//
// Copyright 2016 H. Peter Anvin - All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston MA 02110-1301, USA; either version 2 of the License, or
// (at your option) any later version; incorporated herein by reference.
//
// -----------------------------------------------------------------------
//
// neopixel.v
//
// Driver for WS2812 (neopixel) LED chains
//
// This drives 32 output channels and expects a 25 MHz clock.
// As long as the external RAM can keep up, the number of channels
// should scale with clock frequency.
//
module neopixel (
// System signals
input rst_n,
input clk,
input enable,
// CPU interface to the config RAM
input [7:0] cpu_do,
input [6:0] cpu_a,
input cpu_wr_n,
input cpu_rd_n,
output [7:0] cpu_di,
// SRAM interface
output [18:0] ram_a, // BIT address into system RAM
input ram_q, // Expected one cycle later
// Neopixel pulse output
output reg [31:0] npout
);
// Code assumes pulse0 < pulse1
parameter pulse0 = 8;
parameter pulse1 = 20;
// Pipeline stages:
// A - generate address for config/status RAM
// B - generate address for SRAM, advance staus RAM
// pulse shaping (delay line for data)
reg g_enable; // Global enable, latched on word boundaries
// Bit/channel counters
// 32 channels x 24 bits
reg [9:0] a_ctr;
reg [9:0] b_ctr;
wire [9:0] a_ctr_p1 = a_ctr + 1'b1;
always @(negedge rst_n or posedge clk)
if (~rst_n)
begin
a_ctr <= 10'd0;
b_ctr <= 10'd1;
g_enable <= 1'b0;
end
else
begin
// a_ctr[9:5] counts from 0 to 23 (bits per word)
if (&a_ctr_p1[9:8])
begin
a_ctr <= 10'b0;
g_enable <= enable;
end
else
begin
a_ctr <= a_ctr_p1;
end
b_ctr <= a_ctr;
end // else: !if(~rst_n)
// A stage, counter fed to RAMs
wire [7:0] conf_cpu_q;
wire [31:0] b_conf_q;
// Configuration RAM
npconfram npconfram (
.clock ( clk ),
.address_a ( a_ctr[4:0] ),
.data_a ( 32'bx ),
.wren_a ( 1'b0 ), // Readonly port
.q_a ( b_conf_q ),
.address_b ( cpu_a ),
.data_b ( cpu_do ),
.wren_b ( ~cpu_wr_n ),
.q_b ( conf_cpu_q )
);
assign cpu_di = ~cpu_rd_n ? conf_cpu_q : ~8'b0;
wire [35:0] b_stat_q;
reg [35:0] b_stat_d;
// Status RAM
npstatram npstatram (
.clock ( clk ),
.rdaddress ( a_ctr[4:0] ),
.q ( b_stat_q ),
.wraddress ( b_ctr[4:0] ),
.wren ( 1'b1 ),
.data ( b_stat_d )
);
// B stage, SRAM address generation
wire [15:0] b_conf_addr = b_conf_q[15:0];
wire [7:0] b_conf_plen = b_conf_q[23:16]; // Pattern len
wire [7:0] b_conf_clen = b_conf_q[31:24]; // Chain len
wire [15:0] b_stat_addr = b_stat_q[15:0];
wire [7:0] b_stat_pctr = b_stat_q[23:16];
wire [8:0] b_stat_cctr = b_stat_q[32:24];
// Channel is enabled if plen > 0 or global disable
wire b_ena = |b_conf_plen & g_enable;
// True before stat ctr wrap. Cycles -1 and -2 are for reset
wire b_stat_end = b_stat_cctr[8] & ~b_stat_cctr[0];
always @(*)
begin
b_stat_d[35:33] = 3'bxxx;
b_stat_d[32:0] = b_stat_q[32:0];
if ( ~b_ena )
begin
b_stat_d[15:0] = b_conf_addr;
b_stat_d[32:24] = ~9'b0;
end
else
begin
if (&b_ctr[7:5]) // New byte?
begin
b_stat_d[15:0] = b_stat_addr + 1'b1;
if ( b_ctr[9] ) // New word?
begin
if ( b_stat_end | ~|b_stat_pctr )
b_stat_d[15:0] = b_conf_addr;
b_stat_d[23:16] =
(( b_stat_end | ~|b_stat_pctr ) ?
b_conf_plen : b_stat_pctr) - 1'b1;
b_stat_d[32:24] =
(b_stat_end ?
{1'b0, b_conf_clen} : b_stat_cctr) - 1'b1;
end // if ( b_ctr[9] )
end // if ( &b_ctr[7:5] )
end // else: !if( ~b_ena )
end // always @ (*)
// Output: are we in chain reset?
wire b_in_rst = ~b_ena | b_stat_cctr[8];
// b_ctr[7:5] is inverted as bit order is bigendian
assign ram_a = { b_stat_addr, ~b_ctr[7:5] };
// Pulse generation. We can start the pulse even before the data
// arrives from SRAM, as we will already know if we are in reset!
// Shift register to delay data for pulse generation
reg [pulse0-2:0] data_q;
always @(posedge clk)
data_q <= { ram_q, data_q[pulse0-2:1] };
// Output pulse generation
genvar i;
generate
for (i = 0; i < 32; i = i + 1)
begin: gen_npout
always @(negedge rst_n or posedge clk)
if ( ~rst_n )
npout[i] <= 1'b0;
else
begin
case ( b_ctr[4:0] )
(i & 5'h1f):
npout[i] <= ~b_in_rst;
((i+pulse0) & 5'h1f):
npout[i] <= npout[i] & data_q[0];
((i+pulse1) & 5'h1f):
npout[i] <= 1'b0;
endcase // case ( b_ctr[4:0] )
end // else: !if( ~rst_n )
end // block: gen_npout
endgenerate
endmodule // neopixel
|