From ffd90a6a3663afcfe63b2463a0da7dd20fce32af Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 8 May 2025 15:12:54 +0200 Subject: [PATCH] Turn PWM channels on only when needed --- Hardware/~sensor-node.kicad_pcb.lck | 1 + Hardware/~sensor-node.kicad_sch.lck | 1 + Software/Core/Src/main.c | 32 +++++++++++++++-------------- 3 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 Hardware/~sensor-node.kicad_pcb.lck create mode 100644 Hardware/~sensor-node.kicad_sch.lck diff --git a/Hardware/~sensor-node.kicad_pcb.lck b/Hardware/~sensor-node.kicad_pcb.lck new file mode 100644 index 0000000..cde8aee --- /dev/null +++ b/Hardware/~sensor-node.kicad_pcb.lck @@ -0,0 +1 @@ +{"hostname":"nagata","username":"oskar"} \ No newline at end of file diff --git a/Hardware/~sensor-node.kicad_sch.lck b/Hardware/~sensor-node.kicad_sch.lck new file mode 100644 index 0000000..cde8aee --- /dev/null +++ b/Hardware/~sensor-node.kicad_sch.lck @@ -0,0 +1 @@ +{"hostname":"nagata","username":"oskar"} \ No newline at end of file diff --git a/Software/Core/Src/main.c b/Software/Core/Src/main.c index 3c49ab0..8b5cf2b 100644 --- a/Software/Core/Src/main.c +++ b/Software/Core/Src/main.c @@ -27,6 +27,8 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include + #include "mappings.h" /* USER CODE END Includes */ @@ -54,6 +56,8 @@ static uint8_t dio_values[NUM_DIO_PINS]; // See mappings.h pwm_tim_t TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3}; + +static uint8_t pwm_ch_active[8]; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -157,15 +161,8 @@ int main(void) if (HAL_FDCAN_Start(hMainCAN) != HAL_OK) Error_Handler(); - - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); - HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4); - HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); - HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2); - HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); - HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4); + // Init all channels as stopped + memset(pwm_ch_active, 0, 8); /* USER CODE END 2 */ @@ -352,14 +349,19 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, uint32_t RxFifo0ITs) for (int i = 0; i < header.DataLength; i++) { TIM_HandleTypeDef* htim = PWM_TIM_MAP[PWM_CH_MAP[i].tim]; + + if ((pwm_ch_active[i] == 0) && (dcs[i] == 0)) { + HAL_TIM_PWM_Stop(htim, PWM_CH_MAP[i].ch << 2); + pwm_ch_active[i] = 0; + continue; + } + SetCCR(htim->Instance, PWM_CH_MAP[i].ch, dcs[i]); - // TODO: This does not work, apparently they don't start in RESET - // Maybe have our own init bool array instead - // Manually enabling doesn't seem to work either - if (htim->ChannelState[PWM_CH_MAP[i].ch] == HAL_TIM_CHANNEL_STATE_RESET) + + if (pwm_ch_active[i] == 0) { HAL_TIM_PWM_Start(htim, PWM_CH_MAP[i].ch << 2); - // MAYBE: Stop Timer when DC == 0 on all channels? - // HAL_TIM_PWM_Stop(htim, channel); + pwm_ch_active[i] = 1; + } } break;