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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_hd44780_8b.vhd] - Blame information for rev 242

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

Line No. Rev Author Line
1 194 jshamlet
-- Copyright (c)2013, 2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 213 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 194 jshamlet
--
24 175 jshamlet
-- VHDL Entity: o8_hd44780_8b
25
-- Description: Provides low-level access to a "standard" character LCD using
26
--               the ST/HD44780(U) control ASIC wired in full (8-bit) mode.
27 213 jshamlet
--              All low-level timing of the control signals are handled by
28
--               this module, allowing client firmware to use a simple
29
--               register interface to program the LCD panel.
30 175 jshamlet
--              Init routine initializes the display and displays a single
31
--               character to demonstrate correct function, then listens for
32
--               user data on its external interface.
33 213 jshamlet
--
34
-- Register Map
35
-- Address  Function
36
-- Offset  Bitfield Description                        Read/Write
37
-- 0x0     AAAAAAAA LCD Register Write                 (Write-only)
38
-- 0x1     AAAAAAAA LCD Data Write                     (Write-only)
39
-- 0x2     AAAAAAAA LCD Contrast                       (Read-Write)
40
-- 0x3     AAAAAAAA LCD Backlight                      (Read-Write)
41
--
42
--------------------------------------------------------------------------------
43
-- LCD Controller
44
--------------------------------------------------------------------------------
45
--
46
-- LCD Instruction Set
47
-- Instruction             RS  RW  D7  D6  D5  D4  D3  D2  D1  D0  Time
48
------------------------------------------------------------------------
49
-- Clear Display         | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1.52mS
50
-- Return Home           | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | x | 1.52mS
51
-- Entry Mode            | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ID| S |   37uS
52
-- Display Pwr           | 0 | 0 | 0 | 0 | 0 | 0 | 1 | D | C | B |   37uS
53
-- Cursor/Display Shift  | 0 | 0 | 0 | 0 | 0 | 1 | SC| RL| x | x |   37uS
54
-- Function Set          | 0 | 0 | 0 | 0 | 1 | DL| N | F | x | x |   37uS
55
-- Set CGRAM Address     | 0 | 0 | 0 | 1 | A | A | A | A | A | A |   37uS
56
-- Set DDRAM Address     | 0 | 0 | 1 | A | A | A | A | A | A | A |   37uS
57 175 jshamlet
 
58 213 jshamlet
-- Notes:
59
-- ID = Increment/Decrement DDRAM Address (1 = increment, 0 = decrement)
60
-- S  = Shift Enable (1 = Shift display according to ID, 0 = Don't shift)
61
-- D  = Display On/Off (1 = on, 0 = off)
62
-- C  = Cursor On/Off  (1 = on, 0 = off)
63
-- B  = Cursor Blink   (1 = block cursor, 0 = underline cursor)
64
-- SC / RL = Shift Cursor/Display Right/Left (see data sheet - not needed for init)
65
-- F  = Font (0 = 5x8, 1 = 5x11) Ignored on 2-line displays (N = 1)
66
-- N  = Number of Lines (0 = 1 lines, 1 = 2 lines)
67
-- DL = Data Length (0 = 4-bit bus, 1 = 8-bit bus) This is fixed at 1 in this module
68
-- A  = Address (see data sheet for usage)
69
--
70
-- Revision History
71
-- Author          Date     Change
72
------------------ -------- ---------------------------------------------------
73
-- Seth Henry      01/22/13 Design Start
74
-- Seth Henry      04/10/20 Code & comment cleanup
75 224 jshamlet
-- Seth Henry      04/16/20 Modified to use Open8 bus record
76 213 jshamlet
 
77 175 jshamlet
library ieee;
78
use ieee.std_logic_1164.all;
79
use ieee.std_logic_unsigned.all;
80
use ieee.std_logic_arith.all;
81
 
82
library work;
83
use work.open8_pkg.all;
84
 
85
entity o8_hd44780_8b is
86
generic(
87
  Use_Contrast          : boolean;
88
  Default_Contrast      : std_logic_vector(7 downto 0);
89
  Use_Backlight         : boolean;
90
  Default_Brightness    : std_logic_vector(7 downto 0);
91 224 jshamlet
  Clock_Frequency       : real;
92
  Address               : ADDRESS_TYPE
93 175 jshamlet
);
94
port(
95 223 jshamlet
  Open8_Bus             : in  OPEN8_BUS_TYPE;
96 175 jshamlet
  Rd_Data               : out DATA_TYPE;
97
  Interrupt             : out std_logic;
98
  --
99
  LCD_E                 : out std_logic;
100
  LCD_RW                : out std_logic;
101
  LCD_RS                : out std_logic;
102
  LCD_D                 : out std_logic_vector(7 downto 0);
103
  LCD_CN                : out std_logic;
104
  LCD_BL                : out std_logic
105
);
106
end entity;
107
 
108
architecture behave of o8_hd44780_8b is
109
 
110 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
111
  alias Reset                is Open8_Bus.Reset;
112
  alias uSec_Tick            is Open8_Bus.uSec_Tick;
113
 
114 217 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 2)
115
                               := Address(15 downto 2);
