STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_lcd.c
Go to the documentation of this file.
1 
82 /* Includes ------------------------------------------------------------------*/
83 #include "stm32l4xx_hal.h"
84 
85 #if defined(STM32L433xx) || defined(STM32L443xx) || defined(STM32L476xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
86 
91 #ifdef HAL_LCD_MODULE_ENABLED
92 
98 /* Private typedef -----------------------------------------------------------*/
99 /* Private define ------------------------------------------------------------*/
104 #define LCD_TIMEOUT_VALUE 1000U
105 
110 /* Private macro -------------------------------------------------------------*/
111 /* Private variables ---------------------------------------------------------*/
112 /* Private function prototypes -----------------------------------------------*/
113 /* Exported functions --------------------------------------------------------*/
114 
139 HAL_StatusTypeDef HAL_LCD_Init(LCD_HandleTypeDef *hlcd)
140 {
141  uint32_t tickstart;
142  uint32_t counter;
143  HAL_StatusTypeDef status;
144 
145  /* Check the LCD handle allocation */
146  if (hlcd == NULL)
147  {
148  return HAL_ERROR;
149  }
150 
151  /* Check function parameters */
152  assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
153  assert_param(IS_LCD_PRESCALER(hlcd->Init.Prescaler));
154  assert_param(IS_LCD_DIVIDER(hlcd->Init.Divider));
155  assert_param(IS_LCD_DUTY(hlcd->Init.Duty));
156  assert_param(IS_LCD_BIAS(hlcd->Init.Bias));
157  assert_param(IS_LCD_VOLTAGE_SOURCE(hlcd->Init.VoltageSource));
158  assert_param(IS_LCD_PULSE_ON_DURATION(hlcd->Init.PulseOnDuration));
159  assert_param(IS_LCD_HIGH_DRIVE(hlcd->Init.HighDrive));
160  assert_param(IS_LCD_DEAD_TIME(hlcd->Init.DeadTime));
161  assert_param(IS_LCD_CONTRAST(hlcd->Init.Contrast));
162  assert_param(IS_LCD_BLINK_FREQUENCY(hlcd->Init.BlinkFrequency));
163  assert_param(IS_LCD_BLINK_MODE(hlcd->Init.BlinkMode));
164  assert_param(IS_LCD_MUX_SEGMENT(hlcd->Init.MuxSegment));
165 
166  if (hlcd->State == HAL_LCD_STATE_RESET)
167  {
168  /* Allocate lock resource and initialize it */
169  hlcd->Lock = HAL_UNLOCKED;
170 
171  /* Initialize the low level hardware (MSP) */
172  HAL_LCD_MspInit(hlcd);
173  }
174 
175  hlcd->State = HAL_LCD_STATE_BUSY;
176 
177  /* Disable the peripheral */
178  __HAL_LCD_DISABLE(hlcd);
179 
180  /* Clear the LCD_RAM registers and enable the display request by setting the UDR bit
181  in the LCD_SR register */
182  for (counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
183  {
184  hlcd->Instance->RAM[counter] = 0;
185  }
186  /* Enable the display request */
187  hlcd->Instance->SR |= LCD_SR_UDR;
188  /* Configure the LCD Prescaler, Divider, Blink mode and Blink Frequency:
189  Set PS[3:0] bits according to hlcd->Init.Prescaler value
190  Set DIV[3:0] bits according to hlcd->Init.Divider value
191  Set BLINK[1:0] bits according to hlcd->Init.BlinkMode value
192  Set BLINKF[2:0] bits according to hlcd->Init.BlinkFrequency value
193  Set DEAD[2:0] bits according to hlcd->Init.DeadTime value
194  Set PON[2:0] bits according to hlcd->Init.PulseOnDuration value
195  Set CC[2:0] bits according to hlcd->Init.Contrast value
196  Set HD bit according to hlcd->Init.HighDrive value */
197  MODIFY_REG(hlcd->Instance->FCR, \
198  (LCD_FCR_PS | LCD_FCR_DIV | LCD_FCR_BLINK | LCD_FCR_BLINKF | \
199  LCD_FCR_DEAD | LCD_FCR_PON | LCD_FCR_CC | LCD_FCR_HD), \
200  (hlcd->Init.Prescaler | hlcd->Init.Divider | hlcd->Init.BlinkMode | hlcd->Init.BlinkFrequency | \
201  hlcd->Init.DeadTime | hlcd->Init.PulseOnDuration | hlcd->Init.Contrast | hlcd->Init.HighDrive));
202 
203  /* Wait until LCD Frame Control Register Synchronization flag (FCRSF) is set in the LCD_SR register
204  This bit is set by hardware each time the LCD_FCR register is updated in the LCDCLK
205  domain. It is cleared by hardware when writing to the LCD_FCR register.*/
206  status = LCD_WaitForSynchro(hlcd);
207  if (status != HAL_OK)
208  {
209  return status;
210  }
211 
212  /* Configure the LCD Duty, Bias, Voltage Source, Dead Time, Pulse On Duration and Contrast:
213  Set DUTY[2:0] bits according to hlcd->Init.Duty value
214  Set BIAS[1:0] bits according to hlcd->Init.Bias value
215  Set VSEL bit according to hlcd->Init.VoltageSource value
216  Set MUX_SEG bit according to hlcd->Init.MuxSegment value */
217  MODIFY_REG(hlcd->Instance->CR, \
218  (LCD_CR_DUTY | LCD_CR_BIAS | LCD_CR_VSEL | LCD_CR_MUX_SEG), \
219  (hlcd->Init.Duty | hlcd->Init.Bias | hlcd->Init.VoltageSource | hlcd->Init.MuxSegment));
220 
221  /* Enable the peripheral */
222  __HAL_LCD_ENABLE(hlcd);
223 
224  /* Get timeout */
225  tickstart = HAL_GetTick();
226 
227  /* Wait Until the LCD is enabled */
228  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_ENS) == RESET)
229  {
230  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
231  {
232  hlcd->ErrorCode = HAL_LCD_ERROR_ENS;
233  return HAL_TIMEOUT;
234  }
235  }
236 
237  /* Get timeout */
238  tickstart = HAL_GetTick();
239 
241  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_RDY) == RESET)
242  {
243  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
244  {
245  hlcd->ErrorCode = HAL_LCD_ERROR_RDY;
246  return HAL_TIMEOUT;
247  }
248  }
249 
250  /* Initialize the LCD state */
251  hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
252  hlcd->State = HAL_LCD_STATE_READY;
253 
254  return status;
255 }
256 
262 HAL_StatusTypeDef HAL_LCD_DeInit(LCD_HandleTypeDef *hlcd)
263 {
264  /* Check the LCD handle allocation */
265  if (hlcd == NULL)
266  {
267  return HAL_ERROR;
268  }
269 
270  /* Check the parameters */
271  assert_param(IS_LCD_ALL_INSTANCE(hlcd->Instance));
272 
273  hlcd->State = HAL_LCD_STATE_BUSY;
274 
275  /* DeInit the low level hardware */
276  HAL_LCD_MspDeInit(hlcd);
277 
278  hlcd->ErrorCode = HAL_LCD_ERROR_NONE;
279  hlcd->State = HAL_LCD_STATE_RESET;
280 
281  /* Release Lock */
282  __HAL_UNLOCK(hlcd);
283 
284  return HAL_OK;
285 }
286 
293 {
294  /* Prevent unused argument(s) compilation warning */
295  UNUSED(hlcd);
296 
297  /* NOTE: This function should not be modified, when the callback is needed,
298  the HAL_LCD_MspDeInit it to be implemented in the user file
299  */
300 }
301 
308 {
309  /* Prevent unused argument(s) compilation warning */
310  UNUSED(hlcd);
311 
312  /* NOTE: This function should not be modified, when the callback is needed,
313  the HAL_LCD_MspInit is to be implemented in the user file
314  */
315 }
316 
375 HAL_StatusTypeDef HAL_LCD_Write(LCD_HandleTypeDef *hlcd, uint32_t RAMRegisterIndex, uint32_t RAMRegisterMask, uint32_t Data)
376 {
377  uint32_t tickstart;
378  HAL_LCD_StateTypeDef state = hlcd->State;
379 
380  if ((state == HAL_LCD_STATE_READY) || (state == HAL_LCD_STATE_BUSY))
381  {
382  /* Check the parameters */
383  assert_param(IS_LCD_RAM_REGISTER(RAMRegisterIndex));
384 
385  if (hlcd->State == HAL_LCD_STATE_READY)
386  {
387  /* Process Locked */
388  __HAL_LOCK(hlcd);
389  hlcd->State = HAL_LCD_STATE_BUSY;
390 
391  /* Get timeout */
392  tickstart = HAL_GetTick();
393 
395  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
396  {
397  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
398  {
399  hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
400 
401  /* Process Unlocked */
402  __HAL_UNLOCK(hlcd);
403 
404  return HAL_TIMEOUT;
405  }
406  }
407  }
408 
409  /* Copy the new Data bytes to LCD RAM register */
410  MODIFY_REG(hlcd->Instance->RAM[RAMRegisterIndex], ~(RAMRegisterMask), Data);
411 
412  return HAL_OK;
413  }
414  else
415  {
416  return HAL_ERROR;
417  }
418 }
419 
425 HAL_StatusTypeDef HAL_LCD_Clear(LCD_HandleTypeDef *hlcd)
426 {
427  uint32_t tickstart;
428  uint32_t counter;
429  HAL_StatusTypeDef status = HAL_ERROR;
430  HAL_LCD_StateTypeDef state = hlcd->State;
431 
432  if ((state == HAL_LCD_STATE_READY) || (state == HAL_LCD_STATE_BUSY))
433  {
434  /* Process Locked */
435  __HAL_LOCK(hlcd);
436 
437  hlcd->State = HAL_LCD_STATE_BUSY;
438 
439  /* Get timeout */
440  tickstart = HAL_GetTick();
441 
443  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDR) != RESET)
444  {
445  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
446  {
447  hlcd->ErrorCode = HAL_LCD_ERROR_UDR;
448 
449  /* Process Unlocked */
450  __HAL_UNLOCK(hlcd);
451 
452  return HAL_TIMEOUT;
453  }
454  }
455  /* Clear the LCD_RAM registers */
456  for (counter = LCD_RAM_REGISTER0; counter <= LCD_RAM_REGISTER15; counter++)
457  {
458  hlcd->Instance->RAM[counter] = 0;
459  }
460 
461  /* Update the LCD display */
462  status = HAL_LCD_UpdateDisplayRequest(hlcd);
463  }
464  return status;
465 }
466 
482 {
483  uint32_t tickstart;
484 
485  /* Clear the Update Display Done flag before starting the update display request */
486  __HAL_LCD_CLEAR_FLAG(hlcd, LCD_FLAG_UDD);
487 
488  /* Enable the display request */
489  hlcd->Instance->SR |= LCD_SR_UDR;
490 
491  /* Get timeout */
492  tickstart = HAL_GetTick();
493 
495  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_UDD) == RESET)
496  {
497  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
498  {
499  hlcd->ErrorCode = HAL_LCD_ERROR_UDD;
500 
501  /* Process Unlocked */
502  __HAL_UNLOCK(hlcd);
503 
504  return HAL_TIMEOUT;
505  }
506  }
507 
508  hlcd->State = HAL_LCD_STATE_READY;
509 
510  /* Process Unlocked */
511  __HAL_UNLOCK(hlcd);
512 
513  return HAL_OK;
514 }
515 
541 {
542  /* Return LCD handle state */
543  return hlcd->State;
544 }
545 
552 {
553  return hlcd->ErrorCode;
554 }
555 
573 HAL_StatusTypeDef LCD_WaitForSynchro(LCD_HandleTypeDef *hlcd)
574 {
575  uint32_t tickstart;
576 
577  /* Get timeout */
578  tickstart = HAL_GetTick();
579 
580  /* Loop until FCRSF flag is set */
581  while (__HAL_LCD_GET_FLAG(hlcd, LCD_FLAG_FCRSF) == RESET)
582  {
583  if ((HAL_GetTick() - tickstart) > LCD_TIMEOUT_VALUE)
584  {
585  hlcd->ErrorCode = HAL_LCD_ERROR_FCRSF;
586  return HAL_TIMEOUT;
587  }
588  }
589 
590  return HAL_OK;
591 }
592 
601 #endif /* HAL_LCD_MODULE_ENABLED */
602 
607 #endif /* STM32L433xx || STM32L443xx || STM32L476xx || STM32L486xx || STM32L496xx || STM32L4A6xx */
608 
609 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_LCD_Init(LCD_HandleTypeDef *hlcd)
Initialize the LCD peripheral according to the specified parameters in the LCD_InitStruct and initial...
uint32_t PulseOnDuration
HAL_StatusTypeDef HAL_LCD_DeInit(LCD_HandleTypeDef *hlcd)
DeInitialize the LCD peripheral.
This file contains all the functions prototypes for the HAL module driver.
HAL_LCD_StateTypeDef
HAL LCD State structures definition.
__IO HAL_LCD_StateTypeDef State
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
HAL_StatusTypeDef HAL_LCD_Write(LCD_HandleTypeDef *hlcd, uint32_t RAMRegisterIndex, uint32_t RAMRegisterMask, uint32_t Data)
Write a word in the specific LCD RAM.
__HAL_UNLOCK(hrtc)
HAL_StatusTypeDef HAL_LCD_Clear(LCD_HandleTypeDef *hlcd)
Clear the LCD RAM registers.
__HAL_LOCK(hrtc)
void HAL_LCD_MspInit(LCD_HandleTypeDef *hlcd)
Initialize the LCD MSP.
LCD_InitTypeDef Init
return HAL_OK
uint32_t HAL_LCD_GetError(LCD_HandleTypeDef *hlcd)
Return the LCD error code.
__IO uint32_t ErrorCode
HAL_LockTypeDef Lock
UART handle Structure definition.
LCD_TypeDef * Instance
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
void HAL_LCD_MspDeInit(LCD_HandleTypeDef *hlcd)
DeInitialize the LCD MSP.
HAL_StatusTypeDef HAL_LCD_UpdateDisplayRequest(LCD_HandleTypeDef *hlcd)
Enable the Update Display Request.
HAL_LCD_StateTypeDef HAL_LCD_GetState(LCD_HandleTypeDef *hlcd)
Return the LCD handle state.
HAL_StatusTypeDef LCD_WaitForSynchro(LCD_HandleTypeDef *hlcd)
Wait until the LCD FCR register is synchronized in the LCDCLK domain. This function must be called af...
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))