首页 > 解决方案 > 从示例代码卡在源文件中的函数中

问题描述

我正在 Netbeans IDE 中的 C 中的 NRF52832 微控制器上开发一个程序,该程序通过 SPI 从外部 ADC 接收数据并输出占空比取决于来自 ADC 的数据的 PWM 信号。

我得到了在不同项目中工作的 SPI 驱动程序和 PWM 驱动程序的示例代码,现在我正在尝试将两者结合起来。但是,当我从 PWM 驱动程序向 SPI 驱动程序插入一个函数并在 SPI 驱动程序中插入相应的包含文件时,我陷入了作为示例代码一部分包含的源文件中的 if 循环。我已经到了一个我什至不知道如何开始故障排除的地步,所以任何建议或见解都会受到赞赏。

我目前正在使用工作正常的 SPI 驱动程序,直到我在 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);

当我调试时,一切都很好,直到我得到这个功能

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

当我单步执行此函数时,它会进行一系列幕后初始化,然后卡在以下函数中:

/**
 * 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;
}

这是我的 main.c 代码的其余部分(与 pwm 相关的所有内容都是我添加的,其余的是 SPI 驱动程序的示例代码):

#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);
    }
}

非常感谢任何和所有建议!如果我遗漏了任何关键信息,请要求澄清,我会尽我所能。

更新

嘿伙计们,非常感谢您的回复。我已经创建了一个循环的 Imgur 专辑,因为我使用(希望)图像中屏幕左侧的所有相关变量及其值进行调试:https ://imgur.com/a/HYah1yC

正如您在专辑中看到的那样,指针*p_rd_idx在内部不断增加,nrf_log_frontend.c并且由于某种原因永远不会到达函数的末尾。

@Yunnosch:我找到了以下定义ASSERT

#define ASSERT(expr)

但我认为它没有用,因为 is 的值,exprOUT_OF_SCOPE我尝试右键单击并转到 的声明时expr,什么也没有发生。

标签: cmicrocontrollerspipwmnrf52

解决方案


推荐阅读