卡在示例代码

时间:2018-05-22 04:10:53

标签: c microcontroller spi pwm nrf52

我正在使用Netbeans IDE中的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_idxnrf_log_frontend.c内无休止地递增,并且由于某种原因永远不会到达函数的末尾。

@Yunnosch:我找到了ASSERT的定义:

#define ASSERT(expr)

但我认为它不会有用,因为expr的值是OUT_OF_SCOPE,当我尝试右键单击并转到expr的声明时,没有任何反应

0 个答案:

没有答案