drivers/lcd: Refactor ST7796 to use Kconfig and add Power Management

Previously, the ST7796 driver required the board to pass a struct
st7796_config_s at initialization time, carrying display resolution,
color depth, orientation (MADCTL), BGR flag, SPI frequency, and
initial rotation.

All of these are chip-level properties, not board wiring details.
This created an awkward split: board code computed MADCTL values,
selected BPP, and derived effective resolution depending on orientation
(knowledge that belongs exclusively in the generic driver).

This commit removes that split entirely, following the same pattern used
by the ST7789 driver in drivers/lcd/Kconfig. It also implements the
getpower/setpower interface for standard framebuffer power management.

What was changed (per file):

drivers/lcd/Kconfig:
- Added a full 'if LCD_ST7796' ... 'endif # LCD_ST7796' block containing
  all chip-level configuration.
- Includes LCD_ST7796_XRES/YRES, LCD_ST7796_BPP, LCD_ST7796_FREQUENCY,
  LCD_ST7796_SPIMODE, LCD_ST7796_BGR, LCD_ST7796_INVCOLOR,
  and orientation/rotation choices.

drivers/lcd/st7796.c:
- Removed struct st7796_config_s from struct st7796_dev_s.
- Moved struct st7796_cmd_s from public header to Private Types.
- Added compile-time macros derived from Kconfig:
  ST7796_XRES/YRES (swapped for landscape),
  ST7796_MADCTL_BASE,
  ST7796_SPIMODE,
  ST7796_BYTESPP,
  ST7796_COLORFMT.

- Implemented st7796_getpower() and st7796_setpower() using the
  st7796_board_power() board callback.
- Registered power functions in st7796_fbinitialize() vtable.
- Updated st7796_init_display() to make INVON/INVOFF conditional on
  CONFIG_LCD_ST7796_INVCOLOR.
- Simplified st7796_fbinitialize() signature to only take
  'struct spi_dev_s *spi'.

include/nuttx/lcd/st7796.h:
- Removed legacy macros (ST7796_XRES_RAW, etc.) and configuration
  structures.
- Updated st7796_fbinitialize() prototype.
- Added st7796_board_power() callback prototype.
- Removed private implementation details from the public header.

boards/arm/stm32h7/nucleo-h753zi/Kconfig:
- Removed chip-level menus (settings, orientation) now handled by the
  generic driver.
- Retained only board-specific wiring concerns (pins and SPI bus).

boards/.../stm32_st7796.c:
- Removed g_st7796_config and associated board-side macros.
- Updated st7796_fbinitialize() call to the new signature.
- stm32_st7796_flush_fb() now queries effective resolution
  via g_fb_vtable->getvideoinfo().

Verified on nucleo-h753zi:
Orientation: Landscape (480x320),
Rotation: 180 deg,
BGR: enabled,
INVCOLOR: disabled,
BPP: RGB565,
Frequency: 40 MHz.
Making INVCOLOR a Kconfig option (default n) fixes cases where panels
previously showed inverted colors due to hardcoded INVON.

