I want to use the eFlexPWM peripheral in independent operation to produce a PWM.
The example that i found in SDK of mimxrt1170evkB and its predecessor mimxrt1170evk are for complimentary operation. I tried to lookup for some examples online but even that didn't help. So after reading the documents. and comparing the original SDK example i managed to come up with the following code.
#include "fsl_pwm.h"
#include "fsl_iomuxc.h"
#define PWM_BASEADDR PWM1
#define PWM_SUBMODULE kPWM_Module_2
#define PWM_CHANNEL kPWM_PwmA
#define PWM_FREQ_HZ 10000U
#define DEMO_PWM_CLOCK_DEVIDER kPWM_Prescale_Divide_1
#define PWM_SRC_CLK_FREQ CLOCK_GetRootClockFreq(kCLOCK_Root_Bus)
int init_pwm(void) {
pwm_config_t pwmConfig;
pwm_signal_param_t pwmSignal;
pwm_fault_param_t faultConfig;
PWM_GetDefaultConfig(&pwmConfig);
#ifdef DEMO_PWM_CLOCK_DEVIDER
pwmConfig.prescale = DEMO_PWM_CLOCK_DEVIDER;
#endif
pwmConfig.reloadLogic = kPWM_ReloadImmediate;
pwmConfig.pairOperation = kPWM_Independent;
pwmConfig.enableDebugMode = true;
pwmConfig.clockSource = kPWM_Submodule0Clock;
pwmConfig.enableDebugMode = true;
if (PWM_Init(PWM_BASEADDR, kPWM_Module_2, &pwmConfig) == kStatus_Fail)
{
// PRINTF("PWM initialization failed\n");
return 1;
}
// uint16_t pwmHighPulse = 1 * 100/ 100UL; // PWM counter value for PWM_A
// pwmHighPulse[1] = PWM_MODULO * dutyCyclePercent[1] / 100UL; // PWM counter value for PWM_B
/* Center aligned PWM */
// if(g_pwmMode == 0)
//// {
// (PWM_Type*)PWM_BASEADDR->SM.VAL2 = PWM_VAL2_VAL2((uint16_t)(-(pwmHighPulse / 2)));
// (PWM_Type*)PWM_BASEADDR->SM.VAL3 = PWM_VAL3_VAL3((uint16_t)((pwmHighPulse / 2)));
// (PWM_Type*)PWM_BASEADDR -> SM[0].VAL4 = PWM_VAL4_VAL4((uint16_t)(-(pwmHighPulse[1] / 2)));
// (PWM_Type*)PWM_BASEADDR -> SM[0].VAL5 = PWM_VAL5_VAL5((uint16_t)( (pwmHighPulse[1] / 2)));
// PWM_FaultDefaultConfig(&faultConfig);
// PWM_SetupFaultDisableMap(PWM_BASEADDR, kPWM_Module_2, kPWM_PwmA, kPWM_faultchannel_0,
// kPWM_FaultDisable_0 | kPWM_FaultDisable_1 | kPWM_FaultDisable_2 | kPWM_FaultDisable_3);
pwmSignal.pwmChannel = kPWM_PwmA;
pwmSignal.level = kPWM_HighTrue;
pwmSignal.dutyCyclePercent = 50; /* 1 percent dutycycle */
pwmSignal.pwmchannelenable = true;
pwmSignal.faultState = kPWM_PwmFaultState0;
PWM_SetupPwm(PWM_BASEADDR, PWM_SUBMODULE, &pwmSignal, 1, kPWM_SignedCenterAligned, PWM_FREQ_HZ, PWM_SRC_CLK_FREQ / (1 << DEMO_PWM_CLOCK_DEVIDER));
// PWM_SetPwmLdok(PWM_BASEADDR, kPWM_Control_Module_2 ,true);
PWM_StartTimer(PWM_BASEADDR, kPWM_Control_Module_2);
}
int main(void) {
// Initialize hardware
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
// Initialize PWM
init_pwm();
while (1) {
// Main loop
}
}
pin_mux.c
/***********************************************************************************************************************
* This file was generated by the MCUXpresso Config Tools. Any manual edits made to this file
* will be overwritten if the respective MCUXpresso Config Tools is used to update this file.
**********************************************************************************************************************/
/*
* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
!!GlobalInfo
product: Pins v16.0
processor: MIMXRT1176xxxxx
package_id: MIMXRT1176DVMAA
mcu_data: ksdk2_0
processor_version: 16.2.0
board: MIMXRT1170-EVKB
* BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS ***********
*/
#include "fsl_common.h"
#include "fsl_iomuxc.h"
#include "pin_mux.h"
/* FUNCTION ************************************************************************************************************
*
* Function Name : BOARD_InitBootPins
* Description : Calls initialization functions.
*
* END ****************************************************************************************************************/
void BOARD_InitBootPins(void) {
BOARD_InitPins();
}
/*
* TEXT BELOW IS USED AS SETTING FOR TOOLS *************************************
BOARD_InitPins:
- options: {callFromInitBoot: 'true', coreID: cm7, enableClock: 'true'}
- pin_list:
- {pin_num: M15, peripheral: LPUART1, signal: RXD, pin_signal: GPIO_AD_25}
- {pin_num: L13, peripheral: LPUART1, signal: TXD, pin_signal: GPIO_AD_24}
- {pin_num: M13, peripheral: PWM1, signal: 'A, 2', pin_signal: GPIO_AD_04}
* BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR TOOLS ***********
*/
/* FUNCTION ************************************************************************************************************
*
* Function Name : BOARD_InitPins, assigned for the Cortex-M7F core.
* Description : Configures pin routing and optionally pin electrical features.
*
* END ****************************************************************************************************************/
void BOARD_InitPins(void) {
CLOCK_EnableClock(kCLOCK_Iomuxc); /* LPCG on: LPCG is ON. */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_04_FLEXPWM1_PWM2_A, /* GPIO_AD_04 is configured as FLEXPWM1_PWM2_A */
0U); /* Software Input On Field: Input Path is determined by functionality */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_24_LPUART1_TXD, /* GPIO_AD_24 is configured as LPUART1_TXD */
0U); /* Software Input On Field: Input Path is determined by functionality */
IOMUXC_SetPinMux(
IOMUXC_GPIO_AD_25_LPUART1_RXD, /* GPIO_AD_25 is configured as LPUART1_RXD */
0U); /* Software Input On Field: Input Path is determined by functionality */
// IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_04_FLEXPWM1_PWM2_A, 0x10B0u);
}
/***********************************************************************************************************************
* EOF
**********************************************************************************************************************/
i am trying to configure the user LED present in mimxrt1170evkb board and hence control its brightness, but even after countless attempts i couldn't achieve that. The LED is completely unresponsive. i even varied the duty-cycle from 0-100. But when i configure it for normal LED blinking test, it works. So at-least its not a hardware issue.
Also the SDK example for complementary operation works. with the same PWM module so its not an issue with the module either.
if someone can review my code and see if i missed something it will be of great help.
Appreciate your effort.
Thank you!!
Hi @mithun_ccc,
The user leds that are on the mimxrt1170evkB and mimxrt1170evk are connected via a transistor, this meaning that they can only be on or off, you can check this on the schematics of each evk.
I recommend you test your code with an external led in order to see if the configuration is correct.
Please let me know your results.