Post Go back to editing

Flashing Issue on MAX32675 After PWM Switching Code

Category: Software
Product Number: MAX32675
I flashed the below PWM code on the MAX32675 EVKIT to switch PWM output between P0.9 and P0.17 inside the TMR0 interrupt.
After flashing this code, the board stopped responding to the MAX32625PICO debugger, and I couldn’t program new firmware.
I had to recover it using J-Link mass erase.
Can you please check what might be wrong in this code and why this flashing issue happened?
After loading this firmware:
  • PWM switches initially,
  • But after reset the debugger stops detecting the MCU,
  • Only J-Link mass erase brings the board back.
Can you please review the code and help identify what is causing this issue?
Thanks,
Nandan
/******************************************************************************/

#include <stdio.h>
#include <stdint.h>
#include "mxc_device.h"
#include "mxc_sys.h"
#include "nvic_table.h"
#include "gpio.h"
#include "board.h"
#include "tmr.h"
#include "led.h"

#define CLOCK_SOURCE MXC_TMR_IBRO_CLK
#define PWM_TIMER0 MXC_TMR0

uint8_t timerPinFlag9 = 1;
uint8_t timerPinFlag17 = 0;

void PWMTimer0(uint32_t PwmFre, uint32_t Dutycycle)
{
    mxc_tmr_cfg_t tmr;

    unsigned int periodTicks = MXC_TMR_GetPeriod(PWM_TIMER0, CLOCK_SOURCE, 16, PwmFre);
    unsigned int dutyTicks   = periodTicks * Dutycycle / 100;

    MXC_TMR_Shutdown(PWM_TIMER0);

    tmr.pres    = TMR_PRES_16;
    tmr.mode    = TMR_MODE_PWM;
    tmr.clock   = CLOCK_SOURCE;
    tmr.cmp_cnt = periodTicks;
    tmr.pol     = 1;

    MXC_TMR_Init(PWM_TIMER0, &tmr, true);
    MXC_TMR_SetPWM(PWM_TIMER0, dutyTicks);
    MXC_TMR_Start(PWM_TIMER0);
}

void GPIO_Config_TMR09(uint8_t alt)
{
    mxc_gpio_cfg_t cfg;
    cfg.port   = MXC_GPIO0;
    cfg.mask   = MXC_GPIO_PIN_9;
    cfg.func   = alt;
    cfg.pad    = MXC_GPIO_PAD_NONE;
    cfg.vssel  = MXC_GPIO_VSSEL_VDDIO;
    cfg.drvstr = MXC_GPIO_DRVSTR_0;
    MXC_GPIO_Config(&cfg);
}

void GPIO_Config_TMR017(uint8_t alt)
{
    mxc_gpio_cfg_t cfg;
    cfg.port   = MXC_GPIO0;
    cfg.mask   = MXC_GPIO_PIN_17;   // now correctly 17
    cfg.func   = alt;
    cfg.pad    = MXC_GPIO_PAD_NONE;
    cfg.vssel  = MXC_GPIO_VSSEL_VDDIO;
    cfg.drvstr = MXC_GPIO_DRVSTR_0;
    MXC_GPIO_Config(&cfg);
}

void PwmTimerHandler(void)
{
    MXC_TMR_ClearFlags(PWM_TIMER0);

    if (timerPinFlag9) {
        timerPinFlag9  = 0;
        timerPinFlag17 = 1;

        GPIO_Config_TMR017(4);   // P0.17 → timer
        GPIO_Config_TMR09(1);    // P0.9 → alt1

        PWMTimer0(2000, 30);
    }
    else {
        timerPinFlag9  = 1;
        timerPinFlag17 = 0;

        GPIO_Config_TMR09(4);    // P0.9 → timer
        GPIO_Config_TMR017(1);   // P0.17 → alt1

        PWMTimer0(1000, 50);
    }
}

int main(void)
{
    MXC_NVIC_SetVector(TMR0_IRQn, PwmTimerHandler);
    NVIC_EnableIRQ(TMR0_IRQn);

    GPIO_Config_TMR09(4);
    PWMTimer0(1000, 50);

    while (1) { }
    return 0;
}