2 ******************************************************************************
3 * @file stm32l1xx_hal_lcd.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief LCD Controller HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the LCD Controller (LCD) peripheral:
10 * + Initialization/de-initialization methods
11 * + I/O operation methods
12 * + Peripheral State methods
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..] The LCD HAL driver can be used as follows:
20 (#) Declare a LCD_HandleTypeDef handle structure.
22 (#) Initialize the LCD low level resources by implement the HAL_LCD_MspInit() API:
23 (##) Enable the LCDCLK (same as RTCCLK): to configure the RTCCLK/LCDCLK, proceed as follows:
24 (+) Use RCC function HAL_RCCEx_PeriphCLKConfig in indicating RCC_PERIPHCLK_LCD and
25 selected clock source (HSE, LSI or LSE)
27 -@- The frequency generator allows you to achieve various LCD frame rates
28 starting from an LCD input clock frequency (LCDCLK) which can vary
29 from 32 kHz up to 1 MHz.
30 (##) LCD pins configuration:
31 (+) Enable the clock for the LCD GPIOs.
32 (+) Configure these LCD pins as alternate function no-pull.
33 (##) Enable the LCD interface clock.
35 (#) Program the Prescaler, Divider, Blink mode, Blink Frequency Duty, Bias,
36 Voltage Source, Dead Time, Pulse On Duration and Contrast in the hlcd Init structure.
38 (#) Initialize the LCD registers by calling the HAL_LCD_Init() API.
40 -@- The HAL_LCD_Init() API configures also the low level Hardware GPIO, CLOCK, ...etc)
41 by calling the custumed HAL_LCD_MspInit() API.
42 -@- After calling the HAL_LCD_Init() the LCD RAM memory is cleared
44 (#) Optionally you can update the LCD configuration using these macros:
45 (+) LCD High Drive using the __HAL_LCD_HIGHDRIVER_ENABLE() and __HAL_LCD_HIGHDRIVER_DISABLE() macros
46 (+) LCD Pulse ON Duration using the __HAL_LCD_PULSEONDURATION_CONFIG() macro
47 (+) LCD Dead Time using the __HAL_LCD_DEADTIME_CONFIG() macro
48 (+) The LCD Blink mode and frequency using the __HAL_LCD_BLINK_CONFIG() macro
49 (+) The LCD Contrast using the __HAL_LCD_CONTRAST_CONFIG() macro
51 (#) Write to the LCD RAM memory using the HAL_LCD_Write() API, this API can be called
52 more time to update the different LCD RAM registers before calling
53 HAL_LCD_UpdateDisplayRequest() API.
55 (#) The HAL_LCD_Clear() API can be used to clear the LCD RAM memory.
57 (#) When LCD RAM memory is updated enable the update display request using
58 the HAL_LCD_UpdateDisplayRequest() API.
60 [..] LCD and low power modes:
61 (#) The LCD remain active during STOP mode.
64 ******************************************************************************
67 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
69 * Redistribution and use in source and binary forms, with or without modification,
70 * are permitted provided that the following conditions are met:
71 * 1. Redistributions of source code must retain the above copyright notice,
72 * this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright notice,
74 * this list of conditions and the following disclaimer in the documentation
75 * and/or other materials provided with the distribution.
76 * 3. Neither the name of STMicroelectronics nor the names of its contributors
77 * may be used to endorse or promote products derived from this software
78 * without specific prior written permission.
80 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
81 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
83 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
84 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
86 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
87 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
88 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
89 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91 ******************************************************************************
94 /* Includes ------------------------------------------------------------------*/
95 #include "stm32l1xx_hal.h"
97 /** @addtogroup STM32L1xx_HAL_Driver
101 #ifdef HAL_LCD_MODULE_ENABLED
103 #if defined (STM32L100xB) || defined (STM32L100xBA) || defined (STM32L100xC) ||\
104 defined (STM32L152xB) || defined (STM32L152xBA) || defined (STM32L152xC) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L152xE) ||\
105 defined (STM32L162xC) || defined (STM32L162xCA) || defined (STM32L162xD) || defined (STM32L162xE)
107 /** @defgroup LCD LCD
108 * @brief LCD HAL module driver
112 /* Private typedef -----------------------------------------------------------*/
113 /* Private define ------------------------------------------------------------*/
114 /** @defgroup LCD_Private_Defines LCD Private Defines
118 #define LCD_TIMEOUT_VALUE 1000
124 /* Private macro -------------------------------------------------------------*/
125 /* Private variables ---------------------------------------------------------*/
126 /* Private function prototypes -----------------------------------------------*/
127 /* Private functions ---------------------------------------------------------*/
129 /** @defgroup LCD_Exported_Functions LCD Exported Functions
133 /** @defgroup LCD_Exported_Functions_Group1 Initialization/de-initialization methods
134 * @brief Initialization and Configuration functions
137 ===============================================================================
138 ##### Initialization and Configuration functions #####
139 ===============================================================================
147 * @brief DeInitializes the LCD peripheral.
148 * @param hlcd: LCD handle
151 HAL_StatusTypeDef HAL_LCD_DeInit(LCD_HandleTypeDef *hlcd)
153 /* Check the LCD handle allocation */
159 /* Check the parameters */
160 assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
162 hlcd->State = HAL_LCD_STATE_BUSY;
164 /* DeInit the low level hardware */
165 HAL_LCD_MspDeInit(hlcd);
167 hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
168 hlcd->State = HAL_LCD_STATE_RESET;
177 * @brief Initializes the LCD peripheral according to the specified parameters
178 * in the LCD_InitStruct.
179 * @note This function can be used only when the LCD is disabled.
180 * @param hlcd: LCD handle
183 HAL_StatusTypeDef HAL_LCD_Init(LCD_HandleTypeDef *hlcd)
185 uint32_t tickstart = 0x00;
188 /* Check the LCD handle allocation */
194 /* Check function parameters */
195 assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
196 assert_param(IS_LCD_PRESCALER(hlcd->Init.Prescaler));
197 assert_param(IS_LCD_DIVIDER(hlcd->Init.Divider));
198 assert_param(IS_LCD_DUTY(hlcd->Init.Duty));
199 assert_param(IS_LCD_BIAS(hlcd->Init.Bias));
200 assert_param(IS_LCD_VOLTAGE_SOURCE(hlcd->Init.VoltageSource));
201 assert_param(IS_LCD_PULSE_ON_DURATION(hlcd->Init.PulseOnDuration));
202 assert_param(IS_LCD_DEAD_TIME(hlcd->Init.DeadTime));
203 assert_param(IS_LCD_CONTRAST(hlcd->Init.Contrast));
204 assert_param(IS_LCD_BLINK_FREQUENCY(hlcd->Init.BlinkFrequency));
205 assert_param(IS_LCD_BLINK_MODE(hlcd->Init.BlinkMode));
206 assert_param(IS_LCD_MUXSEGMENT(hlcd->Init.MuxSegment));
208 if(hlcd->State == HAL_LCD_STATE_RESET)
210 /* Initialize the low level hardware (MSP) */
211 HAL_LCD_MspInit(hlcd);
214 hlcd->State = HAL_LCD_STATE_BUSY;
216 /* Disable the peripheral */
217 __HAL_LCD_DISABLE(hlcd);
219 /* Clear the LCD_RAM registers and enable the display request by setting the UDR bit
220 in the LCD_SR register */
221 for(counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
223 hlcd->Instance->RAM[counter] = 0;
225 /* Enable the display request */
226 hlcd->Instance->SR |= LCD_SR_UDR;
228 /* Configure the LCD Prescaler, Divider, Blink mode and Blink Frequency:
229 Set PS[3:0] bits according to hlcd->Init.Prescaler value
230 Set DIV[3:0] bits according to hlcd->Init.Divider value
231 Set BLINK[1:0] bits according to hlcd->Init.BlinkMode value
232 Set BLINKF[2:0] bits according to hlcd->Init.BlinkFrequency value
233 Set DEAD[2:0] bits according to hlcd->Init.DeadTime value
234 Set PON[2:0] bits according to hlcd->Init.PulseOnDuration value
235 Set CC[2:0] bits according to hlcd->Init.Contrast value */
236 MODIFY_REG(hlcd->Instance->FCR, \
237 (LCD_FCR_PS | LCD_FCR_DIV | LCD_FCR_BLINK| LCD_FCR_BLINKF | \
238 LCD_FCR_DEAD | LCD_FCR_PON | LCD_FCR_CC), \
239 (hlcd->Init.Prescaler | hlcd->Init.Divider | hlcd->Init.BlinkMode | hlcd->Init.BlinkFrequency | \
240 hlcd->Init.DeadTime | hlcd->Init.PulseOnDuration | hlcd->Init.Contrast));
242 /* Wait until LCD Frame Control Register Synchronization flag (FCRSF) is set in the LCD_SR register
243 This bit is set by hardware each time the LCD_FCR register is updated in the LCDCLK
244 domain. It is cleared by hardware when writing to the LCD_FCR register.*/
245 LCD_WaitForSynchro(hlcd);
247 /* Configure the LCD Duty, Bias, Voltage Source, Dead Time, Pulse On Duration and Contrast:
248 Set DUTY[2:0] bits according to hlcd->Init.Duty value
249 Set BIAS[1:0] bits according to hlcd->Init.Bias value
250 Set VSEL bit according to hlcd->Init.VoltageSource value
251 Set MUX_SEG bit according to hlcd->Init.MuxSegment value */
252 MODIFY_REG(hlcd->Instance->CR, \
253 (LCD_CR_DUTY | LCD_CR_BIAS | LCD_CR_VSEL | LCD_CR_MUX_SEG), \
254 (hlcd->Init.Duty | hlcd->Init.Bias | hlcd->Init.VoltageSource | hlcd->Init.MuxSegment));
256 /* Enable the peripheral */
257 __HAL_LCD_ENABLE(hlcd);
260 tickstart = HAL_GetTick();
262 /* Wait Until the LCD is enabled */
263 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_ENS) == RESET)
265 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
267 hlcd->ErrorCode = HAL_LCD_ERROR_ENS;
273 tickstart = HAL_GetTick();
275 /*!< Wait Until the LCD Booster is ready */
276 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_RDY) == RESET)
278 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
280 hlcd->ErrorCode = HAL_LCD_ERROR_RDY;
285 /* Initialize the LCD state */
286 hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
287 hlcd->State= HAL_LCD_STATE_READY;
293 * @brief LCD MSP DeInit.
294 * @param hlcd: LCD handle
297 __weak void HAL_LCD_MspDeInit(LCD_HandleTypeDef *hlcd)
299 /* NOTE: This function Should not be modified, when the callback is needed,
300 the HAL_LCD_MspDeInit could be implemented in the user file
305 * @brief LCD MSP Init.
306 * @param hlcd: LCD handle
309 __weak void HAL_LCD_MspInit(LCD_HandleTypeDef *hlcd)
311 /* NOTE: This function Should not be modified, when the callback is needed,
312 the HAL_LCD_MspInit could be implemented in the user file
320 /** @defgroup LCD_Exported_Functions_Group2 IO operation methods
321 * @brief LCD RAM functions
324 ===============================================================================
325 ##### IO operation functions #####
326 ===============================================================================
327 [..] Using its double buffer memory the LCD controller ensures the coherency of the
328 displayed information without having to use interrupts to control LCD_RAM
330 The application software can access the first buffer level (LCD_RAM) through
331 the APB interface. Once it has modified the LCD_RAM using the HAL_LCD_Write() API,
332 it sets the UDR flag in the LCD_SR register using the HAL_LCD_UpdateDisplayRequest() API.
333 This UDR flag (update display request) requests the updated information to be
334 moved into the second buffer level (LCD_DISPLAY).
335 This operation is done synchronously with the frame (at the beginning of the
336 next frame), until the update is completed, the LCD_RAM is write protected and
337 the UDR flag stays high.
338 Once the update is completed another flag (UDD - Update Display Done) is set and
339 generates an interrupt if the UDDIE bit in the LCD_FCR register is set.
340 The time it takes to update LCD_DISPLAY is, in the worst case, one odd and one
342 The update will not occur (UDR = 1 and UDD = 0) until the display is
350 * @brief Writes a word in the specific LCD RAM.
351 * @param hlcd: LCD handle
352 * @param RAMRegisterIndex: specifies the LCD RAM Register.
353 * This parameter can be one of the following values:
354 * @arg LCD_RAM_REGISTER0: LCD RAM Register 0
355 * @arg LCD_RAM_REGISTER1: LCD RAM Register 1
356 * @arg LCD_RAM_REGISTER2: LCD RAM Register 2
357 * @arg LCD_RAM_REGISTER3: LCD RAM Register 3
358 * @arg LCD_RAM_REGISTER4: LCD RAM Register 4
359 * @arg LCD_RAM_REGISTER5: LCD RAM Register 5
360 * @arg LCD_RAM_REGISTER6: LCD RAM Register 6
361 * @arg LCD_RAM_REGISTER7: LCD RAM Register 7
362 * @arg LCD_RAM_REGISTER8: LCD RAM Register 8
363 * @arg LCD_RAM_REGISTER9: LCD RAM Register 9
364 * @arg LCD_RAM_REGISTER10: LCD RAM Register 10
365 * @arg LCD_RAM_REGISTER11: LCD RAM Register 11
366 * @arg LCD_RAM_REGISTER12: LCD RAM Register 12
367 * @arg LCD_RAM_REGISTER13: LCD RAM Register 13
368 * @arg LCD_RAM_REGISTER14: LCD RAM Register 14
369 * @arg LCD_RAM_REGISTER15: LCD RAM Register 15
370 * @param RAMRegisterMask: specifies the LCD RAM Register Data Mask.
371 * @param Data: specifies LCD Data Value to be written.
374 HAL_StatusTypeDef HAL_LCD_Write(LCD_HandleTypeDef *hlcd, uint32_t RAMRegisterIndex, uint32_t RAMRegisterMask, uint32_t Data)
376 uint32_t tickstart = 0x00;
378 if((hlcd->State == HAL_LCD_STATE_READY) || (hlcd->State == HAL_LCD_STATE_BUSY))
380 /* Check the parameters */
381 assert_param(IS_LCD_RAM_REGISTER(RAMRegisterIndex));
383 if(hlcd->State == HAL_LCD_STATE_READY)
387 hlcd->State = HAL_LCD_STATE_BUSY;
390 tickstart = HAL_GetTick();
392 /*!< Wait Until the LCD is ready */
393 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
395 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
397 hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
399 /* Process Unlocked */
407 /* Copy the new Data bytes to LCD RAM register */
408 MODIFY_REG(hlcd->Instance->RAM[RAMRegisterIndex], ~(RAMRegisterMask), Data);
419 * @brief Clears the LCD RAM registers.
420 * @param hlcd: LCD handle
423 HAL_StatusTypeDef HAL_LCD_Clear(LCD_HandleTypeDef *hlcd)
425 uint32_t tickstart = 0x00;
426 uint32_t counter = 0;
428 if((hlcd->State == HAL_LCD_STATE_READY) || (hlcd->State == HAL_LCD_STATE_BUSY))
433 hlcd->State = HAL_LCD_STATE_BUSY;
436 tickstart = HAL_GetTick();
438 /*!< Wait Until the LCD is ready */
439 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
441 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
443 hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
445 /* Process Unlocked */
451 /* Clear the LCD_RAM registers */
452 for(counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
454 hlcd->Instance->RAM[counter] = 0;
457 /* Update the LCD display */
458 HAL_LCD_UpdateDisplayRequest(hlcd);
469 * @brief Enables the Update Display Request.
470 * @param hlcd: LCD handle
471 * @note Each time software modifies the LCD_RAM it must set the UDR bit to
472 * transfer the updated data to the second level buffer.
473 * The UDR bit stays set until the end of the update and during this
474 * time the LCD_RAM is write protected.
475 * @note When the display is disabled, the update is performed for all
476 * LCD_DISPLAY locations.
477 * When the display is enabled, the update is performed only for locations
478 * for which commons are active (depending on DUTY). For example if
479 * DUTY = 1/2, only the LCD_DISPLAY of COM0 and COM1 will be updated.
482 HAL_StatusTypeDef HAL_LCD_UpdateDisplayRequest(LCD_HandleTypeDef *hlcd)
484 uint32_t tickstart = 0x00;
486 /* Clear the Update Display Done flag before starting the update display request */
487 __HAL_LCD_CLEAR_FLAG(hlcd, LCD_FLAG_UDD);
489 /* Enable the display request */
490 hlcd->Instance->SR |= LCD_SR_UDR;
493 tickstart = HAL_GetTick();
495 /*!< Wait Until the LCD display is done */
496 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDD) == RESET)
498 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
500 hlcd->ErrorCode = HAL_LCD_ERROR_UDD;
502 /* Process Unlocked */
509 hlcd->State = HAL_LCD_STATE_READY;
511 /* Process Unlocked */
521 /** @defgroup LCD_Exported_Functions_Group3 Peripheral State methods
522 * @brief LCD State functions
525 ===============================================================================
526 ##### Peripheral State functions #####
527 ===============================================================================
529 This subsection provides a set of functions allowing to control the LCD:
530 (+) HAL_LCD_GetState() API can be helpful to check in run-time the state of the LCD peripheral State.
531 (+) HAL_LCD_GetError() API to return the LCD error code.
537 * @brief Returns the LCD state.
538 * @param hlcd: LCD handle
541 HAL_LCD_StateTypeDef HAL_LCD_GetState(LCD_HandleTypeDef *hlcd)
547 * @brief Return the LCD error code
548 * @param hlcd: LCD handle
549 * @retval LCD Error Code
551 uint32_t HAL_LCD_GetError(LCD_HandleTypeDef *hlcd)
553 return hlcd->ErrorCode;
564 /** @defgroup LCD_Private_Functions LCD Private Functions
569 * @brief Waits until the LCD FCR register is synchronized in the LCDCLK domain.
570 * This function must be called after any write operation to LCD_FCR register.
573 HAL_StatusTypeDef LCD_WaitForSynchro(LCD_HandleTypeDef *hlcd)
575 uint32_t tickstart = 0x00;
578 tickstart = HAL_GetTick();
580 /* Loop until FCRSF flag is set */
581 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_FCRSF) == RESET)
583 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
585 hlcd->ErrorCode = HAL_LCD_ERROR_FCRSF;
601 #endif /* STM32L100xB || STM32L100xBA || STM32L100xC ||... || STM32L162xD || STM32L162xE */
603 #endif /* HAL_LCD_MODULE_ENABLED */
609 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/