9 PWM

9.1 PB24当作motor pwm口configure;方法

1,Find that PB24 corresponds to GPTIM5 and Channel 2;

alt text

2, pinmux.c中, Configure PB24 as the GPTIM5_CH2 function port;

HAL_PIN_Set(PAD_PB24, GPTIM5_CH2, PIN_NOPULL, 0);             // Motor PWM

3,Check the configuration in pwm_config.h, and see that GPTIM5 corresponds to pwm6;,

alt text

4, → RTOS → On-chip Peripheral Drivers → Enable pwm Select PWM6 and enable the following macro;:

#define BSP_USING_PWM6 1

5,Modify the configuration of pwm6 and channel 2 corresponding to PB24;

#define PORT_MOTO        (96+24)
#ifndef  MOTOR_DRV_MODE_GPIO
#define PWM_DEV_NAME      "pwm6"  /* PWM设备名称 */
#define PWM_DEV_CHANNEL     2    /* PWMchannel; */
#endif


alt text

6,如何开启configure;pwmoutput;,can;reference;motor的代码,修change;为你corresponding;的PWMoutput;configure;,as follows;figure;:
alt text

9.2 PA47 当LCDbacklight;, 怎么configure;?

1,Find that PA47 corresponds to GPTIM1 and Channel4;

alt text

2,pinmux.c文件中,Configure it as the GPTIM1_CH4 function port;,as follows;:

#define LCD_BACKLIGHT_USING_PWM
#ifdef LCD_BACKLIGHT_USING_PWM
    HAL_PIN_Set(PAD_PA47, GPTIM1_CH4, PIN_NOPULL, 1);  //backlight pwm  
#else
    HAL_PIN_Set(PAD_PA47, GPIO_A47, PIN_NOPULL, 1);  /GPIO backlight
#endif 

3,查看pwm_config.h中的configure;,Check that GPTIM1 corresponds to pwm2;,
alt text

4, menuconfig中
RTOS On-chip Peripheral Drivers Enable pwm Select PWM2 and enable the following macro;:

#define BSP_USING_PWM2 1

5,In the corresponding screen driver SPD2010_SetBrightness set brightness function, configure PA47 corresponding to pwm2 and channel 4;,代码as follows;:

#define LCD_BACKLIGHT_USING_PWM
#ifdef LCD_BACKLIGHT_USING_PWM
#define LCD_BACKLIGHT_PWM_DEV_NAME "pwm2"
#define LCD_BACKLIGHT_PWM_PERIOD (1 * 1000 * 1000)
#define LCD_BACKLIGHT_PWM_CHANNEL 4
#endif
void SPD2010_SetBrightness(LCDC_HandleTypeDef *hlcdc, uint8_t br)
{
    uint8_t bright = (uint8_t)((int)SPD2010_BRIGHTNESS_MAX * br / 100);
    SPD2010_WriteReg(hlcdc, SPD2010_WBRIGHT, &bright, 1);
//	rt_kprintf("SPD2010_SetBrightness val=%d \n",br);
#ifndef LCD_BACKLIGHT_USING_PWM //If PWM is not used, directly pull up or down GPIO to light up the backlight;
	/* PA70 Backlight ,PA47 1V8_EN*/
		uint8_t bright = (uint8_t)((int)SPD2010_BRIGHTNESS_MAX * br / 100);
		GC9B71_WriteReg(hlcdc, SPD2010_WBRIGHT, &bright, 1);
		rt_pin_mode(LCD_BACKLIGHT_POWER_PIN, PIN_MODE_OUTPUT);
		rt_pin_write(LCD_BACKLIGHT_POWER_PIN, 1);
		LOG_I("SPD2010_SetBrightness,br:%d\n",br);	
	/* PA70 Backlight */
#else
	/* PA47 Backlight PWM,PA70_NC*/
		rt_uint32_t pulse = br * LCD_BACKLIGHT_PWM_PERIOD / 100;
		struct rt_device_pwm *device = RT_NULL;
		device = (struct rt_device_pwm *)rt_device_find(LCD_BACKLIGHT_PWM_DEV_NAME);
		if (!device)
		{
			LOG_I("find pwm:LCD_BACKLIGHT_PWM_DEV_NAME err!",br,pulse);
			return;
		}
		rt_pwm_set(device,LCD_BACKLIGHT_PWM_CHANNEL,LCD_BACKLIGHT_PWM_PERIOD,pulse);
		rt_pwm_enable(device, LCD_BACKLIGHT_PWM_CHANNEL);
		LOG_I("SPD2010_SetBrightness,br:%d,pulse:%d\n",br,pulse);
	/* PA47 Backlight PWM */	
#endif	
}

