-
Notifications
You must be signed in to change notification settings - Fork 0
/
NrfEsbRadioPtx.cpp
126 lines (107 loc) · 2.94 KB
/
NrfEsbRadioPtx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "nRF_Hal/NrfEsbRadioPtx.h"
#include "nrf_error.h"
#include "string.h"
volatile bool NrfEsbRadioPtx::radioBusy = false;
volatile bool NrfEsbRadioPtx::radioSuccess = false;
NrfEsbRadioPtx::NrfEsbRadioPtx(bool autoControlHfClock)
{
radioBusy = false;
radioSuccess = false;
m_enabled = false;
m_autoControlHfClock = autoControlHfClock;
}
void NrfEsbRadioPtx::On()
{
if(m_autoControlHfClock)
{
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
}
nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
nrf_esb_config.retransmit_count = 40;
nrf_esb_config.selective_auto_ack = false;
nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
nrf_esb_config.bitrate = NRF_ESB_BITRATE_1MBPS;
nrf_esb_config.event_handler = NrfEsbRadioPtx::nrfEsbEventHandler;
nrf_esb_config.mode = NRF_ESB_MODE_PTX;
nrf_esb_init(&nrf_esb_config);
m_enabled = true;
nrf_esb_set_base_address_0(m_baseAddress0);
nrf_esb_set_base_address_1(m_baseAddress1);
nrf_esb_set_prefixes(m_prefixes,8);
}
void NrfEsbRadioPtx::SetupAddress0(uint8_t *address)
{
memcpy(m_baseAddress0,address,ESB_ADDRESS_LENGTH);
if(m_enabled)
{
nrf_esb_set_base_address_0(m_baseAddress0);
}
}
void NrfEsbRadioPtx::SetupAddress1(uint8_t *address)
{
memcpy(m_baseAddress1,address,ESB_ADDRESS_LENGTH);
if(m_enabled)
{
nrf_esb_set_base_address_1(m_baseAddress1);
}
}
void NrfEsbRadioPtx::SetupAddressPrefixes(uint8_t *prefixes, uint8_t prefixesCount)
{
memcpy(m_prefixes,prefixes,ESB_PREFIXES_COUNT);
if(m_enabled)
{
nrf_esb_set_prefixes(prefixes, prefixesCount);
}
}
void NrfEsbRadioPtx::Off()
{
nrf_esb_disable();
m_enabled = false;
if(m_autoControlHfClock)
{
NRF_CLOCK->TASKS_HFCLKSTOP = 1;
}
}
void NrfEsbRadioPtx::SendFrame(uint8_t pipe, uint8_t *data, uint8_t dataLength)
{
nrf_esb_payload_t tx_payload;
NrfEsbRadioPtx::radioBusy = true;
NrfEsbRadioPtx::radioSuccess = false;
tx_payload.length = dataLength;
tx_payload.pipe = pipe;
memcpy(tx_payload.data, data, dataLength);
tx_payload.noack = false;
nrf_esb_write_payload(&tx_payload);
}
bool NrfEsbRadioPtx::IsRadioBusy()
{
return NrfEsbRadioPtx::radioBusy;
}
bool NrfEsbRadioPtx::SendSucceeded()
{
return NrfEsbRadioPtx::radioSuccess;
}
void NrfEsbRadioPtx::nrfEsbEventHandler(nrf_esb_evt_t const * p_event)
{
nrf_esb_payload_t rx_payload;
NrfEsbRadioPtx::radioBusy = false;
switch (p_event->evt_id)
{
case NRF_ESB_EVENT_TX_SUCCESS:
NrfEsbRadioPtx::radioSuccess = true;
nrf_esb_flush_tx();
break;
case NRF_ESB_EVENT_TX_FAILED:
nrf_esb_flush_tx();
break;
case NRF_ESB_EVENT_RX_RECEIVED:
while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS);
break;
}
}
void NrfEsbRadioPtx::SetRfChannel(uint8_t rfChannel)
{
nrf_esb_set_rf_channel(rfChannel);
}