px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_nrf905.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2013 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_nrf905.h : Nordic Semiconductor nRF905 Transceiver driver
14 Author(s): Pieter Conradie
15 Creation Date: 2013-07-28
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_nrf905.h"
23#include "px_board.h"
24#include "px_log.h"
25
26/* _____LOCAL DEFINITIONS____________________________________________________ */
27PX_LOG_NAME("px_nrf905");
28
29#define PX_NRF905_INSTR_W_CONFIG 0x00 ///< Write Configuration register
30#define PX_NRF905_INSTR_R_CONFIG 0x10 ///< Read Configuration register
31#define PX_NRF905_INSTR_W_TX_PAYLOAD 0x20 ///< Write TX-payload
32#define PX_NRF905_INSTR_R_TX_PAYLOAD 0x21 ///< Read TX-payload
33#define PX_NRF905_INSTR_W_TX_ADDRESS 0x22 ///< Write TX-address
34#define PX_NRF905_INSTR_R_TX_ADDRESS 0x23 ///< Read TX-address
35#define PX_NRF905_INSTR_R_RX_PAYLOAD 0x24 ///< Read RX-payload
36#define PX_NRF905_INSTR_CHANNEL_CONFIG 0x80 ///< set CH_NO, HFREQ_PLL and PA_PWR
37
38/* _____MACROS_______________________________________________________________ */
39
40/* _____GLOBAL VARIABLES_____________________________________________________ */
41
42/* _____LOCAL VARIABLES______________________________________________________ */
43static px_spi_handle_t * px_nrf905_spi_handle;
44
45/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
46
47/* _____LOCAL FUNCTIONS______________________________________________________ */
48
49/* _____GLOBAL FUNCTIONS_____________________________________________________ */
50void px_nrf905_init(px_spi_handle_t * handle)
51{
52 px_nrf905_spi_handle = handle;
53}
54
55uint8_t px_nrf905_cfg_wr(const px_nrf905_cfg_reg_t * cfg, uint8_t start_byte)
56{
57 uint8_t spi_data[1];
58
59 PX_LOG_ASSERT(start_byte < (sizeof(px_nrf905_cfg_reg_t) - 1));
60
61 // Send instruction
62 spi_data[0] = PX_NRF905_INSTR_W_CONFIG | start_byte;
63 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 1, PX_SPI_FLAG_START);
64
65 // Write data
66 px_spi_wr(px_nrf905_spi_handle,
67 &((const uint8_t *)cfg)[start_byte],
68 sizeof(px_nrf905_cfg_reg_t) - start_byte,
70
71 // Return status register value
72 return spi_data[0];
73}
74
75uint8_t px_nrf905_cfg_rd(px_nrf905_cfg_reg_t * cfg, uint8_t start_byte)
76{
77 uint8_t spi_data[1];
78
79 PX_LOG_ASSERT(start_byte < (sizeof(px_nrf905_cfg_reg_t) - 1));
80
81 // Send instruction
82 spi_data[0] = PX_NRF905_INSTR_R_CONFIG | start_byte;
83 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 1, PX_SPI_FLAG_START);
84
85 // Read data
86 px_spi_rd(px_nrf905_spi_handle,
87 &((uint8_t *)cfg)[start_byte],
88 sizeof(px_nrf905_cfg_reg_t) - start_byte,
90
91 // Return status register
92 return spi_data[0];
93}
94
95uint8_t px_nrf905_tx_payload_wr(const void * data, uint8_t nr_of_bytes)
96{
97 uint8_t spi_data[1];
98
99 PX_LOG_ASSERT( ((nr_of_bytes >= 1) && (nr_of_bytes <= 32)) );
100
101 // Send instruction
102 spi_data[0] = PX_NRF905_INSTR_W_TX_PAYLOAD;
103 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 1, PX_SPI_FLAG_START);
104
105 // Write data
106 px_spi_wr(px_nrf905_spi_handle,
107 data,
108 nr_of_bytes,
110
111 // Return status register value
112 return spi_data[0];
113}
114
115uint8_t px_nrf905_tx_payload_rd(void * data, uint8_t nr_of_bytes)
116{
117 uint8_t spi_data[1];
118
119 PX_LOG_ASSERT( ((nr_of_bytes >= 1) && (nr_of_bytes <= 32)) );
120
121 // Send instruction
122 spi_data[0] = PX_NRF905_INSTR_R_TX_PAYLOAD;
123 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 1, PX_SPI_FLAG_START);
124
125 // Read data
126 px_spi_rd(px_nrf905_spi_handle,
127 data,
128 nr_of_bytes,
130
131 // Return status register
132 return spi_data[0];
133}
134
135uint8_t px_nrf905_rx_payload_rd(void * data, uint8_t nr_of_bytes)
136{
137 uint8_t spi_data[1];
138
139 PX_LOG_ASSERT( ((nr_of_bytes >= 1) && (nr_of_bytes <= 32)) );
140
141 // Send instruction
142 spi_data[0] = PX_NRF905_INSTR_R_RX_PAYLOAD;
143 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 1, PX_SPI_FLAG_START);
144
145 // Read data
146 px_spi_rd(px_nrf905_spi_handle,
147 data,
148 nr_of_bytes,
150
151 // Return status register
152 return spi_data[0];
153}
154
155uint8_t px_nrf905_channel_cfg(uint16_t channel_no, uint8_t hfreq_pll, uint8_t pa_pwr)
156{
157 uint8_t spi_data[2];
158
159 PX_LOG_ASSERT(start_byte < (sizeof(px_nrf905_cfg_reg_t)-1));
160
161 // Send instruction
162 spi_data[0] = PX_NRF905_INSTR_CHANNEL_CONFIG
163 | (pa_pwr << 2)
164 | (hfreq_pll << 1)
165 | (channel_no >> 8);
166 spi_data[1] = channel_no & 0xff;
167 px_spi_xc(px_nrf905_spi_handle, spi_data, spi_data, 2, PX_SPI_FLAG_START);
168
169 // Return status register value
170 return spi_data[0];
171}
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
Definition: px_log.h:349
#define PX_LOG_ASSERT(expression)
Macro that will test an expression, and block indefinitely if false.
Definition: px_log.h:440
NRF905 Configuration register map.
Definition: px_nrf905.h:96
#define PX_SPI_FLAG_START
Begin SPI transaction (take SPI slave's Chip Select line low)
Definition: px_spi.h:117
void px_spi_xc(px_spi_handle_t *handle, const void *data_wr, void *data_rd, size_t nr_of_bytes, uint8_t flags)
Perform an SPI exchange (write and read) transaction with an SPI slave.
Definition: px_spi.c:568
void px_spi_wr(px_spi_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI write transaction with an SPI slave.
Definition: px_spi.c:396
void px_spi_rd(px_spi_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI read transaction with an SPI slave.
Definition: px_spi.c:479
#define PX_SPI_FLAG_STOP
Finish SPI transaction (take SPI slave's Chip Select line high)
Definition: px_spi.h:119
Define SPI handle.
Definition: px_spi.h:126