st7735.c 28.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
/**
  ******************************************************************************
  * @file    st7735.c
  * @author  MCD Application Team
  * @brief   This file includes the driver for ST7735 LCD mounted on the Adafruit
  *          1.8" TFT LCD shield (reference ID 802).
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "st7735.h"

#define DRV_DEBUG
#define LOG_TAG             "st7735"
#include <drv_log.h>

#define IS_BOE_PANEL    (0)

/** @addtogroup BSP
  * @{
  */

/** @addtogroup Components
  * @{
  */

/** @addtogroup ST7735
  * @brief      This file provides a set of functions needed to drive the
  *             ST7735 LCD.
  * @{
  */

/** @defgroup ST7735_Private_Types Private Types
  * @{
  */
typedef struct
{
  uint32_t        Width;
  uint32_t        Height;
  uint32_t        Orientation;
} ST7735_Ctx_t;
/**
  * @}
  */

/** @defgroup ST7735_Private_Variables Private Variables
  * @{
  */
ST7735_LCD_Drv_t   ST7735_LCD_Driver =
{
  ST7735_Init,
  ST7735_DeInit,
  ST7735_ReadID,
  ST7735_DisplayOn,
  ST7735_DisplayOff,
  ST7735_SetBrightness,
  ST7735_GetBrightness,
  ST7735_SetOrientation,
  ST7735_GetOrientation,
  ST7735_SetCursor,
  ST7735_DrawBitmap,
  ST7735_FillRGBRect,
  ST7735_DrawHLine,
  ST7735_DrawVLine,
  ST7735_FillRect,
  ST7735_GetPixel,
  ST7735_SetPixel,
  ST7735_GetXSize,
  ST7735_GetYSize,
};

/* The below table handle the different values to be set to Memory Data Access Control
   depending on the orientation and pbm image writing where the data order is inverted
*/
static uint32_t OrientationTab[4][2] =
{
  {0x48U , 0xC8U}, /* Portrait orientation choice of LCD screen               */
  {0x88U , 0x08U}, /* Portrait rotated 180 orientation choice of LCD screen  */
  {0x28U , 0x68U}, /* Landscape orientation choice of LCD screen              */
  {0xE8U , 0xA8U}  /* Landscape rotated 180 orientation choice of LCD screen */
};

static ST7735_Ctx_t ST7735Ctx;
/**
  * @}
  */

/** @defgroup ST7735_Private_FunctionsPrototypes Private Functions Prototypes
  * @{
  */
static int32_t ST7735_SetDisplayWindow(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Width, uint32_t Height);
static int32_t ST7735_ReadRegWrap(void *Handle, uint8_t Reg, uint8_t* pData);
static int32_t ST7735_WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint32_t Length);
static int32_t ST7735_SendDataWrap(void *Handle, uint8_t *pData, uint32_t Length);
static int32_t ST7735_RecvDataWrap(void *Handle, uint8_t *pData, uint32_t Length);
static int32_t ST7735_IO_Delay(ST7735_Object_t *pObj, uint32_t Delay);
/**
* @}
*/

/** @addtogroup ST7735_Exported_Functions
  * @{
  */
/**
  * @brief  Register component IO bus
  * @param  pObj Component object pointer
  * @param  pIO  Component IO structure pointer
  * @retval Component status
  */
int32_t ST7735_RegisterBusIO (ST7735_Object_t *pObj, ST7735_IO_t *pIO)
{
  int32_t ret;

  if(pObj == NULL)
  {
    ret = ST7735_ERROR;
  }
  else
  {
    pObj->IO.Init      = pIO->Init;
    pObj->IO.DeInit    = pIO->DeInit;
    pObj->IO.Address   = pIO->Address;
    pObj->IO.WriteReg  = pIO->WriteReg;
    pObj->IO.ReadReg   = pIO->ReadReg;
    pObj->IO.SendData  = pIO->SendData;
    pObj->IO.RecvData  = pIO->RecvData;
    pObj->IO.GetTick   = pIO->GetTick;

    pObj->Ctx.ReadReg   = ST7735_ReadRegWrap;
    pObj->Ctx.WriteReg  = ST7735_WriteRegWrap;
    pObj->Ctx.SendData  = ST7735_SendDataWrap;
    pObj->Ctx.RecvData  = ST7735_RecvDataWrap;
    pObj->Ctx.handle    = pObj;

    if(pObj->IO.Init != NULL)
    {
      ret = pObj->IO.Init();
    }
    else
    {
      ret = ST7735_ERROR;
    }
  }

  return ret;
}

