2 ******************************************************************************
3 * @file stm32l0xx_hal_lcd.c
4 * @author MCD Application Team
6 * @date 06-February-2015
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) 2015 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 "stm32l0xx_hal.h"
97 #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx)
99 /** @addtogroup STM32L0xx_HAL_Driver
103 #ifdef HAL_LCD_MODULE_ENABLED
105 /** @defgroup LCD LCD
106 * @brief LCD HAL module driver
110 /* Private typedef -----------------------------------------------------------*/
111 /* Private define ------------------------------------------------------------*/
112 /** @defgroup LCD_Private_Defines LCD Private Defines
115 #define LCD_TIMEOUT_VALUE 1000
119 /* Private macro -------------------------------------------------------------*/
120 /* Private variables ---------------------------------------------------------*/
121 /* Private function prototypes -----------------------------------------------*/
122 /* Private functions ---------------------------------------------------------*/
124 /** @defgroup LCD_Exported_Functions LCD Exported Functions
128 /** @defgroup LCD_Exported_Functions_Group1 Initialization/de-initialization methods
129 * @brief Initialization and Configuration functions
132 ===============================================================================
133 ##### Initialization and Configuration functions #####
134 ===============================================================================
142 * @brief DeInitializes the LCD peripheral.
143 * @param hlcd: LCD handle
146 HAL_StatusTypeDef HAL_LCD_DeInit(LCD_HandleTypeDef *hlcd)
148 /* Check the LCD handle allocation */
154 /* Check the parameters */
155 assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
157 /* Check the LCD peripheral state */
158 if(hlcd->State == HAL_LCD_STATE_BUSY)
163 hlcd->State = HAL_LCD_STATE_BUSY;
165 /* Disable the peripheral */
166 __HAL_LCD_DISABLE(hlcd);
168 /*Disable Highdrive by default*/
169 __HAL_LCD_HIGHDRIVER_DISABLE(hlcd);
171 /* DeInit the low level hardware */
172 HAL_LCD_MspDeInit(hlcd);
174 hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
175 hlcd->State = HAL_LCD_STATE_RESET;
184 * @brief Initializes the LCD peripheral according to the specified parameters
185 * in the LCD_InitStruct.
186 * @note This function can be used only when the LCD is disabled.
187 * The LCD HighDrive can be enabled/disabled using related macros up to user.
188 * @param hlcd: LCD handle
191 HAL_StatusTypeDef HAL_LCD_Init(LCD_HandleTypeDef *hlcd)
193 uint32_t tickstart = 0x00;
196 /* Check the LCD handle allocation */
202 /* Check function parameters */
203 assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
204 assert_param(IS_LCD_PRESCALER(hlcd->Init.Prescaler));
205 assert_param(IS_LCD_DIVIDER(hlcd->Init.Divider));
206 assert_param(IS_LCD_DUTY(hlcd->Init.Duty));
207 assert_param(IS_LCD_BIAS(hlcd->Init.Bias));
208 assert_param(IS_LCD_VOLTAGE_SOURCE(hlcd->Init.VoltageSource));
209 assert_param(IS_LCD_PULSE_ON_DURATION(hlcd->Init.PulseOnDuration));
210 assert_param(IS_LCD_HIGHDRIVE(hlcd->Init.HighDrive));
211 assert_param(IS_LCD_DEAD_TIME(hlcd->Init.DeadTime));
212 assert_param(IS_LCD_CONTRAST(hlcd->Init.Contrast));
213 assert_param(IS_LCD_BLINK_FREQUENCY(hlcd->Init.BlinkFrequency));
214 assert_param(IS_LCD_BLINK_MODE(hlcd->Init.BlinkMode));
216 if(hlcd->State == HAL_LCD_STATE_RESET)
218 /* Initialize the low level hardware (MSP) */
219 HAL_LCD_MspInit(hlcd);
222 hlcd->State = HAL_LCD_STATE_BUSY;
224 /* Disable the peripheral */
225 __HAL_LCD_DISABLE(hlcd);
227 /* Clear the LCD_RAM registers and enable the display request by setting the UDR bit
228 in the LCD_SR register */
229 for(counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
231 hlcd->Instance->RAM[counter] = 0;
233 /* Enable the display request */
234 SET_BIT(hlcd->Instance->SR, LCD_SR_UDR);
236 /* Configure the LCD Prescaler, Divider, Blink mode and Blink Frequency:
237 Set PS[3:0] bits according to hlcd->Init.Prescaler value
238 Set DIV[3:0] bits according to hlcd->Init.Divider value
239 Set BLINK[1:0] bits according to hlcd->Init.BlinkMode value
240 Set BLINKF[2:0] bits according to hlcd->Init.BlinkFrequency value
241 Set DEAD[2:0] bits according to hlcd->Init.DeadTime value
242 Set PON[2:0] bits according to hlcd->Init.PulseOnDuration value
243 Set CC[2:0] bits according to hlcd->Init.Contrast value
244 Set HD[0] bit according to hlcd->Init.HighDrive value*/
246 MODIFY_REG(hlcd->Instance->FCR, \
247 (LCD_FCR_PS | LCD_FCR_DIV | LCD_FCR_BLINK| LCD_FCR_BLINKF | \
248 LCD_FCR_DEAD | LCD_FCR_PON | LCD_FCR_CC), \
249 (hlcd->Init.Prescaler | hlcd->Init.Divider | hlcd->Init.BlinkMode | hlcd->Init.BlinkFrequency | \
250 hlcd->Init.DeadTime | hlcd->Init.PulseOnDuration | hlcd->Init.Contrast | hlcd->Init.HighDrive));
252 /* Wait until LCD Frame Control Register Synchronization flag (FCRSF) is set in the LCD_SR register
253 This bit is set by hardware each time the LCD_FCR register is updated in the LCDCLK
254 domain. It is cleared by hardware when writing to the LCD_FCR register.*/
255 LCD_WaitForSynchro(hlcd);
257 /* Configure the LCD Duty, Bias, Voltage Source, Dead Time:
258 Set DUTY[2:0] bits according to hlcd->Init.Duty value
259 Set BIAS[1:0] bits according to hlcd->Init.Bias value
260 Set VSEL bits according to hlcd->Init.VoltageSource value */
261 MODIFY_REG(hlcd->Instance->CR, \
262 (LCD_CR_DUTY | LCD_CR_BIAS | LCD_CR_VSEL), \
263 (hlcd->Init.Duty | hlcd->Init.Bias | hlcd->Init.VoltageSource));
265 /* Enable the peripheral */
266 __HAL_LCD_ENABLE(hlcd);
269 tickstart = HAL_GetTick();
271 /* Wait Until the LCD is enabled */
272 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_ENS) == RESET)
274 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
276 hlcd->ErrorCode = HAL_LCD_ERROR_ENS;
282 tickstart = HAL_GetTick();
284 /*!< Wait Until the LCD Booster is ready */
285 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_RDY) == RESET)
287 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
289 hlcd->ErrorCode = HAL_LCD_ERROR_RDY;
294 /* Initialize the LCD state */
295 hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
296 hlcd->State= HAL_LCD_STATE_READY;
302 * @brief LCD MSP DeInit.
303 * @param hlcd: LCD handle
306 __weak void HAL_LCD_MspDeInit(LCD_HandleTypeDef *hlcd)
308 /* NOTE: This function Should not be modified, when the callback is needed,
309 the HAL_LCD_MspDeInit could be implemented in the user file
314 * @brief LCD MSP Init.
315 * @param hlcd: LCD handle
318 __weak void HAL_LCD_MspInit(LCD_HandleTypeDef *hlcd)
320 /* NOTE: This function Should not be modified, when the callback is needed,
321 the HAL_LCD_MspInit could be implemented in the user file
329 /** @defgroup LCD_Exported_Functions_Group2 IO operation methods
330 * @brief LCD RAM functions
333 ===============================================================================
334 ##### IO operation functions #####
335 ===============================================================================
336 [..] Using its double buffer memory the LCD controller ensures the coherency of the
337 displayed information without having to use interrupts to control LCD_RAM
339 The application software can access the first buffer level (LCD_RAM) through
340 the APB interface. Once it has modified the LCD_RAM using the HAL_LCD_Write() API,
341 it sets the UDR flag in the LCD_SR register using the HAL_LCD_UpdateDisplayRequest() API.
342 This UDR flag (update display request) requests the updated information to be
343 moved into the second buffer level (LCD_DISPLAY).
344 This operation is done synchronously with the frame (at the beginning of the
345 next frame), until the update is completed, the LCD_RAM is write protected and
346 the UDR flag stays high.
347 Once the update is completed another flag (UDD - Update Display Done) is set and
348 generates an interrupt if the UDDIE bit in the LCD_FCR register is set.
349 The time it takes to update LCD_DISPLAY is, in the worst case, one odd and one
351 The update will not occur (UDR = 1 and UDD = 0) until the display is
359 * @brief Writes a word in the specific LCD RAM.
360 * @param hlcd: LCD handle
361 * @param RAMRegisterIndex: specifies the LCD RAM Register.
362 * This parameter can be one of the following values:
363 * @arg LCD_RAM_REGISTER0: LCD RAM Register 0
364 * @arg LCD_RAM_REGISTER1: LCD RAM Register 1
365 * @arg LCD_RAM_REGISTER2: LCD RAM Register 2
366 * @arg LCD_RAM_REGISTER3: LCD RAM Register 3
367 * @arg LCD_RAM_REGISTER4: LCD RAM Register 4
368 * @arg LCD_RAM_REGISTER5: LCD RAM Register 5
369 * @arg LCD_RAM_REGISTER6: LCD RAM Register 6
370 * @arg LCD_RAM_REGISTER7: LCD RAM Register 7
371 * @arg LCD_RAM_REGISTER8: LCD RAM Register 8
372 * @arg LCD_RAM_REGISTER9: LCD RAM Register 9
373 * @arg LCD_RAM_REGISTER10: LCD RAM Register 10
374 * @arg LCD_RAM_REGISTER11: LCD RAM Register 11
375 * @arg LCD_RAM_REGISTER12: LCD RAM Register 12
376 * @arg LCD_RAM_REGISTER13: LCD RAM Register 13
377 * @arg LCD_RAM_REGISTER14: LCD RAM Register 14
378 * @arg LCD_RAM_REGISTER15: LCD RAM Register 15
379 * @param RAMRegisterMask: specifies the LCD RAM Register Data Mask.
380 * @param Data: specifies LCD Data Value to be written.
381 * @note For LCD glass COM*SEG as 8*40 for example, the LCD common terminals COM[0,7]
382 * are mapped on 32bits LCD_RAM_REGISTER[0,14] according to rules: COM(n) spread
383 * on LCD_RAM_REGISTER(2*n) and LCD_RAM_REGISTER(2*n+1).The segment terminals
384 * SEG[0,39] of COM(n) correspond to LSB bits of related LCD_RAM_REGISTER(2*n)[0,31]
385 * and LCD_RAM_REGISTER(2*n+1)[0,7]
388 HAL_StatusTypeDef HAL_LCD_Write(LCD_HandleTypeDef *hlcd, uint32_t RAMRegisterIndex, uint32_t RAMRegisterMask, uint32_t Data)
390 uint32_t tickstart = 0x00;
392 if((hlcd->State == HAL_LCD_STATE_READY) || (hlcd->State == HAL_LCD_STATE_BUSY))
394 /* Check the parameters */
395 assert_param(IS_LCD_RAM_REGISTER(RAMRegisterIndex));
397 if(hlcd->State == HAL_LCD_STATE_READY)
401 hlcd->State = HAL_LCD_STATE_BUSY;
404 tickstart = HAL_GetTick();
406 /*!< Wait Until the LCD is ready */
407 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
409 if((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
411 hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
412 /* Process Unlocked */
418 /* Copy the new Data bytes to LCD RAM register */
419 MODIFY_REG(hlcd->Instance->RAM[RAMRegisterIndex], ~(RAMRegisterMask), Data);
430 * @brief Clears the LCD RAM registers.
431 * @param hlcd: LCD handle
434 HAL_StatusTypeDef HAL_LCD_Clear(LCD_HandleTypeDef *hlcd)
436 uint32_t tickstart = 0x00;
437 uint32_t counter = 0;
439 if((hlcd->State == HAL_LCD_STATE_READY) || (hlcd->State == HAL_LCD_STATE_BUSY))
444 hlcd->State = HAL_LCD_STATE_BUSY;
447 tickstart = HAL_GetTick();
449 /*!< Wait Until the LCD is ready */
450 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
452 if( (HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
454 hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
456 /* Process Unlocked */
462 /* Clear the LCD_RAM registers */
463 for(counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
465 hlcd->Instance->RAM[counter] = 0;
468 /* Update the LCD display */
469 HAL_LCD_UpdateDisplayRequest(hlcd);
480 * @brief Enables the Update Display Request.
481 * @param hlcd: LCD handle
482 * @note Each time software modifies the LCD_RAM it must set the UDR bit to
483 * transfer the updated data to the second level buffer.
484 * The UDR bit stays set until the end of the update and during this
485 * time the LCD_RAM is write protected.
486 * @note When the display is disabled, the update is performed for all
487 * LCD_DISPLAY locations.
488 * When the display is enabled, the update is performed only for locations
489 * for which commons are active (depending on DUTY). For example if
490 * DUTY = 1/2, only the LCD_DISPLAY of COM0 and COM1 will be updated.
493 HAL_StatusTypeDef HAL_LCD_UpdateDisplayRequest(LCD_HandleTypeDef *hlcd)
495 uint32_t tickstart = 0x00;
497 /* Clear the Update Display Done flag before starting the update display request */
498 __HAL_LCD_CLEAR_FLAG(hlcd, LCD_FLAG_UDD);
500 /* Enable the display request */
501 hlcd->Instance->SR |= LCD_SR_UDR;
504 tickstart = HAL_GetTick();
506 /*!< Wait Until the LCD display is done */
507 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDD) == RESET)
509 if((HAL_GetTick() - tickstart ) > LCD_TIMEOUT_VALUE)
511 hlcd->ErrorCode = HAL_LCD_ERROR_UDD;
513 /* Process Unlocked */
520 hlcd->State = HAL_LCD_STATE_READY;
522 /* Process Unlocked */
532 /** @defgroup LCD_Exported_Functions_Group3 Peripheral State methods
533 * @brief LCD State functions
536 ===============================================================================
537 ##### Peripheral State functions #####
538 ===============================================================================
540 This subsection provides a set of functions allowing to control the LCD:
541 (+) HAL_LCD_GetState() API can be helpful to check in run-time the state of the LCD peripheral State.
542 (+) HAL_LCD_GetError() API to return the LCD error code.
548 * @brief Returns the LCD state.
549 * @param hlcd: LCD handle
552 HAL_LCD_StateTypeDef HAL_LCD_GetState(LCD_HandleTypeDef *hlcd)
558 * @brief Return the LCD error code
559 * @param hlcd: LCD handle
560 * @retval LCD Error Code
562 uint32_t HAL_LCD_GetError(LCD_HandleTypeDef *hlcd)
564 return hlcd->ErrorCode;
575 /** @defgroup LCD_Private_Functions LCD Private Functions
580 * @brief Waits until the LCD FCR register is synchronized in the LCDCLK domain.
581 * This function must be called after any write operation to LCD_FCR register.
582 * @param hlcd: LCD handle
585 HAL_StatusTypeDef LCD_WaitForSynchro(LCD_HandleTypeDef *hlcd)
587 uint32_t tickstart = 0x00;
590 tickstart = HAL_GetTick();
592 /* Loop until FCRSF flag is set */
593 while(__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_FCRSF) == RESET)
595 if((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
597 hlcd->ErrorCode = HAL_LCD_ERROR_FCRSF;
614 #endif /* HAL_LCD_MODULE_ENABLED */
618 #endif /* #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx) */
619 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/