C++ function occasionally fails to turn off PWM

Hi,

I am controlling Xavier Nx’s PWM from a C++ file.

I have to occasionally turn ON/OFF the PWM waveforms and to achieve this I have implemented a function which uses basic file operations like fopen(), fprintf.

    char enable_path[64] = "/sys/devices/3280000.pwm/pwm/pwmchip0/pwm0/enable";
    FILE* f_en = NULL;
    f_en = fopen(enable_path, "w");
    if(f_en != NULL)
    {
        switch (state)
        {
        case START:
            fprintf(f_en, "1");
            break;
        case STOP:
            fprintf(f_en, "0");
            break;
        default:
            break;
        }
        fclose(f_en);
    }
    else
    {
        printf("%s\n", strerror(errno));
    }

This function is able to successfully turn ON/OFF the PWM for most of the times but it also fails to turn it OFF quite often. When this function fails to turn it off, I try executing following command as root user and successfully turn the PWM off,

echo 0 > /sys/devices/3280000.pwm/pwm/pwmchip0/pwm0/enable

I am thinking that perhaps the device was just busy. When this fails, does it have a successfull fopen? Or does fopen fail? If fopen succeeds, could you check the return value of fprintf and see if it wrote the expected number of characters (which would be 1 since it always writes either 0 or 1)?

Hi

I tried several times to reproduce the fault today but failed to do so. I am pretty sure that this will come again. Sorry to keep you waiting. Let’s wait a day or so to see this issue again.

We were having multiple threads trying to control the PWM without any mutex protection.
Probably this was the issue. After adding mutex, I did not see the issue again.

Thanks :)

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.