├── Makefile ├── README.txt ├── a.out ├── gpio.c ├── gpio.h ├── gpio.o ├── lcd ├── main.c ├── main.o ├── rpi_zero_io_pinouts.jpg ├── rtsp_test.sh ├── run.sh ├── spi.c ├── spi.h ├── spi.o ├── st7735.c ├── st7735.h ├── st7735.o ├── sudo ├── tags ├── test1.m4v ├── test2.mpg ├── video1.sh └── video2.sh /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-c -std=c99 -O3 -I ./ 3 | LDFLAGS= 4 | SOURCES=main.c st7735.c gpio.c spi.c 5 | OBJECTS=$(patsubst %.c, %.o, $(wildcard *.c)) 6 | HEADERS = $(wildcard *.h) 7 | TARGET=lcd 8 | 9 | all: $(TARGET) 10 | 11 | 12 | 13 | %.o:%.c $(HEADERS) 14 | @echo "CC $@" 15 | @$(CC) $(CFLAGS) -c $< -o $@ 16 | 17 | .PRECIOUS: $(TARGET) $(OBJECTS) 18 | 19 | $(TARGET): $(OBJECTS) 20 | @echo "LD $@" 21 | @$(CC) $(OBJECTS) -Wall $(LIBS) -o $@ 22 | 23 | 24 | clean: 25 | @echo "[RM] *.o" 26 | @-rm -f *.o 27 | @echo "[RM] $(TARGET)" 28 | @-rm -f $(TARGET) 29 | 30 | run: 31 | sudo ./lcd & 32 | sudo fbset -xres 160 -yres 120 -vxres 160 -vyres 120 -depth 16 33 | sudo pkill lcd 34 | sudo ./lcd & 35 | 36 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | This is a userspace code for mapping raspberry pi default frame buffer (/dev/fb0) to a tft with st7735 driver. CPU USAGE = 3.2 to 4.5% at 20fps. 2 | FPS can be increased by modifying the usleep(50000) value in main.c and also modifying the spi speed in st7735.c. 3 | 4 | Tested on pizero. 5 | 6 | Advantage of this application: 7 | No need of any extra kernel module/driver. 8 | No additional dependency even for gpio/spi drivers. 9 | Easy to understand. 10 | No need to create additional frame buffer. 11 | 12 | 13 | PINOUT: (change st7735.h file to change the pin configuration) 14 | #define CS_PIN 8UL 15 | #define A0_PIN 24UL 16 | #define RST_PIN 25UL 17 | 18 | It is GPIO8, GPIO24, GPIO25 and not the pin based on 0,1,2,3 etc on the board. 19 | 20 | 21 | Usage: 22 | 23 | enable spi for first time using "sudo raspi-config" 24 | 25 | Then make this project 26 | 27 | make clean 28 | 29 | make 30 | 31 | sudo ./run 32 | 33 | 34 | Now tty1 should be mapped to the LCD.. You can type on pi keyboad connected to usb/usb hub to see changes on display. 35 | 36 | You can try ./video1.sh to play test1 video sample. 37 | Similarly other examples (video2.sh, rtsp_test.sh etc) 38 | 39 | 40 | You can try typing startx to start X GUI as well. 41 | 42 | 43 | Visit http://blog.vinu.co.in for more details. 44 | 45 | mail: m a i l @ v i n u . c o . i n 46 | 47 | 48 | THANKS 49 | 50 | :) 51 | -------------------------------------------------------------------------------- /a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/a.out -------------------------------------------------------------------------------- /gpio.c: -------------------------------------------------------------------------------- 1 | /* 2 | gpio.c 3 | http://blog.vinu.co.in 4 | 5 | A very basic raspberrypi gpio driver written only for this project. 6 | So all features are not implemented. The code is written based on 7 | BCM2835-ARM-Peripherals.pdf 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see */ 21 | 22 | #include "gpio.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | volatile unsigned int *gpio = MAP_FAILED; 32 | 33 | void gpio_mode_output(unsigned pin) 34 | { 35 | int x; 36 | 37 | int p = pin; 38 | while (p > 9) { 39 | p -= 10; 40 | } 41 | 42 | gpio[GPIO_GPFSEL0 + pin / 10] |= 1UL << (p * 3); //make as output 43 | //gpio[GPIO_GPFSEL0 + pin/10] &= ~( 1 << (p*3) ); //use this to make input. 44 | } 45 | 46 | void gpio_set_pin(unsigned int pin) 47 | { 48 | gpio[GPIO_GPSET0 + pin / 32] = (1UL << ((pin - (pin / 32) * 32))); 49 | } 50 | 51 | void gpio_clear_pin(unsigned int pin) 52 | { 53 | gpio[GPIO_GPCLR0 + pin / 32] = (1UL << ((pin - (pin / 32) * 32))); 54 | } 55 | 56 | void gpio_init(void) 57 | { 58 | int memfd = -1; 59 | 60 | if ((memfd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) { 61 | fprintf(stderr, "/dev/mem open error\n"); 62 | return; 63 | } 64 | 65 | gpio = 66 | mmap(NULL, 4096, (PROT_READ | PROT_WRITE), MAP_SHARED, memfd, 67 | 0x20200000); 68 | if (gpio == MAP_FAILED) { 69 | fprintf(stderr, "mmap failed\n"); 70 | return; 71 | } 72 | printf("gpio mmap value = %x\n", gpio); 73 | 74 | // gpio = (unsigned int *)GPIO_BASE; 75 | } 76 | -------------------------------------------------------------------------------- /gpio.h: -------------------------------------------------------------------------------- 1 | #define GPIO_BASE 0x3f200000UL 2 | //#define GPIO_BASE 0x20200000UL 3 | //#define GPIO_BASE 0xf2200000UL 4 | 5 | #define GPIO_GPFSEL0 0 6 | #define GPIO_GPFSEL1 1 7 | #define GPIO_GPFSEL2 2 8 | #define GPIO_GPFSEL3 3 9 | #define GPIO_GPFSEL4 4 10 | #define GPIO_GPFSEL5 5 11 | 12 | #define GPIO_GPSET0 7 13 | #define GPIO_GPSET1 8 14 | 15 | #define GPIO_GPCLR0 10 16 | #define GPIO_GPCLR1 11 17 | 18 | #define GPIO_GPLEV0 13 19 | #define GPIO_GPLEV1 14 20 | 21 | #define GPIO_GPEDS0 16 22 | #define GPIO_GPEDS1 17 23 | 24 | #define GPIO_GPREN0 19 25 | #define GPIO_GPREN1 20 26 | 27 | #define GPIO_GPFEN0 22 28 | #define GPIO_GPFEN1 23 29 | 30 | #define GPIO_GPHEN0 25 31 | #define GPIO_GPHEN1 26 32 | 33 | #define GPIO_GPLEN0 28 34 | #define GPIO_GPLEN1 29 35 | 36 | #define GPIO_GPAREN0 31 37 | #define GPIO_GPAREN1 32 38 | 39 | #define GPIO_GPAFEN0 34 40 | #define GPIO_GPAFEN1 35 41 | 42 | #define GPIO_GPPUD 37 43 | #define GPIO_GPPUDCLK0 38 44 | #define GPIO_GPPUDCLK1 39 45 | 46 | 47 | 48 | void gpio_mode_output (unsigned int pin); 49 | void gpio_set_pin (unsigned int pin); 50 | void gpio_clear_pin (unsigned int pin); 51 | void gpio_init(void); 52 | -------------------------------------------------------------------------------- /gpio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/gpio.o -------------------------------------------------------------------------------- /lcd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/lcd -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | ////////////////////////////////////////////////////////////////////// 2 | /* 3 | * main.c 4 | * http://blog.vinu.co.in 5 | * 6 | *brief: Mapping /dev/fb0 to 160x128 16 bit TFT st7735r from user space. 7 | *Cpu usage: 4.5 to 5% APROX at 20fbs 8 | ////////////////////////////////////////////////////////////////////// 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see */ 21 | /////////////////////////////////////////////////////////////////////// 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "st7735.h" 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "spi.h" 35 | #include "gpio.h" 36 | #include 37 | 38 | 39 | //#define debug 40 | 41 | 42 | uint16_t fb2[128 * 160]; 43 | 44 | int 45 | main () 46 | { 47 | int fbfd = 0; 48 | 49 | struct fb_var_screeninfo vinfo; 50 | long int screensize = 0; 51 | uint8_t *fbp = 0; 52 | int i; 53 | uint16_t *p = fb2; 54 | int xx = 0; 55 | int yy = 0; 56 | 57 | fbfd = open ("/dev/fb0", O_RDWR); 58 | 59 | ST7735_Init (); 60 | 61 | ST7735_Orientation (1); 62 | 63 | memset (fb2, 0, sizeof (fb2)); 64 | 65 | //clear frame buffer 66 | CS_L (); 67 | ST7735_AddrSet (0, 0, 160, 128); 68 | A0_H (); 69 | for (i = 0; i < 10; i++) 70 | { 71 | spi_write (0, (char *) p, 4096); 72 | p += 4096 / 2; 73 | } 74 | 75 | 76 | while (1) 77 | { 78 | usleep (50000); 79 | 80 | ioctl (fbfd, FBIOGET_VSCREENINFO, &vinfo); 81 | #ifdef debug 82 | printf ("X = %d, Y = %d\n", vinfo.xres, vinfo.yres); 83 | #endif 84 | CS_L (); 85 | 86 | ST7735_AddrSet (0, 4, 160, 124); 87 | 88 | A0_H (); 89 | 90 | int xoffset = vinfo.xres / 160; 91 | int yoffset = vinfo.xres * (vinfo.yres / 120 - 1); 92 | #ifdef debug 93 | printf ("xoffset = %d, yoffset = %d\n", xoffset, yoffset); 94 | #endif 95 | 96 | screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; 97 | #ifdef debug 98 | printf ("screensize = %d\n", screensize); 99 | #endif 100 | const int PADDING = 4096; 101 | int mmapsize = (screensize + PADDING - 1) & ~(PADDING - 1); 102 | #ifdef debug 103 | printf ("mapsize = %d\n", mmapsize); 104 | #endif 105 | fbp = 106 | (char *) mmap (0, mmapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 107 | 0); 108 | 109 | uint16_t *p = fb2; 110 | uint16_t *x = (uint16_t *) fbp; 111 | for (xx = 0; xx < 120; xx++) 112 | { 113 | for (yy = 0; yy < 160; yy++) 114 | { 115 | *p++ = htons (*x); 116 | x += xoffset; 117 | } 118 | x += yoffset; 119 | } 120 | munmap (fbp, screensize); 121 | 122 | p = fb2; 123 | 124 | for (i = 0; i < 9; i++) 125 | { 126 | spi_write (0, (char *) p, 4096); 127 | p += 4096 / 2; 128 | } 129 | 130 | spi_write (0, (char *) p, 120 * 160 * 2 - 4096 * 9); 131 | } 132 | 133 | CS_H (); 134 | printf ("done\n"); 135 | 136 | close (fbfd); 137 | 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/main.o -------------------------------------------------------------------------------- /rpi_zero_io_pinouts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/rpi_zero_io_pinouts.jpg -------------------------------------------------------------------------------- /rtsp_test.sh: -------------------------------------------------------------------------------- 1 | sudo SDL_VIDEODRIVER=directfb SDL_FBDEV=/dev/fb0 mplayer -vo sdl -framedrop rtsp://mpv.cdn3.bigCDN.com:554/bigCDN/mp4:bigbuckbunnyiphone_400.mp4 2 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | while [ 1 ] 2 | do 3 | sleep 1 4 | sudo ./lcd 5 | done 6 | -------------------------------------------------------------------------------- /spi.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://blog.vinu.co.in 3 | spi.c 4 | A bare minimum spi code for raspberry pi. 5 | 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see */ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | static const char *spiDev0 = "/dev/spidev0.0"; 29 | static const char *spiDev1 = "/dev/spidev0.1"; 30 | static uint32_t spiSpeed; 31 | static int spiFd; 32 | static const uint8_t spiBPW = 8; 33 | static const uint16_t spiDelay = 0; 34 | 35 | int spi_write_16(int channel, unsigned char *data, int len) 36 | { 37 | struct spi_ioc_transfer spi; 38 | 39 | channel &= 1; 40 | 41 | static unsigned char bbb[4096]; 42 | 43 | memset(&spi, 0, sizeof(spi)); 44 | 45 | spi.tx_buf = (unsigned long)data; 46 | spi.rx_buf = (unsigned long)bbb; 47 | spi.len = len; 48 | spi.delay_usecs = spiDelay; 49 | spi.speed_hz = spiSpeed; 50 | spi.bits_per_word = 16; 51 | 52 | return ioctl(spiFd, SPI_IOC_MESSAGE(1), &spi); 53 | } 54 | 55 | int spi_write(int channel, unsigned char *data, int len) 56 | { 57 | struct spi_ioc_transfer spi; 58 | 59 | channel &= 1; 60 | 61 | static unsigned char bbb[4096]; 62 | 63 | memset(&spi, 0, sizeof(spi)); 64 | 65 | spi.tx_buf = (unsigned long)data; 66 | spi.rx_buf = (unsigned long)bbb; 67 | spi.len = len; 68 | spi.delay_usecs = spiDelay; 69 | spi.speed_hz = spiSpeed; 70 | spi.bits_per_word = spiBPW; 71 | 72 | return ioctl(spiFd, SPI_IOC_MESSAGE(1), &spi); 73 | } 74 | 75 | int spi_init(int channel, int speed, int mode) 76 | { 77 | int fd; 78 | 79 | mode &= 3; // Mode is 0, 1, 2 or 3 80 | channel &= 1; // Channel is 0 or 1 81 | 82 | if ((fd = open(channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0) 83 | printf("Unable to open SPI device\n"); 84 | 85 | spiSpeed = speed; 86 | spiFd = fd; 87 | 88 | if (ioctl(fd, SPI_IOC_WR_MODE, &mode) < 0) 89 | printf("SPI mode change failure\n"); 90 | 91 | if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) 92 | printf("SPI BPW change failure\n"); 93 | 94 | if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) 95 | printf("SPI SPEED change failure\n"); 96 | 97 | return fd; 98 | } 99 | -------------------------------------------------------------------------------- /spi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * wiringPiSPI.h: 3 | * Simplified SPI access routines 4 | * Copyright (c) 2012-2015 Gordon Henderson 5 | *********************************************************************** 6 | * This file is part of wiringPi: 7 | * https://projects.drogon.net/raspberry-pi/wiringpi/ 8 | * 9 | * wiringPi is free software: you can redistribute it and/or modify 10 | * it under the terms of the GNU Lesser General Public License as 11 | * published by the Free Software Foundation, either version 3 of the 12 | * License, or (at your option) any later version. 13 | * 14 | * wiringPi is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with wiringPi. 21 | * If not, see . 22 | *********************************************************************** 23 | */ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | int spi_write (int channel, unsigned char *data, int len) ; 30 | int spi_init (int channel, int speed, int mode) ; 31 | 32 | #ifdef __cplusplus 33 | } 34 | #endif 35 | -------------------------------------------------------------------------------- /spi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/spi.o -------------------------------------------------------------------------------- /st7735.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://blog.vinu.co.in 3 | basic driver for st7735r lcd. 4 | This is just to initialize the lcd and not for any rendering purpose. 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see 18 | */ 19 | 20 | #include 21 | #include "st7735.h" 22 | #include 23 | #include 24 | #include "spi.h" 25 | #include "gpio.h" 26 | 27 | uint16_t scr_width; 28 | uint16_t scr_height; 29 | 30 | void ST7735_write(uint8_t data) 31 | { 32 | spi_write(0, &data, 1); 33 | //wiringPiSPIDataW(0, &data, 1) ; 34 | } 35 | 36 | void ST7735_cmd(uint8_t cmd) 37 | { 38 | A0_L(); 39 | ST7735_write(cmd); 40 | } 41 | 42 | void ST7735_data(uint8_t data) 43 | { 44 | A0_H(); 45 | ST7735_write(data); 46 | } 47 | 48 | uint16_t RGB565(uint8_t R, uint8_t G, uint8_t B) 49 | { 50 | return ((R >> 3) << 11) | ((G >> 2) << 5) | (B >> 3); 51 | } 52 | 53 | void ST7735_Init(void) 54 | { 55 | 56 | //init spi 57 | printf("ST7735_Init...\n"); 58 | //wiringPiSetup (); 59 | gpio_init(); 60 | spi_init(0, 30000000, 0); 61 | 62 | gpio_mode_output(CS_PIN); 63 | gpio_mode_output(A0_PIN); 64 | gpio_mode_output(RST_PIN); 65 | gpio_mode_output(23); 66 | gpio_mode_output(17); 67 | 68 | //toggle test for debugging 69 | /*while(1) { 70 | // CS_H(); 71 | // CS_L(); 72 | 73 | // A0_H(); 74 | // A0_L(); 75 | 76 | RST_H(); 77 | usleep(1000); 78 | RST_L(); 79 | usleep(1000); 80 | 81 | } 82 | */ 83 | 84 | // Reset display 85 | CS_H(); 86 | RST_H(); 87 | Delay_US(5); 88 | RST_L(); 89 | Delay_US(5); 90 | RST_H(); 91 | CS_H(); 92 | Delay_US(5); 93 | CS_L(); 94 | 95 | ST7735_cmd(0x11); // Sleep out, booster on 96 | Delay_US(20); 97 | 98 | ST7735_cmd(0xb1); // In normal mode (full colors): 99 | A0_H(); 100 | ST7735_write(0x05); // RTNA set 1-line period: RTNA2, RTNA0 101 | ST7735_write(0x3c); // Front porch: FPA5,FPA4,FPA3,FPA2 102 | ST7735_write(0x3c); // Back porch: BPA5,BPA4,BPA3,BPA2 103 | 104 | ST7735_cmd(0xb2); // In idle mode (8-colors): 105 | A0_H(); 106 | ST7735_write(0x05); // RTNB set 1-line period: RTNAB, RTNB0 107 | ST7735_write(0x3c); // Front porch: FPB5,FPB4,FPB3,FPB2 108 | ST7735_write(0x3c); // Back porch: BPB5,BPB4,BPB3,BPB2 109 | 110 | ST7735_cmd(0xb3); // In partial mode + full colors: 111 | A0_H(); 112 | ST7735_write(0x05); // RTNC set 1-line period: RTNC2, RTNC0 113 | ST7735_write(0x3c); // Front porch: FPC5,FPC4,FPC3,FPC2 114 | ST7735_write(0x3c); // Back porch: BPC5,BPC4,BPC3,BPC2 115 | ST7735_write(0x05); // RTND set 1-line period: RTND2, RTND0 116 | ST7735_write(0x3c); // Front porch: FPD5,FPD4,FPD3,FPD2 117 | ST7735_write(0x3c); // Back porch: BPD5,BPD4,BPD3,BPD2 118 | 119 | ST7735_cmd(0xB4); // Display dot inversion control: 120 | ST7735_data(0x03); // NLB,NLC 121 | 122 | ST7735_cmd(0x3a); // Interface pixel format 123 | //ST7735_data(0x03); // 12-bit/pixel RGB 4-4-4 (4k colors) 124 | ST7735_data(0x05); // 16-bit/pixel RGB 5-6-5 (65k colors) 125 | //ST7735_data(0x06); // 18-bit/pixel RGB 6-6-6 (256k colors) 126 | 127 | ST7735_cmd(0x36); // Memory data access control: 128 | // MY MX MV ML RGB MH - - 129 | //ST7735_data(0x08 + 2+4); // Normal: Top to Bottom; Left to Right; RGB 130 | ST7735_data(0x8); // Y-Mirror: Bottom to top; Left to Right; RGB 131 | //ST7735_data(0x40); // X-Mirror: Top to Bottom; Right to Left; RGB 132 | //ST7735_data(0xc0); // X-Mirror,Y-Mirror: Bottom to top; Right to left; RGB 133 | //ST7735_data(0x20); // X-Y Exchange: X and Y changed positions 134 | //ST7735_data(0xA0); // X-Y Exchange,Y-Mirror 135 | //ST7735_data(0x60); // X-Y Exchange,X-Mirror 136 | //ST7735_data(0xE0); // X-Y Exchange,X-Mirror,Y-Mirror 137 | 138 | ST7735_cmd(0x20); // Display inversion off 139 | //ST7735_cmd(0x21); // Display inversion on 140 | 141 | ST7735_cmd(0x13); // Partial mode off 142 | 143 | ST7735_cmd(0x26); // Gamma curve set: 144 | //ST7735_data(0x01); // Gamma curve 1 (G2.2) or (G1.0) 145 | //ST7735_data(0x02); // Gamma curve 2 (G1.8) or (G2.5) 146 | ST7735_data(0x04); // Gamma curve 3 (G2.5) or (G2.2) 147 | //ST7735_data(0x08); // Gamma curve 4 (G1.0) or (G1.8) 148 | 149 | ST7735_cmd(0x38); // Display on 150 | ST7735_cmd(0x29); // Display on 151 | 152 | CS_H(); 153 | 154 | ST7735_Orientation(scr_normal); 155 | printf("ST7735_Init done!\n"); 156 | } 157 | 158 | void ST7735_Orientation(uint8_t orientation) 159 | { 160 | CS_L(); 161 | ST7735_cmd(0x36); // Memory data access control: 162 | switch (orientation) { 163 | case scr_CW: 164 | scr_width = scr_h; 165 | scr_height = scr_w; 166 | ST7735_data(0xa0); // X-Y Exchange,Y-Mirror 167 | break; 168 | case scr_CCW: 169 | scr_width = scr_h; 170 | scr_height = scr_w; 171 | ST7735_data(0x60); // X-Y Exchange,X-Mirror 172 | break; 173 | case scr_180: 174 | scr_width = scr_w; 175 | scr_height = scr_h; 176 | ST7735_data(0xc0); // X-Mirror,Y-Mirror: Bottom to top; Right to left; RGB 177 | break; 178 | default: 179 | scr_width = scr_w; 180 | scr_height = scr_h; 181 | ST7735_data(0x00); // Normal: Top to Bottom; Left to Right; RGB 182 | break; 183 | } 184 | CS_H(); 185 | } 186 | 187 | void ST7735_AddrSet(uint16_t XS, uint16_t YS, uint16_t XE, uint16_t YE) 188 | { 189 | ST7735_cmd(0x2a); // Column address set 190 | A0_H(); 191 | ST7735_write(XS >> 8); 192 | ST7735_write(XS); 193 | ST7735_write(XE >> 8); 194 | ST7735_write(XE); 195 | 196 | ST7735_cmd(0x2b); // Row address set 197 | A0_H(); 198 | ST7735_write(YS >> 8); 199 | ST7735_write(YS); 200 | ST7735_write(YE >> 8); 201 | ST7735_write(YE); 202 | 203 | ST7735_cmd(0x2c); // Memory write 204 | } 205 | 206 | void ST7735_Clear(uint16_t color) 207 | { 208 | uint16_t i; 209 | uint8_t CH, CL; 210 | 211 | CH = color >> 8; 212 | CL = (uint8_t) color; 213 | 214 | CS_L(); 215 | ST7735_AddrSet(0, 0, scr_width - 1, scr_height - 1); 216 | A0_H(); 217 | for (i = 0; i < scr_width * scr_height; i++) { 218 | ST7735_write(CH); 219 | ST7735_write(CL); 220 | } 221 | CS_H(); 222 | } 223 | -------------------------------------------------------------------------------- /st7735.h: -------------------------------------------------------------------------------- 1 | #ifndef __ST7735_H__ 2 | #define __ST7735_H__ 3 | #include 4 | #include 5 | #include 6 | #include "spi.h" 7 | 8 | #define CS_PIN 8UL 9 | #define A0_PIN 24UL 10 | #define RST_PIN 25UL 11 | 12 | // Screen resolution in normal orientation 13 | #define scr_w 160 14 | #define scr_h 128 15 | 16 | // CS pin macros 17 | #define CS_L() gpio_clear_pin(CS_PIN) 18 | #define CS_H() gpio_set_pin(CS_PIN) 19 | 20 | // A0 pin macros 21 | #define A0_L() gpio_clear_pin(A0_PIN) 22 | #define A0_H() gpio_set_pin(A0_PIN) 23 | 24 | // RESET pin macros 25 | #define RST_L() gpio_clear_pin(RST_PIN) 26 | #define RST_H() gpio_set_pin(RST_PIN) 27 | 28 | 29 | #define Delay_US(x) usleep(x) 30 | // Colors for spaces between symbols for debug view 31 | //#define V_SEP COLOR565_YELLOW 32 | //#define H_SEP COLOR565_SIENNA 33 | //#define V_SEP COLOR565_WHITE 34 | //#define H_SEP COLOR565_WHITE 35 | 36 | typedef enum 37 | { 38 | scr_normal = 0, 39 | scr_CW = 1, 40 | scr_CCW = 2, 41 | scr_180 = 3 42 | } ScrOrientation_TypeDef; 43 | 44 | extern uint16_t scr_width; 45 | extern uint16_t scr_height; 46 | 47 | uint16_t RGB565 (uint8_t R, uint8_t G, uint8_t B); 48 | void ST7735_write (uint8_t data); 49 | void ST7735_Init (void); 50 | void ST7735_AddrSet (uint16_t XS, uint16_t YS, uint16_t XE, uint16_t YE); 51 | void ST7735_Orientation (uint8_t orientation); 52 | void ST7735_Clear (uint16_t color); 53 | extern void Delay_US (volatile uint32_t nTime); 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /st7735.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/st7735.o -------------------------------------------------------------------------------- /sudo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/sudo -------------------------------------------------------------------------------- /tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | A0_H st7735.h 22;" d 8 | A0_L st7735.h 21;" d 9 | A0_PIN st7735.h 9;" d 10 | CC Makefile /^CC=gcc$/;" m 11 | CFLAGS Makefile /^CFLAGS=-c -std=c99 -O3 -I .\/$/;" m 12 | CS_H st7735.h 18;" d 13 | CS_L st7735.h 17;" d 14 | CS_PIN st7735.h 8;" d 15 | Delay_US st7735.h 29;" d 16 | GPIO_BASE gpio.h 1;" d 17 | GPIO_GPAFEN0 gpio.h 39;" d 18 | GPIO_GPAFEN1 gpio.h 40;" d 19 | GPIO_GPAREN0 gpio.h 36;" d 20 | GPIO_GPAREN1 gpio.h 37;" d 21 | GPIO_GPCLR0 gpio.h 15;" d 22 | GPIO_GPCLR1 gpio.h 16;" d 23 | GPIO_GPEDS0 gpio.h 21;" d 24 | GPIO_GPEDS1 gpio.h 22;" d 25 | GPIO_GPFEN0 gpio.h 27;" d 26 | GPIO_GPFEN1 gpio.h 28;" d 27 | GPIO_GPFSEL0 gpio.h 5;" d 28 | GPIO_GPFSEL1 gpio.h 6;" d 29 | GPIO_GPFSEL2 gpio.h 7;" d 30 | GPIO_GPFSEL3 gpio.h 8;" d 31 | GPIO_GPFSEL4 gpio.h 9;" d 32 | GPIO_GPFSEL5 gpio.h 10;" d 33 | GPIO_GPHEN0 gpio.h 30;" d 34 | GPIO_GPHEN1 gpio.h 31;" d 35 | GPIO_GPLEN0 gpio.h 33;" d 36 | GPIO_GPLEN1 gpio.h 34;" d 37 | GPIO_GPLEV0 gpio.h 18;" d 38 | GPIO_GPLEV1 gpio.h 19;" d 39 | GPIO_GPPUD gpio.h 42;" d 40 | GPIO_GPPUDCLK0 gpio.h 43;" d 41 | GPIO_GPPUDCLK1 gpio.h 44;" d 42 | GPIO_GPREN0 gpio.h 24;" d 43 | GPIO_GPREN1 gpio.h 25;" d 44 | GPIO_GPSET0 gpio.h 12;" d 45 | GPIO_GPSET1 gpio.h 13;" d 46 | HEADERS Makefile /^HEADERS = $(wildcard *.h)$/;" m 47 | LDFLAGS Makefile /^LDFLAGS=$/;" m 48 | OBJECTS Makefile /^OBJECTS=$(patsubst %.c, %.o, $(wildcard *.c))$/;" m 49 | RGB565 st7735.c /^uint16_t RGB565(uint8_t R, uint8_t G, uint8_t B)$/;" f 50 | RST_H st7735.h 26;" d 51 | RST_L st7735.h 25;" d 52 | RST_PIN st7735.h 10;" d 53 | SOURCES Makefile /^SOURCES=main.c st7735.c gpio.c spi.c$/;" m 54 | ST7735_AddrSet st7735.c /^void ST7735_AddrSet(uint16_t XS, uint16_t YS, uint16_t XE, uint16_t YE)$/;" f 55 | ST7735_Clear st7735.c /^void ST7735_Clear(uint16_t color)$/;" f 56 | ST7735_Init st7735.c /^void ST7735_Init(void)$/;" f 57 | ST7735_Orientation st7735.c /^void ST7735_Orientation(uint8_t orientation)$/;" f 58 | ST7735_cmd st7735.c /^void ST7735_cmd(uint8_t cmd)$/;" f 59 | ST7735_data st7735.c /^void ST7735_data(uint8_t data)$/;" f 60 | ST7735_write st7735.c /^void ST7735_write(uint8_t data)$/;" f 61 | ScrOrientation_TypeDef st7735.h /^} ScrOrientation_TypeDef;$/;" t typeref:enum:__anon1 62 | TARGET Makefile /^TARGET=lcd$/;" m 63 | __ST7735_H__ st7735.h 2;" d 64 | gpio gpio.c /^volatile unsigned int *gpio = MAP_FAILED;$/;" v 65 | gpio_clear_pin gpio.c /^void gpio_clear_pin(int pin)$/;" f 66 | gpio_init gpio.c /^void gpio_init(void)$/;" f 67 | gpio_mode_output gpio.c /^void gpio_mode_output(int pin)$/;" f 68 | gpio_set_pin gpio.c /^void gpio_set_pin(int pin)$/;" f 69 | main main.c /^int main()$/;" f 70 | scr_180 st7735.h /^ scr_180 = 3$/;" e enum:__anon1 71 | scr_CCW st7735.h /^ scr_CCW = 2,$/;" e enum:__anon1 72 | scr_CW st7735.h /^ scr_CW = 1,$/;" e enum:__anon1 73 | scr_h st7735.h 14;" d 74 | scr_height st7735.c /^uint16_t scr_height;$/;" v 75 | scr_normal st7735.h /^ scr_normal = 0,$/;" e enum:__anon1 76 | scr_w st7735.h 13;" d 77 | scr_width st7735.c /^uint16_t scr_width;$/;" v 78 | spiBPW spi.c /^static const uint8_t spiBPW = 8;$/;" v file: 79 | spiDelay spi.c /^static const uint16_t spiDelay = 0;$/;" v file: 80 | spiDev0 spi.c /^static const char *spiDev0 = "\/dev\/spidev0.0";$/;" v file: 81 | spiDev1 spi.c /^static const char *spiDev1 = "\/dev\/spidev0.1";$/;" v file: 82 | spiFd spi.c /^static int spiFd;$/;" v file: 83 | spiSpeed spi.c /^static uint32_t spiSpeed;$/;" v file: 84 | spi_init spi.c /^int spi_init(int channel, int speed, int mode)$/;" f 85 | spi_write spi.c /^int spi_write(int channel, unsigned char *data, int len)$/;" f 86 | spi_write_16 spi.c /^int spi_write_16(int channel, unsigned char *data, int len)$/;" f 87 | -------------------------------------------------------------------------------- /test1.m4v: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/test1.m4v -------------------------------------------------------------------------------- /test2.mpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinodstanur/raspberry-pi-frame-buffer-mapping-to-160x128-ST7735R-LCD/a5031144e9f60902145473d3a0d3d19d304857c7/test2.mpg -------------------------------------------------------------------------------- /video1.sh: -------------------------------------------------------------------------------- 1 | sudo SDL_VIDEODRIVER=directfb SDL_FBDEV=/dev/fb0 mplayer -vo sdl -framedrop test1.m4v 2 | -------------------------------------------------------------------------------- /video2.sh: -------------------------------------------------------------------------------- 1 | sudo SDL_VIDEODRIVER=directfb SDL_FBDEV=/dev/fb0 mplayer -vo sdl -framedrop test2.mpg 2 | --------------------------------------------------------------------------------