Skip to content

Commit

Permalink
stm32h7/linum-stm32h753bi: add tone support
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Guzman <[email protected]>
  • Loading branch information
JorgeGzm committed Dec 23, 2024
1 parent 8ea3166 commit 3bf3986
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.

72 changes: 72 additions & 0 deletions boards/arm/stm32h7/linum-stm32h753bi/configs/tone/defconfig
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion boards/arm/stm32h7/linum-stm32h753bi/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
4 changes: 4 additions & 0 deletions boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions boards/arm/stm32h7/linum-stm32h753bi/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 21 additions & 1 deletion boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 */
10 changes: 10 additions & 0 deletions boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
174 changes: 174 additions & 0 deletions boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c
Original file line number Diff line number Diff line change
@@ -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 <nuttx/config.h>
#include <nuttx/config.h>

#include <errno.h>
#include <debug.h>
#include <stdio.h>
#include <fcntl.h>

#include <nuttx/timers/pwm.h>
#include <nuttx/timers/oneshot.h>
#include <nuttx/audio/tone.h>
#include <arch/board/board.h>

#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;
}

0 comments on commit 3bf3986

Please sign in to comment.