Creating a project for TT_Mxx

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HX8347.cpp Source File

HX8347.cpp

00001 /**********************************************************************************************
00002  Copyright (c) 2014 DisplayModule. All rights reserved.
00003 
00004  Redistribution and use of this source code, part of this source code or any compiled binary
00005  based on this source code is permitted as long as the above copyright notice and following
00006  disclaimer is retained.
00007 
00008  DISCLAIMER:
00009  THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
00010  NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
00011  ********************************************************************************************/
00012 
00013 //Tested on NUCLEO-F401RE, LPCXpresso11U68, LPCXpresso824-MAX platform.
00014 
00015 #include "HX8347.h"
00016 
00017 
00018 
00019 #define HX8347_DEVICE_ID 0x47
00020 
00021 HX8347::HX8347(PinName cs, PinName dc, PinName mosi, PinName miso, PinName clk)
00022 :GraphicsDisplay("HX8347"), spi(mosi, miso, clk)
00023 {
00024   _cs = cs;
00025   _dc = dc;
00026 }
00027 
00028 HX8347::HX8347(PinName cs, PinName dc, PinName mosi, PinName miso, PinName clk,PinName bl)
00029 :GraphicsDisplay("HX8347"), spi(mosi, miso, clk)
00030 {
00031      _cs = cs;
00032      _dc = dc;
00033      _bl = bl;
00034 }
00035 
00036 HX8347::~HX8347() {
00037 delete _pinCS;
00038 delete _pinDC;
00039 
00040 _pinCS = NULL;
00041 _pinDC = NULL;
00042 _pinBL = NULL;
00043 }
00044 
00045 
00046 
00047 
00048 uint8_t HX8347::read8BitData(uint8_t address)
00049 {
00050   cbi(_pinDC, _bitmaskDC);
00051   spi.write(0x00);
00052   sbi(_pinDC, _bitmaskDC);
00053   return spi.write(address) & 0xff;
00054 }
00055 
00056 bool HX8347::isConnect()
00057 {
00058   if(getDeviceID() == HX8347_DEVICE_ID)
00059     return true;
00060   return false;
00061 }
00062 
00063 uint8_t HX8347::getDeviceID()
00064 {
00065   uint8_t temp;
00066   cbi(_pinCS, _bitmaskCS);
00067   temp = read8BitData(0x00);
00068   sbi(_pinCS, _bitmaskCS);
00069   return temp;
00070 }
00071 
00072 void HX8347::writeBus(uint8_t data) {
00073   spi.write(data);
00074 }
00075 
00076 void HX8347::sendCommand(uint8_t index) {
00077   cbi(_pinDC, _bitmaskDC);
00078   writeBus(index);
00079 }
00080 
00081 void HX8347::send8BitData(uint8_t data) {
00082   sbi(_pinDC, _bitmaskDC);
00083   writeBus(data);
00084 }
00085 
00086 void HX8347::sendData(uint16_t data) {
00087   uint8_t dh = data>>8;
00088   uint8_t dl = data&0xff;
00089 
00090   sbi(_pinDC, _bitmaskDC);
00091   writeBus(dh);
00092   writeBus(dl);
00093 }
00094 
00095 void HX8347::setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
00096     wr_reg(0x03, x0 );
00097     wr_reg(0x02, (x0 >> 8));
00098     wr_reg(0x05, x0+x1-1 );
00099     wr_reg(0x04, ((x0+x1-1) >> 8));
00100     wr_reg(0x07,  y0 );
00101     wr_reg(0x06, ( y0 >> 8));
00102     wr_reg(0x09, ( y0+y1-1 ));
00103     wr_reg(0x08, ( (y0+y1-1) >> 8));
00104     sendCommand(0x22);
00105 }
00106 
00107 void HX8347::wr_reg(uint8_t address,uint8_t value)
00108 {
00109   sendCommand(address);
00110   sendData(value);
00111 }
00112 
00113 int HX8347::width()
00114 {
00115     return 240;
00116 }
00117 
00118 int HX8347::height()
00119 {
00120     return 320;
00121 }
00122 
00123 void HX8347::drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint8_t size)
00124 {
00125     cbi(_pinCS, _bitmaskCS);
00126       //   for (uint8_t i =0; i<5; i++ ) {
00127       //   //uint8_t line = pgm_read_byte(font_+(c*5)+i);
00128       //   for (uint8_t j = 0; j<8; j++) {
00129       //     if (line & 0x1) {
00130       //       pixel(x+i, y+j, color);
00131       //     }
00132       //     line >>= 1;
00133       //   }
00134       // }
00135     sbi(_pinCS, _bitmaskCS);
00136 }
00137 
00138 void HX8347::drawchar(int x,int y,int value,int color)
00139 {
00140     cbi(_pinCS, _bitmaskCS);
00141     character_(x,y,value - 32,color);
00142     sbi(_pinCS, _bitmaskCS);
00143 }
00144 
00145 void HX8347::drawString(uint16_t x, uint16_t y, const char *p,int color)
00146 {
00147   //cbi(_pinCS, _bitmaskCS);
00148   while(*p!='\0')
00149   {
00150     if(x > (width() - FONT_CHAR_WIDTH)) {
00151       x = 0;
00152       y += FONT_CHAR_HEIGHT;
00153     }
00154     if(y > (height() - FONT_CHAR_HEIGHT)) {
00155       y = x = 0;
00156     }
00157     //drawChar(x, y, *p, false);
00158     drawchar(x,y,*p,color);
00159 
00160     x += FONT_CHAR_WIDTH;
00161     //drawchar(x,y,'j');
00162     p++;
00163   }
00164   //sbi(_pinCS, _bitmaskCS);
00165 }
00166 
00167 void HX8347::pixel(int x, int y, int colour)
00168 {
00169     wr_reg(0x03, (x >> 0));
00170     wr_reg(0x02, (x >> 8));
00171     wr_reg(0x07, (y >> 0));
00172     wr_reg(0x06, (y >> 8));
00173     sendCommand(0x22);
00174     sendData(colour);
00175 }
00176 
00177 void HX8347::clearScreenArea(int x,int y,int color)
00178 {
00179     int pos,t;
00180     cbi(_pinCS, _bitmaskCS);
00181     for(x ;x < width();x+= FONT_CHAR_WIDTH)
00182     {
00183        for(pos=0;pos<FONT_CHAR_HEIGHT;pos++) {
00184         for(t=0;t<FONT_CHAR_WIDTH;t++) {
00185             pixel(x + t,y + pos,color);
00186         }
00187       }
00188   }
00189     sbi(_pinCS, _bitmaskCS);
00190 }
00191 
00192 void HX8347::window(unsigned int x,unsigned int y,unsigned int w,unsigned int h)
00193 {
00194     setAddress(x,y,w,h);
00195 }
00196 
00197 void HX8347::clearScreen(uint16_t color)
00198 {
00199   cbi(_pinCS, _bitmaskCS);
00200   setAddress(0,0,width(), height());
00201   for(uint16_t i=0; i<height(); i++) {
00202     for(uint16_t j=0; j<width(); j++) {
00203         sendData(color);
00204     }
00205   }
00206   sbi(_pinCS, _bitmaskCS);
00207 }
00208 
00209 void HX8347::init(void) {
00210   //setTextColor(BLACK, WHITE);
00211   //
00212   _pinBL = new DigitalOut(_bl);
00213   _pinCS = new DigitalOut(_cs);
00214   _pinDC = new DigitalOut(_dc);
00215 
00216   sbi(_pinCS, _bitmaskCS);
00217   //light on
00218   sbi(_pinBL,_bitmaskCS);
00219   spi.format(8,0);
00220   spi.frequency(8000000); // Max SPI speed for display is 10 and for 17 for LPC15xx  
00221 
00222   cbi(_pinCS, _bitmaskCS);
00223   delay(135); // This much delay needed??
00224 
00225   // ILI9341 init
00226     wr_reg(0xEA, 0x00);                 /* Reset Power Control 1                */
00227     wr_reg(0xEB, 0x20);                 /* Power Control 2                      */
00228     wr_reg(0xEC, 0x0C);                 /* Power Control 3                      */
00229     wr_reg(0xED, 0xC4);                 /* Power Control 4                      */
00230     wr_reg(0xE8, 0x40);                 /* Source OPON_N                        */
00231     wr_reg(0xE9, 0x38);                 /* Source OPON_I                        */
00232     wr_reg(0xF1, 0x01);                 /*                                      */
00233     wr_reg(0xF2, 0x10);                 /*                                      */
00234     wr_reg(0x27, 0xA3);                 /* Display Control 2                    */
00235 
00236     /* Power On sequence ---------------------------------------------------------*/
00237     wr_reg(0x1B, 0x1B);                 /* Power Control 2                      */
00238     wr_reg(0x1A, 0x01);                 /* Power Control 1                      */
00239     wr_reg(0x24, 0x2F);                 /* Vcom Control 2                       */
00240     wr_reg(0x25, 0x57);                 /* Vcom Control 3                       */
00241     wr_reg(0x23, 0x8D);                 /* Vcom Control 1                       */
00242 
00243     /* Gamma settings  -----------------------------------------------------------*/
00244     wr_reg(0x40,0x00);   //   default setup
00245     wr_reg(0x41,0x00);   //
00246     wr_reg(0x42,0x01);   //
00247     wr_reg(0x43,0x13);   //
00248     wr_reg(0x44,0x10);   //
00249     wr_reg(0x45,0x26);   //
00250     wr_reg(0x46,0x08);   //
00251     wr_reg(0x47,0x51);   //
00252     wr_reg(0x48,0x02);   //
00253     wr_reg(0x49,0x12);   //
00254     wr_reg(0x4A,0x18);   //
00255     wr_reg(0x4B,0x19);   //
00256     wr_reg(0x4C,0x14);   //
00257     wr_reg(0x50,0x19);   //
00258     wr_reg(0x51,0x2F);   //
00259     wr_reg(0x52,0x2C);   //
00260     wr_reg(0x53,0x3E);   //
00261     wr_reg(0x54,0x3F);   //
00262     wr_reg(0x55,0x3F);   //
00263     wr_reg(0x56,0x2E);   //
00264     wr_reg(0x57,0x77);   //
00265     wr_reg(0x58,0x0B);   //
00266     wr_reg(0x59,0x06);   //
00267     wr_reg(0x5A,0x07);   //
00268     wr_reg(0x5B,0x0D);   //
00269     wr_reg(0x5C,0x1D);   //
00270     wr_reg(0x5D,0xCC);   //
00271 
00272     /* Power + Osc ---------------------------------------------------------------*/
00273     wr_reg(0x18, 0x36);                 /* OSC Control 1                        */
00274     wr_reg(0x19, 0x01);                 /* OSC Control 2                        */
00275     wr_reg(0x01, 0x00);                 /* Display Mode Control                 */
00276     wr_reg(0x1F, 0x88);                 /* Power Control 6                      */
00277     wait_ms(5);                           /* Delay 5 ms                           */
00278     wr_reg(0x1F, 0x80);                 /* Power Control 6                      */
00279     wait_ms(5);                         /* Delay 5 ms                           */
00280     wr_reg(0x1F, 0x90);                 /* Power Control 6                      */
00281     wait_ms(5);                           /* Delay 5 ms                           */
00282     wr_reg(0x1F, 0xD0);                 /* Power Control 6                      */
00283     wait_ms(5);                           /* Delay 5 ms                           */
00284 
00285     wr_reg(0x17, 0x05);                 /* Colmod 16Bit/Pixel                   */
00286 
00287     wr_reg(0x36, 0x00);                 /* Panel Characteristic                 */
00288     wr_reg(0x28, 0x38);                 /* Display Control 3                    */
00289     wait_ms(40);
00290     wr_reg(0x28, 0x3C);                 /* Display Control 3     */
00291     delay(50);
00292 
00293     /*Set Display area*/
00294     wr_reg(0x02,0x00);
00295     wr_reg(0x03,0x00); //Column Start
00296     wr_reg(0x04,0x00);
00297     wr_reg(0x05,0xEF); //Column End
00298     wr_reg(0x06,0x00);
00299     wr_reg(0x07,0x00); //Row Start
00300     wr_reg(0x08,0x01);
00301     wr_reg(0x09,0x3F); //Row End
00302 
00303     sbi(_pinCS, _bitmaskCS);
00304     clearScreen(WHITE);
00305 }
00306 
00307 
00308 
00309 
00310 /*********************************************************************************************************
00311   END FILE
00312 *********************************************************************************************************/
00313 
00314 
00315