MySensors Library & Examples  2.3.2
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 
49 // auto retry delay in us, don't set this value < 1500us@250kbit
50 #define NRF5_ESB_ARD (1500)
51 
52 // auto retry count with noACK is false
53 #define NRF5_ESB_ARC_ACK (15)
54 
55 // auto retry count with noACK is true
56 #define NRF5_ESB_ARC_NOACK (3)
57 
58 // How often broadcast messages are send
59 #define NRF5_ESB_BC_ARC (3)
60 
61 // Node address index
62 #define NRF5_ESB_NODE_ADDR (0)
63 #define NRF5_ESB_NODE_ADDR_MSK (0xffffff00UL)
64 
65 // TX address index
66 #define NRF5_ESB_TX_ADDR (4)
67 #define NRF5_ESB_TX_ADDR_MSK (0xffffff00UL)
68 
69 // BC address index
70 #define NRF5_ESB_BC_ADDR (7)
71 #define NRF5_ESB_BC_ADDR_MSK (0xffffffffUL)
72 
73 // Shorts for RX mode
74 #define NRF5_ESB_SHORTS_RX \
75  (RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
76  RADIO_SHORTS_DISABLED_RXEN_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk | \
77  RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
78 
79 // Shorts for TX mode
80 #define NRF5_ESB_SHORTS_TX \
81  (RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk | \
82  RADIO_SHORTS_DISABLED_TXEN_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk)
83 
84 // Shorts to switch from RX to TX
85 #define NRF5_ESB_SHORTS_RX_TX \
86  (RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_TXEN_Msk | \
87  RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk)
88 
89 // Shorts to switch from TX to RX
90 #define NRF5_ESB_SHORTS_TX_RX \
91  (RADIO_SHORTS_END_DISABLE_Msk | RADIO_SHORTS_DISABLED_RXEN_Msk | \
92  RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_ADDRESS_BCSTART_Msk | \
93  RADIO_SHORTS_ADDRESS_RSSISTART_Msk | RADIO_SHORTS_DISABLED_RSSISTOP_Msk)
94 
95 // PPI Channels for TX
96 #if (NRF5_RADIO_TIMER_IRQN != TIMER0_IRQn)
97 // Use two regular PPI channels
98 #define NRF5_ESB_PPI_TIMER_START 14
99 #define NRF5_ESB_PPI_TIMER_RADIO_DISABLE 15
100 #else
101 // Use one regular PPI channel and one predefined PPI channel
102 #define NRF5_ESB_USE_PREDEFINED_PPI
103 #define NRF5_ESB_PPI_TIMER_START 15
104 #define NRF5_ESB_PPI_TIMER_RADIO_DISABLE 22
105 #endif
106 #define NRF5_ESB_PPI_BITS \
107  ((1 << NRF5_ESB_PPI_TIMER_START) | \
108  (1 << NRF5_ESB_PPI_TIMER_RADIO_DISABLE))
109 
113 #define NRF5_ESB_BITCOUNTER (9)
114 
118 #define NRF5_ESB_RAMP_UP_TIME (140)
119 
120 
121 static bool NRF5_ESB_initialize();
122 static void NRF5_ESB_powerDown();
123 static void NRF5_ESB_powerUp();
124 static void NRF5_ESB_sleep();
125 static void NRF5_ESB_standBy();
126 static bool NRF5_ESB_sanityCheck();
127 
128 static void NRF5_ESB_setNodeAddress(const uint8_t address);
129 static uint8_t NRF5_ESB_getNodeID();
130 
131 static void NRF5_ESB_startListening();
132 static bool NRF5_ESB_isDataAvailable();
133 static uint8_t NRF5_ESB_readMessage(void *data);
134 
135 static bool NRF5_ESB_sendMessage(uint8_t recipient, const void *buf, uint8_t len, const bool noACK);
136 
137 static int16_t NRF5_ESB_getSendingRSSI();
138 static int16_t NRF5_ESB_getReceivingRSSI();
139 
140 // Calculate time to transmit an byte in us as bit shift -> 2^X
141 static inline uint8_t NRF5_ESB_byte_time();
142 
145 typedef struct nrf5_radio_packet_s {
146  // structure written by radio unit
147  struct {
148  uint8_t len;
149  union {
150  uint8_t s1;
151  struct {
152  uint8_t noack : 1;
153  uint8_t pid : 2;
154  };
155  };
156  uint8_t data[MAX_MESSAGE_SIZE];
157  int8_t rssi;
159 #ifdef MY_DEBUG_VERBOSE_NRF5_ESB
160  uint32_t rxmatch;
161 #endif
162  }
163 #ifndef DOXYGEN
164  __attribute__((packed));
165 #endif
167 
168 #ifdef MY_DEBUG_VERBOSE_NRF5_ESB
169 static uint32_t intcntr_bcmatch;
170 static uint32_t intcntr_ready;
171 static uint32_t intcntr_end;
172 #endif
173 
174 #endif // __NRF5_H__
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:653
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:145