@@ -48,22 +48,16 @@ static HardwarePWM * const _HwPWM = HwPWMx[2];
48
48
// Defined a struct, to simplify validation testing ... also provides context when debugging
49
49
class TonePwmConfig {
50
50
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;
55
51
const uintptr_t toneToken = 0x656e6f54 ; // < 'T' 'o' 'n' 'e'
56
- private:
57
52
uint64_t pulse_count; // < total number of PWM pulses
58
53
uint32_t seq0_refresh; // < count of pulses for each SEQ0 iteration
59
54
uint32_t seq1_refresh; // < count of pulses for each SEQ1 iteration
60
55
uint16_t loop_count; // < how many times to restart SEQ0/SEQ1?
61
56
uint16_t time_period; // < how many clock cycles allocated to each PWM pulse?
62
57
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
65
58
nrf_pwm_task_t task_to_start; // < Whether to start playback at SEQ0 or SEQ1
66
59
nrf_pwm_short_mask_t shorts; // < shortcuts to enable
60
+
67
61
public:
68
62
bool ensurePwmPeripheralOwnership (void );
69
63
bool initializeFromPulseCountAndTimePeriod (uint64_t pulse_count, uint16_t time_period);
@@ -98,6 +92,7 @@ constexpr static uint16_t _calculate_time_period(uint32_t frequency) {
98
92
// which fits in 16 bits.
99
93
return 125000 / frequency;
100
94
};
95
+
101
96
constexpr static uint64_t _calculate_pulse_count (uint32_t frequency, uint32_t duration) {
102
97
// range for frequency == [20..25000],
103
98
// range for duration == [ 1..0xFFFF_FFFF]
@@ -111,6 +106,7 @@ constexpr static uint64_t _calculate_pulse_count(uint32_t frequency, uint32_t du
111
106
(duration / 1000ULL ) * frequency :
112
107
(((uint64_t )duration) * frequency / 1000ULL );
113
108
};
109
+
114
110
static int _bits_used (unsigned long x) {
115
111
if (0 == x) return 0 ;
116
112
unsigned int result = 0 ;
@@ -119,6 +115,7 @@ static int _bits_used(unsigned long x) {
119
115
} while (x >>= 1 );
120
116
return result;
121
117
}
118
+
122
119
static int _bits_used (unsigned long long x) {
123
120
if (0 == x) return 0 ;
124
121
unsigned int result = 0 ;
@@ -193,6 +190,7 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
193
190
}
194
191
return ;
195
192
}
193
+
196
194
void noTone (uint8_t pin)
197
195
{
198
196
( void )pin; // avoid unreferenced parameter compiler warning
@@ -310,6 +308,7 @@ bool TonePwmConfig::initializeFromPulseCountAndTimePeriod(uint64_t pulse_count_x
310
308
}
311
309
return true ;
312
310
}
311
+
313
312
bool TonePwmConfig::applyConfiguration (uint32_t pin) {
314
313
if (pin >= PINS_COUNT) {
315
314
return false ;
@@ -319,20 +318,17 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
319
318
}
320
319
this ->stopPlayback (false );
321
320
322
- this ->arduino_pin = pin;
323
- this ->nrf_pin = g_ADigitalPinMap[pin];
324
-
325
321
uint32_t pins[NRF_PWM_CHANNEL_COUNT] = {
326
- this -> nrf_pin ,
322
+ g_ADigitalPinMap[pin] ,
327
323
NRF_PWM_PIN_NOT_CONNECTED,
328
324
NRF_PWM_PIN_NOT_CONNECTED,
329
325
NRF_PWM_PIN_NOT_CONNECTED
330
326
};
331
327
332
328
nrf_pwm_pins_set (_PWMInstance, pins); // must set pins before enabling
333
329
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 );
336
332
nrf_pwm_shorts_set (_PWMInstance, this ->shorts );
337
333
nrf_pwm_int_set (_PWMInstance, 0 );
338
334
@@ -362,6 +358,7 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
362
358
nrf_pwm_event_clear (_PWMInstance, NRF_PWM_EVENT_LOOPSDONE);
363
359
return true ;
364
360
}
361
+
365
362
bool TonePwmConfig::startPlayback (void ) {
366
363
if (!this ->ensurePwmPeripheralOwnership ()) {
367
364
LOG_LV1 (" TON" , " PWM peripheral not available for playback" );
@@ -370,6 +367,7 @@ bool TonePwmConfig::startPlayback(void) {
370
367
nrf_pwm_task_trigger (_PWMInstance, this ->task_to_start );
371
368
return true ;
372
369
}
370
+
373
371
bool TonePwmConfig::stopPlayback (bool releaseOwnership) {
374
372
375
373
bool notInIsr = !isInISR ();
0 commit comments