/**
  * @brief  Initialize the st7735 LCD Component.
  * @param  pObj Component object
  * @param  ColorCoding RGB mode
  * @param  Orientation Display orientation
  * @retval Component status
  */
int32_t ST7735_Init(ST7735_Object_t *pObj, uint32_t ColorCoding, uint32_t Orientation)
{
  uint8_t tmp;
  int32_t ret;

  if(pObj == NULL)
  {
    ret = ST7735_ERROR;
  }
  else
  {
		/* Out of sleep mode, 0 args, delay 120ms */
    tmp = 0x00U;
    ret = st7735_write_reg(&pObj->Ctx, ST7735_SW_RESET, &tmp, 0);
		(void)ST7735_IO_Delay(pObj, 120);

    /* Out of sleep mode, 0 args, no delay */
    tmp = 0x00U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_SLEEP_OUT, &tmp, 1);

		/* Frame rate ctrl - normal mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D)*/
    ret += st7735_write_reg(&pObj->Ctx, ST7735_FRAME_RATE_CTRL1, &tmp, 0);
    tmp = 0x01U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Frame rate control - idle mode, 3 args:Rate = fosc/(1x2+40) * (LINE+2C+2D) */
    tmp = 0x01U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_FRAME_RATE_CTRL2, &tmp, 1);
    tmp = 0x2CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Frame rate ctrl - partial mode, 6 args: Dot inversion mode, Line inversion mode */
    tmp = 0x01U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_FRAME_RATE_CTRL3, &tmp, 1);
    tmp = 0x2CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x01U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Display inversion ctrl, 1 arg, no delay: No inversion */
    tmp = 0x07U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_FRAME_INVERSION_CTRL, &tmp, 1);

    /* Power control, 3 args, no delay: -4.6V , AUTO mode */
    tmp = 0xA2U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PWR_CTRL1, &tmp, 1);
    tmp = 0x02U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x84U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Power control, 1 arg, no delay: VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD */
    tmp = 0xC5U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PWR_CTRL2, &tmp, 1);

    /* Power control, 2 args, no delay: Opamp current small, Boost frequency */
    tmp = 0x0AU;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PWR_CTRL3, &tmp, 1);
    tmp = 0x00U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Power control, 2 args, no delay: BCLK/2, Opamp current small & Medium low */
    tmp = 0x8AU;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PWR_CTRL4, &tmp, 1);
    tmp = 0x2AU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Power control, 2 args, no delay */
    tmp = 0x8AU;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PWR_CTRL5, &tmp, 1);
    tmp = 0xEEU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Power control, 1 arg, no delay */
    tmp = 0x0EU;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_VCOMH_VCOML_CTRL1, &tmp, 1);

#if IS_BOE_PANEL
    /* Not Invert display, no args, no delay */
    ret += st7735_write_reg(&pObj->Ctx, ST7735_DISPLAY_INVERSION_OFF, &tmp, 0);
#else
    /* Invert display, no args, no delay */
    ret += st7735_write_reg(&pObj->Ctx, ST7735_DISPLAY_INVERSION_ON, &tmp, 0);
