Add PWM control functionality
This commit is contained in:
		@ -127,10 +127,10 @@ void Error_Handler(void);
 | 
			
		||||
#define D6_GPIO_Port GPIOB
 | 
			
		||||
#define PWM2_1_Pin GPIO_PIN_5
 | 
			
		||||
#define PWM2_1_GPIO_Port GPIOB
 | 
			
		||||
#define PWM3_2_Pin GPIO_PIN_6
 | 
			
		||||
#define PWM3_1_Pin GPIO_PIN_6
 | 
			
		||||
#define PWM3_1_GPIO_Port GPIOB
 | 
			
		||||
#define PWM3_2_Pin GPIO_PIN_7
 | 
			
		||||
#define PWM3_2_GPIO_Port GPIOB
 | 
			
		||||
#define PWM3_2B7_Pin GPIO_PIN_7
 | 
			
		||||
#define PWM3_2B7_GPIO_Port GPIOB
 | 
			
		||||
 | 
			
		||||
/* USER CODE BEGIN Private defines */
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,8 +2,8 @@
 | 
			
		||||
#define MAPPINGS_H
 | 
			
		||||
 | 
			
		||||
// CHOOSE ONE, comment the other or comment both and use -D SN_FRONT
 | 
			
		||||
#define SN_FRONT
 | 
			
		||||
//#define SN_REAR
 | 
			
		||||
//#define SN_FRONT
 | 
			
		||||
#define SN_REAR
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include "main.h"
 | 
			
		||||
@ -171,6 +171,35 @@ static can_pkt_t CAN_SIGNAL_MAP[NUM_TX_PKT] = {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
#define CAN_PWM_DC_ID 0x0DC
 | 
			
		||||
#define CAN_PWM_CONF_ID 0x0DD
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* user needs
 | 
			
		||||
TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3};
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
    PWM1 = 0, // TIM1
 | 
			
		||||
    PWM3 = 1, // TIM4
 | 
			
		||||
    PWM2 = 2, // TIM3
 | 
			
		||||
} pwm_tim_t; // Used as index for PWM_TIM_MAP
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    uint8_t tim;
 | 
			
		||||
    uint8_t ch;
 | 
			
		||||
} pwm_ch_t;
 | 
			
		||||
 | 
			
		||||
static pwm_ch_t PWM_CH_MAP[8] = {
 | 
			
		||||
    { .tim = PWM1, .ch = 0 }, // TIM1_CH1
 | 
			
		||||
    { .tim = PWM1, .ch = 1 }, // TIM1_CH2
 | 
			
		||||
    { .tim = PWM1, .ch = 2 }, // TIM1_CH3
 | 
			
		||||
    { .tim = PWM1, .ch = 3 }, // TIM1_CH4
 | 
			
		||||
    { .tim = PWM3, .ch = 0 }, // TIM4_CH1
 | 
			
		||||
    { .tim = PWM3, .ch = 1 }, // TIM4_CH2
 | 
			
		||||
    { .tim = PWM2, .ch = 1 }, // TIM3_CH2
 | 
			
		||||
    { .tim = PWM2, .ch = 3 }  // TIM3_CH4
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@ -37,7 +37,7 @@
 | 
			
		||||
 | 
			
		||||
/* Private define ------------------------------------------------------------*/
 | 
			
		||||
/* USER CODE BEGIN PD */
 | 
			
		||||
 | 
			
		||||
#define TIM_BASE_FREQ 96000000UL
 | 
			
		||||
/* USER CODE END PD */
 | 
			
		||||
 | 
			
		||||
/* Private macro -------------------------------------------------------------*/
 | 
			
		||||
@ -51,6 +51,9 @@
 | 
			
		||||
/* Declare buffer in D1 domain SRAM */
 | 
			
		||||
static uint16_t adc_values[NUM_ADC_PINS];
 | 
			
		||||
static uint8_t  dio_values[NUM_DIO_PINS];
 | 
			
		||||
 | 
			
		||||
// See mappings.h pwm_tim_t
 | 
			
		||||
TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3};
 | 
			
		||||
/* USER CODE END PV */
 | 
			
		||||
 | 
			
		||||
/* Private function prototypes -----------------------------------------------*/
 | 
			
		||||
@ -137,7 +140,16 @@ int main(void)
 | 
			
		||||
    FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE) != HAL_OK)
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
 | 
			
		||||
  // TODO: Add Filters and Rx Handler
 | 
			
		||||
  FDCAN_FilterTypeDef filter;
 | 
			
		||||
  filter.IdType = FDCAN_STANDARD_ID;
 | 
			
		||||
  filter.FilterIndex = 0;
 | 
			
		||||
  filter.FilterType = FDCAN_FILTER_MASK;
 | 
			
		||||
  filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
 | 
			
		||||
  filter.FilterID1 = 0x0DD;
 | 
			
		||||
  filter.FilterID2 = 0x7FE; // Match 0x0DC and 0x0DD
 | 
			
		||||
 | 
			
		||||
  if (HAL_FDCAN_ConfigFilter(&hfdcan1, &filter) != HAL_OK)
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
 | 
			
		||||
  if (HAL_FDCAN_Start(&hfdcan1) != HAL_OK)
 | 
			
		||||
    Error_Handler();
 | 
			
		||||
