Skip to content

Commit 688dfca

Browse files
cores/psoc6: Fix minor condition issue to avoid index out of bound.
Signed-off-by: Ramya Subramanyam <[email protected]>
1 parent 607cf79 commit 688dfca

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

cores/psoc6/analog_io.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void analogWriteResolution(int res) {
122122

123123
void analogWrite(pin_size_t pinNumber, int value) {
124124
uint8_t pwm_index = 0;
125+
uint8_t pwm_value = value;
125126
cy_rslt_t result = CY_RSLT_TYPE_ERROR;
126127

127128
if (pinNumber > GPIO_PIN_COUNT) {
@@ -144,19 +145,21 @@ void analogWrite(pin_size_t pinNumber, int value) {
144145
break;
145146
}
146147
}
148+
if (pwm_index < PWM_HOWMANY) {
147149

148-
if (value < 0) {
149-
value = 0;
150-
}
151-
if (value > desiredWriteResolution) {
152-
value = desiredWriteResolution;
153-
}
150+
if (pwm_value <= 0) {
151+
pwm_value = 0;
152+
}
153+
if (pwm_value > desiredWriteResolution) {
154+
pwm_value = desiredWriteResolution;
155+
}
154156

155-
float duty_cycle_pertentage = (value / desiredWriteResolution) * 100.0f;
157+
float duty_cycle_pertentage = (pwm_value / desiredWriteResolution) * 100.0f;
156158

157-
result = cyhal_pwm_set_duty_cycle(&pwm[pwm_index].pwm_obj, duty_cycle_pertentage, PWM_FREQUENCY_HZ);
158-
pwm_assert(result);
159+
result = cyhal_pwm_set_duty_cycle(&pwm[pwm_index].pwm_obj, duty_cycle_pertentage, PWM_FREQUENCY_HZ);
160+
pwm_assert(result);
159161

160-
result = cyhal_pwm_start(&pwm[pwm_index].pwm_obj);
161-
pwm_assert(result);
162+
result = cyhal_pwm_start(&pwm[pwm_index].pwm_obj);
163+
pwm_assert(result);
164+
}
162165
}

0 commit comments

Comments
 (0)