/* * File : drv_lcd.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2009 RT-Thread Develop Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * http://www.rt-thread.org/license/LICENSE * * Change Logs: * Date Author Notes * 2018-09-13 xuzhuoyi first implementation */ #include "drv_lcd.h" #include //#define DEBUG #ifdef DEBUG #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__) #else #define DEBUG_PRINTF(...) #endif typedef struct { rt_uint16_t width; //LCD 宽度 rt_uint16_t height; //LCD 高度 rt_uint16_t id; //LCD ID rt_uint8_t dir; //横屏还是竖屏控制:0,竖屏;1,横屏。 rt_uint16_t wramcmd; //开始写gram指令 rt_uint16_t setxcmd; //设置x坐标指令 rt_uint16_t setycmd; //设置y坐标指令 } lcd_info_t; typedef struct { volatile rt_uint16_t reg; volatile rt_uint16_t ram; } lcd_ili9341_t; //使用NOR/SRAM的 Bank1.sector1,地址位HADDR[27,26]=00 A18作为数据命令区分线 //注意设置时STM32内部会右移一位对其! #define LCD_ILI9341_BASE ((rt_uint32_t)(0x60000000 | 0x0007FFFE)) #define ili9341 ((lcd_ili9341_t *) LCD_ILI9341_BASE) ////////////////////////////////////////////////////////////////////////////////// //扫描方向定义 #define L2R_U2D 0 //从左到右,从上到下 #define L2R_D2U 1 //从左到右,从下到上 #define R2L_U2D 2 //从右到左,从上到下 #define R2L_D2U 3 //从右到左,从下到上 #define U2D_L2R 4 //从上到下,从左到右 #define U2D_R2L 5 //从上到下,从右到左 #define D2U_L2R 6 //从下到上,从左到右 #define D2U_R2L 7 //从下到上,从右到左 #define DFT_SCAN_DIR L2R_U2D //默认的扫描方向 static lcd_info_t lcddev; LTDC_HandleTypeDef LtdcHandler; static RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /* Default LCD configuration with LCD Layer 1 */ static uint32_t ActiveLayer = 0; SPI_HandleTypeDef hspi5; static SPI_HandleTypeDef SpiHandle; void delay_us(rt_uint32_t nus) { //rt_thread_delay(1); while (nus--) { __NOP(); } } void delay_ms(rt_uint32_t nms) { //rt_thread_delay((RT_TICK_PER_SECOND * nms + 999) / 1000); while (nms--) { int i; for (i = 0; i < 10000; i++) { __NOP(); } } } /** * @brief SPI MSP Init. * @param hspi: SPI handle */ static void SPIx_MspInit(SPI_HandleTypeDef *hspi) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPIx clock */ DISCOVERY_SPIx_CLK_ENABLE(); /* Enable DISCOVERY_SPI GPIO clock */ DISCOVERY_SPIx_GPIO_CLK_ENABLE(); /* configure SPI SCK, MOSI and MISO */ GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN); GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_PULLDOWN; GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM; GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF; HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure); } /** * @brief Selects the LCD Layer. * @param LayerIndex: the Layer foreground or background. */ void BSP_LCD_SelectLayer(uint32_t LayerIndex) { ActiveLayer = LayerIndex; } /** * @brief Initializes the LTDC MSP. */ __weak void BSP_LCD_MspInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable the LTDC and DMA2D Clock */ __HAL_RCC_LTDC_CLK_ENABLE(); __HAL_RCC_DMA2D_CLK_ENABLE(); /* Enable GPIOs clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOG_CLK_ENABLE(); /* GPIOs Configuration */ /* +------------------------+-----------------------+----------------------------+ + LCD pins assignment + +------------------------+-----------------------+----------------------------+ | LCD_TFT R2 <-> PC.10 | LCD_TFT G2 <-> PA.06 | LCD_TFT B2 <-> PD.06 | | LCD_TFT R3 <-> PB.00 | LCD_TFT G3 <-> PG.10 | LCD_TFT B3 <-> PG.11 | | LCD_TFT R4 <-> PA.11 | LCD_TFT G4 <-> PB.10 | LCD_TFT B4 <-> PG.12 | | LCD_TFT R5 <-> PA.12 | LCD_TFT G5 <-> PB.11 | LCD_TFT B5 <-> PA.03 | | LCD_TFT R6 <-> PB.01 | LCD_TFT G6 <-> PC.07 | LCD_TFT B6 <-> PB.08 | | LCD_TFT R7 <-> PG.06 | LCD_TFT G7 <-> PD.03 | LCD_TFT B7 <-> PB.09 | ------------------------------------------------------------------------------- | LCD_TFT HSYNC <-> PC.06 | LCDTFT VSYNC <-> PA.04 | | LCD_TFT CLK <-> PG.07 | LCD_TFT DE <-> PF.10 | ----------------------------------------------------- */ /* GPIOA configuration */ GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_11 | GPIO_PIN_12; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; GPIO_InitStructure.Alternate= GPIO_AF14_LTDC; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); /* GPIOB configuration */ GPIO_InitStructure.Pin = GPIO_PIN_8 | \ GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11; HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); /* GPIOC configuration */ GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10; HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); /* GPIOD configuration */ GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_6; HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); /* GPIOF configuration */ GPIO_InitStructure.Pin = GPIO_PIN_10; HAL_GPIO_Init(GPIOF, &GPIO_InitStructure); /* GPIOG configuration */ GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | \ GPIO_PIN_11; HAL_GPIO_Init(GPIOG, &GPIO_InitStructure); /* GPIOB configuration */ GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1; GPIO_InitStructure.Alternate= GPIO_AF9_LTDC; HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); /* GPIOG configuration */ GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_12; HAL_GPIO_Init(GPIOG, &GPIO_InitStructure); } /** * @brief SPIx error treatment function. */ static void SPIx_Error(void) { /* De-initialize the SPI communication BUS */ HAL_SPI_DeInit(&hspi5); /* Re- Initialize the SPI communication BUS */ /* SPI5 parameter configuration*/ hspi5.Instance = SPI5; hspi5.Init.Mode = SPI_MODE_MASTER; hspi5.Init.Direction = SPI_DIRECTION_2LINES; hspi5.Init.DataSize = SPI_DATASIZE_8BIT; hspi5.Init.CLKPolarity = SPI_POLARITY_LOW; hspi5.Init.CLKPhase = SPI_PHASE_1EDGE; hspi5.Init.NSS = SPI_NSS_SOFT; hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi5.Init.TIMode = SPI_TIMODE_DISABLE; hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi5.Init.CRCPolynomial = 10; if (HAL_SPI_Init(&hspi5) != HAL_OK) { //_Error_Handler(__FILE__, __LINE__); } } /** * @brief Writes a byte to device. * @param Value: value to be written */ static void SPIx_Write(uint16_t Value) { HAL_StatusTypeDef status = HAL_OK; status = HAL_SPI_Transmit(&hspi5, (uint8_t*) &Value, 1, 0x1000); /* Check the communication status */ if(status != HAL_OK) { /* Re-Initialize the BUS */ SPIx_Error(); } } /** * @brief Reads 4 bytes from device. * @param ReadSize: Number of bytes to read (max 4 bytes) * @retval Value read on the SPI */ static uint32_t SPIx_Read(uint8_t ReadSize) { HAL_StatusTypeDef status = HAL_OK; uint32_t readvalue; status = HAL_SPI_Receive(&hspi5, (uint8_t*) &readvalue, ReadSize, 0x1000); /* Check the communication status */ if(status != HAL_OK) { /* Re-Initialize the BUS */ SPIx_Error(); } return readvalue; } /** * @brief Configures the LCD_SPI interface. */ void LCD_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure NCS in Output Push-Pull mode */ LCD_WRX_GPIO_CLK_ENABLE(); GPIO_InitStructure.Pin = LCD_WRX_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(LCD_WRX_GPIO_PORT, &GPIO_InitStructure); LCD_RDX_GPIO_CLK_ENABLE(); GPIO_InitStructure.Pin = LCD_RDX_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(LCD_RDX_GPIO_PORT, &GPIO_InitStructure); /* Configure the LCD Control pins ----------------------------------------*/ LCD_NCS_GPIO_CLK_ENABLE(); /* Configure NCS in Output Push-Pull mode */ GPIO_InitStructure.Pin = LCD_NCS_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure); /* Set or Reset the control line */ LCD_CS_LOW(); LCD_CS_HIGH(); /* SPI configuration -----------------------------------------------------*/ SpiHandle.Instance = DISCOVERY_SPIx; /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz) to verify these constraints: - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read - l3gd20 SPI interface max baudrate is 10MHz for write/read - PCLK2 frequency is set to 90 MHz */ SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; /* On STM32F429I-Discovery, LCD ID cannot be read then keep a common configuration */ /* for LCD and GYRO (SPI_DIRECTION_2LINES) */ /* Note: To read a register a LCD, SPI_DIRECTION_1LINE should be set */ SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE; SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW; SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; SpiHandle.Init.CRCPolynomial = 7; SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT; SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; SpiHandle.Init.NSS = SPI_NSS_SOFT; SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; SpiHandle.Init.Mode = SPI_MODE_MASTER; SPIx_MspInit(&SpiHandle); HAL_SPI_Init(&SpiHandle); } /** * @brief Writes to the selected LCD register. * @param LCD_Reg: address of the selected register. * @retval None */ void ili9341_WriteReg(uint8_t LCD_Reg) { /* Reset WRX to send command */ LCD_WRX_LOW(); /* Reset LCD control line(/CS) and Send command */ LCD_CS_LOW(); SPIx_Write(LCD_Reg); /* Deselect: Chip Select high */ LCD_CS_HIGH(); } /** * @brief Writes data to the selected LCD register. * @param LCD_Reg: address of the selected register. * @retval None */ void ili9341_WriteData(uint16_t RegValue) { /* Set WRX to send data */ LCD_WRX_HIGH(); /* Reset LCD control line(/CS) and Send data */ LCD_CS_LOW(); SPIx_Write(RegValue); /* Deselect: Chip Select high */ LCD_CS_HIGH(); } /** * @brief Reads the selected LCD Register. * @param RegValue: Address of the register to read * @param ReadSize: Number of bytes to read * @retval LCD Register Value. */ uint32_t ili9341_ReadData(uint16_t RegValue, uint8_t ReadSize) { uint32_t readvalue = 0; /* Select: Chip Select low */ LCD_CS_LOW(); /* Reset WRX to send command */ LCD_WRX_LOW(); SPIx_Write(RegValue); readvalue = SPIx_Read(ReadSize); /* Set WRX to send data */ LCD_WRX_HIGH(); /* Deselect: Chip Select high */ LCD_CS_HIGH(); return readvalue; } /** * @brief Enables the Display. */ void BSP_LCD_DisplayOn(void) { /* Display On */ ili9341_WriteReg(LCD_DISPLAY_ON); } /** * @brief Disables the Display. */ void BSP_LCD_DisplayOff(void) { /* Display Off */ ili9341_WriteReg(LCD_DISPLAY_OFF); } void ili9341_Init(void) { /* Initialize ILI9341 low level bus layer ----------------------------------*/ LCD_IO_Init(); /* Configure LCD */ ili9341_WriteReg(0xCA); ili9341_WriteData(0xC3); ili9341_WriteData(0x08); ili9341_WriteData(0x50); ili9341_WriteReg(LCD_POWERB); ili9341_WriteData(0x00); ili9341_WriteData(0xC1); ili9341_WriteData(0x30); ili9341_WriteReg(LCD_POWER_SEQ); ili9341_WriteData(0x64); ili9341_WriteData(0x03); ili9341_WriteData(0x12); ili9341_WriteData(0x81); ili9341_WriteReg(LCD_DTCA); ili9341_WriteData(0x85); ili9341_WriteData(0x00); ili9341_WriteData(0x78); ili9341_WriteReg(LCD_POWERA); ili9341_WriteData(0x39); ili9341_WriteData(0x2C); ili9341_WriteData(0x00); ili9341_WriteData(0x34); ili9341_WriteData(0x02); ili9341_WriteReg(LCD_PRC); ili9341_WriteData(0x20); ili9341_WriteReg(LCD_DTCB); ili9341_WriteData(0x00); ili9341_WriteData(0x00); ili9341_WriteReg(LCD_FRMCTR1); ili9341_WriteData(0x00); ili9341_WriteData(0x1B); ili9341_WriteReg(LCD_DFC); ili9341_WriteData(0x0A); ili9341_WriteData(0xA2); ili9341_WriteReg(LCD_POWER1); ili9341_WriteData(0x10); ili9341_WriteReg(LCD_POWER2); ili9341_WriteData(0x10); ili9341_WriteReg(LCD_VCOM1); ili9341_WriteData(0x45); ili9341_WriteData(0x15); ili9341_WriteReg(LCD_VCOM2); ili9341_WriteData(0x90); ili9341_WriteReg(LCD_MAC); ili9341_WriteData(0xC8); ili9341_WriteReg(LCD_3GAMMA_EN); ili9341_WriteData(0x00); ili9341_WriteReg(LCD_RGB_INTERFACE); ili9341_WriteData(0xC2); ili9341_WriteReg(LCD_DFC); ili9341_WriteData(0x0A); ili9341_WriteData(0xA7); ili9341_WriteData(0x27); ili9341_WriteData(0x04); /* Colomn address set */ ili9341_WriteReg(LCD_COLUMN_ADDR); ili9341_WriteData(0x00); ili9341_WriteData(0x00); ili9341_WriteData(0x00); ili9341_WriteData(0xEF); /* Page address set */ ili9341_WriteReg(LCD_PAGE_ADDR); ili9341_WriteData(0x00); ili9341_WriteData(0x00); ili9341_WriteData(0x01); ili9341_WriteData(0x3F); ili9341_WriteReg(LCD_INTERFACE); ili9341_WriteData(0x01); ili9341_WriteData(0x00); ili9341_WriteData(0x06); ili9341_WriteReg(LCD_GRAM); delay_ms(200); ili9341_WriteReg(LCD_GAMMA); ili9341_WriteData(0x01); ili9341_WriteReg(LCD_PGAMMA); ili9341_WriteData(0x0F); ili9341_WriteData(0x29); ili9341_WriteData(0x24); ili9341_WriteData(0x0C); ili9341_WriteData(0x0E); ili9341_WriteData(0x09); ili9341_WriteData(0x4E); ili9341_WriteData(0x78); ili9341_WriteData(0x3C); ili9341_WriteData(0x09); ili9341_WriteData(0x13); ili9341_WriteData(0x05); ili9341_WriteData(0x17); ili9341_WriteData(0x11); ili9341_WriteData(0x00); ili9341_WriteReg(LCD_NGAMMA); ili9341_WriteData(0x00); ili9341_WriteData(0x16); ili9341_WriteData(0x1B); ili9341_WriteData(0x04); ili9341_WriteData(0x11); ili9341_WriteData(0x07); ili9341_WriteData(0x31); ili9341_WriteData(0x33); ili9341_WriteData(0x42); ili9341_WriteData(0x05); ili9341_WriteData(0x0C); ili9341_WriteData(0x0A); ili9341_WriteData(0x28); ili9341_WriteData(0x2F); ili9341_WriteData(0x0F); ili9341_WriteReg(LCD_SLEEP_OUT); delay_ms(200); ili9341_WriteReg(LCD_DISPLAY_ON); /* GRAM start writing */ ili9341_WriteReg(LCD_GRAM); } /** * @brief Initializes the LCD layers. * @param LayerIndex: the layer foreground or background. * @param FB_Address: the layer frame buffer. */ void BSP_LCD_LayerDefaultInit(uint16_t LayerIndex, uint32_t FB_Address) { LTDC_LayerCfgTypeDef Layercfg; /* Layer Init */ Layercfg.WindowX0 = 0; Layercfg.WindowX1 = 240; Layercfg.WindowY0 = 0; Layercfg.WindowY1 = 320; Layercfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565; Layercfg.FBStartAdress = FB_Address; Layercfg.Alpha = 255; Layercfg.Alpha0 = 0; Layercfg.Backcolor.Blue = 0; Layercfg.Backcolor.Green = 0; Layercfg.Backcolor.Red = 0; Layercfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA; Layercfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA; Layercfg.ImageWidth = 240; Layercfg.ImageHeight = 320; HAL_LTDC_ConfigLayer(&LtdcHandler, &Layercfg, LayerIndex); //DrawProp[LayerIndex].BackColor = LCD_COLOR_WHITE; //DrawProp[LayerIndex].pFont = &Font24; //DrawProp[LayerIndex].TextColor = LCD_COLOR_BLACK; /* Dithering activation */ HAL_LTDC_EnableDither(&LtdcHandler); } uint8_t BSP_LCD_Init(void) { /* On STM32F429I-DISCO, it is not possible to read ILI9341 ID because */ /* PIN EXTC is not connected to VDD and then LCD_READ_ID4 is not accessible. */ /* In this case, ReadID function is bypassed.*/ /*if(ili9341_drv.ReadID() == ILI9341_ID)*/ /* LTDC Configuration ----------------------------------------------------*/ LtdcHandler.Instance = LTDC; /* Timing configuration (Typical configuration from ILI9341 datasheet) HSYNC=10 (9+1) HBP=20 (29-10+1) ActiveW=240 (269-20-10+1) HFP=10 (279-240-20-10+1) VSYNC=2 (1+1) VBP=2 (3-2+1) ActiveH=320 (323-2-2+1) VFP=4 (327-320-2-2+1) */ /* Configure horizontal synchronization width */ LtdcHandler.Init.HorizontalSync = ILI9341_HSYNC; /* Configure vertical synchronization height */ LtdcHandler.Init.VerticalSync = ILI9341_VSYNC; /* Configure accumulated horizontal back porch */ LtdcHandler.Init.AccumulatedHBP = ILI9341_HBP; /* Configure accumulated vertical back porch */ LtdcHandler.Init.AccumulatedVBP = ILI9341_VBP; /* Configure accumulated active width */ LtdcHandler.Init.AccumulatedActiveW = 269; /* Configure accumulated active height */ LtdcHandler.Init.AccumulatedActiveH = 323; /* Configure total width */ LtdcHandler.Init.TotalWidth = 279; /* Configure total height */ LtdcHandler.Init.TotalHeigh = 327; /* Configure R,G,B component values for LCD background color */ LtdcHandler.Init.Backcolor.Red= 0; LtdcHandler.Init.Backcolor.Blue= 0; LtdcHandler.Init.Backcolor.Green= 0; /* LCD clock configuration */ /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */ /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 192 Mhz */ /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 192/4 = 48 Mhz */ /* LTDC clock frequency = PLLLCDCLK / LTDC_PLLSAI_DIVR_8 = 48/4 = 6Mhz */ PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC; PeriphClkInitStruct.PLLSAI.PLLSAIN = 192; PeriphClkInitStruct.PLLSAI.PLLSAIR = 4; PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_8; HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); /* Polarity */ LtdcHandler.Init.HSPolarity = LTDC_HSPOLARITY_AL; LtdcHandler.Init.VSPolarity = LTDC_VSPOLARITY_AL; LtdcHandler.Init.DEPolarity = LTDC_DEPOLARITY_AL; LtdcHandler.Init.PCPolarity = LTDC_PCPOLARITY_IPC; BSP_LCD_MspInit(); HAL_LTDC_Init(&LtdcHandler); /* Select the device */ //LcdDrv = &ili9341_drv; /* LCD Init */ ili9341_Init(); /* Initialize the SDRAM */ //BSP_SDRAM_Init(); /* Initialize the font */ //BSP_LCD_SetFont(&LCD_DEFAULT_FONT); return 0; } void BSP_LCD_DrawPixel(uint16_t Xpos, uint16_t Ypos, uint32_t RGB_Code) { /* Write data value to all SDRAM memory */ *(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(Ypos*240 + Xpos))) = RGB_Code; } void BSP_LCD_DrawLine(uint32_t pixel, uint16_t X1, uint16_t Y1, uint16_t X2, uint16_t Y2) { int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0, yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0, curpixel = 0; deltax = ABS(X2 - X1); /* The difference between the x's */ deltay = ABS(Y2 - Y1); /* The difference between the y's */ x = X1; /* Start x off at the first pixel */ y = Y1; /* Start y off at the first pixel */ if (X2 >= X1) /* The x-values are increasing */ { xinc1 = 1; xinc2 = 1; } else /* The x-values are decreasing */ { xinc1 = -1; xinc2 = -1; } if (Y2 >= Y1) /* The y-values are increasing */ { yinc1 = 1; yinc2 = 1; } else /* The y-values are decreasing */ { yinc1 = -1; yinc2 = -1; } if (deltax >= deltay) /* There is at least one x-value for every y-value */ { xinc1 = 0; /* Don't change the x when numerator >= denominator */ yinc2 = 0; /* Don't change the y for every iteration */ den = deltax; num = deltax / 2; numadd = deltay; numpixels = deltax; /* There are more x-values than y-values */ } else /* There is at least one y-value for every x-value */ { xinc2 = 0; /* Don't change the x for every iteration */ yinc1 = 0; /* Don't change the y when numerator >= denominator */ den = deltay; num = deltay / 2; numadd = deltax; numpixels = deltay; /* There are more y-values than x-values */ } for (curpixel = 0; curpixel <= numpixels; curpixel++) { BSP_LCD_DrawPixel(x, y, pixel); /* Draw the current pixel */ num += numadd; /* Increase the numerator by the top of the fraction */ if (num >= den) /* Check if numerator >= denominator */ { num -= den; /* Calculate the new numerator value */ x += xinc1; /* Change the x as appropriate */ y += yinc1; /* Change the y as appropriate */ } x += xinc2; /* Change the x as appropriate */ y += yinc2; /* Change the y as appropriate */ } } rt_uint16_t ili9341_bgr2rgb(rt_uint16_t value) { rt_uint16_t red, green, blue; blue = (value >> 0) & 0x1f; green = (value >> 5) & 0x3f; red = (value >> 11) & 0x1f; return (blue << 11) + (green << 5) + (red << 0); } //static void ili9341_set_scan_direction(rt_uint8_t dir) //{ // rt_uint16_t regval = 0; // rt_uint16_t dirreg = 0; // rt_uint16_t temp; // switch (dir) // { // case L2R_U2D://从左到右,从上到下 // regval |= (0 << 7) | (0 << 6) | (0 << 5); // break; // case L2R_D2U://从左到右,从下到上 // regval |= (1 << 7) | (0 << 6) | (0 << 5); // break; // case R2L_U2D://从右到左,从上到下 // regval |= (0 << 7) | (1 << 6) | (0 << 5); // break; // case R2L_D2U://从右到左,从下到上 // regval |= (1 << 7) | (1 << 6) | (0 << 5); // break; // case U2D_L2R://从上到下,从左到右 // regval |= (0 << 7) | (0 << 6) | (1 << 5); // break; // case U2D_R2L://从上到下,从右到左 // regval |= (0 << 7) | (1 << 6) | (1 << 5); // break; // case D2U_L2R://从下到上,从左到右 // regval |= (1 << 7) | (0 << 6) | (1 << 5); // break; // case D2U_R2L://从下到上,从右到左 // regval |= (1 << 7) | (1 << 6) | (1 << 5); // break; // } // dirreg = 0X36; // ili9341_write_reg_with_value(dirreg, regval); // if (regval & 0X20) // { // if (lcddev.width < lcddev.height)//交换X,Y // { // temp = lcddev.width; // lcddev.width = lcddev.height; // lcddev.height = temp; // } // } // else // { // if (lcddev.width > lcddev.height)//交换X,Y // { // temp = lcddev.width; // lcddev.width = lcddev.height; // lcddev.height = temp; // } // } // // ili9341_write_reg(lcddev.setxcmd); // ili9341_write_data(0); // ili9341_write_data(0); // ili9341_write_data((lcddev.width - 1) >> 8); // ili9341_write_data((lcddev.width - 1) & 0XFF); // ili9341_write_reg(lcddev.setycmd); // ili9341_write_data(0); // ili9341_write_data(0); // ili9341_write_data((lcddev.height - 1) >> 8); // ili9341_write_data((lcddev.height - 1) & 0XFF); //} //void ili9341_set_backlight(rt_uint8_t pwm) //{ // ili9341_write_reg(0xBE); // ili9341_write_data(0x05); // ili9341_write_data(pwm*2.55); // ili9341_write_data(0x01); // ili9341_write_data(0xFF); // ili9341_write_data(0x00); // ili9341_write_data(0x00); //} //void ili9341_set_display_direction(rt_uint8_t dir) //{ // lcddev.dir = dir; // if (dir == 0) // { // lcddev.width = 240; // lcddev.height = 320; // } // else // { // lcddev.width = 320; // lcddev.height = 240; // } // lcddev.wramcmd = 0X2C; // lcddev.setxcmd = 0X2A; // lcddev.setycmd = 0X2B; // ili9341_set_scan_direction(DFT_SCAN_DIR); //} void _lcd_low_level_init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, CSX_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */ GPIO_InitStruct.Pin = CSX_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */ GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); BSP_LCD_Init(); BSP_LCD_LayerDefaultInit(0,0xD0000000); BSP_LCD_SelectLayer(0); BSP_LCD_DisplayOn(); lcddev.width = 240; lcddev.height = 320; //ili9341_set_display_direction(0); //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET); } static rt_err_t lcd_init(rt_device_t dev) { return RT_EOK; } static rt_err_t lcd_open(rt_device_t dev, rt_uint16_t oflag) { return RT_EOK; } static rt_err_t lcd_close(rt_device_t dev) { return RT_EOK; } static rt_err_t lcd_control(rt_device_t dev, int cmd, void *args) { switch (cmd) { case RTGRAPHIC_CTRL_GET_INFO: { struct rt_device_graphic_info *info; info = (struct rt_device_graphic_info*) args; RT_ASSERT(info != RT_NULL); info->bits_per_pixel = 16; info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565; info->framebuffer = RT_NULL; info->width = 240; info->height = 320; } break; case RTGRAPHIC_CTRL_RECT_UPDATE: /* nothong to be done */ break; default: break; } return RT_EOK; } static void ili9341_lcd_set_pixel(const char* pixel, int x, int y) { *(__IO uint16_t*)(LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y * 240 + x))) = *(uint16_t *)pixel; } #ifdef RT_USING_FINSH static void lcd_set_pixel(uint16_t color, int x, int y) { rt_kprintf("lcd set pixel, color: %X, x: %d, y: %d", color, x, y); ili9341_lcd_set_pixel((const char *)&color, x, y); } FINSH_FUNCTION_EXPORT(lcd_set_pixel, set pixel in lcd display); #endif static void ili9341_lcd_get_pixel(char* pixel, int x, int y) { uint32_t ret = 0; if(LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB8888) { /* Read data value from SDRAM memory */ ret = *(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(y * 240 + x))); } else if(LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_RGB888) { /* Read data value from SDRAM memory */ ret = (*(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(y*240 + x))) & 0x00FFFFFF); } else if((LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_RGB565) || \ (LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB4444) || \ (LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_AL88)) { /* Read data value from SDRAM memory */ ret = *(__IO uint16_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x))); } else { /* Read data value from SDRAM memory */ ret = *(__IO uint8_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x))); } *pixel = ret; } #ifdef RT_USING_FINSH static void lcd_get_pixel(int x, int y) { uint16_t pixel; ili9341_lcd_get_pixel((char *)&pixel, x, y); rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d", pixel, x, y); } FINSH_FUNCTION_EXPORT(lcd_get_pixel, get pixel in lcd display); #endif static void ili9341_lcd_draw_hline(const char* pixel, int x1, int x2, int y) { BSP_LCD_DrawLine(*pixel, x1, y, x2, y); } #ifdef RT_USING_FINSH static void lcd_draw_hline(uint16_t pixel, int x1, int x2, int y) { ili9341_lcd_draw_hline((const char *)&pixel, x1, x2, y); rt_kprintf("lcd draw hline, pixel: 0x%X, x1: %d, x2: %d, y: %d", pixel, x1, x2, y); } FINSH_FUNCTION_EXPORT(lcd_draw_hline, draw hline in lcd display); #endif static void ili9341_lcd_draw_vline(const char* pixel, int x, int y1, int y2) { BSP_LCD_DrawLine(*pixel, x, y1, x, y2); } #ifdef RT_USING_FINSH static void lcd_draw_vline(uint16_t pixel, int x, int y1, int y2) { ili9341_lcd_draw_vline((const char *)&pixel, x, y1, y2); rt_kprintf("lcd draw hline, pixel: 0x%X, x: %d, y: %d", pixel, y1, y2); } FINSH_FUNCTION_EXPORT(lcd_draw_vline, draw vline in lcd display); #endif static void ili9341_lcd_blit_line(const char* pixels, int x, int y, rt_size_t size) { int i = 0; while(size--) *(__IO uint16_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x + i++))) = *(uint16_t *)pixels++; } #ifdef RT_USING_FINSH #define LINE_LEN 30 static void lcd_blit_line(int x, int y) { uint16_t pixels[LINE_LEN]; int i; for (i = 0; i < LINE_LEN; i++) { pixels[i] = i * 40 + 50; } ili9341_lcd_blit_line((const char *)pixels, x, y, LINE_LEN); rt_kprintf("lcd blit line, x: %d, y: %d", x, y); } FINSH_FUNCTION_EXPORT(lcd_blit_line, draw blit line in lcd display); #endif static int rt_hw_lcd_init(void) { _lcd_low_level_init(); static struct rt_device lcd_device; static struct rt_device_graphic_ops ili9341_ops = { ili9341_lcd_set_pixel, ili9341_lcd_get_pixel, ili9341_lcd_draw_hline, ili9341_lcd_draw_vline, ili9341_lcd_blit_line }; /* register lcd device */ lcd_device.type = RT_Device_Class_Graphic; lcd_device.init = lcd_init; lcd_device.open = lcd_open; lcd_device.close = lcd_close; lcd_device.control = lcd_control; lcd_device.read = RT_NULL; lcd_device.write = RT_NULL; lcd_device.user_data = &ili9341_ops; /* register graphic device driver */ rt_device_register(&lcd_device, "lcd", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return 0; } INIT_BOARD_EXPORT(rt_hw_lcd_init);