116 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 2);
117 217 jshamlet
  signal Addr_Match          : std_logic := '0';
118 175 jshamlet
 
119 223 jshamlet
  alias  Reg_Addr             is Open8_Bus.Address(1 downto 0);
120 217 jshamlet
  signal Reg_Addr_q          : std_logic_vector(1 downto 0) := (others => '0');
121 175 jshamlet
 
122 217 jshamlet
  signal Wr_En               : std_logic := '0';
123
  signal Wr_Data_q           : DATA_TYPE := x"00";
124
  signal Rd_En               : std_logic := '0';
125 175 jshamlet
 
126 217 jshamlet
  signal Reg_Valid           : std_logic := '0';
127
  signal Reg_Sel             : std_logic := '0';
128
  signal Reg_Data            : DATA_TYPE := x"00";
129 175 jshamlet
 
130 217 jshamlet
  signal Tx_Ready            : std_logic := '0';
131 175 jshamlet
 
132 217 jshamlet
  constant LCD_CONFIG1       : std_logic_vector(7 downto 0) := x"38"; -- Set 4-bit, 2-line mode
133
  constant LCD_CONFIG2       : std_logic_vector(7 downto 0) := x"0C"; -- Turn display on, no cursor
134
  constant LCD_CONFIG3       : std_logic_vector(7 downto 0) := x"01"; -- Clear display
135
  constant LCD_CONFIG4       : std_logic_vector(7 downto 0) := x"06"; -- Positive increment, no shift
136
  constant LCD_CONFIG5       : std_logic_vector(7 downto 0) := x"2A"; -- Print a "*"
137
  constant LCD_CONFIG6       : std_logic_vector(7 downto 0) := x"02"; -- Reset the cursor
138 175 jshamlet
 
139 217 jshamlet
  signal init_count          : std_logic_vector(2 downto 0) := (others => '0');
140 175 jshamlet
 
141 217 jshamlet
  constant INIT_40MS         : integer := 40000;
142
  constant INIT_BITS         : integer := ceil_log2(INIT_40MS);
143
  constant INIT_DELAY        : std_logic_vector(INIT_BITS-1 downto 0) :=
144
                               conv_std_logic_vector(INIT_40MS,INIT_BITS);
145 175 jshamlet
 
146
-- For "long" instructions, such as clear display and return home, we need to wait for more
147
--  than 1.52mS. Experimentally, 2mS seems to work ideally, and for init this isn't an issue
148 217 jshamlet
  constant CLDSP_2MS         : integer := 2000;
149
  constant CLDSP_DELAY       : std_logic_vector(INIT_BITS-1 downto 0) :=
