| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- #include "lcd.h"
- #include "spi.h"
- #include "FreeRTOS.h"
- #define DIV_NUM 10
- #define INVERSE 1
- #define ST_OFFSET 80
- #define BIT(x) (1<<x)
- #define ST77XX_CASET 0x2A
- #define ST77XX_RASET 0x2B
- #define ST77XX_RAMWR 0x2C
- #define ST77XX_RAMRD 0x2E
- #define ST77XX_MADCTL 0x36
- #define ST77XX_PIXEL_FORMAT 0x3A
- #define ST77XX_MADCTL_MH BIT(2)
- #define ST77XX_MADCTL_ML BIT(4)
- #define ST77XX_MADCTL_MV BIT(5)
- #define ST77XX_MADCTL_MX BIT(6)
- #define ST77XX_MADCTL_MY BIT(7)
- #define ST77XX_MADCTL_RGB 0x00
- #define ST77XX_MADCTL_BGR BIT(3)
- //#define delay_ms(x) rt_thread_mdelay(x)
- #define TFT_PORT GPIOD
- #define TFT_RST_PIN GPIO_PIN_9
- #define TFT_DC_PIN GPIO_PIN_8
- #define TFT_BG_PIN GPIO_PIN_10
- #define TFT_SCL_PIN GPIO_PIN_13
- #define TFT_MOSI_PIN GPIO_PIN_15
- #define TFT_CS_PIN GPIO_PIN_12
- #define LCD_SCLK_Clr() gpio_bit_reset(GPIOB,TFT_SCL_PIN)//SCL=SCLK
- #define LCD_SCLK_Set() gpio_bit_set(GPIOB,TFT_SCL_PIN)
- #define LCD_MOSI_Clr() gpio_bit_reset(GPIOB,TFT_MOSI_PIN)//SDA=MOSI
- #define LCD_MOSI_Set() gpio_bit_set(GPIOB,TFT_MOSI_PIN)
- #define LCD_RES_Clr() gpio_bit_reset(TFT_PORT,TFT_RST_PIN)//RES
- #define LCD_RES_Set() gpio_bit_set(TFT_PORT,TFT_RST_PIN)
- #define LCD_DC_Clr() gpio_bit_reset(TFT_PORT,TFT_DC_PIN)//DC
- #define LCD_DC_Set() gpio_bit_set(TFT_PORT,TFT_DC_PIN)
-
- #define LCD_CS_Clr() gpio_bit_reset(GPIOB,TFT_CS_PIN)//CS
- #define LCD_CS_Set() gpio_bit_set(GPIOB,TFT_CS_PIN)
- #define LCD_BLK_Clr() gpio_bit_reset(TFT_PORT,TFT_BG_PIN)//BLK
- #define LCD_BLK_Set() gpio_bit_set(TFT_PORT,TFT_BG_PIN)
- #define SPI_SOFT 0
- #define SPI_HARD 1
- #define SPI_HARD_DMA 2
- #define USE_SPI_TYPE SPI_HARD_DMA
- void st77xx_fill(lcd_handle_t *h, uint32_t color);
- static void spi1_byte_write(uint8_t data)
- {
-
- int i;
- LCD_CS_Clr();
- for(i=0;i<8;i++)
- {
- LCD_SCLK_Clr();
- if(data&0x80)
- {
- LCD_MOSI_Set();
- }
- else
- {
- LCD_MOSI_Clr();
- }
- LCD_SCLK_Set();
- data<<=1;
- }
- LCD_CS_Set();
- //LCD_SCLK_Set();
- }
- static void spi1_byte_hard_write(uint8_t data,uint16_t timeout)
- {
- uint16_t _time = timeout;
- LCD_CS_Clr();
- while(RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_TBE))
- {
- if(timeout)
- {
- timeout --;
- }
- else
- break;
- }
-
- spi_i2s_data_transmit(SPI1,data);
-
- while(RESET == spi_i2s_flag_get(SPI1,SPI_FLAG_RBNE))
- {
- if(_time)
- {
- _time --;
- }
- else
- break;
- }
- spi_i2s_data_receive(SPI1);
- LCD_CS_Set();
- }
- static spi1_dma_data_write(uint16_t *data,uint32_t lenth)
- {
- //LCD_DC_Set();
- LCD_CS_Clr();
- dma_channel_disable(DMA0,DMA_CH4);
- dma_memory_address_config(DMA0,DMA_CH4,(uint32_t)(data));
- dma_transfer_number_config(DMA0,DMA_CH4,lenth);
- dma_channel_enable(DMA0,DMA_CH4);
- spi_dma_enable(SPI1, SPI_DMA_TRANSMIT);
- while(!dma_flag_get(DMA0, DMA_CH4, DMA_FLAG_FTF));
- dma_flag_clear(DMA0, DMA_CH4, DMA_FLAG_FTF);
- LCD_CS_Set();
- }
- static void write_cmd(lcd_handle_t *h, uint8_t reg)
- {
- LCD_DC_Clr();
- #if (USE_SPI_TYPE == SPI_SOFT)
- spi1_byte_write(reg);
- #elif (USE_SPI_TYPE == SPI_HARD)
- spi1_byte_hard_write(reg,200);
- #else
- spi1_dma_data_write(®,1);
- #endif
- LCD_DC_Set();
- }
- static void write_data(lcd_handle_t *h, uint8_t data)
- {
- #if (USE_SPI_TYPE == SPI_SOFT)
- spi1_byte_write(data);
- #elif (USE_SPI_TYPE == SPI_HARD)
- spi1_byte_hard_write(data,200);
- #else
- spi1_dma_data_write(&data,1);
- #endif
-
- }
- static void write_cmd_data(lcd_handle_t *h, uint8_t reg, uint16_t data)
- {
- write_cmd (h,reg);
- write_data(h,data);
- }
- static void read_data(lcd_handle_t *h, uint8_t *data, int length)
- {
- }
- static void write_16bit(lcd_handle_t *h, uint16_t data)
- {
-
-
- #if (USE_SPI_TYPE == SPI_SOFT)
- spi1_byte_write(data >> 8);
- spi1_byte_write(data);
-
- #elif (USE_SPI_TYPE == SPI_HARD)
- spi1_byte_hard_write(data>>8,200);
- spi1_byte_hard_write(data,200);
- #else
- spi1_dma_data_write(&data,2);
- #endif
- }
- void st77xx_rotate(lcd_handle_t *h, int rotate)
- {
- uint8_t madctl=0,mode=INVERSE?ST77XX_MADCTL_BGR:ST77XX_MADCTL_RGB;
-
- switch (rotate)
- {
- case 0:
- madctl = mode;
- break;
-
- case 90:
- madctl = (ST77XX_MADCTL_MV | ST77XX_MADCTL_MX | mode);
- break;
-
- case 180:
- madctl = (ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | mode);
- break;
-
- case 270:
- madctl = (ST77XX_MADCTL_MV | ST77XX_MADCTL_MY | mode);
- break;
-
- default:
- break;
- }
- write_cmd_data(h, ST77XX_MADCTL, madctl);
- }
- void st77xx_set_win(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
- {
- int x1=x,y1=y;
- int x2=x+w-1,y2=y+h-1;
-
- if(hd->rotate == 0)
- {
-
- }else if(hd->rotate == 90)
- {
- }else if(hd->rotate == 180)
- {
- y1+=80;
- y2+=80;
- }else
- {
- x1+=80;
- x2+=80;
- }
-
- write_cmd (hd,ST77XX_CASET);
- write_data(hd,x1>>8);
- write_data(hd,x1);
- write_data(hd,x2>>8);
- write_data(hd,x2);
- write_cmd (hd,ST77XX_RASET);
- write_data(hd,y1>>8);
- write_data(hd,y1);
- write_data(hd,y2>>8);
- write_data(hd,y2);
- write_cmd (hd,ST77XX_RAMWR);
- }
- void st77xx_refresh(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, void *data, uint32_t len)
- {
- if(x>=hd->width || y>=hd->height) {
- return;
- }
- st77xx_set_win(hd, x, y, w, h);
- #if (USE_SPI_TYPE == SPI_SOFT)
- for(int i = 0;i < len;i++)
- {
- spi1_byte_write(*(uint8_t*)(&data[i]));
- }
- #elif (USE_SPI_TYPE == SPI_HARD)
- for(int i = 0;i < len;i++)
- spi1_byte_hard_write(*(uint8_t*)(&data[i]),200);
- #else
- spi1_dma_data_write(data,len);
- #endif
-
- }
- void st77xx_draw_pixel(lcd_handle_t *hd, uint16_t x, uint16_t y, uint32_t color)
- {
- st77xx_set_win(hd, x, y, 1, 1);
- write_16bit(hd, color);
- }
- static void st77xx_reg_init(lcd_handle_t *h)
- {
- write_cmd (h,0x11);
- //delay_ms(150);
- vTaskDelay(120);
- write_cmd (h,0xB2); //Porch Setting
- write_data(h,0x0C);
- write_data(h,0x0C);
- write_data(h,0x00);
- write_data(h,0x33);
- write_data(h,0x33);
-
- write_cmd (h,0x35);
- write_data(h,0x00);
-
- write_cmd (h,ST77XX_MADCTL);
- write_data(h,0x00);
- write_cmd (h,ST77XX_PIXEL_FORMAT);
- write_data(h,0x05); //03=RGB444,05=RGB565,06=RGB666
-
- write_cmd (h,0xb7); //Gate Control
- write_data(h,0x35);
- write_cmd (h,0xbb); //VCOM Setting
- write_data(h,0x2d);
- write_cmd (h,0xC0); //LCM Control
- write_data(h,0x2C);
- write_cmd (h,0xC2); //VDV and VRH Command Enable
- write_data(h,0x01);
-
- write_cmd (h,0xC3); //VRH set
- write_data(h,0x15);
-
- write_cmd (h,0xC4); //VDV set
- write_data(h,0x20);
-
- write_cmd (h,0xC6); //Frame Rate Control
- write_data(h,0x0F);
-
- write_cmd (h,0xD0); //Power Control 1
- write_data(h,0xA4);
- write_data(h,0xA1);
-
- write_cmd (h,0xD6);
- write_data(h,0xA1);
-
- write_cmd (h,0xE0); /* set Positive Voltage Gamma */
- write_data(h,0x70);
- write_data(h,0x05);
- write_data(h,0x0a);
- write_data(h,0x0b);
- write_data(h,0x0a);
- write_data(h,0x27);
- write_data(h,0x2f);
- write_data(h,0x44);
- write_data(h,0x47);
- write_data(h,0x37);
- write_data(h,0x14);
- write_data(h,0x14);
- write_data(h,0x29);
- write_data(h,0x2f);
- write_cmd (h,0xE1); /* set Negative Voltage Gamma */
- write_data(h,0x70);
- write_data(h,0x07);
- write_data(h,0x0c);
- write_data(h,0x08);
- write_data(h,0x08);
- write_data(h,0x04);
- write_data(h,0x2f);
- write_data(h,0x33);
- write_data(h,0x46);
- write_data(h,0x18);
- write_data(h,0x15);
- write_data(h,0x15);
- write_data(h,0x2b);
- write_data(h,0x2d);
- write_cmd (h,0x21); // Dispaly Inversion On
- write_cmd (h,0x29); // Display On
- write_cmd (h,ST77XX_RAMWR); // Memory Write
- //vTaskDelay(50);
- }
- static void st77xx_reset(lcd_handle_t *h)
- {
- LCD_RES_Clr();
- vTaskDelay(100);
- LCD_RES_Set();
- vTaskDelay(120);
- LCD_BLK_Set();
- vTaskDelay(100);
- }
- void LCD_Fill(unsigned short xsta,unsigned short ysta,unsigned short xend,unsigned short yend,unsigned short color)
- {
- unsigned short i,j;
- //LCD_Address_Set(xsta,ysta,xend-1,yend-1);//ÉèÖÃÏÔʾ·¶Î§
-
- st77xx_set_win(NULL,xsta,ysta, xend-1, yend-1);
- for(i=ysta;i<yend;i++)
- {
- for(j=xsta;j<xend;j++)
- {
- write_16bit(NULL,color);
- }
- }
- }
- static void write_color(lcd_handle_t *h, uint16_t color, int cnt)
- {
- int i;
- for(i=0; i<cnt; i++) {
- write_16bit(h, color);
- }
- }
- static void fill_color(lcd_handle_t *h, uint16_t color, int cnt)
- {
- int i,cnt2=(cnt<=h->buflen/2)?cnt:h->buflen/2;
- for(i=0; i<cnt2; i++) {
- *(uint16_t*)(&h->buf[i]) = color;
- }
- // write_color(h,color,cnt2);
- }
- void st77xx_fill_rect(lcd_handle_t *hd, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color)
- {
- int height=hd->height/DIV_NUM;
- int i,y1=0,h1=0,times=h/height,left=h%height;
-
- times += left?1:0;
-
- fill_color(hd, color, hd->buflen/2);
- for(i=0; i<times; i++) {
- y1 = y+i*height;
- h1 = ((h1+height)>h)?(h-h1):height;
-
- st77xx_refresh(hd, x, y1, w, h1, hd->buf, w*h1*2);
- }
- }
- void st77xx_fill(lcd_handle_t *h, uint32_t color)
- {
- st77xx_fill_rect(h, 0, 0, h->width, h->height, color);
- }
- uint8_t lcd_cache[11520] = {0};
- int st77xx_init(lcd_handle_t *h)
- {
- rcu_periph_clock_enable(RCU_GPIOD);
- rcu_periph_clock_enable(RCU_GPIOB);
- #if (USE_SPI_TYPE == SPI_SOFT)
- gpio_init(TFT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, TFT_RST_PIN | TFT_DC_PIN | TFT_BG_PIN);
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, TFT_SCL_PIN | TFT_MOSI_PIN | TFT_CS_PIN);
-
- gpio_bit_set(TFT_PORT,TFT_RST_PIN | TFT_DC_PIN | TFT_BG_PIN);
- gpio_bit_set(GPIOB,TFT_SCL_PIN | TFT_MOSI_PIN | TFT_CS_PIN);
- #else
- gpio_init(TFT_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, TFT_RST_PIN | TFT_DC_PIN | TFT_BG_PIN);
- gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, TFT_SCL_PIN | TFT_MOSI_PIN | TFT_CS_PIN);
-
- gpio_bit_set(TFT_PORT,TFT_RST_PIN | TFT_DC_PIN | TFT_BG_PIN);
- gpio_bit_set(GPIOB,TFT_SCL_PIN | TFT_MOSI_PIN | TFT_CS_PIN);
- spix_init();
- #endif
-
- h->buflen = 11520;
- h->buf = &lcd_cache[0];
-
- st77xx_reset(h);
- st77xx_reg_init(h);
- st77xx_fill(h, COLOR_BLACK);
- st77xx_rotate(h, h->rotate);
-
- //LCD_Fill(0,0,240,240,0XFFFF);
- return 0;
- }
- int st77xx_deinit(lcd_handle_t *h)
- {
-
- return 0;
- }
|