Turn PWM channels on only when needed
This commit is contained in:
		
							
								
								
									
										1
									
								
								Hardware/~sensor-node.kicad_pcb.lck
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Hardware/~sensor-node.kicad_pcb.lck
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
{"hostname":"nagata","username":"oskar"}
 | 
			
		||||
							
								
								
									
										1
									
								
								Hardware/~sensor-node.kicad_sch.lck
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Hardware/~sensor-node.kicad_sch.lck
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
{"hostname":"nagata","username":"oskar"}
 | 
			
		||||
@ -27,6 +27,8 @@
 | 
			
		||||
 | 
			
		||||
/* Private includes ----------------------------------------------------------*/
 | 
			
		||||
/* USER CODE BEGIN Includes */
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#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;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user