This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
rtcs.erver.sb
361 lines (311 loc) · 12.6 KB
/
rtcs.erver.sb
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2015, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++ ++
// +++++ When UwTerminal downloads the app it will store it as a filenname ++
// +++++ which consists of all characters up to the first . and excluding it ++
// +++++ ++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// BL600 custom Real Time Clock (RTC) Service
// This sample application implements the BLE RTC service with
// characteristics to allow setting the time remotely.
//
// Please note that this requires an external MCP79410 or compatible RTC chip
// connected to the BL600 I2C lines (it is not provided on the BL600-DVK)
//
// This is a cut-down version of the BT900 RTC server sample application and
// does not feature the alarm or buzzer functionality.
//
//******************************************************************************
//
// -> Sends BLE adverts with time from RTC
// -> Allows remote time setting
//
#DEFINE BLE_SERVICE_SECONDARY 0
#DEFINE BLE_SERVICE_PRIMARY 1
dim rc //Result code
dim I2Ch //I2C Handle
DIM Service1 //Composite handle for hts primary service
DIM Service1UUID : Service1UUID = BleHandleUuid16(0x1805) //HT Svc UUID Handle, 0x1805 for current time service
DIM Char1 //Characteristic 1 (Current time)
DIM Char2 //Characteristic 2 (Local time)
DIM Char3 //Characteristic 3 (Reference time)
DIM Char4 //Characteristic 4 (Version)
DIM Timezone : Timezone = 0 //Hours off from UTC, see https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.time_zone.xml
DIM DST : DST = 0 //0 for no DST, 4 for +1 hour DST, see https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.dst_offset.xml
DIM SendNotify : SendNotify = 0 //Set to 1 when notifications are enabled
dim APP_VERSION$ : APP_VERSION$ = "0.92"
dim I2CSpeed : I2CSpeed = 250000 //250KHz
dim RTCAddr : RTCAddr = 111 //I2C Address of RTC
dim AdjustReason : AdjustReason = 0 //Reason for last time adjustment
dim DayOfWeek : DayOfWeek = 0 //The current day of the week (0 = unknown)
//******************************************************************************
// Command success handler
//******************************************************************************
SUB AssertRC(rc,ln)
IF rc!=0 THEN
PRINT "\nFail :";integer.h' rc;" at tag ";ln
ENDIF
ENDSUB
FUNCTION HndlrTmr0()
//Timer function to read RTC and output value
dim secVal, minVal, hourVal, dateVal, monthVal, yearVal
rc = I2COpen(I2CSpeed, 0, I2Ch)
rc = I2CReadReg8(RTCAddr, 0, secVal)
rc = I2CReadReg8(RTCAddr, 1, minVal)
rc = I2CReadReg8(RTCAddr, 2, hourVal)
rc = I2CReadReg8(RTCAddr, 4, dateVal)
rc = I2CReadReg8(RTCAddr, 5, monthVal)
rc = I2CReadReg8(RTCAddr, 6, yearVal)
//Close I2C connection
I2CClose(I2Ch)
//Convert I2C RTC values into time & date information
dim Seconds, Minutes, Hours, Day, Month, Year
Seconds = ((secVal & 15) + ((secVal & 112)>>4)*10)
Minutes = ((minVal & 15) + ((minVal & 112)>>4)*10)
Hours = ((hourVal & 15) + ((hourVal & 48)>>4)*10)
Day = ((dateVal & 15) + ((dateVal & 48)>>4)*10)
Month = ((monthVal & 15) + ((monthVal & 16)>>4)*10)
Year = (2001 + (yearVal & 15) + ((yearVal & 240)>>4)*10)
PRINT Hours;":";Minutes;":";Seconds;" on ";Day;"/";Month;"/";Year;"\n"
//Check if the day has rolled over
IF (Seconds == 0 && Minutes == 0 && Hours == 0 && DayOfWeek > 0) THEN
DayOfWeek = DayOfWeek+1
IF (DayOfWeek > 7) THEN
DayOfWeek = 1
ENDIF
ENDIF
//Encode data
dim TmpStr$ : TmpStr$ = ""
rc = BleEncode8(TmpStr$, AdjustReason, 9) //Adjust reason
rc = BleEncode8(TmpStr$, 0, 8) //1/256 seconds
rc = BleEncode8(TmpStr$, DayOfWeek, 7) //Day of week
rc = BleEncode8(TmpStr$, Seconds, 6)
rc = BleEncode8(TmpStr$, Minutes, 5)
rc = BleEncode8(TmpStr$, Hours, 4)
rc = BleEncode8(TmpStr$, Day, 3)
rc = BleEncode8(TmpStr$, Month, 2)
rc = BleEncode16(TmpStr$, Year, 0)
//Update GATT information
rc = BleCharValueWrite(Char1, TmpStr$)
assertrc(rc,89)
//Update BLE advert with new information
DIM advRpt$ : advRpt$ = ""
DIM scRpt$ : scRpt$ = ""
rc = BleAdvRptInit(advRpt$, 2, 1, 10)
AssertRC(rc,95)
rc = BleAdvRptAppendAD(advRpt$, 0x16, TmpStr$)
AssertRC(rc,97)
rc = BleAdvRptsCommit(advRpt$, scRpt$)
AssertRC(rc,99)
//Send notification if enabled
IF SendNotify == 1 THEN
rc = BleCharValueNotify(Char1, TmpStr$)
AssertRC(rc,104)
ENDIF
ENDFUNC 1
FUNCTION HndlrCharCccd(BYVAL charHandle, BYVAL nVal)
//Runs when CCCD has been written
IF nVal == 1 THEN
SendNotify = 1
ELSE
SendNotify = 0
ENDIF
ENDFUNC 1
FUNCTION BleEvent(BYVAL msgID, BYVAL msgCtx)
//Runs when a BLE connection trigger occurs
IF msgID == 0 THEN
//Connection
PRINT "Connect.\n"
ELSEIF msgID == 1 THEN
//Disconnection
PRINT "End!\n"
//Stop notifications
SendNotify = 0
//Restart BLE adverts
dim addr$ : addr$ = ""
PRINT BleAdvertStart(0,addr$,500,0,0)
ENDIF
ENDFUNC 1
FUNCTION HndlrCharVal(BYVAL charHandle, BYVAL offset, BYVAL len)
//When characteristic is written
dim attr$
dim TmpVal, TmpVal2
IF charHandle == Char1 THEN
//Time & date has been updated
rc = BleCharValueRead(charHandle, attr$)
//Open I2C
rc = I2COpen(I2CSpeed, 0, I2Ch)
//Adjust reason
rc = BleDecodeU8(attr$, AdjustReason, 9)
//Day of the week
rc = BleDecodeU8(attr$, DayOfWeek, 7)
//Seconds
rc = BleDecodeU8(attr$, TmpVal, 6)
TmpVal2 = 0
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = (((TmpVal2/10)<<4) + (TmpVal & 15)) | 128
rc = I2CWriteReg8(RTCAddr, 0, TmpVal2) //Seconds
//Minutes
rc = BleDecodeU8(attr$, TmpVal, 5)
TmpVal2 = 0
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = ((TmpVal2/10)<<4) + (TmpVal & 15)
rc = I2CWriteReg8(RTCAddr, 1, TmpVal2) //Minutes
//Hours
rc = BleDecodeU8(attr$, TmpVal, 4)
TmpVal2 = 0
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = (((TmpVal2/10)<<4) + (TmpVal & 15)) & 63
rc = I2CWriteReg8(RTCAddr, 2, TmpVal2) //Hour
//Day
rc = BleDecodeU8(attr$, TmpVal, 3)
TmpVal2 = 0
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = ((TmpVal2/10)<<4) + (TmpVal & 15)
rc = I2CWriteReg8(RTCAddr, 4, TmpVal2) //Day
//Month
rc = BleDecodeU8(attr$, TmpVal, 2)
TmpVal2 = 0
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = ((TmpVal2/10)<<4) + (TmpVal & 15)
rc = I2CWriteReg8(RTCAddr, 5, TmpVal2) //Month
//Year
rc = BleDecodeU16(attr$, TmpVal, 0)
TmpVal2 = 0
TmpVal = TmpVal - 2001
WHILE TmpVal > 10
TmpVal2 = TmpVal2 + 10
TmpVal = TmpVal - 10
ENDWHILE
TmpVal2 = ((TmpVal2/10)<<4) + (TmpVal & 15)
rc = I2CWriteReg8(RTCAddr, 6, TmpVal2) //Year
//Close I2C
I2CClose(I2Ch)
ENDIF
ENDFUNC 1
//Open I2C
rc = I2COpen(I2CSpeed, 0, I2Ch)
IF (rc != 0) THEN
//I2C isn't working
PRINT "Unable to communicate \n"
ENDIF
//Check time
dim secVal, minVal, hourVal, dayVal, monthVal, yearVal
rc = I2CReadReg8(RTCAddr, 0, secVal)
rc = I2CReadReg8(RTCAddr, 1, minVal)
rc = I2CReadReg8(RTCAddr, 4, dayVal)
rc = I2CReadReg8(RTCAddr, 5, monthVal)
rc = I2CReadReg8(RTCAddr, 6, yearVal)
IF ((secVal & 112) == 0 && (minVal & 112) == 0 && (dayVal & 63) == 1 && (monthVal & 31) == 1 && yearVal == 1) THEN
//No settings configured! Set time to Monday, 8:15am
rc = I2CWriteReg8(RTCAddr, 3, 41) //Power and day of week (day 1/Monday)
rc = I2CWriteReg8(RTCAddr, 1, 16+5) //Minutes
rc = I2CWriteReg8(RTCAddr, 2, 8) //Hours (24-hour format)
//Set date to 15/04/2014
rc = I2CWriteReg8(RTCAddr, 4, 21) //15th
rc = I2CWriteReg8(RTCAddr, 5, 4) //April
rc = I2CWriteReg8(RTCAddr, 6, 19) //2014 (2014-2001 = 13)
//Enable RTC
rc = I2CWriteReg8(RTCAddr, 0, 128)
//Output
PRINT "RTC not configured, set to 15/04/2014 @ 8:15am.\n"
ENDIF
//Close I2C
I2CClose(I2Ch)
//Start a BLE server
rc = BleGAPSvcInit("CurrentTimeServer", 0, 256, 80000, 100000, 4000000, 0)
AssertRC(rc,430)
rc = BleSvcRegDevInfo("Laird", "BL600", "", "1.0", "1.0", "", "", "")
AssertRC(rc,432)
//Create service
rc = BleServiceNew(BLE_SERVICE_PRIMARY, Service1UUID, Service1)
AssertRC(rc,436)
//Add first characteristics
dim hrs$ : hrs$ = " "
rc = BleCharNew(0x1A, BleHandleUuid16(0x2A2B), BleAttrMetadata(1,1,10,0,rc), BleAttrMetadata(1,1,2,0,rc),0) //Current time (Read, write & Notify) with CCCD
assertrc(rc,441)
rc = BleCharCommit(Service1, hrs$, Char1)
assertrc(rc,443)
//Second characteristic
hrs$ = "00"
rc = BleCharNew(0x2, BleHandleUuid16(0x2A0F), BleAttrMetadata(1,0,2,0,rc), 0, 0) //Local time (Read)
assertrc(rc,448)
rc = BleCharCommit(Service1, hrs$, Char2)
assertrc(rc,450)
//Third characteristic
hrs$ = "0000"
rc = BleCharNew(0x2, BleHandleUuid16(0x2A14), BleAttrMetadata(1,0,4,0,rc), 0, 0) //Reference time (Read)
assertrc(rc,455)
rc = BleCharCommit(Service1, hrs$, Char3)
assertrc(rc,457)
//Fourth (and final) characteristic
rc = BleCharNew(0x2, BleHandleUuid16(0x2B04), BleAttrMetadata(1,0,5,0,rc), 0, 0) //Version (Read)
assertrc(rc,483)
rc = BleCharCommit(Service1, APP_VERSION$, Char4)
assertrc(rc,485)
//Commit services
rc = BleServiceCommit(Service1)
AssertRC(rc,489)
//Setup local time information characteristic
dim TmpStr$ : TmpStr$ = ""
rc = BleEncode8(TmpStr$, DST, 1) //DST
rc = BleEncode8(TmpStr$, Timezone, 0) //Timezone
//Update GATT information
rc = BleCharValueWrite(Char2, TmpStr$)
//Setup reference time information characteristic
TmpStr$ = ""
rc = BleEncode8(TmpStr$, 255, 3) //Hours since update
rc = BleEncode8(TmpStr$, 255, 2) //Days since update
rc = BleEncode8(TmpStr$, 255, 1) //Accuracy is unknown
rc = BleEncode8(TmpStr$, 4, 0) //Source is manual
//Update GATT information
rc = BleCharValueWrite(Char3, TmpStr$)
//Enable timer to run every second
TimerStart(0, 1000, 1)
//Start BLE adverts
dim addr$ : addr$ = ""
rc = BleAdvertStart(0,addr$,500,0,0)
AssertRC(rc,530)
//Run initial time send
rc = HndlrTmr0()
//Startup message
DIM BTAddr$
BTAddr$ = SysInfo$(4)
PRINT "\nrtcs.erver running with Bluetooth address: ";STRHEXIZE$(BTAddr$);"\nNow accepting connections...\n\n"
//Events
ONEVENT EVTMR0 CALL HndlrTmr0 //Runs when timer0 expires
ONEVENT EVBLEMSG CALL BleEvent //Runs on BLE event
ONEVENT EVCHARCCCD CALL HndlrCharCccd //Runs when CCCD is written
ONEVENT EVCHARVAL CALL HndlrCharVal //Runs when characteristic is written
WAITEVENT