Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Config.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <nrf_sdh_ble.h>
11
19// clang-format off
20
21#define CROWNSTONE_COMPANY_ID 0x038E
22
23#define CS_CONNECTION_PROTOCOL_VERSION 5
24
25// size of the buffer used for characteristics
26//#define GENERAL_BUFFER_SIZE 300
27
29#define DEFAULT_CHAR_VALUE_STRING_LENGTH 50
30
33#define MAX_STRING_STORAGE_SIZE 31
34
35
38//#define SAADC_TIMER_CONFIG_IRQ_PRIORITY 2
39//#define SAADC_CONFIG_IRQ_PRIORITY 2
40//#define COMP_CONFIG_IRQ_PRIORITY 3
41//#define LPCOMP_CONFIG_IRQ_PRIORITY 3
42
43/*
44 */
45#define APP_TIMER_PRESCALER 0
46
50//#define APP_TIMER_OP_QUEUE_SIZE 10
51#define APP_TIMER_OP_QUEUE_SIZE 16
52
57#define SCHED_MAX_EVENT_DATA_SIZE (MAX(20, MAX(APP_TIMER_SCHED_EVENT_DATA_SIZE, NRF_SDH_BLE_EVT_BUF_SIZE)))
58
64#define SCHED_QUEUE_SIZE 32
65
66#define SCHEDULER_QUEUE_ALMOST_FULL (SCHED_QUEUE_SIZE - 10)
67
68
69#define APP_BLE_CONN_CFG_TAG 1 // Connection configuration identifier.
70
71// bonding / security
72#define SEC_PARAM_TIMEOUT 30
73#define SEC_PARAM_BOND 1
74#define SEC_PARAM_MITM 1
75#define SEC_PARAM_IO_CAPABILITIES BLE_GAP_IO_CAPS_DISPLAY_ONLY
76#define SEC_PARAM_OOB 0
77#define SEC_PARAM_MIN_KEY_SIZE 7
78#define SEC_PARAM_MAX_KEY_SIZE 16
80#define SECURITY_REQUEST_DELAY 1500
82#define SWITCH_CLAIM_TIME_MS 2000
84// Max number of schedule entries in the schedule service.
85#define MAX_SCHEDULE_ENTRIES 10
86
87// How many seconds of time jump is regarded a big time jump
88#define SCHEDULE_BIG_TIME_JUMP (75*60)
89
90// How many channels the pwm can control. Limited by the amount of timer channels.
91#define CS_PWM_MAX_CHANNELS 2
92
93// ----- TIMERS -----
94// Soft device uses timer 0
95// Mesh uses timer 3 (BEARER_ACTION_TIMER_INDEX)
96#define CS_PWM_TIMER NRF_TIMER4
97#define CS_PWM_TIMER_IRQ TIMER4_IRQHandler
98#define CS_PWM_IRQn TIMER4_IRQn
99#define CS_PWM_INSTANCE_INDEX TIMER4_INSTANCE_INDEX
100#define CS_PWM_TIMER_ID 4
101#define CS_PWM_TIMER_IRQ_PRIORITY APP_IRQ_PRIORITY_HIGH
102//#define CS_PWM_TIMER_FREQ NRF_TIMER_FREQ_500kHz
103#define CS_PWM_TIMER_FREQ NRF_TIMER_FREQ_4MHz // Max frequency, higher can give issues.
104
105#define CS_ADC_TIMER NRF_TIMER1
106#define CS_ADC_TIMER_IRQ TIMER1_IRQHandler
107#define CS_ADC_TIMER_IRQn TIMER1_IRQn
108#define CS_ADC_INSTANCE_INDEX TIMER1_INSTANCE_INDEX
109#define CS_ADC_TIMER_ID 1
110#define CS_ADC_TIMER_FREQ NRF_TIMER_FREQ_16MHz
111
112
113
114// ----- PPI -----
115// Soft device uses 17-31
116// Mesh SDK uses 8-11
117#define CS_PWM_PPI_CHANNEL_START 0
118#define CS_PWM_PPI_CHANNEL_COUNT (1 + 2 * CS_PWM_MAX_CHANNELS)
119//#define CS_ADC_PPI_CHANNEL_START (CS_PWM_PPI_CHANNEL_START + CS_PWM_PPI_CHANNEL_COUNT)
120#define CS_ADC_PPI_CHANNEL_START 12
121#define CS_ADC_PPI_CHANNEL_COUNT 2
122
123// ----- PPI groups -----
124// Soft device uses 4-5
125#define CS_PWM_PPI_GROUP_START 0
126#define CS_PWM_PPI_GROUP_COUNT 0
127#define CS_ADC_PPI_GROUP_START (CS_PWM_PPI_GROUP_START + CS_PWM_PPI_GROUP_COUNT)
128#define CS_ADC_PPI_GROUP_COUNT 0
129
130// ----- GPIOTE -----
131#define CS_ADC_GPIOTE_CHANNEL_START 0
132#define CS_ADC_GPIOTE_CHANNEL_COUNT 1 // Actually only used for debug
133#define CS_PWM_GPIOTE_CHANNEL_START (CS_ADC_GPIOTE_CHANNEL_START + CS_ADC_GPIOTE_CHANNEL_COUNT)
134#define CS_PWM_GPIOTE_CHANNEL_COUNT (CS_PWM_MAX_CHANNELS)
135
136// ----- SAADC -----
137#define CS_ADC_RESOLUTION NRF_SAADC_RESOLUTION_12BIT // Don't change without changing code.
138#define CS_ADC_IRQ_PRIORITY APP_IRQ_PRIORITY_HIGH
139#define CS_ADC_IRQ SAADC_IRQHandler
140
141// ----- WDT -----
142#define CS_WATCHDOG_PRIORITY APP_IRQ_PRIORITY_HIGH
143#define CS_WATCHDOG_TIMEOUT_MS 60000
144
145
146#define CS_ADC_SAMPLE_INTERVAL_US 200 // 100 samples per period of 50Hz wave
147#define CS_ADC_NUM_CHANNELS 2 // Number of channels (pins) to sample.
148#define CS_ADC_NUM_SAMPLES_PER_CHANNEL (20000 / CS_ADC_SAMPLE_INTERVAL_US) // Make size so it fills up 20ms of data.
149//#define CS_ADC_BUF_SIZE (CS_ADC_NUM_CHANNELS * CS_ADC_NUM_SAMPLES_PER_CHANNEL)
150
151#define CS_ADC_NUM_BUFFERS 9 // 5 buffers are held by processing, 2 queued in SAADC, 1 moves between them, 1 extra for CPU usage peaks.
152#define CS_ADC_TIMEOUT_SAMPLES 2 // Timeout when no buffer has been set at N samples before end of interval.
153
154
155// Buffer size for storage requests. Storage requests get buffered when the device is scanning or meshing.
156#define STORAGE_REQUEST_BUFFER_SIZE 5 // Should be at least 3, because setup pushes 3 storage requests (configs + operation mode + switch state).
157
158#define FACTORY_RESET_CODE 0xdeadbeef
159#define FACTORY_RESET_TIMEOUT 60000 // Timeout before recovery becomes unavailable after reset (ms)
160#define FACTORY_PROCESS_TIMEOUT 200 // Timeout before recovery process step is executed (ms)
161
162#define ENCRYPTION_KEY_LENGTH 16 // 16 byte length, just like SOC_ECB_KEY_LENGTH
163
164#define BROWNOUT_TRIGGER_THRESHOLD NRF_POWER_THRESHOLD_V27
165
166// Octave: a=0.05; x=[0:1000]; y=(1-a).^x; y2=cumsum(y)*a; figure(1); plot(x,y); figure(2); plot(x,y2); find(y2 > 0.99)(1)
167// Python: d=0.1; x=range(0,1000); y=np.power(1.0 - d, x); y2=np.cumsum(y) * d; indices=np.where(y2 > 0.99); print(indices[0][0])
168#define VOLTAGE_ZERO_EXP_AVG_DISCOUNT 20 // Is divided by 1000, so 20 is a discount of 0.02. // 99% of the average is influenced by the last 228 values
169//#define VOLTAGE_ZERO_EXP_AVG_DISCOUNT 1000 // No averaging
170#define CURRENT_ZERO_EXP_AVG_DISCOUNT 100 // Is divided by 1000, so 100 is a discount of 0.1. // 99% of the average is influenced by the last 44 values
171//#define CURRENT_ZERO_EXP_AVG_DISCOUNT 1000 // No averaging
172#define POWER_EXP_AVG_DISCOUNT 200 // Is divided by 1000, so 200 is a discount of 0.2. // 99% of the average is influenced by the last 21 values
173//#define POWER_EXP_AVG_DISCOUNT 1000 // No averaging
174#define POWER_SAMPLING_RMS_WINDOW_SIZE 9 // Windows size used for filtering the power and current rms. Currently can only be 7, 9, or 25!
175
176#define POWER_SAMPLING_CURVE_HALF_WINDOW_SIZE 5 // Half window size used for filtering the current curve. Can't just be any value!
177//#define POWER_SAMPLING_CURVE_HALF_WINDOW_SIZE 16 // Half window size used for filtering the current curve. Can't just be any value!
178
179
180#define POWER_DIFF_THRESHOLD_PART 0.10f // When difference is 10% larger or smaller, consider it a significant change.
181#define POWER_DIFF_THRESHOLD_MIN_WATT 10.0f // But the difference must also be at least so many Watts.
182#define NEGATIVE_POWER_THRESHOLD_WATT -10.0f // Only if power is below threshold, it may be negative.
183#define POWER_DIFF_THRESHOLD_PART_CS_ZERO 0.10f // Same, but for plug zero or builtin zero (less accurate power measurements).
184#define POWER_DIFF_THRESHOLD_MIN_WATT_CS_ZERO 15.0f // Same, but for plug zero or builtin zero (less accurate power measurements).
185#define NEGATIVE_POWER_THRESHOLD_WATT_CS_ZERO -20.0f // Same, but for plug zero or builtin zero (less accurate power measurements).
186
187#define CURRENT_USAGE_THRESHOLD (16000) // Power usage threshold in mA at which the switch should be turned off.
188#define CURRENT_USAGE_THRESHOLD_DIMMER (1000) // Power usage threshold in mA at which the PWM should be turned off.
189#define CURRENT_THRESHOLD_CONSECUTIVE 100 // Number of consecutive times the current has to be above the threshold before triggering the softfuse.
190#define CURRENT_THRESHOLD_DIMMER_CONSECUTIVE 20 // Number of consecutive times the current has to be above the threshold before triggering the softfuse.
191
192
193#define SWITCHCRAFT_THRESHOLD (500000) // Threshold for switch recognition (float).
194
196static const uint32_t SWITCHCRAFT_DOUBLE_TAP_TIMEOUT_MS = 1000;
197
199static const uint8_t DEFAULT_DIM_VALUE = 40;
200
201#define PWM_PERIOD 10000L // Interval in us: 1/10000e-6 = 100 Hz
202
203#define SWITCH_DELAYED_STORE_MS (10 * 1000) // Timeout before storing the pwm switch value is stored.
204#define STATE_RETRY_STORE_DELAY_MS 200 // Time before retrying to store a varable to flash.
205#define MESH_SEND_TIME_INTERVAL_MS (50 * 1000) // Interval at which the time is sent via the mesh.
206#define MESH_SEND_TIME_INTERVAL_MS_VARIATION (20 * 1000) // Max amount that gets added to interval.
207#define MESH_SEND_STATE_INTERVAL_MS (50 * 1000) // Interval at which the stone state is sent via the mesh.
208#define MESH_SEND_STATE_INTERVAL_MS_VARIATION (20 * 1000) // Max amount that gets added to interval.
209#define MESH_SYNC_RETRY_INTERVAL_MS (2500)
210#define MESH_SYNC_GIVE_UP_MS (60 * 1000) // After some time, give up syncing.
211#define CS_MESH_DEFAULT_TTL 10
212
213#define PWM_BOOT_DELAY_MS 60000 // Delay after boot until pwm can be used. Has to be smaller than overflow time of RTC.
214#define DIMMER_BOOT_CHECK_DELAY_MS 5000 // Delay after boot until power measurement is checked to see if dimmer works.
215#define DIMMER_BOOT_CHECK_POWER_MW 3000 // Threshold in milliWatt above which the dimmer is considered to be working.
216#define DIMMER_BOOT_CHECK_POWER_MW_UNCALIBRATED 10000 // Threshold in milliWatt above which the dimmer is considered to be working, in case power zero is not calibrated yet.
217#define DIMMER_SOFT_ON_SPEED 8 // Speed of the soft on feature.
218
219// Stack config values
220// See: https://devzone.nordicsemi.com/question/60/what-is-connection-parameters/
221
222/*
223 * Determines how often the Central will ask for data from the Peripheral. When the Peripheral requests an update, it
224 * supplies a maximum and a minimum wanted interval. The connection interval must be between 7.5 ms and 4 s.
225 * See https://devzone.nordicsemi.com/question/161154/minimum-connection-interval/
226 *
227 * If the connection interval is equal to NRF_SDH_BLE_GAP_EVENT_LENGTH, the connection can take up 100% of the radio time, leaving none for the mesh.
228 * The shortest timeslot the mesh uses is 3.8ms (TIMESLOT_BASE_LENGTH_SHORT_US),
229 * so we could consider setting MIN_CONNECTION_INTERVAL to 4 * 1.25 ms larger than NRF_SDH_BLE_GAP_EVENT_LENGTH.
230 * NRF_SDH_BLE_GAP_EVENT_LENGTH is by default equal to 6.
231 */
232#define MIN_CONNECTION_INTERVAL 6 // In units of 1.25ms.
233//#define MAX_CONNECTION_INTERVAL 136 // In units of 1.25ms. (4 s)
234//#define MAX_CONNECTION_INTERVAL 16 // In units of 1.25ms. (20 ms)
235//
236#define MAX_CONNECTION_INTERVAL 6 // In units of 1.25ms. (7.5 ms)
237
238/*
239 * This timeout determines the timeout from the last data exchange till a link is considered lost. A Central will not
240 * start trying to reconnect before the timeout has passed, so if you have a device which goes in and out of range
241 * often, and you need to notice when that happens, it might make sense to have a short timeout.
242 * - https://devzone.nordicsemi.com/f/nordic-q-a/10636/what-are-latency-and-supervision-timeout-limits
243 */
244#define CONNECTION_SUPERVISION_TIMEOUT 100 // In units of 10ms.
245
246/*
247 * By setting a non-zero slave latency, the Peripheral can choose to not answer when the Central asks for data up to
248 * the slave latency number of times. However, if the Peripheral has data to send, it can choose to send data at any
249 * time. This enables a peripheral to stay sleeping for a longer time, if it doesn't have data to send, but still send
250 * data fast if needed. The text book example of such device is for example keyboard and mice, which want to be
251 * sleeping for as long as possible when there is no data to send, but still have low latency (and for the mouse:
252 * low connection interval) when needed.
253 *
254 * Slave latency increases delay of data from central to peripheral, but not from peripheral to central.
255 * - https://devzone.nordicsemi.com/f/nordic-q-a/53230/what-is-the-impact-of-slave-latencyin-ble
256 *
257 * Timeout will be CONNECTION_SUPERVISION_TIMEOUT * SLAVE_LATENCY.
258 * - https://devzone.nordicsemi.com/question/14029/slave-latency-for-s110s120-connection/
259 */
260#define SLAVE_LATENCY 0
261
262#define ADVERTISING_REFRESH_PERIOD 500 // Push the changes in the advertisement packet to the stack every x milliseconds
263#define ADVERTISING_REFRESH_PERIOD_SETUP 500 // Push the changes in the advertisement packet to the stack every x milliseconds
264
265#define EXTERNAL_STATE_LIST_COUNT 10 // Number of stones to cache the state of, for advertising external state.
266#define EXTERNAL_STATE_TIMEOUT_MS 60000 // Time after which a state of another stone is considered to be timed out.
267
268#define SWITCH_ON_AT_SETUP_BOOT_DELAY 3600 // Seconds until the switch turns on when in setup mode (Crownstone built-in only)
269
270#define SUN_TIME_THROTTLE_PERIOD_SECONDS (60*60*24) // Seconds to throttle writing the sun time to flash.
271
272#define CS_CLEAR_GPREGRET_COUNTER_TIMEOUT_S 60 // Seconds after boot to clear the GPREGRET reset counter.
273
277#define TICK_INTERVAL_MS 100
278
279#define CONFIG_POWER_ZERO_INVALID 0x7FFFFFFF
280
281#ifndef STATE_SWITCH_STATE_DEFAULT
282#define STATE_SWITCH_STATE_DEFAULT 0
283#endif
284
285#ifndef STATE_ACCUMULATED_ENERGY_DEFAULT
286#define STATE_ACCUMULATED_ENERGY_DEFAULT 0
287#endif
288
289#ifndef STATE_POWER_USAGE_DEFAULT
290#define STATE_POWER_USAGE_DEFAULT 0
291#endif
292
293#ifndef STATE_RESET_COUNTER_DEFAULT
294#define STATE_RESET_COUNTER_DEFAULT 0
295#endif
296
297#ifndef STATE_OPERATION_MODE_DEFAULT
298#define STATE_OPERATION_MODE_DEFAULT 0
299#endif
300
301#ifndef STATE_TEMPERATURE_DEFAULT
302#define STATE_TEMPERATURE_DEFAULT 0
303#endif
304
305#ifndef STATE_FACTORY_RESET_DEFAULT
306#define STATE_FACTORY_RESET_DEFAULT 0
307#endif
308
309#ifndef STATE_ERRORS_DEFAULT
310#define STATE_ERRORS_DEFAULT 0
311#endif
312
313#ifndef STATE_BEHAVIOUR_SETTINGS_DEFAULT
314#define STATE_BEHAVIOUR_SETTINGS_DEFAULT 1 // Enabled (bit 0) is true by default.
315#endif
316
317#ifndef STATE_BEHAVIOUR_MASTER_HASH_DEFAULT
318#define STATE_BEHAVIOUR_MASTER_HASH_DEFAULT 0
319#endif
320
321#ifndef STATE_HUB_MODE_DEFAULT
322#define STATE_HUB_MODE_DEFAULT 0
323#endif
324
325// clang-format on
static const uint8_t DEFAULT_DIM_VALUE
The default default dim value.
Definition: cs_Config.h:199
static const uint32_t SWITCHCRAFT_DOUBLE_TAP_TIMEOUT_MS
If 2 switchcraft events happen within this time, it will be regarded as a double tap.
Definition: cs_Config.h:196