forked from mooshim/Mooshimeter-IOSApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MooshimeterProfileTypes.h
284 lines (249 loc) · 8.16 KB
/
MooshimeterProfileTypes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// MooshimeterProfileTypes.h
// Mooshimeter
//
// Created by James Whong on 11/26/13.
// Copyright (c) 2013 mooshim. All rights reserved.
//
#ifndef Mooshimeter_MooshimeterProfileTypes_h
#define Mooshimeter_MooshimeterProfileTypes_h
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
#define SET_W_MASK(target, val, mask) target ^= (mask)&((val)^target)
#ifndef __IAR_SYSTEMS_ICC__
#define LO_UINT16(i) ((i ) & 0xFF)
#define HI_UINT16(i) ((i>>8) & 0xFF)
#endif
#define MOOSHIM_BASE_UUID_128( uuid ) 0xd4, 0xdb, 0x05, 0xe0, 0x54, 0xf2, 0x11, 0xe4, \
0xab, 0x62, 0x00, 0x02, LO_UINT16( uuid ), HI_UINT16( uuid ), 0xc5, 0x1b
#define METER_SERVICE_UUID 0xFFA0
#define METER_INFO 0xFFA1
#define METER_NAME 0xFFA2
#define METER_SETTINGS 0xFFA3
#define METER_LOG_SETTINGS 0xFFA4
#define METER_UTC_TIME 0xFFA5
#define METER_SAMPLE 0xFFA6
#define METER_CH1BUF 0xFFA7
#define METER_CH2BUF 0xFFA8
#define METER_CAL 0xFFA9
#define METER_LOG_DATA 0xFFAA
#define METER_TEMP 0xFFAB
#define METER_BAT 0xFFAC
#define OAD_SERVICE_UUID 0xFFC0
#define OAD_IMAGE_NOTIFY 0xFFC1
#define OAD_IMAGE_BLOCK_REQ 0xFFC2
#define OAD_REBOOT 0xFFC3
#define METER_NAME_LEN 16
#define METER_NAME_DEFAULT "Mooshimeter V.1"
#define N_ADC_SAMPLES_LOG2 8
#define N_ADC_SAMPLES (1<<N_ADC_SAMPLES_LOG2)
#define METER_INFO_DEFAULT {\
METER_PCB_VERSION,\
0,\
0,\
BUILD_TIME,\
{0,0,0,0,0,0,0,0,0,0,0,0}}
#define LOG_SETTINGS_DEFAULT {\
{ 0,\
LOGGING_OFF,\
LOGGING_NO_MEDIA},\
{ LOGGING_OFF,\
0,\
0} }
#define METER_SETTINGS_DEFAULT {\
.ro={ .present_meter_state=METER_SHUTDOWN,}, \
.rw={ .target_meter_state=METER_SHUTDOWN,\
.trigger_settings={.setting=0,.x_offset=0,.crossing={.bytes={0,0,0}}},\
.measure_settings=0x00,\
.calc_settings=0x06,\
.ch1set=0x10,\
.ch2set=0x10,\
.adc_settings=0x00, } }
#define METER_FAKE_CAL { \
0,\
0,\
{{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}},\
{0,0,0,0,0,0,0},\
{{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}},\
{{0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000},{0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,0x8000,}},\
0x8000,\
0x8000,\
0x8000,\
0x8000,\
0x8000\
}
#define METER_EMPTY_CAL { \
0xFFFFFFFF,\
0xFFFF,\
{{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF},{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF}},\
{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF},\
{{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF},{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF}},\
{{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF},{0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,}},\
0xFFFF,\
0xFFFF,\
0xFFFF,\
0xFFFF,\
0xFFFF\
}
#ifdef __IAR_SYSTEMS_ICC__
#include "int24.h"
#else
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef signed char int8;
typedef signed short int16;
typedef union {
char bytes[3];
} int24_test;
#endif
// XCode 64 bit transition makes longs 8 bytes wide
// But IAR for 8051 calls int 2 bytes wide, so we need some compiler
// specific switches
#ifndef __IAR_SYSTEMS_ICC__
typedef unsigned int uint32;
typedef signed int int32;
#endif
typedef enum
#ifndef __IAR_SYSTEMS_ICC__
: uint8
#endif
{
METER_SHUTDOWN=0, // Booting from power down.
METER_STANDBY, // uC sleeping, ADC powered down. Wakes to advertise occasionally.
METER_PAUSED, // uC active, ADC active, not sampling
METER_RUNNING, // uC active, ADC active, sampling until buffer is full, then performing computations and repeating
METER_HIBERNATE, // uC sleeping, radio off. Wake up to test if inputs are shorted, and if they are reboot.
} meter_state_t;
typedef enum
#ifndef __IAR_SYSTEMS_ICC__
: uint8
#endif
{
LOGGING_OFF=0,
LOGGING_CALC, // The output of the computations specified by meter_settings to go in meter_sample are logged
LOGGING_BUF, // TODO: The raw data buffers are logged
LOGGING_STREAMING // TODO: The log is being streamed out to the BLE host
} logging_state_t;
typedef enum
#ifndef __IAR_SYSTEMS_ICC__
: uint8
#endif
{
LOGGING_NO_MEDIA=0,
LOGGING_READY,
LOGGING_INSUFFICIENT_SPACE,
LOGGING_WRITE_ERROR,
LOGGING_END_OF_FILE,
} logging_error_t;
#define TRIGGER_SETTING_SRC_OFF (0x00)
#define TRIGGER_SETTING_SRC_CH1 (0x01)
#define TRIGGER_SETTING_SRC_CH2 (0x02)
#define TRIGGER_SETTING_EDGE_RISING (0x00 <<2)
#define TRIGGER_SETTING_EDGE_FALLING (0x01 <<2)
#define TRIGGER_SETTING_EDGE_EITHER (0x02 <<2)
typedef struct {
uint8 setting;
uint16 x_offset; // TODO: x offset for the trigger point to rest at
int24_test crossing; // Value at which to trigger
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
trigger_settings_t;
typedef struct {
uint8 pcb_version;
uint8 assembly_variant;
uint16 lot_number;
uint32 build_time;
uint8 reserved[12];
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
MeterInfo_t;
// The 7 indices refer to the 7 possible PGA settings
// The gains below are 16 bit fixed point values representing a number
// between 0 and 2
typedef struct {
uint32 build_time;
uint16 cal_temp;
int16 ch_offsets[2][7]; // This contains CH2 in low voltage mode gpio=0x01
int16 ch2_hv_offsets[7];
int16 ch_3_offsets[2][7];
uint16 ch_gain[2][7]; // Gains are fixed point between 0 and 2
uint16 ch1_isns_gain;
uint16 ch2_60v_gain;
uint16 ch2_600v_gain;
uint16 ch2_100na_gain;
uint16 ch2_100ua_gain;
uint16 __pad;
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
MeterFactoryCal_t;
#define METER_MEASURE_SETTINGS_ISRC_ON 0x01
#define METER_MEASURE_SETTINGS_ISRC_LVL 0x02
#define METER_MEASURE_SETTINGS_ACTIVE_PULLDOWN 0x04
#define METER_CALC_SETTINGS_DEPTH_LOG2 0x0F
#define METER_CALC_SETTINGS_MEAN 0x10
#define METER_CALC_SETTINGS_ONESHOT 0x20
#define METER_CALC_SETTINGS_MS 0x40
#define ADC_SETTINGS_SAMPLERATE_MASK 0x07
#define ADC_SETTINGS_GPIO_MASK 0x30
#define METER_CH_SETTINGS_PGA_MASK 0x70
#define METER_CH_SETTINGS_INPUT_MASK 0x0F
typedef struct {
struct {
meter_state_t present_meter_state; // The state of the meter right now.
} ro;
struct {
meter_state_t target_meter_state; // The target state of the meter
trigger_settings_t trigger_settings; // Trigger control
uint8 measure_settings; // Specifies features to turn on and off. Note that voltage gain is controlled through ADC settings
uint8 calc_settings; // Specifies what analysis to run on captured data
uint8 ch1set;
uint8 ch2set;
uint8 adc_settings;
} rw;
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
MeterSettings_t;
typedef struct {
struct {
uint8 sd_present;
logging_state_t present_logging_state;
logging_error_t logging_error;
uint16 file_number; // The log file number. A new record is started every logging session.
uint32 file_offset; // The offset within the file that is being written to (when logging) or read from (when streaming out)
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
ro;
struct {
logging_state_t target_logging_state;
uint16 logging_period_ms; // How long to wait between taking log samples
uint32 logging_n_cycles; // How many samples to take before sleeping forever
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
rw;
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
MeterLogSettings_t;
typedef struct {
int24_test ch1_reading_lsb; // Mean of the sample buffer
int24_test ch2_reading_lsb; // Mean of the sample buffer
float ch1_ms; // Mean square of the buffer. Square root it on the other size.
float ch2_ms;
}
#ifndef __IAR_SYSTEMS_ICC__
__attribute__((packed))
#endif
MeterMeasurement_t;
#endif