-
Notifications
You must be signed in to change notification settings - Fork 59
/
app_start.c
1608 lines (1415 loc) · 62.7 KB
/
app_start.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
*
* MODULE: ZigbeeNodeControlBridge
*
* COMPONENT: app_start.c
*
* $AUTHOR: Faisal Bhaiyat $
*
* DESCRIPTION:
*
* $HeadURL: $
*
* $Revision: 54887 $
*
* $LastChangedBy: nxp29741 $
*
* $LastChangedDate: $
*
* $Id: app_start.c $
*
*****************************************************************************
*
* This software is owned by NXP B.V. and/or its supplier and is protected
* under applicable copyright laws. All rights are reserved. We grant You,
* and any third parties, a license to use this software solely and
* exclusively on NXP products [NXP Microcontrollers such as JN5168, JN5179].
* You, and any third parties must reproduce the copyright and warranty notice
* and any other legend of ownership on each copy or partial copy of the
* software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Copyright NXP B.V. 2016. All rights reserved
*
****************************************************************************/
/****************************************************************************/
/*** Include files ***/
/****************************************************************************/
#include <jendefs.h>
#include "pwrm.h"
#include "pdum_apl.h"
#include "pdum_nwk.h"
#include "pdum_gen.h"
#include "PDM.h"
#include "dbg_uart.h"
#include "dbg.h"
#include "zps_gen.h"
#include "zps_apl_af.h"
#include "AppApi.h"
#include "zps_nwk_pub.h"
#include "zps_mac.h"
#include "rnd_pub.h"
#include "HtsDriver.h"
#include "Button.h"
#include <string.h>
#include "SerialLink.h"
#include "app_Znc_cmds.h"
#include "uart.h"
#include "mac_pib.h"
#include "PDM_IDs.h"
#include "app_common.h"
#include "Log.h"
#include "app_events.h"
#include "zcl_common.h"
#include "rnd_pub.h"
#ifdef STACK_MEASURE
#include "StackMeasure.h"
#endif
#ifdef CLD_OTA
#include "app_ota_server.h"
#endif
#if (JENNIC_CHIP_FAMILY == JN517x)
#include "AHI_ModuleConfiguration.h"
#endif
#ifdef CLD_GREENPOWER
#include "app_green_power.h"
#endif
#include "pdm_flash.h"
/****************************************************************************/
/*** Macro Definitions ***/
/****************************************************************************/
#ifndef DEBUG_WDR
#define DEBUG_WDR FALSE
#endif
#ifndef UART_DEBUGGING
#define UART_DEBUGGING FALSE
#endif
#ifndef TRACE_APPSTART
#define TRACE_APPSTART FALSE
#endif
#ifndef TRACE_MIGRATION
#define TRACE_MIGRATION FALSE
#endif
#ifndef VERSION
#define VERSION 0x0003031e
#endif
#ifndef TRACE_EXC
#define TRACE_EXC FALSE
#endif
#if (JENNIC_CHIP_FAMILY == JN516x)
#define LED1_DIO_PIN ( 1 << 16 )
#define LED2_DIO_PIN ( 1 << 17 )
#define LED_DIO_PINS ( LED1_DIO_PIN |\
LED2_DIO_PIN )
#endif
#if (JENNIC_CHIP_FAMILY == JN517x)
#define LED1_DIO_PIN ( 1 << 14 )
#define LED2_DIO_PIN ( 1 << 15 )
#define LED_DIO_PINS ( LED1_DIO_PIN |\
LED2_DIO_PIN )
#endif
#ifdef CLD_GREENPOWER
PUBLIC uint8 u8GPTimerTick;
#define APP_NUM_GP_TMRS 1
#define GP_TIMER_QUEUE_SIZE 2
#else
#define APP_NUM_GP_TMRS 0
#endif
#ifdef CLD_GREENPOWER
PUBLIC tszQueue APP_msgGPZCLTimerEvents;
uint8 au8GPZCLEvent[ GP_TIMER_QUEUE_SIZE];
uint8 u8GPZCLTimerEvent;
#endif
#define TIMER_QUEUE_SIZE 8
#define MLME_QUEQUE_SIZE 8
#define MCPS_DCFM_QUEUE_SIZE 5
#define MCPS_QUEUE_SIZE 20
#define ZPS_QUEUE_SIZE 2
#define APP_QUEUE_SIZE 8
#define TX_QUEUE_SIZE 150
#define RX_QUEUE_SIZE 150
#define BDB_QUEUE_SIZE 2
#define APP_NUM_STD_TMRS 4
#define APP_ZTIMER_STORAGE (APP_NUM_STD_TMRS + APP_NUM_GP_TMRS)
#if JENNIC_CHIP_FAMILY == JN517x
#define NVIC_INT_PRIO_LEVEL_BBC ( 7 )
#define NVIC_INT_PRIO_LEVEL_UART0 ( 5 )
#endif
/****************************************************************************/
/*** Type Definitions ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Function Prototypes ***/
/****************************************************************************/
PUBLIC void app_vFormatAndSendUpdateLists ( void );
PUBLIC void APP_vMainLoop ( void );
PUBLIC void APP_vInitResources ( void );
PUBLIC void APP_vSetUpHardware ( void );
#if (JENNIC_CHIP_FAMILY == JN516x)
/* Run time exception handlers */
void vBusErrorHandler ( void );
void vAlignmentErrorHandler (void );
void vIllegalInstructionHandler ( void );
void vUnclaimedException ( void );
void vStackOverflowHandler ( void );
void vReportException ( char* sExStr );
#endif
void vfExtendedStatusCallBack ( ZPS_teExtendedStatus eExtendedStatus );
PRIVATE void vInitialiseApp ( void );
#if (defined PDM_EEPROM && DBG_ENABLE)
PRIVATE void vPdmEventHandlerCallback ( uint32 u32EventNumber,
PDM_eSystemEventCode eSystemEventCode );
#endif
#if (defined PDM_EEPROM)
#if TRACE_APPSTART
#endif
#endif
PRIVATE void APP_cbTimerZclTick (void* pvParam);
PRIVATE void APP_MigratePDM( void );
/****************************************************************************/
/*** Exported Variables ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Variables ***/
/****************************************************************************/
bool_t bLedActivate = TRUE;
uint8_t bPowerCEFCC = APP_API_MODULE_HPM05;
bool_t bCtrlFlow = FALSE;
PUBLIC tsLedState s_sLedState = { LED1_DIO_PIN, ZTIMER_TIME_MSEC(1), FALSE };
#ifdef FULL_FUNC_DEVICE
tsZllEndpointInfoTable sEndpointTable;
tsZllGroupInfoTable sGroupTable;
#endif
tsZllState sZllState = {
.eState = FACTORY_NEW,
.eNodeState = E_STARTUP,
.u8DeviceType = 0,
.u8MyChannel = 11,
.u16MyAddr = TL_MIN_ADDR,
#ifdef FULL_FUNC_DEVICE
.u16FreeAddrLow = TL_MIN_ADDR,
.u16FreeAddrHigh = TL_MAX_ADDR,
.u16FreeGroupLow = TL_MIN_GROUP,
.u16FreeGroupHigh = TL_MAX_GROUP,
#endif
#ifdef CLD_OTA
.bValid = FALSE,
.u64IeeeAddrOfServer = 0,
.u16NwkAddrOfServer = 0,
.u8OTAserverEP = 0,
#endif
.u8RawMode = 0,
#ifdef MANUALDEFAULTRESPONSE
.u8DisableDefaultResponseMode = 0
#endif
};
PUBLIC tszQueue APP_msgSerialRx;
PUBLIC tszQueue APP_msgSerialTx;
PUBLIC tszQueue APP_msgBdbEvents;
PUBLIC tszQueue APP_msgAppEvents;
extern PUBLIC tszQueue zps_msgMcpsDcfm;
ZTIMER_tsTimer asTimers[APP_ZTIMER_STORAGE + BDB_ZTIMER_STORAGE];
zps_tsTimeEvent asTimeEvent [ TIMER_QUEUE_SIZE ];
MAC_tsMcpsVsDcfmInd asMacMcpsDcfmInd [ MCPS_QUEUE_SIZE ];
MAC_tsMcpsVsCfmData asMacMcpsDcfm[MCPS_DCFM_QUEUE_SIZE];
MAC_tsMlmeVsDcfmInd asMacMlmeVsDcfmInd [ MLME_QUEQUE_SIZE ];
BDB_tsZpsAfEvent asBdbEvent [ BDB_QUEUE_SIZE ];
APP_tsEvent asAppMsg [ APP_QUEUE_SIZE ];
uint8 au8AtRxBuffer [ RX_QUEUE_SIZE ];
uint8 au8AtTxBuffer [ TX_QUEUE_SIZE ];
uint8 u8IdTimer;
uint8 u8TmrToggleLED;
uint8 u8HaModeTimer;
uint8 u8TickTimer;
uint8 u8JoinedDevice = 0;
uint8 au8LinkRxBuffer[270];
ZPS_tsAfFlashInfoSet sSet;
/****************************************************************************/
/*** Exported Functions ***/
/****************************************************************************/
#ifdef FULL_FUNC_DEVICE
extern PUBLIC void APP_cbTimerCommission ( void* pvParam );
#endif
#if (defined PDM_EEPROM)
#if TRACE_APPSTART
extern PUBLIC uint8 u8PDM_CalculateFileSystemCapacity ( void );
extern PUBLIC uint8 u8PDM_GetFileSystemOccupancy ( void );
#endif
#endif
#if JENNIC_CHIP_FAMILY == JN517x
extern void APP_isrUart ( uint32 u32Device ,
uint32 u32ItemBitmap );
extern void vWatchdogHandler ( uint32 u32Device ,
uint32 u32ItemBitmap );
#endif
/****************************************************************************
*
* NAME: vAppMain
*
* DESCRIPTION:
* Entry point for application from a cold start.
*
* RETURNS:
* Never returns.
*
****************************************************************************/
PUBLIC void vAppMain(void)
{
uint8 u8FormJoin;
uint8 au8LinkTxBuffer[50];
// Wait until FALSE i.e. on XTAL - otherwise uart data will be at wrong speed
while ( bAHI_GetClkSource ( ) == TRUE );
// Now we are running on the XTAL, optimise the flash memory wait states.
vAHI_OptimiseWaitStates ( );
vAHI_DioSetDirection ( 0, LED_DIO_PINS );
vAHI_DioSetOutput ( LED_DIO_PINS, 0 );
/* set up storage in flash for the keys */
sSet.u16CredNodesCount = ZNC_MAX_TCLK_DEVICES;
sSet.u16SectorSize = 32 * 1024;
#ifdef JN5168
sSet.u8SectorSet = 7;
#else
sSet.u8SectorSet = 9;
#endif
#ifdef STACK_MEASURE
vInitStackMeasure ( );
#endif
#ifdef HWDEBUG
vAHI_WatchdogStart ( 12 );
#endif
#if (JENNIC_CHIP_FAMILY == JN516x)
vAHI_WatchdogException ( TRUE );
vAppApiSetHighPowerMode(bPowerCEFCC, TRUE);
#else
vAHI_WatchdogException ( TRUE , vWatchdogHandler );
#endif
/* Initialise debugging */
#if (UART_DEBUGGING == TRUE)
/* Send debug output to DBG_UART */
DBG_vUartInit ( E_AHI_UART_1, DBG_E_UART_BAUD_RATE_115200 );
#else
/* Send debug output through SerialLink to host */
vSL_LogInit ( );
#endif
DBG_vPrintf ( TRACE_APPSTART, "APP: Entering APP_vInitResources()\n" );
APP_vInitResources ( );
DBG_vPrintf ( TRACE_APPSTART, "APP: Entering APP_vSetUpHardware()\n" );
APP_vSetUpHardware ( );
#if (JENNIC_CHIP_FAMILY == JN517x)
vAHI_ModuleConfigure(E_MODULE_DEFAULT);
#endif
UART_vInit ( );
UART_vRtsStartFlow ( );
vLog_Printf ( TRACE_APPSTART,LOG_DEBUG, "\n\nInitialising \n" );
extern void* _stack_low_water_mark;
vSL_LogFlush ( );
vLog_Printf ( TRACE_APPSTART,LOG_INFO, "Stack low water mark = %08x\n", &_stack_low_water_mark );
#if (JENNIC_CHIP_FAMILY == JN516x)
vAHI_SetStackOverflow ( TRUE, ( uint32 ) &_stack_low_water_mark );
#endif
vLog_Printf ( TRACE_EXC, LOG_INFO, "\n** Control Bridge Reset** " );
if ( bAHI_WatchdogResetEvent ( ) )
{
/* Push out anything that might be in the log buffer already */
vLog_Printf ( TRACE_EXC, LOG_CRIT, "\n\n\n%s WATCHDOG RESET @ %08x ", "WDR", ( ( uint32* ) &_heap_location) [ 0 ] );
vSL_LogFlush ( );
}
//bAHI_FlashInit(141, NULL);
vInitialiseApp ();
//app_vFormatAndSendUpdateLists ( );
if (sZllState.eNodeState == E_RUNNING)
{
/* Declared within if statement. If it is declared at the top
* the function, the while loop will cause the data to be on
* the stack forever.
*/
if( sZllState.u8DeviceType >= 1 )
{
u8FormJoin = 0;
}
else
{
u8FormJoin = 1;
}
APP_vSendJoinedFormEventToHost ( u8FormJoin, au8LinkTxBuffer );
vSL_WriteMessage ( E_SL_MSG_NODE_NON_FACTORY_NEW_RESTART,
1,
( uint8* ) &sZllState.eNodeState,
0 ) ;
BDB_vStart();
}
else
{
vSL_WriteMessage( E_SL_MSG_NODE_FACTORY_NEW_RESTART,
1,
( uint8* ) &sZllState.eNodeState,
0);
}
ZTIMER_eStart ( u8TickTimer, ZCL_TICK_TIME );
DBG_vPrintf( TRACE_APPSTART, "APP: Entering APP_vMainLoop()\n");
APP_vMainLoop();
}
void vAppRegisterPWRMCallbacks(void)
{
}
void APP_cbToggleLED ( void* pvParam )
{
tsLedState* psLedState = ( tsLedState* ) pvParam;
if (bLedActivate)
{
vAHI_DioSetOutput ( LED_DIO_PINS, 0 );
if( ZPS_vNwkGetPermitJoiningStatus ( ZPS_pvAplZdoGetNwkHandle ( ) ) )
{
vAHI_DioSetOutput ( LED2_DIO_PIN, (psLedState->u32LedState) & LED_DIO_PINS );
vAHI_DioSetOutput ( LED1_DIO_PIN, (psLedState->u32LedState) & LED_DIO_PINS );
}
else
{
vAHI_DioSetOutput ( ( ~psLedState->u32LedState ) & LED_DIO_PINS, psLedState->u32LedState & LED_DIO_PINS );
}
psLedState->u32LedState = ( ~psLedState->u32LedState ) & LED_DIO_PINS;
if( u8JoinedDevice == 10 )
{
if( !ZPS_vNwkGetPermitJoiningStatus ( ZPS_pvAplZdoGetNwkHandle ( ) ) )
{
psLedState->u32LedToggleTime = ZTIMER_TIME_MSEC ( 1 );
}
if( ZPS_vNwkGetPermitJoiningStatus ( ZPS_pvAplZdoGetNwkHandle ( ) ) )
{
psLedState->u32LedToggleTime = ZTIMER_TIME_MSEC ( 500 );
}
u8JoinedDevice = 0;
}
u8JoinedDevice++;
ZTIMER_eStart( u8TmrToggleLED, psLedState->u32LedToggleTime );
}else{
vAHI_DioSetOutput(0, LED_DIO_PINS);
}
}
#ifdef FULL_FUNC_DEVICE
/****************************************************************************
*
* NAME: APP_vInitialiseNode
*
* DESCRIPTION:
* Initialises the application related functions
*
* RETURNS:
* void
*
****************************************************************************/
PUBLIC tsZllEndpointInfoTable * psGetEndpointRecordTable(void)
{
return &sEndpointTable;
}
/****************************************************************************
*
* NAME: psGetGroupRecordTable
*
* DESCRIPTION:
* return the address of the group record table
*
* RETURNS:
* pointer to the group record table
*
****************************************************************************/
PUBLIC tsZllGroupInfoTable * psGetGroupRecordTable(void)
{
return &sGroupTable;
}
#endif
/****************************************************************************/
/*** Local Functions ***/
/****************************************************************************/
/****************************************************************************
*
* NAME: vInitialiseApp
*
* DESCRIPTION:
* Initialises Zigbee stack, hardware and application.
*
* RETURNS:
* void
*
****************************************************************************/
PRIVATE void vInitialiseApp ( void )
{
uint16 u16DataBytesRead;
BDB_tsInitArgs sArgs;
uint8 u8DeviceType;
PDM_eInitialise ( 63 );
//APP_MigratePDM();
PDUM_vInit ( );
PWRM_vInit ( E_AHI_SLEEP_OSCON_RAMON );
#if (defined PDM_EEPROM && DBG_ENABLE)
PDM_vRegisterSystemCallback ( vPdmEventHandlerCallback );
#endif
#ifdef CLD_GREENPOWER
vAPP_GP_LoadPDMData();
#endif
sZllState.eNodeState = E_STARTUP;
/* Restore any application data previously saved to flash */
PDM_eReadDataFromRecord ( PDM_ID_APP_ZLL_CMSSION,
&sZllState,
sizeof ( tsZllState ),
&u16DataBytesRead );
#ifdef FULL_FUNC_DEVICE
PDM_eReadDataFromRecord ( PDM_ID_APP_END_P_TABLE,
&sEndpointTable,
sizeof ( tsZllEndpointInfoTable ),
&u16DataBytesRead );
PDM_eReadDataFromRecord ( PDM_ID_APP_GROUP_TABLE,
&sGroupTable,
sizeof ( tsZllGroupInfoTable ),
&u16DataBytesRead );
#endif
// ZPS_u32MacSetTxBuffers ( 5 );
#if TRACE_EXC != FALSE
tsZllEndpointInfoTable sEndpointTable;
vLog_Printf ( TRACE_EXC,LOG_DEBUG, "\nPDM: Capacity %d\n", u8PDM_CalculateFileSystemCapacity() );
vLog_Printf ( TRACE_EXC,LOG_DEBUG, "PDM: Occupancy %d\n", u8PDM_GetFileSystemOccupancy() );
vLog_Printf ( TRACE_EXC,LOG_DEBUG, "PDM: u16DataBytesRead %d\n", u16DataBytesRead );
vLog_Printf ( TRACE_EXC,LOG_DEBUG, "PDM: test %d\n", sEndpointTable.u8NumRecords );
#endif
if ( sZllState.eNodeState == E_RUNNING )
{
u8DeviceType = ( sZllState.u8DeviceType >= 2 ) ? 1 : sZllState.u8DeviceType;
APP_vConfigureDevice ( u8DeviceType );
ZPS_eAplAfInit ( );
}
else
{
ZPS_eAplAfInit ( );
sZllState.u8DeviceType = 0;
ZPS_vNwkNibSetChannel ( ZPS_pvAplZdoGetNwkHandle(), DEFAULT_CHANNEL);
ZPS_vNwkNibSetPanId (ZPS_pvAplZdoGetNwkHandle(), (uint16) RND_u32GetRand ( 1, 0xfff0 ) );
}
//Envoie message Start after PDM loaded
uint8_t au8values[1];
uint8_t u8Length=0;
ZNC_BUF_U8_UPD ( &au8values[ 0 ], 0, u8Length );
vSL_WriteMessage ( E_SL_MSG_PDM_LOADED,
1,
au8values,
0 );
/* If the device state has been restored from flash, re-start the stack
* and set the application running again.
*/
sBDB.sAttrib.bbdbNodeIsOnANetwork = ( ( sZllState.eNodeState >= E_RUNNING ) ? ( TRUE ) : ( FALSE ) );
sBDB.sAttrib.bTLStealNotAllowed = !sBDB.sAttrib.bbdbNodeIsOnANetwork;
sArgs.hBdbEventsMsgQ = &APP_msgBdbEvents;
BDB_vInit ( &sArgs );
ZPS_vExtendedStatusSetCallback(vfExtendedStatusCallBack);
APP_ZCL_vInitialise();
/* Needs to be after we initialise the ZCL and only if we are already
* running. If we are not running we will send the notify after we
* have a network formed notification.
*/
if (sZllState.eNodeState == E_RUNNING)
{
#ifdef CLD_OTA
vAppInitOTA();
#endif
}
}
/****************************************************************************
*
* NAME: APP_vMainLoop
*
* DESCRIPTION:
* Main application loop
*
* RETURNS:
* void
*
****************************************************************************/
PUBLIC void APP_vMainLoop(void)
{
bSetPermitJoinForever = TRUE;
while (TRUE)
{
zps_taskZPS ( );
bdb_taskBDB ( );
APP_vHandleAppEvents ( );
APP_vProcessRxData ( );
ZTIMER_vTask ( );
#ifdef DBG_ENABLE
vSL_LogFlush ( ); /* flush buffers */
#endif
/* Re-load the watch-dog timer. Execution must return through the idle
* task before the CPU is suspended by the power manager. This ensures
* that at least one task / ISR has executed within the watchdog period
* otherwise the system will be reset.
*/
vAHI_WatchdogRestart ( );
/*
* suspends CPU operation when the system is idle or puts the device to
* sleep if there are no activities in progress
*/
PWRM_vManagePower();
}
}
/****************************************************************************
*
* NAME: APP_vSetUpHardware
*
* DESCRIPTION:
* Set up interrupts
*
* RETURNS:
* void
*
****************************************************************************/
PUBLIC void APP_vSetUpHardware ( void )
{
#if (JENNIC_CHIP_FAMILY == JN517x)
vAHI_Uart0RegisterCallback ( APP_isrUart );
u32AHI_Init ( );
vAHI_InterruptSetPriority ( MICRO_ISR_MASK_BBC, NVIC_INT_PRIO_LEVEL_BBC );
vAHI_InterruptSetPriority ( MICRO_ISR_MASK_UART0, NVIC_INT_PRIO_LEVEL_UART0 );
#else
TARGET_INITIALISE ( );
/* clear interrupt priority level */
SET_IPL ( 0 );
portENABLE_INTERRUPTS ( );
#endif
}
#ifdef CLD_GREENPOWER
PUBLIC void APP_cbTimerGPZclTick(void *pvParam)
{
ZTIMER_eStart(u8GPTimerTick, GP_ZCL_TICK_TIME);
u8GPZCLTimerEvent = E_ZCL_TIMER_CLICK_MS;
ZQ_bQueueSend(&APP_msgGPZCLTimerEvents, &u8GPZCLTimerEvent);
}
#endif
/****************************************************************************
*
* NAME: APP_vInitResources
*
* DESCRIPTION:
* Initialise resources (timers, queue's etc)
*
* RETURNS:
* void
*
****************************************************************************/
PUBLIC void APP_vInitResources ( void )
{
vLog_Printf ( TRACE_APPSTART,LOG_DEBUG, "APP: Initialising resources...\n");
vLog_Printf ( TRACE_APPSTART,LOG_DEBUG, "APP: ZPS_tsAfEvent = %d bytes\n", sizeof ( ZPS_tsAfEvent ) );
vLog_Printf ( TRACE_APPSTART,LOG_DEBUG, "APP: zps_tsTimeEvent = %d bytes\n", sizeof ( zps_tsTimeEvent ) );
/* Initialise the Z timer module */
ZTIMER_eInit ( asTimers, sizeof(asTimers) / sizeof(ZTIMER_tsTimer));
/* Create Z timers */
ZTIMER_eOpen ( &u8TickTimer, APP_cbTimerZclTick, NULL, ZTIMER_FLAG_PREVENT_SLEEP );
ZTIMER_eOpen ( &u8IdTimer, APP_vIdentifyEffectEnd, NULL, ZTIMER_FLAG_PREVENT_SLEEP );
ZTIMER_eOpen ( &u8TmrToggleLED, APP_cbToggleLED, &s_sLedState, ZTIMER_FLAG_PREVENT_SLEEP );
ZTIMER_eOpen ( &u8HaModeTimer, App_TransportKeyCallback, &u64CallbackMacAddress, ZTIMER_FLAG_PREVENT_SLEEP );
#ifdef CLD_GREENPOWER
ZTIMER_eOpen(&u8GPTimerTick, APP_cbTimerGPZclTick, NULL, ZTIMER_FLAG_PREVENT_SLEEP );
#endif
/* Create all the queues */
ZQ_vQueueCreate ( &APP_msgBdbEvents, BDB_QUEUE_SIZE, sizeof ( BDB_tsZpsAfEvent ), (uint8*)asBdbEvent );
ZQ_vQueueCreate ( &zps_msgMlmeDcfmInd, MLME_QUEQUE_SIZE, sizeof ( MAC_tsMlmeVsDcfmInd ), (uint8*)asMacMlmeVsDcfmInd );
ZQ_vQueueCreate ( &zps_msgMcpsDcfm, MCPS_DCFM_QUEUE_SIZE, sizeof (MAC_tsMcpsVsCfmData), (uint8*)asMacMcpsDcfm);
ZQ_vQueueCreate ( &zps_msgMcpsDcfmInd, MCPS_QUEUE_SIZE, sizeof ( MAC_tsMcpsVsDcfmInd ), (uint8*)asMacMcpsDcfmInd );
ZQ_vQueueCreate ( &zps_TimeEvents, TIMER_QUEUE_SIZE, sizeof ( zps_tsTimeEvent ), (uint8*)asTimeEvent );
ZQ_vQueueCreate ( &APP_msgAppEvents, APP_QUEUE_SIZE, sizeof ( APP_tsEvent ), (uint8*)asAppMsg );
ZQ_vQueueCreate ( &APP_msgSerialTx, TX_QUEUE_SIZE, sizeof ( uint8 ), (uint8*)au8AtTxBuffer );
ZQ_vQueueCreate ( &APP_msgSerialRx, RX_QUEUE_SIZE, sizeof ( uint8 ), (uint8*)au8AtRxBuffer );
#ifdef CLD_GREENPOWER
ZQ_vQueueCreate(&APP_msgGPZCLTimerEvents, GP_TIMER_QUEUE_SIZE, sizeof(uint8), (uint8*)au8GPZCLEvent);
#endif
vZCL_RegisterHandleGeneralCmdCallBack (APP_vProfileWideCommandSupportedForCluster );
vZCL_RegisterCheckForManufCodeCallBack(APP_bZCL_IsManufacturerCodeSupported);
DBG_vPrintf(TRACE_APPSTART, "APP: Initialising resources complete\n");
}
#if (JENNIC_CHIP_FAMILY == JN516x)
/****************************************************************************
*
* NAME: vUnclaimedException
*
* DESCRIPTION:
* Initialises Zigbee stack, hardware and application.
*
* RETURNS:
* void
*
****************************************************************************/
void vUnclaimedException ( void )
{
register uint32 u32PICSR;
register uint32 u32PICMR;
asm volatile ("l.mfspr %0,r0,0x4800" :"=r"(u32PICMR) : );
asm volatile ("l.mfspr %0,r0,0x4802" :"=r"(u32PICSR) : );
vLog_Printf ( TRACE_APPSTART,LOG_EMERG, "Unclaimed interrupt : %x : %x\n", u32PICSR, u32PICSR );
vSL_LogFlush ( );
/* Log the exception */
vLog_Printf ( TRACE_EXC, LOG_CRIT, "\n\n\n%s EXCEPTION @ %08x (PICMR: %08x HP: %08x)", "UCMI",
u32PICMR, u32PICSR, ( ( uint32* ) &_heap_location) [ 0 ] );
vSL_LogFlush ( );
/* Software reset */
vAHI_SwReset ( );
}
void vStackOverflowHandler ( void )
{
vReportException ( "EXS" );
}
void vAlignmentErrorHandler ( void )
{
vReportException ( "EXA" );
}
void vBusErrorHandler ( void )
{
vReportException ( "EXB" );
}
void vIllegalInstructionHandler ( void )
{
vReportException ( "EXI" );
}
void vReportException ( char* sExStr )
{
register uint32 u32EPCR;
register uint32 u32EEAR;
volatile uint32* pu32Stack;
/* TODO - add reg dump too */
asm volatile ("l.mfspr %0,r0,0x0020" :"=r"(u32EPCR) : );
asm volatile ("l.mfspr %0,r0,0x0030" :"=r"(u32EEAR) : );
asm volatile ("l.or %0,r0,r1" :"=r"(pu32Stack) : );
vSL_LogFlush();
/* Log the exception */
vLog_Printf(TRACE_EXC, LOG_CRIT, "\n\n\n%s EXCEPTION @ %08x (EA: %08x SK: %08x HP: %08x)",
sExStr, u32EPCR, u32EEAR, pu32Stack, ( ( uint32* ) &_heap_location) [ 0 ] );
vSL_LogFlush ( );
vLog_Printf ( TRACE_EXC,LOG_CRIT, "Stack dump:\n" );
vSL_LogFlush ( );
#if (DEBUG_WDR == TRUE)
vAHI_WatchdogStop ( );
#endif
/* loop until we hit a 32k boundary. should be top of stack */
while ( ( uint32 ) pu32Stack & 0x7fff )
{
#if (DEBUG_WDR == TRUE)
volatile uint32 u32Delay;
#endif
vLog_Printf ( TRACE_EXC,LOG_CRIT, "% 8x : %08x\n", pu32Stack, *pu32Stack );
vSL_LogFlush ( );
pu32Stack++;
#if (DEBUG_WDR == TRUE)
for (u32Delay = 0; u32Delay < 100000; u32Delay++);
vAHI_DioSetOutput(LED_DIO_PINS, 0);
for (u32Delay = 0; u32Delay < 100000; u32Delay++);
vAHI_DioSetOutput(0, LED_DIO_PINS);
#endif
}
vSL_LogFlush ( );
#if (DEBUG_WDR == FALSE)
/* Software reset */
vAHI_SwReset ( );
#endif
#if (DEBUG_WDR == TRUE)
while(1)
{
volatile uint32 u32Delay;
for (u32Delay = 0; u32Delay < 100000; u32Delay++);
vAHI_DioSetOutput(LED_DIO_PINS, 0);
for (u32Delay = 0; u32Delay < 100000; u32Delay++);
vAHI_DioSetOutput(0, LED_DIO_PINS);
}
#endif
}
#endif
/****************************************************************************
*
* NAME: app_vFormatAndSendUpdateLists
*
* DESCRIPTION:
*
*
* RETURNS:
* void
*
****************************************************************************/
PUBLIC void app_vFormatAndSendUpdateLists ( void )
{
typedef struct
{
uint16 u16Clusterid;
uint16* au16Attibutes;
uint32 u32Asize;
uint8* au8command;
uint32 u32Csize;
} tsAttribCommand;
uint16 u16Length = 0;
uint8 u8Count = 0 ;
uint8 u8BufferLoop = 0;
uint8 au8LinkTxBuffer[256];
/*List of clusters per endpoint */
uint16 u16ClusterListHA [ ] = { 0x0000, 0x0001, 0x0003, 0x0004, 0x0005, 0x0006,
0x0008, 0x0019, 0x0101, 0x1000, 0x0300, 0x0201, 0x0204 };
uint16 u16ClusterListHA2 [ ] = { 0x0405, 0x0500, 0x0400, 0x0402, 0x0403, 0x0405, 0x0406,
0x0702, 0x0b03, 0x0b04 , 0x1000 };
/*list of attributes per cluster */
uint16 u16AttribBasic [ ] = { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,
0x0005, 0x0006, 0x0007, 0x4000,0xF000, 0xFF01, 0xFF02 };
uint16 u16AttribIdentify [ ] = { 0x000 };
uint16 u16AttribGroups [ ] = { 0x000 };
uint16 u16AttribScenes [ ] = { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005 };
uint16 u16AttribOnOff [ ] = { 0x0000, 0x4000, 0x4001, 0x4002 };
uint16 u16AttribLevel [ ] = { 0x0000, 0x0001, 0x0010, 0x0011 };
uint16 u16AttribColour [ ] = { 0x0000, 0x0001, 0x0002, 0x0007, 0x0008,
0x0010, 0x0011, 0x0012, 0x0013, 0x0015,
0x0016, 0x0017, 0x0019, 0x001A, 0x0020,
0x0021, 0x0022, 0x0024, 0x0025, 0x0026,
0x0028, 0x0029, 0x002A, 0x4000, 0x4001,
0x4002, 0x4003, 0x4004, 0x4006, 0x400A,
0x400B, 0x400C };
uint16 u16AttribThermostat [ ] = { 0x0000, 0x0003, 0x0004, 0x0011, 0x0012,
0x001B, 0x001C };
uint16 u16AttribHum [ ] = { 0x0000, 0x0001, 0x0002, 0x0003 };
uint16 u16AttribPower [ ] = { 0x0020, 0x0034 };
uint16 u16AttribIllumM [ ] = { 0x000, 0x0001, 0x0002, 0x0003, 0x0004 };
uint16 u16AttribIllumT [ ] = { 0x000, 0x0001, 0x0002 };
uint16 u16AttribSM [ ] = { 0x0000, 0x0300, 0x0301, 0x0302, 0x0306, 0x0400 };
/*list of commands per cluster */
uint8 u8CommandBasic [ ] = { 0 };
uint8 u8CommandIdentify [ ] = { 0, 1, 0x40 };
uint8 u8CommandGroups [ ] = { 0, 1, 2, 3, 4, 5 };
uint8 u8CommandScenes [ ] = { 0, 1, 2, 3, 4, 5, 6,
0x40, 0x41, 0x42 };
uint8 u8CommandsOnOff [ ] = { 0, 1, 2, 0x40, 0x41, 0x42 };
uint8 u8CommandsLevel [ ] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
uint8 u8CommandsColour [ ] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0xa, 0x40, 0x41, 0x42, 0x43, 0x44, 0x47,
0x4b, 0x4c, 0xfe, 0xff };
uint8 u8CommandThermostat [ ] = {0};
uint8 u8CommandHum [ ] = { 0 };
uint8 u8CommandPower [ ] = { 0 };
uint8 u8CommandIllumM [ ] = { 0 };
uint8 u8CommandIllumT [ ] = { 0 };
uint8 u8CommandSM [ ] = { 0 };
tsAttribCommand asAttribCommand[13] = { { 0x0000, u16AttribBasic, ( sizeof( u16AttribBasic ) / sizeof ( u16AttribBasic [ 0 ] ) ),
u8CommandBasic, ( sizeof ( u8CommandBasic ) / sizeof ( u8CommandBasic [ 0 ] ) )},
{ 0x0003, u16AttribIdentify, ( sizeof ( u16AttribIdentify ) / sizeof ( u16AttribIdentify [ 0 ] ) ),
u8CommandIdentify, ( sizeof ( u8CommandIdentify ) / sizeof ( u8CommandIdentify [ 0 ] ) ) },
{ 0x0004, u16AttribGroups, ( sizeof ( u16AttribGroups ) / sizeof ( u16AttribGroups [ 0 ] ) ),
u8CommandGroups, ( sizeof ( u8CommandGroups ) / sizeof ( u8CommandGroups [ 0 ] ) ) },
{ 0x0005, u16AttribScenes, ( sizeof ( u16AttribScenes ) / sizeof ( u16AttribScenes [ 0 ] ) ),
u8CommandScenes, ( sizeof( u8CommandScenes ) / sizeof ( u8CommandScenes [ 0 ] ) ) },
{ 0x0006, u16AttribOnOff, ( sizeof ( u16AttribOnOff ) / sizeof ( u16AttribOnOff [ 0 ] ) ),
u8CommandsOnOff, ( sizeof ( u8CommandsOnOff )/ sizeof ( u8CommandsOnOff [ 0 ] ) ) },
{ 0x0008, u16AttribLevel, ( sizeof ( u16AttribLevel ) / sizeof ( u16AttribLevel [ 0 ] ) ),
u8CommandsLevel, ( sizeof ( u8CommandsLevel ) / sizeof ( u8CommandsLevel [ 0 ] ) ) },
{ 0x0300, u16AttribColour, ( sizeof ( u16AttribColour ) / sizeof ( u16AttribColour [ 0 ] ) ),
u8CommandsColour, ( sizeof ( u8CommandsColour ) / sizeof ( u8CommandsColour [ 0 ] ) ) },
{ 0x0201, u16AttribThermostat, ( sizeof ( u16AttribThermostat ) / sizeof ( u16AttribThermostat [ 0 ] ) ),
u8CommandThermostat, ( sizeof ( u8CommandThermostat ) / sizeof ( u8CommandThermostat [ 0 ] ) ) },
{ 0x0405, u16AttribHum, ( sizeof ( u16AttribHum ) / sizeof ( u16AttribHum [ 0 ] ) ),
u8CommandHum, ( sizeof ( u8CommandHum ) / sizeof ( u8CommandHum [ 0 ] ) ) },
{ 0x0001, u16AttribPower, ( sizeof ( u16AttribPower ) /sizeof( u16AttribPower [ 0 ] ) ),
u8CommandPower, ( sizeof ( u8CommandPower ) /sizeof( u8CommandPower [ 0 ] ) ) },
{ 0x0400, u16AttribIllumM, ( sizeof ( u16AttribIllumM ) /sizeof( u16AttribIllumM [ 0 ] ) ),
u8CommandIllumM, ( sizeof ( u8CommandIllumM ) /sizeof( u8CommandIllumM [ 0 ] ) ) },
{ 0x0402, u16AttribIllumT, ( sizeof ( u16AttribIllumT ) /sizeof( u16AttribIllumT [ 0 ] ) ),
u8CommandIllumT, ( sizeof ( u8CommandIllumT ) /sizeof( u8CommandIllumT [ 0 ] ) ) },
{ 0x0702, u16AttribSM, ( sizeof ( u16AttribSM ) / sizeof ( u16AttribSM [ 0 ] ) ),
u8CommandSM, ( sizeof ( u8CommandSM ) / sizeof ( u8CommandSM [ 0 ] ) )} };
/* Cluster list endpoint HA */
ZNC_BUF_U8_UPD ( &au8LinkTxBuffer [ 0 ], ZIGBEENODECONTROLBRIDGE_ZLO_ENDPOINT, u16Length );
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], 0x0104, u16Length );
while ( u8BufferLoop < ( sizeof ( u16ClusterListHA ) / sizeof( u16ClusterListHA [ 0 ] ) ) )
{
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], u16ClusterListHA [ u8BufferLoop ], u16Length );
u8BufferLoop++;
}
vSL_WriteMessage ( E_SL_MSG_NODE_CLUSTER_LIST,
u16Length,
au8LinkTxBuffer,
0);
/* Cluster list endpoint HA */
u16Length = u8BufferLoop = 0;
ZNC_BUF_U8_UPD ( &au8LinkTxBuffer [ 0 ], ZIGBEENODECONTROLBRIDGE_ZLO_ENDPOINT, u16Length );
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], 0x0104, u16Length );
while ( u8BufferLoop < ( sizeof ( u16ClusterListHA2 ) / sizeof ( u16ClusterListHA2 [ 0 ] ) ) )
{
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], u16ClusterListHA2 [ u8BufferLoop ], u16Length );
u8BufferLoop++;
}
vSL_WriteMessage ( E_SL_MSG_NODE_CLUSTER_LIST,
u16Length,
au8LinkTxBuffer,
0);
/* Attribute list basic cluster HA EP*/
for ( u8Count = 0; u8Count < 13; u8Count++ )
{
u16Length = 0;
u8BufferLoop = 0;
ZNC_BUF_U8_UPD ( &au8LinkTxBuffer [ 0 ], ZIGBEENODECONTROLBRIDGE_ZLO_ENDPOINT, u16Length );
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], 0x0104, u16Length );
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], asAttribCommand[u8Count].u16Clusterid, u16Length );
while ( u8BufferLoop < asAttribCommand [ u8Count ].u32Asize )
{
ZNC_BUF_U16_UPD ( &au8LinkTxBuffer [ u16Length ], asAttribCommand[u8Count].au16Attibutes [ u8BufferLoop ], u16Length );
u8BufferLoop++;
}
vSL_WriteMessage ( E_SL_MSG_NODE_ATTRIBUTE_LIST,
u16Length,
au8LinkTxBuffer,
0);
u16Length = 0;