From 3bf3986dd2021aa1bc6d362dd2f1b9d0e3b0900b Mon Sep 17 00:00:00 2001 From: Jorge Guzman Date: Wed, 27 Mar 2024 20:39:33 -0300 Subject: [PATCH] stm32h7/linum-stm32h753bi: add tone support Signed-off-by: Jorge Guzman --- .../boards/linum-stm32h753bi/index.rst | 6 + .../linum-stm32h753bi/configs/tone/defconfig | 72 ++++++++ .../stm32h7/linum-stm32h753bi/include/board.h | 2 +- .../linum-stm32h753bi/src/CMakeLists.txt | 4 + .../stm32h7/linum-stm32h753bi/src/Makefile | 4 + .../linum-stm32h753bi/src/linum-stm32h753bi.h | 22 ++- .../linum-stm32h753bi/src/stm32_bringup.c | 10 + .../linum-stm32h753bi/src/stm32_tone.c | 174 ++++++++++++++++++ 8 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 boards/arm/stm32h7/linum-stm32h753bi/configs/tone/defconfig create mode 100644 boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c diff --git a/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst b/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst index a79de00599473..ff5e7dd75f130 100644 --- a/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst +++ b/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst @@ -867,3 +867,9 @@ search the function **lv_nuttx_fbdev_set_file** and modify line 156 as follows: dsc->mem_off_screen = malloc(data_size); to dsc->mem_off_screen = (void*)0xC00000000; + +tone +---- + +This example demonstrates how to use PWM4 and Timer17 to play music using the Tone library and the board's buzzer. + diff --git a/boards/arm/stm32h7/linum-stm32h753bi/configs/tone/defconfig b/boards/arm/stm32h7/linum-stm32h753bi/configs/tone/defconfig new file mode 100644 index 0000000000000..f7b30a8071dd4 --- /dev/null +++ b/boards/arm/stm32h7/linum-stm32h753bi/configs/tone/defconfig @@ -0,0 +1,72 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_DEBUG_ERROR is not set +# CONFIG_NSH_ALIAS is not set +# CONFIG_NSH_DISABLE_IFCONFIG is not set +# CONFIG_NSH_DISABLE_PS is not set +# CONFIG_STANDARD_SERIAL is not set +# CONFIG_STM32H7_USE_LEGACY_PINMAP is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="linum-stm32h753bi" +CONFIG_ARCH_BOARD_LINUM_STM32H753BI=y +CONFIG_ARCH_CHIP="stm32h7" +CONFIG_ARCH_CHIP_STM32H753BI=y +CONFIG_ARCH_CHIP_STM32H7=y +CONFIG_ARCH_CHIP_STM32H7_CORTEXM7=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV7M_DCACHE=y +CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y +CONFIG_ARMV7M_DTCM=y +CONFIG_ARMV7M_ICACHE=y +CONFIG_AUDIO_TONE=y +CONFIG_BOARD_LOOPSPERMSEC=43103 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_PWM=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEBUG_TIMER=y +CONFIG_DRIVERS_AUDIO=y +CONFIG_EXAMPLES_ALARM=y +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INTELHEX_BINARY=y +CONFIG_LIBM=y +CONFIG_MM_REGIONS=4 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_LINELEN=120 +CONFIG_NSH_READLINE=y +CONFIG_ONESHOT=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PWM=y +CONFIG_RAM_SIZE=245760 +CONFIG_RAM_START=0x20010000 +CONFIG_RAW_BINARY=y +CONFIG_READLINE_CMD_HISTORY=y +CONFIG_READLINE_CMD_HISTORY_LINELEN=120 +CONFIG_RR_INTERVAL=200 +CONFIG_RTC_ALARM=y +CONFIG_RTC_DATETIME=y +CONFIG_RTC_DRIVER=y +CONFIG_SCHED_WAITPID=y +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_STM32H7_ONESHOT=y +CONFIG_STM32H7_PWR=y +CONFIG_STM32H7_RTC=y +CONFIG_STM32H7_TIM17=y +CONFIG_STM32H7_TIM4=y +CONFIG_STM32H7_TIM4_CH2OUT=y +CONFIG_STM32H7_TIM4_CHANNEL=2 +CONFIG_STM32H7_TIM4_PWM=y +CONFIG_STM32H7_USART1=y +CONFIG_SYSTEM_CLE=y +CONFIG_SYSTEM_NSH=y +CONFIG_TASK_NAME_SIZE=0 +CONFIG_USART1_SERIAL_CONSOLE=y diff --git a/boards/arm/stm32h7/linum-stm32h753bi/include/board.h b/boards/arm/stm32h7/linum-stm32h753bi/include/board.h index 3bdf7c3889a7c..3d67f5c792165 100644 --- a/boards/arm/stm32h7/linum-stm32h753bi/include/board.h +++ b/boards/arm/stm32h7/linum-stm32h753bi/include/board.h @@ -418,7 +418,7 @@ #define GPIO_I2C3_SDA (GPIO_I2C3_SDA_2|GPIO_SPEED_100MHz) /* PH8 */ /* PWM - Buzzer */ -#define GPIO_TIM4_CH2OUT (GPIO_TIM4_CH2OUT_1|GPIO_SPEED_100MHz) /* PB7 */ +#define GPIO_TIM4_CH2OUT (GPIO_TIM4_CH2OUT_1|GPIO_SPEED_100MHz) /* PB7 */ /* FDCAN1 */ diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt b/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt index 72d80f069725f..fa58e34bf2e96 100644 --- a/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt +++ b/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt @@ -54,6 +54,10 @@ if(CONFIG_PWM) list(APPEND SRCS stm32_pwm.c) endif() +if(CONFIG_AUDIO_TONE) + list(APPEND SRCS stm32_tone.c) +endif() + if(CONFIG_MTD_W25QXXXJV) list(APPEND SRCS stm32_w25q.c) endif() diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile b/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile index 6ab20189ff0f3..ca57095d09a8a 100644 --- a/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile +++ b/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile @@ -76,4 +76,8 @@ ifeq ($(CONFIG_STM32H7_LTDC),y) CSRCS += stm32_lcd.c endif +ifeq ($(CONFIG_AUDIO_TONE),y) + CSRCS += stm32_tone.c +endif + include $(TOPDIR)/boards/Board.mk diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h b/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h index 2f959f00c9090..47f21873451b0 100644 --- a/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h +++ b/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h @@ -103,7 +103,12 @@ /* PWM */ -#define BUZZER_PWMTIMER 4 +#define BUZZER_PWMTIMER 4 + +/* OneShot Timer */ + +#define BOARD_TONE_ONESHOT_TIM 17 /* Timer 17 - Oneshot timer for note timings */ +#define BOARD_TONE_ONESHOT_TIM_RES 10 /* Timer 17 - Oneshot timer resolution (us) */ /* Ethernet * @@ -267,4 +272,19 @@ int board_qencoder_initialize(int devno, int timerno); int stm32_mfrc522initialize(const char *devpath); #endif +/**************************************************************************** + * Name: board_tone_initialize + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/toneN + * + * Description: + * Configure and initialize the tone generator. + * + ****************************************************************************/ + +#ifdef CONFIG_AUDIO_TONE +int board_tone_initialize(int devno); +#endif + #endif /* __BOARDS_ARM_STM32H7_LINUM_STM32H753BI_SRC_LINUM_STM32H753BI_H */ diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c index 036a43b8551ea..0866d2312fdec 100644 --- a/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c +++ b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c @@ -281,6 +281,16 @@ int stm32_bringup(void) } #endif +#ifdef CONFIG_AUDIO_TONE + /* Configure and initialize the tone generator. */ + + ret = board_tone_initialize(0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: board_tone_initialize() failed: %d\n", ret); + } +#endif + #ifdef CONFIG_NETDEV_LATEINIT # ifdef CONFIG_STM32H7_FDCAN1 diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c new file mode 100644 index 0000000000000..f8487c8296ca8 --- /dev/null +++ b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c @@ -0,0 +1,174 @@ +/**************************************************************************** + * boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.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 "linum-stm32h753bi.h" +#include "arm_internal.h" +#include "stm32_pwm.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tone_example + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/toneN + * + * Description: + * Configure and test the tone generator. + * + ****************************************************************************/ + +static void tone_example(int devno) +{ + int ret; + int fd; + char devpath[12]; + const char msg[] = "t120o1l16b9n0baan0bn0bn0baaan0b9n0baan0b"; + + snprintf(devpath, 12, "/dev/tone%d", devno); + fd = open(devpath, O_RDWR); + if (fd < 0) + { + printf("Failed to open device driver at: %s\n", devpath); + return -errno; + } + + ret = write(fd, msg, sizeof(msg)); + if (ret < 0) + { + printf("Failed to write to device driver at: %s\n", devpath); + return ret; + } + + close(fd); + + return ret; +} + +/**************************************************************************** + * Name: board_tone_initialize + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/toneN + * + * Description: + * Configure and initialize the tone generator. + * + ****************************************************************************/ + +int board_tone_initialize(int devno) +{ + static bool initialized = false; + struct pwm_lowerhalf_s *tone; + struct oneshot_lowerhalf_s *oneshot = NULL; + int ret; + char devpath[12]; + + /* Have we already initialized? */ + + if (!initialized) + { + /* Call stm32_pwminitialize() to get an instance of the PWM interface */ + + tone = stm32_pwminitialize(BUZZER_PWMTIMER); + if (!tone) + { + auderr("Failed to get the STM32 PWM lower half to AUDIO TONE\n"); + return -ENODEV; + } + + /* Initialize TONE PWM */ + + tone->ops->setup(tone); + + /* Initialize ONESHOT Timer */ + + oneshot = oneshot_initialize(BOARD_TONE_ONESHOT_TIM, + BOARD_TONE_ONESHOT_TIM_RES); + if (!oneshot) + { + auderr("Failed to initialize ONESHOT Timer!\n"); + return -ENODEV; + } + + /* Register the Audio Tone driver at "/dev/tone0" */ + + snprintf(devpath, 12, "/dev/tone%d", devno); + ret = tone_register(devpath, tone, oneshot); + if (ret < 0) + { + auderr("ERROR: tone_register failed: %d\n", ret); + return ret; + } + + /* Now we are initialized */ + + initialized = true; + + /* Play tone example */ + + tone_example(devno); + } + + return OK; +}