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 223

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

powered by: WebSVN 2.1.0

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