root/trunk/drv_RouterBoard.c

Revision 771, 15.9 kB (checked in by michael, 21 months ago)

lots of compiler warnings removed, C++-style comments removed, changed struc initialisation from 'field:value' to '.field=value'

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * driver for the "Router Board LCD port"
5 * see port details at http://www.routerboard.com
6 *
7 * Copyright (C) 2004  Roman Jozsef <rjoco77@freemail.hu>
8 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9 *
10 * based on the HD44780 parallel port driver and RB SDK example
11 *
12 * This file is part of LCD4Linux.
13 *
14 * LCD4Linux is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * LCD4Linux is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 */
29
30
31/* This particulary board not have paralell port but have a special LCD header
32 * where can connect an HD44780 display.
33 * This port called IOSC0 port, and is write only, this port control the
34 * 4 leds on board too.
35 * Because its a write only port you can't control leds outside lcd driver
36 * or inverse, for this added the socket controlled leds. To send led status
37 * simply open an UDP socket and send to localhost 127.0.0.1 port 3333 one
38 * byte or more anyway only the first byte 4 low bits used the others is
39 * cleared and ignored bit0 = Led1 ,bit1 = Led2 ...
40 * This socket polled via timer callback, for detail see at drv_RB_start()
41 * I add at to end of this file an example!
42 * If you don't want coment #define RB_WITH LEDS and this part will be ignored
43 *
44 * The connection details:
45 *    The IOCS0 port lower 16 bits connected as follows:
46 *     bit   LCD  LEDS
47 *     0..7  data
48 *      8    INITX
49 *      9    SLINX
50 *      10   AFDX
51 *      11   backlit
52 *      12      LED1
53 *      13      LED2
54 *      14      LED3
55 *      15      LED4
56 *   
57 * LCD male header:
58 * 1  Vcc +5V
59 * 2  GND
60 * 3  RS (Register Select,AFDX)
61 * 4  Contrast adjust (controlled) but how? if you know tell me not mentioned on User Manual
62 * 5  E (enable signal, INITX)
63 * 6  R/W (Data read/write or SLINX) not used connect LCD pin to ground
64 * 7  Data 1
65 * 8  Data 0
66 * 9  Data 3
67 * 10 Data 2
68 * 11 Data 5
69 * 12 Data 4
70 * 13 Data 7
71 * 14 Data 6
72 * 15 Backlit GND (controlled) (IOSC0 bit 11)
73 * 16   Backlit Vcc +5V
74 *
75 * If you using this driver and board and you have any fun device connected,
76 * program or story :-) ,please share for me. Thanks.
77 *         
78 * Literature
79 *    [GEODE] Geode SC1100 Information Appliance On a Chip
80 *      (http://www.national.com/ds/SC/SC1100.pdf)     
81 *    [RB User Manual]
82 *      (http://www.routerboard.com)
83 *
84 *
85 * Revision 1.2
86 * Added backlight control
87 */
88
89
90/*
91 *
92 * exported fuctions:
93 *
94 * struct DRIVER drv_RouterBoard
95 *
96 */
97
98
99
100
101
102#include "config.h"
103
104#include <stdio.h>
105#include <stdlib.h>
106#include <string.h>
107#include <errno.h>
108#include <unistd.h>
109#include <sys/io.h>
110
111#include "debug.h"
112#include "cfg.h"
113#include "udelay.h"
114#include "qprintf.h"
115#include "plugin.h"
116#include "widget.h"
117#include "widget_text.h"
118#include "widget_icon.h"
119#include "widget_bar.h"
120#include "drv.h"
121#include "drv_generic_text.h"
122#include "drv_generic_gpio.h"
123
124
125/* #define RB_WITH_LEDS 1 */
126
127#ifdef RB_WITH_LEDS   /* Build without socket&led support */
128
129#include <arpa/inet.h>
130#include <sys/socket.h>
131#include <sys/poll.h>
132
133#define POLL_TIMEOUT  10  /* Socket poll timeout */
134#define MAXMSG_SIZE 100 /* Max messagge we can receive */
135
136static int sock_c;    /* Socket handler */
137static struct sockaddr_in *sacl;
138char sock_msg[MAXMSG_SIZE];
139
140#endif
141
142
143static char Name[] = "RouterBoard";
144
145static int Model;
146static int Capabilities;
147
148/* RouterBoard control signals */
149
150#define LCD_INITX     0x0100
151#define LCD_SLINX     0x0200
152#define LCD_AFDX      0x0400
153#define LCD_BACKLIGHT 0x0800
154#define RB_LEDS       0xF000
155
156#define CAR 0x0CF8
157#define CDR 0x0CFC
158
159
160/* HD44780 execution timings [microseconds]
161 * as these values differ from spec to spec,
162 * we use the worst-case values.
163 */
164
165#define T_INIT1 4100    /* first init sequence:  4.1 msec */
166#define T_INIT2  100    /* second init sequence: 100 usec */
167#define T_EXEC    80    /* normal execution time */
168#define T_WRCG   120    /* CG RAM Write */
169#define T_CLEAR 1680    /* Clear Display */
170#define T_AS      60    /* Address setup time */
171
172
173/* buffer holding the GPO state */
174static unsigned char GPO = 0;
175
176/* buffor holding backlight and LED states */
177static unsigned int RB_Leds = 0;
178
179typedef struct {
180    int type;
181    char *name;
182    int capabilities;
183} MODEL;
184
185#define CAP_HD66712    (1<<0)
186
187static MODEL Models[] = {
188    {0x01, "HD44780", 0},
189    {0x02, "HD66712", CAP_HD66712},
190    {0xff, "Unknown", 0}
191};
192
193
194/****************************************/
195/***  hardware dependant functions    ***/
196/****************************************/
197
198#ifdef RB_WITH_LEDS
199
200static int drv_RB_sock_init()
201{
202
203    if ((sacl = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in))) == NULL) {
204  return -1;
205    }
206
207    memset(sacl, 0, sizeof(struct sockaddr_in));
208    sacl->sin_family = AF_INET;
209    sacl->sin_port = htons(3333); /* Listen Port */
210    sacl->sin_addr.s_addr = inet_addr("127.0.0.1"); /* Listen Address */
211
212    if ((sock_c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
213  error("Socket open failed");
214  free(sacl);
215  return -1;
216    }
217
218    if (bind(sock_c, (struct sockaddr *) sacl, sizeof(struct sockaddr_in)) < 0) {
219  error("Socket bind open failed");
220  free(sacl);
221  return -1;
222    }
223    return 0;
224}
225
226
227static void drv_RB_poll_data(void __attribute__ ((unused)) * notused)
228{
229    int len, size;
230    struct pollfd usfd;
231    usfd.fd = sock_c;
232    usfd.events = POLLIN | POLLPRI;
233    while (poll(&usfd, 1, POLL_TIMEOUT) > 0) {
234  len = sizeof(struct sockaddr_in);
235  if ((size = recvfrom(sock_c, sock_msg, MAXMSG_SIZE, 0, (struct sockaddr *) sacl, (socklen_t *) & len)) < 0);
236  else {
237      RB_Leds &= 0x0fff;
238      RB_Leds |= (sock_msg[0] & 0x0F) << 12;
239      /* fprintf(stderr, "Received data %s\n",sock_msg); */
240  }
241    }
242}
243
244#endif
245
246
247static void drv_RB_outw(const unsigned int data)
248{
249    static unsigned int port = 0;
250
251    /* IOCS0 port number can read from PCI Configuration Space Function 0 (F0) */
252    /* at index 74h as 16 bit value (see [GEODE] 5.3.1 pg.151 and pg.176 Table 5-29 */
253    if (port == 0) {
254  /* get IO permission, here you can't use ioperm command */
255  iopl(3);
256  outl(0x80009074, CAR);
257  port = inw(CDR);
258    }
259
260    outw(data | RB_Leds, port);
261}
262
263
264static int drv_RB_backlight(int backlight)
265{
266    /* -1 is used to query  the current Backlight */
267    if (backlight == -1) {
268  return (RB_Leds & LCD_BACKLIGHT) ? 1 : 0;
269    }
270
271    if (backlight > 0) {
272  /* set bit */
273  RB_Leds |= LCD_BACKLIGHT;
274  backlight = 1;
275    } else {
276  backlight = 0;
277  /* clear bit */
278  RB_Leds &= ~LCD_BACKLIGHT;
279    }
280
281    /* Set backlight output */
282    drv_RB_outw(0);
283
284    return backlight;
285}
286
287
288
289
290static void drv_RB_command(const unsigned char cmd, const int delay)
291{
292
293    drv_RB_outw(LCD_INITX | cmd);
294    ndelay(T_AS);
295    drv_RB_outw(cmd);
296
297    /* wait for command completion */
298    udelay(delay);
299
300}
301
302
303static void drv_RB_data(const char *string, const int len, const int delay)
304{
305    int l = len;
306    unsigned char ch;
307
308    /* sanity check */
309    if (len <= 0)
310  return;
311
312    while (l--) {
313
314  ch = *(string++);
315
316  drv_RB_outw(ch | LCD_AFDX | LCD_INITX);
317  ndelay(T_AS);
318  drv_RB_outw(ch | LCD_AFDX);
319
320  /* wait for command completion */
321  udelay(delay);
322
323    }
324}
325
326
327static void drv_RB_clear(void)
328{
329    drv_RB_command(0x01, T_CLEAR);
330}
331
332
333static void drv_RB_goto(int row, int col)
334{
335    int pos;
336
337    /* 16x1 Displays are organized as 8x2 :-( */
338    if (DCOLS == 16 && DROWS == 1 && col > 7) {
339  row++;
340  col -= 8;
341    }
342
343    if (Capabilities & CAP_HD66712) {
344  /* the HD66712 doesn't have a braindamadged RAM layout */
345  pos = row * 32 + col;
346    } else {
347  /* 16x4 Displays use a slightly different layout */
348  if (DCOLS == 16 && DROWS == 4) {
349      pos = (row % 2) * 64 + (row / 2) * 16 + col;
350  } else {
351      pos = (row % 2) * 64 + (row / 2) * 20 + col;
352  }
353    }
354    drv_RB_command((0x80 | pos), T_EXEC);
355}
356
357
358static void drv_RB_write(const int row, const int col, const char *data, const int len)
359{
360    drv_RB_goto(row, col);
361    drv_RB_data(data, len, T_EXEC);
362}
363
364
365static void drv_RB_defchar(const int ascii, const unsigned char *matrix)
366{
367    int i;
368    char buffer[8];
369
370    for (i = 0; i < 8; i++) {
371  buffer[i] = matrix[i] & 0x1f;
372    }
373
374    drv_RB_command(0x40 | 8 * ascii, T_EXEC);
375    drv_RB_data(buffer, 8, T_WRCG);
376}
377
378
379static int drv_RB_GPO(const int num, const int val)
380{
381    int v;
382
383    if (val > 0) {
384  /* set bit */
385  v = 1;
386  GPO |= 1 << num;
387    } else {
388  /* clear bit */
389  v = 0;
390  GPO &= ~(1 << num);
391    }
392
393    RB_Leds &= 0x0fff;
394    RB_Leds |= GPO << 12;
395
396    drv_RB_outw(0);
397
398    return v;
399}
400
401
402static int drv_RB_start(const char *section, const int quiet)
403{
404    char *model, *strsize;
405    int rows = -1, cols = -1, gpos = -1;
406    int l;
407
408    model = cfg_get(section, "Model", "HD44780");
409    if (model != NULL && *model != '\0') {
410  int i;
411  for (i = 0; Models[i].type != 0xff; i++) {
412      if (strcasecmp(Models[i].name, model) == 0)
413    break;
414  }
415  if (Models[i].type == 0xff) {
416      error("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source());
417      return -1;
418  }
419  Model = i;
420  Capabilities = Models[Model].capabilities;
421  info("%s: using model '%s'", Name, Models[Model].name);
422    } else {
423  error("%s: empty '%s.Model' entry from %s", Name, section, cfg_source());
424  return -1;
425    }
426    free(model);
427
428    strsize = cfg_get(section, "Size", NULL);
429    if (strsize == NULL || *strsize == '\0') {
430  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
431  free(strsize);
432  return -1;
433    }
434    if (sscanf(strsize, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
435  error("%s: bad %s.Size '%s' from %s", Name, section, strsize, cfg_source());
436  free(strsize);
437  return -1;
438    }
439    free(strsize);
440
441    /*set backlight */
442    if (cfg_number(section, "Backlight", 1, 0, 1, &l) > 0) {
443  drv_RB_backlight(l);
444    }
445
446    if (cfg_number(section, "GPOs", 0, 0, 4, &gpos) < 0)
447  return -1;
448    GPOS = gpos;
449    if (GPOS > 0) {
450  info("%s: using %d GPO's", Name, GPOS);
451    }
452#ifdef RB_WITH_LEDS
453
454    if (drv_RB_sock_init() < 0) {
455  error("Sock error");
456  return -1;
457    } else
458  timer_add(drv_RB_poll_data, NULL, 500, 0);
459
460#endif
461
462    DROWS = rows;
463    DCOLS = cols;
464
465    drv_RB_command(0x30, T_INIT1);  /* 8 Bit mode, wait 4.1 ms */
466    drv_RB_command(0x30, T_INIT2);  /* 8 Bit mode, wait 100 us */
467    drv_RB_command(0x38, T_EXEC); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
468
469    drv_RB_command(0x08, T_EXEC); /* Display off, cursor off, blink off */
470    drv_RB_command(0x0c, T_CLEAR);  /* Display on, cursor off, blink off, wait 1.64 ms */
471    drv_RB_command(0x06, T_EXEC); /* curser moves to right, no shift */
472
473    if ((Capabilities & CAP_HD66712) && DROWS > 2) {
474  drv_RB_command(0x3c, T_EXEC); /* set extended register enable bit */
475  drv_RB_command(0x09, T_EXEC); /* set 4-line mode */
476  drv_RB_command(0x38, T_EXEC); /* clear extended register enable bit */
477    }
478
479    drv_RB_clear();
480    drv_RB_command(0x03, T_CLEAR);  /* return home */
481
482    if (!quiet) {
483  char buffer[40];
484  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
485  if (drv_generic_text_greet(buffer, NULL)) {
486      sleep(3);
487      drv_RB_clear();
488  }
489    }
490
491    return 0;
492}
493
494
495/****************************************/
496/***            plugins               ***/
497/****************************************/
498static void plugin_backlight(RESULT * result, const int argc, RESULT * argv[])
499{
500    double backlight;
501
502    switch (argc) {
503    case 0:
504  backlight = drv_RB_backlight(-1);
505  SetResult(&result, R_NUMBER, &backlight);
506  break;
507    case 1:
508  backlight = drv_RB_backlight(R2N(argv[0]));
509  SetResult(&result, R_NUMBER, &backlight);
510  break;
511    default:
512  error("%s::backlight(): wrong number of parameters", Name);
513  SetResult(&result, R_STRING, "");
514    }
515}
516
517
518
519/****************************************/
520/***        widget callbacks          ***/
521/****************************************/
522
523
524/* using drv_generic_text_draw(W) */
525/* using drv_generic_text_icon_draw(W) */
526/* using drv_generic_text_bar_draw(W) */
527/* using drv_generic_gpio_draw(W) */
528
529
530/****************************************/
531/***        exported functions        ***/
532/****************************************/
533
534
535/* list models */
536int drv_RB_list(void)
537{
538    int i;
539
540    for (i = 0; Models[i].type != 0xff; i++) {
541  printf("%s ", Models[i].name);
542    }
543    return 0;
544}
545
546/* initialize driver & display */
547int drv_RB_init(const char *section, const int quiet)
548{
549    WIDGET_CLASS wc;
550    int asc255bug;
551    int ret;
552
553    info("%s: %s", Name, "$Rev$");
554
555    /* display preferences */
556    XRES = 5;     /* pixel width of one char  */
557    YRES = 8;     /* pixel height of one char  */
558    CHARS = 8;      /* number of user-defineable characters */
559    CHAR0 = 0;      /* ASCII of first user-defineable char */
560    GOTO_COST = 2;    /* number of bytes a goto command requires */
561
562    /* real worker functions */
563    drv_generic_text_real_write = drv_RB_write;
564    drv_generic_text_real_defchar = drv_RB_defchar;
565    drv_generic_gpio_real_set = drv_RB_GPO;
566
567
568    /* start display */
569    if ((ret = drv_RB_start(section, quiet)) != 0)
570  return ret;
571
572    /* initialize generic text driver */
573    if ((ret = drv_generic_text_init(section, Name)) != 0)
574  return ret;
575
576    /* initialize generic icon driver */
577    if ((ret = drv_generic_text_icon_init()) != 0)
578  return ret;
579
580    /* initialize generic bar driver */
581    if ((ret = drv_generic_text_bar_init(0)) != 0)
582  return ret;
583
584    /* add fixed chars to the bar driver */
585    /* most displays have a full block on ascii 255, but some have kind of  */
586    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
587    /* char will not be used, but rendered by the bar driver */
588    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
589    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
590    if (!asc255bug)
591  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
592
593    /* initialize generic GPIO driver */
594    if ((ret = drv_generic_gpio_init(section, Name)) != 0)
595  return ret;
596
597    /* register text widget */
598    wc = Widget_Text;
599    wc.draw = drv_generic_text_draw;
600    widget_register(&wc);
601
602    /* register icon widget */
603    wc = Widget_Icon;
604    wc.draw = drv_generic_text_icon_draw;
605    widget_register(&wc);
606
607    /* register bar widget */
608    wc = Widget_Bar;
609    wc.draw = drv_generic_text_bar_draw;
610    widget_register(&wc);
611
612    /* register plugins */
613    AddFunction("LCD::backlight", -1, plugin_backlight);
614
615    return 0;
616}
617
618
619/* close driver & display */
620int drv_RB_quit(const int quiet)
621{
622
623    info("%s: shutting down.", Name);
624
625    drv_generic_text_quit();
626    drv_generic_gpio_quit();
627
628    /* clear *both* displays */
629    drv_RB_clear();
630
631    /* say goodbye... */
632    if (!quiet) {
633  drv_generic_text_greet("goodbye!", NULL);
634    }
635#ifdef RB_WITH_LEDS
636    close(sock_c);
637    free(sacl);     /*close network socket */
638#endif
639
640    return (0);
641}
642
643
644DRIVER drv_RouterBoard = {
645    .name = Name,
646    .list = drv_RB_list,
647    .init = drv_RB_init,
648    .quit = drv_RB_quit,
649};
650
651
652
653#if 0
654
655Simple example to send led status to port 3333
656#include <stdio.h>
657#include <arpa/inet.h>
658#include <errno.h>
659int send_packet(unsigned char leds)
660{
661    struct sockaddr_in *sas;
662    int sock;
663    char msg[20];
664    msg[0] = leds;
665    msg[1] = 0;
666
667    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
668  fprintf(stderr, "Socket option failed.\n");
669  return -1;
670    }
671
672    if ((sas = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in))) == NULL)
673  return -1;
674    memset(sas, 0, sizeof(struct sockaddr_in));
675    sas->sin_family = AF_INET;
676    sas->sin_port = htons(3333);
677    sas->sin_addr.s_addr = inet_addr("127.0.0.1");
678    if (sendto(sock, msg, 6, 0, (struct sockaddr *) sas, sizeof(struct sockaddr_in)) > 0) {
679  free(sas);
680  return 1;
681    }
682    /* sent ok to dest */
683    free(sas);
684    return -1;      /* Send failed */
685}
686
687int main()
688{
689    send_packet(0x03);
690    return 0;
691}
692
693#endif
Note: See TracBrowser for help on using the browser.