Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-pico
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-pico
Commits
b793ad55
Unverified
Commit
b793ad55
authored
Apr 15, 2021
by
Earle F. Philhower, III
Committed by
GitHub
Apr 15, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make Tone multicore safe (#106)
Add a mutex around the Tone mapping for multicore safety.
parent
b5aeb84c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
1 deletion
+23
-1
cores/rp2040/Tone.cpp
cores/rp2040/Tone.cpp
+23
-1
No files found.
cores/rp2040/Tone.cpp
View file @
b793ad55
...
...
@@ -19,6 +19,7 @@
*/
#include <Arduino.h>
#include "CoreMutex.h"
#include <hardware/gpio.h>
#include <pico/time.h>
#include <map>
...
...
@@ -29,12 +30,21 @@ typedef struct {
int
sm
;
}
Tone
;
// Keep std::map safe for multicore use
static
bool
_mutexInitted
=
false
;
static
mutex_t
_mutex
;
#include "tone.pio.h"
static
PIOProgram
_tonePgm
(
&
tone_program
);
static
std
::
map
<
pin_size_t
,
Tone
*>
_toneMap
;
void
tone
(
uint8_t
pin
,
unsigned
int
frequency
,
unsigned
long
duration
)
{
if
(
!
_mutexInitted
)
{
mutex_init
(
&
_mutex
);
_mutexInitted
=
true
;
}
if
((
pin
<
0
)
||
(
pin
>
29
))
{
DEBUGCORE
(
"ERROR: Illegal pin in tone (%d)
\n
"
,
pin
);
return
;
...
...
@@ -43,6 +53,11 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
noTone
(
pin
);
return
;
}
// Ensure only 1 core can start or stop at a time
CoreMutex
m
(
&
_mutex
);
if
(
!
m
)
return
;
// Weird deadlock case
int
us
=
1000000
/
frequency
/
2
;
if
(
us
<
5
)
{
us
=
5
;
...
...
@@ -76,7 +91,14 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
}
void
noTone
(
uint8_t
pin
)
{
if
((
pin
<
0
)
||
(
pin
>
29
))
{
if
(
!
_mutexInitted
)
{
mutex_init
(
&
_mutex
);
_mutexInitted
=
true
;
}
CoreMutex
m
(
&
_mutex
);
if
((
pin
<
0
)
||
(
pin
>
29
)
||
!
m
)
{
DEBUGCORE
(
"ERROR: Illegal pin in tone (%d)
\n
"
,
pin
);
return
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment