blob: 41e08afea99f80f5cf1474ada8e060bfa55723ca [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23/*
24 * COPYRIGHT(c) 2015 STMicroelectronics
25 *
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 ******************************************************************************
49 */
50
51#include <err.h>
52#include <debug.h>
53#include <trace.h>
54#include <target.h>
55#include <compiler.h>
56#include <string.h>
57#include <lib/gfx.h>
58#include <dev/gpio.h>
59#include <dev/display.h>
60#include <arch/ops.h>
61#include <platform/stm32.h>
62
63/*
64 * lcd initialization sequence, taken from
65 * STM32Cube_FW_F7_V1.1.0/Drivers/BSP/STM32756G_EVAL/stm32756g_eval_lcd.[ch]
66 */
67
68/**
69 * @brief AMPIRE640480 Size
70 */
71#define AMPIRE640480_WIDTH ((uint16_t)640) /* LCD PIXEL WIDTH */
72#define AMPIRE640480_HEIGHT ((uint16_t)480) /* LCD PIXEL HEIGHT */
73
74/**
75 * @brief AMPIRE640480 Timing
76 */
77#define AMPIRE640480_HSYNC ((uint16_t)30) /* Horizontal synchronization */
78#define AMPIRE640480_HBP ((uint16_t)114) /* Horizontal back porch */
79#define AMPIRE640480_HFP ((uint16_t)16) /* Horizontal front porch */
80#define AMPIRE640480_VSYNC ((uint16_t)3) /* Vertical synchronization */
81#define AMPIRE640480_VBP ((uint16_t)32) /* Vertical back porch */
82#define AMPIRE640480_VFP ((uint16_t)10) /* Vertical front porch */
83
84/**
85 * @brief AMPIRE640480 frequency divider
86 */
87#define AMPIRE640480_FREQUENCY_DIVIDER 3 /* LCD Frequency divider */
88
89/** @defgroup STM32756G_EVAL_LCD_Exported_Constants
90 * @{
91 */
92#define MAX_LAYER_NUMBER ((uint32_t)2)
93
94#define LCD_LayerCfgTypeDef LTDC_LayerCfgTypeDef
95
96#define LTDC_ACTIVE_LAYER ((uint32_t)1) /* Layer 1 */
97/**
98 * @brief LCD status structure definition
99 */
100#define LCD_OK ((uint8_t)0x00)
101#define LCD_ERROR ((uint8_t)0x01)
102#define LCD_TIMEOUT ((uint8_t)0x02)
103
104/**
105 * @brief LCD FB_StartAddress
106 */
107#define LCD_FB_START_ADDRESS ((uint32_t)SDRAM_BASE)
108//#define LCD_FB_START_ADDRESS ((uint32_t)EXT_SRAM_BASE)
109
110//#define LCD_PIXEL_FORMAT LTDC_PIXEL_FORMAT_ARGB888
111#define LCD_PIXEL_FORMAT LTDC_PIXEL_FORMAT_RGB565
112
113static LTDC_HandleTypeDef hLtdcEval;
114
115/* Default LCD configuration with LCD Layer 1 */
116static uint32_t ActiveLayer = 0;
117
118/**
119 * @brief Gets the LCD X size.
120 * @retval Used LCD X size
121 */
122uint32_t BSP_LCD_GetXSize(void)
123{
124 return hLtdcEval.LayerCfg[ActiveLayer].ImageWidth;
125}
126
127/**
128 * @brief Gets the LCD Y size.
129 * @retval Used LCD Y size
130 */
131uint32_t BSP_LCD_GetYSize(void)
132{
133 return hLtdcEval.LayerCfg[ActiveLayer].ImageHeight;
134}
135
136/**
137 * @brief Set the LCD X size.
138 * @param imageWidthPixels : image width in pixels unit
139 * @retval None
140 */
141void BSP_LCD_SetXSize(uint32_t imageWidthPixels)
142{
143 hLtdcEval.LayerCfg[ActiveLayer].ImageWidth = imageWidthPixels;
144}
145
146/**
147 * @brief Set the LCD Y size.
148 * @param imageHeightPixels : image height in lines unit
149 * @retval None
150 */
151void BSP_LCD_SetYSize(uint32_t imageHeightPixels)
152{
153 hLtdcEval.LayerCfg[ActiveLayer].ImageHeight = imageHeightPixels;
154}
155
156static size_t BSP_LCD_PixelSize(void)
157{
158 return (hLtdcEval.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB8888) ? 4 : 2;
159}
160
161/**
162 * @brief Initializes the LCD layers.
163 * @param LayerIndex: Layer foreground or background
164 * @param FB_Address: Layer frame buffer
165 * @retval None
166 */
167void BSP_LCD_LayerDefaultInit(uint16_t LayerIndex, uint32_t FB_Address)
168{
169 LCD_LayerCfgTypeDef layer_cfg;
170
171 /* Layer Init */
172 layer_cfg.WindowX0 = 0;
173 layer_cfg.WindowX1 = BSP_LCD_GetXSize();
174 layer_cfg.WindowY0 = 0;
175 layer_cfg.WindowY1 = BSP_LCD_GetYSize();
176 layer_cfg.PixelFormat = LCD_PIXEL_FORMAT;
177 layer_cfg.FBStartAdress = FB_Address;
178 layer_cfg.Alpha = 255;
179 layer_cfg.Alpha0 = 0;
180 layer_cfg.Backcolor.Blue = 0;
181 layer_cfg.Backcolor.Green = 0;
182 layer_cfg.Backcolor.Red = 0;
183 layer_cfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
184 layer_cfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
185 layer_cfg.ImageWidth = BSP_LCD_GetXSize();
186 layer_cfg.ImageHeight = BSP_LCD_GetYSize();
187
188 HAL_LTDC_ConfigLayer(&hLtdcEval, &layer_cfg, LayerIndex);
189}
190
191
192/**
193 * @brief Selects the LCD Layer.
194 * @param LayerIndex: Layer foreground or background
195 * @retval None
196 */
197void BSP_LCD_SelectLayer(uint32_t LayerIndex)
198{
199 ActiveLayer = LayerIndex;
200}
201
202/**
203 * @brief Sets an LCD Layer visible
204 * @param LayerIndex: Visible Layer
205 * @param State: New state of the specified layer
206 * This parameter can be one of the following values:
207 * @arg ENABLE
208 * @arg DISABLE
209 * @retval None
210 */
211void BSP_LCD_SetLayerVisible(uint32_t LayerIndex, FunctionalState State)
212{
213 if (State == ENABLE) {
214 __HAL_LTDC_LAYER_ENABLE(&hLtdcEval, LayerIndex);
215 } else {
216 __HAL_LTDC_LAYER_DISABLE(&hLtdcEval, LayerIndex);
217 }
218 __HAL_LTDC_RELOAD_CONFIG(&hLtdcEval);
219}
220
221/**
222 * @brief Configures the transparency.
223 * @param LayerIndex: Layer foreground or background.
224 * @param Transparency: Transparency
225 * This parameter must be a number between Min_Data = 0x00 and Max_Data = 0xFF
226 * @retval None
227 */
228void BSP_LCD_SetTransparency(uint32_t LayerIndex, uint8_t Transparency)
229{
230 HAL_LTDC_SetAlpha(&hLtdcEval, Transparency, LayerIndex);
231}
232
233/**
234 * @brief Sets an LCD layer frame buffer address.
235 * @param LayerIndex: Layer foreground or background
236 * @param Address: New LCD frame buffer value
237 * @retval None
238 */
239void BSP_LCD_SetLayerAddress(uint32_t LayerIndex, uint32_t Address)
240{
241 HAL_LTDC_SetAddress(&hLtdcEval, Address, LayerIndex);
242}
243
244/**
245 * @brief Enables the display.
246 * @retval None
247 */
248void BSP_LCD_DisplayOn(void)
249{
250 /* Display On */
251 __HAL_LTDC_ENABLE(&hLtdcEval);
252}
253
254/**
255 * @brief Disables the display.
256 * @retval None
257 */
258void BSP_LCD_DisplayOff(void)
259{
260 /* Display Off */
261 __HAL_LTDC_DISABLE(&hLtdcEval);
262}
263
264/**
265 * @brief Initializes the LTDC MSP.
266 * @param hltdc: LTDC handle
267 * @retval None
268 */
269static void BSP_LCD_MspInit(LTDC_HandleTypeDef *hltdc, void *Params)
270{
271 GPIO_InitTypeDef gpio_init_structure;
272
273 /* Enable the LTDC and DMA2D clocks */
274 __HAL_RCC_LTDC_CLK_ENABLE();
275 __HAL_RCC_DMA2D_CLK_ENABLE();
276
277 /* Enable GPIOs clock */
278 __HAL_RCC_GPIOI_CLK_ENABLE();
279 __HAL_RCC_GPIOJ_CLK_ENABLE();
280 __HAL_RCC_GPIOK_CLK_ENABLE();
281
282 /*** LTDC Pins configuration ***/
283 /* GPIOI configuration */
284 gpio_init_structure.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
285 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
286 gpio_init_structure.Pull = GPIO_NOPULL;
287 gpio_init_structure.Speed = GPIO_SPEED_FAST;
288 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
289 HAL_GPIO_Init(GPIOI, &gpio_init_structure);
290
291 /* GPIOJ configuration */
292 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \
293 GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | \
294 GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | \
295 GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
296 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
297 gpio_init_structure.Pull = GPIO_NOPULL;
298 gpio_init_structure.Speed = GPIO_SPEED_FAST;
299 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
300 HAL_GPIO_Init(GPIOJ, &gpio_init_structure);
301
302 /* GPIOK configuration */
303 gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | \
304 GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
305 gpio_init_structure.Mode = GPIO_MODE_AF_PP;
306 gpio_init_structure.Pull = GPIO_NOPULL;
307 gpio_init_structure.Speed = GPIO_SPEED_FAST;
308 gpio_init_structure.Alternate = GPIO_AF14_LTDC;
309 HAL_GPIO_Init(GPIOK, &gpio_init_structure);
310}
311
312/**
313 * @brief Clock Config.
314 * @param hltdc: LTDC handle
315 * @note This API is called by BSP_LCD_Init()
316 * Being __weak it can be overwritten by the application
317 * @retval None
318 */
319static void BSP_LCD_ClockConfig(LTDC_HandleTypeDef *hltdc, void *Params)
320{
321 static RCC_PeriphCLKInitTypeDef periph_clk_init_struct;
322
323 /* AMPIRE640480 LCD clock configuration */
324 /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
325 /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 151 Mhz */
326 /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 151/3 = 50.3 Mhz */
327 /* LTDC clock frequency = PLLLCDCLK / LTDC_PLLSAI_DIVR_2 = 50.3/2 = 25.1 Mhz */
328 periph_clk_init_struct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
329 periph_clk_init_struct.PLLSAI.PLLSAIN = 151;
330 periph_clk_init_struct.PLLSAI.PLLSAIR = AMPIRE640480_FREQUENCY_DIVIDER;
331 periph_clk_init_struct.PLLSAIDivR = RCC_PLLSAIDIVR_2;
332 HAL_RCCEx_PeriphCLKConfig(&periph_clk_init_struct);
333}
334
335/**
336 * @brief Initializes the LCD.
337 * @retval LCD state
338 */
339uint8_t BSP_LCD_Init(void)
340{
341 /* Select the used LCD */
342 /* The LCD AMPIRE 640x480 is selected */
343 /* Timing configuration */
344 hLtdcEval.Init.HorizontalSync = (AMPIRE640480_HSYNC - 1);
345 hLtdcEval.Init.VerticalSync = (AMPIRE640480_VSYNC - 1);
346 hLtdcEval.Init.AccumulatedHBP = (AMPIRE640480_HSYNC + AMPIRE640480_HBP - 1);
347 hLtdcEval.Init.AccumulatedVBP = (AMPIRE640480_VSYNC + AMPIRE640480_VBP - 1);
348 hLtdcEval.Init.AccumulatedActiveH = (AMPIRE640480_HEIGHT + AMPIRE640480_VSYNC + AMPIRE640480_VBP - 1);
349 hLtdcEval.Init.AccumulatedActiveW = (AMPIRE640480_WIDTH + AMPIRE640480_HSYNC + AMPIRE640480_HBP - 1);
350 hLtdcEval.Init.TotalHeigh = (AMPIRE640480_HEIGHT + AMPIRE640480_VSYNC + AMPIRE640480_VBP + AMPIRE640480_VFP - 1);
351 hLtdcEval.Init.TotalWidth = (AMPIRE640480_WIDTH + AMPIRE640480_HSYNC + AMPIRE640480_HBP + AMPIRE640480_HFP - 1);
352
353 /* LCD clock configuration */
354 BSP_LCD_ClockConfig(&hLtdcEval, NULL);
355
356 /* Initialize the LCD pixel width and pixel height */
357 hLtdcEval.LayerCfg->ImageWidth = AMPIRE640480_WIDTH;
358 hLtdcEval.LayerCfg->ImageHeight = AMPIRE640480_HEIGHT;
359
360 /* Background value */
361 hLtdcEval.Init.Backcolor.Blue = 0;
362 hLtdcEval.Init.Backcolor.Green = 0;
363 hLtdcEval.Init.Backcolor.Red = 0;
364
365 /* Polarity */
366 hLtdcEval.Init.HSPolarity = LTDC_HSPOLARITY_AL;
367 hLtdcEval.Init.VSPolarity = LTDC_VSPOLARITY_AL;
368 hLtdcEval.Init.DEPolarity = LTDC_DEPOLARITY_AL;
369 hLtdcEval.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
370 hLtdcEval.Instance = LTDC;
371
372 if (HAL_LTDC_GetState(&hLtdcEval) == HAL_LTDC_STATE_RESET) {
373 /* Initialize the LCD Msp: this __weak function can be rewritten by the application */
374 BSP_LCD_MspInit(&hLtdcEval, NULL);
375 }
376 HAL_LTDC_Init(&hLtdcEval);
377
378 /* set a default layer to the base of sdram */
379 BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
380 BSP_LCD_SelectLayer(0);
381
382 /* clear it out */
383 memset((void *)hLtdcEval.LayerCfg[ActiveLayer].FBStartAdress, 0, BSP_LCD_GetXSize() * BSP_LCD_GetYSize() * BSP_LCD_PixelSize());
384
385 /* turn the display on */
386 BSP_LCD_DisplayOn();
387
388 return LCD_OK;
389}
390
391/* LK display api here */
392status_t display_get_info(struct display_info *info)
393{
394 info->framebuffer = (void *)hLtdcEval.LayerCfg[ActiveLayer].FBStartAdress;
395
396 if (hLtdcEval.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB8888) {
397 info->format = GFX_FORMAT_ARGB_8888;
398 } else if (hLtdcEval.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_RGB565) {
399 info->format = GFX_FORMAT_RGB_565;
400 } else {
401 panic("unhandled pixel format\n");
402 return ERR_NOT_FOUND;
403 }
404
405 info->width = BSP_LCD_GetXSize();
406 info->height = BSP_LCD_GetYSize();
407 info->stride = BSP_LCD_GetXSize();
408 info->flush = NULL;
409
410 return NO_ERROR;
411}
412