#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <asm/fcntl.h>
#include <stdio.h>
#define ADDRLCD 0x12380000
#define ADDRLCD_CMD 0x12300000
void setcommand(unsigned short);
void init_clcd();
void writeb(char ch);
unsigned short *pTextlcd;
unsigned short *pTextlcd_cmd;

int main(int argc, char **argv) {
 int fd;
 int i, len1=11, len2=19;
 char buf1[100]="Welcome to";
 char buf2[100] = "The Embedded World!";
 if(argc==2) {
  len1=strlen(argv[1]);
  len2=0;
  strcpy(buf1,argv[1]);
 }
 else if(argc>2) {
  len1=strlen(argv[1]);
  len2=strlen(argv[2]);
  strcpy(buf1,argv[1]);
  strcpy(buf2,argv[2]);
 }
 fd=open("/dev/mem", O_RDWR|O_SYNC);
 pTextlcd = mmap(NULL, 4, PROT_WRITE, MAP_SHARED, fd, ADDRLCD);
 pTextlcd_cmd = mmap(NULL, 4, PROT_WRITE, MAP_SHARED, fd, ADDRLCD_CMD);
 init_clcd();
 for(i=0;i<len1;i++) writeb(buf1[i]);
 setcommand(0xC0);
 for(i=0;i<len2;i++) writeb(buf2[i]);
 munmap(pTextlcd, 4);
 munmap(pTextlcd_cmd, 4);
 close(fd);
}

void setcommand(unsigned short command) {
 command &= 0x00FF;
 *pTextlcd_cmd = command|0x0000;
 *pTextlcd_cmd = command|0x0400;
 *pTextlcd_cmd = command|0x0000;
 usleep(50);
}

void writeb(char ch) {
 unsigned short data;
 data = ch & 0x00FF;
 *pTextlcd = data|0x100;
 *pTextlcd = data|0x500;
 *pTextlcd = data|0x100;
 usleep(50);
}

void init_clcd() {
 setcommand(0x38);
 setcommand(0x38);
 setcommand(0x38);
 setcommand(0x0e);
 setcommand(0x02);
 setcommand(0x01);
 usleep(2000);
}