Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / examples / platforms / efr32mg13 / system.c
1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * @file
31  * @brief
32  *   This file includes the platform-specific initializers.
33  */
34
35 #include <openthread-core-config.h>
36 #include <openthread/config.h>
37
38 #include <assert.h>
39 #include <string.h>
40
41 #include "openthread-system.h"
42 #include <openthread/platform/uart.h>
43
44 #include "common/logging.hpp"
45
46 #include "bsp.h"
47 #include "em_chip.h"
48 #include "em_cmu.h"
49 #include "em_core.h"
50 #include "em_emu.h"
51 #include "em_system.h"
52 #include "hal-config.h"
53 #include "hal_common.h"
54 #include "rail.h"
55 #include "sl_mpu.h"
56 #include "sl_sleeptimer.h"
57
58 #include "openthread-core-efr32-config.h"
59 #include "platform-efr32.h"
60
61 #if (HAL_FEM_ENABLE)
62 #include "fem-control.h"
63 #endif
64
65 #define USE_EFR32_LOG (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
66
67 void halInitChipSpecific(void);
68
69 otInstance *sInstance;
70 static bool (*sCanSleepCallback)(void);
71
72 void otSysInit(int argc, char *argv[])
73 {
74     OT_UNUSED_VARIABLE(argc);
75     OT_UNUSED_VARIABLE(argv);
76     sl_status_t status;
77
78     __disable_irq();
79
80 #undef FIXED_EXCEPTION
81 #define FIXED_EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler)
82 #define EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler, priorityLevel, subpriority) \
83     NVIC_SetPriority(deviceIrqn, NVIC_EncodePriority(PRIGROUP_POSITION, priorityLevel, subpriority));
84 #include NVIC_CONFIG
85 #undef EXCEPTION
86
87     NVIC_SetPriorityGrouping(PRIGROUP_POSITION);
88     CHIP_Init();
89     halInitChipSpecific();
90     BSP_Init(BSP_INIT_BCC);
91
92     CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFRCO);
93     CMU_ClockEnable(cmuClock_CORELE, true);
94     CMU_ClockEnable(cmuClock_RTCC, true);
95     status = sl_sleeptimer_init();
96     assert(status == SL_STATUS_OK);
97
98 #if (HAL_FEM_ENABLE)
99     initFem();
100     wakeupFem();
101 #endif
102
103     __enable_irq();
104
105 #if USE_EFR32_LOG
106     efr32LogInit();
107 #endif
108     efr32RadioInit();
109     efr32AlarmInit();
110     efr32MiscInit();
111     efr32RandomInit();
112 }
113
114 bool otSysPseudoResetWasRequested(void)
115 {
116     return false;
117 }
118
119 void otSysDeinit(void)
120 {
121     efr32RadioDeinit();
122
123 #if USE_EFR32_LOG
124     efr32LogDeinit();
125 #endif
126 }
127
128 void efr32SetSleepCallback(bool (*aCallback)(void))
129 {
130     sCanSleepCallback = aCallback;
131 }
132
133 void efr32Sleep(void)
134 {
135     bool canDeepSleep      = false;
136     int  wakeupProcessTime = 1000;
137     CORE_DECLARE_IRQ_STATE;
138
139     if (RAIL_Sleep(wakeupProcessTime, &canDeepSleep) == RAIL_STATUS_NO_ERROR)
140     {
141         if (canDeepSleep)
142         {
143             CORE_ENTER_ATOMIC();
144             if (sCanSleepCallback != NULL && sCanSleepCallback())
145             {
146                 EMU_EnterEM2(true);
147             }
148             CORE_EXIT_ATOMIC();
149             // TODO OT will handle an interrupt here and it mustn't call any RAIL APIs
150
151             while (RAIL_Wake(0) != RAIL_STATUS_NO_ERROR)
152             {
153             }
154         }
155         else
156         {
157             CORE_ENTER_ATOMIC();
158             if (sCanSleepCallback != NULL && sCanSleepCallback())
159             {
160                 EMU_EnterEM1();
161             }
162             CORE_EXIT_ATOMIC();
163         }
164     }
165 }
166
167 void otSysProcessDrivers(otInstance *aInstance)
168 {
169     sInstance = aInstance;
170
171     // should sleep and wait for interrupts here
172
173     efr32UartProcess();
174     efr32RadioProcess(aInstance);
175     efr32AlarmProcess(aInstance);
176 }
177
178 __WEAK void otSysEventSignalPending(void)
179 {
180     // Intentionally empty
181 }