1
0

fsl_lcdc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_lcdc.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /* Max value of LCD_POL[PCD]. */
  35. #define LCD_PCD_MAX \
  36. ((LCD_POL_PCD_LO_MASK >> LCD_POL_PCD_LO_SHIFT) | \
  37. (LCD_POL_PCD_HI_MASK >> (LCD_POL_PCD_HI_SHIFT - LCD_POL_PCD_LO_SHIFT)))
  38. /* Macro to contruct the LCD_POL[PCD]. */
  39. #if (LCD_POL_PCD_LO_MASK != 0x1F)
  40. #error LCD_POL_PCD_LO is not 5-bit. The macro LCD_POL_PCD_LO_WIDTH should be updated.
  41. #endif
  42. #define LCD_POL_PCD_LO_WIDTH 5U
  43. #define LCD_POL_PCD(pcd) (LCD_POL_PCD_LO(pcd) | LCD_POL_PCD_HI((pcd) >> LCD_POL_PCD_LO_WIDTH))
  44. /* Cursor interrupt. */
  45. #define LCDC_CURSOR_INT_MASK LCD_CRSR_INTMSK_CRSRIM_MASK
  46. /* Interrupts except cursor interrupt. */
  47. #define LCDC_NORMAL_INT_MASK \
  48. (LCD_INTMSK_FUFIM_MASK | LCD_INTMSK_LNBUIM_MASK | LCD_INTMSK_VCOMPIM_MASK | LCD_INTMSK_BERIM_MASK)
  49. /* Detect the cursor interrupt and normal interrupt bits overlap. */
  50. #if (LCDC_CURSOR_INT_MASK & LCDC_NORMAL_INT_MASK)
  51. #error Cursor interrupt and normal interrupt overlap. The driver should be updated.
  52. #endif
  53. /* The max cursor clip value. */
  54. #define LCDC_CLIP_MAX (LCD_CRSR_CLIP_CRSRCLIPX_MASK >> LCD_CRSR_CLIP_CRSRCLIPX_SHIFT)
  55. /*******************************************************************************
  56. * Variables
  57. ******************************************************************************/
  58. static LCD_Type *const s_lcdBases[] = LCD_BASE_PTRS;
  59. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  60. static const clock_ip_name_t s_lcdClocks[] = LCD_CLOCKS;
  61. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  62. static const reset_ip_name_t s_lcdResets[] = LCD_RSTS;
  63. /*******************************************************************************
  64. * Prototypes
  65. ******************************************************************************/
  66. /*!
  67. * @brief Gets the LCD instance according to the LCD base
  68. *
  69. * @param base LCD peripheral base address.
  70. * @return LCD instance.
  71. */
  72. static uint32_t LCDC_GetInstance(LCD_Type *base);
  73. /*!
  74. * @brief Calculate the clock divider to generate desired panel clock.
  75. *
  76. * @param config Pointer to the LCD configuration.
  77. * @param srcClock_Hz The LCD input clock (LCDCLK) frequency in Hz.
  78. * @param divider The divider result.
  79. * @return Return false if no divider available to generate the desired clock,
  80. * otherwise return true;
  81. */
  82. static bool LCDC_GetClockDivider(const lcdc_config_t *config, uint32_t srcClock_Hz, uint32_t *divider);
  83. /*******************************************************************************
  84. * Code
  85. ******************************************************************************/
  86. static uint32_t LCDC_GetInstance(LCD_Type *base)
  87. {
  88. uint32_t instance;
  89. /* Find the instance index from base address mappings. */
  90. for (instance = 0; instance < ARRAY_SIZE(s_lcdBases); instance++)
  91. {
  92. if (s_lcdBases[instance] == base)
  93. {
  94. break;
  95. }
  96. }
  97. assert(instance < ARRAY_SIZE(s_lcdBases));
  98. return instance;
  99. }
  100. static bool LCDC_GetClockDivider(const lcdc_config_t *config, uint32_t srcClock_Hz, uint32_t *divider)
  101. {
  102. uint16_t cpl;
  103. uint32_t pcd;
  104. *divider = 0U;
  105. /* Find the PCD. */
  106. pcd = (srcClock_Hz + (config->panelClock_Hz / 2U)) / config->panelClock_Hz;
  107. if (pcd <= 1U)
  108. {
  109. if (kLCDC_DisplayTFT == config->display)
  110. {
  111. pcd = 0U;
  112. *divider = LCD_POL_BCD_MASK;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. else
  120. {
  121. pcd -= 2U;
  122. /* Verify the PCD value. */
  123. if (pcd > LCD_PCD_MAX)
  124. {
  125. return false;
  126. }
  127. if (((kLCDC_DisplaySingleColorSTN8Bit == config->display) && (pcd < 1U)) ||
  128. ((kLCDC_DisplayDualColorSTN8Bit == config->display) && (pcd < 4U)) ||
  129. ((kLCDC_DisplaySingleMonoSTN4Bit == config->display) && (pcd < 2U)) ||
  130. ((kLCDC_DisplaySingleMonoSTN8Bit == config->display) && (pcd < 8U)) ||
  131. ((kLCDC_DisplayDualMonoSTN4Bit == config->display) && (pcd < 8U)) ||
  132. ((kLCDC_DisplayDualMonoSTN8Bit == config->display) && (pcd < 14U)))
  133. {
  134. return false;
  135. }
  136. }
  137. if (config->display & LCD_CTRL_LCDTFT_MASK)
  138. {
  139. /* TFT panel. */
  140. cpl = config->ppl - 1U;
  141. }
  142. else
  143. {
  144. if (config->display & LCD_CTRL_LCDBW_MASK)
  145. {
  146. if (config->display & LCD_CTRL_LCDMONO8_MASK)
  147. {
  148. /* 8-bit monochrome STN panel. */
  149. cpl = (config->ppl / 8U) - 1U;
  150. }
  151. else
  152. {
  153. /* 4-bit monochrome STN panel. */
  154. cpl = (config->ppl / 4U) - 1U;
  155. }
  156. }
  157. else
  158. {
  159. /* Color STN panel. */
  160. cpl = ((config->ppl * 3U) / 8U) - 1U;
  161. }
  162. }
  163. *divider |= (LCD_POL_CPL(cpl) | LCD_POL_PCD(pcd));
  164. return true;
  165. }
  166. status_t LCDC_Init(LCD_Type *base, const lcdc_config_t *config, uint32_t srcClock_Hz)
  167. {
  168. assert(config);
  169. assert(srcClock_Hz);
  170. assert((config->ppl & 0xFU) == 0U);
  171. assert((config->upperPanelAddr & 0x07U) == 0U);
  172. assert((config->lowerPanelAddr & 0x07U) == 0U);
  173. uint32_t reg;
  174. uint32_t divider;
  175. uint32_t instance;
  176. /* Verify the clock here. */
  177. if (!LCDC_GetClockDivider(config, srcClock_Hz, &divider))
  178. {
  179. return kStatus_InvalidArgument;
  180. }
  181. instance = LCDC_GetInstance(base);
  182. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  183. CLOCK_EnableClock(s_lcdClocks[instance]);
  184. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  185. /* Reset the module */
  186. RESET_PeripheralReset(s_lcdResets[instance]);
  187. /* Set register CTRL. */
  188. reg = base->CTRL & (LCD_CTRL_LCDVCOMP_MASK | LCD_CTRL_WATERMARK_MASK);
  189. reg |= (uint32_t)(config->dataFormat) | (uint32_t)(config->display) | LCD_CTRL_LCDBPP(config->bpp);
  190. if (config->swapRedBlue)
  191. {
  192. reg |= LCD_CTRL_BGR_MASK;
  193. }
  194. base->CTRL = reg;
  195. /* Clean pending interrupts and disable all interrupts. */
  196. base->INTCLR = LCDC_NORMAL_INT_MASK;
  197. base->CRSR_INTCLR = LCDC_CURSOR_INT_MASK;
  198. base->INTMSK = 0U;
  199. base->CRSR_INTMSK = 0U;
  200. /* Configure timing. */
  201. base->TIMH = LCD_TIMH_PPL((config->ppl / 16U) - 1U) | LCD_TIMH_HSW(config->hsw - 1U) |
  202. LCD_TIMH_HFP(config->hfp - 1U) | LCD_TIMH_HBP(config->hbp - 1U);
  203. base->TIMV = LCD_TIMV_LPP(config->lpp - 1U) | LCD_TIMV_VSW(config->vsw - 1U) | LCD_TIMV_VFP(config->vfp - 1U) |
  204. LCD_TIMV_VBP(config->vbp - 1U);
  205. base->POL = (uint32_t)(config->polarityFlags) | LCD_POL_ACB(config->acBiasFreq - 1U) | divider;
  206. /* Line end configuration. */
  207. if (config->enableLineEnd)
  208. {
  209. base->LE = LCD_LE_LED(config->lineEndDelay - 1U) | LCD_LE_LEE_MASK;
  210. }
  211. else
  212. {
  213. base->LE = 0U;
  214. }
  215. /* Set panel frame base address. */
  216. base->UPBASE = config->upperPanelAddr;
  217. base->LPBASE = config->lowerPanelAddr;
  218. return kStatus_Success;
  219. }
  220. void LCDC_Deinit(LCD_Type *base)
  221. {
  222. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  223. CLOCK_EnableClock(s_lcdClocks[LCDC_GetInstance(base)]);
  224. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  225. }
  226. void LCDC_GetDefaultConfig(lcdc_config_t *config)
  227. {
  228. config->panelClock_Hz = 0U;
  229. config->ppl = 0U;
  230. config->hsw = 0U;
  231. config->hfp = 0U;
  232. config->hbp = 0U;
  233. config->lpp = 0U;
  234. config->vsw = 0U;
  235. config->vfp = 0U;
  236. config->vbp = 0U;
  237. config->acBiasFreq = 1U;
  238. config->polarityFlags = 0U;
  239. config->enableLineEnd = false;
  240. config->lineEndDelay = 0U;
  241. config->upperPanelAddr = 0U;
  242. config->lowerPanelAddr = 0U;
  243. config->bpp = kLCDC_1BPP;
  244. config->dataFormat = kLCDC_LittleEndian;
  245. config->swapRedBlue = false;
  246. config->display = kLCDC_DisplayTFT;
  247. }
  248. void LCDC_SetPanelAddr(LCD_Type *base, lcdc_panel_t panel, uint32_t addr)
  249. {
  250. /* The base address must be doubleword aligned. */
  251. assert((addr & 0x07U) == 0U);
  252. if (kLCDC_UpperPanel == panel)
  253. {
  254. base->UPBASE = addr;
  255. }
  256. else
  257. {
  258. base->LPBASE = addr;
  259. }
  260. }
  261. void LCDC_SetPalette(LCD_Type *base, const uint32_t *palette, uint8_t count_words)
  262. {
  263. assert(count_words <= ARRAY_SIZE(base->PAL));
  264. uint32_t i;
  265. for (i = 0; i < count_words; i++)
  266. {
  267. base->PAL[i] = palette[i];
  268. }
  269. }
  270. void LCDC_EnableInterrupts(LCD_Type *base, uint32_t mask)
  271. {
  272. uint32_t reg;
  273. reg = mask & LCDC_CURSOR_INT_MASK;
  274. if (reg)
  275. {
  276. base->CRSR_INTMSK |= reg;
  277. }
  278. reg = mask & LCDC_NORMAL_INT_MASK;
  279. if (reg)
  280. {
  281. base->INTMSK |= reg;
  282. }
  283. }
  284. void LCDC_DisableInterrupts(LCD_Type *base, uint32_t mask)
  285. {
  286. uint32_t reg;
  287. reg = mask & LCDC_CURSOR_INT_MASK;
  288. if (reg)
  289. {
  290. base->CRSR_INTMSK &= ~reg;
  291. }
  292. reg = mask & LCDC_NORMAL_INT_MASK;
  293. if (reg)
  294. {
  295. base->INTMSK &= ~reg;
  296. }
  297. }
  298. uint32_t LCDC_GetInterruptsPendingStatus(LCD_Type *base)
  299. {
  300. uint32_t reg;
  301. reg = base->CRSR_INTRAW;
  302. reg |= base->INTRAW;
  303. return reg;
  304. }
  305. uint32_t LCDC_GetEnabledInterruptsPendingStatus(LCD_Type *base)
  306. {
  307. uint32_t reg;
  308. reg = base->CRSR_INTSTAT;
  309. reg |= base->INTSTAT;
  310. return reg;
  311. }
  312. void LCDC_ClearInterruptsStatus(LCD_Type *base, uint32_t mask)
  313. {
  314. uint32_t reg;
  315. reg = mask & LCDC_CURSOR_INT_MASK;
  316. if (reg)
  317. {
  318. base->CRSR_INTCLR = reg;
  319. }
  320. reg = mask & LCDC_NORMAL_INT_MASK;
  321. if (reg)
  322. {
  323. base->INTCLR = reg;
  324. }
  325. }
  326. void LCDC_SetCursorConfig(LCD_Type *base, const lcdc_cursor_config_t *config)
  327. {
  328. assert(config);
  329. uint32_t i;
  330. base->CRSR_CFG = LCD_CRSR_CFG_CRSRSIZE(config->size) | LCD_CRSR_CFG_FRAMESYNC(config->syncMode);
  331. /* Set position. */
  332. LCDC_SetCursorPosition(base, 0, 0);
  333. /* Palette. */
  334. base->CRSR_PAL0 = ((uint32_t)config->palette0.red << LCD_CRSR_PAL0_RED_SHIFT) |
  335. ((uint32_t)config->palette0.blue << LCD_CRSR_PAL0_BLUE_SHIFT) |
  336. ((uint32_t)config->palette0.green << LCD_CRSR_PAL0_GREEN_SHIFT);
  337. base->CRSR_PAL1 = ((uint32_t)config->palette1.red << LCD_CRSR_PAL1_RED_SHIFT) |
  338. ((uint32_t)config->palette1.blue << LCD_CRSR_PAL1_BLUE_SHIFT) |
  339. ((uint32_t)config->palette1.green << LCD_CRSR_PAL1_GREEN_SHIFT);
  340. /* Image of cursors. */
  341. if (kLCDC_CursorSize64 == config->size)
  342. {
  343. assert(config->image[0]);
  344. LCDC_SetCursorImage(base, config->size, 0, config->image[0]);
  345. }
  346. else
  347. {
  348. for (i = 0; i < LCDC_CURSOR_COUNT; i++)
  349. {
  350. if (config->image[i])
  351. {
  352. LCDC_SetCursorImage(base, config->size, i, config->image[i]);
  353. }
  354. }
  355. }
  356. }
  357. void LCDC_CursorGetDefaultConfig(lcdc_cursor_config_t *config)
  358. {
  359. uint32_t i;
  360. config->size = kLCDC_CursorSize32;
  361. config->syncMode = kLCDC_CursorAsync;
  362. config->palette0.red = 0U;
  363. config->palette0.green = 0U;
  364. config->palette0.blue = 0U;
  365. config->palette1.red = 255U;
  366. config->palette1.green = 255U;
  367. config->palette1.blue = 255U;
  368. for (i = 0; i < LCDC_CURSOR_COUNT; i++)
  369. {
  370. config->image[i] = (uint32_t *)0;
  371. }
  372. }
  373. void LCDC_SetCursorPosition(LCD_Type *base, int32_t positionX, int32_t positionY)
  374. {
  375. uint32_t clipX;
  376. uint32_t clipY;
  377. if (positionX < 0)
  378. {
  379. clipX = -positionX;
  380. positionX = 0U;
  381. /* If clip value too large, set to the max value. */
  382. if (clipX > LCDC_CLIP_MAX)
  383. {
  384. clipX = LCDC_CLIP_MAX;
  385. }
  386. }
  387. else
  388. {
  389. clipX = 0U;
  390. }
  391. if (positionY < 0)
  392. {
  393. clipY = -positionY;
  394. positionY = 0U;
  395. /* If clip value too large, set to the max value. */
  396. if (clipY > LCDC_CLIP_MAX)
  397. {
  398. clipY = LCDC_CLIP_MAX;
  399. }
  400. }
  401. else
  402. {
  403. clipY = 0U;
  404. }
  405. base->CRSR_CLIP = LCD_CRSR_CLIP_CRSRCLIPX(clipX) | LCD_CRSR_CLIP_CRSRCLIPY(clipY);
  406. base->CRSR_XY = LCD_CRSR_XY_CRSRX(positionX) | LCD_CRSR_XY_CRSRY(positionY);
  407. }
  408. void LCDC_SetCursorImage(LCD_Type *base, lcdc_cursor_size_t size, uint8_t index, const uint32_t *image)
  409. {
  410. uint32_t regStart;
  411. uint32_t i;
  412. uint32_t len;
  413. if (kLCDC_CursorSize64 == size)
  414. {
  415. regStart = 0U;
  416. len = LCDC_CURSOR_IMG_64X64_WORDS;
  417. }
  418. else
  419. {
  420. regStart = index * LCDC_CURSOR_IMG_32X32_WORDS;
  421. len = LCDC_CURSOR_IMG_32X32_WORDS;
  422. }
  423. for (i = 0U; i < len; i++)
  424. {
  425. base->CRSR_IMG[regStart + i] = image[i];
  426. }
  427. }