I've recently bought a 2.8" spi tft on ebay and tried to access it with the raspberry pi using the bcm2835 spi library.
http://www.ebay.at/itm/2-8-inch-320x240 ... 789wt_1188
unfortunately I am not able to receive the ID from the display.
Here is my code
Code: Select all
/**
*
*
* // gcc -o spi -l rt main.c -l bcm2835
* // sudo ./main
*
**/
#include <bcm2835.h>
#include <stdio.h>
//#include "GLCD.h"
//#include "TouchPanel.h"
#define MAX_X 240
#define MAX_Y 320
#define SPI_START (0x70) /* Start byte for SPI transfer */
#define SPI_RD (0x01) /* WR bit 1 within start */
#define SPI_WR (0x00) /* WR bit 0 within start */
#define SPI_DATA (0x02) /* RS bit 1 within start byte */
#define SPI_INDEX (0x00) /* RS bit 0 within start byte */
/* LCD color */
#define White 0xFFFF
#define Black 0x0000
#define Grey 0xF7DE
#define Blue 0x001F
#define Blue2 0x051F
#define Red 0xF800
#define Magenta 0xF81F
#define Green 0x07E0
#define Cyan 0x7FFF
#define Yellow 0xFFE0
#define RESET RPI_GPIO_P1_22 //GPIO25
#define BACKLIGHT RPI_GPIO_P1_12 //GPIO18
unsigned char LPC17xx_SPI_SendRecvByte (unsigned char);
void LCD_Initializtion(void);
void LCD_WriteReg( unsigned short , unsigned short);
void LCD_WriteIndex(unsigned char);
void LCD_WriteData( unsigned short);
unsigned char LPC17xx_SPI_SendRecvByte (unsigned char);
static void LCD_Configuration(void);
void LCD_SetPoint( unsigned short, unsigned short, unsigned short);
void TP_DrawPoint(unsigned int ,unsigned int);
unsigned short LCD_ReadReg( unsigned short );
unsigned short LCD_ReadData(void);
static void LCD_SetCursor( unsigned short , unsigned short );
int main(void)
{
if (!bcm2835_init())
return 1;
// Set the RESET pin to be an output
bcm2835_gpio_fsel(RESET, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(RESET, HIGH); //reset is low active
bcm2835_gpio_fsel(BACKLIGHT, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(BACKLIGHT, HIGH); //HIGH=on, LOW = off;
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // MODE1
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default BCM2835_SPI_CLOCK_DIVIDER_ 4096
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
// Send a some bytes to the slave and simultaneously read
// some bytes back from the slave
// Most SPI devices expect one or 2 bytes of command, after which they will send back
// some data. In such a case you will have the command bytes first in the buffer,
// followed by as many 0 bytes as you expect returned data bytes. After the transfer, you
// Can the read the reply bytes from the buffer.
// If you tie MISO to MOSI, you should read back what was sent.
//char buf[] = { 0x01, 0x02, 0x11, 0x33 }; // Data to send
//bcm2835_spi_transfern(buf, sizeof(buf));
// buf will now be filled with the data that was read from the slave
//printf("Read from SPI: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]);
//bcm2835_spi_end();
//bcm2835_close();
//return 0;
//TP_Init();
LCD_Initializtion();
//TouchPanel_Calibrate();
/* Infinite loop */
//while (1)
{
//getDisplayPoint(&display, Read_Ads7846(), &matrix ) ;
//TP_DrawPoint(display.x,display.y);
printf("TP_DrawPoint\n");
TP_DrawPoint(22,33);
printf("END:TP_DrawPoint\n");
//TP_DrawPoint(100,56);
//TP_DrawPoint(100,57);
//TP_DrawPoint(100,58);
//TP_DrawPoint(100,59);
//TP_DrawPoint(100,60);
}
return 0;
}
/*******************************************************************************
* Function Name : LCD_Initializtion
* Description : Initialize TFT Controller.
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_Initializtion(void)
{
unsigned short DeviceCode;
//LCD_Configuration();
DeviceCode = LCD_ReadReg(0x0000); /* Read ID */
/* Different driver IC initialization*/
if( DeviceCode == 0x9320 || DeviceCode == 0x9300 )
{
printf("DeviceCode: \n");
printf("%hu", DeviceCode);
printf("\n");
}
else
{
printf("other Code!\n");
printf("%hu", DeviceCode);
printf("\n");
}
LCD_WriteReg(0x00,0x0000);
LCD_WriteReg(0x01,0x0100); /* Driver Output Contral */
LCD_WriteReg(0x02,0x0700); /* LCD Driver Waveform Contral */
LCD_WriteReg(0x03,0x1038); /* Set the scan mode */
LCD_WriteReg(0x04,0x0000); /* Scalling Contral */
LCD_WriteReg(0x08,0x0202); /* Display Contral 2 */
LCD_WriteReg(0x09,0x0000); /* Display Contral 3 */
LCD_WriteReg(0x0a,0x0000); /* Frame Cycle Contal */
LCD_WriteReg(0x0c,(1<<0)); /* Extern Display Interface Contral 1 */
LCD_WriteReg(0x0d,0x0000); /* Frame Maker Position */
LCD_WriteReg(0x0f,0x0000); /* Extern Display Interface Contral 2 */
delay(50);
LCD_WriteReg(0x07,0x0101); /* Display Contral */
delay(50);
LCD_WriteReg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); /* Power Control 1 */
LCD_WriteReg(0x11,0x0007); /* Power Control 2 */
LCD_WriteReg(0x12,(1<<8)|(1<<4)|(0<<0)); /* Power Control 3 */
LCD_WriteReg(0x13,0x0b00); /* Power Control 4 */
LCD_WriteReg(0x29,0x0000); /* Power Control 7 */
LCD_WriteReg(0x2b,(1<<14)|(1<<4));
LCD_WriteReg(0x50,0); /* Set X Start */
LCD_WriteReg(0x51,239); /* Set X End */
LCD_WriteReg(0x52,0); /* Set Y Start */
LCD_WriteReg(0x53,319); /* Set Y End */
delay(50);
LCD_WriteReg(0x60,0x2700); /* Driver Output Control */
LCD_WriteReg(0x61,0x0001); /* Driver Output Control */
LCD_WriteReg(0x6a,0x0000); /* Vertical Srcoll Control */
LCD_WriteReg(0x80,0x0000); /* Display Position? Partial Display 1 */
LCD_WriteReg(0x81,0x0000); /* RAM Address Start? Partial Display 1 */
LCD_WriteReg(0x82,0x0000); /* RAM Address End-Partial Display 1 */
LCD_WriteReg(0x83,0x0000); /* Displsy Position? Partial Display 2 */
LCD_WriteReg(0x84,0x0000); /* RAM Address Start? Partial Display 2 */
LCD_WriteReg(0x85,0x0000); /* RAM Address End? Partial Display 2 */
LCD_WriteReg(0x90,(0<<7)|(16<<0)); /* Frame Cycle Contral */
LCD_WriteReg(0x92,0x0000); /* Panel Interface Contral 2 */
LCD_WriteReg(0x93,0x0001); /* Panel Interface Contral 3 */
LCD_WriteReg(0x95,0x0110); /* Frame Cycle Contral */
LCD_WriteReg(0x97,(0<<8));
LCD_WriteReg(0x98,0x0000); /* Frame Cycle Contral */
LCD_WriteReg(0x07,0x0133);
delay(100); /* delay 50 ms */
}
/*******************************************************************************
* Function Name : Lcd_Configuration
* Description : Configures LCD Control lines
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_Configuration(void)
{
//PINSEL_CFG_Type PinCfg;
//SSP_CFG_Type SSP_ConfigStruct;
/*
* Initialize SPI pin connect
* P0.15 - LCD_CSB - used as GPIO
* P0.16 - LCD_SCK
* P0.17 - LCD_MISO
* P0.18 - LCD_MOSI
*/
//PinCfg.Funcnum = 2;
//PinCfg.OpenDrain = 0;
//PinCfg.Pinmode = 0;
//PinCfg.Portnum = 0;
//PinCfg.Pinnum = 15;
//PINSEL_ConfigPin(&PinCfg);
//PinCfg.Pinnum = 17;
//PINSEL_ConfigPin(&PinCfg);
//PinCfg.Pinnum = 18;
//PINSEL_ConfigPin(&PinCfg);
//PinCfg.Funcnum = 0;
//PinCfg.Portnum = 0;
//PinCfg.Pinnum = 16;
//PINSEL_ConfigPin(&PinCfg);
/* P1.31 LCD_CSB is output */
//GPIO_SetDir(CSB_PORT_NUM, ( (uint32_t) 1 << CSB_PIN_NUM ), 1);
//GPIO_SetValue(CSB_PORT_NUM, ( (uint32_t) 1 << CSB_PIN_NUM ) );
/* initialize SSP configuration structure to default */
//SSP_ConfigStructInit(&SSP_ConfigStruct);
//SSP_ConfigStruct.CPHA = SSP_CPHA_SECOND;
//SSP_ConfigStruct.CPOL = SSP_CPOL_LO;
//SSP_ConfigStruct.ClockRate = 25000000;
/* Initialize SSP peripheral with parameter given in structure above */
//SSP_Init(LPC_SSP0, &SSP_ConfigStruct);
/* Enable SSP peripheral */
//SSP_Cmd(LPC_SSP0, ENABLE);
}
/*******************************************************************************
* Function Name : LCD_WriteReg
* Description : Writes to the selected LCD register.
* Input : - LCD_Reg: address of the selected register.
* - LCD_RegValue: value to write to the selected register.
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_WriteReg( unsigned short LCD_Reg, unsigned short LCD_RegValue)
{
//printf("LCD_WriteReg: ");
/* Write 16-bit Index, then Write Reg */
LCD_WriteIndex(LCD_Reg);
/* Write 16-bit Reg */
LCD_WriteData(LCD_RegValue);
//printf("end_LCD_WriteReg;\n");
}
/*******************************************************************************
* Function Name : LCD_WriteIndex
* Description : LCD write register address
* Input : - index: register address
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_WriteIndex(unsigned char index)
{
//SPI_CS_LOW;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
//bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
/* SPI write data */
// LPC17xx_SPI_SendRecvByte(SPI_START | SPI_WR | SPI_INDEX); /* Write : RS = 0, RW = 0 */
// LPC17xx_SPI_SendRecvByte(0);
// LPC17xx_SPI_SendRecvByte(index);
char tbuf[] = { SPI_START | SPI_WR | SPI_INDEX, 0, index}; // Data to send
char rbuf[] = {0,0,0};
bcm2835_spi_transfernb(tbuf, rbuf, sizeof(tbuf));
printf("SPI: WriteIndex: %02X %02X %02X \n", rbuf[0], rbuf[1], rbuf[2]);
//SPI_CS_HIGH;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
// bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH); // the default
}
/*******************************************************************************
* Function Name : LCD_WriteData
* Description : LCD write register data
* Input : - data: register data
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_WriteData( unsigned short data)
{
//SPI_CS_LOW;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
//bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
//LPC17xx_SPI_SendRecvByte(SPI_START | SPI_WR | SPI_DATA); /* Write : RS = 1, RW = 0 */
//LPC17xx_SPI_SendRecvByte((data >> 8)); /* Write D8..D15 */
//LPC17xx_SPI_SendRecvByte((data & 0xFF)); /* Write D0..D7 */
char tbuf[] = { SPI_START | SPI_WR | SPI_DATA, (data >> 8), (data & 0xFF)}; // Data to send
char rbuf[] = {0,0,0};
bcm2835_spi_transfernb(tbuf, rbuf, sizeof(tbuf));
printf("SPI: WriteData: %02X %02X %02X \n", rbuf[0], rbuf[1], rbuf[2]);
//SPI_CS_HIGH;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
// bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH); // the default
}
/*******************************************************************************
* Function Name : LPC17xx_SPI_SendRecvByte
* Description : Send one byte then recv one byte of response
* Input : - byte_s: byte_s
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
// unsigned char LPC17xx_SPI_SendRecvByte (unsigned char byte_s)
// {
// /* wait for current SSP activity complete */
// //while (SSP_GetStatus(LPC_SSP0, SSP_STAT_BUSY) == SET);
// //SSP_SendData(LPC_SSP0, (unsigned short) byte_s);
// // Send a byte to the slave and simultaneously read a byte back from the slave
// // If you tie MISO to MOSI, you should read back what was sent
// uint8_t data = bcm2835_spi_transfer(byte_s); //0x23
// printf("Read from SPI: %02X\n", data);
// //while (SSP_GetStatus(LPC_SSP0, SSP_STAT_RXFIFO_NOTEMPTY) == RESET);
// //return (SSP_ReceiveData(LPC_SSP0));
// return data;
// }
/******************************************************************************
* Function Name : LCD_SetPoint
* Description : Drawn at a specified point coordinates
* Input : - Xpos: Row Coordinate
* - Ypos: Line Coordinate
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_SetPoint( unsigned short Xpos, unsigned short Ypos, unsigned short point)
{
if( Xpos >= MAX_X || Ypos >= MAX_Y )
{
return;
}
//printf("SetPoint");
LCD_SetCursor(Xpos,Ypos);
LCD_WriteReg(0x0022,point); // (REG, VALUE)
//printf("end_SetPoint\n");
}
/*******************************************************************************
* Function Name : TP_DrawPoint
* Description : Draw point Must have a LCD driver
* Input : - Xpos: Row Coordinate
* - Ypos: Line Coordinate
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void TP_DrawPoint(unsigned int Xpos,unsigned int Ypos)
{
LCD_SetPoint(Xpos,Ypos,0xf800); /* Center point */
LCD_SetPoint(Xpos+1,Ypos,0xf800);
LCD_SetPoint(Xpos,Ypos+1,0xf800);
LCD_SetPoint(Xpos+1,Ypos+1,0xf800);
}
/*******************************************************************************
* Function Name : LCD_ReadReg
* Description : Reads the selected LCD Register.
* Input : None
* Output : None
* Return : LCD Register Value.
* Attention : None
*******************************************************************************/
unsigned short LCD_ReadReg( unsigned short LCD_Reg)
{
unsigned short LCD_RAM;
/* Write 16-bit Index (then Read Reg) */
LCD_WriteIndex(LCD_Reg);
/* Read 16-bit Reg */
LCD_RAM = LCD_ReadData();
return LCD_RAM;
}
/*******************************************************************************
* Function Name : LCD_ReadData
* Description : LCD read data
* Input : None
* Output : None
* Return : return data
* Attention : None
*******************************************************************************/
unsigned short LCD_ReadData(void)
{
unsigned short value;
//SPI_CS_LOW;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
//bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
// LPC17xx_SPI_SendRecvByte(SPI_START | SPI_RD | SPI_DATA); /* Read: RS = 1, RW = 1 */
// LPC17xx_SPI_SendRecvByte(0); /* Dummy read 1 */
// value = LPC17xx_SPI_SendRecvByte(0); /* Read D8..D15 */
// value <<= 8;
// value |= LPC17xx_SPI_SendRecvByte(0); /* Read D0..D7 */
char tbuf[] = { SPI_START | SPI_RD | SPI_DATA, 0, 0,0}; // Data to send
char rbuf[] = {0,0,0,0};
bcm2835_spi_transfernb(tbuf, rbuf, sizeof(tbuf));
value = (short int)rbuf[3] + (short int)rbuf[2]<<8;
printf("SPI: ReadData: %02X %02X %02X %02X \n", rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
//SPI_CS_HIGH;
//bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
// bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, HIGH); // the default
return value;
}
/*******************************************************************************
* Function Name : LCD_SetCursor
* Description : Sets the cursor position.
* Input : - Xpos: specifies the X position.
* - Ypos: specifies the Y position.
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
static void LCD_SetCursor( unsigned short Xpos, unsigned short Ypos )
{
/* 0x9320 */
//printf("SetCursor");
LCD_WriteReg(0x0020, Xpos );
LCD_WriteReg(0x0021, Ypos );
//printf("end_SetCursor");
}
cheers