OpenCores
URL https://opencores.org/ocsvn/aemb/aemb/trunk

Subversion Repositories aemb

[/] [aemb/] [trunk/] [lib/] [vpio/] [vpio_mspi.v] - Blame information for rev 197

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 195 sybreon
/* $Id: fasm_sparam.v,v 1.2 2008/06/05 20:55:15 sybreon Exp $
2
**
3
** VIRTUAL PERIPHERAL INPUT/OUTPUT LIBRARY
4
** Copyright (C) 2004-2009 Shawn Tan <shawn.tan@aeste.net>
5
** All rights reserved.
6
**
7
** LITE is free software: you can redistribute it and/or modify it
8
** under the terms of the GNU Lesser General Public License as
9
** published by the Free Software Foundation, either version 3 of the
10
** License, or (at your option) any later version.
11
**
12
** LITE is distributed in the hope that it will be useful, but WITHOUT
13
** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
** or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
15
** Public License for more details.
16
**
17
** You should have received a copy of the GNU Lesser General Public
18
** License along with FASM. If not, see <http:**www.gnu.org/licenses/>.
19
*/
20
/*
21
 * MASTER SERIAL PERIPHERAL INTERFACE
22
 */
23
 
24
module vpio_mspi (/*AUTOARG*/
25
   // Outputs
26
   wb_dat_o, wb_ack_o, int_o, mspi_dat_o, mspi_clk_o,
27
   // Inputs
28
   wb_dat_i, wb_adr_i, wb_sel_i, wb_wre_i, wb_stb_i, wb_clk_i,
29
   wb_rst_i, mspi_dat_i
30
   );
31
 
32
   // WISHBONE SLAVE INTERFACE
33
   output [7:0] wb_dat_o;
34
   output       wb_ack_o;
35
   output       int_o;
36
   input [7:0]   wb_dat_i;
37
   input        wb_adr_i;
38
   input        wb_sel_i,
39
                wb_wre_i,
40
                wb_stb_i,
41
                //wb_cyc_i,
42
                wb_clk_i,
43
                wb_rst_i;
44
 
45
   // MASTER SPI INTERFACE
46
   output       mspi_dat_o, // MOSI
47
                //mspi_sel_o, // SSEL
48
                mspi_clk_o; // SCLK
49
   input        mspi_dat_i; // MISO
50
 
51
   /*AUTOREG*/
52
   // Beginning of automatic regs (for this module's undeclared outputs)
53
   reg                  int_o;
54
   reg                  mspi_clk_o;
55
   reg                  wb_ack_o;
56
   reg [7:0]             wb_dat_o;
57
   // End of automatics
58
 
59
   localparam [1:0] // synopsys enum state FSM
60
     FSM_IDLE = 2'o0,
61
     FSM_PHA0 = 2'o1,
62
     FSM_PHA1 = 2'o2,
63
     FSM_NULL = 2'o3;
64
 
65
   reg [1:0]             // synopsys enum state FSM              
66
                        rFSM, rFSM_;
67
 
68
   reg [1:0]             rSPIC_MODE, rSPIC_DIVI;
69
   reg                  rSPIC_SPIE, rSPIC_SPEN;
70
   reg                  rSPIC_SPIF, rSPIC_WCOL;
71
 
72
   reg                  rFULL;
73
   reg [7:0]             rSPID, rTBUF;
74
   reg [2:0]             rBCNT;
75
 
76
   wire [7:0]            rSPIC = {rSPIC_SPIE, // RW-SPI interrupt enable
77
                                 rSPIC_SPEN, // RW_SPI enable
78
                                 rSPIC_SPIF, // RO-flag
79
                                 rSPIC_WCOL, // RO-write collision
80
                                 rSPIC_MODE, // RW-SPI mode
81
                                 rSPIC_DIVI}; // RW-SPI clock divider
82
 
83
   wire                 CPOL = rSPIC[3]; // CPOL
84
   wire                 CPHA = rSPIC[2]; // CPHA
85
   wire [1:0]            CDIV = rSPIC[1:0]; // CDIV
86
   wire                 SPIE = rSPIC[7];
87
   wire                 SPEN = rSPIC[6];
88
 
89
   wire                 wb_stb = wb_sel_i & wb_stb_i;
90
   wire                 wb_wre = wb_sel_i & wb_stb_i & wb_wre_i;
91
 
92
   // WISHBONE INTERFACE - Synchronous Single Transfers
93
   always @(posedge wb_clk_i)
94
     if (wb_rst_i) begin
95
        /*AUTORESET*/
96
        // Beginning of autoreset for uninitialized flops
97
        rSPIC_DIVI <= 2'h0;
98
        rSPIC_MODE <= 2'h0;
99
        rSPIC_SPEN <= 1'h0;
100
        rSPIC_SPIE <= 1'h0;
101
        rSPID <= 8'h0;
102
        // End of automatics
103
     end else if (wb_wre) begin
104
        if (!wb_adr_i) {rSPIC_SPIE,
105
                        rSPIC_SPEN,
106
                        rSPIC_MODE,
107
                        rSPIC_DIVI} = #1 {wb_dat_i[7:6],
