embeddedlibrary
reusable software modules for embedded systems
nrf24network.h
Go to the documentation of this file.
1 
38 #ifndef NRF24NETWORK_H
39 #define NRF24NETWORK_H
40 
42 typedef void (*nrf24_handler_fn_t)(uint8_t * data, uint8_t len, uint8_t from);
43 
47 typedef union {
48  uint16_t word;
49  uint8_t b[2];
50  struct {
51  uint16_t to_leaf: 3;
52  uint16_t to_branch: 3;
53  uint16_t from_leaf: 3;
54  uint16_t from_branch: 3;
55  uint16_t multi_msg: 1;
56  uint16_t msg_len: 3;
57  };
58  struct {
59  uint16_t to: 6;
60  uint16_t from: 6;
61  uint16_t reserved: 4;
62  };
64 
65 #define NRF24_CHANNEL_BRANCH_1 30
66 #define NRF24_CHANNEL_BRANCH_2 34
67 #define NRF24_CHANNEL_BRANCH_3 38
68 #define NRF24_CHANNEL_BRANCH_4 42
69 #define NRF24_CHANNEL_BRANCH_5 46
70 #define NRF24_CHANNEL_BRANCH_6 50
71 
72 #define NRF24_ADDRESS_MASK 0x6C6C6C7800
73 
74 #define NRF24_MIN_WINDOW_MS 200 //TODO change this to 4
75 #define NRF24_BRANCH_WINDOW_MS 100 //TODO change this to 4
76 #define NRF24_TICK_MS 10 //TODO change this to 1
77 #define NRF24_MISSING_NODE_TIMEOUT 500
78 
79 // The third payload byte (after the two address bytes) will be the message ID
81  SYSTEM_MSG = 0,
97 };
98 
99 enum nrf24_system_msg {
100  NULL_MSG = 0
101 };
102 
103 // The fourth payload byte of control messages will be a control message sub-ID
105  ONBOARD_LED,
106  LINEAR_ACTUATOR,
107  COFFEE_POT,
108  TOASTER,
110 };
111 
112 // The fourth payload byte of sensor messages will be a sensor message sub-ID
114  TEMPERATURE_SENSOR,
115  SPEED_SENSOR,
116  TORQUE_SENSOR,
118 };
119 
120 enum nrf24_address {
121  // assign fixed node addresses
122  MASTER = 0x00,
123  LECAKES = 0x08,
124  ALEYAN,
125  AMRITKAR,
126  CANDELARIA,
127  CARLUCCIO,
128  DONOW,
129  LECAKES_ALL = 0x0F,
130  BLAZEJEWSKI = 0x10,
131  HAAS,
132  HARRIS,
133  HENSHAW,
134  JACOBSEN,
135  KLODNICKI,
136  BLAZEJEWSKI_ALL = 0x17,
137  MORRIS = 0x18,
138  LABARCK,
139  LIU,
140  RITCHIE_III,
141  RUPP,
142  RUSSO,
143  MORRIS_ALL = 0x1F,
144  TRAFFORD = 0x20,
145  WHALEN,
146  WIBLE,
147  MACCARONE,
148  VOTTA,
149  TRAFFORD_ALL = 0x27,
150  MERLINO = 0x28,
151  GOODMAN,
152  MUHLBAIER,
153  LAST_ADDRESS,
154  ALL_ALL = 0x3F
155 };
156 
157 enum nrfstate_e {
158  NRFNET_INITIALIZED = 0x00,
159  NRFNET_NORMAL_STATE = 0x00,
160  NRFNET_WAITING_FOR_MIN_WINDOW
161  };
162 
163 enum nrfrole_e {ROLE_MASTER, ROLE_BRANCH, ROLE_LEAF};
164 
165 enum pipe_status_e {
166  CHILD_INITIALIZED,
167  CHILD_ACTIVE,
168  CHILD_MISSING
169 };
170 
171 typedef struct {
172  uint8_t length;
173  uint8_t child; // child 0xFF means unused buffer slot
174  uint8_t age; // how long this msg has been on the buffer (relative, 0 is newest)
175  uint8_t data[32];
176 } nrfnet_msg_t;
177 
178 #define NRFNET_MSG_BUFFER_LENGTH 16
179 typedef struct nrfnet_s {
180  nrf24_t radio;
181  uint8_t sys_id;
182  uint8_t node;
183  char name[5];
184  enum nrfrole_e role;
185  enum nrfstate_e state;
186  volatile uint8_t current_child;
187  tint_t child_time[6];
188  tint_t switch_to_tx_time;
189  enum pipe_status_e child_status[6];
190  uint8_t channel[6];
191  nrfnet_msg_t msg_buf[NRFNET_MSG_BUFFER_LENGTH];
193  nrf24_handler_fn_t control_handler[LAST_CONTROL_ID];
194  nrf24_handler_fn_t sensor_handler[LAST_SENSOR_ID];
195 } nrfnet_t;
196 
207 void nrf24_NetworkInit(void (*ce)(uint8_t), void (*csn)(uint8_t), uint8_t spi_channel);
208 void nrf24_NetworkInitN(nrfnet_t * net, void (*ce)(uint8_t), void (*csn)(uint8_t), uint8_t spi_channel, uint8_t node);
209 
233 void nrf24_RegisterMsgHandlerN(nrfnet_t * net, enum nrf24_msg_id msg_id, nrf24_handler_fn_t fn_ptr);
234 
244 void nrf24_SendMsg(uint8_t to, enum nrf24_msg_id msg_id, uint8_t * data, uint8_t len);
245 void nrf24_SendMsgN(nrfnet_t * net, uint8_t to, enum nrf24_msg_id msg_id, uint8_t * data, uint8_t len);
246 
257 void nrf24_RegisterControlHandlerN(nrfnet_t * net, enum nrf24_control_msg control_id, nrf24_handler_fn_t fn_ptr);
258 
269 void nrf24_RegisterSensorHandlerN(nrfnet_t * net, enum nrf24_sensor_msg sensor_id, nrf24_handler_fn_t fn_ptr);
270 
271 void nrf24_NetworkISRHandler(void);
272 #define nrf24_NetworkISRHandlerN(net) nRF24_ISR(&net.radio)
273 
279 char * NameFromAddress(uint8_t address);
280 
286 uint8_t AddressFromName(char * name);
287 
288 // possible additions:
289 // - add method to send a control message
290 // - add method to request sensor data
291 // - add method to support automatic response of sensor data request
292 // - add network level nack for undeliverable packets
293 
294 #endif /* NRF24NETWORK_H */
Used for testing the network.
Definition: nrf24network.h:95
void nrf24_SendMsg(uint8_t to, enum nrf24_msg_id msg_id, uint8_t *data, uint8_t len)
Definition: nrf24network.c:189
nrf24_msg_id
Definition: nrf24network.h:80
#define tint_t
Definition: timing.h:26
A Team.
Definition: nrf24network.h:92
nrf24_control_msg
Definition: nrf24network.h:104
Merlino.
Definition: nrf24network.h:94
void(* nrf24_handler_fn_t)(uint8_t *data, uint8_t len, uint8_t from)
Definition: nrf24network.h:42
George L.
Definition: nrf24network.h:88
Lee, Dan, Eric, and Lee aka Team James Ritchie III.
Definition: nrf24network.h:91
Matt and Tom.
Definition: nrf24network.h:90
built-in / Jon W.
Definition: nrf24network.h:84
void nrf24_NetworkInit(void(*ce)(uint8_t), void(*csn)(uint8_t), uint8_t spi_channel)
Initialize nRF24 Network Module.
Definition: nrf24network.c:111
Jimmy+Josh.
Definition: nrf24network.h:89
Jon W. Name TBD.
Definition: nrf24network.h:86
general sensor msg / max payload 4+ bytes
Definition: nrf24network.h:83
reserved name to determine number of control msgs supported
Definition: nrf24network.h:109
reserved name to determine number of message IDs supported
Definition: nrf24network.h:96
Definition: nrf24.h:29
general control msg / max payload 5+ bytes
Definition: nrf24network.h:82
Know.
Definition: nrf24network.h:93
void nrf24_RegisterMsgHandler(enum nrf24_msg_id msg_id, nrf24_handler_fn_t fn_ptr)
Definition: nrf24network.c:176
Definition: nrf24network.h:47
uint8_t AddressFromName(char *name)
Definition: nrf24network.c:443
Team Enigma.
Definition: nrf24network.h:87
void nrf24_RegisterSensorHandler(enum nrf24_sensor_msg sensor_id, nrf24_handler_fn_t fn_ptr)
Definition: nrf24network.c:257
char * NameFromAddress(uint8_t address)
Definition: nrf24network.c:436
Definition: nrf24network.h:179
Jon W. Name TBD.
Definition: nrf24network.h:85
built-in / MM
Definition: nrf24network.h:81
nrf24_sensor_msg
Definition: nrf24network.h:113
reserved name to determine number of sensors supported
Definition: nrf24network.h:117
Definition: nrf24network.h:171
void nrf24_RegisterControlHandler(enum nrf24_control_msg control_id, nrf24_handler_fn_t fn_ptr)
Definition: nrf24network.c:248