树莓派串口2挂ESP32C3(推荐)

1---展示树莓派4B所有串口   dtoverlay -a | grep uart

查看特定串口信息 --> dtoverlay -h uart0

查看特定串口信息 --> dtoverlay -h uart1 

查看特定串口信息 --> dtoverlay -h uart2

看上去0 1雷同 采用UART2吧

 树莓派4B的CPU系统里查到为BCM2835而非BCM2711 - 啊哈彭 - 博客园

查询我的CPU

cat /proc/cpuinfo

 满足条件

如何开始U2呢

树莓派4使用2-5号串口_wxtcstt的专栏-CSDN博客

本来干干净净的PI只有这个 我修改为
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

++++++++++++++

dtoverlay=uart2 

也就是 在 /boot/config.txt  del 添加 dtoverlay=uart2 

重新启动在/dev目录下发现新的串口 /dev/ttyAMA1


pi@raspberrypi:~ $ ls -l /dev

crw-rw---- 1 root dialout 204,  64 Oct 20 03:42 ttyAMA0
crw-rw---- 1 root dialout 204,  65 Oct 20 03:41 ttyAMA1

现在开始验证U2 也就是

UART2:
TXD2-->GPIO0
RXD2-->GPIO1

准备文件

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
 
int main(void)
{
    int hs1;
    int snum = 0;
 
    wiringPiSetup();                        // 使用wiring编码去初始化GPIO序号
    //hs1 = serialOpen("/dev/ttyS0", 115200); // 打开 /dev/ttyS0 串口设备,波特率115200
    hs1 = serialOpen("/dev/ttyAMA0", 115200);
    printf("ttyS0 uart test1:\n");          // 终端打印
    serialPrintf(hs1, "Hello World!\r\n");  // 串口打印
    serialPrintf(hs1, "Enter 10 letters:\r\n");
 
    /* 串口缓存区读取10个字节,并完整输出接收到的数据 */
    do{
        snum = serialDataAvail(hs1);        // 获取串口接收缓存区的可用字节数
    }while(snum < 10);
 
    while(snum--)
    {
        serialPutchar(hs1,serialGetchar(hs1));  // 通过串口发送字节
    }
    serialPrintf(hs1, "\r\nserial close\r\n\r\n");  // 串口打印
 
    serialClose(hs1);                       // 关闭串口
    return 0;
}

   14  sudo apt-get install wiringpi
   15  gpio -v
   17  gcc -Wall -o c c1.c   -lwiringPi
   18  ls

可以了  接线是

+++++到这里串口好了+++++++++++

直接执行

sudo hciattach -s 115200 /dev/ttyAMA1 any 115200 noflow nosleep

也就是HCI安装在U2 不要硬件流控 效果

还没有连接ESP32 现在就是刚刚蓝色TTL

看到刚刚的窗口 已经变化了 收到了 HCI协议的东西 就是刚刚的指令

测试一下 发送 sudo hciconfig hci1 up
 

收到的数据是01 03 10 00 ---01 01 10 00---- 01 09 10 00

后面再看


 sudo btattach -N -B /dev/ttyAMA1 -S 115200

sudo hciattach -s 115200 /dev/ttyAMA1 any 115200 noflow nosleep

两种都是挂起来0

现在修改ESP32C3        代码 不要硬件的2个IO

 #define GPIO_UART_RTS_OUT  (-1)
#define GPIO_UART_CTS_IN   (-1)

D:\A-ESP-IDF\examples\bluetooth\hci\controller_hci_uart_esp32c3\main\uhci_uart_demo.c

