0

我在 k_fifo-useage 中有一个奇怪的错误:

Basis是来自nordic的“外设uart”项目

我想回答通过蓝牙发送的特定命令

static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
          uint16_t len)
{
int err;
char addr[BT_ADDR_LE_STR_LEN] = {0};
struct answer_buffer_t *buf;
    uint8_t answer[UART_BUF_SIZE];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));

LOG_INF("Received %i data %s from: %s", len, data, log_strdup(addr));
                           

    if (strncmp(data, "&I", 2) == 0 )
    {
    uint8_t stringbuffer[100];
      buf = k_malloc(sizeof(*buf));
      iCounter++;
      LOG_INF("&I %i", iCounter);
      buf->len = sprintf(stringbuffer, "&I %i\n", iCounter);
      memcpy(stringbuffer, &buf, buf->len);

      k_fifo_put(&answer_buffer, buf);
    }
}

和蓝牙发送者线程:

void ble_write_thread(void)
{
/* Don't go any further until BLE is initialized */
k_sem_take(&ble_init_ok, K_FOREVER);

for (;;) {
    /* Wait indefinitely for data to be sent over bluetooth */
    struct answer_buffer_t *buf = k_fifo_get(&answer_buffer,
                         K_FOREVER);

    if (bt_nus_send(NULL, buf->data, buf->len)) {
        LOG_WRN("Failed to send data over BLE connection");
    }

    k_free(buf);
  }
}
K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
    NULL, PRIORITY, 0, 0);

但是当我发送“&I”zephyr 崩溃时:

[00:00:34.164,001] [0m<inf> peripheral_uart: &I 1[0m
[00:00:35.179,046] [1;31m<err> os: r0/a1:  0x00000004  r1/a2:  0x000000d1  r2/a3:  0x00000001[0m
[00:00:35.179,046] [1;31m<err> os: r3/a4:  0x0001dbbd r12/ip:  0x00000000 r14/lr:  0x00011d2b[0m
[00:00:35.179,046] [1;31m<err> os:  xpsr:  0x41000000[0m
[00:00:35.179,046] [1;31m<err> os: Faulting instruction address (r15/pc): 0x00028d2c[0m
[00:00:35.179,077] [1;31m<err> os: >>> ZEPHYR FATAL ERROR 4: Kernel panic on CPU 0[0m
[00:00:35.179,077] [1;31m<err> os: Current thread: 0x200017c8 (unknown)[0m
[00:00:35.376,464] [1;31m<err> fatal_error: Resetting system[0m

我做错了什么?

4

1 回答 1

0

我发现了这一行:

LOG_INF("Received %i data %s from: %s", len, data, log_strdup(addr));

没有这个它的工作,但我在这里有另一个错误(由我的尝试和错误引起):

这是正确的代码:

    if (strncmp(data, "&I", 2) == 0 )
    {
      buf = k_malloc(sizeof(*buf));
      iCounter++;
      LOG_INF("&I %i", iCounter);
      buf->len = sprintf(buf->data, "&I %i\n", iCounter);
      k_fifo_put(&answer_buffer, buf);
    }
于 2022-02-03T17:33:16.673 回答