root/trunk/drv_M50530.c

Revision 840, 16.0 kB (checked in by michael, 15 months ago)

email address changed

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style driver for M50530-based displays
5 *
6 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * This file is part of LCD4Linux.
10 *
11 * LCD4Linux is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * LCD4Linux is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27/*
28 *
29 * exported fuctions:
30 *
31 * struct DRIVER drv_M50530
32 *
33 */
34
35#include "config.h"
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <errno.h>
41#include <unistd.h>
42#include <termios.h>
43#include <fcntl.h>
44#include <sys/time.h>
45
46#include "debug.h"
47#include "cfg.h"
48#include "qprintf.h"
49#include "udelay.h"
50#include "plugin.h"
51#include "widget.h"
52#include "widget_text.h"
53#include "widget_icon.h"
54#include "widget_bar.h"
55#include "drv.h"
56#include "drv_generic_text.h"
57#include "drv_generic_gpio.h"
58#include "drv_generic_parport.h"
59
60static char Name[] = "M50530";
61
62static int Model;
63
64/* Timings */
65static int T_SU, T_W, T_D, T_H;
66static int T_INIT, T_EXEC, T_CLEAR;
67static int T_GPO_ST, T_GPO_PW;
68
69static unsigned char SIGNAL_RW;
70static unsigned char SIGNAL_EX;
71static unsigned char SIGNAL_IOC1;
72static unsigned char SIGNAL_IOC2;
73static unsigned char SIGNAL_GPO;
74
75static int FONT5X11;
76static int DDRAM;
77static int DUTY;
78
79/* maximum time to wait for the busy-flag (in usec) */
80#define MAX_BUSYFLAG_WAIT 10000
81
82/* maximum busy flag errors before falling back to busy-waiting */
83#define MAX_BUSYFLAG_ERRORS 20
84
85/* flag for busy-waiting vs. busy flag checking */
86static int UseBusy = 0;
87
88/* buffer holding the GPO state */
89static unsigned char GPO = 0;
90
91
92typedef struct {
93    int type;
94    char *name;
95} MODEL;
96
97static MODEL Models[] = {
98    {0x01, "generic"},
99    {0xff, "Unknown"}
100};
101
102
103/****************************************/
104/***  hardware dependant functions    ***/
105/****************************************/
106
107static void drv_M5_busy(void)
108{
109    static unsigned int errors = 0;
110
111    unsigned char data = 0xFF;
112    unsigned char busymask = 0x88;
113    unsigned int counter;
114
115    /* set data-lines to input */
116    drv_generic_parport_direction(1);
117
118    /* clear I/OC1 and I/OC2, set R/W */
119    drv_generic_parport_control(SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_RW, SIGNAL_RW);
120
121    /* Control data setup time */
122    ndelay(T_SU);
123
124    counter = 0;
125    while (1) {
126
127  /* rise enable */
128  drv_generic_parport_control(SIGNAL_EX, SIGNAL_EX);
129
130  /* data output delay time */
131  ndelay(T_D);
132
133  /* read the busy flag */
134  data = drv_generic_parport_read();
135
136  /* lower enable */
137  /* as T_D is larger than T_W, we don't need to delay again */
138  drv_generic_parport_control(SIGNAL_EX, 0);
139
140  if ((data & busymask) == 0) {
141      errors = 0;
142      break;
143  }
144
145  /* make sure we don't wait forever
146   * - but only check after 5 iterations
147   * that way, we won't slow down normal mode
148   * (where we don't need the timeout anyway)
149   */
150  counter++;
151
152  if (counter >= 5) {
153      struct timeval now, end;
154
155      if (counter == 5) {
156    /* determine the time when the timeout has expired */
157    gettimeofday(&end, NULL);
158    end.tv_usec += MAX_BUSYFLAG_WAIT;
159    while (end.tv_usec > 1000000) {
160        end.tv_usec -= 1000000;
161        end.tv_sec++;
162    }
163      }
164
165      /* get the current time */
166      gettimeofday(&now, NULL);
167      if (now.tv_sec == end.tv_sec ? now.tv_usec >= end.tv_usec : now.tv_sec >= end.tv_sec) {
168    error("%s: timeout waiting for busy flag (0x%02x)", Name, data);
169    if (++errors >= MAX_BUSYFLAG_ERRORS) {
170        error("%s: too many busy flag failures, turning off busy flag checking.", Name);
171        UseBusy = 0;
172    }
173    break;
174      }
175  }
176    }
177
178    /* clear R/W */
179    drv_generic_parport_control(SIGNAL_RW, 0);
180
181    /* honour data hold time */
182    ndelay(T_H);
183
184    /* set data-lines to output */
185    drv_generic_parport_direction(0);
186
187}
188
189
190static void drv_M5_command(const unsigned int cmd, const int delay)
191{
192
193    if (UseBusy)
194  drv_M5_busy();
195
196    /* put data on DB1..DB8 */
197    drv_generic_parport_data(cmd & 0xff);
198
199    /* set I/OC1 */
200    /* set I/OC2 */
201    /* clear RW */
202    drv_generic_parport_control(SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_RW,
203        (cmd & 0x100 ? SIGNAL_IOC1 : 0) | (cmd & 0x200 ? SIGNAL_IOC2 : 0));
204
205    /* Control data setup time */
206    ndelay(T_SU);
207
208    /* send command */
209    drv_generic_parport_toggle(SIGNAL_EX, 1, T_W);
210
211    if (UseBusy) {
212  /* honour data hold time */
213  ndelay(T_H);
214    } else {
215  /* wait for command completion */
216  udelay(delay);
217    }
218}
219
220
221static void drv_M5_clear(void)
222{
223    drv_M5_command(0x0001, T_CLEAR);  /* clear display */
224}
225
226
227static void drv_M5_write(const int row, const int col, const char *data, const int len)
228{
229    int l = len;
230    unsigned int cmd;
231    unsigned int pos;
232
233    if (row < 4) {
234  pos = row * (DDRAM >> DUTY) + col;
235    } else {
236  pos = (row - 4) * (DDRAM >> DUTY) + (DDRAM >> DUTY) / 2 + col;
237    }
238
239    drv_M5_command(0x300 | pos, T_EXEC);
240
241    while (l--) {
242  cmd = *(unsigned char *) data++;
243  drv_M5_command(0x200 | cmd, T_EXEC);
244    }
245}
246
247
248static void drv_M5_defchar(const int ascii, const unsigned char *matrix)
249{
250    int i;
251
252    drv_M5_command(0x300 + DDRAM + 8 * (ascii - CHAR0), T_EXEC);
253
254    for (i = 0; i < YRES; i++) {
255  drv_M5_command(0x200 | (matrix[i] & 0x3f), T_EXEC);
256    }
257}
258
259
260static int drv_M5_GPO(const int num, const int val)
261{
262    int v;
263
264    if (val > 0) {
265  /* set bit */
266  v = 1;
267  GPO |= 1 << num;
268    } else {
269  /* clear bit */
270  v = 0;
271  GPO &= ~(1 << num);
272    }
273
274    /* put data on DB1..DB8 */
275    drv_generic_parport_data(GPO);
276
277    /* 74HCT573 set-up time */
278    ndelay(T_GPO_ST);
279
280    /* send data */
281    /* 74HCT573 enable pulse width */
282    drv_generic_parport_toggle(SIGNAL_GPO, 1, T_GPO_PW);
283
284    return v;
285}
286
287
288static int drv_M5_start(const char *section, const int quiet)
289{
290    char *s;
291    int n;
292    int rows = -1, cols = -1;
293    unsigned int SF;
294
295    s = cfg_get(section, "Model", "generic");
296    if (s != NULL && *s != '\0') {
297  int i;
298  for (i = 0; Models[i].type != 0xff; i++) {
299      if (strcasecmp(Models[i].name, s) == 0)
300    break;
301  }
302  free(s);
303  if (Models[i].type == 0xff) {
304      error("%s: %s.Model '%s' is unknown from %s", Name, section, s, cfg_source());
305      return -1;
306  }
307  Model = i;
308  info("%s: using model '%s'", Name, Models[Model].name);
309    } else {
310  error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source());
311  free(s);
312  return -1;
313    }
314
315    s = cfg_get(section, "Size", NULL);
316    if (s == NULL || *s == '\0') {
317  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
318  free(s);
319  return -1;
320    }
321    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
322  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
323  free(s);
324  return -1;
325    }
326    free(s);
327    DROWS = rows;
328    DCOLS = cols;
329
330    s = cfg_get(section, "Font", "5x7");
331    if (s == NULL && *s == '\0') {
332  error("%s: empty '%s.Font' entry from %s", Name, section, cfg_source());
333  free(s);
334  return -1;
335    }
336    if (strcasecmp(s, "5x7") == 0) {
337  XRES = 5;
338  YRES = 7;
339  FONT5X11 = 0;
340    } else if (strcasecmp(s, "5x11") == 0) {
341  XRES = 5;
342  YRES = 11;
343  FONT5X11 = 1;
344    } else {
345  error("%s: bad '%s.Font' entry '%s' from %s", Name, section, s, cfg_source());
346  error("%s: should be '5x7' or '5x11'", Name);
347  free(s);
348  return -1;
349    }
350    free(s);
351
352
353    if (DCOLS * DROWS > 256) {
354  error("%s: %s.Size '%dx%d' is too big, would require %d bytes", Name, section, DCOLS, DROWS, DCOLS * DROWS);
355  return -1;
356    } else if (DCOLS * DROWS > 224) {
357  DDRAM = 256;
358  CHARS = 0;
359    } else if (DCOLS * DROWS > 192) {
360  DDRAM = 224;
361  CHARS = FONT5X11 ? 2 : 4;
362    } else if (DCOLS * DROWS > 160) {
363  DDRAM = 192;
364  CHARS = FONT5X11 ? 4 : 8;
365    } else {
366  DDRAM = 160;
367  CHARS = FONT5X11 ? 6 : 12;
368    }
369    info("%s: using %d words DDRAM / %d words CGRAM (%d user chars)", Name, DDRAM, 256 - DDRAM, CHARS);
370
371    if (cfg_number(section, "DUTY", 2, 0, 2, &DUTY) < 0)
372  return -1;
373    info("%s: using duty %d: 1/%d, %d lines x %d words", Name, DUTY, (XRES + 1) << DUTY, 1 << DUTY, DDRAM >> DUTY);
374
375
376    if (cfg_number(section, "GPOs", 0, 0, 8, &n) < 0)
377  return -1;
378    GPOS = n;
379    if (GPOS > 0) {
380  info("%s: controlling %d GPO's", Name, GPOS);
381    }
382
383    if (drv_generic_parport_open(section, Name) != 0) {
384  error("%s: could not initialize parallel port!", Name);
385  return -1;
386    }
387
388    if ((SIGNAL_RW = drv_generic_parport_wire_ctrl("RW", "GND")) == 0xff)
389  return -1;
390    if ((SIGNAL_EX = drv_generic_parport_wire_ctrl("EX", "STROBE")) == 0xff)
391  return -1;
392    if ((SIGNAL_IOC1 = drv_generic_parport_wire_ctrl("IOC1", "SLCTIN")) == 0xff)
393  return -1;
394    if ((SIGNAL_IOC2 = drv_generic_parport_wire_ctrl("IOC2", "AUTOFD")) == 0xff)
395  return -1;
396    if ((SIGNAL_GPO = drv_generic_parport_wire_ctrl("GPO", "GND")) == 0xff)
397  return -1;
398
399    /* Timings */
400
401    /* low level communication timings [nanoseconds]
402     * we use the worst-case default values, but allow
403     * modification from the config file.
404     */
405
406    T_SU = timing(Name, section, "SU", 200, "ns");  /* control data setup time */
407    T_W = timing(Name, section, "W", 500, "ns");  /* EX signal pulse width */
408    T_D = timing(Name, section, "D", 300, "ns");  /* Data output delay time */
409    T_H = timing(Name, section, "H", 100, "ns");  /* Data hold time */
410
411    /* GPO timing */
412    if (SIGNAL_GPO != 0) {
413  T_GPO_ST = timing(Name, section, "GPO_ST", 20, "ns"); /* 74HCT573 set-up time */
414  T_GPO_PW = timing(Name, section, "GPO_PW", 230, "ns");  /* 74HCT573 enable pulse width */
415    } else {
416  T_GPO_ST = 0;
417  T_GPO_PW = 0;
418    }
419
420    /* M50530 execution timings [microseconds]
421     * we use the worst-case default values, but allow
422     * modification from the config file.
423     */
424
425    T_EXEC = timing(Name, section, "EXEC", 20, "us"); /* normal execution time */
426    T_CLEAR = timing(Name, section, "CLEAR", 1250, "us"); /* 'clear display' execution time */
427    T_INIT = timing(Name, section, "INIT", 2000, "us"); /* mysterious initialization time */
428
429
430    /* maybe use busy-flag from now on  */
431    cfg_number(section, "UseBusy", 0, 0, 1, &UseBusy);
432
433    /* make sure we don't use the busy flag with RW wired to GND */
434    if (UseBusy && !SIGNAL_RW) {
435  error("%s: busy-flag checking is impossible with RW wired to GND!", Name);
436  UseBusy = 0;
437    }
438    info("%s: %susing busy-flag checking", Name, UseBusy ? "" : "not ");
439
440    /* clear all signals */
441    drv_generic_parport_control(SIGNAL_RW | SIGNAL_EX | SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_GPO, 0);
442
443    /* for some mysterious reason, this delay is necessary... */
444    udelay(T_INIT);
445
446    /* set direction: write */
447    drv_generic_parport_direction(0);
448
449
450    /* set function (SF) mode:
451     * Bit 0 : RAM 0
452     * Bit 1 : RAM 1
453     * Bit 2 : DUTY 0
454     * Bit 3 : DUTY 1
455     * Bit 4 : FONT: 0 = 5x12,  1 = 5x8
456     * Bit 5 : I/O:  0 = 4-bit, 1 = 8-bit
457     * Bit 6 : 1
458     * Bit 7 : 1
459     *
460     * RAM layout:
461     * 00: 256 words DDRAM,  0 words CGRAM
462     * 01: 224 words DDRAM, 32 words CGRAM
463     * 10: 192 words DDRAM, 64 words CGRAM
464     * 11: 160 words DDRAM, 96 words CGRAM
465     *
466     * Duty:
467     * 00: 1/8  (1/12 for 5x11), 1 line
468     * 00: 1/16 (1/24 for 5x11), 2 lines
469     * 00: 1/32 (1/48 for 5x11), 4 lines
470     * 11: undefined
471     */
472
473    /* 11100000 : SF, 8-bit mode */
474    SF = 0xE0;
475
476    /* RAM layout */
477    switch (DDRAM) {
478    case 160:
479  SF |= 0x03;
480  break;
481    case 192:
482  SF |= 0x02;
483  break;
484    case 224:
485  SF |= 0x01;
486  break;
487    case 256:
488  SF |= 0x00;
489  break;
490    }
491
492    /* Duty */
493    SF |= (DUTY << 2);
494
495    /* Font */
496    SF |= (!FONT5X11) << 4;
497
498
499    debug("SET FUNCTION MODE 0x%02x", SF);
500    drv_M5_command(SF, T_EXEC);
501
502
503    /* set display (SD) mode:
504     * Bit 0 : 1 = turn on character blinking
505     * Bit 0 : 1 = turn on cursor blinking
506     * Bit 0 : 1 = turn on underline display
507     * Bit 0 : 1 = turn on cursor
508     * Bit 0 : 1 = turn on display
509     * Bit 0 : 1
510     * Bit 0 : 0
511     * Bit 0 : 0
512     */
513
514    /* SD: 0x20 = 00100000 */
515    /* SD: turn off display */
516    drv_M5_command(0x0020, T_EXEC);
517
518
519    /* set entry (SE) mode:
520     * Bit 0 : 1 = inc/dec cursor address after Read
521     * Bit 1 : 1 = inc/dec cursor address after Write
522     * Bit 2 : 0 = inc, 1 = dec
523     * Bit 3 : 1 = inc/dec display start addr after Read
524     * Bit 4 : 1 = inc/dec display start addr after Write
525     * Bit 5 : 0 = inc, 1 = dec
526     * Bit 6 : 1
527     * Bit 7 : 0
528     *
529     */
530
531    /* SE: 0x50 = 01010000 */
532    /* SE: increment display start after Write */
533    drv_M5_command(0x0050, T_EXEC);
534
535
536    /* SD: 0x30 = 00110000 */
537    /* SD: turn on display */
538    drv_M5_command(0x0030, T_EXEC);
539
540
541    drv_M5_clear();
542
543    if (!quiet) {
544  char buffer[40];
545  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
546  if (drv_generic_text_greet(buffer, NULL)) {
547      sleep(3);
548      drv_M5_clear();
549  }
550    }
551
552    return 0;
553}
554
555
556/****************************************/
557/***            plugins               ***/
558/****************************************/
559
560/* none at the moment */
561
562
563/****************************************/
564/***        widget callbacks          ***/
565/****************************************/
566
567/* using drv_generic_text_draw(W) */
568/* using drv_generic_text_icon_draw(W) */
569/* using drv_generic_text_bar_draw(W) */
570/* using drv_generic_gpio_draw(W) */
571
572
573/****************************************/
574/***        exported functions        ***/
575/****************************************/
576
577
578/* list models */
579int drv_M5_list(void)
580{
581    int i;
582
583    for (i = 0; Models[i].type != 0xff; i++) {
584  printf("%s ", Models[i].name);
585    }
586    return 0;
587}
588
589
590/* initialize driver & display */
591int drv_M5_init(const char *section, const int quiet)
592{
593    WIDGET_CLASS wc;
594    int ret;
595
596    info("%s: %s", Name, "$Rev$");
597
598    /* display preferences */
599    XRES = -1;      /* pixel width of one char  */
600    YRES = -1;      /* pixel height of one char  */
601    CHARS = 0;      /* number of user-defineable characters */
602    CHAR0 = 248;    /* ASCII of first user-defineable char */
603    GOTO_COST = 1;    /* number of bytes a goto command requires */
604
605    /* real worker functions */
606    drv_generic_text_real_write = drv_M5_write;
607    drv_generic_text_real_defchar = drv_M5_defchar;
608    drv_generic_gpio_real_set = drv_M5_GPO;
609
610
611    /* start display */
612    if ((ret = drv_M5_start(section, quiet)) != 0)
613  return ret;
614
615    /* initialize generic text driver */
616    if ((ret = drv_generic_text_init(section, Name)) != 0)
617  return ret;
618
619    /* initialize generic icon driver */
620    if ((ret = drv_generic_text_icon_init()) != 0)
621  return ret;
622
623    /* initialize generic bar driver */
624    if ((ret = drv_generic_text_bar_init(0)) != 0)
625  return ret;
626
627    /* add fixed chars to the bar driver */
628    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
629
630    /* initialize generic GPIO driver */
631    if ((ret = drv_generic_gpio_init(section, Name)) != 0)
632  return ret;
633
634    /* register text widget */
635    wc = Widget_Text;
636    wc.draw = drv_generic_text_draw;
637    widget_register(&wc);
638
639    /* register icon widget */
640    wc = Widget_Icon;
641    wc.draw = drv_generic_text_icon_draw;
642    widget_register(&wc);
643
644    /* register bar widget */
645    wc = Widget_Bar;
646    wc.draw = drv_generic_text_bar_draw;
647    widget_register(&wc);
648
649    /* register plugins */
650    /* none at the moment */
651
652    return 0;
653}
654
655
656/* close driver & display */
657int drv_M5_quit(const int quiet)
658{
659
660    info("%s: shutting down.", Name);
661
662    drv_generic_text_quit();
663    drv_generic_gpio_quit();
664
665    /* clear display */
666    drv_M5_clear();
667
668    /* say goodbye... */
669    if (!quiet) {
670  drv_generic_text_greet("goodbye!", NULL);
671    }
672
673    /* clear all signals */
674    drv_generic_parport_control(SIGNAL_RW | SIGNAL_EX | SIGNAL_IOC1 | SIGNAL_IOC2 | SIGNAL_GPO, 0);
675
676    /* close port */
677    drv_generic_parport_close();
678
679    return (0);
680}
681
682
683DRIVER drv_M50530 = {
684    .name = Name,
685    .list = drv_M5_list,
686    .init = drv_M5_init,
687    .quit = drv_M5_quit,
688};
Note: See TracBrowser for help on using the browser.