esb通信,也就是纯2.4g的一个通信,据说是兼容24L01+芯片,没有试过,这里是用两个nrf52832芯片做的通信实验。
相关历程参考 如下:
历程里面注释确实不多。
里面函数api介绍可以参考官方SDK文档:https://infocenter.nordicsemi.com/index.jsp
esb通信和24L01+芯片初始化内容大致是类似的,基本上就是初始化通信速度,模式,重发时间,通道,地址,等,
小区别:
esb多了一个回调函数。
地址 =基地址(4byte)+前缀地址(1byte )两部分组成,地址长度是5个字节,在24L01+中是没有这样分开配置的。同样的,除了pipe0的基地址可以改,其它pipe只有使用同样的基地址,所有的pipe前缀地址都可以改变。
//发射初始化
uint32_t esb_init_tx( uint8_t ch, uint8_t *addr )
{
clocks_start();
uint32_t err_code;
//uint8_t base_addr_0[4] = {0x57, 0xE7, 0x87, 0xE7};
//uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
nrf_esb_config.retransmit_delay = 600;
nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
nrf_esb_config.event_handler = nrf_esb_event_handler_tx;
nrf_esb_config.mode = NRF_ESB_MODE_PTX;
nrf_esb_config.selective_auto_ack = false;
err_code = nrf_esb_init(&nrf_esb_config);
err_code =nrf_esb_set_rf_channel(ch); //通信通道
err_code = nrf_esb_set_base_address_0(addr); //只用pipe 0,4个地址可变
//err_code = nrf_esb_set_base_address_1(base_addr_1);
err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
return err_code;
}
本文章是对esb通信进行了封装,方便直接调用使用
所有源码:
#include "wp_esp.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "nrf_esb.h"
#include "nrf_log.h"
static nrf_esb_payload_t tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x0, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00);
nrf_esb_payload_t rx_payload;
void nrf_esb_event_handler_rx(nrf_esb_evt_t const * p_event)
{
switch (p_event->evt_id)
{
case NRF_ESB_EVENT_TX_SUCCESS:
printf("TX SUCCESS EVENT\r\n");
break;
case NRF_ESB_EVENT_TX_FAILED:
printf("TX FAILED EVENT\r\n");
break;
case NRF_ESB_EVENT_RX_RECEIVED:
printf("RX RECEIVED EVENT\r\n");
if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
{
printf("Receiving packet: %02x\r\n", rx_payload.data[1]);
}
break;
}
}
//接收初始化
uint32_t esb_init_rx( uint8_t ch, uint8_t *addr )
{
uint32_t err_code;
//uint8_t base_addr_0[4] = {0xe7, 0xE7, 0xE7, 0xE7};
//uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
nrf_esb_config.payload_length = 8;
nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
nrf_esb_config.mode = NRF_ESB_MODE_PRX;
nrf_esb_config.event_handler = nrf_esb_event_handler_rx;
nrf_esb_config.selective_auto_ack = false;
err_code = nrf_esb_init(&nrf_esb_config);
err_code =nrf_esb_set_rf_channel(ch);
err_code = nrf_esb_set_base_address_0(addr);
//err_code = nrf_esb_set_base_address_1(base_addr_1);
err_code = nrf_esb_set_prefixes(addr_prefix, 8);
return err_code;
}
void nrf_esb_event_handler_tx(nrf_esb_evt_t const * p_event)
{
switch (p_event->evt_id)
{
case NRF_ESB_EVENT_TX_SUCCESS:
printf("TX SUCCESS EVENT\r\n");
break;
case NRF_ESB_EVENT_TX_FAILED:
printf("TX FAILED EVENT\r\n");
(void) nrf_esb_flush_tx();
(void) nrf_esb_start_tx();
break;
case NRF_ESB_EVENT_RX_RECEIVED:
printf("RX RECEIVED EVENT\r\n");
while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
{
if (rx_payload.length > 0)
{
printf("RX RECEIVED PAYLOAD %x\r\n",rx_payload.data[1]);
}
}
break;
}
}
//发射初始化
uint32_t esb_init_tx( uint8_t ch, uint8_t *addr )
{
uint32_t err_code;
//uint8_t base_addr_0[4] = {0x57, 0xE7, 0x87, 0xE7};
//uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
nrf_esb_config.retransmit_delay = 600;
nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
nrf_esb_config.event_handler = nrf_esb_event_handler_tx;
nrf_esb_config.mode = NRF_ESB_MODE_PTX;
nrf_esb_config.selective_auto_ack = false;
err_code = nrf_esb_init(&nrf_esb_config);
err_code =nrf_esb_set_rf_channel(ch);
err_code = nrf_esb_set_base_address_0(addr); //只用pipe 0,4个地址可变
//err_code = nrf_esb_set_base_address_1(base_addr_1);
err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
return err_code;
}
uint32_t esp_start_rx(void )
{
return nrf_esb_start_rx();
}
void test_send(void )
{
tx_payload.noack = false;
if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
{
//printf("Sending packet ok \r\n");
tx_payload.data[1]++;
}
else
{
printf("Sending packet failed \r\n");
}
}
发射端调用:
u8 ch =10;
u8 addr[4] ={0x57, 0xE7, 0x87, 0xE7};
esb_init_tx(ch,addr);
printf("ESP TX \r\n");
printf("Sending packet ...");
while(1)
{
test_send();
nrf_delay_ms(1000);
}
接收端使用:
u8 ch =10;
u8 addr[4] ={0x57, 0xE7, 0x87, 0xE7};
esb_init_rx(ch,addr);
esp_start_rx();
printf("ESP RX \r\n");
while (1)
{
}
可以看到,只有发射端在主任务里面做了发送处理,接收端所有的处理都在回调函数里面,比24L01芯片好用多了,这个例子也是实现了双向传输,官方的例子是单向传输。
运行效果: