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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_hd44780_4b.vhd] - Blame information for rev 224

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
-- (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
--
24 175 jshamlet
-- VHDL Entity: o8_hd44780_4b
25
-- Description: Provides low-level access to a "standard" character LCD using
26
--               the ST/HD44780(U) control ASIC wired in reduced (4-bit) mode.
27
--              All low-level timing of the control signals are handled by this
28
--               module, allowing client firmware to use a simple register
29
--               interface to program the LCD panel.
30
--              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_4b is
86
generic(
87 217 jshamlet
  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 217 jshamlet
  Rd_Data                    : out DATA_TYPE;
97
  Interrupt                  : out std_logic;
98 175 jshamlet
  --
99 217 jshamlet
  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 4);
103
  LCD_CN                     : out std_logic;
104
  LCD_BL                     : out std_logic
105 175 jshamlet
);
106
end entity;
107
 
108
architecture behave of o8_hd44780_4b 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            : std_logic_vector(7 downto 0) := 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 4) := x"3";  -- Init to 4-bit mode
133
  constant LCD_CONFIG2       : std_logic_vector(7 downto 0) := x"28"; -- Set 4-bit, 2-line mode
134
  constant LCD_CONFIG3       : std_logic_vector(7 downto 0) := x"0C"; -- Turn display on, no cursor
135
  constant LCD_CONFIG4       : std_logic_vector(7 downto 0) := x"01"; -- Clear display
136
  constant LCD_CONFIG5       : std_logic_vector(7 downto 0) := x"06"; -- Positive increment, no shift
137
  constant LCD_CONFIG6       : std_logic_vector(7 downto 0) := x"2A"; -- Print a "*"
138
  constant LCD_CONFIG7       : std_logic_vector(7 downto 0) := x"02"; -- Reset the cursor
139 175 jshamlet
 
140 217 jshamlet
  signal init_count          : std_logic_vector(2 downto 0) := (others => '0');
141 175 jshamlet
 
142 217 jshamlet
  constant INIT_40MS         : integer := 40000;
143
  constant INIT_BITS         : integer := ceil_log2(INIT_40MS);
144
  constant INIT_DELAY        : std_logic_vector(INIT_BITS-1 downto 0) :=
145
                               conv_std_logic_vector(INIT_40MS,INIT_BITS);
146 175 jshamlet
 
147
-- For "long" instructions, such as clear display and return home, we need to wait for more
148
--  than 1.52mS. Experimentally, 2mS seems to work ideally, and for init this isn't an issue
149 217 jshamlet
  constant CLDSP_2MS         : integer := 2000;
150
  constant CLDSP_DELAY       : std_logic_vector(INIT_BITS-1 downto 0) :=
151
                               conv_std_logic_vector(CLDSP_2MS,INIT_BITS);
152 175 jshamlet
 
153
 -- For some reason, we are required to wait 80uS before checking the busy flag, despite
154
 --  most instructions completing in 37uS. No clue as to why, but it works
155 217 jshamlet
  constant BUSY_50US         : integer := 50;
156
  constant BUSY_DELAY        : std_logic_vector(INIT_BITS-1 downto 0) :=
157
                               conv_std_logic_vector(BUSY_50US-1, INIT_BITS);
158 175 jshamlet
 
159 217 jshamlet
  signal busy_timer          : std_logic_vector(INIT_BITS-1 downto 0);
160 175 jshamlet
 
161 224 jshamlet
  constant SNH_600NS         : integer := integer(Clock_Frequency * 0.000000600);
162 217 jshamlet
  constant SNH_BITS          : integer := ceil_log2(SNH_600NS);
163
  constant SNH_DELAY         : std_logic_vector(SNH_BITS-1 downto 0) :=
164
                               conv_std_logic_vector(SNH_600NS-1, SNH_BITS);
165 175 jshamlet
 
166 217 jshamlet
  signal io_timer            : std_logic_vector(SNH_BITS - 1 downto 0) :=
167
                               (others => '0');
168 175 jshamlet
 
169
  type IO_STATES is (INIT, PWR_WAIT, INIT_S1, INIT_H1,
170
                     INIT_WAIT, FN_JUMP, IDLE,
171 213 jshamlet
                                                   WR_PREP, WR_SETUP_UB, WR_HOLD_UB, WR_SETUP_LB, WR_HOLD_LB,
172 175 jshamlet
                     BUSY_PREP, BUSY_WAIT,
173
                     ISSUE_INT );