@ -293,6 +305,51 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
 | 
			
		||||
    __asm volatile ("NOP");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SetCCR(TIM_TypeDef* Instance, unsigned int ch, uint8_t dc) {
 | 
			
		||||
  (&(Instance->CCR1))[ch] = dc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, uint32_t RxFifo0ITs)
 | 
			
		||||
{
 | 
			
		||||
  if (handle != &hfdcan1 || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET)
 | 
			
		||||
    return;
 | 
			
		||||
  
 | 
			
		||||
  static FDCAN_RxHeaderTypeDef header;
 | 
			
		||||
  static uint8_t data[8];
 | 
			
		||||
  if (HAL_FDCAN_GetRxMessage(&hfdcan1, FDCAN_RX_FIFO0, &header, data) != HAL_OK)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  if (header.FDFormat != FDCAN_CLASSIC_CAN ||
 | 
			
		||||
      header.RxFrameType != FDCAN_DATA_FRAME ||
 | 
			
		||||
      header.IdType != FDCAN_STANDARD_ID)
 | 
			
		||||
    return;
 | 
			
		||||
  
 | 
			
		||||
  switch (header.Identifier) {
 | 
			
		||||
    case CAN_PWM_DC_ID:
 | 
			
		||||
      uint8_t* dcs = data;
 | 
			
		||||
      for (int i = 0; i < header.DataLength; i++) {
 | 
			
		||||
        TIM_HandleTypeDef* htim = PWM_TIM_MAP[PWM_CH_MAP[i].tim];
 | 
			
		||||
        SetCCR(htim->Instance, PWM_CH_MAP[i].ch, dcs[i]);
 | 
			
		||||
        if (htim->ChannelState[PWM_CH_MAP[i].ch] == HAL_TIM_CHANNEL_STATE_RESET)
 | 
			
		||||
          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);
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    case CAN_PWM_CONF_ID:
 | 
			
		||||
      uint16_t* freqs = (uint16_t*) data;
 | 
			
		||||
      for (int i = 0; i < (header.DataLength/2); i++) {
 | 
			
		||||
        uint32_t prescaler = (TIM_BASE_FREQ / (255UL * freqs[i])); // cast?
 | 
			
		||||
        PWM_TIM_MAP[i]->Instance->PSC = prescaler;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* USER CODE END 4 */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 | 
			
		||||
@ -437,7 +437,7 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
 | 
			
		||||
    PB6     ------> TIM4_CH1
 | 
			
		||||
    PB7     ------> TIM4_CH2
 | 
			
		||||
    */
 | 
			
		||||
    GPIO_InitStruct.Pin = PWM3_2_Pin|PWM3_2B7_Pin;
 | 
			
		||||
    GPIO_InitStruct.Pin = PWM3_1_Pin|PWM3_2_Pin;
 | 
			
		||||
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 | 
			
		||||
    GPIO_InitStruct.Pull = GPIO_NOPULL;
 | 
			
		||||
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 | 
			
		||||
 | 
			
		||||
@ -354,7 +354,7 @@ PB5.GPIOParameters=GPIO_Label
 | 
			
		||||
PB5.GPIO_Label=PWM2_1
 | 
			
		||||
PB5.Signal=S_TIM3_CH2
 | 
			
		||||
PB6.GPIOParameters=GPIO_Label
 | 
			
		||||
PB6.GPIO_Label=PWM3_2
 | 
			
		||||
PB6.GPIO_Label=PWM3_1
 | 
			
		||||
PB6.Signal=S_TIM4_CH1
 | 
			
		||||
PB7.GPIOParameters=GPIO_Label
 | 
			
		||||
PB7.GPIO_Label=PWM3_2
 | 
			
		||||
@ -607,13 +607,19 @@ TIM1.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 | 
			
		||||
TIM1.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM1.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
 | 
			
		||||
TIM1.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4
 | 
			
		||||
TIM1.IPParameters=Channel-PWM Generation4 CH4,Channel-PWM Generation3 CH3,Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2
 | 
			
		||||
TIM1.IPParameters=Channel-PWM Generation4 CH4,Channel-PWM Generation3 CH3,Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period
 | 
			
		||||
TIM1.Period=255
 | 
			
		||||
TIM1.Prescaler=753
 | 
			
		||||
TIM3.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM3.Channel-PWM\ Generation4\ CH4=TIM_CHANNEL_4
 | 
			
		||||
TIM3.IPParameters=Channel-PWM Generation2 CH2,Channel-PWM Generation4 CH4
 | 
			
		||||
TIM3.IPParameters=Channel-PWM Generation4 CH4,Channel-PWM Generation2 CH2,Prescaler,Period
 | 
			
		||||
TIM3.Period=255
 | 
			
		||||
TIM3.Prescaler=1203
 | 
			
		||||
TIM4.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
 | 
			
		||||
TIM4.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
 | 
			
		||||
TIM4.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2
 | 
			
		||||
TIM4.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period
 | 
			
		||||
TIM4.Period=255
 | 
			
		||||
TIM4.Prescaler=19
 | 
			
		||||
TIM6.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_DISABLE
 | 
			
		||||
TIM6.IPParameters=Prescaler,Period,TIM_MasterOutputTrigger,AutoReloadPreload
 | 
			
		||||
TIM6.Period=2000-1
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user