Skip to content

Commit 7993737

Browse files
authored
Merge pull request #560 from henrygab/PWM_Cleanup1
Minor cleanup
2 parents 3d7ead8 + eb60126 commit 7993737

File tree

1 file changed

+5
-26
lines changed

1 file changed

+5
-26
lines changed

cores/nRF5/Tone.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class TonePwmConfig {
6464
uint8_t nrf_pin; //< the nrf pin for playback
6565
nrf_pwm_task_t task_to_start; //< Whether to start playback at SEQ0 or SEQ1
6666
nrf_pwm_short_mask_t shorts; //< shortcuts to enable
67-
6867
public:
6968
bool ensurePwmPeripheralOwnership(void);
7069
bool initializeFromPulseCountAndTimePeriod(uint64_t pulse_count, uint16_t time_period);
@@ -74,7 +73,7 @@ class TonePwmConfig {
7473
};
7574
TonePwmConfig _pwm_config;
7675

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) {
7877
bool isEnabled =
7978
(pwm_instance->ENABLE & PWM_ENABLE_ENABLE_Msk) ==
8079
(PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
@@ -93,13 +92,13 @@ inline static bool _is_pwm_enabled(NRF_PWM_Type const * pwm_instance) {
9392
See https://gist.github.com/henrygab/6b570ebd51354bf247633c72b8dc383b
9493
for code that compares the new lambdas to the old calculations.
9594
*/
96-
constexpr inline static uint16_t _calculate_time_period(uint32_t frequency) {
95+
constexpr static uint16_t _calculate_time_period(uint32_t frequency) {
9796
// range for frequency == [20..25000],
9897
// so range of result == [ 5..62500]
9998
// which fits in 16 bits.
10099
return 125000 / frequency;
101100
};
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) {
103102
// range for frequency == [20..25000],
104103
// range for duration == [ 1..0xFFFF_FFFF]
105104
// 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
112111
(duration / 1000ULL) * frequency :
113112
(((uint64_t)duration) * frequency / 1000ULL);
114113
};
115-
inline static int _bits_used(unsigned long x) {
114+
static int _bits_used(unsigned long x) {
116115
if (0 == x) return 0;
117116
unsigned int result = 0;
118117
do {
119118
result++;
120119
} while (x >>= 1);
121120
return result;
122121
}
123-
inline static int _bits_used(unsigned long long x) {
122+
static int _bits_used(unsigned long long x) {
124123
if (0 == x) return 0;
125124
unsigned int result = 0;
126125
do {
@@ -172,32 +171,13 @@ inline static int _bits_used(unsigned long long x) {
172171
*/
173172
void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
174173
{
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-
188174
// limit frequency to reasonable audible range
189175
if((frequency < 20) | (frequency > 25000)) {
190176
LOG_LV1("TON", "frequency outside range [20..25000] -- ignoring");
191177
return;
192178
}
193-
194-
if(xSemaphoreTake(_tone_semaphore, portMAX_DELAY) != pdTRUE) {
195-
LOG_LV1("TON", "error acquiring semaphore (should never occur?)");
196-
return;
197-
}
198179
uint64_t pulse_count = _calculate_pulse_count(frequency, duration);
199180
uint16_t time_period = _calculate_time_period(frequency);
200-
201181
if (!_pwm_config.ensurePwmPeripheralOwnership()) {
202182
LOG_LV1("TON", "Unable to acquire PWM peripheral");
203183
} else if (!_pwm_config.stopPlayback()) {
@@ -211,7 +191,6 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
211191
} else {
212192
//LOG_LV2("TON", "Started playback of tone at frequency %d duration %ld", frequency, duration);
213193
}
214-
xSemaphoreGive(_tone_semaphore);
215194
return;
216195
}
217196
void noTone(uint8_t pin)

0 commit comments

Comments
 (0)