@@ -48,22 +48,16 @@ static HardwarePWM * const _HwPWM = HwPWMx[2];
4848// Defined a struct, to simplify validation testing ... also provides context when debugging
4949class TonePwmConfig {
5050 private:
51- const nrf_pwm_clk_t clock_frequency = NRF_PWM_CLK_125kHz;
52- const nrf_pwm_mode_t pwm_mode = NRF_PWM_MODE_UP;
53- const nrf_pwm_dec_step_t step_mode = NRF_PWM_STEP_AUTO;
54- const nrf_pwm_dec_load_t load_mode = NRF_PWM_LOAD_COMMON;
5551 const uintptr_t toneToken = 0x656e6f54 ; // < 'T' 'o' 'n' 'e'
56- private:
5752 uint64_t pulse_count; // < total number of PWM pulses
5853 uint32_t seq0_refresh; // < count of pulses for each SEQ0 iteration
5954 uint32_t seq1_refresh; // < count of pulses for each SEQ1 iteration
6055 uint16_t loop_count; // < how many times to restart SEQ0/SEQ1?
6156 uint16_t time_period; // < how many clock cycles allocated to each PWM pulse?
6257 uint16_t duty_with_polarity; // < SEQ[N].PTR will point here, length == 1
63- uint8_t arduino_pin; // < the arduino pin for playback
64- uint8_t nrf_pin; // < the nrf pin for playback
6558 nrf_pwm_task_t task_to_start; // < Whether to start playback at SEQ0 or SEQ1
6659 nrf_pwm_short_mask_t shorts; // < shortcuts to enable
60+
6761 public:
6862 bool ensurePwmPeripheralOwnership (void );
6963 bool initializeFromPulseCountAndTimePeriod (uint64_t pulse_count, uint16_t time_period);
@@ -98,6 +92,7 @@ constexpr static uint16_t _calculate_time_period(uint32_t frequency) {
9892 // which fits in 16 bits.
9993 return 125000 / frequency;
10094};
95+
10196constexpr static uint64_t _calculate_pulse_count (uint32_t frequency, uint32_t duration) {
10297 // range for frequency == [20..25000],
10398 // range for duration == [ 1..0xFFFF_FFFF]
@@ -111,6 +106,7 @@ constexpr static uint64_t _calculate_pulse_count(uint32_t frequency, uint32_t du
111106 (duration / 1000ULL ) * frequency :
112107 (((uint64_t )duration) * frequency / 1000ULL );
113108};
109+
114110static int _bits_used (unsigned long x) {
115111 if (0 == x) return 0 ;
116112 unsigned int result = 0 ;
@@ -119,6 +115,7 @@ static int _bits_used(unsigned long x) {
119115 } while (x >>= 1 );
120116 return result;
121117}
118+
122119static int _bits_used (unsigned long long x) {
123120 if (0 == x) return 0 ;
124121 unsigned int result = 0 ;
@@ -193,6 +190,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
193190 }
194191 return ;
195192}
193+
196194void noTone (uint8_t pin)
197195{
198196 ( void )pin; // avoid unreferenced parameter compiler warning
@@ -310,6 +308,7 @@ bool TonePwmConfig::initializeFromPulseCountAndTimePeriod(uint64_t pulse_count_x
310308 }
311309 return true ;
312310}
311+
313312bool TonePwmConfig::applyConfiguration (uint32_t pin) {
314313 if (pin >= PINS_COUNT) {
315314 return false ;
@@ -319,20 +318,17 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
319318 }
320319 this ->stopPlayback (false );
321320
322- this ->arduino_pin = pin;
323- this ->nrf_pin = g_ADigitalPinMap[pin];
324-
325321 uint32_t pins[NRF_PWM_CHANNEL_COUNT] = {
326- this -> nrf_pin ,
322+ g_ADigitalPinMap[pin] ,
327323 NRF_PWM_PIN_NOT_CONNECTED,
328324 NRF_PWM_PIN_NOT_CONNECTED,
329325 NRF_PWM_PIN_NOT_CONNECTED
330326 };
331327
332328 nrf_pwm_pins_set (_PWMInstance, pins); // must set pins before enabling
333329 nrf_pwm_enable (_PWMInstance);
334- nrf_pwm_configure (_PWMInstance, TonePwmConfig::clock_frequency, TonePwmConfig::pwm_mode , this ->time_period );
335- nrf_pwm_decoder_set (_PWMInstance, TonePwmConfig::load_mode, TonePwmConfig::step_mode );
330+ nrf_pwm_configure (_PWMInstance, NRF_PWM_CLK_125kHz, NRF_PWM_MODE_UP , this ->time_period );
331+ nrf_pwm_decoder_set (_PWMInstance, NRF_PWM_LOAD_COMMON, NRF_PWM_STEP_AUTO );
336332 nrf_pwm_shorts_set (_PWMInstance, this ->shorts );
337333 nrf_pwm_int_set (_PWMInstance, 0 );
338334
@@ -362,6 +358,7 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
362358 nrf_pwm_event_clear (_PWMInstance, NRF_PWM_EVENT_LOOPSDONE);
363359 return true ;
364360}
361+
365362bool TonePwmConfig::startPlayback (void ) {
366363 if (!this ->ensurePwmPeripheralOwnership ()) {
367364 LOG_LV1 (" TON" , " PWM peripheral not available for playback" );
@@ -370,6 +367,7 @@ bool TonePwmConfig::startPlayback(void) {
370367 nrf_pwm_task_trigger (_PWMInstance, this ->task_to_start );
371368 return true ;
372369}
370+
373371bool TonePwmConfig::stopPlayback (bool releaseOwnership) {
374372
375373 bool notInIsr = !isInISR ();
0 commit comments