1 /*
2  * Copyright 2019, 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_DBI_H_
9 #define _FSL_DBI_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup dbi
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 /* Use the legacy way */
22 #ifndef MCUX_DBI_LEGACY
23 #define MCUX_DBI_LEGACY 1
24 #endif
25 
26 #ifndef MCUX_DBI_IFACE_ENABLE_READ
27 #define MCUX_DBI_IFACE_ENABLE_READ 0
28 #endif
29 
30 /*!
31  * @brief MIPI DBI command, same with the MIPI DSC (Display Command Set)
32  */
33 enum _mipi_dbi_cmd
34 {
35     kMIPI_DBI_Nop                  = 0x00u,
36     kMIPI_DBI_SoftReset            = 0x01u,
37     kMIPI_DBI_GetRedChannel        = 0x06u,
38     kMIPI_DBI_GetGreenChannel      = 0x07u,
39     kMIPI_DBI_GetBlueChannel       = 0x08u,
40     kMIPI_DBI_GetPowerMode         = 0x0Au,
41     kMIPI_DBI_GetAddressMode       = 0x0Bu,
42     kMIPI_DBI_GetPixelFormat       = 0x0Cu,
43     kMIPI_DBI_GetDisplayMode       = 0x0Du,
44     kMIPI_DBI_GetSignalMode        = 0x0Eu,
45     kMIPI_DBI_GetDiagnosticResult  = 0x0Fu,
46     kMIPI_DBI_EnterSleepMode       = 0x10u,
47     kMIPI_DBI_ExitSleepMode        = 0x11u,
48     kMIPI_DBI_EnterPartialMode     = 0x12u,
49     kMIPI_DBI_EnterNormalMode      = 0x13u,
50     kMIPI_DBI_ExitInvertMode       = 0x20u,
51     kMIPI_DBI_EnterInvertMode      = 0x21u,
52     kMIPI_DBI_SetGammaCurve        = 0x26u,
53     kMIPI_DBI_SetDisplayOff        = 0x28u,
54     kMIPI_DBI_SetDisplayOn         = 0x29u,
55     kMIPI_DBI_SetColumnAddress     = 0x2au,
56     kMIPI_DBI_SetPageAddress       = 0x2bu,
57     kMIPI_DBI_WriteMemoryStart     = 0x2Cu,
58     kMIPI_DBI_WriteLUT             = 0x2Du,
59     kMIPI_DBI_ReadMemoryStart      = 0x2Eu,
60     kMIPI_DBI_SetPartialRows       = 0x30u,
61     kMIPI_DBI_SetPartialColumns    = 0x31u,
62     kMIPI_DBI_SetScrollArea        = 0x33u,
63     kMIPI_DBI_SetTearOff           = 0x34u,
64     kMIPI_DBI_SetTearOn            = 0x35u,
65     kMIPI_DBI_SetAddressMode       = 0x36u,
66     kMIPI_DBI_SetScrollStart       = 0x37u,
67     kMIPI_DBI_ExitIdleMode         = 0x38u,
68     kMIPI_DBI_EnterIdleMode        = 0x39u,
69     kMIPI_DBI_SetPixelFormat       = 0x3Au,
70     kMIPI_DBI_WriteMemoryContinue  = 0x3Cu,
71     kMIPI_DBI_Set3DControl         = 0x3Du,
72     kMIPI_DBI_ReadMemoryContinue   = 0x3Eu,
73     kMIPI_DBI_Get3DControl         = 0x3Fu,
74     kMIPI_DBI_SetVsyncTiming       = 0x40u,
75     kMIPI_DBI_SetTearScanline      = 0x44u,
76     kMIPI_DBI_GetScanline          = 0x45u,
77     kMIPI_DBI_SetDisplayBrightness = 0x51u,
78     kMIPI_DBI_GetDisplayBrightness = 0x52u,
79     kMIPI_DBI_WriteControlDisplay  = 0x53u,
80     kMIPI_DBI_GetControlDisplay    = 0x54u,
81     kMIPI_DBI_WritePowerSave       = 0x55u,
82     kMIPI_DBI_GetPowerSave         = 0x56u,
83     kMIPI_DBI_SetCABCMinBrightness = 0x5Eu,
84     kMIPI_DBI_GetCABCMinBrightness = 0x5Fu,
85     kMIPI_DBI_ReadDDBStart         = 0xA1u,
86     kMIPI_DBI_ReadDDBContinue      = 0xA8u,
87 };
88 
89 /*!
90  * @brief Callback function when the writeMemory or readMemory finished.
91  *
92  * If transfer done successfully, the @p status is kStatus_Success.
93  */
94 typedef void (*dbi_mem_done_callback_t)(status_t status, void *userData);
95 
96 #if MCUX_DBI_LEGACY
97 /*!
98  * @brief DBI interface (MCU LCD) transfer operation.
99  *
100  * The API @ref writeCommand and @ref writeData are blocking method, they returns
101  * only when transfer finished. They are usually used to transfer small data, for
102  * example, sending the cofigurations.
103  *
104  * The API @ref writeMemory and @ref readMemory are non-blocking method, they are
105  * used to write or read the LCD contoller video memory. These APIs start transfer
106  * and return directly, upper layer could be notified by callback when transfer
107  * done. The callback function is set by @ref setMemoryDoneCallback.
108  */
109 typedef struct _dbi_xfer_ops
110 {
111     status_t (*writeCommand)(void *dbiXferHandle, uint32_t command);           /*!< Write command. */
112     status_t (*writeData)(void *dbiXferHandle, void *data, uint32_t len_byte); /*!< Write data. */
113     status_t (*writeCommandData)(void *dbiXferHandle,
114                                  uint32_t command,
115                                  const void *data,
116                                  uint32_t len_byte); /*!< Write command and data in blocking way. */
117     status_t (*writeMemory)(void *dbiXferHandle,
118                             uint32_t command,
119                             const void *data,
120                             uint32_t len_byte); /*!< Write to the memory. */
121     status_t (*readMemory)(void *dbiXferHandle,
122                            uint32_t command,
123                            void *data,
124                            uint32_t len_byte); /*!< Read from the memory. */
125     void (*setMemoryDoneCallback)(void *dbiXferHandle,
126                                   dbi_mem_done_callback_t callback,
127                                   void *userData); /*!< Set the memory access done callback. */
128 } dbi_xfer_ops_t;
129 
130 #else /* not MCUX_DBI_LEGACY */
131 
132 /* Define a function pointer prototype to toggle D/C pin. */
133 typedef void (*dbi_dc_pin_func_t)(bool high);
134 
135 typedef struct _dbi_iface dbi_iface_t;
136 
137 /*!
138  * @brief DBI interface (MCU LCD) transfer operation.
139  *
140  * The API @ref writeCommandData and @ref readData are blocking method, they returns
141  * only when transfer finished. They are usually used to transfer small data, for
142  * example, sending the cofigurations, read LCD controller ID to verify the connection.
143  *
144  * The API @ref writeMemory is non-blocking method, it is to write the LCD contoller
145  * video memory(or graphics memory, GRAM). These APIs start transfer
146  * and return directly, upper layer could be notified by callback when transfer
147  * done. The callback function is set by @ref setMemoryDoneCallback.
148  */
149 typedef struct _dbi_iface_xfer_ops
150 {
151     status_t (*writeCommandData)(dbi_iface_t *dbiIface,
152                                  uint8_t command,
153                                  const void *data,
154                                  uint32_t len_byte); /*!< Write command and data in blocking way. MUST Have */
155 
156 #if MCUX_DBI_IFACE_ENABLE_READ
157     status_t (*readData)(dbi_iface_t *dbiIface,
158                          uint8_t command,
159                          void *data,
160                          uint32_t len_byte); /*!< Read data from LCD controller in blocking way. It can be
161                          used to read ID of LCD controller. Optional. */
162 #endif /* MCUX_DBI_IFACE_ENABLE_READ */
163 
164     status_t (*writeMemory)(dbi_iface_t *dbiIface,
165                             uint8_t command,
166                             const void *data,
167                             uint32_t len_byte); /*!< Write to the memory. MUST Have */
168 } dbi_iface_xfer_ops_t;
169 
170 /*@brief DBI transfer interface.*/
171 struct _dbi_iface
172 {
173     const dbi_iface_xfer_ops_t *xferOps;     /*!< Pointer to the DBI transfer operations. */
174     void *prvData;                           /*!< DBI interface specific private data. */
175     dbi_mem_done_callback_t memDoneCallback; /*!< The callback function when video memory access done. */
176     void *memDoneCallbackParam;              /*!< Parameter of @ref memDoneCallback */
177 };
178 
179 #endif /* MCUX_DBI_LEGACY */
180 
181 /*******************************************************************************
182  * API
183  ******************************************************************************/
184 #if defined(__cplusplus)
185 extern "C" {
186 #endif
187 
188 #if !MCUX_DBI_LEGACY
189 /*!
190  * @brief Write command and data to the LCD controller.
191  *
192  * @param[in] dbiIface Pointer to the DBI device.
193  * @param[in] command The command to be sent.
194  * @param[in] data The data to be sent.
195  * @param[in] length The length of the data to be sent.
196  * @return Execution status.
197  */
DBI_IFACE_WriteCmdData(dbi_iface_t * dbiIface,uint8_t cmd,const uint8_t * data,uint32_t length)198 static inline status_t DBI_IFACE_WriteCmdData(dbi_iface_t *dbiIface, uint8_t cmd, const uint8_t *data, uint32_t length)
199 {
200     return dbiIface->xferOps->writeCommandData(dbiIface, cmd, data, length);
201 }
202 
203 #if MCUX_DBI_IFACE_ENABLE_READ
204 /*!
205  * @brief Read data from the LCD controller.
206  *
207  * @param[in] dbiIface Pointer to the DBI interface.
208  * @param[in] command The command to be sent.
209  * @param[out] data The data to be read.
210  * @param[in] length The length of the data to be read.
211  * @return Execution status.
212  */
DBI_IFACE_ReadData(dbi_iface_t * dbiIface,uint8_t cmd,uint8_t * data,uint32_t length)213 static inline status_t DBI_IFACE_ReadData(dbi_iface_t *dbiIface, uint8_t cmd, uint8_t *data, uint32_t length)
214 {
215     return dbiIface->xferOps->readData(dbiIface, cmd, data, length);
216 }
217 #endif
218 
219 /*!
220  * @brief Soft reset the LCD controller.
221  *
222  * @param[in] dbiIface Pointer to the DBI interface.
223  * @return Execution status.
224  */
DBI_IFACE_SoftReset(dbi_iface_t * dbiIface)225 static inline status_t DBI_IFACE_SoftReset(dbi_iface_t *dbiIface)
226 {
227     return dbiIface->xferOps->writeCommandData(dbiIface, kMIPI_DBI_SoftReset, NULL, 0);
228 }
229 
230 /*!
231  * @brief Set the display on of off.
232  *
233  * @param[in] dbiIface Pointer to the DBI interface.
234  * @param[in] on True for display on, false for display off.
235  */
DBI_IFACE_SetDiplayOn(dbi_iface_t * dbiIface,bool on)236 static inline status_t DBI_IFACE_SetDiplayOn(dbi_iface_t *dbiIface, bool on)
237 {
238     uint8_t cmd = (on ? (uint8_t)kMIPI_DBI_SetDisplayOn : (uint8_t)kMIPI_DBI_SetDisplayOff);
239 
240     return dbiIface->xferOps->writeCommandData(dbiIface, cmd, NULL, 0);
241 }
242 
243 /*!
244  * @brief Select the area to update next.
245  *
246  * @param[in] dbiIface Pointer to the DBI interface.
247  * @param[in] startX The start X position of the area.
248  * @param[in] startY The start Y position of the area.
249  * @param[in] endX The end X position of the area.
250  * @param[in] endY The end Y position of the area.
251  * @return Execution status.
252  */
253 status_t DBI_IFACE_SelectArea(dbi_iface_t *dbiIface, uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY);
254 
255 /*!
256  * @brief Write data to the LCD controller video memory.
257  *
258  * @param[in] dbiIface Pointer to the DBI device.
259  * @param[in] data The data to be sent.
260  * @param[in] length The length of the data to be sent.
261  * @return Execution status.
262  * @note This function is non-blocking, upper layer could be notified by callback when transfer done.
263  */
DBI_IFACE_WriteMemory(dbi_iface_t * dbiIface,const uint8_t * data,uint32_t length)264 static inline status_t DBI_IFACE_WriteMemory(dbi_iface_t *dbiIface, const uint8_t *data, uint32_t length)
265 {
266     return dbiIface->xferOps->writeMemory(dbiIface, kMIPI_DBI_WriteMemoryStart, data, length);
267 }
268 
269 /*!
270  * @brief Write data to the LCD controller video memory.
271  *
272  * @param[in] dbiIface Pointer to the DBI device.
273  * @param[in] data The data to be sent.
274  * @param[in] length The length of the data to be sent.
275  * @return Execution status.
276  * @note This function is non-blocking, upper layer could be notified by callback when transfer done.
277  */
DBI_IFACE_WriteMemoryContinue(dbi_iface_t * dbiIface,const uint8_t * data,uint32_t length)278 static inline status_t DBI_IFACE_WriteMemoryContinue(dbi_iface_t *dbiIface, const uint8_t *data, uint32_t length)
279 {
280     return dbiIface->xferOps->writeMemory(dbiIface, kMIPI_DBI_WriteMemoryContinue, data, length);
281 }
282 
283 /*!
284  * @brief Register the callback function called when memory function done.
285  *
286  * The memory write function is non-blocking function, when transaction
287  * finished, callback is called to inform higher layer.
288  *
289  * @param[in] dbiIface Pointer to the DBI device.
290  * @param[in] callback The callback when memory read or write finished.
291  * @param[in] userData Parameter of the callback.
292  */
293 void DBI_IFACE_SetMemoryDoneCallback(dbi_iface_t *dbiIface, dbi_mem_done_callback_t callback, void *userData);
294 #endif /* !MCUX_DBI_LEGACY */
295 
296 #if defined(__cplusplus)
297 }
298 #endif
299 
300 /*! @} */
301 
302 #endif /* _FSL_DBI_H_ */
303