BeagleBone Green Wireless >> C/C++

2.0" IPS 320x240(ILI9335)(Register)


main.c

#include <linux/init.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/gpio.h>
#include <asm/io.h>
 
#define ILI9335_SLCD_D17       77
#define ILI9335_SLCD_D16       76
#define ILI9335_SLCD_D15       75
#define ILI9335_SLCD_D14       74
#define ILI9335_SLCD_D13       73
#define ILI9335_SLCD_D12       72
#define ILI9335_SLCD_D11       71
#define ILI9335_SLCD_D10       70
  
#define ILI9335_SLCD_RS        86
#define ILI9335_SLCD_CS        89
#define ILI9335_SLCD_RD        88
#define ILI9335_SLCD_WR        87
#define ILI9335_SLCD_RST       20

#define GPIO0                 0x44E07000
#define GPIO1                 0x4804C000
#define GPIO2                 0x481AC000
#define GPIO3                 0x481AE000 
 
#define GPIO_REVISION         0x00
#define GPIO_SYSCONFIG        0x10
#define GPIO_EOI              0x20
#define GPIO_IRQSTATUS_RAW_0  0x24
#define GPIO_IRQSTATUS_RAW_1  0x28
#define GPIO_IRQSTATUS_0      0x2c
#define GPIO_IRQSTATUS_1      0x30
#define GPIO_IRQSTATUS_SET_0  0x34
#define GPIO_IRQSTATUS_SET_1  0x38
#define GPIO_IRQSTATUS_CLR_0  0x3c
#define GPIO_IRQSTATUS_CLR_1  0x40
#define GPIO_IRQWAKEN_0       0x44
#define GPIO_IRQWAKEN_1       0x48
#define GPIO_SYSSTATUS        0x114
#define GPIO_CTRL             0x130
#define GPIO_OE               0x134
#define GPIO_DATAIN           0x138
#define GPIO_DATAOUT          0x13c
#define GPIO_LEVELDETECT0     0x140
#define GPIO_LEVELDETECT1     0x144
#define GPIO_RISINGDETECT     0x148
#define GPIO_FALLINGDETECT    0x14c
#define GPIO_DEBOUNCENABLE    0x150
#define GPIO_DEBOUNCINGTIME   0x154
#define GPIO_CLEARDATAOUT     0x190
#define GPIO_SETDATAOUT       0x194
 
#define do_request(pin, name) \
  if(gpio_request(pin, name) < 0){ \
    printk("failed to request gpio: %s\n", name); \
  } \
  else{ \
    printk("request successfully for gpio: %s\n", name); \
    gpio_direction_output(pin, 1); \
  }
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steward_Fu");
MODULE_DESCRIPTION("ILI9335 SLCD Driver for BBGW");
 
static int g_blink_period=1000;
static struct timer_list g_blink_timer;
static volatile unsigned long *gpio2_dataout;
 
static void ili9335_reset(void)
{
  gpio_set_value(ILI9335_SLCD_RST, 1);  
  mdelay(100);
  gpio_set_value(ILI9335_SLCD_RST, 0);
  mdelay(100);  
  gpio_set_value(ILI9335_SLCD_RST, 1);  
  mdelay(100);
}
 
static void ili9335_send_command(unsigned int val)
{
  unsigned char i, dat=0;

  *gpio2_dataout&= ~(1 << (ILI9335_SLCD_RS - 64));
  *gpio2_dataout|=  (1 << (ILI9335_SLCD_RD - 64));
  *gpio2_dataout&= ~(1 << (ILI9335_SLCD_CS - 64));
  for(i=0; i<2; i++){
    dat = (i == 0) ? (val >> 8) : val;
    if(dat & 0x01){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D10 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D10 - 64));}
    if(dat & 0x02){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D11 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D11 - 64));}
    if(dat & 0x04){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D12 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D12 - 64));}
    if(dat & 0x08){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D13 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D13 - 64));}
    if(dat & 0x10){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D14 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D14 - 64));}
    if(dat & 0x20){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D15 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D15 - 64));}
    if(dat & 0x40){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D16 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D16 - 64));}
    if(dat & 0x80){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D17 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D17 - 64));}
    *gpio2_dataout&= ~(1 << (ILI9335_SLCD_WR - 64));
    *gpio2_dataout|=  (1 << (ILI9335_SLCD_WR - 64));
  }
  *gpio2_dataout|= (1 << (ILI9335_SLCD_CS - 64));
}
 
static void ili9335_send_data(unsigned int val)
{
  unsigned char i, dat=0;

  *gpio2_dataout|=  (1 << (ILI9335_SLCD_RS - 64));
  *gpio2_dataout|=  (1 << (ILI9335_SLCD_RD - 64));
  *gpio2_dataout&= ~(1 << (ILI9335_SLCD_CS - 64));
  for(i=0; i<2; i++){
    dat = (i == 0) ? (val >> 8) : val;
    if(dat & 0x01){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D10 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D10 - 64));}
    if(dat & 0x02){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D11 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D11 - 64));}
    if(dat & 0x04){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D12 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D12 - 64));}
    if(dat & 0x08){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D13 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D13 - 64));}
    if(dat & 0x10){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D14 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D14 - 64));}
    if(dat & 0x20){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D15 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D15 - 64));}
    if(dat & 0x40){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D16 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D16 - 64));}
    if(dat & 0x80){  *gpio2_dataout|=  (1 << (ILI9335_SLCD_D17 - 64));}
    else{            *gpio2_dataout&= ~(1 << (ILI9335_SLCD_D17 - 64));}
    *gpio2_dataout&= ~(1 << (ILI9335_SLCD_WR - 64));
    *gpio2_dataout|=  (1 << (ILI9335_SLCD_WR - 64));
  }
  *gpio2_dataout|= (1 << (ILI9335_SLCD_CS - 64));
}
 