150
                               conv_std_logic_vector(CLDSP_2MS,INIT_BITS);
151 175 jshamlet
 
152
 -- For some reason, we are required to wait 80uS before checking the busy flag, despite
153
 --  most instructions completing in 37uS. No clue as to why, but it works
154 217 jshamlet
  constant BUSY_50US         : integer := 50;
155
  constant BUSY_DELAY        : std_logic_vector(INIT_BITS-1 downto 0) :=
156
                               conv_std_logic_vector(BUSY_50US-1, INIT_BITS);
157 175 jshamlet
 
158 217 jshamlet
  signal busy_timer          : std_logic_vector(INIT_BITS-1 downto 0) := (others => '0');
159 175 jshamlet
 
160 224 jshamlet
  constant SNH_600NS         : integer := integer(Clock_Frequency * 0.000000600);
161 217 jshamlet
  constant SNH_BITS          : integer := ceil_log2(SNH_600NS);
162
  constant SNH_DELAY         : std_logic_vector(SNH_BITS-1 downto 0) :=
163
                               conv_std_logic_vector(SNH_600NS-1, SNH_BITS);
164 175 jshamlet
 
165 217 jshamlet
  signal io_timer            : std_logic_vector(SNH_BITS - 1 downto 0) := (others => '0');
166 175 jshamlet
 
167
  type IO_STATES is (INIT, FN_JUMP, IDLE,
168
                     WR_PREP, WR_SETUP, WR_HOLD,
169
                     BUSY_PREP, BUSY_WAIT,
170
                     ISSUE_INT );
171 217 jshamlet
  signal io_state            : IO_STATES;
172 175 jshamlet
 
173 217 jshamlet
  signal LCD_Data            : DATA_TYPE := x"00";
174
  signal LCD_Addr            : std_logic := '0';
175 175 jshamlet
 
176
--------------------------------------------------------------------------------
177
-- Backlight & Contrast signals
178
--------------------------------------------------------------------------------
179
 
180 217 jshamlet
  signal LCD_Contrast        : DATA_TYPE := x"00";
181
  signal LCD_Bright          : DATA_TYPE := x"00";
182 175 jshamlet
 
183
begin
184
 
185
--------------------------------------------------------------------------------
186
-- Open8 Register interface
187
--------------------------------------------------------------------------------
188
 
189 217 jshamlet
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
190 175 jshamlet
 
191
  io_reg: process( Clock, Reset )
192
  begin
193
    if( Reset = Reset_Level )then
194 217 jshamlet
      Reg_Addr_q             <= (others => '0');
195
      Wr_Data_q              <= (others => '0');
196
      Wr_En                  <= '0';
197
      Rd_En                  <= '0';
198
      Rd_Data                <= OPEN8_NULLBUS;
199 175 jshamlet
 
200 217 jshamlet
      Reg_Valid              <= '0';
201
      Reg_Sel                <= '0';
202
      Reg_Data               <= x"00";
203 175 jshamlet
 
204 217 jshamlet
      LCD_Contrast           <= Default_Contrast;
205
      LCD_Bright             <= Default_Brightness;
206 175 jshamlet
    elsif( rising_edge( Clock ) )then
207 217 jshamlet
      Reg_Addr_q             <= Reg_Addr;
208 175 jshamlet
 
209 223 jshamlet
      Wr_Data_q              <= Open8_Bus.Wr_Data;
210
      Wr_En                  <= Addr_Match and Open8_Bus.Wr_En;
211 175 jshamlet
 
212 217 jshamlet
      Reg_Valid              <= '0';
213 175 jshamlet
 
214
      if( Wr_En = '1' )then
215
        case( Reg_Addr_q )is
216
          when "00" | "01" =>
217 217 jshamlet
            Reg_Valid        <= '1';
218
            Reg_Sel          <= Reg_Addr_q(0);
219
            Reg_Data         <= Wr_Data_q;
220 175 jshamlet
          when "10" =>