/*
   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include <string.h>
#include "driver/periph_ctrl.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "soc/lldesc.h"
#include "esp_private/gdma.h"
#include "hal/uhci_ll.h"
#include "esp_bt.h"
#include "esp_log.h"

static const char *tag = "UHCI";

#define UART_HCI_NUM       (1)

#define UART_RX_THRS       (120)

#define GPIO_UART_TXD_OUT  (4)
#define GPIO_UART_RXD_IN   (5)
#define GPIO_UART_RTS_OUT  (-1)
#define GPIO_UART_CTS_IN   (-1)

#define GPIO_OUTPUT_PIN_SEL  ((1ULL<<GPIO_UART_TXD_OUT) | (1ULL<<GPIO_UART_RTS_OUT))
#define GPIO_INPUT_PIN_SEL   ((1ULL<<GPIO_UART_RXD_IN) | (1ULL<<GPIO_UART_CTS_IN))

// Operation functions for HCI UART Transport Layer
static bool hci_uart_tl_init(void);
static void hci_uart_tl_deinit(void);
static void hci_uart_tl_recv_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg);
static void hci_uart_tl_send_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg);
static void hci_uart_tl_flow_on(void);
static bool hci_uart_tl_flow_off(void);
static void hci_uart_tl_finish_transfers(void);

struct uart_txrxchannel {
    esp_bt_hci_tl_callback_t callback;
    void *arg;
    lldesc_t link;
};

struct uart_env_tag {
    struct uart_txrxchannel tx;
    struct uart_txrxchannel rx;
};

struct uart_env_tag uart_env;

static volatile uhci_dev_t *s_uhci_hw = &UHCI0;
static gdma_channel_handle_t s_rx_channel;
static gdma_channel_handle_t s_tx_channel;

static esp_bt_hci_tl_t s_hci_uart_tl_funcs = {
    ._magic = ESP_BT_HCI_TL_MAGIC_VALUE,
    ._version = ESP_BT_HCI_TL_VERSION,
    ._reserved = 0,
    ._open = (void *)hci_uart_tl_init,
    ._close = (void *)hci_uart_tl_deinit,
    ._finish_transfers = (void *)hci_uart_tl_finish_transfers,
    ._recv = (void *)hci_uart_tl_recv_async,
    ._send = (void *)hci_uart_tl_send_async,
    ._flow_on = (void *)hci_uart_tl_flow_on,
    ._flow_off = (void *)hci_uart_tl_flow_off,
};

static bool hci_uart_tl_init(void)
{
    return true;
}

static void hci_uart_tl_deinit(void)
{
}

static IRAM_ATTR void hci_uart_tl_recv_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg)
{
    assert(buf != NULL);
    assert(size != 0);
    assert(callback != NULL);
    uart_env.rx.callback = callback;
    uart_env.rx.arg = arg;

    memset(&uart_env.rx.link, 0, sizeof(lldesc_t));
    uart_env.rx.link.buf = buf;
    uart_env.rx.link.size = size;

    s_uhci_hw->pkt_thres.thrs = size;

    gdma_start(s_rx_channel, (intptr_t)(&uart_env.rx.link));
}

static IRAM_ATTR void hci_uart_tl_send_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg)
{
    assert(buf != NULL);
    assert(size != 0);
    assert(callback != NULL);

    uart_env.tx.callback = callback;
    uart_env.tx.arg = arg;

    memset(&uart_env.tx.link, 0, sizeof(lldesc_t));
    uart_env.tx.link.length = size;
    uart_env.tx.link.buf = buf;
    uart_env.tx.link.eof = 1;

    gdma_start(s_tx_channel, (intptr_t)(&uart_env.tx.link));
}

static void hci_uart_tl_flow_on(void)
{
}

static bool hci_uart_tl_flow_off(void)
{
    return true;
}

static void hci_uart_tl_finish_transfers(void)
{
}

static IRAM_ATTR bool hci_uart_tl_rx_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
{
    assert(dma_chan == s_rx_channel);
    assert(uart_env.rx.callback != NULL);
    esp_bt_hci_tl_callback_t callback = uart_env.rx.callback;
    void *arg = uart_env.rx.arg;

    // clear callback pointer
    uart_env.rx.callback = NULL;
    uart_env.rx.arg = NULL;

    // call handler
    callback(arg, ESP_BT_HCI_TL_STATUS_OK);

    // send notification to Bluetooth Controller task
    esp_bt_h4tl_eif_io_event_notify(1);

    return true;
}

static IRAM_ATTR bool hci_uart_tl_tx_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
{
    assert(dma_chan == s_tx_channel);
    assert(uart_env.tx.callback != NULL);
    esp_bt_hci_tl_callback_t callback = uart_env.tx.callback;
    void *arg = uart_env.tx.arg;

    // clear callback pointer
    uart_env.tx.callback = NULL;
    uart_env.tx.arg = NULL;

    // call handler
    callback(arg, ESP_BT_HCI_TL_STATUS_OK);

    // send notification to Bluetooth Controller task
    esp_bt_h4tl_eif_io_event_notify(1);

    return true;
}

static void uart_gpio_set(void)
{
    gpio_config_t io_output_conf = {
        .intr_type = GPIO_PIN_INTR_DISABLE,    //disable interrupt
        .mode = GPIO_MODE_OUTPUT,    // output mode
        .pin_bit_mask = GPIO_OUTPUT_PIN_SEL,    // bit mask of the output pins
        .pull_down_en = 0,    // disable pull-down mode
        .pull_up_en = 0,    // disable pull-up mode
    };
    gpio_config(&io_output_conf);

    gpio_config_t io_input_conf = {
        .intr_type = GPIO_PIN_INTR_DISABLE,    //disable interrupt
        .mode = GPIO_MODE_INPUT,    // input mode
        .pin_bit_mask = GPIO_INPUT_PIN_SEL,  // bit mask of the input pins
        .pull_down_en = 0,    // disable pull-down mode
        .pull_up_en = 0,    // disable pull-down mode
    };
    gpio_config(&io_input_conf);

    uart_set_pin(UART_HCI_NUM, GPIO_UART_TXD_OUT, GPIO_UART_RXD_IN, GPIO_UART_RTS_OUT, GPIO_UART_CTS_IN);
}

void uhci_uart_install(void)
{
    periph_module_enable(PERIPH_UHCI0_MODULE);
    periph_module_reset(PERIPH_UHCI0_MODULE);

    periph_module_enable(PERIPH_UART1_MODULE);
    periph_module_reset(PERIPH_UART1_MODULE);

    uart_gpio_set();

    // configure UART1
    uart_config_t uart_config = {
        .baud_rate = CONFIG_EXAMPLE_HCI_UART_BAUDRATE,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
        .rx_flow_ctrl_thresh = UART_RX_THRS,
        .source_clk = UART_SCLK_APB,
    };
    ESP_ERROR_CHECK(uart_param_config(UART_HCI_NUM, &uart_config));

    // install DMA driver
    gdma_channel_alloc_config_t tx_channel_config = {
        .flags.reserve_sibling = 1,
        .direction = GDMA_CHANNEL_DIRECTION_TX,
    };
    ESP_ERROR_CHECK(gdma_new_channel(&tx_channel_config, &s_tx_channel));
    gdma_channel_alloc_config_t rx_channel_config = {
        .direction = GDMA_CHANNEL_DIRECTION_RX,
        .sibling_chan = s_tx_channel,
    };
    ESP_ERROR_CHECK(gdma_new_channel(&rx_channel_config, &s_rx_channel));

    gdma_connect(s_tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_UART, 0));
    gdma_connect(s_rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_UART, 0));

    gdma_strategy_config_t strategy_config = {
        .auto_update_desc = false,
        .owner_check = false
    };
    gdma_apply_strategy(s_tx_channel, &strategy_config);
    gdma_apply_strategy(s_rx_channel, &strategy_config);

    gdma_rx_event_callbacks_t rx_cbs = {
        .on_recv_eof = hci_uart_tl_rx_eof_callback
    };
    gdma_register_rx_event_callbacks(s_rx_channel, &rx_cbs, NULL);

    gdma_tx_event_callbacks_t tx_cbs = {
        .on_trans_eof = hci_uart_tl_tx_eof_callback
    };
    gdma_register_tx_event_callbacks(s_tx_channel, &tx_cbs, NULL);

    // configure UHCI
    uhci_ll_init(s_uhci_hw);
    uhci_ll_set_eof_mode(s_uhci_hw, UHCI_RX_LEN_EOF);
    // disable software flow control
    s_uhci_hw->escape_conf.val = 0;
    uhci_ll_attach_uart_port(s_uhci_hw, 1);
}


void app_main(void)
{
    esp_err_t ret;

    uhci_uart_install();

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    bt_cfg.hci_tl_funcs = &s_hci_uart_tl_funcs;

    ret = esp_bt_controller_init(&bt_cfg);
    if (ret != ESP_OK) {
        ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
        return;
    }

    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret != ESP_OK) {
        ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret));
        return;
    }

    ESP_LOGI(tag, "HCI messages can be communicated over UART%d: \n"
             "--PINs: TxD %d, RxD %d, RTS %d, CTS %d\n"
             "--Baudrate: %d", UART_HCI_NUM,
             GPIO_UART_TXD_OUT, GPIO_UART_RXD_IN, GPIO_UART_RTS_OUT, GPIO_UART_CTS_IN,
             CONFIG_EXAMPLE_HCI_UART_BAUDRATE);
}

更换这个程序以后  发现BT的方式可以挂上去了

继续测试HCI的方式 发现是不行的

小结:

1---ESP32C3固件--需要C3修改不要硬件控制 或者在PI里面U2增加硬件的参数

2---接线--TXRX

3---PI--打开U2 加一句话

+++++++++++++++++整理这里+++++++++++++++

全新PI开始

烧录固件---SD卡加入2个文件--SD卡文件最后增加一句话dtoverlay=uart2----上电---

 sudo btattach -N -B /dev/ttyAMA1 -S 115200

重新启用一个SSH即可hciconfig看到

+++++++++++++++++++++++++++++++++++++++

换个角度看

全新开始
1----https://blog.csdn.net/weixin_44415639/article/details/115004935
ls -l /dev

显示如下
lrwxrwxrwx 1 root root           7 Sep 17 10:30 serial1 -> ttyAMA0

ls -l /dev | grep serial

2----https://www.cnblogs.com/jinsheng-79/p/15157437.html

串口查看命令:
展示所有串口 --> dtoverlay -a | grep uart
查看特定串口信息 --> dtoverlay -h uart0

3--https://blog.csdn.net/wxtcstt/article/details/114889699
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值