note;:
during;用PA口output;PWMwhen;,如果enable;already;#define BSP_PM_FREQ_SCALING 1 的Hcpumain frequency;frequency reduction;function;,
Hcpuenter;idlethread;后,main frequency;will;decrease;,相corresponding;Hcpu的PA31口的PWMfrequency;也will;change;
解决方案1:
disable;#define BSP_PM_FREQ_SCALING 1宏,sacrifice;hcpu亮屏power consumption;
根本解决方案2:
Switch to PB port to output PWM;

9.3 睡眠下output;PWM方法

application;场景:In order to continue outputting PWM in sleep mode, it is necessary to use pwm controlled by Lcpu lptim3;
use;方法:
1、需need;during;menuconfig 使能Lcpu 的lptim3timer;及PWM
alt text

alt text

2、Set the corresponding pin to lptim3_pwm mode;,比如PB44,常用的引脚为PB43~PB46

HAL_PIN_Set(PAD_PB44, LPTIM3_OUT, PIN_NOPULL, 0)
MODIFY_REG(hwp_lpsys_aon->DBGMUX,LPSYS_AON_DBGMUX_PB44_SEL_Msk,                        MAKE_REG_VAL(1,LPSYS_AON_DBGMUX_PB44_SEL_Msk,LPSYS_AON_DBGMUX_PB44_SEL_Pos));

3、define;宏PM_WAKEUP_PIN_AS_OUTPUT_IN_SLEEP 让睡眠下也继续output;
alt text
4、修change;sdk1.1.3前原有的bug,后续sdkwill;更新

alt text


alt text

5、reference;代码as follows;:use;PB44作为pwmoutput;

alt text

6、use;note;matters;:
Since PB44 also outputs waveforms when sleep is enabled, other wake-up pins of PB need to be connected to fixed pull-up or pull-down hardware, otherwise there will be leakage current;。

9.4 55Xseries;,56Xseries;,52Xsystem; PWM差异

1,The 55X series can only output PWM with fixed IO ports;,reference;## 9.2,Whereas in the 56X and 52X series, any port with PXXX_TIM functionality can output PWM;
alt text
2. If the 56X, 55X, and 52X series use Hcpu to output PWM and enable the Hcpu main frequency reduction function with #define BSP_PM_FREQ_SCALING 1, after Hcpu enters the idle thread, the main frequency will decrease, and the corresponding PWM frequency of Hcpu’s PA port will also change;,
解决方案:
55X,56Xseries;,change;用PB口,or;sacrifice;亮屏power consumption;disable;BSP_PM_FREQ_SCALINGmain frequency;frequency reduction;function;,
52Xseries;,can;adopt;GPTIM2output;PWM,因为specially;done;process;,GPTIM2的PWMnot affected;system;frequency;change;的impact;,

3,56X,52Xseries;,能output;PWM的IO口,both;can;configure;任一个TIM和channel,
reference;pwm_config.h中,对pwmcorresponding;的cpusupport;的timer;TIM和channel,比如:

HAL_PIN_Set(PAD_PA31, GPTIM1_CH3, PIN_NOPULL, 1);
HAL_PIN_Set(PAD_PA31, GPTIM1_CH1, PIN_NOPULL, 1); 

9.5 同一个PWM的different;channelconnect;to;different;device;的note;matters;

1,52x的PWM的different;channelcan;connect;to;different;device;的,typical;application;include;backlight;和motor;;
对于同一个PWM的different;channel,only;能configure;成同样的period;,但can;configure;成different;的duty cycle;;
下figure;is;,backlight;和motor;both;configure;1kHzwhen;,respective;channel;output;的waveform;as follows;:
alt text
2,当backlight;和motor;must;need;configure;成different;period;when;,backlight;during;亮屏的when;候is;always;use;的,unable;achieve;和motor;time-sharing;multiplexing;。so;,需need;backlight;和motor;configure;to;different;的PWM。
HDK52Xonly;有PWM3not affected;automatic;frequency reduction;impact;,so;generally;backlight;use;PWM3,motor;only;能choose;use;PWM2;
due to;PWM2 will;with;automatic;frequency reduction;change;output;waveform;的period;,so;best;,during;motor;use;period;consider;not;frequency reduction;。
reference;implementation;as follows;:

alt text