Hi all,
I am attempting to tackle an MCU limitation in the PWM Module in extreme cases in 0% and 100% duty cycle. Upon reading the following threads:
- https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/p/448664/1612679#pi239031350=2
- https://e2e.ti.com/support/microcontrollers/tiva_arm/f/908/t/354826
I figured it be best that I just attempt toggling the configuration of the pins.
My current apparatus is as follows:
- Custom board utilizing the TM4C1294NCPDT MCU
- EK-TM4C1294XL Development Board as my debugger/programmer
- Code Composer Studio 6.1.2 as my coding environment
My question is, how would I go about switching between a GPIO configuration to a PWM configuration?
Currently the program initiates the pins of interest (noted in the initPWM() function below) as a PWM GPIO pin type. I am interested in figuring out a way to change said pins into an digital output and back [to PWM pin type] at users request.
Am I required to repeat the whole configuration process every time I switch between the two (see code below)? Or do I just call one of the GPIOPinTypex() functions?
void initPWM (void) {
    // Set the PWM clock to the system clock / 1.
	MAP_SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
	// TM4C1294NCPDT MCU only has one module for PWM use (M0PWMn)
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
	// Enable the ports used in the pwm configuration.
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
	// Configure pins being used for it's corresponding PWM function
	MAP_GPIOPinConfigure(GPIO_PF0_M0PWM0);
	MAP_GPIOPinConfigure(GPIO_PF1_M0PWM1);
	MAP_GPIOPinConfigure(GPIO_PF2_M0PWM2);	
	MAP_GPIOPinConfigure(GPIO_PF3_M0PWM3);	
	MAP_GPIOPinConfigure(GPIO_PG0_M0PWM4);	
	MAP_GPIOPinConfigure(GPIO_PG1_M0PWM5);	
	MAP_GPIOPinConfigure(GPIO_PK4_M0PWM6);
	MAP_GPIOPinConfigure(GPIO_PK5_M0PWM7);
    
    // Configure the PWM function for the pins used on the board.
	MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0);	// M0PWM0
	MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1);	// M0PWM1
	MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2);	// M0PWM2
	MAP_GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3);	// M0PWM3
	MAP_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_0);	// M0PWM4
	MAP_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_1);	// M0PWM5
	MAP_GPIOPinTypePWM(GPIO_PORTK_BASE, GPIO_PIN_4);	// M0PWM6
	MAP_GPIOPinTypePWM(GPIO_PORTK_BASE, GPIO_PIN_5);	// M0PWM7
    // Configure the PWM0 to count up/down without synchronization.
	MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
	MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
	MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
	MAP_PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
	// Default period during setup is 1% duty cycle at 10KHz
    MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 12000);
	MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, 12000);
	MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, 12000);
	MAP_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, 12000);
	// Start up duty cycle is set at 1% as starting at 0% causes issues with the PWM output.
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_0) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_1) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_2) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_3) * 0.01);
	MAP_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM0_BASE, PWM_GEN_3) * 0.01);
    // Enable the PWM generator block to output PWM
	MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_0);
	MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_1);
	MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_2);
	MAP_PWMGenEnable(PWM0_BASE, PWM_GEN_3);
	// Default the PWM outputs to normally closed during initialization and start up.
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_1_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_3_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_4_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_5_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, true);
	MAP_PWMOutputState(PWM0_BASE, PWM_OUT_7_BIT, true);
}
void initDigital(void) {
    // Enables ports that digital pins are attached to
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP);
    // Configure correspoding pins as digital outputs
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_1); // DIG1
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_0); // DIG2
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_5); // DIG3
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_4); // DIG4
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_3); // DIG5
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_2); // DIG6
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1); // DIG7
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_2); // DIG8    
    // Configure outputs with weak pull-down and output current of up to 4 mA.
    // DIG2, DIG1, DIG8
    MAP_GPIOPadConfigSet(GPIO_PORTP_BASE, (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2), 
        GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPD);
    // DIG7, DIG6, DIG5, DIG4, DIG3
    MAP_GPIOPadConfigSet(GPIO_PORTN_BASE, (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5), 
        GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPD);
}
								 
				 
		 
					 
                           
				