@@ -274,37 +274,39 @@ size_t PWMAudio::write(const uint8_t *buffer, size_t size) {
274
274
void PWMAudio::find_pacer_fraction (int target, uint16_t *numerator, uint16_t *denominator) {
275
275
const uint16_t max = 0xFFFF ;
276
276
277
- /* Cache last results so we dont have to recalculate*/
277
+ // Cache last results so we dont have to recalculate
278
278
static int last_target;
279
279
static uint16_t bestNum;
280
280
static uint16_t bestDenom;
281
- /* Check if we can load the previous values*/
281
+ // Check if we can load the previous values
282
282
if (target == last_target) {
283
283
*numerator = bestNum;
284
284
*denominator = bestDenom;
285
285
return ;
286
286
}
287
287
288
- // See if it's one of the precalculated values
289
- for (size_t i = 0 ; i < sizeof (__PWMAudio_pacer) / sizeof (__PWMAudio_pacer[0 ]); i++) {
290
- if (target == (int )__PWMAudio_pacer[i].freq ) {
291
- last_target = target;
292
- bestNum = __PWMAudio_pacer[i].n ;
293
- bestDenom = __PWMAudio_pacer[i].d ;
294
- *numerator = bestNum;
295
- *denominator = bestDenom;
296
- return ;
288
+ // See if it's one of the precalculated values, and we're at the precalculated frequency
289
+ if (rp2040.f_cpu () == F_CPU) {
290
+ for (size_t i = 0 ; i < sizeof (__PWMAudio_pacer) / sizeof (__PWMAudio_pacer[0 ]); i++) {
291
+ if (target == (int )__PWMAudio_pacer[i].freq ) {
292
+ last_target = target;
293
+ bestNum = __PWMAudio_pacer[i].n ;
294
+ bestDenom = __PWMAudio_pacer[i].d ;
295
+ *numerator = bestNum;
296
+ *denominator = bestDenom;
297
+ return ;
298
+ }
297
299
}
298
300
}
299
301
300
302
// Nope, do exhaustive search. This is gonna be slooooow
301
- float targetRatio = (float )F_CPU / target;
303
+ float targetRatio = (float )rp2040. f_cpu () / target;
302
304
float lowestError = HUGE_VALF;
303
305
304
306
for (uint16_t denom = 1 ; denom < max; denom++) {
305
- uint16_t num = (int )((targetRatio * denom) + 0 .5f ); /* Calculate numerator, rounding to nearest integer*/
307
+ uint16_t num = (int )((targetRatio * denom) + 0 .5f ); // Calculate numerator, rounding to nearest integer
306
308
307
- /* Check if numerator is within bounds*/
309
+ // Check if numerator is within bounds
308
310
if (num > 0 && num < max) {
309
311
float actualRatio = (float )num / denom;
310
312
float error = fabsf (actualRatio - targetRatio);
0 commit comments