Signed-off-by: vrmay23 <vmay.sweden@gmail.com>
This commit is contained in:
vrmay23
2026-03-07 17:54:56 +01:00
committed by Alan C. Assis
parent 8ddcf81484
commit 45651ea6e5
3 changed files with 367 additions and 138 deletions
+23 -68
View File
@@ -31,71 +31,12 @@
#include <stdint.h>
#include <stdbool.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Display raw dimensions (before orientation transform)
*
* These must be in the public header because board code needs them to
* calculate the effective xres/yres based on Kconfig orientation settings
* at compile-time. Without these, boards would need to hardcode dimensions.
*/
#define ST7796_XRES_RAW 320
#define ST7796_YRES_RAW 480
/* Default SPI frequency
*
* Must be in the public header because board code uses this as a safe
* default when the user hasn't specified a custom frequency via Kconfig.
* Centralizes the datasheet specification to prevent duplication.
*/
#define ST7796_SPI_MAXFREQUENCY 40000000
/* Pre-defined MADCTL values for common orientations
*
* These must be in the public header because board code selects the
* appropriate MADCTL value based on hardware orientation and RGB/BGR
* panel type configured via Kconfig. This abstracts the internal MADCTL
* bit manipulation from board code. Values are absolute to eliminate
* dependency on internal bit definitions.
*/
#define ST7796_MADCTL_PORTRAIT 0x40
#define ST7796_MADCTL_PORTRAIT_BGR 0x48
#define ST7796_MADCTL_RPORTRAIT 0x80
#define ST7796_MADCTL_RPORTRAIT_BGR 0x88
#define ST7796_MADCTL_LANDSCAPE 0x20
#define ST7796_MADCTL_LANDSCAPE_BGR 0x28
#define ST7796_MADCTL_RLANDSCAPE 0xe0
#define ST7796_MADCTL_RLANDSCAPE_BGR 0xe8
/****************************************************************************
* Public Types
****************************************************************************/
#ifndef __ASSEMBLY__
/* Board-specific configuration passed to driver at initialization.
*
* This structure must be in the public header because it defines the
* driver's configuration API. Board code instantiates and populates this
* structure to configure the driver without requiring board-specific
* Kconfig options in the generic driver.
*/
struct st7796_config_s
{
uint32_t frequency; /* SPI clock frequency in Hz */
uint16_t xres; /* Horizontal resolution (after orientation) */
uint16_t yres; /* Vertical resolution (after orientation) */
uint16_t rotation; /* Initial rotation: 0, 90, 180, or 270 */
uint8_t bpp; /* Bits per pixel: 16 (RGB565) or 18 (RGB666) */
uint8_t madctl; /* Base MADCTL register value for orientation */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
@@ -111,14 +52,14 @@ extern "C"
* Description:
* Initialize the ST7796 LCD controller as a framebuffer device.
*
* This function initializes the ST7796 display controller using the
* provided configuration and returns a framebuffer virtual table.
* The driver uses CONFIG_SPI_CMDDATA to control the DC (Data/Command)
* pin automatically via the SPI driver.
* This function initializes the ST7796 display controller and returns
* a framebuffer virtual table. Display parameters (resolution, color
* depth, orientation, frequency) are taken from CONFIG_LCD_ST7796_*
* Kconfig options. The driver uses CONFIG_SPI_CMDDATA to control the
* DC (Data/Command) pin automatically via the SPI driver.
*
* Input Parameters:
* spi - SPI device instance configured for the ST7796
* config - Board-specific configuration (frequency, resolution, etc.)
* spi - SPI device instance configured for the ST7796
*
* Returned Value:
* Pointer to framebuffer vtable on success; NULL on failure.
@@ -131,9 +72,7 @@ extern "C"
*
****************************************************************************/
FAR struct fb_vtable_s *st7796_fbinitialize(FAR struct spi_dev_s *spi,
FAR const struct st7796_config_s
*config);
FAR struct fb_vtable_s *st7796_fbinitialize(FAR struct spi_dev_s *spi);
/****************************************************************************
* Name: st7796_setrotation
@@ -170,6 +109,22 @@ int st7796_setrotation(FAR struct fb_vtable_s *vtable, uint16_t rotation);
uint16_t st7796_getrotation(FAR struct fb_vtable_s *vtable);
/****************************************************************************
* Name: st7796_board_power
*
* Description:
* Board-level callback to control display backlight/power.
* Called by the ST7796 driver when FBIOSET_POWER ioctl is
* received. The board must implement this function to
* control the backlight GPIO pin.
*
* Input Parameters:
* on - true to turn backlight on, false to turn off
*
****************************************************************************/
void st7796_board_power(bool on);
#ifdef __cplusplus
}
#endif