32f9346741f06a9b48fa27bb65ef537d37648729
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / examples / platforms / efr32mg21 / system.c
1 /*
2  *  Copyright (c) 2019, 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 "platform-efr32.h"
59
60 #if (HAL_FEM_ENABLE)
61 #include "fem-control.h"
62 #endif
63
64 #define USE_EFR32_LOG (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_PLATFORM_DEFINED)
65
66 void halInitChipSpecific(void);
67
68 otInstance *sInstance;
69 static bool (*sCanSleepCallback)(void);
70 static void (*sDeviceOutOfSleepCb)(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 - 1, priorityLevel, subpriority));
84 #include NVIC_CONFIG
85 #undef EXCEPTION
86
87     NVIC_SetPriorityGrouping(PRIGROUP_POSITION - 1);
88     CHIP_Init();
89     halInitChipSpecific();
90     BSP_Init(BSP_INIT_BCC);
91
92     CMU_OscillatorEnable(cmuOsc_LFRCO, true, true);
93     CMU_ClockEnable(cmuClock_RTCC, true);
94
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 }
112
113 bool otSysPseudoResetWasRequested(void)
114 {
115     return false;
116 }
117
118 void otSysDeinit(void)
119 {
120     efr32RadioDeinit();
121
122 #if USE_EFR32_LOG
123     efr32LogDeinit();
124 #endif
125 }
126
127 void efr32SetSleepCallback(bool (*aCallback)(void), void (*aCallbackWake)(void))
128 {
129     sCanSleepCallback   = aCallback;
130     sDeviceOutOfSleepCb = aCallbackWake;
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             if (sDeviceOutOfSleepCb != NULL)
156             {
157                 sDeviceOutOfSleepCb();
158             }
159         }
160         else
161         {
162             CORE_ENTER_ATOMIC();
163             if (sCanSleepCallback != NULL && sCanSleepCallback())
164             {
165                 EMU_EnterEM1();
166             }
167             CORE_EXIT_ATOMIC();
168         }
169     }
170 }
171
172 void otSysProcessDrivers(otInstance *aInstance)
173 {
174     sInstance = aInstance;
175
176     // should sleep and wait for interrupts here
177
178     efr32UartProcess();
179     efr32RadioProcess(aInstance);
180     efr32AlarmProcess(aInstance);
181 }
182
183 __WEAK void otSysEventSignalPending(void)
184 {
185     // Intentionally empty
186 }