#include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include #include #include #include #include #include #include using namespace std; #define HW_PINCTRL_DOE1_SET 0x80018b14 #define HW_PINCTRL_DOE2_SET 0x80018b24 #define HW_PINCTRL_DOE3_SET 0x80018b34 #define HW_PINCTRL_DOUT1_SET 0x80018714 #define HW_PINCTRL_DOUT2_SET 0x80018724 #define HW_PINCTRL_DOUT3_SET 0x80018734 #define HW_PINCTRL_DOUT1_CLR 0x80018718 #define HW_PINCTRL_DOUT2_CLR 0x80018728 #define HW_PINCTRL_DOUT3_CLR 0x80018738 #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) // color definitions #define BLACK 0x0000 #define BLUE 0x001F #define RED 0xF800 #define GREEN 0x07E0 #define CYAN 0x07FF #define MAGENTA 0xF81F #define YELLOW 0xFFE0 #define WHITE 0xFFFF #define HX8357_SWRESET 0x01 #define HX8357_GET_RED_CHANNEL 0x06 #define HX8357_GET_GREEN_CHANNEL 0x07 #define HX8357_GET_BLUE_CHANNEL 0x08 #define HX8357_GET_POWER_MODE 0x0a #define HX8357_GET_MADCTL 0x0b #define HX8357_GET_PIXEL_FORMAT 0x0c #define HX8357_GET_DISPLAY_MODE 0x0d #define HX8357_GET_SIGNAL_MODE 0x0e #define HX8357_GET_DIAGNOSTIC_RESULT 0x0f #define HX8357_ENTER_SLEEP_MODE 0x10 #define HX8357_EXIT_SLEEP_MODE 0x11 #define HX8357_ENTER_PARTIAL_MODE 0x12 #define HX8357_ENTER_NORMAL_MODE 0x13 #define HX8357_EXIT_INVERSION_MODE 0x20 #define HX8357_ENTER_INVERSION_MODE 0x21 #define HX8357_SET_DISPLAY_OFF 0x28 #define HX8357_SET_DISPLAY_ON 0x29 #define HX8357_SET_COLUMN_ADDRESS 0x2a #define HX8357_SET_PAGE_ADDRESS 0x2b #define HX8357_WRITE_MEMORY_START 0x2c #define HX8357_READ_MEMORY_START 0x2e #define HX8357_SET_PARTIAL_AREA 0x30 #define HX8357_SET_SCROLL_AREA 0x33 #define HX8357_SET_TEAR_OFF 0x34 #define HX8357_SET_TEAR_ON 0x35 #define HX8357_SET_ADDRESS_MODE 0x36 #define HX8357_SET_SCROLL_START 0x37 #define HX8357_EXIT_IDLE_MODE 0x38 #define HX8357_ENTER_IDLE_MODE 0x39 #define HX8357_SET_PIXEL_FORMAT 0x3a #define HX8357_SET_PIXEL_FORMAT_DBI_3BIT (0x1) #define HX8357_SET_PIXEL_FORMAT_DBI_16BIT (0x5) #define HX8357_SET_PIXEL_FORMAT_DBI_18BIT (0x6) #define HX8357_SET_PIXEL_FORMAT_DPI_3BIT (0x1 << 4) #define HX8357_SET_PIXEL_FORMAT_DPI_16BIT (0x5 << 4) #define HX8357_SET_PIXEL_FORMAT_DPI_18BIT (0x6 << 4) #define HX8357_WRITE_MEMORY_CONTINUE 0x3c #define HX8357_READ_MEMORY_CONTINUE 0x3e #define HX8357_SET_TEAR_SCAN_LINES 0x44 #define HX8357_GET_SCAN_LINES 0x45 #define HX8357_READ_DDB_START 0xa1 #define HX8357_SET_DISPLAY_MODE 0xb4 #define HX8357_SET_DISPLAY_MODE_RGB_THROUGH (0x3) #define HX8357_SET_DISPLAY_MODE_RGB_INTERFACE (1 << 4) #define HX8357_SET_PANEL_DRIVING 0xc0 #define HX8357_SET_DISPLAY_FRAME 0xc5 #define HX8357_SET_RGB 0xc6 #define HX8357_SET_RGB_ENABLE_HIGH (1 << 1) #define HX8357_SET_GAMMA 0xc8 #define HX8357_SET_POWER 0xd0 #define HX8357_SET_VCOM 0xd1 #define HX8357_SET_POWER_NORMAL 0xd2 #define HX8357_SET_PANEL_RELATED 0xe9 void *set_data_lines_cd_rd_mmap = 0; void *set_wr_mmap = 0; void *set_backlight_cs_reset_mmap = 0; void *clr_data_lines_cd_rd_mmap = 0; void *clr_wr_mmap = 0; void *clr_backlight_cs_reset_mmap = 0; void *CS_pin, *CD_pin, *WR_pin, *RESET_pin, *RD_pin, *BACKLIGHT_pin; int devmem_handle = 0; vector LCD_BUS; static inline void *fixup_addr(void *addr, size_t size); // ************************************************* // static unsigned long x=123456789, y=362436069, z=521288629; unsigned long xorshf96(void) { //period 2^96-1 unsigned long t; x ^= x << 16; x ^= x >> 5; x ^= x << 1; t = x; x = y; y = z; z = t ^ x ^ y; return z; } // ************************************************* // static inline void sleep_nop(unsigned long nops) { unsigned long i=0; do { asm("nop"); i++; } while(i> 8,'d'); write_to_LCD(x1 & 0xFF,'d'); //start 0x0000 write_to_LCD(x2 >> 8,'d'); write_to_LCD(x2 & 0xFF,'d'); //end 0x00EF write_to_LCD(HX8357_SET_PAGE_ADDRESS,'c'); //page address set write_to_LCD(y1 >> 8,'d'); write_to_LCD(y1 & 0xFF,'d'); //end 0x013F write_to_LCD(y2 >> 8,'d'); write_to_LCD(y2 & 0xFF,'d'); //end 0x013F } // ************************************************* // void display_home(void) { write_to_LCD(HX8357_SET_COLUMN_ADDRESS,'c'); write_to_LCD(0x00,'d'); write_to_LCD(0x00,'d'); write_to_LCD(0x01,'d'); // 01 write_to_LCD(0x3F,'d'); //3F write_to_LCD(HX8357_SET_PAGE_ADDRESS,'c'); write_to_LCD(0x00,'d'); write_to_LCD(0x00,'d'); write_to_LCD(0x01,'d'); // 00 write_to_LCD(0xDF,'d'); // EF write_to_LCD(HX8357_WRITE_MEMORY_START,'c'); } // ************************************************* // void show_single_color_on_display(unsigned int color ) { unsigned int t,l; for(l=0;l<480;l++) { for(t=0;t<320;t++) { write_to_LCD(color,'d'); } } } // ************************************************* // void color_bars() { unsigned int i,j; display_home(); for(i=0;i<480;i++) { for(j=0;j<320;j++) { if(i>419)write_to_LCD(BLACK,'d'); else if(i>359)write_to_LCD(MAGENTA,'d'); else if(i>299)write_to_LCD(BLUE,'d'); else if(i>239)write_to_LCD(CYAN,'d'); else if(i>179)write_to_LCD(GREEN,'d'); else if(i>119)write_to_LCD(YELLOW,'d'); else if(i>59)write_to_LCD(RED,'d'); else write_to_LCD(WHITE,'d'); } } } // ************************************************* // void export_gpio_line(unsigned int line) { string export_gpios; fstream exports; exports.open("/sys/class/gpio/export",fstream::out); if(exports.is_open()) { exports << itoa(line) << endl; exports.close(); } else cout << "couldn't open file" << endl; string directionstring; directionstring += "/sys/class/gpio/gpio"; directionstring += itoa(line); directionstring += "/direction"; exports.open(directionstring.c_str(),fstream::out); if(exports.is_open()) { exports << "out" << endl; exports.close(); } else cout << "couldn't open file" << endl; } // ************************************************* // void put_pixel(short x, short y, int color) { set_window(x, x, y, y); write_to_LCD(HX8357_WRITE_MEMORY_START,'c'); write_to_LCD(color,'d'); } // ************************************************* // void line(int x1, int y1, int x2, int y2, int color) { signed int x, y, addx, addy, dx, dy; signed long P; int i; dx = abs((signed int)(x2 - x1)); dy = abs((signed int)(y2 - y1)); x = x1; y = y1; if(x1 > x2) addx = -1; else addx = 1; if(y1 > y2) addy = -1; else addy = 1; if(dx >= dy) { P = 2*dy - dx; for(i=0; i<=dx; ++i) { put_pixel(x, y, color); if(P < 0) { P += 2*dy; x += addx; } else { P += 2*dy - 2*dx; x += addx; y += addy; } } } else { P = 2*dx - dy; for(i=0; i<=dy; ++i) { put_pixel(x, y, color); if(P < 0) { P += 2*dx; y += addy; } else { P += 2*dx - 2*dy; x += addx; y += addy; } } } } // ************************************************* // void rect(int x1, int y1, int x2, int y2, int fill, int color) { if(fill) { int y, ymax; // Find the y min and max if(y1 < y2) { y = y1; ymax = y2; } else { y = y2; ymax = y1; } for(; y<=ymax; ++y) // Draw lines to fill the rectangle line(x1, y, x2, y, color); } else { line(x1, y1, x2, y1, color); // Draw the 4 sides line(x1, y2, x2, y2, color); line(x1, y1, x1, y2, color); line(x2, y1, x2, y2, color); } } // ************************************************* // static unsigned int g_seed; //Used to seed the generator. inline void fast_srand( int seed ) { g_seed = seed; } // ************************************************* // inline int fastrand() { g_seed = (214013*g_seed+2531011); return (g_seed>>16)&0x7FFF; } // ************************************************* // void random_rectangles() { int count = 100000; srand(1); /* seed for rand */ while(0 < count--) { int x1, y1; int x2, y2; int random_color, fill; int temp; /* random number between 0 and xres */ x1 = (int)((320*1.0*rand())/(RAND_MAX+1.0)); x2 = (int)((320*1.0*rand())/(RAND_MAX+1.0)); /* random number between 0 and yres */ y1 = (int)((480*1.0*rand())/(RAND_MAX+1.0)); y2 = (int)((480*1.0*rand())/(RAND_MAX+1.0)); if(x1 > x2) { temp = x1; x1 = x2; x2 = temp; } if(y1 > y2) { temp = y1; y1 = y2; y2 = temp; } // random_color = (int)(65536.0*rand()/(RAND_MAX+1.0)); random_color = (int)(fastrand()<<1);///(RAND_MAX+1.0)); // random_color = (int)(xorshf96()/(RAND_MAX+1.0)); // fill = 0; fill = 1; // fill = (int)(rand()%2); rect(x1,y1,x2,y2,fill,random_color); } } // ************************************************* // int main() { do_hw_pinctrl_data_output_enable(); create_persistent_memory_maps(); // set the lower 16 pins of bank 1 to output for(unsigned char i=0; i < 16; i++) { export_gpio_line(i+32); } // export the control lines export_gpio_line(116); export_gpio_line(126); export_gpio_line(101); export_gpio_line(80); export_gpio_line(59); export_gpio_line(56); export_gpio_line(116); // turn on the backlight system("echo 1 > /sys/class/gpio/gpio116/value"); // Initialize the display initialization(); // clear / black out the display show_single_color_on_display(GREEN); color_bars(); // clear / black out the display show_single_color_on_display(BLACK); random_rectangles(); // unmap the memory for i/o unmap_memory(); // close out /dev/mem close(devmem_handle); return 0; } // ************************************************* // static inline void *fixup_addr(void *addr, size_t size) { #ifdef FORCE_STRICT_ALIGNMENT unsigned long aligned_addr = (unsigned long)addr; aligned_addr &= ~(size - 1); addr = (void *)aligned_addr; #endif return addr; }