1

I'm developing a program on a NRF52832 microcontroller in c in the Netbeans IDE that receives data from an external ADC via SPI and outputs a PWM signal with a duty cycle that depends on the data from the ADC.

I got the example code for the SPI driver and the PWM driver working in separate projects and now I'm trying to combine the two. However, when I insert a function from, say, the PWM driver to the SPI driver and insert the corresponding include file in the SPI driver, I get stuck in an if loop in a source file that was included as part of the example code. I've reached a point where I don't even know how to start troubleshooting, so any suggestions or insights are appreciated.

I'm currently working from an SPI driver that is working fine until I add the following code in main.c:

ret_code_t err_code;

/* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 5. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(125L, 5);

/* Switch the polarity of the second channel. */
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM1);
app_pwm_channel_duty_set(&PWM1, 0, Duty);

When I debug, everything is fine until I get to the funtion

err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);

When I step through this function, it does a bunch of behind-the-scenes initialization then gets stuck in the following function:

/**
 * Function examines current header and omits pushed strings and packets which are in progress.
 */
static bool invalid_packets_pushed_str_omit(nrf_log_header_t const * p_header, uint32_t * p_rd_idx)
{
    bool ret = false;
    if ((p_header->base.generic.type == HEADER_TYPE_PUSHED) || (p_header->base.generic.in_progress == 1))
    {
        if (p_header->base.generic.in_progress == 1)
        {
            switch (p_header->base.generic.type)
            {
            case HEADER_TYPE_STD:
                *p_rd_idx += (HEADER_SIZE + p_header->base.std.nargs);
                break;
            case HEADER_TYPE_HEXDUMP:
                *p_rd_idx += (HEADER_SIZE + p_header->base.hexdump.len);
                break;
            default:
                ASSERT(0);
                break;
            }
        }
        else
        {
            *p_rd_idx +=
                    (PUSHED_HEADER_SIZE + p_header->base.pushed.len + p_header->base.pushed.offset);
        }
        ret = true;
    }
    return ret;
}

Here is the rest of my main.c code (everything that has to do with pwm was added by me, the rest is example code for an SPI driver):

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "/Users/tom/opt/nRF5_SDK_15.0.0/nRF5_SDK_15.0.0_a53641a/components/libraries/pwm/app_pwm.h" 

#define SPI_INSTANCE  0 /**< SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */

#define TEST_STRING "Nordic"
static uint8_t       m_tx_buf[] = TEST_STRING;           /**< TX buffer. */
static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];    /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */


//PWM INITIALIZATION (Added)
uint32_t Duty = 90;

APP_PWM_INSTANCE(PWM1,1);                   // Create the instance "PWM1" using TIMER1.

static volatile bool ready_flag;            // A flag indicating PWM status.

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}

//End added

/**
 * @brief SPI user event handler.
 * @param event
 */

void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                       void *                    p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
    if (m_rx_buf[0] != 0)
    {
        NRF_LOG_INFO(" Received:");
        NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
    }
}

int main(void)
{
    //Added

    ret_code_t err_code;

    /* 1-channel PWM, 100000 microseconds period = 0.1 second , output on pin 5. */
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(125L, 5);

    /* Switch the polarity of the second channel. */
    pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
    app_pwm_channel_duty_set(&PWM1, 0, Duty);

    //End Added


 //   bsp_board_init(BSP_INIT_LEDS);

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SPI_SS_PIN;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));

    NRF_LOG_INFO("SPI example started.");

    while (1)
    {
        // Reset rx buffer and transfer done flag
        memset(m_rx_buf, 0, m_length);
        spi_xfer_done = false;

        APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));

        while (!spi_xfer_done)
        {
            __WFE();
        }

        NRF_LOG_FLUSH();

 //       bsp_board_led_invert(BSP_BOARD_LED_0);
 //       nrf_delay_ms(200);
    }
}

Any and all suggestions are greatly appreciated! If I omitted any critical information please ask for clarification and I will provide what I can.

UPDATE

Hey guys, thanks a bunch for the responses. I've created an Imgur album of the loop as I debug with (hopefully) all the relevant variables and their values on the left side of the screen in the images: https://i.stack.imgur.com/VoPHh.jpg

As you can see in the album, the pointer *p_rd_idx endlessly increments inside nrf_log_frontend.c and for some reason never reaches the end of the function.

@Yunnosch: I found the definition of ASSERT:

#define ASSERT(expr)

but I don't think it will be useful since the value of expr is OUT_OF_SCOPE and when I try to right-click and go to the declaration of expr, nothing happens.

  • 1
    *gets stuck in the following function*, describe more precisely the problem... Which line, what are parameter values, etc. – Jean-Baptiste Yunès May 22 '18 at 04:30
  • 1
    Good, Properly asked question. I feel like the only possible way to get stuck in invalid_packets_pushed_str_omit is improper value of p_header->base.generic.type . then it hits ASSERT(0) and then blocks. – Devidas May 22 '18 at 04:56
  • Can you show the definition of `ASSERT();`? – Yunnosch May 22 '18 at 05:14
  • What is the value of `p_header->base.generic.type`? What should it be? – Yunnosch May 22 '18 at 05:14
  • What is the definition of `PUSHED_HEADER_SIZE`? Is this a multithreaded application? Is `invalid_packets_pushed_str_omit()` being called from an interrupt? Check that the stack pointer has not exceeded the bounds of your stack space. – kkrambo May 22 '18 at 12:34

0 Answers0