@@ -64,7 +64,6 @@ class TonePwmConfig {
64
64
uint8_t nrf_pin; // < the nrf pin for playback
65
65
nrf_pwm_task_t task_to_start; // < Whether to start playback at SEQ0 or SEQ1
66
66
nrf_pwm_short_mask_t shorts; // < shortcuts to enable
67
-
68
67
public:
69
68
bool ensurePwmPeripheralOwnership (void );
70
69
bool initializeFromPulseCountAndTimePeriod (uint64_t pulse_count, uint16_t time_period);
@@ -74,7 +73,7 @@ class TonePwmConfig {
74
73
};
75
74
TonePwmConfig _pwm_config;
76
75
77
- inline static bool _is_pwm_enabled (NRF_PWM_Type const * pwm_instance) {
76
+ static bool _is_pwm_enabled (NRF_PWM_Type const * pwm_instance) {
78
77
bool isEnabled =
79
78
(pwm_instance->ENABLE & PWM_ENABLE_ENABLE_Msk) ==
80
79
(PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
@@ -93,13 +92,13 @@ inline static bool _is_pwm_enabled(NRF_PWM_Type const * pwm_instance) {
93
92
See https://gist.github.com/henrygab/6b570ebd51354bf247633c72b8dc383b
94
93
for code that compares the new lambdas to the old calculations.
95
94
*/
96
- constexpr inline static uint16_t _calculate_time_period (uint32_t frequency) {
95
+ constexpr static uint16_t _calculate_time_period (uint32_t frequency) {
97
96
// range for frequency == [20..25000],
98
97
// so range of result == [ 5..62500]
99
98
// which fits in 16 bits.
100
99
return 125000 / frequency;
101
100
};
102
- constexpr inline static uint64_t _calculate_pulse_count (uint32_t frequency, uint32_t duration) {
101
+ constexpr static uint64_t _calculate_pulse_count (uint32_t frequency, uint32_t duration) {
103
102
// range for frequency == [20..25000],
104
103
// range for duration == [ 1..0xFFFF_FFFF]
105
104
// so range of result == [ 1..0x18_FFFF_FFE7] (requires 37 bits)
@@ -112,15 +111,15 @@ constexpr inline static uint64_t _calculate_pulse_count(uint32_t frequency, uint
112
111
(duration / 1000ULL ) * frequency :
113
112
(((uint64_t )duration) * frequency / 1000ULL );
114
113
};
115
- inline static int _bits_used (unsigned long x) {
114
+ static int _bits_used (unsigned long x) {
116
115
if (0 == x) return 0 ;
117
116
unsigned int result = 0 ;
118
117
do {
119
118
result++;
120
119
} while (x >>= 1 );
121
120
return result;
122
121
}
123
- inline static int _bits_used (unsigned long long x) {
122
+ static int _bits_used (unsigned long long x) {
124
123
if (0 == x) return 0 ;
125
124
unsigned int result = 0 ;
126
125
do {
@@ -172,32 +171,13 @@ inline static int _bits_used(unsigned long long x) {
172
171
*/
173
172
void tone (uint8_t pin, unsigned int frequency, unsigned long duration)
174
173
{
175
- // Used only to protect calls against simultaneous multiple calls to tone().
176
- // Using a function-local static to avoid accidental reference from ISR or elsewhere,
177
- // and to simplify ensuring the semaphore gets initialized.
178
- static StaticSemaphore_t _tone_semaphore_allocation;
179
- static auto init_semaphore = [] () { // < use a lambda to both initialize AND give the mutex
180
- SemaphoreHandle_t handle = xSemaphoreCreateMutexStatic (&_tone_semaphore_allocation);
181
- auto mustSucceed = xSemaphoreGive (handle);
182
- (void )mustSucceed;
183
- NRFX_ASSERT (mustSucceed == pdTRUE);
184
- return handle;
185
- };
186
- static SemaphoreHandle_t _tone_semaphore = init_semaphore ();
187
-
188
174
// limit frequency to reasonable audible range
189
175
if ((frequency < 20 ) | (frequency > 25000 )) {
190
176
LOG_LV1 (" TON" , " frequency outside range [20..25000] -- ignoring" );
191
177
return ;
192
178
}
193
-
194
- if (xSemaphoreTake (_tone_semaphore, portMAX_DELAY) != pdTRUE) {
195
- LOG_LV1 (" TON" , " error acquiring semaphore (should never occur?)" );
196
- return ;
197
- }
198
179
uint64_t pulse_count = _calculate_pulse_count (frequency, duration);
199
180
uint16_t time_period = _calculate_time_period (frequency);
200
-
201
181
if (!_pwm_config.ensurePwmPeripheralOwnership ()) {
202
182
LOG_LV1 (" TON" , " Unable to acquire PWM peripheral" );
203
183
} else if (!_pwm_config.stopPlayback ()) {
@@ -211,7 +191,6 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
211
191
} else {
212
192
// LOG_LV2("TON", "Started playback of tone at frequency %d duration %ld", frequency, duration);
213
193
}
214
- xSemaphoreGive (_tone_semaphore);
215
194
return ;
216
195
}
217
196
void noTone (uint8_t pin)
0 commit comments