1 /*
2  * Copyright 2023-2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_ST7796S_H_
9 #define _FSL_ST7796S_H_
10 
11 #include <stdbool.h>
12 
13 #include "fsl_dbi.h"
14 
15 /*!
16  * @addtogroup st7796s
17  * @{
18  */
19 
20 /*
21  * Change log:
22  *
23  *   1.0.0
24  *     - Initial version
25  */
26 
27 /*******************************************************************************
28  * Definitions
29  ******************************************************************************/
30 
31 /*! @brief Panel driver preset. */
32 typedef enum _st7796s_driver_preset
33 {
34     kST7796S_DriverPresetLCDPARS035, /*!< Driving preset for LCD-PAR-S035. */
35 } st7796s_driver_preset_t;
36 
37 /*! @brief Display Orientation. */
38 typedef enum _st7796s_orientation_mode
39 {
40     kST7796S_Orientation0   = 0x08U, /*!< Rotation 0 degrees */
41     kST7796S_Orientation90  = 0x68U, /*!< Rotation 90 degrees */
42     kST7796S_Orientation180 = 0xC8U, /*!< Rotation 180 degrees */
43     kST7796S_Orientation270 = 0xA8U, /*!< Rotation 270 degrees */
44 } st7796s_orientation_mode_t;
45 
46 /*! @brief Pixel format, does not affect interface width */
47 typedef enum _st7796s_pixel_format
48 {
49     kST7796S_PixelFormatRGB565 = 5U, /*!< Pixel format RGB565, 16bits per pixel */
50     kST7796S_PixelFormatRGB666 = 6U, /*!< Pixel format RGB666, 18bits per pixel */
51     kST7796S_PixelFormatRGB888 = 7U, /*!< Pixel format RGB888, 24bits per pixel, internally dithered to 18bits */
52     kST7796S_PixelFormatRGB444 = 3U, /*!< Pixel format RGB444, 12bits per pixel */
53 } st7796s_pixel_format_t;
54 
55 /*! @brief TE (tearing effect) output configuration */
56 typedef enum _st7796s_te_config
57 {
58     kST7796S_TEDisabled, /*!< TE output disabled (no output) */
59     kST7796s_TEVSync,    /*!< TE output enabled on VSync */
60     kST7796S_TEHVSync,   /*!< TE output enabled on both HSync and VSync */
61 } st7796s_te_config_t;
62 
63 /*! @brief Panel configuration structure */
64 typedef struct _st7796s_config
65 {
66     st7796s_driver_preset_t driverPreset;       /*!< Driver configuration preset */
67     st7796s_pixel_format_t pixelFormat;         /*!< Color format */
68     st7796s_orientation_mode_t orientationMode; /*!< Display orientation */
69     st7796s_te_config_t teConfig;               /*!< TE line configuration */
70     bool invertDisplay;                         /*!< Invert display color */
71     bool flipDisplay;                           /*!< Mirror display image */
72     bool bgrFilter;                             /*!< Use BGR color filter instead of RGB */
73 } st7796s_config_t;
74 
75 /*! @brief ST7796S driver handle structure */
76 typedef struct _st7796s_handle
77 {
78 #if MCUX_DBI_LEGACY
79     const dbi_xfer_ops_t *xferOps; /*!< Bus transfer operations. */
80     void *xferOpsData;             /*!< Data used for transfer operations. */
81 #else
82     dbi_iface_t *dbiIface; /*!< DBI interface handle. */
83 #endif
84     st7796s_orientation_mode_t orientationMode; /*!< Current orientation mode */
85 } st7796s_handle_t;
86 
87 /*******************************************************************************
88  * API
89  ******************************************************************************/
90 
91 #if defined(__cplusplus)
92 extern "C" {
93 #endif
94 
95 #if MCUX_DBI_LEGACY
96 /*!
97  * @brief Initialize ST7796S controller.
98  *
99  * This function performs a software reset and initialize the display driver using
100  * a preset configuration. This function does not turn on the display, application
101  * could perform the @ref ST7796S_EnableDisplay later after filling the frame buffer.
102  *
103  * @param handle ST7796S handle structure.
104  * @param config Pointer to the panel configuration structure.
105  * @param xferOps DBI interface transfer operation functions.
106  * @param xferOpsData Private data used by the DBI interface.
107  *
108  * @return This function returns kStatus_Success after successfully initialized.
109  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
110  *         Please refer to the corresponding modules for an specific error coding.
111  */
112 status_t ST7796S_Init(st7796s_handle_t *handle,
113                       const st7796s_config_t *config,
114                       const dbi_xfer_ops_t *xferOps,
115                       void *xferOpsData);
116 #else
117 /*!
118  * @brief Initialize ST7796S controller.
119  *
120  * This function performs a software reset and initialize the display driver using
121  * a preset configuration. This function does not turn on the display, application
122  * could perform the @ref ST7796S_EnableDisplay later after filling the frame buffer.
123  *
124  * @param handle ST7796S handle structure.
125  * @param config Pointer to the panel configuration structure.
126  * @param dbiIface DBI interface handle.
127  *
128  * @return This function returns kStatus_Success after successfully initialized.
129  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
130  *         Please refer to the corresponding modules for an specific error coding.
131  */
132 status_t ST7796S_Init(st7796s_handle_t *handle, const st7796s_config_t *config, dbi_iface_t *dbiIface);
133 #endif
134 
135 /*!
136  * @brief Invert or restore display color.
137  *
138  * This function invert the display color to the panel. Invert is also related
139  * to the panel technology, for instance, an IPS panel will need the display color
140  * to be inverted, while traditional TFT panels will not.
141  *
142  * @param handle ST7796S handle structure.
143  * @param invert Whether to invert the display color.
144  *
145  * @return This function returns kStatus_Success after successfully executed.
146  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
147  *         Please refer to the corresponding modules for an specific error coding.
148  */
149 status_t ST7796S_InvertDisplay(st7796s_handle_t *handle, bool invert);
150 
151 /*!
152  * @brief Enable or disable the display output.
153  *
154  * This function enables or disables the panel driving output. When driving
155  * output is enabled, the panel will scanning out the frame buffer content.
156  * When driving output is disabled, the panel will display a blank screen
157  * depending the panel technology (un-driven state).
158  *
159  * @param handle ST7796S handle structure.
160  * @param enable Whether to enable driving output
161  *
162  * @return This function returns kStatus_Success after successfully executed.
163  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
164  *         Please refer to the corresponding modules for an specific error coding.
165  */
166 status_t ST7796S_EnableDisplay(st7796s_handle_t *handle, bool enable);
167 
168 /*!
169  * @brief Set display pixel format.
170  *
171  * This function set the pixel format of the controller. The controller supports one of the
172  * pixel formats defined in @ref st7796s_pixel_format_t. This does not affect the hardware interface selection.
173  *
174  * @param handle ST7796S handle structure.
175  * @param pixelFormat Pixel format to be set
176  *
177  * @return This function returns kStatus_Success after successfully executed.
178  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
179  *         Please refer to the corresponding modules for an specific error coding.
180  */
181 status_t ST7796S_SetPixelFormat(st7796s_handle_t *handle, st7796s_pixel_format_t pixelFormat);
182 
183 /*!
184  * @brief Configure the Tearing Effect (TE) line output.
185  *
186  * This function configures the TE line output to one of the following states:
187  * Disabled: No TE output
188  * VSync: TE asserts on the V-blanking periods only
189  * HVSync: TE asserts on both H-blanking periods and V-blanking periods
190  * Refer to @ref st7796s_te_config_t.
191  *
192  * @param handle ST7796S handle structure.
193  * @param teConfig TE line configuration to be set.
194  *
195  * @return This function returns kStatus_Success after successfully executed.
196  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
197  *         Please refer to the corresponding modules for an specific error coding.
198  */
199 status_t ST7796S_SetTEConfig(st7796s_handle_t *handle, st7796s_te_config_t teConfig);
200 
201 /*!
202  * @brief Set panel configurations.
203  *
204  * This function configures some vital parameters of the display panel. Please refer to
205  * @ref st7796s_config_t for available configuration items.
206  *
207  * @param handle ST7796S handle structure.
208  * @param config Pointer to the configuration structure.
209  *
210  * @return This function returns kStatus_Success after successfully executed.
211  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
212  *         Please refer to the corresponding modules for an specific error coding.
213  */
214 status_t ST7796S_Config(st7796s_handle_t *handle, const st7796s_config_t *config);
215 
216 /*!
217  * @brief Select draw area of the frame buffer.
218  *
219  * This function will set the internal write area to the rectangle specified by the coordinate.
220  * After the write area is set, pixels will be stored start from the top left of the area, and
221  * will be wrapped back to top left if more pixels are written to the frame memory. This function
222  * internally reset the write pointer to the start point.
223  *
224  * @param handle ST7796S handle structure.
225  * @param startX X start point
226  * @param startY Y start point
227  * @param endX X end point
228  * @param endY Y end point
229  *
230  * @return This function returns kStatus_Success after successfully executed.
231  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
232  *         Please refer to the corresponding modules for an specific error coding.
233  */
234 status_t ST7796S_SelectArea(st7796s_handle_t *handle, uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY);
235 
236 /*!
237  * @brief Write pixels to LCD frame buffer
238  *
239  * This function will perform a write to the internal frame buffer on the ST7796S controller. The start and end points
240  * are provided by previous call to the ST7796S, and will self-increment after each write. This function will return
241  * immediately after called, and user has to set a callback using @ref ST7796S_SetMemoryDoneCallback to check the
242  * result and get notified when the background DMA operation is completed.
243  *
244  * @param handle ST7796S handle structure.
245  * @param pixels buffer to be updated to the controller
246  * @param len pixel count.
247  *
248  * @return This function returns kStatus_Success after successfully executed.
249  *         Appropriate kStatus_ prefixed errors will be given when an error occurred.
250  *         Please refer to the corresponding modules for an specific error coding.
251  */
252 status_t ST7796S_WritePixels(st7796s_handle_t *handle, uint16_t *pixels, uint32_t len);
253 
254 #if MCUX_DBI_LEGACY
255 /*!
256  * @brief Set the memory access done callback.
257  *
258  * @param handle ST7796S handle structure.
259  * @param callback Callback function when the video memory operation finished.
260  *        @ref ST7796S_WritePixels is non-blocking function, upper layer is notified
261  *        by this callback function after transfer done.
262  * @param userData Parameter of @ref dbi_mem_done_callback_t.
263  */
264 void ST7796S_SetMemoryDoneCallback(st7796s_handle_t *handle, dbi_mem_done_callback_t callback, void *userData);
265 #endif
266 
267 #if defined(__cplusplus)
268 }
269 #endif
270 
271 /*! @} */
272 
273 #endif // _FSL_ST7796S_H_
274