vrmay23 commented on code in PR #18397: URL: https://github.com/apache/nuttx/pull/18397#discussion_r2809982107
########## boards/arm/stm32h7/nucleo-h753zi/src/stm32_st7796.c: ########## @@ -0,0 +1,700 @@ +/**************************************************************************** + * boards/arm/stm32h7/nucleo-h753zi/src/stm32_st7796.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <syslog.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include <nuttx/board.h> +#include <nuttx/spi/spi.h> +#include <nuttx/video/fb.h> +#include <nuttx/lcd/st7796.h> +#include <nuttx/clock.h> +#include <nuttx/signal.h> + +#include "nucleo-h753zi.h" +#include "stm32_gpio.h" +#include "stm32_spi.h" + +#if defined(CONFIG_LCD_ST7796) && defined(CONFIG_NUCLEO_H753ZI_ST7796_ENABLE) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* SPI port selection from Kconfig */ + +#ifdef CONFIG_NUCLEO_H753ZI_ST7796_SPI1 +# define ST7796_SPI_PORTNO 1 +# ifndef CONFIG_STM32H7_SPI1 +# error "ST7796 configured for SPI1 but CONFIG_STM32H7_SPI1 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI1_ENABLE +# error "ST7796 configured for SPI1 but board SPI1 not enabled" +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_SPI2) +# define ST7796_SPI_PORTNO 2 +# ifndef CONFIG_STM32H7_SPI2 +# error "ST7796 configured for SPI2 but CONFIG_STM32H7_SPI2 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI2_ENABLE +# error "ST7796 configured for SPI2 but board SPI2 not enabled" +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_SPI3) +# define ST7796_SPI_PORTNO 3 +# ifndef CONFIG_STM32H7_SPI3 +# error "ST7796 configured for SPI3 but CONFIG_STM32H7_SPI3 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI3_ENABLE +# error "ST7796 configured for SPI3 but board SPI3 not enabled" +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_SPI4) +# define ST7796_SPI_PORTNO 4 +# ifndef CONFIG_STM32H7_SPI4 +# error "ST7796 configured for SPI4 but CONFIG_STM32H7_SPI4 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI4_ENABLE +# error "ST7796 configured for SPI4 but board SPI4 not enabled" +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_SPI5) +# define ST7796_SPI_PORTNO 5 +# ifndef CONFIG_STM32H7_SPI5 +# error "ST7796 configured for SPI5 but CONFIG_STM32H7_SPI5 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI5_ENABLE +# error "ST7796 configured for SPI5 but board SPI5 not enabled" +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_SPI6) +# define ST7796_SPI_PORTNO 6 +# ifndef CONFIG_STM32H7_SPI6 +# error "ST7796 configured for SPI6 but CONFIG_STM32H7_SPI6 not enabled" +# endif +# ifndef CONFIG_NUCLEO_H753ZI_SPI6_ENABLE +# error "ST7796 configured for SPI6 but board SPI6 not enabled" +# endif +#else +# error "No SPI port selected for ST7796" +#endif + +/* Verify CONFIG_SPI_CMDDATA is enabled */ + +#ifndef CONFIG_SPI_CMDDATA +# error "CONFIG_SPI_CMDDATA must be enabled for ST7796 driver" +#endif + +/* Pin configuration from Kconfig with defaults */ + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_CS_PIN +# define CONFIG_NUCLEO_H753ZI_ST7796_CS_PIN "PA4" +#endif + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_DC_PIN +# define CONFIG_NUCLEO_H753ZI_ST7796_DC_PIN "PA3" +#endif + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_RESET_PIN +# define CONFIG_NUCLEO_H753ZI_ST7796_RESET_PIN "PA2" +#endif + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_LED_PIN +# define CONFIG_NUCLEO_H753ZI_ST7796_LED_PIN "PA1" +#endif + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_CS_ACTIVE_LOW +# define CONFIG_NUCLEO_H753ZI_ST7796_CS_ACTIVE_LOW true +#endif + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_DEVID +# define CONFIG_NUCLEO_H753ZI_ST7796_DEVID 0 +#endif + +/* SPI frequency from Kconfig with default */ + +#ifndef CONFIG_NUCLEO_H753ZI_ST7796_FREQUENCY +# define CONFIG_NUCLEO_H753ZI_ST7796_FREQUENCY ST7796_SPI_MAXFREQUENCY +#endif + +/* Reset timing (from ST7796 datasheet) */ + +#define ST7796_RESET_DELAY_MS 10 +#define ST7796_RESET_HOLD_MS 10 +#define ST7796_RESET_RELEASE_MS 120 + +/* GPIO helper macros */ + +#define ST7796_GPIO_CONFIG_MASK 0xffff0000 +#define ST7796_GPIO_IN_FLOAT (GPIO_INPUT | GPIO_FLOAT | GPIO_SPEED_50MHz) + +/* Display resolution based on orientation from Kconfig */ + +#if defined(CONFIG_NUCLEO_H753ZI_ST7796_LANDSCAPE) || \ + defined(CONFIG_NUCLEO_H753ZI_ST7796_RLANDSCAPE) +# define ST7796_XRES ST7796_YRES_RAW +# define ST7796_YRES ST7796_XRES_RAW +#else +# define ST7796_XRES ST7796_XRES_RAW +# define ST7796_YRES ST7796_YRES_RAW +#endif + +/* Base MADCTL value based on orientation and BGR from Kconfig */ + +#if defined(CONFIG_NUCLEO_H753ZI_ST7796_LANDSCAPE) +# ifdef CONFIG_NUCLEO_H753ZI_ST7796_BGR +# define ST7796_MADCTL_BASE ST7796_MADCTL_LANDSCAPE_BGR +# else +# define ST7796_MADCTL_BASE ST7796_MADCTL_LANDSCAPE +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_RPORTRAIT) +# ifdef CONFIG_NUCLEO_H753ZI_ST7796_BGR +# define ST7796_MADCTL_BASE ST7796_MADCTL_RPORTRAIT_BGR +# else +# define ST7796_MADCTL_BASE ST7796_MADCTL_RPORTRAIT +# endif +#elif defined(CONFIG_NUCLEO_H753ZI_ST7796_RLANDSCAPE) +# ifdef CONFIG_NUCLEO_H753ZI_ST7796_BGR +# define ST7796_MADCTL_BASE ST7796_MADCTL_RLANDSCAPE_BGR +# else +# define ST7796_MADCTL_BASE ST7796_MADCTL_RLANDSCAPE +# endif +#else /* Portrait (default) */ +# ifdef CONFIG_NUCLEO_H753ZI_ST7796_BGR +# define ST7796_MADCTL_BASE ST7796_MADCTL_PORTRAIT_BGR +# else +# define ST7796_MADCTL_BASE ST7796_MADCTL_PORTRAIT +# endif +#endif + +/* Initial rotation from Kconfig */ + +#ifdef CONFIG_NUCLEO_H753ZI_ST7796_ROTATION_180 +# define ST7796_INIT_ROTATION 180 +#else +# define ST7796_INIT_ROTATION 0 +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static uint32_t g_reset_pin; +static uint32_t g_led_pin; +static bool g_st7796_initialized = false; +static FAR struct spi_dev_s *g_spi_dev = NULL; +static FAR struct fb_vtable_s *g_fb_vtable = NULL; + +/* Board-specific configuration passed to generic driver */ + +static struct st7796_config_s g_st7796_config = +{ + .frequency = CONFIG_NUCLEO_H753ZI_ST7796_FREQUENCY, + .xres = ST7796_XRES, + .yres = ST7796_YRES, + .bpp = 16, + .madctl = ST7796_MADCTL_BASE /* 0x28 -> will fix it */, + .rotation = ST7796_INIT_ROTATION, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: parse_gpio_pin + * + * Description: + * Parse GPIO pin string like "PA0" into STM32 GPIO configuration. + * + ****************************************************************************/ + +static uint32_t parse_gpio_pin(FAR const char *pin_str, FAR int *error) Review Comment: Why not? I did not follow in nuttx documentation any place is telling it is wrong. could you explain it better please? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