174
 
175 217 jshamlet
  signal io_state            : IO_STATES := INIT;
176 175 jshamlet
 
177 217 jshamlet
  signal LCD_Data            : DATA_TYPE := x"00";
178
  signal LCD_Addr            : std_logic := '0';
179 175 jshamlet
 
180
--------------------------------------------------------------------------------
181 213 jshamlet
-- Backlight & Contrast signals
182 175 jshamlet
--------------------------------------------------------------------------------
183
 
184 217 jshamlet
  signal LCD_Contrast        : DATA_TYPE := x"00";
185
  signal LCD_Bright          : DATA_TYPE := x"00";
186 175 jshamlet
 
187
begin
188
 
189
--------------------------------------------------------------------------------
190
-- Open8 Register interface
191
--------------------------------------------------------------------------------
192
 
193 217 jshamlet
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
194 175 jshamlet
 
195
  io_reg: process( Clock, Reset )
196
  begin
197
    if( Reset = Reset_Level )then
198 217 jshamlet
      Reg_Addr_q             <= (others => '0');
199
      Wr_Data_q              <= (others => '0');
200
      Wr_En                  <= '0';
201
      Rd_En                  <= '0';
202
      Rd_Data                <= OPEN8_NULLBUS;
203 175 jshamlet
 
204 217 jshamlet
      Reg_Valid              <= '0';
205
      Reg_Sel                <= '0';
206
      Reg_Data               <= x"00";
207 175 jshamlet
 
208 217 jshamlet
      LCD_Contrast           <= Default_Contrast;
209
      LCD_Bright             <= Default_Brightness;
210 175 jshamlet
    elsif( rising_edge( Clock ) )then
211 217 jshamlet
      Reg_Addr_q             <= Reg_Addr;
212 175 jshamlet
 
213 223 jshamlet
      Wr_Data_q              <= Open8_Bus.Wr_Data;
214
      Wr_En                  <= Addr_Match and Open8_Bus.Wr_En;
215 175 jshamlet
 
216 217 jshamlet
      Reg_Valid              <= '0';
217 175 jshamlet
 
218
      if( Wr_En = '1' )then
219
        case( Reg_Addr_q )is
220
          when "00" | "01" =>
221 217 jshamlet
            Reg_Valid        <= '1';
222
            Reg_Sel          <= Reg_Addr_q(0);
223
            Reg_Data         <= Wr_Data_q;
224 175 jshamlet
          when "10" =>
225 217 jshamlet
            LCD_Contrast     <= Wr_Data_q;
226 175 jshamlet
          when "11" =>
227 217 jshamlet
            LCD_Bright       <= Wr_Data_q;
228 175 jshamlet
          when others => null;
229
        end case;
230
      end if;
231
 
232 217 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
233 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
234 175 jshamlet
      if( Rd_En = '1' )then
235
        case( Reg_Addr_q )is
236
          when "00" | "01" =>
237 217 jshamlet
            Rd_Data(7)       <= Tx_Ready;
238 175 jshamlet
          when "10" =>
239 217 jshamlet
            Rd_Data          <= LCD_Contrast;
240 175 jshamlet
          when "11" =>
241 217 jshamlet
            Rd_Data          <= LCD_Bright;
242 175 jshamlet
          when others => null;
243
        end case;
244
      end if;
245
    end if;
246
  end process;
247
 
248
--------------------------------------------------------------------------------
249
-- LCD and Register logic
250
--------------------------------------------------------------------------------
251
 
252 217 jshamlet
  LCD_RW                     <= '0'; -- Permanently wire the RW line low
253 175 jshamlet
 
254
  LCD_IO: process( Clock, Reset )
255
  begin
256
    if( Reset = Reset_Level )then
257 217 jshamlet
      io_state               <= INIT;
258
      init_count             <= (others => '0');
259
      io_timer               <= (others => '0');
260
      busy_timer             <= (others => '0');
261
      LCD_Data               <= (others => '0');
262
      LCD_Addr               <= '0';
263
      LCD_E                  <= '0';
