Skip to content

Commit

Permalink
Safety checks for if out-of-range index is passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
ameall committed Mar 17, 2024
1 parent d0f27ab commit 6255718
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Core/Inc/compute.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ bool compute_charger_connected();
*
* @param new_fan_speed
* @param fan_select
* @return uint8_t
*
* @return uint8_t 0 = success, 1 = fan_select is out of range, 2 = PWM channel not able to be configured
*/
uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select);

Expand Down
7 changes: 5 additions & 2 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select)
// Define variables
TIM_HandleTypeDef htim;
TIM_OC_InitTypeDef PWMConfig;
uint16_t CCR_value;
uint16_t CCR_value = 0;

// Index of array +1 corresponds to which fan the channel controls
uint32_t channels[6] = {TIM_CHANNEL_3, TIM_CHANNEL_1, TIM_CHANNEL_4, TIM_CHANNEL_3, TIM_CHANNEL_2, TIM_CHANNEL_1};
Expand All @@ -123,6 +123,9 @@ uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select)
htim.Instance = TIM8;
CCR_value = (TIM8->ARR * new_fan_speed) / 100;
}
else{
return 1;
}

// Set object to how PWM channel should be configured
PWMConfig.OCMode = TIM_OCMODE_PWM1;
Expand All @@ -132,7 +135,7 @@ uint8_t compute_set_fan_speed(uint8_t new_fan_speed, uint8_t fan_select)

// Attempt to configure PWM channel
if (HAL_TIM_PWM_ConfigChannel(&htim, &PWMConfig, channels[fan_select]) != HAL_OK){
return 1;
return 2;
}

return 0;
Expand Down

0 comments on commit 6255718

Please sign in to comment.