libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
radio_common.c
Go to the documentation of this file.
1/** @addtogroup radio_file RADIO peripheral API
2 *
3 * @brief <b>Access functions for the 2.4 GHz Radio </b>
4 *
5 * @ingroup peripheral_apis
6 * LGPL License Terms @ref lgpl_license
7 * @author @htmlonly &copy; @endhtmlonly 2016
8 * Maxim Sloyko <maxims@google.com>
9 *
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
16 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
17 *
18 * This library is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with this library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32#include <libopencm3/nrf/ficr.h>
34
35/**@{*/
36
37/** @brief Set radio transmission power.
38 *
39 * @details Note, not all supported power levels are BLE compliant.
40 *
41 * @param[in] txpower enum radio_txpower
42 * */
44{
45 RADIO_TXPOWER = txpower;
46}
47
48/** @brief Set bit transmission order to LSB first. */
50{
51 RADIO_PCNF1 &= ~RADIO_PCNF1_ENDIAN_BIG;
52}
53
54/** @brief Set bit transmission order to MSB first. */
56{
58}
59
60/** @brief Enable on the air data whitening
61 *
62 * @details the in-memory data will remain unwhitened.
63 * */
65{
67}
68
69/** @brief Disable on the air data whitening. */
71{
72 RADIO_PCNF1 &= ~RADIO_PCNF1_WHITEEN;
73}
74
75/** @brief Set CRC length in number of bytes.
76 *
77 * @param[in] crc_len uint8_t CRC length in number of bytes (1-3), 0 = CRC disabled.
78 */
79void radio_set_crclen(uint8_t crc_len)
80{
81 uint32_t reg_crc = RADIO_CRCCNF;
82 reg_crc &= ~RADIO_CRCCNF_LEN_MASK;
83 RADIO_CRCCNF = reg_crc | RADIO_CRCCNF_LEN_MASKED(crc_len);
84}
85
86/** @brief Disable CRC calculation. */
88{
89 RADIO_CRCCNF &= ~RADIO_CRCCNF_LEN_MASK;
90}
91
92/** @brief Enable the peripheral. */
93void radio_enable(void)
94{
96}
97
98/** @brief Disable the peripheral. */
99void radio_disable(void)
100{
102}
103
104
105/** @brief Set Base Address length.
106 *
107 * @param[in] ba_len uint8_t Base Address length in number of bytes (2-4).
108 */
109void radio_set_balen(uint8_t ba_len)
110{
111 uint32_t reg_pcnf1 = RADIO_PCNF1;
112 reg_pcnf1 &= ~RADIO_PCNF1_BALEN_MASK;
113 RADIO_PCNF1 = reg_pcnf1 | RADIO_PCNF1_BALEN_MASKED(ba_len);
114}
115
116/** @brief Set maximum transmission length in number of bytes.
117 *
118 * @param[in] maxlen uint8_t maximum transmission length.
119 */
120void radio_set_maxlen(uint8_t maxlen)
121{
122 uint32_t reg_pcnf1 = RADIO_PCNF1;
123 reg_pcnf1 &= ~RADIO_PCNF1_MAXLEN_MASK;
124 RADIO_PCNF1 = reg_pcnf1 | RADIO_PCNF1_MAXLEN_MASKED(maxlen);
125}
126
127/** @brief Exclude access address from CRC calculation.
128 *
129 * @param[in] is_skip_addr bool If true, CRC will be calculated over PDU only,
130 * if false, it will also include the Access Address.
131 */
132void radio_set_crc_skipaddr(bool is_skip_addr)
133{
134 if (is_skip_addr) {
136 } else {
137 RADIO_CRCCNF &= ~RADIO_CRCCNF_SKIPADDR;
138 }
139}
140
141/** @brief Configure the radio to be used in BLE mode.
142 *
143 * @details This needs to be called before the radio can be used in BLE mode.
144 * It will set som BLE standard parameters, like Inter-Frame Spacing time,
145 * LSB first, enable whitening, properly configure CRC (for advertising) and address length.
146 */
148{
149#ifdef NRF51
151#endif
155
161}
162
163/** @brief Configure the packet.
164 *
165 * @details See the data sheet for details.
166 */
167void radio_configure_packet(uint8_t lf_len_bits, uint8_t s0_len_bytes, uint8_t s1_len_bits)
168{
170 | RADIO_PCNF0_S0LEN_MASKED(s0_len_bytes)
171 | RADIO_PCNF0_S1LEN_MASKED(s1_len_bits);
172}
173
174/** @brief Set radio frequency.
175 *
176 * @param[in] freq uint8_t Frequency offset from 2.4GHz in MHz, for example "29" will
177 * tune the radio to 2429MHz
178 */
179void radio_set_frequency(uint8_t freq)
180{
181 RADIO_FREQUENCY = freq;
182}
183
184/** @brief Set Data Whitening Initialization Vector.
185 *
186 * @param[in] iv uint8_t Initialization Vector. For BLE, this is channel index.
187 */
188void radio_set_datawhiteiv(uint8_t iv)
189{
191}
192
193/* @brief Set Address (base and prefix)
194 *
195 * @details Note that bases are shared between addresses 1-7,
196 * so changing one of them will change others too.
197 *
198 * @param[in] addr_index uint8_t address index (0-7)
199 * @param[in] base uint32_t base part of the address. If balen < 4, appropriate number
200 * of LSBs will be thrown away.
201 * @param[in] prefix uint8_t Address prefix.
202 */
203void radio_set_addr(uint8_t addr_index, uint32_t base, uint8_t prefix)
204{
205 if (addr_index == 0) {
206 RADIO_BASE0 = base;
207 } else {
208 RADIO_BASE1 = base;
209 }
210
211 uint32_t reg_prefix = RADIO_PREFIX_AP(addr_index);
212 reg_prefix &= ~RADIO_PREFIX_AP_MASK(addr_index);
213 RADIO_PREFIX_AP_SET(addr_index, reg_prefix | RADIO_PREFIX_AP_MASKED(addr_index, prefix));
214}
215
216/* @brief Set TX address index
217 *
218 * @details The address needs to be previously configured with radio_set_addr()
219 *
220 * @param[in] address_index uint8_t address index (0-7)
221 */
222void radio_set_tx_address(uint8_t addr_index)
223{
224 RADIO_TXADDRESS = addr_index;
225}
226
227/* @brief Set pointer for RX/TX data
228 *
229 * @param[in] packet_ptr uint8_t* packet buffer address.
230 */
231void radio_set_packet_ptr(uint8_t *packet_ptr)
232{
233 RADIO_PACKETPTR = (uint32_t)packet_ptr;
234}
235
236/* @brief Enable radio Transmitter */
238{
240}
241
242/* @brief Enable radio Receiver */
244{
246}
247
248
249/**@}*/
250
@ RADIO_MODE_BLE_1MBIT
Definition: 51/radio.h:44
#define PERIPH_TRIGGER_TASK(task)
#define RADIO_TXPOWER
Definition: common/radio.h:71
#define RADIO_BLE_CRCLEN
Definition: common/radio.h:266
#define RADIO_POWER_ENABLED
Definition: common/radio.h:261
#define RADIO_PCNF0_S1LEN_MASKED(V)
Definition: common/radio.h:144
#define RADIO_CRCCNF
Definition: common/radio.h:81
#define RADIO_CRCCNF_LEN_MASKED(V)
Definition: common/radio.h:229
#define RADIO_POWER
Definition: common/radio.h:99
#define RADIO_TASK_TXEN
Definition: common/radio.h:39
#define RADIO_TXADDRESS
Definition: common/radio.h:79
#define RADIO_PREFIX_AP_MASKED(n, V)
Definition: common/radio.h:208
#define RADIO_PCNF0_LFLEN_MASKED(V)
Definition: common/radio.h:134
#define RADIO_PREFIX_AP_SET(n, V)
Definition: common/radio.h:212
#define RADIO_BASE0
Definition: common/radio.h:75
#define RADIO_DATAWHITEIV
Definition: common/radio.h:88
#define RADIO_FREQUENCY
Definition: common/radio.h:70
#define RADIO_CRCPOLY
Definition: common/radio.h:82
#define RADIO_PACKETPTR
Definition: common/radio.h:69
#define RADIO_BASE1
Definition: common/radio.h:76
#define RADIO_POWER_DISABLED
Definition: common/radio.h:262
radio_txpower
Definition: common/radio.h:270
#define RADIO_PCNF1_ENDIAN_BIG
Definition: common/radio.h:162
#define RADIO_PCNF1
Definition: common/radio.h:74
#define RADIO_TASK_RXEN
Definition: common/radio.h:40
#define RADIO_CRCCNF_SKIPADDR
Definition: common/radio.h:232
#define RADIO_PREFIX_AP(n)
Definition: common/radio.h:205
#define RADIO_PCNF0
Definition: common/radio.h:73
#define RADIO_PCNF1_BALEN_MASKED(V)
Definition: common/radio.h:159
#define RADIO_TIFS
Definition: common/radio.h:85
#define RADIO_BLE_TIFS
Definition: common/radio.h:265
#define RADIO_PCNF1_MAXLEN_MASKED(V)
Definition: common/radio.h:149
#define RADIO_BLE_CRCINIT
Definition: common/radio.h:268
#define RADIO_BLE_CRCPOLY
Definition: common/radio.h:267
#define RADIO_PCNF0_S0LEN_MASKED(V)
Definition: common/radio.h:139
#define RADIO_PCNF1_WHITEEN
Definition: common/radio.h:163
#define RADIO_CRCINIT
Definition: common/radio.h:83
void radio_set_crclen(uint8_t crc_len)
Set CRC length in number of bytes.
Definition: radio_common.c:79
void radio_set_packet_ptr(uint8_t *packet_ptr)
Definition: radio_common.c:231
void radio_disable(void)
Disable the peripheral.
Definition: radio_common.c:99
void radio_set_datawhiteiv(uint8_t iv)
Set Data Whitening Initialization Vector.
Definition: radio_common.c:188
void radio_configure_ble(void)
Configure the radio to be used in BLE mode.
Definition: radio_common.c:147
void radio_set_frequency(uint8_t freq)
Set radio frequency.
Definition: radio_common.c:179
void radio_set_tx_address(uint8_t addr_index)
Definition: radio_common.c:222
void radio_enable_whitening(void)
Enable on the air data whitening.
Definition: radio_common.c:64
void radio_set_maxlen(uint8_t maxlen)
Set maximum transmission length in number of bytes.
Definition: radio_common.c:120
void radio_set_mode(enum radio_mode mode)
Set radio mode.
Definition: radio.c:39
void radio_enable(void)
Enable the peripheral.
Definition: radio_common.c:93
void radio_set_balen(uint8_t ba_len)
Set Base Address length.
Definition: radio_common.c:109
void radio_disable_whitening(void)
Disable on the air data whitening.
Definition: radio_common.c:70
void radio_configure_packet(uint8_t lf_len_bits, uint8_t s0_len_bytes, uint8_t s1_len_bits)
Configure the packet.
Definition: radio_common.c:167
void radio_set_crc_skipaddr(bool is_skip_addr)
Exclude access address from CRC calculation.
Definition: radio_common.c:132
void radio_enable_rx(void)
Definition: radio_common.c:243
void radio_set_lsbfirst(void)
Set bit transmission order to LSB first.
Definition: radio_common.c:49
void radio_set_txpower(enum radio_txpower txpower)
Set radio transmission power.
Definition: radio_common.c:43
void radio_set_addr(uint8_t addr_index, uint32_t base, uint8_t prefix)
Definition: radio_common.c:203
void radio_enable_tx(void)
Definition: radio_common.c:237
void radio_disable_crc(void)
Disable CRC calculation.
Definition: radio_common.c:87
void radio_set_msbfirst(void)
Set bit transmission order to MSB first.
Definition: radio_common.c:55