Configure and Debug Clock Control

Hello!

I don't know if there is a way to configure a nrf52832-QFAA's clock source on my custom board software-wise.

Just a disclaimer but please correct me if I'm wrong anywhere because there is little to no reliable tutorial for clock control and I'm also just as confused as you might be when looking at the code snippet I'm providing.

We were planning to use the 32MHz high frequency internal RC:

This is the snippet for the clock control section of the application:

dts file:

```

&clock {
    status = "okay";
   
    lf_clk {
        clock-source = <0>; /* RC oscillator */
        calibration = <1>; /*optional; default is true for RC*/
    };
};

```

defconfig file: 

```

# Enable Clock Control
CONFIG_CLOCK_CONTROL=y
CONFIG_CLOCK_CONTROL_NRF=y

# Enable DCDC and internal low freq clock
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y 

```

main.c

```

int start_hfclk(void)
{
    const struct device *clock_dev = DEVICE_DT_GET(DT_NODELABEL(clock));
   
    if (!device_is_ready(clock_dev)) {
        LOG_ERR("Clock device not ready");
        return -1;
    }

    // Start HFCLK - clock_control_on returns 0 on success
    int ret = clock_control_on(clock_dev, CLOCK_CONTROL_NRF_SUBSYS_HF);
    if (ret != 0) {
        LOG_ERR("Failed to start HFCLK: %d", ret);
        return -1;
    }

    // Wait for HFCLK to be ready
    int timeout = 5000; // Timeout in milliseconds
    int elapsed = 0;

    while (elapsed < timeout) {
        enum clock_control_status status = clock_control_get_status(clock_dev, CLOCK_CONTROL_NRF_SUBSYS_HF);
        if (status == CLOCK_CONTROL_STATUS_ON) {
            break; // HFCLK is ready
        }
        k_msleep(10);
        elapsed += 10;
    }

    if (elapsed >= timeout) {
        LOG_ERR("HFCLK ready timeout");
        return -1;
    }

    // Workaround for anomaly [68]: Add delay to ensure clock stability
    k_msleep(1); // Small delay to ensure HFCLK is truly stable
   
    LOG_INF("HFCLK is ready and stable");
    return 0;
}

```

The application works fine at first but then for some reason the clock_control_on() function returns error code 11 and stopped working ever since. I'm not sure if it's a problem software-wise or hardware-wise, since my group is using a custom designed board. My groupmate already made another private ticket for hardware debugging, but I want to see if there's any possible solution software-wise because this doesn't seem ideal.

Thank you for your help, and sorry if this code snippet confuses you in any way.

Regards,

Jackie

Related