Skip to content

Commit 4d7be17

Browse files
JorgeGzmxiaoxiang781216
authored andcommitted
stm32h7/linum-stm32h753bi: add tone support
Signed-off-by: Jorge Guzman <[email protected]>
1 parent 88a300d commit 4d7be17

File tree

8 files changed

+294
-2
lines changed

8 files changed

+294
-2
lines changed

Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,9 @@ search the function **lv_nuttx_fbdev_set_file** and modify line 156 as follows:
867867
dsc->mem_off_screen = malloc(data_size);
868868
to
869869
dsc->mem_off_screen = (void*)0xC00000000;
870+
871+
tone
872+
----
873+
874+
This example demonstrates how to use PWM4 and Timer17 to play music using the Tone library and the board's buzzer.
875+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#
2+
# This file is autogenerated: PLEASE DO NOT EDIT IT.
3+
#
4+
# You can use "make menuconfig" to make any modifications to the installed .config file.
5+
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
6+
# modifications.
7+
#
8+
# CONFIG_DEBUG_ERROR is not set
9+
# CONFIG_NSH_ALIAS is not set
10+
# CONFIG_NSH_DISABLE_IFCONFIG is not set
11+
# CONFIG_NSH_DISABLE_PS is not set
12+
# CONFIG_STANDARD_SERIAL is not set
13+
# CONFIG_STM32H7_USE_LEGACY_PINMAP is not set
14+
CONFIG_ARCH="arm"
15+
CONFIG_ARCH_BOARD="linum-stm32h753bi"
16+
CONFIG_ARCH_BOARD_LINUM_STM32H753BI=y
17+
CONFIG_ARCH_CHIP="stm32h7"
18+
CONFIG_ARCH_CHIP_STM32H753BI=y
19+
CONFIG_ARCH_CHIP_STM32H7=y
20+
CONFIG_ARCH_CHIP_STM32H7_CORTEXM7=y
21+
CONFIG_ARCH_STACKDUMP=y
22+
CONFIG_ARMV7M_DCACHE=y
23+
CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y
24+
CONFIG_ARMV7M_DTCM=y
25+
CONFIG_ARMV7M_ICACHE=y
26+
CONFIG_AUDIO_TONE=y
27+
CONFIG_BOARD_LOOPSPERMSEC=43103
28+
CONFIG_BUILTIN=y
29+
CONFIG_DEBUG_FEATURES=y
30+
CONFIG_DEBUG_PWM=y
31+
CONFIG_DEBUG_SYMBOLS=y
32+
CONFIG_DEBUG_TIMER=y
33+
CONFIG_DRIVERS_AUDIO=y
34+
CONFIG_EXAMPLES_ALARM=y
35+
CONFIG_INIT_ENTRYPOINT="nsh_main"
36+
CONFIG_INTELHEX_BINARY=y
37+
CONFIG_LIBM=y
38+
CONFIG_MM_REGIONS=4
39+
CONFIG_NSH_ARCHINIT=y
40+
CONFIG_NSH_BUILTIN_APPS=y
41+
CONFIG_NSH_FILEIOSIZE=512
42+
CONFIG_NSH_LINELEN=120
43+
CONFIG_NSH_READLINE=y
44+
CONFIG_ONESHOT=y
45+
CONFIG_PREALLOC_TIMERS=4
46+
CONFIG_PWM=y
47+
CONFIG_RAM_SIZE=245760
48+
CONFIG_RAM_START=0x20010000
49+
CONFIG_RAW_BINARY=y
50+
CONFIG_READLINE_CMD_HISTORY=y
51+
CONFIG_READLINE_CMD_HISTORY_LINELEN=120
52+
CONFIG_RR_INTERVAL=200
53+
CONFIG_RTC_ALARM=y
54+
CONFIG_RTC_DATETIME=y
55+
CONFIG_RTC_DRIVER=y
56+
CONFIG_SCHED_WAITPID=y
57+
CONFIG_START_DAY=6
58+
CONFIG_START_MONTH=12
59+
CONFIG_START_YEAR=2011
60+
CONFIG_STM32H7_ONESHOT=y
61+
CONFIG_STM32H7_PWR=y
62+
CONFIG_STM32H7_RTC=y
63+
CONFIG_STM32H7_TIM17=y
64+
CONFIG_STM32H7_TIM4=y
65+
CONFIG_STM32H7_TIM4_CH2OUT=y
66+
CONFIG_STM32H7_TIM4_CHANNEL=2
67+
CONFIG_STM32H7_TIM4_PWM=y
68+
CONFIG_STM32H7_USART1=y
69+
CONFIG_SYSTEM_CLE=y
70+
CONFIG_SYSTEM_NSH=y
71+
CONFIG_TASK_NAME_SIZE=0
72+
CONFIG_USART1_SERIAL_CONSOLE=y