108
                                          wb_dat_i[3:0]};
109
        if (wb_adr_i) rSPID <= #1 wb_dat_i;
110
     end
111
 
112
   always @(posedge wb_clk_i)
113
     if (wb_rst_i) begin
114
        /*AUTORESET*/
115
        // Beginning of autoreset for uninitialized flops
116
        wb_ack_o <= 1'h0;
117
        wb_dat_o <= 8'h0;
118
        // End of automatics
119
     end else if (wb_stb) begin
120
 
121
        wb_ack_o <= !wb_ack_o & wb_stb_i;
122
 
123
        case (wb_adr_i)
124
          1'b0: wb_dat_o <= rSPIC;
125
          1'b1: wb_dat_o <= rSPID;
126
        endcase // case (wb_adr_i)
127
     end
128
 
129
   // CLOCK - Loadable Counter
130
   reg [3:0] rFCNT;
131
   wire      wena = ~|rFCNT;
132
   always @(posedge wb_clk_i)
133
     if (wb_rst_i) begin // reset
134
        /*AUTORESET*/
135
        // Beginning of autoreset for uninitialized flops
136
        rFCNT <= 4'h0;
137
        // End of automatics
138
     end else if (rFSM == FSM_IDLE) begin
139
        case (CDIV)
140
          2'o0: rFCNT <= #1 4'h0; // 2  -- original HC11
141
          2'o1: rFCNT <= #1 4'h1; // 4  -- original HC11
142
          2'o2: rFCNT <= #1 4'h7; // 16 -- original HC11
143
          2'o3: rFCNT <= #1 4'hF; // 32 -- original HC11
144
        endcase // case (CDIV)
145
     end else if (|rFCNT) begin
146
        rFCNT <= #1 rFCNT - 1;
147
     end
148
 
149
   // STATE MACHINE
150
   wire wbit = ~|rBCNT;
151
   assign mspi_dat_o = rTBUF[7];
152
 
153
   always @(posedge wb_clk_i)
154
     if (wb_rst_i) begin
155
        rFSM <= FSM_IDLE;
156
        /*AUTORESET*/
157
        // Beginning of autoreset for uninitialized flops
158
        mspi_clk_o <= 1'h0;
159
        rBCNT <= 3'h0;
160
        rFULL <= 1'h0;
161
        rTBUF <= 8'h0;
162
        // End of automatics
163
     end else begin
164
 
165
        // BIT COUNT
166
        case (rFSM)
167
          FSM_IDLE: rBCNT <= #1 3'o7;
168
          FSM_PHA1: rBCNT <= #1 rBCNT - 1;
169
          default: rBCNT <= #1 rBCNT;
170
        endcase // case (rFSM)
171
 
172
        // MOSI/MISO
173
        case (rFSM)
174
          FSM_IDLE: rTBUF <= #1 rSPID;
175
          FSM_PHA1: rTBUF <= #1 {rTBUF[6:0], mspi_dat_i};
176
          default: rTBUF <= #1 rTBUF;
177
        endcase // case (rFSM)
178
 
179
        // SCLK
180
        case (rFSM)
181
          FSM_IDLE: mspi_clk_o <= #1 CPOL;
182
          FSM_PHA0: mspi_clk_o <= #1 (wena) ? ~mspi_clk_o : mspi_clk_o;
183
          FSM_PHA1: mspi_clk_o <= #1 (!wena) ? mspi_clk_o : (wbit) ? CPOL : ~mspi_clk_o;
184
          /*
185
          FSM_PHA1: case ({wena, wbit})
186
                      2'o3: mspi_clk_o <= #1 CPOL;
187
                      2'o2: mspi_clk_o <= #1 ~mspi_clk_o;
188
                      default: mspi_clk_o <= #1 mspi_clk_o;
189
                    endcase // case ({wena, wbit})
190
           */
191
          default: mspi_clk_o <= #1 mspi_clk_o;
192
        endcase // case (rFSM)
193
 
194
        // FULL/WCOL    
195
        case (rFSM)
196
          FSM_IDLE: rFULL <= #1 (wb_wre & wb_adr_i) | rFULL;
197
          FSM_NULL: rFULL <= #1 1'b0;
198
          default: rFULL <= #1 rFULL;
199
        endcase // case (rFSM)
200
 
201
        // STATE
202
        case (rFSM)
203
          FSM_IDLE: rFSM <= #1 (rFULL) ? FSM_PHA0 : FSM_IDLE;
204
          FSM_PHA0: rFSM <= #1 (wena) ? FSM_PHA1 : FSM_PHA0;
205
          FSM_PHA1: rFSM <= #1 (!wena) ? FSM_PHA1 : (~|rBCNT) ? FSM_IDLE : FSM_PHA0;
206
          default: rFSM <= #1 FSM_IDLE;
207
        endcase // case (rFSM)
208
 
209
     end // else: !if(wb_rst_i)
210
 
211
endmodule // vpio_mspi

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.