My issue is that I cannot get the ST7735R library to work on the RPI. I know that there is a kernel module for it, but I just want to use the Adafruit library because I have a lot of stuff that uses the GFX library which makes porting my code to this wonderful TFT screen much easier. This is what I have do so far:
I have connected the TFT screen to the RPI as follows (as described at thttp://www.whence.com/rpi/):
5V Power LITE
GPIO 11 (SCLK) SCK
GPIO 10 (MOSI) MOSI
GPIO 8 (CE0) TFT_CS
GPIO 24 D/C
GPIO 25 RESET
5V Power VCC
Ground Gnd
These pins are connected correctly since my library resets the TFT screen. The CE0 and D/C pin are also connected correctly I guess because nothing is happening when I disconnect them .
I am trying to get this to work based on the Wiring PI library because basic SPI is supported (wrapped) by it. I only get "snow" on my TFT after the init. I guess that means that something is send to the TFT screen, but obviously its not correct. I have put a lot of debug stuff in my code and I cannot see what is going wrong. I really think that I am sending the correct commands (as described in the command table).
Here is the usage and the SPI/constructor/init code of the library that I have right now. I truly hope to get some help here:
Usage:
Code: Select all
#define TFT_SPI_CHANNEL 0
#define TFT_SPI_PIN_DATA_COMMAND 5
#define TFT_SPI_PIN_RESET 6
#define TFT_SPI_SPEED 4000000
// TFT Object
Adafruit_ST7735 *display2 = new Adafruit_ST7735(TFT_SPI_CHANNEL, TFT_SPI_PIN_DATA_COMMAND, TFT_SPI_PIN_RESET, TFT_SPI_SPEED);
// If your TFT's plastic wrap has a Red Tab, use the following:
display2->initR(INITR_REDTAB); // initialize a ST7735R chip, red tab
Code: Select all
Adafruit_ST7735::Adafruit_ST7735(uint8_t channel, uint8_t rs, uint8_t rst, int speed) {
_channel = channel; // SPI channel GPIO8 is channel 0
_rs = rs; // Data Command GPIO24
_rst = rst; // Reset GPIO25
_speed = speed;
}
void Adafruit_ST7735::writecommand(uint8_t c) {
uint8_t data = c;
digitalWrite(_rs, LOW);
if( wiringPiSPIDataRW (_channel, &data, 1) < 0){
printf("Adafruit_ST7735::writecommand error %s\n", strerror(errno) );
}
}
void Adafruit_ST7735::writedata(unsigned char *data, int len) {
digitalWrite(_rs, HIGH);
if( wiringPiSPIDataRW (_channel, data, len) < 0){
printf("Adafruit_ST7735::writedata error %s\n", strerror(errno) );
}
}
// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in PROGMEM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.
#define DELAY 0x80
static unsigned char
Bcmd[] = { // Initialization commands for 7735B screens
18, // 18 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
0x05, // 16-bit color
10, // 10 ms delay
ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
0x08, // Row addr/col addr, bottom to top refresh
ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
0x21, 0x1B, 0x13, 0x19, // these config values represent)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (ditto)
0x22, 0x1D, 0x18, 0x1E,
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 18: Main screen turn on, no args, w/delay
255 }, // 255 = 500 ms delay
Rcmd1[] = { // Init for 7735R, part 1 (red or green tab)
15, // 15 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3 , // 3: Frame rate ctrl - normal mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3 , // 4: Frame rate control - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6 , // 5: Frame rate ctrl - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR , 1 , // 6: Display inversion ctrl, 1 arg, no delay:
0x07, // No inversion
ST7735_PWCTR1 , 3 , // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2 , 1 , // 8: Power control, 1 arg, no delay:
0xC5, // VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
ST7735_PWCTR3 , 2 , // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4 , 2 , // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2, Opamp current small & Medium low
0x2A,
ST7735_PWCTR5 , 2 , // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1 , 1 , // 12: Power control, 1 arg, no delay:
0x0E,
ST7735_INVOFF , 0 , // 13: Don't invert display, no args, no delay
ST7735_MADCTL , 1 , // 14: Memory access control (directions), 1 arg:
0xC8, // row addr/col addr, bottom to top refresh
ST7735_COLMOD , 1 , // 15: set color mode, 1 arg, no delay:
0x05 }, // 16-bit color
Rcmd2green[] = { // Init for 7735R, part 2 (green tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01 }, // XEND = 159
Rcmd2red[] = { // Init for 7735R, part 2 (red tab only)
2, // 2 commands in list:
ST7735_CASET , 4 , // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST7735_RASET , 4 , // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
Rcmd3[] = { // Init for 7735R, part 3 (red or green tab)
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Magical unicorn dust, 16 args, no delay:
0x02, 0x1c, 0x07, 0x12,
0x37, 0x32, 0x29, 0x2d,
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Sparkles and rainbows, 16 args, no delay:
0x03, 0x1d, 0x07, 0x06,
0x2E, 0x2C, 0x29, 0x2D,
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST7735_NORON , DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST7735_DISPON , DELAY, // 4: Main screen turn on, no args w/delay
100 }; // 100 ms delay
// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void Adafruit_ST7735::commandList(uint8_t *addr) {
uint8_t numCommands, numArgs;
uint16_t ms;
//printf("START---------------------------------------------------------------------------------\n");
numCommands = *addr++; // Number of commands to follow
//printf("Adafruit_ST7735::commandList number of commands: %d\n", numCommands);
while(numCommands--) { // For each command...
writecommand(*addr++); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & DELAY; // If hibit set, delay follows args
numArgs &= ~DELAY; // Mask out delay bit
if(numArgs > 0){
//printf("Adafruit_ST7735::commandList numArgs %d\n", numArgs);
memcpy(spi_buf, addr, numArgs);
writedata(spi_buf, numArgs);
}
addr += numArgs;
if(ms) {
ms = *addr++; // Read post-command delay time (ms)
if(ms == 255) ms = 500; // If 255, delay for 500 ms
delay(ms);
}
}
//printf("END---------------------------------------------------------------------------------\n\n\n\n");
}
// Initialization code common to both 'B' and 'R' type displays
void Adafruit_ST7735::commonInit(uint8_t *cmdList) {
constructor(ST7735_TFTWIDTH, ST7735_TFTHEIGHT);
colstart = rowstart = 0; // May be overridden in init func
pinMode(_rs, OUTPUT);
//Hardcoded for now in: wiringPi/wiringPiSPI.c
//spiMode = 0 ;
// SPI Bits per Word
//spiBPW = 8;
// 0 is SPI0_CE0_N which is GPIO8 (first chip select line) => Channel 0
if (wiringPiSPISetup (_channel, _speed) < 0){
// Handle error
printf("Adafruit_ST7735::commonInit Error setting up SPI\n");
}
// toggle RST low to reset; CS low so it'll listen to us
if (_rst) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(500);
digitalWrite(_rst, LOW);
delay(500);
digitalWrite(_rst, HIGH);
delay(500);
}
if(cmdList) commandList(cmdList);
}
// Initialization for ST7735B screens
void Adafruit_ST7735::initB(void) {
commonInit(Bcmd);
}
// Initialization for ST7735R screens (green or red tabs)
void Adafruit_ST7735::initR(uint8_t options) {
commonInit(Rcmd1);
if(options == INITR_GREENTAB) {
commandList(Rcmd2green);
colstart = 2;
rowstart = 1;
} else {
// colstart, rowstart left at default '0' values
commandList(Rcmd2red);
}
commandList(Rcmd3);
}