221 217 jshamlet
            LCD_Contrast     <= Wr_Data_q;
222 175 jshamlet
          when "11" =>
223 217 jshamlet
            LCD_Bright       <= Wr_Data_q;
224 175 jshamlet
          when others => null;
225
        end case;
226
      end if;
227
 
228 217 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
229 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
230 175 jshamlet
      if( Rd_En = '1' )then
231
        case( Reg_Addr_q )is
232
          when "00" | "01" =>
233 217 jshamlet
            Rd_Data(7)       <= Tx_Ready;
234 175 jshamlet
          when "10" =>
235 217 jshamlet
            Rd_Data          <= LCD_Contrast;
236 175 jshamlet
          when "11" =>
237 217 jshamlet
            Rd_Data          <= LCD_Bright;
238 175 jshamlet
          when others => null;
239
        end case;
240
      end if;
241
    end if;
242
  end process;
243
 
244
--------------------------------------------------------------------------------
245
-- LCD and Register logic
246
--------------------------------------------------------------------------------
247
 
248 217 jshamlet
  LCD_RW                     <= '0'; -- Permanently wire the RW line low
249 175 jshamlet
 
250
  LCD_IO: process( Clock, Reset )
251
  begin
252
    if( Reset = Reset_Level )then
253 217 jshamlet
      io_state               <= INIT;
254
      init_count             <= (others => '0');
255
      io_timer               <= (others => '0');
256
      busy_timer             <= (others => '0');
257
      LCD_Data               <= (others => '0');
258
      LCD_Addr               <= '0';
259
      LCD_E                  <= '0';
260
      LCD_RS                 <= '0';
261
      LCD_D                  <= (others => '0');
262
      Tx_Ready               <= '0';
263
      Interrupt              <= '0';
264 175 jshamlet
    elsif( rising_edge(Clock) )then
265 217 jshamlet
      LCD_E                  <= '0';
266
      LCD_RS                 <= '0';
267
      LCD_D                  <= (others => '0');
268
      Tx_Ready               <= '0';
269
      Interrupt              <= '0';
270
      io_timer               <= io_timer - 1;
271
      busy_timer             <= busy_timer - uSec_Tick;
272 175 jshamlet
      case( io_state )is
273
 
274
        when INIT =>
275 217 jshamlet
          busy_timer         <= INIT_DELAY;
276
          init_count         <= (others => '1');
277
          io_state           <= BUSY_WAIT;
278 175 jshamlet
 
279
        when FN_JUMP =>
280 217 jshamlet
          io_state           <= WR_PREP;
281 175 jshamlet
          case( init_count )is
282
            when "000" =>
283 217 jshamlet
              io_state       <= IDLE;
284 175 jshamlet
            when "001" =>
285 217 jshamlet
              LCD_Addr       <= '0';
286
              LCD_Data       <= LCD_CONFIG6; -- Reset the Cursor
287 175 jshamlet
            when "010" =>
288 217 jshamlet
              LCD_Addr       <= '1';         -- Print a "*", and
289
              LCD_Data       <= LCD_CONFIG5; --  set RS to 1
290 175 jshamlet
            when "011" =>
291 217 jshamlet
              LCD_Data       <= LCD_CONFIG4; -- Entry mode
292 175 jshamlet
            when "100" =>
293 217 jshamlet
              LCD_Data       <= LCD_CONFIG3; -- Clear Display
294 175 jshamlet
            when "101" =>
295 217 jshamlet
              LCD_Data       <= LCD_CONFIG2; -- Display control
296 175 jshamlet
            when "110" | "111" =>
297 217 jshamlet
              LCD_Addr       <= '0';
298
              LCD_Data       <= LCD_CONFIG1; -- Function set
299 175 jshamlet
            when others => null;
300
          end case;
301
 
302
        when IDLE =>
303 217 jshamlet
          Tx_Ready           <= '1';
304 175 jshamlet
          if( Reg_Valid = '1' )then
