-
Notifications
You must be signed in to change notification settings - Fork 142
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
Open
jcarranz97
wants to merge
2
commits into
raspberrypi:master
Choose a base branch
from
jcarranz97:include-pico-tone
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 | ||
*/ | ||
|
||
// PICO_TONE_SILENCE_DELAY_MS, Default delay between tones in milliseconds | ||
#ifndef PICO_TONE_SILENCE_DELAY_MS | ||
#define PICO_TONE_SILENCE_DELAY_MS 10U | ||
#endif | ||
|
||
/*! \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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 | ||
// This formula is assuming we are running at 125MHz. | ||
// TODO: Make this work for any frequency | ||
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); | ||
|
||
// Make silence for some ms to distinguish between tones | ||
no_tone(gpio); | ||
sleep_ms(PICO_TONE_SILENCE_DELAY_MS); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.