2b5087bb6df8d7a66acdded77b7a5fb71e40bd09
[platform/upstream/connectedhomeip.git] / third_party / openthread / repo / examples / platforms / efr32mg1 / 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 "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
71 void otSysInit(int argc, char *argv[])
72 {
73     OT_UNUSED_VARIABLE(argc);
74     OT_UNUSED_VARIABLE(argv);
75     sl_status_t status;
76
77     __disable_irq();
78
79 #undef FIXED_EXCEPTION
80 #define FIXED_EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler)
81 #define EXCEPTION(vectorNumber, functionName, deviceIrqn, deviceIrqHandler, priorityLevel, subpriority) \
82     NVIC_SetPriority(deviceIrqn, NVIC_EncodePriority(PRIGROUP_POSITION, priorityLevel, subpriority));
83 #include NVIC_CONFIG
84 #undef EXCEPTION
85
86     NVIC_SetPriorityGrouping(PRIGROUP_POSITION);
87     CHIP_Init();
88     halInitChipSpecific();
89     BSP_Init(BSP_INIT_BCC);
90
91     CMU_ClockSelectSet(cmuClock_LFE, cmuSelect_LFRCO);
92     CMU_ClockEnable(cmuClock_CORELE, true);
93     CMU_ClockEnable(cmuClock_RTCC, true);
94     status = sl_sleeptimer_init();
95     assert(status == SL_STATUS_OK);
96
97 #if (HAL_FEM_ENABLE)
98     initFem();
99     wakeupFem();
100 #endif
101
102     __enable_irq();
103
104 #if USE_EFR32_LOG
105     efr32LogInit();
106 #endif
107     efr32RadioInit();
108     efr32AlarmInit();
109     efr32MiscInit();
110     efr32RandomInit();
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))
128 {
129     sCanSleepCallback = aCallback;
130 }
131
132 void efr32Sleep(void)
133 {
134     bool canDeepSleep      = false;
135     int  wakeupProcessTime = 1000;
136     CORE_DECLARE_IRQ_STATE;
137
138     if (RAIL_Sleep(wakeupProcessTime, &canDeepSleep) == RAIL_STATUS_NO_ERROR)
139     {
140         if (canDeepSleep)
141         {
142             CORE_ENTER_ATOMIC();
143             if (sCanSleepCallback != NULL && sCanSleepCallback())
144             {
145                 EMU_EnterEM2(true);
146             }
147             CORE_EXIT_ATOMIC();
148             // TODO OT will handle an interrupt here and it mustn't call any RAIL APIs
149
150             while (RAIL_Wake(0) != RAIL_STATUS_NO_ERROR)
151             {
152             }
153         }
154         else
155         {
156             CORE_ENTER_ATOMIC();
157             if (sCanSleepCallback != NULL && sCanSleepCallback())
158             {
159                 EMU_EnterEM1();
160             }
161             CORE_EXIT_ATOMIC();
162         }
163     }
164 }
165
166 void otSysProcessDrivers(otInstance *aInstance)
167 {
168     sInstance = aInstance;
169
170     // should sleep and wait for interrupts here
171
172     efr32UartProcess();
173     efr32RadioProcess(aInstance);
174     efr32AlarmProcess(aInstance);
175 }
176
177 __WEAK void otSysEventSignalPending(void)
178 {
179     // Intentionally empty
180 }