264
      LCD_RS                 <= '0';
265
      LCD_D                  <= (others => '0');
266
      Tx_Ready               <= '0';
267
      Interrupt              <= '0';
268 175 jshamlet
    elsif( rising_edge(Clock) )then
269 217 jshamlet
      LCD_E                  <= '0';
270
      LCD_RS                 <= '0';
271
      LCD_D                  <= (others => '0');
272
      Tx_Ready               <= '0';
273
      Interrupt              <= '0';
274
      io_timer               <= io_timer - 1;
275
      busy_timer             <= busy_timer - uSec_Tick;
276 175 jshamlet
      case( io_state )is
277
 
278
        when INIT =>
279 217 jshamlet
          busy_timer         <= INIT_DELAY;
280
          init_count         <= (others => '1');
281
          io_state           <= PWR_WAIT;
282 175 jshamlet
 
283
        -- We wait for at least 40mS before continuing initalization.
284
        when PWR_WAIT =>
285
          if( busy_timer = 0 )then
286 217 jshamlet
            io_timer         <= SNH_DELAY;
287
            io_state         <= INIT_S1;
288 175 jshamlet
          end if;
289
 
290
        -- We write out the first init byte as if we were using an 8-bit
291
        --  data bus, with a single cycle. This is an exception, and the
292
        --  rest of the commands are sent using 2-cycle transfers.
293
        when INIT_S1 =>
294 217 jshamlet
          LCD_D              <= LCD_CONFIG1;
295
          LCD_E              <= '1';
296 175 jshamlet
          if( io_timer = 0 )then
297 217 jshamlet
            io_timer         <= SNH_DELAY;
298
            io_state         <= INIT_H1;
299 175 jshamlet
          end if;
300
 
301
        when INIT_H1 =>
302 217 jshamlet
          LCD_D              <= LCD_CONFIG1;
303 175 jshamlet
          if( io_timer = 0 )then
304 217 jshamlet
            busy_timer       <= BUSY_DELAY;
305
            io_state         <= INIT_WAIT;
306 175 jshamlet
          end if;
307
 
308
        when INIT_WAIT =>
309
          if( busy_timer = 0 )then
310 217 jshamlet
            io_state         <= FN_JUMP;
311 175 jshamlet
          end if;
312
 
313
        when FN_JUMP =>
314 217 jshamlet
          io_state           <= WR_PREP;
315 175 jshamlet
          case( init_count )is
316
            when "000" =>
317 217 jshamlet
              io_state       <= IDLE;
318 175 jshamlet
            when "001" =>
319 217 jshamlet
              LCD_Addr       <= '0';
320
              LCD_Data       <= LCD_CONFIG7; -- Reset the Cursor
321 175 jshamlet
            when "010" =>
322 217 jshamlet
              LCD_Addr       <= '1';         -- Print a "*", and
323
              LCD_Data       <= LCD_CONFIG6; --  set RS to 1
324 175 jshamlet
            when "011" =>
325 217 jshamlet
              LCD_Data       <= LCD_CONFIG5; -- Entry mode
326 175 jshamlet
            when "100" =>
327 217 jshamlet
              LCD_Data       <= LCD_CONFIG4; -- Clear Display
328 175 jshamlet
            when "101" =>
329 217 jshamlet
              LCD_Data       <= LCD_CONFIG3; -- Display control
330 175 jshamlet
            when "110" | "111" =>
331 217 jshamlet
              LCD_Addr       <= '0';
332
              LCD_Data       <= LCD_CONFIG2; -- Function set
333 175 jshamlet
            when others => null;
334
          end case;
335
 
336
        when IDLE =>
337 217 jshamlet
          Tx_Ready           <= '1';
338 175 jshamlet
          if( Reg_Valid = '1' )then
339 217 jshamlet
            LCD_Addr         <= Reg_Sel;
340
            LCD_Data         <= Reg_Data;
341
            io_state         <= WR_PREP;
342 175 jshamlet
          end if;
343
 
344
        when WR_PREP =>
345 217 jshamlet
          io_timer           <= SNH_DELAY;
346
          io_state           <= WR_SETUP_UB;
347 175 jshamlet
 
348
        when WR_SETUP_UB =>
349 217 jshamlet
          LCD_RS             <= LCD_Addr;