static void ili9335_send_register(unsigned int cmd, unsigned int data)
{
  ili9335_send_command(cmd);
  ili9335_send_data(data);
}
 
static void ili9335_init(void)
{
  ili9335_send_register(0x0001, 0x0100);
  ili9335_send_register(0x0002, 0x0200);
  ili9335_send_register(0x0003, 0x1018);
  ili9335_send_register(0x0008, 0x0202);
  ili9335_send_register(0x0009, 0x0000);
  ili9335_send_register(0x000A, 0x0000);
  ili9335_send_register(0x000C, 0x0000);
  ili9335_send_register(0x000D, 0x0000);
  ili9335_send_register(0x0060, 0x2700);  
  ili9335_send_register(0x0061, 0x0000);
  ili9335_send_register(0x006A, 0x0000);
  mdelay(10);
  ili9335_send_register(0x0010, 0x1490);
  ili9335_send_register(0x0011, 0x0227);
  mdelay(80);
  ili9335_send_register(0x0012, 0x000c);
  mdelay(10);
  ili9335_send_register(0x0013, 0x1000);
  ili9335_send_register(0x0029, 0x000b);
  ili9335_send_register(0x002b, 0x000b);
  mdelay(10);
  ili9335_send_register(0x0020, 0x0000);
  ili9335_send_register(0x0021, 0x0000);
   
  ili9335_send_register(0x0030, 0x0000);
  ili9335_send_register(0x0031, 0x0406);
  ili9335_send_register(0x0032, 0x0002);
  ili9335_send_register(0x0035, 0x0402);
  ili9335_send_register(0x0036, 0x0004);
  ili9335_send_register(0x0037, 0x0507);
  ili9335_send_register(0x0038, 0x0103);
  ili9335_send_register(0x0039, 0x0707);
  ili9335_send_register(0x003c, 0x0204);
  ili9335_send_register(0x003d, 0x0004);
   
  ili9335_send_register(0x0050, 0x0000);
  ili9335_send_register(0x0051, 0x00ef);
  ili9335_send_register(0x0052, 0x0000);
  ili9335_send_register(0x0053, 0x013f);
 
  mdelay(10);
  ili9335_send_register(0x0007, 0x0133);  
}
 
static void show_color(unsigned int val)
{
  int i, j;
 
  ili9335_send_command(0x0022);
  for(i=0; i<240; i++){
    for(j=0; j<320; j++){
      ili9335_send_data(val);
    }
  }
}

static void print_time(void)
{
  struct timeval t;
  struct tm broken;
 
  do_gettimeofday(&t);
  time_to_tm(t.tv_sec, 0, &broken);
  printk("%d:%d:%d:%ld\n", broken.tm_hour, broken.tm_min, broken.tm_sec, t.tv_usec);
}

void blink_handler(unsigned long unused)
{
#if 0
  static int i=0;
 
  unsigned long col[]={0xf800, 0x7e0, 0x1f};
  show_color(col[i++ % 3]);
#else
  int i, j;

  print_time();
  ili9335_send_command(0x0022);
  for(i=0; i<240; i++){
    for(j=0; j<320; j++){
      if(i < 80){
        ili9335_send_data(0xf800);
      }
      else if(i < 160){
        ili9335_send_data(0x7e0);
      }
      else if(i < 240){
        ili9335_send_data(0x1f);
      }
    }
  }
  print_time();
#endif
 
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
}
 
static int __init main_init(void)
{
  do_request(ILI9335_SLCD_WR, "lcd_wr");
  do_request(ILI9335_SLCD_RD, "lcd_rd");
  do_request(ILI9335_SLCD_CS, "lcd_cs");
  do_request(ILI9335_SLCD_RS, "lcd_rs");
  do_request(ILI9335_SLCD_RST, "lcd_rst");
  do_request(ILI9335_SLCD_D10, "lcd_d10");
  do_request(ILI9335_SLCD_D11, "lcd_d11");
  do_request(ILI9335_SLCD_D12, "lcd_d12");
  do_request(ILI9335_SLCD_D13, "lcd_d13");
  do_request(ILI9335_SLCD_D14, "lcd_d14");
  do_request(ILI9335_SLCD_D15, "lcd_d15");
  do_request(ILI9335_SLCD_D16, "lcd_d16");
  do_request(ILI9335_SLCD_D17, "lcd_d17");

  gpio2_dataout = ioremap(GPIO2 + GPIO_DATAOUT, 4);
  printk("gpio2_dataout: 0x%x\n", (unsigned int)gpio2_dataout);
   
  ili9335_reset();
  ili9335_init();
   
  setup_timer(&g_blink_timer, blink_handler, 0);
  mod_timer(&g_blink_timer, jiffies + msecs_to_jiffies(g_blink_period));
  return 0;
}
  
static void __exit main_exit(void)
{
  del_timer(&g_blink_timer);
  gpio_free(ILI9335_SLCD_WR);
  gpio_free(ILI9335_SLCD_RD);
  gpio_free(ILI9335_SLCD_CS);
  gpio_free(ILI9335_SLCD_RS);
  gpio_free(ILI9335_SLCD_RST);
  gpio_free(ILI9335_SLCD_D10);
  gpio_free(ILI9335_SLCD_D11);
  gpio_free(ILI9335_SLCD_D12);
  gpio_free(ILI9335_SLCD_D13);
  gpio_free(ILI9335_SLCD_D14);
  gpio_free(ILI9335_SLCD_D15);
  gpio_free(ILI9335_SLCD_D16);
  gpio_free(ILI9335_SLCD_D17);
  iounmap(gpio2_dataout);
}
  
module_init(main_init);
module_exit(main_exit);

完成


返回上一頁