From 74b38d3c654b219e33132898e3acf1651790e9f2 Mon Sep 17 00:00:00 2001 From: Sergey Nikitenko Date: Sun, 30 Oct 2022 23:33:30 +0300 Subject: [PATCH] boards/stm32wb/flipperzero: adding LCD support --- boards/arm/stm32wb/flipperzero/README.txt | 4 +- .../stm32wb/flipperzero/configs/nsh/defconfig | 16 ++ .../arm/stm32wb/flipperzero/include/board.h | 17 ++ boards/arm/stm32wb/flipperzero/src/Makefile | 8 + .../arm/stm32wb/flipperzero/src/flipperzero.h | 12 ++ .../stm32wb/flipperzero/src/stm32_appinit.c | 8 + .../arm/stm32wb/flipperzero/src/stm32_boot.c | 11 ++ .../flipperzero/src/stm32_lcd_st7565.c | 185 ++++++++++++++++++ .../arm/stm32wb/flipperzero/src/stm32_spi.c | 101 ++++++++++ 9 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 boards/arm/stm32wb/flipperzero/src/stm32_lcd_st7565.c create mode 100644 boards/arm/stm32wb/flipperzero/src/stm32_spi.c diff --git a/boards/arm/stm32wb/flipperzero/README.txt b/boards/arm/stm32wb/flipperzero/README.txt index 11729de522..215da94432 100644 --- a/boards/arm/stm32wb/flipperzero/README.txt +++ b/boards/arm/stm32wb/flipperzero/README.txt @@ -36,7 +36,7 @@ Device features Status ====== - Oct 2022: initial nsh configuration. + Oct 2022: initial nsh configuration. LCD seems working. Programming =========== @@ -58,3 +58,5 @@ Configurations Configures the NuttShell (nsh) located at examples/nsh. This configuration is focused on low level, command-line driver testing. + + nsh> nxhello diff --git a/boards/arm/stm32wb/flipperzero/configs/nsh/defconfig b/boards/arm/stm32wb/flipperzero/configs/nsh/defconfig index 844afd1713..555f731b08 100644 --- a/boards/arm/stm32wb/flipperzero/configs/nsh/defconfig +++ b/boards/arm/stm32wb/flipperzero/configs/nsh/defconfig @@ -6,6 +6,7 @@ # modifications. # # CONFIG_ARCH_LEDS is not set +# CONFIG_NX_DISABLE_1BPP is not set CONFIG_ARCH="arm" CONFIG_ARCH_BOARD="flipperzero" CONFIG_ARCH_BOARD_FLIPPERZERO=y @@ -17,24 +18,39 @@ CONFIG_ARCH_STACKDUMP=y CONFIG_BOARD_LOOPSPERMSEC=6500 CONFIG_BUILTIN=y CONFIG_DEBUG_SYMBOLS=y +CONFIG_EXAMPLES_NXDEMO=y +CONFIG_EXAMPLES_NXHELLO=y +CONFIG_EXAMPLES_NXHELLO_BPP=1 +CONFIG_EXAMPLES_NXTEXT=y +CONFIG_EXAMPLES_NXTEXT_BPP=1 +CONFIG_EXAMPLES_NXTEXT_LINESPACING=0 CONFIG_HAVE_CXX=y CONFIG_HAVE_CXXINITIALIZE=y CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INTELHEX_BINARY=y +CONFIG_LCD=y +CONFIG_LCD_NOGETRUN=y +CONFIG_LCD_ST7565=y +CONFIG_MQ_MAXMSGSIZE=64 CONFIG_NSH_ARCHINIT=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_LINELEN=64 CONFIG_NSH_READLINE=y +CONFIG_NX=y +CONFIG_NXFONT_X11_MISC_FIXED_6X10=y +CONFIG_NX_BLOCKING=y CONFIG_PREALLOC_TIMERS=4 CONFIG_RAM_SIZE=196608 CONFIG_RAM_START=0x20000000 CONFIG_RAW_BINARY=y CONFIG_RR_INTERVAL=200 CONFIG_SCHED_WAITPID=y +CONFIG_ST7565_MIRROR_Y=y CONFIG_STM32WB_DISABLE_IDLE_SLEEP_DURING_DEBUG=y CONFIG_STM32WB_DMA1=y CONFIG_STM32WB_PWR=y +CONFIG_STM32WB_SPI2=y CONFIG_STM32WB_USART1=y CONFIG_SYSTEM_NSH=y CONFIG_TASK_NAME_SIZE=0 diff --git a/boards/arm/stm32wb/flipperzero/include/board.h b/boards/arm/stm32wb/flipperzero/include/board.h index 94cdb76445..7e0e47081c 100644 --- a/boards/arm/stm32wb/flipperzero/include/board.h +++ b/boards/arm/stm32wb/flipperzero/include/board.h @@ -45,6 +45,12 @@ #define GPIO_USART1_TX GPIO_USART1_TX_2 /* PB6 */ #define GPIO_USART1_RX GPIO_USART1_RX_2 /* PB7 */ +/* SPI */ + +#define GPIO_SPI2_SCK GPIO_SPI2_SCK_3 /* PD1 */ +#define GPIO_SPI2_MISO GPIO_SPI2_MISO_2 /* PC2 */ +#define GPIO_SPI2_MOSI GPIO_SPI2_MOSI_1 /* PB15 */ + /* LEDs */ /* LED index values for use with board_userled() */ @@ -74,6 +80,17 @@ #define BUTTON_SW2_BIT (1 << BUTTON_SW2) #define BUTTON_SW3_BIT (1 << BUTTON_SW3) +/* LCD */ + +#define STM32WB_LCD_SPINO 2 /* SPI2 */ + +#define STM32WB_LCD_CS (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_5MHz |\ + GPIO_OUTPUT_SET | GPIO_PORTC | GPIO_PIN11) +#define STM32WB_LCD_RST (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_5MHz |\ + GPIO_OUTPUT_SET | GPIO_PORTB | GPIO_PIN0) +#define STM32WB_LCD_A0 (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_5MHz |\ + GPIO_OUTPUT_SET | GPIO_PORTB | GPIO_PIN1) + /**************************************************************************** * Public Data ****************************************************************************/ diff --git a/boards/arm/stm32wb/flipperzero/src/Makefile b/boards/arm/stm32wb/flipperzero/src/Makefile index 568804dcf0..d3e24600a4 100644 --- a/boards/arm/stm32wb/flipperzero/src/Makefile +++ b/boards/arm/stm32wb/flipperzero/src/Makefile @@ -26,4 +26,12 @@ ifeq ($(CONFIG_BOARDCTL),y) CSRCS += stm32_appinit.c endif +ifeq ($(CONFIG_SPI),y) +CSRCS += stm32_spi.c +endif + +ifeq ($(CONFIG_LCD_ST7565),y) +CSRCS += stm32_lcd_st7565.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/boards/arm/stm32wb/flipperzero/src/flipperzero.h b/boards/arm/stm32wb/flipperzero/src/flipperzero.h index 2edf26ef1b..ec54726f8d 100644 --- a/boards/arm/stm32wb/flipperzero/src/flipperzero.h +++ b/boards/arm/stm32wb/flipperzero/src/flipperzero.h @@ -79,5 +79,17 @@ * Public Function Prototypes ****************************************************************************/ +/**************************************************************************** + * Name: stm32wb_spidev_initialize + * + * Description: + * Called to configure SPI chip select GPIO pins. + * + ****************************************************************************/ + +#ifdef CONFIG_SPI +void weak_function stm32wb_spidev_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_STM32WB_FLIPPERZERO_SRC_FLIPPERZERO_H */ diff --git a/boards/arm/stm32wb/flipperzero/src/stm32_appinit.c b/boards/arm/stm32wb/flipperzero/src/stm32_appinit.c index 8c6c26bee2..e315410462 100644 --- a/boards/arm/stm32wb/flipperzero/src/stm32_appinit.c +++ b/boards/arm/stm32wb/flipperzero/src/stm32_appinit.c @@ -155,6 +155,14 @@ int board_app_initialize(uintptr_t arg) } #endif +#ifdef CONFIG_LCD + ret = board_lcd_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to initialize LCD.\n"); + } +#endif + #ifdef CONFIG_STM32WB_BLE /* Initialize and register BLE HCI driver */ diff --git a/boards/arm/stm32wb/flipperzero/src/stm32_boot.c b/boards/arm/stm32wb/flipperzero/src/stm32_boot.c index d2575d00f2..3f24c75dfb 100644 --- a/boards/arm/stm32wb/flipperzero/src/stm32_boot.c +++ b/boards/arm/stm32wb/flipperzero/src/stm32_boot.c @@ -50,6 +50,17 @@ void stm32wb_board_initialize(void) { + /* Configure SPI chip selects if 1) SPI is not disabled, and 2) the weak + * function stm32_spidev_initialize() has been brought into the link. + */ + +#ifdef CONFIG_SPI + if (stm32wb_spidev_initialize) + { + stm32wb_spidev_initialize(); + } +#endif + /* Configure on-board LEDs if LED support has been selected. */ #ifdef CONFIG_ARCH_LEDS diff --git a/boards/arm/stm32wb/flipperzero/src/stm32_lcd_st7565.c b/boards/arm/stm32wb/flipperzero/src/stm32_lcd_st7565.c new file mode 100644 index 0000000000..7e68e72758 --- /dev/null +++ b/boards/arm/stm32wb/flipperzero/src/stm32_lcd_st7565.c @@ -0,0 +1,185 @@ +/**************************************************************************** + * boards/arm/stm32wb/flipperzero/src/stm32_lcd_st7565.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 + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "arm_internal.h" +#include "stm32wb_gpio.h" +#include "stm32wb_spi.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_LCD_CONTRAST +# define CONFIG_LCD_CONTRAST 0x5f +#endif + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void stm32wb_st7565_reset(struct st7565_lcd_s *lcd, bool on); +static void stm32wb_st7565_select(struct st7565_lcd_s *lcd); +static void stm32wb_st7565_deselect(struct st7565_lcd_s *lcd); +static void stm32wb_st7565_cmddata(struct st7565_lcd_s *lcd, + const uint8_t cmd); +static int stm32wb_st7565_senddata(struct st7565_lcd_s *lcd, + const uint8_t *data, int size); +static int stm32wb_st7565_backlight(struct st7565_lcd_s *lcd, int level); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct spi_dev_s *g_spidev; +static struct lcd_dev_s *g_lcddev; + +static struct st7565_lcd_s g_st7565_dev = +{ + .reset = stm32wb_st7565_reset, + .select = stm32wb_st7565_select, + .deselect = stm32wb_st7565_deselect, + .cmddata = stm32wb_st7565_cmddata, + .senddata = stm32wb_st7565_senddata, + .backlight = stm32wb_st7565_backlight +}; + +static void stm32wb_st7565_reset(struct st7565_lcd_s *lcd, bool on) +{ + stm32wb_gpiowrite(STM32WB_LCD_RST, !on); +} + +static void stm32wb_st7565_select(struct st7565_lcd_s *lcd) +{ + stm32wb_gpiowrite(STM32WB_LCD_CS, 0); +} + +static void stm32wb_st7565_deselect(struct st7565_lcd_s *lcd) +{ + stm32wb_gpiowrite(STM32WB_LCD_CS, 1); +} + +static void stm32wb_st7565_cmddata(struct st7565_lcd_s *lcd, + const uint8_t cmd) +{ + stm32wb_gpiowrite(STM32WB_LCD_A0, !cmd); +} + +static int stm32wb_st7565_senddata(struct st7565_lcd_s *lcd, + const uint8_t *data, int size) +{ + SPI_SNDBLOCK(g_spidev, data, size); + return 0; +} + +static int stm32wb_st7565_backlight(struct st7565_lcd_s *lcd, int level) +{ + return 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_lcd_initialize + ****************************************************************************/ + +int board_lcd_initialize(void) +{ + stm32wb_configgpio(STM32WB_LCD_RST); + stm32wb_configgpio(STM32WB_LCD_A0); + + g_spidev = stm32wb_spibus_initialize(STM32WB_LCD_SPINO); + + if (!g_spidev) + { + lcderr("ERROR: Failed to initialize SPI port %d\n", STM32WB_LCD_SPINO); + return -ENODEV; + } + + g_spidev->ops->setmode(g_spidev, SPIDEV_MODE3); + g_spidev->ops->setbits(g_spidev, 8); + g_spidev->ops->setfrequency(g_spidev, 1000000); + + stm32wb_gpiowrite(STM32WB_LCD_RST, 0); + up_mdelay(1); + stm32wb_gpiowrite(STM32WB_LCD_RST, 1); + + return OK; +} + +/**************************************************************************** + * Name: board_lcd_getdev + ****************************************************************************/ + +struct lcd_dev_s *board_lcd_getdev(int lcddev) +{ + g_lcddev = st7565_initialize(&g_st7565_dev, lcddev); + if (!g_lcddev) + { + lcderr("ERROR: Failed to bind SPI port %d to LCD %d\n", + STM32WB_LCD_SPINO, lcddev); + } + else + { + lcdinfo("SPI port %d bound to LCD %d\n", + STM32WB_LCD_SPINO, lcddev); + + /* And turn the LCD on (CONFIG_LCD_MAXPOWER should be 1) */ + + g_lcddev->setpower(g_lcddev, CONFIG_LCD_MAXPOWER); + + /* Set contrast to right value, otherwise background too dark */ + + g_lcddev->setcontrast(g_lcddev, CONFIG_LCD_CONTRAST); + + return g_lcddev; + } + + return NULL; +} + +/**************************************************************************** + * Name: board_lcd_uninitialize + ****************************************************************************/ + +void board_lcd_uninitialize(void) +{ + /* TO-FIX */ +} diff --git a/boards/arm/stm32wb/flipperzero/src/stm32_spi.c b/boards/arm/stm32wb/flipperzero/src/stm32_spi.c new file mode 100644 index 0000000000..a3659772ed --- /dev/null +++ b/boards/arm/stm32wb/flipperzero/src/stm32_spi.c @@ -0,0 +1,101 @@ +/**************************************************************************** + * boards/arm/stm32wb/flipperzero/src/stm32_spi.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 + +#include +#include +#include + +#include +#include + +#include "stm32wb_gpio.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32wb_spidev_initialize + * + * Description: + * Called to configure SPI chip select GPIO pins. + * + ****************************************************************************/ + +void weak_function stm32wb_spidev_initialize(void) +{ + /* NOTE: Clocking was already provided in stm32wb_rcc.c. + * Here, we only initialize chip select pins unique to the board + * architecture. + */ + +#ifdef CONFIG_LCD_ST7565 + stm32wb_configgpio(STM32WB_LCD_CS); /* ST7565 chip select */ +#endif +} + +/**************************************************************************** + * Name: stm32wb_spi1/2select and stm32wb_spi1/2status + * + * Description: + * The external functions, stm32wb_spi1/2select and stm32wb_spi1/2status + * must be provided by board-specific logic. They are implementations of + * the select and status methods of the SPI interface defined by struct + * spi_ops_s (see include/nuttx/spi/spi.h). All other methods (including + * stm32wb_spibus_initialize()) are provided by common STM32 logic. + * To use this common SPI logic on your board: + * + * 1. Provide logic in stm32wb_boardinitialize() to configure SPI chip + * select pins. + * 2. Provide stm32wb_spi1/2select() and stm32wb_spi1/2status() functions + * in your board-specific logic. These functions will perform chip + * selection and status operations using GPIOs in the way your board is + * configured. + * 3. Add a calls to stm32wb_spibus_initialize() in your low level + * application initialization logic + * 4. The handle returned by stm32wb_spibus_initialize() may then be used + * to bind the SPI driver to higher level logic (e.g., calling + * mmcsd_spislotinitialize(), for example, will bind the SPI driver to + * the SPI MMC/SD driver). + * + ****************************************************************************/ + +#ifdef CONFIG_STM32WB_SPI2 +void stm32wb_spi2select(struct spi_dev_s *dev, uint32_t devid, bool selected) +{ +#ifdef CONFIG_LCD_ST7565 + if (devid == SPIDEV_DISPLAY(0)) + { + stm32wb_gpiowrite(STM32WB_LCD_CS, !selected); + } +#endif +} + +uint8_t stm32wb_spi2status(struct spi_dev_s *dev, uint32_t devid) +{ + return 0; +} +#endif