350
          LCD_D              <= LCD_Data(7 downto 4);
351
          LCD_E              <= '1';
352 175 jshamlet
          if( io_timer = 0 )then
353 217 jshamlet
            io_timer         <= SNH_DELAY;
354
            io_state         <= WR_HOLD_UB;
355 175 jshamlet
          end if;
356
 
357
        when WR_HOLD_UB =>
358 217 jshamlet
          LCD_RS             <= LCD_Addr;
359
          LCD_D              <= LCD_Data(7 downto 4);
360 175 jshamlet
          if( io_timer = 0 )then
361 217 jshamlet
            LCD_E            <= '0';
362
            io_timer         <= SNH_DELAY;
363
            io_state         <= WR_SETUP_LB;
364 175 jshamlet
          end if;
365
 
366
        when WR_SETUP_LB =>
367 217 jshamlet
          LCD_RS             <= LCD_Addr;
368
          LCD_D              <= LCD_Data(3 downto 0);
369
          LCD_E              <= '1';
370 175 jshamlet
          if( io_timer = 0 )then
371 217 jshamlet
            io_timer         <= SNH_DELAY;
372
            io_state         <= WR_HOLD_LB;
373 175 jshamlet
          end if;
374
 
375
        when WR_HOLD_LB =>
376 217 jshamlet
          LCD_RS             <= LCD_Addr;
377
          LCD_D              <= LCD_Data(3 downto 0);
378 175 jshamlet
          if( io_timer = 0 )then
379 217 jshamlet
            io_state         <= BUSY_WAIT;
380 175 jshamlet
          end if;
381
 
382
        when BUSY_PREP =>
383 217 jshamlet
          busy_timer         <= BUSY_DELAY;
384 175 jshamlet
          if( LCD_Addr = '0' and LCD_Data < 4 )then
385 217 jshamlet
            busy_timer       <= CLDSP_DELAY;
386 175 jshamlet
          end if;
387 217 jshamlet
          io_state           <= BUSY_WAIT;
388 175 jshamlet
 
389
        when BUSY_WAIT =>
390
          if( busy_timer = 0 )then
391 217 jshamlet
            io_state         <= ISSUE_INT;
392 175 jshamlet
            if( init_count > 0 )then
393 217 jshamlet
              init_count     <= init_count - 1;
394
              io_state       <= FN_JUMP;
395 175 jshamlet
            end if;
396
          end if;
397
 
398
        when ISSUE_INT =>
399 217 jshamlet
          Interrupt          <= '1';
400
          io_state           <= IDLE;
401 175 jshamlet
 
402
        when others => null;
403
 
404
      end case;
405
 
406
    end if;
407
  end process;
408
 
409
--------------------------------------------------------------------------------
410
-- Contrast control logic (optional)
411
--------------------------------------------------------------------------------
412
 
413
Contrast_Disabled: if( not Use_Contrast )generate
414
  LCD_CN                <= '0';
415
end generate;
416
 
417
Contrast_Enabled: if( Use_Contrast )generate
418
 
419 217 jshamlet
  U_CN : entity work.vdsm8
420
  generic map(
421
    Reset_Level              => Reset_Level
422
  )
423
  port map(
424
    Clock                    => Clock,
425
    Reset                    => Reset,
426
    DACin                    => LCD_Contrast,
427
    DACout                   => LCD_CN
428
  );
429 175 jshamlet
 
430
end generate;
431
 
432
--------------------------------------------------------------------------------
433
-- Backlight control logic (optional)
434
--------------------------------------------------------------------------------
435
 
436
Backlight_Disabled: if( not Use_Backlight )generate
437
  LCD_BL                <= '0';
438
end generate;
439
 
440
Backlight_Enabled: if( Use_Backlight )generate
441
 
442 217 jshamlet
  U_BL : entity work.vdsm8
443
  generic map(
444
    Reset_Level              => Reset_Level
445
  )
446
  port map(
447
    Clock                    => Clock,
448
    Reset                    => Reset,
449
    DACin                    => LCD_Bright,
450
    DACout                   => LCD_BL
451
  );
452 175 jshamlet
 
453
end generate;
454
 
455
end architecture;

powered by: WebSVN 2.1.0

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