#endif
    /* Set color mode, 1 arg, no delay */
    ret += st7735_write_reg(&pObj->Ctx, ST7735_COLOR_MODE, (uint8_t*)&ColorCoding, 1);

    /* Magical unicorn dust, 16 args, no delay */
    tmp = 0x02U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_PV_GAMMA_CTRL, &tmp, 1);
    tmp = 0x1CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x07U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x12U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x37U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x32U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x29U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x29U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x25U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2BU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x39U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x00U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x01U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x03U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x10U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Sparkles and rainbows, 16 args, no delay */
    tmp = 0x03U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_NV_GAMMA_CTRL, &tmp, 1);
    tmp = 0x1DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x07U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x06U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2EU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2CU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x29U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2DU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2EU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x2EU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x37U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x3FU;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x00U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x00U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x02U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
    tmp = 0x10U;
    ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

    /* Normal display on, no args, no delay */
    tmp  = 0x00U;
    ret += st7735_write_reg(&pObj->Ctx, ST7735_NORMAL_DISPLAY_OFF, &tmp, 1);

    /* Main screen turn on, no delay */
    ret += st7735_write_reg(&pObj->Ctx, ST7735_DISPLAY_ON, &tmp, 1);

    /* Set the display Orientation and the default display window */
    ret += ST7735_SetOrientation(pObj, Orientation);
  }

  if(ret != ST7735_OK)
  {
      LOG_E("error %d", ret);
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  De-Initialize the st7735 LCD Component.
  * @param  pObj Component object
  * @retval Component status
  */
int32_t ST7735_DeInit(ST7735_Object_t *pObj)
{
  (void)(pObj);

  return ST7735_OK;
}

/**
  * @brief  Get the st7735 ID.
  * @param  pObj Component object
  * @param  Id Component ID
  * @retval The component status
  */
int32_t ST7735_ReadID(ST7735_Object_t *pObj, uint32_t *Id)
{
  int32_t ret;
  uint8_t tmp[3];

  if(st7735_read_reg(&pObj->Ctx, ST7735_READ_ID1, &tmp[0]) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
  else if(st7735_read_reg(&pObj->Ctx, ST7735_READ_ID2, &tmp[1]) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
	else if(st7735_read_reg(&pObj->Ctx, ST7735_READ_ID3, &tmp[2]) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
  else
  {

    *Id = ((uint32_t)tmp[2])<<0| ((uint32_t)tmp[1])<<8 | ((uint32_t)tmp[0])<<16;
		//*Id = __rbit(*Id);
    ret = ST7735_OK;
  }

  return ret;
}

/**
  * @brief  Enables the Display.
  * @param  pObj Component object
  * @retval The component status
  */
int32_t ST7735_DisplayOn(ST7735_Object_t *pObj)
{
  int32_t ret;
  uint8_t tmp = 0;

  ret = st7735_write_reg(&pObj->Ctx, ST7735_NORMAL_DISPLAY_OFF, &tmp, 0);
  (void)ST7735_IO_Delay(pObj, 10);
  ret += st7735_write_reg(&pObj->Ctx, ST7735_DISPLAY_ON, &tmp, 0);
  (void)ST7735_IO_Delay(pObj, 10);
  ret += st7735_write_reg(&pObj->Ctx, ST7735_MADCTL, &tmp, 0);
  tmp = (uint8_t)OrientationTab[ST7735Ctx.Orientation][1];
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Disables the Display.
  * @param  pObj Component object
  * @retval The component status
  */
int32_t ST7735_DisplayOff(ST7735_Object_t *pObj)
{
  int32_t ret;
  uint8_t tmp = 0;

  ret = st7735_write_reg(&pObj->Ctx, ST7735_NORMAL_DISPLAY_OFF, &tmp, 0);
  (void)ST7735_IO_Delay(pObj, 10);
  ret += st7735_write_reg(&pObj->Ctx, ST7735_DISPLAY_OFF, &tmp, 0);
  (void)ST7735_IO_Delay(pObj, 10);
  ret += st7735_write_reg(&pObj->Ctx, ST7735_MADCTL, &tmp, 0);
  tmp = (uint8_t)OrientationTab[ST7735Ctx.Orientation][1];
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Set the display brightness.
  * @param  pObj Component object
  * @param  Brightness   display brightness to be set
  * @retval Component status
  */
int32_t ST7735_SetBrightness(ST7735_Object_t *pObj, uint32_t Brightness)
{
  (void)(pObj);
  (void)(Brightness);

  /* Feature not supported */
  return ST7735_ERROR;
}

/**
  * @brief  Get the display brightness.
  * @param  pObj Component object
  * @param  Brightness   display brightness to be returned
  * @retval Component status
  */
int32_t ST7735_GetBrightness(ST7735_Object_t *pObj, uint32_t *Brightness)
{
  (void)(pObj);
  (void)(Brightness);

  /* Feature not supported */
  return ST7735_ERROR;
}

/**
  * @brief  Set the Display Orientation.
  * @param  pObj Component object
  * @param  Orientation ST7735_ORIENTATION_PORTRAIT, ST7735_ORIENTATION_PORTRAIT_ROT180
  *                     ST7735_ORIENTATION_LANDSCAPE or ST7735_ORIENTATION_LANDSCAPE_ROT180
  * @retval The component status
  */
int32_t ST7735_SetOrientation(ST7735_Object_t *pObj, uint32_t Orientation)
{
  int32_t ret;
  uint8_t tmp;

  if((Orientation == ST7735_ORIENTATION_PORTRAIT) || (Orientation == ST7735_ORIENTATION_PORTRAIT_ROT180))
  {
    ST7735Ctx.Width  = ST7735_WIDTH;
    ST7735Ctx.Height = ST7735_HEIGHT;
  }
  else
  {
    ST7735Ctx.Width  = ST7735_HEIGHT;
    ST7735Ctx.Height = ST7735_WIDTH;
  }
	ST7735Ctx.Orientation = Orientation;

  ret = ST7735_SetDisplayWindow(pObj, 0U, 0U, ST7735Ctx.Width, ST7735Ctx.Height);

  tmp = (uint8_t)OrientationTab[Orientation][1];
  ret += st7735_write_reg(&pObj->Ctx, ST7735_MADCTL, &tmp, 1);



  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Set the Display Orientation.
  * @param  pObj Component object
  * @param  Orientation ST7735_ORIENTATION_PORTRAIT, ST7735_ORIENTATION_LANDSCAPE
  *                      or ST7735_ORIENTATION_LANDSCAPE_ROT180
  * @retval The component status
  */
int32_t ST7735_GetOrientation(ST7735_Object_t *pObj, uint32_t *Orientation)
{

  *Orientation = ST7735Ctx.Orientation;

  return ST7735_OK;
}

/**
  * @brief  Set Cursor position.
  * @param  pObj Component object
  * @param  Xpos specifies the X position.
  * @param  Ypos specifies the Y position.
  * @retval The component status
  */
int32_t ST7735_SetCursor(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos)
{
  int32_t ret;
  uint8_t tmp;

	/* Cursor calibration */
	if(ST7735Ctx.Orientation <= ST7735_ORIENTATION_PORTRAIT_ROT180)
	{
#if IS_BOE_PANEL
		Xpos += 24;
		Ypos += 0;
#else
		Xpos += 26;
		Ypos += 1;
#endif
	}
	else
	{
#if IS_BOE_PANEL
		Xpos += 0;
		Ypos += 24;
#else
		Xpos += 1;
		Ypos += 26;
#endif
	}

  ret = st7735_write_reg(&pObj->Ctx, ST7735_CASET, &tmp, 0);
  tmp = (uint8_t)(Xpos >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)(Xpos & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

  ret += st7735_write_reg(&pObj->Ctx, ST7735_RASET, &tmp, 0);
  tmp = (uint8_t)(Ypos >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)(Ypos & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  ret += st7735_write_reg(&pObj->Ctx, ST7735_WRITE_RAM, &tmp, 0);

  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Displays a bitmap picture.
  * @param  pObj Component object
  * @param  Xpos Bmp X position in the LCD
  * @param  Ypos Bmp Y position in the LCD
  * @param  pBmp Bmp picture address.
  * @retval The component status
  */
int32_t ST7735_DrawBitmap(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint8_t *pBmp)
{
  int32_t ret = ST7735_OK;
  uint32_t index, size, width, height, y_pos;
  uint8_t pixel_val[2], tmp;
  uint8_t *pbmp;
  uint32_t counter = 0;

  /* Get bitmap data address offset */
  index = (uint32_t)pBmp[10] + ((uint32_t)pBmp[11] << 8) + ((uint32_t)pBmp[12] << 16)  + ((uint32_t)pBmp[13] << 24);

  /* Read bitmap width */
  width = (uint32_t)pBmp[18] + ((uint32_t)pBmp[19] << 8) + ((uint32_t)pBmp[20] << 16)  + ((uint32_t)pBmp[21] << 24);

  /* Read bitmap height */
  height = (uint32_t)pBmp[22] + ((uint32_t)pBmp[23] << 8) + ((uint32_t)pBmp[24] << 16)  + ((uint32_t)pBmp[25] << 24);

  /* Read bitmap size */
  size = (uint32_t)pBmp[2] + ((uint32_t)pBmp[3] << 8) + ((uint32_t)pBmp[4] << 16)  + ((uint32_t)pBmp[5] << 24);
  size = size - index;

  pbmp = pBmp + index;

  /* Remap Ypos, st7735 works with inverted X in case of bitmap */
  /* X = 0, cursor is on Top corner */
  y_pos = ST7735Ctx.Height - Ypos - height;

  if(ST7735_SetDisplayWindow(pObj, Xpos, y_pos, width, height) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
  else
  {
    /* Set GRAM write direction and BGR = 0 */
    tmp = (uint8_t)OrientationTab[ST7735Ctx.Orientation][0];

    if(st7735_write_reg(&pObj->Ctx, ST7735_MADCTL, &tmp, 1) != ST7735_OK)
    {
      ret = ST7735_ERROR;
    }/* Set Cursor */
    else if(ST7735_SetCursor(pObj, Xpos, y_pos) != ST7735_OK)
    {
      ret = ST7735_ERROR;
    }
    else
    {
      do
      {
        pixel_val[0] = *(pbmp + 1);
        pixel_val[1] = *(pbmp);
        if(st7735_send_data(&pObj->Ctx, pixel_val, 2U) != ST7735_OK)
        {
          ret = ST7735_ERROR;
          break;
        }
        counter +=2U;
        pbmp += 2;
      }while(counter < size);

      tmp = (uint8_t)OrientationTab[ST7735Ctx.Orientation][1];
      if(st7735_write_reg(&pObj->Ctx, ST7735_MADCTL, &tmp, 1) != ST7735_OK)
      {
        ret = ST7735_ERROR;
      }
      else
      {
        if(ST7735_SetDisplayWindow(pObj, 0U, 0U, ST7735Ctx.Width, ST7735Ctx.Height) != ST7735_OK)
        {
          ret = ST7735_ERROR;
        }
      }
    }
  }

  return ret;
}

/**
  * @brief  Draws a full RGB rectangle
  * @param  pObj Component object
  * @param  Xpos   specifies the X position.
  * @param  Ypos   specifies the Y position.
  * @param  pData  pointer to RGB data
  * @param  Width  specifies the rectangle width.
  * @param  Height Specifies the rectangle height
  * @retval The component status
  */
int32_t ST7735_FillRGBRect(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint8_t *pData, uint32_t Width, uint32_t Height)
{
  int32_t ret = ST7735_OK;
  static uint8_t pdata[640];
  uint8_t *rgb_data = pData;
  uint32_t i, j;

  if(((Xpos + Width) > ST7735Ctx.Width) || ((Ypos + Height) > ST7735Ctx.Height))
  {
    ret = ST7735_ERROR;
  }/* Set Cursor */
  else
  {
    for(j = 0; j < Height; j++)
    {
      if(ST7735_SetCursor(pObj, Xpos, Ypos+j) != ST7735_OK)
      {
        ret = ST7735_ERROR;
      }
      else
      {
        for(i = 0; i < Width; i++)
        {
          pdata[2U*i] = (uint8_t)(*(rgb_data));
          pdata[(2U*i) + 1U] = (uint8_t)(*(rgb_data + 1));
          rgb_data +=2;
        }
        if(st7735_send_data(&pObj->Ctx, (uint8_t*)&pdata[0], 2U*Width) != ST7735_OK)
        {
          ret = ST7735_ERROR;
        }
      }
    }
  }

  return ret;
}

/**
  * @brief  Draw Horizontal line.
  * @param  pObj Component object
  * @param  Xpos   specifies the X position.
  * @param  Ypos   specifies the Y position.
  * @param  Length specifies the Line length.
  * @param  Color  Specifies the RGB color in RGB565 format
  * @retval The component status
  */
int32_t ST7735_DrawHLine(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Length, uint32_t Color)
{
  int32_t ret = ST7735_OK;
  uint32_t i;
  static uint8_t pdata[640];

  if((Xpos + Length) > ST7735Ctx.Width)
  {
    ret = ST7735_ERROR;
  }/* Set Cursor */
  else if(ST7735_SetCursor(pObj, Xpos, Ypos) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
  else
  {
    for(i = 0; i < Length; i++)
    {
      /* Exchange LSB and MSB to fit LCD specification */
      pdata[2U*i] = (uint8_t)(Color >> 8);
      pdata[(2U*i) + 1U] = (uint8_t)(Color);

//      pdata[(2U*i) + 1U] = (uint8_t)(Color >> 8);
//      pdata[2U*i] = (uint8_t)(Color);
    }
    if(st7735_send_data(&pObj->Ctx, (uint8_t*)&pdata[0], 2U*Length) != ST7735_OK)
    {
      ret = ST7735_ERROR;
    }
  }

  return ret;
}

/**
  * @brief  Draw vertical line.
  * @param  pObj Component object
  * @param  Color    Specifies the RGB color
  * @param  Xpos     specifies the X position.
  * @param  Ypos     specifies the Y position.
  * @param  Length   specifies the Line length.
  * @retval The component status
  */
int32_t ST7735_DrawVLine(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Length, uint32_t Color)
{
  int32_t ret = ST7735_OK;
  uint32_t counter;

  if((Ypos + Length) > ST7735Ctx.Height)
  {
    ret = ST7735_ERROR;
  }
  else
  {
    for(counter = 0; counter < Length; counter++)
    {
      if(ST7735_SetPixel(pObj, Xpos, Ypos + counter, Color) != ST7735_OK)
      {
        ret = ST7735_ERROR;
        break;
      }
    }
  }

  return ret;
}

/**
  * @brief  Fill rectangle
  * @param  pObj Component object
  * @param  Xpos X position
  * @param  Ypos Y position
  * @param  Width Rectangle width
  * @param  Height Rectangle height
  * @param  Color Draw color
  * @retval Component status
  */
int32_t ST7735_FillRect(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Width, uint32_t Height, uint32_t Color)
{
  int32_t ret = ST7735_OK;
  uint32_t i, y_pos = Ypos;

  for(i = 0; i < Height; i++)
  {
    if(ST7735_DrawHLine(pObj, Xpos, y_pos, Width, Color) != ST7735_OK)
    {
      ret = ST7735_ERROR;
      break;
    }
    y_pos++;
  }

  return ret;
}

/**
  * @brief  Write pixel.
  * @param  pObj Component object
  * @param  Xpos specifies the X position.
  * @param  Ypos specifies the Y position.
  * @param  Color the RGB pixel color in RGB565 format
  * @retval The component status
  */
int32_t ST7735_SetPixel(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Color)
{
  int32_t ret = ST7735_OK;
  uint16_t color;

  /* Exchange LSB and MSB to fit LCD specification */
  color = (uint16_t)((uint16_t)Color << 8);
  color |= (uint16_t)((uint16_t)(Color >> 8));

  if((Xpos >= ST7735Ctx.Width) || (Ypos >= ST7735Ctx.Height))
  {
    ret = ST7735_ERROR;
  }/* Set Cursor */
  else if(ST7735_SetCursor(pObj, Xpos, Ypos) != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }
  else
  {
    /* Write RAM data */
    if(st7735_send_data(&pObj->Ctx, (uint8_t*)&color, 2) != ST7735_OK)
    {
      ret = ST7735_ERROR;
    }
  }

  return ret;
}

/**
  * @brief  Read pixel.
  * @param  pObj Component object
  * @param  Xpos specifies the X position.
  * @param  Ypos specifies the Y position.
  * @param  Color the RGB pixel color in RGB565 format
  * @retval The component status
  */
int32_t ST7735_GetPixel(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t *Color)
{
  int32_t ret;
  uint8_t pixel_lsb, pixel_msb;
  uint8_t tmp;


  /* Set Cursor */
  ret = ST7735_SetCursor(pObj, Xpos, Ypos);

  /* Prepare to read LCD RAM */
  ret += st7735_read_reg(&pObj->Ctx, ST7735_READ_RAM, &tmp);   /* RAM read data command */

  /* Dummy read */
  ret += st7735_recv_data(&pObj->Ctx, &tmp, 1);

  /* Read first part of the RGB888 data */
  ret += st7735_recv_data(&pObj->Ctx, &pixel_lsb, 1);
  /* Read first part of the RGB888 data */
  ret += st7735_recv_data(&pObj->Ctx, &pixel_msb, 1);

  *Color = ((uint32_t)(pixel_lsb)) + ((uint32_t)(pixel_msb) << 8);

  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Get the LCD pixel Width.
  * @param  pObj Component object
  * @retval The Lcd Pixel Width
  */
int32_t ST7735_GetXSize(ST7735_Object_t *pObj, uint32_t *XSize)
{
  (void)pObj;

  *XSize = ST7735Ctx.Width;

  return ST7735_OK;
}

/**
  * @brief  Get the LCD pixel Height.
  * @param  pObj Component object
  * @retval The Lcd Pixel Height
  */
int32_t ST7735_GetYSize(ST7735_Object_t *pObj, uint32_t *YSize)
{
  (void)pObj;

  *YSize = ST7735Ctx.Height;

  return ST7735_OK;
}

/**
  * @}
  */

/** @defgroup ST7735_Private_Functions  Private Functions
  * @{
  */
/**
  * @brief  Sets a display window
  * @param  Xpos   specifies the X bottom left position.
  * @param  Ypos   specifies the Y bottom left position.
  * @param  Height display window height.
  * @param  Width  display window width.
  * @retval Component status
  */
static int32_t ST7735_SetDisplayWindow(ST7735_Object_t *pObj, uint32_t Xpos, uint32_t Ypos, uint32_t Width, uint32_t Height)
{
  int32_t ret;
  uint8_t tmp;

	/* Cursor calibration */
	if(ST7735Ctx.Orientation <= ST7735_ORIENTATION_PORTRAIT_ROT180)
	{
#if IS_BOE_PANEL
		Xpos += 24;
		Ypos += 0;
#else
		Xpos += 26;
		Ypos += 1;
#endif
	}
	else
	{
#if IS_BOE_PANEL
		Xpos += 0;
		Ypos += 24;
#else
		Xpos += 1;
		Ypos += 26;
#endif
	}

  /* Column addr set, 4 args, no delay: XSTART = Xpos, XEND = (Xpos + Width - 1) */
  ret = st7735_write_reg(&pObj->Ctx, ST7735_CASET, &tmp, 0);
  tmp = (uint8_t)(Xpos >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)(Xpos & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)((Xpos + Width - 1U) >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)((Xpos + Width - 1U) & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

  /* Row addr set, 4 args, no delay: YSTART = Ypos, YEND = (Ypos + Height - 1) */
  ret += st7735_write_reg(&pObj->Ctx, ST7735_RASET, &tmp, 0);
  tmp = (uint8_t)(Ypos >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)(Ypos & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)((Ypos + Height - 1U) >> 8U);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);
  tmp = (uint8_t)((Ypos + Height - 1U) & 0xFFU);
  ret += st7735_send_data(&pObj->Ctx, &tmp, 1);

  if(ret != ST7735_OK)
  {
    ret = ST7735_ERROR;
  }

  return ret;
}

/**
  * @brief  Wrap component ReadReg to Bus Read function
  * @param  Handle  Component object handle
  * @param  Reg  The target register address to write
  * @param  pData  The target register value to be written
  * @retval Component error status
  */
static int32_t ST7735_ReadRegWrap(void *Handle, uint8_t Reg, uint8_t* pData)
{
  ST7735_Object_t *pObj = (ST7735_Object_t *)Handle;

  return pObj->IO.ReadReg(Reg, pData);
}

/**
  * @brief  Wrap component WriteReg to Bus Write function
  * @param  handle  Component object handle
  * @param  Reg  The target register address to write
  * @param  pData  The target register value to be written
  * @param  Length  buffer size to be written
  * @retval Component error status
  */
static int32_t ST7735_WriteRegWrap(void *Handle, uint8_t Reg, uint8_t *pData, uint32_t Length)
{
  ST7735_Object_t *pObj = (ST7735_Object_t *)Handle;

  return pObj->IO.WriteReg(Reg, pData, Length);
}


/**
  * @brief  Wrap component SendData to Bus Write function
  * @param  handle  Component object handle
  * @param  pData  The target register value to be written
  * @retval Component error status
  */
static int32_t ST7735_SendDataWrap(void *Handle, uint8_t *pData, uint32_t Length)
{
  ST7735_Object_t *pObj = (ST7735_Object_t *)Handle;

  return pObj->IO.SendData(pData, Length);
}

/**
  * @brief  Wrap component SendData to Bus Write function
  * @param  handle  Component object handle
  * @param  pData  The target register value to be written
  * @retval Component error status
  */
static int32_t ST7735_RecvDataWrap(void *Handle, uint8_t *pData, uint32_t Length)
{
  ST7735_Object_t *pObj = (ST7735_Object_t *)Handle;

  return pObj->IO.RecvData(pData, Length);
}

/**
  * @brief  ST7735 delay
  * @param  Delay  Delay in ms
  * @retval Component error status
  */
static int32_t ST7735_IO_Delay(ST7735_Object_t *pObj, uint32_t Delay)
{
  rt_thread_mdelay(Delay);
  return ST7735_OK;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/