boards/arm/stm32h7/linum-stm32h753bi/include/board.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@
418418
#define GPIO_I2C3_SDA (GPIO_I2C3_SDA_2|GPIO_SPEED_100MHz) /* PH8 */
419419

420420
/* PWM - Buzzer */
421-
#define GPIO_TIM4_CH2OUT (GPIO_TIM4_CH2OUT_1|GPIO_SPEED_100MHz) /* PB7 */
421+
#define GPIO_TIM4_CH2OUT (GPIO_TIM4_CH2OUT_1|GPIO_SPEED_100MHz) /* PB7 */
422422

423423
/* FDCAN1 */
424424

boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ if(CONFIG_PWM)
5454
list(APPEND SRCS stm32_pwm.c)
5555
endif()
5656

57+
if(CONFIG_AUDIO_TONE)
58+
list(APPEND SRCS stm32_tone.c)
59+
endif()
60+
5761
if(CONFIG_MTD_W25QXXXJV)
5862
list(APPEND SRCS stm32_w25q.c)
5963
endif()

boards/arm/stm32h7/linum-stm32h753bi/src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ ifeq ($(CONFIG_STM32H7_LTDC),y)
7676
CSRCS += stm32_lcd.c
7777
endif
7878

79+
ifeq ($(CONFIG_AUDIO_TONE),y)
80+
CSRCS += stm32_tone.c
81+
endif
82+
7983
include $(TOPDIR)/boards/Board.mk

boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@
103103

104104
/* PWM */
105105

106-
#define BUZZER_PWMTIMER 4
106+
#define BUZZER_PWMTIMER 4
107+
108+
/* OneShot Timer */
109+
110+
#define BOARD_TONE_ONESHOT_TIM 17 /* Timer 17 - Oneshot timer for note timings */
111+
#define BOARD_TONE_ONESHOT_TIM_RES 10 /* Timer 17 - Oneshot timer resolution (us) */
107112

108113
/* Ethernet
109114
*
@@ -267,4 +272,19 @@ int board_qencoder_initialize(int devno, int timerno);
267272
int stm32_mfrc522initialize(const char *devpath);
268273
#endif
269274

275+
/****************************************************************************
276+
* Name: board_tone_initialize
277+
*
278+
* Input Parameters:
279+
* devno - The device number, used to build the device path as /dev/toneN
280+
*
281+
* Description:
282+
* Configure and initialize the tone generator.
283+
*
284+
****************************************************************************/
285+
286+
#ifdef CONFIG_AUDIO_TONE
287+
int board_tone_initialize(int devno);
288+
#endif
289+
270290
#endif /* __BOARDS_ARM_STM32H7_LINUM_STM32H753BI_SRC_LINUM_STM32H753BI_H */

boards/arm/stm32h7/linum-stm32h753bi/src/stm32_bringup.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ int stm32_bringup(void)
281281
}
282282
#endif
283283

284+
#ifdef CONFIG_AUDIO_TONE
285+
/* Configure and initialize the tone generator. */
286+
287+
ret = board_tone_initialize(0);
288+
if (ret < 0)
289+
{
290+
syslog(LOG_ERR, "ERROR: board_tone_initialize() failed: %d\n", ret);
291+
}
292+
#endif
293+
284294
#ifdef CONFIG_NETDEV_LATEINIT
285295

