@@ -47,6 +47,9 @@ static void syncTCC(Tcc* TCCx) {
47
47
#define MAX_PERIOD 0xFFFF
48
48
#endif
49
49
50
+ // API uses 10 bit resolution
51
+ #define PWM_API_RESOLUTION 10
52
+
50
53
static inline unsigned long calcPrescaler (uint32_t frequency, uint32_t &period)
51
54
{
52
55
// if it's a rest, set to 1Hz (below audio range)
@@ -68,6 +71,14 @@ static inline unsigned long calcPrescaler(uint32_t frequency, uint32_t &period)
68
71
i++;
69
72
}
70
73
74
+ #if defined(__SAMD51__)
75
+ period = MAX_PERIOD;
76
+ #else
77
+ // Ensure that our period does not erode the API resolution
78
+ if (period < (1 <<PWM_API_RESOLUTION))
79
+ period = (1 <<PWM_API_RESOLUTION) - 1 ;
80
+ #endif
81
+
71
82
switch (i - 1 )
72
83
{
73
84
case 0 :
@@ -117,7 +128,7 @@ void pwm(uint32_t outputPin, uint32_t frequency, uint32_t duty)
117
128
uint32_t period;
118
129
119
130
prescalerConfigVal = calcPrescaler (frequency, period);
120
- duty = map (duty, 0 , 1024 , 0 , period);
131
+ duty = map (duty, 0 , ( 1 <<PWM_API_RESOLUTION) , 0 , period);
121
132
122
133
uint32_t tcNum = GetTCNumber (pinDesc.ulPWMChannel );
123
134
uint8_t tcChannel = GetTCChannelNumber (pinDesc.ulPWMChannel );
@@ -275,7 +286,7 @@ void pwm(uint32_t outputPin, uint32_t frequency, uint32_t duty)
275
286
}
276
287
else
277
288
{
278
- duty = map (duty, 0 , 1024 , 0 , period);
289
+ duty = map (duty, 0 , ( 1 <<PWM_API_RESOLUTION) , 0 , period);
279
290
// -- Configure TCC
280
291
Tcc *TCCx = (Tcc *)GetTC (pinDesc.ulPWMChannel );
281
292
// Disable TCCx
0 commit comments