-
Notifications
You must be signed in to change notification settings - Fork 150
Include pico_tone library #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| if (NOT TARGET pico_tone) | ||
| pico_add_library(pico_tone) | ||
|
|
||
| target_include_directories(pico_tone_headers INTERFACE | ||
| ${CMAKE_CURRENT_LIST_DIR}/include) | ||
|
|
||
| target_sources(pico_tone INTERFACE | ||
| ${CMAKE_CURRENT_LIST_DIR}/tone.c) | ||
|
|
||
| target_link_libraries(pico_tone INTERFACE hardware_pwm) | ||
| endif() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||
| * | ||
| * SPDX-License-Identifier: BSD-3-Clause | ||
| */ | ||
|
|
||
| #ifndef _PICO_TONE_H | ||
| #define _PICO_TONE_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** \file tone.h | ||
| * \defgroup pico_tone pico_tone | ||
| * | ||
| * Adds support for playing tones using PWM. | ||
| * | ||
| * Every sound humans encounter consists of one or more frequencies, and the | ||
| * way the ear interprets those sounds is called pitch. | ||
| * | ||
| * In order to produce a variety of pitches, a digital signal needs to convey | ||
| * the frequency of sound it is trying to reproduce. The simplest approach is | ||
| * to generate a 50% duty cycle pulse stream and set the frequency to the | ||
| * desired pitch. | ||
| * | ||
| * References: | ||
| * - https://www.hackster.io/106958/pwm-sound-synthesis-9596f0#overview | ||
| */ | ||
|
|
||
| /*! \brief Initialise the tone generator | ||
| * \ingroup pico_tone | ||
| * | ||
| * Initilise PWM on the given GPIO using the default pwm config. | ||
| * | ||
| * \param gpio The GPIO to use for the tone generator | ||
| */ | ||
| void tone_init(uint gpio); | ||
|
|
||
| /*! \brief Play a tone for a given duration | ||
| * \ingroup pico_tone | ||
| * | ||
| * Play a tone on the given GPIO for the given duration. | ||
| * | ||
| * \param gpio The GPIO to use for the tone generator | ||
| * \param freq The frequency of the tone in Hz | ||
| * \param duration_ms The duration of the tone in milliseconds | ||
| */ | ||
| void tone(uint gpio, uint freq, uint32_t duration_ms); | ||
|
|
||
| /*! \brief Do not play any tone. | ||
| * \ingroup pico_tone | ||
| * | ||
| * Stop playing a tone on the given GPIO. | ||
| * | ||
| * \param gpio The GPIO to use for the tone generator | ||
| */ | ||
| void no_tone(uint gpio); | ||
|
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,34 @@ | ||||||
| /* | ||||||
| * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||||||
| * | ||||||
| * SPDX-License-Identifier: BSD-3-Clause | ||||||
| */ | ||||||
| #include "pico/stdlib.h" | ||||||
| #include "pico/tone.h" | ||||||
| #include "hardware/pwm.h" | ||||||
|
|
||||||
| void tone_init(uint gpio) { | ||||||
| // Configure GPIO for PWM output | ||||||
| gpio_set_function(gpio, GPIO_FUNC_PWM); | ||||||
| // Find out which PWM slice is connected to GPIO | ||||||
| uint slice_num = pwm_gpio_to_slice_num(gpio); | ||||||
| // Get default configuration for PWM slice and initialise PWM with it | ||||||
| pwm_config config = pwm_get_default_config(); | ||||||
| pwm_init(slice_num, &config, true); | ||||||
| } | ||||||
|
|
||||||
| void no_tone(uint gpio) { | ||||||
| pwm_set_gpio_level(gpio, 0); | ||||||
| } | ||||||
|
|
||||||
| void tone(uint gpio, uint freq, uint32_t duration_ms) { | ||||||
| // Calculate and cofigure new clock divider according to the frequency | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| // This formula is assuming we are running at 125MHz. | ||||||
| // TODO: Make this work for any frequency | ||||||
|
Comment on lines
+26
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to fix this TODO ? You can use |
||||||
| float clkdiv = (1.f / freq) * 2000.f; | ||||||
| uint slice_num = pwm_gpio_to_slice_num(gpio); | ||||||
| pwm_set_clkdiv(slice_num, clkdiv); | ||||||
| // Configure duty to 50% ((2**16)-1)/2) to generate a square wave | ||||||
| pwm_set_gpio_level(gpio, 32768U); | ||||||
| sleep_ms(duration_ms); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a bit odd for this function to accept a pwm_set_gpio_level(gpio, 32768U);
sleep_ms(duration_ms);
pwm_set_gpio_level(gpio, 0);and then get rid of the separate |
||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.