305 217 jshamlet
            LCD_Addr         <= Reg_Sel;
306
            LCD_Data         <= Reg_Data;
307
            io_state         <= WR_PREP;
308 175 jshamlet
          end if;
309
 
310
        when WR_PREP =>
311 217 jshamlet
          io_timer           <= SNH_DELAY;
312
          io_state           <= WR_SETUP;
313 175 jshamlet
 
314
        when WR_SETUP =>
315 217 jshamlet
          LCD_RS             <= LCD_Addr;
316
          LCD_D              <= LCD_Data;
317
          LCD_E              <= '1';
318 175 jshamlet
          if( io_timer = 0 )then
319 217 jshamlet
            io_timer         <= SNH_DELAY;
320
            io_state         <= WR_HOLD;
321 175 jshamlet
          end if;
322
 
323
        when WR_HOLD =>
324 217 jshamlet
          LCD_RS             <= LCD_Addr;
325
          LCD_D              <= LCD_Data;
326 175 jshamlet
          if( io_timer = 0 )then
327 217 jshamlet
            LCD_E            <= '0';
328
            io_state         <= BUSY_PREP;
329 175 jshamlet
          end if;
330
 
331
        when BUSY_PREP =>
332 217 jshamlet
          busy_timer         <= BUSY_DELAY;
333 175 jshamlet
          if( LCD_Addr = '0' and LCD_Data < 4 )then
334 217 jshamlet
            busy_timer       <= CLDSP_DELAY;
335 175 jshamlet
          end if;
336 217 jshamlet
          io_state           <= BUSY_WAIT;
337 175 jshamlet
 
338
        when BUSY_WAIT =>
339
          if( busy_timer = 0 )then
340 217 jshamlet
            io_state         <= ISSUE_INT;
341 175 jshamlet
            if( init_count > 0 )then
342 217 jshamlet
              init_count     <= init_count - 1;
343
              io_state       <= FN_JUMP;
344 175 jshamlet
            end if;
345
          end if;
346
 
347
        when ISSUE_INT =>
348 217 jshamlet
          Interrupt          <= '1';
349
          io_state           <= IDLE;
350 175 jshamlet
 
351
        when others => null;
352
 
353
      end case;
354
 
355
    end if;
356
  end process;
357
 
358
--------------------------------------------------------------------------------
359
-- Contrast control logic (optional)
360
--------------------------------------------------------------------------------
361
 
362
Contrast_Disabled: if( not Use_Contrast )generate
363 217 jshamlet
  LCD_CN                     <= '0';
364 175 jshamlet
end generate;
365
 
366
Contrast_Enabled: if( Use_Contrast )generate
367
 
368 217 jshamlet
  U_CN : entity work.vdsm8
369
  generic map(
370
    Reset_Level              => Reset_Level
371
  )
372
  port map(
373
    Clock                    => Clock,
374
    Reset                    => Reset,
375
    DACin                    => LCD_Contrast,
376
    DACout                   => LCD_CN
377
  );
378 175 jshamlet
 
379
end generate;
380
 
381
--------------------------------------------------------------------------------
382
-- Backlight control logic (optional)
383
--------------------------------------------------------------------------------
384
 
385
Backlight_Disabled: if( not Use_Backlight )generate
386 217 jshamlet
  LCD_BL                     <= '0';
387 175 jshamlet
end generate;
388
 
389
Backlight_Enabled: if( Use_Backlight )generate
390
 
391 217 jshamlet
  U_BL : entity work.vdsm8
392
  generic map(
393
    Reset_Level              => Reset_Level
394
  )
395
  port map(
396
    Clock                    => Clock,
397
    Reset                    => Reset,
398
    DACin                    => LCD_Bright,
399
    DACout                   => LCD_BL
400
  );
401 175 jshamlet
 
402
end generate;
403
 
404
end architecture;

powered by: WebSVN 2.1.0

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