3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
29 #include <mbedtls/threading.h>
31 #include <platform/CHIPDeviceLayer.h>
32 #include <platform/KeyValueStoreManager.h>
33 #include <support/CHIPMem.h>
34 #include <support/CHIPPlatformMemory.h>
38 #include "AppConfig.h"
39 #include "DataModelHandler.h"
41 #include "init_efrPlatform.h"
43 #ifdef HEAP_MONITORING
44 #include "MemMonitoring.h"
51 #if CHIP_ENABLE_OPENTHREAD
52 #include <mbedtls/platform.h>
53 #include <openthread/cli.h>
54 #include <openthread/dataset.h>
55 #include <openthread/error.h>
56 #include <openthread/heap.h>
57 #include <openthread/icmp6.h>
58 #include <openthread/instance.h>
59 #include <openthread/link.h>
60 #include <openthread/platform/openthread-system.h>
61 #include <openthread/tasklet.h>
62 #include <openthread/thread.h>
63 #endif // CHIP_ENABLE_OPENTHREAD
69 using namespace ::chip;
70 using namespace ::chip::Inet;
71 using namespace ::chip::DeviceLayer;
73 #define UNUSED_PARAMETER(a) (a = a)
75 volatile int apperror_cnt;
76 // ================================================================================
78 //=================================================================================
79 void appError(int err)
81 EFR32_LOG("!!!!!!!!!!!! App Critical Error: %d !!!!!!!!!!!", err);
82 portDISABLE_INTERRUPTS();
87 // ================================================================================
89 // ================================================================================
90 extern "C" void vApplicationIdleHook(void)
92 // FreeRTOS Idle callback
94 // Check CHIP Config nvm3 and repack flash if necessary.
95 Internal::EFR32Config::RepackNvm3Flash();
98 // ================================================================================
100 // ================================================================================
103 int ret = CHIP_ERROR_MAX;
106 mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
112 #ifdef HEAP_MONITORING
113 MemMonitoring::startHeapMonitoring();
116 // Initialize mbedtls threading support on EFR32
119 EFR32_LOG("==================================================");
120 EFR32_LOG("chip-efr32-lighting-example starting");
121 EFR32_LOG("==================================================");
123 EFR32_LOG("Init CHIP Stack");
125 // Init Chip memory management before the stack
126 chip::Platform::MemoryInit();
127 chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();
129 ret = PlatformMgr().InitChipStack();
130 if (ret != CHIP_NO_ERROR)
132 EFR32_LOG("PlatformMgr().InitChipStack() failed");
135 chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName("EFR32_LIGHT");
136 #if CHIP_ENABLE_OPENTHREAD
137 EFR32_LOG("Initializing OpenThread stack");
138 ret = ThreadStackMgr().InitThreadStack();
139 if (ret != CHIP_NO_ERROR)
141 EFR32_LOG("ThreadStackMgr().InitThreadStack() failed");
145 ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
146 if (ret != CHIP_NO_ERROR)
148 EFR32_LOG("ConnectivityMgr().SetThreadDeviceType() failed");
151 #endif // CHIP_ENABLE_OPENTHREAD
153 EFR32_LOG("Starting Platform Manager Event Loop");
154 ret = PlatformMgr().StartEventLoopTask();
155 if (ret != CHIP_NO_ERROR)
157 EFR32_LOG("PlatformMgr().StartEventLoopTask() failed");
161 #if CHIP_ENABLE_OPENTHREAD
162 EFR32_LOG("Starting OpenThread task");
164 // Start OpenThread task
165 ret = ThreadStackMgrImpl().StartThreadTask();
166 if (ret != CHIP_NO_ERROR)
168 EFR32_LOG("ThreadStackMgr().StartThreadTask() failed");
171 #endif // CHIP_ENABLE_OPENTHREAD
172 EFR32_LOG("Starting App Task");
173 ret = GetAppTask().StartAppTask();
174 if (ret != CHIP_NO_ERROR)
176 EFR32_LOG("GetAppTask().Init() failed");
180 EFR32_LOG("Starting FreeRTOS scheduler");
181 vTaskStartScheduler();
183 chip::Platform::MemoryShutdown();
185 // Should never get here.
186 EFR32_LOG("vTaskStartScheduler() failed");