MySensors Library & Examples  2.3.2-62-ge298769
Radio_ESB.h
1 /*
2  * The MySensors Arduino library handles the wireless radio link and protocol
3  * between your home built sensors/actuators and HA controller of choice.
4  * The sensors forms a self healing radio network with optional repeaters. Each
5  * repeater and gateway builds a routing tables in EEPROM which keeps track of
6  * the
7  * network topology allowing messages to be routed to nodes.
8  *
9  * Created by Frank Holtz
10  * Copyright (C) 2017 Frank Holtz
11  * Full contributor list:
12  * https://github.com/mysensors/MySensors/graphs/contributors
13  *
14  * Documentation: http://www.mysensors.org
15  * Support Forum: http://forum.mysensors.org
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * version 2 as published by the Free Software Foundation.
20  */
21 
22 #ifndef __NRF5_ESB_H__
23 #define __NRF5_ESB_H__
24 
25 #include "Radio.h"
26 #include <Arduino.h>
27 
28 // Check maximum message length
29 #if MAX_MESSAGE_SIZE > (32)
30 #error "Unsupported message size. (MAX_MESSAGE_SIZE)"
31 #endif
32 
33 // check rx buffer size
34 #if MY_NRF5_ESB_RX_BUFFER_SIZE < (4)
35 #error "MY_NRF5_ESB_RX_BUFFER_SIZE must be greater than 3."
36 #endif
37 
46 #define NRF5_ESB_ACK_WAIT \
47  ((NRF5_ESB_RAMP_UP_TIME << 1) + (9 << NRF5_ESB_byte_time()))
48 
51 #define NRF5_ESB_MAX_PACKET_TIME \
52  ((MAX_MESSAGE_SIZE+sizeof(nrf5_radio_packet_s)) << NRF5_ESB_byte_time())
53 
54 // auto retry delay in us, don't set this value < 1500us@250kbit
55 #define NRF5_ESB_ARD (1500)
56 
57 // auto retry count with noACK is false
58 #define NRF5_ESB_ARC_ACK (3)
59 
60 // auto retry count with noACK is true
61 #define NRF5_ESB_ARC_NOACK (3)
62 
63 // How often broadcast messages are send
64 #define NRF5_ESB_BC_ARC (3)
65 
66 // Node address index
67 #define NRF5_ESB_NODE_ADDR (0)
68 #define NRF5_ESB_NODE_ADDR_MSK (0xffffff00UL)
69 
70 // TX address index
71 #define NRF5_ESB_TX_ADDR (4)
72 #define NRF5_ESB_TX_ADDR_MSK (0xffffff00UL)
73 
74 // Broadcast address index
75 #define NRF5_ESB_BC_ADDR (7)
76 #define NRF5_ESB_BC_ADDR_MSK (0xffffffffUL)
77 
78 // Interrupt mask RX
79 #define NRF5_EBS_RADIO_INT_RX (RADIO_INTENSET_ADDRESS_Msk | RADIO_INTENSET_END_Msk | RADIO_INTENSET_DISABLED_Msk)
80 
81 // Interrupt mask TX
82 #define NRF5_EBS_RADIO_INT_TX (RADIO_INTENSET_ADDRESS_Msk | RADIO_INTENSET_DISABLED_Msk)
83 
84 // Shorts for RX mode
85 #define NRF5_ESB_SHORTS_RX \
86  (RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
87  RADIO_SHORTS_DISABLED_RXEN_Msk | \
88  RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
89 
90 // Shorts for TX mode
91 #define NRF5_ESB_SHORTS_TX \
92  (RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
93  RADIO_SHORTS_DISABLED_TXEN_Msk)
94 
95 // Shorts to switch from RX to TX
96 #define NRF5_ESB_SHORTS_RX_TX \
97  (RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_TXEN_Msk | \
98  RADIO_SHORTS_READY_START_Msk)
99 
100 // Shorts to switch from TX to RX
101 #define NRF5_ESB_SHORTS_TX_RX \
102  (RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_RXEN_Msk | \
103  RADIO_SHORTS_READY_START_Msk | \
104  RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
105 
110 #define NRF5_ESB_PPI_LAST_CHANNEL (PPI_CH_NUM - 1)
111 #if (NRF5_RADIO_TIMER_IRQN != TIMER0_IRQn)
112 // Use two regular PPI channels
113 #define NRF5_ESB_PPI_TIMER_START (NRF5_ESB_PPI_LAST_CHANNEL - 1)
114 #define NRF5_ESB_PPI_TIMER_RADIO_DISABLE (NRF5_ESB_PPI_LAST_CHANNEL)
115 #else
116 // Use one regular PPI channel and one predefined PPI channel
117 #define NRF5_ESB_USE_PREDEFINED_PPI
118 #define NRF5_ESB_PPI_TIMER_START (NRF5_ESB_PPI_LAST_CHANNEL)
119 #define NRF5_ESB_PPI_TIMER_RADIO_DISABLE 22
120 #endif
121 #define NRF5_ESB_PPI_BITS \
122  ((1 << NRF5_ESB_PPI_TIMER_START) | \
123  (1 << NRF5_ESB_PPI_TIMER_RADIO_DISABLE))
124 
128 #define NRF5_ESB_RAMP_UP_TIME (140)
129 
130 
131 static bool NRF5_ESB_initialize();
132 static void NRF5_ESB_powerDown();
133 static void NRF5_ESB_powerUp();
134 static void NRF5_ESB_sleep();
135 static void NRF5_ESB_standBy();
136 static bool NRF5_ESB_sanityCheck();
137 
138 static void NRF5_ESB_setNodeAddress(const uint8_t address);
139 static uint8_t NRF5_ESB_getNodeID();
140 
141 static void NRF5_ESB_startListening();
142 static bool NRF5_ESB_isDataAvailable();
143 static uint8_t NRF5_ESB_readMessage(void *data);
144 
145 static bool NRF5_ESB_sendMessage(uint8_t recipient, const void *buf, uint8_t len, const bool noACK);
146 
147 static int16_t NRF5_ESB_getSendingRSSI();
148 static int16_t NRF5_ESB_getReceivingRSSI();
149 
150 // Calculate time to transmit an byte in us as bit shift -> 2^X
151 static inline uint8_t NRF5_ESB_byte_time();
152 
155 typedef struct nrf5_radio_packet_s {
156  // structure written by radio unit
157  struct {
158  uint8_t len;
159  union {
160  uint8_t s1;
161  struct {
162  uint8_t noack : 1;
163  uint8_t pid : 2;
164  };
165  };
166  uint8_t data[MAX_MESSAGE_SIZE];
167  int8_t rssi;
169 #ifdef MY_DEBUG_VERBOSE_NRF5_ESB
170  uint32_t rxmatch;
171 #endif
172  }
173 #ifndef DOXYGEN
174  __attribute__((packed));
175 #endif
177 
178 #ifdef MY_DEBUG_VERBOSE_NRF5_ESB
179 static uint32_t intcntr_addrmatch;
180 static uint32_t intcntr_ready;
181 static uint32_t intcntr_end;
182 static uint32_t intcntr_disabled;
183 static uint32_t intcntr_timer_cc3;
184 #endif
185 
186 #endif // __NRF5_H__
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:654
MAX_MESSAGE_SIZE
#define MAX_MESSAGE_SIZE
The maximum size of a message (including header)
Definition: MyMessage.h:59
__attribute__
struct @4::@5 __attribute__
Doxygen will complain without this comment.
Definition: DigitalPin.h:65
nrf5_radio_packet_s
Definition: Radio_ESB.h:155