286296
# ifdef CONFIG_STM32H7_FDCAN1
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/****************************************************************************
2+
* boards/arm/stm32h7/linum-stm32h753bi/src/stm32_tone.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
#include <nuttx/config.h>
29+
30+
#include <errno.h>
31+
#include <debug.h>
32+
#include <stdio.h>
33+
#include <fcntl.h>
34+
35+
#include <nuttx/timers/pwm.h>
36+
#include <nuttx/timers/oneshot.h>
37+
#include <nuttx/audio/tone.h>
38+
#include <arch/board/board.h>
39+
40+
#include "linum-stm32h753bi.h"
41+
#include "arm_internal.h"
42+
#include "stm32_pwm.h"
43+
44+
/****************************************************************************
45+
* Pre-processor Definitions
46+
****************************************************************************/
47+
48+
/****************************************************************************
49+
* Private Types
50+
****************************************************************************/
51+
52+
/****************************************************************************
53+
* Private Function Prototypes
54+
****************************************************************************/
55+
56+
/****************************************************************************
57+
* Private Data
58+
****************************************************************************/
59+
60+
/****************************************************************************
61+
* Public Data
62+
****************************************************************************/
63+
64+
/****************************************************************************
65+
* Private Functions
66+
****************************************************************************/
67+
68+
/****************************************************************************
69+
* Public Functions
70+
****************************************************************************/
71+
72+
/****************************************************************************
73+
* Name: tone_example
74+
*
75+
* Input Parameters:
76+
* devno - The device number, used to build the device path as /dev/toneN
77+
*
78+
* Description:
79+
* Configure and test the tone generator.
80+
*
81+
****************************************************************************/
82+
83+
static int tone_example(int devno)
84+
{
85+
int ret;
86+
int fd;
87+
char devpath[12];
88+
const char msg[] = "t120o1l16b9n0baan0bn0bn0baaan0b9n0baan0b";
89+
90+
snprintf(devpath, 12, "/dev/tone%d", devno);
91+
fd = open(devpath, O_RDWR);
92+
if (fd < 0)
93+
{
94+
printf("Failed to open device driver at: %s\n", devpath);
95+
return -errno;
96+
}
97+
98+
ret = write(fd, msg, sizeof(msg));
99+
if (ret < 0)
100+
{
101+
printf("Failed to write to device driver at: %s\n", devpath);
102+
return ret;
103+
}
104+
105+
close(fd);
106+
107+
return ret;
108+
}
109+
110+
/****************************************************************************
111+
* Name: board_tone_initialize
112+
*
113+
* Input Parameters:
114+
* devno - The device number, used to build the device path as /dev/toneN
115+
*
116+
* Description:
117+
* Configure and initialize the tone generator.
118+
*
119+
****************************************************************************/
120+
121+
int board_tone_initialize(int devno)
122+
{
123+
static bool initialized = false;
124+
struct pwm_lowerhalf_s *tone;
125+
struct oneshot_lowerhalf_s *oneshot = NULL;
126+
int ret;
127+
char devpath[12];
128+
129+
/* Have we already initialized? */
130+
131+
if (!initialized)
132+
{
133+
/* Call stm32_pwminitialize() to get an instance of the PWM interface */
134+
135+
tone = stm32_pwminitialize(BUZZER_PWMTIMER);
136+
if (!tone)
137+
{
138+
auderr("Failed to get the STM32 PWM lower half to AUDIO TONE\n");
139+
return -ENODEV;
140+
}
141+
142+
/* Initialize TONE PWM */
143+
144+
tone->ops->setup(tone);
145+
146+
/* Initialize ONESHOT Timer */
147+
148+
oneshot = oneshot_initialize(BOARD_TONE_ONESHOT_TIM,
149+
BOARD_TONE_ONESHOT_TIM_RES);
150+
if (!oneshot)
151+
{
152+
auderr("Failed to initialize ONESHOT Timer!\n");
153+
return -ENODEV;
154+
}
155+
156+
/* Register the Audio Tone driver at "/dev/tone0" */
157+
158+
snprintf(devpath, 12, "/dev/tone%d", devno);
159+
ret = tone_register(devpath, tone, oneshot);
160+
if (ret < 0)
161+
{
162+
auderr("ERROR: tone_register failed: %d\n", ret);
163+
return ret;
164+
}
165+
166+
/* Now we are initialized */
167+
168+
initialized = true;
169+
170+
/* Play tone example */
171+
172+
tone_example(devno);
173+
}
174+
175+
return OK;
176+
}

0 commit comments

Comments
 (0)